popt386.pas 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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. {$i fpcdefs.inc}
  20. Interface
  21. Uses Aasmbase,aasmtai,aasmcpu,verbose;
  22. Procedure PrePeepHoleOpts(AsmL: TAAsmOutput; BlockStart, BlockEnd: Tai);
  23. Procedure PeepHoleOptPass1(AsmL: TAAsmOutput; BlockStart, BlockEnd: Tai);
  24. Procedure PeepHoleOptPass2(AsmL: TAAsmOutput; BlockStart, BlockEnd: Tai);
  25. Procedure PostPeepHoleOpts(AsmL: TAAsmOutput; BlockStart, BlockEnd: Tai);
  26. Implementation
  27. Uses
  28. globtype,systems,
  29. globals,cgbase,
  30. symsym,symdef,
  31. {$ifdef finaldestdebug}
  32. cobjects,
  33. {$endif finaldestdebug}
  34. cpuinfo,cpubase,DAOpt386,cginfo,rgobj;
  35. Function RegUsedAfterInstruction(Reg: Tregister; p: Tai; Var UsedRegs: TRegSet): Boolean;
  36. Begin
  37. if reg.enum>lastreg then
  38. internalerror(200301081);
  39. reg := reg32(reg);
  40. UpdateUsedRegs(UsedRegs, Tai(p.Next));
  41. RegUsedAfterInstruction :=
  42. (Reg.enum in UsedRegs) and
  43. (not(getNextInstruction(p,p)) or
  44. not(regLoadedWithNewValue(reg,false,p)));
  45. End;
  46. function doFpuLoadStoreOpt(asmL: TAAsmoutput; var p: Tai): boolean;
  47. { returns true if a "continue" should be done after this optimization }
  48. var hp1, hp2: Tai;
  49. begin
  50. doFpuLoadStoreOpt := false;
  51. if (Taicpu(p).oper[0].typ = top_ref) and
  52. getNextInstruction(p, hp1) and
  53. (hp1.typ = ait_instruction) and
  54. (((Taicpu(hp1).opcode = A_FLD) and
  55. (Taicpu(p).opcode = A_FSTP)) or
  56. ((Taicpu(p).opcode = A_FISTP) and
  57. (Taicpu(hp1).opcode = A_FILD))) and
  58. (Taicpu(hp1).oper[0].typ = top_ref) and
  59. (Taicpu(hp1).opsize = Taicpu(p).opsize) and
  60. refsEqual(Taicpu(p).oper[0].ref^, Taicpu(hp1).oper[0].ref^) then
  61. begin
  62. if getNextInstruction(hp1, hp2) and
  63. (hp2.typ = ait_instruction) and
  64. ((Taicpu(hp2).opcode = A_LEAVE) or
  65. (Taicpu(hp2).opcode = A_RET)) and
  66. (Taicpu(p).oper[0].ref^.Base.enum = current_procinfo.FramePointer.enum) and
  67. (Taicpu(p).oper[0].ref^.Offset >= tvarsym(current_procinfo.procdef.funcretsym).adjusted_address) and
  68. (Taicpu(p).oper[0].ref^.Index.enum = R_NO) then
  69. begin
  70. asml.remove(p);
  71. asml.remove(hp1);
  72. p.free;
  73. hp1.free;
  74. p := hp2;
  75. removeLastDeallocForFuncRes(asmL, p);
  76. doFPULoadStoreOpt := true;
  77. end
  78. else
  79. { fst can't store an extended value! }
  80. if (Taicpu(p).opsize <> S_FX) and
  81. (Taicpu(p).opsize <> S_IQ) then
  82. begin
  83. if (Taicpu(p).opcode = A_FSTP) then
  84. Taicpu(p).opcode := A_FST
  85. else Taicpu(p).opcode := A_FIST;
  86. asml.remove(hp1);
  87. hp1.free;
  88. end
  89. end;
  90. end;
  91. Procedure PrePeepHoleOpts(AsmL: TAAsmOutput; BlockStart, BlockEnd: Tai);
  92. var
  93. p,hp1: Tai;
  94. l: Aword;
  95. tmpRef: treference;
  96. Begin
  97. P := BlockStart;
  98. While (P <> BlockEnd) Do
  99. Begin
  100. Case p.Typ Of
  101. Ait_Instruction:
  102. Begin
  103. Case Taicpu(p).opcode Of
  104. A_IMUL:
  105. {changes certain "imul const, %reg"'s to lea sequences}
  106. Begin
  107. If (Taicpu(p).oper[0].typ = Top_Const) And
  108. (Taicpu(p).oper[1].typ = Top_Reg) And
  109. (Taicpu(p).opsize = S_L) Then
  110. If (Taicpu(p).oper[0].val = 1) Then
  111. If (Taicpu(p).oper[2].typ = Top_None) Then
  112. {remove "imul $1, reg"}
  113. Begin
  114. hp1 := Tai(p.Next);
  115. asml.Remove(p);
  116. p.free;
  117. p := hp1;
  118. Continue;
  119. End
  120. Else
  121. {change "imul $1, reg1, reg2" to "mov reg1, reg2"}
  122. Begin
  123. hp1 := Taicpu.Op_Reg_Reg(A_MOV, S_L, Taicpu(p).oper[1].reg,Taicpu(p).oper[2].reg);
  124. InsertLLItem(AsmL, p.previous, p.next, hp1);
  125. p.free;
  126. p := hp1;
  127. End
  128. Else If
  129. ((Taicpu(p).oper[2].typ = Top_Reg) or
  130. (Taicpu(p).oper[2].typ = Top_None)) And
  131. (aktoptprocessor < ClassP6) And
  132. (Taicpu(p).oper[0].val <= 12) And
  133. Not(CS_LittleSize in aktglobalswitches) And
  134. (Not(GetNextInstruction(p, hp1)) Or
  135. {GetNextInstruction(p, hp1) And}
  136. Not((Tai(hp1).typ = ait_instruction) And
  137. ((Taicpu(hp1).opcode=A_Jcc) and
  138. (Taicpu(hp1).condition in [C_O,C_NO]))))
  139. Then
  140. Begin
  141. reference_reset_old(tmpref);
  142. Case Taicpu(p).oper[0].val Of
  143. 3: Begin
  144. {imul 3, reg1, reg2 to
  145. lea (reg1,reg1,2), reg2
  146. imul 3, reg1 to
  147. lea (reg1,reg1,2), reg1}
  148. TmpRef.base := Taicpu(p).oper[1].reg;
  149. TmpRef.Index := Taicpu(p).oper[1].reg;
  150. TmpRef.ScaleFactor := 2;
  151. If (Taicpu(p).oper[2].typ = Top_None) Then
  152. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[1].reg)
  153. Else
  154. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[2].reg);
  155. InsertLLItem(AsmL,p.previous, p.next, hp1);
  156. p.free;
  157. p := hp1;
  158. End;
  159. 5: Begin
  160. {imul 5, reg1, reg2 to
  161. lea (reg1,reg1,4), reg2
  162. imul 5, reg1 to
  163. lea (reg1,reg1,4), reg1}
  164. TmpRef.base := Taicpu(p).oper[1].reg;
  165. TmpRef.Index := Taicpu(p).oper[1].reg;
  166. TmpRef.ScaleFactor := 4;
  167. If (Taicpu(p).oper[2].typ = Top_None) Then
  168. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[1].reg)
  169. Else
  170. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[2].reg);
  171. InsertLLItem(AsmL,p.previous, p.next, hp1);
  172. p.free;
  173. p := hp1;
  174. End;
  175. 6: Begin
  176. {imul 6, reg1, reg2 to
  177. lea (,reg1,2), reg2
  178. lea (reg2,reg1,4), reg2
  179. imul 6, reg1 to
  180. lea (reg1,reg1,2), reg1
  181. add reg1, reg1}
  182. If (aktoptprocessor <= Class386)
  183. Then
  184. Begin
  185. TmpRef.Index := Taicpu(p).oper[1].reg;
  186. If (Taicpu(p).oper[2].typ = Top_Reg)
  187. Then
  188. Begin
  189. TmpRef.base := Taicpu(p).oper[2].reg;
  190. TmpRef.ScaleFactor := 4;
  191. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[1].reg);
  192. End
  193. Else
  194. Begin
  195. hp1 := Taicpu.op_reg_reg(A_ADD, S_L,
  196. Taicpu(p).oper[1].reg,Taicpu(p).oper[1].reg);
  197. End;
  198. InsertLLItem(AsmL,p, p.next, hp1);
  199. reference_reset_old(tmpref);
  200. TmpRef.Index := Taicpu(p).oper[1].reg;
  201. TmpRef.ScaleFactor := 2;
  202. If (Taicpu(p).oper[2].typ = Top_Reg)
  203. Then
  204. Begin
  205. TmpRef.base.enum := R_NO;
  206. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef,
  207. Taicpu(p).oper[2].reg);
  208. End
  209. Else
  210. Begin
  211. TmpRef.base := Taicpu(p).oper[1].reg;
  212. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[1].reg);
  213. End;
  214. InsertLLItem(AsmL,p.previous, p.next, hp1);
  215. p.free;
  216. p := Tai(hp1.next);
  217. End
  218. End;
  219. 9: Begin
  220. {imul 9, reg1, reg2 to
  221. lea (reg1,reg1,8), reg2
  222. imul 9, reg1 to
  223. lea (reg1,reg1,8), reg1}
  224. TmpRef.base := Taicpu(p).oper[1].reg;
  225. TmpRef.Index := Taicpu(p).oper[1].reg;
  226. TmpRef.ScaleFactor := 8;
  227. If (Taicpu(p).oper[2].typ = Top_None) Then
  228. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[1].reg)
  229. Else
  230. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[2].reg);
  231. InsertLLItem(AsmL,p.previous, p.next, hp1);
  232. p.free;
  233. p := hp1;
  234. End;
  235. 10: Begin
  236. {imul 10, reg1, reg2 to
  237. lea (reg1,reg1,4), reg2
  238. add reg2, reg2
  239. imul 10, reg1 to
  240. lea (reg1,reg1,4), reg1
  241. add reg1, reg1}
  242. If (aktoptprocessor <= Class386) Then
  243. Begin
  244. If (Taicpu(p).oper[2].typ = Top_Reg) Then
  245. hp1 := Taicpu.op_reg_reg(A_ADD, S_L,
  246. Taicpu(p).oper[2].reg,Taicpu(p).oper[2].reg)
  247. Else
  248. hp1 := Taicpu.op_reg_reg(A_ADD, S_L,
  249. Taicpu(p).oper[1].reg,Taicpu(p).oper[1].reg);
  250. InsertLLItem(AsmL,p, p.next, hp1);
  251. TmpRef.base := Taicpu(p).oper[1].reg;
  252. TmpRef.Index := Taicpu(p).oper[1].reg;
  253. TmpRef.ScaleFactor := 4;
  254. If (Taicpu(p).oper[2].typ = Top_Reg)
  255. Then
  256. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[2].reg)
  257. Else
  258. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[1].reg);
  259. InsertLLItem(AsmL,p.previous, p.next, hp1);
  260. p.free;
  261. p := Tai(hp1.next);
  262. End
  263. End;
  264. 12: Begin
  265. {imul 12, reg1, reg2 to
  266. lea (,reg1,4), reg2
  267. lea (,reg1,8) reg2
  268. imul 12, reg1 to
  269. lea (reg1,reg1,2), reg1
  270. lea (,reg1,4), reg1}
  271. If (aktoptprocessor <= Class386)
  272. Then
  273. Begin
  274. TmpRef.Index := Taicpu(p).oper[1].reg;
  275. If (Taicpu(p).oper[2].typ = Top_Reg) Then
  276. Begin
  277. TmpRef.base := Taicpu(p).oper[2].reg;
  278. TmpRef.ScaleFactor := 8;
  279. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[2].reg);
  280. End
  281. Else
  282. Begin
  283. TmpRef.base.enum := R_NO;
  284. TmpRef.ScaleFactor := 4;
  285. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[1].reg);
  286. End;
  287. InsertLLItem(AsmL,p, p.next, hp1);
  288. reference_reset_old(tmpref);
  289. TmpRef.Index := Taicpu(p).oper[1].reg;
  290. If (Taicpu(p).oper[2].typ = Top_Reg) Then
  291. Begin
  292. TmpRef.base.enum := R_NO;
  293. TmpRef.ScaleFactor := 4;
  294. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[2].reg);
  295. End
  296. Else
  297. Begin
  298. TmpRef.base := Taicpu(p).oper[1].reg;
  299. TmpRef.ScaleFactor := 2;
  300. hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef, Taicpu(p).oper[1].reg);
  301. End;
  302. InsertLLItem(AsmL,p.previous, p.next, hp1);
  303. p.free;
  304. p := Tai(hp1.next);
  305. End
  306. End
  307. End;
  308. End;
  309. End;
  310. A_SAR, A_SHR:
  311. {changes the code sequence
  312. shr/sar const1, x
  313. shl const2, x
  314. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  315. Begin
  316. If GetNextInstruction(p, hp1) And
  317. (Tai(hp1).typ = ait_instruction) and
  318. (Taicpu(hp1).opcode = A_SHL) and
  319. (Taicpu(p).oper[0].typ = top_const) and
  320. (Taicpu(hp1).oper[0].typ = top_const) and
  321. (Taicpu(hp1).opsize = Taicpu(p).opsize) And
  322. (Taicpu(hp1).oper[1].typ = Taicpu(p).oper[1].typ) And
  323. OpsEqual(Taicpu(hp1).oper[1], Taicpu(p).oper[1])
  324. Then
  325. If (Taicpu(p).oper[0].val > Taicpu(hp1).oper[0].val) And
  326. Not(CS_LittleSize In aktglobalswitches)
  327. Then
  328. { shr/sar const1, %reg
  329. shl const2, %reg
  330. with const1 > const2 }
  331. Begin
  332. Taicpu(p).LoadConst(0,Taicpu(p).oper[0].val-Taicpu(hp1).oper[0].val);
  333. Taicpu(hp1).opcode := A_AND;
  334. l := (1 shl (Taicpu(hp1).oper[0].val)) - 1;
  335. Case Taicpu(p).opsize Of
  336. S_L: Taicpu(hp1).LoadConst(0,l Xor aword($ffffffff));
  337. S_B: Taicpu(hp1).LoadConst(0,l Xor $ff);
  338. S_W: Taicpu(hp1).LoadConst(0,l Xor $ffff);
  339. End;
  340. End
  341. Else
  342. If (Taicpu(p).oper[0].val<Taicpu(hp1).oper[0].val) And
  343. Not(CS_LittleSize In aktglobalswitches)
  344. Then
  345. { shr/sar const1, %reg
  346. shl const2, %reg
  347. with const1 < const2 }
  348. Begin
  349. Taicpu(hp1).LoadConst(0,Taicpu(hp1).oper[0].val-Taicpu(p).oper[0].val);
  350. Taicpu(p).opcode := A_AND;
  351. l := (1 shl (Taicpu(p).oper[0].val))-1;
  352. Case Taicpu(p).opsize Of
  353. S_L: Taicpu(p).LoadConst(0,l Xor aword($ffffffff));
  354. S_B: Taicpu(p).LoadConst(0,l Xor $ff);
  355. S_W: Taicpu(p).LoadConst(0,l Xor $ffff);
  356. End;
  357. End
  358. Else
  359. { shr/sar const1, %reg
  360. shl const2, %reg
  361. with const1 = const2 }
  362. if (Taicpu(p).oper[0].val = Taicpu(hp1).oper[0].val) then
  363. Begin
  364. Taicpu(p).opcode := A_AND;
  365. l := (1 shl (Taicpu(p).oper[0].val))-1;
  366. Case Taicpu(p).opsize Of
  367. S_B: Taicpu(p).LoadConst(0,l Xor $ff);
  368. S_W: Taicpu(p).LoadConst(0,l Xor $ffff);
  369. S_L: Taicpu(p).LoadConst(0,l Xor aword($ffffffff));
  370. End;
  371. asml.remove(hp1);
  372. hp1.free;
  373. End;
  374. End;
  375. A_XOR:
  376. If (Taicpu(p).oper[0].typ = top_reg) And
  377. (Taicpu(p).oper[1].typ = top_reg) And
  378. (Taicpu(p).oper[0].reg.enum = Taicpu(p).oper[1].reg.enum) then
  379. { temporarily change this to 'mov reg,0' to make it easier }
  380. { for the CSE. Will be changed back in pass 2 }
  381. begin
  382. Taicpu(p).opcode := A_MOV;
  383. Taicpu(p).loadconst(0,0);
  384. end;
  385. End;
  386. End;
  387. End;
  388. p := Tai(p.next)
  389. End;
  390. End;
  391. Procedure PeepHoleOptPass1(Asml: TAAsmOutput; BlockStart, BlockEnd: Tai);
  392. {First pass of peepholeoptimizations}
  393. Var
  394. l,l1 : longint;
  395. p,hp1,hp2 : Tai;
  396. hp3,hp4: Tai;
  397. TmpRef: TReference;
  398. UsedRegs, TmpUsedRegs: TRegSet;
  399. TmpBool1, TmpBool2: Boolean;
  400. r:Tregister;
  401. Function SkipLabels(hp: Tai; var hp2: Tai): boolean;
  402. {skips all labels and returns the next "real" instruction}
  403. Begin
  404. While assigned(hp.next) and
  405. (Tai(hp.next).typ In SkipInstr + [ait_label,ait_align]) Do
  406. hp := Tai(hp.next);
  407. If assigned(hp.next) Then
  408. Begin
  409. SkipLabels := True;
  410. hp2 := Tai(hp.next)
  411. End
  412. Else
  413. Begin
  414. hp2 := hp;
  415. SkipLabels := False
  416. End;
  417. End;
  418. function GetFinalDestination(AsmL: TAAsmOutput; hp: Taicpu; level: longint): boolean;
  419. {traces sucessive jumps to their final destination and sets it, e.g.
  420. je l1 je l3
  421. <code> <code>
  422. l1: becomes l1:
  423. je l2 je l3
  424. <code> <code>
  425. l2: l2:
  426. jmp l3 jmp l3
  427. the level parameter denotes how deeep we have already followed the jump,
  428. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  429. Var p1, p2: Tai;
  430. l: tasmlabel;
  431. Function FindAnyLabel(hp: Tai; var l: tasmlabel): Boolean;
  432. Begin
  433. FindAnyLabel := false;
  434. While assigned(hp.next) and
  435. (Tai(hp.next).typ In (SkipInstr+[ait_align])) Do
  436. hp := Tai(hp.next);
  437. If assigned(hp.next) and
  438. (Tai(hp.next).typ = ait_label) Then
  439. Begin
  440. FindAnyLabel := true;
  441. l := Tai_label(hp.next).l;
  442. End
  443. End;
  444. Begin
  445. if level > 20 then
  446. exit;
  447. GetfinalDestination := false;
  448. If (tasmlabel(hp.oper[0].sym).labelnr >= LoLab) and
  449. (tasmlabel(hp.oper[0].sym).labelnr <= HiLab) and {range check, a jump can go past an assembler block!}
  450. Assigned(LTable^[tasmlabel(hp.oper[0].sym).labelnr-LoLab].TaiObj) Then
  451. Begin
  452. p1 := LTable^[tasmlabel(hp.oper[0].sym).labelnr-LoLab].TaiObj; {the jump's destination}
  453. SkipLabels(p1,p1);
  454. If (Tai(p1).typ = ait_instruction) and
  455. (Taicpu(p1).is_jmp) Then
  456. If { the next instruction after the label where the jump hp arrives}
  457. { is unconditional or of the same type as hp, so continue }
  458. (Taicpu(p1).condition in [C_None,hp.condition]) or
  459. { the next instruction after the label where the jump hp arrives}
  460. { is the opposite of hp (so this one is never taken), but after }
  461. { that one there is a branch that will be taken, so perform a }
  462. { little hack: set p1 equal to this instruction (that's what the}
  463. { last SkipLabels is for, only works with short bool evaluation)}
  464. ((Taicpu(p1).condition = inverse_cond[hp.condition]) and
  465. SkipLabels(p1,p2) and
  466. (p2.typ = ait_instruction) and
  467. (Taicpu(p2).is_jmp) and
  468. (Taicpu(p2).condition in [C_None,hp.condition]) and
  469. SkipLabels(p1,p1)) Then
  470. Begin
  471. { quick check for loops of the form "l5: ; jmp l5 }
  472. if (tasmlabel(Taicpu(p1).oper[0].sym).labelnr =
  473. tasmlabel(hp.oper[0].sym).labelnr) then
  474. exit;
  475. if not GetFinalDestination(asml, Taicpu(p1),succ(level)) then
  476. exit;
  477. tasmlabel(hp.oper[0].sym).decrefs;
  478. hp.oper[0].sym:=Taicpu(p1).oper[0].sym;
  479. tasmlabel(hp.oper[0].sym).increfs;
  480. End
  481. Else
  482. If (Taicpu(p1).condition = inverse_cond[hp.condition]) then
  483. if not FindAnyLabel(p1,l) then
  484. begin
  485. {$ifdef finaldestdebug}
  486. insertllitem(asml,p1,p1.next,tai_comment.Create(
  487. strpnew('previous label inserted'))));
  488. {$endif finaldestdebug}
  489. objectlibrary.getlabel(l);
  490. insertllitem(asml,p1,p1.next,Tai_label.Create(l));
  491. tasmlabel(Taicpu(hp).oper[0].sym).decrefs;
  492. hp.oper[0].sym := l;
  493. l.increfs;
  494. { this won't work, since the new label isn't in the labeltable }
  495. { so it will fail the rangecheck. Labeltable should become a }
  496. { hashtable to support this: }
  497. { GetFinalDestination(asml, hp); }
  498. end
  499. else
  500. begin
  501. {$ifdef finaldestdebug}
  502. insertllitem(asml,p1,p1.next,tai_comment.Create(
  503. strpnew('next label reused'))));
  504. {$endif finaldestdebug}
  505. l.increfs;
  506. hp.oper[0].sym := l;
  507. if not GetFinalDestination(asml, hp,succ(level)) then
  508. exit;
  509. end;
  510. End;
  511. GetFinalDestination := true;
  512. End;
  513. Function DoSubAddOpt(var p: Tai): Boolean;
  514. Begin
  515. DoSubAddOpt := False;
  516. If GetLastInstruction(p, hp1) And
  517. (hp1.typ = ait_instruction) And
  518. (Taicpu(hp1).opsize = Taicpu(p).opsize) then
  519. Case Taicpu(hp1).opcode Of
  520. A_DEC:
  521. If (Taicpu(hp1).oper[0].typ = top_reg) And
  522. (Taicpu(hp1).oper[0].reg.enum = Taicpu(p).oper[1].reg.enum) Then
  523. Begin
  524. Taicpu(p).LoadConst(0,Taicpu(p).oper[0].val+1);
  525. asml.Remove(hp1);
  526. hp1.free;
  527. End;
  528. A_SUB:
  529. If (Taicpu(hp1).oper[0].typ = top_const) And
  530. (Taicpu(hp1).oper[1].typ = top_reg) And
  531. (Taicpu(hp1).oper[1].reg.enum = Taicpu(p).oper[1].reg.enum) Then
  532. Begin
  533. Taicpu(p).LoadConst(0,Taicpu(p).oper[0].val+Taicpu(hp1).oper[0].val);
  534. asml.Remove(hp1);
  535. hp1.free;
  536. End;
  537. A_ADD:
  538. If (Taicpu(hp1).oper[0].typ = top_const) And
  539. (Taicpu(hp1).oper[1].typ = top_reg) And
  540. (Taicpu(hp1).oper[1].reg.enum = Taicpu(p).oper[1].reg.enum) Then
  541. Begin
  542. Taicpu(p).LoadConst(0,AWord(int64(Taicpu(p).oper[0].val)-int64(Taicpu(hp1).oper[0].val)));
  543. asml.Remove(hp1);
  544. hp1.free;
  545. If (Taicpu(p).oper[0].val = 0) Then
  546. Begin
  547. hp1 := Tai(p.next);
  548. asml.Remove(p);
  549. p.free;
  550. If Not GetLastInstruction(hp1, p) Then
  551. p := hp1;
  552. DoSubAddOpt := True;
  553. End
  554. End;
  555. End;
  556. End;
  557. Begin
  558. P := BlockStart;
  559. UsedRegs := [];
  560. While (P <> BlockEnd) Do
  561. Begin
  562. UpDateUsedRegs(UsedRegs, Tai(p.next));
  563. Case p.Typ Of
  564. ait_instruction:
  565. Begin
  566. { Handle Jmp Optimizations }
  567. if Taicpu(p).is_jmp then
  568. begin
  569. {the following if-block removes all code between a jmp and the next label,
  570. because it can never be executed}
  571. If (Taicpu(p).opcode = A_JMP) Then
  572. Begin
  573. While GetNextInstruction(p, hp1) and
  574. (hp1.typ <> ait_label) do
  575. If not(hp1.typ in ([ait_label,ait_align]+skipinstr)) Then
  576. Begin
  577. asml.Remove(hp1);
  578. hp1.free;
  579. End
  580. else break;
  581. End;
  582. { remove jumps to a label coming right after them }
  583. If GetNextInstruction(p, hp1) then
  584. Begin
  585. if FindLabel(tasmlabel(Taicpu(p).oper[0].sym), hp1) and
  586. {$warning FIXME removing the first instruction fails}
  587. (p<>blockstart) then
  588. Begin
  589. hp2:=Tai(hp1.next);
  590. asml.remove(p);
  591. p.free;
  592. p:=hp2;
  593. continue;
  594. end
  595. Else
  596. Begin
  597. if hp1.typ = ait_label then
  598. SkipLabels(hp1,hp1);
  599. If (Tai(hp1).typ=ait_instruction) and
  600. (Taicpu(hp1).opcode=A_JMP) and
  601. GetNextInstruction(hp1, hp2) And
  602. FindLabel(tasmlabel(Taicpu(p).oper[0].sym), hp2)
  603. Then
  604. Begin
  605. if Taicpu(p).opcode=A_Jcc then
  606. Taicpu(p).condition:=inverse_cond[Taicpu(p).condition]
  607. else
  608. begin
  609. If (LabDif <> 0) Then
  610. GetFinalDestination(asml, Taicpu(p),0);
  611. p:=Tai(p.next);
  612. continue;
  613. end;
  614. Tai_label(hp2).l.decrefs;
  615. Taicpu(p).oper[0].sym:=Taicpu(hp1).oper[0].sym;
  616. Taicpu(p).oper[0].sym.increfs;
  617. asml.remove(hp1);
  618. hp1.free;
  619. If (LabDif <> 0) Then
  620. GetFinalDestination(asml, Taicpu(p),0);
  621. end
  622. else
  623. If (LabDif <> 0) Then
  624. GetFinalDestination(asml, Taicpu(p),0);
  625. end;
  626. end;
  627. end
  628. else
  629. { All other optimizes }
  630. begin
  631. For l := 0 to 2 Do
  632. If (Taicpu(p).oper[l].typ = top_ref) Then
  633. With Taicpu(p).oper[l].ref^ Do
  634. Begin
  635. If (base.enum = R_NO) And
  636. (index.enum <> R_NO) And
  637. (scalefactor in [0,1])
  638. Then
  639. Begin
  640. base := index;
  641. index.enum := R_NO
  642. End
  643. End;
  644. Case Taicpu(p).opcode Of
  645. A_AND:
  646. Begin
  647. If (Taicpu(p).oper[0].typ = top_const) And
  648. (Taicpu(p).oper[1].typ = top_reg) And
  649. GetNextInstruction(p, hp1) And
  650. (Tai(hp1).typ = ait_instruction) And
  651. (Taicpu(hp1).opcode = A_AND) And
  652. (Taicpu(hp1).oper[0].typ = top_const) And
  653. (Taicpu(hp1).oper[1].typ = top_reg) And
  654. (Taicpu(p).oper[1].reg.enum = Taicpu(hp1).oper[1].reg.enum)
  655. Then
  656. {change "and const1, reg; and const2, reg" to "and (const1 and const2), reg"}
  657. Begin
  658. Taicpu(p).LoadConst(0,Taicpu(p).oper[0].val And Taicpu(hp1).oper[0].val);
  659. asml.Remove(hp1);
  660. hp1.free;
  661. End
  662. Else
  663. {change "and x, reg; jxx" to "test x, reg", if reg is deallocated before the
  664. jump, but only if it's a conditional jump (PFV) }
  665. If (Taicpu(p).oper[1].typ = top_reg) And
  666. GetNextInstruction(p, hp1) And
  667. (hp1.typ = ait_instruction) And
  668. (Taicpu(hp1).is_jmp) and
  669. (Taicpu(hp1).opcode<>A_JMP) and
  670. Not(Taicpu(p).oper[1].reg.enum in UsedRegs) Then
  671. Taicpu(p).opcode := A_TEST;
  672. End;
  673. A_CMP:
  674. Begin
  675. If (Taicpu(p).oper[0].typ = top_const) And
  676. (Taicpu(p).oper[1].typ in [top_reg,top_ref]) And
  677. (Taicpu(p).oper[0].val = 0) and
  678. GetNextInstruction(p, hp1) And
  679. (hp1.typ = ait_instruction) And
  680. (Taicpu(hp1).is_jmp) and
  681. (Taicpu(hp1).opcode=A_Jcc) and
  682. (Taicpu(hp1).condition in [C_LE,C_BE]) and
  683. GetNextInstruction(hp1,hp2) and
  684. (hp2.typ = ait_instruction) and
  685. (Taicpu(hp2).opcode = A_DEC) And
  686. OpsEqual(Taicpu(hp2).oper[0],Taicpu(p).oper[1]) And
  687. GetNextInstruction(hp2, hp3) And
  688. (hp3.typ = ait_instruction) and
  689. (Taicpu(hp3).is_jmp) and
  690. (Taicpu(hp3).opcode = A_JMP) And
  691. GetNextInstruction(hp3, hp4) And
  692. FindLabel(tasmlabel(Taicpu(hp1).oper[0].sym),hp4) Then
  693. Begin
  694. Taicpu(hp2).Opcode := A_SUB;
  695. Taicpu(hp2).Loadoper(1,Taicpu(hp2).oper[0]);
  696. Taicpu(hp2).LoadConst(0,1);
  697. Taicpu(hp2).ops:=2;
  698. Taicpu(hp3).Opcode := A_Jcc;
  699. Case Taicpu(hp1).condition of
  700. C_LE: Taicpu(hp3).condition := C_GE;
  701. C_BE: Taicpu(hp3).condition := C_AE;
  702. End;
  703. asml.Remove(p);
  704. asml.Remove(hp1);
  705. p.free;
  706. hp1.free;
  707. p := hp2;
  708. continue;
  709. End
  710. End;
  711. A_FLD:
  712. Begin
  713. If (Taicpu(p).oper[0].typ = top_reg) And
  714. GetNextInstruction(p, hp1) And
  715. (hp1.typ = Ait_Instruction) And
  716. (Taicpu(hp1).oper[0].typ = top_reg) And
  717. (Taicpu(hp1).oper[1].typ = top_reg) And
  718. (Taicpu(hp1).oper[0].reg.enum = R_ST) And
  719. (Taicpu(hp1).oper[1].reg.enum = R_ST1) Then
  720. { change to
  721. fld reg fxxx reg,st
  722. fxxxp st, st1 (hp1)
  723. Remark: non commutative operations must be reversed!
  724. }
  725. begin
  726. Case Taicpu(hp1).opcode Of
  727. A_FMULP,A_FADDP,
  728. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  729. begin
  730. Case Taicpu(hp1).opcode Of
  731. A_FADDP: Taicpu(hp1).opcode := A_FADD;
  732. A_FMULP: Taicpu(hp1).opcode := A_FMUL;
  733. A_FSUBP: Taicpu(hp1).opcode := A_FSUBR;
  734. A_FSUBRP: Taicpu(hp1).opcode := A_FSUB;
  735. A_FDIVP: Taicpu(hp1).opcode := A_FDIVR;
  736. A_FDIVRP: Taicpu(hp1).opcode := A_FDIV;
  737. End;
  738. Taicpu(hp1).oper[0].reg := Taicpu(p).oper[0].reg;
  739. Taicpu(hp1).oper[1].reg.enum := R_ST;
  740. asml.Remove(p);
  741. p.free;
  742. p := hp1;
  743. Continue;
  744. end;
  745. end;
  746. end
  747. else
  748. If (Taicpu(p).oper[0].typ = top_ref) And
  749. GetNextInstruction(p, hp2) And
  750. (hp2.typ = Ait_Instruction) And
  751. (Taicpu(hp2).oper[0].typ = top_reg) And
  752. (Taicpu(hp2).oper[1].typ = top_reg) And
  753. (Taicpu(p).opsize in [S_FS, S_FL]) And
  754. (Taicpu(hp2).oper[0].reg.enum = R_ST) And
  755. (Taicpu(hp2).oper[1].reg.enum = R_ST1) Then
  756. If GetLastInstruction(p, hp1) And
  757. (hp1.typ = Ait_Instruction) And
  758. ((Taicpu(hp1).opcode = A_FLD) Or
  759. (Taicpu(hp1).opcode = A_FST)) And
  760. (Taicpu(hp1).opsize = Taicpu(p).opsize) And
  761. (Taicpu(hp1).oper[0].typ = top_ref) And
  762. RefsEqual(Taicpu(p).oper[0].ref^, Taicpu(hp1).oper[0].ref^) Then
  763. If ((Taicpu(hp2).opcode = A_FMULP) Or
  764. (Taicpu(hp2).opcode = A_FADDP)) Then
  765. { change to
  766. fld/fst mem1 (hp1) fld/fst mem1
  767. fld mem1 (p) fadd/
  768. faddp/ fmul st, st
  769. fmulp st, st1 (hp2) }
  770. Begin
  771. asml.Remove(p);
  772. p.free;
  773. p := hp1;
  774. If (Taicpu(hp2).opcode = A_FADDP) Then
  775. Taicpu(hp2).opcode := A_FADD
  776. Else
  777. Taicpu(hp2).opcode := A_FMUL;
  778. Taicpu(hp2).oper[1].reg.enum := R_ST;
  779. End
  780. Else
  781. { change to
  782. fld/fst mem1 (hp1) fld/fst mem1
  783. fld mem1 (p) fld st}
  784. Begin
  785. Taicpu(p).changeopsize(S_FL);
  786. r.enum:=R_ST;
  787. Taicpu(p).loadreg(0,r);
  788. End
  789. Else
  790. Begin
  791. Case Taicpu(hp2).opcode Of
  792. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  793. { change to
  794. fld/fst mem1 (hp1) fld/fst mem1
  795. fld mem2 (p) fxxx mem2
  796. fxxxp st, st1 (hp2) }
  797. Begin
  798. Case Taicpu(hp2).opcode Of
  799. A_FADDP: Taicpu(p).opcode := A_FADD;
  800. A_FMULP: Taicpu(p).opcode := A_FMUL;
  801. A_FSUBP: Taicpu(p).opcode := A_FSUBR;
  802. A_FSUBRP: Taicpu(p).opcode := A_FSUB;
  803. A_FDIVP: Taicpu(p).opcode := A_FDIVR;
  804. A_FDIVRP: Taicpu(p).opcode := A_FDIV;
  805. End;
  806. asml.Remove(hp2);
  807. hp2.free;
  808. End
  809. End
  810. End
  811. End;
  812. A_FSTP,A_FISTP:
  813. if doFpuLoadStoreOpt(asmL,p) then
  814. continue;
  815. A_LEA:
  816. Begin
  817. {removes seg register prefixes from LEA operations, as they
  818. don't do anything}
  819. Taicpu(p).oper[0].ref^.Segment.enum := R_NO;
  820. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  821. If (Taicpu(p).oper[0].ref^.Base.enum In [R_EAX..R_EDI]) And
  822. (Taicpu(p).oper[0].ref^.Index.enum = R_NO) And
  823. (Not(Assigned(Taicpu(p).oper[0].ref^.Symbol))) Then
  824. If (Taicpu(p).oper[0].ref^.Base.enum <> Taicpu(p).oper[1].reg.enum)
  825. and (Taicpu(p).oper[0].ref^.Offset = 0)
  826. Then
  827. Begin
  828. hp1 := Taicpu.op_reg_reg(A_MOV, S_L,Taicpu(p).oper[0].ref^.Base,
  829. Taicpu(p).oper[1].reg);
  830. InsertLLItem(AsmL,p.previous,p.next, hp1);
  831. p.free;
  832. p := hp1;
  833. Continue;
  834. End
  835. Else
  836. if (Taicpu(p).oper[0].ref^.Offset = 0) then
  837. Begin
  838. hp1 := Tai(p.Next);
  839. asml.Remove(p);
  840. p.free;
  841. p := hp1;
  842. Continue;
  843. End
  844. else
  845. with Taicpu(p).oper[0].ref^ do
  846. if (Base.enum = Taicpu(p).oper[1].reg.enum) then
  847. begin
  848. l := offset+offsetfixup;
  849. if (l=1) then
  850. begin
  851. Taicpu(p).opcode := A_INC;
  852. Taicpu(p).loadreg(0,Taicpu(p).oper[1].reg);
  853. Taicpu(p).ops := 1
  854. end
  855. else
  856. if (l=-1) then
  857. begin
  858. Taicpu(p).opcode := A_DEC;
  859. Taicpu(p).loadreg(0,Taicpu(p).oper[1].reg);
  860. Taicpu(p).ops := 1;
  861. end
  862. else
  863. begin
  864. Taicpu(p).opcode := A_ADD;
  865. Taicpu(p).loadconst(0,aword(l));
  866. end;
  867. end;
  868. End;
  869. A_MOV:
  870. Begin
  871. TmpUsedRegs := UsedRegs;
  872. If (Taicpu(p).oper[1].typ = top_reg) And
  873. (Taicpu(p).oper[1].reg.enum In [R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI]) And
  874. GetNextInstruction(p, hp1) And
  875. (Tai(hp1).typ = ait_instruction) And
  876. (Taicpu(hp1).opcode = A_MOV) And
  877. (Taicpu(hp1).oper[0].typ = top_reg) And
  878. (Taicpu(hp1).oper[0].reg.enum = Taicpu(p).oper[1].reg.enum)
  879. Then
  880. {we have "mov x, %treg; mov %treg, y}
  881. If not(RegUsedAfterInstruction(Taicpu(p).oper[1].reg, hp1, TmpUsedRegs)) then
  882. {we've got "mov x, %treg; mov %treg, y; with %treg is not used after }
  883. Case Taicpu(p).oper[0].typ Of
  884. top_reg:
  885. Begin
  886. { change "mov %reg, %treg; mov %treg, y"
  887. to "mov %reg, y" }
  888. Taicpu(p).LoadOper(1,Taicpu(hp1).oper[1]);
  889. asml.Remove(hp1);
  890. hp1.free;
  891. continue;
  892. End;
  893. top_ref:
  894. If (Taicpu(hp1).oper[1].typ = top_reg) Then
  895. Begin
  896. { change "mov mem, %treg; mov %treg, %reg"
  897. to "mov mem, %reg" }
  898. Taicpu(p).Loadoper(1,Taicpu(hp1).oper[1]);
  899. asml.Remove(hp1);
  900. hp1.free;
  901. continue;
  902. End;
  903. End
  904. Else
  905. Else
  906. {Change "mov %reg1, %reg2; xxx %reg2, ???" to
  907. "mov %reg1, %reg2; xxx %reg1, ???" to avoid a write/read
  908. penalty}
  909. If (Taicpu(p).oper[0].typ = top_reg) And
  910. (Taicpu(p).oper[1].typ = top_reg) And
  911. GetNextInstruction(p,hp1) And
  912. (Tai(hp1).typ = ait_instruction) And
  913. (Taicpu(hp1).oper[0].typ = top_reg) And
  914. (Taicpu(hp1).oper[0].reg.enum = Taicpu(p).oper[1].reg.enum)
  915. Then
  916. {we have "mov %reg1, %reg2; XXX %reg2, ???"}
  917. Begin
  918. If ((Taicpu(hp1).opcode = A_OR) Or
  919. (Taicpu(hp1).opcode = A_TEST)) And
  920. (Taicpu(hp1).oper[1].typ = top_reg) And
  921. (Taicpu(hp1).oper[0].reg.enum = Taicpu(hp1).oper[1].reg.enum)
  922. Then
  923. {we have "mov %reg1, %reg2; test/or %reg2, %reg2"}
  924. Begin
  925. TmpUsedRegs := UsedRegs;
  926. { reg1 will be used after the first instruction, }
  927. { so update the allocation info }
  928. allocRegBetween(asmL,Taicpu(p).oper[0].reg,p,hp1);
  929. If GetNextInstruction(hp1, hp2) And
  930. (hp2.typ = ait_instruction) And
  931. Taicpu(hp2).is_jmp and
  932. Not(RegUsedAfterInstruction(Taicpu(hp1).oper[0].reg, hp1, TmpUsedRegs))
  933. Then
  934. {change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  935. "test %reg1, %reg1; jxx"}
  936. Begin
  937. Taicpu(hp1).Loadoper(0,Taicpu(p).oper[0]);
  938. Taicpu(hp1).Loadoper(1,Taicpu(p).oper[0]);
  939. asml.Remove(p);
  940. p.free;
  941. p := hp1;
  942. continue
  943. End
  944. Else
  945. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  946. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  947. Begin
  948. Taicpu(hp1).Loadoper(0,Taicpu(p).oper[0]);
  949. Taicpu(hp1).Loadoper(1,Taicpu(p).oper[0]);
  950. End;
  951. End
  952. { Else
  953. If (Taicpu(p.next)^.opcode
  954. In [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  955. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  956. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  957. End
  958. Else
  959. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  960. x >= RetOffset) as it doesn't do anything (it writes either to a
  961. parameter or to the temporary storage room for the function
  962. result)}
  963. If GetNextInstruction(p, hp1) And
  964. (Tai(hp1).typ = ait_instruction)
  965. Then
  966. If ((Taicpu(hp1).opcode = A_LEAVE) Or
  967. (Taicpu(hp1).opcode = A_RET)) And
  968. (Taicpu(p).oper[1].typ = top_ref) And
  969. (Taicpu(p).oper[1].ref^.base.enum = current_procinfo.FramePointer.enum) And
  970. (Taicpu(p).oper[1].ref^.offset >= tvarsym(current_procinfo.procdef.funcretsym).adjusted_address) And
  971. (Taicpu(p).oper[1].ref^.index.enum = R_NO) And
  972. (Taicpu(p).oper[0].typ = top_reg)
  973. Then
  974. Begin
  975. asml.Remove(p);
  976. p.free;
  977. p := hp1;
  978. RemoveLastDeallocForFuncRes(asmL,p);
  979. End
  980. Else
  981. If (Taicpu(p).oper[0].typ = top_reg) And
  982. (Taicpu(p).oper[1].typ = top_ref) And
  983. (Taicpu(p).opsize = Taicpu(hp1).opsize) And
  984. (Taicpu(hp1).opcode = A_CMP) And
  985. (Taicpu(hp1).oper[1].typ = top_ref) And
  986. RefsEqual(Taicpu(p).oper[1].ref^, Taicpu(hp1).oper[1].ref^) Then
  987. {change "mov reg1, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  988. begin
  989. Taicpu(hp1).loadreg(1,Taicpu(p).oper[0].reg);
  990. allocRegBetween(asmL,Taicpu(p).oper[0].reg,p,hp1);
  991. end;
  992. { Next instruction is also a MOV ? }
  993. If GetNextInstruction(p, hp1) And
  994. (Tai(hp1).typ = ait_instruction) and
  995. (Taicpu(hp1).opcode = A_MOV) and
  996. (Taicpu(hp1).opsize = Taicpu(p).opsize)
  997. Then
  998. Begin
  999. If (Taicpu(hp1).oper[0].typ = Taicpu(p).oper[1].typ) and
  1000. (Taicpu(hp1).oper[1].typ = Taicpu(p).oper[0].typ)
  1001. Then
  1002. {mov reg1, mem1 or mov mem1, reg1
  1003. mov mem2, reg2 mov reg2, mem2}
  1004. Begin
  1005. If OpsEqual(Taicpu(hp1).oper[1],Taicpu(p).oper[0]) Then
  1006. {mov reg1, mem1 or mov mem1, reg1
  1007. mov mem2, reg1 mov reg2, mem1}
  1008. Begin
  1009. If OpsEqual(Taicpu(hp1).oper[0],Taicpu(p).oper[1]) Then
  1010. { Removes the second statement from
  1011. mov reg1, mem1/reg2
  1012. mov mem1/reg2, reg1 }
  1013. Begin
  1014. if (Taicpu(p).oper[0].typ = top_reg) then
  1015. AllocRegBetween(asmL,Taicpu(p).oper[0].reg,p,hp1);
  1016. asml.remove(hp1);
  1017. hp1.free;
  1018. End
  1019. Else
  1020. Begin
  1021. TmpUsedRegs := UsedRegs;
  1022. UpdateUsedRegs(TmpUsedRegs, Tai(hp1.next));
  1023. If (Taicpu(p).oper[1].typ = top_ref) And
  1024. { mov reg1, mem1
  1025. mov mem2, reg1 }
  1026. GetNextInstruction(hp1, hp2) And
  1027. (hp2.typ = ait_instruction) And
  1028. (Taicpu(hp2).opcode = A_CMP) And
  1029. (Taicpu(hp2).opsize = Taicpu(p).opsize) and
  1030. (Taicpu(hp2).oper[0].typ = TOp_Ref) And
  1031. (Taicpu(hp2).oper[1].typ = TOp_Reg) And
  1032. RefsEqual(Taicpu(hp2).oper[0].ref^, Taicpu(p).oper[1].ref^) And
  1033. (Taicpu(hp2).oper[1].reg .enum= Taicpu(p).oper[0].reg.enum) And
  1034. Not(RegUsedAfterInstruction(Taicpu(p).oper[0].reg, hp2, TmpUsedRegs)) Then
  1035. { change to
  1036. mov reg1, mem1 mov reg1, mem1
  1037. mov mem2, reg1 cmp reg1, mem2
  1038. cmp mem1, reg1 }
  1039. Begin
  1040. asml.Remove(hp2);
  1041. hp2.free;
  1042. Taicpu(hp1).opcode := A_CMP;
  1043. Taicpu(hp1).loadref(1,Taicpu(hp1).oper[0].ref^);
  1044. Taicpu(hp1).loadreg(0,Taicpu(p).oper[0].reg);
  1045. End;
  1046. End;
  1047. End
  1048. Else
  1049. Begin
  1050. tmpUsedRegs := UsedRegs;
  1051. r.enum:=R_EDI;
  1052. If GetNextInstruction(hp1, hp2) And
  1053. (Taicpu(p).oper[0].typ = top_ref) And
  1054. (Taicpu(p).oper[1].typ = top_reg) And
  1055. (Taicpu(hp1).oper[0].typ = top_reg) And
  1056. (Taicpu(hp1).oper[0].reg.enum = Taicpu(p).oper[1].reg.enum) And
  1057. (Taicpu(hp1).oper[1].typ = top_ref) And
  1058. (Tai(hp2).typ = ait_instruction) And
  1059. (Taicpu(hp2).opcode = A_MOV) And
  1060. (Taicpu(hp2).opsize = Taicpu(p).opsize) and
  1061. (Taicpu(hp2).oper[1].typ = top_reg) And
  1062. (Taicpu(hp2).oper[0].typ = top_ref) And
  1063. RefsEqual(Taicpu(hp2).oper[0].ref^, Taicpu(hp1).oper[1].ref^) Then
  1064. If not regInRef(Taicpu(hp2).oper[1].reg,Taicpu(hp2).oper[0].ref^) and
  1065. (Taicpu(p).oper[1].reg.enum in [R_DI,R_EDI]) and
  1066. not(RegUsedAfterInstruction(r,hp1,tmpUsedRegs)) Then
  1067. { mov mem1, %edi
  1068. mov %edi, mem2
  1069. mov mem2, reg2
  1070. to:
  1071. mov mem1, reg2
  1072. mov reg2, mem2}
  1073. Begin
  1074. AllocRegBetween(asmL,reg32(Taicpu(hp2).oper[1].reg),p,hp2);
  1075. Taicpu(p).Loadoper(1,Taicpu(hp2).oper[1]);
  1076. Taicpu(hp1).loadoper(0,Taicpu(hp2).oper[1]);
  1077. asml.Remove(hp2);
  1078. hp2.free;
  1079. End
  1080. Else
  1081. If (Taicpu(p).oper[1].reg.enum <> Taicpu(hp2).oper[1].reg.enum) And
  1082. not(RegInRef(Taicpu(p).oper[1].reg,Taicpu(p).oper[0].ref^)) And
  1083. not(RegInRef(Taicpu(hp2).oper[1].reg,Taicpu(hp2).oper[0].ref^))
  1084. Then
  1085. { mov mem1, reg1 mov mem1, reg1
  1086. mov reg1, mem2 mov reg1, mem2
  1087. mov mem2, reg2 mov mem2, reg1
  1088. to: to:
  1089. mov mem1, reg1 mov mem1, reg1
  1090. mov mem1, reg2 mov reg1, mem2
  1091. mov reg1, mem2
  1092. or (if mem1 depends on reg1
  1093. and/or if mem2 depends on reg2)
  1094. to:
  1095. mov mem1, reg1
  1096. mov reg1, mem2
  1097. mov reg1, reg2
  1098. }
  1099. Begin
  1100. Taicpu(hp1).LoadRef(0,Taicpu(p).oper[0].ref^);
  1101. Taicpu(hp1).LoadReg(1,Taicpu(hp2).oper[1].reg);
  1102. Taicpu(hp2).LoadRef(1,Taicpu(hp2).oper[0].ref^);
  1103. Taicpu(hp2).LoadReg(0,Taicpu(p).oper[1].reg);
  1104. allocRegBetween(asmL,Taicpu(p).oper[1].reg,p,hp2);
  1105. { if (Taicpu(p).oper[0].ref^.base.enum in (rg.usableregsint+[R_EDI])) then
  1106. allocRegBetween(asmL,Taicpu(p).oper[0].ref^.base,p,hp2);
  1107. if (Taicpu(p).oper[0].ref^.index.enum in (rg.usableregsint+[R_EDI])) then
  1108. allocRegBetween(asmL,Taicpu(p).oper[0].ref^.index,p,hp2);}
  1109. if (Taicpu(p).oper[0].ref^.base.enum in [R_EDI]) then
  1110. allocRegBetween(asmL,Taicpu(p).oper[0].ref^.base,p,hp2);
  1111. if (Taicpu(p).oper[0].ref^.index.enum in [R_EDI]) then
  1112. allocRegBetween(asmL,Taicpu(p).oper[0].ref^.index,p,hp2);
  1113. End
  1114. Else
  1115. If (Taicpu(hp1).Oper[0].reg.enum <> Taicpu(hp2).Oper[1].reg.enum) Then
  1116. begin
  1117. Taicpu(hp2).LoadReg(0,Taicpu(hp1).Oper[0].reg);
  1118. allocRegBetween(asmL,Taicpu(p).oper[1].reg,p,hp2);
  1119. end
  1120. else
  1121. begin
  1122. asml.Remove(hp2);
  1123. hp2.free;
  1124. end
  1125. End;
  1126. End
  1127. Else
  1128. (* {movl [mem1],reg1
  1129. movl [mem1],reg2
  1130. to:
  1131. movl [mem1],reg1
  1132. movl reg1,reg2 }
  1133. If (Taicpu(p).oper[0].typ = top_ref) and
  1134. (Taicpu(p).oper[1].typ = top_reg) and
  1135. (Taicpu(hp1).oper[0].typ = top_ref) and
  1136. (Taicpu(hp1).oper[1].typ = top_reg) and
  1137. (Taicpu(p).opsize = Taicpu(hp1).opsize) and
  1138. RefsEqual(TReference(Taicpu(p).oper[0]^),Taicpu(hp1).oper[0]^.ref^) and
  1139. (Taicpu(p).oper[1].reg<>Taicpu(hp1).oper[0]^.ref^.base) and
  1140. (Taicpu(p).oper[1].reg<>Taicpu(hp1).oper[0]^.ref^.index) then
  1141. Taicpu(hp1).LoadReg(0,Taicpu(p).oper[1].reg)
  1142. Else*)
  1143. { movl const1,[mem1]
  1144. movl [mem1],reg1
  1145. to:
  1146. movl const1,reg1
  1147. movl reg1,[mem1] }
  1148. If (Taicpu(p).oper[0].typ = top_const) and
  1149. (Taicpu(p).oper[1].typ = top_ref) and
  1150. (Taicpu(hp1).oper[0].typ = top_ref) and
  1151. (Taicpu(hp1).oper[1].typ = top_reg) and
  1152. (Taicpu(p).opsize = Taicpu(hp1).opsize) and
  1153. RefsEqual(Taicpu(hp1).oper[0].ref^,Taicpu(p).oper[1].ref^) then
  1154. Begin
  1155. allocregbetween(asml,Taicpu(hp1).oper[1].reg,p,hp1);
  1156. { allocregbetween doesn't insert this because at }
  1157. { this time, no regalloc info is available in }
  1158. { the optinfo field, so do it manually (JM) }
  1159. hp2 := tai_regalloc.Alloc(Taicpu(hp1).oper[1].reg);
  1160. insertllitem(asml,p.previous,p,hp2);
  1161. Taicpu(hp1).LoadReg(0,Taicpu(hp1).oper[1].reg);
  1162. Taicpu(hp1).LoadRef(1,Taicpu(p).oper[1].ref^);
  1163. Taicpu(p).LoadReg(1,Taicpu(hp1).oper[0].reg);
  1164. End
  1165. End;
  1166. End;
  1167. A_MOVZX:
  1168. Begin
  1169. {removes superfluous And's after movzx's}
  1170. If (Taicpu(p).oper[1].typ = top_reg) And
  1171. GetNextInstruction(p, hp1) And
  1172. (Tai(hp1).typ = ait_instruction) And
  1173. (Taicpu(hp1).opcode = A_AND) And
  1174. (Taicpu(hp1).oper[0].typ = top_const) And
  1175. (Taicpu(hp1).oper[1].typ = top_reg) And
  1176. (Taicpu(hp1).oper[1].reg.enum = Taicpu(p).oper[1].reg.enum)
  1177. Then
  1178. Case Taicpu(p).opsize Of
  1179. S_BL, S_BW:
  1180. If (Taicpu(hp1).oper[0].val = $ff) Then
  1181. Begin
  1182. asml.Remove(hp1);
  1183. hp1.free;
  1184. End;
  1185. S_WL:
  1186. If (Taicpu(hp1).oper[0].val = $ffff) Then
  1187. Begin
  1188. asml.Remove(hp1);
  1189. hp1.free;
  1190. End;
  1191. End;
  1192. {changes some movzx constructs to faster synonims (all examples
  1193. are given with eax/ax, but are also valid for other registers)}
  1194. If (Taicpu(p).oper[1].typ = top_reg) Then
  1195. If (Taicpu(p).oper[0].typ = top_reg) Then
  1196. Case Taicpu(p).opsize of
  1197. S_BW:
  1198. Begin
  1199. If (changeregsize(Taicpu(p).oper[0].reg,S_W).enum=Taicpu(p).oper[1].reg.enum) And
  1200. Not(CS_LittleSize In aktglobalswitches)
  1201. Then
  1202. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  1203. Begin
  1204. Taicpu(p).opcode := A_AND;
  1205. Taicpu(p).changeopsize(S_W);
  1206. Taicpu(p).LoadConst(0,$ff);
  1207. End
  1208. Else
  1209. If GetNextInstruction(p, hp1) And
  1210. (Tai(hp1).typ = ait_instruction) And
  1211. (Taicpu(hp1).opcode = A_AND) And
  1212. (Taicpu(hp1).oper[0].typ = top_const) And
  1213. (Taicpu(hp1).oper[1].typ = top_reg) And
  1214. (Taicpu(hp1).oper[1].reg.enum = Taicpu(p).oper[1].reg.enum)
  1215. Then
  1216. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  1217. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  1218. Begin
  1219. Taicpu(p).opcode := A_MOV;
  1220. Taicpu(p).changeopsize(S_W);
  1221. Taicpu(p).LoadReg(0,changeregsize(Taicpu(p).oper[0].reg,S_W));
  1222. Taicpu(hp1).LoadConst(0,Taicpu(hp1).oper[0].val And $ff);
  1223. End;
  1224. End;
  1225. S_BL:
  1226. Begin
  1227. If (changeregsize(Taicpu(p).oper[0].reg,S_L).enum=Taicpu(p).oper[1].reg.enum) And
  1228. Not(CS_LittleSize in aktglobalswitches)
  1229. Then
  1230. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  1231. Begin
  1232. Taicpu(p).opcode := A_AND;
  1233. Taicpu(p).changeopsize(S_L);
  1234. Taicpu(p).loadconst(0,$ff)
  1235. End
  1236. Else
  1237. If GetNextInstruction(p, hp1) And
  1238. (Tai(hp1).typ = ait_instruction) And
  1239. (Taicpu(hp1).opcode = A_AND) And
  1240. (Taicpu(hp1).oper[0].typ = top_const) And
  1241. (Taicpu(hp1).oper[1].typ = top_reg) And
  1242. (Taicpu(hp1).oper[1].reg.enum = Taicpu(p).oper[1].reg.enum)
  1243. Then
  1244. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  1245. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  1246. Begin
  1247. Taicpu(p).opcode := A_MOV;
  1248. Taicpu(p).changeopsize(S_L);
  1249. Taicpu(p).LoadReg(0,changeregsize(Taicpu(p).oper[0].reg,S_L));
  1250. Taicpu(hp1).LoadConst(0,Taicpu(hp1).oper[0].val And $ff);
  1251. End
  1252. End;
  1253. S_WL:
  1254. Begin
  1255. If (changeregsize(Taicpu(p).oper[0].reg,S_L).enum=Taicpu(p).oper[1].reg.enum) And
  1256. Not(CS_LittleSize In aktglobalswitches)
  1257. Then
  1258. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  1259. Begin
  1260. Taicpu(p).opcode := A_AND;
  1261. Taicpu(p).changeopsize(S_L);
  1262. Taicpu(p).LoadConst(0,$ffff);
  1263. End
  1264. Else
  1265. If GetNextInstruction(p, hp1) And
  1266. (Tai(hp1).typ = ait_instruction) And
  1267. (Taicpu(hp1).opcode = A_AND) And
  1268. (Taicpu(hp1).oper[0].typ = top_const) And
  1269. (Taicpu(hp1).oper[1].typ = top_reg) And
  1270. (Taicpu(hp1).oper[1].reg.enum = Taicpu(p).oper[1].reg.enum)
  1271. Then
  1272. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  1273. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  1274. Begin
  1275. Taicpu(p).opcode := A_MOV;
  1276. Taicpu(p).changeopsize(S_L);
  1277. Taicpu(p).LoadReg(0,changeregsize(Taicpu(p).oper[0].reg,S_L));
  1278. Taicpu(hp1).LoadConst(0,Taicpu(hp1).oper[0].val And $ffff);
  1279. End;
  1280. End;
  1281. End
  1282. Else
  1283. If (Taicpu(p).oper[0].typ = top_ref) Then
  1284. Begin
  1285. If GetNextInstruction(p, hp1) And
  1286. (Tai(hp1).typ = ait_instruction) And
  1287. (Taicpu(hp1).opcode = A_AND) And
  1288. (Taicpu(hp1).oper[0].typ = Top_Const) And
  1289. (Taicpu(hp1).oper[1].typ = Top_Reg) And
  1290. (Taicpu(hp1).oper[1].reg.enum = Taicpu(p).oper[1].reg.enum) Then
  1291. Begin
  1292. Taicpu(p).opcode := A_MOV;
  1293. Case Taicpu(p).opsize Of
  1294. S_BL:
  1295. Begin
  1296. Taicpu(p).changeopsize(S_L);
  1297. Taicpu(hp1).LoadConst(0,Taicpu(hp1).oper[0].val And $ff);
  1298. End;
  1299. S_WL:
  1300. Begin
  1301. Taicpu(p).changeopsize(S_L);
  1302. Taicpu(hp1).LoadConst(0,Taicpu(hp1).oper[0].val And $ffff);
  1303. End;
  1304. S_BW:
  1305. Begin
  1306. Taicpu(p).changeopsize(S_W);
  1307. Taicpu(hp1).LoadConst(0,Taicpu(hp1).oper[0].val And $ff);
  1308. End;
  1309. End;
  1310. End;
  1311. End;
  1312. End;
  1313. A_POP:
  1314. Begin
  1315. if target_info.system=system_i386_go32v2 then
  1316. begin
  1317. { Transform a series of pop/pop/pop/push/push/push to }
  1318. { 'movl x(%esp),%reg' for go32v2 (not for the rest, }
  1319. { because I'm not sure whether they can cope with }
  1320. { 'movl x(%esp),%reg' with x > 0, I believe we had }
  1321. { such a problem when using esp as frame pointer (JM) }
  1322. if (Taicpu(p).oper[0].typ = top_reg) then
  1323. begin
  1324. hp1 := p;
  1325. hp2 := p;
  1326. l := 0;
  1327. while getNextInstruction(hp1,hp1) and
  1328. (hp1.typ = ait_instruction) and
  1329. (Taicpu(hp1).opcode = A_POP) and
  1330. (Taicpu(hp1).oper[0].typ = top_reg) do
  1331. begin
  1332. hp2 := hp1;
  1333. inc(l,4);
  1334. end;
  1335. getLastInstruction(p,hp3);
  1336. l1 := 0;
  1337. while (hp2 <> hp3) and
  1338. assigned(hp1) and
  1339. (hp1.typ = ait_instruction) and
  1340. (Taicpu(hp1).opcode = A_PUSH) and
  1341. (Taicpu(hp1).oper[0].typ = top_reg) and
  1342. (Taicpu(hp1).oper[0].reg.enum = Taicpu(hp2).oper[0].reg.enum) do
  1343. begin
  1344. { change it to a two op operation }
  1345. Taicpu(hp2).oper[1].typ:=top_none;
  1346. Taicpu(hp2).ops:=2;
  1347. Taicpu(hp2).opcode := A_MOV;
  1348. Taicpu(hp2).Loadoper(1,Taicpu(hp1).oper[0]);
  1349. reference_reset_old(tmpref);
  1350. tmpRef.base.enum:=R_INTREGISTER;
  1351. tmpRef.base.number:=NR_STACK_POINTER_REG;
  1352. convert_register_to_enum(tmpref.base);
  1353. tmpRef.offset := l;
  1354. Taicpu(hp2).loadRef(0,tmpRef);
  1355. hp4 := hp1;
  1356. getNextInstruction(hp1,hp1);
  1357. asml.remove(hp4);
  1358. hp4.free;
  1359. getLastInstruction(hp2,hp2);
  1360. dec(l,4);
  1361. inc(l1);
  1362. end;
  1363. if l <> -4 then
  1364. begin
  1365. inc(l,4);
  1366. for l1 := l1 downto 1 do
  1367. begin
  1368. getNextInstruction(hp2,hp2);
  1369. dec(Taicpu(hp2).oper[0].ref^.offset,l);
  1370. end
  1371. end
  1372. end
  1373. end
  1374. else
  1375. begin
  1376. if (Taicpu(p).oper[0].typ = top_reg) And
  1377. GetNextInstruction(p, hp1) And
  1378. (Tai(hp1).typ=ait_instruction) and
  1379. (Taicpu(hp1).opcode=A_PUSH) and
  1380. (Taicpu(hp1).oper[0].typ = top_reg) And
  1381. (Taicpu(hp1).oper[0].reg.enum=Taicpu(p).oper[0].reg.enum) then
  1382. Begin
  1383. { change it to a two op operation }
  1384. Taicpu(p).oper[1].typ:=top_none;
  1385. Taicpu(p).ops:=2;
  1386. Taicpu(p).opcode := A_MOV;
  1387. Taicpu(p).Loadoper(1,Taicpu(p).oper[0]);
  1388. reference_reset_old(tmpref);
  1389. TmpRef.base.enum := R_ESP;
  1390. Taicpu(p).LoadRef(0,TmpRef);
  1391. asml.Remove(hp1);
  1392. hp1.free;
  1393. End;
  1394. end;
  1395. end;
  1396. A_PUSH:
  1397. Begin
  1398. If (Taicpu(p).opsize = S_W) And
  1399. (Taicpu(p).oper[0].typ = Top_Const) And
  1400. GetNextInstruction(p, hp1) And
  1401. (Tai(hp1).typ = ait_instruction) And
  1402. (Taicpu(hp1).opcode = A_PUSH) And
  1403. (Taicpu(hp1).oper[0].typ = Top_Const) And
  1404. (Taicpu(hp1).opsize = S_W) Then
  1405. Begin
  1406. Taicpu(p).changeopsize(S_L);
  1407. Taicpu(p).LoadConst(0,Taicpu(p).oper[0].val shl 16 + word(Taicpu(hp1).oper[0].val));
  1408. asml.Remove(hp1);
  1409. hp1.free;
  1410. End;
  1411. End;
  1412. A_SHL, A_SAL:
  1413. Begin
  1414. If (Taicpu(p).oper[0].typ = Top_Const) And
  1415. (Taicpu(p).oper[1].typ = Top_Reg) And
  1416. (Taicpu(p).opsize = S_L) And
  1417. (Taicpu(p).oper[0].val <= 3)
  1418. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1419. Then
  1420. Begin
  1421. TmpBool1 := True; {should we check the next instruction?}
  1422. TmpBool2 := False; {have we found an add/sub which could be
  1423. integrated in the lea?}
  1424. reference_reset_old(tmpref);
  1425. TmpRef.index := Taicpu(p).oper[1].reg;
  1426. TmpRef.scalefactor := 1 shl Taicpu(p).oper[0].val;
  1427. While TmpBool1 And
  1428. GetNextInstruction(p, hp1) And
  1429. (Tai(hp1).typ = ait_instruction) And
  1430. ((((Taicpu(hp1).opcode = A_ADD) Or
  1431. (Taicpu(hp1).opcode = A_SUB)) And
  1432. (Taicpu(hp1).oper[1].typ = Top_Reg) And
  1433. (Taicpu(hp1).oper[1].reg.enum = Taicpu(p).oper[1].reg.enum)) or
  1434. (((Taicpu(hp1).opcode = A_INC) or
  1435. (Taicpu(hp1).opcode = A_DEC)) and
  1436. (Taicpu(hp1).oper[0].typ = Top_Reg) and
  1437. (Taicpu(hp1).oper[0].reg.enum = Taicpu(p).oper[1].reg.enum))) Do
  1438. Begin
  1439. TmpBool1 := False;
  1440. If (Taicpu(hp1).oper[0].typ = Top_Const)
  1441. Then
  1442. Begin
  1443. TmpBool1 := True;
  1444. TmpBool2 := True;
  1445. case Taicpu(hp1).opcode of
  1446. A_ADD:
  1447. inc(TmpRef.offset, longint(Taicpu(hp1).oper[0].val));
  1448. A_SUB:
  1449. dec(TmpRef.offset, longint(Taicpu(hp1).oper[0].val));
  1450. end;
  1451. asml.Remove(hp1);
  1452. hp1.free;
  1453. End
  1454. Else
  1455. If (Taicpu(hp1).oper[0].typ = Top_Reg) And
  1456. (((Taicpu(hp1).opcode = A_ADD) And
  1457. (TmpRef.base.enum = R_NO)) or
  1458. (Taicpu(hp1).opcode = A_INC) or
  1459. (Taicpu(hp1).opcode = A_DEC)) Then
  1460. Begin
  1461. TmpBool1 := True;
  1462. TmpBool2 := True;
  1463. case Taicpu(hp1).opcode of
  1464. A_ADD:
  1465. TmpRef.base := Taicpu(hp1).oper[0].reg;
  1466. A_INC:
  1467. inc(TmpRef.offset);
  1468. A_DEC:
  1469. dec(TmpRef.offset);
  1470. end;
  1471. asml.Remove(hp1);
  1472. hp1.free;
  1473. End;
  1474. End;
  1475. If TmpBool2 Or
  1476. ((aktoptprocessor < ClassP6) And
  1477. (Taicpu(p).oper[0].val <= 3) And
  1478. Not(CS_LittleSize in aktglobalswitches))
  1479. Then
  1480. Begin
  1481. If Not(TmpBool2) And
  1482. (Taicpu(p).oper[0].val = 1)
  1483. Then
  1484. Begin
  1485. hp1 := Taicpu.Op_reg_reg(A_ADD,Taicpu(p).opsize,
  1486. Taicpu(p).oper[1].reg, Taicpu(p).oper[1].reg)
  1487. End
  1488. Else hp1 := Taicpu.op_ref_reg(A_LEA, S_L, TmpRef,
  1489. Taicpu(p).oper[1].reg);
  1490. InsertLLItem(AsmL,p.previous, p.next, hp1);
  1491. p.free;
  1492. p := hp1;
  1493. End;
  1494. End
  1495. Else
  1496. If (aktoptprocessor < ClassP6) And
  1497. (Taicpu(p).oper[0].typ = top_const) And
  1498. (Taicpu(p).oper[1].typ = top_reg) Then
  1499. If (Taicpu(p).oper[0].val = 1)
  1500. Then
  1501. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1502. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  1503. (unlike shl, which is only Tairable in the U pipe)}
  1504. Begin
  1505. hp1 := Taicpu.Op_reg_reg(A_ADD,Taicpu(p).opsize,
  1506. Taicpu(p).oper[1].reg, Taicpu(p).oper[1].reg);
  1507. InsertLLItem(AsmL,p.previous, p.next, hp1);
  1508. p.free;
  1509. p := hp1;
  1510. End
  1511. Else If (Taicpu(p).opsize = S_L) and
  1512. (Taicpu(p).oper[0].val<= 3) Then
  1513. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1514. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1515. Begin
  1516. reference_reset_old(tmpref);
  1517. TmpRef.index := Taicpu(p).oper[1].reg;
  1518. TmpRef.scalefactor := 1 shl Taicpu(p).oper[0].val;
  1519. hp1 := Taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, Taicpu(p).oper[1].reg);
  1520. InsertLLItem(AsmL,p.previous, p.next, hp1);
  1521. p.free;
  1522. p := hp1;
  1523. End
  1524. End;
  1525. A_SETcc :
  1526. { changes
  1527. setcc (funcres) setcc reg
  1528. movb (funcres), reg to leave/ret
  1529. leave/ret }
  1530. Begin
  1531. If (Taicpu(p).oper[0].typ = top_ref) And
  1532. GetNextInstruction(p, hp1) And
  1533. GetNextInstruction(hp1, hp2) And
  1534. (hp2.typ = ait_instruction) And
  1535. ((Taicpu(hp2).opcode = A_LEAVE) or
  1536. (Taicpu(hp2).opcode = A_RET)) And
  1537. (Taicpu(p).oper[0].ref^.Base.enum = current_procinfo.FramePointer.enum) And
  1538. (Taicpu(p).oper[0].ref^.Index.enum = R_NO) And
  1539. (Taicpu(p).oper[0].ref^.Offset >= tvarsym(current_procinfo.procdef.funcretsym).adjusted_address) And
  1540. (hp1.typ = ait_instruction) And
  1541. (Taicpu(hp1).opcode = A_MOV) And
  1542. (Taicpu(hp1).opsize = S_B) And
  1543. (Taicpu(hp1).oper[0].typ = top_ref) And
  1544. RefsEqual(Taicpu(hp1).oper[0].ref^, Taicpu(p).oper[0].ref^) Then
  1545. Begin
  1546. Taicpu(p).LoadReg(0,Taicpu(hp1).oper[1].reg);
  1547. asml.Remove(hp1);
  1548. hp1.free;
  1549. End
  1550. End;
  1551. A_SUB:
  1552. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1553. { * change "sub/add const1, reg" or "dec reg" followed by
  1554. "sub const2, reg" to one "sub ..., reg" }
  1555. Begin
  1556. If (Taicpu(p).oper[0].typ = top_const) And
  1557. (Taicpu(p).oper[1].typ = top_reg) Then
  1558. If (Taicpu(p).oper[0].val = 2) And
  1559. (Taicpu(p).oper[1].reg.enum = R_ESP) and
  1560. { Don't do the sub/push optimization if the sub }
  1561. { comes from setting up the stack frame (JM) }
  1562. (not getLastInstruction(p,hp1) or
  1563. (hp1.typ <> ait_instruction) or
  1564. (Taicpu(hp1).opcode <> A_MOV) or
  1565. (Taicpu(hp1).oper[0].typ <> top_reg) or
  1566. (Taicpu(hp1).oper[0].reg.enum <> R_ESP) or
  1567. (Taicpu(hp1).oper[1].typ <> top_reg) or
  1568. (Taicpu(hp1).oper[1].reg.enum <> R_EBP)) then
  1569. Begin
  1570. hp1 := Tai(p.next);
  1571. r.enum:=R_ESP;
  1572. While Assigned(hp1) And
  1573. (Tai(hp1).typ In [ait_instruction]+SkipInstr) And
  1574. not regReadByInstruction(r,hp1) and
  1575. not regModifiedByInstruction(r,hp1) do
  1576. hp1 := Tai(hp1.next);
  1577. If Assigned(hp1) And
  1578. (Tai(hp1).typ = ait_instruction) And
  1579. (Taicpu(hp1).opcode = A_PUSH) And
  1580. (Taicpu(hp1).opsize = S_W)
  1581. Then
  1582. Begin
  1583. Taicpu(hp1).changeopsize(S_L);
  1584. if Taicpu(hp1).oper[0].typ=top_reg then
  1585. Taicpu(hp1).LoadReg(0,changeregsize(Taicpu(hp1).oper[0].reg,S_L));
  1586. hp1 := Tai(p.next);
  1587. asml.Remove(p);
  1588. p.free;
  1589. p := hp1;
  1590. Continue
  1591. End;
  1592. If DoSubAddOpt(p) Then continue;
  1593. End
  1594. Else If DoSubAddOpt(p) Then Continue
  1595. End;
  1596. A_XOR:
  1597. If (Taicpu(p).oper[0].typ = top_reg) And
  1598. (Taicpu(p).oper[1].typ = top_reg) And
  1599. (Taicpu(p).oper[0].reg.enum = Taicpu(p).oper[1].reg.enum) then
  1600. { temporarily change this to 'mov reg,0' to make it easier }
  1601. { for the CSE. Will be changed back in pass 2 }
  1602. begin
  1603. Taicpu(p).opcode := A_MOV;
  1604. Taicpu(p).loadconst(0,0);
  1605. end;
  1606. End;
  1607. end; { if is_jmp }
  1608. End;
  1609. { ait_label:
  1610. Begin
  1611. If labelCanBeSkipped(Tai_label(p))
  1612. Then
  1613. Begin
  1614. hp1 := Tai(p.next);
  1615. asml.Remove(p);
  1616. p.free;
  1617. p := hp1;
  1618. Continue
  1619. End;
  1620. End;}
  1621. End;
  1622. updateUsedRegs(UsedRegs,p);
  1623. p:=Tai(p.next);
  1624. end;
  1625. end;
  1626. function isFoldableArithOp(hp1: Taicpu; reg: Toldregister): boolean;
  1627. begin
  1628. IsFoldableArithOp := False;
  1629. case hp1.opcode of
  1630. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  1631. isFoldableArithOp :=
  1632. ((Taicpu(hp1).oper[0].typ = top_const) or
  1633. ((Taicpu(hp1).oper[0].typ = top_reg) and
  1634. (Taicpu(hp1).oper[0].reg.enum <> reg))) and
  1635. (Taicpu(hp1).oper[1].typ = top_reg) and
  1636. (Taicpu(hp1).oper[1].reg.enum = reg);
  1637. A_INC,A_DEC:
  1638. isFoldableArithOp :=
  1639. (Taicpu(hp1).oper[0].typ = top_reg) and
  1640. (Taicpu(hp1).oper[0].reg.enum = reg);
  1641. end;
  1642. end;
  1643. Procedure PeepHoleOptPass2(AsmL: TAAsmOutput; BlockStart, BlockEnd: Tai);
  1644. {$ifdef USECMOV}
  1645. function CanBeCMOV(p : Tai) : boolean;
  1646. begin
  1647. CanBeCMOV:=assigned(p) and (p.typ=ait_instruction) and
  1648. (Taicpu(p).opcode=A_MOV) and
  1649. (Taicpu(p).opsize in [S_L,S_W]) and
  1650. (Taicpu(p).oper[0].typ in [top_reg,top_ref]) and
  1651. (Taicpu(p).oper[1].typ in [top_reg]);
  1652. end;
  1653. {$endif USECMOV}
  1654. var
  1655. p,hp1,hp2: Tai;
  1656. {$ifdef USECMOV}
  1657. l : longint;
  1658. condition : tasmcond;
  1659. hp3: Tai;
  1660. {$endif USECMOV}
  1661. UsedRegs, TmpUsedRegs: TRegSet;
  1662. Begin
  1663. P := BlockStart;
  1664. UsedRegs := [];
  1665. While (P <> BlockEnd) Do
  1666. Begin
  1667. UpdateUsedRegs(UsedRegs, Tai(p.next));
  1668. Case p.Typ Of
  1669. Ait_Instruction:
  1670. Begin
  1671. Case Taicpu(p).opcode Of
  1672. {$ifdef USECMOV}
  1673. A_Jcc:
  1674. if (aktspecificoptprocessor=ClassP6) then
  1675. begin
  1676. { check for
  1677. jCC xxx
  1678. <several movs>
  1679. xxx:
  1680. }
  1681. l:=0;
  1682. GetNextInstruction(p, hp1);
  1683. while assigned(hp1) And
  1684. CanBeCMOV(hp1) do
  1685. begin
  1686. inc(l);
  1687. GetNextInstruction(hp1,hp1);
  1688. end;
  1689. if assigned(hp1) then
  1690. begin
  1691. if FindLabel(tasmlabel(Taicpu(p).oper[0].sym),hp1) then
  1692. begin
  1693. if (l<=4) and (l>0) then
  1694. begin
  1695. condition:=inverse_cond[Taicpu(p).condition];
  1696. GetNextInstruction(p,hp1);
  1697. asml.remove(p);
  1698. p.free;
  1699. p:=hp1;
  1700. repeat
  1701. Taicpu(hp1).opcode:=A_CMOVcc;
  1702. Taicpu(hp1).condition:=condition;
  1703. GetNextInstruction(hp1,hp1);
  1704. until not(assigned(hp1)) or
  1705. not(CanBeCMOV(hp1));
  1706. asml.remove(hp1);
  1707. hp1.free;
  1708. continue;
  1709. end;
  1710. end
  1711. else
  1712. begin
  1713. { check further for
  1714. jCC xxx
  1715. <several movs>
  1716. jmp yyy
  1717. xxx:
  1718. <several movs>
  1719. yyy:
  1720. }
  1721. { hp2 points to jmp xxx }
  1722. hp2:=hp1;
  1723. { skip hp1 to xxx }
  1724. GetNextInstruction(hp1, hp1);
  1725. if assigned(hp2) and
  1726. assigned(hp1) and
  1727. (l<=3) and
  1728. (hp2.typ=ait_instruction) and
  1729. (Taicpu(hp2).is_jmp) and
  1730. (Taicpu(hp2).condition=C_None) and
  1731. FindLabel(tasmlabel(Taicpu(p).oper[0].sym),hp1) then
  1732. begin
  1733. l:=0;
  1734. while assigned(hp1) And
  1735. CanBeCMOV(hp1) do
  1736. begin
  1737. inc(l);
  1738. GetNextInstruction(hp1, hp1);
  1739. end;
  1740. end;
  1741. {
  1742. if assigned(hp1) and
  1743. FindLabel(tasmlabel(Taicpu(hp2).oper[0].sym),hp1) then
  1744. begin
  1745. condition:=inverse_cond[Taicpu(p).condition];
  1746. GetNextInstruction(p,hp1);
  1747. asml.remove(p);
  1748. p.free;
  1749. p:=hp1;
  1750. repeat
  1751. Taicpu(hp1).opcode:=A_CMOVcc;
  1752. Taicpu(hp1).condition:=condition;
  1753. GetNextInstruction(hp1,hp1);
  1754. until not(assigned(hp1)) or
  1755. not(CanBeCMOV(hp1));
  1756. hp2:=hp1.next;
  1757. condition:=inverse_cond[condition];
  1758. asml.remove(hp1.next)
  1759. hp1.next.free;
  1760. asml.remove(hp1);
  1761. hp1.free;
  1762. continue;
  1763. end;
  1764. }
  1765. end;
  1766. end;
  1767. end;
  1768. {$endif USECMOV}
  1769. A_FSTP,A_FISTP:
  1770. if doFpuLoadStoreOpt(asmL,p) then
  1771. continue;
  1772. A_IMUL:
  1773. begin
  1774. if ((Taicpu(p).oper[0].typ = top_const) or
  1775. (Taicpu(p).oper[0].typ = top_symbol)) and
  1776. (Taicpu(p).oper[1].typ = top_reg) and
  1777. ((Taicpu(p).oper[2].typ = top_none) or
  1778. ((Taicpu(p).oper[2].typ = top_reg) and
  1779. (Taicpu(p).oper[2].reg.enum = Taicpu(p).oper[1].reg.enum))) and
  1780. getLastInstruction(p,hp1) and
  1781. (hp1.typ = ait_instruction) and
  1782. (Taicpu(hp1).opcode = A_MOV) and
  1783. (Taicpu(hp1).oper[0].typ = top_reg) and
  1784. (Taicpu(hp1).oper[1].typ = top_reg) and
  1785. (Taicpu(hp1).oper[1].reg.enum = Taicpu(p).oper[1].reg.enum) then
  1786. { change "mov reg1,reg2; imul y,reg2" to "imul y,reg1,reg2" }
  1787. begin
  1788. Taicpu(p).ops := 3;
  1789. Taicpu(p).loadreg(1,Taicpu(hp1).oper[0].reg);
  1790. Taicpu(p).loadreg(2,Taicpu(hp1).oper[1].reg);
  1791. asml.remove(hp1);
  1792. hp1.free;
  1793. end;
  1794. end;
  1795. A_MOV:
  1796. Begin
  1797. If (Taicpu(p).oper[0].typ = top_reg) And
  1798. (Taicpu(p).oper[1].typ = top_reg) And
  1799. GetNextInstruction(p, hp1) And
  1800. (hp1.typ = ait_Instruction) And
  1801. ((Taicpu(hp1).opcode = A_MOV) or
  1802. (Taicpu(hp1).opcode = A_MOVZX) or
  1803. (Taicpu(hp1).opcode = A_MOVSX)) And
  1804. (Taicpu(hp1).oper[0].typ = top_ref) And
  1805. (Taicpu(hp1).oper[1].typ = top_reg) And
  1806. ((Taicpu(hp1).oper[0].ref^.Base.enum = Taicpu(p).oper[1].reg.enum) Or
  1807. (Taicpu(hp1).oper[0].ref^.Index.enum = Taicpu(p).oper[1].reg.enum)) And
  1808. (Reg32(Taicpu(hp1).oper[1].reg).enum = Taicpu(p).oper[1].reg.enum) Then
  1809. {mov reg1, reg2
  1810. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  1811. Begin
  1812. If (Taicpu(hp1).oper[0].ref^.Base.enum = Taicpu(p).oper[1].reg.enum) Then
  1813. Taicpu(hp1).oper[0].ref^.Base.enum := Taicpu(p).oper[0].reg.enum;
  1814. If (Taicpu(hp1).oper[0].ref^.Index.enum = Taicpu(p).oper[1].reg.enum) Then
  1815. Taicpu(hp1).oper[0].ref^.Index.enum := Taicpu(p).oper[0].reg.enum;
  1816. asml.Remove(p);
  1817. p.free;
  1818. p := hp1;
  1819. Continue;
  1820. End
  1821. Else If (Taicpu(p).oper[0].typ = top_ref) And
  1822. GetNextInstruction(p,hp1) And
  1823. (hp1.typ = ait_instruction) And
  1824. IsFoldableArithOp(Taicpu(hp1),Taicpu(p).oper[1].reg.enum) And
  1825. GetNextInstruction(hp1,hp2) And
  1826. (hp2.typ = ait_instruction) And
  1827. (Taicpu(hp2).opcode = A_MOV) And
  1828. (Taicpu(hp2).oper[0].typ = top_reg) And
  1829. (Taicpu(hp2).oper[0].reg.enum = Taicpu(p).oper[1].reg.enum) And
  1830. (Taicpu(hp2).oper[1].typ = top_ref) Then
  1831. Begin
  1832. TmpUsedRegs := UsedRegs;
  1833. UpdateUsedRegs(TmpUsedRegs,Tai(hp1.next));
  1834. If (RefsEqual(Taicpu(hp2).oper[1].ref^, Taicpu(p).oper[0].ref^) And
  1835. Not(RegUsedAfterInstruction(Taicpu(p).oper[1].reg,
  1836. hp2, TmpUsedRegs)))
  1837. Then
  1838. { change mov (ref), reg }
  1839. { add/sub/or/... reg2/$const, reg }
  1840. { mov reg, (ref) }
  1841. { # release reg }
  1842. { to add/sub/or/... reg2/$const, (ref) }
  1843. Begin
  1844. case Taicpu(hp1).opcode of
  1845. A_INC,A_DEC:
  1846. Taicpu(hp1).LoadRef(0,Taicpu(p).oper[0].ref^)
  1847. else
  1848. Taicpu(hp1).LoadRef(1,Taicpu(p).oper[0].ref^);
  1849. end;
  1850. asml.Remove(p);
  1851. asml.Remove(hp2);
  1852. p.free;
  1853. hp2.free;
  1854. p := hp1
  1855. End;
  1856. End
  1857. End;
  1858. End;
  1859. End;
  1860. End;
  1861. p := Tai(p.next)
  1862. End;
  1863. End;
  1864. Procedure PostPeepHoleOpts(AsmL: TAAsmOutput; BlockStart, BlockEnd: Tai);
  1865. var
  1866. p,hp1,hp2: Tai;
  1867. Begin
  1868. P := BlockStart;
  1869. While (P <> BlockEnd) Do
  1870. Begin
  1871. Case p.Typ Of
  1872. Ait_Instruction:
  1873. Begin
  1874. Case Taicpu(p).opcode Of
  1875. A_CALL:
  1876. If (AktOptProcessor < ClassP6) And
  1877. GetNextInstruction(p, hp1) And
  1878. (hp1.typ = ait_instruction) And
  1879. (Taicpu(hp1).opcode = A_JMP) And
  1880. (Taicpu(hp1).oper[0].typ = top_symbol) Then
  1881. Begin
  1882. hp2 := Taicpu.Op_sym(A_PUSH,S_L,Taicpu(hp1).oper[0].sym);
  1883. InsertLLItem(AsmL, p.previous, p, hp2);
  1884. Taicpu(p).opcode := A_JMP;
  1885. Taicpu(p).is_jmp := true;
  1886. asml.Remove(hp1);
  1887. hp1.free;
  1888. End;
  1889. A_CMP:
  1890. Begin
  1891. if (Taicpu(p).oper[0].typ = top_const) and
  1892. (Taicpu(p).oper[0].val = 0) and
  1893. (Taicpu(p).oper[1].typ = top_reg) then
  1894. {change "cmp $0, %reg" to "test %reg, %reg"}
  1895. begin
  1896. Taicpu(p).opcode := A_TEST;
  1897. Taicpu(p).loadreg(0,Taicpu(p).oper[1].reg);
  1898. continue;
  1899. end;
  1900. End;
  1901. (*
  1902. Optimization is not safe; xor clears the carry flag.
  1903. See test/tgadint64 in the test suite.
  1904. A_MOV:
  1905. if (Taicpu(p).oper[0].typ = Top_Const) And
  1906. (Taicpu(p).oper[0].val = 0) And
  1907. (Taicpu(p).oper[1].typ = Top_Reg) Then
  1908. { change "mov $0, %reg" into "xor %reg, %reg" }
  1909. Begin
  1910. Taicpu(p).opcode := A_XOR;
  1911. Taicpu(p).LoadReg(0,Taicpu(p).oper[1].reg);
  1912. End;
  1913. *)
  1914. A_MOVZX:
  1915. { if register vars are on, it's possible there is code like }
  1916. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  1917. { so we can't safely replace the movzx then with xor/mov, }
  1918. { since that would change the flags (JM) }
  1919. if not(cs_regalloc in aktglobalswitches) then
  1920. Begin
  1921. If (Taicpu(p).oper[1].typ = top_reg) Then
  1922. If (Taicpu(p).oper[0].typ = top_reg)
  1923. Then
  1924. Case Taicpu(p).opsize of
  1925. S_BL:
  1926. Begin
  1927. If IsGP32Reg(Taicpu(p).oper[1].reg) And
  1928. Not(CS_LittleSize in aktglobalswitches) And
  1929. (aktoptprocessor = ClassP5)
  1930. Then
  1931. {Change "movzbl %reg1, %reg2" to
  1932. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1933. PentiumMMX}
  1934. Begin
  1935. hp1 := Taicpu.op_reg_reg(A_XOR, S_L,
  1936. Taicpu(p).oper[1].reg, Taicpu(p).oper[1].reg);
  1937. InsertLLItem(AsmL,p.previous, p, hp1);
  1938. Taicpu(p).opcode := A_MOV;
  1939. Taicpu(p).changeopsize(S_B);
  1940. Taicpu(p).LoadReg(1,changeregsize(Taicpu(p).oper[1].reg,S_B));
  1941. End;
  1942. End;
  1943. End
  1944. Else
  1945. If (Taicpu(p).oper[0].typ = top_ref) And
  1946. (Taicpu(p).oper[0].ref^.base.enum <> Taicpu(p).oper[1].reg.enum) And
  1947. (Taicpu(p).oper[0].ref^.index.enum <> Taicpu(p).oper[1].reg.enum) And
  1948. Not(CS_LittleSize in aktglobalswitches) And
  1949. IsGP32Reg(Taicpu(p).oper[1].reg) And
  1950. (aktoptprocessor = ClassP5) And
  1951. (Taicpu(p).opsize = S_BL)
  1952. Then
  1953. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1954. Pentium and PentiumMMX}
  1955. Begin
  1956. hp1 := Taicpu.Op_reg_reg(A_XOR, S_L, Taicpu(p).oper[1].reg,
  1957. Taicpu(p).oper[1].reg);
  1958. Taicpu(p).opcode := A_MOV;
  1959. Taicpu(p).changeopsize(S_B);
  1960. Taicpu(p).LoadReg(1,changeregsize(Taicpu(p).oper[1].reg,S_B));
  1961. InsertLLItem(AsmL,p.previous, p, hp1);
  1962. End;
  1963. End;
  1964. A_TEST, A_OR:
  1965. {removes the line marked with (x) from the sequence
  1966. And/or/xor/add/sub/... $x, %y
  1967. test/or %y, %y (x)
  1968. j(n)z _Label
  1969. as the first instruction already adjusts the ZF}
  1970. Begin
  1971. If OpsEqual(Taicpu(p).oper[0],Taicpu(p).oper[1]) Then
  1972. If GetLastInstruction(p, hp1) And
  1973. (Tai(hp1).typ = ait_instruction) Then
  1974. Case Taicpu(hp1).opcode Of
  1975. A_ADD, A_SUB, A_OR, A_XOR, A_AND{, A_SHL, A_SHR}:
  1976. Begin
  1977. If OpsEqual(Taicpu(hp1).oper[1],Taicpu(p).oper[0]) Then
  1978. Begin
  1979. hp1 := Tai(p.next);
  1980. asml.remove(p);
  1981. p.free;
  1982. p := Tai(hp1);
  1983. continue
  1984. End;
  1985. End;
  1986. A_DEC, A_INC, A_NEG:
  1987. Begin
  1988. If OpsEqual(Taicpu(hp1).oper[0],Taicpu(p).oper[0]) Then
  1989. Begin
  1990. Case Taicpu(hp1).opcode Of
  1991. A_DEC, A_INC:
  1992. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  1993. Begin
  1994. Case Taicpu(hp1).opcode Of
  1995. A_DEC: Taicpu(hp1).opcode := A_SUB;
  1996. A_INC: Taicpu(hp1).opcode := A_ADD;
  1997. End;
  1998. Taicpu(hp1).Loadoper(1,Taicpu(hp1).oper[0]);
  1999. Taicpu(hp1).LoadConst(0,1);
  2000. Taicpu(hp1).ops:=2;
  2001. End
  2002. End;
  2003. hp1 := Tai(p.next);
  2004. asml.remove(p);
  2005. p.free;
  2006. p := Tai(hp1);
  2007. continue
  2008. End;
  2009. End
  2010. End
  2011. End;
  2012. End;
  2013. End;
  2014. End;
  2015. p := Tai(p.next)
  2016. End;
  2017. End;
  2018. End.
  2019. {
  2020. $Log$
  2021. Revision 1.46 2003-06-03 21:09:05 peter
  2022. * internal changeregsize for optimizer
  2023. * fix with a hack to not remove the first instruction of a block
  2024. which will leave blockstart pointing to invalid memory
  2025. Revision 1.45 2003/06/02 21:42:05 jonas
  2026. * function results can now also be regvars
  2027. - removed tprocinfo.return_offset, never use it again since it's invalid
  2028. if the result is a regvar
  2029. Revision 1.44 2003/05/30 23:57:08 peter
  2030. * more sparc cleanup
  2031. * accumulator removed, splitted in function_return_reg (called) and
  2032. function_result_reg (caller)
  2033. Revision 1.43 2003/04/27 11:21:35 peter
  2034. * aktprocdef renamed to current_procdef
  2035. * procinfo renamed to current_procinfo
  2036. * procinfo will now be stored in current_module so it can be
  2037. cleaned up properly
  2038. * gen_main_procsym changed to create_main_proc and release_main_proc
  2039. to also generate a tprocinfo structure
  2040. * fixed unit implicit initfinal
  2041. Revision 1.42 2003/03/28 19:16:57 peter
  2042. * generic constructor working for i386
  2043. * remove fixed self register
  2044. * esi added as address register for i386
  2045. Revision 1.41 2003/02/26 13:24:59 daniel
  2046. * Disabled mov reg,0 -> xor reg,reg optimization
  2047. Revision 1.40 2003/02/25 07:41:54 daniel
  2048. * Properly fixed reversed operands bug
  2049. Revision 1.39 2003/02/24 21:27:01 daniel
  2050. * Reversed operand order in an optimization in postpeepholeopt
  2051. Revision 1.38 2003/02/19 22:39:56 daniel
  2052. * Fixed a few issues
  2053. Revision 1.37 2003/02/19 22:00:16 daniel
  2054. * Code generator converted to new register notation
  2055. - Horribily outdated todo.txt removed
  2056. Revision 1.36 2003/01/08 18:43:57 daniel
  2057. * Tregister changed into a record
  2058. Revision 1.35 2002/11/15 16:30:54 peter
  2059. * made tasmsymbol.refs private (merged)
  2060. Revision 1.34 2002/08/18 20:06:30 peter
  2061. * inlining is now also allowed in interface
  2062. * renamed write/load to ppuwrite/ppuload
  2063. * tnode storing in ppu
  2064. * nld,ncon,nbas are already updated for storing in ppu
  2065. Revision 1.33 2002/08/17 09:23:46 florian
  2066. * first part of procinfo rewrite
  2067. Revision 1.32 2002/08/11 14:32:30 peter
  2068. * renamed current_library to objectlibrary
  2069. Revision 1.31 2002/08/11 13:24:17 peter
  2070. * saving of asmsymbols in ppu supported
  2071. * asmsymbollist global is removed and moved into a new class
  2072. tasmlibrarydata that will hold the info of a .a file which
  2073. corresponds with a single module. Added librarydata to tmodule
  2074. to keep the library info stored for the module. In the future the
  2075. objectfiles will also be stored to the tasmlibrarydata class
  2076. * all getlabel/newasmsymbol and friends are moved to the new class
  2077. Revision 1.30 2002/07/26 21:15:43 florian
  2078. * rewrote the system handling
  2079. Revision 1.29 2002/07/01 18:46:34 peter
  2080. * internal linker
  2081. * reorganized aasm layer
  2082. Revision 1.28 2002/06/09 12:55:23 jonas
  2083. * fixed detection of register usage
  2084. Revision 1.27 2002/05/18 13:34:25 peter
  2085. * readded missing revisions
  2086. Revision 1.26 2002/05/16 19:46:52 carl
  2087. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2088. + try to fix temp allocation (still in ifdef)
  2089. + generic constructor calls
  2090. + start of tassembler / tmodulebase class cleanup
  2091. Revision 1.24 2002/05/12 16:53:18 peter
  2092. * moved entry and exitcode to ncgutil and cgobj
  2093. * foreach gets extra argument for passing local data to the
  2094. iterator function
  2095. * -CR checks also class typecasts at runtime by changing them
  2096. into as
  2097. * fixed compiler to cycle with the -CR option
  2098. * fixed stabs with elf writer, finally the global variables can
  2099. be watched
  2100. * removed a lot of routines from cga unit and replaced them by
  2101. calls to cgobj
  2102. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2103. u32bit then the other is typecasted also to u32bit without giving
  2104. a rangecheck warning/error.
  2105. * fixed pascal calling method with reversing also the high tree in
  2106. the parast, detected by tcalcst3 test
  2107. Revision 1.23 2002/04/21 15:40:49 carl
  2108. * changeregsize -> changeregsize
  2109. Revision 1.22 2002/04/20 21:37:07 carl
  2110. + generic FPC_CHECKPOINTER
  2111. + first parameter offset in stack now portable
  2112. * rename some constants
  2113. + move some cpu stuff to other units
  2114. - remove unused constents
  2115. * fix stacksize for some targets
  2116. * fix generic size problems which depend now on EXTEND_SIZE constant
  2117. * removing frame pointer in routines is only available for : i386,m68k and vis targets
  2118. Revision 1.21 2002/04/15 19:44:21 peter
  2119. * fixed stackcheck that would be called recursively when a stack
  2120. error was found
  2121. * generic changeregsize(reg,size) for i386 register resizing
  2122. * removed some more routines from cga unit
  2123. * fixed returnvalue handling
  2124. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  2125. Revision 1.20 2002/04/02 20:30:16 jonas
  2126. + support for folding inc/dec in shl/add/sub sequences toa single lea
  2127. instruction
  2128. Revision 1.19 2002/04/02 13:01:58 jonas
  2129. * fixed nasty bug in "and" peepholeoptimization that caused wrong
  2130. optimizations after Peter's big location patch
  2131. Revision 1.18 2002/03/31 20:26:40 jonas
  2132. + a_loadfpu_* and a_loadmm_* methods in tcg
  2133. * register allocation is now handled by a class and is mostly processor
  2134. independent (+rgobj.pas and i386/rgcpu.pas)
  2135. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2136. * some small improvements and fixes to the optimizer
  2137. * some register allocation fixes
  2138. * some fpuvaroffset fixes in the unary minus node
  2139. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2140. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2141. also better optimizable)
  2142. * fixed and optimized register saving/restoring for new/dispose nodes
  2143. * LOC_FPU locations now also require their "register" field to be set to
  2144. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2145. - list field removed of the tnode class because it's not used currently
  2146. and can cause hard-to-find bugs
  2147. }