aoptx86.pas 226 KB

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