csopt386.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Jonas Maebe, member of the Free Pascal
  4. development team
  5. This unit contains the common subexpression elimination procedure.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. Unit CSOpt386;
  20. Interface
  21. Uses aasm;
  22. {Procedure CSOpt386(First, Last: Pai);}
  23. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  24. Implementation
  25. Uses
  26. {$ifdef replaceregdebug}cutils,{$endif}
  27. verbose, hcodegen, globals,cpubase,cpuasm,DAOpt386, tgeni386;
  28. {
  29. Function PaiInSequence(P: Pai; Const Seq: TContent): Boolean;
  30. Var P1: Pai;
  31. Counter: Byte;
  32. TmpResult: Boolean;
  33. Begin
  34. TmpResult := False;
  35. P1 := Seq.StartMod;
  36. Counter := 1;
  37. While Not(TmpResult) And
  38. (Counter <= Seq.NrOfMods) Do
  39. Begin
  40. If (P = P1) Then TmpResult := True;
  41. Inc(Counter);
  42. p1 := Pai(p1^.Next);
  43. End;
  44. PaiInSequence := TmpResult;
  45. End;
  46. }
  47. function modifiesMemLocation(p1: pai): boolean;
  48. var p: paicpu;
  49. opCount: byte;
  50. begin
  51. modifiesMemLocation := false;
  52. if p1^.typ <> ait_instruction then
  53. exit;
  54. p := paicpu(p1);
  55. for opCount := 1 to MaxCh do
  56. case InsProp[p^.opcode].Ch[opCount] of
  57. Ch_MOp1,CH_WOp1,CH_RWOp1:
  58. if p^.oper[0].typ = top_ref then
  59. begin
  60. modifiesMemLocation := true;
  61. exit
  62. end;
  63. Ch_MOp2,CH_WOp2,CH_RWOp2:
  64. if p^.oper[1].typ = top_ref then
  65. begin
  66. modifiesMemLocation := true;
  67. exit
  68. end;
  69. Ch_MOp3,CH_WOp3,CH_RWOp3:
  70. if p^.oper[2].typ = top_ref then
  71. begin
  72. modifiesMemLocation := true;
  73. exit
  74. end;
  75. Ch_WMemEDI:
  76. begin
  77. modifiesMemLocation := true;
  78. exit;
  79. end;
  80. end;
  81. end;
  82. function getPrevSequence(reg: tregister; current: pai; var prev: pai; var passedJump: boolean):
  83. tregister;
  84. function stillValid(p: pai): boolean;
  85. begin
  86. stillValid :=
  87. (p^.typ = ait_instruction) and
  88. (paicpu(p)^.opcode <> a_jmp) and
  89. (ppaiprop(p^.optinfo)^.regs[reg].state =
  90. ppaiprop(current^.optinfo)^.regs[reg].state) and
  91. { in case destroyreg is called with doIncState = false }
  92. (ppaiprop(p^.optinfo)^.regs[reg].typ =
  93. ppaiprop(current^.optinfo)^.regs[reg].typ);
  94. passedJump :=
  95. (p^.typ = ait_instruction) and
  96. (paicpu(p)^.is_jmp);
  97. end;
  98. function findChangedRegister(p: pai): tregister;
  99. var
  100. regCounter: tregister;
  101. begin
  102. for regCounter := R_EAX to R_EDI do
  103. with ppaiprop(p^.optinfo)^.regs[regCounter] do
  104. if ((startmod <>
  105. ppaiprop(current^.optinfo)^.regs[regCounter].startmod) or
  106. (nrOfMods <>
  107. ppaiprop(current^.optinfo)^.regs[regCounter].nrOfMods)) and
  108. (not ppaiprop(p^.optinfo)^.canBeRemoved) and
  109. (ppaiprop(p^.optinfo)^.regs[regCounter].typ in
  110. [con_ref,con_noRemoveRef]) then
  111. begin
  112. findChangedRegister := regCounter;
  113. exit;
  114. end;
  115. findChangedRegister := R_NO;
  116. end;
  117. var
  118. hp, prevFound: pai;
  119. tmpResult: tregister;
  120. begin
  121. getPrevSequence := R_NO;
  122. { no memory writes (could be refined further) }
  123. if modifiesMemLocation(current) or
  124. not getLastInstruction(current,hp) then
  125. exit;
  126. tmpResult := R_NO;
  127. while (tmpResult = R_NO) and
  128. stillValid(hp) do
  129. begin
  130. { in case getPreviousInstruction fails and sets hp to nil in the }
  131. { next iteration }
  132. prevFound := hp;
  133. tmpResult := findChangedRegister(hp);
  134. if modifiesMemLocation(hp) or
  135. { do not load the self pointer or a regvar before a (conditional) }
  136. { jump with a new value, since if the jump is taken, the old value }
  137. { is (probably) still necessary }
  138. (passedJump and not(reg in (usableregs+[R_EDI]))) or
  139. not getLastInstruction(hp,hp) then
  140. break;
  141. end;
  142. getPrevSequence := tmpResult;
  143. if tmpResult <> R_NO then
  144. prev := prevFound;
  145. end;
  146. {checks whether the current instruction sequence (starting with p) and the
  147. one between StartMod and EndMod of Reg are the same. If so, the number of
  148. instructions that match is stored in Found and true is returned, otherwise
  149. Found holds the number of instructions between StartMod and EndMod and false
  150. is returned}
  151. Function CheckSequence(p: Pai; var prev: pai; Reg: TRegister; Var Found: Longint;
  152. Var RegInfo: TRegInfo): Boolean;
  153. function getNextRegToTest(var orgP: pai; currentReg: tregister): tregister;
  154. const
  155. checkingPrevSequences: boolean = false;
  156. passedJump: boolean = false;
  157. begin
  158. if currentReg = R_NO then
  159. checkingPrevSequences := false;
  160. if not checkingPrevSequences then
  161. begin
  162. Repeat
  163. Inc(currentReg);
  164. Until (currentReg > R_EDI) or
  165. (ppaiprop(orgP^.optInfo)^.regs[currentReg].typ
  166. in [con_ref,con_noRemoveRef]);
  167. if currentReg > R_EDI then
  168. begin
  169. if not modifiesMemLocation(orgP) and
  170. (ppaiprop(orgP^.optinfo)^.regs[reg].rstate =
  171. ppaiprop(p^.optinfo)^.regs[reg].rstate) then
  172. begin
  173. checkingPrevSequences := true;
  174. passedJump := false;
  175. end
  176. else
  177. getNextRegToTest := R_NO;
  178. end
  179. else getNextRegToTest := currentReg;
  180. end;
  181. if checkingPrevSequences then
  182. getNextRegToTest := getPrevSequence(reg,orgP,orgP, passedJump);
  183. end;
  184. Var hp2, hp3{, EndMod},highPrev, orgPrev: Pai;
  185. {Cnt,} OldNrOfMods: Longint;
  186. startRegInfo, OrgRegInfo, HighRegInfo: TRegInfo;
  187. HighFound, OrgRegFound: Byte;
  188. RegCounter, regCounter2: TRegister;
  189. OrgRegResult: Boolean;
  190. TmpResult: Boolean;
  191. {TmpState: Byte;}
  192. Begin {CheckSequence}
  193. Reg := Reg32(Reg);
  194. TmpResult := False;
  195. FillChar(OrgRegInfo, SizeOf(OrgRegInfo), 0);
  196. FillChar(startRegInfo, sizeof(startRegInfo), 0);
  197. OrgRegFound := 0;
  198. HighFound := 0;
  199. OrgRegResult := False;
  200. with startRegInfo do
  201. begin
  202. newRegsEncountered := [procinfo^.FramePointer, stack_pointer];
  203. new2OldReg[procinfo^.FramePointer] := procinfo^.FramePointer;
  204. new2OldReg[stack_pointer] := stack_pointer;
  205. oldRegsEncountered := newRegsEncountered;
  206. end;
  207. GetLastInstruction(p, prev);
  208. regCounter := getNextRegToTest(prev,R_NO);
  209. While (RegCounter <> R_NO) Do
  210. Begin
  211. regInfo := startRegInfo;
  212. Found := 0;
  213. hp2 := PPaiProp(prev^.OptInfo)^.Regs[RegCounter].StartMod;
  214. If (prev <> PPaiProp(prev^.OptInfo)^.Regs[RegCounter].StartMod)
  215. Then OldNrOfMods := PPaiProp(prev^.OptInfo)^.Regs[RegCounter].NrOfMods
  216. Else OldNrOfMods := 1;
  217. hp3 := p;
  218. While (Found <> OldNrOfMods) And
  219. { old new }
  220. InstructionsEquivalent(hp2, hp3, RegInfo) Do
  221. Begin
  222. if (hp3^.typ = ait_instruction) and
  223. ((paicpu(hp3)^.opcode = A_MOV) or
  224. (paicpu(hp3)^.opcode = A_MOVZX) or
  225. (paicpu(hp3)^.opcode = A_MOVSX)) and
  226. (paicpu(hp3)^.oper[0].typ in
  227. [top_const,top_ref,top_symbol]) and
  228. (paicpu(hp3)^.oper[1].typ = top_reg) and
  229. not(regInRef(reg32(paicpu(hp3)^.oper[1].reg),
  230. paicpu(hp3)^.oper[0].ref^)) then
  231. regInfo.lastReload
  232. [reg32(paicpu(hp3)^.oper[1].reg)] := hp3;
  233. GetNextInstruction(hp2, hp2);
  234. GetNextInstruction(hp3, hp3);
  235. Inc(Found)
  236. End;
  237. for regCounter2 := R_EAX to R_EDX do
  238. if (regInfo.new2OldReg[regCounter2] <> R_NO) and
  239. (regCounter2 in PPaiProp(hp3^.optInfo)^.usedRegs) and
  240. not regLoadedWithNewValue(regCounter2,false,hp3) then
  241. include(regInfo.regsStillUsedAfterSeq,regCounter2);
  242. If (Found <> OldNrOfMods) or
  243. { the following is to avoid problems with rangecheck code (see testcse2) }
  244. (assigned(hp3) and
  245. ((reg in regInfo.regsLoadedForRef) and
  246. (reg in PPaiProp(hp3^.optInfo)^.usedRegs) and
  247. not regLoadedWithNewValue(reg,false,hp3))) then
  248. Begin
  249. TmpResult := False;
  250. If (found > 0) then
  251. {this is correct because we only need to turn off the CanBeRemoved flag
  252. when an instruction has already been processed by CheckSequence
  253. (otherwise CanBeRemoved can't be true and thus can't have to be turned off).
  254. If it has already been processed by CheckSequence and flagged to be
  255. removed, it means that it has been checked against a previous sequence
  256. and that it was equal (otherwise CheckSequence would have returned false
  257. and the instruction wouldn't have been removed). If this "If found > 0"
  258. check is left out, incorrect optimizations are performed.}
  259. Found := PPaiProp(Pai(p)^.OptInfo)^.Regs[Reg].NrOfMods
  260. End
  261. Else TmpResult := True;
  262. If TmpResult And
  263. (Found > HighFound)
  264. Then
  265. Begin
  266. highPrev := prev;
  267. HighFound := Found;
  268. HighRegInfo := RegInfo;
  269. End;
  270. If (RegCounter = Reg) Then
  271. Begin
  272. orgPrev := prev;
  273. OrgRegFound := Found;
  274. OrgRegResult := TmpResult;
  275. OrgRegInfo := RegInfo
  276. End;
  277. regCounter := getNextRegToTest(prev,regCounter);
  278. End;
  279. If (HighFound > 0) And
  280. (Not(OrgRegResult) Or
  281. (HighFound > OrgRegFound))
  282. Then
  283. Begin
  284. {$ifndef fpc}
  285. TmpResult := True;
  286. {$else fpc}
  287. CheckSequence := True;
  288. {$endif fpc}
  289. prev := highPrev;
  290. RegInfo := HighRegInfo;
  291. Found := HighFound
  292. End
  293. Else
  294. Begin
  295. {$ifndef fpc}
  296. TmpResult := OrgRegResult;
  297. {$else fpc}
  298. CheckSequence := OrgRegResult;
  299. {$endif fpc}
  300. prev := orgPrev;
  301. Found := OrgRegFound;
  302. RegInfo := OrgRegInfo;
  303. End;
  304. {$ifndef fpc}
  305. CheckSequence := TmpResult;
  306. {$endif fpc}
  307. End; {CheckSequence}
  308. Procedure SetAlignReg(p: Pai);
  309. Const alignSearch = 12;
  310. var regsUsable: TRegSet;
  311. prevInstrCount, nextInstrCount: Longint;
  312. prevState, nextWState,nextRState: Array[R_EAX..R_EDI] of byte;
  313. regCounter, lastRemoved: TRegister;
  314. prev, next: Pai;
  315. {$ifdef alignregdebug}
  316. temp: Pai;
  317. {$endif alignregdebug}
  318. begin
  319. regsUsable := [R_EAX,R_ECX,R_EDX,R_EBX,{R_ESP,R_EBP,}R_ESI,R_EDI];
  320. for regCounter := R_EAX to R_EDI do
  321. begin
  322. prevState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  323. nextWState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  324. nextRState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].rState;
  325. end;
  326. getLastInstruction(p,prev);
  327. getNextInstruction(p,next);
  328. lastRemoved := pai_align(p)^.reg;
  329. nextInstrCount := 0;
  330. prevInstrCount := 0;
  331. while ((assigned(prev) and
  332. assigned(prev^.optInfo) and
  333. (prevInstrCount < alignSearch)) or
  334. (assigned(next) and
  335. assigned(next^.optInfo) and
  336. (nextInstrCount < alignSearch))) And
  337. (regsUsable <> []) do
  338. begin
  339. {$ifdef alignregdebug}
  340. if assigned(prev) then
  341. begin
  342. temp := new(pai_asm_comment,init(strpnew('got here')));
  343. temp^.next := prev^.next;
  344. temp^.previous := prev;
  345. prev^.next := temp;
  346. if assigned(temp^.next) then
  347. temp^.next^.previous := temp;
  348. end;
  349. {$endif alignregdebug}
  350. if assigned(prev) and assigned(prev^.optinfo) and
  351. (prevInstrCount < alignSearch) then
  352. begin
  353. if (prev^.typ = ait_instruction) And
  354. (insProp[PaiCpu(prev)^.opcode].ch[1] <> Ch_ALL) and
  355. (PaiCpu(prev)^.opcode <> A_JMP) then
  356. begin
  357. inc(prevInstrCount);
  358. for regCounter := R_EAX to R_EDI do
  359. begin
  360. if (regCounter in regsUsable) And
  361. (PPaiProp(prev^.optInfo)^.Regs[regCounter].wState <>
  362. prevState[regCounter]) then
  363. begin
  364. lastRemoved := regCounter;
  365. exclude(regsUsable,regCounter);
  366. {$ifdef alignregdebug}
  367. temp := new(pai_asm_comment,init(strpnew(
  368. att_reg2str[regCounter]+' removed')));
  369. temp^.next := prev^.next;
  370. temp^.previous := prev;
  371. prev^.next := temp;
  372. if assigned(temp^.next) then
  373. temp^.next^.previous := temp;
  374. if regsUsable = [] then
  375. begin
  376. temp := new(pai_asm_comment,init(strpnew(
  377. 'regsUsable empty here')));
  378. temp^.next := prev^.next;
  379. temp^.previous := prev;
  380. prev^.next := temp;
  381. if assigned(temp^.next) then
  382. temp^.next^.previous := temp;
  383. end;
  384. {$endif alignregdebug}
  385. end;
  386. prevState[regCounter] :=
  387. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState;
  388. end;
  389. getLastInstruction(prev,prev);
  390. end
  391. else
  392. If GetLastInstruction(prev,prev) and
  393. assigned(prev^.optinfo) then
  394. for regCounter := R_EAX to R_EDI do
  395. prevState[regCounter] :=
  396. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState
  397. end;
  398. if assigned(next) and assigned(next^.optInfo) and
  399. (nextInstrCount < alignSearch) then
  400. begin
  401. if (next^.typ = ait_instruction) and
  402. (insProp[PaiCpu(next)^.opcode].ch[1] <> Ch_ALL) and
  403. (PaiCpu(next)^.opcode <> A_JMP) then
  404. begin
  405. inc(nextInstrCount);
  406. for regCounter := R_EAX to R_EDI do
  407. begin
  408. if (regCounter in regsUsable) And
  409. ((PPaiProp(next^.optInfo)^.Regs[regCounter].wState <>
  410. nextWState[regCounter]) or
  411. (PPaiProp(next^.optInfo)^.Regs[regCounter].rState <>
  412. nextRState[regCounter])) Then
  413. begin
  414. lastRemoved := regCounter;
  415. exclude(regsUsable,regCounter);
  416. {$ifdef alignregdebug}
  417. temp := new(pai_asm_comment,init(strpnew(
  418. att_reg2str[regCounter]+' removed')));
  419. temp^.next := next^.next;
  420. temp^.previous := next;
  421. next^.next := temp;
  422. if assigned(temp^.next) then
  423. temp^.next^.previous := temp;
  424. if regsUsable = [] then
  425. begin
  426. temp := new(pai_asm_comment,init(strpnew(
  427. 'regsUsable empty here')));
  428. temp^.next := next^.next;
  429. temp^.previous := next;
  430. next^.next := temp;
  431. if assigned(temp^.next) then
  432. temp^.next^.previous := temp;
  433. end;
  434. {$endif alignregdebug}
  435. end;
  436. nextWState[regCounter] :=
  437. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  438. nextRState[regCounter] :=
  439. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  440. end
  441. end
  442. else
  443. for regCounter := R_EAX to R_EDI do
  444. begin
  445. nextWState[regCounter] :=
  446. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  447. nextRState[regCounter] :=
  448. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  449. end;
  450. getNextInstruction(next,next);
  451. end;
  452. end;
  453. if regsUsable <> [] then
  454. for regCounter := R_EAX to R_EDI do
  455. if regCounter in regsUsable then
  456. begin
  457. lastRemoved := regCounter;
  458. break
  459. end;
  460. {$ifdef alignregdebug}
  461. next := new(pai_asm_comment,init(strpnew(att_reg2str[lastRemoved]+
  462. ' chosen as alignment register')));
  463. next^.next := p^.next;
  464. next^.previous := p;
  465. p^.next := next;
  466. if assigned(next^.next) then
  467. next^.next^.previous := next;
  468. {$endif alignregdebug}
  469. pai_align(p)^.reg := lastRemoved;
  470. End;
  471. Procedure RestoreRegContentsTo(reg: TRegister; const c: TContent; p, endP: pai);
  472. var
  473. {$ifdef replaceregdebug}
  474. hp: pai;
  475. l: longint;
  476. {$endif replaceregdebug}
  477. tmpState: byte;
  478. begin
  479. {$ifdef replaceregdebug}
  480. l := random(1000);
  481. hp := new(pai_asm_comment,init(strpnew(
  482. 'restored '+att_reg2str[reg]+' with data from here... '+tostr(l))));
  483. hp^.next := p;
  484. hp^.previous := p^.previous;
  485. p^.previous := hp;
  486. if assigned(hp^.previous) then
  487. hp^.previous^.next := hp;
  488. {$endif replaceregdebug}
  489. { PPaiProp(p^.optInfo)^.Regs[reg] := c;}
  490. While (p <> endP) Do
  491. Begin
  492. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  493. getNextInstruction(p,p);
  494. end;
  495. tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
  496. repeat
  497. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  498. until not getNextInstruction(p,p) or
  499. (PPaiProp(p^.optInfo)^.Regs[reg].wState <> tmpState);
  500. {$ifdef replaceregdebug}
  501. if assigned(p) then
  502. begin
  503. hp := new(pai_asm_comment,init(strpnew(
  504. 'restored '+att_reg2str[reg]+' till here... '+tostr(l))));
  505. hp^.next := p;
  506. hp^.previous := p^.previous;
  507. p^.previous := hp;
  508. if assigned(hp^.previous) then
  509. hp^.previous^.next := hp;
  510. end;
  511. {$endif replaceregdebug}
  512. end;
  513. function FindRegDealloc(reg: tregister; p: pai): boolean;
  514. { assumes reg is a 32bit register }
  515. var
  516. hp: pai;
  517. first: boolean;
  518. begin
  519. findregdealloc := false;
  520. first := true;
  521. while assigned(p^.previous) and
  522. ((Pai(p^.previous)^.typ in (skipinstr+[ait_align])) or
  523. ((Pai(p^.previous)^.typ = ait_label) and
  524. labelCanBeSkipped(pai_label(p^.previous)))) do
  525. begin
  526. p := pai(p^.previous);
  527. if (p^.typ = ait_regalloc) and
  528. (pairegalloc(p)^.reg = reg) then
  529. if not(pairegalloc(p)^.allocation) then
  530. if first then
  531. begin
  532. findregdealloc := true;
  533. break;
  534. end
  535. else
  536. begin
  537. findRegDealloc :=
  538. getNextInstruction(p,hp) and
  539. regLoadedWithNewValue(reg,false,hp);
  540. break
  541. end
  542. else
  543. first := false;
  544. end
  545. end;
  546. Procedure ClearRegContentsFrom(reg: TRegister; p, endP: pai);
  547. { first clears the contents of reg from p till endP. Then the contents are }
  548. { cleared until the first instruction that changes reg }
  549. var
  550. {$ifdef replaceregdebug}
  551. hp: pai;
  552. l: longint;
  553. {$endif replaceregdebug}
  554. oldStartmod: pai;
  555. begin
  556. {$ifdef replaceregdebug}
  557. l := random(1000);
  558. hp := new(pai_asm_comment,init(strpnew(
  559. 'cleared '+att_reg2str[reg]+' from here... '+tostr(l))));
  560. hp^.next := p;
  561. hp^.previous := p^.previous;
  562. p^.previous := hp;
  563. if assigned(hp^.previous) then
  564. hp^.previous^.next := hp;
  565. {$endif replaceregdebug}
  566. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  567. While (p <> endP) Do
  568. Begin
  569. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  570. getNextInstruction(p,p);
  571. end;
  572. oldStartmod := PPaiProp(p^.optInfo)^.Regs[reg].startmod;
  573. repeat
  574. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  575. until not getNextInstruction(p,p) or
  576. (PPaiProp(p^.optInfo)^.Regs[reg].startmod <> oldStartmod);
  577. {$ifdef replaceregdebug}
  578. if assigned(p) then
  579. begin
  580. hp := new(pai_asm_comment,init(strpnew(
  581. 'cleared '+att_reg2str[reg]+' till here... '+tostr(l))));
  582. hp^.next := p;
  583. hp^.previous := p^.previous;
  584. p^.previous := hp;
  585. if assigned(hp^.previous) then
  586. hp^.previous^.next := hp;
  587. end;
  588. {$endif replaceregdebug}
  589. end;
  590. function NoHardCodedRegs(p: paicpu; orgReg, newReg: tRegister): boolean;
  591. var chCount: byte;
  592. begin
  593. case p^.opcode of
  594. A_IMUL: noHardCodedRegs := p^.ops <> 1;
  595. A_SHL,A_SHR,A_SHLD,A_SHRD: noHardCodedRegs :=
  596. (p^.oper[0].typ <> top_reg) or
  597. ((orgReg <> R_ECX) and (newReg <> R_ECX));
  598. else
  599. begin
  600. NoHardCodedRegs := true;
  601. with InsProp[p^.opcode] do
  602. for chCount := 1 to MaxCh do
  603. if Ch[chCount] in ([Ch_REAX..Ch_MEDI,Ch_WMemEDI,Ch_All]-[Ch_RESP,Ch_WESP,Ch_RWESP]) then
  604. begin
  605. NoHardCodedRegs := false;
  606. break
  607. end;
  608. end;
  609. end;
  610. end;
  611. function ChangeReg(var Reg: TRegister; orgReg, newReg: TRegister): boolean;
  612. begin
  613. changeReg := true;
  614. if reg = newReg then
  615. reg := orgReg
  616. else if reg = regtoreg8(newReg) then
  617. reg := regtoreg8(orgReg)
  618. else if reg = regtoreg16(newReg) then
  619. reg := regtoreg16(orgReg)
  620. else changeReg := false;
  621. end;
  622. function changeOp(var o: toper; orgReg, newReg: tregister): boolean;
  623. begin
  624. case o.typ of
  625. top_reg: changeOp := changeReg(o.reg,orgReg,newReg);
  626. top_ref:
  627. begin
  628. changeOp :=
  629. changeReg(o.ref^.base,orgReg,newReg) or
  630. changeReg(o.ref^.index,orgReg,newReg);
  631. end;
  632. end;
  633. end;
  634. procedure updateStates(orgReg,newReg: tregister; hp: pai; writeStateToo: boolean);
  635. var
  636. prev: pai;
  637. newOrgRegRState, newOrgRegWState: byte;
  638. begin
  639. if getLastInstruction(hp,prev) then
  640. with ppaiprop(prev^.optinfo)^ do
  641. begin
  642. newOrgRegRState := regs[orgReg].rState +
  643. ppaiprop(hp^.optinfo)^.regs[newReg].rState - regs[newReg].rstate;
  644. if writeStateToo then
  645. newOrgRegWState := regs[orgReg].wState +
  646. ppaiprop(hp^.optinfo)^.regs[newReg].wState - regs[newReg].wstate;
  647. end
  648. else
  649. with ppaiprop(hp^.optinfo)^.regs[newReg] do
  650. begin
  651. newOrgRegRState := rState;
  652. if writeStateToo then
  653. newOrgRegWState := wState;
  654. end;
  655. with ppaiprop(hp^.optinfo)^.regs[orgReg] do
  656. begin
  657. rState := newOrgRegRState;
  658. if writeStateToo then
  659. wState := newOrgRegwState;
  660. end;
  661. end;
  662. function doReplaceReg(orgReg,newReg: tregister; hp: paicpu): boolean;
  663. var
  664. opCount: byte;
  665. tmpResult: boolean;
  666. begin
  667. for opCount := 0 to 2 do
  668. tmpResult :=
  669. changeOp(hp^.oper[opCount],orgReg,newReg) or tmpResult;
  670. doReplaceReg := tmpResult;
  671. end;
  672. function RegSizesOK(oldReg,newReg: TRegister; p: paicpu): boolean;
  673. { oldreg and newreg must be 32bit components }
  674. var opCount: byte;
  675. begin
  676. RegSizesOK := true;
  677. { if only one of them is a general purpose register ... }
  678. if (IsGP32reg(oldReg) xor IsGP32Reg(newReg)) then
  679. begin
  680. for opCount := 0 to 2 do
  681. if (p^.oper[opCount].typ = top_reg) and
  682. (p^.oper[opCount].reg in [R_AL..R_DH]) then
  683. begin
  684. RegSizesOK := false;
  685. break
  686. end
  687. end;
  688. end;
  689. function doReplaceReadReg(orgReg,newReg: tregister; p: paicpu): boolean;
  690. var opCount: byte;
  691. begin
  692. doReplaceReadReg := false;
  693. { handle special case }
  694. case p^.opcode of
  695. A_IMUL:
  696. begin
  697. case p^.ops of
  698. 1: internalerror(1301001);
  699. 2,3:
  700. begin
  701. if changeOp(p^.oper[0],orgReg,newReg) then
  702. begin
  703. { updateStates(orgReg,newReg,p,false);}
  704. doReplaceReadReg := true;
  705. end;
  706. if p^.ops = 3 then
  707. if changeOp(p^.oper[1],orgReg,newReg) then
  708. begin
  709. { updateStates(orgReg,newReg,p,false);}
  710. doReplaceReadReg := true;
  711. end;
  712. end;
  713. end;
  714. end;
  715. A_DIV,A_IDIV,A_MUL: internalerror(1301002);
  716. else
  717. begin
  718. for opCount := 0 to 2 do
  719. if p^.oper[opCount].typ = top_ref then
  720. if changeOp(p^.oper[opCount],orgReg,newReg) then
  721. begin
  722. { updateStates(orgReg,newReg,p,false);}
  723. doReplaceReadReg := true;
  724. end;
  725. for opCount := 1 to MaxCh do
  726. case InsProp[p^.opcode].Ch[opCount] of
  727. Ch_ROp1:
  728. if p^.oper[0].typ = top_reg then
  729. if changeReg(p^.oper[0].reg,orgReg,newReg) then
  730. begin
  731. { updateStates(orgReg,newReg,p,false);}
  732. doReplaceReadReg := true;
  733. end;
  734. Ch_ROp2:
  735. if p^.oper[1].typ = top_reg then
  736. if changeReg(p^.oper[1].reg,orgReg,newReg) then
  737. begin
  738. { updateStates(orgReg,newReg,p,false);}
  739. doReplaceReadReg := true;
  740. end;
  741. Ch_ROp3:
  742. if p^.oper[2].typ = top_reg then
  743. if changeReg(p^.oper[2].reg,orgReg,newReg) then
  744. begin
  745. { updateStates(orgReg,newReg,p,false);}
  746. doReplaceReadReg := true;
  747. end;
  748. end;
  749. end;
  750. end;
  751. end;
  752. procedure updateState(reg: tregister; p: pai);
  753. { this procedure updates the read and write states of the instructions }
  754. { coming after p. It's called when the read/write state of p has been }
  755. { changed and this change has to be propagated to the following }
  756. { instructions as well }
  757. var
  758. newRState, newWState: byte;
  759. prevRState, prevWState: byte;
  760. doRState, doWState: boolean;
  761. begin
  762. { get the new read/write states from p }
  763. with ppaiprop(p^.optinfo)^.regs[reg] do
  764. begin
  765. newRState := rState;
  766. newWState := wState;
  767. end;
  768. if not GetNextInstruction(p,p) then
  769. exit;
  770. { get the old read/write states from the next instruction, to know }
  771. { when we can stop updating }
  772. with ppaiprop(p^.optinfo)^.regs[reg] do
  773. begin
  774. prevRState := rState;
  775. prevWState := wState;
  776. end;
  777. { adjust the states if this next instruction reads/writes the register }
  778. if regReadByInstruction(reg,p) then
  779. incState(newRState,1);
  780. if regModifiedByInstruction(reg,p) then
  781. incState(newWState,1);
  782. { do we still have to update the read and/or write states? }
  783. doRState := true;
  784. doWState := true;
  785. repeat
  786. { update the states }
  787. with ppaiprop(p^.optinfo)^.regs[reg] do
  788. begin
  789. if doRState then
  790. rState := newRState;
  791. if doWState then
  792. wState := newWState;
  793. end;
  794. if not getNextInstruction(p,p) then
  795. break;
  796. with ppaiprop(p^.optinfo)^.regs[reg] do
  797. begin
  798. { stop updating the read state if it changes }
  799. doRState :=
  800. doRState and (rState = prevRState);
  801. { if, by accident, this changed state is the same as the one }
  802. { we've been using, change it to a value that's definitely }
  803. { different from the previous and next state }
  804. if not doRState and
  805. (rState = newRState) then
  806. begin
  807. incState(newRState,1);
  808. prevRState := rState;
  809. doRState := true;
  810. end;
  811. { ditto for the write state }
  812. doWState :=
  813. doWState and (WState = prevWState);
  814. if not doWState and
  815. (wState = newWState) then
  816. begin
  817. incState(newWState,1);
  818. prevWState := wState;
  819. doWState := true;
  820. end;
  821. end;
  822. { stop when we don't have to update either state anymore }
  823. until not(doRState or doWState);
  824. end;
  825. function ReplaceReg(asmL: PaasmOutput; orgReg, newReg: TRegister; p: pai;
  826. const c: TContent; orgRegCanBeModified: Boolean;
  827. var returnEndP: pai): Boolean;
  828. { Tries to replace orgreg with newreg in all instructions coming after p }
  829. { until orgreg gets loaded with a new value. Returns true if successful, }
  830. { false otherwise. If successful, the contents of newReg are set to c, }
  831. { which should hold the contents of newReg before the current sequence }
  832. { started }
  833. { if the function returns true, returnEndP holds the last instruction }
  834. { where newReg was replaced by orgReg }
  835. var endP, hp: Pai;
  836. removeLast, sequenceEnd, tmpResult, newRegModified, orgRegRead,
  837. stateChanged, readStateChanged: Boolean;
  838. function storeBack(p1: pai): boolean;
  839. { returns true if p1 contains an instruction that stores the contents }
  840. { of newReg back to orgReg }
  841. begin
  842. storeBack :=
  843. (p1^.typ = ait_instruction) and
  844. (paicpu(p1)^.opcode = A_MOV) and
  845. (paicpu(p1)^.oper[0].typ = top_reg) and
  846. (paicpu(p1)^.oper[0].reg = newReg) and
  847. (paicpu(p1)^.oper[1].typ = top_reg) and
  848. (paicpu(p1)^.oper[1].reg = orgReg);
  849. end;
  850. begin
  851. ReplaceReg := false;
  852. tmpResult := true;
  853. sequenceEnd := false;
  854. newRegModified := false;
  855. orgRegRead := false;
  856. removeLast := false;
  857. endP := p;
  858. while tmpResult and not sequenceEnd do
  859. begin
  860. tmpResult :=
  861. getNextInstruction(endP,endP) and
  862. (endP^.typ = ait_instruction);
  863. if tmpresult and not assigned(endP^.optInfo) then
  864. begin
  865. { hp := new(pai_asm_comment,init(strpnew('next no optinfo')));
  866. hp^.next := endp;
  867. hp^.previous := endp^.previous;
  868. endp^.previous := hp;
  869. if assigned(hp^.previous) then
  870. hp^.previous^.next := hp;}
  871. exit;
  872. end;
  873. If tmpResult and
  874. { don't take into account instructions that will be removed }
  875. Not (PPaiProp(endP^.optInfo)^.canBeRemoved) then
  876. begin
  877. { if the newReg gets stored back to the oldReg, we can change }
  878. { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
  879. { %oldReg" to "<operations on %oldReg>" }
  880. removeLast := storeBack(endP);
  881. sequenceEnd :=
  882. { no support for (i)div, mul and imul with hardcoded operands }
  883. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  884. { if newReg gets loaded with a new value, we can stop }
  885. { replacing newReg with oldReg here (possibly keeping }
  886. { the original contents of oldReg so we still know them }
  887. { afterwards) }
  888. RegLoadedWithNewValue(newReg,true,paicpu(endP)) or
  889. { we can also stop if we reached the end of the use of }
  890. { newReg's current contents }
  891. (GetNextInstruction(endp,hp) and
  892. FindRegDealloc(newReg,hp)));
  893. { to be able to remove the first and last instruction of }
  894. { movl %reg1, %reg2 }
  895. { <operations on %reg2> (replacing reg2 with reg1 here) }
  896. { movl %reg2, %reg1 }
  897. { %reg2 must not be use afterwards (it can be as the }
  898. { result of a peepholeoptimization) }
  899. removeLast := removeLast and sequenceEnd;
  900. newRegModified :=
  901. newRegModified or
  902. (not(regLoadedWithNewValue(newReg,true,paicpu(endP))) and
  903. RegModifiedByInstruction(newReg,endP));
  904. orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
  905. sequenceEnd := SequenceEnd and
  906. (removeLast or
  907. { since newReg will be replaced by orgReg, we can't allow that newReg }
  908. { gets modified if orgReg is still read afterwards (since after }
  909. { replacing, this would mean that orgReg first gets modified and then }
  910. { gets read in the assumption it still contains the unmodified value) }
  911. not(newRegModified and orgRegRead)) (* and
  912. { since newReg will be replaced by orgReg, we can't allow that newReg }
  913. { gets modified if orgRegCanBeModified = false }
  914. { this now gets checked after the loop (JM) }
  915. (orgRegCanBeModified or not(newRegModified)) *);
  916. tmpResult :=
  917. not(removeLast) and
  918. not(newRegModified and orgRegRead) and
  919. (* (orgRegCanBeModified or not(newRegModified)) and *)
  920. (endP^.typ = ait_instruction) and
  921. not(paicpu(endP)^.is_jmp) and
  922. NoHardCodedRegs(paicpu(endP),orgReg,newReg) and
  923. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  924. not RegModifiedByInstruction(orgReg,endP);
  925. end;
  926. end;
  927. sequenceEnd := sequenceEnd and
  928. (removeLast or
  929. (orgRegCanBeModified or not(newRegModified))) and
  930. (not(assigned(endp)) or
  931. not(endp^.typ = ait_instruction) or
  932. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  933. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  934. not(newRegModified and
  935. (orgReg in PPaiProp(endP^.optInfo)^.usedRegs) and
  936. not(RegLoadedWithNewValue(orgReg,true,paicpu(endP))))));
  937. if SequenceEnd then
  938. begin
  939. {$ifdef replaceregdebug}
  940. hp := new(pai_asm_comment,init(strpnew(
  941. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  942. ' from here...')));
  943. hp^.next := p;
  944. hp^.previous := p^.previous;
  945. p^.previous := hp;
  946. if assigned(hp^.previous) then
  947. hp^.previous^.next := hp;
  948. hp := new(pai_asm_comment,init(strpnew(
  949. 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  950. ' till here')));
  951. hp^.next := endp^.next;
  952. hp^.previous := endp;
  953. endp^.next := hp;
  954. if assigned(hp^.next) then
  955. hp^.next^.previous := hp;
  956. {$endif replaceregdebug}
  957. replaceReg := true;
  958. returnEndP := endP;
  959. getNextInstruction(p,hp);
  960. stateChanged := false;
  961. while hp <> endP do
  962. begin
  963. if {not(PPaiProp(hp^.optInfo)^.canBeRemoved) and }
  964. (hp^.typ = ait_instruction) then
  965. stateChanged :=
  966. doReplaceReg(orgReg,newReg,paicpu(hp)) or stateChanged;
  967. if stateChanged then
  968. updateStates(orgReg,newReg,hp,true);
  969. getNextInstruction(hp,hp)
  970. end;
  971. if assigned(endp) and (endp^.typ = ait_instruction) then
  972. readStateChanged :=
  973. DoReplaceReadReg(orgReg,newReg,paicpu(endP));
  974. if stateChanged or readStateChanged then
  975. updateStates(orgReg,newReg,endP,stateChanged);
  976. if stateChanged or readStateChanged then
  977. updateState(orgReg,endP);
  978. { the replacing stops either at the moment that }
  979. { a) the newreg gets loaded with a new value (one not depending on the }
  980. { current value of newreg) }
  981. { b) newreg is completely replaced in this sequence and it's current value }
  982. { isn't used anymore }
  983. { In case b, the newreg was completely replaced by oldreg, so it's contents }
  984. { are unchanged compared the start of this sequence, so restore them }
  985. If removeLast or
  986. RegLoadedWithNewValue(newReg,true,endP) then
  987. GetLastInstruction(endP,hp)
  988. else hp := endP;
  989. if removeLast or
  990. (p <> endp) or
  991. not RegLoadedWithNewValue(newReg,true,endP) then
  992. RestoreRegContentsTo(newReg,c,p,hp);
  993. { In both case a and b, it is possible that the new register was modified }
  994. { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
  995. { oldreg's contents have been changed. To take this into account, we simply }
  996. { set the contents of orgreg to "unknown" after this sequence }
  997. if newRegModified then
  998. ClearRegContentsFrom(orgReg,p,hp);
  999. if removeLast then
  1000. ppaiprop(endP^.optinfo)^.canBeRemoved := true;
  1001. allocRegBetween(asml,orgReg,p,endP);
  1002. end
  1003. {$ifdef replaceregdebug}
  1004. else
  1005. begin
  1006. hp := new(pai_asm_comment,init(strpnew(
  1007. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  1008. ' from here...')));
  1009. hp^.previous := p^.previous;
  1010. hp^.next := p;
  1011. p^.previous := hp;
  1012. if assigned(hp^.previous) then
  1013. hp^.previous^.next := hp;
  1014. hp := new(pai_asm_comment,init(strpnew(
  1015. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  1016. ' failed here')));
  1017. hp^.next := endp^.next;
  1018. hp^.previous := endp;
  1019. endp^.next := hp;
  1020. if assigned(hp^.next) then
  1021. hp^.next^.previous := hp;
  1022. end;
  1023. {$endif replaceregdebug}
  1024. End;
  1025. Function FindRegWithConst(p: Pai; size: topsize; l: longint; Var Res: TRegister): Boolean;
  1026. {Finds a register which contains the constant l}
  1027. Var Counter: TRegister;
  1028. {$ifdef testing}
  1029. hp: pai;
  1030. {$endif testing}
  1031. tmpresult: boolean;
  1032. Begin
  1033. Counter := R_NO;
  1034. repeat
  1035. inc(counter);
  1036. tmpresult := (ppaiprop(p^.optInfo)^.regs[counter].typ in
  1037. [con_const,con_noRemoveConst]) and
  1038. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.opsize = size) and
  1039. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].typ = top_const) and
  1040. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].val = l);
  1041. {$ifdef testing}
  1042. if (ppaiprop(p^.optInfo)^.regs[counter].typ in [con_const,con_noRemoveConst]) then
  1043. begin
  1044. hp := new(pai_asm_comment,init(strpnew(
  1045. 'checking const load of '+tostr(l)+' here...')));
  1046. hp^.next := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod;
  1047. hp^.previous := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous;
  1048. PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
  1049. if assigned(hp^.previous) then
  1050. hp^.previous^.next := hp;
  1051. end;
  1052. {$endif testing}
  1053. until tmpresult or (Counter = R_EDI);
  1054. res := counter;
  1055. FindRegWithConst := tmpResult;
  1056. End;
  1057. procedure removePrevNotUsedLoad(p: pai; reg: tRegister; check: boolean);
  1058. { If check = true, it means the procedure has to check whether it isn't }
  1059. { possible that the contents are still used after p (used when removing }
  1060. { instructions because of a "call"), otherwise this is not necessary }
  1061. { (e.g. when you have a "mov 8(%ebp),%eax", you can be sure the previous }
  1062. { value of %eax isn't used anymore later on) }
  1063. var
  1064. hp1: pai;
  1065. begin
  1066. if getLastInstruction(p,hp1) then
  1067. with ppaiprop(hp1^.optInfo)^.regs[reg] do
  1068. if (typ in [con_ref,con_invalid]) and
  1069. (nrOfMods = 1) and
  1070. (rState = ppaiprop(startmod^.optInfo)^.regs[reg].rState) and
  1071. (not(check) or
  1072. (not(regInInstruction(reg,p)) and
  1073. (not(reg in usableregs) and
  1074. (startmod^.typ = ait_instruction) and
  1075. ((paicpu(startmod)^.opcode = A_MOV) or
  1076. (paicpu(startmod)^.opcode = A_MOVZX) or
  1077. (paicpu(startmod)^.opcode = A_MOVSX)) and
  1078. (paicpu(startmod)^.oper[0].typ = top_ref) and
  1079. (paicpu(startmod)^.oper[0].ref^.base = stack_pointer)) or
  1080. not(reg in ppaiprop(hp1^.optInfo)^.usedRegs) or
  1081. findRegDealloc(reg,p))) then
  1082. ppaiprop(startMod^.optInfo)^.canBeRemoved := true;
  1083. end;
  1084. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  1085. {marks the instructions that can be removed by RemoveInstructs. They're not
  1086. removed immediately because sometimes an instruction needs to be checked in
  1087. two different sequences}
  1088. var cnt, cnt2, cnt3: longint;
  1089. p, hp1, hp2, prevSeq, prevSeq_next: Pai;
  1090. hp3, hp4: pai;
  1091. hp5 : pai;
  1092. RegInfo: TRegInfo;
  1093. RegCounter: TRegister;
  1094. Begin
  1095. p := First;
  1096. SkipHead(p);
  1097. First := p;
  1098. While (p <> Last) Do
  1099. Begin
  1100. Case p^.typ Of
  1101. ait_align:
  1102. if not(pai_align(p)^.use_op) then
  1103. SetAlignReg(p);
  1104. ait_instruction:
  1105. Begin
  1106. Case Paicpu(p)^.opcode Of
  1107. A_CALL:
  1108. for regCounter := R_EAX to R_EBX do
  1109. removePrevNotUsedLoad(p,regCounter,true);
  1110. A_CLD: If GetLastInstruction(p, hp1) And
  1111. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  1112. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1113. A_MOV, A_MOVZX, A_MOVSX:
  1114. Begin
  1115. Case Paicpu(p)^.oper[0].typ Of
  1116. Top_Ref:
  1117. Begin {destination is always a register in this case}
  1118. With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  1119. Begin
  1120. If (p = StartMod) And
  1121. GetLastInstruction (p, hp1) And
  1122. (hp1^.typ <> ait_marker) Then
  1123. {so we don't try to check a sequence when p is the first instruction of the block}
  1124. begin
  1125. {$ifdef csdebug}
  1126. hp5 := new(pai_asm_comment,init(strpnew(
  1127. 'cse checking '+att_reg2str[Reg32(Paicpu(p)^.oper[1].reg)])));
  1128. insertLLItem(asml,p,p^.next,hp5);
  1129. {$endif csdebug}
  1130. If CheckSequence(p,prevSeq,Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
  1131. (Cnt > 0) Then
  1132. Begin
  1133. hp1 := nil;
  1134. { although it's perfectly ok to remove an instruction which doesn't contain }
  1135. { the register that we've just checked (CheckSequence takes care of that), }
  1136. { the sequence containing this other register should also be completely }
  1137. { checked and removed, otherwise we may get situations like this: }
  1138. { }
  1139. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  1140. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  1141. { movl 8(%edx), %edx movl 8(%edx), %edx }
  1142. { movl (%eax), eax movl (%eax), eax }
  1143. { cmpl %eax, %edx cmpl %eax, %edx }
  1144. { jnz l123 getting converted to jnz l123 }
  1145. { movl 12(%ebp), %edx movl 4(%eax), eax }
  1146. { movl 16(%ebp), %eax }
  1147. { movl 8(%edx), %edx }
  1148. { movl 4(%eax), eax }
  1149. hp2 := p;
  1150. Cnt2 := 1;
  1151. While Cnt2 <= Cnt Do
  1152. Begin
  1153. If Not(RegInInstruction(Paicpu(hp2)^.oper[1].reg, p)) then
  1154. begin
  1155. if ((p^.typ = ait_instruction) And
  1156. ((paicpu(p)^.OpCode = A_MOV) or
  1157. (paicpu(p)^.opcode = A_MOVZX) or
  1158. (paicpu(p)^.opcode = A_MOVSX)) And
  1159. (paicpu(p)^.Oper[0].typ in
  1160. [top_const,top_ref,top_symbol])) and
  1161. (paicpu(p)^.oper[1].typ = top_reg) then
  1162. begin
  1163. regCounter := reg32(paicpu(p)^.oper[1].reg);
  1164. if (regCounter in reginfo.regsStillUsedAfterSeq) then
  1165. begin
  1166. if (hp1 = nil) then
  1167. hp1 := reginfo.lastReload[regCounter];
  1168. end
  1169. {$ifndef noremove}
  1170. else
  1171. begin
  1172. hp5 := p;
  1173. for cnt3 := ppaiprop(p^.optinfo)^.regs[regCounter].nrofmods downto 1 do
  1174. begin
  1175. if regModifiedByInstruction(regCounter,hp5) then
  1176. PPaiProp(hp5^.OptInfo)^.CanBeRemoved := True;
  1177. getNextInstruction(hp5,hp5);
  1178. end;
  1179. end
  1180. {$endif noremove}
  1181. end
  1182. end
  1183. {$ifndef noremove}
  1184. else
  1185. PPaiProp(p^.OptInfo)^.CanBeRemoved := True
  1186. {$endif noremove}
  1187. ; Inc(Cnt2);
  1188. GetNextInstruction(p, p);
  1189. End;
  1190. {hp4 is used to get the contents of the registers before the sequence}
  1191. GetLastInstruction(hp2, hp4);
  1192. getNextInstruction(prevSeq,prevSeq_next);
  1193. {$IfDef CSDebug}
  1194. For RegCounter := R_EAX To R_EDI Do
  1195. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  1196. Begin
  1197. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  1198. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  1199. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  1200. End;
  1201. {$EndIf CSDebug}
  1202. { If some registers were different in the old and the new sequence, move }
  1203. { the contents of those old registers to the new ones }
  1204. For RegCounter := R_EAX To R_EDI Do
  1205. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  1206. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  1207. Begin
  1208. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  1209. PPaiProp(prevSeq^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,prevSeq_next);
  1210. if hp4 <> prevSeq then
  1211. begin
  1212. if assigned(reginfo.lastReload[regCounter]) then
  1213. getLastInstruction(reginfo.lastReload[regCounter],hp3)
  1214. else hp3 := hp4;
  1215. if prevSeq <> hp3 then
  1216. clearRegContentsFrom(regCounter,prevSeq_next,
  1217. hp3);
  1218. allocRegBetween(asmL,regCounter,prevSeq,hp3);
  1219. end;
  1220. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  1221. {old reg new reg}
  1222. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  1223. Begin
  1224. getLastInstruction(p,hp3);
  1225. If (hp4 <> prevSeq) or
  1226. not(regCounter in usableRegs + [R_EDI,R_ESI]) or
  1227. not ReplaceReg(asmL,RegInfo.New2OldReg[RegCounter],
  1228. regCounter,hp3,
  1229. PPaiProp(PrevSeq^.optInfo)^.Regs[regCounter],true,hp5) then
  1230. begin
  1231. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  1232. InsertLLItem(AsmL, prevSeq, Pai(prevSeq^.next), hp3);
  1233. hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
  1234. {old reg new reg}
  1235. RegInfo.New2OldReg[RegCounter], RegCounter));
  1236. InsertLLItem(AsmL, prevSeq, Pai(prevSeq^.next), hp3);
  1237. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  1238. InsertLLItem(AsmL, prevSeq, Pai(prevSeq^.next), hp3);
  1239. { adjusts states in previous instruction so that it will }
  1240. { definitely be different from the previous or next state }
  1241. incstate(ppaiprop(prevSeq_next^.optinfo)^.
  1242. regs[RegInfo.New2OldReg[RegCounter]].rstate,20);
  1243. incstate(ppaiprop(prevSeq_next^.optinfo)^.
  1244. regs[regCounter].wstate,20);
  1245. updateState(RegInfo.New2OldReg[RegCounter],
  1246. prevSeq_next);
  1247. end
  1248. End
  1249. Else
  1250. { imagine the following code: }
  1251. { normal wrong optimized }
  1252. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  1253. { movl (%eax), %eax movl (%eax), %eax }
  1254. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  1255. { jne l1 jne l1 }
  1256. { movl 8(%ebp), %eax }
  1257. { movl (%eax), %edi movl %eax, %edi }
  1258. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  1259. { movl 8(%ebp), %eax }
  1260. { pushl 70(%eax) pushl 70(%eax) }
  1261. { }
  1262. { The error is that at the moment that the last instruction is executed, }
  1263. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  1264. { registers that are completely removed from a sequence (= registers in }
  1265. { RegLoadedForRef, have to be changed to their contents from before the }
  1266. { sequence. }
  1267. If RegCounter in RegInfo.RegsLoadedForRef Then
  1268. Begin
  1269. hp3 := hp2;
  1270. { cnt still holds the number of instructions }
  1271. { of the sequence, so go to the end of it }
  1272. for cnt2 := 1 to pred(cnt) Do
  1273. getNextInstruction(hp3,hp3);
  1274. { hp4 = instruction prior to start of sequence }
  1275. restoreRegContentsTo(regCounter,
  1276. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter],
  1277. hp2,hp3);
  1278. End;
  1279. End;
  1280. If hp1 <> nil Then
  1281. p := hp1;
  1282. Continue;
  1283. End
  1284. Else
  1285. If (PPaiProp(p^.OptInfo)^.
  1286. regs[reg32(paicpu(p)^.oper[1].reg)].typ
  1287. in [con_ref,con_noRemoveRef]) and
  1288. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  1289. if (cnt > 0) then
  1290. begin
  1291. hp2 := p;
  1292. Cnt2 := 1;
  1293. While Cnt2 <= Cnt Do
  1294. Begin
  1295. If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Then
  1296. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  1297. Inc(Cnt2);
  1298. GetNextInstruction(p, p);
  1299. End;
  1300. Continue;
  1301. End
  1302. else
  1303. begin
  1304. { Fix for web bug 972 }
  1305. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  1306. cnt := PPaiProp(p^.optInfo)^.Regs[regCounter].nrOfMods;
  1307. hp3 := p;
  1308. for cnt2 := 1 to cnt do
  1309. if not(regModifiedByInstruction(regCounter,hp3) and
  1310. not(PPaiProp(hp3^.optInfo)^.canBeRemoved)) then
  1311. getNextInstruction(hp3,hp3)
  1312. else
  1313. break;
  1314. getLastInstruction(p,hp4);
  1315. RestoreRegContentsTo(regCounter,
  1316. PPaiProp(hp4^.optInfo)^.Regs[regCounter],
  1317. p,hp3);
  1318. end;
  1319. End;
  1320. End;
  1321. if not ppaiprop(p^.optinfo)^.canBeRemoved and
  1322. not regInRef(reg32(paicpu(p)^.oper[1].reg),
  1323. paicpu(p)^.oper[0].ref^) then
  1324. removePrevNotUsedLoad(p,reg32(paicpu(p)^.oper[1].reg),false);
  1325. End;
  1326. top_Reg:
  1327. { try to replace the new reg with the old reg }
  1328. if not(PPaiProp(p^.optInfo)^.canBeRemoved) and
  1329. { only remove if we're not storing something in a regvar }
  1330. (paicpu(p)^.oper[1].reg in (usableregs+[R_EDI])) and
  1331. (paicpu(p)^.opcode = A_MOV) and
  1332. getLastInstruction(p,hp4) then
  1333. begin
  1334. case paicpu(p)^.oper[1].typ of
  1335. top_Reg:
  1336. { we only have to start replacing from the instruction after the mov, }
  1337. { but replacereg only starts with getnextinstruction(p,p) }
  1338. if ReplaceReg(asmL,paicpu(p)^.oper[0].reg,
  1339. paicpu(p)^.oper[1].reg,p,
  1340. PPaiProp(hp4^.optInfo)^.Regs[paicpu(p)^.oper[1].reg],false,hp1) then
  1341. begin
  1342. PPaiProp(p^.optInfo)^.canBeRemoved := true;
  1343. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,
  1344. PPaiProp(p^.optInfo)^.regs[paicpu(p)^.oper[0].reg].startMod,
  1345. hp1);
  1346. end;
  1347. end
  1348. end;
  1349. top_symbol,Top_Const:
  1350. Begin
  1351. Case Paicpu(p)^.oper[1].typ Of
  1352. Top_Reg:
  1353. Begin
  1354. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  1355. If GetLastInstruction(p, hp1) Then
  1356. With PPaiProp(hp1^.OptInfo)^.Regs[regCounter] Do
  1357. if (typ in [con_const,con_noRemoveConst]) and
  1358. (paicpu(startMod)^.opsize >= paicpu(p)^.opsize) and
  1359. opsequal(paicpu(StartMod)^.oper[0],paicpu(p)^.oper[0]) Then
  1360. begin
  1361. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  1362. allocRegBetween(asmL,regCounter,startMod,p);
  1363. end;
  1364. End;
  1365. Top_Ref:
  1366. if (paicpu(p)^.oper[0].typ = top_const) and
  1367. getLastInstruction(p,hp1) and
  1368. findRegWithConst(hp1,paicpu(p)^.opsize,paicpu(p)^.oper[0].val,regCounter) then
  1369. begin
  1370. paicpu(p)^.loadreg(0,regCounter);
  1371. allocRegBetween(AsmL,reg32(regCounter),
  1372. PPaiProp(hp1^.optinfo)^.regs[regCounter].startMod,p);
  1373. end;
  1374. End;
  1375. End;
  1376. End;
  1377. End;
  1378. A_STD: If GetLastInstruction(p, hp1) And
  1379. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  1380. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1381. End
  1382. End;
  1383. End;
  1384. GetNextInstruction(p, p);
  1385. End;
  1386. End;
  1387. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  1388. { Removes the marked instructions and disposes the PPaiProps of the other }
  1389. { instructions }
  1390. Var p, hp1: Pai;
  1391. begin
  1392. p := First;
  1393. While (p <> Last) Do
  1394. Begin
  1395. If (p^.typ = ait_marker) and
  1396. (pai_marker(p)^.kind in [noPropInfoStart,noPropInfoEnd]) then
  1397. begin
  1398. hp1 := pai(p^.next);
  1399. asmL^.remove(p);
  1400. dispose(p,done);
  1401. p := hp1
  1402. end
  1403. else
  1404. {$ifndef noinstremove}
  1405. if assigned(p^.optInfo) and
  1406. PPaiProp(p^.optInfo)^.canBeRemoved then
  1407. begin
  1408. {$IfDef TP}
  1409. Dispose(PPaiProp(p^.OptInfo));
  1410. {$EndIf}
  1411. hp1 := pai(p^.next);
  1412. AsmL^.Remove(p);
  1413. Dispose(p, Done);
  1414. p := hp1;
  1415. End
  1416. Else
  1417. {$endif noinstremove}
  1418. Begin
  1419. {$IfDef TP}
  1420. if assigned(p^.optInfo) then
  1421. Dispose(PPaiProp(p^.OptInfo));
  1422. {$EndIf TP}
  1423. p^.OptInfo := nil;
  1424. p := pai(p^.next);;
  1425. End;
  1426. End;
  1427. {$IfNDef TP}
  1428. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  1429. {$EndIf TP}
  1430. End;
  1431. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  1432. Begin
  1433. DoCSE(AsmL, First, Last);
  1434. RemoveInstructs(AsmL, First, Last);
  1435. End;
  1436. End.
  1437. {
  1438. $Log$
  1439. Revision 1.8 2000-09-20 15:00:58 jonas
  1440. + much improved CSE: the CSE now searches further back for sequences it
  1441. can reuse. After I've also implemented register renaming, the effect
  1442. should be even better (afaik web bug 1088 will then even be optimized
  1443. properly). I don't know about the slow down factor this adds. Maybe
  1444. a new optimization level should be introduced?
  1445. Revision 1.7 2000/08/25 19:40:45 jonas
  1446. * refined previous fix a bit, some instructions weren't being removed
  1447. while they could (merged from fixes branch)
  1448. * made checksequence a bit faster
  1449. Revision 1.6 2000/08/23 12:55:10 jonas
  1450. * fix for web bug 1112 and a bit of clean up in csopt386 (merged from
  1451. fixes branch)
  1452. Revision 1.5 2000/08/04 20:08:03 jonas
  1453. * improved detection of range of instructions which use a register
  1454. (merged from fixes branch)
  1455. Revision 1.4 2000/07/21 15:19:54 jonas
  1456. * daopt386: changes to getnextinstruction/getlastinstruction so they
  1457. ignore labels who have is_addr set
  1458. + daopt386/csopt386: remove loads of registers which are overwritten
  1459. before their contents are used (especially usefull for removing superfluous
  1460. maybe_loadesi outputs and push/pops transformed by below optimization
  1461. + popt386: transform pop/pop/pop/.../push/push/push to sequences of
  1462. 'movl x(%esp),%reg' (only active when compiling a go32v2 compiler
  1463. currently because I don't know whether it's safe to do this under Win32/
  1464. Linux (because of problems we had when using esp as frame pointer on
  1465. those os'es)
  1466. Revision 1.3 2000/07/14 05:11:48 michael
  1467. + Patch to 1.1
  1468. Revision 1.2 2000/07/13 11:32:39 michael
  1469. + removed logs
  1470. }