popt386.pas 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl and Jonas Maebe
  4. This unit contains the peephole optimizer.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit POpt386;
  19. {$ifdef newOptimizations}
  20. {$define foropt}
  21. {$define replacereg}
  22. {$define arithopt}
  23. {$define foldarithops}
  24. {$endif newOptimizations}
  25. Interface
  26. Uses Aasm;
  27. Procedure PeepHoleOptPass1(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  28. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  29. Implementation
  30. Uses
  31. globtype,systems,
  32. globals,verbose,hcodegen,
  33. {$ifdef finaldestdebug}
  34. cobjects,
  35. {$endif finaldestdebug}
  36. cpubase,cpuasm,DAOpt386,tgeni386;
  37. Function RegUsedAfterInstruction(Reg: TRegister; p: Pai; Var UsedRegs: TRegSet): Boolean;
  38. Begin
  39. reg := reg32(reg);
  40. UpdateUsedRegs(UsedRegs, Pai(p^.Next));
  41. RegUsedAfterInstruction :=
  42. (Reg in UsedRegs) and
  43. (not(getNextInstruction(p,p)) or
  44. not(regLoadedWithNewValue(reg,false,p)));
  45. End;
  46. function doFpuLoadStoreOpt(asmL: paasmoutput; var p: pai): boolean;
  47. { returns true if a "continue" should be done after this optimization }
  48. var hp1, hp2: pai;
  49. begin
  50. doFpuLoadStoreOpt := false;
  51. if (paicpu(p)^.oper[0].typ = top_ref) and
  52. getNextInstruction(p, hp1) and
  53. (hp1^.typ = ait_instruction) and
  54. (((paicpu(hp1)^.opcode = A_FLD) and
  55. (paicpu(p)^.opcode = A_FSTP)) or
  56. ((paicpu(p)^.opcode = A_FISTP) and
  57. (paicpu(hp1)^.opcode = A_FILD))) and
  58. (paicpu(hp1)^.oper[0].typ = top_ref) and
  59. (paicpu(hp1)^.opsize = Paicpu(p)^.opsize) and
  60. refsEqual(paicpu(p)^.oper[0].ref^, paicpu(hp1)^.oper[0].ref^) then
  61. begin
  62. if getNextInstruction(hp1, hp2) and
  63. (hp2^.typ = ait_instruction) and
  64. ((paicpu(hp2)^.opcode = A_LEAVE) or
  65. (paicpu(hp2)^.opcode = A_RET)) and
  66. (paicpu(p)^.oper[0].ref^.Base = procinfo^.FramePointer) and
  67. (paicpu(p)^.oper[0].ref^.Offset >= procinfo^.Return_Offset) and
  68. (paicpu(p)^.oper[0].ref^.Index = R_NO) then
  69. begin
  70. asmL^.remove(p);
  71. asmL^.remove(hp1);
  72. dispose(p, done);
  73. dispose(hp1, done);
  74. p := hp2;
  75. removeLastDeallocForFuncRes(asmL, p);
  76. doFPULoadStoreOpt := true;
  77. end
  78. else
  79. { fst can't store an extended value! }
  80. if (paicpu(p)^.opsize <> S_FX) and
  81. (paicpu(p)^.opsize <> S_IQ) then
  82. begin
  83. if (paicpu(p)^.opcode = A_FSTP) then
  84. paicpu(p)^.opcode := A_FST
  85. else Paicpu(p)^.opcode := A_FIST;
  86. asmL^.remove(hp1);
  87. dispose(hp1, done)
  88. end
  89. end;
  90. end;
  91. Procedure PeepHoleOptPass1(Asml: PAasmOutput; BlockStart, BlockEnd: Pai);
  92. {First pass of peepholeoptimizations}
  93. Var
  94. l : longint;
  95. p,hp1,hp2 : pai;
  96. {$ifdef foropt}
  97. hp3,hp4: pai;
  98. {$endif foropt}
  99. TmpBool1, TmpBool2: Boolean;
  100. TmpRef: TReference;
  101. UsedRegs, TmpUsedRegs: TRegSet;
  102. Function SkipLabels(hp: Pai; var hp2: pai): boolean;
  103. {skips all labels and returns the next "real" instruction}
  104. Begin
  105. While assigned(hp^.next) and
  106. (pai(hp^.next)^.typ In SkipInstr + [ait_label,ait_align]) Do
  107. hp := pai(hp^.next);
  108. If assigned(hp^.next) Then
  109. Begin
  110. SkipLabels := True;
  111. hp2 := pai(hp^.next)
  112. End
  113. Else
  114. Begin
  115. hp2 := hp;
  116. SkipLabels := False
  117. End;
  118. End;
  119. Procedure GetFinalDestination(AsmL: PAAsmOutput; hp: paicpu);
  120. {traces sucessive jumps to their final destination and sets it, e.g.
  121. je l1 je l3
  122. <code> <code>
  123. l1: becomes l1:
  124. je l2 je l3
  125. <code> <code>
  126. l2: l2:
  127. jmp l3 jmp l3}
  128. Var p1, p2: pai;
  129. l: pasmlabel;
  130. Function FindAnyLabel(hp: pai; var l: pasmlabel): Boolean;
  131. Begin
  132. FindAnyLabel := false;
  133. While assigned(hp^.next) and
  134. (pai(hp^.next)^.typ In (SkipInstr+[ait_align])) Do
  135. hp := pai(hp^.next);
  136. If assigned(hp^.next) and
  137. (pai(hp^.next)^.typ = ait_label) Then
  138. Begin
  139. FindAnyLabel := true;
  140. l := pai_label(hp^.next)^.l;
  141. End
  142. End;
  143. Begin
  144. If (pasmlabel(hp^.oper[0].sym)^.labelnr >= LoLab) and
  145. (pasmlabel(hp^.oper[0].sym)^.labelnr <= HiLab) and {range check, a jump can go past an assembler block!}
  146. Assigned(LTable^[pasmlabel(hp^.oper[0].sym)^.labelnr-LoLab].PaiObj) Then
  147. Begin
  148. p1 := LTable^[pasmlabel(hp^.oper[0].sym)^.labelnr-LoLab].PaiObj; {the jump's destination}
  149. SkipLabels(p1,p1);
  150. If (pai(p1)^.typ = ait_instruction) and
  151. (paicpu(p1)^.is_jmp) Then
  152. If { the next instruction after the label where the jump hp arrives}
  153. { is unconditional or of the same type as hp, so continue }
  154. (paicpu(p1)^.condition in [C_None,hp^.condition]) or
  155. { the next instruction after the label where the jump hp arrives}
  156. { is the opposite of hp (so this one is never taken), but after }
  157. { that one there is a branch that will be taken, so perform a }
  158. { little hack: set p1 equal to this instruction (that's what the}
  159. { last SkipLabels is for, only works with short bool evaluation)}
  160. ((paicpu(p1)^.condition = inverse_cond[hp^.condition]) and
  161. SkipLabels(p1,p2) and
  162. (p2^.typ = ait_instruction) and
  163. (paicpu(p2)^.is_jmp) and
  164. (paicpu(p2)^.condition in [C_None,hp^.condition]) and
  165. SkipLabels(p1,p1)) Then
  166. Begin
  167. GetFinalDestination(asml, paicpu(p1));
  168. Dec(pasmlabel(hp^.oper[0].sym)^.refs);
  169. hp^.oper[0].sym:=paicpu(p1)^.oper[0].sym;
  170. inc(pasmlabel(hp^.oper[0].sym)^.refs);
  171. End
  172. Else
  173. If (paicpu(p1)^.condition = inverse_cond[hp^.condition]) then
  174. if not FindAnyLabel(p1,l) then
  175. begin
  176. {$ifdef finaldestdebug}
  177. insertllitem(asml,p1,p1^.next,new(pai_asm_comment,init(
  178. strpnew('previous label inserted'))));
  179. {$endif finaldestdebug}
  180. getlabel(l);
  181. insertllitem(asml,p1,p1^.next,new(pai_label,init(l)));
  182. dec(pasmlabel(paicpu(hp)^.oper[0].sym)^.refs);
  183. hp^.oper[0].sym := l;
  184. inc(l^.refs);
  185. { this won't work, since the new label isn't in the labeltable }
  186. { so it will fail the rangecheck. Labeltable should become a }
  187. { hashtable to support this: }
  188. { GetFinalDestination(asml, hp); }
  189. end
  190. else
  191. begin
  192. {$ifdef finaldestdebug}
  193. insertllitem(asml,p1,p1^.next,new(pai_asm_comment,init(
  194. strpnew('next label reused'))));
  195. {$endif finaldestdebug}
  196. inc(l^.refs);
  197. hp^.oper[0].sym := l;
  198. GetFinalDestination(asml, hp);
  199. end;
  200. End;
  201. End;
  202. Function DoSubAddOpt(var p: Pai): Boolean;
  203. Begin
  204. DoSubAddOpt := False;
  205. If GetLastInstruction(p, hp1) And
  206. (hp1^.typ = ait_instruction) And
  207. (Paicpu(hp1)^.opsize = Paicpu(p)^.opsize) then
  208. Case Paicpu(hp1)^.opcode Of
  209. A_DEC:
  210. If (Paicpu(hp1)^.oper[0].typ = top_reg) And
  211. (Paicpu(hp1)^.oper[0].reg = Paicpu(p)^.oper[1].reg) Then
  212. Begin
  213. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val+1);
  214. AsmL^.Remove(hp1);
  215. Dispose(hp1, Done)
  216. End;
  217. A_SUB:
  218. If (Paicpu(hp1)^.oper[0].typ = top_const) And
  219. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  220. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg) Then
  221. Begin
  222. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val+Paicpu(hp1)^.oper[0].val);
  223. AsmL^.Remove(hp1);
  224. Dispose(hp1, Done)
  225. End;
  226. A_ADD:
  227. If (Paicpu(hp1)^.oper[0].typ = top_const) And
  228. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  229. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg) Then
  230. Begin
  231. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val-Paicpu(hp1)^.oper[0].val);
  232. AsmL^.Remove(hp1);
  233. Dispose(hp1, Done);
  234. If (Paicpu(p)^.oper[0].val = 0) Then
  235. Begin
  236. hp1 := Pai(p^.next);
  237. AsmL^.Remove(p);
  238. Dispose(p, Done);
  239. If Not GetLastInstruction(hp1, p) Then
  240. p := hp1;
  241. DoSubAddOpt := True;
  242. End
  243. End;
  244. End;
  245. End;
  246. Begin
  247. P := BlockStart;
  248. UsedRegs := [];
  249. While (P <> BlockEnd) Do
  250. Begin
  251. UpDateUsedRegs(UsedRegs, Pai(p^.next));
  252. Case P^.Typ Of
  253. ait_instruction:
  254. Begin
  255. { Handle Jmp Optimizations }
  256. if Paicpu(p)^.is_jmp then
  257. begin
  258. {the following if-block removes all code between a jmp and the next label,
  259. because it can never be executed}
  260. If (paicpu(p)^.opcode = A_JMP) Then
  261. Begin
  262. While GetNextInstruction(p, hp1) and
  263. ((hp1^.typ <> ait_label) or
  264. { skip unused labels, they're not referenced anywhere }
  265. Not(Pai_Label(hp1)^.l^.is_used)) Do
  266. If not(hp1^.typ in ([ait_label,ait_align]+skipinstr)) Then
  267. Begin
  268. AsmL^.Remove(hp1);
  269. Dispose(hp1, done);
  270. End;
  271. End;
  272. If GetNextInstruction(p, hp1) then
  273. Begin
  274. if FindLabel(pasmlabel(paicpu(p)^.oper[0].sym), hp1) then
  275. Begin
  276. hp2:=pai(hp1^.next);
  277. asml^.remove(p);
  278. dispose(p,done);
  279. p:=hp2;
  280. continue;
  281. end
  282. Else
  283. Begin
  284. if hp1^.typ = ait_label then
  285. SkipLabels(hp1,hp1);
  286. If (pai(hp1)^.typ=ait_instruction) and
  287. (paicpu(hp1)^.opcode=A_JMP) and
  288. GetNextInstruction(hp1, hp2) And
  289. FindLabel(PAsmLabel(paicpu(p)^.oper[0].sym), hp2)
  290. Then
  291. Begin
  292. if paicpu(p)^.opcode=A_Jcc then
  293. paicpu(p)^.condition:=inverse_cond[paicpu(p)^.condition]
  294. else
  295. begin
  296. If (LabDif <> 0) Then
  297. GetFinalDestination(asml, paicpu(p));
  298. p:=pai(p^.next);
  299. continue;
  300. end;
  301. Dec(pai_label(hp2)^.l^.refs);
  302. paicpu(p)^.oper[0].sym:=paicpu(hp1)^.oper[0].sym;
  303. Inc(paicpu(p)^.oper[0].sym^.refs);
  304. asml^.remove(hp1);
  305. dispose(hp1,done);
  306. If (LabDif <> 0) Then
  307. GetFinalDestination(asml, paicpu(p));
  308. end
  309. else
  310. If (LabDif <> 0) Then
  311. GetFinalDestination(asml, paicpu(p));
  312. end;
  313. end;
  314. end
  315. else
  316. { All other optimizes }
  317. begin
  318. For l := 0 to 2 Do
  319. If (Paicpu(p)^.oper[l].typ = top_ref) Then
  320. With Paicpu(p)^.oper[l].ref^ Do
  321. Begin
  322. If (base = R_NO) And
  323. (index <> R_NO) And
  324. (scalefactor in [0,1])
  325. Then
  326. Begin
  327. base := index;
  328. index := R_NO
  329. End
  330. End;
  331. Case Paicpu(p)^.opcode Of
  332. A_AND:
  333. Begin
  334. If (Paicpu(p)^.oper[0].typ = top_const) And
  335. (Paicpu(p)^.oper[1].typ = top_reg) And
  336. GetNextInstruction(p, hp1) And
  337. (Pai(hp1)^.typ = ait_instruction) And
  338. (Paicpu(hp1)^.opcode = A_AND) And
  339. (Paicpu(hp1)^.oper[0].typ = top_const) And
  340. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  341. (Paicpu(hp1)^.oper[1].reg = Paicpu(hp1)^.oper[1].reg)
  342. Then
  343. {change "and const1, reg; and const2, reg" to "and (const1 and const2), reg"}
  344. Begin
  345. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val And Paicpu(hp1)^.oper[0].val);
  346. AsmL^.Remove(hp1);
  347. Dispose(hp1, Done)
  348. End
  349. Else
  350. {change "and x, reg; jxx" to "test x, reg", if reg is deallocated before the
  351. jump, but only if it's a conditional jump (PFV) }
  352. If (Paicpu(p)^.oper[1].typ = top_reg) And
  353. GetNextInstruction(p, hp1) And
  354. (hp1^.typ = ait_instruction) And
  355. (Paicpu(hp1)^.is_jmp) and
  356. (Paicpu(hp1)^.opcode<>A_JMP) and
  357. Not(Paicpu(p)^.oper[1].reg in UsedRegs) Then
  358. Paicpu(p)^.opcode := A_TEST;
  359. End;
  360. A_CMP:
  361. Begin
  362. If (Paicpu(p)^.oper[0].typ = top_const) And
  363. (Paicpu(p)^.oper[1].typ in [top_reg,top_ref]) And
  364. (Paicpu(p)^.oper[0].val = 0) Then
  365. {$ifdef foropt}
  366. If GetNextInstruction(p, hp1) And
  367. (hp1^.typ = ait_instruction) And
  368. (Paicpu(hp1)^.is_jmp) and
  369. (paicpu(hp1)^.opcode=A_Jcc) and
  370. (paicpu(hp1)^.condition in [C_LE,C_BE]) and
  371. GetNextInstruction(hp1,hp2) and
  372. (hp2^.typ = ait_instruction) and
  373. (Paicpu(hp2)^.opcode = A_DEC) And
  374. OpsEqual(Paicpu(hp2)^.oper[0],Paicpu(p)^.oper[1]) And
  375. GetNextInstruction(hp2, hp3) And
  376. (hp3^.typ = ait_instruction) and
  377. (Paicpu(hp3)^.is_jmp) and
  378. (Paicpu(hp3)^.opcode = A_JMP) And
  379. GetNextInstruction(hp3, hp4) And
  380. FindLabel(PAsmLabel(paicpu(hp1)^.oper[0].sym),hp4)
  381. Then
  382. Begin
  383. Paicpu(hp2)^.Opcode := A_SUB;
  384. Paicpu(hp2)^.Loadoper(1,Paicpu(hp2)^.oper[0]);
  385. Paicpu(hp2)^.LoadConst(0,1);
  386. Paicpu(hp2)^.ops:=2;
  387. Paicpu(hp3)^.Opcode := A_Jcc;
  388. Case paicpu(hp1)^.condition of
  389. C_LE: Paicpu(hp3)^.condition := C_GE;
  390. C_BE: Paicpu(hp3)^.condition := C_AE;
  391. End;
  392. AsmL^.Remove(p);
  393. AsmL^.Remove(hp1);
  394. Dispose(p, Done);
  395. Dispose(hp1, Done);
  396. p := hp2;
  397. continue;
  398. End
  399. Else
  400. {$endif foropt}
  401. {change "cmp $0, %reg" to "test %reg, %reg"}
  402. If (Paicpu(p)^.oper[1].typ = top_reg) Then
  403. Begin
  404. Paicpu(p)^.opcode := A_TEST;
  405. Paicpu(p)^.loadreg(0,Paicpu(p)^.oper[1].reg);
  406. End;
  407. End;
  408. A_FLD:
  409. Begin
  410. If (Paicpu(p)^.oper[0].typ = top_reg) And
  411. GetNextInstruction(p, hp1) And
  412. (hp1^.typ = Ait_Instruction) And
  413. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  414. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  415. (Paicpu(hp1)^.oper[0].reg = R_ST) And
  416. (Paicpu(hp1)^.oper[1].reg = R_ST1) Then
  417. { change to
  418. fld reg fxxx reg,st
  419. fxxxp st, st1 (hp1)
  420. Remark: non commutative operations must be reversed!
  421. }
  422. begin
  423. Case Paicpu(hp1)^.opcode Of
  424. A_FMULP,A_FADDP,
  425. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  426. begin
  427. Case Paicpu(hp1)^.opcode Of
  428. A_FADDP: Paicpu(hp1)^.opcode := A_FADD;
  429. A_FMULP: Paicpu(hp1)^.opcode := A_FMUL;
  430. A_FSUBP: Paicpu(hp1)^.opcode := A_FSUBR;
  431. A_FSUBRP: Paicpu(hp1)^.opcode := A_FSUB;
  432. A_FDIVP: Paicpu(hp1)^.opcode := A_FDIVR;
  433. A_FDIVRP: Paicpu(hp1)^.opcode := A_FDIV;
  434. End;
  435. Paicpu(hp1)^.oper[0].reg := Paicpu(p)^.oper[0].reg;
  436. Paicpu(hp1)^.oper[1].reg := R_ST;
  437. AsmL^.Remove(p);
  438. Dispose(p, Done);
  439. p := hp1;
  440. Continue;
  441. end;
  442. end;
  443. end
  444. else
  445. If (Paicpu(p)^.oper[0].typ = top_ref) And
  446. GetNextInstruction(p, hp2) And
  447. (hp2^.typ = Ait_Instruction) And
  448. (Paicpu(hp2)^.oper[0].typ = top_reg) And
  449. (Paicpu(hp2)^.oper[1].typ = top_reg) And
  450. (Paicpu(p)^.opsize in [S_FS, S_FL]) And
  451. (Paicpu(hp2)^.oper[0].reg = R_ST) And
  452. (Paicpu(hp2)^.oper[1].reg = R_ST1) Then
  453. If GetLastInstruction(p, hp1) And
  454. (hp1^.typ = Ait_Instruction) And
  455. ((Paicpu(hp1)^.opcode = A_FLD) Or
  456. (Paicpu(hp1)^.opcode = A_FST)) And
  457. (Paicpu(hp1)^.opsize = Paicpu(p)^.opsize) And
  458. (Paicpu(hp1)^.oper[0].typ = top_ref) And
  459. RefsEqual(Paicpu(p)^.oper[0].ref^, Paicpu(hp1)^.oper[0].ref^) Then
  460. If ((Paicpu(hp2)^.opcode = A_FMULP) Or
  461. (Paicpu(hp2)^.opcode = A_FADDP)) Then
  462. { change to
  463. fld/fst mem1 (hp1) fld/fst mem1
  464. fld mem1 (p) fadd/
  465. faddp/ fmul st, st
  466. fmulp st, st1 (hp2) }
  467. Begin
  468. AsmL^.Remove(p);
  469. Dispose(p, Done);
  470. p := hp1;
  471. If (Paicpu(hp2)^.opcode = A_FADDP) Then
  472. Paicpu(hp2)^.opcode := A_FADD
  473. Else
  474. Paicpu(hp2)^.opcode := A_FMUL;
  475. Paicpu(hp2)^.oper[1].reg := R_ST;
  476. End
  477. Else
  478. { change to
  479. fld/fst mem1 (hp1) fld/fst mem1
  480. fld mem1 (p) fld st}
  481. Begin
  482. Paicpu(p)^.changeopsize(S_FL);
  483. Paicpu(p)^.loadreg(0,R_ST);
  484. End
  485. Else
  486. Begin
  487. Case Paicpu(hp2)^.opcode Of
  488. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  489. { change to
  490. fld/fst mem1 (hp1) fld/fst mem1
  491. fld mem2 (p) fxxx mem2
  492. fxxxp st, st1 (hp2) }
  493. Begin
  494. Case Paicpu(hp2)^.opcode Of
  495. A_FADDP: Paicpu(p)^.opcode := A_FADD;
  496. A_FMULP: Paicpu(p)^.opcode := A_FMUL;
  497. A_FSUBP: Paicpu(p)^.opcode := A_FSUBR;
  498. A_FSUBRP: Paicpu(p)^.opcode := A_FSUB;
  499. A_FDIVP: Paicpu(p)^.opcode := A_FDIVR;
  500. A_FDIVRP: Paicpu(p)^.opcode := A_FDIV;
  501. End;
  502. AsmL^.Remove(hp2);
  503. Dispose(hp2, Done)
  504. End
  505. End
  506. End
  507. End;
  508. A_FSTP,A_FISTP:
  509. if doFpuLoadStoreOpt(asmL,p) then
  510. continue;
  511. A_IMUL:
  512. {changes certain "imul const, %reg"'s to lea sequences}
  513. Begin
  514. If (Paicpu(p)^.oper[0].typ = Top_Const) And
  515. (Paicpu(p)^.oper[1].typ = Top_Reg) And
  516. (Paicpu(p)^.opsize = S_L) Then
  517. If (Paicpu(p)^.oper[0].val = 1) Then
  518. If (Paicpu(p)^.oper[2].typ = Top_None) Then
  519. {remove "imul $1, reg"}
  520. Begin
  521. hp1 := Pai(p^.Next);
  522. AsmL^.Remove(p);
  523. Dispose(p, Done);
  524. p := hp1;
  525. Continue;
  526. End
  527. Else
  528. {change "imul $1, reg1, reg2" to "mov reg1, reg2"}
  529. Begin
  530. hp1 := New(Paicpu, Op_Reg_Reg(A_MOV, S_L, Paicpu(p)^.oper[1].reg,Paicpu(p)^.oper[2].reg));
  531. InsertLLItem(AsmL, p^.previous, p^.next, hp1);
  532. Dispose(p, Done);
  533. p := hp1;
  534. End
  535. Else If
  536. ((Paicpu(p)^.oper[2].typ = Top_Reg) or
  537. (Paicpu(p)^.oper[2].typ = Top_None)) And
  538. (aktoptprocessor < ClassP6) And
  539. (Paicpu(p)^.oper[0].val <= 12) And
  540. Not(CS_LittleSize in aktglobalswitches) And
  541. (Not(GetNextInstruction(p, hp1)) Or
  542. {GetNextInstruction(p, hp1) And}
  543. Not((Pai(hp1)^.typ = ait_instruction) And
  544. ((paicpu(hp1)^.opcode=A_Jcc) and
  545. (paicpu(hp1)^.condition in [C_O,C_NO]))))
  546. Then
  547. Begin
  548. Reset_reference(tmpref);
  549. Case Paicpu(p)^.oper[0].val Of
  550. 3: Begin
  551. {imul 3, reg1, reg2 to
  552. lea (reg1,reg1,2), reg2
  553. imul 3, reg1 to
  554. lea (reg1,reg1,2), reg1}
  555. TmpRef.base := Paicpu(p)^.oper[1].reg;
  556. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  557. TmpRef.ScaleFactor := 2;
  558. If (Paicpu(p)^.oper[2].typ = Top_None) Then
  559. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg))
  560. Else
  561. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  562. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  563. Dispose(p, Done);
  564. p := hp1;
  565. End;
  566. 5: Begin
  567. {imul 5, reg1, reg2 to
  568. lea (reg1,reg1,4), reg2
  569. imul 5, reg1 to
  570. lea (reg1,reg1,4), reg1}
  571. TmpRef.base := Paicpu(p)^.oper[1].reg;
  572. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  573. TmpRef.ScaleFactor := 4;
  574. If (Paicpu(p)^.oper[2].typ = Top_None) Then
  575. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg))
  576. Else
  577. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  578. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  579. Dispose(p, Done);
  580. p := hp1;
  581. End;
  582. 6: Begin
  583. {imul 6, reg1, reg2 to
  584. lea (,reg1,2), reg2
  585. lea (reg2,reg1,4), reg2
  586. imul 6, reg1 to
  587. lea (reg1,reg1,2), reg1
  588. add reg1, reg1}
  589. If (aktoptprocessor <= Class386)
  590. Then
  591. Begin
  592. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  593. If (Paicpu(p)^.oper[2].typ = Top_Reg)
  594. Then
  595. Begin
  596. TmpRef.base := Paicpu(p)^.oper[2].reg;
  597. TmpRef.ScaleFactor := 4;
  598. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  599. End
  600. Else
  601. Begin
  602. hp1 := New(Paicpu, op_reg_reg(A_ADD, S_L,
  603. Paicpu(p)^.oper[1].reg,Paicpu(p)^.oper[1].reg));
  604. End;
  605. InsertLLItem(AsmL,p, p^.next, hp1);
  606. Reset_reference(tmpref);
  607. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  608. TmpRef.ScaleFactor := 2;
  609. If (Paicpu(p)^.oper[2].typ = Top_Reg)
  610. Then
  611. Begin
  612. TmpRef.base := R_NO;
  613. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef),
  614. Paicpu(p)^.oper[2].reg));
  615. End
  616. Else
  617. Begin
  618. TmpRef.base := Paicpu(p)^.oper[1].reg;
  619. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  620. End;
  621. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  622. Dispose(p, Done);
  623. p := Pai(hp1^.next);
  624. End
  625. End;
  626. 9: Begin
  627. {imul 9, reg1, reg2 to
  628. lea (reg1,reg1,8), reg2
  629. imul 9, reg1 to
  630. lea (reg1,reg1,8), reg1}
  631. TmpRef.base := Paicpu(p)^.oper[1].reg;
  632. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  633. TmpRef.ScaleFactor := 8;
  634. If (Paicpu(p)^.oper[2].typ = Top_None) Then
  635. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg))
  636. Else
  637. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  638. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  639. Dispose(p, Done);
  640. p := hp1;
  641. End;
  642. 10: Begin
  643. {imul 10, reg1, reg2 to
  644. lea (reg1,reg1,4), reg2
  645. add reg2, reg2
  646. imul 10, reg1 to
  647. lea (reg1,reg1,4), reg1
  648. add reg1, reg1}
  649. If (aktoptprocessor <= Class386) Then
  650. Begin
  651. If (Paicpu(p)^.oper[2].typ = Top_Reg) Then
  652. hp1 := New(Paicpu, op_reg_reg(A_ADD, S_L,
  653. Paicpu(p)^.oper[2].reg,Paicpu(p)^.oper[2].reg))
  654. Else
  655. hp1 := New(Paicpu, op_reg_reg(A_ADD, S_L,
  656. Paicpu(p)^.oper[1].reg,Paicpu(p)^.oper[1].reg));
  657. InsertLLItem(AsmL,p, p^.next, hp1);
  658. TmpRef.base := Paicpu(p)^.oper[1].reg;
  659. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  660. TmpRef.ScaleFactor := 4;
  661. If (Paicpu(p)^.oper[2].typ = Top_Reg)
  662. Then
  663. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg))
  664. Else
  665. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  666. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  667. Dispose(p, Done);
  668. p := Pai(hp1^.next);
  669. End
  670. End;
  671. 12: Begin
  672. {imul 12, reg1, reg2 to
  673. lea (,reg1,4), reg2
  674. lea (,reg1,8) reg2
  675. imul 12, reg1 to
  676. lea (reg1,reg1,2), reg1
  677. lea (,reg1,4), reg1}
  678. If (aktoptprocessor <= Class386)
  679. Then
  680. Begin
  681. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  682. If (Paicpu(p)^.oper[2].typ = Top_Reg) Then
  683. Begin
  684. TmpRef.base := Paicpu(p)^.oper[2].reg;
  685. TmpRef.ScaleFactor := 8;
  686. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  687. End
  688. Else
  689. Begin
  690. TmpRef.base := R_NO;
  691. TmpRef.ScaleFactor := 4;
  692. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  693. End;
  694. InsertLLItem(AsmL,p, p^.next, hp1);
  695. Reset_reference(tmpref);
  696. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  697. If (Paicpu(p)^.oper[2].typ = Top_Reg) Then
  698. Begin
  699. TmpRef.base := R_NO;
  700. TmpRef.ScaleFactor := 4;
  701. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  702. End
  703. Else
  704. Begin
  705. TmpRef.base := Paicpu(p)^.oper[1].reg;
  706. TmpRef.ScaleFactor := 2;
  707. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  708. End;
  709. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  710. Dispose(p, Done);
  711. p := Pai(hp1^.next);
  712. End
  713. End
  714. End;
  715. End;
  716. End;
  717. A_LEA:
  718. Begin
  719. {removes seg register prefixes from LEA operations, as they
  720. don't do anything}
  721. Paicpu(p)^.oper[0].ref^.Segment := R_NO;
  722. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  723. If (Paicpu(p)^.oper[0].ref^.Base In [R_EAX..R_EDI]) And
  724. (Paicpu(p)^.oper[0].ref^.Index = R_NO) And
  725. {$ifndef newOptimizations}
  726. (Paicpu(p)^.oper[0].ref^.Offset = 0) And
  727. {$endif newOptimizations}
  728. (Not(Assigned(Paicpu(p)^.oper[0].ref^.Symbol))) Then
  729. If (Paicpu(p)^.oper[0].ref^.Base <> Paicpu(p)^.oper[1].reg)
  730. {$ifdef newOptimizations}
  731. and (Paicpu(p)^.oper[0].ref^.Offset = 0)
  732. {$endif newOptimizations}
  733. Then
  734. Begin
  735. hp1 := New(Paicpu, op_reg_reg(A_MOV, S_L,Paicpu(p)^.oper[0].ref^.Base,
  736. Paicpu(p)^.oper[1].reg));
  737. InsertLLItem(AsmL,p^.previous,p^.next, hp1);
  738. Dispose(p, Done);
  739. p := hp1;
  740. Continue;
  741. End
  742. Else
  743. {$ifdef newOptimizations}
  744. if (Paicpu(p)^.oper[0].ref^.Offset = 0) then
  745. {$endif newOptimizations}
  746. Begin
  747. hp1 := Pai(p^.Next);
  748. AsmL^.Remove(p);
  749. Dispose(p, Done);
  750. p := hp1;
  751. Continue;
  752. End
  753. {$ifdef newOptimizations}
  754. else
  755. with Paicpu(p)^.oper[0].ref^ do
  756. if (Base = Paicpu(p)^.oper[1].reg) then
  757. begin
  758. l := offset+offsetfixup;
  759. case l of
  760. 1,-1:
  761. begin
  762. if l = 1 then
  763. paicpu(p)^.opcode := A_INC
  764. else paicpu(p)^.opcode := A_DEC;
  765. paicpu(p)^.loadreg(0,Paicpu(p)^.oper[1].reg);
  766. paicpu(p)^.ops := 1;
  767. end;
  768. else
  769. begin
  770. paicpu(p)^.opcode := A_ADD;
  771. paicpu(p)^.loadconst(0,offset+offsetfixup);
  772. end;
  773. end;
  774. end;
  775. {$endif newOptimizations}
  776. End;
  777. A_MOV:
  778. Begin
  779. TmpUsedRegs := UsedRegs;
  780. If (Paicpu(p)^.oper[1].typ = top_reg) And
  781. (Paicpu(p)^.oper[1].reg In [R_EAX, R_EBX, R_EDX, R_EDI]) And
  782. GetNextInstruction(p, hp1) And
  783. (Pai(hp1)^.typ = ait_instruction) And
  784. (Paicpu(hp1)^.opcode = A_MOV) And
  785. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  786. (Paicpu(hp1)^.oper[0].reg = Paicpu(p)^.oper[1].reg)
  787. Then
  788. {we have "mov x, %treg; mov %treg, y}
  789. If not(RegUsedAfterInstruction(Paicpu(p)^.oper[1].reg, hp1, TmpUsedRegs)) then
  790. {we've got "mov x, %treg; mov %treg, y; with %treg is not used after }
  791. Case Paicpu(p)^.oper[0].typ Of
  792. top_reg:
  793. Begin
  794. { change "mov %reg, %treg; mov %treg, y"
  795. to "mov %reg, y" }
  796. Paicpu(hp1)^.LoadOper(0,Paicpu(p)^.oper[0]);
  797. AsmL^.Remove(p);
  798. Dispose(p, Done);
  799. p := hp1;
  800. continue;
  801. End;
  802. top_ref:
  803. If (Paicpu(hp1)^.oper[1].typ = top_reg) Then
  804. Begin
  805. { change "mov mem, %treg; mov %treg, %reg"
  806. to "mov mem, %reg" }
  807. Paicpu(p)^.Loadoper(1,Paicpu(hp1)^.oper[1]);
  808. AsmL^.Remove(hp1);
  809. Dispose(hp1, Done);
  810. continue;
  811. End;
  812. End
  813. Else
  814. Else
  815. {Change "mov %reg1, %reg2; xxx %reg2, ???" to
  816. "mov %reg1, %reg2; xxx %reg1, ???" to avoid a write/read
  817. penalty}
  818. If (Paicpu(p)^.oper[0].typ = top_reg) And
  819. (Paicpu(p)^.oper[1].typ = top_reg) And
  820. GetNextInstruction(p,hp1) And
  821. (Pai(hp1)^.typ = ait_instruction) And
  822. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  823. (Paicpu(hp1)^.oper[0].reg = Paicpu(p)^.oper[1].reg)
  824. Then
  825. {we have "mov %reg1, %reg2; XXX %reg2, ???"}
  826. Begin
  827. If ((Paicpu(hp1)^.opcode = A_OR) Or
  828. (Paicpu(hp1)^.opcode = A_TEST)) And
  829. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  830. (Paicpu(hp1)^.oper[0].reg = Paicpu(hp1)^.oper[1].reg)
  831. Then
  832. {we have "mov %reg1, %reg2; test/or %reg2, %reg2"}
  833. Begin
  834. TmpUsedRegs := UsedRegs;
  835. { reg1 will be used after the first instruction, }
  836. { so update the allocation info }
  837. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,p,hp1);
  838. If GetNextInstruction(hp1, hp2) And
  839. (hp2^.typ = ait_instruction) And
  840. paicpu(hp2)^.is_jmp and
  841. Not(RegUsedAfterInstruction(Paicpu(hp1)^.oper[0].reg, hp1, TmpUsedRegs))
  842. Then
  843. {change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  844. "test %reg1, %reg1; jxx"}
  845. Begin
  846. Paicpu(hp1)^.Loadoper(0,Paicpu(p)^.oper[0]);
  847. Paicpu(hp1)^.Loadoper(1,Paicpu(p)^.oper[0]);
  848. AsmL^.Remove(p);
  849. Dispose(p, done);
  850. p := hp1;
  851. continue
  852. End
  853. Else
  854. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  855. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  856. Begin
  857. Paicpu(hp1)^.Loadoper(0,Paicpu(p)^.oper[0]);
  858. Paicpu(hp1)^.Loadoper(1,Paicpu(p)^.oper[0]);
  859. End;
  860. End
  861. { Else
  862. If (Paicpu(p^.next)^.opcode
  863. In [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  864. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  865. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  866. End
  867. Else
  868. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  869. x >= RetOffset) as it doesn't do anything (it writes either to a
  870. parameter or to the temporary storage room for the function
  871. result)}
  872. If GetNextInstruction(p, hp1) And
  873. (Pai(hp1)^.typ = ait_instruction)
  874. Then
  875. If ((Paicpu(hp1)^.opcode = A_LEAVE) Or
  876. (Paicpu(hp1)^.opcode = A_RET)) And
  877. (Paicpu(p)^.oper[1].typ = top_ref) And
  878. (Paicpu(p)^.oper[1].ref^.base = procinfo^.FramePointer) And
  879. (Paicpu(p)^.oper[1].ref^.offset >= procinfo^.Return_Offset) And
  880. (Paicpu(p)^.oper[1].ref^.index = R_NO) And
  881. (Paicpu(p)^.oper[0].typ = top_reg)
  882. Then
  883. Begin
  884. AsmL^.Remove(p);
  885. Dispose(p, done);
  886. p := hp1;
  887. RemoveLastDeallocForFuncRes(asmL,p);
  888. End
  889. Else
  890. If (Paicpu(p)^.oper[0].typ = top_reg) And
  891. (Paicpu(p)^.oper[1].typ = top_ref) And
  892. (Paicpu(p)^.opsize = Paicpu(hp1)^.opsize) And
  893. (Paicpu(hp1)^.opcode = A_CMP) And
  894. (Paicpu(hp1)^.oper[1].typ = top_ref) And
  895. RefsEqual(Paicpu(p)^.oper[1].ref^, Paicpu(hp1)^.oper[1].ref^) Then
  896. {change "mov reg1, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  897. begin
  898. Paicpu(hp1)^.loadreg(1,Paicpu(p)^.oper[0].reg);
  899. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,p,hp1);
  900. end;
  901. { Next instruction is also a MOV ? }
  902. If GetNextInstruction(p, hp1) And
  903. (pai(hp1)^.typ = ait_instruction) and
  904. (Paicpu(hp1)^.opcode = A_MOV) and
  905. (Paicpu(hp1)^.opsize = Paicpu(p)^.opsize)
  906. Then
  907. Begin
  908. If (Paicpu(hp1)^.oper[0].typ = Paicpu(p)^.oper[1].typ) and
  909. (Paicpu(hp1)^.oper[1].typ = Paicpu(p)^.oper[0].typ)
  910. Then
  911. {mov reg1, mem1 or mov mem1, reg1
  912. mov mem2, reg2 mov reg2, mem2}
  913. Begin
  914. If OpsEqual(Paicpu(hp1)^.oper[1],Paicpu(p)^.oper[0]) Then
  915. {mov reg1, mem1 or mov mem1, reg1
  916. mov mem2, reg1 mov reg2, mem1}
  917. Begin
  918. If OpsEqual(Paicpu(hp1)^.oper[0],Paicpu(p)^.oper[1]) Then
  919. { Removes the second statement from
  920. mov reg1, mem1/reg2
  921. mov mem1/reg2, reg1 }
  922. Begin
  923. if (paicpu(p)^.oper[0].typ = top_reg) then
  924. AllocRegBetween(asmL,paicpu(p)^.oper[0].reg,p,hp1);
  925. AsmL^.remove(hp1);
  926. Dispose(hp1,done);
  927. End
  928. Else
  929. Begin
  930. TmpUsedRegs := UsedRegs;
  931. UpdateUsedRegs(TmpUsedRegs, Pai(hp1^.next));
  932. If (Paicpu(p)^.oper[0].typ = top_reg) And
  933. { mov reg1, mem1
  934. mov mem2, reg1 }
  935. GetNextInstruction(hp1, hp2) And
  936. (hp2^.typ = ait_instruction) And
  937. (Paicpu(hp2)^.opcode = A_CMP) And
  938. (Paicpu(hp2)^.opsize = Paicpu(p)^.opsize) and
  939. (Paicpu(hp2)^.oper[0].typ = TOp_Ref) And
  940. (Paicpu(hp2)^.oper[1].typ = TOp_Reg) And
  941. RefsEqual(Paicpu(hp2)^.oper[0].ref^, Paicpu(p)^.oper[1].ref^) And
  942. (Paicpu(hp2)^.oper[1].reg = Paicpu(p)^.oper[0].reg) And
  943. Not(RegUsedAfterInstruction(Paicpu(p)^.oper[0].reg, hp2, TmpUsedRegs)) Then
  944. { change to
  945. mov reg1, mem1 mov reg1, mem1
  946. mov mem2, reg1 cmp reg1, mem2
  947. cmp mem1, reg1 }
  948. Begin
  949. AsmL^.Remove(hp2);
  950. Dispose(hp2, Done);
  951. Paicpu(hp1)^.opcode := A_CMP;
  952. Paicpu(hp1)^.loadref(1,newreference(Paicpu(hp1)^.oper[0].ref^));
  953. Paicpu(hp1)^.loadreg(0,Paicpu(p)^.oper[0].reg);
  954. End;
  955. End;
  956. End
  957. Else
  958. Begin
  959. tmpUsedRegs := UsedRegs;
  960. If GetNextInstruction(hp1, hp2) And
  961. (Paicpu(p)^.oper[0].typ = top_ref) And
  962. (Paicpu(p)^.oper[1].typ = top_reg) And
  963. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  964. (Paicpu(hp1)^.oper[0].reg = Paicpu(p)^.oper[1].reg) And
  965. (Paicpu(hp1)^.oper[1].typ = top_ref) And
  966. (Pai(hp2)^.typ = ait_instruction) And
  967. (Paicpu(hp2)^.opcode = A_MOV) And
  968. (Paicpu(hp2)^.opsize = Paicpu(p)^.opsize) and
  969. (Paicpu(hp2)^.oper[1].typ = top_reg) And
  970. (Paicpu(hp2)^.oper[0].typ = top_ref) And
  971. RefsEqual(Paicpu(hp2)^.oper[0].ref^, Paicpu(hp1)^.oper[1].ref^) Then
  972. If (Paicpu(p)^.oper[1].reg in [R_DI,R_EDI]) and
  973. not(RegUsedAfterInstruction(R_EDI,hp1,tmpUsedRegs)) Then
  974. { mov mem1, %edi
  975. mov %edi, mem2
  976. mov mem2, reg2
  977. to:
  978. mov mem1, reg2
  979. mov reg2, mem2}
  980. Begin
  981. Paicpu(p)^.Loadoper(1,Paicpu(hp2)^.oper[1]);
  982. Paicpu(hp1)^.loadoper(0,Paicpu(hp2)^.oper[1]);
  983. AsmL^.Remove(hp2);
  984. Dispose(hp2,Done);
  985. End
  986. Else
  987. If (Paicpu(p)^.oper[1].reg <> Paicpu(hp2)^.oper[1].reg) And
  988. not(RegInRef(Paicpu(p)^.oper[1].reg,Paicpu(p)^.oper[0].ref^)) And
  989. not(RegInRef(Paicpu(hp2)^.oper[1].reg,Paicpu(hp2)^.oper[0].ref^))
  990. Then
  991. { mov mem1, reg1 mov mem1, reg1
  992. mov reg1, mem2 mov reg1, mem2
  993. mov mem2, reg2 mov mem2, reg1
  994. to: to:
  995. mov mem1, reg1 mov mem1, reg1
  996. mov mem1, reg2 mov reg1, mem2
  997. mov reg1, mem2
  998. or (if mem1 depends on reg1
  999. and/or if mem2 depends on reg2)
  1000. to:
  1001. mov mem1, reg1
  1002. mov reg1, mem2
  1003. mov reg1, reg2
  1004. }
  1005. Begin
  1006. Paicpu(hp1)^.LoadRef(0,newreference(Paicpu(p)^.oper[0].ref^));
  1007. Paicpu(hp1)^.LoadReg(1,Paicpu(hp2)^.oper[1].reg);
  1008. Paicpu(hp2)^.LoadRef(1,newreference(Paicpu(hp2)^.oper[0].ref^));
  1009. Paicpu(hp2)^.LoadReg(0,Paicpu(p)^.oper[1].reg);
  1010. allocRegBetween(asmL,paicpu(p)^.oper[1].reg,p,hp2);
  1011. if (paicpu(p)^.oper[0].ref^.base in (usableregs+[R_EDI])) then
  1012. allocRegBetween(asmL,paicpu(p)^.oper[0].ref^.base,p,hp2);
  1013. if (paicpu(p)^.oper[0].ref^.index in (usableregs+[R_EDI])) then
  1014. allocRegBetween(asmL,paicpu(p)^.oper[0].ref^.index,p,hp2);
  1015. End
  1016. Else
  1017. If (Paicpu(hp1)^.Oper[0].reg <> Paicpu(hp2)^.Oper[1].reg) Then
  1018. begin
  1019. Paicpu(hp2)^.LoadReg(0,Paicpu(hp1)^.Oper[0].reg);
  1020. allocRegBetween(asmL,paicpu(p)^.oper[1].reg,p,hp2);
  1021. end
  1022. Else
  1023. Begin
  1024. AsmL^.Remove(hp2);
  1025. Dispose(hp2, Done);
  1026. End
  1027. End;
  1028. End
  1029. Else
  1030. (* {movl [mem1],reg1
  1031. movl [mem1],reg2
  1032. to:
  1033. movl [mem1],reg1
  1034. movl reg1,reg2 }
  1035. If (Paicpu(p)^.oper[0].typ = top_ref) and
  1036. (Paicpu(p)^.oper[1].typ = top_reg) and
  1037. (Paicpu(hp1)^.oper[0].typ = top_ref) and
  1038. (Paicpu(hp1)^.oper[1].typ = top_reg) and
  1039. (Paicpu(p)^.opsize = Paicpu(hp1)^.opsize) and
  1040. RefsEqual(TReference(Paicpu(p)^.oper[0]^),Paicpu(hp1)^.oper[0]^.ref^) and
  1041. (Paicpu(p)^.oper[1].reg<>Paicpu(hp1)^.oper[0]^.ref^.base) and
  1042. (Paicpu(p)^.oper[1].reg<>Paicpu(hp1)^.oper[0]^.ref^.index) then
  1043. Paicpu(hp1)^.LoadReg(0,Paicpu(p)^.oper[1].reg)
  1044. Else*)
  1045. { movl const1,[mem1]
  1046. movl [mem1],reg1
  1047. to:
  1048. movl const1,reg1
  1049. movl reg1,[mem1] }
  1050. If (Paicpu(p)^.oper[0].typ = top_const) and
  1051. (Paicpu(p)^.oper[1].typ = top_ref) and
  1052. (Paicpu(hp1)^.oper[0].typ = top_ref) and
  1053. (Paicpu(hp1)^.oper[1].typ = top_reg) and
  1054. (Paicpu(p)^.opsize = Paicpu(hp1)^.opsize) and
  1055. RefsEqual(Paicpu(hp1)^.oper[0].ref^,Paicpu(p)^.oper[1].ref^) then
  1056. Begin
  1057. Paicpu(hp1)^.LoadReg(0,Paicpu(hp1)^.oper[1].reg);
  1058. Paicpu(hp1)^.LoadRef(1,newreference(Paicpu(p)^.oper[1].ref^));
  1059. Paicpu(p)^.LoadReg(1,Paicpu(hp1)^.oper[0].reg);
  1060. End
  1061. End;
  1062. End;
  1063. A_MOVZX:
  1064. Begin
  1065. {removes superfluous And's after movzx's}
  1066. If (Paicpu(p)^.oper[1].typ = top_reg) And
  1067. GetNextInstruction(p, hp1) And
  1068. (Pai(hp1)^.typ = ait_instruction) And
  1069. (Paicpu(hp1)^.opcode = A_AND) And
  1070. (Paicpu(hp1)^.oper[0].typ = top_const) And
  1071. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1072. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg)
  1073. Then
  1074. Case Paicpu(p)^.opsize Of
  1075. S_BL, S_BW:
  1076. If (Paicpu(hp1)^.oper[0].val = $ff) Then
  1077. Begin
  1078. AsmL^.Remove(hp1);
  1079. Dispose(hp1, Done);
  1080. End;
  1081. S_WL:
  1082. If (Paicpu(hp1)^.oper[0].val = $ffff) Then
  1083. Begin
  1084. AsmL^.Remove(hp1);
  1085. Dispose(hp1, Done);
  1086. End;
  1087. End;
  1088. {changes some movzx constructs to faster synonims (all examples
  1089. are given with eax/ax, but are also valid for other registers)}
  1090. If (Paicpu(p)^.oper[1].typ = top_reg) Then
  1091. If (Paicpu(p)^.oper[0].typ = top_reg) Then
  1092. Case Paicpu(p)^.opsize of
  1093. S_BW:
  1094. Begin
  1095. If (Paicpu(p)^.oper[0].reg = Reg16ToReg8(Paicpu(p)^.oper[1].reg)) And
  1096. Not(CS_LittleSize In aktglobalswitches)
  1097. Then
  1098. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  1099. Begin
  1100. Paicpu(p)^.opcode := A_AND;
  1101. Paicpu(p)^.changeopsize(S_W);
  1102. Paicpu(p)^.LoadConst(0,$ff);
  1103. End
  1104. Else
  1105. If GetNextInstruction(p, hp1) And
  1106. (Pai(hp1)^.typ = ait_instruction) And
  1107. (Paicpu(hp1)^.opcode = A_AND) And
  1108. (Paicpu(hp1)^.oper[0].typ = top_const) And
  1109. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1110. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg)
  1111. Then
  1112. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  1113. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  1114. Begin
  1115. Paicpu(p)^.opcode := A_MOV;
  1116. Paicpu(p)^.changeopsize(S_W);
  1117. Paicpu(p)^.LoadReg(0,Reg8ToReg16(Paicpu(p)^.oper[0].reg));
  1118. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ff);
  1119. End;
  1120. End;
  1121. S_BL:
  1122. Begin
  1123. If (Paicpu(p)^.oper[0].reg = Reg32ToReg8(Paicpu(p)^.oper[1].reg)) And
  1124. Not(CS_LittleSize in aktglobalswitches)
  1125. Then
  1126. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  1127. Begin
  1128. Paicpu(p)^.opcode := A_AND;
  1129. Paicpu(p)^.changeopsize(S_L);
  1130. Paicpu(p)^.loadconst(0,$ff)
  1131. End
  1132. Else
  1133. If GetNextInstruction(p, hp1) And
  1134. (Pai(hp1)^.typ = ait_instruction) And
  1135. (Paicpu(hp1)^.opcode = A_AND) And
  1136. (Paicpu(hp1)^.oper[0].typ = top_const) And
  1137. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1138. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg)
  1139. Then
  1140. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  1141. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  1142. Begin
  1143. Paicpu(p)^.opcode := A_MOV;
  1144. Paicpu(p)^.changeopsize(S_L);
  1145. Paicpu(p)^.LoadReg(0,Reg8ToReg32(Paicpu(p)^.oper[0].reg));
  1146. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ff);
  1147. End
  1148. End;
  1149. S_WL:
  1150. Begin
  1151. If (Paicpu(p)^.oper[0].reg = Reg32ToReg16(Paicpu(p)^.oper[1].reg)) And
  1152. Not(CS_LittleSize In aktglobalswitches)
  1153. Then
  1154. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  1155. Begin
  1156. Paicpu(p)^.opcode := A_AND;
  1157. Paicpu(p)^.changeopsize(S_L);
  1158. Paicpu(p)^.LoadConst(0,$ffff);
  1159. End
  1160. Else
  1161. If GetNextInstruction(p, hp1) And
  1162. (Pai(hp1)^.typ = ait_instruction) And
  1163. (Paicpu(hp1)^.opcode = A_AND) And
  1164. (Paicpu(hp1)^.oper[0].typ = top_const) And
  1165. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1166. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg)
  1167. Then
  1168. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  1169. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  1170. Begin
  1171. Paicpu(p)^.opcode := A_MOV;
  1172. Paicpu(p)^.changeopsize(S_L);
  1173. Paicpu(p)^.LoadReg(0,Reg16ToReg32(Paicpu(p)^.oper[0].reg));
  1174. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ffff);
  1175. End;
  1176. End;
  1177. End
  1178. Else
  1179. If (Paicpu(p)^.oper[0].typ = top_ref) Then
  1180. Begin
  1181. If GetNextInstruction(p, hp1) And
  1182. (Pai(hp1)^.typ = ait_instruction) And
  1183. (Paicpu(hp1)^.opcode = A_AND) And
  1184. (Paicpu(hp1)^.oper[0].typ = Top_Const) And
  1185. (Paicpu(hp1)^.oper[1].typ = Top_Reg) And
  1186. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg) Then
  1187. Begin
  1188. Paicpu(p)^.opcode := A_MOV;
  1189. Case Paicpu(p)^.opsize Of
  1190. S_BL:
  1191. Begin
  1192. Paicpu(p)^.changeopsize(S_L);
  1193. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ff);
  1194. End;
  1195. S_WL:
  1196. Begin
  1197. Paicpu(p)^.changeopsize(S_L);
  1198. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ffff);
  1199. End;
  1200. S_BW:
  1201. Begin
  1202. Paicpu(p)^.changeopsize(S_W);
  1203. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ff);
  1204. End;
  1205. End;
  1206. End;
  1207. End;
  1208. End;
  1209. A_POP:
  1210. Begin
  1211. if (Paicpu(p)^.oper[0].typ = top_reg) And
  1212. GetNextInstruction(p, hp1) And
  1213. (pai(hp1)^.typ=ait_instruction) and
  1214. (Paicpu(hp1)^.opcode=A_PUSH) and
  1215. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  1216. (Paicpu(hp1)^.oper[0].reg=Paicpu(p)^.oper[0].reg) then
  1217. { This can't be done, because the register which is popped
  1218. can still be used after the push (PFV)
  1219. If (Not(cs_regalloc in aktglobalswitches)) Then
  1220. Begin
  1221. hp2:=pai(hp1^.next);
  1222. asml^.remove(p);
  1223. asml^.remove(hp1);
  1224. dispose(p,done);
  1225. dispose(hp1,done);
  1226. p:=hp2;
  1227. continue
  1228. End
  1229. Else }
  1230. Begin
  1231. { change it to a two op operation }
  1232. Paicpu(p)^.oper[1].typ:=top_none;
  1233. Paicpu(p)^.ops:=2;
  1234. Paicpu(p)^.opcode := A_MOV;
  1235. Paicpu(p)^.Loadoper(1,Paicpu(p)^.oper[0]);
  1236. Reset_reference(tmpref);
  1237. TmpRef.base := R_ESP;
  1238. Paicpu(p)^.LoadRef(0,newReference(TmpRef));
  1239. AsmL^.Remove(hp1);
  1240. Dispose(hp1, Done)
  1241. End;
  1242. end;
  1243. A_PUSH:
  1244. Begin
  1245. If (Paicpu(p)^.opsize = S_W) And
  1246. (Paicpu(p)^.oper[0].typ = Top_Const) And
  1247. GetNextInstruction(p, hp1) And
  1248. (Pai(hp1)^.typ = ait_instruction) And
  1249. (Paicpu(hp1)^.opcode = A_PUSH) And
  1250. (Paicpu(hp1)^.oper[0].typ = Top_Const) And
  1251. (Paicpu(hp1)^.opsize = S_W) Then
  1252. Begin
  1253. Paicpu(p)^.changeopsize(S_L);
  1254. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val shl 16 + word(Paicpu(hp1)^.oper[0].val));
  1255. AsmL^.Remove(hp1);
  1256. Dispose(hp1, Done)
  1257. End;
  1258. End;
  1259. A_SHL, A_SAL:
  1260. Begin
  1261. If (Paicpu(p)^.oper[0].typ = Top_Const) And
  1262. (Paicpu(p)^.oper[1].typ = Top_Reg) And
  1263. (Paicpu(p)^.opsize = S_L) And
  1264. (Paicpu(p)^.oper[0].val <= 3)
  1265. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1266. Then
  1267. Begin
  1268. TmpBool1 := True; {should we check the next instruction?}
  1269. TmpBool2 := False; {have we found an add/sub which could be
  1270. integrated in the lea?}
  1271. Reset_reference(tmpref);
  1272. TmpRef.index := Paicpu(p)^.oper[1].reg;
  1273. TmpRef.scalefactor := 1 shl Paicpu(p)^.oper[0].val;
  1274. While TmpBool1 And
  1275. GetNextInstruction(p, hp1) And
  1276. (Pai(hp1)^.typ = ait_instruction) And
  1277. ((Paicpu(hp1)^.opcode = A_ADD) Or
  1278. (Paicpu(hp1)^.opcode = A_SUB)) And
  1279. (Paicpu(hp1)^.oper[1].typ = Top_Reg) And
  1280. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg) Do
  1281. Begin
  1282. TmpBool1 := False;
  1283. If (Paicpu(hp1)^.oper[0].typ = Top_Const)
  1284. Then
  1285. Begin
  1286. TmpBool1 := True;
  1287. TmpBool2 := True;
  1288. If Paicpu(hp1)^.opcode = A_ADD Then
  1289. Inc(TmpRef.offset, Paicpu(hp1)^.oper[0].val)
  1290. Else
  1291. Dec(TmpRef.offset, Paicpu(hp1)^.oper[0].val);
  1292. AsmL^.Remove(hp1);
  1293. Dispose(hp1, Done);
  1294. End
  1295. Else
  1296. If (Paicpu(hp1)^.oper[0].typ = Top_Reg) And
  1297. (Paicpu(hp1)^.opcode = A_ADD) And
  1298. (TmpRef.base = R_NO) Then
  1299. Begin
  1300. TmpBool1 := True;
  1301. TmpBool2 := True;
  1302. TmpRef.base := Paicpu(hp1)^.oper[0].reg;
  1303. AsmL^.Remove(hp1);
  1304. Dispose(hp1, Done);
  1305. End;
  1306. End;
  1307. If TmpBool2 Or
  1308. ((aktoptprocessor < ClassP6) And
  1309. (Paicpu(p)^.oper[0].val <= 3) And
  1310. Not(CS_LittleSize in aktglobalswitches))
  1311. Then
  1312. Begin
  1313. If Not(TmpBool2) And
  1314. (Paicpu(p)^.oper[0].val = 1)
  1315. Then
  1316. Begin
  1317. hp1 := new(Paicpu,op_reg_reg(A_ADD,Paicpu(p)^.opsize,
  1318. Paicpu(p)^.oper[1].reg, Paicpu(p)^.oper[1].reg))
  1319. End
  1320. Else hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef),
  1321. Paicpu(p)^.oper[1].reg));
  1322. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1323. Dispose(p, Done);
  1324. p := hp1;
  1325. End;
  1326. End
  1327. Else
  1328. If (aktoptprocessor < ClassP6) And
  1329. (Paicpu(p)^.oper[0].typ = top_const) And
  1330. (Paicpu(p)^.oper[1].typ = top_reg) Then
  1331. If (Paicpu(p)^.oper[0].val = 1)
  1332. Then
  1333. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1334. but faster on a 486, and pairable in both U and V pipes on the Pentium
  1335. (unlike shl, which is only pairable in the U pipe)}
  1336. Begin
  1337. hp1 := new(Paicpu,op_reg_reg(A_ADD,Paicpu(p)^.opsize,
  1338. Paicpu(p)^.oper[1].reg, Paicpu(p)^.oper[1].reg));
  1339. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1340. Dispose(p, done);
  1341. p := hp1;
  1342. End
  1343. Else If (Paicpu(p)^.opsize = S_L) and
  1344. (Paicpu(p)^.oper[0].val<= 3) Then
  1345. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1346. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1347. Begin
  1348. Reset_reference(tmpref);
  1349. TmpRef.index := Paicpu(p)^.oper[1].reg;
  1350. TmpRef.scalefactor := 1 shl Paicpu(p)^.oper[0].val;
  1351. hp1 := new(Paicpu,op_ref_reg(A_LEA,S_L,newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  1352. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1353. Dispose(p, done);
  1354. p := hp1;
  1355. End
  1356. End;
  1357. A_SAR, A_SHR:
  1358. {changes the code sequence
  1359. shr/sar const1, x
  1360. shl const2, x
  1361. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  1362. Begin
  1363. If GetNextInstruction(p, hp1) And
  1364. (pai(hp1)^.typ = ait_instruction) and
  1365. (Paicpu(hp1)^.opcode = A_SHL) and
  1366. (Paicpu(p)^.oper[0].typ = top_const) and
  1367. (Paicpu(hp1)^.oper[0].typ = top_const) and
  1368. (Paicpu(hp1)^.opsize = Paicpu(p)^.opsize) And
  1369. (Paicpu(hp1)^.oper[1].typ = Paicpu(p)^.oper[1].typ) And
  1370. OpsEqual(Paicpu(hp1)^.oper[1], Paicpu(p)^.oper[1])
  1371. Then
  1372. If (Paicpu(p)^.oper[0].val > Paicpu(hp1)^.oper[0].val) And
  1373. Not(CS_LittleSize In aktglobalswitches)
  1374. Then
  1375. { shr/sar const1, %reg
  1376. shl const2, %reg
  1377. with const1 > const2 }
  1378. Begin
  1379. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val-Paicpu(hp1)^.oper[0].val);
  1380. Paicpu(hp1)^.opcode := A_AND;
  1381. l := (1 shl (Paicpu(hp1)^.oper[0].val)) - 1;
  1382. Case Paicpu(p)^.opsize Of
  1383. S_L: Paicpu(hp1)^.LoadConst(0,l Xor longint(-1));
  1384. S_B: Paicpu(hp1)^.LoadConst(0,l Xor $ff);
  1385. S_W: Paicpu(hp1)^.LoadConst(0,l Xor $ffff);
  1386. End;
  1387. End
  1388. Else
  1389. If (Paicpu(p)^.oper[0].val<Paicpu(hp1)^.oper[0].val) And
  1390. Not(CS_LittleSize In aktglobalswitches)
  1391. Then
  1392. { shr/sar const1, %reg
  1393. shl const2, %reg
  1394. with const1 < const2 }
  1395. Begin
  1396. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val-Paicpu(p)^.oper[0].val);
  1397. Paicpu(p)^.opcode := A_AND;
  1398. l := (1 shl (Paicpu(p)^.oper[0].val))-1;
  1399. Case Paicpu(p)^.opsize Of
  1400. S_L: Paicpu(p)^.LoadConst(0,l Xor $ffffffff);
  1401. S_B: Paicpu(p)^.LoadConst(0,l Xor $ff);
  1402. S_W: Paicpu(p)^.LoadConst(0,l Xor $ffff);
  1403. End;
  1404. End
  1405. Else
  1406. { shr/sar const1, %reg
  1407. shl const2, %reg
  1408. with const1 = const2 }
  1409. Begin
  1410. Paicpu(p)^.opcode := A_AND;
  1411. l := (1 shl (Paicpu(p)^.oper[0].val))-1;
  1412. Case Paicpu(p)^.opsize Of
  1413. S_B: Paicpu(p)^.LoadConst(0,l Xor $ff);
  1414. S_W: Paicpu(p)^.LoadConst(0,l Xor $ffff);
  1415. S_L: Paicpu(p)^.LoadConst(0,l Xor $ffffffff);
  1416. End;
  1417. AsmL^.remove(hp1);
  1418. dispose(hp1, done);
  1419. End;
  1420. End;
  1421. A_SETcc :
  1422. { changes
  1423. setcc (funcres) setcc reg
  1424. movb (funcres), reg to leave/ret
  1425. leave/ret }
  1426. Begin
  1427. If (Paicpu(p)^.oper[0].typ = top_ref) And
  1428. GetNextInstruction(p, hp1) And
  1429. GetNextInstruction(hp1, hp2) And
  1430. (hp2^.typ = ait_instruction) And
  1431. ((Paicpu(hp2)^.opcode = A_LEAVE) or
  1432. (Paicpu(hp2)^.opcode = A_RET)) And
  1433. (Paicpu(p)^.oper[0].ref^.Base = procinfo^.FramePointer) And
  1434. (Paicpu(p)^.oper[0].ref^.Index = R_NO) And
  1435. (Paicpu(p)^.oper[0].ref^.Offset >= procinfo^.Return_Offset) And
  1436. (hp1^.typ = ait_instruction) And
  1437. (Paicpu(hp1)^.opcode = A_MOV) And
  1438. (Paicpu(hp1)^.opsize = S_B) And
  1439. (Paicpu(hp1)^.oper[0].typ = top_ref) And
  1440. RefsEqual(Paicpu(hp1)^.oper[0].ref^, Paicpu(p)^.oper[0].ref^) Then
  1441. Begin
  1442. Paicpu(p)^.LoadReg(0,Paicpu(hp1)^.oper[1].reg);
  1443. AsmL^.Remove(hp1);
  1444. Dispose(hp1, Done)
  1445. End
  1446. End;
  1447. A_SUB:
  1448. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1449. { * change "sub/add const1, reg" or "dec reg" followed by
  1450. "sub const2, reg" to one "sub ..., reg" }
  1451. Begin
  1452. If (Paicpu(p)^.oper[0].typ = top_const) And
  1453. (Paicpu(p)^.oper[1].typ = top_reg) Then
  1454. If (Paicpu(p)^.oper[0].val = 2) And
  1455. (Paicpu(p)^.oper[1].reg = R_ESP) Then
  1456. Begin
  1457. hp1 := Pai(p^.next);
  1458. While Assigned(hp1) And
  1459. (Pai(hp1)^.typ In [ait_instruction]+SkipInstr) And
  1460. Not((Pai(hp1)^.typ = ait_instruction) And
  1461. ((Paicpu(hp1)^.opcode = A_CALL) or
  1462. (Paicpu(hp1)^.opcode = A_PUSH) or
  1463. ((Paicpu(hp1)^.opcode = A_MOV) And
  1464. (Paicpu(hp1)^.oper[1].typ = top_ref) And
  1465. (Paicpu(hp1)^.oper[1].ref^.base = R_ESP)))) do
  1466. hp1 := Pai(hp1^.next);
  1467. If Assigned(hp1) And
  1468. (Pai(hp1)^.typ = ait_instruction) And
  1469. (Paicpu(hp1)^.opcode = A_PUSH) And
  1470. (Paicpu(hp1)^.opsize = S_W)
  1471. Then
  1472. Begin
  1473. Paicpu(hp1)^.changeopsize(S_L);
  1474. if Paicpu(hp1)^.oper[0].typ=top_reg then
  1475. Paicpu(hp1)^.LoadReg(0,Reg16ToReg32(Paicpu(hp1)^.oper[0].reg));
  1476. hp1 := Pai(p^.next);
  1477. AsmL^.Remove(p);
  1478. Dispose(p, Done);
  1479. p := hp1;
  1480. Continue
  1481. End;
  1482. If DoSubAddOpt(p) Then continue;
  1483. End
  1484. Else If DoSubAddOpt(p) Then Continue
  1485. End;
  1486. A_TEST, A_OR:
  1487. {removes the line marked with (x) from the sequence
  1488. And/or/xor/add/sub/... $x, %y
  1489. test/or %y, %y (x)
  1490. j(n)z _Label
  1491. as the first instruction already adjusts the ZF}
  1492. Begin
  1493. If OpsEqual(Paicpu(p)^.oper[0],Paicpu(p)^.oper[1]) Then
  1494. If GetLastInstruction(p, hp1) And
  1495. (pai(hp1)^.typ = ait_instruction) Then
  1496. Case Paicpu(hp1)^.opcode Of
  1497. A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_SHL, A_SHR:
  1498. Begin
  1499. If OpsEqual(Paicpu(hp1)^.oper[1],Paicpu(p)^.oper[0]) Then
  1500. Begin
  1501. hp1 := pai(p^.next);
  1502. asml^.remove(p);
  1503. dispose(p, done);
  1504. p := pai(hp1);
  1505. continue
  1506. End;
  1507. End;
  1508. A_DEC, A_INC, A_NEG:
  1509. Begin
  1510. If OpsEqual(Paicpu(hp1)^.oper[0],Paicpu(p)^.oper[0]) Then
  1511. Begin
  1512. Case Paicpu(hp1)^.opcode Of
  1513. A_DEC, A_INC:
  1514. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  1515. Begin
  1516. Case Paicpu(hp1)^.opcode Of
  1517. A_DEC: Paicpu(hp1)^.opcode := A_SUB;
  1518. A_INC: Paicpu(hp1)^.opcode := A_ADD;
  1519. End;
  1520. Paicpu(hp1)^.Loadoper(1,Paicpu(hp1)^.oper[0]);
  1521. Paicpu(hp1)^.LoadConst(0,1);
  1522. Paicpu(hp1)^.ops:=2;
  1523. End
  1524. End;
  1525. hp1 := pai(p^.next);
  1526. asml^.remove(p);
  1527. dispose(p, done);
  1528. p := pai(hp1);
  1529. continue
  1530. End;
  1531. End
  1532. End
  1533. Else
  1534. End;
  1535. A_XOR:
  1536. If (Paicpu(p)^.oper[0].typ = top_reg) And
  1537. (Paicpu(p)^.oper[1].typ = top_reg) And
  1538. (Paicpu(p)^.oper[0].reg = Paicpu(p)^.oper[1].reg) then
  1539. { temporarily change this to 'mov reg,0' to make it easier }
  1540. { for the CSE. Will be changed back in pass 2 }
  1541. begin
  1542. paicpu(p)^.opcode := A_MOV;
  1543. paicpu(p)^.loadconst(0,0);
  1544. end;
  1545. End;
  1546. end; { if is_jmp }
  1547. End;
  1548. { ait_label:
  1549. Begin
  1550. If Not(Pai_Label(p)^.l^.is_used)
  1551. Then
  1552. Begin
  1553. hp1 := Pai(p^.next);
  1554. AsmL^.Remove(p);
  1555. Dispose(p, Done);
  1556. p := hp1;
  1557. Continue
  1558. End;
  1559. End;}
  1560. End;
  1561. p:=pai(p^.next);
  1562. end;
  1563. end;
  1564. {$ifdef foldArithOps}
  1565. Function IsArithOp(opcode: TAsmOp): Boolean;
  1566. Begin
  1567. IsArithOp := False;
  1568. Case opcode Of
  1569. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR: IsArithOp := True
  1570. End;
  1571. End;
  1572. {$endif foldArithOps}
  1573. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  1574. function CanBeCMOV(p : pai) : boolean;
  1575. begin
  1576. CanBeCMOV:=assigned(p) and (p^.typ=ait_instruction) and
  1577. (paicpu(p)^.opcode=A_MOV) and
  1578. (paicpu(p)^.opsize in [S_L,S_W]) and
  1579. (paicpu(p)^.oper[0].typ in [top_reg,top_ref]) and
  1580. (paicpu(p)^.oper[1].typ in [top_reg,top_ref]);
  1581. end;
  1582. var
  1583. p,hp1,hp2: pai;
  1584. {$ifdef USECMOV}
  1585. l : longint;
  1586. condition : tasmcond;
  1587. hp3: pai;
  1588. {$endif USECMOV}
  1589. {$ifdef foldArithOps}
  1590. UsedRegs, TmpUsedRegs: TRegSet;
  1591. {$endif foldArithOps}
  1592. Begin
  1593. P := BlockStart;
  1594. {$ifdef foldArithOps}
  1595. UsedRegs := [];
  1596. {$endif foldArithOps}
  1597. While (P <> BlockEnd) Do
  1598. Begin
  1599. {$ifdef foldArithOps}
  1600. UpdateUsedRegs(UsedRegs, Pai(p^.next));
  1601. {$endif foldArithOps}
  1602. Case P^.Typ Of
  1603. Ait_Instruction:
  1604. Begin
  1605. Case Paicpu(p)^.opcode Of
  1606. A_CALL:
  1607. If (AktOptProcessor < ClassP6) And
  1608. GetNextInstruction(p, hp1) And
  1609. (hp1^.typ = ait_instruction) And
  1610. (paicpu(hp1)^.opcode = A_JMP) Then
  1611. Begin
  1612. Inc(paicpu(hp1)^.oper[0].sym^.refs);
  1613. hp2 := New(Paicpu,op_sym(A_PUSH,S_L,paicpu(hp1)^.oper[0].sym));
  1614. InsertLLItem(AsmL, p^.previous, p, hp2);
  1615. Paicpu(p)^.opcode := A_JMP;
  1616. AsmL^.Remove(hp1);
  1617. Dispose(hp1, Done)
  1618. End;
  1619. {$ifdef USECMOV}
  1620. A_Jcc:
  1621. if (aktspecificoptprocessor=ClassP6) then
  1622. begin
  1623. { check for
  1624. jCC xxx
  1625. <several movs>
  1626. xxx:
  1627. }
  1628. l:=0;
  1629. GetNextInstruction(p, hp1);
  1630. while assigned(hp1) And
  1631. CanBeCMOV(hp1) do
  1632. begin
  1633. inc(l);
  1634. GetNextInstruction(hp1,hp1);
  1635. end;
  1636. if assigned(hp1) then
  1637. begin
  1638. if FindLabel(PAsmLabel(paicpu(p)^.oper[0].sym),hp1) then
  1639. begin
  1640. if (l<=4) and (l>0) then
  1641. begin
  1642. condition:=inverse_cond[paicpu(p)^.condition];
  1643. GetNextInstruction(p,hp1);
  1644. asml^.remove(p);
  1645. dispose(p,done);
  1646. p:=hp1;
  1647. repeat
  1648. paicpu(hp1)^.opcode:=A_CMOVcc;
  1649. paicpu(hp1)^.condition:=condition;
  1650. GetNextInstruction(hp1,hp1);
  1651. until not(assigned(hp1)) or
  1652. not(CanBeCMOV(hp1));
  1653. asml^.remove(hp1);
  1654. dispose(hp1,done);
  1655. continue;
  1656. end;
  1657. end
  1658. else
  1659. begin
  1660. { check further for
  1661. jCC xxx
  1662. <several movs>
  1663. jmp yyy
  1664. xxx:
  1665. <several movs>
  1666. yyy:
  1667. }
  1668. { hp2 points to jmp xxx }
  1669. hp2:=hp1;
  1670. { skip hp1 to xxx }
  1671. GetNextInstruction(hp1, hp1);
  1672. if assigned(hp2) and
  1673. assigned(hp1) and
  1674. (l<=3) and
  1675. (hp2^.typ=ait_instruction) and
  1676. (paicpu(hp2)^.is_jmp) and
  1677. (paicpu(hp2)^.condition=C_None) and
  1678. FindLabel(PAsmLabel(paicpu(p)^.oper[0].sym),hp1) then
  1679. begin
  1680. l:=0;
  1681. while assigned(hp1) And
  1682. CanBeCMOV(hp1) do
  1683. begin
  1684. inc(l);
  1685. GetNextInstruction(hp1, hp1);
  1686. end;
  1687. end;
  1688. {
  1689. if assigned(hp1) and
  1690. FindLabel(PAsmLabel(paicpu(hp2)^.oper[0].sym),hp1) then
  1691. begin
  1692. condition:=inverse_cond[paicpu(p)^.condition];
  1693. GetNextInstruction(p,hp1);
  1694. asml^.remove(p);
  1695. dispose(p,done);
  1696. p:=hp1;
  1697. repeat
  1698. paicpu(hp1)^.opcode:=A_CMOVcc;
  1699. paicpu(hp1)^.condition:=condition;
  1700. GetNextInstruction(hp1,hp1);
  1701. until not(assigned(hp1)) or
  1702. not(CanBeCMOV(hp1));
  1703. hp2:=hp1^.next;
  1704. condition:=inverse_cond[condition];
  1705. asml^.remove(hp1^.next)
  1706. dispose(hp1^.next,done);
  1707. asml^.remove(hp1);
  1708. dispose(hp1,done);
  1709. continue;
  1710. end;
  1711. }
  1712. end;
  1713. end;
  1714. end;
  1715. {$endif USECMOV}
  1716. A_FSTP,A_FISTP:
  1717. if doFpuLoadStoreOpt(asmL,p) then
  1718. continue;
  1719. {$ifdef foldArithOps}
  1720. A_IMUL:
  1721. begin
  1722. if ((paicpu(p)^.oper[0].typ = top_const) or
  1723. (paicpu(p)^.oper[0].typ = top_symbol)) and
  1724. (paicpu(p)^.oper[1].typ = top_reg) and
  1725. ((paicpu(p)^.oper[2].typ = top_none) or
  1726. ((paicpu(p)^.oper[2].typ = top_reg) and
  1727. (paicpu(p)^.oper[2].reg = paicpu(p)^.oper[1].reg))) and
  1728. getLastInstruction(p,hp1) and
  1729. (hp1^.typ = ait_instruction) and
  1730. (paicpu(hp1)^.opcode = A_MOV) and
  1731. (paicpu(hp1)^.oper[0].typ = top_reg) and
  1732. (paicpu(hp1)^.oper[1].typ = top_reg) and
  1733. (paicpu(hp1)^.oper[1].reg = paicpu(p)^.oper[1].reg) then
  1734. { change "mov reg1,reg2; imul y,reg2" to "imul y,reg1,reg2" }
  1735. begin
  1736. paicpu(p)^.ops := 3;
  1737. paicpu(p)^.loadreg(1,paicpu(hp1)^.oper[0].reg);
  1738. paicpu(p)^.loadreg(2,paicpu(hp1)^.oper[1].reg);
  1739. asmL^.remove(hp1);
  1740. dispose(hp1,done);
  1741. end;
  1742. end;
  1743. {$endif foldArithOps}
  1744. A_MOV:
  1745. Begin
  1746. If (Paicpu(p)^.oper[0].typ = top_reg) And
  1747. (Paicpu(p)^.oper[1].typ = top_reg) And
  1748. GetNextInstruction(p, hp1) And
  1749. (hp1^.typ = ait_Instruction) And
  1750. ((Paicpu(hp1)^.opcode = A_MOV) or
  1751. (Paicpu(hp1)^.opcode = A_MOVZX) or
  1752. (Paicpu(hp1)^.opcode = A_MOVSX)) And
  1753. (Paicpu(hp1)^.oper[0].typ = top_ref) And
  1754. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1755. ((Paicpu(hp1)^.oper[0].ref^.Base = Paicpu(p)^.oper[1].reg) Or
  1756. (Paicpu(hp1)^.oper[0].ref^.Index = Paicpu(p)^.oper[1].reg)) And
  1757. (Reg32(Paicpu(hp1)^.oper[1].reg) = Paicpu(p)^.oper[1].reg) Then
  1758. {mov reg1, reg2
  1759. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  1760. Begin
  1761. If (Paicpu(hp1)^.oper[0].ref^.Base = Paicpu(p)^.oper[1].reg) Then
  1762. Paicpu(hp1)^.oper[0].ref^.Base := Paicpu(p)^.oper[0].reg;
  1763. If (Paicpu(hp1)^.oper[0].ref^.Index = Paicpu(p)^.oper[1].reg) Then
  1764. Paicpu(hp1)^.oper[0].ref^.Index := Paicpu(p)^.oper[0].reg;
  1765. AsmL^.Remove(p);
  1766. Dispose(p, Done);
  1767. p := hp1;
  1768. Continue;
  1769. End
  1770. {$ifdef foldArithOps}
  1771. Else If (Paicpu(p)^.oper[0].typ = top_ref) And
  1772. GetNextInstruction(p,hp1) And
  1773. (hp1^.typ = ait_instruction) And
  1774. IsArithOp(Paicpu(hp1)^.opcode) And
  1775. (Paicpu(hp1)^.oper[0].typ in [top_reg,top_const]) And
  1776. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1777. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg) And
  1778. GetNextInstruction(hp1,hp2) And
  1779. (hp2^.typ = ait_instruction) And
  1780. (Paicpu(hp2)^.opcode = A_MOV) And
  1781. (Paicpu(hp2)^.oper[0].typ = top_reg) And
  1782. (Paicpu(hp2)^.oper[0].reg = Paicpu(p)^.oper[1].reg) And
  1783. (Paicpu(hp2)^.oper[1].typ = top_ref) Then
  1784. Begin
  1785. TmpUsedRegs := UsedRegs;
  1786. UpdateUsedRegs(TmpUsedRegs,Pai(hp1^.next));
  1787. If (RefsEqual(Paicpu(hp2)^.oper[1].ref^, Paicpu(p)^.oper[0].ref^) And
  1788. Not(RegUsedAfterInstruction(Paicpu(p)^.oper[1].reg,
  1789. hp2, TmpUsedRegs)))
  1790. Then
  1791. { change mov (ref), reg }
  1792. { add/sub/or/... reg2/$const, reg }
  1793. { mov (reg), ref }
  1794. { # release reg }
  1795. { to add/sub/or/... reg2/$const, (ref) }
  1796. Begin
  1797. Paicpu(hp1)^.LoadRef(1,newreference(Paicpu(p)^.oper[0].ref^));
  1798. AsmL^.Remove(p);
  1799. AsmL^.Remove(hp2);
  1800. Dispose(p,done);
  1801. Dispose(hp2,Done);
  1802. p := hp1
  1803. End;
  1804. End
  1805. {$endif foldArithOps}
  1806. else if (Paicpu(p)^.oper[0].typ = Top_Const) And
  1807. (Paicpu(p)^.oper[0].val = 0) And
  1808. (Paicpu(p)^.oper[1].typ = Top_Reg) Then
  1809. { change "mov $0, %reg" into "xor %reg, %reg" }
  1810. Begin
  1811. Paicpu(p)^.opcode := A_XOR;
  1812. Paicpu(p)^.LoadReg(0,Paicpu(p)^.oper[1].reg);
  1813. End
  1814. End;
  1815. A_MOVZX:
  1816. Begin
  1817. If (Paicpu(p)^.oper[1].typ = top_reg) Then
  1818. If (Paicpu(p)^.oper[0].typ = top_reg)
  1819. Then
  1820. Case Paicpu(p)^.opsize of
  1821. S_BL:
  1822. Begin
  1823. If IsGP32Reg(Paicpu(p)^.oper[1].reg) And
  1824. Not(CS_LittleSize in aktglobalswitches) And
  1825. (aktoptprocessor = ClassP5)
  1826. Then
  1827. {Change "movzbl %reg1, %reg2" to
  1828. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1829. PentiumMMX}
  1830. Begin
  1831. hp1 := New(Paicpu, op_reg_reg(A_XOR, S_L,
  1832. Paicpu(p)^.oper[1].reg, Paicpu(p)^.oper[1].reg));
  1833. InsertLLItem(AsmL,p^.previous, p, hp1);
  1834. Paicpu(p)^.opcode := A_MOV;
  1835. Paicpu(p)^.changeopsize(S_B);
  1836. Paicpu(p)^.LoadReg(1,Reg32ToReg8(Paicpu(p)^.oper[1].reg));
  1837. End;
  1838. End;
  1839. End
  1840. Else
  1841. If (Paicpu(p)^.oper[0].typ = top_ref) And
  1842. (Paicpu(p)^.oper[0].ref^.base <> Paicpu(p)^.oper[1].reg) And
  1843. (Paicpu(p)^.oper[0].ref^.index <> Paicpu(p)^.oper[1].reg) And
  1844. Not(CS_LittleSize in aktglobalswitches) And
  1845. IsGP32Reg(Paicpu(p)^.oper[1].reg) And
  1846. (aktoptprocessor = ClassP5) And
  1847. (Paicpu(p)^.opsize = S_BL)
  1848. Then
  1849. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1850. Pentium and PentiumMMX}
  1851. Begin
  1852. hp1 := New(Paicpu,op_reg_reg(A_XOR, S_L, Paicpu(p)^.oper[1].reg,
  1853. Paicpu(p)^.oper[1].reg));
  1854. Paicpu(p)^.opcode := A_MOV;
  1855. Paicpu(p)^.changeopsize(S_B);
  1856. Paicpu(p)^.LoadReg(1,Reg32ToReg8(Paicpu(p)^.oper[1].reg));
  1857. InsertLLItem(AsmL,p^.previous, p, hp1);
  1858. End;
  1859. End;
  1860. End;
  1861. End;
  1862. End;
  1863. p := Pai(p^.next)
  1864. End;
  1865. End;
  1866. End.
  1867. {
  1868. $Log$
  1869. Revision 1.90 2000-03-26 10:58:47 jonas
  1870. * some more allocRegBetween fixes (-al didn't function previously
  1871. if the compiler was compiled with -OG2p3r)
  1872. Revision 1.89 2000/03/26 08:46:52 jonas
  1873. * fixed bug in regUsedAfterInstruction (it didn't convert the reg
  1874. to 32bit before checking)
  1875. * result: make cycle now works with -OG3p3r!!!!
  1876. Revision 1.88 2000/03/25 18:57:02 jonas
  1877. * remove dealloc/alloc of reg1 between "movl %reg1,%reg2;
  1878. movl %reg2,%reg1" when removing the second instruction (it
  1879. confused the CSE and caused errors with -Or)
  1880. Revision 1.87 2000/02/13 14:21:50 jonas
  1881. * modifications to make the compiler functional when compiled with
  1882. -Or
  1883. Revision 1.86 2000/02/12 19:28:56 jonas
  1884. * fix for imul optimization in popt386 (exclude top_ref as first
  1885. argument)
  1886. * in csopt386: change "mov reg1,reg2; <several operations on reg2>;
  1887. mov reg2,reg1" to "<several operations on reg1>" (-dnewopt...)
  1888. Revision 1.85 2000/02/12 14:10:15 jonas
  1889. + change "mov reg1,reg2;imul x,reg2" to "imul x,reg1,reg2" in popt386
  1890. (-dnewoptimizations)
  1891. * shl(d) and shr(d) are considered to have a hardcoded register if
  1892. they use cl as shift count (since you can't replace them with
  1893. another register) in csopt386 (also for -dnewoptimizations)
  1894. Revision 1.84 2000/02/09 13:22:58 peter
  1895. * log truncated
  1896. Revision 1.83 2000/02/04 13:53:04 jonas
  1897. * fpuloadstore optimizations are now done before and after the CSE
  1898. Revision 1.82 2000/01/24 12:17:24 florian
  1899. * some improvemenst to cmov support
  1900. * disabled excpetion frame generation in cosntructors temporarily
  1901. Revision 1.81 2000/01/23 21:29:17 florian
  1902. * CMOV support in optimizer (in define USECMOV)
  1903. + start of support of exceptions in constructors
  1904. Revision 1.80 2000/01/22 16:05:15 jonas
  1905. + change "lea x(reg),reg" to "add x,reg" (-dnewoptimizations)
  1906. * detection whether edi is used after instructions (since regalloc
  1907. info for it is now available)
  1908. * better regUsedAfterInstruction function
  1909. Revision 1.79 2000/01/21 11:26:19 pierre
  1910. * bug fix for bug 802
  1911. Revision 1.78 2000/01/11 17:14:49 jonas
  1912. * fixed a serious memory leak
  1913. Revision 1.77 2000/01/09 12:35:02 jonas
  1914. * changed edi allocation to use getexplicitregister32/ungetregister
  1915. (adapted tgeni386 a bit for this) and enabled it by default
  1916. * fixed very big and stupid bug of mine in cg386mat that broke the
  1917. include() code (and make cycle :( ) if you compiled without
  1918. -dnewoptimizations
  1919. Revision 1.76 2000/01/07 01:14:30 peter
  1920. * updated copyright to 2000
  1921. Revision 1.75 1999/12/30 17:56:44 peter
  1922. * fixed and;jmp being translated into test;jmp
  1923. Revision 1.74 1999/12/05 16:48:43 jonas
  1924. * CSE of constant loading in regs works properly again
  1925. + if a constant is stored into memory using "mov const, ref" and
  1926. there is a reg that contains this const, it is changed into
  1927. "mov reg, ref"
  1928. Revision 1.73 1999/12/02 11:26:41 peter
  1929. * newoptimizations define added
  1930. Revision 1.72 1999/11/30 10:40:45 peter
  1931. + ttype, tsymlist
  1932. Revision 1.71 1999/11/27 23:47:55 jonas
  1933. + change "mov var,reg; add/shr/... x,reg; mov reg,var" to
  1934. "add/shr/... x,var" (if x is a const or reg, suggestion from Peter)
  1935. Enable with -dfoldArithOps
  1936. Revision 1.70 1999/11/21 13:09:41 jonas
  1937. * fixed some missed optimizations because 8bit regs were not always
  1938. taken into account
  1939. Revision 1.69 1999/11/13 19:03:56 jonas
  1940. * don't remove align objects between JMP's and labels
  1941. Revision 1.68 1999/11/06 16:24:00 jonas
  1942. * getfinaldestination works completely again (a lot of functionality
  1943. got lost in the conversion resulting from the removal of
  1944. ait_labeled_instruction)
  1945. Revision 1.67 1999/11/06 14:34:23 peter
  1946. * truncated log to 20 revs
  1947. Revision 1.66 1999/09/27 23:44:55 peter
  1948. * procinfo is now a pointer
  1949. * support for result setting in sub procedure
  1950. Revision 1.65 1999/09/05 14:27:19 florian
  1951. + fld reg;fxxx to fxxxr reg optimization
  1952. Revision 1.64 1999/08/25 12:00:02 jonas
  1953. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1954. }