aoptx86.pas 185 KB

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