csopt386.pas 61 KB

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