popt386.pas 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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);
  22. Procedure PeepHoleOptPass2(AsmL: PAasmOutput);
  23. Implementation
  24. Uses globals, systems, verbose, hcodegen
  25. {$ifdef i386}
  26. ,i386, DAOpt386
  27. {$endif i386}
  28. ;
  29. Function RegUsedAfterInstruction(Reg: TRegister; p: Pai; Var UsedRegs: TRegSet): Boolean;
  30. Begin
  31. UpdateUsedRegs(UsedRegs, Pai(p^.Next));
  32. RegUsedAfterInstruction := Reg in UsedRegs
  33. End;
  34. Procedure PeepHoleOptPass1(Asml: PAasmOutput);
  35. {First pass of peepholeoptimizations}
  36. Var
  37. p ,hp1, hp2: pai;
  38. TmpBool1, TmpBool2: Boolean;
  39. TmpRef: PReference;
  40. UsedRegs, TmpUsedRegs: TRegSet;
  41. Procedure GetFinalDestination(hp: pai_labeled);
  42. {traces sucessive jumps to their final destination and sets it, e.g.
  43. je l1 je l3
  44. <code> <code>
  45. l1: becomes l1:
  46. je l2 je l3
  47. <code> <code>
  48. l2: l2:
  49. jmp l3 jmp l3}
  50. Var p1: pai;
  51. Function SkipLabels(hp: Pai): Pai;
  52. {skips all labels and returns the next "real" instruction; it is
  53. assumed that hp is of the type ait_label}
  54. Begin
  55. While assigned(hp^.next) and
  56. (pai(hp^.next)^.typ In SkipInstr + [ait_label]) Do
  57. hp := pai(hp^.next);
  58. If assigned(hp^.next)
  59. Then SkipLabels := pai(hp^.next)
  60. Else SkipLabels := hp;
  61. End;
  62. Begin
  63. If (hp^.lab^.nb >= LoLab) and
  64. (hp^.lab^.nb <= HiLab) and {range check, necessary?}
  65. (Pointer(LTable^[hp^.lab^.nb-LoLab].PaiObj) <> Pointer(0)) Then
  66. Begin
  67. p1 := LTable^[hp^.lab^.nb-LoLab].PaiObj; {the jump's destination}
  68. p1 := SkipLabels(p1);
  69. If (pai(p1)^.typ = ait_labeled_instruction) and
  70. ((pai_labeled(p1)^._operator = A_JMP) or
  71. (pai_labeled(p1)^._operator = hp^._operator))
  72. Then
  73. Begin
  74. GetFinalDestination(pai_labeled(p1));
  75. Dec(hp^.lab^.refcount);
  76. If (hp^.lab^.refcount = 0) Then
  77. hp^.lab^.is_used := False;
  78. hp^.lab := pai_labeled(p1)^.lab;
  79. Inc(hp^.lab^.refcount);
  80. End
  81. End
  82. End;
  83. Begin
  84. P := Pai(AsmL^.First);
  85. UsedRegs := [];
  86. While Assigned(P) Do
  87. Begin
  88. UpDateUsedRegs(UsedRegs, Pai(p^.next));
  89. Case P^.Typ Of
  90. Ait_Labeled_Instruction:
  91. Begin
  92. {the following if-block removes all code between a jmp and the next label,
  93. because it can never be executed}
  94. If (pai_labeled(p)^._operator = A_JMP) Then
  95. Begin
  96. hp1 := pai(p^.next);
  97. While GetNextInstruction(p, hp1) and
  98. ((hp1^.typ <> ait_label) or
  99. { skip unused labels, they're not referenced anywhere }
  100. Not(Pai_Label(hp1)^.l^.is_used)) Do
  101. If (hp1^.typ <> ait_label) Then
  102. Begin
  103. AsmL^.Remove(hp1);
  104. Dispose(hp1, done);
  105. End;
  106. End;
  107. If GetNextInstruction(p, hp1) then
  108. Begin
  109. If (pai(hp1)^.typ=ait_labeled_instruction) and
  110. (pai_labeled(hp1)^._operator=A_JMP) and
  111. GetNextInstruction(hp1, hp2) And
  112. FindLabel(pai_labeled(p)^.lab, hp2)
  113. Then
  114. Begin
  115. Case pai_labeled(p)^._operator Of
  116. A_JE : pai_labeled(p)^._operator:=A_JNE;
  117. A_JNE : pai_labeled(p)^._operator:=A_JE;
  118. A_JL : pai_labeled(p)^._operator:=A_JGE;
  119. A_JG : pai_labeled(p)^._operator:=A_JLE;
  120. A_JLE : pai_labeled(p)^._operator:=A_JG;
  121. A_JGE : pai_labeled(p)^._operator:=A_JL;
  122. A_JNZ : pai_labeled(p)^._operator:=A_JZ;
  123. A_JNO : pai_labeled(p)^._operator:=A_JO;
  124. A_JZ : pai_labeled(p)^._operator:=A_JNZ;
  125. A_JS : pai_labeled(p)^._operator:=A_JNS;
  126. A_JNS : pai_labeled(p)^._operator:=A_JS;
  127. A_JO : pai_labeled(p)^._operator:=A_JNO;
  128. A_JC : pai_labeled(p)^._operator:=A_JNC;
  129. A_JNC : pai_labeled(p)^._operator:=A_JC;
  130. A_JA : pai_labeled(p)^._operator:=A_JBE;
  131. A_JAE : pai_labeled(p)^._operator:=A_JB;
  132. A_JB : pai_labeled(p)^._operator:=A_JAE;
  133. A_JBE : pai_labeled(p)^._operator:=A_JA;
  134. Else
  135. begin
  136. If (LabDif <> 0) Then GetFinalDestination(pai_labeled(p));
  137. p:=pai(p^.next);
  138. continue;
  139. end;
  140. end;
  141. Dec(pai_label(hp2)^.l^.refcount);
  142. If (pai_label(hp2)^.l^.refcount = 0) Then
  143. Begin
  144. pai_label(hp2)^.l^.is_used := False;
  145. { AsmL^.remove(hp2);
  146. Dispose(hp2, done);}
  147. End;
  148. pai_labeled(p)^.lab:=pai_labeled(hp1)^.lab;
  149. Inc(pai_labeled(p)^.lab^.refcount);
  150. asml^.remove(hp1);
  151. dispose(hp1,done);
  152. If (LabDif <> 0) Then GetFinalDestination(pai_labeled(p));
  153. end
  154. else
  155. if FindLabel(pai_labeled(p)^.lab, hp1) then
  156. Begin
  157. hp2:=pai(hp1^.next);
  158. asml^.remove(p);
  159. dispose(p,done);
  160. p:=hp2;
  161. continue;
  162. end
  163. Else If (LabDif <> 0) Then GetFinalDestination(pai_labeled(p));
  164. end
  165. end;
  166. ait_instruction:
  167. Begin
  168. If (Pai386(p)^.op1t = top_ref) Then
  169. With TReference(Pai386(p)^.op1^) Do
  170. Begin
  171. If (base = R_NO) And
  172. (scalefactor = 1)
  173. Then
  174. Begin
  175. base := index;
  176. index := r_no
  177. End
  178. End;
  179. If (Pai386(p)^.op2t = top_ref) Then
  180. With TReference(Pai386(p)^.op2^) Do
  181. Begin
  182. If (base = R_NO) And
  183. (scalefactor = 1)
  184. Then
  185. Begin
  186. base := index;
  187. index := r_no
  188. End
  189. End;
  190. Case Pai386(p)^._operator Of
  191. A_AND:
  192. Begin
  193. If (Pai386(p)^.op1t = top_const) And
  194. (Pai386(p)^.op2t = top_reg) And
  195. GetNextInstruction(p, hp1) And
  196. (Pai(hp1)^.typ = ait_instruction) And
  197. (Pai386(hp1)^._operator = A_AND) And
  198. (Pai386(hp1)^.op1t = top_const) And
  199. (Pai386(hp1)^.op2t = top_reg) And
  200. (Pai386(hp1)^.op2 = Pai386(hp1)^.op2)
  201. Then
  202. {change "and const1, reg; and const2, reg" to "and (const1 and const2), reg"}
  203. Begin
  204. Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) And Longint(Pai386(hp1)^.op1));
  205. AsmL^.Remove(hp1);
  206. Dispose(hp1, Done)
  207. End
  208. Else
  209. {change "and x, reg; jxx" to "test x, reg", if reg is deallocated before the
  210. jump}
  211. If (Pai386(p)^.op2t = top_reg) And
  212. GetNextInstruction(p, hp1) And
  213. (hp1^.typ = ait_labeled_instruction) And
  214. Not(TRegister(Pai386(p)^.op2) in UsedRegs)
  215. Then Pai386(p)^._operator := A_TEST;
  216. End;
  217. A_CMP:
  218. Begin
  219. If (Pai386(p)^.op1t = top_const) And
  220. (Pai386(p)^.op2t = top_reg) And
  221. (Pai386(p)^.op1 = Pointer(0)) Then
  222. {change "cmp $0, %reg" to "test %reg, %reg"}
  223. Begin
  224. Pai386(p)^._operator := A_TEST;
  225. Pai386(p)^.opxt := Top_reg+Top_reg shl 4;
  226. Pai386(p)^.op1 := Pai386(p)^.op2;
  227. End;
  228. End;
  229. A_FLD:
  230. Begin
  231. If (Pai386(p)^.op1t = top_ref) And
  232. GetNextInstruction(p, hp2) And
  233. (hp2^.typ = Ait_Instruction) And
  234. (Pai386(hp2)^.Op1t = top_reg) And
  235. (Pai386(hp2)^.Op2t = top_reg) And
  236. (Pai386(p)^.Size in [S_FS, S_FL]) And
  237. (TRegister(Pai386(hp2)^.Op1) = R_ST) And
  238. (TRegister(Pai386(hp2)^.Op2) = R_ST1) Then
  239. If GetLastInstruction(p, hp1) And
  240. (hp1^.typ = Ait_Instruction) And
  241. ((Pai386(hp1)^._operator = A_FLD) Or
  242. (Pai386(hp1)^._operator = A_FST)) And
  243. (Pai386(hp1)^.size = Pai386(p)^.size) And
  244. (Pai386(hp1)^.op1t = top_ref) And
  245. RefsEqual(TReference(Pai386(p)^.Op1^), TReference(Pai386(hp1)^.Op1^)) Then
  246. If ((Pai386(hp2)^._operator = A_FMULP) Or
  247. (Pai386(hp2)^._operator = A_FADDP)) Then
  248. { change to
  249. fld/fst mem1 (hp1) fld/fst mem1
  250. fld mem1 (p) fadd/
  251. faddp/ fmul st, st
  252. fmulp st, st1 (hp2) }
  253. Begin
  254. AsmL^.Remove(p);
  255. Dispose(p, Done);
  256. p := hp1;
  257. If (Pai386(hp2)^._operator = A_FADDP)
  258. Then Pai386(hp2)^._operator := A_FADD
  259. Else Pai386(hp2)^._operator := A_FMUL;
  260. Pai386(hp2)^.op2 := Pointer(R_ST);
  261. End
  262. Else
  263. { change to
  264. fld/fst mem1 (hp1) fld/fst mem1
  265. fld mem1 (p) fld st}
  266. Begin
  267. Pai386(p)^.Size := S_FL;
  268. Clear_Reference(TReference(Pai386(p)^.Op1^));
  269. Pai386(p)^.Op1 := Pointer(R_ST);
  270. Pai386(p)^.Opxt := top_reg;
  271. End
  272. Else
  273. Begin
  274. Case Pai386(hp2)^._operator Of
  275. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  276. { change to
  277. fld/fst mem1 (hp1) fld/fst mem1
  278. fld mem2 (p) fxxx mem2
  279. fxxxp st, st1 (hp2) }
  280. Begin
  281. Case Pai386(hp2)^._operator Of
  282. A_FADDP: Pai386(p)^._operator := A_FADD;
  283. A_FMULP: Pai386(p)^._operator := A_FMUL;
  284. A_FSUBP: Pai386(p)^._operator := A_FSUBR;
  285. A_FSUBRP: Pai386(p)^._operator := A_FSUB;
  286. A_FDIVP: Pai386(p)^._operator := A_FDIVR;
  287. A_FDIVRP: Pai386(p)^._operator := A_FDIV;
  288. End;
  289. AsmL^.Remove(hp2);
  290. Dispose(hp2, Done)
  291. End
  292. End
  293. End
  294. End;
  295. A_FSTP:
  296. Begin
  297. If (Pai386(p)^.op1t = top_ref) And
  298. GetNextInstruction(p, hp1) And
  299. (Pai(hp1)^.typ = ait_instruction) And
  300. (Pai386(hp1)^._operator = A_FLD) And
  301. (Pai386(hp1)^.op1t = top_ref) And
  302. (Pai386(hp1)^.Size = Pai386(p)^.Size) And
  303. RefsEqual(TReference(Pai386(p)^.op1^), TReference(Pai386(hp1)^.op1^))
  304. Then
  305. Begin
  306. If GetNextInstruction(hp1, hp2) And
  307. (hp2^.typ = ait_instruction) And
  308. ((Pai386(hp2)^._operator = A_LEAVE) Or
  309. (Pai386(hp2)^._operator = A_RET)) And
  310. (TReference(Pai386(p)^.op1^).Base = ProcInfo.FramePointer) And
  311. (TReference(Pai386(p)^.op1^).Offset >= ProcInfo.RetOffset) And
  312. (TReference(Pai386(p)^.op1^).Index = R_NO)
  313. Then
  314. Begin
  315. AsmL^.Remove(p);
  316. AsmL^.Remove(hp1);
  317. Dispose(p, Done);
  318. Dispose(hp1, Done);
  319. p := hp2;
  320. Continue
  321. End
  322. Else
  323. {fst can't store an extended value!}
  324. If (Pai386(p)^.Size <> S_FX) Then
  325. Begin
  326. Pai386(p)^._operator := A_FST;
  327. AsmL^.Remove(hp1);
  328. Dispose(hp1, done)
  329. End
  330. End;
  331. End;
  332. A_IMUL:
  333. {changes certain "imul const, %reg"'s to lea sequences}
  334. Begin
  335. If (Pai386(p)^.op1t = Top_Const) And
  336. (Pai386(p)^.op2t = Top_Reg) And
  337. (Pai386(p)^.Size = S_L) Then
  338. If (Longint(Pai386(p)^.op1) = 1) Then
  339. If (Pai386(p)^.op3t = Top_None) Then
  340. {remove "imul $1, reg"}
  341. Begin
  342. hp1 := Pai(p^.Next);
  343. AsmL^.Remove(p);
  344. Dispose(p, Done);
  345. p := hp1;
  346. Continue;
  347. End
  348. Else
  349. {change "imul $1, reg1, reg2" to "mov reg1, reg2"}
  350. Begin
  351. hp1 := New(Pai386, Op_Reg_Reg(A_MOV, S_L, TRegister(TwoWords(Pai386(p)^.op2).Word1),
  352. TRegister(TwoWords(Pai386(p)^.op2).Word2)));
  353. hp1^.fileinfo := p^.fileinfo;
  354. InsertLLItem(AsmL, p^.previous, p^.next, hp1);
  355. Dispose(p, Done);
  356. p := hp1;
  357. End
  358. Else If
  359. ((Pai386(p)^.op3t = Top_Reg) or
  360. (Pai386(p)^.op3t = Top_None)) And
  361. (aktoptprocessor < ClassP6) And
  362. (Longint(Pai386(p)^.op1) <= 12) And
  363. Not(CS_LittleSize in aktglobalswitches) And
  364. (Not(GetNextInstruction(p, hp1)) Or
  365. {GetNextInstruction(p, hp1) And}
  366. Not((Pai(hp1)^.typ = ait_labeled_instruction) And
  367. ((pai_labeled(hp1)^._operator = A_JO) or
  368. (pai_labeled(hp1)^._operator = A_JNO))))
  369. Then
  370. Begin
  371. New(TmpRef);
  372. TmpRef^.segment := R_DEFAULT_SEG;
  373. TmpRef^.symbol := nil;
  374. TmpRef^.isintvalue := false;
  375. TmpRef^.offset := 0;
  376. Case Longint(Pai386(p)^.op1) Of
  377. 3: Begin
  378. {imul 3, reg1, reg2 to
  379. lea (reg1,reg1,2), reg2
  380. imul 3, reg1 to
  381. lea (reg1,reg1,2), reg1}
  382. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).Word1);
  383. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  384. TmpRef^.ScaleFactor := 2;
  385. If (Pai386(p)^.op3t = Top_None)
  386. Then hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, TRegister(Pai386(p)^.op2)))
  387. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  388. TRegister(twowords(Pai386(p)^.op2).word2)));
  389. hp1^.fileinfo := p^.fileinfo;
  390. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  391. Dispose(p, Done);
  392. p := hp1;
  393. End;
  394. 5: Begin
  395. {imul 5, reg1, reg2 to
  396. lea (reg1,reg1,4), reg2
  397. imul 5, reg1 to
  398. lea (reg1,reg1,4), reg1}
  399. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).Word1);
  400. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  401. TmpRef^.ScaleFactor := 4;
  402. If (Pai386(p)^.op3t = Top_None)
  403. Then hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, TRegister(Pai386(p)^.op2)))
  404. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  405. TRegister(twowords(Pai386(p)^.op2).word2)));
  406. hp1^.fileinfo:= p^.fileinfo;
  407. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  408. Dispose(p, Done);
  409. p := hp1;
  410. End;
  411. 6: Begin
  412. {imul 6, reg1, reg2 to
  413. lea (,reg1,2), reg2
  414. lea (reg2,reg1,4), reg2
  415. imul 6, reg1 to
  416. lea (reg1,reg1,2), reg1
  417. add reg1, reg1}
  418. If (aktoptprocessor <= Class386)
  419. Then
  420. Begin
  421. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  422. If (Pai386(p)^.op3t = Top_Reg)
  423. Then
  424. Begin
  425. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).word2);
  426. TmpRef^.ScaleFactor := 4;
  427. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  428. TRegister(twowords(Pai386(p)^.op2).word2)));
  429. End
  430. Else
  431. Begin
  432. Dispose(TmpRef);
  433. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  434. TRegister(Pai386(p)^.op2),TRegister(Pai386(p)^.op2)));
  435. End;
  436. hp1^.fileinfo := p^.fileinfo;
  437. InsertLLItem(AsmL,p, p^.next, hp1);
  438. New(TmpRef);
  439. TmpRef^.segment := R_DEFAULT_SEG;
  440. TmpRef^.symbol := nil;
  441. TmpRef^.isintvalue := false;
  442. TmpRef^.offset := 0;
  443. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  444. TmpRef^.ScaleFactor := 2;
  445. If (Pai386(p)^.op3t = Top_Reg)
  446. Then
  447. Begin
  448. TmpRef^.base := R_NO;
  449. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  450. TRegister(twowords(Pai386(p)^.op2).word2)));
  451. End
  452. Else
  453. Begin
  454. TmpRef^.base := TRegister(Pai386(p)^.op2);
  455. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, TRegister(Pai386(p)^.op2)));
  456. End;
  457. hp1^.fileinfo := p^.fileinfo;
  458. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  459. Dispose(p, Done);
  460. p := Pai(hp1^.next);
  461. End
  462. Else Dispose(TmpRef);
  463. End;
  464. 9: Begin
  465. {imul 9, reg1, reg2 to
  466. lea (reg1,reg1,8), reg2
  467. imul 9, reg1 to
  468. lea (reg1,reg1,8), reg1}
  469. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).Word1);
  470. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  471. TmpRef^.ScaleFactor := 8;
  472. If (Pai386(p)^.op3t = Top_None)
  473. Then hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, TRegister(Pai386(p)^.op2)))
  474. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  475. TRegister(twowords(Pai386(p)^.op2).word2)));
  476. hp1^.fileinfo := p^.fileinfo;
  477. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  478. Dispose(p, Done);
  479. p := hp1;
  480. End;
  481. 10: Begin
  482. {imul 10, reg1, reg2 to
  483. lea (reg1,reg1,4), reg2
  484. add reg2, reg2
  485. imul 10, reg1 to
  486. lea (reg1,reg1,4), reg1
  487. add reg1, reg1}
  488. If (aktoptprocessor <= Class386) Then
  489. Begin
  490. If (Pai386(p)^.op3t = Top_Reg)
  491. Then
  492. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  493. Tregister(twowords(Pai386(p)^.op2).word2),
  494. Tregister(twowords(Pai386(p)^.op2).word2)))
  495. Else hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  496. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)));
  497. hp1^.fileinfo := p^.fileinfo;
  498. InsertLLItem(AsmL,p, p^.next, hp1);
  499. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).Word1);
  500. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  501. TmpRef^.ScaleFactor := 4;
  502. If (Pai386(p)^.op3t = Top_Reg)
  503. Then
  504. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  505. TRegister(twowords(Pai386(p)^.op2).word2)))
  506. Else
  507. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  508. TRegister(Pai386(p)^.op2)));
  509. hp1^.fileinfo := p^.fileinfo;
  510. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  511. Dispose(p, Done);
  512. p := Pai(hp1^.next);
  513. End
  514. Else Dispose(TmpRef);
  515. End;
  516. 12: Begin
  517. {imul 12, reg1, reg2 to
  518. lea (,reg1,4), reg2
  519. lea (,reg1,8) reg2
  520. imul 12, reg1 to
  521. lea (reg1,reg1,2), reg1
  522. lea (,reg1,4), reg1}
  523. If (aktoptprocessor <= Class386)
  524. Then
  525. Begin
  526. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  527. If (Pai386(p)^.op3t = Top_Reg)
  528. Then
  529. Begin
  530. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).word2);
  531. TmpRef^.ScaleFactor := 8;
  532. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  533. TRegister(twowords(Pai386(p)^.op2).word2)));
  534. End
  535. Else
  536. Begin
  537. TmpRef^.base := R_NO;
  538. TmpRef^.ScaleFactor := 4;
  539. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  540. TRegister(Pai386(p)^.op2)));
  541. End;
  542. hp1^.fileinfo := p^.fileinfo;
  543. InsertLLItem(AsmL,p, p^.next, hp1);
  544. New(TmpRef);
  545. TmpRef^.segment := R_DEFAULT_SEG;
  546. TmpRef^.symbol := nil;
  547. TmpRef^.isintvalue := false;
  548. TmpRef^.offset := 0;
  549. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  550. If (Pai386(p)^.op3t = Top_Reg)
  551. Then
  552. Begin
  553. TmpRef^.base := R_NO;
  554. TmpRef^.ScaleFactor := 4;
  555. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  556. TRegister(twowords(Pai386(p)^.op2).word2)));
  557. End
  558. Else
  559. Begin
  560. TmpRef^.base := TRegister(Pai386(p)^.op2);
  561. TmpRef^.ScaleFactor := 2;
  562. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  563. TRegister(Pai386(p)^.op2)));
  564. End;
  565. hp1^.fileinfo := p^.fileinfo;
  566. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  567. Dispose(p, Done);
  568. p := Pai(hp1^.next);
  569. End
  570. Else Dispose(TmpRef);
  571. End
  572. Else Dispose(TmpRef);
  573. End;
  574. End;
  575. End;
  576. A_LEA:
  577. Begin
  578. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  579. If (PReference(Pai386(p)^.op1)^.Base In [R_EAX..R_EDI]) And
  580. (PReference(Pai386(p)^.op1)^.Index = R_NO) And
  581. (PReference(Pai386(p)^.op1)^.Offset = 0) And
  582. (Not(Assigned(PReference(Pai386(p)^.op1)^.Symbol))) Then
  583. If (PReference(Pai386(p)^.op1)^.Base <> TRegister(Pai386(p)^.op2))
  584. Then
  585. Begin
  586. hp1 := New(Pai386, op_reg_reg(A_MOV, S_L,PReference(Pai386(p)^.op1)^.Base,
  587. TRegister(Pai386(p)^.op2)));
  588. hp1^.fileinfo := p^.fileinfo;
  589. InsertLLItem(AsmL,p^.previous,p^.next, hp1);
  590. Dispose(p, Done);
  591. p := hp1;
  592. Continue;
  593. End
  594. Else
  595. Begin
  596. hp1 := Pai(p^.Next);
  597. AsmL^.Remove(p);
  598. Dispose(p, Done);
  599. p := hp1;
  600. Continue;
  601. End;
  602. End;
  603. A_MOV:
  604. Begin
  605. TmpUsedRegs := UsedRegs;
  606. If (Pai386(p)^.op2t = top_reg) And
  607. (TRegister(Pai386(p)^.op2) In [R_EAX, R_EBX, R_EDX, R_EDI]) And
  608. GetNextInstruction(p, hp1) And
  609. (Pai(hp1)^.typ = ait_instruction) And
  610. (Pai386(hp1)^._operator = A_MOV) And
  611. (Pai386(hp1)^.op1t = top_reg) And
  612. (Pai386(hp1)^.op1 = Pai386(p)^.op2)
  613. Then
  614. {we have "mov x, %treg; mov %treg, y}
  615. If (Pai386(hp1)^.op2t <> top_reg) Or
  616. (GetNextInstruction(hp1, hp2) And
  617. (RegUsedAfterInstruction(TRegister(Pai386(hp1)^.op2), hp1, TmpUsedRegs) or
  618. {now TmpUsedRegs contains the regalloc data after hp1}
  619. (RegInInstruction(TRegister(Pai386(hp1)^.op2), hp2))) And
  620. Not(TRegister(Pai386(hp1)^.op1) in TmpUsedRegs))
  621. Then
  622. {we've got "mov x, %treg; mov %treg, y; XXX y" (ie. y is used in
  623. the third instruction)}
  624. Case Pai386(p)^.op1t Of
  625. top_reg:
  626. {change "mov %reg, %treg; mov %treg, y"
  627. to "mov %reg, y"}
  628. Begin
  629. Pai386(hp1)^.op1 := Pai386(p)^.op1;
  630. AsmL^.Remove(p);
  631. Dispose(p, Done);
  632. p := hp1;
  633. continue;
  634. End;
  635. top_ref:
  636. If (Pai386(hp1)^.op2t = top_reg)
  637. Then
  638. {change "mov mem, %treg; mov %treg, %reg"
  639. to "mov mem, %reg"}
  640. Begin
  641. Pai386(p)^.op2 := Pai386(hp1)^.op2;
  642. AsmL^.Remove(hp1);
  643. Dispose(hp1, Done);
  644. continue;
  645. End;
  646. End
  647. Else
  648. {remove an instruction which never makes sense: we've got
  649. "mov mem, %reg1; mov %reg1, %edi" and then EDI isn't used anymore!}
  650. { Begin
  651. If (TRegister(Pai386(hp1)^.op2) = R_EDI) And
  652. Not(GetNextInstruction(hp1, hp2) And
  653. (Pai(hp2)^.typ = ait_instruction) And
  654. (Pai386(hp2)^.op2t = top_reg) And
  655. (Pai386(hp2)^.op2 = Pointer(R_ESI))) Then
  656. Begin
  657. AsmL^.Remove(hp1);
  658. Dispose(hp1, Done);
  659. Continue;
  660. End
  661. End}
  662. Else
  663. {Change "mov %reg1, %reg2; xxx %reg2, ???" to
  664. "mov %reg1, %reg2; xxx %reg1, ???" to avoid a write/read
  665. penalty}
  666. If (Pai386(p)^.op1t = top_reg) And
  667. (Pai386(p)^.op2t = top_reg) And
  668. GetNextInstruction(p,hp1) And
  669. (Pai(hp1)^.typ = ait_instruction) And
  670. (Pai386(hp1)^.op1t = top_reg) And
  671. (Pai386(hp1)^.op1 = Pai386(p)^.op2)
  672. Then
  673. {we have "mov %reg1, %reg2; XXX %reg2, ???"}
  674. Begin
  675. If ((Pai386(hp1)^._operator = A_OR) Or
  676. (Pai386(hp1)^._operator = A_TEST)) And
  677. (Pai386(hp1)^.op2t = top_reg) And
  678. (Pai386(hp1)^.op1 = Pai386(hp1)^.op2)
  679. Then
  680. {we have "mov %reg1, %reg2; test/or %reg2, %reg2"}
  681. Begin
  682. TmpUsedRegs := UsedRegs;
  683. If GetNextInstruction(hp1, hp2) And
  684. (hp2^.typ = ait_labeled_instruction) And
  685. Not(RegUsedAfterInstruction(TRegister(Pai386(hp1)^.op1), hp1, TmpUsedRegs))
  686. Then
  687. {change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  688. "test %reg1, %reg1; jxx"}
  689. Begin
  690. Pai386(hp1)^.op1 := Pai386(p)^.op1;
  691. Pai386(hp1)^.op2 := Pai386(p)^.op1;
  692. AsmL^.Remove(p);
  693. Dispose(p, done);
  694. p := hp1;
  695. continue
  696. End
  697. Else
  698. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  699. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  700. Begin
  701. Pai386(hp1)^.op1 := Pai386(p)^.op1;
  702. Pai386(hp1)^.op2 := Pai386(p)^.op1;
  703. End;
  704. End
  705. { Else
  706. If (Pai386(p^.next)^._operator
  707. In [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  708. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  709. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  710. End
  711. Else
  712. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  713. x >= RetOffset) as it doesn't do anything (it writes either to a
  714. parameter or to the temporary storage room for the function
  715. result)}
  716. If GetNextInstruction(p, hp1) And
  717. (Pai(hp1)^.typ = ait_instruction)
  718. Then
  719. If ((Pai386(hp1)^._operator = A_LEAVE) Or
  720. (Pai386(hp1)^._operator = A_RET)) And
  721. (Pai386(p)^.op2t = top_ref) And
  722. (TReference(Pai386(p)^.op2^).base = ProcInfo.FramePointer) And
  723. (TReference(Pai386(p)^.op2^).offset >= ProcInfo.RetOffset) And
  724. (TReference(Pai386(p)^.op2^).index = R_NO) And
  725. (Pai386(p)^.op1t = top_reg)
  726. Then
  727. Begin
  728. AsmL^.Remove(p);
  729. Dispose(p, done);
  730. p := hp1;
  731. End
  732. Else
  733. If (Pai386(p)^.op1t = top_reg) And
  734. (Pai386(p)^.op2t = top_ref) And
  735. (Pai386(p)^.Size = Pai386(hp1)^.Size) And
  736. (Pai386(hp1)^._operator = A_CMP) And
  737. (Pai386(hp1)^.op2t = top_ref) And
  738. RefsEqual(TReference(Pai386(p)^.op2^),
  739. TReference(Pai386(hp1)^.op2^))
  740. Then
  741. {change "mov reg, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  742. Begin
  743. Dispose(PReference(Pai386(hp1)^.op2));
  744. Pai386(hp1)^.opxt := Pai386(hp1)^.op1t + (top_reg shl 4);
  745. Pai386(hp1)^.op2 := Pai386(p)^.op1
  746. End;
  747. { Next instruction is also a MOV ? }
  748. If GetNextInstruction(p, hp1) And
  749. (pai(hp1)^.typ = ait_instruction) and
  750. (Pai386(hp1)^._operator = A_MOV)
  751. Then
  752. Begin
  753. If (Pai386(hp1)^.op1t = Pai386(p)^.op2t) and
  754. (Pai386(hp1)^.op2t = Pai386(p)^.op1t)
  755. Then
  756. {mov reg1, mem1 or mov mem1, reg1
  757. mov mem2, reg2 mov reg2, mem2}
  758. Begin
  759. If (Pai386(hp1)^.op2t = top_ref)
  760. Then
  761. TmpBool1 := RefsEqual(TReference(Pai386(hp1)^.op2^), TReference(Pai386(p)^.op1^))
  762. Else
  763. TmpBool1 := Pai386(hp1)^.op2 = Pai386(p)^.op1;
  764. If TmpBool1
  765. Then
  766. {mov reg1, mem1 or mov mem1, reg1
  767. mov mem2, reg1 mov reg2, mem1}
  768. Begin
  769. If (Pai386(hp1)^.op1t = top_ref)
  770. Then
  771. TmpBool1 := RefsEqual(TReference(Pai386(hp1)^.op1^),
  772. TReference(Pai386(p)^.op2^))
  773. Else TmpBool1 := (Pai386(hp1)^.op1 = Pai386(p)^.op2);
  774. If TmpBool1 Then
  775. { Removes the second statement from
  776. mov reg1, mem1
  777. mov mem1, reg1 }
  778. Begin
  779. AsmL^.remove(hp1);
  780. Dispose(hp1,done);
  781. End;
  782. End
  783. Else
  784. Begin
  785. If GetNextInstruction(hp1, hp2) And
  786. (Pai386(p)^.op1t = top_ref) And
  787. (Pai386(p)^.op2t = top_reg) And
  788. (Pai386(hp1)^.op1t = top_reg) And
  789. (Pai386(hp1)^.op1 = Pai386(p)^.op2) And
  790. (Pai386(hp1)^.op2t = top_ref) And
  791. (Pai(hp2)^.typ = ait_instruction) And
  792. (Pai386(hp2)^._operator = A_MOV) And
  793. (Pai386(hp2)^.op2t = top_reg) And
  794. (Pai386(hp2)^.op1t = top_ref) And
  795. RefsEqual(TReference(Pai386(hp2)^.op1^),
  796. TReference(Pai386(hp1)^.op2^))
  797. Then
  798. If (TRegister(Pai386(p)^.op2) = R_EDI)
  799. Then
  800. { mov mem1, %edi
  801. mov %edi, mem2
  802. mov mem2, reg2
  803. to:
  804. mov mem1, reg2
  805. mov reg2, mem2}
  806. Begin
  807. Pai386(p)^.op2 := Pai386(hp2)^.op2;
  808. Pai386(hp1)^.op1 := Pai386(hp2)^.op2;
  809. AsmL^.Remove(hp2);
  810. Dispose(hp2,Done);
  811. End
  812. Else
  813. { mov mem1, reg1 mov mem1, reg1
  814. mov reg1, mem2 mov reg1, mem2
  815. mov mem2, reg2 mov mem2, reg1
  816. to: to:
  817. mov mem1, reg1 mov mem1, reg1
  818. mov mem1, reg2 mov reg1, mem2
  819. mov reg1, mem2}
  820. Begin
  821. If (Pai386(p)^.op2 <> Pai386(hp2)^.op2) Then
  822. Begin
  823. Pai386(hp1)^.opxt := top_ref + top_reg shl 4;
  824. If Assigned(TReference(Pai386(hp1)^.op2^).Symbol)
  825. Then Freemem(TReference(Pai386(hp1)^.op2^).Symbol,
  826. Length(TReference(Pai386(hp1)^.op2^).Symbol^)+1);
  827. Pai386(hp1)^.op1 := Pai386(hp1)^.op2; {move the treference}
  828. TReference(Pai386(hp1)^.op1^) := TReference(Pai386(p)^.op1^);
  829. If Assigned(TReference(Pai386(p)^.op1^).Symbol) Then
  830. Begin
  831. Getmem(TReference(Pai386(hp1)^.op1^).Symbol,
  832. Length(TReference(Pai386(p)^.op1^).Symbol^)+1);
  833. TReference(Pai386(hp1)^.op1^).Symbol^ :=
  834. TReference(Pai386(p)^.op1^).Symbol^;
  835. End;
  836. Pai386(hp1)^.op2 := Pai386(hp2)^.op2;
  837. End
  838. Else
  839. Begin
  840. AsmL^.Remove(hp1);
  841. Dispose(hp1, Done)
  842. End;
  843. Pai386(hp2)^.opxt := top_reg + top_ref shl 4;
  844. Pai386(hp2)^.op2 := Pai386(hp2)^.op1;
  845. Pai386(hp2)^.op1 := Pai386(p)^.op2;
  846. End;
  847. End;
  848. End
  849. Else
  850. (* {movl [mem1],reg1
  851. movl [mem1],reg2
  852. to:
  853. movl [mem1],reg1
  854. movl reg1,reg2 }
  855. If (Pai386(p)^.op1t = top_ref) and
  856. (Pai386(p)^.op2t = top_reg) and
  857. (Pai386(hp1)^.op1t = top_ref) and
  858. (Pai386(hp1)^.op2t = top_reg) and
  859. (Pai386(p)^.size = Pai386(hp1)^.size) and
  860. RefsEqual(TReference(Pai386(p)^.op1^),TReference(Pai386(hp1)^.op1^)) and
  861. (TRegister(Pai386(p)^.op2)<>TReference(Pai386(hp1)^.op1^).base) and
  862. (TRegister(Pai386(p)^.op2)<>TReference(Pai386(hp1)^.op1^).index) then
  863. Begin
  864. Dispose(PReference(Pai386(hp1)^.op1));
  865. Pai386(hp1)^.op1:=Pai386(p)^.op2;
  866. Pai386(hp1)^.opxt:=Top_reg+Top_reg shl 4;
  867. End
  868. Else*)
  869. { movl const1,[mem1]
  870. movl [mem1],reg1
  871. to:
  872. movl const1,reg1
  873. movl reg1,[mem1] }
  874. If (Pai386(p)^.op1t = top_const) and
  875. (Pai386(p)^.op2t = top_ref) and
  876. (Pai386(hp1)^.op1t = top_ref) and
  877. (Pai386(hp1)^.op2t = top_reg) and
  878. (Pai386(p)^.size = Pai386(hp1)^.size) and
  879. RefsEqual(TReference(Pai386(hp1)^.op1^),TReference(Pai386(p)^.op2^)) then
  880. Begin
  881. Pai386(hp1)^.op1:=Pai386(hp1)^.op2;
  882. Pai386(hp1)^.op2:=Pai386(p)^.op2;
  883. Pai386(hp1)^.opxt:=Top_reg+Top_ref shl 4;
  884. Pai386(p)^.op2:=Pai386(hp1)^.op1;
  885. Pai386(p)^.opxt:=Top_const+(top_reg shl 4);
  886. End
  887. End;
  888. {changes "mov $0, %reg" into "xor %reg, %reg"}
  889. If (Pai386(p)^.op1t = Top_Const) And
  890. (Pai386(p)^.op1 = Pointer(0)) And
  891. (Pai386(p)^.op2t = Top_Reg)
  892. Then
  893. Begin
  894. Pai386(p)^._operator := A_XOR;
  895. Pai386(p)^.opxt := Top_Reg+Top_reg shl 4;
  896. Pai386(p)^.op1 := Pai386(p)^.op2;
  897. End;
  898. End;
  899. A_MOVZX:
  900. Begin
  901. {removes superfluous And's after movzx's}
  902. If (Pai386(p)^.op2t = top_reg) And
  903. GetNextInstruction(p, hp1) And
  904. (Pai(hp1)^.typ = ait_instruction) And
  905. (Pai386(hp1)^._operator = A_AND) And
  906. (Pai386(hp1)^.op1t = top_const) And
  907. (Pai386(hp1)^.op2t = top_reg) And
  908. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  909. Then
  910. Case Pai386(p)^.Size Of
  911. S_BL, S_BW:
  912. If (Longint(Pai386(hp1)^.op1) = $ff)
  913. Then
  914. Begin
  915. AsmL^.Remove(hp1);
  916. Dispose(hp1, Done);
  917. End;
  918. S_WL:
  919. If (Longint(Pai386(hp1)^.op1) = $ffff)
  920. Then
  921. Begin
  922. AsmL^.Remove(hp1);
  923. Dispose(hp1, Done);
  924. End;
  925. End;
  926. {changes some movzx constructs to faster synonims (all examples
  927. are given with eax/ax, but are also valid for other registers)}
  928. If (Pai386(p)^.op2t = top_reg) Then
  929. If (Pai386(p)^.op1t = top_reg)
  930. Then
  931. Case Pai386(p)^.size of
  932. S_BW:
  933. Begin
  934. If (TRegister(Pai386(p)^.op1) = Reg16ToReg8(TRegister(Pai386(p)^.op2))) And
  935. Not(CS_LittleSize In aktglobalswitches)
  936. Then
  937. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  938. Begin
  939. Pai386(p)^._operator := A_AND;
  940. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  941. Longint(Pai386(p)^.op1) := $ff;
  942. Pai386(p)^.Size := S_W
  943. End
  944. Else
  945. If GetNextInstruction(p, hp1) And
  946. (Pai(hp1)^.typ = ait_instruction) And
  947. (Pai386(hp1)^._operator = A_AND) And
  948. (Pai386(hp1)^.op1t = top_const) And
  949. (Pai386(hp1)^.op2t = top_reg) And
  950. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  951. Then
  952. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  953. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  954. Begin
  955. Pai386(p)^._operator := A_MOV;
  956. Pai386(p)^.Size := S_W;
  957. Pai386(p)^.op1 := Pointer(Reg8ToReg16(TRegister(Pai386(p)^.op1)));
  958. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ff);
  959. End;
  960. End;
  961. S_BL:
  962. Begin
  963. If (TRegister(Pai386(p)^.op1) = Reg32ToReg8(TRegister(Pai386(p)^.op2))) And
  964. Not(CS_LittleSize in aktglobalswitches)
  965. Then
  966. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  967. Begin
  968. Pai386(p)^._operator := A_AND;
  969. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  970. Longint(Pai386(p)^.op1) := $ff;
  971. Pai386(p)^.Size := S_L;
  972. End
  973. Else
  974. If GetNextInstruction(p, hp1) And
  975. (Pai(hp1)^.typ = ait_instruction) And
  976. (Pai386(hp1)^._operator = A_AND) And
  977. (Pai386(hp1)^.op1t = top_const) And
  978. (Pai386(hp1)^.op2t = top_reg) And
  979. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  980. Then
  981. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  982. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  983. Begin
  984. Pai386(p)^._operator := A_MOV;
  985. Pai386(p)^.Size := S_L;
  986. Pai386(p)^.op1 := Pointer(Reg8ToReg32(TRegister(Pai386(p)^.op1)));
  987. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ff);
  988. End
  989. End;
  990. S_WL:
  991. Begin
  992. If (TRegister(Pai386(p)^.op1) = Reg32ToReg16(TRegister(Pai386(p)^.op2))) And
  993. Not(CS_LittleSize In aktglobalswitches)
  994. Then
  995. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  996. Begin
  997. Pai386(p)^._operator := A_AND;
  998. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  999. Longint(Pai386(p)^.op1) := $ffff;
  1000. Pai386(p)^.Size := S_L
  1001. End
  1002. Else
  1003. If GetNextInstruction(p, hp1) And
  1004. (Pai(hp1)^.typ = ait_instruction) And
  1005. (Pai386(hp1)^._operator = A_AND) And
  1006. (Pai386(hp1)^.op1t = top_const) And
  1007. (Pai386(hp1)^.op2t = top_reg) And
  1008. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  1009. Then
  1010. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  1011. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  1012. Begin
  1013. Pai386(p)^._operator := A_MOV;
  1014. Pai386(p)^.Size := S_L;
  1015. Pai386(p)^.op1 := Pointer(Reg16ToReg32(TRegister(Pai386(p)^.op1)));
  1016. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ffff);
  1017. End;
  1018. End;
  1019. End
  1020. Else
  1021. If (Pai386(p)^.op1t = top_ref) Then
  1022. Begin
  1023. If GetNextInstruction(p, hp1) And
  1024. (Pai(hp1)^.typ = ait_instruction) And
  1025. (Pai386(hp1)^._operator = A_AND) And
  1026. (Pai386(hp1)^.op1t = Top_Const) And
  1027. (Pai386(hp1)^.op2t = Top_Reg) And
  1028. (Pai386(hp1)^.op2 = Pai386(p)^.op2) Then
  1029. Begin
  1030. Pai386(p)^._operator := A_MOV;
  1031. Case Pai386(p)^.Size Of
  1032. S_BL:
  1033. Begin
  1034. Pai386(p)^.Size := S_L;
  1035. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  1036. And $ff);
  1037. End;
  1038. S_WL:
  1039. Begin
  1040. Pai386(p)^.Size := S_L;
  1041. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  1042. And $ffff);
  1043. End;
  1044. S_BW:
  1045. Begin
  1046. Pai386(p)^.Size := S_W;
  1047. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  1048. And $ff);
  1049. End;
  1050. End;
  1051. End;
  1052. End;
  1053. End;
  1054. A_POP:
  1055. Begin
  1056. if (Pai386(p)^.op1t = top_reg) And
  1057. GetNextInstruction(p, hp1) And
  1058. (pai(hp1)^.typ=ait_instruction) and
  1059. (Pai386(hp1)^._operator=A_PUSH) and
  1060. (Pai386(hp1)^.op1t = top_reg) And
  1061. (Pai386(hp1)^.op1=Pai386(p)^.op1) then
  1062. If (Not(cs_regalloc in aktglobalswitches)) Then
  1063. Begin
  1064. hp2:=pai(hp1^.next);
  1065. asml^.remove(p);
  1066. asml^.remove(hp1);
  1067. dispose(p,done);
  1068. dispose(hp1,done);
  1069. p:=hp2;
  1070. continue
  1071. End
  1072. Else
  1073. Begin
  1074. Pai386(p)^._operator := A_MOV;
  1075. Pai386(p)^.op2 := Pai386(p)^.op1;
  1076. Pai386(p)^.opxt := top_ref + top_reg shl 4;
  1077. New(TmpRef);
  1078. TmpRef^.segment := R_DEFAULT_SEG;
  1079. TmpRef^.base := R_ESP;
  1080. TmpRef^.index := R_NO;
  1081. TmpRef^.scalefactor := 1;
  1082. TmpRef^.symbol := nil;
  1083. TmpRef^.isintvalue := false;
  1084. TmpRef^.offset := 0;
  1085. Pai386(p)^.op1 := Pointer(TmpRef);
  1086. hp1 := Pai(p^.next);
  1087. AsmL^.Remove(hp1);
  1088. Dispose(hp1, Done)
  1089. End
  1090. end;
  1091. A_PUSH:
  1092. Begin
  1093. If (Pai386(p)^.size = S_W) And
  1094. (Pai386(p)^.op1t = Top_Const) And
  1095. GetNextInstruction(p, hp1) And
  1096. (Pai(hp1)^.typ = ait_instruction) And
  1097. (Pai386(hp1)^._operator = A_PUSH) And
  1098. (Pai386(hp1)^.op1t = Top_Const) And
  1099. (Pai386(hp1)^.size = S_W) Then
  1100. Begin
  1101. Pai386(p)^.Size := S_L;
  1102. Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) shl 16 + Longint(Pai386(hp1)^.op1));
  1103. AsmL^.Remove(hp1);
  1104. Dispose(hp1, Done)
  1105. End;
  1106. End;
  1107. A_SHL, A_SAL:
  1108. Begin
  1109. If (Pai386(p)^.op1t = Top_Const) And
  1110. (Pai386(p)^.op2t = Top_Reg) And
  1111. (Pai386(p)^.Size = S_L) And
  1112. (Longint(Pai386(p)^.op1) <= 3)
  1113. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1114. Then
  1115. Begin
  1116. TmpBool1 := True; {should we check the next instruction?}
  1117. TmpBool2 := False; {have we found an add/sub which could be
  1118. integrated in the lea?}
  1119. New(TmpRef);
  1120. TmpRef^.segment := R_DEFAULT_SEG;
  1121. TmpRef^.base := R_NO;
  1122. TmpRef^.index := TRegister(Pai386(p)^.op2);
  1123. TmpRef^.scalefactor := 1 shl Longint(Pai386(p)^.op1);
  1124. TmpRef^.symbol := nil;
  1125. TmpRef^.isintvalue := false;
  1126. TmpRef^.offset := 0;
  1127. While TmpBool1 And
  1128. GetNextInstruction(p, hp1) And
  1129. (Pai(hp1)^.typ = ait_instruction) And
  1130. ((Pai386(hp1)^._operator = A_ADD) Or
  1131. (Pai386(hp1)^._operator = A_SUB)) And
  1132. (Pai386(hp1)^.op2t = Top_Reg) And
  1133. (Pai386(hp1)^.op2 = Pai386(p)^.op2) Do
  1134. Begin
  1135. TmpBool1 := False;
  1136. If (Pai386(hp1)^.op1t = Top_Const)
  1137. Then
  1138. Begin
  1139. TmpBool1 := True;
  1140. TmpBool2 := True;
  1141. If Pai386(hp1)^._operator = A_ADD
  1142. Then Inc(TmpRef^.offset, Longint(Pai386(hp1)^.op1))
  1143. Else Dec(TmpRef^.offset, Longint(Pai386(hp1)^.op1));
  1144. AsmL^.Remove(hp1);
  1145. Dispose(hp1, Done);
  1146. End
  1147. Else
  1148. If (Pai386(hp1)^.op1t = Top_Reg) And
  1149. (Pai386(hp1)^._operator = A_ADD) And
  1150. (TmpRef^.base = R_NO) Then
  1151. Begin
  1152. TmpBool1 := True;
  1153. TmpBool2 := True;
  1154. TmpRef^.base := TRegister(Pai386(hp1)^.op1);
  1155. AsmL^.Remove(hp1);
  1156. Dispose(hp1, Done);
  1157. End;
  1158. End;
  1159. If TmpBool2 Or
  1160. ((aktoptprocessor < ClassP6) And
  1161. (Longint(Pai386(p)^.op1) <= 3) And
  1162. Not(CS_LittleSize in aktglobalswitches))
  1163. Then
  1164. Begin
  1165. If Not(TmpBool2) And
  1166. (Longint(Pai386(p)^.op1) = 1)
  1167. Then
  1168. Begin
  1169. Dispose(TmpRef);
  1170. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.Size,
  1171. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)))
  1172. End
  1173. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  1174. TRegister(Pai386(p)^.op2)));
  1175. hp1^.fileinfo := p^.fileinfo;
  1176. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1177. Dispose(p, Done);
  1178. p := hp1;
  1179. End;
  1180. End
  1181. Else
  1182. If (aktoptprocessor < ClassP6) And
  1183. (Pai386(p)^.op1t = top_const) And
  1184. (Pai386(p)^.op2t = top_reg) Then
  1185. If (Longint(Pai386(p)^.op1) = 1)
  1186. Then
  1187. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1188. but faster on a 486, and pairable in both U and V pipes on the Pentium
  1189. (unlike shl, which is only pairable in the U pipe)}
  1190. Begin
  1191. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.Size,
  1192. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)));
  1193. hp1^.fileinfo := p^.fileinfo;
  1194. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1195. Dispose(p, done);
  1196. p := hp1;
  1197. End
  1198. Else If (Pai386(p)^.size = S_L) and
  1199. (Longint(Pai386(p)^.op1) <= 3) Then
  1200. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1201. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1202. Begin
  1203. New(TmpRef);
  1204. TmpRef^.segment := R_DEFAULT_SEG;
  1205. TmpRef^.base := R_NO;
  1206. TmpRef^.index := TRegister(Pai386(p)^.op2);
  1207. TmpRef^.scalefactor := 1 shl Longint(Pai386(p)^.op1);
  1208. TmpRef^.symbol := nil;
  1209. TmpRef^.isintvalue := false;
  1210. TmpRef^.offset := 0;
  1211. hp1 := new(Pai386,op_ref_reg(A_LEA,S_L,TmpRef, TRegister(Pai386(p)^.op2)));
  1212. hp1^.fileinfo := p^.fileinfo;
  1213. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1214. Dispose(p, done);
  1215. p := hp1;
  1216. End
  1217. End;
  1218. A_SAR, A_SHR:
  1219. {changes the code sequence
  1220. shr/sar const1, %reg
  1221. shl const2, %reg
  1222. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  1223. Begin
  1224. If GetNextInstruction(p, hp1) And
  1225. (pai(hp1)^.typ = ait_instruction) and
  1226. (Pai386(hp1)^._operator = A_SHL) and
  1227. (Pai386(p)^.op1t = top_const) and
  1228. (Pai386(hp1)^.op1t = top_const)
  1229. Then
  1230. If (Longint(Pai386(p)^.op1) > Longint(Pai386(hp1)^.op1)) And
  1231. (Pai386(p)^.op2t = Top_reg) And
  1232. Not(CS_LittleSize In aktglobalswitches) And
  1233. ((Pai386(p)^.Size = S_B) Or
  1234. (Pai386(p)^.Size = S_L))
  1235. Then
  1236. Begin
  1237. Dec(Longint(Pai386(p)^.op1), Longint(Pai386(hp1)^.op1));
  1238. Pai386(hp1)^._operator := A_And;
  1239. Pai386(hp1)^.op1 := Pointer(1 shl Longint(Pai386(hp1)^.op1)-1);
  1240. If (Pai386(p)^.Size = S_L)
  1241. Then Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffffffff)
  1242. Else Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ff);
  1243. End
  1244. Else
  1245. If (Longint(Pai386(p)^.op1) < Longint(Pai386(hp1)^.op1)) And
  1246. (Pai386(p)^.op2t = Top_reg) And
  1247. Not(CS_LittleSize In aktglobalswitches) And
  1248. ((Pai386(p)^.Size = S_B) Or
  1249. (Pai386(p)^.Size = S_L))
  1250. Then
  1251. Begin
  1252. Dec(Longint(Pai386(hp1)^.op1), Longint(Pai386(p)^.op1));
  1253. Pai386(p)^._operator := A_And;
  1254. Pai386(p)^.op1 := Pointer(1 shl Longint(Pai386(p)^.op1)-1);
  1255. If (Pai386(p)^.Size = S_L)
  1256. Then Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffffffff)
  1257. Else Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ff);
  1258. End
  1259. Else
  1260. Begin
  1261. Pai386(p)^._operator := A_And;
  1262. Pai386(p)^.op1 := Pointer(1 shl Longint(Pai386(p)^.op1)-1);
  1263. Case Pai386(p)^.Size Of
  1264. S_B: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ff);
  1265. S_W: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffff);
  1266. S_L: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor
  1267. $ffffffff);
  1268. End;
  1269. AsmL^.remove(hp1);
  1270. dispose(hp1, done);
  1271. End;
  1272. End;
  1273. {$IfDef Ver0_99_11}
  1274. A_SETE..A_SETGE,A_SETC,A_SETNC,A_SETA..A_SETBE,A_SETO..A_SETNLE:
  1275. Begin
  1276. If (Pai386(p)^.Op1t = top_ref) And
  1277. GetNextInstruction(p, hp1) And
  1278. GetNextInstruction(hp1, hp2) And
  1279. (hp2^.typ = ait_instruction) And
  1280. ((Pai386(hp2)^._operator = A_LEAVE) or
  1281. (Pai386(hp2)^._operator = A_RET])) And
  1282. (TReference(Pai386(p)^.Op1^).Base = ProcInfo.FramePointer) And
  1283. (TReference(Pai386(p)^.Op1^).Index = R_NO) And
  1284. (TReference(Pai386(p)^.Op1^).Offset >= ProcInfo.RetOffset) And
  1285. (hp1^.typ = ait_instruction) And
  1286. (Pai386(hp1)^._operator = A_MOV) And
  1287. (Pai386(hp1)^.Size = S_B) And
  1288. (Pai386(hp1)^.Op1t = top_ref) And
  1289. RefsEqual(TReference(Pai386(hp1)^.Op1^), TReference(Pai386(p)^.Op1^)) Then
  1290. Begin
  1291. Dispose(PReference(Pai386(p)^.Op1));
  1292. Pai386(p)^.Op1 := Pai386(hp1)^.Op2;
  1293. Pai386(p)^.Opxt := top_reg;
  1294. AsmL^.Remove(hp1);
  1295. Dispose(hp1, Done)
  1296. End
  1297. End;
  1298. {$EndIf Ver0_99_11}
  1299. A_SUB:
  1300. {change "subl $2, %esp; pushw x" to "pushl x"}
  1301. Begin
  1302. If (Pai386(p)^.op1t = top_const) And
  1303. (Longint(Pai386(p)^.op1) = 2) And
  1304. (Pai386(p)^.op2t = top_reg) And
  1305. (TRegister(Pai386(p)^.op2) = R_ESP)
  1306. Then
  1307. Begin
  1308. hp1 := Pai(p^.next);
  1309. While Assigned(hp1) And
  1310. (Pai(hp1)^.typ In [ait_instruction]+SkipInstr) And
  1311. Not((Pai(hp1)^.typ = ait_instruction) And
  1312. ((Pai386(hp1)^._operator = A_CALL) or
  1313. (Pai386(hp1)^._operator = A_PUSH) or
  1314. ((Pai386(hp1)^._operator = A_MOV) And
  1315. (Pai386(hp1)^.op2t = top_ref) And
  1316. (TReference(Pai386(hp1)^.op2^).base = R_ESP)))) do
  1317. hp1 := Pai(hp1^.next);
  1318. If Assigned(hp1) And
  1319. (Pai(hp1)^.typ = ait_instruction) And
  1320. (Pai386(hp1)^._operator = A_PUSH) And
  1321. (Pai386(hp1)^.Size = S_W)
  1322. Then
  1323. Begin
  1324. Pai386(hp1)^.size := S_L;
  1325. If (Pai386(hp1)^.op1t = top_reg) Then
  1326. Pai386(hp1)^.op1 := Pointer(Reg16ToReg32(TRegister(Pai386(hp1)^.op1)));
  1327. hp1 := Pai(p^.next);
  1328. AsmL^.Remove(p);
  1329. Dispose(p, Done);
  1330. p := hp1;
  1331. Continue
  1332. End
  1333. Else
  1334. If GetLastInstruction(p, hp1) And
  1335. (Pai(hp1)^.typ = ait_instruction) And
  1336. (Pai386(hp1)^._operator = A_SUB) And
  1337. (Pai386(hp1)^.op1t = top_const) And
  1338. (Pai386(hp1)^.op2t = top_reg) And
  1339. (TRegister(Pai386(hp1)^.Op2) = R_ESP)
  1340. Then
  1341. Begin
  1342. Inc(Longint(Pai386(p)^.op1), Longint(Pai386(hp1)^.op1));
  1343. AsmL^.Remove(hp1);
  1344. Dispose(hp1, Done);
  1345. End;
  1346. End;
  1347. End;
  1348. A_TEST, A_OR:
  1349. {removes the line marked with (x) from the sequence
  1350. And/or/xor/add/sub/... $x, %y
  1351. test/or %y, %y (x)
  1352. j(n)z _Label
  1353. as the first instruction already adjusts the ZF}
  1354. Begin
  1355. If (Pai386(p)^.op1 = Pai386(p)^.op2) And
  1356. GetLastInstruction(p, hp1) And
  1357. (pai(hp1)^.typ = ait_instruction) Then
  1358. Case Pai386(hp1)^._operator Of
  1359. A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_SHL, A_SHR:
  1360. Begin
  1361. If (Pai386(hp1)^.op2 = Pai386(p)^.op1) Then
  1362. Begin
  1363. hp1 := pai(p^.next);
  1364. asml^.remove(p);
  1365. dispose(p, done);
  1366. p := pai(hp1);
  1367. continue
  1368. End;
  1369. End;
  1370. A_DEC, A_INC, A_NEG:
  1371. Begin
  1372. If (Pai386(hp1)^.op1 = Pai386(p)^.op1) Then
  1373. Begin
  1374. hp1 := pai(p^.next);
  1375. asml^.remove(p);
  1376. dispose(p, done);
  1377. p := pai(hp1);
  1378. continue
  1379. End;
  1380. End
  1381. End;
  1382. End;
  1383. End;
  1384. End;
  1385. { ait_label:
  1386. Begin
  1387. If Not(Pai_Label(p)^.l^.is_used)
  1388. Then
  1389. Begin
  1390. hp1 := Pai(p^.next);
  1391. AsmL^.Remove(p);
  1392. Dispose(p, Done);
  1393. p := hp1;
  1394. Continue
  1395. End;
  1396. End;}
  1397. End;
  1398. p:=pai(p^.next);
  1399. end;
  1400. end;
  1401. Procedure PeepHoleOptPass2(AsmL: PAasmOutput);
  1402. var
  1403. p,hp1,hp2: pai;
  1404. Begin
  1405. P := Pai(AsmL^.First);
  1406. While Assigned(p) Do
  1407. Begin
  1408. Case P^.Typ Of
  1409. Ait_Instruction:
  1410. Begin
  1411. Case Pai386(p)^._operator Of
  1412. A_CALL:
  1413. If (AktOptProcessor < ClassP6) And
  1414. GetNextInstruction(p, hp1) And
  1415. (hp1^.typ = ait_labeled_instruction) And
  1416. (Pai_Labeled(hp1)^._operator = A_JMP) Then
  1417. Begin
  1418. hp2 := New(Pai386,op_csymbol(A_PUSH,S_L,NewCSymbol(Lab2Str(Pai_Labeled(hp1)^.lab),0)));
  1419. hp2^.fileinfo := p^.fileinfo;
  1420. InsertLLItem(AsmL, p^.previous, p, hp2);
  1421. Pai386(p)^._operator := A_JMP;
  1422. AsmL^.Remove(hp1);
  1423. Dispose(hp1, Done)
  1424. End;
  1425. A_MOV:
  1426. Begin
  1427. If (Pai386(p)^.op1t = top_reg) And
  1428. (Pai386(p)^.op2t = top_reg) And
  1429. GetNextInstruction(p, hp1) And
  1430. (hp1^.typ = ait_Instruction) And
  1431. (Pai386(hp1)^._operator = A_MOV) And
  1432. (Pai386(hp1)^.op1t = top_ref) And
  1433. (Pai386(hp1)^.op2t = top_reg) And
  1434. ((TReference(Pai386(hp1)^.op1^).Base = TRegister(Pai386(p)^.op2)) Or
  1435. (TReference(Pai386(hp1)^.op1^).Index = TRegister(Pai386(p)^.op2))) And
  1436. (TRegister(Pai386(hp1)^.op2) = TRegister(Pai386(p)^.op2)) Then
  1437. {mov reg1, reg2
  1438. mov (reg2, ..), reg2 to mov (reg1, ..), reg2}
  1439. Begin
  1440. If (TReference(Pai386(hp1)^.op1^).Base = TRegister(Pai386(p)^.op2)) Then
  1441. TReference(Pai386(hp1)^.op1^).Base := TRegister(Pai386(p)^.op1);
  1442. If (TReference(Pai386(hp1)^.op1^).Index = TRegister(Pai386(p)^.op2)) Then
  1443. TReference(Pai386(hp1)^.op1^).Index := TRegister(Pai386(p)^.op1);
  1444. AsmL^.Remove(p);
  1445. Dispose(p, Done);
  1446. p := hp1;
  1447. Continue;
  1448. End;
  1449. End;
  1450. A_MOVZX:
  1451. Begin
  1452. If (Pai386(p)^.op2t = top_reg) Then
  1453. If (Pai386(p)^.op1t = top_reg)
  1454. Then
  1455. Case Pai386(p)^.size of
  1456. S_BL:
  1457. Begin
  1458. If IsGP32Reg(TRegister(Pai386(p)^.op2)) And
  1459. Not(CS_LittleSize in aktglobalswitches) And
  1460. (aktoptprocessor = ClassP5)
  1461. Then
  1462. {Change "movzbl %reg1, %reg2" to
  1463. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1464. PentiumMMX}
  1465. Begin
  1466. hp1 := New(Pai386, op_reg_reg(A_XOR, S_L,
  1467. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)));
  1468. hp1^.fileinfo := p^.fileinfo;
  1469. InsertLLItem(AsmL,p^.previous, p, hp1);
  1470. Pai386(p)^._operator := A_MOV;
  1471. Pai386(p)^.size := S_B;
  1472. Pai386(p)^.op2 :=
  1473. Pointer(Reg32ToReg8(TRegister(Pai386(p)^.op2)));
  1474. { Jonas
  1475. InsertLLItem(AsmL,p, p^.next, hp2);
  1476. I think you forgot to delete this line PM
  1477. Indeed, I had forgotten that one (JM) }
  1478. End;
  1479. End;
  1480. End
  1481. Else
  1482. If (Pai386(p)^.op1t = top_ref) And
  1483. (PReference(Pai386(p)^.op1)^.base <> TRegister(Pai386(p)^.op2)) And
  1484. (PReference(Pai386(p)^.op1)^.index <> TRegister(Pai386(p)^.op2)) And
  1485. Not(CS_LittleSize in aktglobalswitches) And
  1486. IsGP32Reg(TRegister(Pai386(p)^.op2)) And
  1487. (aktoptprocessor = ClassP5) And
  1488. (Pai386(p)^.Size = S_BL)
  1489. Then
  1490. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1491. Pentium and PentiumMMX}
  1492. Begin
  1493. hp1 := New(Pai386,op_reg_reg(A_XOR, S_L, TRegister(Pai386(p)^.op2),
  1494. TRegister(Pai386(p)^.op2)));
  1495. hp1^.fileinfo := p^.fileinfo;
  1496. Pai386(p)^._operator := A_MOV;
  1497. Pai386(p)^.size := S_B;
  1498. Pai386(p)^.op2 := Pointer(Reg32ToReg8(TRegister(Pai386(p)^.op2)));
  1499. InsertLLItem(AsmL,p^.previous, p, hp1);
  1500. End;
  1501. End;
  1502. End;
  1503. End;
  1504. End;
  1505. p := Pai(p^.next)
  1506. End;
  1507. End;
  1508. End.
  1509. {
  1510. $Log$
  1511. Revision 1.25 1998-12-02 16:23:29 jonas
  1512. * changed "if longintvar in set" to case or "if () or () .." statements
  1513. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  1514. Revision 1.24 1998/11/26 15:41:45 jonas
  1515. + change "setxx mem; movb mem, reg8" to "setxx reg8" if mem is a local
  1516. variable/parameter or function result (between {$ifdef ver0_99_11})
  1517. Revision 1.23 1998/11/03 16:26:09 jonas
  1518. * "call x;jmp y" optimization not done anymore for P6 and equivalents
  1519. * made FPU optimizations simpler and more effective
  1520. Revision 1.22 1998/10/29 18:37:55 jonas
  1521. + change "call x; jmp y" to "push y; jmp x" (suggestion from Daniel)
  1522. Revision 1.19 1998/10/23 15:38:23 jonas
  1523. + some small FPU peephole optimizations (use value in FP regs instead of loading it
  1524. from memory if possible, mostly with var1+var1 and var1*var1)
  1525. Revision 1.18 1998/10/05 14:41:14 jonas
  1526. * fixed small memory leak
  1527. * fixed small inefficiency
  1528. * tested multiple line comments ability of my new MacCVS client :)
  1529. Revision 1.17 1998/10/02 17:29:56 jonas
  1530. + removal of "lea (reg), reg)", "imul $1, reg", change "mov reg1, reg2; mov (reg2), reg2" to "mov (reg1), reg2"
  1531. Revision 1.16 1998/10/01 20:19:57 jonas
  1532. * moved UpdateUsedRegs (+ bugfix) to daopt386
  1533. Revision 1.15 1998/09/30 12:18:29 peter
  1534. * fixed subl $2,esp;psuhw bug
  1535. Revision 1.14 1998/09/20 17:11:51 jonas
  1536. * released REGALLOC
  1537. Revision 1.13 1998/09/16 18:00:00 jonas
  1538. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  1539. Revision 1.12 1998/09/15 14:05:22 jonas
  1540. * fixed optimizer incompatibilities with freelabel code in psub
  1541. Revision 1.11 1998/08/28 10:57:02 peter
  1542. * removed warnings
  1543. Revision 1.10 1998/08/27 15:17:50 florian
  1544. * reinstated Jonas' bugfix
  1545. Revision 1.9 1998/08/25 16:58:59 pierre
  1546. * removed a line that add no sense and
  1547. introduce garbage in the asmlist
  1548. (uninitialized data !)
  1549. Revision 1.7 1998/08/19 16:07:53 jonas
  1550. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1551. Revision 1.6 1998/08/10 14:50:14 peter
  1552. + localswitches, moduleswitches, globalswitches splitting
  1553. Revision 1.5 1998/08/06 19:40:28 jonas
  1554. * removed $ before and after Log in comment
  1555. Revision 1.4 1998/08/05 16:27:17 jonas
  1556. * fstp/fld bugfix (fstt does not exist)
  1557. Revision 1.3 1998/08/05 16:00:15 florian
  1558. * some fixes for ansi strings
  1559. * log to Log changed
  1560. }