popt386.pas 98 KB

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