aoptx86.pas 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857
  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 aoptx86;
  18. {$i fpcdefs.inc}
  19. { $define DEBUG_AOPTCPU}
  20. interface
  21. uses
  22. globtype,
  23. cpubase,
  24. aasmtai,aasmcpu,
  25. cgbase,cgutils,
  26. aopt,aoptobj;
  27. type
  28. TX86AsmOptimizer = class(TAsmOptimizer)
  29. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  30. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  31. function RegReadByInstruction(reg : TRegister; hp : tai) : boolean;
  32. protected
  33. { checks whether loading a new value in reg1 overwrites the entirety of reg2 }
  34. function Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  35. { checks whether reading the value in reg1 depends on the value of reg2. This
  36. is very similar to SuperRegisterEquals, except it takes into account that
  37. R_SUBH and R_SUBL are independendent (e.g. reading from AL does not
  38. depend on the value in AH). }
  39. function Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  40. procedure DebugMsg(const s : string; p : tai);inline;
  41. class function IsExitCode(p : tai) : boolean;
  42. class function isFoldableArithOp(hp1 : taicpu; reg : tregister) : boolean;
  43. procedure RemoveLastDeallocForFuncRes(p : tai);
  44. function DoSubAddOpt(var p : tai) : Boolean;
  45. function PrePeepholeOptSxx(var p : tai) : boolean;
  46. function PrePeepholeOptIMUL(var p : tai) : boolean;
  47. function OptPass1AND(var p : tai) : boolean;
  48. function OptPass1VMOVAP(var p : tai) : boolean;
  49. function OptPass1VOP(var p : tai) : boolean;
  50. function OptPass1MOV(var p : tai) : boolean;
  51. function OptPass1Movx(var p : tai) : boolean;
  52. function OptPass1MOVAP(var p : tai) : boolean;
  53. function OptPass1MOVXX(var p : tai) : boolean;
  54. function OptPass1OP(var p : tai) : boolean;
  55. function OptPass1LEA(var p : tai) : boolean;
  56. function OptPass1Sub(var p : tai) : boolean;
  57. function OptPass1SHLSAL(var p : tai) : boolean;
  58. function OptPass1SETcc(var p: tai): boolean;
  59. function OptPass2MOV(var p : tai) : boolean;
  60. function OptPass2Imul(var p : tai) : boolean;
  61. function OptPass2Jmp(var p : tai) : boolean;
  62. function OptPass2Jcc(var p : tai) : boolean;
  63. function PostPeepholeOptMov(var p : tai) : Boolean;
  64. {$ifdef x86_64} { These post-peephole optimisations only affect 64-bit registers. [Kit] }
  65. function PostPeepholeOptMovzx(var p : tai) : Boolean;
  66. function PostPeepholeOptXor(var p : tai) : Boolean;
  67. {$endif}
  68. function PostPeepholeOptCmp(var p : tai) : Boolean;
  69. function PostPeepholeOptTestOr(var p : tai) : Boolean;
  70. function PostPeepholeOptCall(var p : tai) : Boolean;
  71. function PostPeepholeOptLea(var p : tai) : Boolean;
  72. procedure OptReferences;
  73. end;
  74. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  75. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  76. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  77. function MatchInstruction(const instr: tai; const ops: array of TAsmOp; const opsize: topsizes): boolean;
  78. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  79. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  80. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  81. function RefsEqual(const r1, r2: treference): boolean;
  82. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  83. { returns true, if ref is a reference using only the registers passed as base and index
  84. and having an offset }
  85. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  86. {$ifdef DEBUG_AOPTCPU}
  87. const
  88. SPeepholeOptimization: shortstring = 'Peephole Optimization: ';
  89. {$else DEBUG_AOPTCPU}
  90. { Empty strings help the optimizer to remove string concatenations that won't
  91. ever appear to the user on release builds. [Kit] }
  92. const
  93. SPeepholeOptimization = '';
  94. {$endif DEBUG_AOPTCPU}
  95. implementation
  96. uses
  97. cutils,verbose,
  98. globals,
  99. cpuinfo,
  100. procinfo,
  101. aasmbase,
  102. aoptutils,
  103. symconst,symsym,
  104. cgx86,
  105. itcpugas;
  106. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  107. begin
  108. result :=
  109. (instr.typ = ait_instruction) and
  110. (taicpu(instr).opcode = op) and
  111. ((opsize = []) or (taicpu(instr).opsize in opsize));
  112. end;
  113. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  114. begin
  115. result :=
  116. (instr.typ = ait_instruction) and
  117. ((taicpu(instr).opcode = op1) or
  118. (taicpu(instr).opcode = op2)
  119. ) and
  120. ((opsize = []) or (taicpu(instr).opsize in opsize));
  121. end;
  122. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  123. begin
  124. result :=
  125. (instr.typ = ait_instruction) and
  126. ((taicpu(instr).opcode = op1) or
  127. (taicpu(instr).opcode = op2) or
  128. (taicpu(instr).opcode = op3)
  129. ) and
  130. ((opsize = []) or (taicpu(instr).opsize in opsize));
  131. end;
  132. function MatchInstruction(const instr : tai;const ops : array of TAsmOp;
  133. const opsize : topsizes) : boolean;
  134. var
  135. op : TAsmOp;
  136. begin
  137. result:=false;
  138. for op in ops do
  139. begin
  140. if (instr.typ = ait_instruction) and
  141. (taicpu(instr).opcode = op) and
  142. ((opsize = []) or (taicpu(instr).opsize in opsize)) then
  143. begin
  144. result:=true;
  145. exit;
  146. end;
  147. end;
  148. end;
  149. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  150. begin
  151. result := (oper.typ = top_reg) and (oper.reg = reg);
  152. end;
  153. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  154. begin
  155. result := (oper.typ = top_const) and (oper.val = a);
  156. end;
  157. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  158. begin
  159. result := oper1.typ = oper2.typ;
  160. if result then
  161. case oper1.typ of
  162. top_const:
  163. Result:=oper1.val = oper2.val;
  164. top_reg:
  165. Result:=oper1.reg = oper2.reg;
  166. top_ref:
  167. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  168. else
  169. internalerror(2013102801);
  170. end
  171. end;
  172. function RefsEqual(const r1, r2: treference): boolean;
  173. begin
  174. RefsEqual :=
  175. (r1.offset = r2.offset) and
  176. (r1.segment = r2.segment) and (r1.base = r2.base) and
  177. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  178. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  179. (r1.relsymbol = r2.relsymbol) and
  180. (r1.volatility=[]) and
  181. (r2.volatility=[]);
  182. end;
  183. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  184. begin
  185. Result:=(ref.offset=0) and
  186. (ref.scalefactor in [0,1]) and
  187. (ref.segment=NR_NO) and
  188. (ref.symbol=nil) and
  189. (ref.relsymbol=nil) and
  190. ((base=NR_INVALID) or
  191. (ref.base=base)) and
  192. ((index=NR_INVALID) or
  193. (ref.index=index)) and
  194. (ref.volatility=[]);
  195. end;
  196. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  197. begin
  198. Result:=(ref.scalefactor in [0,1]) and
  199. (ref.segment=NR_NO) and
  200. (ref.symbol=nil) and
  201. (ref.relsymbol=nil) and
  202. ((base=NR_INVALID) or
  203. (ref.base=base)) and
  204. ((index=NR_INVALID) or
  205. (ref.index=index)) and
  206. (ref.volatility=[]);
  207. end;
  208. function InstrReadsFlags(p: tai): boolean;
  209. begin
  210. InstrReadsFlags := true;
  211. case p.typ of
  212. ait_instruction:
  213. if InsProp[taicpu(p).opcode].Ch*
  214. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  215. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  216. Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc,Ch_All]<>[] then
  217. exit;
  218. ait_label:
  219. exit;
  220. end;
  221. InstrReadsFlags := false;
  222. end;
  223. function TX86AsmOptimizer.InstructionLoadsFromReg(const reg: TRegister;const hp: tai): boolean;
  224. begin
  225. Result:=RegReadByInstruction(reg,hp);
  226. end;
  227. function TX86AsmOptimizer.RegReadByInstruction(reg: TRegister; hp: tai): boolean;
  228. var
  229. p: taicpu;
  230. opcount: longint;
  231. begin
  232. RegReadByInstruction := false;
  233. if hp.typ <> ait_instruction then
  234. exit;
  235. p := taicpu(hp);
  236. case p.opcode of
  237. A_CALL:
  238. regreadbyinstruction := true;
  239. A_IMUL:
  240. case p.ops of
  241. 1:
  242. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  243. (
  244. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  245. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  246. );
  247. 2,3:
  248. regReadByInstruction :=
  249. reginop(reg,p.oper[0]^) or
  250. reginop(reg,p.oper[1]^);
  251. end;
  252. A_MUL:
  253. begin
  254. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  255. (
  256. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  257. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  258. );
  259. end;
  260. A_IDIV,A_DIV:
  261. begin
  262. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  263. (
  264. (getregtype(reg)=R_INTREGISTER) and
  265. (
  266. (getsupreg(reg)=RS_EAX) or ((getsupreg(reg)=RS_EDX) and (p.opsize<>S_B))
  267. )
  268. );
  269. end;
  270. else
  271. begin
  272. if (p.opcode=A_LEA) and is_segment_reg(reg) then
  273. begin
  274. RegReadByInstruction := false;
  275. exit;
  276. end;
  277. for opcount := 0 to p.ops-1 do
  278. if (p.oper[opCount]^.typ = top_ref) and
  279. RegInRef(reg,p.oper[opcount]^.ref^) then
  280. begin
  281. RegReadByInstruction := true;
  282. exit
  283. end;
  284. { special handling for SSE MOVSD }
  285. if (p.opcode=A_MOVSD) and (p.ops>0) then
  286. begin
  287. if p.ops<>2 then
  288. internalerror(2017042702);
  289. regReadByInstruction := reginop(reg,p.oper[0]^) or
  290. (
  291. (p.oper[1]^.typ=top_reg) and (p.oper[0]^.typ=top_reg) and reginop(reg, p.oper[1]^)
  292. );
  293. exit;
  294. end;
  295. with insprop[p.opcode] do
  296. begin
  297. if getregtype(reg)=R_INTREGISTER then
  298. begin
  299. case getsupreg(reg) of
  300. RS_EAX:
  301. if [Ch_REAX,Ch_RWEAX,Ch_MEAX]*Ch<>[] then
  302. begin
  303. RegReadByInstruction := true;
  304. exit
  305. end;
  306. RS_ECX:
  307. if [Ch_RECX,Ch_RWECX,Ch_MECX]*Ch<>[] then
  308. begin
  309. RegReadByInstruction := true;
  310. exit
  311. end;
  312. RS_EDX:
  313. if [Ch_REDX,Ch_RWEDX,Ch_MEDX]*Ch<>[] then
  314. begin
  315. RegReadByInstruction := true;
  316. exit
  317. end;
  318. RS_EBX:
  319. if [Ch_REBX,Ch_RWEBX,Ch_MEBX]*Ch<>[] then
  320. begin
  321. RegReadByInstruction := true;
  322. exit
  323. end;
  324. RS_ESP:
  325. if [Ch_RESP,Ch_RWESP,Ch_MESP]*Ch<>[] then
  326. begin
  327. RegReadByInstruction := true;
  328. exit
  329. end;
  330. RS_EBP:
  331. if [Ch_REBP,Ch_RWEBP,Ch_MEBP]*Ch<>[] then
  332. begin
  333. RegReadByInstruction := true;
  334. exit
  335. end;
  336. RS_ESI:
  337. if [Ch_RESI,Ch_RWESI,Ch_MESI]*Ch<>[] then
  338. begin
  339. RegReadByInstruction := true;
  340. exit
  341. end;
  342. RS_EDI:
  343. if [Ch_REDI,Ch_RWEDI,Ch_MEDI]*Ch<>[] then
  344. begin
  345. RegReadByInstruction := true;
  346. exit
  347. end;
  348. end;
  349. end;
  350. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  351. begin
  352. if (Ch_RFLAGScc in Ch) and not(getsubreg(reg) in [R_SUBW,R_SUBD,R_SUBQ]) then
  353. begin
  354. case p.condition of
  355. C_A,C_NBE, { CF=0 and ZF=0 }
  356. C_BE,C_NA: { CF=1 or ZF=1 }
  357. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY,R_SUBFLAGZERO];
  358. C_AE,C_NB,C_NC, { CF=0 }
  359. C_B,C_NAE,C_C: { CF=1 }
  360. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY];
  361. C_NE,C_NZ, { ZF=0 }
  362. C_E,C_Z: { ZF=1 }
  363. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO];
  364. C_G,C_NLE, { ZF=0 and SF=OF }
  365. C_LE,C_NG: { ZF=1 or SF<>OF }
  366. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO,R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  367. C_GE,C_NL, { SF=OF }
  368. C_L,C_NGE: { SF<>OF }
  369. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  370. C_NO, { OF=0 }
  371. C_O: { OF=1 }
  372. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGOVERFLOW];
  373. C_NP,C_PO, { PF=0 }
  374. C_P,C_PE: { PF=1 }
  375. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGPARITY];
  376. C_NS, { SF=0 }
  377. C_S: { SF=1 }
  378. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN];
  379. else
  380. internalerror(2017042701);
  381. end;
  382. if RegReadByInstruction then
  383. exit;
  384. end;
  385. case getsubreg(reg) of
  386. R_SUBW,R_SUBD,R_SUBQ:
  387. RegReadByInstruction :=
  388. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  389. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  390. Ch_RDirFlag,Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc]*Ch<>[];
  391. R_SUBFLAGCARRY:
  392. RegReadByInstruction:=[Ch_RCarryFlag,Ch_RWCarryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  393. R_SUBFLAGPARITY:
  394. RegReadByInstruction:=[Ch_RParityFlag,Ch_RWParityFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  395. R_SUBFLAGAUXILIARY:
  396. RegReadByInstruction:=[Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  397. R_SUBFLAGZERO:
  398. RegReadByInstruction:=[Ch_RZeroFlag,Ch_RWZeroFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  399. R_SUBFLAGSIGN:
  400. RegReadByInstruction:=[Ch_RSignFlag,Ch_RWSignFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  401. R_SUBFLAGOVERFLOW:
  402. RegReadByInstruction:=[Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  403. R_SUBFLAGINTERRUPT:
  404. RegReadByInstruction:=[Ch_RFlags,Ch_RWFlags]*Ch<>[];
  405. R_SUBFLAGDIRECTION:
  406. RegReadByInstruction:=[Ch_RDirFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  407. else
  408. internalerror(2017042601);
  409. end;
  410. exit;
  411. end;
  412. if (Ch_NoReadIfEqualRegs in Ch) and (p.ops=2) and
  413. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  414. (p.oper[0]^.reg=p.oper[1]^.reg) then
  415. exit;
  416. if ([CH_RWOP1,CH_ROP1,CH_MOP1]*Ch<>[]) and reginop(reg,p.oper[0]^) then
  417. begin
  418. RegReadByInstruction := true;
  419. exit
  420. end;
  421. if ([Ch_RWOP2,Ch_ROP2,Ch_MOP2]*Ch<>[]) and reginop(reg,p.oper[1]^) then
  422. begin
  423. RegReadByInstruction := true;
  424. exit
  425. end;
  426. if ([Ch_RWOP3,Ch_ROP3,Ch_MOP3]*Ch<>[]) and reginop(reg,p.oper[2]^) then
  427. begin
  428. RegReadByInstruction := true;
  429. exit
  430. end;
  431. if ([Ch_RWOP4,Ch_ROP4,Ch_MOP4]*Ch<>[]) and reginop(reg,p.oper[3]^) then
  432. begin
  433. RegReadByInstruction := true;
  434. exit
  435. end;
  436. end;
  437. end;
  438. end;
  439. end;
  440. {$ifdef DEBUG_AOPTCPU}
  441. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);
  442. begin
  443. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  444. end;
  445. function debug_tostr(i: tcgint): string; inline;
  446. begin
  447. Result := tostr(i);
  448. end;
  449. function debug_regname(r: TRegister): string; inline;
  450. begin
  451. Result := '%' + std_regname(r);
  452. end;
  453. { Debug output function - creates a string representation of an operator }
  454. function debug_operstr(oper: TOper): string;
  455. begin
  456. case oper.typ of
  457. top_const:
  458. Result := '$' + debug_tostr(oper.val);
  459. top_reg:
  460. Result := debug_regname(oper.reg);
  461. top_ref:
  462. begin
  463. if oper.ref^.offset <> 0 then
  464. Result := debug_tostr(oper.ref^.offset) + '('
  465. else
  466. Result := '(';
  467. if (oper.ref^.base <> NR_INVALID) and (oper.ref^.base <> NR_NO) then
  468. begin
  469. Result := Result + debug_regname(oper.ref^.base);
  470. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  471. Result := Result + ',' + debug_regname(oper.ref^.index);
  472. end
  473. else
  474. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  475. Result := Result + debug_regname(oper.ref^.index);
  476. if (oper.ref^.scalefactor > 1) then
  477. Result := Result + ',' + debug_tostr(oper.ref^.scalefactor) + ')'
  478. else
  479. Result := Result + ')';
  480. end;
  481. else
  482. Result := '[UNKNOWN]';
  483. end;
  484. end;
  485. function debug_op2str(opcode: tasmop): string; inline;
  486. begin
  487. Result := std_op2str[opcode];
  488. end;
  489. function debug_opsize2str(opsize: topsize): string; inline;
  490. begin
  491. Result := gas_opsize2str[opsize];
  492. end;
  493. {$else DEBUG_AOPTCPU}
  494. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  495. begin
  496. end;
  497. function debug_tostr(i: tcgint): string; inline;
  498. begin
  499. Result := '';
  500. end;
  501. function debug_regname(r: TRegister): string; inline;
  502. begin
  503. Result := '';
  504. end;
  505. function debug_operstr(oper: TOper): string; inline;
  506. begin
  507. Result := '';
  508. end;
  509. function debug_op2str(opcode: tasmop): string; inline;
  510. begin
  511. Result := '';
  512. end;
  513. function debug_opsize2str(opsize: topsize): string; inline;
  514. begin
  515. Result := '';
  516. end;
  517. {$endif DEBUG_AOPTCPU}
  518. function TX86AsmOptimizer.Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  519. begin
  520. if not SuperRegistersEqual(reg1,reg2) then
  521. exit(false);
  522. if getregtype(reg1)<>R_INTREGISTER then
  523. exit(true); {because SuperRegisterEqual is true}
  524. case getsubreg(reg1) of
  525. { A write to R_SUBL doesn't change R_SUBH and if reg2 is R_SUBW or
  526. higher, it preserves the high bits, so the new value depends on
  527. reg2's previous value. In other words, it is equivalent to doing:
  528. reg2 := (reg2 and $ffffff00) or byte(reg1); }
  529. R_SUBL:
  530. exit(getsubreg(reg2)=R_SUBL);
  531. { A write to R_SUBH doesn't change R_SUBL and if reg2 is R_SUBW or
  532. higher, it actually does a:
  533. reg2 := (reg2 and $ffff00ff) or (reg1 and $ff00); }
  534. R_SUBH:
  535. exit(getsubreg(reg2)=R_SUBH);
  536. { If reg2 is R_SUBD or larger, a write to R_SUBW preserves the high 16
  537. bits of reg2:
  538. reg2 := (reg2 and $ffff0000) or word(reg1); }
  539. R_SUBW:
  540. exit(getsubreg(reg2) in [R_SUBL,R_SUBH,R_SUBW]);
  541. { a write to R_SUBD always overwrites every other subregister,
  542. because it clears the high 32 bits of R_SUBQ on x86_64 }
  543. R_SUBD,
  544. R_SUBQ:
  545. exit(true);
  546. else
  547. internalerror(2017042801);
  548. end;
  549. end;
  550. function TX86AsmOptimizer.Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  551. begin
  552. if not SuperRegistersEqual(reg1,reg2) then
  553. exit(false);
  554. if getregtype(reg1)<>R_INTREGISTER then
  555. exit(true); {because SuperRegisterEqual is true}
  556. case getsubreg(reg1) of
  557. R_SUBL:
  558. exit(getsubreg(reg2)<>R_SUBH);
  559. R_SUBH:
  560. exit(getsubreg(reg2)<>R_SUBL);
  561. R_SUBW,
  562. R_SUBD,
  563. R_SUBQ:
  564. exit(true);
  565. else
  566. internalerror(2017042802);
  567. end;
  568. end;
  569. function TX86AsmOptimizer.PrePeepholeOptSxx(var p : tai) : boolean;
  570. var
  571. hp1 : tai;
  572. l : TCGInt;
  573. begin
  574. result:=false;
  575. { changes the code sequence
  576. shr/sar const1, x
  577. shl const2, x
  578. to
  579. either "sar/and", "shl/and" or just "and" depending on const1 and const2 }
  580. if GetNextInstruction(p, hp1) and
  581. MatchInstruction(hp1,A_SHL,[]) and
  582. (taicpu(p).oper[0]^.typ = top_const) and
  583. (taicpu(hp1).oper[0]^.typ = top_const) and
  584. (taicpu(hp1).opsize = taicpu(p).opsize) and
  585. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[1]^.typ) and
  586. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) then
  587. begin
  588. if (taicpu(p).oper[0]^.val > taicpu(hp1).oper[0]^.val) and
  589. not(cs_opt_size in current_settings.optimizerswitches) then
  590. begin
  591. { shr/sar const1, %reg
  592. shl const2, %reg
  593. with const1 > const2 }
  594. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  595. taicpu(hp1).opcode := A_AND;
  596. l := (1 shl (taicpu(hp1).oper[0]^.val)) - 1;
  597. case taicpu(p).opsize Of
  598. S_B: taicpu(hp1).loadConst(0,l Xor $ff);
  599. S_W: taicpu(hp1).loadConst(0,l Xor $ffff);
  600. S_L: taicpu(hp1).loadConst(0,l Xor aint($ffffffff));
  601. S_Q: taicpu(hp1).loadConst(0,l Xor aint($ffffffffffffffff));
  602. else
  603. Internalerror(2017050703)
  604. end;
  605. end
  606. else if (taicpu(p).oper[0]^.val<taicpu(hp1).oper[0]^.val) and
  607. not(cs_opt_size in current_settings.optimizerswitches) then
  608. begin
  609. { shr/sar const1, %reg
  610. shl const2, %reg
  611. with const1 < const2 }
  612. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val-taicpu(p).oper[0]^.val);
  613. taicpu(p).opcode := A_AND;
  614. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  615. case taicpu(p).opsize Of
  616. S_B: taicpu(p).loadConst(0,l Xor $ff);
  617. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  618. S_L: taicpu(p).loadConst(0,l Xor aint($ffffffff));
  619. S_Q: taicpu(p).loadConst(0,l Xor aint($ffffffffffffffff));
  620. else
  621. Internalerror(2017050702)
  622. end;
  623. end
  624. else if (taicpu(p).oper[0]^.val = taicpu(hp1).oper[0]^.val) then
  625. begin
  626. { shr/sar const1, %reg
  627. shl const2, %reg
  628. with const1 = const2 }
  629. taicpu(p).opcode := A_AND;
  630. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  631. case taicpu(p).opsize Of
  632. S_B: taicpu(p).loadConst(0,l Xor $ff);
  633. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  634. S_L: taicpu(p).loadConst(0,l Xor aint($ffffffff));
  635. S_Q: taicpu(p).loadConst(0,l Xor aint($ffffffffffffffff));
  636. else
  637. Internalerror(2017050701)
  638. end;
  639. asml.remove(hp1);
  640. hp1.free;
  641. end;
  642. end;
  643. end;
  644. function TX86AsmOptimizer.PrePeepholeOptIMUL(var p : tai) : boolean;
  645. var
  646. opsize : topsize;
  647. hp1 : tai;
  648. tmpref : treference;
  649. ShiftValue : Cardinal;
  650. BaseValue : TCGInt;
  651. begin
  652. result:=false;
  653. opsize:=taicpu(p).opsize;
  654. { changes certain "imul const, %reg"'s to lea sequences }
  655. if (MatchOpType(taicpu(p),top_const,top_reg) or
  656. MatchOpType(taicpu(p),top_const,top_reg,top_reg)) and
  657. (opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) then
  658. if (taicpu(p).oper[0]^.val = 1) then
  659. if (taicpu(p).ops = 2) then
  660. { remove "imul $1, reg" }
  661. begin
  662. hp1 := tai(p.Next);
  663. asml.remove(p);
  664. DebugMsg(SPeepholeOptimization + 'Imul2Nop done',p);
  665. p.free;
  666. p := hp1;
  667. result:=true;
  668. end
  669. else
  670. { change "imul $1, reg1, reg2" to "mov reg1, reg2" }
  671. begin
  672. hp1 := taicpu.Op_Reg_Reg(A_MOV, opsize, taicpu(p).oper[1]^.reg,taicpu(p).oper[2]^.reg);
  673. InsertLLItem(p.previous, p.next, hp1);
  674. DebugMsg(SPeepholeOptimization + 'Imul2Mov done',p);
  675. p.free;
  676. p := hp1;
  677. end
  678. else if
  679. ((taicpu(p).ops <= 2) or
  680. (taicpu(p).oper[2]^.typ = Top_Reg)) and
  681. not(cs_opt_size in current_settings.optimizerswitches) and
  682. (not(GetNextInstruction(p, hp1)) or
  683. not((tai(hp1).typ = ait_instruction) and
  684. ((taicpu(hp1).opcode=A_Jcc) and
  685. (taicpu(hp1).condition in [C_O,C_NO])))) then
  686. begin
  687. {
  688. imul X, reg1, reg2 to
  689. lea (reg1,reg1,Y), reg2
  690. shl ZZ,reg2
  691. imul XX, reg1 to
  692. lea (reg1,reg1,YY), reg1
  693. shl ZZ,reg2
  694. This optimziation makes sense for pretty much every x86, except the VIA Nano3000: it has IMUL latency 2, lea/shl pair as well,
  695. it does not exist as a separate optimization target in FPC though.
  696. This optimziation can be applied as long as only two bits are set in the constant and those two bits are separated by
  697. at most two zeros
  698. }
  699. reference_reset(tmpref,1,[]);
  700. if (PopCnt(QWord(taicpu(p).oper[0]^.val))=2) and (BsrQWord(taicpu(p).oper[0]^.val)-BsfQWord(taicpu(p).oper[0]^.val)<=3) then
  701. begin
  702. ShiftValue:=BsfQWord(taicpu(p).oper[0]^.val);
  703. BaseValue:=taicpu(p).oper[0]^.val shr ShiftValue;
  704. TmpRef.base := taicpu(p).oper[1]^.reg;
  705. TmpRef.index := taicpu(p).oper[1]^.reg;
  706. if not(BaseValue in [3,5,9]) then
  707. Internalerror(2018110101);
  708. TmpRef.ScaleFactor := BaseValue-1;
  709. if (taicpu(p).ops = 2) then
  710. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[1]^.reg)
  711. else
  712. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[2]^.reg);
  713. AsmL.InsertAfter(hp1,p);
  714. DebugMsg(SPeepholeOptimization + 'Imul2LeaShl done',p);
  715. AsmL.Remove(p);
  716. taicpu(hp1).fileinfo:=taicpu(p).fileinfo;
  717. p.free;
  718. p := hp1;
  719. if ShiftValue>0 then
  720. AsmL.InsertAfter(taicpu.op_const_reg(A_SHL, opsize, ShiftValue, taicpu(hp1).oper[1]^.reg),hp1);
  721. end;
  722. end;
  723. end;
  724. function TX86AsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  725. var
  726. p: taicpu;
  727. begin
  728. if not assigned(hp) or
  729. (hp.typ <> ait_instruction) then
  730. begin
  731. Result := false;
  732. exit;
  733. end;
  734. p := taicpu(hp);
  735. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  736. with insprop[p.opcode] do
  737. begin
  738. case getsubreg(reg) of
  739. R_SUBW,R_SUBD,R_SUBQ:
  740. Result:=
  741. RegLoadedWithNewValue(NR_CARRYFLAG,hp) and
  742. RegLoadedWithNewValue(NR_PARITYFLAG,hp) and
  743. RegLoadedWithNewValue(NR_AUXILIARYFLAG,hp) and
  744. RegLoadedWithNewValue(NR_ZEROFLAG,hp) and
  745. RegLoadedWithNewValue(NR_SIGNFLAG,hp) and
  746. RegLoadedWithNewValue(NR_OVERFLOWFLAG,hp);
  747. R_SUBFLAGCARRY:
  748. Result:=[Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag,Ch_WFlags]*Ch<>[];
  749. R_SUBFLAGPARITY:
  750. Result:=[Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag,Ch_WFlags]*Ch<>[];
  751. R_SUBFLAGAUXILIARY:
  752. Result:=[Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag,Ch_WFlags]*Ch<>[];
  753. R_SUBFLAGZERO:
  754. Result:=[Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag,Ch_WFlags]*Ch<>[];
  755. R_SUBFLAGSIGN:
  756. Result:=[Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag,Ch_WFlags]*Ch<>[];
  757. R_SUBFLAGOVERFLOW:
  758. Result:=[Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag,Ch_WFlags]*Ch<>[];
  759. R_SUBFLAGINTERRUPT:
  760. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*Ch<>[];
  761. R_SUBFLAGDIRECTION:
  762. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*Ch<>[];
  763. else
  764. begin
  765. writeln(getsubreg(reg));
  766. internalerror(2017050501);
  767. end;
  768. end;
  769. exit;
  770. end;
  771. Result :=
  772. (((p.opcode = A_MOV) or
  773. (p.opcode = A_MOVZX) or
  774. (p.opcode = A_MOVSX) or
  775. (p.opcode = A_LEA) or
  776. (p.opcode = A_VMOVSS) or
  777. (p.opcode = A_VMOVSD) or
  778. (p.opcode = A_VMOVAPD) or
  779. (p.opcode = A_VMOVAPS) or
  780. (p.opcode = A_VMOVQ) or
  781. (p.opcode = A_MOVSS) or
  782. (p.opcode = A_MOVSD) or
  783. (p.opcode = A_MOVQ) or
  784. (p.opcode = A_MOVAPD) or
  785. (p.opcode = A_MOVAPS) or
  786. {$ifndef x86_64}
  787. (p.opcode = A_LDS) or
  788. (p.opcode = A_LES) or
  789. {$endif not x86_64}
  790. (p.opcode = A_LFS) or
  791. (p.opcode = A_LGS) or
  792. (p.opcode = A_LSS)) and
  793. (p.ops=2) and { A_MOVSD can have zero operands, so this check is needed }
  794. (p.oper[1]^.typ = top_reg) and
  795. (Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg)) and
  796. ((p.oper[0]^.typ = top_const) or
  797. ((p.oper[0]^.typ = top_reg) and
  798. not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  799. ((p.oper[0]^.typ = top_ref) and
  800. not RegInRef(reg,p.oper[0]^.ref^)))) or
  801. ((p.opcode = A_POP) and
  802. (Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg))) or
  803. ((p.opcode = A_IMUL) and
  804. (p.ops=3) and
  805. (Reg1WriteOverwritesReg2Entirely(p.oper[2]^.reg,reg)) and
  806. (((p.oper[1]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg))) or
  807. ((p.oper[1]^.typ=top_ref) and not(RegInRef(reg,p.oper[1]^.ref^))))) or
  808. ((((p.opcode = A_IMUL) or
  809. (p.opcode = A_MUL)) and
  810. (p.ops=1)) and
  811. (((p.oper[0]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  812. ((p.oper[0]^.typ=top_ref) and not(RegInRef(reg,p.oper[0]^.ref^)))) and
  813. (((p.opsize=S_B) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  814. ((p.opsize=S_W) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  815. ((p.opsize=S_L) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg))
  816. {$ifdef x86_64}
  817. or ((p.opsize=S_Q) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg))
  818. {$endif x86_64}
  819. )) or
  820. ((p.opcode = A_CWD) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  821. ((p.opcode = A_CDQ) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg)) or
  822. {$ifdef x86_64}
  823. ((p.opcode = A_CQO) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg)) or
  824. {$endif x86_64}
  825. ((p.opcode = A_CBW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  826. {$ifndef x86_64}
  827. ((p.opcode = A_LDS) and (reg=NR_DS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  828. ((p.opcode = A_LES) and (reg=NR_ES) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  829. {$endif not x86_64}
  830. ((p.opcode = A_LFS) and (reg=NR_FS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  831. ((p.opcode = A_LGS) and (reg=NR_GS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  832. ((p.opcode = A_LSS) and (reg=NR_SS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  833. {$ifndef x86_64}
  834. ((p.opcode = A_AAM) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  835. {$endif not x86_64}
  836. ((p.opcode = A_LAHF) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  837. ((p.opcode = A_LODSB) and Reg1WriteOverwritesReg2Entirely(NR_AL,reg)) or
  838. ((p.opcode = A_LODSW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg)) or
  839. ((p.opcode = A_LODSD) and Reg1WriteOverwritesReg2Entirely(NR_EAX,reg)) or
  840. {$ifdef x86_64}
  841. ((p.opcode = A_LODSQ) and Reg1WriteOverwritesReg2Entirely(NR_RAX,reg)) or
  842. {$endif x86_64}
  843. ((p.opcode = A_SETcc) and (p.oper[0]^.typ=top_reg) and Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  844. (((p.opcode = A_FSTSW) or
  845. (p.opcode = A_FNSTSW)) and
  846. (p.oper[0]^.typ=top_reg) and
  847. Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  848. (((p.opcode = A_XOR) or (p.opcode = A_SUB) or (p.opcode = A_SBB)) and
  849. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  850. (p.oper[0]^.reg=p.oper[1]^.reg) and
  851. Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg));
  852. end;
  853. class function TX86AsmOptimizer.IsExitCode(p : tai) : boolean;
  854. var
  855. hp2,hp3 : tai;
  856. begin
  857. { some x86-64 issue a NOP before the real exit code }
  858. if MatchInstruction(p,A_NOP,[]) then
  859. GetNextInstruction(p,p);
  860. result:=assigned(p) and (p.typ=ait_instruction) and
  861. ((taicpu(p).opcode = A_RET) or
  862. ((taicpu(p).opcode=A_LEAVE) and
  863. GetNextInstruction(p,hp2) and
  864. MatchInstruction(hp2,A_RET,[S_NO])
  865. ) or
  866. ((((taicpu(p).opcode=A_MOV) and
  867. MatchOpType(taicpu(p),top_reg,top_reg) and
  868. (taicpu(p).oper[0]^.reg=current_procinfo.framepointer) and
  869. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)) or
  870. ((taicpu(p).opcode=A_LEA) and
  871. MatchOpType(taicpu(p),top_ref,top_reg) and
  872. (taicpu(p).oper[0]^.ref^.base=current_procinfo.framepointer) and
  873. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  874. )
  875. ) and
  876. GetNextInstruction(p,hp2) and
  877. MatchInstruction(hp2,A_POP,[reg2opsize(current_procinfo.framepointer)]) and
  878. MatchOpType(taicpu(hp2),top_reg) and
  879. (taicpu(hp2).oper[0]^.reg=current_procinfo.framepointer) and
  880. GetNextInstruction(hp2,hp3) and
  881. MatchInstruction(hp3,A_RET,[S_NO])
  882. )
  883. );
  884. end;
  885. class function TX86AsmOptimizer.isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  886. begin
  887. isFoldableArithOp := False;
  888. case hp1.opcode of
  889. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  890. isFoldableArithOp :=
  891. ((taicpu(hp1).oper[0]^.typ = top_const) or
  892. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  893. (taicpu(hp1).oper[0]^.reg <> reg))) and
  894. (taicpu(hp1).oper[1]^.typ = top_reg) and
  895. (taicpu(hp1).oper[1]^.reg = reg);
  896. A_INC,A_DEC,A_NEG,A_NOT:
  897. isFoldableArithOp :=
  898. (taicpu(hp1).oper[0]^.typ = top_reg) and
  899. (taicpu(hp1).oper[0]^.reg = reg);
  900. end;
  901. end;
  902. procedure TX86AsmOptimizer.RemoveLastDeallocForFuncRes(p: tai);
  903. procedure DoRemoveLastDeallocForFuncRes( supreg: tsuperregister);
  904. var
  905. hp2: tai;
  906. begin
  907. hp2 := p;
  908. repeat
  909. hp2 := tai(hp2.previous);
  910. if assigned(hp2) and
  911. (hp2.typ = ait_regalloc) and
  912. (tai_regalloc(hp2).ratype=ra_dealloc) and
  913. (getregtype(tai_regalloc(hp2).reg) = R_INTREGISTER) and
  914. (getsupreg(tai_regalloc(hp2).reg) = supreg) then
  915. begin
  916. asml.remove(hp2);
  917. hp2.free;
  918. break;
  919. end;
  920. until not(assigned(hp2)) or regInInstruction(newreg(R_INTREGISTER,supreg,R_SUBWHOLE),hp2);
  921. end;
  922. begin
  923. case current_procinfo.procdef.returndef.typ of
  924. arraydef,recorddef,pointerdef,
  925. stringdef,enumdef,procdef,objectdef,errordef,
  926. filedef,setdef,procvardef,
  927. classrefdef,forwarddef:
  928. DoRemoveLastDeallocForFuncRes(RS_EAX);
  929. orddef:
  930. if current_procinfo.procdef.returndef.size <> 0 then
  931. begin
  932. DoRemoveLastDeallocForFuncRes(RS_EAX);
  933. { for int64/qword }
  934. if current_procinfo.procdef.returndef.size = 8 then
  935. DoRemoveLastDeallocForFuncRes(RS_EDX);
  936. end;
  937. end;
  938. end;
  939. function TX86AsmOptimizer.OptPass1MOVAP(var p : tai) : boolean;
  940. var
  941. hp1,hp2 : tai;
  942. begin
  943. result:=false;
  944. if MatchOpType(taicpu(p),top_reg,top_reg) and
  945. GetNextInstruction(p, hp1) and
  946. (hp1.typ = ait_instruction) and
  947. GetNextInstruction(hp1, hp2) and
  948. MatchInstruction(hp2,taicpu(p).opcode,[]) and
  949. OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  950. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  951. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  952. (((taicpu(p).opcode=A_MOVAPS) and
  953. ((taicpu(hp1).opcode=A_ADDSS) or (taicpu(hp1).opcode=A_SUBSS) or
  954. (taicpu(hp1).opcode=A_MULSS) or (taicpu(hp1).opcode=A_DIVSS))) or
  955. ((taicpu(p).opcode=A_MOVAPD) and
  956. ((taicpu(hp1).opcode=A_ADDSD) or (taicpu(hp1).opcode=A_SUBSD) or
  957. (taicpu(hp1).opcode=A_MULSD) or (taicpu(hp1).opcode=A_DIVSD)))
  958. ) then
  959. { change
  960. movapX reg,reg2
  961. addsX/subsX/... reg3, reg2
  962. movapX reg2,reg
  963. to
  964. addsX/subsX/... reg3,reg
  965. }
  966. begin
  967. TransferUsedRegs(TmpUsedRegs);
  968. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  969. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  970. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  971. begin
  972. DebugMsg(SPeepholeOptimization + 'MovapXOpMovapX2Op ('+
  973. debug_op2str(taicpu(p).opcode)+' '+
  974. debug_op2str(taicpu(hp1).opcode)+' '+
  975. debug_op2str(taicpu(hp2).opcode)+') done',p);
  976. { we cannot eliminate the first move if
  977. the operations uses the same register for source and dest }
  978. if not(OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp1).oper[0]^)) then
  979. begin
  980. asml.remove(p);
  981. p.Free;
  982. end;
  983. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  984. asml.remove(hp2);
  985. hp2.Free;
  986. p:=hp1;
  987. result:=true;
  988. end;
  989. end
  990. end;
  991. function TX86AsmOptimizer.OptPass1VMOVAP(var p : tai) : boolean;
  992. var
  993. hp1,hp2 : tai;
  994. begin
  995. result:=false;
  996. if MatchOpType(taicpu(p),top_reg,top_reg) then
  997. begin
  998. { vmova* reg1,reg1
  999. =>
  1000. <nop> }
  1001. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  1002. begin
  1003. GetNextInstruction(p,hp1);
  1004. asml.Remove(p);
  1005. p.Free;
  1006. p:=hp1;
  1007. result:=true;
  1008. end
  1009. else if GetNextInstruction(p,hp1) then
  1010. begin
  1011. if MatchInstruction(hp1,[taicpu(p).opcode],[S_NO]) and
  1012. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  1013. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1014. begin
  1015. { vmova* reg1,reg2
  1016. vmova* reg2,reg3
  1017. dealloc reg2
  1018. =>
  1019. vmova* reg1,reg3 }
  1020. TransferUsedRegs(TmpUsedRegs);
  1021. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1022. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1023. begin
  1024. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1025. asml.Remove(hp1);
  1026. hp1.Free;
  1027. result:=true;
  1028. end
  1029. { special case:
  1030. vmova* reg1,reg2
  1031. vmova* reg2,reg1
  1032. =>
  1033. vmova* reg1,reg2 }
  1034. else if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) then
  1035. begin
  1036. asml.Remove(hp1);
  1037. hp1.Free;
  1038. result:=true;
  1039. end
  1040. end
  1041. else if MatchInstruction(hp1,[A_VFMADD132PD,A_VFNMADD231SD,A_VFMADD231SD],[S_NO]) and
  1042. { we mix single and double opperations here because we assume that the compiler
  1043. generates vmovapd only after double operations and vmovaps only after single operations }
  1044. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[2]^) and
  1045. GetNextInstruction(hp1,hp2) and
  1046. MatchInstruction(hp2,A_VMOVAPD,A_VMOVAPS,[S_NO]) and
  1047. MatchOperand(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) then
  1048. begin
  1049. TransferUsedRegs(TmpUsedRegs);
  1050. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1051. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1052. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs))
  1053. then
  1054. begin
  1055. taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
  1056. asml.Remove(p);
  1057. p.Free;
  1058. asml.Remove(hp2);
  1059. hp2.Free;
  1060. p:=hp1;
  1061. end;
  1062. end;
  1063. end;
  1064. end;
  1065. end;
  1066. function TX86AsmOptimizer.OptPass1VOP(var p : tai) : boolean;
  1067. var
  1068. hp1 : tai;
  1069. begin
  1070. result:=false;
  1071. { replace
  1072. V<Op>X %mreg1,%mreg2,%mreg3
  1073. VMovX %mreg3,%mreg4
  1074. dealloc %mreg3
  1075. by
  1076. V<Op>X %mreg1,%mreg2,%mreg4
  1077. ?
  1078. }
  1079. if GetNextInstruction(p,hp1) and
  1080. { we mix single and double operations here because we assume that the compiler
  1081. generates vmovapd only after double operations and vmovaps only after single operations }
  1082. MatchInstruction(hp1,A_VMOVAPD,A_VMOVAPS,[S_NO]) and
  1083. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  1084. (taicpu(hp1).oper[1]^.typ=top_reg) then
  1085. begin
  1086. TransferUsedRegs(TmpUsedRegs);
  1087. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1088. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)
  1089. ) then
  1090. begin
  1091. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  1092. DebugMsg(SPeepholeOptimization + 'VOpVmov2VOp done',p);
  1093. asml.Remove(hp1);
  1094. hp1.Free;
  1095. result:=true;
  1096. end;
  1097. end;
  1098. end;
  1099. function TX86AsmOptimizer.OptPass1MOV(var p : tai) : boolean;
  1100. var
  1101. hp1, hp2: tai;
  1102. GetNextInstruction_p: Boolean;
  1103. PreMessage, RegName1, RegName2, InputVal, MaskNum: string;
  1104. NewSize: topsize;
  1105. begin
  1106. Result:=false;
  1107. GetNextInstruction_p:=GetNextInstruction(p, hp1);
  1108. { remove mov reg1,reg1? }
  1109. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^)
  1110. then
  1111. begin
  1112. DebugMsg(SPeepholeOptimization + 'Mov2Nop done',p);
  1113. { take care of the register (de)allocs following p }
  1114. UpdateUsedRegs(tai(p.next));
  1115. asml.remove(p);
  1116. p.free;
  1117. p:=hp1;
  1118. Result:=true;
  1119. exit;
  1120. end;
  1121. if GetNextInstruction_p and
  1122. MatchInstruction(hp1,A_AND,[]) and
  1123. (taicpu(p).oper[1]^.typ = top_reg) and
  1124. MatchOpType(taicpu(hp1),top_const,top_reg) then
  1125. begin
  1126. if MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) then
  1127. begin
  1128. case taicpu(p).opsize of
  1129. S_L:
  1130. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  1131. begin
  1132. { Optimize out:
  1133. mov x, %reg
  1134. and ffffffffh, %reg
  1135. }
  1136. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 1 done',p);
  1137. asml.remove(hp1);
  1138. hp1.free;
  1139. Result:=true;
  1140. exit;
  1141. end;
  1142. S_Q: { TODO: Confirm if this is even possible }
  1143. if (taicpu(hp1).oper[0]^.val = $ffffffffffffffff) then
  1144. begin
  1145. { Optimize out:
  1146. mov x, %reg
  1147. and ffffffffffffffffh, %reg
  1148. }
  1149. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 2 done',p);
  1150. asml.remove(hp1);
  1151. hp1.free;
  1152. Result:=true;
  1153. exit;
  1154. end;
  1155. end;
  1156. end
  1157. else if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(hp1).oper[1]^.typ = top_reg) and
  1158. (taicpu(p).oper[0]^.typ <> top_const) and { MOVZX only supports registers and memory, not immediates (use MOV for that!) }
  1159. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  1160. then
  1161. begin
  1162. InputVal := debug_operstr(taicpu(p).oper[0]^);
  1163. MaskNum := debug_tostr(taicpu(hp1).oper[0]^.val);
  1164. case taicpu(p).opsize of
  1165. S_B:
  1166. if (taicpu(hp1).oper[0]^.val = $ff) then
  1167. begin
  1168. { Convert:
  1169. movb x, %regl movb x, %regl
  1170. andw ffh, %regw andl ffh, %regd
  1171. To:
  1172. movzbw x, %regd movzbl x, %regd
  1173. (Identical registers, just different sizes)
  1174. }
  1175. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 8-bit register name }
  1176. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 16/32-bit register name }
  1177. case taicpu(hp1).opsize of
  1178. S_W: NewSize := S_BW;
  1179. S_L: NewSize := S_BL;
  1180. {$ifdef x86_64}
  1181. S_Q: NewSize := S_BQ;
  1182. {$endif x86_64}
  1183. else
  1184. InternalError(2018011510);
  1185. end;
  1186. end
  1187. else
  1188. NewSize := S_NO;
  1189. S_W:
  1190. if (taicpu(hp1).oper[0]^.val = $ffff) then
  1191. begin
  1192. { Convert:
  1193. movw x, %regw
  1194. andl ffffh, %regd
  1195. To:
  1196. movzwl x, %regd
  1197. (Identical registers, just different sizes)
  1198. }
  1199. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 16-bit register name }
  1200. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 32-bit register name }
  1201. case taicpu(hp1).opsize of
  1202. S_L: NewSize := S_WL;
  1203. {$ifdef x86_64}
  1204. S_Q: NewSize := S_WQ;
  1205. {$endif x86_64}
  1206. else
  1207. InternalError(2018011511);
  1208. end;
  1209. end
  1210. else
  1211. NewSize := S_NO;
  1212. else
  1213. NewSize := S_NO;
  1214. end;
  1215. if NewSize <> S_NO then
  1216. begin
  1217. PreMessage := 'mov' + debug_opsize2str(taicpu(p).opsize) + ' ' + InputVal + ',' + RegName1;
  1218. { The actual optimization }
  1219. taicpu(p).opcode := A_MOVZX;
  1220. taicpu(p).changeopsize(NewSize);
  1221. taicpu(p).oper[1]^ := taicpu(hp1).oper[1]^;
  1222. { Safeguard if "and" is followed by a conditional command }
  1223. TransferUsedRegs(TmpUsedRegs);
  1224. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  1225. if (RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  1226. begin
  1227. { At this point, the "and" command is effectively equivalent to
  1228. "test %reg,%reg". This will be handled separately by the
  1229. Peephole Optimizer. [Kit] }
  1230. DebugMsg(SPeepholeOptimization + PreMessage +
  1231. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  1232. end
  1233. else
  1234. begin
  1235. DebugMsg(SPeepholeOptimization + PreMessage + '; and' + debug_opsize2str(taicpu(hp1).opsize) + ' $' + MaskNum + ',' + RegName2 +
  1236. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  1237. asml.Remove(hp1);
  1238. hp1.Free;
  1239. end;
  1240. Result := True;
  1241. Exit;
  1242. end;
  1243. end;
  1244. end
  1245. else if GetNextInstruction_p and
  1246. MatchInstruction(hp1,A_MOV,[]) and
  1247. (taicpu(p).oper[1]^.typ = top_reg) and
  1248. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1249. begin
  1250. TransferUsedRegs(TmpUsedRegs);
  1251. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  1252. { we have
  1253. mov x, %treg
  1254. mov %treg, y
  1255. }
  1256. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^)) and
  1257. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  1258. { we've got
  1259. mov x, %treg
  1260. mov %treg, y
  1261. with %treg is not used after }
  1262. case taicpu(p).oper[0]^.typ Of
  1263. top_reg:
  1264. begin
  1265. { change
  1266. mov %reg, %treg
  1267. mov %treg, y
  1268. to
  1269. mov %reg, y
  1270. }
  1271. if taicpu(hp1).oper[1]^.typ=top_reg then
  1272. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1273. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  1274. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 2 done',p);
  1275. asml.remove(hp1);
  1276. hp1.free;
  1277. Result:=true;
  1278. Exit;
  1279. end;
  1280. top_const:
  1281. begin
  1282. { change
  1283. mov const, %treg
  1284. mov %treg, y
  1285. to
  1286. mov const, y
  1287. }
  1288. if (taicpu(hp1).oper[1]^.typ=top_reg) or
  1289. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  1290. begin
  1291. if taicpu(hp1).oper[1]^.typ=top_reg then
  1292. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1293. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  1294. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 5 done',p);
  1295. asml.remove(hp1);
  1296. hp1.free;
  1297. Result:=true;
  1298. Exit;
  1299. end;
  1300. end;
  1301. top_ref:
  1302. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  1303. begin
  1304. { change
  1305. mov mem, %treg
  1306. mov %treg, %reg
  1307. to
  1308. mov mem, %reg"
  1309. }
  1310. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1311. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3 done',p);
  1312. asml.remove(hp1);
  1313. hp1.free;
  1314. Result:=true;
  1315. Exit;
  1316. end;
  1317. end;
  1318. end
  1319. else
  1320. { Change
  1321. mov %reg1, %reg2
  1322. xxx %reg2, ???
  1323. to
  1324. mov %reg1, %reg2
  1325. xxx %reg1, ???
  1326. to avoid a write/read penalty
  1327. }
  1328. if MatchOpType(taicpu(p),top_reg,top_reg) and
  1329. GetNextInstruction(p,hp1) and
  1330. (tai(hp1).typ = ait_instruction) and
  1331. (taicpu(hp1).ops >= 1) and
  1332. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1333. { we have
  1334. mov %reg1, %reg2
  1335. XXX %reg2, ???
  1336. }
  1337. begin
  1338. if ((taicpu(hp1).opcode = A_OR) or
  1339. (taicpu(hp1).opcode = A_AND) or
  1340. (taicpu(hp1).opcode = A_TEST)) and
  1341. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1342. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) then
  1343. { we have
  1344. mov %reg1, %reg2
  1345. test/or/and %reg2, %reg2
  1346. }
  1347. begin
  1348. TransferUsedRegs(TmpUsedRegs);
  1349. { reg1 will be used after the first instruction,
  1350. so update the allocation info }
  1351. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1352. if GetNextInstruction(hp1, hp2) and
  1353. (hp2.typ = ait_instruction) and
  1354. taicpu(hp2).is_jmp and
  1355. not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, hp1, TmpUsedRegs)) then
  1356. { change
  1357. mov %reg1, %reg2
  1358. test/or/and %reg2, %reg2
  1359. jxx
  1360. to
  1361. test %reg1, %reg1
  1362. jxx
  1363. }
  1364. begin
  1365. taicpu(hp1).loadoper(0,taicpu(p).oper[0]^);
  1366. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  1367. DebugMsg(SPeepholeOptimization + 'MovTestJxx2TestMov done',p);
  1368. asml.remove(p);
  1369. p.free;
  1370. p := hp1;
  1371. Exit;
  1372. end
  1373. else
  1374. { change
  1375. mov %reg1, %reg2
  1376. test/or/and %reg2, %reg2
  1377. to
  1378. mov %reg1, %reg2
  1379. test/or/and %reg1, %reg1
  1380. }
  1381. begin
  1382. taicpu(hp1).loadoper(0,taicpu(p).oper[0]^);
  1383. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  1384. DebugMsg(SPeepholeOptimization + 'MovTestJxx2MovTestJxx done',p);
  1385. end;
  1386. end
  1387. end
  1388. else
  1389. { leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  1390. x >= RetOffset) as it doesn't do anything (it writes either to a
  1391. parameter or to the temporary storage room for the function
  1392. result)
  1393. }
  1394. if GetNextInstruction_p and
  1395. (tai(hp1).typ = ait_instruction) then
  1396. begin
  1397. if IsExitCode(hp1) and
  1398. MatchOpType(taicpu(p),top_reg,top_ref) and
  1399. (taicpu(p).oper[1]^.ref^.base = current_procinfo.FramePointer) and
  1400. not(assigned(current_procinfo.procdef.funcretsym) and
  1401. (taicpu(p).oper[1]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  1402. (taicpu(p).oper[1]^.ref^.index = NR_NO) then
  1403. begin
  1404. asml.remove(p);
  1405. p.free;
  1406. p:=hp1;
  1407. DebugMsg(SPeepholeOptimization + 'removed deadstore before leave/ret',p);
  1408. RemoveLastDeallocForFuncRes(p);
  1409. exit;
  1410. end
  1411. { change
  1412. mov reg1, mem1
  1413. test/cmp x, mem1
  1414. to
  1415. mov reg1, mem1
  1416. test/cmp x, reg1
  1417. }
  1418. else if MatchOpType(taicpu(p),top_reg,top_ref) and
  1419. MatchInstruction(hp1,A_CMP,A_TEST,[taicpu(p).opsize]) and
  1420. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1421. RefsEqual(taicpu(p).oper[1]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  1422. begin
  1423. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  1424. DebugMsg(SPeepholeOptimization + 'MovTestCmp2MovTestCmp 1',hp1);
  1425. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1426. end;
  1427. end;
  1428. { Next instruction is also a MOV ? }
  1429. if GetNextInstruction_p and
  1430. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) then
  1431. begin
  1432. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  1433. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  1434. { mov reg1, mem1 or mov mem1, reg1
  1435. mov mem2, reg2 mov reg2, mem2}
  1436. begin
  1437. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  1438. { mov reg1, mem1 or mov mem1, reg1
  1439. mov mem2, reg1 mov reg2, mem1}
  1440. begin
  1441. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  1442. { Removes the second statement from
  1443. mov reg1, mem1/reg2
  1444. mov mem1/reg2, reg1 }
  1445. begin
  1446. if taicpu(p).oper[0]^.typ=top_reg then
  1447. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1448. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 1',p);
  1449. asml.remove(hp1);
  1450. hp1.free;
  1451. Result:=true;
  1452. exit;
  1453. end
  1454. else
  1455. begin
  1456. TransferUsedRegs(TmpUsedRegs);
  1457. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1458. if (taicpu(p).oper[1]^.typ = top_ref) and
  1459. { mov reg1, mem1
  1460. mov mem2, reg1 }
  1461. (taicpu(hp1).oper[0]^.ref^.refaddr = addr_no) and
  1462. GetNextInstruction(hp1, hp2) and
  1463. MatchInstruction(hp2,A_CMP,[taicpu(p).opsize]) and
  1464. OpsEqual(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  1465. OpsEqual(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) and
  1466. not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs)) then
  1467. { change to
  1468. mov reg1, mem1 mov reg1, mem1
  1469. mov mem2, reg1 cmp reg1, mem2
  1470. cmp mem1, reg1
  1471. }
  1472. begin
  1473. asml.remove(hp2);
  1474. hp2.free;
  1475. taicpu(hp1).opcode := A_CMP;
  1476. taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
  1477. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  1478. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  1479. DebugMsg(SPeepholeOptimization + 'MovMovCmp2MovCmp done',hp1);
  1480. end;
  1481. end;
  1482. end
  1483. else if (taicpu(p).oper[1]^.typ=top_ref) and
  1484. OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  1485. begin
  1486. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  1487. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  1488. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov1 done',p);
  1489. end
  1490. else
  1491. begin
  1492. TransferUsedRegs(TmpUsedRegs);
  1493. if GetNextInstruction(hp1, hp2) and
  1494. MatchOpType(taicpu(p),top_ref,top_reg) and
  1495. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  1496. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1497. MatchInstruction(hp2,A_MOV,[taicpu(p).opsize]) and
  1498. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  1499. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  1500. if not RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^) and
  1501. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,tmpUsedRegs)) then
  1502. { mov mem1, %reg1
  1503. mov %reg1, mem2
  1504. mov mem2, reg2
  1505. to:
  1506. mov mem1, reg2
  1507. mov reg2, mem2}
  1508. begin
  1509. AllocRegBetween(taicpu(hp2).oper[1]^.reg,p,hp2,usedregs);
  1510. DebugMsg(SPeepholeOptimization + 'MovMovMov2MovMov 1 done',p);
  1511. taicpu(p).loadoper(1,taicpu(hp2).oper[1]^);
  1512. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  1513. asml.remove(hp2);
  1514. hp2.free;
  1515. end
  1516. {$ifdef i386}
  1517. { this is enabled for i386 only, as the rules to create the reg sets below
  1518. are too complicated for x86-64, so this makes this code too error prone
  1519. on x86-64
  1520. }
  1521. else if (taicpu(p).oper[1]^.reg <> taicpu(hp2).oper[1]^.reg) and
  1522. not(RegInRef(taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.ref^)) and
  1523. not(RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^)) then
  1524. { mov mem1, reg1 mov mem1, reg1
  1525. mov reg1, mem2 mov reg1, mem2
  1526. mov mem2, reg2 mov mem2, reg1
  1527. to: to:
  1528. mov mem1, reg1 mov mem1, reg1
  1529. mov mem1, reg2 mov reg1, mem2
  1530. mov reg1, mem2
  1531. or (if mem1 depends on reg1
  1532. and/or if mem2 depends on reg2)
  1533. to:
  1534. mov mem1, reg1
  1535. mov reg1, mem2
  1536. mov reg1, reg2
  1537. }
  1538. begin
  1539. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  1540. taicpu(hp1).loadReg(1,taicpu(hp2).oper[1]^.reg);
  1541. taicpu(hp2).loadRef(1,taicpu(hp2).oper[0]^.ref^);
  1542. taicpu(hp2).loadReg(0,taicpu(p).oper[1]^.reg);
  1543. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1544. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  1545. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1546. AllocRegBetween(taicpu(p).oper[0]^.ref^.base,p,hp2,usedregs);
  1547. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and
  1548. (getsupreg(taicpu(p).oper[0]^.ref^.index) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1549. AllocRegBetween(taicpu(p).oper[0]^.ref^.index,p,hp2,usedregs);
  1550. end
  1551. else if (taicpu(hp1).Oper[0]^.reg <> taicpu(hp2).Oper[1]^.reg) then
  1552. begin
  1553. taicpu(hp2).loadReg(0,taicpu(hp1).Oper[0]^.reg);
  1554. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1555. end
  1556. else
  1557. begin
  1558. asml.remove(hp2);
  1559. hp2.free;
  1560. end
  1561. {$endif i386}
  1562. ;
  1563. end;
  1564. end
  1565. (* { movl [mem1],reg1
  1566. movl [mem1],reg2
  1567. to
  1568. movl [mem1],reg1
  1569. movl reg1,reg2
  1570. }
  1571. else if (taicpu(p).oper[0]^.typ = top_ref) and
  1572. (taicpu(p).oper[1]^.typ = top_reg) and
  1573. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1574. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1575. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1576. RefsEqual(TReference(taicpu(p).oper[0]^^),taicpu(hp1).oper[0]^^.ref^) and
  1577. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.base) and
  1578. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.index) then
  1579. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg)
  1580. else*)
  1581. { movl const1,[mem1]
  1582. movl [mem1],reg1
  1583. to
  1584. movl const1,reg1
  1585. movl reg1,[mem1]
  1586. }
  1587. else if MatchOpType(Taicpu(p),top_const,top_ref) and
  1588. MatchOpType(Taicpu(hp1),top_ref,top_reg) and
  1589. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1590. RefsEqual(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.ref^) and
  1591. not(RegInRef(taicpu(hp1).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^)) then
  1592. begin
  1593. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1594. taicpu(hp1).loadReg(0,taicpu(hp1).oper[1]^.reg);
  1595. taicpu(hp1).loadRef(1,taicpu(p).oper[1]^.ref^);
  1596. taicpu(p).loadReg(1,taicpu(hp1).oper[0]^.reg);
  1597. taicpu(hp1).fileinfo := taicpu(p).fileinfo;
  1598. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 1',p);
  1599. end
  1600. {
  1601. mov* x,reg1
  1602. mov* y,reg1
  1603. to
  1604. mov* y,reg1
  1605. }
  1606. else if (taicpu(p).oper[1]^.typ=top_reg) and
  1607. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  1608. not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^)) then
  1609. begin
  1610. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 4 done',p);
  1611. { take care of the register (de)allocs following p }
  1612. UpdateUsedRegs(tai(p.next));
  1613. asml.remove(p);
  1614. p.free;
  1615. p:=hp1;
  1616. Result:=true;
  1617. exit;
  1618. end;
  1619. end
  1620. else if (taicpu(p).oper[1]^.typ = top_reg) and
  1621. GetNextInstruction_p and
  1622. (hp1.typ = ait_instruction) and
  1623. GetNextInstruction(hp1, hp2) and
  1624. MatchInstruction(hp2,A_MOV,[]) and
  1625. (SuperRegistersEqual(taicpu(hp2).oper[0]^.reg,taicpu(p).oper[1]^.reg)) and
  1626. (IsFoldableArithOp(taicpu(hp1), taicpu(p).oper[1]^.reg) or
  1627. ((taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and (taicpu(hp2).opsize=S_L) and
  1628. IsFoldableArithOp(taicpu(hp1), newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[1]^.reg),R_SUBQ)))
  1629. ) then
  1630. begin
  1631. if OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  1632. (taicpu(hp2).oper[0]^.typ=top_reg) then
  1633. { change movsX/movzX reg/ref, reg2
  1634. add/sub/or/... reg3/$const, reg2
  1635. mov reg2 reg/ref
  1636. dealloc reg2
  1637. to
  1638. add/sub/or/... reg3/$const, reg/ref }
  1639. begin
  1640. TransferUsedRegs(TmpUsedRegs);
  1641. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1642. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1643. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1644. begin
  1645. { by example:
  1646. movswl %si,%eax movswl %si,%eax p
  1647. decl %eax addl %edx,%eax hp1
  1648. movw %ax,%si movw %ax,%si hp2
  1649. ->
  1650. movswl %si,%eax movswl %si,%eax p
  1651. decw %eax addw %edx,%eax hp1
  1652. movw %ax,%si movw %ax,%si hp2
  1653. }
  1654. DebugMsg(SPeepholeOptimization + 'MovOpMov2Op ('+
  1655. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  1656. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  1657. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize),p);
  1658. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  1659. {
  1660. ->
  1661. movswl %si,%eax movswl %si,%eax p
  1662. decw %si addw %dx,%si hp1
  1663. movw %ax,%si movw %ax,%si hp2
  1664. }
  1665. case taicpu(hp1).ops of
  1666. 1:
  1667. begin
  1668. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  1669. if taicpu(hp1).oper[0]^.typ=top_reg then
  1670. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1671. end;
  1672. 2:
  1673. begin
  1674. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1675. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  1676. (taicpu(hp1).opcode<>A_SHL) and
  1677. (taicpu(hp1).opcode<>A_SHR) and
  1678. (taicpu(hp1).opcode<>A_SAR) then
  1679. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1680. end;
  1681. else
  1682. internalerror(2008042701);
  1683. end;
  1684. {
  1685. ->
  1686. decw %si addw %dx,%si p
  1687. }
  1688. asml.remove(p);
  1689. asml.remove(hp2);
  1690. p.Free;
  1691. hp2.Free;
  1692. p := hp1;
  1693. end;
  1694. end
  1695. else if MatchOpType(taicpu(hp2),top_reg,top_reg) and
  1696. not(SuperRegistersEqual(taicpu(hp1).oper[0]^.reg,taicpu(hp2).oper[1]^.reg)) and
  1697. (not((taicpu(hp1).opsize=S_Q) and (taicpu(hp2).opsize=S_L)) or
  1698. { opsize matters for these opcodes, we could probably work around this, but it is not worth the effort }
  1699. ((taicpu(hp1).opcode<>A_SHL) and (taicpu(hp1).opcode<>A_SHR) and (taicpu(hp1).opcode<>A_SAR))
  1700. )
  1701. {$ifdef i386}
  1702. { byte registers of esi, edi, ebp, esp are not available on i386 }
  1703. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  1704. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(p).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  1705. {$endif i386}
  1706. then
  1707. { change movsX/movzX reg/ref, reg2
  1708. add/sub/or/... regX/$const, reg2
  1709. mov reg2, reg3
  1710. dealloc reg2
  1711. to
  1712. movsX/movzX reg/ref, reg3
  1713. add/sub/or/... reg3/$const, reg3
  1714. }
  1715. begin
  1716. TransferUsedRegs(TmpUsedRegs);
  1717. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1718. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1719. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1720. begin
  1721. { by example:
  1722. movswl %si,%eax movswl %si,%eax p
  1723. decl %eax addl %edx,%eax hp1
  1724. movw %ax,%si movw %ax,%si hp2
  1725. ->
  1726. movswl %si,%eax movswl %si,%eax p
  1727. decw %eax addw %edx,%eax hp1
  1728. movw %ax,%si movw %ax,%si hp2
  1729. }
  1730. DebugMsg(SPeepholeOptimization + 'MovOpMov2MovOp ('+
  1731. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  1732. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  1733. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize),p);
  1734. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  1735. taicpu(p).changeopsize(taicpu(hp2).opsize);
  1736. if taicpu(p).oper[0]^.typ=top_reg then
  1737. setsubreg(taicpu(p).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1738. taicpu(p).loadoper(1, taicpu(hp2).oper[1]^);
  1739. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,usedregs);
  1740. {
  1741. ->
  1742. movswl %si,%eax movswl %si,%eax p
  1743. decw %si addw %dx,%si hp1
  1744. movw %ax,%si movw %ax,%si hp2
  1745. }
  1746. case taicpu(hp1).ops of
  1747. 1:
  1748. begin
  1749. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  1750. if taicpu(hp1).oper[0]^.typ=top_reg then
  1751. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1752. end;
  1753. 2:
  1754. begin
  1755. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1756. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  1757. (taicpu(hp1).opcode<>A_SHL) and
  1758. (taicpu(hp1).opcode<>A_SHR) and
  1759. (taicpu(hp1).opcode<>A_SAR) then
  1760. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  1761. end;
  1762. else
  1763. internalerror(2018111801);
  1764. end;
  1765. {
  1766. ->
  1767. decw %si addw %dx,%si p
  1768. }
  1769. asml.remove(hp2);
  1770. hp2.Free;
  1771. end;
  1772. end;
  1773. end
  1774. else if GetNextInstruction_p and
  1775. MatchInstruction(hp1,A_BTS,A_BTR,[Taicpu(p).opsize]) and
  1776. GetNextInstruction(hp1, hp2) and
  1777. MatchInstruction(hp2,A_OR,[Taicpu(p).opsize]) and
  1778. MatchOperand(Taicpu(p).oper[0]^,0) and
  1779. (Taicpu(p).oper[1]^.typ = top_reg) and
  1780. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp1).oper[1]^) and
  1781. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp2).oper[1]^) then
  1782. { mov reg1,0
  1783. bts reg1,operand1 --> mov reg1,operand2
  1784. or reg1,operand2 bts reg1,operand1}
  1785. begin
  1786. Taicpu(hp2).opcode:=A_MOV;
  1787. asml.remove(hp1);
  1788. insertllitem(hp2,hp2.next,hp1);
  1789. asml.remove(p);
  1790. p.free;
  1791. p:=hp1;
  1792. end
  1793. else if GetNextInstruction_p and
  1794. MatchInstruction(hp1,A_LEA,[S_L]) and
  1795. MatchOpType(Taicpu(p),top_ref,top_reg) and
  1796. ((MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(hp1).oper[1]^.reg,Taicpu(p).oper[1]^.reg) and
  1797. (Taicpu(hp1).oper[0]^.ref^.base<>Taicpu(p).oper[1]^.reg)
  1798. ) or
  1799. (MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(p).oper[1]^.reg,Taicpu(hp1).oper[1]^.reg) and
  1800. (Taicpu(hp1).oper[0]^.ref^.index<>Taicpu(p).oper[1]^.reg)
  1801. )
  1802. ) then
  1803. { mov reg1,ref
  1804. lea reg2,[reg1,reg2]
  1805. to
  1806. add reg2,ref}
  1807. begin
  1808. TransferUsedRegs(TmpUsedRegs);
  1809. { reg1 may not be used afterwards }
  1810. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  1811. begin
  1812. Taicpu(hp1).opcode:=A_ADD;
  1813. Taicpu(hp1).oper[0]^.ref^:=Taicpu(p).oper[0]^.ref^;
  1814. DebugMsg(SPeepholeOptimization + 'MovLea2Add done',hp1);
  1815. asml.remove(p);
  1816. p.free;
  1817. p:=hp1;
  1818. end;
  1819. end;
  1820. end;
  1821. function TX86AsmOptimizer.OptPass1MOVXX(var p : tai) : boolean;
  1822. var
  1823. hp1 : tai;
  1824. begin
  1825. Result:=false;
  1826. if taicpu(p).ops <> 2 then
  1827. exit;
  1828. if GetNextInstruction(p,hp1) and
  1829. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  1830. (taicpu(hp1).ops = 2) then
  1831. begin
  1832. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  1833. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  1834. { movXX reg1, mem1 or movXX mem1, reg1
  1835. movXX mem2, reg2 movXX reg2, mem2}
  1836. begin
  1837. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  1838. { movXX reg1, mem1 or movXX mem1, reg1
  1839. movXX mem2, reg1 movXX reg2, mem1}
  1840. begin
  1841. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  1842. begin
  1843. { Removes the second statement from
  1844. movXX reg1, mem1/reg2
  1845. movXX mem1/reg2, reg1
  1846. }
  1847. if taicpu(p).oper[0]^.typ=top_reg then
  1848. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1849. { Removes the second statement from
  1850. movXX mem1/reg1, reg2
  1851. movXX reg2, mem1/reg1
  1852. }
  1853. if (taicpu(p).oper[1]^.typ=top_reg) and
  1854. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)) then
  1855. begin
  1856. asml.remove(p);
  1857. p.free;
  1858. GetNextInstruction(hp1,p);
  1859. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2Nop 1 done',p);
  1860. end
  1861. else
  1862. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
  1863. asml.remove(hp1);
  1864. hp1.free;
  1865. Result:=true;
  1866. exit;
  1867. end
  1868. end;
  1869. end;
  1870. end;
  1871. end;
  1872. function TX86AsmOptimizer.OptPass1OP(var p : tai) : boolean;
  1873. var
  1874. hp1 : tai;
  1875. begin
  1876. result:=false;
  1877. { replace
  1878. <Op>X %mreg1,%mreg2 // Op in [ADD,MUL]
  1879. MovX %mreg2,%mreg1
  1880. dealloc %mreg2
  1881. by
  1882. <Op>X %mreg2,%mreg1
  1883. ?
  1884. }
  1885. if GetNextInstruction(p,hp1) and
  1886. { we mix single and double opperations here because we assume that the compiler
  1887. generates vmovapd only after double operations and vmovaps only after single operations }
  1888. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  1889. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  1890. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  1891. (taicpu(p).oper[0]^.typ=top_reg) then
  1892. begin
  1893. TransferUsedRegs(TmpUsedRegs);
  1894. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1895. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1896. begin
  1897. taicpu(p).loadoper(0,taicpu(hp1).oper[0]^);
  1898. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1899. DebugMsg(SPeepholeOptimization + 'OpMov2Op done',p);
  1900. asml.Remove(hp1);
  1901. hp1.Free;
  1902. result:=true;
  1903. end;
  1904. end;
  1905. end;
  1906. function TX86AsmOptimizer.OptPass1LEA(var p : tai) : boolean;
  1907. var
  1908. hp1 : tai;
  1909. l : ASizeInt;
  1910. begin
  1911. Result:=false;
  1912. { removes seg register prefixes from LEA operations, as they
  1913. don't do anything}
  1914. taicpu(p).oper[0]^.ref^.Segment:=NR_NO;
  1915. { changes "lea (%reg1), %reg2" into "mov %reg1, %reg2" }
  1916. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  1917. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  1918. { do not mess with leas acessing the stack pointer }
  1919. (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) and
  1920. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  1921. begin
  1922. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  1923. (taicpu(p).oper[0]^.ref^.offset = 0) then
  1924. begin
  1925. hp1:=taicpu.op_reg_reg(A_MOV,taicpu(p).opsize,taicpu(p).oper[0]^.ref^.base,
  1926. taicpu(p).oper[1]^.reg);
  1927. InsertLLItem(p.previous,p.next, hp1);
  1928. DebugMsg(SPeepholeOptimization + 'Lea2Mov done',hp1);
  1929. p.free;
  1930. p:=hp1;
  1931. Result:=true;
  1932. exit;
  1933. end
  1934. else if (taicpu(p).oper[0]^.ref^.offset = 0) then
  1935. begin
  1936. hp1:=taicpu(p.Next);
  1937. DebugMsg(SPeepholeOptimization + 'Lea2Nop done',p);
  1938. asml.remove(p);
  1939. p.free;
  1940. p:=hp1;
  1941. Result:=true;
  1942. exit;
  1943. end
  1944. { continue to use lea to adjust the stack pointer,
  1945. it is the recommended way, but only if not optimizing for size }
  1946. else if (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) or
  1947. (cs_opt_size in current_settings.optimizerswitches) then
  1948. with taicpu(p).oper[0]^.ref^ do
  1949. if (base = taicpu(p).oper[1]^.reg) then
  1950. begin
  1951. l:=offset;
  1952. if (l=1) and UseIncDec then
  1953. begin
  1954. taicpu(p).opcode:=A_INC;
  1955. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  1956. taicpu(p).ops:=1;
  1957. DebugMsg(SPeepholeOptimization + 'Lea2Inc done',p);
  1958. end
  1959. else if (l=-1) and UseIncDec then
  1960. begin
  1961. taicpu(p).opcode:=A_DEC;
  1962. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  1963. taicpu(p).ops:=1;
  1964. DebugMsg(SPeepholeOptimization + 'Lea2Dec done',p);
  1965. end
  1966. else
  1967. begin
  1968. if (l<0) and (l<>-2147483648) then
  1969. begin
  1970. taicpu(p).opcode:=A_SUB;
  1971. taicpu(p).loadConst(0,-l);
  1972. DebugMsg(SPeepholeOptimization + 'Lea2Sub done',p);
  1973. end
  1974. else
  1975. begin
  1976. taicpu(p).opcode:=A_ADD;
  1977. taicpu(p).loadConst(0,l);
  1978. DebugMsg(SPeepholeOptimization + 'Lea2Add done',p);
  1979. end;
  1980. end;
  1981. Result:=true;
  1982. exit;
  1983. end;
  1984. end;
  1985. if GetNextInstruction(p,hp1) and
  1986. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  1987. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  1988. MatchOpType(Taicpu(hp1),top_reg,top_reg) and
  1989. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) then
  1990. begin
  1991. TransferUsedRegs(TmpUsedRegs);
  1992. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1993. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1994. begin
  1995. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1996. DebugMsg(SPeepholeOptimization + 'LeaMov2Lea done',p);
  1997. asml.Remove(hp1);
  1998. hp1.Free;
  1999. result:=true;
  2000. end;
  2001. end;
  2002. end;
  2003. function TX86AsmOptimizer.DoSubAddOpt(var p: tai): Boolean;
  2004. var
  2005. hp1 : tai;
  2006. begin
  2007. DoSubAddOpt := False;
  2008. if GetLastInstruction(p, hp1) and
  2009. (hp1.typ = ait_instruction) and
  2010. (taicpu(hp1).opsize = taicpu(p).opsize) then
  2011. case taicpu(hp1).opcode Of
  2012. A_DEC:
  2013. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  2014. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2015. begin
  2016. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
  2017. asml.remove(hp1);
  2018. hp1.free;
  2019. end;
  2020. A_SUB:
  2021. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  2022. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  2023. begin
  2024. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
  2025. asml.remove(hp1);
  2026. hp1.free;
  2027. end;
  2028. A_ADD:
  2029. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  2030. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  2031. begin
  2032. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  2033. asml.remove(hp1);
  2034. hp1.free;
  2035. if (taicpu(p).oper[0]^.val = 0) then
  2036. begin
  2037. hp1 := tai(p.next);
  2038. asml.remove(p);
  2039. p.free;
  2040. if not GetLastInstruction(hp1, p) then
  2041. p := hp1;
  2042. DoSubAddOpt := True;
  2043. end
  2044. end;
  2045. end;
  2046. end;
  2047. function TX86AsmOptimizer.OptPass1Sub(var p : tai) : boolean;
  2048. {$ifdef i386}
  2049. var
  2050. hp1 : tai;
  2051. {$endif i386}
  2052. begin
  2053. Result:=false;
  2054. { * change "subl $2, %esp; pushw x" to "pushl x"}
  2055. { * change "sub/add const1, reg" or "dec reg" followed by
  2056. "sub const2, reg" to one "sub ..., reg" }
  2057. if MatchOpType(taicpu(p),top_const,top_reg) then
  2058. begin
  2059. {$ifdef i386}
  2060. if (taicpu(p).oper[0]^.val = 2) and
  2061. (taicpu(p).oper[1]^.reg = NR_ESP) and
  2062. { Don't do the sub/push optimization if the sub }
  2063. { comes from setting up the stack frame (JM) }
  2064. (not(GetLastInstruction(p,hp1)) or
  2065. not(MatchInstruction(hp1,A_MOV,[S_L]) and
  2066. MatchOperand(taicpu(hp1).oper[0]^,NR_ESP) and
  2067. MatchOperand(taicpu(hp1).oper[0]^,NR_EBP))) then
  2068. begin
  2069. hp1 := tai(p.next);
  2070. while Assigned(hp1) and
  2071. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  2072. not RegReadByInstruction(NR_ESP,hp1) and
  2073. not RegModifiedByInstruction(NR_ESP,hp1) do
  2074. hp1 := tai(hp1.next);
  2075. if Assigned(hp1) and
  2076. MatchInstruction(hp1,A_PUSH,[S_W]) then
  2077. begin
  2078. taicpu(hp1).changeopsize(S_L);
  2079. if taicpu(hp1).oper[0]^.typ=top_reg then
  2080. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  2081. hp1 := tai(p.next);
  2082. asml.remove(p);
  2083. p.free;
  2084. p := hp1;
  2085. Result:=true;
  2086. exit;
  2087. end;
  2088. end;
  2089. {$endif i386}
  2090. if DoSubAddOpt(p) then
  2091. Result:=true;
  2092. end;
  2093. end;
  2094. function TX86AsmOptimizer.OptPass1SHLSAL(var p : tai) : boolean;
  2095. var
  2096. TmpBool1,TmpBool2 : Boolean;
  2097. tmpref : treference;
  2098. hp1,hp2: tai;
  2099. begin
  2100. Result:=false;
  2101. if MatchOpType(taicpu(p),top_const,top_reg) and
  2102. (taicpu(p).opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  2103. (taicpu(p).oper[0]^.val <= 3) then
  2104. { Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement }
  2105. begin
  2106. { should we check the next instruction? }
  2107. TmpBool1 := True;
  2108. { have we found an add/sub which could be
  2109. integrated in the lea? }
  2110. TmpBool2 := False;
  2111. reference_reset(tmpref,2,[]);
  2112. TmpRef.index := taicpu(p).oper[1]^.reg;
  2113. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  2114. while TmpBool1 and
  2115. GetNextInstruction(p, hp1) and
  2116. (tai(hp1).typ = ait_instruction) and
  2117. ((((taicpu(hp1).opcode = A_ADD) or
  2118. (taicpu(hp1).opcode = A_SUB)) and
  2119. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  2120. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  2121. (((taicpu(hp1).opcode = A_INC) or
  2122. (taicpu(hp1).opcode = A_DEC)) and
  2123. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  2124. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg))) and
  2125. (not GetNextInstruction(hp1,hp2) or
  2126. not instrReadsFlags(hp2)) Do
  2127. begin
  2128. TmpBool1 := False;
  2129. if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  2130. begin
  2131. TmpBool1 := True;
  2132. TmpBool2 := True;
  2133. case taicpu(hp1).opcode of
  2134. A_ADD:
  2135. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  2136. A_SUB:
  2137. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  2138. end;
  2139. asml.remove(hp1);
  2140. hp1.free;
  2141. end
  2142. else
  2143. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  2144. (((taicpu(hp1).opcode = A_ADD) and
  2145. (TmpRef.base = NR_NO)) or
  2146. (taicpu(hp1).opcode = A_INC) or
  2147. (taicpu(hp1).opcode = A_DEC)) then
  2148. begin
  2149. TmpBool1 := True;
  2150. TmpBool2 := True;
  2151. case taicpu(hp1).opcode of
  2152. A_ADD:
  2153. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  2154. A_INC:
  2155. inc(TmpRef.offset);
  2156. A_DEC:
  2157. dec(TmpRef.offset);
  2158. end;
  2159. asml.remove(hp1);
  2160. hp1.free;
  2161. end;
  2162. end;
  2163. if TmpBool2
  2164. {$ifndef x86_64}
  2165. or
  2166. ((current_settings.optimizecputype < cpu_Pentium2) and
  2167. (taicpu(p).oper[0]^.val <= 3) and
  2168. not(cs_opt_size in current_settings.optimizerswitches))
  2169. {$endif x86_64}
  2170. then
  2171. begin
  2172. if not(TmpBool2) and
  2173. (taicpu(p).oper[0]^.val = 1) then
  2174. begin
  2175. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  2176. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  2177. end
  2178. else
  2179. hp1 := taicpu.op_ref_reg(A_LEA, taicpu(p).opsize, TmpRef,
  2180. taicpu(p).oper[1]^.reg);
  2181. InsertLLItem(p.previous, p.next, hp1);
  2182. p.free;
  2183. p := hp1;
  2184. end;
  2185. end
  2186. {$ifndef x86_64}
  2187. else if (current_settings.optimizecputype < cpu_Pentium2) and
  2188. MatchOpType(taicpu(p),top_const,top_reg) then
  2189. begin
  2190. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  2191. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  2192. (unlike shl, which is only Tairable in the U pipe) }
  2193. if taicpu(p).oper[0]^.val=1 then
  2194. begin
  2195. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  2196. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  2197. InsertLLItem(p.previous, p.next, hp1);
  2198. p.free;
  2199. p := hp1;
  2200. end
  2201. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  2202. "shl $3, %reg" to "lea (,%reg,8), %reg }
  2203. else if (taicpu(p).opsize = S_L) and
  2204. (taicpu(p).oper[0]^.val<= 3) then
  2205. begin
  2206. reference_reset(tmpref,2,[]);
  2207. TmpRef.index := taicpu(p).oper[1]^.reg;
  2208. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  2209. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  2210. InsertLLItem(p.previous, p.next, hp1);
  2211. p.free;
  2212. p := hp1;
  2213. end;
  2214. end
  2215. {$endif x86_64}
  2216. ;
  2217. end;
  2218. function TX86AsmOptimizer.OptPass1SETcc(var p: tai): boolean;
  2219. var
  2220. hp1,hp2,next: tai; SetC, JumpC: TAsmCond;
  2221. begin
  2222. Result:=false;
  2223. if MatchOpType(taicpu(p),top_reg) and
  2224. GetNextInstruction(p, hp1) and
  2225. MatchInstruction(hp1, A_TEST, [S_B]) and
  2226. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2227. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  2228. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  2229. GetNextInstruction(hp1, hp2) and
  2230. MatchInstruction(hp2, A_Jcc, []) then
  2231. { Change from: To:
  2232. set(C) %reg j(~C) label
  2233. test %reg,%reg
  2234. je label
  2235. set(C) %reg j(C) label
  2236. test %reg,%reg
  2237. jne label
  2238. }
  2239. begin
  2240. next := tai(p.Next);
  2241. TransferUsedRegs(TmpUsedRegs);
  2242. UpdateUsedRegs(TmpUsedRegs, next);
  2243. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2244. asml.Remove(hp1);
  2245. hp1.Free;
  2246. JumpC := taicpu(hp2).condition;
  2247. if conditions_equal(JumpC, C_E) then
  2248. SetC := inverse_cond(taicpu(p).condition)
  2249. else if conditions_equal(JumpC, C_NE) then
  2250. SetC := taicpu(p).condition
  2251. else
  2252. InternalError(2018061400);
  2253. if SetC = C_NONE then
  2254. InternalError(2018061401);
  2255. taicpu(hp2).SetCondition(SetC);
  2256. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
  2257. begin
  2258. asml.Remove(p);
  2259. UpdateUsedRegs(next);
  2260. p.Free;
  2261. Result := True;
  2262. p := hp2;
  2263. end;
  2264. DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> Jcc',p);
  2265. end;
  2266. end;
  2267. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  2268. var
  2269. hp1,hp2: tai;
  2270. {$ifdef x86_64}
  2271. hp3: tai;
  2272. {$endif x86_64}
  2273. begin
  2274. Result:=false;
  2275. if MatchOpType(taicpu(p),top_reg,top_reg) and
  2276. GetNextInstruction(p, hp1) and
  2277. {$ifdef x86_64}
  2278. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  2279. {$else x86_64}
  2280. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  2281. {$endif x86_64}
  2282. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2283. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  2284. { mov reg1, reg2 mov reg1, reg2
  2285. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  2286. begin
  2287. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  2288. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  2289. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  2290. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  2291. TransferUsedRegs(TmpUsedRegs);
  2292. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2293. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  2294. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  2295. then
  2296. begin
  2297. asml.remove(p);
  2298. p.free;
  2299. p := hp1;
  2300. Result:=true;
  2301. end;
  2302. exit;
  2303. end
  2304. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  2305. GetNextInstruction(p, hp1) and
  2306. {$ifdef x86_64}
  2307. MatchInstruction(hp1,[A_MOV,A_MOVZX,A_MOVSX,A_MOVSXD],[]) and
  2308. {$else x86_64}
  2309. MatchInstruction(hp1,A_MOV,A_MOVZX,A_MOVSX,[]) and
  2310. {$endif x86_64}
  2311. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  2312. ((taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg)
  2313. or
  2314. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg)
  2315. ) and
  2316. (getsupreg(taicpu(hp1).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) then
  2317. { mov reg1, reg2
  2318. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  2319. begin
  2320. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) then
  2321. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[0]^.reg;
  2322. if (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) then
  2323. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  2324. DebugMsg(SPeepholeOptimization + 'MovMovXX2MoVXX 1 done',p);
  2325. asml.remove(p);
  2326. p.free;
  2327. p := hp1;
  2328. Result:=true;
  2329. exit;
  2330. end
  2331. else if (taicpu(p).oper[0]^.typ = top_ref) and
  2332. GetNextInstruction(p,hp1) and
  2333. (hp1.typ = ait_instruction) and
  2334. { while the GetNextInstruction(hp1,hp2) call could be factored out,
  2335. doing it separately in both branches allows to do the cheap checks
  2336. with low probability earlier }
  2337. ((IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  2338. GetNextInstruction(hp1,hp2) and
  2339. MatchInstruction(hp2,A_MOV,[])
  2340. ) or
  2341. ((taicpu(hp1).opcode=A_LEA) and
  2342. GetNextInstruction(hp1,hp2) and
  2343. MatchInstruction(hp2,A_MOV,[]) and
  2344. ((MatchReference(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  2345. (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg)
  2346. ) or
  2347. (MatchReference(taicpu(hp1).oper[0]^.ref^,NR_INVALID,
  2348. taicpu(p).oper[1]^.reg) and
  2349. (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg)) or
  2350. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_NO)) or
  2351. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,NR_NO,taicpu(p).oper[1]^.reg))
  2352. ) and
  2353. ((MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^)) or not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)))
  2354. )
  2355. ) and
  2356. MatchOperand(taicpu(hp1).oper[taicpu(hp1).ops-1]^,taicpu(hp2).oper[0]^) and
  2357. (taicpu(hp2).oper[1]^.typ = top_ref) then
  2358. begin
  2359. TransferUsedRegs(TmpUsedRegs);
  2360. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  2361. UpdateUsedRegs(TmpUsedRegs,tai(hp1.next));
  2362. if (RefsEqual(taicpu(hp2).oper[1]^.ref^,taicpu(p).oper[0]^.ref^) and
  2363. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,TmpUsedRegs))) then
  2364. { change mov (ref), reg
  2365. add/sub/or/... reg2/$const, reg
  2366. mov reg, (ref)
  2367. # release reg
  2368. to add/sub/or/... reg2/$const, (ref) }
  2369. begin
  2370. case taicpu(hp1).opcode of
  2371. A_INC,A_DEC,A_NOT,A_NEG :
  2372. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  2373. A_LEA :
  2374. begin
  2375. taicpu(hp1).opcode:=A_ADD;
  2376. if (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.index<>NR_NO) then
  2377. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.index)
  2378. else if (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.base<>NR_NO) then
  2379. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.base)
  2380. else
  2381. taicpu(hp1).loadconst(0,taicpu(hp1).oper[0]^.ref^.offset);
  2382. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  2383. DebugMsg(SPeepholeOptimization + 'FoldLea done',hp1);
  2384. end
  2385. else
  2386. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  2387. end;
  2388. asml.remove(p);
  2389. asml.remove(hp2);
  2390. p.free;
  2391. hp2.free;
  2392. p := hp1
  2393. end;
  2394. Exit;
  2395. {$ifdef x86_64}
  2396. end
  2397. else if (taicpu(p).opsize = S_L) and
  2398. (taicpu(p).oper[1]^.typ = top_reg) and
  2399. (
  2400. GetNextInstruction(p, hp1) and
  2401. MatchInstruction(hp1, A_MOV,[]) and
  2402. (taicpu(hp1).opsize = S_L) and
  2403. (taicpu(hp1).oper[1]^.typ = top_reg)
  2404. ) and (
  2405. GetNextInstruction(hp1, hp2) and
  2406. (tai(hp2).typ=ait_instruction) and
  2407. (taicpu(hp2).opsize = S_Q) and
  2408. (
  2409. (
  2410. MatchInstruction(hp2, A_ADD,[]) and
  2411. (taicpu(hp2).opsize = S_Q) and
  2412. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  2413. (
  2414. (
  2415. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  2416. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2417. ) or (
  2418. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  2419. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  2420. )
  2421. )
  2422. ) or (
  2423. MatchInstruction(hp2, A_LEA,[]) and
  2424. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  2425. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  2426. (
  2427. (
  2428. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  2429. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2430. ) or (
  2431. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  2432. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  2433. )
  2434. ) and (
  2435. (
  2436. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2437. ) or (
  2438. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  2439. )
  2440. )
  2441. )
  2442. )
  2443. ) and (
  2444. GetNextInstruction(hp2, hp3) and
  2445. MatchInstruction(hp3, A_SHR,[]) and
  2446. (taicpu(hp3).opsize = S_Q) and
  2447. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  2448. (taicpu(hp3).oper[0]^.val = 1) and
  2449. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  2450. ) then
  2451. begin
  2452. { Change movl x, reg1d movl x, reg1d
  2453. movl y, reg2d movl y, reg2d
  2454. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  2455. shrq $1, reg1q shrq $1, reg1q
  2456. ( reg1d and reg2d can be switched around in the first two instructions )
  2457. To movl x, reg1d
  2458. addl y, reg1d
  2459. rcrl $1, reg1d
  2460. This corresponds to the common expression (x + y) shr 1, where
  2461. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  2462. smaller code, but won't account for x + y causing an overflow). [Kit]
  2463. }
  2464. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  2465. { Change first MOV command to have the same register as the final output }
  2466. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  2467. else
  2468. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  2469. { Change second MOV command to an ADD command. This is easier than
  2470. converting the existing command because it means we don't have to
  2471. touch 'y', which might be a complicated reference, and also the
  2472. fact that the third command might either be ADD or LEA. [Kit] }
  2473. taicpu(hp1).opcode := A_ADD;
  2474. { Delete old ADD/LEA instruction }
  2475. asml.remove(hp2);
  2476. hp2.free;
  2477. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  2478. taicpu(hp3).opcode := A_RCR;
  2479. taicpu(hp3).changeopsize(S_L);
  2480. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  2481. {$endif x86_64}
  2482. end;
  2483. end;
  2484. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  2485. var
  2486. hp1 : tai;
  2487. begin
  2488. Result:=false;
  2489. if (taicpu(p).ops >= 2) and
  2490. ((taicpu(p).oper[0]^.typ = top_const) or
  2491. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  2492. (taicpu(p).oper[1]^.typ = top_reg) and
  2493. ((taicpu(p).ops = 2) or
  2494. ((taicpu(p).oper[2]^.typ = top_reg) and
  2495. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  2496. GetLastInstruction(p,hp1) and
  2497. MatchInstruction(hp1,A_MOV,[]) and
  2498. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  2499. ((taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) or
  2500. ((taicpu(hp1).opsize=S_L) and (taicpu(p).opsize=S_Q) and SuperRegistersEqual(taicpu(hp1).oper[1]^.reg,taicpu(p).oper[1]^.reg))) then
  2501. begin
  2502. TransferUsedRegs(TmpUsedRegs);
  2503. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) then
  2504. { change
  2505. mov reg1,reg2
  2506. imul y,reg2 to imul y,reg1,reg2 }
  2507. begin
  2508. taicpu(p).ops := 3;
  2509. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  2510. taicpu(p).loadreg(2,taicpu(hp1).oper[1]^.reg);
  2511. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  2512. asml.remove(hp1);
  2513. hp1.free;
  2514. result:=true;
  2515. end;
  2516. end;
  2517. end;
  2518. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  2519. var
  2520. hp1 : tai;
  2521. begin
  2522. {
  2523. change
  2524. jmp .L1
  2525. ...
  2526. .L1:
  2527. ret
  2528. into
  2529. ret
  2530. }
  2531. result:=false;
  2532. if (taicpu(p).oper[0]^.typ=top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full) and (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  2533. (taicpu(p).oper[0]^.ref^.index=NR_NO) then
  2534. begin
  2535. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  2536. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and
  2537. MatchInstruction(hp1,A_RET,[S_NO]) then
  2538. begin
  2539. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  2540. taicpu(p).opcode:=A_RET;
  2541. taicpu(p).is_jmp:=false;
  2542. taicpu(p).ops:=taicpu(hp1).ops;
  2543. case taicpu(hp1).ops of
  2544. 0:
  2545. taicpu(p).clearop(0);
  2546. 1:
  2547. taicpu(p).loadconst(0,taicpu(hp1).oper[0]^.val);
  2548. else
  2549. internalerror(2016041301);
  2550. end;
  2551. result:=true;
  2552. end;
  2553. end;
  2554. end;
  2555. function CanBeCMOV(p : tai) : boolean;
  2556. begin
  2557. CanBeCMOV:=assigned(p) and
  2558. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  2559. { we can't use cmov ref,reg because
  2560. ref could be nil and cmov still throws an exception
  2561. if ref=nil but the mov isn't done (FK)
  2562. or ((taicpu(p).oper[0]^.typ = top_ref) and
  2563. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  2564. }
  2565. MatchOpType(taicpu(p),top_reg,top_reg);
  2566. end;
  2567. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  2568. var
  2569. hp1,hp2,hp3,hp4,hpmov2: tai;
  2570. carryadd_opcode : TAsmOp;
  2571. l : Longint;
  2572. condition : TAsmCond;
  2573. symbol: TAsmSymbol;
  2574. begin
  2575. result:=false;
  2576. symbol:=nil;
  2577. if GetNextInstruction(p,hp1) then
  2578. begin
  2579. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  2580. if (hp1.typ=ait_instruction) and
  2581. GetNextInstruction(hp1,hp2) and (hp2.typ=ait_label) and
  2582. (Tasmlabel(symbol) = Tai_label(hp2).labsym) then
  2583. { jb @@1 cmc
  2584. inc/dec operand --> adc/sbb operand,0
  2585. @@1:
  2586. ... and ...
  2587. jnb @@1
  2588. inc/dec operand --> adc/sbb operand,0
  2589. @@1: }
  2590. begin
  2591. carryadd_opcode:=A_NONE;
  2592. if Taicpu(p).condition in [C_NAE,C_B] then
  2593. begin
  2594. if Taicpu(hp1).opcode=A_INC then
  2595. carryadd_opcode:=A_ADC;
  2596. if Taicpu(hp1).opcode=A_DEC then
  2597. carryadd_opcode:=A_SBB;
  2598. if carryadd_opcode<>A_NONE then
  2599. begin
  2600. Taicpu(p).clearop(0);
  2601. Taicpu(p).ops:=0;
  2602. Taicpu(p).is_jmp:=false;
  2603. Taicpu(p).opcode:=A_CMC;
  2604. Taicpu(p).condition:=C_NONE;
  2605. Taicpu(hp1).ops:=2;
  2606. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  2607. Taicpu(hp1).loadconst(0,0);
  2608. Taicpu(hp1).opcode:=carryadd_opcode;
  2609. result:=true;
  2610. exit;
  2611. end;
  2612. end;
  2613. if Taicpu(p).condition in [C_AE,C_NB] then
  2614. begin
  2615. if Taicpu(hp1).opcode=A_INC then
  2616. carryadd_opcode:=A_ADC;
  2617. if Taicpu(hp1).opcode=A_DEC then
  2618. carryadd_opcode:=A_SBB;
  2619. if carryadd_opcode<>A_NONE then
  2620. begin
  2621. asml.remove(p);
  2622. p.free;
  2623. Taicpu(hp1).ops:=2;
  2624. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  2625. Taicpu(hp1).loadconst(0,0);
  2626. Taicpu(hp1).opcode:=carryadd_opcode;
  2627. p:=hp1;
  2628. result:=true;
  2629. exit;
  2630. end;
  2631. end;
  2632. end;
  2633. if ((hp1.typ = ait_label) and (symbol = tai_label(hp1).labsym))
  2634. or ((hp1.typ = ait_align) and GetNextInstruction(hp1, hp2) and (hp2.typ = ait_label) and (symbol = tai_label(hp2).labsym)) then
  2635. begin
  2636. { If Jcc is immediately followed by the label that it's supposed to jump to, remove it }
  2637. DebugMsg(SPeepholeOptimization + 'Removed conditional jump whose destination was immediately after it', p);
  2638. UpdateUsedRegs(hp1);
  2639. TAsmLabel(symbol).decrefs;
  2640. { if the label refs. reach zero, remove any alignment before the label }
  2641. if (hp1.typ = ait_align) then
  2642. begin
  2643. UpdateUsedRegs(hp2);
  2644. if (TAsmLabel(symbol).getrefs = 0) then
  2645. begin
  2646. asml.Remove(hp1);
  2647. hp1.Free;
  2648. end;
  2649. hp1 := hp2; { Set hp1 to the label }
  2650. end;
  2651. asml.remove(p);
  2652. p.free;
  2653. if (TAsmLabel(symbol).getrefs = 0) then
  2654. begin
  2655. GetNextInstruction(hp1, p); { Instruction following the label }
  2656. asml.remove(hp1);
  2657. hp1.free;
  2658. UpdateUsedRegs(p);
  2659. Result := True;
  2660. end
  2661. else
  2662. begin
  2663. { We don't need to set the result to True because we know hp1
  2664. is a label and won't trigger any optimisation routines. [Kit] }
  2665. p := hp1;
  2666. end;
  2667. Exit;
  2668. end;
  2669. end;
  2670. {$ifndef i8086}
  2671. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  2672. begin
  2673. { check for
  2674. jCC xxx
  2675. <several movs>
  2676. xxx:
  2677. }
  2678. l:=0;
  2679. GetNextInstruction(p, hp1);
  2680. while assigned(hp1) and
  2681. CanBeCMOV(hp1) and
  2682. { stop on labels }
  2683. not(hp1.typ=ait_label) do
  2684. begin
  2685. inc(l);
  2686. GetNextInstruction(hp1,hp1);
  2687. end;
  2688. if assigned(hp1) then
  2689. begin
  2690. if FindLabel(tasmlabel(symbol),hp1) then
  2691. begin
  2692. if (l<=4) and (l>0) then
  2693. begin
  2694. condition:=inverse_cond(taicpu(p).condition);
  2695. GetNextInstruction(p,hp1);
  2696. repeat
  2697. if not Assigned(hp1) then
  2698. InternalError(2018062900);
  2699. taicpu(hp1).opcode:=A_CMOVcc;
  2700. taicpu(hp1).condition:=condition;
  2701. UpdateUsedRegs(hp1);
  2702. GetNextInstruction(hp1,hp1);
  2703. until not(CanBeCMOV(hp1));
  2704. { Don't decrement the reference count on the label yet, otherwise
  2705. GetNextInstruction might skip over the label if it drops to
  2706. zero. }
  2707. GetNextInstruction(hp1,hp2);
  2708. { if the label refs. reach zero, remove any alignment before the label }
  2709. if (hp1.typ = ait_align) and (hp2.typ = ait_label) then
  2710. begin
  2711. { Ref = 1 means it will drop to zero }
  2712. if (tasmlabel(symbol).getrefs=1) then
  2713. begin
  2714. asml.Remove(hp1);
  2715. hp1.Free;
  2716. end;
  2717. end
  2718. else
  2719. hp2 := hp1;
  2720. if not Assigned(hp2) then
  2721. InternalError(2018062910);
  2722. if (hp2.typ <> ait_label) then
  2723. begin
  2724. { There's something other than CMOVs here. Move the original jump
  2725. to right before this point, then break out.
  2726. Originally this was part of the above internal error, but it got
  2727. triggered on the bootstrapping process sometimes. Investigate. [Kit] }
  2728. asml.remove(p);
  2729. asml.insertbefore(p, hp2);
  2730. DebugMsg('Jcc/CMOVcc drop-out', p);
  2731. UpdateUsedRegs(p);
  2732. Result := True;
  2733. Exit;
  2734. end;
  2735. { Now we can safely decrement the reference count }
  2736. tasmlabel(symbol).decrefs;
  2737. { Remove the original jump }
  2738. asml.Remove(p);
  2739. p.Free;
  2740. GetNextInstruction(hp2, p); { Instruction after the label }
  2741. { Remove the label if this is its final reference }
  2742. if (tasmlabel(symbol).getrefs=0) then
  2743. begin
  2744. asml.remove(hp2);
  2745. hp2.free;
  2746. end;
  2747. if Assigned(p) then
  2748. begin
  2749. UpdateUsedRegs(p);
  2750. result:=true;
  2751. end;
  2752. exit;
  2753. end;
  2754. end
  2755. else
  2756. begin
  2757. { check further for
  2758. jCC xxx
  2759. <several movs 1>
  2760. jmp yyy
  2761. xxx:
  2762. <several movs 2>
  2763. yyy:
  2764. }
  2765. { hp2 points to jmp yyy }
  2766. hp2:=hp1;
  2767. { skip hp1 to xxx (or an align right before it) }
  2768. GetNextInstruction(hp1, hp1);
  2769. if assigned(hp2) and
  2770. assigned(hp1) and
  2771. (l<=3) and
  2772. (hp2.typ=ait_instruction) and
  2773. (taicpu(hp2).is_jmp) and
  2774. (taicpu(hp2).condition=C_None) and
  2775. { real label and jump, no further references to the
  2776. label are allowed }
  2777. (tasmlabel(symbol).getrefs=1) and
  2778. FindLabel(tasmlabel(symbol),hp1) then
  2779. begin
  2780. l:=0;
  2781. { skip hp1 to <several moves 2> }
  2782. if (hp1.typ = ait_align) then
  2783. GetNextInstruction(hp1, hp1);
  2784. GetNextInstruction(hp1, hpmov2);
  2785. hp1 := hpmov2;
  2786. while assigned(hp1) and
  2787. CanBeCMOV(hp1) do
  2788. begin
  2789. inc(l);
  2790. GetNextInstruction(hp1, hp1);
  2791. end;
  2792. { hp1 points to yyy (or an align right before it) }
  2793. hp3 := hp1;
  2794. if assigned(hp1) and
  2795. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  2796. begin
  2797. condition:=inverse_cond(taicpu(p).condition);
  2798. GetNextInstruction(p,hp1);
  2799. repeat
  2800. taicpu(hp1).opcode:=A_CMOVcc;
  2801. taicpu(hp1).condition:=condition;
  2802. UpdateUsedRegs(hp1);
  2803. GetNextInstruction(hp1,hp1);
  2804. until not(assigned(hp1)) or
  2805. not(CanBeCMOV(hp1));
  2806. condition:=inverse_cond(condition);
  2807. hp1 := hpmov2;
  2808. { hp1 is now at <several movs 2> }
  2809. while Assigned(hp1) and CanBeCMOV(hp1) do
  2810. begin
  2811. taicpu(hp1).opcode:=A_CMOVcc;
  2812. taicpu(hp1).condition:=condition;
  2813. UpdateUsedRegs(hp1);
  2814. GetNextInstruction(hp1,hp1);
  2815. end;
  2816. hp1 := p;
  2817. { Get first instruction after label }
  2818. GetNextInstruction(hp3, p);
  2819. if assigned(p) and (hp3.typ = ait_align) then
  2820. GetNextInstruction(p, p);
  2821. { Don't dereference yet, as doing so will cause
  2822. GetNextInstruction to skip the label and
  2823. optional align marker. [Kit] }
  2824. GetNextInstruction(hp2, hp4);
  2825. { remove jCC }
  2826. asml.remove(hp1);
  2827. hp1.free;
  2828. { Remove label xxx (it will have a ref of zero due to the initial check }
  2829. if (hp4.typ = ait_align) then
  2830. begin
  2831. { Account for alignment as well }
  2832. GetNextInstruction(hp4, hp1);
  2833. asml.remove(hp1);
  2834. hp1.free;
  2835. end;
  2836. asml.remove(hp4);
  2837. hp4.free;
  2838. { Now we can safely decrement it }
  2839. tasmlabel(symbol).decrefs;
  2840. { remove jmp }
  2841. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  2842. asml.remove(hp2);
  2843. hp2.free;
  2844. { Remove label yyy (and the optional alignment) if its reference will fall to zero }
  2845. if tasmlabel(symbol).getrefs = 1 then
  2846. begin
  2847. if (hp3.typ = ait_align) then
  2848. begin
  2849. { Account for alignment as well }
  2850. GetNextInstruction(hp3, hp1);
  2851. asml.remove(hp1);
  2852. hp1.free;
  2853. end;
  2854. asml.remove(hp3);
  2855. hp3.free;
  2856. { As before, now we can safely decrement it }
  2857. tasmlabel(symbol).decrefs;
  2858. end;
  2859. if Assigned(p) then
  2860. begin
  2861. UpdateUsedRegs(p);
  2862. result:=true;
  2863. end;
  2864. exit;
  2865. end;
  2866. end;
  2867. end;
  2868. end;
  2869. end;
  2870. {$endif i8086}
  2871. end;
  2872. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  2873. var
  2874. hp1,hp2: tai;
  2875. begin
  2876. result:=false;
  2877. if (taicpu(p).oper[1]^.typ = top_reg) and
  2878. GetNextInstruction(p,hp1) and
  2879. (hp1.typ = ait_instruction) and
  2880. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  2881. GetNextInstruction(hp1,hp2) and
  2882. MatchInstruction(hp2,A_MOV,[]) and
  2883. (taicpu(hp2).oper[0]^.typ = top_reg) and
  2884. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  2885. {$ifdef i386}
  2886. { not all registers have byte size sub registers on i386 }
  2887. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  2888. {$endif i386}
  2889. (((taicpu(hp1).ops=2) and
  2890. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  2891. ((taicpu(hp1).ops=1) and
  2892. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  2893. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  2894. begin
  2895. { change movsX/movzX reg/ref, reg2
  2896. add/sub/or/... reg3/$const, reg2
  2897. mov reg2 reg/ref
  2898. to add/sub/or/... reg3/$const, reg/ref }
  2899. { by example:
  2900. movswl %si,%eax movswl %si,%eax p
  2901. decl %eax addl %edx,%eax hp1
  2902. movw %ax,%si movw %ax,%si hp2
  2903. ->
  2904. movswl %si,%eax movswl %si,%eax p
  2905. decw %eax addw %edx,%eax hp1
  2906. movw %ax,%si movw %ax,%si hp2
  2907. }
  2908. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  2909. {
  2910. ->
  2911. movswl %si,%eax movswl %si,%eax p
  2912. decw %si addw %dx,%si hp1
  2913. movw %ax,%si movw %ax,%si hp2
  2914. }
  2915. case taicpu(hp1).ops of
  2916. 1:
  2917. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  2918. 2:
  2919. begin
  2920. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  2921. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  2922. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2923. end;
  2924. else
  2925. internalerror(2008042701);
  2926. end;
  2927. {
  2928. ->
  2929. decw %si addw %dx,%si p
  2930. }
  2931. DebugMsg(SPeepholeOptimization + 'var3',p);
  2932. asml.remove(p);
  2933. asml.remove(hp2);
  2934. p.free;
  2935. hp2.free;
  2936. p:=hp1;
  2937. end
  2938. else if taicpu(p).opcode=A_MOVZX then
  2939. begin
  2940. { removes superfluous And's after movzx's }
  2941. if (taicpu(p).oper[1]^.typ = top_reg) and
  2942. GetNextInstruction(p, hp1) and
  2943. (tai(hp1).typ = ait_instruction) and
  2944. (taicpu(hp1).opcode = A_AND) and
  2945. (taicpu(hp1).oper[0]^.typ = top_const) and
  2946. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2947. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  2948. begin
  2949. case taicpu(p).opsize Of
  2950. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  2951. if (taicpu(hp1).oper[0]^.val = $ff) then
  2952. begin
  2953. DebugMsg(SPeepholeOptimization + 'var4',p);
  2954. asml.remove(hp1);
  2955. hp1.free;
  2956. end;
  2957. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  2958. if (taicpu(hp1).oper[0]^.val = $ffff) then
  2959. begin
  2960. DebugMsg(SPeepholeOptimization + 'var5',p);
  2961. asml.remove(hp1);
  2962. hp1.free;
  2963. end;
  2964. {$ifdef x86_64}
  2965. S_LQ:
  2966. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  2967. begin
  2968. if (cs_asm_source in current_settings.globalswitches) then
  2969. asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
  2970. asml.remove(hp1);
  2971. hp1.Free;
  2972. end;
  2973. {$endif x86_64}
  2974. end;
  2975. end;
  2976. { changes some movzx constructs to faster synonims (all examples
  2977. are given with eax/ax, but are also valid for other registers)}
  2978. if (taicpu(p).oper[1]^.typ = top_reg) then
  2979. if (taicpu(p).oper[0]^.typ = top_reg) then
  2980. case taicpu(p).opsize of
  2981. S_BW:
  2982. begin
  2983. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  2984. not(cs_opt_size in current_settings.optimizerswitches) then
  2985. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  2986. begin
  2987. taicpu(p).opcode := A_AND;
  2988. taicpu(p).changeopsize(S_W);
  2989. taicpu(p).loadConst(0,$ff);
  2990. DebugMsg(SPeepholeOptimization + 'var7',p);
  2991. end
  2992. else if GetNextInstruction(p, hp1) and
  2993. (tai(hp1).typ = ait_instruction) and
  2994. (taicpu(hp1).opcode = A_AND) and
  2995. (taicpu(hp1).oper[0]^.typ = top_const) and
  2996. (taicpu(hp1).oper[1]^.typ = top_reg) and
  2997. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  2998. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  2999. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  3000. begin
  3001. DebugMsg(SPeepholeOptimization + 'var8',p);
  3002. taicpu(p).opcode := A_MOV;
  3003. taicpu(p).changeopsize(S_W);
  3004. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  3005. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3006. end;
  3007. end;
  3008. S_BL:
  3009. begin
  3010. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3011. not(cs_opt_size in current_settings.optimizerswitches) then
  3012. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  3013. begin
  3014. taicpu(p).opcode := A_AND;
  3015. taicpu(p).changeopsize(S_L);
  3016. taicpu(p).loadConst(0,$ff)
  3017. end
  3018. else if GetNextInstruction(p, hp1) and
  3019. (tai(hp1).typ = ait_instruction) and
  3020. (taicpu(hp1).opcode = A_AND) and
  3021. (taicpu(hp1).oper[0]^.typ = top_const) and
  3022. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3023. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3024. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  3025. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  3026. begin
  3027. DebugMsg(SPeepholeOptimization + 'var10',p);
  3028. taicpu(p).opcode := A_MOV;
  3029. taicpu(p).changeopsize(S_L);
  3030. { do not use R_SUBWHOLE
  3031. as movl %rdx,%eax
  3032. is invalid in assembler PM }
  3033. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3034. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3035. end
  3036. end;
  3037. {$ifndef i8086}
  3038. S_WL:
  3039. begin
  3040. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  3041. not(cs_opt_size in current_settings.optimizerswitches) then
  3042. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  3043. begin
  3044. DebugMsg(SPeepholeOptimization + 'var11',p);
  3045. taicpu(p).opcode := A_AND;
  3046. taicpu(p).changeopsize(S_L);
  3047. taicpu(p).loadConst(0,$ffff);
  3048. end
  3049. else if GetNextInstruction(p, hp1) and
  3050. (tai(hp1).typ = ait_instruction) and
  3051. (taicpu(hp1).opcode = A_AND) and
  3052. (taicpu(hp1).oper[0]^.typ = top_const) and
  3053. (taicpu(hp1).oper[1]^.typ = top_reg) and
  3054. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3055. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  3056. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  3057. begin
  3058. DebugMsg(SPeepholeOptimization + 'var12',p);
  3059. taicpu(p).opcode := A_MOV;
  3060. taicpu(p).changeopsize(S_L);
  3061. { do not use R_SUBWHOLE
  3062. as movl %rdx,%eax
  3063. is invalid in assembler PM }
  3064. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3065. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  3066. end;
  3067. end;
  3068. {$endif i8086}
  3069. end
  3070. else if (taicpu(p).oper[0]^.typ = top_ref) then
  3071. begin
  3072. if GetNextInstruction(p, hp1) and
  3073. (tai(hp1).typ = ait_instruction) and
  3074. (taicpu(hp1).opcode = A_AND) and
  3075. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3076. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  3077. begin
  3078. taicpu(p).opcode := A_MOV;
  3079. case taicpu(p).opsize Of
  3080. S_BL:
  3081. begin
  3082. DebugMsg(SPeepholeOptimization + 'var13',p);
  3083. taicpu(p).changeopsize(S_L);
  3084. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3085. end;
  3086. S_WL:
  3087. begin
  3088. DebugMsg(SPeepholeOptimization + 'var14',p);
  3089. taicpu(p).changeopsize(S_L);
  3090. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  3091. end;
  3092. S_BW:
  3093. begin
  3094. DebugMsg(SPeepholeOptimization + 'var15',p);
  3095. taicpu(p).changeopsize(S_W);
  3096. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  3097. end;
  3098. {$ifdef x86_64}
  3099. S_BQ:
  3100. begin
  3101. DebugMsg(SPeepholeOptimization + 'var16',p);
  3102. taicpu(p).changeopsize(S_Q);
  3103. taicpu(hp1).loadConst(
  3104. 0, taicpu(hp1).oper[0]^.val and $ff);
  3105. end;
  3106. S_WQ:
  3107. begin
  3108. DebugMsg(SPeepholeOptimization + 'var17',p);
  3109. taicpu(p).changeopsize(S_Q);
  3110. taicpu(hp1).loadConst(0, taicpu(hp1).oper[0]^.val and $ffff);
  3111. end;
  3112. S_LQ:
  3113. begin
  3114. DebugMsg(SPeepholeOptimization + 'var18',p);
  3115. taicpu(p).changeopsize(S_Q);
  3116. taicpu(hp1).loadConst(
  3117. 0, taicpu(hp1).oper[0]^.val and $ffffffff);
  3118. end;
  3119. {$endif x86_64}
  3120. else
  3121. Internalerror(2017050704)
  3122. end;
  3123. end;
  3124. end;
  3125. end;
  3126. end;
  3127. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  3128. var
  3129. hp1 : tai;
  3130. MaskLength : Cardinal;
  3131. begin
  3132. Result:=false;
  3133. if GetNextInstruction(p, hp1) then
  3134. begin
  3135. if MatchOpType(taicpu(p),top_const,top_reg) and
  3136. MatchInstruction(hp1,A_AND,[]) and
  3137. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3138. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3139. { the second register must contain the first one, so compare their subreg types }
  3140. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  3141. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  3142. { change
  3143. and const1, reg
  3144. and const2, reg
  3145. to
  3146. and (const1 and const2), reg
  3147. }
  3148. begin
  3149. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  3150. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  3151. asml.remove(p);
  3152. p.Free;
  3153. p:=hp1;
  3154. Result:=true;
  3155. exit;
  3156. end
  3157. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3158. MatchInstruction(hp1,A_MOVZX,[]) and
  3159. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3160. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3161. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3162. (((taicpu(p).opsize=S_W) and
  3163. (taicpu(hp1).opsize=S_BW)) or
  3164. ((taicpu(p).opsize=S_L) and
  3165. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3166. {$ifdef x86_64}
  3167. or
  3168. ((taicpu(p).opsize=S_Q) and
  3169. (taicpu(hp1).opsize in [S_BQ,S_WQ]))
  3170. {$endif x86_64}
  3171. ) then
  3172. begin
  3173. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3174. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  3175. ) or
  3176. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3177. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  3178. then
  3179. begin
  3180. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  3181. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  3182. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  3183. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  3184. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  3185. }
  3186. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  3187. asml.remove(hp1);
  3188. hp1.free;
  3189. Exit;
  3190. end;
  3191. end
  3192. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3193. MatchInstruction(hp1,A_SHL,[]) and
  3194. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3195. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  3196. begin
  3197. {$ifopt R+}
  3198. {$define RANGE_WAS_ON}
  3199. {$R-}
  3200. {$endif}
  3201. { get length of potential and mask }
  3202. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  3203. { really a mask? }
  3204. {$ifdef RANGE_WAS_ON}
  3205. {$R+}
  3206. {$endif}
  3207. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  3208. { unmasked part shifted out? }
  3209. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  3210. begin
  3211. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  3212. { take care of the register (de)allocs following p }
  3213. UpdateUsedRegs(tai(p.next));
  3214. asml.remove(p);
  3215. p.free;
  3216. p:=hp1;
  3217. Result:=true;
  3218. exit;
  3219. end;
  3220. end
  3221. else if MatchOpType(taicpu(p),top_const,top_reg) and
  3222. MatchInstruction(hp1,A_MOVSX{$ifdef x86_64},A_MOVSXD{$endif x86_64},[]) and
  3223. (taicpu(hp1).oper[0]^.typ = top_reg) and
  3224. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  3225. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  3226. (((taicpu(p).opsize=S_W) and
  3227. (taicpu(hp1).opsize=S_BW)) or
  3228. ((taicpu(p).opsize=S_L) and
  3229. (taicpu(hp1).opsize in [S_WL,S_BL]))
  3230. {$ifdef x86_64}
  3231. or
  3232. ((taicpu(p).opsize=S_Q) and
  3233. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_LQ]))
  3234. {$endif x86_64}
  3235. ) then
  3236. begin
  3237. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  3238. ((taicpu(p).oper[0]^.val and $7f)=taicpu(p).oper[0]^.val)
  3239. ) or
  3240. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  3241. ((taicpu(p).oper[0]^.val and $7fff)=taicpu(p).oper[0]^.val))
  3242. {$ifdef x86_64}
  3243. or
  3244. (((taicpu(hp1).opsize)=S_LQ) and
  3245. ((taicpu(p).oper[0]^.val and $7fffffff)=taicpu(p).oper[0]^.val)
  3246. )
  3247. {$endif x86_64}
  3248. then
  3249. begin
  3250. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  3251. asml.remove(hp1);
  3252. hp1.free;
  3253. Exit;
  3254. end;
  3255. end
  3256. else if (taicpu(p).oper[1]^.typ = top_reg) and
  3257. (hp1.typ = ait_instruction) and
  3258. (taicpu(hp1).is_jmp) and
  3259. (taicpu(hp1).opcode<>A_JMP) and
  3260. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  3261. begin
  3262. { change
  3263. and x, reg
  3264. jxx
  3265. to
  3266. test x, reg
  3267. jxx
  3268. if reg is deallocated before the
  3269. jump, but only if it's a conditional jump (PFV)
  3270. }
  3271. taicpu(p).opcode := A_TEST;
  3272. Exit;
  3273. end;
  3274. end;
  3275. { Lone AND tests }
  3276. if MatchOpType(taicpu(p),top_const,top_reg) then
  3277. begin
  3278. {
  3279. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  3280. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  3281. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  3282. }
  3283. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  3284. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  3285. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  3286. begin
  3287. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg)
  3288. end;
  3289. end;
  3290. end;
  3291. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  3292. begin
  3293. Result:=false;
  3294. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3295. MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  3296. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  3297. begin
  3298. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  3299. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  3300. taicpu(p).opcode:=A_ADD;
  3301. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  3302. result:=true;
  3303. end
  3304. else if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  3305. MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  3306. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  3307. begin
  3308. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  3309. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  3310. taicpu(p).opcode:=A_ADD;
  3311. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  3312. result:=true;
  3313. end;
  3314. end;
  3315. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  3316. var
  3317. Value, RegName: string;
  3318. begin
  3319. Result:=false;
  3320. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  3321. begin
  3322. case taicpu(p).oper[0]^.val of
  3323. 0:
  3324. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  3325. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  3326. begin
  3327. { change "mov $0,%reg" into "xor %reg,%reg" }
  3328. taicpu(p).opcode := A_XOR;
  3329. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  3330. Result := True;
  3331. end;
  3332. $1..$FFFFFFFF:
  3333. begin
  3334. { Code size reduction by J. Gareth "Kit" Moreton }
  3335. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  3336. case taicpu(p).opsize of
  3337. S_Q:
  3338. begin
  3339. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  3340. Value := debug_tostr(taicpu(p).oper[0]^.val);
  3341. { The actual optimization }
  3342. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3343. taicpu(p).changeopsize(S_L);
  3344. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  3345. Result := True;
  3346. end;
  3347. end;
  3348. end;
  3349. end;
  3350. end;
  3351. end;
  3352. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  3353. begin
  3354. Result:=false;
  3355. { change "cmp $0, %reg" to "test %reg, %reg" }
  3356. if MatchOpType(taicpu(p),top_const,top_reg) and
  3357. (taicpu(p).oper[0]^.val = 0) then
  3358. begin
  3359. taicpu(p).opcode := A_TEST;
  3360. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  3361. Result:=true;
  3362. end;
  3363. end;
  3364. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  3365. var
  3366. IsTestConstX : Boolean;
  3367. hp1,hp2 : tai;
  3368. begin
  3369. Result:=false;
  3370. { removes the line marked with (x) from the sequence
  3371. and/or/xor/add/sub/... $x, %y
  3372. test/or %y, %y | test $-1, %y (x)
  3373. j(n)z _Label
  3374. as the first instruction already adjusts the ZF
  3375. %y operand may also be a reference }
  3376. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  3377. MatchOperand(taicpu(p).oper[0]^,-1);
  3378. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  3379. GetLastInstruction(p, hp1) and
  3380. (tai(hp1).typ = ait_instruction) and
  3381. GetNextInstruction(p,hp2) and
  3382. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  3383. case taicpu(hp1).opcode Of
  3384. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  3385. begin
  3386. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  3387. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3388. { and in case of carry for A(E)/B(E)/C/NC }
  3389. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  3390. ((taicpu(hp1).opcode <> A_ADD) and
  3391. (taicpu(hp1).opcode <> A_SUB))) then
  3392. begin
  3393. hp1 := tai(p.next);
  3394. asml.remove(p);
  3395. p.free;
  3396. p := tai(hp1);
  3397. Result:=true;
  3398. end;
  3399. end;
  3400. A_SHL, A_SAL, A_SHR, A_SAR:
  3401. begin
  3402. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  3403. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  3404. { therefore, it's only safe to do this optimization for }
  3405. { shifts by a (nonzero) constant }
  3406. (taicpu(hp1).oper[0]^.typ = top_const) and
  3407. (taicpu(hp1).oper[0]^.val <> 0) and
  3408. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3409. { and in case of carry for A(E)/B(E)/C/NC }
  3410. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  3411. begin
  3412. hp1 := tai(p.next);
  3413. asml.remove(p);
  3414. p.free;
  3415. p := tai(hp1);
  3416. Result:=true;
  3417. end;
  3418. end;
  3419. A_DEC, A_INC, A_NEG:
  3420. begin
  3421. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  3422. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  3423. { and in case of carry for A(E)/B(E)/C/NC }
  3424. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  3425. begin
  3426. case taicpu(hp1).opcode Of
  3427. A_DEC, A_INC:
  3428. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  3429. begin
  3430. case taicpu(hp1).opcode Of
  3431. A_DEC: taicpu(hp1).opcode := A_SUB;
  3432. A_INC: taicpu(hp1).opcode := A_ADD;
  3433. end;
  3434. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  3435. taicpu(hp1).loadConst(0,1);
  3436. taicpu(hp1).ops:=2;
  3437. end
  3438. end;
  3439. hp1 := tai(p.next);
  3440. asml.remove(p);
  3441. p.free;
  3442. p := tai(hp1);
  3443. Result:=true;
  3444. end;
  3445. end
  3446. else
  3447. { change "test $-1,%reg" into "test %reg,%reg" }
  3448. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  3449. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  3450. end { case }
  3451. { change "test $-1,%reg" into "test %reg,%reg" }
  3452. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  3453. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  3454. end;
  3455. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  3456. var
  3457. hp1 : tai;
  3458. {$ifndef x86_64}
  3459. hp2 : taicpu;
  3460. {$endif x86_64}
  3461. begin
  3462. Result:=false;
  3463. {$ifndef x86_64}
  3464. { don't do this on modern CPUs, this really hurts them due to
  3465. broken call/ret pairing }
  3466. if (current_settings.optimizecputype < cpu_Pentium2) and
  3467. not(cs_create_pic in current_settings.moduleswitches) and
  3468. GetNextInstruction(p, hp1) and
  3469. MatchInstruction(hp1,A_JMP,[S_NO]) and
  3470. MatchOpType(taicpu(hp1),top_ref) and
  3471. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  3472. begin
  3473. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  3474. InsertLLItem(p.previous, p, hp2);
  3475. taicpu(p).opcode := A_JMP;
  3476. taicpu(p).is_jmp := true;
  3477. asml.remove(hp1);
  3478. hp1.free;
  3479. Result:=true;
  3480. end
  3481. else
  3482. {$endif x86_64}
  3483. { replace
  3484. call procname
  3485. ret
  3486. by
  3487. jmp procname
  3488. this should never hurt except when pic is used, not sure
  3489. how to handle it then
  3490. but do it only on level 4 because it destroys stack back traces
  3491. }
  3492. if (cs_opt_level4 in current_settings.optimizerswitches) and
  3493. not(cs_create_pic in current_settings.moduleswitches) and
  3494. GetNextInstruction(p, hp1) and
  3495. MatchInstruction(hp1,A_RET,[S_NO]) and
  3496. (taicpu(hp1).ops=0) then
  3497. begin
  3498. taicpu(p).opcode := A_JMP;
  3499. taicpu(p).is_jmp := true;
  3500. asml.remove(hp1);
  3501. hp1.free;
  3502. Result:=true;
  3503. end;
  3504. end;
  3505. {$ifdef x86_64}
  3506. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  3507. var
  3508. PreMessage: string;
  3509. begin
  3510. Result := False;
  3511. { Code size reduction by J. Gareth "Kit" Moreton }
  3512. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  3513. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  3514. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  3515. then
  3516. begin
  3517. { Has 64-bit register name and opcode suffix }
  3518. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  3519. { The actual optimization }
  3520. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3521. if taicpu(p).opsize = S_BQ then
  3522. taicpu(p).changeopsize(S_BL)
  3523. else
  3524. taicpu(p).changeopsize(S_WL);
  3525. DebugMsg(SPeepholeOptimization + PreMessage +
  3526. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  3527. end;
  3528. end;
  3529. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  3530. var
  3531. PreMessage, RegName: string;
  3532. begin
  3533. { Code size reduction by J. Gareth "Kit" Moreton }
  3534. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  3535. as this removes the REX prefix }
  3536. Result := False;
  3537. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  3538. Exit;
  3539. if taicpu(p).oper[0]^.typ <> top_reg then
  3540. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  3541. InternalError(2018011500);
  3542. case taicpu(p).opsize of
  3543. S_Q:
  3544. if (getsupreg(taicpu(p).oper[0]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP]) then
  3545. begin
  3546. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  3547. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  3548. { The actual optimization }
  3549. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  3550. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  3551. taicpu(p).changeopsize(S_L);
  3552. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  3553. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  3554. end;
  3555. end;
  3556. end;
  3557. {$endif}
  3558. procedure TX86AsmOptimizer.OptReferences;
  3559. var
  3560. p: tai;
  3561. i: Integer;
  3562. begin
  3563. p := BlockStart;
  3564. while (p <> BlockEnd) Do
  3565. begin
  3566. if p.typ=ait_instruction then
  3567. begin
  3568. for i:=0 to taicpu(p).ops-1 do
  3569. if taicpu(p).oper[i]^.typ=top_ref then
  3570. optimize_ref(taicpu(p).oper[i]^.ref^,false);
  3571. end;
  3572. p:=tai(p.next);
  3573. end;
  3574. end;
  3575. end.