aoptcpu.pas 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Jonas Maebe
  3. This unit contains the peephole optimizer for i386
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aoptcpu;
  18. {$i fpcdefs.inc}
  19. { $define DEBUG_AOPTCPU}
  20. Interface
  21. uses
  22. cgbase,
  23. cpubase, aoptobj, aoptcpub, aopt, aoptx86,
  24. Aasmbase,aasmtai,aasmdata;
  25. Type
  26. TCpuAsmOptimizer = class(TX86AsmOptimizer)
  27. procedure Optimize; override;
  28. procedure PrePeepHoleOpts; override;
  29. procedure PeepHoleOptPass1; override;
  30. procedure PeepHoleOptPass2; override;
  31. procedure PostPeepHoleOpts; override;
  32. function DoFpuLoadStoreOpt(var p : tai) : boolean;
  33. function RegReadByInstruction(reg : TRegister; hp : tai) : boolean;
  34. function InstructionLoadsFromReg(const reg : TRegister;const hp : tai) : boolean;override;
  35. end;
  36. Var
  37. AsmOptimizer : TCpuAsmOptimizer;
  38. Implementation
  39. uses
  40. verbose,globtype,globals,
  41. cutils,
  42. aoptbase,
  43. cpuinfo,
  44. aasmcpu,
  45. procinfo,
  46. cgutils,cgx86,
  47. { units we should get rid off: }
  48. symsym,symconst;
  49. function TCPUAsmoptimizer.DoFpuLoadStoreOpt(var p: tai): boolean;
  50. { returns true if a "continue" should be done after this optimization }
  51. var hp1, hp2: tai;
  52. begin
  53. DoFpuLoadStoreOpt := false;
  54. if (taicpu(p).oper[0]^.typ = top_ref) and
  55. getNextInstruction(p, hp1) and
  56. (hp1.typ = ait_instruction) and
  57. (((taicpu(hp1).opcode = A_FLD) and
  58. (taicpu(p).opcode = A_FSTP)) or
  59. ((taicpu(p).opcode = A_FISTP) and
  60. (taicpu(hp1).opcode = A_FILD))) and
  61. (taicpu(hp1).oper[0]^.typ = top_ref) and
  62. (taicpu(hp1).opsize = taicpu(p).opsize) and
  63. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  64. begin
  65. { replacing fstp f;fld f by fst f is only valid for extended because of rounding }
  66. if (taicpu(p).opsize=S_FX) and
  67. getNextInstruction(hp1, hp2) and
  68. (hp2.typ = ait_instruction) and
  69. IsExitCode(hp2) and
  70. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  71. not(assigned(current_procinfo.procdef.funcretsym) and
  72. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  73. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  74. begin
  75. asml.remove(p);
  76. asml.remove(hp1);
  77. p.free;
  78. hp1.free;
  79. p := hp2;
  80. removeLastDeallocForFuncRes(p);
  81. doFPULoadStoreOpt := true;
  82. end
  83. (* can't be done because the store operation rounds
  84. else
  85. { fst can't store an extended value! }
  86. if (taicpu(p).opsize <> S_FX) and
  87. (taicpu(p).opsize <> S_IQ) then
  88. begin
  89. if (taicpu(p).opcode = A_FSTP) then
  90. taicpu(p).opcode := A_FST
  91. else taicpu(p).opcode := A_FIST;
  92. asml.remove(hp1);
  93. hp1.free;
  94. end
  95. *)
  96. end;
  97. end;
  98. { converts a TChange variable to a TRegister }
  99. function tch2reg(ch: tinschange): tsuperregister;
  100. const
  101. ch2reg: array[CH_REAX..CH_REDI] of tsuperregister = (RS_EAX,RS_ECX,RS_EDX,RS_EBX,RS_ESP,RS_EBP,RS_ESI,RS_EDI);
  102. begin
  103. if (ch <= CH_REDI) then
  104. tch2reg := ch2reg[ch]
  105. else if (ch <= CH_WEDI) then
  106. tch2reg := ch2reg[tinschange(ord(ch) - ord(CH_REDI))]
  107. else if (ch <= CH_RWEDI) then
  108. tch2reg := ch2reg[tinschange(ord(ch) - ord(CH_WEDI))]
  109. else if (ch <= CH_MEDI) then
  110. tch2reg := ch2reg[tinschange(ord(ch) - ord(CH_RWEDI))]
  111. else
  112. InternalError(2016041901)
  113. end;
  114. { Checks if the register is a 32 bit general purpose register }
  115. function isgp32reg(reg: TRegister): boolean;
  116. begin
  117. {$push}{$warnings off}
  118. isgp32reg:=(getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)>=RS_EAX) and (getsupreg(reg)<=RS_EBX);
  119. {$pop}
  120. end;
  121. function TCpuAsmOptimizer.InstructionLoadsFromReg(const reg: TRegister;const hp: tai): boolean;
  122. begin
  123. Result:=RegReadByInstruction(reg,hp);
  124. end;
  125. function TCpuAsmOptimizer.RegReadByInstruction(reg: TRegister; hp: tai): boolean;
  126. var
  127. p: taicpu;
  128. opcount: longint;
  129. begin
  130. RegReadByInstruction := false;
  131. if hp.typ <> ait_instruction then
  132. exit;
  133. p := taicpu(hp);
  134. case p.opcode of
  135. A_CALL:
  136. regreadbyinstruction := true;
  137. A_IMUL:
  138. case p.ops of
  139. 1:
  140. regReadByInstruction :=
  141. (reg = NR_EAX) or RegInOp(reg,p.oper[0]^);
  142. 2,3:
  143. regReadByInstruction :=
  144. reginop(reg,p.oper[0]^) or
  145. reginop(reg,p.oper[1]^);
  146. end;
  147. A_IDIV,A_DIV,A_MUL:
  148. begin
  149. regReadByInstruction :=
  150. RegInOp(reg,p.oper[0]^) or (getsupreg(reg) in [RS_EAX,RS_EDX]);
  151. end;
  152. else
  153. begin
  154. for opcount := 0 to p.ops-1 do
  155. if (p.oper[opCount]^.typ = top_ref) and
  156. RegInRef(reg,p.oper[opcount]^.ref^) then
  157. begin
  158. RegReadByInstruction := true;
  159. exit
  160. end;
  161. for opcount := 1 to maxinschanges do
  162. case insprop[p.opcode].ch[opcount] of
  163. CH_REAX..CH_REDI,CH_RWEAX..CH_MEDI:
  164. if getsupreg(reg) = tch2reg(insprop[p.opcode].ch[opcount]) then
  165. begin
  166. RegReadByInstruction := true;
  167. exit
  168. end;
  169. CH_RWOP1,CH_ROP1,CH_MOP1:
  170. if reginop(reg,p.oper[0]^) then
  171. begin
  172. RegReadByInstruction := true;
  173. exit
  174. end;
  175. Ch_RWOP2,Ch_ROP2,Ch_MOP2:
  176. if reginop(reg,p.oper[1]^) then
  177. begin
  178. RegReadByInstruction := true;
  179. exit
  180. end;
  181. Ch_RWOP3,Ch_ROP3,Ch_MOP3:
  182. if reginop(reg,p.oper[2]^) then
  183. begin
  184. RegReadByInstruction := true;
  185. exit
  186. end;
  187. Ch_RFlags,Ch_RWFlags:
  188. if reg=NR_DEFAULTFLAGS then
  189. begin
  190. RegReadByInstruction := true;
  191. exit
  192. end;
  193. end;
  194. end;
  195. end;
  196. end;
  197. { returns true if p contains a memory operand with a segment set }
  198. function InsContainsSegRef(p: taicpu): boolean;
  199. var
  200. i: longint;
  201. begin
  202. result:=true;
  203. for i:=0 to p.opercnt-1 do
  204. if (p.oper[i]^.typ=top_ref) and
  205. (p.oper[i]^.ref^.segment<>NR_NO) then
  206. exit;
  207. result:=false;
  208. end;
  209. function InstrReadsFlags(p: tai): boolean;
  210. var
  211. l: longint;
  212. begin
  213. InstrReadsFlags := true;
  214. case p.typ of
  215. ait_instruction:
  216. begin
  217. for l := 1 to maxinschanges do
  218. if InsProp[taicpu(p).opcode].Ch[l] in [Ch_RFlags,Ch_RWFlags,Ch_All] then
  219. exit;
  220. end;
  221. ait_label:
  222. exit;
  223. end;
  224. InstrReadsFlags := false;
  225. end;
  226. procedure TCPUAsmOptimizer.PrePeepHoleOpts;
  227. var
  228. p,hp1: tai;
  229. l: aint;
  230. tmpRef: treference;
  231. begin
  232. p := BlockStart;
  233. while (p <> BlockEnd) Do
  234. begin
  235. case p.Typ Of
  236. Ait_Instruction:
  237. begin
  238. if InsContainsSegRef(taicpu(p)) then
  239. begin
  240. p := tai(p.next);
  241. continue;
  242. end;
  243. case taicpu(p).opcode Of
  244. A_IMUL:
  245. {changes certain "imul const, %reg"'s to lea sequences}
  246. begin
  247. if (taicpu(p).oper[0]^.typ = Top_Const) and
  248. (taicpu(p).oper[1]^.typ = Top_Reg) and
  249. (taicpu(p).opsize = S_L) then
  250. if (taicpu(p).oper[0]^.val = 1) then
  251. if (taicpu(p).ops = 2) then
  252. {remove "imul $1, reg"}
  253. begin
  254. hp1 := tai(p.Next);
  255. asml.remove(p);
  256. p.free;
  257. p := hp1;
  258. continue;
  259. end
  260. else
  261. {change "imul $1, reg1, reg2" to "mov reg1, reg2"}
  262. begin
  263. hp1 := taicpu.Op_Reg_Reg(A_MOV, S_L, taicpu(p).oper[1]^.reg,taicpu(p).oper[2]^.reg);
  264. InsertLLItem(p.previous, p.next, hp1);
  265. p.free;
  266. p := hp1;
  267. end
  268. else if
  269. ((taicpu(p).ops <= 2) or
  270. (taicpu(p).oper[2]^.typ = Top_Reg)) and
  271. (taicpu(p).oper[0]^.val <= 12) and
  272. not(cs_opt_size in current_settings.optimizerswitches) and
  273. (not(GetNextInstruction(p, hp1)) or
  274. {GetNextInstruction(p, hp1) and}
  275. not((tai(hp1).typ = ait_instruction) and
  276. ((taicpu(hp1).opcode=A_Jcc) and
  277. (taicpu(hp1).condition in [C_O,C_NO])))) then
  278. begin
  279. reference_reset(tmpref,1);
  280. case taicpu(p).oper[0]^.val Of
  281. 3: begin
  282. {imul 3, reg1, reg2 to
  283. lea (reg1,reg1,2), reg2
  284. imul 3, reg1 to
  285. lea (reg1,reg1,2), reg1}
  286. TmpRef.base := taicpu(p).oper[1]^.reg;
  287. TmpRef.index := taicpu(p).oper[1]^.reg;
  288. TmpRef.ScaleFactor := 2;
  289. if (taicpu(p).ops = 2) then
  290. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg)
  291. else
  292. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  293. InsertLLItem(p.previous, p.next, hp1);
  294. p.free;
  295. p := hp1;
  296. end;
  297. 5: begin
  298. {imul 5, reg1, reg2 to
  299. lea (reg1,reg1,4), reg2
  300. imul 5, reg1 to
  301. lea (reg1,reg1,4), reg1}
  302. TmpRef.base := taicpu(p).oper[1]^.reg;
  303. TmpRef.index := taicpu(p).oper[1]^.reg;
  304. TmpRef.ScaleFactor := 4;
  305. if (taicpu(p).ops = 2) then
  306. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg)
  307. else
  308. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  309. InsertLLItem(p.previous, p.next, hp1);
  310. p.free;
  311. p := hp1;
  312. end;
  313. 6: begin
  314. {imul 6, reg1, reg2 to
  315. lea (,reg1,2), reg2
  316. lea (reg2,reg1,4), reg2
  317. imul 6, reg1 to
  318. lea (reg1,reg1,2), reg1
  319. add reg1, reg1}
  320. if (current_settings.optimizecputype <= cpu_386) then
  321. begin
  322. TmpRef.index := taicpu(p).oper[1]^.reg;
  323. if (taicpu(p).ops = 3) then
  324. begin
  325. TmpRef.base := taicpu(p).oper[2]^.reg;
  326. TmpRef.ScaleFactor := 4;
  327. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  328. end
  329. else
  330. begin
  331. hp1 := taicpu.op_reg_reg(A_ADD, S_L,
  332. taicpu(p).oper[1]^.reg,taicpu(p).oper[1]^.reg);
  333. end;
  334. InsertLLItem(p, p.next, hp1);
  335. reference_reset(tmpref,2);
  336. TmpRef.index := taicpu(p).oper[1]^.reg;
  337. TmpRef.ScaleFactor := 2;
  338. if (taicpu(p).ops = 3) then
  339. begin
  340. TmpRef.base := NR_NO;
  341. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef,
  342. taicpu(p).oper[2]^.reg);
  343. end
  344. else
  345. begin
  346. TmpRef.base := taicpu(p).oper[1]^.reg;
  347. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  348. end;
  349. InsertLLItem(p.previous, p.next, hp1);
  350. p.free;
  351. p := tai(hp1.next);
  352. end
  353. end;
  354. 9: begin
  355. {imul 9, reg1, reg2 to
  356. lea (reg1,reg1,8), reg2
  357. imul 9, reg1 to
  358. lea (reg1,reg1,8), reg1}
  359. TmpRef.base := taicpu(p).oper[1]^.reg;
  360. TmpRef.index := taicpu(p).oper[1]^.reg;
  361. TmpRef.ScaleFactor := 8;
  362. if (taicpu(p).ops = 2) then
  363. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg)
  364. else
  365. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  366. InsertLLItem(p.previous, p.next, hp1);
  367. p.free;
  368. p := hp1;
  369. end;
  370. 10: begin
  371. {imul 10, reg1, reg2 to
  372. lea (reg1,reg1,4), reg2
  373. add reg2, reg2
  374. imul 10, reg1 to
  375. lea (reg1,reg1,4), reg1
  376. add reg1, reg1}
  377. if (current_settings.optimizecputype <= cpu_386) then
  378. begin
  379. if (taicpu(p).ops = 3) then
  380. hp1 := taicpu.op_reg_reg(A_ADD, S_L,
  381. taicpu(p).oper[2]^.reg,taicpu(p).oper[2]^.reg)
  382. else
  383. hp1 := taicpu.op_reg_reg(A_ADD, S_L,
  384. taicpu(p).oper[1]^.reg,taicpu(p).oper[1]^.reg);
  385. InsertLLItem(p, p.next, hp1);
  386. TmpRef.base := taicpu(p).oper[1]^.reg;
  387. TmpRef.index := taicpu(p).oper[1]^.reg;
  388. TmpRef.ScaleFactor := 4;
  389. if (taicpu(p).ops = 3) then
  390. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg)
  391. else
  392. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  393. InsertLLItem(p.previous, p.next, hp1);
  394. p.free;
  395. p := tai(hp1.next);
  396. end
  397. end;
  398. 12: begin
  399. {imul 12, reg1, reg2 to
  400. lea (,reg1,4), reg2
  401. lea (reg2,reg1,8), reg2
  402. imul 12, reg1 to
  403. lea (reg1,reg1,2), reg1
  404. lea (,reg1,4), reg1}
  405. if (current_settings.optimizecputype <= cpu_386)
  406. then
  407. begin
  408. TmpRef.index := taicpu(p).oper[1]^.reg;
  409. if (taicpu(p).ops = 3) then
  410. begin
  411. TmpRef.base := taicpu(p).oper[2]^.reg;
  412. TmpRef.ScaleFactor := 8;
  413. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  414. end
  415. else
  416. begin
  417. TmpRef.base := NR_NO;
  418. TmpRef.ScaleFactor := 4;
  419. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  420. end;
  421. InsertLLItem(p, p.next, hp1);
  422. reference_reset(tmpref,2);
  423. TmpRef.index := taicpu(p).oper[1]^.reg;
  424. if (taicpu(p).ops = 3) then
  425. begin
  426. TmpRef.base := NR_NO;
  427. TmpRef.ScaleFactor := 4;
  428. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  429. end
  430. else
  431. begin
  432. TmpRef.base := taicpu(p).oper[1]^.reg;
  433. TmpRef.ScaleFactor := 2;
  434. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  435. end;
  436. InsertLLItem(p.previous, p.next, hp1);
  437. p.free;
  438. p := tai(hp1.next);
  439. end
  440. end
  441. end;
  442. end;
  443. end;
  444. A_SAR, A_SHR:
  445. {changes the code sequence
  446. shr/sar const1, x
  447. shl const2, x
  448. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  449. begin
  450. if GetNextInstruction(p, hp1) and
  451. (tai(hp1).typ = ait_instruction) and
  452. (taicpu(hp1).opcode = A_SHL) and
  453. (taicpu(p).oper[0]^.typ = top_const) and
  454. (taicpu(hp1).oper[0]^.typ = top_const) and
  455. (taicpu(hp1).opsize = taicpu(p).opsize) and
  456. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[1]^.typ) and
  457. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) then
  458. if (taicpu(p).oper[0]^.val > taicpu(hp1).oper[0]^.val) and
  459. not(cs_opt_size in current_settings.optimizerswitches) then
  460. { shr/sar const1, %reg
  461. shl const2, %reg
  462. with const1 > const2 }
  463. begin
  464. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  465. taicpu(hp1).opcode := A_AND;
  466. l := (1 shl (taicpu(hp1).oper[0]^.val)) - 1;
  467. case taicpu(p).opsize Of
  468. S_L: taicpu(hp1).loadConst(0,l Xor aint($ffffffff));
  469. S_B: taicpu(hp1).loadConst(0,l Xor $ff);
  470. S_W: taicpu(hp1).loadConst(0,l Xor $ffff);
  471. end;
  472. end
  473. else if (taicpu(p).oper[0]^.val<taicpu(hp1).oper[0]^.val) and
  474. not(cs_opt_size in current_settings.optimizerswitches) then
  475. { shr/sar const1, %reg
  476. shl const2, %reg
  477. with const1 < const2 }
  478. begin
  479. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val-taicpu(p).oper[0]^.val);
  480. taicpu(p).opcode := A_AND;
  481. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  482. case taicpu(p).opsize Of
  483. S_L: taicpu(p).loadConst(0,l Xor aint($ffffffff));
  484. S_B: taicpu(p).loadConst(0,l Xor $ff);
  485. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  486. end;
  487. end
  488. else
  489. { shr/sar const1, %reg
  490. shl const2, %reg
  491. with const1 = const2 }
  492. if (taicpu(p).oper[0]^.val = taicpu(hp1).oper[0]^.val) then
  493. begin
  494. taicpu(p).opcode := A_AND;
  495. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  496. case taicpu(p).opsize Of
  497. S_B: taicpu(p).loadConst(0,l Xor $ff);
  498. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  499. S_L: taicpu(p).loadConst(0,l Xor aint($ffffffff));
  500. end;
  501. asml.remove(hp1);
  502. hp1.free;
  503. end;
  504. end;
  505. A_XOR:
  506. if (taicpu(p).oper[0]^.typ = top_reg) and
  507. (taicpu(p).oper[1]^.typ = top_reg) and
  508. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  509. { temporarily change this to 'mov reg,0' to make it easier }
  510. { for the CSE. Will be changed back in pass 2 }
  511. begin
  512. taicpu(p).opcode := A_MOV;
  513. taicpu(p).loadConst(0,0);
  514. end;
  515. end;
  516. end;
  517. end;
  518. p := tai(p.next)
  519. end;
  520. end;
  521. { skips all labels and returns the next "real" instruction }
  522. function SkipLabels(hp: tai; var hp2: tai): boolean;
  523. begin
  524. while assigned(hp.next) and
  525. (tai(hp.next).typ in SkipInstr + [ait_label,ait_align]) Do
  526. hp := tai(hp.next);
  527. if assigned(hp.next) then
  528. begin
  529. SkipLabels := True;
  530. hp2 := tai(hp.next)
  531. end
  532. else
  533. begin
  534. hp2 := hp;
  535. SkipLabels := False
  536. end;
  537. end;
  538. { First pass of peephole optimizations }
  539. procedure TCPUAsmOPtimizer.PeepHoleOptPass1;
  540. function WriteOk : Boolean;
  541. begin
  542. writeln('Ok');
  543. Result:=True;
  544. end;
  545. var
  546. l : longint;
  547. p,hp1,hp2 : tai;
  548. hp3,hp4: tai;
  549. v:aint;
  550. TmpRef: TReference;
  551. TmpUsedRegs: TAllUsedRegs;
  552. TmpBool1, TmpBool2: Boolean;
  553. function GetFinalDestination(asml: TAsmList; hp: taicpu; level: longint): boolean;
  554. {traces sucessive jumps to their final destination and sets it, e.g.
  555. je l1 je l3
  556. <code> <code>
  557. l1: becomes l1:
  558. je l2 je l3
  559. <code> <code>
  560. l2: l2:
  561. jmp l3 jmp l3
  562. the level parameter denotes how deeep we have already followed the jump,
  563. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  564. var p1, p2: tai;
  565. l: tasmlabel;
  566. function FindAnyLabel(hp: tai; var l: tasmlabel): Boolean;
  567. begin
  568. FindAnyLabel := false;
  569. while assigned(hp.next) and
  570. (tai(hp.next).typ in (SkipInstr+[ait_align])) Do
  571. hp := tai(hp.next);
  572. if assigned(hp.next) and
  573. (tai(hp.next).typ = ait_label) then
  574. begin
  575. FindAnyLabel := true;
  576. l := tai_label(hp.next).labsym;
  577. end
  578. end;
  579. begin
  580. GetfinalDestination := false;
  581. if level > 20 then
  582. exit;
  583. p1 := getlabelwithsym(tasmlabel(hp.oper[0]^.ref^.symbol));
  584. if assigned(p1) then
  585. begin
  586. SkipLabels(p1,p1);
  587. if (tai(p1).typ = ait_instruction) and
  588. (taicpu(p1).is_jmp) then
  589. if { the next instruction after the label where the jump hp arrives}
  590. { is unconditional or of the same type as hp, so continue }
  591. (taicpu(p1).condition in [C_None,hp.condition]) or
  592. { the next instruction after the label where the jump hp arrives}
  593. { is the opposite of hp (so this one is never taken), but after }
  594. { that one there is a branch that will be taken, so perform a }
  595. { little hack: set p1 equal to this instruction (that's what the}
  596. { last SkipLabels is for, only works with short bool evaluation)}
  597. ((taicpu(p1).condition = inverse_cond(hp.condition)) and
  598. SkipLabels(p1,p2) and
  599. (p2.typ = ait_instruction) and
  600. (taicpu(p2).is_jmp) and
  601. (taicpu(p2).condition in [C_None,hp.condition]) and
  602. SkipLabels(p1,p1)) then
  603. begin
  604. { quick check for loops of the form "l5: ; jmp l5 }
  605. if (tasmlabel(taicpu(p1).oper[0]^.ref^.symbol).labelnr =
  606. tasmlabel(hp.oper[0]^.ref^.symbol).labelnr) then
  607. exit;
  608. if not GetFinalDestination(asml, taicpu(p1),succ(level)) then
  609. exit;
  610. tasmlabel(hp.oper[0]^.ref^.symbol).decrefs;
  611. hp.oper[0]^.ref^.symbol:=taicpu(p1).oper[0]^.ref^.symbol;
  612. tasmlabel(hp.oper[0]^.ref^.symbol).increfs;
  613. end
  614. else
  615. if (taicpu(p1).condition = inverse_cond(hp.condition)) then
  616. if not FindAnyLabel(p1,l) then
  617. begin
  618. {$ifdef finaldestdebug}
  619. insertllitem(asml,p1,p1.next,tai_comment.Create(
  620. strpnew('previous label inserted'))));
  621. {$endif finaldestdebug}
  622. current_asmdata.getjumplabel(l);
  623. insertllitem(p1,p1.next,tai_label.Create(l));
  624. tasmlabel(taicpu(hp).oper[0]^.ref^.symbol).decrefs;
  625. hp.oper[0]^.ref^.symbol := l;
  626. l.increfs;
  627. { this won't work, since the new label isn't in the labeltable }
  628. { so it will fail the rangecheck. Labeltable should become a }
  629. { hashtable to support this: }
  630. { GetFinalDestination(asml, hp); }
  631. end
  632. else
  633. begin
  634. {$ifdef finaldestdebug}
  635. insertllitem(asml,p1,p1.next,tai_comment.Create(
  636. strpnew('next label reused'))));
  637. {$endif finaldestdebug}
  638. l.increfs;
  639. hp.oper[0]^.ref^.symbol := l;
  640. if not GetFinalDestination(asml, hp,succ(level)) then
  641. exit;
  642. end;
  643. end;
  644. GetFinalDestination := true;
  645. end;
  646. function DoSubAddOpt(var p: tai): Boolean;
  647. begin
  648. DoSubAddOpt := False;
  649. if GetLastInstruction(p, hp1) and
  650. (hp1.typ = ait_instruction) and
  651. (taicpu(hp1).opsize = taicpu(p).opsize) then
  652. case taicpu(hp1).opcode Of
  653. A_DEC:
  654. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  655. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  656. begin
  657. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
  658. asml.remove(hp1);
  659. hp1.free;
  660. end;
  661. A_SUB:
  662. if (taicpu(hp1).oper[0]^.typ = top_const) and
  663. (taicpu(hp1).oper[1]^.typ = top_reg) and
  664. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  665. begin
  666. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
  667. asml.remove(hp1);
  668. hp1.free;
  669. end;
  670. A_ADD:
  671. if (taicpu(hp1).oper[0]^.typ = top_const) and
  672. (taicpu(hp1).oper[1]^.typ = top_reg) and
  673. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  674. begin
  675. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  676. asml.remove(hp1);
  677. hp1.free;
  678. if (taicpu(p).oper[0]^.val = 0) then
  679. begin
  680. hp1 := tai(p.next);
  681. asml.remove(p);
  682. p.free;
  683. if not GetLastInstruction(hp1, p) then
  684. p := hp1;
  685. DoSubAddOpt := True;
  686. end
  687. end;
  688. end;
  689. end;
  690. begin
  691. p := BlockStart;
  692. ClearUsedRegs;
  693. while (p <> BlockEnd) Do
  694. begin
  695. UpDateUsedRegs(UsedRegs, tai(p.next));
  696. case p.Typ Of
  697. ait_instruction:
  698. begin
  699. current_filepos:=taicpu(p).fileinfo;
  700. if InsContainsSegRef(taicpu(p)) then
  701. begin
  702. p := tai(p.next);
  703. continue;
  704. end;
  705. { Handle Jmp Optimizations }
  706. if taicpu(p).is_jmp then
  707. begin
  708. {the following if-block removes all code between a jmp and the next label,
  709. because it can never be executed}
  710. if (taicpu(p).opcode = A_JMP) then
  711. begin
  712. hp2:=p;
  713. while GetNextInstruction(hp2, hp1) and
  714. (hp1.typ <> ait_label) do
  715. if not(hp1.typ in ([ait_label,ait_align]+skipinstr)) then
  716. begin
  717. { don't kill start/end of assembler block,
  718. no-line-info-start/end etc }
  719. if hp1.typ<>ait_marker then
  720. begin
  721. asml.remove(hp1);
  722. hp1.free;
  723. end
  724. else
  725. hp2:=hp1;
  726. end
  727. else break;
  728. end;
  729. { remove jumps to a label coming right after them }
  730. if GetNextInstruction(p, hp1) then
  731. begin
  732. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp1) and
  733. { TODO: FIXME removing the first instruction fails}
  734. (p<>blockstart) then
  735. begin
  736. hp2:=tai(hp1.next);
  737. asml.remove(p);
  738. p.free;
  739. p:=hp2;
  740. continue;
  741. end
  742. else
  743. begin
  744. if hp1.typ = ait_label then
  745. SkipLabels(hp1,hp1);
  746. if (tai(hp1).typ=ait_instruction) and
  747. (taicpu(hp1).opcode=A_JMP) and
  748. GetNextInstruction(hp1, hp2) and
  749. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp2) then
  750. begin
  751. if taicpu(p).opcode=A_Jcc then
  752. begin
  753. taicpu(p).condition:=inverse_cond(taicpu(p).condition);
  754. tai_label(hp2).labsym.decrefs;
  755. taicpu(p).oper[0]^.ref^.symbol:=taicpu(hp1).oper[0]^.ref^.symbol;
  756. { when free'ing hp1, the ref. isn't decresed, so we don't
  757. increase it (FK)
  758. taicpu(p).oper[0]^.ref^.symbol.increfs;
  759. }
  760. asml.remove(hp1);
  761. hp1.free;
  762. GetFinalDestination(asml, taicpu(p),0);
  763. end
  764. else
  765. begin
  766. GetFinalDestination(asml, taicpu(p),0);
  767. p:=tai(p.next);
  768. continue;
  769. end;
  770. end
  771. else
  772. GetFinalDestination(asml, taicpu(p),0);
  773. end;
  774. end;
  775. end
  776. else
  777. { All other optimizes }
  778. begin
  779. for l := 0 to taicpu(p).ops-1 Do
  780. if (taicpu(p).oper[l]^.typ = top_ref) then
  781. With taicpu(p).oper[l]^.ref^ Do
  782. begin
  783. if (base = NR_NO) and
  784. (index <> NR_NO) and
  785. (scalefactor in [0,1]) then
  786. begin
  787. base := index;
  788. index := NR_NO
  789. end
  790. end;
  791. case taicpu(p).opcode Of
  792. A_AND:
  793. if OptPass1And(p) then
  794. continue;
  795. A_CMP:
  796. begin
  797. { cmp register,$8000 neg register
  798. je target --> jo target
  799. .... only if register is deallocated before jump.}
  800. case Taicpu(p).opsize of
  801. S_B: v:=$80;
  802. S_W: v:=$8000;
  803. S_L: v:=aint($80000000);
  804. else
  805. internalerror(2013112905);
  806. end;
  807. if (taicpu(p).oper[0]^.typ=Top_const) and
  808. (taicpu(p).oper[0]^.val=v) and
  809. (Taicpu(p).oper[1]^.typ=top_reg) and
  810. GetNextInstruction(p, hp1) and
  811. (hp1.typ=ait_instruction) and
  812. (taicpu(hp1).opcode=A_Jcc) and
  813. (Taicpu(hp1).condition in [C_E,C_NE]) and
  814. not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, UsedRegs)) then
  815. begin
  816. Taicpu(p).opcode:=A_NEG;
  817. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  818. Taicpu(p).clearop(1);
  819. Taicpu(p).ops:=1;
  820. if Taicpu(hp1).condition=C_E then
  821. Taicpu(hp1).condition:=C_O
  822. else
  823. Taicpu(hp1).condition:=C_NO;
  824. continue;
  825. end;
  826. {
  827. @@2: @@2:
  828. .... ....
  829. cmp operand1,0
  830. jle/jbe @@1
  831. dec operand1 --> sub operand1,1
  832. jmp @@2 jge/jae @@2
  833. @@1: @@1:
  834. ... ....}
  835. if (taicpu(p).oper[0]^.typ = top_const) and
  836. (taicpu(p).oper[1]^.typ in [top_reg,top_ref]) and
  837. (taicpu(p).oper[0]^.val = 0) and
  838. GetNextInstruction(p, hp1) and
  839. (hp1.typ = ait_instruction) and
  840. (taicpu(hp1).is_jmp) and
  841. (taicpu(hp1).opcode=A_Jcc) and
  842. (taicpu(hp1).condition in [C_LE,C_BE]) and
  843. GetNextInstruction(hp1,hp2) and
  844. (hp2.typ = ait_instruction) and
  845. (taicpu(hp2).opcode = A_DEC) and
  846. OpsEqual(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  847. GetNextInstruction(hp2, hp3) and
  848. (hp3.typ = ait_instruction) and
  849. (taicpu(hp3).is_jmp) and
  850. (taicpu(hp3).opcode = A_JMP) and
  851. GetNextInstruction(hp3, hp4) and
  852. FindLabel(tasmlabel(taicpu(hp1).oper[0]^.ref^.symbol),hp4) then
  853. begin
  854. taicpu(hp2).Opcode := A_SUB;
  855. taicpu(hp2).loadoper(1,taicpu(hp2).oper[0]^);
  856. taicpu(hp2).loadConst(0,1);
  857. taicpu(hp2).ops:=2;
  858. taicpu(hp3).Opcode := A_Jcc;
  859. case taicpu(hp1).condition of
  860. C_LE: taicpu(hp3).condition := C_GE;
  861. C_BE: taicpu(hp3).condition := C_AE;
  862. end;
  863. asml.remove(p);
  864. asml.remove(hp1);
  865. p.free;
  866. hp1.free;
  867. p := hp2;
  868. continue;
  869. end
  870. end;
  871. A_FLD:
  872. begin
  873. if (taicpu(p).oper[0]^.typ = top_reg) and
  874. GetNextInstruction(p, hp1) and
  875. (hp1.typ = Ait_Instruction) and
  876. (taicpu(hp1).oper[0]^.typ = top_reg) and
  877. (taicpu(hp1).oper[1]^.typ = top_reg) and
  878. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  879. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  880. { change to
  881. fld reg fxxx reg,st
  882. fxxxp st, st1 (hp1)
  883. Remark: non commutative operations must be reversed!
  884. }
  885. begin
  886. case taicpu(hp1).opcode Of
  887. A_FMULP,A_FADDP,
  888. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  889. begin
  890. case taicpu(hp1).opcode Of
  891. A_FADDP: taicpu(hp1).opcode := A_FADD;
  892. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  893. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  894. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  895. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  896. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  897. end;
  898. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  899. taicpu(hp1).oper[1]^.reg := NR_ST;
  900. asml.remove(p);
  901. p.free;
  902. p := hp1;
  903. continue;
  904. end;
  905. end;
  906. end
  907. else
  908. if (taicpu(p).oper[0]^.typ = top_ref) and
  909. GetNextInstruction(p, hp2) and
  910. (hp2.typ = Ait_Instruction) and
  911. (taicpu(hp2).ops = 2) and
  912. (taicpu(hp2).oper[0]^.typ = top_reg) and
  913. (taicpu(hp2).oper[1]^.typ = top_reg) and
  914. (taicpu(p).opsize in [S_FS, S_FL]) and
  915. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  916. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  917. if GetLastInstruction(p, hp1) and
  918. (hp1.typ = Ait_Instruction) and
  919. ((taicpu(hp1).opcode = A_FLD) or
  920. (taicpu(hp1).opcode = A_FST)) and
  921. (taicpu(hp1).opsize = taicpu(p).opsize) and
  922. (taicpu(hp1).oper[0]^.typ = top_ref) and
  923. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  924. if ((taicpu(hp2).opcode = A_FMULP) or
  925. (taicpu(hp2).opcode = A_FADDP)) then
  926. { change to
  927. fld/fst mem1 (hp1) fld/fst mem1
  928. fld mem1 (p) fadd/
  929. faddp/ fmul st, st
  930. fmulp st, st1 (hp2) }
  931. begin
  932. asml.remove(p);
  933. p.free;
  934. p := hp1;
  935. if (taicpu(hp2).opcode = A_FADDP) then
  936. taicpu(hp2).opcode := A_FADD
  937. else
  938. taicpu(hp2).opcode := A_FMUL;
  939. taicpu(hp2).oper[1]^.reg := NR_ST;
  940. end
  941. else
  942. { change to
  943. fld/fst mem1 (hp1) fld/fst mem1
  944. fld mem1 (p) fld st}
  945. begin
  946. taicpu(p).changeopsize(S_FL);
  947. taicpu(p).loadreg(0,NR_ST);
  948. end
  949. else
  950. begin
  951. case taicpu(hp2).opcode Of
  952. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  953. { change to
  954. fld/fst mem1 (hp1) fld/fst mem1
  955. fld mem2 (p) fxxx mem2
  956. fxxxp st, st1 (hp2) }
  957. begin
  958. case taicpu(hp2).opcode Of
  959. A_FADDP: taicpu(p).opcode := A_FADD;
  960. A_FMULP: taicpu(p).opcode := A_FMUL;
  961. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  962. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  963. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  964. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  965. end;
  966. asml.remove(hp2);
  967. hp2.free;
  968. end
  969. end
  970. end
  971. end;
  972. A_FSTP,A_FISTP:
  973. if doFpuLoadStoreOpt(p) then
  974. continue;
  975. A_LEA:
  976. begin
  977. {removes seg register prefixes from LEA operations, as they
  978. don't do anything}
  979. taicpu(p).oper[0]^.ref^.Segment := NR_NO;
  980. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  981. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  982. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX..RS_ESP]) and
  983. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  984. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  985. begin
  986. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  987. (taicpu(p).oper[0]^.ref^.offset = 0) then
  988. begin
  989. hp1 := taicpu.op_reg_reg(A_MOV, S_L,taicpu(p).oper[0]^.ref^.base,
  990. taicpu(p).oper[1]^.reg);
  991. InsertLLItem(p.previous,p.next, hp1);
  992. p.free;
  993. p := hp1;
  994. continue;
  995. end
  996. else if (taicpu(p).oper[0]^.ref^.offset = 0) then
  997. begin
  998. hp1 := tai(p.Next);
  999. asml.remove(p);
  1000. p.free;
  1001. p := hp1;
  1002. continue;
  1003. end
  1004. { continue to use lea to adjust the stack pointer,
  1005. it is the recommended way, but only if not optimizing for size }
  1006. else if (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) or
  1007. (cs_opt_size in current_settings.optimizerswitches) then
  1008. with taicpu(p).oper[0]^.ref^ do
  1009. if (base = taicpu(p).oper[1]^.reg) then
  1010. begin
  1011. l := offset;
  1012. if (l=1) and UseIncDec then
  1013. begin
  1014. taicpu(p).opcode := A_INC;
  1015. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  1016. taicpu(p).ops := 1
  1017. end
  1018. else if (l=-1) and UseIncDec then
  1019. begin
  1020. taicpu(p).opcode := A_DEC;
  1021. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  1022. taicpu(p).ops := 1;
  1023. end
  1024. else
  1025. begin
  1026. if (l<0) and (l<>-2147483648) then
  1027. begin
  1028. taicpu(p).opcode := A_SUB;
  1029. taicpu(p).loadConst(0,-l);
  1030. end
  1031. else
  1032. begin
  1033. taicpu(p).opcode := A_ADD;
  1034. taicpu(p).loadConst(0,l);
  1035. end;
  1036. end;
  1037. end;
  1038. end
  1039. (*
  1040. This is unsafe, lea doesn't modify the flags but "add"
  1041. does. This breaks webtbs/tw15694.pp. The above
  1042. transformations are also unsafe, but they don't seem to
  1043. be triggered by code that FPC generators (or that at
  1044. least does not occur in the tests...). This needs to be
  1045. fixed by checking for the liveness of the flags register.
  1046. else if MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) then
  1047. begin
  1048. hp1:=taicpu.op_reg_reg(A_ADD,S_L,taicpu(p).oper[0]^.ref^.index,
  1049. taicpu(p).oper[0]^.ref^.base);
  1050. InsertLLItem(asml,p.previous,p.next, hp1);
  1051. DebugMsg('Peephole Lea2AddBase done',hp1);
  1052. p.free;
  1053. p:=hp1;
  1054. continue;
  1055. end
  1056. else if MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) then
  1057. begin
  1058. hp1:=taicpu.op_reg_reg(A_ADD,S_L,taicpu(p).oper[0]^.ref^.base,
  1059. taicpu(p).oper[0]^.ref^.index);
  1060. InsertLLItem(asml,p.previous,p.next,hp1);
  1061. DebugMsg('Peephole Lea2AddIndex done',hp1);
  1062. p.free;
  1063. p:=hp1;
  1064. continue;
  1065. end
  1066. *)
  1067. end;
  1068. A_MOV:
  1069. begin
  1070. If OptPass1MOV(p) then
  1071. Continue;
  1072. end;
  1073. A_MOVSX,
  1074. A_MOVZX :
  1075. begin
  1076. if (taicpu(p).oper[1]^.typ = top_reg) and
  1077. GetNextInstruction(p,hp1) and
  1078. (hp1.typ = ait_instruction) and
  1079. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  1080. (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX]) and
  1081. GetNextInstruction(hp1,hp2) and
  1082. MatchInstruction(hp2,A_MOV,[]) and
  1083. (taicpu(hp2).oper[0]^.typ = top_reg) and
  1084. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  1085. (((taicpu(hp1).ops=2) and
  1086. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  1087. ((taicpu(hp1).ops=1) and
  1088. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  1089. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  1090. { change movsX/movzX reg/ref, reg2 }
  1091. { add/sub/or/... reg3/$const, reg2 }
  1092. { mov reg2 reg/ref }
  1093. { to add/sub/or/... reg3/$const, reg/ref }
  1094. begin
  1095. { by example:
  1096. movswl %si,%eax movswl %si,%eax p
  1097. decl %eax addl %edx,%eax hp1
  1098. movw %ax,%si movw %ax,%si hp2
  1099. ->
  1100. movswl %si,%eax movswl %si,%eax p
  1101. decw %eax addw %edx,%eax hp1
  1102. movw %ax,%si movw %ax,%si hp2
  1103. }
  1104. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  1105. {
  1106. ->
  1107. movswl %si,%eax movswl %si,%eax p
  1108. decw %si addw %dx,%si hp1
  1109. movw %ax,%si movw %ax,%si hp2
  1110. }
  1111. case taicpu(hp1).ops of
  1112. 1:
  1113. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  1114. 2:
  1115. begin
  1116. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  1117. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  1118. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1119. end;
  1120. else
  1121. internalerror(2008042701);
  1122. end;
  1123. {
  1124. ->
  1125. decw %si addw %dx,%si p
  1126. }
  1127. asml.remove(p);
  1128. asml.remove(hp2);
  1129. p.free;
  1130. hp2.free;
  1131. p := hp1
  1132. end
  1133. { removes superfluous And's after movzx's }
  1134. else if taicpu(p).opcode=A_MOVZX then
  1135. begin
  1136. if (taicpu(p).oper[1]^.typ = top_reg) and
  1137. GetNextInstruction(p, hp1) and
  1138. (tai(hp1).typ = ait_instruction) and
  1139. (taicpu(hp1).opcode = A_AND) and
  1140. (taicpu(hp1).oper[0]^.typ = top_const) and
  1141. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1142. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1143. case taicpu(p).opsize Of
  1144. S_BL, S_BW:
  1145. if (taicpu(hp1).oper[0]^.val = $ff) then
  1146. begin
  1147. asml.remove(hp1);
  1148. hp1.free;
  1149. end;
  1150. S_WL:
  1151. if (taicpu(hp1).oper[0]^.val = $ffff) then
  1152. begin
  1153. asml.remove(hp1);
  1154. hp1.free;
  1155. end;
  1156. end;
  1157. {changes some movzx constructs to faster synonims (all examples
  1158. are given with eax/ax, but are also valid for other registers)}
  1159. if (taicpu(p).oper[1]^.typ = top_reg) then
  1160. if (taicpu(p).oper[0]^.typ = top_reg) then
  1161. case taicpu(p).opsize of
  1162. S_BW:
  1163. begin
  1164. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  1165. not(cs_opt_size in current_settings.optimizerswitches) then
  1166. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  1167. begin
  1168. taicpu(p).opcode := A_AND;
  1169. taicpu(p).changeopsize(S_W);
  1170. taicpu(p).loadConst(0,$ff);
  1171. end
  1172. else if GetNextInstruction(p, hp1) and
  1173. (tai(hp1).typ = ait_instruction) and
  1174. (taicpu(hp1).opcode = A_AND) and
  1175. (taicpu(hp1).oper[0]^.typ = top_const) and
  1176. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1177. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1178. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  1179. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  1180. begin
  1181. taicpu(p).opcode := A_MOV;
  1182. taicpu(p).changeopsize(S_W);
  1183. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  1184. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1185. end;
  1186. end;
  1187. S_BL:
  1188. begin
  1189. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  1190. not(cs_opt_size in current_settings.optimizerswitches) then
  1191. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  1192. begin
  1193. taicpu(p).opcode := A_AND;
  1194. taicpu(p).changeopsize(S_L);
  1195. taicpu(p).loadConst(0,$ff)
  1196. end
  1197. else if GetNextInstruction(p, hp1) and
  1198. (tai(hp1).typ = ait_instruction) and
  1199. (taicpu(hp1).opcode = A_AND) and
  1200. (taicpu(hp1).oper[0]^.typ = top_const) and
  1201. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1202. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1203. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  1204. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  1205. begin
  1206. taicpu(p).opcode := A_MOV;
  1207. taicpu(p).changeopsize(S_L);
  1208. setsubreg(taicpu(p).oper[0]^.reg,R_SUBWHOLE);
  1209. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1210. end
  1211. end;
  1212. S_WL:
  1213. begin
  1214. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  1215. not(cs_opt_size in current_settings.optimizerswitches) then
  1216. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  1217. begin
  1218. taicpu(p).opcode := A_AND;
  1219. taicpu(p).changeopsize(S_L);
  1220. taicpu(p).loadConst(0,$ffff);
  1221. end
  1222. else if GetNextInstruction(p, hp1) and
  1223. (tai(hp1).typ = ait_instruction) and
  1224. (taicpu(hp1).opcode = A_AND) and
  1225. (taicpu(hp1).oper[0]^.typ = top_const) and
  1226. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1227. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1228. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  1229. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  1230. begin
  1231. taicpu(p).opcode := A_MOV;
  1232. taicpu(p).changeopsize(S_L);
  1233. setsubreg(taicpu(p).oper[0]^.reg,R_SUBWHOLE);
  1234. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  1235. end;
  1236. end;
  1237. end
  1238. else if (taicpu(p).oper[0]^.typ = top_ref) then
  1239. begin
  1240. if GetNextInstruction(p, hp1) and
  1241. (tai(hp1).typ = ait_instruction) and
  1242. (taicpu(hp1).opcode = A_AND) and
  1243. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  1244. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  1245. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1246. begin
  1247. taicpu(p).opcode := A_MOV;
  1248. case taicpu(p).opsize Of
  1249. S_BL:
  1250. begin
  1251. taicpu(p).changeopsize(S_L);
  1252. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1253. end;
  1254. S_WL:
  1255. begin
  1256. taicpu(p).changeopsize(S_L);
  1257. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  1258. end;
  1259. S_BW:
  1260. begin
  1261. taicpu(p).changeopsize(S_W);
  1262. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1263. end;
  1264. end;
  1265. end;
  1266. end;
  1267. end;
  1268. end;
  1269. (* should not be generated anymore by the current code generator
  1270. A_POP:
  1271. begin
  1272. if target_info.system=system_i386_go32v2 then
  1273. begin
  1274. { Transform a series of pop/pop/pop/push/push/push to }
  1275. { 'movl x(%esp),%reg' for go32v2 (not for the rest, }
  1276. { because I'm not sure whether they can cope with }
  1277. { 'movl x(%esp),%reg' with x > 0, I believe we had }
  1278. { such a problem when using esp as frame pointer (JM) }
  1279. if (taicpu(p).oper[0]^.typ = top_reg) then
  1280. begin
  1281. hp1 := p;
  1282. hp2 := p;
  1283. l := 0;
  1284. while getNextInstruction(hp1,hp1) and
  1285. (hp1.typ = ait_instruction) and
  1286. (taicpu(hp1).opcode = A_POP) and
  1287. (taicpu(hp1).oper[0]^.typ = top_reg) do
  1288. begin
  1289. hp2 := hp1;
  1290. inc(l,4);
  1291. end;
  1292. getLastInstruction(p,hp3);
  1293. l1 := 0;
  1294. while (hp2 <> hp3) and
  1295. assigned(hp1) and
  1296. (hp1.typ = ait_instruction) and
  1297. (taicpu(hp1).opcode = A_PUSH) and
  1298. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1299. (taicpu(hp1).oper[0]^.reg.enum = taicpu(hp2).oper[0]^.reg.enum) do
  1300. begin
  1301. { change it to a two op operation }
  1302. taicpu(hp2).oper[1]^.typ:=top_none;
  1303. taicpu(hp2).ops:=2;
  1304. taicpu(hp2).opcode := A_MOV;
  1305. taicpu(hp2).loadoper(1,taicpu(hp1).oper[0]^);
  1306. reference_reset(tmpref);
  1307. tmpRef.base.enum:=R_INTREGISTER;
  1308. tmpRef.base.number:=NR_STACK_POINTER_REG;
  1309. convert_register_to_enum(tmpref.base);
  1310. tmpRef.offset := l;
  1311. taicpu(hp2).loadRef(0,tmpRef);
  1312. hp4 := hp1;
  1313. getNextInstruction(hp1,hp1);
  1314. asml.remove(hp4);
  1315. hp4.free;
  1316. getLastInstruction(hp2,hp2);
  1317. dec(l,4);
  1318. inc(l1);
  1319. end;
  1320. if l <> -4 then
  1321. begin
  1322. inc(l,4);
  1323. for l1 := l1 downto 1 do
  1324. begin
  1325. getNextInstruction(hp2,hp2);
  1326. dec(taicpu(hp2).oper[0]^.ref^.offset,l);
  1327. end
  1328. end
  1329. end
  1330. end
  1331. else
  1332. begin
  1333. if (taicpu(p).oper[0]^.typ = top_reg) and
  1334. GetNextInstruction(p, hp1) and
  1335. (tai(hp1).typ=ait_instruction) and
  1336. (taicpu(hp1).opcode=A_PUSH) and
  1337. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1338. (taicpu(hp1).oper[0]^.reg.enum=taicpu(p).oper[0]^.reg.enum) then
  1339. begin
  1340. { change it to a two op operation }
  1341. taicpu(p).oper[1]^.typ:=top_none;
  1342. taicpu(p).ops:=2;
  1343. taicpu(p).opcode := A_MOV;
  1344. taicpu(p).loadoper(1,taicpu(p).oper[0]^);
  1345. reference_reset(tmpref);
  1346. TmpRef.base.enum := R_ESP;
  1347. taicpu(p).loadRef(0,TmpRef);
  1348. asml.remove(hp1);
  1349. hp1.free;
  1350. end;
  1351. end;
  1352. end;
  1353. *)
  1354. A_PUSH:
  1355. begin
  1356. if (taicpu(p).opsize = S_W) and
  1357. (taicpu(p).oper[0]^.typ = Top_Const) and
  1358. GetNextInstruction(p, hp1) and
  1359. (tai(hp1).typ = ait_instruction) and
  1360. (taicpu(hp1).opcode = A_PUSH) and
  1361. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  1362. (taicpu(hp1).opsize = S_W) then
  1363. begin
  1364. taicpu(p).changeopsize(S_L);
  1365. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  1366. asml.remove(hp1);
  1367. hp1.free;
  1368. end;
  1369. end;
  1370. A_SHL, A_SAL:
  1371. begin
  1372. if (taicpu(p).oper[0]^.typ = Top_Const) and
  1373. (taicpu(p).oper[1]^.typ = Top_Reg) and
  1374. (taicpu(p).opsize = S_L) and
  1375. (taicpu(p).oper[0]^.val <= 3) then
  1376. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1377. begin
  1378. TmpBool1 := True; {should we check the next instruction?}
  1379. TmpBool2 := False; {have we found an add/sub which could be
  1380. integrated in the lea?}
  1381. reference_reset(tmpref,2);
  1382. TmpRef.index := taicpu(p).oper[1]^.reg;
  1383. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  1384. while TmpBool1 and
  1385. GetNextInstruction(p, hp1) and
  1386. (tai(hp1).typ = ait_instruction) and
  1387. ((((taicpu(hp1).opcode = A_ADD) or
  1388. (taicpu(hp1).opcode = A_SUB)) and
  1389. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  1390. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  1391. (((taicpu(hp1).opcode = A_INC) or
  1392. (taicpu(hp1).opcode = A_DEC)) and
  1393. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  1394. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg))) and
  1395. (not GetNextInstruction(hp1,hp2) or
  1396. not instrReadsFlags(hp2)) Do
  1397. begin
  1398. TmpBool1 := False;
  1399. if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  1400. begin
  1401. TmpBool1 := True;
  1402. TmpBool2 := True;
  1403. case taicpu(hp1).opcode of
  1404. A_ADD:
  1405. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  1406. A_SUB:
  1407. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  1408. end;
  1409. asml.remove(hp1);
  1410. hp1.free;
  1411. end
  1412. else
  1413. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  1414. (((taicpu(hp1).opcode = A_ADD) and
  1415. (TmpRef.base = NR_NO)) or
  1416. (taicpu(hp1).opcode = A_INC) or
  1417. (taicpu(hp1).opcode = A_DEC)) then
  1418. begin
  1419. TmpBool1 := True;
  1420. TmpBool2 := True;
  1421. case taicpu(hp1).opcode of
  1422. A_ADD:
  1423. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  1424. A_INC:
  1425. inc(TmpRef.offset);
  1426. A_DEC:
  1427. dec(TmpRef.offset);
  1428. end;
  1429. asml.remove(hp1);
  1430. hp1.free;
  1431. end;
  1432. end;
  1433. if TmpBool2 or
  1434. ((current_settings.optimizecputype < cpu_Pentium2) and
  1435. (taicpu(p).oper[0]^.val <= 3) and
  1436. not(cs_opt_size in current_settings.optimizerswitches)) then
  1437. begin
  1438. if not(TmpBool2) and
  1439. (taicpu(p).oper[0]^.val = 1) then
  1440. begin
  1441. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  1442. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  1443. end
  1444. else
  1445. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef,
  1446. taicpu(p).oper[1]^.reg);
  1447. InsertLLItem(p.previous, p.next, hp1);
  1448. p.free;
  1449. p := hp1;
  1450. end;
  1451. end
  1452. else
  1453. if (current_settings.optimizecputype < cpu_Pentium2) and
  1454. (taicpu(p).oper[0]^.typ = top_const) and
  1455. (taicpu(p).oper[1]^.typ = top_reg) then
  1456. if (taicpu(p).oper[0]^.val = 1) then
  1457. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1458. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  1459. (unlike shl, which is only Tairable in the U pipe)}
  1460. begin
  1461. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  1462. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  1463. InsertLLItem(p.previous, p.next, hp1);
  1464. p.free;
  1465. p := hp1;
  1466. end
  1467. else if (taicpu(p).opsize = S_L) and
  1468. (taicpu(p).oper[0]^.val<= 3) then
  1469. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1470. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1471. begin
  1472. reference_reset(tmpref,2);
  1473. TmpRef.index := taicpu(p).oper[1]^.reg;
  1474. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  1475. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  1476. InsertLLItem(p.previous, p.next, hp1);
  1477. p.free;
  1478. p := hp1;
  1479. end
  1480. end;
  1481. A_SETcc :
  1482. { changes
  1483. setcc (funcres) setcc reg
  1484. movb (funcres), reg to leave/ret
  1485. leave/ret }
  1486. begin
  1487. if (taicpu(p).oper[0]^.typ = top_ref) and
  1488. GetNextInstruction(p, hp1) and
  1489. GetNextInstruction(hp1, hp2) and
  1490. IsExitCode(hp2) and
  1491. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  1492. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  1493. not(assigned(current_procinfo.procdef.funcretsym) and
  1494. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  1495. (hp1.typ = ait_instruction) and
  1496. (taicpu(hp1).opcode = A_MOV) and
  1497. (taicpu(hp1).opsize = S_B) and
  1498. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1499. RefsEqual(taicpu(hp1).oper[0]^.ref^, taicpu(p).oper[0]^.ref^) then
  1500. begin
  1501. taicpu(p).loadReg(0,taicpu(hp1).oper[1]^.reg);
  1502. asml.remove(hp1);
  1503. hp1.free;
  1504. end
  1505. end;
  1506. A_SUB:
  1507. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1508. { * change "sub/add const1, reg" or "dec reg" followed by
  1509. "sub const2, reg" to one "sub ..., reg" }
  1510. begin
  1511. if (taicpu(p).oper[0]^.typ = top_const) and
  1512. (taicpu(p).oper[1]^.typ = top_reg) then
  1513. if (taicpu(p).oper[0]^.val = 2) and
  1514. (taicpu(p).oper[1]^.reg = NR_ESP) and
  1515. { Don't do the sub/push optimization if the sub }
  1516. { comes from setting up the stack frame (JM) }
  1517. (not getLastInstruction(p,hp1) or
  1518. (hp1.typ <> ait_instruction) or
  1519. (taicpu(hp1).opcode <> A_MOV) or
  1520. (taicpu(hp1).oper[0]^.typ <> top_reg) or
  1521. (taicpu(hp1).oper[0]^.reg <> NR_ESP) or
  1522. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  1523. (taicpu(hp1).oper[1]^.reg <> NR_EBP)) then
  1524. begin
  1525. hp1 := tai(p.next);
  1526. while Assigned(hp1) and
  1527. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  1528. not RegReadByInstruction(NR_ESP,hp1) and
  1529. not RegModifiedByInstruction(NR_ESP,hp1) do
  1530. hp1 := tai(hp1.next);
  1531. if Assigned(hp1) and
  1532. (tai(hp1).typ = ait_instruction) and
  1533. (taicpu(hp1).opcode = A_PUSH) and
  1534. (taicpu(hp1).opsize = S_W) then
  1535. begin
  1536. taicpu(hp1).changeopsize(S_L);
  1537. if taicpu(hp1).oper[0]^.typ=top_reg then
  1538. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  1539. hp1 := tai(p.next);
  1540. asml.remove(p);
  1541. p.free;
  1542. p := hp1;
  1543. continue
  1544. end;
  1545. if DoSubAddOpt(p) then
  1546. continue;
  1547. end
  1548. else if DoSubAddOpt(p) then
  1549. continue
  1550. end;
  1551. A_VMOVAPS,
  1552. A_VMOVAPD:
  1553. if OptPass1VMOVAP(p) then
  1554. continue;
  1555. A_VDIVSD,
  1556. A_VDIVSS,
  1557. A_VSUBSD,
  1558. A_VSUBSS,
  1559. A_VMULSD,
  1560. A_VMULSS,
  1561. A_VADDSD,
  1562. A_VADDSS:
  1563. if OptPass1VOP(p) then
  1564. continue;
  1565. end;
  1566. end; { if is_jmp }
  1567. end;
  1568. end;
  1569. updateUsedRegs(UsedRegs,p);
  1570. p:=tai(p.next);
  1571. end;
  1572. end;
  1573. procedure TCPUAsmOptimizer.PeepHoleOptPass2;
  1574. {$ifdef DEBUG_AOPTCPU}
  1575. procedure DebugMsg(const s: string;p : tai);
  1576. begin
  1577. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  1578. end;
  1579. {$else DEBUG_AOPTCPU}
  1580. procedure DebugMsg(const s: string;p : tai);inline;
  1581. begin
  1582. end;
  1583. {$endif DEBUG_AOPTCPU}
  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)
  1590. { we can't use cmov ref,reg because
  1591. ref could be nil and cmov still throws an exception
  1592. if ref=nil but the mov isn't done (FK)
  1593. or ((taicpu(p).oper[0]^.typ = top_ref) and
  1594. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  1595. }
  1596. ) and
  1597. (taicpu(p).oper[1]^.typ in [top_reg]);
  1598. end;
  1599. var
  1600. p,hp1,hp2,hp3: tai;
  1601. l : longint;
  1602. condition : tasmcond;
  1603. carryadd_opcode: Tasmop;
  1604. begin
  1605. p := BlockStart;
  1606. ClearUsedRegs;
  1607. while (p <> BlockEnd) Do
  1608. begin
  1609. UpdateUsedRegs(UsedRegs, tai(p.next));
  1610. case p.Typ Of
  1611. Ait_Instruction:
  1612. begin
  1613. if InsContainsSegRef(taicpu(p)) then
  1614. begin
  1615. p := tai(p.next);
  1616. continue;
  1617. end;
  1618. case taicpu(p).opcode Of
  1619. A_Jcc:
  1620. begin
  1621. { jb @@1 cmc
  1622. inc/dec operand --> adc/sbb operand,0
  1623. @@1:
  1624. ... and ...
  1625. jnb @@1
  1626. inc/dec operand --> adc/sbb operand,0
  1627. @@1: }
  1628. if GetNextInstruction(p,hp1) and (hp1.typ=ait_instruction) and
  1629. GetNextInstruction(hp1,hp2) and (hp2.typ=ait_label) and
  1630. (Tasmlabel(Taicpu(p).oper[0]^.ref^.symbol)=Tai_label(hp2).labsym) then
  1631. begin
  1632. carryadd_opcode:=A_NONE;
  1633. if Taicpu(p).condition in [C_NAE,C_B] then
  1634. begin
  1635. if Taicpu(hp1).opcode=A_INC then
  1636. carryadd_opcode:=A_ADC;
  1637. if Taicpu(hp1).opcode=A_DEC then
  1638. carryadd_opcode:=A_SBB;
  1639. if carryadd_opcode<>A_NONE then
  1640. begin
  1641. Taicpu(p).clearop(0);
  1642. Taicpu(p).ops:=0;
  1643. Taicpu(p).is_jmp:=false;
  1644. Taicpu(p).opcode:=A_CMC;
  1645. Taicpu(p).condition:=C_NONE;
  1646. Taicpu(hp1).ops:=2;
  1647. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  1648. Taicpu(hp1).loadconst(0,0);
  1649. Taicpu(hp1).opcode:=carryadd_opcode;
  1650. continue;
  1651. end;
  1652. end;
  1653. if Taicpu(p).condition in [C_AE,C_NB] then
  1654. begin
  1655. if Taicpu(hp1).opcode=A_INC then
  1656. carryadd_opcode:=A_ADC;
  1657. if Taicpu(hp1).opcode=A_DEC then
  1658. carryadd_opcode:=A_SBB;
  1659. if carryadd_opcode<>A_NONE then
  1660. begin
  1661. asml.remove(p);
  1662. p.free;
  1663. Taicpu(hp1).ops:=2;
  1664. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  1665. Taicpu(hp1).loadconst(0,0);
  1666. Taicpu(hp1).opcode:=carryadd_opcode;
  1667. p:=hp1;
  1668. continue;
  1669. end;
  1670. end;
  1671. end;
  1672. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  1673. begin
  1674. { check for
  1675. jCC xxx
  1676. <several movs>
  1677. xxx:
  1678. }
  1679. l:=0;
  1680. GetNextInstruction(p, hp1);
  1681. while assigned(hp1) and
  1682. CanBeCMOV(hp1) and
  1683. { stop on labels }
  1684. not(hp1.typ=ait_label) 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]^.ref^.symbol),hp1) then
  1692. begin
  1693. if (l<=4) and (l>0) then
  1694. begin
  1695. condition:=inverse_cond(taicpu(p).condition);
  1696. hp2:=p;
  1697. GetNextInstruction(p,hp1);
  1698. p:=hp1;
  1699. repeat
  1700. taicpu(hp1).opcode:=A_CMOVcc;
  1701. taicpu(hp1).condition:=condition;
  1702. GetNextInstruction(hp1,hp1);
  1703. until not(assigned(hp1)) or
  1704. not(CanBeCMOV(hp1));
  1705. { wait with removing else GetNextInstruction could
  1706. ignore the label if it was the only usage in the
  1707. jump moved away }
  1708. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1709. asml.remove(hp2);
  1710. hp2.free;
  1711. continue;
  1712. end;
  1713. end
  1714. else
  1715. begin
  1716. { check further for
  1717. jCC xxx
  1718. <several movs 1>
  1719. jmp yyy
  1720. xxx:
  1721. <several movs 2>
  1722. yyy:
  1723. }
  1724. { hp2 points to jmp yyy }
  1725. hp2:=hp1;
  1726. { skip hp1 to xxx }
  1727. GetNextInstruction(hp1, hp1);
  1728. if assigned(hp2) and
  1729. assigned(hp1) and
  1730. (l<=3) and
  1731. (hp2.typ=ait_instruction) and
  1732. (taicpu(hp2).is_jmp) and
  1733. (taicpu(hp2).condition=C_None) and
  1734. { real label and jump, no further references to the
  1735. label are allowed }
  1736. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=1) and
  1737. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1738. begin
  1739. l:=0;
  1740. { skip hp1 to <several moves 2> }
  1741. GetNextInstruction(hp1, hp1);
  1742. while assigned(hp1) and
  1743. CanBeCMOV(hp1) do
  1744. begin
  1745. inc(l);
  1746. GetNextInstruction(hp1, hp1);
  1747. end;
  1748. { hp1 points to yyy: }
  1749. if assigned(hp1) and
  1750. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  1751. begin
  1752. condition:=inverse_cond(taicpu(p).condition);
  1753. GetNextInstruction(p,hp1);
  1754. hp3:=p;
  1755. p:=hp1;
  1756. repeat
  1757. taicpu(hp1).opcode:=A_CMOVcc;
  1758. taicpu(hp1).condition:=condition;
  1759. GetNextInstruction(hp1,hp1);
  1760. until not(assigned(hp1)) or
  1761. not(CanBeCMOV(hp1));
  1762. { hp2 is still at jmp yyy }
  1763. GetNextInstruction(hp2,hp1);
  1764. { hp2 is now at xxx: }
  1765. condition:=inverse_cond(condition);
  1766. GetNextInstruction(hp1,hp1);
  1767. { hp1 is now at <several movs 2> }
  1768. repeat
  1769. taicpu(hp1).opcode:=A_CMOVcc;
  1770. taicpu(hp1).condition:=condition;
  1771. GetNextInstruction(hp1,hp1);
  1772. until not(assigned(hp1)) or
  1773. not(CanBeCMOV(hp1));
  1774. {
  1775. asml.remove(hp1.next)
  1776. hp1.next.free;
  1777. asml.remove(hp1);
  1778. hp1.free;
  1779. }
  1780. { remove jCC }
  1781. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  1782. asml.remove(hp3);
  1783. hp3.free;
  1784. { remove jmp }
  1785. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1786. asml.remove(hp2);
  1787. hp2.free;
  1788. continue;
  1789. end;
  1790. end;
  1791. end;
  1792. end;
  1793. end;
  1794. end;
  1795. A_FSTP,A_FISTP:
  1796. if DoFpuLoadStoreOpt(p) then
  1797. continue;
  1798. A_IMUL:
  1799. begin
  1800. if (taicpu(p).ops >= 2) and
  1801. ((taicpu(p).oper[0]^.typ = top_const) or
  1802. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  1803. (taicpu(p).oper[1]^.typ = top_reg) and
  1804. ((taicpu(p).ops = 2) or
  1805. ((taicpu(p).oper[2]^.typ = top_reg) and
  1806. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  1807. getLastInstruction(p,hp1) and
  1808. (hp1.typ = ait_instruction) and
  1809. (taicpu(hp1).opcode = A_MOV) and
  1810. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1811. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1812. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1813. { change "mov reg1,reg2; imul y,reg2" to "imul y,reg1,reg2" }
  1814. begin
  1815. taicpu(p).ops := 3;
  1816. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  1817. taicpu(p).loadreg(2,taicpu(hp1).oper[1]^.reg);
  1818. asml.remove(hp1);
  1819. hp1.free;
  1820. end;
  1821. end;
  1822. A_JMP:
  1823. {
  1824. change
  1825. jmp .L1
  1826. ...
  1827. .L1:
  1828. ret
  1829. into
  1830. ret
  1831. }
  1832. if (taicpu(p).oper[0]^.typ=top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full) then
  1833. begin
  1834. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  1835. if assigned(hp1) and SkipLabels(hp1,hp1) and (hp1.typ=ait_instruction) and (taicpu(hp1).opcode=A_RET) and (taicpu(p).condition=C_None) then
  1836. begin
  1837. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  1838. taicpu(p).opcode:=A_RET;
  1839. taicpu(p).is_jmp:=false;
  1840. taicpu(p).ops:=taicpu(hp1).ops;
  1841. case taicpu(hp1).ops of
  1842. 0:
  1843. taicpu(p).clearop(0);
  1844. 1:
  1845. taicpu(p).loadconst(0,taicpu(hp1).oper[0]^.val);
  1846. else
  1847. internalerror(2016041301);
  1848. end;
  1849. continue;
  1850. end;
  1851. end;
  1852. A_MOV:
  1853. if OptPass2MOV(p) then
  1854. continue;
  1855. end;
  1856. end;
  1857. end;
  1858. p := tai(p.next)
  1859. end;
  1860. end;
  1861. procedure TCPUAsmOptimizer.PostPeepHoleOpts;
  1862. var
  1863. p,hp1,hp2: tai;
  1864. IsTestConstX: boolean;
  1865. begin
  1866. p := BlockStart;
  1867. ClearUsedRegs;
  1868. while (p <> BlockEnd) Do
  1869. begin
  1870. UpdateUsedRegs(UsedRegs, tai(p.next));
  1871. case p.Typ Of
  1872. Ait_Instruction:
  1873. begin
  1874. if InsContainsSegRef(taicpu(p)) then
  1875. begin
  1876. p := tai(p.next);
  1877. continue;
  1878. end;
  1879. case taicpu(p).opcode Of
  1880. A_CALL:
  1881. begin
  1882. { don't do this on modern CPUs, this really hurts them due to
  1883. broken call/ret pairing }
  1884. if (current_settings.optimizecputype < cpu_Pentium2) and
  1885. not(cs_create_pic in current_settings.moduleswitches) and
  1886. GetNextInstruction(p, hp1) and
  1887. (hp1.typ = ait_instruction) and
  1888. (taicpu(hp1).opcode = A_JMP) and
  1889. ((taicpu(hp1).oper[0]^.typ=top_ref) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full)) then
  1890. begin
  1891. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  1892. InsertLLItem(p.previous, p, hp2);
  1893. taicpu(p).opcode := A_JMP;
  1894. taicpu(p).is_jmp := true;
  1895. asml.remove(hp1);
  1896. hp1.free;
  1897. end
  1898. { replace
  1899. call procname
  1900. ret
  1901. by
  1902. jmp procname
  1903. this should never hurt except when pic is used, not sure
  1904. how to handle it then
  1905. but do it only on level 4 because it destroys stack back traces
  1906. }
  1907. else if (cs_opt_level4 in current_settings.optimizerswitches) and
  1908. not(cs_create_pic in current_settings.moduleswitches) and
  1909. GetNextInstruction(p, hp1) and
  1910. (hp1.typ = ait_instruction) and
  1911. (taicpu(hp1).opcode = A_RET) and
  1912. (taicpu(hp1).ops=0) then
  1913. begin
  1914. taicpu(p).opcode := A_JMP;
  1915. taicpu(p).is_jmp := true;
  1916. asml.remove(hp1);
  1917. hp1.free;
  1918. end;
  1919. end;
  1920. A_CMP:
  1921. begin
  1922. if (taicpu(p).oper[0]^.typ = top_const) and
  1923. (taicpu(p).oper[0]^.val = 0) and
  1924. (taicpu(p).oper[1]^.typ = top_reg) then
  1925. {change "cmp $0, %reg" to "test %reg, %reg"}
  1926. begin
  1927. taicpu(p).opcode := A_TEST;
  1928. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  1929. continue;
  1930. end;
  1931. end;
  1932. A_MOV:
  1933. PostPeepholeOptMov(p);
  1934. A_MOVZX:
  1935. { if register vars are on, it's possible there is code like }
  1936. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  1937. { so we can't safely replace the movzx then with xor/mov, }
  1938. { since that would change the flags (JM) }
  1939. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  1940. begin
  1941. if (taicpu(p).oper[1]^.typ = top_reg) then
  1942. if (taicpu(p).oper[0]^.typ = top_reg)
  1943. then
  1944. case taicpu(p).opsize of
  1945. S_BL:
  1946. begin
  1947. if IsGP32Reg(taicpu(p).oper[1]^.reg) and
  1948. not(cs_opt_size in current_settings.optimizerswitches) and
  1949. (current_settings.optimizecputype = cpu_Pentium) then
  1950. {Change "movzbl %reg1, %reg2" to
  1951. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1952. PentiumMMX}
  1953. begin
  1954. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  1955. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  1956. InsertLLItem(p.previous, p, hp1);
  1957. taicpu(p).opcode := A_MOV;
  1958. taicpu(p).changeopsize(S_B);
  1959. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  1960. end;
  1961. end;
  1962. end
  1963. else if (taicpu(p).oper[0]^.typ = top_ref) and
  1964. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  1965. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  1966. not(cs_opt_size in current_settings.optimizerswitches) and
  1967. IsGP32Reg(taicpu(p).oper[1]^.reg) and
  1968. (current_settings.optimizecputype = cpu_Pentium) and
  1969. (taicpu(p).opsize = S_BL) then
  1970. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1971. Pentium and PentiumMMX}
  1972. begin
  1973. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  1974. taicpu(p).oper[1]^.reg);
  1975. taicpu(p).opcode := A_MOV;
  1976. taicpu(p).changeopsize(S_B);
  1977. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  1978. InsertLLItem(p.previous, p, hp1);
  1979. end;
  1980. end;
  1981. A_TEST, A_OR:
  1982. {removes the line marked with (x) from the sequence
  1983. and/or/xor/add/sub/... $x, %y
  1984. test/or %y, %y | test $-1, %y (x)
  1985. j(n)z _Label
  1986. as the first instruction already adjusts the ZF
  1987. %y operand may also be a reference }
  1988. begin
  1989. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  1990. MatchOperand(taicpu(p).oper[0]^,-1);
  1991. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  1992. GetLastInstruction(p, hp1) and
  1993. (tai(hp1).typ = ait_instruction) and
  1994. GetNextInstruction(p,hp2) and
  1995. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  1996. case taicpu(hp1).opcode Of
  1997. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  1998. begin
  1999. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  2000. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  2001. { and in case of carry for A(E)/B(E)/C/NC }
  2002. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  2003. ((taicpu(hp1).opcode <> A_ADD) and
  2004. (taicpu(hp1).opcode <> A_SUB))) then
  2005. begin
  2006. hp1 := tai(p.next);
  2007. asml.remove(p);
  2008. p.free;
  2009. p := tai(hp1);
  2010. continue
  2011. end;
  2012. end;
  2013. A_SHL, A_SAL, A_SHR, A_SAR:
  2014. begin
  2015. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  2016. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  2017. { therefore, it's only safe to do this optimization for }
  2018. { shifts by a (nonzero) constant }
  2019. (taicpu(hp1).oper[0]^.typ = top_const) and
  2020. (taicpu(hp1).oper[0]^.val <> 0) and
  2021. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  2022. { and in case of carry for A(E)/B(E)/C/NC }
  2023. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  2024. begin
  2025. hp1 := tai(p.next);
  2026. asml.remove(p);
  2027. p.free;
  2028. p := tai(hp1);
  2029. continue
  2030. end;
  2031. end;
  2032. A_DEC, A_INC, A_NEG:
  2033. begin
  2034. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  2035. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  2036. { and in case of carry for A(E)/B(E)/C/NC }
  2037. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  2038. begin
  2039. case taicpu(hp1).opcode Of
  2040. A_DEC, A_INC:
  2041. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  2042. begin
  2043. case taicpu(hp1).opcode Of
  2044. A_DEC: taicpu(hp1).opcode := A_SUB;
  2045. A_INC: taicpu(hp1).opcode := A_ADD;
  2046. end;
  2047. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  2048. taicpu(hp1).loadConst(0,1);
  2049. taicpu(hp1).ops:=2;
  2050. end
  2051. end;
  2052. hp1 := tai(p.next);
  2053. asml.remove(p);
  2054. p.free;
  2055. p := tai(hp1);
  2056. continue
  2057. end;
  2058. end
  2059. else
  2060. { change "test $-1,%reg" into "test %reg,%reg" }
  2061. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  2062. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  2063. end { case }
  2064. else
  2065. { change "test $-1,%reg" into "test %reg,%reg" }
  2066. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  2067. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  2068. end;
  2069. end;
  2070. end;
  2071. end;
  2072. p := tai(p.next)
  2073. end;
  2074. end;
  2075. Procedure TCpuAsmOptimizer.Optimize;
  2076. Var
  2077. HP: Tai;
  2078. pass: longint;
  2079. slowopt, changed, lastLoop: boolean;
  2080. Begin
  2081. slowopt := (cs_opt_level3 in current_settings.optimizerswitches);
  2082. pass := 0;
  2083. changed := false;
  2084. repeat
  2085. lastLoop :=
  2086. not(slowopt) or
  2087. (not changed and (pass > 2)) or
  2088. { prevent endless loops }
  2089. (pass = 4);
  2090. changed := false;
  2091. { Setup labeltable, always necessary }
  2092. blockstart := tai(asml.first);
  2093. pass_1;
  2094. { Blockend now either contains an ait_marker with Kind = mark_AsmBlockStart, }
  2095. { or nil }
  2096. While Assigned(BlockStart) Do
  2097. Begin
  2098. if (cs_opt_peephole in current_settings.optimizerswitches) then
  2099. begin
  2100. if (pass = 0) then
  2101. PrePeepHoleOpts;
  2102. { Peephole optimizations }
  2103. PeepHoleOptPass1;
  2104. { Only perform them twice in the first pass }
  2105. if pass = 0 then
  2106. PeepHoleOptPass1;
  2107. end;
  2108. { More peephole optimizations }
  2109. if (cs_opt_peephole in current_settings.optimizerswitches) then
  2110. begin
  2111. PeepHoleOptPass2;
  2112. if lastLoop then
  2113. PostPeepHoleOpts;
  2114. end;
  2115. { Continue where we left off, BlockEnd is either the start of an }
  2116. { assembler block or nil }
  2117. BlockStart := BlockEnd;
  2118. While Assigned(BlockStart) And
  2119. (BlockStart.typ = ait_Marker) And
  2120. (Tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  2121. Begin
  2122. { We stopped at an assembler block, so skip it }
  2123. Repeat
  2124. BlockStart := Tai(BlockStart.Next);
  2125. Until (BlockStart.Typ = Ait_Marker) And
  2126. (Tai_Marker(Blockstart).Kind = mark_AsmBlockEnd);
  2127. { Blockstart now contains a Tai_marker(mark_AsmBlockEnd) }
  2128. If GetNextInstruction(BlockStart, HP) And
  2129. ((HP.typ <> ait_Marker) Or
  2130. (Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
  2131. { There is no assembler block anymore after the current one, so }
  2132. { optimize the next block of "normal" instructions }
  2133. pass_1
  2134. { Otherwise, skip the next assembler block }
  2135. else
  2136. blockStart := hp;
  2137. End;
  2138. End;
  2139. inc(pass);
  2140. until lastLoop;
  2141. dfa.free;
  2142. End;
  2143. begin
  2144. casmoptimizer:=TCpuAsmOptimizer;
  2145. end.