csopt386.pas 71 KB

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