popt386.pas 82 KB

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