popt386.pas 86 KB

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