popt386.pas 83 KB

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