csopt386.pas 73 KB

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