csopt386.pas 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  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. var
  756. tmpresult: boolean;
  757. begin
  758. changeOp := false;
  759. case o.typ of
  760. top_reg: changeOp := changeReg(o.reg,newReg,orgReg);
  761. top_ref:
  762. begin
  763. tmpresult := changeReg(o.ref^.base,newReg,orgReg);
  764. changeop := changeReg(o.ref^.index,newReg,orgReg) or tmpresult;
  765. end;
  766. end;
  767. end;
  768. procedure updateStates(orgReg,newReg: tregister; hp: Tai; writeStateToo: boolean);
  769. var
  770. prev: Tai;
  771. newOrgRegRState, newOrgRegWState: byte;
  772. begin
  773. if getLastInstruction(hp,prev) then
  774. with pTaiprop(prev.optinfo)^ do
  775. begin
  776. {$ifopt r+}
  777. {$define rangeon}
  778. {$r-}
  779. {$endif}
  780. newOrgRegRState := regs[orgReg].rState +
  781. pTaiprop(hp.optinfo)^.regs[newReg].rState - regs[newReg].rstate;
  782. if writeStateToo then
  783. newOrgRegWState := regs[orgReg].wState +
  784. pTaiprop(hp.optinfo)^.regs[newReg].wState - regs[newReg].wstate;
  785. {$ifdef rangeon}
  786. {$undef rangeon}
  787. {$r+}
  788. {$endif}
  789. end
  790. else
  791. with pTaiprop(hp.optinfo)^.regs[newReg] do
  792. begin
  793. newOrgRegRState := rState;
  794. if writeStateToo then
  795. newOrgRegWState := wState;
  796. end;
  797. with pTaiprop(hp.optinfo)^.regs[orgReg] do
  798. begin
  799. rState := newOrgRegRState;
  800. if writeStateToo then
  801. wState := newOrgRegwState;
  802. end;
  803. end;
  804. function doReplaceReg(hp: Taicpu; newReg, orgReg: tregister): boolean;
  805. var
  806. opCount: longint;
  807. tmpResult: boolean;
  808. begin
  809. for opCount := 0 to hp.ops-1 do
  810. tmpResult :=
  811. changeOp(hp.oper[opCount],newReg,orgReg) or tmpResult;
  812. doReplaceReg := tmpResult;
  813. end;
  814. function RegSizesOK(oldReg,newReg: TRegister; p: Taicpu): boolean;
  815. { oldreg and newreg must be 32bit components }
  816. var opCount: byte;
  817. begin
  818. RegSizesOK := true;
  819. { if only one of them is a general purpose register ... }
  820. if (IsGP32reg(oldReg) xor IsGP32Reg(newReg)) then
  821. begin
  822. for opCount := 0 to 2 do
  823. if (p.oper[opCount].typ = top_reg) and
  824. (p.oper[opCount].reg in [R_AL..R_DH]) then
  825. begin
  826. RegSizesOK := false;
  827. break
  828. end
  829. end;
  830. end;
  831. function doReplaceReadReg(p: Taicpu; newReg,orgReg: tregister): boolean;
  832. var opCount: byte;
  833. begin
  834. doReplaceReadReg := false;
  835. { handle special case }
  836. case p.opcode of
  837. A_IMUL:
  838. begin
  839. case p.ops of
  840. 1: internalerror(1301001);
  841. 2,3:
  842. begin
  843. if changeOp(p.oper[0],newReg,orgReg) then
  844. begin
  845. { updateStates(orgReg,newReg,p,false);}
  846. doReplaceReadReg := true;
  847. end;
  848. if p.ops = 3 then
  849. if changeOp(p.oper[1],newReg,orgReg) then
  850. begin
  851. { updateStates(orgReg,newReg,p,false);}
  852. doReplaceReadReg := true;
  853. end;
  854. end;
  855. end;
  856. end;
  857. A_DIV,A_IDIV,A_MUL: internalerror(1301002);
  858. else
  859. begin
  860. for opCount := 0 to 2 do
  861. if p.oper[opCount].typ = top_ref then
  862. if changeOp(p.oper[opCount],newReg,orgReg) then
  863. begin
  864. { updateStates(orgReg,newReg,p,false);}
  865. doReplaceReadReg := true;
  866. end;
  867. for opCount := 1 to MaxCh do
  868. case InsProp[p.opcode].Ch[opCount] of
  869. Ch_ROp1:
  870. if p.oper[0].typ = top_reg then
  871. if changeReg(p.oper[0].reg,newReg,orgReg) then
  872. begin
  873. { updateStates(orgReg,newReg,p,false);}
  874. doReplaceReadReg := true;
  875. end;
  876. Ch_ROp2:
  877. if p.oper[1].typ = top_reg then
  878. if changeReg(p.oper[1].reg,newReg,orgReg) then
  879. begin
  880. { updateStates(orgReg,newReg,p,false);}
  881. doReplaceReadReg := true;
  882. end;
  883. Ch_ROp3:
  884. if p.oper[2].typ = top_reg then
  885. if changeReg(p.oper[2].reg,newReg,orgReg) then
  886. begin
  887. { updateStates(orgReg,newReg,p,false);}
  888. doReplaceReadReg := true;
  889. end;
  890. end;
  891. end;
  892. end;
  893. end;
  894. procedure updateState(reg: tregister; p: Tai);
  895. { this procedure updates the read and write states of the instructions }
  896. { coming after p. It's called when the read/write state of p has been }
  897. { changed and this change has to be propagated to the following }
  898. { instructions as well }
  899. var
  900. newRState, newWState: byte;
  901. prevRState, prevWState: byte;
  902. doRState, doWState: boolean;
  903. begin
  904. { get the new read/write states from p }
  905. with pTaiprop(p.optinfo)^.regs[reg] do
  906. begin
  907. newRState := rState;
  908. newWState := wState;
  909. end;
  910. if not GetNextInstruction(p,p) then
  911. exit;
  912. { get the old read/write states from the next instruction, to know }
  913. { when we can stop updating }
  914. with pTaiprop(p.optinfo)^.regs[reg] do
  915. begin
  916. prevRState := rState;
  917. prevWState := wState;
  918. end;
  919. { adjust the states if this next instruction reads/writes the register }
  920. if regReadByInstruction(reg,p) then
  921. incState(newRState,1);
  922. if regModifiedByInstruction(reg,p) then
  923. incState(newWState,1);
  924. { do we still have to update the read and/or write states? }
  925. doRState := true;
  926. doWState := true;
  927. repeat
  928. { update the states }
  929. with pTaiprop(p.optinfo)^.regs[reg] do
  930. begin
  931. if doRState then
  932. rState := newRState;
  933. if doWState then
  934. wState := newWState;
  935. end;
  936. if not getNextInstruction(p,p) then
  937. break;
  938. with pTaiprop(p.optinfo)^.regs[reg] do
  939. begin
  940. { stop updating the read state if it changes }
  941. doRState :=
  942. doRState and (rState = prevRState);
  943. { if, by accident, this changed state is the same as the one }
  944. { we've been using, change it to a value that's definitely }
  945. { different from the previous and next state }
  946. if not doRState and
  947. (rState = newRState) then
  948. begin
  949. incState(newRState,1);
  950. prevRState := rState;
  951. doRState := true;
  952. end;
  953. { ditto for the write state }
  954. doWState :=
  955. doWState and (WState = prevWState);
  956. if not doWState and
  957. (wState = newWState) then
  958. begin
  959. incState(newWState,1);
  960. prevWState := wState;
  961. doWState := true;
  962. end;
  963. end;
  964. { stop when we don't have to update either state anymore }
  965. until not(doRState or doWState);
  966. end;
  967. function storeBack(p1: Tai; orgReg, newReg: tregister): boolean;
  968. { returns true if p1 contains an instruction that stores the contents }
  969. { of newReg back to orgReg }
  970. begin
  971. storeBack :=
  972. (p1.typ = ait_instruction) and
  973. (Taicpu(p1).opcode = A_MOV) and
  974. (Taicpu(p1).oper[0].typ = top_reg) and
  975. (Taicpu(p1).oper[0].reg = newReg) and
  976. (Taicpu(p1).oper[1].typ = top_reg) and
  977. (Taicpu(p1).oper[1].reg = orgReg);
  978. end;
  979. function ReplaceReg(asmL: TAAsmOutput; orgReg, newReg: TRegister; p: Tai;
  980. const c: TContent; orgRegCanBeModified: Boolean;
  981. var returnEndP: Tai): Boolean;
  982. { Tries to replace orgreg with newreg in all instructions coming after p }
  983. { until orgreg gets loaded with a new value. Returns true if successful, }
  984. { false otherwise. If successful, the contents of newReg are set to c, }
  985. { which should hold the contents of newReg before the current sequence }
  986. { started }
  987. { if the function returns true, returnEndP holds the last instruction }
  988. { where newReg was replaced by orgReg }
  989. var endP, hp: Tai;
  990. removeLast, sequenceEnd, tmpResult, newRegModified, orgRegRead,
  991. stateChanged, readStateChanged: Boolean;
  992. begin
  993. ReplaceReg := false;
  994. tmpResult := true;
  995. sequenceEnd := false;
  996. newRegModified := false;
  997. orgRegRead := false;
  998. removeLast := false;
  999. endP := p;
  1000. while tmpResult and not sequenceEnd do
  1001. begin
  1002. tmpResult :=
  1003. getNextInstruction(endP,endP) and
  1004. (endp.typ = ait_instruction) and
  1005. not(Taicpu(endp).is_jmp);
  1006. if tmpresult and not assigned(endp.optInfo) then
  1007. begin
  1008. { hp := Tai_asm_comment.Create(strpnew('next no optinfo'));
  1009. hp.next := endp;
  1010. hp.previous := endp.previous;
  1011. endp.previous := hp;
  1012. if assigned(hp.previous) then
  1013. hp.previous^.next := hp;}
  1014. exit;
  1015. end;
  1016. If tmpResult and
  1017. { don't take into account instructions that will be removed }
  1018. Not (PTaiProp(endp.optInfo)^.canBeRemoved) then
  1019. begin
  1020. { if the newReg gets stored back to the oldReg, we can change }
  1021. { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
  1022. { %oldReg" to "<operations on %oldReg>" }
  1023. removeLast := storeBack(endP, orgReg, newReg);
  1024. sequenceEnd :=
  1025. { no support for (i)div, mul and imul with hardcoded operands }
  1026. (noHardCodedRegs(Taicpu(endP),orgReg,newReg) and
  1027. { if newReg gets loaded with a new value, we can stop }
  1028. { replacing newReg with oldReg here (possibly keeping }
  1029. { the original contents of oldReg so we still know them }
  1030. { afterwards) }
  1031. RegLoadedWithNewValue(newReg,true,Taicpu(endP)) or
  1032. { we can also stop if we reached the end of the use of }
  1033. { newReg's current contents }
  1034. (GetNextInstruction(endp,hp) and
  1035. FindRegDealloc(newReg,hp)));
  1036. { to be able to remove the first and last instruction of }
  1037. { movl %reg1, %reg2 }
  1038. { <operations on %reg2> (replacing reg2 with reg1 here) }
  1039. { movl %reg2, %reg1 }
  1040. { %reg2 must not be use afterwards (it can be as the }
  1041. { result of a peepholeoptimization) }
  1042. removeLast := removeLast and sequenceEnd;
  1043. newRegModified :=
  1044. newRegModified or
  1045. (not(regLoadedWithNewValue(newReg,true,Taicpu(endP))) and
  1046. RegModifiedByInstruction(newReg,endP));
  1047. orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
  1048. sequenceEnd := SequenceEnd and
  1049. (removeLast or
  1050. { since newReg will be replaced by orgReg, we can't allow that newReg }
  1051. { gets modified if orgReg is still read afterwards (since after }
  1052. { replacing, this would mean that orgReg first gets modified and then }
  1053. { gets read in the assumption it still contains the unmodified value) }
  1054. not(newRegModified and orgRegRead)) (* and
  1055. { since newReg will be replaced by orgReg, we can't allow that newReg }
  1056. { gets modified if orgRegCanBeModified = false }
  1057. { this now gets checked after the loop (JM) }
  1058. (orgRegCanBeModified or not(newRegModified)) *);
  1059. tmpResult :=
  1060. not(removeLast) and
  1061. not(newRegModified and orgRegRead) and
  1062. (* (orgRegCanBeModified or not(newRegModified)) and *)
  1063. (* already check at the top
  1064. (endp.typ = ait_instruction) and *)
  1065. NoHardCodedRegs(Taicpu(endP),orgReg,newReg) and
  1066. RegSizesOk(orgReg,newReg,Taicpu(endP)) and
  1067. not RegModifiedByInstruction(orgReg,endP);
  1068. end;
  1069. end;
  1070. sequenceEnd := sequenceEnd and
  1071. (removeLast or
  1072. (orgRegCanBeModified or not(newRegModified))) and
  1073. (not(assigned(endp)) or
  1074. not(endp.typ = ait_instruction) or
  1075. (noHardCodedRegs(Taicpu(endP),orgReg,newReg) and
  1076. RegSizesOk(orgReg,newReg,Taicpu(endP)) and
  1077. not(newRegModified and
  1078. (orgReg in PTaiProp(endp.optInfo)^.usedRegs) and
  1079. not(RegLoadedWithNewValue(orgReg,true,Taicpu(endP))))));
  1080. if SequenceEnd then
  1081. begin
  1082. {$ifdef replaceregdebug}
  1083. hp := Tai_asm_comment.Create(strpnew(
  1084. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  1085. ' from here...')));
  1086. hp.next := p;
  1087. hp.previous := p.previous;
  1088. p.previous := hp;
  1089. if assigned(hp.previous) then
  1090. hp.previous^.next := hp;
  1091. hp := Tai_asm_comment.Create(strpnew(
  1092. 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  1093. ' till here')));
  1094. hp.next := endp.next;
  1095. hp.previous := endp;
  1096. endp.next := hp;
  1097. if assigned(hp.next) then
  1098. hp.next.previous := hp;
  1099. {$endif replaceregdebug}
  1100. replaceReg := true;
  1101. returnEndP := endP;
  1102. getNextInstruction(p,hp);
  1103. stateChanged := false;
  1104. while hp <> endP do
  1105. begin
  1106. if {not(PTaiProp(hp.optInfo)^.canBeRemoved) and }
  1107. (hp.typ = ait_instruction) then
  1108. stateChanged :=
  1109. doReplaceReg(Taicpu(hp),newReg,orgReg) or stateChanged;
  1110. if stateChanged then
  1111. updateStates(orgReg,newReg,hp,true);
  1112. getNextInstruction(hp,hp)
  1113. end;
  1114. if assigned(endp) and (endp.typ = ait_instruction) then
  1115. readStateChanged :=
  1116. DoReplaceReadReg(Taicpu(endP),newReg,orgReg);
  1117. if stateChanged or readStateChanged then
  1118. updateStates(orgReg,newReg,endP,stateChanged);
  1119. if stateChanged or readStateChanged then
  1120. updateState(orgReg,endP);
  1121. { the replacing stops either at the moment that }
  1122. { a) the newreg gets loaded with a new value (one not depending on the }
  1123. { current value of newreg) }
  1124. { b) newreg is completely replaced in this sequence and it's current value }
  1125. { isn't used anymore }
  1126. { In case b, the newreg was completely replaced by oldreg, so it's contents }
  1127. { are unchanged compared the start of this sequence, so restore them }
  1128. If removeLast or
  1129. RegLoadedWithNewValue(newReg,true,endP) then
  1130. GetLastInstruction(endP,hp)
  1131. else hp := endP;
  1132. if removeLast or
  1133. (p <> endp) or
  1134. not RegLoadedWithNewValue(newReg,true,endP) then
  1135. RestoreRegContentsTo(newReg,c,p,hp);
  1136. { In both case a and b, it is possible that the new register was modified }
  1137. { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
  1138. { oldreg's contents have been changed. To take this into account, we simply }
  1139. { set the contents of orgreg to "unknown" after this sequence }
  1140. if newRegModified then
  1141. ClearRegContentsFrom(orgReg,p,hp);
  1142. if removeLast then
  1143. pTaiprop(endp.optinfo)^.canBeRemoved := true;
  1144. allocRegBetween(asml,orgReg,p,endP);
  1145. end
  1146. {$ifdef replaceregdebug}
  1147. else
  1148. begin
  1149. hp := Tai_asm_comment.Create(strpnew(
  1150. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  1151. ' from here...')));
  1152. hp.previous := p.previous;
  1153. hp.next := p;
  1154. p.previous := hp;
  1155. if assigned(hp.previous) then
  1156. hp.previous^.next := hp;
  1157. hp := Tai_asm_comment.Create(strpnew(
  1158. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  1159. ' failed here')));
  1160. hp.next := endp.next;
  1161. hp.previous := endp;
  1162. endp.next := hp;
  1163. if assigned(hp.next) then
  1164. hp.next.previous := hp;
  1165. end;
  1166. {$endif replaceregdebug}
  1167. End;
  1168. Function FindRegWithConst(p: Tai; size: topsize; l: longint; Var Res: TRegister): Boolean;
  1169. {Finds a register which contains the constant l}
  1170. Var Counter: TRegister;
  1171. {$ifdef testing}
  1172. hp: Tai;
  1173. {$endif testing}
  1174. tmpresult: boolean;
  1175. Begin
  1176. Counter := R_NO;
  1177. repeat
  1178. inc(counter);
  1179. tmpresult := (pTaiprop(p.optInfo)^.regs[counter].typ in
  1180. [con_const,con_noRemoveConst]) and
  1181. (Taicpu(PTaiProp(p.OptInfo)^.Regs[Counter].StartMod).opsize = size) and
  1182. (Taicpu(PTaiProp(p.OptInfo)^.Regs[Counter].StartMod).oper[0].typ = top_const) and
  1183. (Taicpu(PTaiProp(p.OptInfo)^.Regs[Counter].StartMod).oper[0].val = l);
  1184. {$ifdef testing}
  1185. if (pTaiprop(p.optInfo)^.regs[counter].typ in [con_const,con_noRemoveConst]) then
  1186. begin
  1187. hp := Tai_asm_comment.Create(strpnew(
  1188. 'checking const load of '+tostr(l)+' here...')));
  1189. hp.next := PTaiProp(p.OptInfo)^.Regs[Counter].StartMod;
  1190. hp.previous := PTaiProp(p.OptInfo)^.Regs[Counter].StartMod^.previous;
  1191. PTaiProp(p.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
  1192. if assigned(hp.previous) then
  1193. hp.previous^.next := hp;
  1194. end;
  1195. {$endif testing}
  1196. until tmpresult or (Counter = R_EDI);
  1197. if tmpResult then
  1198. res := Taicpu(PTaiProp(p.OptInfo)^.Regs[Counter].StartMod).oper[1].reg;
  1199. FindRegWithConst := tmpResult;
  1200. End;
  1201. procedure removePrevNotUsedLoad(p: Tai; reg: tRegister; check: boolean);
  1202. { If check = true, it means the procedure has to check whether it isn't }
  1203. { possible that the contents are still used after p (used when removing }
  1204. { instructions because of a "call"), otherwise this is not necessary }
  1205. { (e.g. when you have a "mov 8(%ebp),%eax", you can be sure the previous }
  1206. { value of %eax isn't used anymore later on) }
  1207. var
  1208. hp1: Tai;
  1209. begin
  1210. if getLastInstruction(p,hp1) then
  1211. with pTaiprop(hp1.optInfo)^.regs[reg] do
  1212. if (typ in [con_ref,con_invalid]) and
  1213. (nrOfMods = 1) and
  1214. (rState = pTaiprop(startmod.optInfo)^.regs[reg].rState) and
  1215. (not(check) or
  1216. (not(regInInstruction(reg,p)) and
  1217. (not(reg in usableregs) and
  1218. (startmod.typ = ait_instruction) and
  1219. ((Taicpu(startmod).opcode = A_MOV) or
  1220. (Taicpu(startmod).opcode = A_MOVZX) or
  1221. (Taicpu(startmod).opcode = A_MOVSX)) and
  1222. (Taicpu(startmod).oper[0].typ = top_ref) and
  1223. (Taicpu(startmod).oper[0].ref^.base = stack_pointer)) or
  1224. not(reg in pTaiprop(hp1.optInfo)^.usedRegs) or
  1225. findRegDealloc(reg,p))) then
  1226. pTaiprop(startMod.optInfo)^.canBeRemoved := true;
  1227. end;
  1228. function is_mov_for_div(p: Taicpu): boolean;
  1229. begin
  1230. is_mov_for_div :=
  1231. (p.opcode = A_MOV) and
  1232. (p.oper[0].typ = top_const) and
  1233. (p.oper[1].typ = top_reg) and
  1234. (p.oper[1].reg = R_EDX) and
  1235. getNextInstruction(p,p) and
  1236. (p.typ = ait_instruction) and
  1237. ((p.opcode = A_DIV) or
  1238. (p.opcode = A_IDIV));
  1239. end;
  1240. procedure DoCSE(AsmL: TAAsmOutput; First, Last: Tai; findPrevSeqs, doSubOpts: boolean);
  1241. {marks the instructions that can be removed by RemoveInstructs. They're not
  1242. removed immediately because sometimes an instruction needs to be checked in
  1243. two different sequences}
  1244. var cnt, cnt2, cnt3, orgNrOfMods: longint;
  1245. p, hp1, hp2, prevSeq, prevSeq_next: Tai;
  1246. hp3, hp4: Tai;
  1247. hp5 : Tai;
  1248. RegInfo: TRegInfo;
  1249. RegCounter: TRegister;
  1250. Begin
  1251. p := First;
  1252. SkipHead(p);
  1253. While (p <> Last) Do
  1254. Begin
  1255. Case p.typ Of
  1256. ait_align:
  1257. if not(Tai_align(p).use_op) then
  1258. SetAlignReg(p);
  1259. ait_instruction:
  1260. Begin
  1261. Case Taicpu(p).opcode Of
  1262. A_CALL:
  1263. for regCounter := R_EAX to R_EBX do
  1264. removePrevNotUsedLoad(p,regCounter,true);
  1265. A_CLD: If GetLastInstruction(p, hp1) And
  1266. (PTaiProp(hp1.OptInfo)^.DirFlag = F_NotSet) Then
  1267. PTaiProp(Tai(p).OptInfo)^.CanBeRemoved := True;
  1268. A_MOV, A_MOVZX, A_MOVSX:
  1269. Begin
  1270. hp2 := p;
  1271. Case Taicpu(p).oper[0].typ Of
  1272. top_ref, top_reg:
  1273. if (Taicpu(p).oper[1].typ = top_reg) then
  1274. Begin
  1275. With PTaiProp(p.OptInfo)^.Regs[Reg32(Taicpu(p).oper[1].reg)] Do
  1276. Begin
  1277. if (startmod = p) then
  1278. orgNrOfMods := nrOfMods
  1279. else
  1280. orgNrOfMods := 0;
  1281. If (p = StartMod) And
  1282. GetLastInstruction (p, hp1) And
  1283. (hp1.typ <> ait_marker) Then
  1284. {so we don't try to check a sequence when p is the first instruction of the block}
  1285. begin
  1286. {$ifdef csdebug}
  1287. hp5 := Tai_asm_comment.Create(strpnew(
  1288. 'cse checking '+att_reg2str[Reg32(Taicpu(p).oper[1].reg)])));
  1289. insertLLItem(asml,p,p.next,hp5);
  1290. {$endif csdebug}
  1291. If CheckSequence(p,prevSeq,Taicpu(p).oper[1].reg, Cnt, RegInfo, findPrevSeqs) And
  1292. (Cnt > 0) Then
  1293. Begin
  1294. hp1 := nil;
  1295. { although it's perfectly ok to remove an instruction which doesn't contain }
  1296. { the register that we've just checked (CheckSequence takes care of that), }
  1297. { the sequence containing this other register should also be completely }
  1298. { checked and removed, otherwise we may get situations like this: }
  1299. { }
  1300. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  1301. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  1302. { movl 8(%edx), %edx movl 8(%edx), %edx }
  1303. { movl (%eax), eax movl (%eax), eax }
  1304. { cmpl %eax, %edx cmpl %eax, %edx }
  1305. { jnz l123 getting converted to jnz l123 }
  1306. { movl 12(%ebp), %edx movl 4(%eax), eax }
  1307. { movl 16(%ebp), %eax }
  1308. { movl 8(%edx), %edx }
  1309. { movl 4(%eax), eax }
  1310. Cnt2 := 1;
  1311. While Cnt2 <= Cnt Do
  1312. Begin
  1313. If not(regInInstruction(Taicpu(hp2).oper[1].reg, p)) and
  1314. not(pTaiprop(p.optinfo)^.canBeRemoved) then
  1315. begin
  1316. if (p.typ = ait_instruction) And
  1317. ((Taicpu(p).OpCode = A_MOV) or
  1318. (Taicpu(p).opcode = A_MOVZX) or
  1319. (Taicpu(p).opcode = A_MOVSX)) And
  1320. (Taicpu(p).oper[1].typ = top_reg) then
  1321. if not is_mov_for_div(Taicpu(p)) then
  1322. begin
  1323. regCounter := reg32(Taicpu(p).oper[1].reg);
  1324. if (regCounter in reginfo.regsStillUsedAfterSeq) then
  1325. begin
  1326. if (hp1 = nil) then
  1327. hp1 := reginfo.lastReload[regCounter];
  1328. end
  1329. {$ifndef noremove}
  1330. else
  1331. begin
  1332. hp5 := p;
  1333. for cnt3 := pTaiprop(p.optinfo)^.regs[regCounter].nrofmods downto 1 do
  1334. begin
  1335. if regModifiedByInstruction(regCounter,hp5) then
  1336. PTaiProp(hp5.OptInfo)^.CanBeRemoved := True;
  1337. getNextInstruction(hp5,hp5);
  1338. end;
  1339. end
  1340. {$endif noremove}
  1341. end
  1342. {$ifndef noremove}
  1343. else
  1344. PTaiProp(p.OptInfo)^.CanBeRemoved := True
  1345. {$endif noremove}
  1346. end
  1347. {$ifndef noremove}
  1348. else
  1349. PTaiProp(p.OptInfo)^.CanBeRemoved := True
  1350. {$endif noremove}
  1351. ; Inc(Cnt2);
  1352. GetNextInstruction(p, p);
  1353. End;
  1354. {hp4 is used to get the contents of the registers before the sequence}
  1355. GetLastInstruction(hp2, hp4);
  1356. getNextInstruction(prevSeq,prevSeq_next);
  1357. {$IfDef CSDebug}
  1358. For RegCounter := R_EAX To R_EDI Do
  1359. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  1360. Begin
  1361. hp5 := Tai_asm_comment.Create(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  1362. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  1363. InsertLLItem(AsmL, Tai(hp2.previous), hp2, hp5);
  1364. End;
  1365. {$EndIf CSDebug}
  1366. { If some registers were different in the old and the new sequence, move }
  1367. { the contents of those old registers to the new ones }
  1368. For RegCounter := R_EAX To R_EDI Do
  1369. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  1370. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  1371. Begin
  1372. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  1373. PTaiProp(prevSeq.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,hp2);
  1374. if hp4 <> prevSeq then
  1375. begin
  1376. if assigned(reginfo.lastReload[regCounter]) then
  1377. getLastInstruction(reginfo.lastReload[regCounter],hp3)
  1378. else if assigned(reginfo.lastReload[regInfo.New2OldReg[regCounter]]) then
  1379. getLastInstruction(reginfo.lastReload[regInfo.new2OldReg[regCounter]],hp3)
  1380. else hp3 := hp4;
  1381. clearRegContentsFrom(regCounter,prevSeq_next,hp3);
  1382. getnextInstruction(hp3,hp3);
  1383. allocRegBetween(asmL,regCounter,prevSeq,hp3);
  1384. end;
  1385. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  1386. {old reg new reg}
  1387. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  1388. Begin
  1389. getLastInstruction(p,hp3);
  1390. If (hp4 <> prevSeq) or
  1391. not(regCounter in usableRegs + [R_EDI,R_ESI]) or
  1392. not ReplaceReg(asmL,RegInfo.New2OldReg[RegCounter],
  1393. regCounter,hp3,
  1394. PTaiProp(PrevSeq.optInfo)^.Regs[regCounter],true,hp5) then
  1395. begin
  1396. hp3 := Tai_Marker.Create(NoPropInfoStart);
  1397. InsertLLItem(AsmL, prevSeq_next.previous,Tai(prevSeq_next), hp3);
  1398. hp5 := Taicpu.Op_Reg_Reg(A_MOV, S_L,
  1399. {old reg new reg}
  1400. RegInfo.New2OldReg[RegCounter], RegCounter);
  1401. new(pTaiprop(hp5.optinfo));
  1402. pTaiprop(hp5.optinfo)^ := pTaiprop(prevSeq_next.optinfo)^;
  1403. pTaiprop(hp5.optinfo)^.canBeRemoved := false;
  1404. InsertLLItem(AsmL, prevSeq_next.previous, Tai(prevSeq_next), hp5);
  1405. hp3 := Tai_Marker.Create(NoPropInfoEnd);
  1406. InsertLLItem(AsmL, prevSeq_next.previous, Tai(prevSeq_next), hp3);
  1407. { adjusts states in previous instruction so that it will }
  1408. { definitely be different from the previous or next state }
  1409. incstate(pTaiprop(hp5.optinfo)^.
  1410. regs[RegInfo.New2OldReg[RegCounter]].rstate,20);
  1411. incstate(pTaiprop(hp5.optinfo)^.
  1412. regs[regCounter].wstate,20);
  1413. updateState(RegInfo.New2OldReg[RegCounter],hp5);
  1414. end
  1415. End
  1416. Else
  1417. { imagine the following code: }
  1418. { normal wrong optimized }
  1419. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  1420. { movl (%eax), %eax movl (%eax), %eax }
  1421. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  1422. { jne l1 jne l1 }
  1423. { movl 8(%ebp), %eax }
  1424. { movl (%eax), %edi movl %eax, %edi }
  1425. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  1426. { movl 8(%ebp), %eax }
  1427. { pushl 70(%eax) pushl 70(%eax) }
  1428. { }
  1429. { The error is that at the moment that the last instruction is executed, }
  1430. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  1431. { registers that are completely removed from a sequence (= registers in }
  1432. { RegLoadedForRef, have to be changed to their contents from before the }
  1433. { sequence. }
  1434. If RegCounter in RegInfo.RegsLoadedForRef Then
  1435. Begin
  1436. hp3 := hp2;
  1437. { cnt still holds the number of instructions }
  1438. { of the sequence, so go to the end of it }
  1439. for cnt2 := 1 to pred(cnt) Do
  1440. getNextInstruction(hp3,hp3);
  1441. { hp4 = instruction prior to start of sequence }
  1442. restoreRegContentsTo(regCounter,
  1443. PTaiProp(hp4.OptInfo)^.Regs[RegCounter],
  1444. hp2,hp3);
  1445. End;
  1446. End;
  1447. If hp1 <> nil Then
  1448. p := hp1;
  1449. Continue;
  1450. End
  1451. Else
  1452. If (PTaiProp(p.OptInfo)^.
  1453. regs[reg32(Taicpu(p).oper[1].reg)].typ
  1454. in [con_ref,con_noRemoveRef]) and
  1455. (PTaiProp(p.OptInfo)^.CanBeRemoved) Then
  1456. if (cnt > 0) then
  1457. begin
  1458. p := hp2;
  1459. Cnt2 := 1;
  1460. While Cnt2 <= Cnt Do
  1461. Begin
  1462. If RegInInstruction(Taicpu(hp2).oper[1].reg, p) Then
  1463. PTaiProp(p.OptInfo)^.CanBeRemoved := False;
  1464. Inc(Cnt2);
  1465. GetNextInstruction(p, p);
  1466. End;
  1467. Continue;
  1468. End
  1469. else
  1470. begin
  1471. { Fix for web bug 972 }
  1472. regCounter := Reg32(Taicpu(p).oper[1].reg);
  1473. cnt := PTaiProp(p.optInfo)^.Regs[regCounter].nrOfMods;
  1474. hp3 := p;
  1475. for cnt2 := 1 to cnt do
  1476. if not(regModifiedByInstruction(regCounter,hp3) and
  1477. not(PTaiProp(hp3.optInfo)^.canBeRemoved)) then
  1478. getNextInstruction(hp3,hp3)
  1479. else
  1480. break;
  1481. getLastInstruction(p,hp4);
  1482. RestoreRegContentsTo(regCounter,
  1483. PTaiProp(hp4.optInfo)^.Regs[regCounter],
  1484. p,hp3);
  1485. end;
  1486. End;
  1487. End;
  1488. { try to replace the new reg with the old reg }
  1489. if not(PTaiProp(p.optInfo)^.canBeRemoved) then
  1490. if (Taicpu(p).oper[0].typ = top_reg) and
  1491. (Taicpu(p).oper[1].typ = top_reg) and
  1492. { only remove if we're not storing something in a regvar }
  1493. (Taicpu(p).oper[1].reg in (usableregs+[R_EDI])) and
  1494. (Taicpu(p).opcode = A_MOV) and
  1495. getLastInstruction(p,hp4) and
  1496. { we only have to start replacing from the instruction after the mov, }
  1497. { but replacereg only starts with getnextinstruction(p,p) }
  1498. replaceReg(asmL,Taicpu(p).oper[0].reg,
  1499. Taicpu(p).oper[1].reg,p,
  1500. pTaiprop(hp4.optInfo)^.regs[Taicpu(p).oper[1].reg],false,hp1) then
  1501. begin
  1502. pTaiprop(p.optInfo)^.canBeRemoved := true;
  1503. allocRegBetween(asmL,Taicpu(p).oper[0].reg,
  1504. pTaiProp(p.optInfo)^.regs[Taicpu(p).oper[0].reg].startMod,hp1);
  1505. end
  1506. else
  1507. if (Taicpu(p).oper[1].typ = top_reg) and
  1508. not regInOp(Taicpu(p).oper[1].reg,Taicpu(p).oper[0]) then
  1509. removePrevNotUsedLoad(p,reg32(Taicpu(p).oper[1].reg),false);
  1510. { at first, only try optimizations of large blocks, because doing }
  1511. { doing smaller ones may prevent bigger ones from completing in }
  1512. { in the next pass }
  1513. if not doSubOpts and (orgNrOfMods <> 0) then
  1514. begin
  1515. p := hp2;
  1516. for cnt := 1 to pred(orgNrOfMods) do
  1517. getNextInstruction(p,p);
  1518. end;
  1519. End;
  1520. top_symbol,Top_Const:
  1521. Begin
  1522. Case Taicpu(p).oper[1].typ Of
  1523. Top_Reg:
  1524. Begin
  1525. regCounter := Reg32(Taicpu(p).oper[1].reg);
  1526. If GetLastInstruction(p, hp1) Then
  1527. With PTaiProp(hp1.OptInfo)^.Regs[regCounter] Do
  1528. if (typ in [con_const,con_noRemoveConst]) and
  1529. (Taicpu(startMod).opsize >= Taicpu(p).opsize) and
  1530. opsequal(Taicpu(StartMod).oper[0],Taicpu(p).oper[0]) Then
  1531. begin
  1532. PTaiProp(p.OptInfo)^.CanBeRemoved := True;
  1533. allocRegBetween(asmL,regCounter,startMod,p);
  1534. end
  1535. else
  1536. removePrevNotUsedLoad(p,reg32(Taicpu(p).oper[1].reg),false);
  1537. End;
  1538. Top_Ref:
  1539. if (Taicpu(p).oper[0].typ = top_const) and
  1540. getLastInstruction(p,hp1) and
  1541. findRegWithConst(hp1,Taicpu(p).opsize,Taicpu(p).oper[0].val,regCounter) then
  1542. begin
  1543. Taicpu(p).loadreg(0,regCounter);
  1544. allocRegBetween(AsmL,reg32(regCounter),
  1545. PTaiProp(hp1.optinfo)^.regs[regCounter].startMod,p);
  1546. end;
  1547. End;
  1548. End;
  1549. End;
  1550. End;
  1551. A_STD: If GetLastInstruction(p, hp1) And
  1552. (PTaiProp(hp1.OptInfo)^.DirFlag = F_Set) Then
  1553. PTaiProp(Tai(p).OptInfo)^.CanBeRemoved := True;
  1554. End
  1555. End;
  1556. End;
  1557. GetNextInstruction(p, p);
  1558. End;
  1559. End;
  1560. function removeInstructs(asmL: TAAsmoutput; first, last: Tai): boolean;
  1561. { Removes the marked instructions and disposes the PTaiProps of the other }
  1562. { instructions }
  1563. Var
  1564. p, hp1: Tai;
  1565. nopropinfolevel: longint;
  1566. begin
  1567. removeInstructs := false;
  1568. p := First;
  1569. nopropinfolevel := 0;
  1570. While (p <> Last) Do
  1571. Begin
  1572. If (p.typ = ait_marker) and
  1573. (Tai_marker(p).kind = noPropInfoStart) then
  1574. begin
  1575. hp1 := Tai(p.next);
  1576. asmL.remove(p);
  1577. p.free;
  1578. nopropinfolevel := 1;
  1579. while (nopropinfolevel <> 0) do
  1580. begin
  1581. p := Tai(hp1.next);
  1582. {$ifndef noinstremove}
  1583. { allocregbetween can insert new ait_regalloc objects }
  1584. { without optinfo }
  1585. if (hp1.typ = ait_marker) then
  1586. begin
  1587. case Tai_marker(hp1).kind of
  1588. { they can be nested! }
  1589. noPropInfoStart: inc(nopropinfolevel);
  1590. noPropInfoEnd: dec(nopropinfolevel);
  1591. else
  1592. begin
  1593. hp1 := p;
  1594. continue;
  1595. end;
  1596. end;
  1597. asmL.remove(hp1);
  1598. hp1.free;
  1599. end
  1600. else if assigned(hp1.optinfo) then
  1601. if pTaiprop(hp1.optinfo)^.canBeRemoved then
  1602. begin
  1603. dispose(pTaiprop(hp1.optinfo));
  1604. hp1.optinfo := nil;
  1605. asmL.remove(hp1);
  1606. hp1.free;
  1607. end
  1608. else
  1609. {$endif noinstremove}
  1610. begin
  1611. dispose(pTaiprop(hp1.optinfo));
  1612. hp1.optinfo := nil;
  1613. end;
  1614. hp1 := p;
  1615. end;
  1616. end
  1617. else
  1618. {$ifndef noinstremove}
  1619. if assigned(p.optInfo) and
  1620. PTaiProp(p.optInfo)^.canBeRemoved then
  1621. begin
  1622. hp1 := Tai(p.next);
  1623. AsmL.Remove(p);
  1624. p.free;
  1625. p := hp1;
  1626. removeInstructs := true;
  1627. End
  1628. Else
  1629. {$endif noinstremove}
  1630. Begin
  1631. p.OptInfo := nil;
  1632. p := Tai(p.next);;
  1633. End;
  1634. End;
  1635. FreeMem(TaiPropBlock, NrOfTaiObjs*SizeOf(TTaiProp))
  1636. End;
  1637. function CSE(AsmL: TAAsmOutput; First, Last: Tai; pass: longint): boolean;
  1638. Begin
  1639. DoCSE(AsmL, First, Last, not(cs_slowoptimize in aktglobalswitches) or (pass >= 2),
  1640. not(cs_slowoptimize in aktglobalswitches) or (pass >= 1));
  1641. { register renaming }
  1642. if not(cs_slowoptimize in aktglobalswitches) or (pass > 0) then
  1643. doRenaming(asmL, first, last);
  1644. cse := removeInstructs(asmL, first, last);
  1645. End;
  1646. End.
  1647. {
  1648. $Log$
  1649. Revision 1.15 2001-04-06 16:24:38 jonas
  1650. * fixed bug due to short boolean evaluation
  1651. Revision 1.14 2001/04/02 21:20:36 peter
  1652. * resulttype rewrite
  1653. Revision 1.13 2001/01/10 08:52:40 michael
  1654. + Patch from jonas so 1.0.2 can be used to cycle
  1655. Revision 1.12 2001/01/07 15:51:17 jonas
  1656. * fixed crashing bug to due previous changes
  1657. Revision 1.11 2001/01/06 23:35:05 jonas
  1658. * fixed webbug 1323
  1659. Revision 1.10 2000/12/25 00:07:31 peter
  1660. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1661. tlinkedlist objects)
  1662. Revision 1.9 2000/12/05 09:33:42 jonas
  1663. * when searching for constants in registers, the returned register
  1664. sometimes didn't have the same size as the requested size
  1665. Revision 1.8 2000/11/29 00:30:43 florian
  1666. * unused units removed from uses clause
  1667. * some changes for widestrings
  1668. Revision 1.7 2000/11/28 16:32:11 jonas
  1669. + support for optimizing simple sequences with div/idiv/mul opcodes
  1670. Revision 1.6 2000/11/14 12:17:34 jonas
  1671. * fixed some bugs in checksequence
  1672. Revision 1.5 2000/11/09 12:34:44 jonas
  1673. * fixed range check error
  1674. Revision 1.4 2000/11/03 17:53:24 jonas
  1675. * some small improvements
  1676. Revision 1.3 2000/11/01 22:53:30 jonas
  1677. * register contents were not cleared if there was only 1 instruction
  1678. between de previous sequence and the current one
  1679. Revision 1.2 2000/10/24 10:40:53 jonas
  1680. + register renaming ("fixes" bug1088)
  1681. * changed command line options meanings for optimizer:
  1682. O2 now means peepholopts, CSE and register renaming in 1 pass
  1683. O3 is the same, but repeated until no further optimizations are
  1684. possible or until 5 passes have been done (to avoid endless loops)
  1685. * changed aopt386 so it does this looping
  1686. * added some procedures from csopt386 to the interface because they're
  1687. used by rropt386 as well
  1688. * some changes to csopt386 and daopt386 so that newly added instructions
  1689. by the CSE get optimizer info (they were simply skipped previously),
  1690. this fixes some bugs
  1691. Revision 1.1 2000/10/15 09:47:43 peter
  1692. * moved to i386/
  1693. Revision 1.14 2000/09/30 13:07:23 jonas
  1694. * fixed support for -Or with new features of CSE
  1695. Revision 1.13 2000/09/29 23:14:45 jonas
  1696. * search much further back for CSE sequences (non-conflicting stores are
  1697. now passed)
  1698. * remove more unnecessary loads of registers (especially the self pointer)
  1699. Revision 1.12 2000/09/26 11:49:41 jonas
  1700. * writes to register variables and to the self pointer now also count as
  1701. memore writes
  1702. Revision 1.11 2000/09/25 09:50:29 jonas
  1703. - removed TP conditional code
  1704. Revision 1.10 2000/09/24 15:06:14 peter
  1705. * use defines.inc
  1706. Revision 1.9 2000/09/22 15:01:59 jonas
  1707. * fixed some bugs in the previous improvements: in some cases, esi was
  1708. still being replaced before a conditional jump (the code that
  1709. detected conditional jumps sometimes skipped over them)
  1710. Revision 1.8 2000/09/20 15:00:58 jonas
  1711. + much improved CSE: the CSE now searches further back for sequences it
  1712. can reuse. After I've also implemented register renaming, the effect
  1713. should be even better (afaik web bug 1088 will then even be optimized
  1714. properly). I don't know about the slow down factor this adds. Maybe
  1715. a new optimization level should be introduced?
  1716. Revision 1.7 2000/08/25 19:40:45 jonas
  1717. * refined previous fix a bit, some instructions weren't being removed
  1718. while they could (merged from fixes branch)
  1719. * made checksequence a bit faster
  1720. Revision 1.6 2000/08/23 12:55:10 jonas
  1721. * fix for web bug 1112 and a bit of clean up in csopt386 (merged from
  1722. fixes branch)
  1723. Revision 1.5 2000/08/04 20:08:03 jonas
  1724. * improved detection of range of instructions which use a register
  1725. (merged from fixes branch)
  1726. Revision 1.4 2000/07/21 15:19:54 jonas
  1727. * daopt386: changes to getnextinstruction/getlastinstruction so they
  1728. ignore labels who have is_addr set
  1729. + daopt386/csopt386: remove loads of registers which are overwritten
  1730. before their contents are used (especially usefull for removing superfluous
  1731. maybe_loadself outputs and push/pops transformed by below optimization
  1732. + popt386: transform pop/pop/pop/.../push/push/push to sequences of
  1733. 'movl x(%esp),%reg' (only active when compiling a go32v2 compiler
  1734. currently because I don't know whether it's safe to do this under Win32/
  1735. Linux (because of problems we had when using esp as frame pointer on
  1736. those os'es)
  1737. Revision 1.3 2000/07/14 05:11:48 michael
  1738. + Patch to 1.1
  1739. Revision 1.2 2000/07/13 11:32:39 michael
  1740. + removed logs
  1741. }