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