popt386.pas 90 KB

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