aoptx86.pas 251 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912
  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. TransferUsedRegs(TmpUsedRegs);
  3635. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3636. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3637. if not GetNextInstruction(hp1, hp2) or
  3638. (
  3639. { The FLAGS register isn't always tracked properly, so do not
  3640. perform this optimisation if a conditional statement follows }
  3641. not RegReadByInstruction(NR_DEFAULTFLAGS, hp2) and
  3642. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)
  3643. ) then
  3644. begin
  3645. reference_reset(NewRef, 1, []);
  3646. NewRef.base := taicpu(p).oper[0]^.reg;
  3647. NewRef.scalefactor := 1;
  3648. if taicpu(hp1).opcode = A_ADD then
  3649. begin
  3650. DebugMsg(SPeepholeOptimization + 'MovAdd2Lea', p);
  3651. NewRef.offset := taicpu(hp1).oper[0]^.val;
  3652. end
  3653. else
  3654. begin
  3655. DebugMsg(SPeepholeOptimization + 'MovSub2Lea', p);
  3656. NewRef.offset := -taicpu(hp1).oper[0]^.val;
  3657. end;
  3658. taicpu(p).opcode := A_LEA;
  3659. taicpu(p).loadref(0, NewRef);
  3660. Asml.Remove(hp1);
  3661. hp1.Free;
  3662. Result := True;
  3663. Exit;
  3664. end;
  3665. end
  3666. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3667. {$ifdef x86_64}
  3668. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  3669. {$else x86_64}
  3670. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  3671. {$endif x86_64}
  3672. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3673. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  3674. { mov reg1, reg2 mov reg1, reg2
  3675. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  3676. begin
  3677. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  3678. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  3679. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  3680. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  3681. TransferUsedRegs(TmpUsedRegs);
  3682. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3683. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  3684. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  3685. then
  3686. begin
  3687. asml.remove(p);
  3688. p.free;
  3689. p := hp1;
  3690. Result:=true;
  3691. end;
  3692. exit;
  3693. end
  3694. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3695. IsXCHGAcceptable and
  3696. { XCHG doesn't support 8-byte registers }
  3697. (taicpu(p).opsize <> S_B) and
  3698. MatchInstruction(hp1, A_MOV, []) and
  3699. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3700. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  3701. GetNextInstruction(hp1, hp2) and
  3702. MatchInstruction(hp2, A_MOV, []) and
  3703. { Don't need to call MatchOpType for hp2 because the operand matches below cover for it }
  3704. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  3705. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) then
  3706. begin
  3707. { mov %reg1,%reg2
  3708. mov %reg3,%reg1 -> xchg %reg3,%reg1
  3709. mov %reg2,%reg3
  3710. (%reg2 not used afterwards)
  3711. Note that xchg takes 3 cycles to execute, and generally mov's take
  3712. only one cycle apiece, but the first two mov's can be executed in
  3713. parallel, only taking 2 cycles overall. Older processors should
  3714. therefore only optimise for size. [Kit]
  3715. }
  3716. TransferUsedRegs(TmpUsedRegs);
  3717. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3718. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3719. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs) then
  3720. begin
  3721. DebugMsg(SPeepholeOptimization + 'MovMovMov2XChg', p);
  3722. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp1, UsedRegs);
  3723. taicpu(hp1).opcode := A_XCHG;
  3724. asml.Remove(p);
  3725. asml.Remove(hp2);
  3726. p.Free;
  3727. hp2.Free;
  3728. p := hp1;
  3729. Result := True;
  3730. Exit;
  3731. end;
  3732. end
  3733. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3734. {$ifdef x86_64}
  3735. MatchInstruction(hp1,[A_MOV,A_MOVZX,A_MOVSX,A_MOVSXD],[]) and
  3736. {$else x86_64}
  3737. MatchInstruction(hp1,A_MOV,A_MOVZX,A_MOVSX,[]) and
  3738. {$endif x86_64}
  3739. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  3740. ((taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg)
  3741. or
  3742. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg)
  3743. ) and
  3744. (getsupreg(taicpu(hp1).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) then
  3745. { mov reg1, reg2
  3746. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  3747. begin
  3748. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) then
  3749. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[0]^.reg;
  3750. if (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) then
  3751. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  3752. DebugMsg(SPeepholeOptimization + 'MovMovXX2MoVXX 1 done',p);
  3753. asml.remove(p);
  3754. p.free;
  3755. p := hp1;
  3756. Result:=true;
  3757. exit;
  3758. end
  3759. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  3760. MatchInstruction(hp1, A_SAR, []) then
  3761. begin
  3762. if MatchOperand(taicpu(hp1).oper[0]^, 31) then
  3763. begin
  3764. { the use of %edx also covers the opsize being S_L }
  3765. if MatchOperand(taicpu(hp1).oper[1]^, NR_EDX) then
  3766. begin
  3767. { Note it has to be specifically "movl %eax,%edx", and those specific sub-registers }
  3768. if (taicpu(p).oper[0]^.reg = NR_EAX) and
  3769. (taicpu(p).oper[1]^.reg = NR_EDX) then
  3770. begin
  3771. { Change:
  3772. movl %eax,%edx
  3773. sarl $31,%edx
  3774. To:
  3775. cltd
  3776. }
  3777. DebugMsg(SPeepholeOptimization + 'MovSar2Cltd', p);
  3778. Asml.Remove(hp1);
  3779. hp1.Free;
  3780. taicpu(p).opcode := A_CDQ;
  3781. taicpu(p).opsize := S_NO;
  3782. taicpu(p).clearop(1);
  3783. taicpu(p).clearop(0);
  3784. taicpu(p).ops:=0;
  3785. Result := True;
  3786. end
  3787. else if (cs_opt_size in current_settings.optimizerswitches) and
  3788. (taicpu(p).oper[0]^.reg = NR_EDX) and
  3789. (taicpu(p).oper[1]^.reg = NR_EAX) then
  3790. begin
  3791. { Change:
  3792. movl %edx,%eax
  3793. sarl $31,%edx
  3794. To:
  3795. movl %edx,%eax
  3796. cltd
  3797. Note that this creates a dependency between the two instructions,
  3798. so only perform if optimising for size.
  3799. }
  3800. DebugMsg(SPeepholeOptimization + 'MovSar2MovCltd', p);
  3801. taicpu(hp1).opcode := A_CDQ;
  3802. taicpu(hp1).opsize := S_NO;
  3803. taicpu(hp1).clearop(1);
  3804. taicpu(hp1).clearop(0);
  3805. taicpu(hp1).ops:=0;
  3806. end;
  3807. {$ifndef x86_64}
  3808. end
  3809. { Don't bother if CMOV is supported, because a more optimal
  3810. sequence would have been generated for the Abs() intrinsic }
  3811. else if not(CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and
  3812. { the use of %eax also covers the opsize being S_L }
  3813. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) and
  3814. (taicpu(p).oper[0]^.reg = NR_EAX) and
  3815. (taicpu(p).oper[1]^.reg = NR_EDX) and
  3816. GetNextInstruction(hp1, hp2) and
  3817. MatchInstruction(hp2, A_XOR, [S_L]) and
  3818. MatchOperand(taicpu(hp2).oper[0]^, NR_EAX) and
  3819. MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) and
  3820. GetNextInstruction(hp2, hp3) and
  3821. MatchInstruction(hp3, A_SUB, [S_L]) and
  3822. MatchOperand(taicpu(hp3).oper[0]^, NR_EAX) and
  3823. MatchOperand(taicpu(hp3).oper[1]^, NR_EDX) then
  3824. begin
  3825. { Change:
  3826. movl %eax,%edx
  3827. sarl $31,%eax
  3828. xorl %eax,%edx
  3829. subl %eax,%edx
  3830. (Instruction that uses %edx)
  3831. (%eax deallocated)
  3832. (%edx deallocated)
  3833. To:
  3834. cltd
  3835. xorl %edx,%eax <-- Note the registers have swapped
  3836. subl %edx,%eax
  3837. (Instruction that uses %eax) <-- %eax rather than %edx
  3838. }
  3839. TransferUsedRegs(TmpUsedRegs);
  3840. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3841. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3842. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  3843. if not RegUsedAfterInstruction(NR_EAX, hp3, TmpUsedRegs) then
  3844. begin
  3845. if GetNextInstruction(hp3, hp4) and
  3846. not RegModifiedByInstruction(NR_EDX, hp4) and
  3847. not RegUsedAfterInstruction(NR_EDX, hp4, TmpUsedRegs) then
  3848. begin
  3849. DebugMsg(SPeepholeOptimization + 'abs() intrinsic optimisation', p);
  3850. taicpu(p).opcode := A_CDQ;
  3851. taicpu(p).clearop(1);
  3852. taicpu(p).clearop(0);
  3853. taicpu(p).ops:=0;
  3854. AsmL.Remove(hp1);
  3855. hp1.Free;
  3856. taicpu(hp2).loadreg(0, NR_EDX);
  3857. taicpu(hp2).loadreg(1, NR_EAX);
  3858. taicpu(hp3).loadreg(0, NR_EDX);
  3859. taicpu(hp3).loadreg(1, NR_EAX);
  3860. AllocRegBetween(NR_EAX, hp3, hp4, TmpUsedRegs);
  3861. { Convert references in the following instruction (hp4) from %edx to %eax }
  3862. for OperIdx := 0 to taicpu(hp4).ops - 1 do
  3863. with taicpu(hp4).oper[OperIdx]^ do
  3864. case typ of
  3865. top_reg:
  3866. if reg = NR_EDX then
  3867. reg := NR_EAX;
  3868. top_ref:
  3869. begin
  3870. if ref^.base = NR_EDX then
  3871. ref^.base := NR_EAX;
  3872. if ref^.index = NR_EDX then
  3873. ref^.index := NR_EAX;
  3874. end;
  3875. else
  3876. ;
  3877. end;
  3878. end;
  3879. end;
  3880. {$else x86_64}
  3881. end;
  3882. end
  3883. else if MatchOperand(taicpu(hp1).oper[0]^, 63) and
  3884. { the use of %rdx also covers the opsize being S_Q }
  3885. MatchOperand(taicpu(hp1).oper[1]^, NR_RDX) then
  3886. begin
  3887. { Note it has to be specifically "movq %rax,%rdx", and those specific sub-registers }
  3888. if (taicpu(p).oper[0]^.reg = NR_RAX) and
  3889. (taicpu(p).oper[1]^.reg = NR_RDX) then
  3890. begin
  3891. { Change:
  3892. movq %rax,%rdx
  3893. sarq $63,%rdx
  3894. To:
  3895. cqto
  3896. }
  3897. DebugMsg(SPeepholeOptimization + 'MovSar2Cqto', p);
  3898. Asml.Remove(hp1);
  3899. hp1.Free;
  3900. taicpu(p).opcode := A_CQO;
  3901. taicpu(p).opsize := S_NO;
  3902. taicpu(p).clearop(1);
  3903. taicpu(p).clearop(0);
  3904. taicpu(p).ops:=0;
  3905. Result := True;
  3906. end
  3907. else if (cs_opt_size in current_settings.optimizerswitches) and
  3908. (taicpu(p).oper[0]^.reg = NR_RDX) and
  3909. (taicpu(p).oper[1]^.reg = NR_RAX) then
  3910. begin
  3911. { Change:
  3912. movq %rdx,%rax
  3913. sarq $63,%rdx
  3914. To:
  3915. movq %rdx,%rax
  3916. cqto
  3917. Note that this creates a dependency between the two instructions,
  3918. so only perform if optimising for size.
  3919. }
  3920. DebugMsg(SPeepholeOptimization + 'MovSar2MovCqto', p);
  3921. taicpu(hp1).opcode := A_CQO;
  3922. taicpu(hp1).opsize := S_NO;
  3923. taicpu(hp1).clearop(1);
  3924. taicpu(hp1).clearop(0);
  3925. taicpu(hp1).ops:=0;
  3926. {$endif x86_64}
  3927. end;
  3928. end;
  3929. end
  3930. else if MatchInstruction(hp1, A_MOV, []) and
  3931. (taicpu(hp1).oper[1]^.typ = top_reg) then
  3932. { Though "GetNextInstruction" could be factored out, along with
  3933. the instructions that depend on hp2, it is an expensive call that
  3934. should be delayed for as long as possible, hence we do cheaper
  3935. checks first that are likely to be False. [Kit] }
  3936. begin
  3937. if MatchOperand(taicpu(p).oper[1]^, NR_EDX) and
  3938. (
  3939. (
  3940. (taicpu(hp1).oper[1]^.reg = NR_EAX) and
  3941. (
  3942. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  3943. MatchOperand(taicpu(hp1).oper[0]^, NR_EDX)
  3944. )
  3945. ) or
  3946. (
  3947. (taicpu(hp1).oper[1]^.reg = NR_EDX) and
  3948. (
  3949. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  3950. MatchOperand(taicpu(hp1).oper[0]^, NR_EAX)
  3951. )
  3952. )
  3953. ) and
  3954. GetNextInstruction(hp1, hp2) and
  3955. MatchInstruction(hp2, A_SAR, []) and
  3956. MatchOperand(taicpu(hp2).oper[0]^, 31) then
  3957. begin
  3958. if MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) then
  3959. begin
  3960. { Change:
  3961. movl r/m,%edx movl r/m,%eax movl r/m,%edx movl r/m,%eax
  3962. movl %edx,%eax or movl %eax,%edx or movl r/m,%eax or movl r/m,%edx
  3963. sarl $31,%edx sarl $31,%edx sarl $31,%edx sarl $31,%edx
  3964. To:
  3965. movl r/m,%eax <- Note the change in register
  3966. cltd
  3967. }
  3968. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCltd', p);
  3969. AllocRegBetween(NR_EAX, p, hp1, UsedRegs);
  3970. taicpu(p).loadreg(1, NR_EAX);
  3971. taicpu(hp1).opcode := A_CDQ;
  3972. taicpu(hp1).clearop(1);
  3973. taicpu(hp1).clearop(0);
  3974. taicpu(hp1).ops:=0;
  3975. AsmL.Remove(hp2);
  3976. hp2.Free;
  3977. (*
  3978. {$ifdef x86_64}
  3979. end
  3980. else if MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) and
  3981. { This code sequence does not get generated - however it might become useful
  3982. if and when 128-bit signed integer types make an appearance, so the code
  3983. is kept here for when it is eventually needed. [Kit] }
  3984. (
  3985. (
  3986. (taicpu(hp1).oper[1]^.reg = NR_RAX) and
  3987. (
  3988. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  3989. MatchOperand(taicpu(hp1).oper[0]^, NR_RDX)
  3990. )
  3991. ) or
  3992. (
  3993. (taicpu(hp1).oper[1]^.reg = NR_RDX) and
  3994. (
  3995. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  3996. MatchOperand(taicpu(hp1).oper[0]^, NR_RAX)
  3997. )
  3998. )
  3999. ) and
  4000. GetNextInstruction(hp1, hp2) and
  4001. MatchInstruction(hp2, A_SAR, [S_Q]) and
  4002. MatchOperand(taicpu(hp2).oper[0]^, 63) and
  4003. MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) then
  4004. begin
  4005. { Change:
  4006. movq r/m,%rdx movq r/m,%rax movq r/m,%rdx movq r/m,%rax
  4007. movq %rdx,%rax or movq %rax,%rdx or movq r/m,%rax or movq r/m,%rdx
  4008. sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx
  4009. To:
  4010. movq r/m,%rax <- Note the change in register
  4011. cqto
  4012. }
  4013. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCqto', p);
  4014. AllocRegBetween(NR_RAX, p, hp1, UsedRegs);
  4015. taicpu(p).loadreg(1, NR_RAX);
  4016. taicpu(hp1).opcode := A_CQO;
  4017. taicpu(hp1).clearop(1);
  4018. taicpu(hp1).clearop(0);
  4019. taicpu(hp1).ops:=0;
  4020. AsmL.Remove(hp2);
  4021. hp2.Free;
  4022. {$endif x86_64}
  4023. *)
  4024. end;
  4025. end;
  4026. end
  4027. else if (taicpu(p).oper[0]^.typ = top_ref) and
  4028. (hp1.typ = ait_instruction) and
  4029. { while the GetNextInstruction(hp1,hp2) call could be factored out,
  4030. doing it separately in both branches allows to do the cheap checks
  4031. with low probability earlier }
  4032. ((IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  4033. GetNextInstruction(hp1,hp2) and
  4034. MatchInstruction(hp2,A_MOV,[])
  4035. ) or
  4036. ((taicpu(hp1).opcode=A_LEA) and
  4037. GetNextInstruction(hp1,hp2) and
  4038. MatchInstruction(hp2,A_MOV,[]) and
  4039. ((MatchReference(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  4040. (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg)
  4041. ) or
  4042. (MatchReference(taicpu(hp1).oper[0]^.ref^,NR_INVALID,
  4043. taicpu(p).oper[1]^.reg) and
  4044. (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg)) or
  4045. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_NO)) or
  4046. (MatchReferenceWithOffset(taicpu(hp1).oper[0]^.ref^,NR_NO,taicpu(p).oper[1]^.reg))
  4047. ) and
  4048. ((MatchOperand(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^)) or not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)))
  4049. )
  4050. ) and
  4051. MatchOperand(taicpu(hp1).oper[taicpu(hp1).ops-1]^,taicpu(hp2).oper[0]^) and
  4052. (taicpu(hp2).oper[1]^.typ = top_ref) then
  4053. begin
  4054. TransferUsedRegs(TmpUsedRegs);
  4055. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  4056. UpdateUsedRegs(TmpUsedRegs,tai(hp1.next));
  4057. if (RefsEqual(taicpu(hp2).oper[1]^.ref^,taicpu(p).oper[0]^.ref^) and
  4058. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,TmpUsedRegs))) then
  4059. { change mov (ref), reg
  4060. add/sub/or/... reg2/$const, reg
  4061. mov reg, (ref)
  4062. # release reg
  4063. to add/sub/or/... reg2/$const, (ref) }
  4064. begin
  4065. case taicpu(hp1).opcode of
  4066. A_INC,A_DEC,A_NOT,A_NEG :
  4067. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  4068. A_LEA :
  4069. begin
  4070. taicpu(hp1).opcode:=A_ADD;
  4071. if (taicpu(hp1).oper[0]^.ref^.index<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.index<>NR_NO) then
  4072. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.index)
  4073. else if (taicpu(hp1).oper[0]^.ref^.base<>taicpu(p).oper[1]^.reg) and (taicpu(hp1).oper[0]^.ref^.base<>NR_NO) then
  4074. taicpu(hp1).loadreg(0,taicpu(hp1).oper[0]^.ref^.base)
  4075. else
  4076. taicpu(hp1).loadconst(0,taicpu(hp1).oper[0]^.ref^.offset);
  4077. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  4078. DebugMsg(SPeepholeOptimization + 'FoldLea done',hp1);
  4079. end
  4080. else
  4081. taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
  4082. end;
  4083. asml.remove(p);
  4084. asml.remove(hp2);
  4085. p.free;
  4086. hp2.free;
  4087. p := hp1
  4088. end;
  4089. Exit;
  4090. {$ifdef x86_64}
  4091. end
  4092. else if (taicpu(p).opsize = S_L) and
  4093. (taicpu(p).oper[1]^.typ = top_reg) and
  4094. (
  4095. MatchInstruction(hp1, A_MOV,[]) and
  4096. (taicpu(hp1).opsize = S_L) and
  4097. (taicpu(hp1).oper[1]^.typ = top_reg)
  4098. ) and (
  4099. GetNextInstruction(hp1, hp2) and
  4100. (tai(hp2).typ=ait_instruction) and
  4101. (taicpu(hp2).opsize = S_Q) and
  4102. (
  4103. (
  4104. MatchInstruction(hp2, A_ADD,[]) and
  4105. (taicpu(hp2).opsize = S_Q) and
  4106. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4107. (
  4108. (
  4109. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4110. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4111. ) or (
  4112. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4113. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4114. )
  4115. )
  4116. ) or (
  4117. MatchInstruction(hp2, A_LEA,[]) and
  4118. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  4119. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  4120. (
  4121. (
  4122. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4123. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4124. ) or (
  4125. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4126. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  4127. )
  4128. ) and (
  4129. (
  4130. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4131. ) or (
  4132. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4133. )
  4134. )
  4135. )
  4136. )
  4137. ) and (
  4138. GetNextInstruction(hp2, hp3) and
  4139. MatchInstruction(hp3, A_SHR,[]) and
  4140. (taicpu(hp3).opsize = S_Q) and
  4141. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4142. (taicpu(hp3).oper[0]^.val = 1) and
  4143. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  4144. ) then
  4145. begin
  4146. { Change movl x, reg1d movl x, reg1d
  4147. movl y, reg2d movl y, reg2d
  4148. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  4149. shrq $1, reg1q shrq $1, reg1q
  4150. ( reg1d and reg2d can be switched around in the first two instructions )
  4151. To movl x, reg1d
  4152. addl y, reg1d
  4153. rcrl $1, reg1d
  4154. This corresponds to the common expression (x + y) shr 1, where
  4155. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  4156. smaller code, but won't account for x + y causing an overflow). [Kit]
  4157. }
  4158. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  4159. { Change first MOV command to have the same register as the final output }
  4160. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  4161. else
  4162. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  4163. { Change second MOV command to an ADD command. This is easier than
  4164. converting the existing command because it means we don't have to
  4165. touch 'y', which might be a complicated reference, and also the
  4166. fact that the third command might either be ADD or LEA. [Kit] }
  4167. taicpu(hp1).opcode := A_ADD;
  4168. { Delete old ADD/LEA instruction }
  4169. asml.remove(hp2);
  4170. hp2.free;
  4171. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  4172. taicpu(hp3).opcode := A_RCR;
  4173. taicpu(hp3).changeopsize(S_L);
  4174. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  4175. {$endif x86_64}
  4176. end;
  4177. end;
  4178. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  4179. var
  4180. hp1 : tai;
  4181. begin
  4182. Result:=false;
  4183. if (taicpu(p).ops >= 2) and
  4184. ((taicpu(p).oper[0]^.typ = top_const) or
  4185. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  4186. (taicpu(p).oper[1]^.typ = top_reg) and
  4187. ((taicpu(p).ops = 2) or
  4188. ((taicpu(p).oper[2]^.typ = top_reg) and
  4189. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  4190. GetLastInstruction(p,hp1) and
  4191. MatchInstruction(hp1,A_MOV,[]) and
  4192. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  4193. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4194. begin
  4195. TransferUsedRegs(TmpUsedRegs);
  4196. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) or
  4197. ((taicpu(p).ops = 3) and (taicpu(p).oper[1]^.reg=taicpu(p).oper[2]^.reg)) then
  4198. { change
  4199. mov reg1,reg2
  4200. imul y,reg2 to imul y,reg1,reg2 }
  4201. begin
  4202. taicpu(p).ops := 3;
  4203. taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
  4204. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  4205. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  4206. asml.remove(hp1);
  4207. hp1.free;
  4208. result:=true;
  4209. end;
  4210. end;
  4211. end;
  4212. procedure TX86AsmOptimizer.ConvertJumpToRET(const p: tai; const ret_p: tai);
  4213. var
  4214. ThisLabel: TAsmLabel;
  4215. begin
  4216. ThisLabel := tasmlabel(taicpu(p).oper[0]^.ref^.symbol);
  4217. ThisLabel.decrefs;
  4218. taicpu(p).opcode := A_RET;
  4219. taicpu(p).is_jmp := false;
  4220. taicpu(p).ops := taicpu(ret_p).ops;
  4221. case taicpu(ret_p).ops of
  4222. 0:
  4223. taicpu(p).clearop(0);
  4224. 1:
  4225. taicpu(p).loadconst(0,taicpu(ret_p).oper[0]^.val);
  4226. else
  4227. internalerror(2016041301);
  4228. end;
  4229. { If the original label is now dead, it might turn out that the label
  4230. immediately follows p. As a result, everything beyond it, which will
  4231. be just some final register configuration and a RET instruction, is
  4232. now dead code. [Kit] }
  4233. { NOTE: This is much faster than introducing a OptPass2RET routine and
  4234. running RemoveDeadCodeAfterJump for each RET instruction, because
  4235. this optimisation rarely happens and most RETs appear at the end of
  4236. routines where there is nothing that can be stripped. [Kit] }
  4237. if not ThisLabel.is_used then
  4238. RemoveDeadCodeAfterJump(p);
  4239. end;
  4240. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  4241. var
  4242. hp1, hp2, hp3: tai;
  4243. OperIdx: Integer;
  4244. begin
  4245. result:=false;
  4246. 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
  4247. (taicpu(p).oper[0]^.ref^.index=NR_NO) and (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) then
  4248. begin
  4249. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  4250. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and (hp1.typ = ait_instruction) then
  4251. begin
  4252. case taicpu(hp1).opcode of
  4253. A_RET:
  4254. {
  4255. change
  4256. jmp .L1
  4257. ...
  4258. .L1:
  4259. ret
  4260. into
  4261. ret
  4262. }
  4263. begin
  4264. ConvertJumpToRET(p, hp1);
  4265. result:=true;
  4266. end;
  4267. A_MOV:
  4268. {
  4269. change
  4270. jmp .L1
  4271. ...
  4272. .L1:
  4273. mov ##, ##
  4274. ret
  4275. into
  4276. mov ##, ##
  4277. ret
  4278. }
  4279. { This optimisation tends to increase code size if the pass 1 MOV optimisations aren't
  4280. re-run, so only do this particular optimisation if optimising for speed or when
  4281. optimisations are very in-depth. [Kit] }
  4282. if (current_settings.optimizerswitches * [cs_opt_level3, cs_opt_size]) <> [cs_opt_size] then
  4283. begin
  4284. GetNextInstruction(hp1, hp2);
  4285. if not Assigned(hp2) then
  4286. Exit;
  4287. if (hp2.typ in [ait_label, ait_align]) then
  4288. SkipLabels(hp2,hp2);
  4289. if Assigned(hp2) and MatchInstruction(hp2, A_RET, [S_NO]) then
  4290. begin
  4291. { Duplicate the MOV instruction }
  4292. hp3:=tai(hp1.getcopy);
  4293. asml.InsertBefore(hp3, p);
  4294. { Make sure the compiler knows about any final registers written here }
  4295. for OperIdx := 0 to 1 do
  4296. with taicpu(hp3).oper[OperIdx]^ do
  4297. begin
  4298. case typ of
  4299. top_ref:
  4300. begin
  4301. if (ref^.base <> NR_NO) {$ifdef x86_64} and (ref^.base <> NR_RIP) {$endif x86_64} then
  4302. AllocRegBetween(ref^.base, hp3, tai(p.Next), UsedRegs);
  4303. if (ref^.index <> NR_NO) {$ifdef x86_64} and (ref^.index <> NR_RIP) {$endif x86_64} then
  4304. AllocRegBetween(ref^.index, hp3, tai(p.Next), UsedRegs);
  4305. end;
  4306. top_reg:
  4307. AllocRegBetween(reg, hp3, tai(p.Next), UsedRegs);
  4308. else
  4309. ;
  4310. end;
  4311. end;
  4312. { Now change the jump into a RET instruction }
  4313. ConvertJumpToRET(p, hp2);
  4314. result:=true;
  4315. end;
  4316. end;
  4317. else
  4318. ;
  4319. end;
  4320. end;
  4321. end;
  4322. end;
  4323. function CanBeCMOV(p : tai) : boolean;
  4324. begin
  4325. CanBeCMOV:=assigned(p) and
  4326. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  4327. { we can't use cmov ref,reg because
  4328. ref could be nil and cmov still throws an exception
  4329. if ref=nil but the mov isn't done (FK)
  4330. or ((taicpu(p).oper[0]^.typ = top_ref) and
  4331. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  4332. }
  4333. (MatchOpType(taicpu(p),top_reg,top_reg) or
  4334. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  4335. it is not expected that this can cause a seg. violation }
  4336. (MatchOpType(taicpu(p),top_ref,top_reg) and
  4337. (((taicpu(p).oper[0]^.ref^.base=NR_NO) and (taicpu(p).oper[0]^.ref^.refaddr=addr_no)){$ifdef x86_64} or
  4338. ((taicpu(p).oper[0]^.ref^.base=NR_RIP) and (taicpu(p).oper[0]^.ref^.refaddr=addr_pic)){$endif x86_64}
  4339. ) and
  4340. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  4341. (taicpu(p).oper[0]^.ref^.offset=0)
  4342. )
  4343. );
  4344. end;
  4345. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  4346. var
  4347. hp1,hp2,hp3,hp4,hpmov2: tai;
  4348. carryadd_opcode : TAsmOp;
  4349. l : Longint;
  4350. condition : TAsmCond;
  4351. symbol: TAsmSymbol;
  4352. begin
  4353. result:=false;
  4354. symbol:=nil;
  4355. if GetNextInstruction(p,hp1) then
  4356. begin
  4357. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  4358. if (hp1.typ=ait_instruction) and
  4359. GetNextInstruction(hp1,hp2) and (hp2.typ=ait_label) and
  4360. (Tasmlabel(symbol) = Tai_label(hp2).labsym) then
  4361. { jb @@1 cmc
  4362. inc/dec operand --> adc/sbb operand,0
  4363. @@1:
  4364. ... and ...
  4365. jnb @@1
  4366. inc/dec operand --> adc/sbb operand,0
  4367. @@1: }
  4368. begin
  4369. carryadd_opcode:=A_NONE;
  4370. if Taicpu(p).condition in [C_NAE,C_B] then
  4371. begin
  4372. if Taicpu(hp1).opcode=A_INC then
  4373. carryadd_opcode:=A_ADC;
  4374. if Taicpu(hp1).opcode=A_DEC then
  4375. carryadd_opcode:=A_SBB;
  4376. if carryadd_opcode<>A_NONE then
  4377. begin
  4378. Taicpu(p).clearop(0);
  4379. Taicpu(p).ops:=0;
  4380. Taicpu(p).is_jmp:=false;
  4381. Taicpu(p).opcode:=A_CMC;
  4382. Taicpu(p).condition:=C_NONE;
  4383. Taicpu(hp1).ops:=2;
  4384. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  4385. Taicpu(hp1).loadconst(0,0);
  4386. Taicpu(hp1).opcode:=carryadd_opcode;
  4387. result:=true;
  4388. exit;
  4389. end;
  4390. end;
  4391. if Taicpu(p).condition in [C_AE,C_NB] then
  4392. begin
  4393. if Taicpu(hp1).opcode=A_INC then
  4394. carryadd_opcode:=A_ADC;
  4395. if Taicpu(hp1).opcode=A_DEC then
  4396. carryadd_opcode:=A_SBB;
  4397. if carryadd_opcode<>A_NONE then
  4398. begin
  4399. asml.remove(p);
  4400. p.free;
  4401. Taicpu(hp1).ops:=2;
  4402. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  4403. Taicpu(hp1).loadconst(0,0);
  4404. Taicpu(hp1).opcode:=carryadd_opcode;
  4405. p:=hp1;
  4406. result:=true;
  4407. exit;
  4408. end;
  4409. end;
  4410. end;
  4411. { Detect the following:
  4412. jmp<cond> @Lbl1
  4413. jmp @Lbl2
  4414. ...
  4415. @Lbl1:
  4416. ret
  4417. Change to:
  4418. jmp<inv_cond> @Lbl2
  4419. ret
  4420. }
  4421. if MatchInstruction(hp1,A_JMP,[]) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  4422. begin
  4423. hp2:=getlabelwithsym(TAsmLabel(symbol));
  4424. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  4425. MatchInstruction(hp2,A_RET,[S_NO]) then
  4426. begin
  4427. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  4428. { Change label address to that of the unconditional jump }
  4429. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  4430. TAsmLabel(symbol).DecRefs;
  4431. taicpu(hp1).opcode := A_RET;
  4432. taicpu(hp1).is_jmp := false;
  4433. taicpu(hp1).ops := taicpu(hp2).ops;
  4434. DebugMsg(SPeepholeOptimization+'JccJmpRet2J!ccRet',p);
  4435. case taicpu(hp2).ops of
  4436. 0:
  4437. taicpu(hp1).clearop(0);
  4438. 1:
  4439. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  4440. else
  4441. internalerror(2016041302);
  4442. end;
  4443. end;
  4444. end;
  4445. end;
  4446. {$ifndef i8086}
  4447. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  4448. begin
  4449. { check for
  4450. jCC xxx
  4451. <several movs>
  4452. xxx:
  4453. }
  4454. l:=0;
  4455. GetNextInstruction(p, hp1);
  4456. while assigned(hp1) and
  4457. CanBeCMOV(hp1) and
  4458. { stop on labels }
  4459. not(hp1.typ=ait_label) do
  4460. begin
  4461. inc(l);
  4462. GetNextInstruction(hp1,hp1);
  4463. end;
  4464. if assigned(hp1) then
  4465. begin
  4466. if FindLabel(tasmlabel(symbol),hp1) then
  4467. begin
  4468. if (l<=4) and (l>0) then
  4469. begin
  4470. condition:=inverse_cond(taicpu(p).condition);
  4471. GetNextInstruction(p,hp1);
  4472. repeat
  4473. if not Assigned(hp1) then
  4474. InternalError(2018062900);
  4475. taicpu(hp1).opcode:=A_CMOVcc;
  4476. taicpu(hp1).condition:=condition;
  4477. UpdateUsedRegs(hp1);
  4478. GetNextInstruction(hp1,hp1);
  4479. until not(CanBeCMOV(hp1));
  4480. { Remember what hp1 is in case there's multiple aligns to get rid of }
  4481. hp2 := hp1;
  4482. repeat
  4483. if not Assigned(hp2) then
  4484. InternalError(2018062910);
  4485. case hp2.typ of
  4486. ait_label:
  4487. { What we expected - break out of the loop (it won't be a dead label at the top of
  4488. a cluster because that was optimised at an earlier stage) }
  4489. Break;
  4490. ait_align:
  4491. { Go to the next entry until a label is found (may be multiple aligns before it) }
  4492. begin
  4493. hp2 := tai(hp2.Next);
  4494. Continue;
  4495. end;
  4496. else
  4497. begin
  4498. { Might be a comment or temporary allocation entry }
  4499. if not (hp2.typ in SkipInstr) then
  4500. InternalError(2018062911);
  4501. hp2 := tai(hp2.Next);
  4502. Continue;
  4503. end;
  4504. end;
  4505. until False;
  4506. { Now we can safely decrement the reference count }
  4507. tasmlabel(symbol).decrefs;
  4508. DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
  4509. { Remove the original jump }
  4510. asml.Remove(p);
  4511. p.Free;
  4512. GetNextInstruction(hp2, p); { Instruction after the label }
  4513. { Remove the label if this is its final reference }
  4514. if (tasmlabel(symbol).getrefs=0) then
  4515. StripLabelFast(hp1);
  4516. if Assigned(p) then
  4517. begin
  4518. UpdateUsedRegs(p);
  4519. result:=true;
  4520. end;
  4521. exit;
  4522. end;
  4523. end
  4524. else
  4525. begin
  4526. { check further for
  4527. jCC xxx
  4528. <several movs 1>
  4529. jmp yyy
  4530. xxx:
  4531. <several movs 2>
  4532. yyy:
  4533. }
  4534. { hp2 points to jmp yyy }
  4535. hp2:=hp1;
  4536. { skip hp1 to xxx (or an align right before it) }
  4537. GetNextInstruction(hp1, hp1);
  4538. if assigned(hp2) and
  4539. assigned(hp1) and
  4540. (l<=3) and
  4541. (hp2.typ=ait_instruction) and
  4542. (taicpu(hp2).is_jmp) and
  4543. (taicpu(hp2).condition=C_None) and
  4544. { real label and jump, no further references to the
  4545. label are allowed }
  4546. (tasmlabel(symbol).getrefs=1) and
  4547. FindLabel(tasmlabel(symbol),hp1) then
  4548. begin
  4549. l:=0;
  4550. { skip hp1 to <several moves 2> }
  4551. if (hp1.typ = ait_align) then
  4552. GetNextInstruction(hp1, hp1);
  4553. GetNextInstruction(hp1, hpmov2);
  4554. hp1 := hpmov2;
  4555. while assigned(hp1) and
  4556. CanBeCMOV(hp1) do
  4557. begin
  4558. inc(l);
  4559. GetNextInstruction(hp1, hp1);
  4560. end;
  4561. { hp1 points to yyy (or an align right before it) }
  4562. hp3 := hp1;
  4563. if assigned(hp1) and
  4564. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  4565. begin
  4566. condition:=inverse_cond(taicpu(p).condition);
  4567. GetNextInstruction(p,hp1);
  4568. repeat
  4569. taicpu(hp1).opcode:=A_CMOVcc;
  4570. taicpu(hp1).condition:=condition;
  4571. UpdateUsedRegs(hp1);
  4572. GetNextInstruction(hp1,hp1);
  4573. until not(assigned(hp1)) or
  4574. not(CanBeCMOV(hp1));
  4575. condition:=inverse_cond(condition);
  4576. hp1 := hpmov2;
  4577. { hp1 is now at <several movs 2> }
  4578. while Assigned(hp1) and CanBeCMOV(hp1) do
  4579. begin
  4580. taicpu(hp1).opcode:=A_CMOVcc;
  4581. taicpu(hp1).condition:=condition;
  4582. UpdateUsedRegs(hp1);
  4583. GetNextInstruction(hp1,hp1);
  4584. end;
  4585. hp1 := p;
  4586. { Get first instruction after label }
  4587. GetNextInstruction(hp3, p);
  4588. if assigned(p) and (hp3.typ = ait_align) then
  4589. GetNextInstruction(p, p);
  4590. { Don't dereference yet, as doing so will cause
  4591. GetNextInstruction to skip the label and
  4592. optional align marker. [Kit] }
  4593. GetNextInstruction(hp2, hp4);
  4594. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
  4595. { remove jCC }
  4596. asml.remove(hp1);
  4597. hp1.free;
  4598. { Now we can safely decrement it }
  4599. tasmlabel(symbol).decrefs;
  4600. { Remove label xxx (it will have a ref of zero due to the initial check }
  4601. StripLabelFast(hp4);
  4602. { remove jmp }
  4603. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  4604. asml.remove(hp2);
  4605. hp2.free;
  4606. { As before, now we can safely decrement it }
  4607. tasmlabel(symbol).decrefs;
  4608. { Remove label yyy (and the optional alignment) if its reference falls to zero }
  4609. if tasmlabel(symbol).getrefs = 0 then
  4610. StripLabelFast(hp3);
  4611. if Assigned(p) then
  4612. begin
  4613. UpdateUsedRegs(p);
  4614. result:=true;
  4615. end;
  4616. exit;
  4617. end;
  4618. end;
  4619. end;
  4620. end;
  4621. end;
  4622. {$endif i8086}
  4623. end;
  4624. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  4625. var
  4626. hp1,hp2: tai;
  4627. begin
  4628. result:=false;
  4629. if (taicpu(p).oper[1]^.typ = top_reg) and
  4630. GetNextInstruction(p,hp1) and
  4631. (hp1.typ = ait_instruction) and
  4632. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  4633. GetNextInstruction(hp1,hp2) and
  4634. MatchInstruction(hp2,A_MOV,[]) and
  4635. (taicpu(hp2).oper[0]^.typ = top_reg) and
  4636. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  4637. {$ifdef i386}
  4638. { not all registers have byte size sub registers on i386 }
  4639. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  4640. {$endif i386}
  4641. (((taicpu(hp1).ops=2) and
  4642. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  4643. ((taicpu(hp1).ops=1) and
  4644. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  4645. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  4646. begin
  4647. { change movsX/movzX reg/ref, reg2
  4648. add/sub/or/... reg3/$const, reg2
  4649. mov reg2 reg/ref
  4650. to add/sub/or/... reg3/$const, reg/ref }
  4651. { by example:
  4652. movswl %si,%eax movswl %si,%eax p
  4653. decl %eax addl %edx,%eax hp1
  4654. movw %ax,%si movw %ax,%si hp2
  4655. ->
  4656. movswl %si,%eax movswl %si,%eax p
  4657. decw %eax addw %edx,%eax hp1
  4658. movw %ax,%si movw %ax,%si hp2
  4659. }
  4660. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  4661. {
  4662. ->
  4663. movswl %si,%eax movswl %si,%eax p
  4664. decw %si addw %dx,%si hp1
  4665. movw %ax,%si movw %ax,%si hp2
  4666. }
  4667. case taicpu(hp1).ops of
  4668. 1:
  4669. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  4670. 2:
  4671. begin
  4672. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  4673. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  4674. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  4675. end;
  4676. else
  4677. internalerror(2008042701);
  4678. end;
  4679. {
  4680. ->
  4681. decw %si addw %dx,%si p
  4682. }
  4683. DebugMsg(SPeepholeOptimization + 'var3',p);
  4684. asml.remove(p);
  4685. asml.remove(hp2);
  4686. p.free;
  4687. hp2.free;
  4688. p:=hp1;
  4689. end
  4690. else if taicpu(p).opcode=A_MOVZX then
  4691. begin
  4692. { removes superfluous And's after movzx's }
  4693. if (taicpu(p).oper[1]^.typ = top_reg) and
  4694. GetNextInstruction(p, hp1) and
  4695. (tai(hp1).typ = ait_instruction) and
  4696. (taicpu(hp1).opcode = A_AND) and
  4697. (taicpu(hp1).oper[0]^.typ = top_const) and
  4698. (taicpu(hp1).oper[1]^.typ = top_reg) and
  4699. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4700. begin
  4701. case taicpu(p).opsize Of
  4702. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  4703. if (taicpu(hp1).oper[0]^.val = $ff) then
  4704. begin
  4705. DebugMsg(SPeepholeOptimization + 'var4',p);
  4706. asml.remove(hp1);
  4707. hp1.free;
  4708. end;
  4709. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  4710. if (taicpu(hp1).oper[0]^.val = $ffff) then
  4711. begin
  4712. DebugMsg(SPeepholeOptimization + 'var5',p);
  4713. asml.remove(hp1);
  4714. hp1.free;
  4715. end;
  4716. {$ifdef x86_64}
  4717. S_LQ:
  4718. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  4719. begin
  4720. if (cs_asm_source in current_settings.globalswitches) then
  4721. asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
  4722. asml.remove(hp1);
  4723. hp1.Free;
  4724. end;
  4725. {$endif x86_64}
  4726. else
  4727. ;
  4728. end;
  4729. end;
  4730. { changes some movzx constructs to faster synonims (all examples
  4731. are given with eax/ax, but are also valid for other registers)}
  4732. if (taicpu(p).oper[1]^.typ = top_reg) then
  4733. if (taicpu(p).oper[0]^.typ = top_reg) then
  4734. case taicpu(p).opsize of
  4735. S_BW:
  4736. begin
  4737. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  4738. not(cs_opt_size in current_settings.optimizerswitches) then
  4739. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  4740. begin
  4741. taicpu(p).opcode := A_AND;
  4742. taicpu(p).changeopsize(S_W);
  4743. taicpu(p).loadConst(0,$ff);
  4744. DebugMsg(SPeepholeOptimization + 'var7',p);
  4745. end
  4746. else if GetNextInstruction(p, hp1) and
  4747. (tai(hp1).typ = ait_instruction) and
  4748. (taicpu(hp1).opcode = A_AND) and
  4749. (taicpu(hp1).oper[0]^.typ = top_const) and
  4750. (taicpu(hp1).oper[1]^.typ = top_reg) and
  4751. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4752. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  4753. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  4754. begin
  4755. DebugMsg(SPeepholeOptimization + 'var8',p);
  4756. taicpu(p).opcode := A_MOV;
  4757. taicpu(p).changeopsize(S_W);
  4758. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  4759. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  4760. end;
  4761. end;
  4762. S_BL:
  4763. begin
  4764. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  4765. not(cs_opt_size in current_settings.optimizerswitches) then
  4766. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  4767. begin
  4768. taicpu(p).opcode := A_AND;
  4769. taicpu(p).changeopsize(S_L);
  4770. taicpu(p).loadConst(0,$ff)
  4771. end
  4772. else if GetNextInstruction(p, hp1) and
  4773. (tai(hp1).typ = ait_instruction) and
  4774. (taicpu(hp1).opcode = A_AND) and
  4775. (taicpu(hp1).oper[0]^.typ = top_const) and
  4776. (taicpu(hp1).oper[1]^.typ = top_reg) and
  4777. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4778. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  4779. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  4780. begin
  4781. DebugMsg(SPeepholeOptimization + 'var10',p);
  4782. taicpu(p).opcode := A_MOV;
  4783. taicpu(p).changeopsize(S_L);
  4784. { do not use R_SUBWHOLE
  4785. as movl %rdx,%eax
  4786. is invalid in assembler PM }
  4787. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  4788. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  4789. end
  4790. end;
  4791. {$ifndef i8086}
  4792. S_WL:
  4793. begin
  4794. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  4795. not(cs_opt_size in current_settings.optimizerswitches) then
  4796. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  4797. begin
  4798. DebugMsg(SPeepholeOptimization + 'var11',p);
  4799. taicpu(p).opcode := A_AND;
  4800. taicpu(p).changeopsize(S_L);
  4801. taicpu(p).loadConst(0,$ffff);
  4802. end
  4803. else if GetNextInstruction(p, hp1) and
  4804. (tai(hp1).typ = ait_instruction) and
  4805. (taicpu(hp1).opcode = A_AND) and
  4806. (taicpu(hp1).oper[0]^.typ = top_const) and
  4807. (taicpu(hp1).oper[1]^.typ = top_reg) and
  4808. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4809. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  4810. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  4811. begin
  4812. DebugMsg(SPeepholeOptimization + 'var12',p);
  4813. taicpu(p).opcode := A_MOV;
  4814. taicpu(p).changeopsize(S_L);
  4815. { do not use R_SUBWHOLE
  4816. as movl %rdx,%eax
  4817. is invalid in assembler PM }
  4818. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  4819. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  4820. end;
  4821. end;
  4822. {$endif i8086}
  4823. else
  4824. ;
  4825. end
  4826. else if (taicpu(p).oper[0]^.typ = top_ref) then
  4827. begin
  4828. if GetNextInstruction(p, hp1) and
  4829. (tai(hp1).typ = ait_instruction) and
  4830. (taicpu(hp1).opcode = A_AND) and
  4831. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4832. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  4833. begin
  4834. //taicpu(p).opcode := A_MOV;
  4835. case taicpu(p).opsize Of
  4836. S_BL:
  4837. begin
  4838. DebugMsg(SPeepholeOptimization + 'var13',p);
  4839. taicpu(hp1).changeopsize(S_L);
  4840. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  4841. end;
  4842. S_WL:
  4843. begin
  4844. DebugMsg(SPeepholeOptimization + 'var14',p);
  4845. taicpu(hp1).changeopsize(S_L);
  4846. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  4847. end;
  4848. S_BW:
  4849. begin
  4850. DebugMsg(SPeepholeOptimization + 'var15',p);
  4851. taicpu(hp1).changeopsize(S_W);
  4852. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  4853. end;
  4854. {$ifdef x86_64}
  4855. S_BQ:
  4856. begin
  4857. DebugMsg(SPeepholeOptimization + 'var16',p);
  4858. taicpu(hp1).changeopsize(S_Q);
  4859. taicpu(hp1).loadConst(
  4860. 0, taicpu(hp1).oper[0]^.val and $ff);
  4861. end;
  4862. S_WQ:
  4863. begin
  4864. DebugMsg(SPeepholeOptimization + 'var17',p);
  4865. taicpu(hp1).changeopsize(S_Q);
  4866. taicpu(hp1).loadConst(0, taicpu(hp1).oper[0]^.val and $ffff);
  4867. end;
  4868. S_LQ:
  4869. begin
  4870. DebugMsg(SPeepholeOptimization + 'var18',p);
  4871. taicpu(hp1).changeopsize(S_Q);
  4872. taicpu(hp1).loadConst(
  4873. 0, taicpu(hp1).oper[0]^.val and $ffffffff);
  4874. end;
  4875. {$endif x86_64}
  4876. else
  4877. Internalerror(2017050704)
  4878. end;
  4879. end;
  4880. end;
  4881. end;
  4882. end;
  4883. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  4884. var
  4885. hp1 : tai;
  4886. MaskLength : Cardinal;
  4887. begin
  4888. Result:=false;
  4889. if GetNextInstruction(p, hp1) then
  4890. begin
  4891. if MatchOpType(taicpu(p),top_const,top_reg) and
  4892. MatchInstruction(hp1,A_AND,[]) and
  4893. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4894. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4895. { the second register must contain the first one, so compare their subreg types }
  4896. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  4897. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  4898. { change
  4899. and const1, reg
  4900. and const2, reg
  4901. to
  4902. and (const1 and const2), reg
  4903. }
  4904. begin
  4905. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  4906. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  4907. asml.remove(p);
  4908. p.Free;
  4909. p:=hp1;
  4910. Result:=true;
  4911. exit;
  4912. end
  4913. else if MatchOpType(taicpu(p),top_const,top_reg) and
  4914. MatchInstruction(hp1,A_MOVZX,[]) and
  4915. (taicpu(hp1).oper[0]^.typ = top_reg) and
  4916. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  4917. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4918. (((taicpu(p).opsize=S_W) and
  4919. (taicpu(hp1).opsize=S_BW)) or
  4920. ((taicpu(p).opsize=S_L) and
  4921. (taicpu(hp1).opsize in [S_WL,S_BL]))
  4922. {$ifdef x86_64}
  4923. or
  4924. ((taicpu(p).opsize=S_Q) and
  4925. (taicpu(hp1).opsize in [S_BQ,S_WQ]))
  4926. {$endif x86_64}
  4927. ) then
  4928. begin
  4929. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  4930. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  4931. ) or
  4932. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  4933. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  4934. then
  4935. begin
  4936. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  4937. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  4938. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  4939. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  4940. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  4941. }
  4942. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  4943. asml.remove(hp1);
  4944. hp1.free;
  4945. Exit;
  4946. end;
  4947. end
  4948. else if MatchOpType(taicpu(p),top_const,top_reg) and
  4949. MatchInstruction(hp1,A_SHL,[]) and
  4950. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4951. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  4952. begin
  4953. {$ifopt R+}
  4954. {$define RANGE_WAS_ON}
  4955. {$R-}
  4956. {$endif}
  4957. { get length of potential and mask }
  4958. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  4959. { really a mask? }
  4960. {$ifdef RANGE_WAS_ON}
  4961. {$R+}
  4962. {$endif}
  4963. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  4964. { unmasked part shifted out? }
  4965. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  4966. begin
  4967. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  4968. { take care of the register (de)allocs following p }
  4969. UpdateUsedRegs(tai(p.next));
  4970. asml.remove(p);
  4971. p.free;
  4972. p:=hp1;
  4973. Result:=true;
  4974. exit;
  4975. end;
  4976. end
  4977. else if MatchOpType(taicpu(p),top_const,top_reg) and
  4978. MatchInstruction(hp1,A_MOVSX{$ifdef x86_64},A_MOVSXD{$endif x86_64},[]) and
  4979. (taicpu(hp1).oper[0]^.typ = top_reg) and
  4980. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) and
  4981. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4982. (((taicpu(p).opsize=S_W) and
  4983. (taicpu(hp1).opsize=S_BW)) or
  4984. ((taicpu(p).opsize=S_L) and
  4985. (taicpu(hp1).opsize in [S_WL,S_BL]))
  4986. {$ifdef x86_64}
  4987. or
  4988. ((taicpu(p).opsize=S_Q) and
  4989. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_LQ]))
  4990. {$endif x86_64}
  4991. ) then
  4992. begin
  4993. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  4994. ((taicpu(p).oper[0]^.val and $7f)=taicpu(p).oper[0]^.val)
  4995. ) or
  4996. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  4997. ((taicpu(p).oper[0]^.val and $7fff)=taicpu(p).oper[0]^.val))
  4998. {$ifdef x86_64}
  4999. or
  5000. (((taicpu(hp1).opsize)=S_LQ) and
  5001. ((taicpu(p).oper[0]^.val and $7fffffff)=taicpu(p).oper[0]^.val)
  5002. )
  5003. {$endif x86_64}
  5004. then
  5005. begin
  5006. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  5007. asml.remove(hp1);
  5008. hp1.free;
  5009. Exit;
  5010. end;
  5011. end
  5012. else if (taicpu(p).oper[1]^.typ = top_reg) and
  5013. (hp1.typ = ait_instruction) and
  5014. (taicpu(hp1).is_jmp) and
  5015. (taicpu(hp1).opcode<>A_JMP) and
  5016. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  5017. begin
  5018. { change
  5019. and x, reg
  5020. jxx
  5021. to
  5022. test x, reg
  5023. jxx
  5024. if reg is deallocated before the
  5025. jump, but only if it's a conditional jump (PFV)
  5026. }
  5027. taicpu(p).opcode := A_TEST;
  5028. Exit;
  5029. end;
  5030. end;
  5031. { Lone AND tests }
  5032. if MatchOpType(taicpu(p),top_const,top_reg) then
  5033. begin
  5034. {
  5035. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  5036. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  5037. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  5038. }
  5039. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  5040. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  5041. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  5042. begin
  5043. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg)
  5044. end;
  5045. end;
  5046. end;
  5047. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  5048. begin
  5049. Result:=false;
  5050. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  5051. MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  5052. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  5053. begin
  5054. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  5055. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  5056. taicpu(p).opcode:=A_ADD;
  5057. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  5058. result:=true;
  5059. end
  5060. else if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) and
  5061. MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  5062. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  5063. begin
  5064. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  5065. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  5066. taicpu(p).opcode:=A_ADD;
  5067. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  5068. result:=true;
  5069. end;
  5070. end;
  5071. function TX86AsmOptimizer.OptPass2SUB(var p: tai): Boolean;
  5072. var
  5073. hp1: tai; NewRef: TReference;
  5074. begin
  5075. { Change:
  5076. subl/q $x,%reg1
  5077. movl/q %reg1,%reg2
  5078. To:
  5079. leal/q $-x(%reg1),%reg2
  5080. subl/q $x,%reg1
  5081. Breaks the dependency chain and potentially permits the removal of
  5082. a CMP instruction if one follows.
  5083. }
  5084. Result := False;
  5085. if not (cs_opt_size in current_settings.optimizerswitches) and
  5086. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  5087. MatchOpType(taicpu(p),top_const,top_reg) and
  5088. GetNextInstruction(p, hp1) and
  5089. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  5090. (taicpu(hp1).oper[1]^.typ = top_reg) and
  5091. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) then
  5092. begin
  5093. { Change the MOV instruction to a LEA instruction, and update the
  5094. first operand }
  5095. reference_reset(NewRef, 1, []);
  5096. NewRef.base := taicpu(p).oper[1]^.reg;
  5097. NewRef.scalefactor := 1;
  5098. NewRef.offset := -taicpu(p).oper[0]^.val;
  5099. taicpu(hp1).opcode := A_LEA;
  5100. taicpu(hp1).loadref(0, NewRef);
  5101. { Move what is now the LEA instruction to before the SUB instruction }
  5102. Asml.Remove(hp1);
  5103. Asml.InsertBefore(hp1, p);
  5104. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  5105. DebugMsg(SPeepholeOptimization + 'SubMov2LeaSub', p);
  5106. Result := True;
  5107. end;
  5108. end;
  5109. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  5110. function SkipSimpleInstructions(var hp1 : tai) : Boolean;
  5111. begin
  5112. { we can skip all instructions not messing with the stack pointer }
  5113. while assigned(hp1) and {MatchInstruction(taicpu(hp1),[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  5114. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  5115. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  5116. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  5117. ({(taicpu(hp1).ops=0) or }
  5118. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  5119. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  5120. ) and }
  5121. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  5122. )
  5123. ) do
  5124. GetNextInstruction(hp1,hp1);
  5125. Result:=assigned(hp1);
  5126. end;
  5127. var
  5128. hp1, hp2, hp3: tai;
  5129. begin
  5130. Result:=false;
  5131. { replace
  5132. leal(q) x(<stackpointer>),<stackpointer>
  5133. call procname
  5134. leal(q) -x(<stackpointer>),<stackpointer>
  5135. ret
  5136. by
  5137. jmp procname
  5138. but do it only on level 4 because it destroys stack back traces
  5139. }
  5140. if (cs_opt_level4 in current_settings.optimizerswitches) and
  5141. MatchOpType(taicpu(p),top_ref,top_reg) and
  5142. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  5143. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  5144. { the -8 or -24 are not required, but bail out early if possible,
  5145. higher values are unlikely }
  5146. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  5147. (taicpu(p).oper[0]^.ref^.offset=-24)) and
  5148. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  5149. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  5150. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  5151. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  5152. GetNextInstruction(p, hp1) and
  5153. { trick to skip label }
  5154. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  5155. SkipSimpleInstructions(hp1) and
  5156. MatchInstruction(hp1,A_CALL,[S_NO]) and
  5157. GetNextInstruction(hp1, hp2) and
  5158. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  5159. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  5160. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  5161. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  5162. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  5163. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  5164. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  5165. (taicpu(hp2).oper[0]^.ref^.segment=NR_NO) and
  5166. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  5167. GetNextInstruction(hp2, hp3) and
  5168. { trick to skip label }
  5169. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  5170. MatchInstruction(hp3,A_RET,[S_NO]) and
  5171. (taicpu(hp3).ops=0) then
  5172. begin
  5173. taicpu(hp1).opcode := A_JMP;
  5174. taicpu(hp1).is_jmp := true;
  5175. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  5176. RemoveCurrentP(p);
  5177. AsmL.Remove(hp2);
  5178. hp2.free;
  5179. AsmL.Remove(hp3);
  5180. hp3.free;
  5181. Result:=true;
  5182. end;
  5183. end;
  5184. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  5185. var
  5186. Value, RegName: string;
  5187. begin
  5188. Result:=false;
  5189. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  5190. begin
  5191. case taicpu(p).oper[0]^.val of
  5192. 0:
  5193. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  5194. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  5195. begin
  5196. { change "mov $0,%reg" into "xor %reg,%reg" }
  5197. taicpu(p).opcode := A_XOR;
  5198. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  5199. Result := True;
  5200. end;
  5201. $1..$FFFFFFFF:
  5202. begin
  5203. { Code size reduction by J. Gareth "Kit" Moreton }
  5204. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  5205. case taicpu(p).opsize of
  5206. S_Q:
  5207. begin
  5208. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  5209. Value := debug_tostr(taicpu(p).oper[0]^.val);
  5210. { The actual optimization }
  5211. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5212. taicpu(p).changeopsize(S_L);
  5213. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  5214. Result := True;
  5215. end;
  5216. else
  5217. { Do nothing };
  5218. end;
  5219. end;
  5220. -1:
  5221. { Don't make this optimisation if the CPU flags are required, since OR scrambles them }
  5222. if (cs_opt_size in current_settings.optimizerswitches) and
  5223. (taicpu(p).opsize <> S_B) and
  5224. not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  5225. begin
  5226. { change "mov $-1,%reg" into "or $-1,%reg" }
  5227. { NOTES:
  5228. - No size saving is made when changing a Word-sized assignment unless the register is AX (smaller encoding)
  5229. - This operation creates a false dependency on the register, so only do it when optimising for size
  5230. - 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
  5231. }
  5232. taicpu(p).opcode := A_OR;
  5233. Result := True;
  5234. end;
  5235. end;
  5236. end;
  5237. end;
  5238. function TX86AsmOptimizer.PostPeepholeOptMOVSX(var p : tai) : boolean;
  5239. begin
  5240. Result := False;
  5241. if not MatchOpType(taicpu(p), top_reg, top_reg) then
  5242. Exit;
  5243. { Convert:
  5244. movswl %ax,%eax -> cwtl
  5245. movslq %eax,%rax -> cdqe
  5246. NOTE: Don't convert movswl %al,%ax to cbw, because cbw and cwde
  5247. refer to the same opcode and depends only on the assembler's
  5248. current operand-size attribute. [Kit]
  5249. }
  5250. with taicpu(p) do
  5251. case opsize of
  5252. S_WL:
  5253. if (oper[0]^.reg = NR_AX) and (oper[1]^.reg = NR_EAX) then
  5254. begin
  5255. DebugMsg(SPeepholeOptimization + 'Converted movswl %ax,%eax to cwtl', p);
  5256. opcode := A_CWDE;
  5257. clearop(0);
  5258. clearop(1);
  5259. ops := 0;
  5260. Result := True;
  5261. end;
  5262. {$ifdef x86_64}
  5263. S_LQ:
  5264. if (oper[0]^.reg = NR_EAX) and (oper[1]^.reg = NR_RAX) then
  5265. begin
  5266. DebugMsg(SPeepholeOptimization + 'Converted movslq %eax,%rax to cltq', p);
  5267. opcode := A_CDQE;
  5268. clearop(0);
  5269. clearop(1);
  5270. ops := 0;
  5271. Result := True;
  5272. end;
  5273. {$endif x86_64}
  5274. else
  5275. ;
  5276. end;
  5277. end;
  5278. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  5279. begin
  5280. Result:=false;
  5281. { change "cmp $0, %reg" to "test %reg, %reg" }
  5282. if MatchOpType(taicpu(p),top_const,top_reg) and
  5283. (taicpu(p).oper[0]^.val = 0) then
  5284. begin
  5285. taicpu(p).opcode := A_TEST;
  5286. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  5287. Result:=true;
  5288. end;
  5289. end;
  5290. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  5291. var
  5292. IsTestConstX : Boolean;
  5293. hp1,hp2 : tai;
  5294. begin
  5295. Result:=false;
  5296. { removes the line marked with (x) from the sequence
  5297. and/or/xor/add/sub/... $x, %y
  5298. test/or %y, %y | test $-1, %y (x)
  5299. j(n)z _Label
  5300. as the first instruction already adjusts the ZF
  5301. %y operand may also be a reference }
  5302. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  5303. MatchOperand(taicpu(p).oper[0]^,-1);
  5304. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  5305. GetLastInstruction(p, hp1) and
  5306. (tai(hp1).typ = ait_instruction) and
  5307. GetNextInstruction(p,hp2) and
  5308. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  5309. case taicpu(hp1).opcode Of
  5310. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  5311. begin
  5312. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  5313. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  5314. { and in case of carry for A(E)/B(E)/C/NC }
  5315. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  5316. ((taicpu(hp1).opcode <> A_ADD) and
  5317. (taicpu(hp1).opcode <> A_SUB))) then
  5318. begin
  5319. hp1 := tai(p.next);
  5320. asml.remove(p);
  5321. p.free;
  5322. p := tai(hp1);
  5323. Result:=true;
  5324. end;
  5325. end;
  5326. A_SHL, A_SAL, A_SHR, A_SAR:
  5327. begin
  5328. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  5329. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  5330. { therefore, it's only safe to do this optimization for }
  5331. { shifts by a (nonzero) constant }
  5332. (taicpu(hp1).oper[0]^.typ = top_const) and
  5333. (taicpu(hp1).oper[0]^.val <> 0) and
  5334. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  5335. { and in case of carry for A(E)/B(E)/C/NC }
  5336. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  5337. begin
  5338. hp1 := tai(p.next);
  5339. asml.remove(p);
  5340. p.free;
  5341. p := tai(hp1);
  5342. Result:=true;
  5343. end;
  5344. end;
  5345. A_DEC, A_INC, A_NEG:
  5346. begin
  5347. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  5348. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  5349. { and in case of carry for A(E)/B(E)/C/NC }
  5350. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  5351. begin
  5352. case taicpu(hp1).opcode of
  5353. A_DEC, A_INC:
  5354. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  5355. begin
  5356. case taicpu(hp1).opcode Of
  5357. A_DEC: taicpu(hp1).opcode := A_SUB;
  5358. A_INC: taicpu(hp1).opcode := A_ADD;
  5359. else
  5360. ;
  5361. end;
  5362. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  5363. taicpu(hp1).loadConst(0,1);
  5364. taicpu(hp1).ops:=2;
  5365. end;
  5366. else
  5367. ;
  5368. end;
  5369. hp1 := tai(p.next);
  5370. asml.remove(p);
  5371. p.free;
  5372. p := tai(hp1);
  5373. Result:=true;
  5374. end;
  5375. end
  5376. else
  5377. { change "test $-1,%reg" into "test %reg,%reg" }
  5378. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  5379. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  5380. end { case }
  5381. { change "test $-1,%reg" into "test %reg,%reg" }
  5382. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  5383. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  5384. end;
  5385. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  5386. var
  5387. hp1 : tai;
  5388. {$ifndef x86_64}
  5389. hp2 : taicpu;
  5390. {$endif x86_64}
  5391. begin
  5392. Result:=false;
  5393. {$ifndef x86_64}
  5394. { don't do this on modern CPUs, this really hurts them due to
  5395. broken call/ret pairing }
  5396. if (current_settings.optimizecputype < cpu_Pentium2) and
  5397. not(cs_create_pic in current_settings.moduleswitches) and
  5398. GetNextInstruction(p, hp1) and
  5399. MatchInstruction(hp1,A_JMP,[S_NO]) and
  5400. MatchOpType(taicpu(hp1),top_ref) and
  5401. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  5402. begin
  5403. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  5404. InsertLLItem(p.previous, p, hp2);
  5405. taicpu(p).opcode := A_JMP;
  5406. taicpu(p).is_jmp := true;
  5407. asml.remove(hp1);
  5408. hp1.free;
  5409. Result:=true;
  5410. end
  5411. else
  5412. {$endif x86_64}
  5413. { replace
  5414. call procname
  5415. ret
  5416. by
  5417. jmp procname
  5418. but do it only on level 4 because it destroys stack back traces
  5419. }
  5420. if (cs_opt_level4 in current_settings.optimizerswitches) and
  5421. GetNextInstruction(p, hp1) and
  5422. MatchInstruction(hp1,A_RET,[S_NO]) and
  5423. (taicpu(hp1).ops=0) then
  5424. begin
  5425. taicpu(p).opcode := A_JMP;
  5426. taicpu(p).is_jmp := true;
  5427. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  5428. asml.remove(hp1);
  5429. hp1.free;
  5430. Result:=true;
  5431. end;
  5432. end;
  5433. {$ifdef x86_64}
  5434. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  5435. var
  5436. PreMessage: string;
  5437. begin
  5438. Result := False;
  5439. { Code size reduction by J. Gareth "Kit" Moreton }
  5440. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  5441. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  5442. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  5443. then
  5444. begin
  5445. { Has 64-bit register name and opcode suffix }
  5446. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  5447. { The actual optimization }
  5448. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5449. if taicpu(p).opsize = S_BQ then
  5450. taicpu(p).changeopsize(S_BL)
  5451. else
  5452. taicpu(p).changeopsize(S_WL);
  5453. DebugMsg(SPeepholeOptimization + PreMessage +
  5454. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  5455. end;
  5456. end;
  5457. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  5458. var
  5459. PreMessage, RegName: string;
  5460. begin
  5461. { Code size reduction by J. Gareth "Kit" Moreton }
  5462. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  5463. as this removes the REX prefix }
  5464. Result := False;
  5465. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  5466. Exit;
  5467. if taicpu(p).oper[0]^.typ <> top_reg then
  5468. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  5469. InternalError(2018011500);
  5470. case taicpu(p).opsize of
  5471. S_Q:
  5472. begin
  5473. 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
  5474. begin
  5475. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  5476. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  5477. { The actual optimization }
  5478. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  5479. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  5480. taicpu(p).changeopsize(S_L);
  5481. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  5482. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  5483. end;
  5484. end;
  5485. else
  5486. ;
  5487. end;
  5488. end;
  5489. {$endif}
  5490. class procedure TX86AsmOptimizer.OptimizeRefs(var p: taicpu);
  5491. var
  5492. OperIdx: Integer;
  5493. begin
  5494. for OperIdx := 0 to p.ops - 1 do
  5495. if p.oper[OperIdx]^.typ = top_ref then
  5496. optimize_ref(p.oper[OperIdx]^.ref^, False);
  5497. end;
  5498. end.