aoptx86.pas 250 KB

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