2
0

csopt386.pas 71 KB

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