aoptx86.pas 251 KB

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