popt386.pas 84 KB

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