csopt386.pas 71 KB

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