popt386.pas 115 KB

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