popt386.pas 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$ifdef newOptimizations}
  20. {$define foropt}
  21. {$define replacereg}
  22. {$define arithopt}
  23. {$define foldarithops}
  24. {$endif newOptimizations}
  25. Interface
  26. Uses Aasm;
  27. Procedure PeepHoleOptPass1(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  28. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  29. Implementation
  30. Uses
  31. globtype,systems,
  32. globals,verbose,hcodegen,
  33. {$ifdef finaldestdebug}
  34. cobjects,
  35. {$endif finaldestdebug}
  36. cpubase,cpuasm,DAOpt386,tgeni386;
  37. Function RegUsedAfterInstruction(Reg: TRegister; p: Pai; Var UsedRegs: TRegSet): Boolean;
  38. Begin
  39. reg := reg32(reg);
  40. UpdateUsedRegs(UsedRegs, Pai(p^.Next));
  41. RegUsedAfterInstruction :=
  42. (Reg in UsedRegs) and
  43. (not(getNextInstruction(p,p)) or
  44. not(regLoadedWithNewValue(reg,false,p)));
  45. End;
  46. function doFpuLoadStoreOpt(asmL: paasmoutput; var p: pai): boolean;
  47. { returns true if a "continue" should be done after this optimization }
  48. var hp1, hp2: pai;
  49. begin
  50. doFpuLoadStoreOpt := false;
  51. if (paicpu(p)^.oper[0].typ = top_ref) and
  52. getNextInstruction(p, hp1) and
  53. (hp1^.typ = ait_instruction) and
  54. (((paicpu(hp1)^.opcode = A_FLD) and
  55. (paicpu(p)^.opcode = A_FSTP)) or
  56. ((paicpu(p)^.opcode = A_FISTP) and
  57. (paicpu(hp1)^.opcode = A_FILD))) and
  58. (paicpu(hp1)^.oper[0].typ = top_ref) and
  59. (paicpu(hp1)^.opsize = Paicpu(p)^.opsize) and
  60. refsEqual(paicpu(p)^.oper[0].ref^, paicpu(hp1)^.oper[0].ref^) then
  61. begin
  62. if getNextInstruction(hp1, hp2) and
  63. (hp2^.typ = ait_instruction) and
  64. ((paicpu(hp2)^.opcode = A_LEAVE) or
  65. (paicpu(hp2)^.opcode = A_RET)) and
  66. (paicpu(p)^.oper[0].ref^.Base = procinfo^.FramePointer) and
  67. (paicpu(p)^.oper[0].ref^.Offset >= procinfo^.Return_Offset) and
  68. (paicpu(p)^.oper[0].ref^.Index = R_NO) then
  69. begin
  70. asmL^.remove(p);
  71. asmL^.remove(hp1);
  72. dispose(p, done);
  73. dispose(hp1, done);
  74. p := hp2;
  75. removeLastDeallocForFuncRes(asmL, p);
  76. doFPULoadStoreOpt := true;
  77. end
  78. else
  79. { fst can't store an extended value! }
  80. if (paicpu(p)^.opsize <> S_FX) and
  81. (paicpu(p)^.opsize <> S_IQ) then
  82. begin
  83. if (paicpu(p)^.opcode = A_FSTP) then
  84. paicpu(p)^.opcode := A_FST
  85. else Paicpu(p)^.opcode := A_FIST;
  86. asmL^.remove(hp1);
  87. dispose(hp1, done)
  88. end
  89. end;
  90. end;
  91. Procedure PeepHoleOptPass1(Asml: PAasmOutput; BlockStart, BlockEnd: Pai);
  92. {First pass of peepholeoptimizations}
  93. Var
  94. l : longint;
  95. p,hp1,hp2 : pai;
  96. {$ifdef foropt}
  97. hp3,hp4: pai;
  98. {$endif foropt}
  99. TmpBool1, TmpBool2: Boolean;
  100. TmpRef: TReference;
  101. UsedRegs, TmpUsedRegs: TRegSet;
  102. Function SkipLabels(hp: Pai; var hp2: pai): boolean;
  103. {skips all labels and returns the next "real" instruction}
  104. Begin
  105. While assigned(hp^.next) and
  106. (pai(hp^.next)^.typ In SkipInstr + [ait_label,ait_align]) Do
  107. hp := pai(hp^.next);
  108. If assigned(hp^.next) Then
  109. Begin
  110. SkipLabels := True;
  111. hp2 := pai(hp^.next)
  112. End
  113. Else
  114. Begin
  115. hp2 := hp;
  116. SkipLabels := False
  117. End;
  118. End;
  119. Procedure GetFinalDestination(AsmL: PAAsmOutput; hp: paicpu);
  120. {traces sucessive jumps to their final destination and sets it, e.g.
  121. je l1 je l3
  122. <code> <code>
  123. l1: becomes l1:
  124. je l2 je l3
  125. <code> <code>
  126. l2: l2:
  127. jmp l3 jmp l3}
  128. Var p1, p2: pai;
  129. l: pasmlabel;
  130. Function FindAnyLabel(hp: pai; var l: pasmlabel): Boolean;
  131. Begin
  132. FindAnyLabel := false;
  133. While assigned(hp^.next) and
  134. (pai(hp^.next)^.typ In (SkipInstr+[ait_align])) Do
  135. hp := pai(hp^.next);
  136. If assigned(hp^.next) and
  137. (pai(hp^.next)^.typ = ait_label) Then
  138. Begin
  139. FindAnyLabel := true;
  140. l := pai_label(hp^.next)^.l;
  141. End
  142. End;
  143. Begin
  144. If (pasmlabel(hp^.oper[0].sym)^.labelnr >= LoLab) and
  145. (pasmlabel(hp^.oper[0].sym)^.labelnr <= HiLab) and {range check, a jump can go past an assembler block!}
  146. Assigned(LTable^[pasmlabel(hp^.oper[0].sym)^.labelnr-LoLab].PaiObj) Then
  147. Begin
  148. p1 := LTable^[pasmlabel(hp^.oper[0].sym)^.labelnr-LoLab].PaiObj; {the jump's destination}
  149. SkipLabels(p1,p1);
  150. If (pai(p1)^.typ = ait_instruction) and
  151. (paicpu(p1)^.is_jmp) Then
  152. If { the next instruction after the label where the jump hp arrives}
  153. { is unconditional or of the same type as hp, so continue }
  154. (paicpu(p1)^.condition in [C_None,hp^.condition]) or
  155. { the next instruction after the label where the jump hp arrives}
  156. { is the opposite of hp (so this one is never taken), but after }
  157. { that one there is a branch that will be taken, so perform a }
  158. { little hack: set p1 equal to this instruction (that's what the}
  159. { last SkipLabels is for, only works with short bool evaluation)}
  160. ((paicpu(p1)^.condition = inverse_cond[hp^.condition]) and
  161. SkipLabels(p1,p2) and
  162. (p2^.typ = ait_instruction) and
  163. (paicpu(p2)^.is_jmp) and
  164. (paicpu(p2)^.condition in [C_None,hp^.condition]) and
  165. SkipLabels(p1,p1)) Then
  166. Begin
  167. GetFinalDestination(asml, paicpu(p1));
  168. Dec(pasmlabel(hp^.oper[0].sym)^.refs);
  169. hp^.oper[0].sym:=paicpu(p1)^.oper[0].sym;
  170. inc(pasmlabel(hp^.oper[0].sym)^.refs);
  171. End
  172. Else
  173. If (paicpu(p1)^.condition = inverse_cond[hp^.condition]) then
  174. if not FindAnyLabel(p1,l) then
  175. begin
  176. {$ifdef finaldestdebug}
  177. insertllitem(asml,p1,p1^.next,new(pai_asm_comment,init(
  178. strpnew('previous label inserted'))));
  179. {$endif finaldestdebug}
  180. getlabel(l);
  181. insertllitem(asml,p1,p1^.next,new(pai_label,init(l)));
  182. dec(pasmlabel(paicpu(hp)^.oper[0].sym)^.refs);
  183. hp^.oper[0].sym := l;
  184. inc(l^.refs);
  185. { this won't work, since the new label isn't in the labeltable }
  186. { so it will fail the rangecheck. Labeltable should become a }
  187. { hashtable to support this: }
  188. { GetFinalDestination(asml, hp); }
  189. end
  190. else
  191. begin
  192. {$ifdef finaldestdebug}
  193. insertllitem(asml,p1,p1^.next,new(pai_asm_comment,init(
  194. strpnew('next label reused'))));
  195. {$endif finaldestdebug}
  196. inc(l^.refs);
  197. hp^.oper[0].sym := l;
  198. GetFinalDestination(asml, hp);
  199. end;
  200. End;
  201. End;
  202. Function DoSubAddOpt(var p: Pai): Boolean;
  203. Begin
  204. DoSubAddOpt := False;
  205. If GetLastInstruction(p, hp1) And
  206. (hp1^.typ = ait_instruction) And
  207. (Paicpu(hp1)^.opsize = Paicpu(p)^.opsize) then
  208. Case Paicpu(hp1)^.opcode Of
  209. A_DEC:
  210. If (Paicpu(hp1)^.oper[0].typ = top_reg) And
  211. (Paicpu(hp1)^.oper[0].reg = Paicpu(p)^.oper[1].reg) Then
  212. Begin
  213. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val+1);
  214. AsmL^.Remove(hp1);
  215. Dispose(hp1, Done)
  216. End;
  217. A_SUB:
  218. If (Paicpu(hp1)^.oper[0].typ = top_const) And
  219. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  220. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg) Then
  221. Begin
  222. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val+Paicpu(hp1)^.oper[0].val);
  223. AsmL^.Remove(hp1);
  224. Dispose(hp1, Done)
  225. End;
  226. A_ADD:
  227. If (Paicpu(hp1)^.oper[0].typ = top_const) And
  228. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  229. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg) Then
  230. Begin
  231. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val-Paicpu(hp1)^.oper[0].val);
  232. AsmL^.Remove(hp1);
  233. Dispose(hp1, Done);
  234. If (Paicpu(p)^.oper[0].val = 0) Then
  235. Begin
  236. hp1 := Pai(p^.next);
  237. AsmL^.Remove(p);
  238. Dispose(p, Done);
  239. If Not GetLastInstruction(hp1, p) Then
  240. p := hp1;
  241. DoSubAddOpt := True;
  242. End
  243. End;
  244. End;
  245. End;
  246. Begin
  247. P := BlockStart;
  248. UsedRegs := [];
  249. While (P <> BlockEnd) Do
  250. Begin
  251. UpDateUsedRegs(UsedRegs, Pai(p^.next));
  252. Case P^.Typ Of
  253. ait_instruction:
  254. Begin
  255. { Handle Jmp Optimizations }
  256. if Paicpu(p)^.is_jmp then
  257. begin
  258. {the following if-block removes all code between a jmp and the next label,
  259. because it can never be executed}
  260. If (paicpu(p)^.opcode = A_JMP) Then
  261. Begin
  262. While GetNextInstruction(p, hp1) and
  263. ((hp1^.typ <> ait_label) or
  264. { skip unused labels, they're not referenced anywhere }
  265. Not(Pai_Label(hp1)^.l^.is_used)) Do
  266. If not(hp1^.typ in ([ait_label,ait_align]+skipinstr)) Then
  267. Begin
  268. AsmL^.Remove(hp1);
  269. Dispose(hp1, done);
  270. End;
  271. End;
  272. If GetNextInstruction(p, hp1) then
  273. Begin
  274. if FindLabel(pasmlabel(paicpu(p)^.oper[0].sym), hp1) then
  275. Begin
  276. hp2:=pai(hp1^.next);
  277. asml^.remove(p);
  278. dispose(p,done);
  279. p:=hp2;
  280. continue;
  281. end
  282. Else
  283. Begin
  284. if hp1^.typ = ait_label then
  285. SkipLabels(hp1,hp1);
  286. If (pai(hp1)^.typ=ait_instruction) and
  287. (paicpu(hp1)^.opcode=A_JMP) and
  288. GetNextInstruction(hp1, hp2) And
  289. FindLabel(PAsmLabel(paicpu(p)^.oper[0].sym), hp2)
  290. Then
  291. Begin
  292. if paicpu(p)^.opcode=A_Jcc then
  293. paicpu(p)^.condition:=inverse_cond[paicpu(p)^.condition]
  294. else
  295. begin
  296. If (LabDif <> 0) Then
  297. GetFinalDestination(asml, paicpu(p));
  298. p:=pai(p^.next);
  299. continue;
  300. end;
  301. Dec(pai_label(hp2)^.l^.refs);
  302. paicpu(p)^.oper[0].sym:=paicpu(hp1)^.oper[0].sym;
  303. Inc(paicpu(p)^.oper[0].sym^.refs);
  304. asml^.remove(hp1);
  305. dispose(hp1,done);
  306. If (LabDif <> 0) Then
  307. GetFinalDestination(asml, paicpu(p));
  308. end
  309. else
  310. If (LabDif <> 0) Then
  311. GetFinalDestination(asml, paicpu(p));
  312. end;
  313. end;
  314. end
  315. else
  316. { All other optimizes }
  317. begin
  318. For l := 0 to 2 Do
  319. If (Paicpu(p)^.oper[l].typ = top_ref) Then
  320. With Paicpu(p)^.oper[l].ref^ Do
  321. Begin
  322. If (base = R_NO) And
  323. (index <> R_NO) And
  324. (scalefactor in [0,1])
  325. Then
  326. Begin
  327. base := index;
  328. index := R_NO
  329. End
  330. End;
  331. Case Paicpu(p)^.opcode Of
  332. A_AND:
  333. Begin
  334. If (Paicpu(p)^.oper[0].typ = top_const) And
  335. (Paicpu(p)^.oper[1].typ = top_reg) And
  336. GetNextInstruction(p, hp1) And
  337. (Pai(hp1)^.typ = ait_instruction) And
  338. (Paicpu(hp1)^.opcode = A_AND) And
  339. (Paicpu(hp1)^.oper[0].typ = top_const) And
  340. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  341. (Paicpu(hp1)^.oper[1].reg = Paicpu(hp1)^.oper[1].reg)
  342. Then
  343. {change "and const1, reg; and const2, reg" to "and (const1 and const2), reg"}
  344. Begin
  345. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val And Paicpu(hp1)^.oper[0].val);
  346. AsmL^.Remove(hp1);
  347. Dispose(hp1, Done)
  348. End
  349. Else
  350. {change "and x, reg; jxx" to "test x, reg", if reg is deallocated before the
  351. jump, but only if it's a conditional jump (PFV) }
  352. If (Paicpu(p)^.oper[1].typ = top_reg) And
  353. GetNextInstruction(p, hp1) And
  354. (hp1^.typ = ait_instruction) And
  355. (Paicpu(hp1)^.is_jmp) and
  356. (Paicpu(hp1)^.opcode<>A_JMP) and
  357. Not(Paicpu(p)^.oper[1].reg in UsedRegs) Then
  358. Paicpu(p)^.opcode := A_TEST;
  359. End;
  360. A_CMP:
  361. Begin
  362. If (Paicpu(p)^.oper[0].typ = top_const) And
  363. (Paicpu(p)^.oper[1].typ in [top_reg,top_ref]) And
  364. (Paicpu(p)^.oper[0].val = 0) Then
  365. {$ifdef foropt}
  366. If GetNextInstruction(p, hp1) And
  367. (hp1^.typ = ait_instruction) And
  368. (Paicpu(hp1)^.is_jmp) and
  369. (paicpu(hp1)^.opcode=A_Jcc) and
  370. (paicpu(hp1)^.condition in [C_LE,C_BE]) and
  371. GetNextInstruction(hp1,hp2) and
  372. (hp2^.typ = ait_instruction) and
  373. (Paicpu(hp2)^.opcode = A_DEC) And
  374. OpsEqual(Paicpu(hp2)^.oper[0],Paicpu(p)^.oper[1]) And
  375. GetNextInstruction(hp2, hp3) And
  376. (hp3^.typ = ait_instruction) and
  377. (Paicpu(hp3)^.is_jmp) and
  378. (Paicpu(hp3)^.opcode = A_JMP) And
  379. GetNextInstruction(hp3, hp4) And
  380. FindLabel(PAsmLabel(paicpu(hp1)^.oper[0].sym),hp4)
  381. Then
  382. Begin
  383. Paicpu(hp2)^.Opcode := A_SUB;
  384. Paicpu(hp2)^.Loadoper(1,Paicpu(hp2)^.oper[0]);
  385. Paicpu(hp2)^.LoadConst(0,1);
  386. Paicpu(hp2)^.ops:=2;
  387. Paicpu(hp3)^.Opcode := A_Jcc;
  388. Case paicpu(hp1)^.condition of
  389. C_LE: Paicpu(hp3)^.condition := C_GE;
  390. C_BE: Paicpu(hp3)^.condition := C_AE;
  391. End;
  392. AsmL^.Remove(p);
  393. AsmL^.Remove(hp1);
  394. Dispose(p, Done);
  395. Dispose(hp1, Done);
  396. p := hp2;
  397. continue;
  398. End
  399. Else
  400. {$endif foropt}
  401. {change "cmp $0, %reg" to "test %reg, %reg"}
  402. If (Paicpu(p)^.oper[1].typ = top_reg) Then
  403. Begin
  404. Paicpu(p)^.opcode := A_TEST;
  405. Paicpu(p)^.loadreg(0,Paicpu(p)^.oper[1].reg);
  406. End;
  407. End;
  408. A_FLD:
  409. Begin
  410. If (Paicpu(p)^.oper[0].typ = top_reg) And
  411. GetNextInstruction(p, hp1) And
  412. (hp1^.typ = Ait_Instruction) And
  413. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  414. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  415. (Paicpu(hp1)^.oper[0].reg = R_ST) And
  416. (Paicpu(hp1)^.oper[1].reg = R_ST1) Then
  417. { change to
  418. fld reg fxxx reg,st
  419. fxxxp st, st1 (hp1)
  420. Remark: non commutative operations must be reversed!
  421. }
  422. begin
  423. Case Paicpu(hp1)^.opcode Of
  424. A_FMULP,A_FADDP,
  425. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  426. begin
  427. Case Paicpu(hp1)^.opcode Of
  428. A_FADDP: Paicpu(hp1)^.opcode := A_FADD;
  429. A_FMULP: Paicpu(hp1)^.opcode := A_FMUL;
  430. A_FSUBP: Paicpu(hp1)^.opcode := A_FSUBR;
  431. A_FSUBRP: Paicpu(hp1)^.opcode := A_FSUB;
  432. A_FDIVP: Paicpu(hp1)^.opcode := A_FDIVR;
  433. A_FDIVRP: Paicpu(hp1)^.opcode := A_FDIV;
  434. End;
  435. Paicpu(hp1)^.oper[0].reg := Paicpu(p)^.oper[0].reg;
  436. Paicpu(hp1)^.oper[1].reg := R_ST;
  437. AsmL^.Remove(p);
  438. Dispose(p, Done);
  439. p := hp1;
  440. Continue;
  441. end;
  442. end;
  443. end
  444. else
  445. If (Paicpu(p)^.oper[0].typ = top_ref) And
  446. GetNextInstruction(p, hp2) And
  447. (hp2^.typ = Ait_Instruction) And
  448. (Paicpu(hp2)^.oper[0].typ = top_reg) And
  449. (Paicpu(hp2)^.oper[1].typ = top_reg) And
  450. (Paicpu(p)^.opsize in [S_FS, S_FL]) And
  451. (Paicpu(hp2)^.oper[0].reg = R_ST) And
  452. (Paicpu(hp2)^.oper[1].reg = R_ST1) Then
  453. If GetLastInstruction(p, hp1) And
  454. (hp1^.typ = Ait_Instruction) And
  455. ((Paicpu(hp1)^.opcode = A_FLD) Or
  456. (Paicpu(hp1)^.opcode = A_FST)) And
  457. (Paicpu(hp1)^.opsize = Paicpu(p)^.opsize) And
  458. (Paicpu(hp1)^.oper[0].typ = top_ref) And
  459. RefsEqual(Paicpu(p)^.oper[0].ref^, Paicpu(hp1)^.oper[0].ref^) Then
  460. If ((Paicpu(hp2)^.opcode = A_FMULP) Or
  461. (Paicpu(hp2)^.opcode = A_FADDP)) Then
  462. { change to
  463. fld/fst mem1 (hp1) fld/fst mem1
  464. fld mem1 (p) fadd/
  465. faddp/ fmul st, st
  466. fmulp st, st1 (hp2) }
  467. Begin
  468. AsmL^.Remove(p);
  469. Dispose(p, Done);
  470. p := hp1;
  471. If (Paicpu(hp2)^.opcode = A_FADDP) Then
  472. Paicpu(hp2)^.opcode := A_FADD
  473. Else
  474. Paicpu(hp2)^.opcode := A_FMUL;
  475. Paicpu(hp2)^.oper[1].reg := R_ST;
  476. End
  477. Else
  478. { change to
  479. fld/fst mem1 (hp1) fld/fst mem1
  480. fld mem1 (p) fld st}
  481. Begin
  482. Paicpu(p)^.changeopsize(S_FL);
  483. Paicpu(p)^.loadreg(0,R_ST);
  484. End
  485. Else
  486. Begin
  487. Case Paicpu(hp2)^.opcode Of
  488. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  489. { change to
  490. fld/fst mem1 (hp1) fld/fst mem1
  491. fld mem2 (p) fxxx mem2
  492. fxxxp st, st1 (hp2) }
  493. Begin
  494. Case Paicpu(hp2)^.opcode Of
  495. A_FADDP: Paicpu(p)^.opcode := A_FADD;
  496. A_FMULP: Paicpu(p)^.opcode := A_FMUL;
  497. A_FSUBP: Paicpu(p)^.opcode := A_FSUBR;
  498. A_FSUBRP: Paicpu(p)^.opcode := A_FSUB;
  499. A_FDIVP: Paicpu(p)^.opcode := A_FDIVR;
  500. A_FDIVRP: Paicpu(p)^.opcode := A_FDIV;
  501. End;
  502. AsmL^.Remove(hp2);
  503. Dispose(hp2, Done)
  504. End
  505. End
  506. End
  507. End;
  508. A_FSTP,A_FISTP:
  509. if doFpuLoadStoreOpt(asmL,p) then
  510. continue;
  511. A_IMUL:
  512. {changes certain "imul const, %reg"'s to lea sequences}
  513. Begin
  514. If (Paicpu(p)^.oper[0].typ = Top_Const) And
  515. (Paicpu(p)^.oper[1].typ = Top_Reg) And
  516. (Paicpu(p)^.opsize = S_L) Then
  517. If (Paicpu(p)^.oper[0].val = 1) Then
  518. If (Paicpu(p)^.oper[2].typ = Top_None) Then
  519. {remove "imul $1, reg"}
  520. Begin
  521. hp1 := Pai(p^.Next);
  522. AsmL^.Remove(p);
  523. Dispose(p, Done);
  524. p := hp1;
  525. Continue;
  526. End
  527. Else
  528. {change "imul $1, reg1, reg2" to "mov reg1, reg2"}
  529. Begin
  530. hp1 := New(Paicpu, Op_Reg_Reg(A_MOV, S_L, Paicpu(p)^.oper[1].reg,Paicpu(p)^.oper[2].reg));
  531. InsertLLItem(AsmL, p^.previous, p^.next, hp1);
  532. Dispose(p, Done);
  533. p := hp1;
  534. End
  535. Else If
  536. ((Paicpu(p)^.oper[2].typ = Top_Reg) or
  537. (Paicpu(p)^.oper[2].typ = Top_None)) And
  538. (aktoptprocessor < ClassP6) And
  539. (Paicpu(p)^.oper[0].val <= 12) And
  540. Not(CS_LittleSize in aktglobalswitches) And
  541. (Not(GetNextInstruction(p, hp1)) Or
  542. {GetNextInstruction(p, hp1) And}
  543. Not((Pai(hp1)^.typ = ait_instruction) And
  544. ((paicpu(hp1)^.opcode=A_Jcc) and
  545. (paicpu(hp1)^.condition in [C_O,C_NO]))))
  546. Then
  547. Begin
  548. Reset_reference(tmpref);
  549. Case Paicpu(p)^.oper[0].val Of
  550. 3: Begin
  551. {imul 3, reg1, reg2 to
  552. lea (reg1,reg1,2), reg2
  553. imul 3, reg1 to
  554. lea (reg1,reg1,2), reg1}
  555. TmpRef.base := Paicpu(p)^.oper[1].reg;
  556. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  557. TmpRef.ScaleFactor := 2;
  558. If (Paicpu(p)^.oper[2].typ = Top_None) Then
  559. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg))
  560. Else
  561. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  562. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  563. Dispose(p, Done);
  564. p := hp1;
  565. End;
  566. 5: Begin
  567. {imul 5, reg1, reg2 to
  568. lea (reg1,reg1,4), reg2
  569. imul 5, reg1 to
  570. lea (reg1,reg1,4), reg1}
  571. TmpRef.base := Paicpu(p)^.oper[1].reg;
  572. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  573. TmpRef.ScaleFactor := 4;
  574. If (Paicpu(p)^.oper[2].typ = Top_None) Then
  575. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg))
  576. Else
  577. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  578. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  579. Dispose(p, Done);
  580. p := hp1;
  581. End;
  582. 6: Begin
  583. {imul 6, reg1, reg2 to
  584. lea (,reg1,2), reg2
  585. lea (reg2,reg1,4), reg2
  586. imul 6, reg1 to
  587. lea (reg1,reg1,2), reg1
  588. add reg1, reg1}
  589. If (aktoptprocessor <= Class386)
  590. Then
  591. Begin
  592. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  593. If (Paicpu(p)^.oper[2].typ = Top_Reg)
  594. Then
  595. Begin
  596. TmpRef.base := Paicpu(p)^.oper[2].reg;
  597. TmpRef.ScaleFactor := 4;
  598. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  599. End
  600. Else
  601. Begin
  602. hp1 := New(Paicpu, op_reg_reg(A_ADD, S_L,
  603. Paicpu(p)^.oper[1].reg,Paicpu(p)^.oper[1].reg));
  604. End;
  605. InsertLLItem(AsmL,p, p^.next, hp1);
  606. Reset_reference(tmpref);
  607. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  608. TmpRef.ScaleFactor := 2;
  609. If (Paicpu(p)^.oper[2].typ = Top_Reg)
  610. Then
  611. Begin
  612. TmpRef.base := R_NO;
  613. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef),
  614. Paicpu(p)^.oper[2].reg));
  615. End
  616. Else
  617. Begin
  618. TmpRef.base := Paicpu(p)^.oper[1].reg;
  619. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  620. End;
  621. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  622. Dispose(p, Done);
  623. p := Pai(hp1^.next);
  624. End
  625. End;
  626. 9: Begin
  627. {imul 9, reg1, reg2 to
  628. lea (reg1,reg1,8), reg2
  629. imul 9, reg1 to
  630. lea (reg1,reg1,8), reg1}
  631. TmpRef.base := Paicpu(p)^.oper[1].reg;
  632. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  633. TmpRef.ScaleFactor := 8;
  634. If (Paicpu(p)^.oper[2].typ = Top_None) Then
  635. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg))
  636. Else
  637. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  638. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  639. Dispose(p, Done);
  640. p := hp1;
  641. End;
  642. 10: Begin
  643. {imul 10, reg1, reg2 to
  644. lea (reg1,reg1,4), reg2
  645. add reg2, reg2
  646. imul 10, reg1 to
  647. lea (reg1,reg1,4), reg1
  648. add reg1, reg1}
  649. If (aktoptprocessor <= Class386) Then
  650. Begin
  651. If (Paicpu(p)^.oper[2].typ = Top_Reg) Then
  652. hp1 := New(Paicpu, op_reg_reg(A_ADD, S_L,
  653. Paicpu(p)^.oper[2].reg,Paicpu(p)^.oper[2].reg))
  654. Else
  655. hp1 := New(Paicpu, op_reg_reg(A_ADD, S_L,
  656. Paicpu(p)^.oper[1].reg,Paicpu(p)^.oper[1].reg));
  657. InsertLLItem(AsmL,p, p^.next, hp1);
  658. TmpRef.base := Paicpu(p)^.oper[1].reg;
  659. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  660. TmpRef.ScaleFactor := 4;
  661. If (Paicpu(p)^.oper[2].typ = Top_Reg)
  662. Then
  663. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg))
  664. Else
  665. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  666. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  667. Dispose(p, Done);
  668. p := Pai(hp1^.next);
  669. End
  670. End;
  671. 12: Begin
  672. {imul 12, reg1, reg2 to
  673. lea (,reg1,4), reg2
  674. lea (,reg1,8) reg2
  675. imul 12, reg1 to
  676. lea (reg1,reg1,2), reg1
  677. lea (,reg1,4), reg1}
  678. If (aktoptprocessor <= Class386)
  679. Then
  680. Begin
  681. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  682. If (Paicpu(p)^.oper[2].typ = Top_Reg) Then
  683. Begin
  684. TmpRef.base := Paicpu(p)^.oper[2].reg;
  685. TmpRef.ScaleFactor := 8;
  686. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  687. End
  688. Else
  689. Begin
  690. TmpRef.base := R_NO;
  691. TmpRef.ScaleFactor := 4;
  692. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  693. End;
  694. InsertLLItem(AsmL,p, p^.next, hp1);
  695. Reset_reference(tmpref);
  696. TmpRef.Index := Paicpu(p)^.oper[1].reg;
  697. If (Paicpu(p)^.oper[2].typ = Top_Reg) Then
  698. Begin
  699. TmpRef.base := R_NO;
  700. TmpRef.ScaleFactor := 4;
  701. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[2].reg));
  702. End
  703. Else
  704. Begin
  705. TmpRef.base := Paicpu(p)^.oper[1].reg;
  706. TmpRef.ScaleFactor := 2;
  707. hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  708. End;
  709. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  710. Dispose(p, Done);
  711. p := Pai(hp1^.next);
  712. End
  713. End
  714. End;
  715. End;
  716. End;
  717. A_LEA:
  718. Begin
  719. {removes seg register prefixes from LEA operations, as they
  720. don't do anything}
  721. Paicpu(p)^.oper[0].ref^.Segment := R_NO;
  722. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  723. If (Paicpu(p)^.oper[0].ref^.Base In [R_EAX..R_EDI]) And
  724. (Paicpu(p)^.oper[0].ref^.Index = R_NO) And
  725. {$ifndef newOptimizations}
  726. (Paicpu(p)^.oper[0].ref^.Offset = 0) And
  727. {$endif newOptimizations}
  728. (Not(Assigned(Paicpu(p)^.oper[0].ref^.Symbol))) Then
  729. If (Paicpu(p)^.oper[0].ref^.Base <> Paicpu(p)^.oper[1].reg)
  730. {$ifdef newOptimizations}
  731. and (Paicpu(p)^.oper[0].ref^.Offset = 0)
  732. {$endif newOptimizations}
  733. Then
  734. Begin
  735. hp1 := New(Paicpu, op_reg_reg(A_MOV, S_L,Paicpu(p)^.oper[0].ref^.Base,
  736. Paicpu(p)^.oper[1].reg));
  737. InsertLLItem(AsmL,p^.previous,p^.next, hp1);
  738. Dispose(p, Done);
  739. p := hp1;
  740. Continue;
  741. End
  742. Else
  743. {$ifdef newOptimizations}
  744. if (Paicpu(p)^.oper[0].ref^.Offset = 0) then
  745. {$endif newOptimizations}
  746. Begin
  747. hp1 := Pai(p^.Next);
  748. AsmL^.Remove(p);
  749. Dispose(p, Done);
  750. p := hp1;
  751. Continue;
  752. End
  753. {$ifdef newOptimizations}
  754. else
  755. with Paicpu(p)^.oper[0].ref^ do
  756. if (Base = Paicpu(p)^.oper[1].reg) then
  757. begin
  758. l := offset+offsetfixup;
  759. case l of
  760. 1,-1:
  761. begin
  762. if l = 1 then
  763. paicpu(p)^.opcode := A_INC
  764. else paicpu(p)^.opcode := A_DEC;
  765. paicpu(p)^.loadreg(0,Paicpu(p)^.oper[1].reg);
  766. paicpu(p)^.ops := 1;
  767. end;
  768. else
  769. begin
  770. paicpu(p)^.opcode := A_ADD;
  771. paicpu(p)^.loadconst(0,offset+offsetfixup);
  772. end;
  773. end;
  774. end;
  775. {$endif newOptimizations}
  776. End;
  777. A_MOV:
  778. Begin
  779. TmpUsedRegs := UsedRegs;
  780. If (Paicpu(p)^.oper[1].typ = top_reg) And
  781. (Paicpu(p)^.oper[1].reg In [R_EAX, R_EBX, R_EDX, R_EDI]) And
  782. GetNextInstruction(p, hp1) And
  783. (Pai(hp1)^.typ = ait_instruction) And
  784. (Paicpu(hp1)^.opcode = A_MOV) And
  785. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  786. (Paicpu(hp1)^.oper[0].reg = Paicpu(p)^.oper[1].reg)
  787. Then
  788. {we have "mov x, %treg; mov %treg, y}
  789. If not(RegUsedAfterInstruction(Paicpu(p)^.oper[1].reg, hp1, TmpUsedRegs)) then
  790. {we've got "mov x, %treg; mov %treg, y; with %treg is not used after }
  791. Case Paicpu(p)^.oper[0].typ Of
  792. top_reg:
  793. Begin
  794. { change "mov %reg, %treg; mov %treg, y"
  795. to "mov %reg, y" }
  796. Paicpu(p)^.LoadOper(1,Paicpu(hp1)^.oper[1]);
  797. AsmL^.Remove(hp1);
  798. Dispose(hp1, Done);
  799. continue;
  800. End;
  801. top_ref:
  802. If (Paicpu(hp1)^.oper[1].typ = top_reg) Then
  803. Begin
  804. { change "mov mem, %treg; mov %treg, %reg"
  805. to "mov mem, %reg" }
  806. Paicpu(p)^.Loadoper(1,Paicpu(hp1)^.oper[1]);
  807. AsmL^.Remove(hp1);
  808. Dispose(hp1, Done);
  809. continue;
  810. End;
  811. End
  812. Else
  813. Else
  814. {Change "mov %reg1, %reg2; xxx %reg2, ???" to
  815. "mov %reg1, %reg2; xxx %reg1, ???" to avoid a write/read
  816. penalty}
  817. If (Paicpu(p)^.oper[0].typ = top_reg) And
  818. (Paicpu(p)^.oper[1].typ = top_reg) And
  819. GetNextInstruction(p,hp1) And
  820. (Pai(hp1)^.typ = ait_instruction) And
  821. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  822. (Paicpu(hp1)^.oper[0].reg = Paicpu(p)^.oper[1].reg)
  823. Then
  824. {we have "mov %reg1, %reg2; XXX %reg2, ???"}
  825. Begin
  826. If ((Paicpu(hp1)^.opcode = A_OR) Or
  827. (Paicpu(hp1)^.opcode = A_TEST)) And
  828. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  829. (Paicpu(hp1)^.oper[0].reg = Paicpu(hp1)^.oper[1].reg)
  830. Then
  831. {we have "mov %reg1, %reg2; test/or %reg2, %reg2"}
  832. Begin
  833. TmpUsedRegs := UsedRegs;
  834. { reg1 will be used after the first instruction, }
  835. { so update the allocation info }
  836. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,p,hp1);
  837. If GetNextInstruction(hp1, hp2) And
  838. (hp2^.typ = ait_instruction) And
  839. paicpu(hp2)^.is_jmp and
  840. Not(RegUsedAfterInstruction(Paicpu(hp1)^.oper[0].reg, hp1, TmpUsedRegs))
  841. Then
  842. {change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  843. "test %reg1, %reg1; jxx"}
  844. Begin
  845. Paicpu(hp1)^.Loadoper(0,Paicpu(p)^.oper[0]);
  846. Paicpu(hp1)^.Loadoper(1,Paicpu(p)^.oper[0]);
  847. AsmL^.Remove(p);
  848. Dispose(p, done);
  849. p := hp1;
  850. continue
  851. End
  852. Else
  853. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  854. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  855. Begin
  856. Paicpu(hp1)^.Loadoper(0,Paicpu(p)^.oper[0]);
  857. Paicpu(hp1)^.Loadoper(1,Paicpu(p)^.oper[0]);
  858. End;
  859. End
  860. { Else
  861. If (Paicpu(p^.next)^.opcode
  862. In [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  863. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  864. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  865. End
  866. Else
  867. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  868. x >= RetOffset) as it doesn't do anything (it writes either to a
  869. parameter or to the temporary storage room for the function
  870. result)}
  871. If GetNextInstruction(p, hp1) And
  872. (Pai(hp1)^.typ = ait_instruction)
  873. Then
  874. If ((Paicpu(hp1)^.opcode = A_LEAVE) Or
  875. (Paicpu(hp1)^.opcode = A_RET)) And
  876. (Paicpu(p)^.oper[1].typ = top_ref) And
  877. (Paicpu(p)^.oper[1].ref^.base = procinfo^.FramePointer) And
  878. (Paicpu(p)^.oper[1].ref^.offset >= procinfo^.Return_Offset) And
  879. (Paicpu(p)^.oper[1].ref^.index = R_NO) And
  880. (Paicpu(p)^.oper[0].typ = top_reg)
  881. Then
  882. Begin
  883. AsmL^.Remove(p);
  884. Dispose(p, done);
  885. p := hp1;
  886. RemoveLastDeallocForFuncRes(asmL,p);
  887. End
  888. Else
  889. If (Paicpu(p)^.oper[0].typ = top_reg) And
  890. (Paicpu(p)^.oper[1].typ = top_ref) And
  891. (Paicpu(p)^.opsize = Paicpu(hp1)^.opsize) And
  892. (Paicpu(hp1)^.opcode = A_CMP) And
  893. (Paicpu(hp1)^.oper[1].typ = top_ref) And
  894. RefsEqual(Paicpu(p)^.oper[1].ref^, Paicpu(hp1)^.oper[1].ref^) Then
  895. {change "mov reg1, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  896. begin
  897. Paicpu(hp1)^.loadreg(1,Paicpu(p)^.oper[0].reg);
  898. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,p,hp1);
  899. end;
  900. { Next instruction is also a MOV ? }
  901. If GetNextInstruction(p, hp1) And
  902. (pai(hp1)^.typ = ait_instruction) and
  903. (Paicpu(hp1)^.opcode = A_MOV) and
  904. (Paicpu(hp1)^.opsize = Paicpu(p)^.opsize)
  905. Then
  906. Begin
  907. If (Paicpu(hp1)^.oper[0].typ = Paicpu(p)^.oper[1].typ) and
  908. (Paicpu(hp1)^.oper[1].typ = Paicpu(p)^.oper[0].typ)
  909. Then
  910. {mov reg1, mem1 or mov mem1, reg1
  911. mov mem2, reg2 mov reg2, mem2}
  912. Begin
  913. If OpsEqual(Paicpu(hp1)^.oper[1],Paicpu(p)^.oper[0]) Then
  914. {mov reg1, mem1 or mov mem1, reg1
  915. mov mem2, reg1 mov reg2, mem1}
  916. Begin
  917. If OpsEqual(Paicpu(hp1)^.oper[0],Paicpu(p)^.oper[1]) Then
  918. { Removes the second statement from
  919. mov reg1, mem1/reg2
  920. mov mem1/reg2, reg1 }
  921. Begin
  922. if (paicpu(p)^.oper[0].typ = top_reg) then
  923. AllocRegBetween(asmL,paicpu(p)^.oper[0].reg,p,hp1);
  924. AsmL^.remove(hp1);
  925. Dispose(hp1,done);
  926. End
  927. Else
  928. Begin
  929. TmpUsedRegs := UsedRegs;
  930. UpdateUsedRegs(TmpUsedRegs, Pai(hp1^.next));
  931. If (Paicpu(p)^.oper[0].typ = top_reg) And
  932. { mov reg1, mem1
  933. mov mem2, reg1 }
  934. GetNextInstruction(hp1, hp2) And
  935. (hp2^.typ = ait_instruction) And
  936. (Paicpu(hp2)^.opcode = A_CMP) And
  937. (Paicpu(hp2)^.opsize = Paicpu(p)^.opsize) and
  938. (Paicpu(hp2)^.oper[0].typ = TOp_Ref) And
  939. (Paicpu(hp2)^.oper[1].typ = TOp_Reg) And
  940. RefsEqual(Paicpu(hp2)^.oper[0].ref^, Paicpu(p)^.oper[1].ref^) And
  941. (Paicpu(hp2)^.oper[1].reg = Paicpu(p)^.oper[0].reg) And
  942. Not(RegUsedAfterInstruction(Paicpu(p)^.oper[0].reg, hp2, TmpUsedRegs)) Then
  943. { change to
  944. mov reg1, mem1 mov reg1, mem1
  945. mov mem2, reg1 cmp reg1, mem2
  946. cmp mem1, reg1 }
  947. Begin
  948. AsmL^.Remove(hp2);
  949. Dispose(hp2, Done);
  950. Paicpu(hp1)^.opcode := A_CMP;
  951. Paicpu(hp1)^.loadref(1,newreference(Paicpu(hp1)^.oper[0].ref^));
  952. Paicpu(hp1)^.loadreg(0,Paicpu(p)^.oper[0].reg);
  953. End;
  954. End;
  955. End
  956. Else
  957. Begin
  958. tmpUsedRegs := UsedRegs;
  959. If GetNextInstruction(hp1, hp2) And
  960. (Paicpu(p)^.oper[0].typ = top_ref) And
  961. (Paicpu(p)^.oper[1].typ = top_reg) And
  962. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  963. (Paicpu(hp1)^.oper[0].reg = Paicpu(p)^.oper[1].reg) And
  964. (Paicpu(hp1)^.oper[1].typ = top_ref) And
  965. (Pai(hp2)^.typ = ait_instruction) And
  966. (Paicpu(hp2)^.opcode = A_MOV) And
  967. (Paicpu(hp2)^.opsize = Paicpu(p)^.opsize) and
  968. (Paicpu(hp2)^.oper[1].typ = top_reg) And
  969. (Paicpu(hp2)^.oper[0].typ = top_ref) And
  970. RefsEqual(Paicpu(hp2)^.oper[0].ref^, Paicpu(hp1)^.oper[1].ref^) Then
  971. If not regInRef(Paicpu(hp2)^.oper[1].reg,Paicpu(hp2)^.oper[0].ref^) and
  972. (Paicpu(p)^.oper[1].reg in [R_DI,R_EDI]) and
  973. not(RegUsedAfterInstruction(R_EDI,hp1,tmpUsedRegs)) Then
  974. { mov mem1, %edi
  975. mov %edi, mem2
  976. mov mem2, reg2
  977. to:
  978. mov mem1, reg2
  979. mov reg2, mem2}
  980. Begin
  981. Paicpu(p)^.Loadoper(1,Paicpu(hp2)^.oper[1]);
  982. Paicpu(hp1)^.loadoper(0,Paicpu(hp2)^.oper[1]);
  983. AsmL^.Remove(hp2);
  984. Dispose(hp2,Done);
  985. End
  986. Else
  987. If (Paicpu(p)^.oper[1].reg <> Paicpu(hp2)^.oper[1].reg) And
  988. not(RegInRef(Paicpu(p)^.oper[1].reg,Paicpu(p)^.oper[0].ref^)) And
  989. not(RegInRef(Paicpu(hp2)^.oper[1].reg,Paicpu(hp2)^.oper[0].ref^))
  990. Then
  991. { mov mem1, reg1 mov mem1, reg1
  992. mov reg1, mem2 mov reg1, mem2
  993. mov mem2, reg2 mov mem2, reg1
  994. to: to:
  995. mov mem1, reg1 mov mem1, reg1
  996. mov mem1, reg2 mov reg1, mem2
  997. mov reg1, mem2
  998. or (if mem1 depends on reg1
  999. and/or if mem2 depends on reg2)
  1000. to:
  1001. mov mem1, reg1
  1002. mov reg1, mem2
  1003. mov reg1, reg2
  1004. }
  1005. Begin
  1006. Paicpu(hp1)^.LoadRef(0,newreference(Paicpu(p)^.oper[0].ref^));
  1007. Paicpu(hp1)^.LoadReg(1,Paicpu(hp2)^.oper[1].reg);
  1008. Paicpu(hp2)^.LoadRef(1,newreference(Paicpu(hp2)^.oper[0].ref^));
  1009. Paicpu(hp2)^.LoadReg(0,Paicpu(p)^.oper[1].reg);
  1010. allocRegBetween(asmL,paicpu(p)^.oper[1].reg,p,hp2);
  1011. if (paicpu(p)^.oper[0].ref^.base in (usableregs+[R_EDI])) then
  1012. allocRegBetween(asmL,paicpu(p)^.oper[0].ref^.base,p,hp2);
  1013. if (paicpu(p)^.oper[0].ref^.index in (usableregs+[R_EDI])) then
  1014. allocRegBetween(asmL,paicpu(p)^.oper[0].ref^.index,p,hp2);
  1015. End
  1016. Else
  1017. If (Paicpu(hp1)^.Oper[0].reg <> Paicpu(hp2)^.Oper[1].reg) Then
  1018. begin
  1019. Paicpu(hp2)^.LoadReg(0,Paicpu(hp1)^.Oper[0].reg);
  1020. allocRegBetween(asmL,paicpu(p)^.oper[1].reg,p,hp2);
  1021. end
  1022. else
  1023. begin
  1024. asmL^.Remove(hp2);
  1025. dispose(hp2, done);
  1026. end
  1027. End;
  1028. End
  1029. Else
  1030. (* {movl [mem1],reg1
  1031. movl [mem1],reg2
  1032. to:
  1033. movl [mem1],reg1
  1034. movl reg1,reg2 }
  1035. If (Paicpu(p)^.oper[0].typ = top_ref) and
  1036. (Paicpu(p)^.oper[1].typ = top_reg) and
  1037. (Paicpu(hp1)^.oper[0].typ = top_ref) and
  1038. (Paicpu(hp1)^.oper[1].typ = top_reg) and
  1039. (Paicpu(p)^.opsize = Paicpu(hp1)^.opsize) and
  1040. RefsEqual(TReference(Paicpu(p)^.oper[0]^),Paicpu(hp1)^.oper[0]^.ref^) and
  1041. (Paicpu(p)^.oper[1].reg<>Paicpu(hp1)^.oper[0]^.ref^.base) and
  1042. (Paicpu(p)^.oper[1].reg<>Paicpu(hp1)^.oper[0]^.ref^.index) then
  1043. Paicpu(hp1)^.LoadReg(0,Paicpu(p)^.oper[1].reg)
  1044. Else*)
  1045. { movl const1,[mem1]
  1046. movl [mem1],reg1
  1047. to:
  1048. movl const1,reg1
  1049. movl reg1,[mem1] }
  1050. If (Paicpu(p)^.oper[0].typ = top_const) and
  1051. (Paicpu(p)^.oper[1].typ = top_ref) and
  1052. (Paicpu(hp1)^.oper[0].typ = top_ref) and
  1053. (Paicpu(hp1)^.oper[1].typ = top_reg) and
  1054. (Paicpu(p)^.opsize = Paicpu(hp1)^.opsize) and
  1055. RefsEqual(Paicpu(hp1)^.oper[0].ref^,Paicpu(p)^.oper[1].ref^) then
  1056. Begin
  1057. allocregbetween(asml,Paicpu(hp1)^.oper[1].reg,p,hp1);
  1058. { allocregbetween doesn't insert this because at }
  1059. { this time, no regalloc info is available in }
  1060. { the optinfo field, so do it manually (JM) }
  1061. hp2 := new(paiRegalloc,alloc(Paicpu(hp1)^.oper[1].reg));
  1062. insertllitem(asml,p^.previous,p,hp2);
  1063. Paicpu(hp1)^.LoadReg(0,Paicpu(hp1)^.oper[1].reg);
  1064. Paicpu(hp1)^.LoadRef(1,newreference(Paicpu(p)^.oper[1].ref^));
  1065. Paicpu(p)^.LoadReg(1,Paicpu(hp1)^.oper[0].reg);
  1066. End
  1067. End;
  1068. End;
  1069. A_MOVZX:
  1070. Begin
  1071. {removes superfluous And's after movzx's}
  1072. If (Paicpu(p)^.oper[1].typ = top_reg) And
  1073. GetNextInstruction(p, hp1) And
  1074. (Pai(hp1)^.typ = ait_instruction) And
  1075. (Paicpu(hp1)^.opcode = A_AND) And
  1076. (Paicpu(hp1)^.oper[0].typ = top_const) And
  1077. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1078. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg)
  1079. Then
  1080. Case Paicpu(p)^.opsize Of
  1081. S_BL, S_BW:
  1082. If (Paicpu(hp1)^.oper[0].val = $ff) Then
  1083. Begin
  1084. AsmL^.Remove(hp1);
  1085. Dispose(hp1, Done);
  1086. End;
  1087. S_WL:
  1088. If (Paicpu(hp1)^.oper[0].val = $ffff) Then
  1089. Begin
  1090. AsmL^.Remove(hp1);
  1091. Dispose(hp1, Done);
  1092. End;
  1093. End;
  1094. {changes some movzx constructs to faster synonims (all examples
  1095. are given with eax/ax, but are also valid for other registers)}
  1096. If (Paicpu(p)^.oper[1].typ = top_reg) Then
  1097. If (Paicpu(p)^.oper[0].typ = top_reg) Then
  1098. Case Paicpu(p)^.opsize of
  1099. S_BW:
  1100. Begin
  1101. If (Paicpu(p)^.oper[0].reg = Reg16ToReg8(Paicpu(p)^.oper[1].reg)) And
  1102. Not(CS_LittleSize In aktglobalswitches)
  1103. Then
  1104. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  1105. Begin
  1106. Paicpu(p)^.opcode := A_AND;
  1107. Paicpu(p)^.changeopsize(S_W);
  1108. Paicpu(p)^.LoadConst(0,$ff);
  1109. End
  1110. Else
  1111. If GetNextInstruction(p, hp1) And
  1112. (Pai(hp1)^.typ = ait_instruction) And
  1113. (Paicpu(hp1)^.opcode = A_AND) And
  1114. (Paicpu(hp1)^.oper[0].typ = top_const) And
  1115. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1116. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg)
  1117. Then
  1118. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  1119. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  1120. Begin
  1121. Paicpu(p)^.opcode := A_MOV;
  1122. Paicpu(p)^.changeopsize(S_W);
  1123. Paicpu(p)^.LoadReg(0,Reg8ToReg16(Paicpu(p)^.oper[0].reg));
  1124. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ff);
  1125. End;
  1126. End;
  1127. S_BL:
  1128. Begin
  1129. If (Paicpu(p)^.oper[0].reg = Reg32ToReg8(Paicpu(p)^.oper[1].reg)) And
  1130. Not(CS_LittleSize in aktglobalswitches)
  1131. Then
  1132. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  1133. Begin
  1134. Paicpu(p)^.opcode := A_AND;
  1135. Paicpu(p)^.changeopsize(S_L);
  1136. Paicpu(p)^.loadconst(0,$ff)
  1137. End
  1138. Else
  1139. If GetNextInstruction(p, hp1) And
  1140. (Pai(hp1)^.typ = ait_instruction) And
  1141. (Paicpu(hp1)^.opcode = A_AND) And
  1142. (Paicpu(hp1)^.oper[0].typ = top_const) And
  1143. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1144. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg)
  1145. Then
  1146. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  1147. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  1148. Begin
  1149. Paicpu(p)^.opcode := A_MOV;
  1150. Paicpu(p)^.changeopsize(S_L);
  1151. Paicpu(p)^.LoadReg(0,Reg8ToReg32(Paicpu(p)^.oper[0].reg));
  1152. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ff);
  1153. End
  1154. End;
  1155. S_WL:
  1156. Begin
  1157. If (Paicpu(p)^.oper[0].reg = Reg32ToReg16(Paicpu(p)^.oper[1].reg)) And
  1158. Not(CS_LittleSize In aktglobalswitches)
  1159. Then
  1160. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  1161. Begin
  1162. Paicpu(p)^.opcode := A_AND;
  1163. Paicpu(p)^.changeopsize(S_L);
  1164. Paicpu(p)^.LoadConst(0,$ffff);
  1165. End
  1166. Else
  1167. If GetNextInstruction(p, hp1) And
  1168. (Pai(hp1)^.typ = ait_instruction) And
  1169. (Paicpu(hp1)^.opcode = A_AND) And
  1170. (Paicpu(hp1)^.oper[0].typ = top_const) And
  1171. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1172. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg)
  1173. Then
  1174. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  1175. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  1176. Begin
  1177. Paicpu(p)^.opcode := A_MOV;
  1178. Paicpu(p)^.changeopsize(S_L);
  1179. Paicpu(p)^.LoadReg(0,Reg16ToReg32(Paicpu(p)^.oper[0].reg));
  1180. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ffff);
  1181. End;
  1182. End;
  1183. End
  1184. Else
  1185. If (Paicpu(p)^.oper[0].typ = top_ref) Then
  1186. Begin
  1187. If GetNextInstruction(p, hp1) And
  1188. (Pai(hp1)^.typ = ait_instruction) And
  1189. (Paicpu(hp1)^.opcode = A_AND) And
  1190. (Paicpu(hp1)^.oper[0].typ = Top_Const) And
  1191. (Paicpu(hp1)^.oper[1].typ = Top_Reg) And
  1192. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg) Then
  1193. Begin
  1194. Paicpu(p)^.opcode := A_MOV;
  1195. Case Paicpu(p)^.opsize Of
  1196. S_BL:
  1197. Begin
  1198. Paicpu(p)^.changeopsize(S_L);
  1199. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ff);
  1200. End;
  1201. S_WL:
  1202. Begin
  1203. Paicpu(p)^.changeopsize(S_L);
  1204. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ffff);
  1205. End;
  1206. S_BW:
  1207. Begin
  1208. Paicpu(p)^.changeopsize(S_W);
  1209. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val And $ff);
  1210. End;
  1211. End;
  1212. End;
  1213. End;
  1214. End;
  1215. A_POP:
  1216. Begin
  1217. if (Paicpu(p)^.oper[0].typ = top_reg) And
  1218. GetNextInstruction(p, hp1) And
  1219. (pai(hp1)^.typ=ait_instruction) and
  1220. (Paicpu(hp1)^.opcode=A_PUSH) and
  1221. (Paicpu(hp1)^.oper[0].typ = top_reg) And
  1222. (Paicpu(hp1)^.oper[0].reg=Paicpu(p)^.oper[0].reg) then
  1223. { This can't be done, because the register which is popped
  1224. can still be used after the push (PFV)
  1225. If (Not(cs_regalloc in aktglobalswitches)) Then
  1226. Begin
  1227. hp2:=pai(hp1^.next);
  1228. asml^.remove(p);
  1229. asml^.remove(hp1);
  1230. dispose(p,done);
  1231. dispose(hp1,done);
  1232. p:=hp2;
  1233. continue
  1234. End
  1235. Else }
  1236. Begin
  1237. { change it to a two op operation }
  1238. Paicpu(p)^.oper[1].typ:=top_none;
  1239. Paicpu(p)^.ops:=2;
  1240. Paicpu(p)^.opcode := A_MOV;
  1241. Paicpu(p)^.Loadoper(1,Paicpu(p)^.oper[0]);
  1242. Reset_reference(tmpref);
  1243. TmpRef.base := R_ESP;
  1244. Paicpu(p)^.LoadRef(0,newReference(TmpRef));
  1245. AsmL^.Remove(hp1);
  1246. Dispose(hp1, Done)
  1247. End;
  1248. end;
  1249. A_PUSH:
  1250. Begin
  1251. If (Paicpu(p)^.opsize = S_W) And
  1252. (Paicpu(p)^.oper[0].typ = Top_Const) And
  1253. GetNextInstruction(p, hp1) And
  1254. (Pai(hp1)^.typ = ait_instruction) And
  1255. (Paicpu(hp1)^.opcode = A_PUSH) And
  1256. (Paicpu(hp1)^.oper[0].typ = Top_Const) And
  1257. (Paicpu(hp1)^.opsize = S_W) Then
  1258. Begin
  1259. Paicpu(p)^.changeopsize(S_L);
  1260. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val shl 16 + word(Paicpu(hp1)^.oper[0].val));
  1261. AsmL^.Remove(hp1);
  1262. Dispose(hp1, Done)
  1263. End;
  1264. End;
  1265. A_SHL, A_SAL:
  1266. Begin
  1267. If (Paicpu(p)^.oper[0].typ = Top_Const) And
  1268. (Paicpu(p)^.oper[1].typ = Top_Reg) And
  1269. (Paicpu(p)^.opsize = S_L) And
  1270. (Paicpu(p)^.oper[0].val <= 3)
  1271. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1272. Then
  1273. Begin
  1274. TmpBool1 := True; {should we check the next instruction?}
  1275. TmpBool2 := False; {have we found an add/sub which could be
  1276. integrated in the lea?}
  1277. Reset_reference(tmpref);
  1278. TmpRef.index := Paicpu(p)^.oper[1].reg;
  1279. TmpRef.scalefactor := 1 shl Paicpu(p)^.oper[0].val;
  1280. While TmpBool1 And
  1281. GetNextInstruction(p, hp1) And
  1282. (Pai(hp1)^.typ = ait_instruction) And
  1283. ((Paicpu(hp1)^.opcode = A_ADD) Or
  1284. (Paicpu(hp1)^.opcode = A_SUB)) And
  1285. (Paicpu(hp1)^.oper[1].typ = Top_Reg) And
  1286. (Paicpu(hp1)^.oper[1].reg = Paicpu(p)^.oper[1].reg) Do
  1287. Begin
  1288. TmpBool1 := False;
  1289. If (Paicpu(hp1)^.oper[0].typ = Top_Const)
  1290. Then
  1291. Begin
  1292. TmpBool1 := True;
  1293. TmpBool2 := True;
  1294. If Paicpu(hp1)^.opcode = A_ADD Then
  1295. Inc(TmpRef.offset, Paicpu(hp1)^.oper[0].val)
  1296. Else
  1297. Dec(TmpRef.offset, Paicpu(hp1)^.oper[0].val);
  1298. AsmL^.Remove(hp1);
  1299. Dispose(hp1, Done);
  1300. End
  1301. Else
  1302. If (Paicpu(hp1)^.oper[0].typ = Top_Reg) And
  1303. (Paicpu(hp1)^.opcode = A_ADD) And
  1304. (TmpRef.base = R_NO) Then
  1305. Begin
  1306. TmpBool1 := True;
  1307. TmpBool2 := True;
  1308. TmpRef.base := Paicpu(hp1)^.oper[0].reg;
  1309. AsmL^.Remove(hp1);
  1310. Dispose(hp1, Done);
  1311. End;
  1312. End;
  1313. If TmpBool2 Or
  1314. ((aktoptprocessor < ClassP6) And
  1315. (Paicpu(p)^.oper[0].val <= 3) And
  1316. Not(CS_LittleSize in aktglobalswitches))
  1317. Then
  1318. Begin
  1319. If Not(TmpBool2) And
  1320. (Paicpu(p)^.oper[0].val = 1)
  1321. Then
  1322. Begin
  1323. hp1 := new(Paicpu,op_reg_reg(A_ADD,Paicpu(p)^.opsize,
  1324. Paicpu(p)^.oper[1].reg, Paicpu(p)^.oper[1].reg))
  1325. End
  1326. Else hp1 := New(Paicpu, op_ref_reg(A_LEA, S_L, newReference(TmpRef),
  1327. Paicpu(p)^.oper[1].reg));
  1328. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1329. Dispose(p, Done);
  1330. p := hp1;
  1331. End;
  1332. End
  1333. Else
  1334. If (aktoptprocessor < ClassP6) And
  1335. (Paicpu(p)^.oper[0].typ = top_const) And
  1336. (Paicpu(p)^.oper[1].typ = top_reg) Then
  1337. If (Paicpu(p)^.oper[0].val = 1)
  1338. Then
  1339. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1340. but faster on a 486, and pairable in both U and V pipes on the Pentium
  1341. (unlike shl, which is only pairable in the U pipe)}
  1342. Begin
  1343. hp1 := new(Paicpu,op_reg_reg(A_ADD,Paicpu(p)^.opsize,
  1344. Paicpu(p)^.oper[1].reg, Paicpu(p)^.oper[1].reg));
  1345. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1346. Dispose(p, done);
  1347. p := hp1;
  1348. End
  1349. Else If (Paicpu(p)^.opsize = S_L) and
  1350. (Paicpu(p)^.oper[0].val<= 3) Then
  1351. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1352. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1353. Begin
  1354. Reset_reference(tmpref);
  1355. TmpRef.index := Paicpu(p)^.oper[1].reg;
  1356. TmpRef.scalefactor := 1 shl Paicpu(p)^.oper[0].val;
  1357. hp1 := new(Paicpu,op_ref_reg(A_LEA,S_L,newReference(TmpRef), Paicpu(p)^.oper[1].reg));
  1358. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1359. Dispose(p, done);
  1360. p := hp1;
  1361. End
  1362. End;
  1363. A_SAR, A_SHR:
  1364. {changes the code sequence
  1365. shr/sar const1, x
  1366. shl const2, x
  1367. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  1368. Begin
  1369. If GetNextInstruction(p, hp1) And
  1370. (pai(hp1)^.typ = ait_instruction) and
  1371. (Paicpu(hp1)^.opcode = A_SHL) and
  1372. (Paicpu(p)^.oper[0].typ = top_const) and
  1373. (Paicpu(hp1)^.oper[0].typ = top_const) and
  1374. (Paicpu(hp1)^.opsize = Paicpu(p)^.opsize) And
  1375. (Paicpu(hp1)^.oper[1].typ = Paicpu(p)^.oper[1].typ) And
  1376. OpsEqual(Paicpu(hp1)^.oper[1], Paicpu(p)^.oper[1])
  1377. Then
  1378. If (Paicpu(p)^.oper[0].val > Paicpu(hp1)^.oper[0].val) And
  1379. Not(CS_LittleSize In aktglobalswitches)
  1380. Then
  1381. { shr/sar const1, %reg
  1382. shl const2, %reg
  1383. with const1 > const2 }
  1384. Begin
  1385. Paicpu(p)^.LoadConst(0,Paicpu(p)^.oper[0].val-Paicpu(hp1)^.oper[0].val);
  1386. Paicpu(hp1)^.opcode := A_AND;
  1387. l := (1 shl (Paicpu(hp1)^.oper[0].val)) - 1;
  1388. Case Paicpu(p)^.opsize Of
  1389. S_L: Paicpu(hp1)^.LoadConst(0,l Xor longint(-1));
  1390. S_B: Paicpu(hp1)^.LoadConst(0,l Xor $ff);
  1391. S_W: Paicpu(hp1)^.LoadConst(0,l Xor $ffff);
  1392. End;
  1393. End
  1394. Else
  1395. If (Paicpu(p)^.oper[0].val<Paicpu(hp1)^.oper[0].val) And
  1396. Not(CS_LittleSize In aktglobalswitches)
  1397. Then
  1398. { shr/sar const1, %reg
  1399. shl const2, %reg
  1400. with const1 < const2 }
  1401. Begin
  1402. Paicpu(hp1)^.LoadConst(0,Paicpu(hp1)^.oper[0].val-Paicpu(p)^.oper[0].val);
  1403. Paicpu(p)^.opcode := A_AND;
  1404. l := (1 shl (Paicpu(p)^.oper[0].val))-1;
  1405. Case Paicpu(p)^.opsize Of
  1406. S_L: Paicpu(p)^.LoadConst(0,l Xor $ffffffff);
  1407. S_B: Paicpu(p)^.LoadConst(0,l Xor $ff);
  1408. S_W: Paicpu(p)^.LoadConst(0,l Xor $ffff);
  1409. End;
  1410. End
  1411. Else
  1412. { shr/sar const1, %reg
  1413. shl const2, %reg
  1414. with const1 = const2 }
  1415. Begin
  1416. Paicpu(p)^.opcode := A_AND;
  1417. l := (1 shl (Paicpu(p)^.oper[0].val))-1;
  1418. Case Paicpu(p)^.opsize Of
  1419. S_B: Paicpu(p)^.LoadConst(0,l Xor $ff);
  1420. S_W: Paicpu(p)^.LoadConst(0,l Xor $ffff);
  1421. S_L: Paicpu(p)^.LoadConst(0,l Xor $ffffffff);
  1422. End;
  1423. AsmL^.remove(hp1);
  1424. dispose(hp1, done);
  1425. End;
  1426. End;
  1427. A_SETcc :
  1428. { changes
  1429. setcc (funcres) setcc reg
  1430. movb (funcres), reg to leave/ret
  1431. leave/ret }
  1432. Begin
  1433. If (Paicpu(p)^.oper[0].typ = top_ref) And
  1434. GetNextInstruction(p, hp1) And
  1435. GetNextInstruction(hp1, hp2) And
  1436. (hp2^.typ = ait_instruction) And
  1437. ((Paicpu(hp2)^.opcode = A_LEAVE) or
  1438. (Paicpu(hp2)^.opcode = A_RET)) And
  1439. (Paicpu(p)^.oper[0].ref^.Base = procinfo^.FramePointer) And
  1440. (Paicpu(p)^.oper[0].ref^.Index = R_NO) And
  1441. (Paicpu(p)^.oper[0].ref^.Offset >= procinfo^.Return_Offset) And
  1442. (hp1^.typ = ait_instruction) And
  1443. (Paicpu(hp1)^.opcode = A_MOV) And
  1444. (Paicpu(hp1)^.opsize = S_B) And
  1445. (Paicpu(hp1)^.oper[0].typ = top_ref) And
  1446. RefsEqual(Paicpu(hp1)^.oper[0].ref^, Paicpu(p)^.oper[0].ref^) Then
  1447. Begin
  1448. Paicpu(p)^.LoadReg(0,Paicpu(hp1)^.oper[1].reg);
  1449. AsmL^.Remove(hp1);
  1450. Dispose(hp1, Done)
  1451. End
  1452. End;
  1453. A_SUB:
  1454. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1455. { * change "sub/add const1, reg" or "dec reg" followed by
  1456. "sub const2, reg" to one "sub ..., reg" }
  1457. Begin
  1458. If (Paicpu(p)^.oper[0].typ = top_const) And
  1459. (Paicpu(p)^.oper[1].typ = top_reg) Then
  1460. If (Paicpu(p)^.oper[0].val = 2) And
  1461. (Paicpu(p)^.oper[1].reg = R_ESP) and
  1462. { Don't do the sub/push optimization if the sub }
  1463. { comes from setting up the stack frame (JM) }
  1464. (not getLastInstruction(p,hp1) or
  1465. (hp1^.typ <> ait_instruction) or
  1466. (paicpu(hp1)^.opcode <> A_MOV) or
  1467. (paicpu(hp1)^.oper[0].typ <> top_reg) or
  1468. (paicpu(hp1)^.oper[0].reg <> R_ESP) or
  1469. (paicpu(hp1)^.oper[1].typ <> top_reg) or
  1470. (paicpu(hp1)^.oper[1].reg <> R_EBP)) then
  1471. Begin
  1472. hp1 := Pai(p^.next);
  1473. While Assigned(hp1) And
  1474. (Pai(hp1)^.typ In [ait_instruction]+SkipInstr) And
  1475. Not((Pai(hp1)^.typ = ait_instruction) And
  1476. ((Paicpu(hp1)^.opcode = A_CALL) or
  1477. (Paicpu(hp1)^.opcode = A_PUSH) or
  1478. ((Paicpu(hp1)^.opcode = A_MOV) And
  1479. (Paicpu(hp1)^.oper[1].typ = top_ref) And
  1480. (Paicpu(hp1)^.oper[1].ref^.base = R_ESP)))) do
  1481. hp1 := Pai(hp1^.next);
  1482. If Assigned(hp1) And
  1483. (Pai(hp1)^.typ = ait_instruction) And
  1484. (Paicpu(hp1)^.opcode = A_PUSH) And
  1485. (Paicpu(hp1)^.opsize = S_W)
  1486. Then
  1487. Begin
  1488. Paicpu(hp1)^.changeopsize(S_L);
  1489. if Paicpu(hp1)^.oper[0].typ=top_reg then
  1490. Paicpu(hp1)^.LoadReg(0,Reg16ToReg32(Paicpu(hp1)^.oper[0].reg));
  1491. hp1 := Pai(p^.next);
  1492. AsmL^.Remove(p);
  1493. Dispose(p, Done);
  1494. p := hp1;
  1495. Continue
  1496. End;
  1497. If DoSubAddOpt(p) Then continue;
  1498. End
  1499. Else If DoSubAddOpt(p) Then Continue
  1500. End;
  1501. A_XOR:
  1502. If (Paicpu(p)^.oper[0].typ = top_reg) And
  1503. (Paicpu(p)^.oper[1].typ = top_reg) And
  1504. (Paicpu(p)^.oper[0].reg = Paicpu(p)^.oper[1].reg) then
  1505. { temporarily change this to 'mov reg,0' to make it easier }
  1506. { for the CSE. Will be changed back in pass 2 }
  1507. begin
  1508. paicpu(p)^.opcode := A_MOV;
  1509. paicpu(p)^.loadconst(0,0);
  1510. end;
  1511. End;
  1512. end; { if is_jmp }
  1513. End;
  1514. { ait_label:
  1515. Begin
  1516. If Not(Pai_Label(p)^.l^.is_used)
  1517. Then
  1518. Begin
  1519. hp1 := Pai(p^.next);
  1520. AsmL^.Remove(p);
  1521. Dispose(p, Done);
  1522. p := hp1;
  1523. Continue
  1524. End;
  1525. End;}
  1526. End;
  1527. p:=pai(p^.next);
  1528. end;
  1529. end;
  1530. {$ifdef foldArithOps}
  1531. function isFoldableArithOp(hp1: paicpu; reg: tregister): boolean;
  1532. begin
  1533. IsFoldableArithOp := False;
  1534. case hp1^.opcode of
  1535. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR,A_IMUL:
  1536. isFoldableArithOp :=
  1537. ((paicpu(hp1)^.oper[0].typ = top_reg) or
  1538. ((paicpu(hp1)^.oper[0].typ = top_const) and
  1539. (hp1^.opcode <> A_IMUL))) and
  1540. (paicpu(hp1)^.oper[1].typ = top_reg) and
  1541. (paicpu(hp1)^.oper[1].reg = reg);
  1542. A_INC,A_DEC:
  1543. isFoldableArithOp :=
  1544. (paicpu(hp1)^.oper[0].typ = top_reg) and
  1545. (paicpu(hp1)^.oper[0].reg = reg);
  1546. end;
  1547. end;
  1548. {$endif foldArithOps}
  1549. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  1550. function CanBeCMOV(p : pai) : boolean;
  1551. begin
  1552. CanBeCMOV:=assigned(p) and (p^.typ=ait_instruction) and
  1553. (paicpu(p)^.opcode=A_MOV) and
  1554. (paicpu(p)^.opsize in [S_L,S_W]) and
  1555. (paicpu(p)^.oper[0].typ in [top_reg,top_ref]) and
  1556. (paicpu(p)^.oper[1].typ in [top_reg,top_ref]);
  1557. end;
  1558. var
  1559. p,hp1,hp2: pai;
  1560. {$ifdef USECMOV}
  1561. l : longint;
  1562. condition : tasmcond;
  1563. hp3: pai;
  1564. {$endif USECMOV}
  1565. {$ifdef foldArithOps}
  1566. UsedRegs, TmpUsedRegs: TRegSet;
  1567. {$endif foldArithOps}
  1568. Begin
  1569. P := BlockStart;
  1570. {$ifdef foldArithOps}
  1571. UsedRegs := [];
  1572. {$endif foldArithOps}
  1573. While (P <> BlockEnd) Do
  1574. Begin
  1575. {$ifdef foldArithOps}
  1576. UpdateUsedRegs(UsedRegs, Pai(p^.next));
  1577. {$endif foldArithOps}
  1578. Case P^.Typ Of
  1579. Ait_Instruction:
  1580. Begin
  1581. Case Paicpu(p)^.opcode Of
  1582. A_CALL:
  1583. If (AktOptProcessor < ClassP6) And
  1584. GetNextInstruction(p, hp1) And
  1585. (hp1^.typ = ait_instruction) And
  1586. (paicpu(hp1)^.opcode = A_JMP) Then
  1587. Begin
  1588. Inc(paicpu(hp1)^.oper[0].sym^.refs);
  1589. hp2 := New(Paicpu,op_sym(A_PUSH,S_L,paicpu(hp1)^.oper[0].sym));
  1590. InsertLLItem(AsmL, p^.previous, p, hp2);
  1591. Paicpu(p)^.opcode := A_JMP;
  1592. AsmL^.Remove(hp1);
  1593. Dispose(hp1, Done)
  1594. End;
  1595. {$ifdef USECMOV}
  1596. A_Jcc:
  1597. if (aktspecificoptprocessor=ClassP6) then
  1598. begin
  1599. { check for
  1600. jCC xxx
  1601. <several movs>
  1602. xxx:
  1603. }
  1604. l:=0;
  1605. GetNextInstruction(p, hp1);
  1606. while assigned(hp1) And
  1607. CanBeCMOV(hp1) do
  1608. begin
  1609. inc(l);
  1610. GetNextInstruction(hp1,hp1);
  1611. end;
  1612. if assigned(hp1) then
  1613. begin
  1614. if FindLabel(PAsmLabel(paicpu(p)^.oper[0].sym),hp1) then
  1615. begin
  1616. if (l<=4) and (l>0) then
  1617. begin
  1618. condition:=inverse_cond[paicpu(p)^.condition];
  1619. GetNextInstruction(p,hp1);
  1620. asml^.remove(p);
  1621. dispose(p,done);
  1622. p:=hp1;
  1623. repeat
  1624. paicpu(hp1)^.opcode:=A_CMOVcc;
  1625. paicpu(hp1)^.condition:=condition;
  1626. GetNextInstruction(hp1,hp1);
  1627. until not(assigned(hp1)) or
  1628. not(CanBeCMOV(hp1));
  1629. asml^.remove(hp1);
  1630. dispose(hp1,done);
  1631. continue;
  1632. end;
  1633. end
  1634. else
  1635. begin
  1636. { check further for
  1637. jCC xxx
  1638. <several movs>
  1639. jmp yyy
  1640. xxx:
  1641. <several movs>
  1642. yyy:
  1643. }
  1644. { hp2 points to jmp xxx }
  1645. hp2:=hp1;
  1646. { skip hp1 to xxx }
  1647. GetNextInstruction(hp1, hp1);
  1648. if assigned(hp2) and
  1649. assigned(hp1) and
  1650. (l<=3) and
  1651. (hp2^.typ=ait_instruction) and
  1652. (paicpu(hp2)^.is_jmp) and
  1653. (paicpu(hp2)^.condition=C_None) and
  1654. FindLabel(PAsmLabel(paicpu(p)^.oper[0].sym),hp1) then
  1655. begin
  1656. l:=0;
  1657. while assigned(hp1) And
  1658. CanBeCMOV(hp1) do
  1659. begin
  1660. inc(l);
  1661. GetNextInstruction(hp1, hp1);
  1662. end;
  1663. end;
  1664. {
  1665. if assigned(hp1) and
  1666. FindLabel(PAsmLabel(paicpu(hp2)^.oper[0].sym),hp1) then
  1667. begin
  1668. condition:=inverse_cond[paicpu(p)^.condition];
  1669. GetNextInstruction(p,hp1);
  1670. asml^.remove(p);
  1671. dispose(p,done);
  1672. p:=hp1;
  1673. repeat
  1674. paicpu(hp1)^.opcode:=A_CMOVcc;
  1675. paicpu(hp1)^.condition:=condition;
  1676. GetNextInstruction(hp1,hp1);
  1677. until not(assigned(hp1)) or
  1678. not(CanBeCMOV(hp1));
  1679. hp2:=hp1^.next;
  1680. condition:=inverse_cond[condition];
  1681. asml^.remove(hp1^.next)
  1682. dispose(hp1^.next,done);
  1683. asml^.remove(hp1);
  1684. dispose(hp1,done);
  1685. continue;
  1686. end;
  1687. }
  1688. end;
  1689. end;
  1690. end;
  1691. {$endif USECMOV}
  1692. A_FSTP,A_FISTP:
  1693. if doFpuLoadStoreOpt(asmL,p) then
  1694. continue;
  1695. {$ifdef foldArithOps}
  1696. A_IMUL:
  1697. begin
  1698. if ((paicpu(p)^.oper[0].typ = top_const) or
  1699. (paicpu(p)^.oper[0].typ = top_symbol)) and
  1700. (paicpu(p)^.oper[1].typ = top_reg) and
  1701. ((paicpu(p)^.oper[2].typ = top_none) or
  1702. ((paicpu(p)^.oper[2].typ = top_reg) and
  1703. (paicpu(p)^.oper[2].reg = paicpu(p)^.oper[1].reg))) and
  1704. getLastInstruction(p,hp1) and
  1705. (hp1^.typ = ait_instruction) and
  1706. (paicpu(hp1)^.opcode = A_MOV) and
  1707. (paicpu(hp1)^.oper[0].typ = top_reg) and
  1708. (paicpu(hp1)^.oper[1].typ = top_reg) and
  1709. (paicpu(hp1)^.oper[1].reg = paicpu(p)^.oper[1].reg) then
  1710. { change "mov reg1,reg2; imul y,reg2" to "imul y,reg1,reg2" }
  1711. begin
  1712. paicpu(p)^.ops := 3;
  1713. paicpu(p)^.loadreg(1,paicpu(hp1)^.oper[0].reg);
  1714. paicpu(p)^.loadreg(2,paicpu(hp1)^.oper[1].reg);
  1715. asmL^.remove(hp1);
  1716. dispose(hp1,done);
  1717. end;
  1718. end;
  1719. {$endif foldArithOps}
  1720. A_MOV:
  1721. Begin
  1722. If (Paicpu(p)^.oper[0].typ = top_reg) And
  1723. (Paicpu(p)^.oper[1].typ = top_reg) And
  1724. GetNextInstruction(p, hp1) And
  1725. (hp1^.typ = ait_Instruction) And
  1726. ((Paicpu(hp1)^.opcode = A_MOV) or
  1727. (Paicpu(hp1)^.opcode = A_MOVZX) or
  1728. (Paicpu(hp1)^.opcode = A_MOVSX)) And
  1729. (Paicpu(hp1)^.oper[0].typ = top_ref) And
  1730. (Paicpu(hp1)^.oper[1].typ = top_reg) And
  1731. ((Paicpu(hp1)^.oper[0].ref^.Base = Paicpu(p)^.oper[1].reg) Or
  1732. (Paicpu(hp1)^.oper[0].ref^.Index = Paicpu(p)^.oper[1].reg)) And
  1733. (Reg32(Paicpu(hp1)^.oper[1].reg) = Paicpu(p)^.oper[1].reg) Then
  1734. {mov reg1, reg2
  1735. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  1736. Begin
  1737. If (Paicpu(hp1)^.oper[0].ref^.Base = Paicpu(p)^.oper[1].reg) Then
  1738. Paicpu(hp1)^.oper[0].ref^.Base := Paicpu(p)^.oper[0].reg;
  1739. If (Paicpu(hp1)^.oper[0].ref^.Index = Paicpu(p)^.oper[1].reg) Then
  1740. Paicpu(hp1)^.oper[0].ref^.Index := Paicpu(p)^.oper[0].reg;
  1741. AsmL^.Remove(p);
  1742. Dispose(p, Done);
  1743. p := hp1;
  1744. Continue;
  1745. End
  1746. {$ifdef foldArithOps}
  1747. Else If (Paicpu(p)^.oper[0].typ = top_ref) And
  1748. GetNextInstruction(p,hp1) And
  1749. (hp1^.typ = ait_instruction) And
  1750. IsFoldableArithOp(paicpu(hp1),Paicpu(p)^.oper[1].reg) And
  1751. GetNextInstruction(hp1,hp2) And
  1752. (hp2^.typ = ait_instruction) And
  1753. (Paicpu(hp2)^.opcode = A_MOV) And
  1754. (Paicpu(hp2)^.oper[0].typ = top_reg) And
  1755. (Paicpu(hp2)^.oper[0].reg = Paicpu(p)^.oper[1].reg) And
  1756. (Paicpu(hp2)^.oper[1].typ = top_ref) Then
  1757. Begin
  1758. TmpUsedRegs := UsedRegs;
  1759. UpdateUsedRegs(TmpUsedRegs,Pai(hp1^.next));
  1760. If (RefsEqual(Paicpu(hp2)^.oper[1].ref^, Paicpu(p)^.oper[0].ref^) And
  1761. Not(RegUsedAfterInstruction(Paicpu(p)^.oper[1].reg,
  1762. hp2, TmpUsedRegs)))
  1763. Then
  1764. { change mov (ref), reg }
  1765. { add/sub/or/... reg2/$const, reg }
  1766. { mov reg, (ref) }
  1767. { # release reg }
  1768. { to add/sub/or/... reg2/$const, (ref) }
  1769. Begin
  1770. case paicpu(hp1)^.opcode of
  1771. A_INC,A_DEC:
  1772. paicpu(hp1)^.LoadRef(0,newreference(Paicpu(p)^.oper[0].ref^))
  1773. else
  1774. paicpu(hp1)^.LoadRef(1,newreference(Paicpu(p)^.oper[0].ref^));
  1775. end;
  1776. AsmL^.Remove(p);
  1777. AsmL^.Remove(hp2);
  1778. Dispose(p,done);
  1779. Dispose(hp2,Done);
  1780. p := hp1
  1781. End;
  1782. End
  1783. {$endif foldArithOps}
  1784. else if (Paicpu(p)^.oper[0].typ = Top_Const) And
  1785. (Paicpu(p)^.oper[0].val = 0) And
  1786. (Paicpu(p)^.oper[1].typ = Top_Reg) Then
  1787. { change "mov $0, %reg" into "xor %reg, %reg" }
  1788. Begin
  1789. Paicpu(p)^.opcode := A_XOR;
  1790. Paicpu(p)^.LoadReg(0,Paicpu(p)^.oper[1].reg);
  1791. End
  1792. End;
  1793. A_MOVZX:
  1794. Begin
  1795. If (Paicpu(p)^.oper[1].typ = top_reg) Then
  1796. If (Paicpu(p)^.oper[0].typ = top_reg)
  1797. Then
  1798. Case Paicpu(p)^.opsize of
  1799. S_BL:
  1800. Begin
  1801. If IsGP32Reg(Paicpu(p)^.oper[1].reg) And
  1802. Not(CS_LittleSize in aktglobalswitches) And
  1803. (aktoptprocessor = ClassP5)
  1804. Then
  1805. {Change "movzbl %reg1, %reg2" to
  1806. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1807. PentiumMMX}
  1808. Begin
  1809. hp1 := New(Paicpu, op_reg_reg(A_XOR, S_L,
  1810. Paicpu(p)^.oper[1].reg, Paicpu(p)^.oper[1].reg));
  1811. InsertLLItem(AsmL,p^.previous, p, hp1);
  1812. Paicpu(p)^.opcode := A_MOV;
  1813. Paicpu(p)^.changeopsize(S_B);
  1814. Paicpu(p)^.LoadReg(1,Reg32ToReg8(Paicpu(p)^.oper[1].reg));
  1815. End;
  1816. End;
  1817. End
  1818. Else
  1819. If (Paicpu(p)^.oper[0].typ = top_ref) And
  1820. (Paicpu(p)^.oper[0].ref^.base <> Paicpu(p)^.oper[1].reg) And
  1821. (Paicpu(p)^.oper[0].ref^.index <> Paicpu(p)^.oper[1].reg) And
  1822. Not(CS_LittleSize in aktglobalswitches) And
  1823. IsGP32Reg(Paicpu(p)^.oper[1].reg) And
  1824. (aktoptprocessor = ClassP5) And
  1825. (Paicpu(p)^.opsize = S_BL)
  1826. Then
  1827. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1828. Pentium and PentiumMMX}
  1829. Begin
  1830. hp1 := New(Paicpu,op_reg_reg(A_XOR, S_L, Paicpu(p)^.oper[1].reg,
  1831. Paicpu(p)^.oper[1].reg));
  1832. Paicpu(p)^.opcode := A_MOV;
  1833. Paicpu(p)^.changeopsize(S_B);
  1834. Paicpu(p)^.LoadReg(1,Reg32ToReg8(Paicpu(p)^.oper[1].reg));
  1835. InsertLLItem(AsmL,p^.previous, p, hp1);
  1836. End;
  1837. End;
  1838. A_TEST, A_OR:
  1839. {removes the line marked with (x) from the sequence
  1840. And/or/xor/add/sub/... $x, %y
  1841. test/or %y, %y (x)
  1842. j(n)z _Label
  1843. as the first instruction already adjusts the ZF}
  1844. Begin
  1845. If OpsEqual(Paicpu(p)^.oper[0],Paicpu(p)^.oper[1]) Then
  1846. If GetLastInstruction(p, hp1) And
  1847. (pai(hp1)^.typ = ait_instruction) Then
  1848. Case Paicpu(hp1)^.opcode Of
  1849. A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_SHL, A_SHR:
  1850. Begin
  1851. If OpsEqual(Paicpu(hp1)^.oper[1],Paicpu(p)^.oper[0]) Then
  1852. Begin
  1853. hp1 := pai(p^.next);
  1854. asml^.remove(p);
  1855. dispose(p, done);
  1856. p := pai(hp1);
  1857. continue
  1858. End;
  1859. End;
  1860. A_DEC, A_INC, A_NEG:
  1861. Begin
  1862. If OpsEqual(Paicpu(hp1)^.oper[0],Paicpu(p)^.oper[0]) Then
  1863. Begin
  1864. Case Paicpu(hp1)^.opcode Of
  1865. A_DEC, A_INC:
  1866. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  1867. Begin
  1868. Case Paicpu(hp1)^.opcode Of
  1869. A_DEC: Paicpu(hp1)^.opcode := A_SUB;
  1870. A_INC: Paicpu(hp1)^.opcode := A_ADD;
  1871. End;
  1872. Paicpu(hp1)^.Loadoper(1,Paicpu(hp1)^.oper[0]);
  1873. Paicpu(hp1)^.LoadConst(0,1);
  1874. Paicpu(hp1)^.ops:=2;
  1875. End
  1876. End;
  1877. hp1 := pai(p^.next);
  1878. asml^.remove(p);
  1879. dispose(p, done);
  1880. p := pai(hp1);
  1881. continue
  1882. End;
  1883. End
  1884. End
  1885. End;
  1886. End;
  1887. End;
  1888. End;
  1889. p := Pai(p^.next)
  1890. End;
  1891. End;
  1892. End.
  1893. {
  1894. $Log$
  1895. Revision 1.1 2000-07-13 06:29:54 michael
  1896. + Initial import
  1897. Revision 1.97 2000/07/10 08:00:22 jonas
  1898. * real fix for web bug 1032 (removed previous coment since it was false)
  1899. Revision 1.95 2000/07/06 12:30:31 jonas
  1900. * moved "<flag setting operation>; test/or reg,reg" to "<flag setting
  1901. operation>" optimization to pass 2 because it caused problems
  1902. with -dnewoptimizations
  1903. Revision 1.94 2000/06/14 06:05:06 jonas
  1904. + support for inc/dec/imul in foldarithops
  1905. Revision 1.93 2000/05/23 10:58:46 jonas
  1906. * fixed bug in "subl $2,%esp; .. ; pushw mem" optimization when the
  1907. sub comes from setting up the stack frame instead of from aligning
  1908. esp (I hope)
  1909. Revision 1.92 2000/04/23 14:56:36 jonas
  1910. * changed "mov reg1, reg2; mov reg2, y" optimization that caused
  1911. regalloc info to become invalid (it's still performed, but the
  1912. regalloc info stays valid now)
  1913. Revision 1.91 2000/04/16 16:46:43 jonas
  1914. * small regalloc fix
  1915. Revision 1.90 2000/03/26 10:58:47 jonas
  1916. * some more allocRegBetween fixes (-al didn't function previously
  1917. if the compiler was compiled with -OG2p3r)
  1918. Revision 1.89 2000/03/26 08:46:52 jonas
  1919. * fixed bug in regUsedAfterInstruction (it didn't convert the reg
  1920. to 32bit before checking)
  1921. * result: make cycle now works with -OG3p3r!!!!
  1922. Revision 1.88 2000/03/25 18:57:02 jonas
  1923. * remove dealloc/alloc of reg1 between "movl %reg1,%reg2;
  1924. movl %reg2,%reg1" when removing the second instruction (it
  1925. confused the CSE and caused errors with -Or)
  1926. Revision 1.87 2000/02/13 14:21:50 jonas
  1927. * modifications to make the compiler functional when compiled with
  1928. -Or
  1929. Revision 1.86 2000/02/12 19:28:56 jonas
  1930. * fix for imul optimization in popt386 (exclude top_ref as first
  1931. argument)
  1932. * in csopt386: change "mov reg1,reg2; <several operations on reg2>;
  1933. mov reg2,reg1" to "<several operations on reg1>" (-dnewopt...)
  1934. Revision 1.85 2000/02/12 14:10:15 jonas
  1935. + change "mov reg1,reg2;imul x,reg2" to "imul x,reg1,reg2" in popt386
  1936. (-dnewoptimizations)
  1937. * shl(d) and shr(d) are considered to have a hardcoded register if
  1938. they use cl as shift count (since you can't replace them with
  1939. another register) in csopt386 (also for -dnewoptimizations)
  1940. Revision 1.84 2000/02/09 13:22:58 peter
  1941. * log truncated
  1942. Revision 1.83 2000/02/04 13:53:04 jonas
  1943. * fpuloadstore optimizations are now done before and after the CSE
  1944. Revision 1.82 2000/01/24 12:17:24 florian
  1945. * some improvemenst to cmov support
  1946. * disabled excpetion frame generation in cosntructors temporarily
  1947. Revision 1.81 2000/01/23 21:29:17 florian
  1948. * CMOV support in optimizer (in define USECMOV)
  1949. + start of support of exceptions in constructors
  1950. Revision 1.80 2000/01/22 16:05:15 jonas
  1951. + change "lea x(reg),reg" to "add x,reg" (-dnewoptimizations)
  1952. * detection whether edi is used after instructions (since regalloc
  1953. info for it is now available)
  1954. * better regUsedAfterInstruction function
  1955. Revision 1.79 2000/01/21 11:26:19 pierre
  1956. * bug fix for bug 802
  1957. Revision 1.78 2000/01/11 17:14:49 jonas
  1958. * fixed a serious memory leak
  1959. Revision 1.77 2000/01/09 12:35:02 jonas
  1960. * changed edi allocation to use getexplicitregister32/ungetregister
  1961. (adapted tgeni386 a bit for this) and enabled it by default
  1962. * fixed very big and stupid bug of mine in cg386mat that broke the
  1963. include() code (and make cycle :( ) if you compiled without
  1964. -dnewoptimizations
  1965. Revision 1.76 2000/01/07 01:14:30 peter
  1966. * updated copyright to 2000
  1967. Revision 1.75 1999/12/30 17:56:44 peter
  1968. * fixed and;jmp being translated into test;jmp
  1969. Revision 1.74 1999/12/05 16:48:43 jonas
  1970. * CSE of constant loading in regs works properly again
  1971. + if a constant is stored into memory using "mov const, ref" and
  1972. there is a reg that contains this const, it is changed into
  1973. "mov reg, ref"
  1974. Revision 1.73 1999/12/02 11:26:41 peter
  1975. * newoptimizations define added
  1976. Revision 1.72 1999/11/30 10:40:45 peter
  1977. + ttype, tsymlist
  1978. Revision 1.71 1999/11/27 23:47:55 jonas
  1979. + change "mov var,reg; add/shr/... x,reg; mov reg,var" to
  1980. "add/shr/... x,var" (if x is a const or reg, suggestion from Peter)
  1981. Enable with -dfoldArithOps
  1982. Revision 1.70 1999/11/21 13:09:41 jonas
  1983. * fixed some missed optimizations because 8bit regs were not always
  1984. taken into account
  1985. Revision 1.69 1999/11/13 19:03:56 jonas
  1986. * don't remove align objects between JMP's and labels
  1987. Revision 1.68 1999/11/06 16:24:00 jonas
  1988. * getfinaldestination works completely again (a lot of functionality
  1989. got lost in the conversion resulting from the removal of
  1990. ait_labeled_instruction)
  1991. Revision 1.67 1999/11/06 14:34:23 peter
  1992. * truncated log to 20 revs
  1993. Revision 1.66 1999/09/27 23:44:55 peter
  1994. * procinfo is now a pointer
  1995. * support for result setting in sub procedure
  1996. Revision 1.65 1999/09/05 14:27:19 florian
  1997. + fld reg;fxxx to fxxxr reg optimization
  1998. Revision 1.64 1999/08/25 12:00:02 jonas
  1999. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  2000. }