popt386.pas 75 KB

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