aoptx86.pas 343 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036
  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. TOptsToCheck = (
  29. aoc_MovAnd2Mov_3
  30. );
  31. TX86AsmOptimizer = class(TAsmOptimizer)
  32. { some optimizations are very expensive to check, so the
  33. pre opt pass can be used to set some flags, depending on the found
  34. instructions if it is worth to check a certain optimization }
  35. OptsToCheck : set of TOptsToCheck;
  36. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  37. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  38. function RegReadByInstruction(reg : TRegister; hp : tai) : boolean;
  39. function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  40. function GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  41. {
  42. In comparison with GetNextInstructionUsingReg, GetNextInstructionUsingRegTrackingUse tracks
  43. the use of a register by allocs/dealloc, so it can ignore calls.
  44. In the following example, GetNextInstructionUsingReg will return the second movq,
  45. GetNextInstructionUsingRegTrackingUse won't.
  46. movq %rdi,%rax
  47. # Register rdi released
  48. # Register rdi allocated
  49. movq %rax,%rdi
  50. While in this example:
  51. movq %rdi,%rax
  52. call proc
  53. movq %rdi,%rax
  54. GetNextInstructionUsingRegTrackingUse will return the second instruction while GetNextInstructionUsingReg
  55. won't.
  56. }
  57. function GetNextInstructionUsingRegTrackingUse(Current: tai; out Next: tai; reg: TRegister): Boolean;
  58. function RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean; override;
  59. private
  60. function SkipSimpleInstructions(var hp1: tai): Boolean;
  61. protected
  62. class function IsMOVZXAcceptable: Boolean; static; inline;
  63. { checks whether loading a new value in reg1 overwrites the entirety of reg2 }
  64. function Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  65. { checks whether reading the value in reg1 depends on the value of reg2. This
  66. is very similar to SuperRegisterEquals, except it takes into account that
  67. R_SUBH and R_SUBL are independendent (e.g. reading from AL does not
  68. depend on the value in AH). }
  69. function Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  70. { Replaces all references to AOldReg in a memory reference to ANewReg }
  71. class function ReplaceRegisterInRef(var ref: TReference; const AOldReg, ANewReg: TRegister): Boolean; static;
  72. { Replaces all references to AOldReg in an operand to ANewReg }
  73. class function ReplaceRegisterInOper(const p: taicpu; const OperIdx: Integer; const AOldReg, ANewReg: TRegister): Boolean; static;
  74. { Replaces all references to AOldReg in an instruction to ANewReg,
  75. except where the register is being written }
  76. function ReplaceRegisterInInstruction(const p: taicpu; const AOldReg, ANewReg: TRegister): Boolean;
  77. { Returns true if the reference only refers to ESP or EBP (or their 64-bit equivalents),
  78. or writes to a global symbol }
  79. class function IsRefSafe(const ref: PReference): Boolean; static; inline;
  80. { Returns true if the given MOV instruction can be safely converted to CMOV }
  81. class function CanBeCMOV(p : tai) : boolean; static;
  82. { Converts the LEA instruction to ADD/INC/SUB/DEC. Returns True if the
  83. conversion was successful }
  84. function ConvertLEA(const p : taicpu): Boolean;
  85. function DeepMOVOpt(const p_mov: taicpu; const hp: taicpu): Boolean;
  86. procedure DebugMsg(const s : string; p : tai);inline;
  87. class function IsExitCode(p : tai) : boolean; static;
  88. class function isFoldableArithOp(hp1 : taicpu; reg : tregister) : boolean; static;
  89. procedure RemoveLastDeallocForFuncRes(p : tai);
  90. function DoSubAddOpt(var p : tai) : Boolean;
  91. function PrePeepholeOptSxx(var p : tai) : boolean;
  92. function PrePeepholeOptIMUL(var p : tai) : boolean;
  93. function OptPass1Add(var p: tai): boolean;
  94. function OptPass1AND(var p : tai) : boolean;
  95. function OptPass1_V_MOVAP(var p : tai) : boolean;
  96. function OptPass1VOP(var p : tai) : boolean;
  97. function OptPass1MOV(var p : tai) : boolean;
  98. function OptPass1Movx(var p : tai) : boolean;
  99. function OptPass1MOVXX(var p : tai) : boolean;
  100. function OptPass1OP(var p : tai) : boolean;
  101. function OptPass1LEA(var p : tai) : boolean;
  102. function OptPass1Sub(var p : tai) : boolean;
  103. function OptPass1SHLSAL(var p : tai) : boolean;
  104. function OptPass1SETcc(var p : tai) : boolean;
  105. function OptPass1FSTP(var p : tai) : boolean;
  106. function OptPass1FLD(var p : tai) : boolean;
  107. function OptPass1Cmp(var p : tai) : boolean;
  108. function OptPass1PXor(var p : tai) : boolean;
  109. function OptPass1VPXor(var p: tai): boolean;
  110. function OptPass1Imul(var p : tai) : boolean;
  111. function OptPass2Movx(var p : tai): Boolean;
  112. function OptPass2MOV(var p : tai) : boolean;
  113. function OptPass2Imul(var p : tai) : boolean;
  114. function OptPass2Jmp(var p : tai) : boolean;
  115. function OptPass2Jcc(var p : tai) : boolean;
  116. function OptPass2Lea(var p: tai): Boolean;
  117. function OptPass2SUB(var p: tai): Boolean;
  118. function OptPass2ADD(var p : tai): Boolean;
  119. function PostPeepholeOptMov(var p : tai) : Boolean;
  120. function PostPeepholeOptMovzx(var p : tai) : Boolean;
  121. {$ifdef x86_64} { These post-peephole optimisations only affect 64-bit registers. [Kit] }
  122. function PostPeepholeOptXor(var p : tai) : Boolean;
  123. {$endif}
  124. function PostPeepholeOptAnd(var p : tai) : boolean;
  125. function PostPeepholeOptMOVSX(var p : tai) : boolean;
  126. function PostPeepholeOptCmp(var p : tai) : Boolean;
  127. function PostPeepholeOptTestOr(var p : tai) : Boolean;
  128. function PostPeepholeOptCall(var p : tai) : Boolean;
  129. function PostPeepholeOptLea(var p : tai) : Boolean;
  130. function PostPeepholeOptPush(var p: tai): Boolean;
  131. function PostPeepholeOptShr(var p : tai) : boolean;
  132. procedure ConvertJumpToRET(const p: tai; const ret_p: tai);
  133. { Processor-dependent reference optimisation }
  134. class procedure OptimizeRefs(var p: taicpu); static;
  135. end;
  136. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  137. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  138. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  139. function MatchInstruction(const instr: tai; const ops: array of TAsmOp; const opsize: topsizes): boolean;
  140. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  141. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  142. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  143. {$if max_operands>2}
  144. function MatchOperand(const oper1: TOper; const oper2: TOper; const oper3: TOper): boolean;
  145. {$endif max_operands>2}
  146. function RefsEqual(const r1, r2: treference): boolean;
  147. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  148. { returns true, if ref is a reference using only the registers passed as base and index
  149. and having an offset }
  150. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  151. implementation
  152. uses
  153. cutils,verbose,
  154. systems,
  155. globals,
  156. cpuinfo,
  157. procinfo,
  158. paramgr,
  159. aasmbase,
  160. aoptbase,aoptutils,
  161. symconst,symsym,
  162. cgx86,
  163. itcpugas;
  164. {$ifdef DEBUG_AOPTCPU}
  165. const
  166. SPeepholeOptimization: shortstring = 'Peephole Optimization: ';
  167. {$else DEBUG_AOPTCPU}
  168. { Empty strings help the optimizer to remove string concatenations that won't
  169. ever appear to the user on release builds. [Kit] }
  170. const
  171. SPeepholeOptimization = '';
  172. {$endif DEBUG_AOPTCPU}
  173. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  174. begin
  175. result :=
  176. (instr.typ = ait_instruction) and
  177. (taicpu(instr).opcode = op) and
  178. ((opsize = []) or (taicpu(instr).opsize in opsize));
  179. end;
  180. function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
  181. begin
  182. result :=
  183. (instr.typ = ait_instruction) and
  184. ((taicpu(instr).opcode = op1) or
  185. (taicpu(instr).opcode = op2)
  186. ) and
  187. ((opsize = []) or (taicpu(instr).opsize in opsize));
  188. end;
  189. function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
  190. begin
  191. result :=
  192. (instr.typ = ait_instruction) and
  193. ((taicpu(instr).opcode = op1) or
  194. (taicpu(instr).opcode = op2) or
  195. (taicpu(instr).opcode = op3)
  196. ) and
  197. ((opsize = []) or (taicpu(instr).opsize in opsize));
  198. end;
  199. function MatchInstruction(const instr : tai;const ops : array of TAsmOp;
  200. const opsize : topsizes) : boolean;
  201. var
  202. op : TAsmOp;
  203. begin
  204. result:=false;
  205. for op in ops do
  206. begin
  207. if (instr.typ = ait_instruction) and
  208. (taicpu(instr).opcode = op) and
  209. ((opsize = []) or (taicpu(instr).opsize in opsize)) then
  210. begin
  211. result:=true;
  212. exit;
  213. end;
  214. end;
  215. end;
  216. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  217. begin
  218. result := (oper.typ = top_reg) and (oper.reg = reg);
  219. end;
  220. function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
  221. begin
  222. result := (oper.typ = top_const) and (oper.val = a);
  223. end;
  224. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  225. begin
  226. result := oper1.typ = oper2.typ;
  227. if result then
  228. case oper1.typ of
  229. top_const:
  230. Result:=oper1.val = oper2.val;
  231. top_reg:
  232. Result:=oper1.reg = oper2.reg;
  233. top_ref:
  234. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  235. else
  236. internalerror(2013102801);
  237. end
  238. end;
  239. function MatchOperand(const oper1: TOper; const oper2: TOper; const oper3: TOper): boolean;
  240. begin
  241. result := (oper1.typ = oper2.typ) and (oper1.typ = oper3.typ);
  242. if result then
  243. case oper1.typ of
  244. top_const:
  245. Result:=(oper1.val = oper2.val) and (oper1.val = oper3.val);
  246. top_reg:
  247. Result:=(oper1.reg = oper2.reg) and (oper1.reg = oper3.reg);
  248. top_ref:
  249. Result:=RefsEqual(oper1.ref^, oper2.ref^) and RefsEqual(oper1.ref^, oper3.ref^);
  250. else
  251. internalerror(2020052401);
  252. end
  253. end;
  254. function RefsEqual(const r1, r2: treference): boolean;
  255. begin
  256. RefsEqual :=
  257. (r1.offset = r2.offset) and
  258. (r1.segment = r2.segment) and (r1.base = r2.base) and
  259. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  260. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  261. (r1.relsymbol = r2.relsymbol) and
  262. (r1.volatility=[]) and
  263. (r2.volatility=[]);
  264. end;
  265. function MatchReference(const ref : treference;base,index : TRegister) : Boolean;
  266. begin
  267. Result:=(ref.offset=0) and
  268. (ref.scalefactor in [0,1]) and
  269. (ref.segment=NR_NO) and
  270. (ref.symbol=nil) and
  271. (ref.relsymbol=nil) and
  272. ((base=NR_INVALID) or
  273. (ref.base=base)) and
  274. ((index=NR_INVALID) or
  275. (ref.index=index)) and
  276. (ref.volatility=[]);
  277. end;
  278. function MatchReferenceWithOffset(const ref : treference;base,index : TRegister) : Boolean;
  279. begin
  280. Result:=(ref.scalefactor in [0,1]) and
  281. (ref.segment=NR_NO) and
  282. (ref.symbol=nil) and
  283. (ref.relsymbol=nil) and
  284. ((base=NR_INVALID) or
  285. (ref.base=base)) and
  286. ((index=NR_INVALID) or
  287. (ref.index=index)) and
  288. (ref.volatility=[]);
  289. end;
  290. function InstrReadsFlags(p: tai): boolean;
  291. begin
  292. InstrReadsFlags := true;
  293. case p.typ of
  294. ait_instruction:
  295. if InsProp[taicpu(p).opcode].Ch*
  296. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  297. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  298. Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc,Ch_All]<>[] then
  299. exit;
  300. ait_label:
  301. exit;
  302. else
  303. ;
  304. end;
  305. InstrReadsFlags := false;
  306. end;
  307. function TX86AsmOptimizer.GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  308. begin
  309. Next:=Current;
  310. repeat
  311. Result:=GetNextInstruction(Next,Next);
  312. until not (Result) or
  313. not(cs_opt_level3 in current_settings.optimizerswitches) or
  314. (Next.typ<>ait_instruction) or
  315. RegInInstruction(reg,Next) or
  316. is_calljmp(taicpu(Next).opcode);
  317. end;
  318. function TX86AsmOptimizer.GetNextInstructionUsingRegTrackingUse(Current: tai; out Next: tai; reg: TRegister): Boolean;
  319. begin
  320. if not(cs_opt_level3 in current_settings.optimizerswitches) then
  321. begin
  322. Result:=GetNextInstruction(Current,Next);
  323. exit;
  324. end;
  325. Next:=tai(Current.Next);
  326. Result:=false;
  327. while assigned(Next) do
  328. begin
  329. if ((Next.typ=ait_instruction) and is_calljmp(taicpu(Next).opcode) and not(taicpu(Next).opcode=A_CALL)) or
  330. ((Next.typ=ait_regalloc) and (getsupreg(tai_regalloc(Next).reg)=getsupreg(reg))) or
  331. ((Next.typ=ait_label) and not(labelCanBeSkipped(Tai_Label(Next)))) then
  332. exit
  333. else if (Next.typ=ait_instruction) and RegInInstruction(reg,Next) and not(taicpu(Next).opcode=A_CALL) then
  334. begin
  335. Result:=true;
  336. exit;
  337. end;
  338. Next:=tai(Next.Next);
  339. end;
  340. end;
  341. function TX86AsmOptimizer.InstructionLoadsFromReg(const reg: TRegister;const hp: tai): boolean;
  342. begin
  343. Result:=RegReadByInstruction(reg,hp);
  344. end;
  345. function TX86AsmOptimizer.RegReadByInstruction(reg: TRegister; hp: tai): boolean;
  346. var
  347. p: taicpu;
  348. opcount: longint;
  349. begin
  350. RegReadByInstruction := false;
  351. if hp.typ <> ait_instruction then
  352. exit;
  353. p := taicpu(hp);
  354. case p.opcode of
  355. A_CALL:
  356. regreadbyinstruction := true;
  357. A_IMUL:
  358. case p.ops of
  359. 1:
  360. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  361. (
  362. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  363. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  364. );
  365. 2,3:
  366. regReadByInstruction :=
  367. reginop(reg,p.oper[0]^) or
  368. reginop(reg,p.oper[1]^);
  369. else
  370. InternalError(2019112801);
  371. end;
  372. A_MUL:
  373. begin
  374. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  375. (
  376. ((getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)=RS_EAX)) and
  377. ((getsubreg(reg)<>R_SUBH) or (p.opsize<>S_B))
  378. );
  379. end;
  380. A_IDIV,A_DIV:
  381. begin
  382. regReadByInstruction := RegInOp(reg,p.oper[0]^) or
  383. (
  384. (getregtype(reg)=R_INTREGISTER) and
  385. (
  386. (getsupreg(reg)=RS_EAX) or ((getsupreg(reg)=RS_EDX) and (p.opsize<>S_B))
  387. )
  388. );
  389. end;
  390. else
  391. begin
  392. if (p.opcode=A_LEA) and is_segment_reg(reg) then
  393. begin
  394. RegReadByInstruction := false;
  395. exit;
  396. end;
  397. for opcount := 0 to p.ops-1 do
  398. if (p.oper[opCount]^.typ = top_ref) and
  399. RegInRef(reg,p.oper[opcount]^.ref^) then
  400. begin
  401. RegReadByInstruction := true;
  402. exit
  403. end;
  404. { special handling for SSE MOVSD }
  405. if (p.opcode=A_MOVSD) and (p.ops>0) then
  406. begin
  407. if p.ops<>2 then
  408. internalerror(2017042702);
  409. regReadByInstruction := reginop(reg,p.oper[0]^) or
  410. (
  411. (p.oper[1]^.typ=top_reg) and (p.oper[0]^.typ=top_reg) and reginop(reg, p.oper[1]^)
  412. );
  413. exit;
  414. end;
  415. with insprop[p.opcode] do
  416. begin
  417. if getregtype(reg)=R_INTREGISTER then
  418. begin
  419. case getsupreg(reg) of
  420. RS_EAX:
  421. if [Ch_REAX,Ch_RWEAX,Ch_MEAX]*Ch<>[] then
  422. begin
  423. RegReadByInstruction := true;
  424. exit
  425. end;
  426. RS_ECX:
  427. if [Ch_RECX,Ch_RWECX,Ch_MECX]*Ch<>[] then
  428. begin
  429. RegReadByInstruction := true;
  430. exit
  431. end;
  432. RS_EDX:
  433. if [Ch_REDX,Ch_RWEDX,Ch_MEDX]*Ch<>[] then
  434. begin
  435. RegReadByInstruction := true;
  436. exit
  437. end;
  438. RS_EBX:
  439. if [Ch_REBX,Ch_RWEBX,Ch_MEBX]*Ch<>[] then
  440. begin
  441. RegReadByInstruction := true;
  442. exit
  443. end;
  444. RS_ESP:
  445. if [Ch_RESP,Ch_RWESP,Ch_MESP]*Ch<>[] then
  446. begin
  447. RegReadByInstruction := true;
  448. exit
  449. end;
  450. RS_EBP:
  451. if [Ch_REBP,Ch_RWEBP,Ch_MEBP]*Ch<>[] then
  452. begin
  453. RegReadByInstruction := true;
  454. exit
  455. end;
  456. RS_ESI:
  457. if [Ch_RESI,Ch_RWESI,Ch_MESI]*Ch<>[] then
  458. begin
  459. RegReadByInstruction := true;
  460. exit
  461. end;
  462. RS_EDI:
  463. if [Ch_REDI,Ch_RWEDI,Ch_MEDI]*Ch<>[] then
  464. begin
  465. RegReadByInstruction := true;
  466. exit
  467. end;
  468. end;
  469. end;
  470. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  471. begin
  472. if (Ch_RFLAGScc in Ch) and not(getsubreg(reg) in [R_SUBW,R_SUBD,R_SUBQ]) then
  473. begin
  474. case p.condition of
  475. C_A,C_NBE, { CF=0 and ZF=0 }
  476. C_BE,C_NA: { CF=1 or ZF=1 }
  477. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY,R_SUBFLAGZERO];
  478. C_AE,C_NB,C_NC, { CF=0 }
  479. C_B,C_NAE,C_C: { CF=1 }
  480. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGCARRY];
  481. C_NE,C_NZ, { ZF=0 }
  482. C_E,C_Z: { ZF=1 }
  483. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO];
  484. C_G,C_NLE, { ZF=0 and SF=OF }
  485. C_LE,C_NG: { ZF=1 or SF<>OF }
  486. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGZERO,R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  487. C_GE,C_NL, { SF=OF }
  488. C_L,C_NGE: { SF<>OF }
  489. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN,R_SUBFLAGOVERFLOW];
  490. C_NO, { OF=0 }
  491. C_O: { OF=1 }
  492. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGOVERFLOW];
  493. C_NP,C_PO, { PF=0 }
  494. C_P,C_PE: { PF=1 }
  495. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGPARITY];
  496. C_NS, { SF=0 }
  497. C_S: { SF=1 }
  498. RegReadByInstruction:=getsubreg(reg) in [R_SUBFLAGSIGN];
  499. else
  500. internalerror(2017042701);
  501. end;
  502. if RegReadByInstruction then
  503. exit;
  504. end;
  505. case getsubreg(reg) of
  506. R_SUBW,R_SUBD,R_SUBQ:
  507. RegReadByInstruction :=
  508. [Ch_RCarryFlag,Ch_RParityFlag,Ch_RAuxiliaryFlag,Ch_RZeroFlag,Ch_RSignFlag,Ch_ROverflowFlag,
  509. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  510. Ch_RDirFlag,Ch_RFlags,Ch_RWFlags,Ch_RFLAGScc]*Ch<>[];
  511. R_SUBFLAGCARRY:
  512. RegReadByInstruction:=[Ch_RCarryFlag,Ch_RWCarryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  513. R_SUBFLAGPARITY:
  514. RegReadByInstruction:=[Ch_RParityFlag,Ch_RWParityFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  515. R_SUBFLAGAUXILIARY:
  516. RegReadByInstruction:=[Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  517. R_SUBFLAGZERO:
  518. RegReadByInstruction:=[Ch_RZeroFlag,Ch_RWZeroFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  519. R_SUBFLAGSIGN:
  520. RegReadByInstruction:=[Ch_RSignFlag,Ch_RWSignFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  521. R_SUBFLAGOVERFLOW:
  522. RegReadByInstruction:=[Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  523. R_SUBFLAGINTERRUPT:
  524. RegReadByInstruction:=[Ch_RFlags,Ch_RWFlags]*Ch<>[];
  525. R_SUBFLAGDIRECTION:
  526. RegReadByInstruction:=[Ch_RDirFlag,Ch_RFlags,Ch_RWFlags]*Ch<>[];
  527. else
  528. internalerror(2017042601);
  529. end;
  530. exit;
  531. end;
  532. if (Ch_NoReadIfEqualRegs in Ch) and (p.ops=2) and
  533. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  534. (p.oper[0]^.reg=p.oper[1]^.reg) then
  535. exit;
  536. if ([CH_RWOP1,CH_ROP1,CH_MOP1]*Ch<>[]) and reginop(reg,p.oper[0]^) then
  537. begin
  538. RegReadByInstruction := true;
  539. exit
  540. end;
  541. if ([Ch_RWOP2,Ch_ROP2,Ch_MOP2]*Ch<>[]) and reginop(reg,p.oper[1]^) then
  542. begin
  543. RegReadByInstruction := true;
  544. exit
  545. end;
  546. if ([Ch_RWOP3,Ch_ROP3,Ch_MOP3]*Ch<>[]) and reginop(reg,p.oper[2]^) then
  547. begin
  548. RegReadByInstruction := true;
  549. exit
  550. end;
  551. if ([Ch_RWOP4,Ch_ROP4,Ch_MOP4]*Ch<>[]) and reginop(reg,p.oper[3]^) then
  552. begin
  553. RegReadByInstruction := true;
  554. exit
  555. end;
  556. end;
  557. end;
  558. end;
  559. end;
  560. function TX86AsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  561. begin
  562. result:=false;
  563. if p1.typ<>ait_instruction then
  564. exit;
  565. if (Ch_All in insprop[taicpu(p1).opcode].Ch) then
  566. exit(true);
  567. if (getregtype(reg)=R_INTREGISTER) and
  568. { change information for xmm movsd are not correct }
  569. ((taicpu(p1).opcode<>A_MOVSD) or (taicpu(p1).ops=0)) then
  570. begin
  571. case getsupreg(reg) of
  572. { RS_EAX = RS_RAX on x86-64 }
  573. RS_EAX:
  574. result:=([Ch_REAX,Ch_RRAX,Ch_WEAX,Ch_WRAX,Ch_RWEAX,Ch_RWRAX,Ch_MEAX,Ch_MRAX]*insprop[taicpu(p1).opcode].Ch)<>[];
  575. RS_ECX:
  576. result:=([Ch_RECX,Ch_RRCX,Ch_WECX,Ch_WRCX,Ch_RWECX,Ch_RWRCX,Ch_MECX,Ch_MRCX]*insprop[taicpu(p1).opcode].Ch)<>[];
  577. RS_EDX:
  578. result:=([Ch_REDX,Ch_RRDX,Ch_WEDX,Ch_WRDX,Ch_RWEDX,Ch_RWRDX,Ch_MEDX,Ch_MRDX]*insprop[taicpu(p1).opcode].Ch)<>[];
  579. RS_EBX:
  580. result:=([Ch_REBX,Ch_RRBX,Ch_WEBX,Ch_WRBX,Ch_RWEBX,Ch_RWRBX,Ch_MEBX,Ch_MRBX]*insprop[taicpu(p1).opcode].Ch)<>[];
  581. RS_ESP:
  582. result:=([Ch_RESP,Ch_RRSP,Ch_WESP,Ch_WRSP,Ch_RWESP,Ch_RWRSP,Ch_MESP,Ch_MRSP]*insprop[taicpu(p1).opcode].Ch)<>[];
  583. RS_EBP:
  584. result:=([Ch_REBP,Ch_RRBP,Ch_WEBP,Ch_WRBP,Ch_RWEBP,Ch_RWRBP,Ch_MEBP,Ch_MRBP]*insprop[taicpu(p1).opcode].Ch)<>[];
  585. RS_ESI:
  586. result:=([Ch_RESI,Ch_RRSI,Ch_WESI,Ch_WRSI,Ch_RWESI,Ch_RWRSI,Ch_MESI,Ch_MRSI,Ch_RMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  587. RS_EDI:
  588. result:=([Ch_REDI,Ch_RRDI,Ch_WEDI,Ch_WRDI,Ch_RWEDI,Ch_RWRDI,Ch_MEDI,Ch_MRDI,Ch_WMemEDI]*insprop[taicpu(p1).opcode].Ch)<>[];
  589. else
  590. ;
  591. end;
  592. if result then
  593. exit;
  594. end
  595. else if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  596. begin
  597. if ([Ch_RFlags,Ch_WFlags,Ch_RWFlags,Ch_RFLAGScc]*insprop[taicpu(p1).opcode].Ch)<>[] then
  598. exit(true);
  599. case getsubreg(reg) of
  600. R_SUBFLAGCARRY:
  601. Result:=([Ch_RCarryFlag,Ch_RWCarryFlag,Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  602. R_SUBFLAGPARITY:
  603. Result:=([Ch_RParityFlag,Ch_RWParityFlag,Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  604. R_SUBFLAGAUXILIARY:
  605. Result:=([Ch_RAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  606. R_SUBFLAGZERO:
  607. Result:=([Ch_RZeroFlag,Ch_RWZeroFlag,Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  608. R_SUBFLAGSIGN:
  609. Result:=([Ch_RSignFlag,Ch_RWSignFlag,Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  610. R_SUBFLAGOVERFLOW:
  611. Result:=([Ch_ROverflowFlag,Ch_RWOverflowFlag,Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag]*insprop[taicpu(p1).opcode].Ch)<>[];
  612. R_SUBFLAGINTERRUPT:
  613. Result:=([Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  614. R_SUBFLAGDIRECTION:
  615. Result:=([Ch_RDirFlag,Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*insprop[taicpu(p1).opcode].Ch)<>[];
  616. else
  617. ;
  618. end;
  619. if result then
  620. exit;
  621. end
  622. else if (getregtype(reg)=R_FPUREGISTER) and (Ch_FPU in insprop[taicpu(p1).opcode].Ch) then
  623. exit(true);
  624. Result:=inherited RegInInstruction(Reg, p1);
  625. end;
  626. function TX86AsmOptimizer.RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean;
  627. begin
  628. Result := False;
  629. if p1.typ <> ait_instruction then
  630. exit;
  631. with insprop[taicpu(p1).opcode] do
  632. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  633. begin
  634. case getsubreg(reg) of
  635. R_SUBW,R_SUBD,R_SUBQ:
  636. Result :=
  637. [Ch_WCarryFlag,Ch_WParityFlag,Ch_WAuxiliaryFlag,Ch_WZeroFlag,Ch_WSignFlag,Ch_WOverflowFlag,
  638. Ch_RWCarryFlag,Ch_RWParityFlag,Ch_RWAuxiliaryFlag,Ch_RWZeroFlag,Ch_RWSignFlag,Ch_RWOverflowFlag,
  639. Ch_W0DirFlag,Ch_W1DirFlag,Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  640. R_SUBFLAGCARRY:
  641. Result:=[Ch_WCarryFlag,Ch_RWCarryFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  642. R_SUBFLAGPARITY:
  643. Result:=[Ch_WParityFlag,Ch_RWParityFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  644. R_SUBFLAGAUXILIARY:
  645. Result:=[Ch_WAuxiliaryFlag,Ch_RWAuxiliaryFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  646. R_SUBFLAGZERO:
  647. Result:=[Ch_WZeroFlag,Ch_RWZeroFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  648. R_SUBFLAGSIGN:
  649. Result:=[Ch_WSignFlag,Ch_RWSignFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  650. R_SUBFLAGOVERFLOW:
  651. Result:=[Ch_WOverflowFlag,Ch_RWOverflowFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  652. R_SUBFLAGINTERRUPT:
  653. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  654. R_SUBFLAGDIRECTION:
  655. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags,Ch_RWFlags]*Ch<>[];
  656. else
  657. internalerror(2017042602);
  658. end;
  659. exit;
  660. end;
  661. case taicpu(p1).opcode of
  662. A_CALL:
  663. { We could potentially set Result to False if the register in
  664. question is non-volatile for the subroutine's calling convention,
  665. but this would require detecting the calling convention in use and
  666. also assuming that the routine doesn't contain malformed assembly
  667. language, for example... so it could only be done under -O4 as it
  668. would be considered a side-effect. [Kit] }
  669. Result := True;
  670. A_MOVSD:
  671. { special handling for SSE MOVSD }
  672. if (taicpu(p1).ops>0) then
  673. begin
  674. if taicpu(p1).ops<>2 then
  675. internalerror(2017042703);
  676. Result := (taicpu(p1).oper[1]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[1]^);
  677. end;
  678. { VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  679. so fix it here (FK)
  680. }
  681. A_VMOVSS,
  682. A_VMOVSD:
  683. begin
  684. Result := (taicpu(p1).ops=3) and (taicpu(p1).oper[2]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[2]^);
  685. exit;
  686. end;
  687. A_IMUL:
  688. Result := (taicpu(p1).oper[taicpu(p1).ops-1]^.typ=top_reg) and RegInOp(reg,taicpu(p1).oper[taicpu(p1).ops-1]^);
  689. else
  690. ;
  691. end;
  692. if Result then
  693. exit;
  694. with insprop[taicpu(p1).opcode] do
  695. begin
  696. if getregtype(reg)=R_INTREGISTER then
  697. begin
  698. case getsupreg(reg) of
  699. RS_EAX:
  700. if [Ch_WEAX,Ch_RWEAX,Ch_MEAX]*Ch<>[] then
  701. begin
  702. Result := True;
  703. exit
  704. end;
  705. RS_ECX:
  706. if [Ch_WECX,Ch_RWECX,Ch_MECX]*Ch<>[] then
  707. begin
  708. Result := True;
  709. exit
  710. end;
  711. RS_EDX:
  712. if [Ch_WEDX,Ch_RWEDX,Ch_MEDX]*Ch<>[] then
  713. begin
  714. Result := True;
  715. exit
  716. end;
  717. RS_EBX:
  718. if [Ch_WEBX,Ch_RWEBX,Ch_MEBX]*Ch<>[] then
  719. begin
  720. Result := True;
  721. exit
  722. end;
  723. RS_ESP:
  724. if [Ch_WESP,Ch_RWESP,Ch_MESP]*Ch<>[] then
  725. begin
  726. Result := True;
  727. exit
  728. end;
  729. RS_EBP:
  730. if [Ch_WEBP,Ch_RWEBP,Ch_MEBP]*Ch<>[] then
  731. begin
  732. Result := True;
  733. exit
  734. end;
  735. RS_ESI:
  736. if [Ch_WESI,Ch_RWESI,Ch_MESI]*Ch<>[] then
  737. begin
  738. Result := True;
  739. exit
  740. end;
  741. RS_EDI:
  742. if [Ch_WEDI,Ch_RWEDI,Ch_MEDI]*Ch<>[] then
  743. begin
  744. Result := True;
  745. exit
  746. end;
  747. end;
  748. end;
  749. if ([CH_RWOP1,CH_WOP1,CH_MOP1]*Ch<>[]) and reginop(reg,taicpu(p1).oper[0]^) then
  750. begin
  751. Result := true;
  752. exit
  753. end;
  754. if ([Ch_RWOP2,Ch_WOP2,Ch_MOP2]*Ch<>[]) and reginop(reg,taicpu(p1).oper[1]^) then
  755. begin
  756. Result := true;
  757. exit
  758. end;
  759. if ([Ch_RWOP3,Ch_WOP3,Ch_MOP3]*Ch<>[]) and reginop(reg,taicpu(p1).oper[2]^) then
  760. begin
  761. Result := true;
  762. exit
  763. end;
  764. if ([Ch_RWOP4,Ch_WOP4,Ch_MOP4]*Ch<>[]) and reginop(reg,taicpu(p1).oper[3]^) then
  765. begin
  766. Result := true;
  767. exit
  768. end;
  769. end;
  770. end;
  771. {$ifdef DEBUG_AOPTCPU}
  772. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);
  773. begin
  774. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  775. end;
  776. function debug_tostr(i: tcgint): string; inline;
  777. begin
  778. Result := tostr(i);
  779. end;
  780. function debug_regname(r: TRegister): string; inline;
  781. begin
  782. Result := '%' + std_regname(r);
  783. end;
  784. { Debug output function - creates a string representation of an operator }
  785. function debug_operstr(oper: TOper): string;
  786. begin
  787. case oper.typ of
  788. top_const:
  789. Result := '$' + debug_tostr(oper.val);
  790. top_reg:
  791. Result := debug_regname(oper.reg);
  792. top_ref:
  793. begin
  794. if oper.ref^.offset <> 0 then
  795. Result := debug_tostr(oper.ref^.offset) + '('
  796. else
  797. Result := '(';
  798. if (oper.ref^.base <> NR_INVALID) and (oper.ref^.base <> NR_NO) then
  799. begin
  800. Result := Result + debug_regname(oper.ref^.base);
  801. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  802. Result := Result + ',' + debug_regname(oper.ref^.index);
  803. end
  804. else
  805. if (oper.ref^.index <> NR_INVALID) and (oper.ref^.index <> NR_NO) then
  806. Result := Result + debug_regname(oper.ref^.index);
  807. if (oper.ref^.scalefactor > 1) then
  808. Result := Result + ',' + debug_tostr(oper.ref^.scalefactor) + ')'
  809. else
  810. Result := Result + ')';
  811. end;
  812. else
  813. Result := '[UNKNOWN]';
  814. end;
  815. end;
  816. function debug_op2str(opcode: tasmop): string; inline;
  817. begin
  818. Result := std_op2str[opcode];
  819. end;
  820. function debug_opsize2str(opsize: topsize): string; inline;
  821. begin
  822. Result := gas_opsize2str[opsize];
  823. end;
  824. {$else DEBUG_AOPTCPU}
  825. procedure TX86AsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  826. begin
  827. end;
  828. function debug_tostr(i: tcgint): string; inline;
  829. begin
  830. Result := '';
  831. end;
  832. function debug_regname(r: TRegister): string; inline;
  833. begin
  834. Result := '';
  835. end;
  836. function debug_operstr(oper: TOper): string; inline;
  837. begin
  838. Result := '';
  839. end;
  840. function debug_op2str(opcode: tasmop): string; inline;
  841. begin
  842. Result := '';
  843. end;
  844. function debug_opsize2str(opsize: topsize): string; inline;
  845. begin
  846. Result := '';
  847. end;
  848. {$endif DEBUG_AOPTCPU}
  849. class function TX86AsmOptimizer.IsMOVZXAcceptable: Boolean; inline;
  850. begin
  851. {$ifdef x86_64}
  852. { Always fine on x86-64 }
  853. Result := True;
  854. {$else x86_64}
  855. Result :=
  856. {$ifdef i8086}
  857. (current_settings.cputype >= cpu_386) and
  858. {$endif i8086}
  859. (
  860. { Always accept if optimising for size }
  861. (cs_opt_size in current_settings.optimizerswitches) or
  862. { From the Pentium II onwards, MOVZX only takes 1 cycle. [Kit] }
  863. (current_settings.optimizecputype >= cpu_Pentium2)
  864. );
  865. {$endif x86_64}
  866. end;
  867. function TX86AsmOptimizer.Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  868. begin
  869. if not SuperRegistersEqual(reg1,reg2) then
  870. exit(false);
  871. if getregtype(reg1)<>R_INTREGISTER then
  872. exit(true); {because SuperRegisterEqual is true}
  873. case getsubreg(reg1) of
  874. { A write to R_SUBL doesn't change R_SUBH and if reg2 is R_SUBW or
  875. higher, it preserves the high bits, so the new value depends on
  876. reg2's previous value. In other words, it is equivalent to doing:
  877. reg2 := (reg2 and $ffffff00) or byte(reg1); }
  878. R_SUBL:
  879. exit(getsubreg(reg2)=R_SUBL);
  880. { A write to R_SUBH doesn't change R_SUBL and if reg2 is R_SUBW or
  881. higher, it actually does a:
  882. reg2 := (reg2 and $ffff00ff) or (reg1 and $ff00); }
  883. R_SUBH:
  884. exit(getsubreg(reg2)=R_SUBH);
  885. { If reg2 is R_SUBD or larger, a write to R_SUBW preserves the high 16
  886. bits of reg2:
  887. reg2 := (reg2 and $ffff0000) or word(reg1); }
  888. R_SUBW:
  889. exit(getsubreg(reg2) in [R_SUBL,R_SUBH,R_SUBW]);
  890. { a write to R_SUBD always overwrites every other subregister,
  891. because it clears the high 32 bits of R_SUBQ on x86_64 }
  892. R_SUBD,
  893. R_SUBQ:
  894. exit(true);
  895. else
  896. internalerror(2017042801);
  897. end;
  898. end;
  899. function TX86AsmOptimizer.Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  900. begin
  901. if not SuperRegistersEqual(reg1,reg2) then
  902. exit(false);
  903. if getregtype(reg1)<>R_INTREGISTER then
  904. exit(true); {because SuperRegisterEqual is true}
  905. case getsubreg(reg1) of
  906. R_SUBL:
  907. exit(getsubreg(reg2)<>R_SUBH);
  908. R_SUBH:
  909. exit(getsubreg(reg2)<>R_SUBL);
  910. R_SUBW,
  911. R_SUBD,
  912. R_SUBQ:
  913. exit(true);
  914. else
  915. internalerror(2017042802);
  916. end;
  917. end;
  918. function TX86AsmOptimizer.PrePeepholeOptSxx(var p : tai) : boolean;
  919. var
  920. hp1 : tai;
  921. l : TCGInt;
  922. begin
  923. result:=false;
  924. { changes the code sequence
  925. shr/sar const1, x
  926. shl const2, x
  927. to
  928. either "sar/and", "shl/and" or just "and" depending on const1 and const2 }
  929. if GetNextInstruction(p, hp1) and
  930. MatchInstruction(hp1,A_SHL,[]) and
  931. (taicpu(p).oper[0]^.typ = top_const) and
  932. (taicpu(hp1).oper[0]^.typ = top_const) and
  933. (taicpu(hp1).opsize = taicpu(p).opsize) and
  934. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[1]^.typ) and
  935. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) then
  936. begin
  937. if (taicpu(p).oper[0]^.val > taicpu(hp1).oper[0]^.val) and
  938. not(cs_opt_size in current_settings.optimizerswitches) then
  939. begin
  940. { shr/sar const1, %reg
  941. shl const2, %reg
  942. with const1 > const2 }
  943. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  944. taicpu(hp1).opcode := A_AND;
  945. l := (1 shl (taicpu(hp1).oper[0]^.val)) - 1;
  946. case taicpu(p).opsize Of
  947. S_B: taicpu(hp1).loadConst(0,l Xor $ff);
  948. S_W: taicpu(hp1).loadConst(0,l Xor $ffff);
  949. S_L: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffff));
  950. S_Q: taicpu(hp1).loadConst(0,l Xor tcgint($ffffffffffffffff));
  951. else
  952. Internalerror(2017050703)
  953. end;
  954. end
  955. else if (taicpu(p).oper[0]^.val<taicpu(hp1).oper[0]^.val) and
  956. not(cs_opt_size in current_settings.optimizerswitches) then
  957. begin
  958. { shr/sar const1, %reg
  959. shl const2, %reg
  960. with const1 < const2 }
  961. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val-taicpu(p).oper[0]^.val);
  962. taicpu(p).opcode := A_AND;
  963. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  964. case taicpu(p).opsize Of
  965. S_B: taicpu(p).loadConst(0,l Xor $ff);
  966. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  967. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  968. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  969. else
  970. Internalerror(2017050702)
  971. end;
  972. end
  973. else if (taicpu(p).oper[0]^.val = taicpu(hp1).oper[0]^.val) then
  974. begin
  975. { shr/sar const1, %reg
  976. shl const2, %reg
  977. with const1 = const2 }
  978. taicpu(p).opcode := A_AND;
  979. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  980. case taicpu(p).opsize Of
  981. S_B: taicpu(p).loadConst(0,l Xor $ff);
  982. S_W: taicpu(p).loadConst(0,l Xor $ffff);
  983. S_L: taicpu(p).loadConst(0,l Xor tcgint($ffffffff));
  984. S_Q: taicpu(p).loadConst(0,l Xor tcgint($ffffffffffffffff));
  985. else
  986. Internalerror(2017050701)
  987. end;
  988. RemoveInstruction(hp1);
  989. end;
  990. end;
  991. end;
  992. function TX86AsmOptimizer.PrePeepholeOptIMUL(var p : tai) : boolean;
  993. var
  994. opsize : topsize;
  995. hp1 : tai;
  996. tmpref : treference;
  997. ShiftValue : Cardinal;
  998. BaseValue : TCGInt;
  999. begin
  1000. result:=false;
  1001. opsize:=taicpu(p).opsize;
  1002. { changes certain "imul const, %reg"'s to lea sequences }
  1003. if (MatchOpType(taicpu(p),top_const,top_reg) or
  1004. MatchOpType(taicpu(p),top_const,top_reg,top_reg)) and
  1005. (opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) then
  1006. if (taicpu(p).oper[0]^.val = 1) then
  1007. if (taicpu(p).ops = 2) then
  1008. { remove "imul $1, reg" }
  1009. begin
  1010. DebugMsg(SPeepholeOptimization + 'Imul2Nop done',p);
  1011. Result := RemoveCurrentP(p);
  1012. end
  1013. else
  1014. { change "imul $1, reg1, reg2" to "mov reg1, reg2" }
  1015. begin
  1016. hp1 := taicpu.Op_Reg_Reg(A_MOV, opsize, taicpu(p).oper[1]^.reg,taicpu(p).oper[2]^.reg);
  1017. InsertLLItem(p.previous, p.next, hp1);
  1018. DebugMsg(SPeepholeOptimization + 'Imul2Mov done',p);
  1019. p.free;
  1020. p := hp1;
  1021. end
  1022. else if ((taicpu(p).ops <= 2) or
  1023. (taicpu(p).oper[2]^.typ = Top_Reg)) and
  1024. not(cs_opt_size in current_settings.optimizerswitches) and
  1025. (not(GetNextInstruction(p, hp1)) or
  1026. not((tai(hp1).typ = ait_instruction) and
  1027. ((taicpu(hp1).opcode=A_Jcc) and
  1028. (taicpu(hp1).condition in [C_O,C_NO])))) then
  1029. begin
  1030. {
  1031. imul X, reg1, reg2 to
  1032. lea (reg1,reg1,Y), reg2
  1033. shl ZZ,reg2
  1034. imul XX, reg1 to
  1035. lea (reg1,reg1,YY), reg1
  1036. shl ZZ,reg2
  1037. This optimziation makes sense for pretty much every x86, except the VIA Nano3000: it has IMUL latency 2, lea/shl pair as well,
  1038. it does not exist as a separate optimization target in FPC though.
  1039. This optimziation can be applied as long as only two bits are set in the constant and those two bits are separated by
  1040. at most two zeros
  1041. }
  1042. reference_reset(tmpref,1,[]);
  1043. if (PopCnt(QWord(taicpu(p).oper[0]^.val))=2) and (BsrQWord(taicpu(p).oper[0]^.val)-BsfQWord(taicpu(p).oper[0]^.val)<=3) then
  1044. begin
  1045. ShiftValue:=BsfQWord(taicpu(p).oper[0]^.val);
  1046. BaseValue:=taicpu(p).oper[0]^.val shr ShiftValue;
  1047. TmpRef.base := taicpu(p).oper[1]^.reg;
  1048. TmpRef.index := taicpu(p).oper[1]^.reg;
  1049. if not(BaseValue in [3,5,9]) then
  1050. Internalerror(2018110101);
  1051. TmpRef.ScaleFactor := BaseValue-1;
  1052. if (taicpu(p).ops = 2) then
  1053. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[1]^.reg)
  1054. else
  1055. hp1 := taicpu.op_ref_reg(A_LEA, opsize, TmpRef, taicpu(p).oper[2]^.reg);
  1056. AsmL.InsertAfter(hp1,p);
  1057. DebugMsg(SPeepholeOptimization + 'Imul2LeaShl done',p);
  1058. taicpu(hp1).fileinfo:=taicpu(p).fileinfo;
  1059. RemoveCurrentP(p, hp1);
  1060. if ShiftValue>0 then
  1061. AsmL.InsertAfter(taicpu.op_const_reg(A_SHL, opsize, ShiftValue, taicpu(hp1).oper[1]^.reg),hp1);
  1062. end;
  1063. end;
  1064. end;
  1065. function TX86AsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  1066. var
  1067. p: taicpu;
  1068. begin
  1069. if not assigned(hp) or
  1070. (hp.typ <> ait_instruction) then
  1071. begin
  1072. Result := false;
  1073. exit;
  1074. end;
  1075. p := taicpu(hp);
  1076. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) then
  1077. with insprop[p.opcode] do
  1078. begin
  1079. case getsubreg(reg) of
  1080. R_SUBW,R_SUBD,R_SUBQ:
  1081. Result:=
  1082. RegLoadedWithNewValue(NR_CARRYFLAG,hp) and
  1083. RegLoadedWithNewValue(NR_PARITYFLAG,hp) and
  1084. RegLoadedWithNewValue(NR_AUXILIARYFLAG,hp) and
  1085. RegLoadedWithNewValue(NR_ZEROFLAG,hp) and
  1086. RegLoadedWithNewValue(NR_SIGNFLAG,hp) and
  1087. RegLoadedWithNewValue(NR_OVERFLOWFLAG,hp);
  1088. R_SUBFLAGCARRY:
  1089. Result:=[Ch_W0CarryFlag,Ch_W1CarryFlag,Ch_WCarryFlag,Ch_WUCarryFlag,Ch_WFlags]*Ch<>[];
  1090. R_SUBFLAGPARITY:
  1091. Result:=[Ch_W0ParityFlag,Ch_W1ParityFlag,Ch_WParityFlag,Ch_WUParityFlag,Ch_WFlags]*Ch<>[];
  1092. R_SUBFLAGAUXILIARY:
  1093. Result:=[Ch_W0AuxiliaryFlag,Ch_W1AuxiliaryFlag,Ch_WAuxiliaryFlag,Ch_WUAuxiliaryFlag,Ch_WFlags]*Ch<>[];
  1094. R_SUBFLAGZERO:
  1095. Result:=[Ch_W0ZeroFlag,Ch_W1ZeroFlag,Ch_WZeroFlag,Ch_WUZeroFlag,Ch_WFlags]*Ch<>[];
  1096. R_SUBFLAGSIGN:
  1097. Result:=[Ch_W0SignFlag,Ch_W1SignFlag,Ch_WSignFlag,Ch_WUSignFlag,Ch_WFlags]*Ch<>[];
  1098. R_SUBFLAGOVERFLOW:
  1099. Result:=[Ch_W0OverflowFlag,Ch_W1OverflowFlag,Ch_WOverflowFlag,Ch_WUOverflowFlag,Ch_WFlags]*Ch<>[];
  1100. R_SUBFLAGINTERRUPT:
  1101. Result:=[Ch_W0IntFlag,Ch_W1IntFlag,Ch_WFlags]*Ch<>[];
  1102. R_SUBFLAGDIRECTION:
  1103. Result:=[Ch_W0DirFlag,Ch_W1DirFlag,Ch_WFlags]*Ch<>[];
  1104. else
  1105. begin
  1106. writeln(getsubreg(reg));
  1107. internalerror(2017050501);
  1108. end;
  1109. end;
  1110. exit;
  1111. end;
  1112. Result :=
  1113. (((p.opcode = A_MOV) or
  1114. (p.opcode = A_MOVZX) or
  1115. (p.opcode = A_MOVSX) or
  1116. (p.opcode = A_LEA) or
  1117. (p.opcode = A_VMOVSS) or
  1118. (p.opcode = A_VMOVSD) or
  1119. (p.opcode = A_VMOVAPD) or
  1120. (p.opcode = A_VMOVAPS) or
  1121. (p.opcode = A_VMOVQ) or
  1122. (p.opcode = A_MOVSS) or
  1123. (p.opcode = A_MOVSD) or
  1124. (p.opcode = A_MOVQ) or
  1125. (p.opcode = A_MOVAPD) or
  1126. (p.opcode = A_MOVAPS) or
  1127. {$ifndef x86_64}
  1128. (p.opcode = A_LDS) or
  1129. (p.opcode = A_LES) or
  1130. {$endif not x86_64}
  1131. (p.opcode = A_LFS) or
  1132. (p.opcode = A_LGS) or
  1133. (p.opcode = A_LSS)) and
  1134. (p.ops=2) and { A_MOVSD can have zero operands, so this check is needed }
  1135. (p.oper[1]^.typ = top_reg) and
  1136. (Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg)) and
  1137. ((p.oper[0]^.typ = top_const) or
  1138. ((p.oper[0]^.typ = top_reg) and
  1139. not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  1140. ((p.oper[0]^.typ = top_ref) and
  1141. not RegInRef(reg,p.oper[0]^.ref^)))) or
  1142. ((p.opcode = A_POP) and
  1143. (Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg))) or
  1144. ((p.opcode = A_IMUL) and
  1145. (p.ops=3) and
  1146. (Reg1WriteOverwritesReg2Entirely(p.oper[2]^.reg,reg)) and
  1147. (((p.oper[1]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg))) or
  1148. ((p.oper[1]^.typ=top_ref) and not(RegInRef(reg,p.oper[1]^.ref^))))) or
  1149. ((((p.opcode = A_IMUL) or
  1150. (p.opcode = A_MUL)) and
  1151. (p.ops=1)) and
  1152. (((p.oper[0]^.typ=top_reg) and not(Reg1ReadDependsOnReg2(p.oper[0]^.reg,reg))) or
  1153. ((p.oper[0]^.typ=top_ref) and not(RegInRef(reg,p.oper[0]^.ref^)))) and
  1154. (((p.opsize=S_B) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  1155. ((p.opsize=S_W) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  1156. ((p.opsize=S_L) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg))
  1157. {$ifdef x86_64}
  1158. or ((p.opsize=S_Q) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg))
  1159. {$endif x86_64}
  1160. )) or
  1161. ((p.opcode = A_CWD) and Reg1WriteOverwritesReg2Entirely(NR_DX,reg)) or
  1162. ((p.opcode = A_CDQ) and Reg1WriteOverwritesReg2Entirely(NR_EDX,reg)) or
  1163. {$ifdef x86_64}
  1164. ((p.opcode = A_CQO) and Reg1WriteOverwritesReg2Entirely(NR_RDX,reg)) or
  1165. {$endif x86_64}
  1166. ((p.opcode = A_CBW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg) and not(Reg1ReadDependsOnReg2(NR_AL,reg))) or
  1167. {$ifndef x86_64}
  1168. ((p.opcode = A_LDS) and (reg=NR_DS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1169. ((p.opcode = A_LES) and (reg=NR_ES) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1170. {$endif not x86_64}
  1171. ((p.opcode = A_LFS) and (reg=NR_FS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1172. ((p.opcode = A_LGS) and (reg=NR_GS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1173. ((p.opcode = A_LSS) and (reg=NR_SS) and not(RegInRef(reg,p.oper[0]^.ref^))) or
  1174. {$ifndef x86_64}
  1175. ((p.opcode = A_AAM) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  1176. {$endif not x86_64}
  1177. ((p.opcode = A_LAHF) and Reg1WriteOverwritesReg2Entirely(NR_AH,reg)) or
  1178. ((p.opcode = A_LODSB) and Reg1WriteOverwritesReg2Entirely(NR_AL,reg)) or
  1179. ((p.opcode = A_LODSW) and Reg1WriteOverwritesReg2Entirely(NR_AX,reg)) or
  1180. ((p.opcode = A_LODSD) and Reg1WriteOverwritesReg2Entirely(NR_EAX,reg)) or
  1181. {$ifdef x86_64}
  1182. ((p.opcode = A_LODSQ) and Reg1WriteOverwritesReg2Entirely(NR_RAX,reg)) or
  1183. {$endif x86_64}
  1184. ((p.opcode = A_SETcc) and (p.oper[0]^.typ=top_reg) and Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  1185. (((p.opcode = A_FSTSW) or
  1186. (p.opcode = A_FNSTSW)) and
  1187. (p.oper[0]^.typ=top_reg) and
  1188. Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) or
  1189. (((p.opcode = A_XOR) or (p.opcode = A_SUB) or (p.opcode = A_SBB)) and
  1190. (p.oper[0]^.typ=top_reg) and (p.oper[1]^.typ=top_reg) and
  1191. (p.oper[0]^.reg=p.oper[1]^.reg) and
  1192. Reg1WriteOverwritesReg2Entirely(p.oper[1]^.reg,reg));
  1193. end;
  1194. class function TX86AsmOptimizer.IsExitCode(p : tai) : boolean;
  1195. var
  1196. hp2,hp3 : tai;
  1197. begin
  1198. { some x86-64 issue a NOP before the real exit code }
  1199. if MatchInstruction(p,A_NOP,[]) then
  1200. GetNextInstruction(p,p);
  1201. result:=assigned(p) and (p.typ=ait_instruction) and
  1202. ((taicpu(p).opcode = A_RET) or
  1203. ((taicpu(p).opcode=A_LEAVE) and
  1204. GetNextInstruction(p,hp2) and
  1205. MatchInstruction(hp2,A_RET,[S_NO])
  1206. ) or
  1207. (((taicpu(p).opcode=A_LEA) and
  1208. MatchOpType(taicpu(p),top_ref,top_reg) and
  1209. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  1210. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  1211. ) and
  1212. GetNextInstruction(p,hp2) and
  1213. MatchInstruction(hp2,A_RET,[S_NO])
  1214. ) or
  1215. ((((taicpu(p).opcode=A_MOV) and
  1216. MatchOpType(taicpu(p),top_reg,top_reg) and
  1217. (taicpu(p).oper[0]^.reg=current_procinfo.framepointer) and
  1218. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)) or
  1219. ((taicpu(p).opcode=A_LEA) and
  1220. MatchOpType(taicpu(p),top_ref,top_reg) and
  1221. (taicpu(p).oper[0]^.ref^.base=current_procinfo.framepointer) and
  1222. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG)
  1223. )
  1224. ) and
  1225. GetNextInstruction(p,hp2) and
  1226. MatchInstruction(hp2,A_POP,[reg2opsize(current_procinfo.framepointer)]) and
  1227. MatchOpType(taicpu(hp2),top_reg) and
  1228. (taicpu(hp2).oper[0]^.reg=current_procinfo.framepointer) and
  1229. GetNextInstruction(hp2,hp3) and
  1230. MatchInstruction(hp3,A_RET,[S_NO])
  1231. )
  1232. );
  1233. end;
  1234. class function TX86AsmOptimizer.isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  1235. begin
  1236. isFoldableArithOp := False;
  1237. case hp1.opcode of
  1238. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  1239. isFoldableArithOp :=
  1240. ((taicpu(hp1).oper[0]^.typ = top_const) or
  1241. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  1242. (taicpu(hp1).oper[0]^.reg <> reg))) and
  1243. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1244. (taicpu(hp1).oper[1]^.reg = reg);
  1245. A_INC,A_DEC,A_NEG,A_NOT:
  1246. isFoldableArithOp :=
  1247. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1248. (taicpu(hp1).oper[0]^.reg = reg);
  1249. else
  1250. ;
  1251. end;
  1252. end;
  1253. procedure TX86AsmOptimizer.RemoveLastDeallocForFuncRes(p: tai);
  1254. procedure DoRemoveLastDeallocForFuncRes( supreg: tsuperregister);
  1255. var
  1256. hp2: tai;
  1257. begin
  1258. hp2 := p;
  1259. repeat
  1260. hp2 := tai(hp2.previous);
  1261. if assigned(hp2) and
  1262. (hp2.typ = ait_regalloc) and
  1263. (tai_regalloc(hp2).ratype=ra_dealloc) and
  1264. (getregtype(tai_regalloc(hp2).reg) = R_INTREGISTER) and
  1265. (getsupreg(tai_regalloc(hp2).reg) = supreg) then
  1266. begin
  1267. RemoveInstruction(hp2);
  1268. break;
  1269. end;
  1270. until not(assigned(hp2)) or regInInstruction(newreg(R_INTREGISTER,supreg,R_SUBWHOLE),hp2);
  1271. end;
  1272. begin
  1273. case current_procinfo.procdef.returndef.typ of
  1274. arraydef,recorddef,pointerdef,
  1275. stringdef,enumdef,procdef,objectdef,errordef,
  1276. filedef,setdef,procvardef,
  1277. classrefdef,forwarddef:
  1278. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1279. orddef:
  1280. if current_procinfo.procdef.returndef.size <> 0 then
  1281. begin
  1282. DoRemoveLastDeallocForFuncRes(RS_EAX);
  1283. { for int64/qword }
  1284. if current_procinfo.procdef.returndef.size = 8 then
  1285. DoRemoveLastDeallocForFuncRes(RS_EDX);
  1286. end;
  1287. else
  1288. ;
  1289. end;
  1290. end;
  1291. function TX86AsmOptimizer.OptPass1_V_MOVAP(var p : tai) : boolean;
  1292. var
  1293. hp1,hp2 : tai;
  1294. begin
  1295. result:=false;
  1296. if MatchOpType(taicpu(p),top_reg,top_reg) then
  1297. begin
  1298. { vmova* reg1,reg1
  1299. =>
  1300. <nop> }
  1301. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  1302. begin
  1303. RemoveCurrentP(p);
  1304. result:=true;
  1305. exit;
  1306. end
  1307. else if GetNextInstruction(p,hp1) then
  1308. begin
  1309. if MatchInstruction(hp1,[taicpu(p).opcode],[S_NO]) and
  1310. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1311. begin
  1312. { vmova* reg1,reg2
  1313. vmova* reg2,reg3
  1314. dealloc reg2
  1315. =>
  1316. vmova* reg1,reg3 }
  1317. TransferUsedRegs(TmpUsedRegs);
  1318. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1319. if MatchOpType(taicpu(hp1),top_reg,top_reg) and
  1320. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1321. begin
  1322. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 1',p);
  1323. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1324. RemoveInstruction(hp1);
  1325. result:=true;
  1326. exit;
  1327. end
  1328. { special case:
  1329. vmova* reg1,<op>
  1330. vmova* <op>,reg1
  1331. =>
  1332. vmova* reg1,<op> }
  1333. else if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  1334. ((taicpu(p).oper[0]^.typ<>top_ref) or
  1335. (not(vol_read in taicpu(p).oper[0]^.ref^.volatility))
  1336. ) then
  1337. begin
  1338. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 2',p);
  1339. RemoveInstruction(hp1);
  1340. result:=true;
  1341. exit;
  1342. end
  1343. end
  1344. else if ((MatchInstruction(p,[A_MOVAPS,A_VMOVAPS],[S_NO]) and
  1345. MatchInstruction(hp1,[A_MOVSS,A_VMOVSS],[S_NO])) or
  1346. ((MatchInstruction(p,[A_MOVAPD,A_VMOVAPD],[S_NO]) and
  1347. MatchInstruction(hp1,[A_MOVSD,A_VMOVSD],[S_NO])))
  1348. ) and
  1349. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  1350. begin
  1351. { vmova* reg1,reg2
  1352. vmovs* reg2,<op>
  1353. dealloc reg2
  1354. =>
  1355. vmovs* reg1,reg3 }
  1356. TransferUsedRegs(TmpUsedRegs);
  1357. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1358. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  1359. begin
  1360. DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVS*2(V)MOVS* 1',p);
  1361. taicpu(p).opcode:=taicpu(hp1).opcode;
  1362. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  1363. RemoveInstruction(hp1);
  1364. result:=true;
  1365. exit;
  1366. end
  1367. end;
  1368. end;
  1369. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) then
  1370. begin
  1371. if MatchInstruction(hp1,[A_VFMADDPD,
  1372. A_VFMADD132PD,
  1373. A_VFMADD132PS,
  1374. A_VFMADD132SD,
  1375. A_VFMADD132SS,
  1376. A_VFMADD213PD,
  1377. A_VFMADD213PS,
  1378. A_VFMADD213SD,
  1379. A_VFMADD213SS,
  1380. A_VFMADD231PD,
  1381. A_VFMADD231PS,
  1382. A_VFMADD231SD,
  1383. A_VFMADD231SS,
  1384. A_VFMADDSUB132PD,
  1385. A_VFMADDSUB132PS,
  1386. A_VFMADDSUB213PD,
  1387. A_VFMADDSUB213PS,
  1388. A_VFMADDSUB231PD,
  1389. A_VFMADDSUB231PS,
  1390. A_VFMSUB132PD,
  1391. A_VFMSUB132PS,
  1392. A_VFMSUB132SD,
  1393. A_VFMSUB132SS,
  1394. A_VFMSUB213PD,
  1395. A_VFMSUB213PS,
  1396. A_VFMSUB213SD,
  1397. A_VFMSUB213SS,
  1398. A_VFMSUB231PD,
  1399. A_VFMSUB231PS,
  1400. A_VFMSUB231SD,
  1401. A_VFMSUB231SS,
  1402. A_VFMSUBADD132PD,
  1403. A_VFMSUBADD132PS,
  1404. A_VFMSUBADD213PD,
  1405. A_VFMSUBADD213PS,
  1406. A_VFMSUBADD231PD,
  1407. A_VFMSUBADD231PS,
  1408. A_VFNMADD132PD,
  1409. A_VFNMADD132PS,
  1410. A_VFNMADD132SD,
  1411. A_VFNMADD132SS,
  1412. A_VFNMADD213PD,
  1413. A_VFNMADD213PS,
  1414. A_VFNMADD213SD,
  1415. A_VFNMADD213SS,
  1416. A_VFNMADD231PD,
  1417. A_VFNMADD231PS,
  1418. A_VFNMADD231SD,
  1419. A_VFNMADD231SS,
  1420. A_VFNMSUB132PD,
  1421. A_VFNMSUB132PS,
  1422. A_VFNMSUB132SD,
  1423. A_VFNMSUB132SS,
  1424. A_VFNMSUB213PD,
  1425. A_VFNMSUB213PS,
  1426. A_VFNMSUB213SD,
  1427. A_VFNMSUB213SS,
  1428. A_VFNMSUB231PD,
  1429. A_VFNMSUB231PS,
  1430. A_VFNMSUB231SD,
  1431. A_VFNMSUB231SS],[S_NO]) and
  1432. { we mix single and double opperations here because we assume that the compiler
  1433. generates vmovapd only after double operations and vmovaps only after single operations }
  1434. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[2]^) and
  1435. GetNextInstruction(hp1,hp2) and
  1436. MatchInstruction(hp2,[A_VMOVAPD,A_VMOVAPS,A_MOVAPD,A_MOVAPS],[S_NO]) and
  1437. MatchOperand(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) then
  1438. begin
  1439. TransferUsedRegs(TmpUsedRegs);
  1440. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1441. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1442. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1443. begin
  1444. taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
  1445. RemoveCurrentP(p, hp1); // <-- Is this actually safe? hp1 is not necessarily the next instruction. [Kit]
  1446. RemoveInstruction(hp2);
  1447. end;
  1448. end
  1449. else if (hp1.typ = ait_instruction) and
  1450. GetNextInstruction(hp1, hp2) and
  1451. MatchInstruction(hp2,taicpu(p).opcode,[]) and
  1452. OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  1453. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  1454. MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  1455. (((taicpu(p).opcode=A_MOVAPS) and
  1456. ((taicpu(hp1).opcode=A_ADDSS) or (taicpu(hp1).opcode=A_SUBSS) or
  1457. (taicpu(hp1).opcode=A_MULSS) or (taicpu(hp1).opcode=A_DIVSS))) or
  1458. ((taicpu(p).opcode=A_MOVAPD) and
  1459. ((taicpu(hp1).opcode=A_ADDSD) or (taicpu(hp1).opcode=A_SUBSD) or
  1460. (taicpu(hp1).opcode=A_MULSD) or (taicpu(hp1).opcode=A_DIVSD)))
  1461. ) then
  1462. { change
  1463. movapX reg,reg2
  1464. addsX/subsX/... reg3, reg2
  1465. movapX reg2,reg
  1466. to
  1467. addsX/subsX/... reg3,reg
  1468. }
  1469. begin
  1470. TransferUsedRegs(TmpUsedRegs);
  1471. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1472. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1473. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  1474. begin
  1475. DebugMsg(SPeepholeOptimization + 'MovapXOpMovapX2Op ('+
  1476. debug_op2str(taicpu(p).opcode)+' '+
  1477. debug_op2str(taicpu(hp1).opcode)+' '+
  1478. debug_op2str(taicpu(hp2).opcode)+') done',p);
  1479. { we cannot eliminate the first move if
  1480. the operations uses the same register for source and dest }
  1481. if not(OpsEqual(taicpu(hp1).oper[1]^,taicpu(hp1).oper[0]^)) then
  1482. RemoveCurrentP(p, nil);
  1483. p:=hp1;
  1484. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  1485. RemoveInstruction(hp2);
  1486. result:=true;
  1487. end;
  1488. end;
  1489. end;
  1490. end;
  1491. end;
  1492. function TX86AsmOptimizer.OptPass1VOP(var p : tai) : boolean;
  1493. var
  1494. hp1 : tai;
  1495. begin
  1496. result:=false;
  1497. { replace
  1498. V<Op>X %mreg1,%mreg2,%mreg3
  1499. VMovX %mreg3,%mreg4
  1500. dealloc %mreg3
  1501. by
  1502. V<Op>X %mreg1,%mreg2,%mreg4
  1503. ?
  1504. }
  1505. if GetNextInstruction(p,hp1) and
  1506. { we mix single and double operations here because we assume that the compiler
  1507. generates vmovapd only after double operations and vmovaps only after single operations }
  1508. MatchInstruction(hp1,A_VMOVAPD,A_VMOVAPS,[S_NO]) and
  1509. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  1510. (taicpu(hp1).oper[1]^.typ=top_reg) then
  1511. begin
  1512. TransferUsedRegs(TmpUsedRegs);
  1513. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  1514. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  1515. begin
  1516. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  1517. DebugMsg(SPeepholeOptimization + 'VOpVmov2VOp done',p);
  1518. RemoveInstruction(hp1);
  1519. result:=true;
  1520. end;
  1521. end;
  1522. end;
  1523. { Replaces all references to AOldReg in a memory reference to ANewReg }
  1524. class function TX86AsmOptimizer.ReplaceRegisterInRef(var ref: TReference; const AOldReg, ANewReg: TRegister): Boolean;
  1525. begin
  1526. Result := False;
  1527. { For safety reasons, only check for exact register matches }
  1528. { Check base register }
  1529. if (ref.base = AOldReg) then
  1530. begin
  1531. ref.base := ANewReg;
  1532. Result := True;
  1533. end;
  1534. { Check index register }
  1535. if (ref.index = AOldReg) then
  1536. begin
  1537. ref.index := ANewReg;
  1538. Result := True;
  1539. end;
  1540. end;
  1541. { Replaces all references to AOldReg in an operand to ANewReg }
  1542. class function TX86AsmOptimizer.ReplaceRegisterInOper(const p: taicpu; const OperIdx: Integer; const AOldReg, ANewReg: TRegister): Boolean;
  1543. var
  1544. OldSupReg, NewSupReg: TSuperRegister;
  1545. OldSubReg, NewSubReg: TSubRegister;
  1546. OldRegType: TRegisterType;
  1547. ThisOper: POper;
  1548. begin
  1549. ThisOper := p.oper[OperIdx]; { Faster to access overall }
  1550. Result := False;
  1551. if (AOldReg = NR_NO) or (ANewReg = NR_NO) then
  1552. InternalError(2020011801);
  1553. OldSupReg := getsupreg(AOldReg);
  1554. OldSubReg := getsubreg(AOldReg);
  1555. OldRegType := getregtype(AOldReg);
  1556. NewSupReg := getsupreg(ANewReg);
  1557. NewSubReg := getsubreg(ANewReg);
  1558. if OldRegType <> getregtype(ANewReg) then
  1559. InternalError(2020011802);
  1560. if OldSubReg <> NewSubReg then
  1561. InternalError(2020011803);
  1562. case ThisOper^.typ of
  1563. top_reg:
  1564. if (
  1565. (ThisOper^.reg = AOldReg) or
  1566. (
  1567. (OldRegType = R_INTREGISTER) and
  1568. (getsupreg(ThisOper^.reg) = OldSupReg) and
  1569. (getregtype(ThisOper^.reg) = R_INTREGISTER) and
  1570. (
  1571. (getsubreg(ThisOper^.reg) <= OldSubReg)
  1572. {$ifndef x86_64}
  1573. and (
  1574. { Under i386 and i8086, ESI, EDI, EBP and ESP
  1575. don't have an 8-bit representation }
  1576. (getsubreg(ThisOper^.reg) >= R_SUBW) or
  1577. not (NewSupReg in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  1578. )
  1579. {$endif x86_64}
  1580. )
  1581. )
  1582. ) then
  1583. begin
  1584. ThisOper^.reg := newreg(getregtype(ANewReg), NewSupReg, getsubreg(p.oper[OperIdx]^.reg));
  1585. Result := True;
  1586. end;
  1587. top_ref:
  1588. if ReplaceRegisterInRef(ThisOper^.ref^, AOldReg, ANewReg) then
  1589. Result := True;
  1590. else
  1591. ;
  1592. end;
  1593. end;
  1594. { Replaces all references to AOldReg in an instruction to ANewReg }
  1595. function TX86AsmOptimizer.ReplaceRegisterInInstruction(const p: taicpu; const AOldReg, ANewReg: TRegister): Boolean;
  1596. const
  1597. ReadFlag: array[0..3] of TInsChange = (Ch_Rop1, Ch_Rop2, Ch_Rop3, Ch_Rop4);
  1598. var
  1599. OperIdx: Integer;
  1600. begin
  1601. Result := False;
  1602. for OperIdx := 0 to p.ops - 1 do
  1603. if (ReadFlag[OperIdx] in InsProp[p.Opcode].Ch) and
  1604. { The shift and rotate instructions can only use CL }
  1605. not (
  1606. (OperIdx = 0) and
  1607. { This second condition just helps to avoid unnecessarily
  1608. calling MatchInstruction for 10 different opcodes }
  1609. (p.oper[0]^.reg = NR_CL) and
  1610. MatchInstruction(p, [A_RCL, A_RCR, A_ROL, A_ROR, A_SAL, A_SAR, A_SHL, A_SHLD, A_SHR, A_SHRD], [])
  1611. ) then
  1612. Result := ReplaceRegisterInOper(p, OperIdx, AOldReg, ANewReg) or Result;
  1613. end;
  1614. class function TX86AsmOptimizer.IsRefSafe(const ref: PReference): Boolean; inline;
  1615. begin
  1616. Result :=
  1617. (ref^.index = NR_NO) and
  1618. (
  1619. {$ifdef x86_64}
  1620. (
  1621. (ref^.base = NR_RIP) and
  1622. (ref^.refaddr in [addr_pic, addr_pic_no_got])
  1623. ) or
  1624. {$endif x86_64}
  1625. (ref^.base = NR_STACK_POINTER_REG) or
  1626. (ref^.base = current_procinfo.framepointer)
  1627. );
  1628. end;
  1629. function TX86AsmOptimizer.ConvertLEA(const p: taicpu): Boolean;
  1630. var
  1631. l: asizeint;
  1632. begin
  1633. Result := False;
  1634. { Should have been checked previously }
  1635. if p.opcode <> A_LEA then
  1636. InternalError(2020072501);
  1637. { do not mess with the stack point as adjusting it by lea is recommend, except if we optimize for size }
  1638. if (p.oper[1]^.reg=NR_STACK_POINTER_REG) and
  1639. not(cs_opt_size in current_settings.optimizerswitches) then
  1640. exit;
  1641. with p.oper[0]^.ref^ do
  1642. begin
  1643. if (base <> p.oper[1]^.reg) or
  1644. (index <> NR_NO) or
  1645. assigned(symbol) then
  1646. exit;
  1647. l:=offset;
  1648. if (l=1) and UseIncDec then
  1649. begin
  1650. p.opcode:=A_INC;
  1651. p.loadreg(0,p.oper[1]^.reg);
  1652. p.ops:=1;
  1653. DebugMsg(SPeepholeOptimization + 'Lea2Inc done',p);
  1654. end
  1655. else if (l=-1) and UseIncDec then
  1656. begin
  1657. p.opcode:=A_DEC;
  1658. p.loadreg(0,p.oper[1]^.reg);
  1659. p.ops:=1;
  1660. DebugMsg(SPeepholeOptimization + 'Lea2Dec done',p);
  1661. end
  1662. else
  1663. begin
  1664. if (l<0) and (l<>-2147483648) then
  1665. begin
  1666. p.opcode:=A_SUB;
  1667. p.loadConst(0,-l);
  1668. DebugMsg(SPeepholeOptimization + 'Lea2Sub done',p);
  1669. end
  1670. else
  1671. begin
  1672. p.opcode:=A_ADD;
  1673. p.loadConst(0,l);
  1674. DebugMsg(SPeepholeOptimization + 'Lea2Add done',p);
  1675. end;
  1676. end;
  1677. end;
  1678. Result := True;
  1679. end;
  1680. function TX86AsmOptimizer.DeepMOVOpt(const p_mov: taicpu; const hp: taicpu): Boolean;
  1681. var
  1682. CurrentReg, ReplaceReg: TRegister;
  1683. begin
  1684. Result := False;
  1685. ReplaceReg := taicpu(p_mov).oper[0]^.reg;
  1686. CurrentReg := taicpu(p_mov).oper[1]^.reg;
  1687. case hp.opcode of
  1688. A_FSTSW, A_FNSTSW,
  1689. A_IN, A_INS, A_OUT, A_OUTS,
  1690. A_CMPS, A_LODS, A_MOVS, A_SCAS, A_STOS:
  1691. { These routines have explicit operands, but they are restricted in
  1692. what they can be (e.g. IN and OUT can only read from AL, AX or
  1693. EAX. }
  1694. Exit;
  1695. A_IMUL:
  1696. begin
  1697. { The 1-operand version writes to implicit registers
  1698. The 2-operand version reads from the first operator, and reads
  1699. from and writes to the second (equivalent to Ch_ROp1, ChRWOp2).
  1700. the 3-operand version reads from a register that it doesn't write to
  1701. }
  1702. case hp.ops of
  1703. 1:
  1704. if (
  1705. (
  1706. (hp.opsize = S_B) and (getsupreg(CurrentReg) <> RS_EAX)
  1707. ) or
  1708. not (getsupreg(CurrentReg) in [RS_EAX, RS_EDX])
  1709. ) and ReplaceRegisterInOper(hp, 0, CurrentReg, ReplaceReg) then
  1710. begin
  1711. Result := True;
  1712. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 1)', hp);
  1713. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  1714. end;
  1715. 2:
  1716. { Only modify the first parameter }
  1717. if ReplaceRegisterInOper(hp, 0, CurrentReg, ReplaceReg) then
  1718. begin
  1719. Result := True;
  1720. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 2)', hp);
  1721. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  1722. end;
  1723. 3:
  1724. { Only modify the second parameter }
  1725. if ReplaceRegisterInOper(hp, 1, CurrentReg, ReplaceReg) then
  1726. begin
  1727. Result := True;
  1728. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovIMul2MovIMul 3)', hp);
  1729. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  1730. end;
  1731. else
  1732. InternalError(2020012901);
  1733. end;
  1734. end;
  1735. else
  1736. if (hp.ops > 0) and
  1737. ReplaceRegisterInInstruction(hp, CurrentReg, ReplaceReg) then
  1738. begin
  1739. Result := True;
  1740. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + debug_regname(ReplaceReg) + '; changed to minimise pipeline stall (MovXXX2MovXXX)', hp);
  1741. AllocRegBetween(ReplaceReg, p_mov, hp, UsedRegs);
  1742. end;
  1743. end;
  1744. end;
  1745. function TX86AsmOptimizer.OptPass1MOV(var p : tai) : boolean;
  1746. var
  1747. hp1, hp2, hp3: tai;
  1748. procedure convert_mov_value(signed_movop: tasmop; max_value: tcgint); inline;
  1749. begin
  1750. if taicpu(hp1).opcode = signed_movop then
  1751. begin
  1752. if taicpu(p).oper[0]^.val > max_value shr 1 then
  1753. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val - max_value - 1 { Convert to signed }
  1754. end
  1755. else
  1756. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and max_value; { Trim to unsigned }
  1757. end;
  1758. var
  1759. GetNextInstruction_p, TempRegUsed: Boolean;
  1760. PreMessage, RegName1, RegName2, InputVal, MaskNum: string;
  1761. NewSize: topsize;
  1762. CurrentReg: TRegister;
  1763. begin
  1764. Result:=false;
  1765. GetNextInstruction_p:=GetNextInstruction(p, hp1);
  1766. { remove mov reg1,reg1? }
  1767. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^)
  1768. then
  1769. begin
  1770. DebugMsg(SPeepholeOptimization + 'Mov2Nop 1 done',p);
  1771. { take care of the register (de)allocs following p }
  1772. RemoveCurrentP(p, hp1);
  1773. Result:=true;
  1774. exit;
  1775. end;
  1776. { All the next optimisations require a next instruction }
  1777. if not GetNextInstruction_p or (hp1.typ <> ait_instruction) then
  1778. Exit;
  1779. { Look for:
  1780. mov %reg1,%reg2
  1781. ??? %reg2,r/m
  1782. Change to:
  1783. mov %reg1,%reg2
  1784. ??? %reg1,r/m
  1785. }
  1786. if MatchOpType(taicpu(p), top_reg, top_reg) then
  1787. begin
  1788. CurrentReg := taicpu(p).oper[1]^.reg;
  1789. if RegReadByInstruction(CurrentReg, hp1) and
  1790. DeepMOVOpt(taicpu(p), taicpu(hp1)) then
  1791. begin
  1792. TransferUsedRegs(TmpUsedRegs);
  1793. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  1794. if not RegUsedAfterInstruction(CurrentReg, hp1, TmpUsedRegs) and
  1795. { Just in case something didn't get modified (e.g. an
  1796. implicit register) }
  1797. not RegReadByInstruction(CurrentReg, hp1) then
  1798. begin
  1799. { We can remove the original MOV }
  1800. DebugMsg(SPeepholeOptimization + 'Mov2Nop 3 done',p);
  1801. RemoveCurrentp(p, hp1);
  1802. { TmpUsedRegs contains the results of "UpdateUsedRegs(tai(p.Next))" already,
  1803. so just restore it to UsedRegs instead of calculating it again }
  1804. RestoreUsedRegs(TmpUsedRegs);
  1805. Result := True;
  1806. Exit;
  1807. end;
  1808. { If we know a MOV instruction has become a null operation, we might as well
  1809. get rid of it now to save time. }
  1810. if (taicpu(hp1).opcode = A_MOV) and
  1811. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1812. SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[0]^.reg) and
  1813. { Just being a register is enough to confirm it's a null operation }
  1814. (taicpu(hp1).oper[0]^.typ = top_reg) then
  1815. begin
  1816. Result := True;
  1817. { Speed-up to reduce a pipeline stall... if we had something like...
  1818. movl %eax,%edx
  1819. movw %dx,%ax
  1820. ... the second instruction would change to movw %ax,%ax, but
  1821. given that it is now %ax that's active rather than %eax,
  1822. penalties might occur due to a partial register write, so instead,
  1823. change it to a MOVZX instruction when optimising for speed.
  1824. }
  1825. if not (cs_opt_size in current_settings.optimizerswitches) and
  1826. IsMOVZXAcceptable and
  1827. (taicpu(hp1).opsize < taicpu(p).opsize)
  1828. {$ifdef x86_64}
  1829. { operations already implicitly set the upper 64 bits to zero }
  1830. and not ((taicpu(hp1).opsize = S_L) and (taicpu(p).opsize = S_Q))
  1831. {$endif x86_64}
  1832. then
  1833. begin
  1834. CurrentReg := taicpu(hp1).oper[1]^.reg;
  1835. DebugMsg(SPeepholeOptimization + 'Zero-extension to minimise pipeline stall (Mov2Movz)',hp1);
  1836. case taicpu(p).opsize of
  1837. S_W:
  1838. if taicpu(hp1).opsize = S_B then
  1839. taicpu(hp1).opsize := S_BL
  1840. else
  1841. InternalError(2020012911);
  1842. S_L{$ifdef x86_64}, S_Q{$endif x86_64}:
  1843. case taicpu(hp1).opsize of
  1844. S_B:
  1845. taicpu(hp1).opsize := S_BL;
  1846. S_W:
  1847. taicpu(hp1).opsize := S_WL;
  1848. else
  1849. InternalError(2020012912);
  1850. end;
  1851. else
  1852. InternalError(2020012910);
  1853. end;
  1854. taicpu(hp1).opcode := A_MOVZX;
  1855. taicpu(hp1).oper[1]^.reg := newreg(getregtype(CurrentReg), getsupreg(CurrentReg), R_SUBD)
  1856. end
  1857. else
  1858. begin
  1859. GetNextInstruction_p := GetNextInstruction(hp1, hp2);
  1860. DebugMsg(SPeepholeOptimization + 'Mov2Nop 4 done',hp1);
  1861. RemoveInstruction(hp1);
  1862. { The instruction after what was hp1 is now the immediate next instruction,
  1863. so we can continue to make optimisations if it's present }
  1864. if not GetNextInstruction_p or (hp2.typ <> ait_instruction) then
  1865. Exit;
  1866. hp1 := hp2;
  1867. end;
  1868. end;
  1869. end;
  1870. end;
  1871. { Depending on the DeepMOVOpt above, it may turn out that hp1 completely
  1872. overwrites the original destination register. e.g.
  1873. movl ###,%reg2d
  1874. movslq ###,%reg2q (### doesn't have to be the same as the first one)
  1875. In this case, we can remove the MOV (Go to "Mov2Nop 5" below)
  1876. }
  1877. if (taicpu(p).oper[1]^.typ = top_reg) and
  1878. MatchInstruction(hp1, [A_LEA, A_MOV, A_MOVSX, A_MOVZX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}], []) and
  1879. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1880. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, taicpu(p).oper[1]^.reg) then
  1881. begin
  1882. if RegInOp(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[0]^) then
  1883. begin
  1884. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  1885. case taicpu(p).oper[0]^.typ of
  1886. top_const:
  1887. { We have something like:
  1888. movb $x, %regb
  1889. movzbl %regb,%regd
  1890. Change to:
  1891. movl $x, %regd
  1892. }
  1893. begin
  1894. case taicpu(hp1).opsize of
  1895. S_BW:
  1896. begin
  1897. convert_mov_value(A_MOVSX, $FF);
  1898. setsubreg(taicpu(p).oper[1]^.reg, R_SUBW);
  1899. taicpu(p).opsize := S_W;
  1900. end;
  1901. S_BL:
  1902. begin
  1903. convert_mov_value(A_MOVSX, $FF);
  1904. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  1905. taicpu(p).opsize := S_L;
  1906. end;
  1907. S_WL:
  1908. begin
  1909. convert_mov_value(A_MOVSX, $FFFF);
  1910. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  1911. taicpu(p).opsize := S_L;
  1912. end;
  1913. {$ifdef x86_64}
  1914. S_BQ:
  1915. begin
  1916. convert_mov_value(A_MOVSX, $FF);
  1917. setsubreg(taicpu(p).oper[1]^.reg, R_SUBQ);
  1918. taicpu(p).opsize := S_Q;
  1919. end;
  1920. S_WQ:
  1921. begin
  1922. convert_mov_value(A_MOVSX, $FFFF);
  1923. setsubreg(taicpu(p).oper[1]^.reg, R_SUBQ);
  1924. taicpu(p).opsize := S_Q;
  1925. end;
  1926. S_LQ:
  1927. begin
  1928. convert_mov_value(A_MOVSXD, $FFFFFFFF); { Note it's MOVSXD, not MOVSX }
  1929. setsubreg(taicpu(p).oper[1]^.reg, R_SUBQ);
  1930. taicpu(p).opsize := S_Q;
  1931. end;
  1932. {$endif x86_64}
  1933. else
  1934. { If hp1 was a MOV instruction, it should have been
  1935. optimised already }
  1936. InternalError(2020021001);
  1937. end;
  1938. DebugMsg(SPeepholeOptimization + 'MovMovXX2MovXX 2 done',p);
  1939. RemoveInstruction(hp1);
  1940. Result := True;
  1941. Exit;
  1942. end;
  1943. top_ref:
  1944. { We have something like:
  1945. movb mem, %regb
  1946. movzbl %regb,%regd
  1947. Change to:
  1948. movzbl mem, %regd
  1949. }
  1950. if (taicpu(p).oper[0]^.ref^.refaddr<>addr_full) and (IsMOVZXAcceptable or (taicpu(hp1).opcode<>A_MOVZX)) then
  1951. begin
  1952. DebugMsg(SPeepholeOptimization + 'MovMovXX2MovXX 1 done',p);
  1953. taicpu(hp1).loadref(0,taicpu(p).oper[0]^.ref^);
  1954. RemoveCurrentP(p, hp1);
  1955. Result:=True;
  1956. Exit;
  1957. end;
  1958. else
  1959. if (taicpu(hp1).opcode <> A_MOV) and (taicpu(hp1).opcode <> A_LEA) then
  1960. { Just to make a saving, since there are no more optimisations with MOVZX and MOVSX/D }
  1961. Exit;
  1962. end;
  1963. end
  1964. { The RegInOp check makes sure that movl r/m,%reg1l; movzbl (%reg1l),%reg1l"
  1965. and "movl r/m,%reg1; leal $1(%reg1,%reg2),%reg1" etc. are not incorrectly
  1966. optimised }
  1967. else
  1968. begin
  1969. DebugMsg(SPeepholeOptimization + 'Mov2Nop 5 done',p);
  1970. RemoveCurrentP(p, hp1);
  1971. Result := True;
  1972. Exit;
  1973. end;
  1974. end;
  1975. if (taicpu(hp1).opcode = A_AND) and
  1976. (taicpu(p).oper[1]^.typ = top_reg) and
  1977. MatchOpType(taicpu(hp1),top_const,top_reg) then
  1978. begin
  1979. if MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[1]^) then
  1980. begin
  1981. case taicpu(p).opsize of
  1982. S_L:
  1983. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  1984. begin
  1985. { Optimize out:
  1986. mov x, %reg
  1987. and ffffffffh, %reg
  1988. }
  1989. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 1 done',p);
  1990. RemoveInstruction(hp1);
  1991. Result:=true;
  1992. exit;
  1993. end;
  1994. S_Q: { TODO: Confirm if this is even possible }
  1995. if (taicpu(hp1).oper[0]^.val = $ffffffffffffffff) then
  1996. begin
  1997. { Optimize out:
  1998. mov x, %reg
  1999. and ffffffffffffffffh, %reg
  2000. }
  2001. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 2 done',p);
  2002. RemoveInstruction(hp1);
  2003. Result:=true;
  2004. exit;
  2005. end;
  2006. else
  2007. ;
  2008. end;
  2009. if ((taicpu(p).oper[0]^.typ=top_reg) or
  2010. ((taicpu(p).oper[0]^.typ=top_ref) and (taicpu(p).oper[0]^.ref^.refaddr<>addr_full))) and
  2011. GetNextInstruction(hp1,hp2) and
  2012. MatchInstruction(hp2,A_TEST,[taicpu(p).opsize]) and
  2013. MatchOperand(taicpu(hp1).oper[1]^,taicpu(hp2).oper[1]^) and
  2014. MatchOperand(taicpu(hp2).oper[0]^,taicpu(hp2).oper[1]^) and
  2015. GetNextInstruction(hp2,hp3) and
  2016. MatchInstruction(hp3,A_Jcc,A_Setcc,[S_NO]) and
  2017. (taicpu(hp3).condition in [C_E,C_NE]) then
  2018. begin
  2019. TransferUsedRegs(TmpUsedRegs);
  2020. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  2021. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  2022. if not(RegUsedAfterInstruction(taicpu(hp2).oper[1]^.reg, hp2, TmpUsedRegs)) then
  2023. begin
  2024. DebugMsg(SPeepholeOptimization + 'MovAndTest2Test done',p);
  2025. taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
  2026. taicpu(hp1).opcode:=A_TEST;
  2027. RemoveInstruction(hp2);
  2028. RemoveCurrentP(p, hp1);
  2029. Result:=true;
  2030. exit;
  2031. end;
  2032. end;
  2033. end
  2034. else if IsMOVZXAcceptable and
  2035. (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(hp1).oper[1]^.typ = top_reg) and
  2036. (taicpu(p).oper[0]^.typ <> top_const) and { MOVZX only supports registers and memory, not immediates (use MOV for that!) }
  2037. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  2038. then
  2039. begin
  2040. InputVal := debug_operstr(taicpu(p).oper[0]^);
  2041. MaskNum := debug_tostr(taicpu(hp1).oper[0]^.val);
  2042. case taicpu(p).opsize of
  2043. S_B:
  2044. if (taicpu(hp1).oper[0]^.val = $ff) then
  2045. begin
  2046. { Convert:
  2047. movb x, %regl movb x, %regl
  2048. andw ffh, %regw andl ffh, %regd
  2049. To:
  2050. movzbw x, %regd movzbl x, %regd
  2051. (Identical registers, just different sizes)
  2052. }
  2053. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 8-bit register name }
  2054. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 16/32-bit register name }
  2055. case taicpu(hp1).opsize of
  2056. S_W: NewSize := S_BW;
  2057. S_L: NewSize := S_BL;
  2058. {$ifdef x86_64}
  2059. S_Q: NewSize := S_BQ;
  2060. {$endif x86_64}
  2061. else
  2062. InternalError(2018011510);
  2063. end;
  2064. end
  2065. else
  2066. NewSize := S_NO;
  2067. S_W:
  2068. if (taicpu(hp1).oper[0]^.val = $ffff) then
  2069. begin
  2070. { Convert:
  2071. movw x, %regw
  2072. andl ffffh, %regd
  2073. To:
  2074. movzwl x, %regd
  2075. (Identical registers, just different sizes)
  2076. }
  2077. RegName1 := debug_regname(taicpu(p).oper[1]^.reg); { 16-bit register name }
  2078. RegName2 := debug_regname(taicpu(hp1).oper[1]^.reg); { 32-bit register name }
  2079. case taicpu(hp1).opsize of
  2080. S_L: NewSize := S_WL;
  2081. {$ifdef x86_64}
  2082. S_Q: NewSize := S_WQ;
  2083. {$endif x86_64}
  2084. else
  2085. InternalError(2018011511);
  2086. end;
  2087. end
  2088. else
  2089. NewSize := S_NO;
  2090. else
  2091. NewSize := S_NO;
  2092. end;
  2093. if NewSize <> S_NO then
  2094. begin
  2095. PreMessage := 'mov' + debug_opsize2str(taicpu(p).opsize) + ' ' + InputVal + ',' + RegName1;
  2096. { The actual optimization }
  2097. taicpu(p).opcode := A_MOVZX;
  2098. taicpu(p).changeopsize(NewSize);
  2099. taicpu(p).oper[1]^ := taicpu(hp1).oper[1]^;
  2100. { Safeguard if "and" is followed by a conditional command }
  2101. TransferUsedRegs(TmpUsedRegs);
  2102. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  2103. if (RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  2104. begin
  2105. { At this point, the "and" command is effectively equivalent to
  2106. "test %reg,%reg". This will be handled separately by the
  2107. Peephole Optimizer. [Kit] }
  2108. DebugMsg(SPeepholeOptimization + PreMessage +
  2109. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  2110. end
  2111. else
  2112. begin
  2113. DebugMsg(SPeepholeOptimization + PreMessage + '; and' + debug_opsize2str(taicpu(hp1).opsize) + ' $' + MaskNum + ',' + RegName2 +
  2114. ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
  2115. RemoveInstruction(hp1);
  2116. end;
  2117. Result := True;
  2118. Exit;
  2119. end;
  2120. end;
  2121. end;
  2122. { Next instruction is also a MOV ? }
  2123. if MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) then
  2124. begin
  2125. if (taicpu(p).oper[1]^.typ = top_reg) and
  2126. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
  2127. begin
  2128. CurrentReg := taicpu(p).oper[1]^.reg;
  2129. TransferUsedRegs(TmpUsedRegs);
  2130. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  2131. { we have
  2132. mov x, %treg
  2133. mov %treg, y
  2134. }
  2135. if not(RegInOp(CurrentReg, taicpu(hp1).oper[1]^)) then
  2136. if not(RegUsedAfterInstruction(CurrentReg, hp1, TmpUsedRegs)) then
  2137. { we've got
  2138. mov x, %treg
  2139. mov %treg, y
  2140. with %treg is not used after }
  2141. case taicpu(p).oper[0]^.typ Of
  2142. { top_reg is covered by DeepMOVOpt }
  2143. top_const:
  2144. begin
  2145. { change
  2146. mov const, %treg
  2147. mov %treg, y
  2148. to
  2149. mov const, y
  2150. }
  2151. if (taicpu(hp1).oper[1]^.typ=top_reg) or
  2152. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  2153. begin
  2154. if taicpu(hp1).oper[1]^.typ=top_reg then
  2155. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  2156. taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
  2157. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 5 done',p);
  2158. RemoveInstruction(hp1);
  2159. Result:=true;
  2160. Exit;
  2161. end;
  2162. end;
  2163. top_ref:
  2164. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  2165. begin
  2166. { change
  2167. mov mem, %treg
  2168. mov %treg, %reg
  2169. to
  2170. mov mem, %reg"
  2171. }
  2172. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  2173. taicpu(p).loadreg(1, taicpu(hp1).oper[1]^.reg);
  2174. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3 done',p);
  2175. RemoveInstruction(hp1);
  2176. Result:=true;
  2177. Exit;
  2178. end;
  2179. else
  2180. ;
  2181. end
  2182. else
  2183. { %treg is used afterwards, but all eventualities
  2184. other than the first MOV instruction being a constant
  2185. are covered by DeepMOVOpt, so only check for that }
  2186. if (taicpu(p).oper[0]^.typ = top_const) and
  2187. (
  2188. { For MOV operations, a size saving is only made if the register/const is byte-sized }
  2189. not (cs_opt_size in current_settings.optimizerswitches) or
  2190. (taicpu(hp1).opsize = S_B)
  2191. ) and
  2192. (
  2193. (taicpu(hp1).oper[1]^.typ = top_reg) or
  2194. ((taicpu(p).oper[0]^.val >= low(longint)) and (taicpu(p).oper[0]^.val <= high(longint)))
  2195. ) then
  2196. begin
  2197. DebugMsg(SPeepholeOptimization + debug_operstr(taicpu(hp1).oper[0]^) + ' = $' + debug_tostr(taicpu(p).oper[0]^.val) + '; changed to minimise pipeline stall (MovMov2Mov 6b)',hp1);
  2198. taicpu(hp1).loadconst(0, taicpu(p).oper[0]^.val);
  2199. end;
  2200. end;
  2201. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  2202. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  2203. { mov reg1, mem1 or mov mem1, reg1
  2204. mov mem2, reg2 mov reg2, mem2}
  2205. begin
  2206. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  2207. { mov reg1, mem1 or mov mem1, reg1
  2208. mov mem2, reg1 mov reg2, mem1}
  2209. begin
  2210. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2211. { Removes the second statement from
  2212. mov reg1, mem1/reg2
  2213. mov mem1/reg2, reg1 }
  2214. begin
  2215. if taicpu(p).oper[0]^.typ=top_reg then
  2216. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2217. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 1',p);
  2218. RemoveInstruction(hp1);
  2219. Result:=true;
  2220. exit;
  2221. end
  2222. else
  2223. begin
  2224. TransferUsedRegs(TmpUsedRegs);
  2225. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2226. if (taicpu(p).oper[1]^.typ = top_ref) and
  2227. { mov reg1, mem1
  2228. mov mem2, reg1 }
  2229. (taicpu(hp1).oper[0]^.ref^.refaddr = addr_no) and
  2230. GetNextInstruction(hp1, hp2) and
  2231. MatchInstruction(hp2,A_CMP,[taicpu(p).opsize]) and
  2232. OpsEqual(taicpu(p).oper[1]^,taicpu(hp2).oper[0]^) and
  2233. OpsEqual(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) and
  2234. not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs)) then
  2235. { change to
  2236. mov reg1, mem1 mov reg1, mem1
  2237. mov mem2, reg1 cmp reg1, mem2
  2238. cmp mem1, reg1
  2239. }
  2240. begin
  2241. RemoveInstruction(hp2);
  2242. taicpu(hp1).opcode := A_CMP;
  2243. taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
  2244. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  2245. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  2246. DebugMsg(SPeepholeOptimization + 'MovMovCmp2MovCmp done',hp1);
  2247. end;
  2248. end;
  2249. end
  2250. else if (taicpu(p).oper[1]^.typ=top_ref) and
  2251. OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2252. begin
  2253. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  2254. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  2255. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov1 done',p);
  2256. end
  2257. else
  2258. begin
  2259. TransferUsedRegs(TmpUsedRegs);
  2260. if GetNextInstruction(hp1, hp2) and
  2261. MatchOpType(taicpu(p),top_ref,top_reg) and
  2262. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2263. (taicpu(hp1).oper[1]^.typ = top_ref) and
  2264. MatchInstruction(hp2,A_MOV,[taicpu(p).opsize]) and
  2265. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  2266. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  2267. if not RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^) and
  2268. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,tmpUsedRegs)) then
  2269. { mov mem1, %reg1
  2270. mov %reg1, mem2
  2271. mov mem2, reg2
  2272. to:
  2273. mov mem1, reg2
  2274. mov reg2, mem2}
  2275. begin
  2276. AllocRegBetween(taicpu(hp2).oper[1]^.reg,p,hp2,usedregs);
  2277. DebugMsg(SPeepholeOptimization + 'MovMovMov2MovMov 1 done',p);
  2278. taicpu(p).loadoper(1,taicpu(hp2).oper[1]^);
  2279. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  2280. RemoveInstruction(hp2);
  2281. end
  2282. {$ifdef i386}
  2283. { this is enabled for i386 only, as the rules to create the reg sets below
  2284. are too complicated for x86-64, so this makes this code too error prone
  2285. on x86-64
  2286. }
  2287. else if (taicpu(p).oper[1]^.reg <> taicpu(hp2).oper[1]^.reg) and
  2288. not(RegInRef(taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.ref^)) and
  2289. not(RegInRef(taicpu(hp2).oper[1]^.reg,taicpu(hp2).oper[0]^.ref^)) then
  2290. { mov mem1, reg1 mov mem1, reg1
  2291. mov reg1, mem2 mov reg1, mem2
  2292. mov mem2, reg2 mov mem2, reg1
  2293. to: to:
  2294. mov mem1, reg1 mov mem1, reg1
  2295. mov mem1, reg2 mov reg1, mem2
  2296. mov reg1, mem2
  2297. or (if mem1 depends on reg1
  2298. and/or if mem2 depends on reg2)
  2299. to:
  2300. mov mem1, reg1
  2301. mov reg1, mem2
  2302. mov reg1, reg2
  2303. }
  2304. begin
  2305. taicpu(hp1).loadRef(0,taicpu(p).oper[0]^.ref^);
  2306. taicpu(hp1).loadReg(1,taicpu(hp2).oper[1]^.reg);
  2307. taicpu(hp2).loadRef(1,taicpu(hp2).oper[0]^.ref^);
  2308. taicpu(hp2).loadReg(0,taicpu(p).oper[1]^.reg);
  2309. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  2310. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  2311. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  2312. AllocRegBetween(taicpu(p).oper[0]^.ref^.base,p,hp2,usedregs);
  2313. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and
  2314. (getsupreg(taicpu(p).oper[0]^.ref^.index) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  2315. AllocRegBetween(taicpu(p).oper[0]^.ref^.index,p,hp2,usedregs);
  2316. end
  2317. else if (taicpu(hp1).Oper[0]^.reg <> taicpu(hp2).Oper[1]^.reg) then
  2318. begin
  2319. taicpu(hp2).loadReg(0,taicpu(hp1).Oper[0]^.reg);
  2320. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  2321. end
  2322. else
  2323. begin
  2324. RemoveInstruction(hp2);
  2325. end
  2326. {$endif i386}
  2327. ;
  2328. end;
  2329. end
  2330. { movl [mem1],reg1
  2331. movl [mem1],reg2
  2332. to
  2333. movl [mem1],reg1
  2334. movl reg1,reg2
  2335. }
  2336. else if MatchOpType(taicpu(p),top_ref,top_reg) and
  2337. MatchOpType(taicpu(hp1),top_ref,top_reg) and
  2338. (taicpu(p).opsize = taicpu(hp1).opsize) and
  2339. RefsEqual(taicpu(p).oper[0]^.ref^,taicpu(hp1).oper[0]^.ref^) and
  2340. (taicpu(p).oper[0]^.ref^.volatility=[]) and
  2341. (taicpu(hp1).oper[0]^.ref^.volatility=[]) and
  2342. not(SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^.base)) and
  2343. not(SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^.index)) then
  2344. begin
  2345. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 2',p);
  2346. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  2347. end;
  2348. { movl const1,[mem1]
  2349. movl [mem1],reg1
  2350. to
  2351. movl const1,reg1
  2352. movl reg1,[mem1]
  2353. }
  2354. if MatchOpType(Taicpu(p),top_const,top_ref) and
  2355. MatchOpType(Taicpu(hp1),top_ref,top_reg) and
  2356. (taicpu(p).opsize = taicpu(hp1).opsize) and
  2357. RefsEqual(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.ref^) and
  2358. not(RegInRef(taicpu(hp1).oper[1]^.reg,taicpu(hp1).oper[0]^.ref^)) then
  2359. begin
  2360. AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  2361. taicpu(hp1).loadReg(0,taicpu(hp1).oper[1]^.reg);
  2362. taicpu(hp1).loadRef(1,taicpu(p).oper[1]^.ref^);
  2363. taicpu(p).loadReg(1,taicpu(hp1).oper[0]^.reg);
  2364. taicpu(hp1).fileinfo := taicpu(p).fileinfo;
  2365. DebugMsg(SPeepholeOptimization + 'MovMov2MovMov 1',p);
  2366. Result:=true;
  2367. exit;
  2368. end;
  2369. { mov x,reg1; mov y,reg1 -> mov y,reg1 is handled by the Mov2Nop 5 optimisation }
  2370. end;
  2371. { search further than the next instruction for a mov }
  2372. if
  2373. { check as much as possible before the expensive GetNextInstructionUsingReg call }
  2374. (taicpu(p).oper[1]^.typ = top_reg) and
  2375. (taicpu(p).oper[0]^.typ in [top_reg,top_const]) and
  2376. not RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp1) and
  2377. { we work with hp2 here, so hp1 can be still used later on when
  2378. checking for GetNextInstruction_p }
  2379. { GetNextInstructionUsingReg only searches one instruction ahead unless -O3 is specified }
  2380. GetNextInstructionUsingReg(hp1,hp2,taicpu(p).oper[1]^.reg) and
  2381. (hp2.typ=ait_instruction) then
  2382. begin
  2383. case taicpu(hp2).opcode of
  2384. A_MOV:
  2385. if MatchOperand(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^.reg) and
  2386. ((taicpu(p).oper[0]^.typ=top_const) or
  2387. ((taicpu(p).oper[0]^.typ=top_reg) and
  2388. not(RegUsedBetween(taicpu(p).oper[0]^.reg, p, hp2))
  2389. )
  2390. ) then
  2391. begin
  2392. { we have
  2393. mov x, %treg
  2394. mov %treg, y
  2395. }
  2396. TransferUsedRegs(TmpUsedRegs);
  2397. TmpUsedRegs[R_INTREGISTER].Update(tai(p.Next));
  2398. { We don't need to call UpdateUsedRegs for every instruction between
  2399. p and hp2 because the register we're concerned about will not
  2400. become deallocated (otherwise GetNextInstructionUsingReg would
  2401. have stopped at an earlier instruction). [Kit] }
  2402. TempRegUsed :=
  2403. RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs) or
  2404. RegReadByInstruction(taicpu(p).oper[1]^.reg, hp1);
  2405. case taicpu(p).oper[0]^.typ Of
  2406. top_reg:
  2407. begin
  2408. { change
  2409. mov %reg, %treg
  2410. mov %treg, y
  2411. to
  2412. mov %reg, y
  2413. }
  2414. CurrentReg := taicpu(p).oper[0]^.reg; { Saves on a handful of pointer dereferences }
  2415. RegName1 := debug_regname(taicpu(hp2).oper[0]^.reg);
  2416. if taicpu(hp2).oper[1]^.reg = CurrentReg then
  2417. begin
  2418. { %reg = y - remove hp2 completely (doing it here instead of relying on
  2419. the "mov %reg,%reg" optimisation might cut down on a pass iteration) }
  2420. if TempRegUsed then
  2421. begin
  2422. DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + RegName1 + '; removed unnecessary instruction (MovMov2MovNop 6b}',hp2);
  2423. AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
  2424. RemoveInstruction(hp2);
  2425. end
  2426. else
  2427. begin
  2428. RemoveInstruction(hp2);
  2429. { We can remove the original MOV too }
  2430. DebugMsg(SPeepholeOptimization + 'MovMov2NopNop 6b done',p);
  2431. RemoveCurrentP(p, hp1);
  2432. Result:=true;
  2433. Exit;
  2434. end;
  2435. end
  2436. else
  2437. begin
  2438. AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
  2439. taicpu(hp2).loadReg(0, CurrentReg);
  2440. if TempRegUsed then
  2441. begin
  2442. { Don't remove the first instruction if the temporary register is in use }
  2443. DebugMsg(SPeepholeOptimization + RegName1 + ' = ' + debug_regname(CurrentReg) + '; changed to minimise pipeline stall (MovMov2Mov 6a}',hp2);
  2444. { No need to set Result to True. If there's another instruction later on
  2445. that can be optimised, it will be detected when the main Pass 1 loop
  2446. reaches what is now hp2 and passes it through OptPass1MOV. [Kit] };
  2447. end
  2448. else
  2449. begin
  2450. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 6 done',p);
  2451. RemoveCurrentP(p, hp1);
  2452. Result:=true;
  2453. Exit;
  2454. end;
  2455. end;
  2456. end;
  2457. top_const:
  2458. if not (cs_opt_size in current_settings.optimizerswitches) or (taicpu(hp2).opsize = S_B) then
  2459. begin
  2460. { change
  2461. mov const, %treg
  2462. mov %treg, y
  2463. to
  2464. mov const, y
  2465. }
  2466. if (taicpu(hp2).oper[1]^.typ=top_reg) or
  2467. ((taicpu(p).oper[0]^.val>=low(longint)) and (taicpu(p).oper[0]^.val<=high(longint))) then
  2468. begin
  2469. RegName1 := debug_regname(taicpu(hp2).oper[0]^.reg);
  2470. taicpu(hp2).loadOper(0,taicpu(p).oper[0]^);
  2471. if TempRegUsed then
  2472. begin
  2473. { Don't remove the first instruction if the temporary register is in use }
  2474. DebugMsg(SPeepholeOptimization + RegName1 + ' = ' + debug_tostr(taicpu(p).oper[0]^.val) + '; changed to minimise pipeline stall (MovMov2Mov 7a)',hp2);
  2475. { No need to set Result to True. If there's another instruction later on
  2476. that can be optimised, it will be detected when the main Pass 1 loop
  2477. reaches what is now hp2 and passes it through OptPass1MOV. [Kit] };
  2478. end
  2479. else
  2480. begin
  2481. DebugMsg(SPeepholeOptimization + 'MovMov2Mov 7 done',p);
  2482. RemoveCurrentP(p, hp1);
  2483. Result:=true;
  2484. Exit;
  2485. end;
  2486. end;
  2487. end;
  2488. else
  2489. Internalerror(2019103001);
  2490. end;
  2491. end;
  2492. A_MOVZX, A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif x86_64}:
  2493. if MatchOpType(taicpu(hp2), top_reg, top_reg) and
  2494. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  2495. SuperRegistersEqual(taicpu(hp2).oper[1]^.reg, taicpu(p).oper[1]^.reg) then
  2496. begin
  2497. {
  2498. Change from:
  2499. mov ###, %reg
  2500. ...
  2501. movs/z %reg,%reg (Same register, just different sizes)
  2502. To:
  2503. movs/z ###, %reg (Longer version)
  2504. ...
  2505. (remove)
  2506. }
  2507. DebugMsg(SPeepholeOptimization + 'MovMovs/z2Mov/s/z done', p);
  2508. taicpu(p).oper[1]^.reg := taicpu(hp2).oper[1]^.reg;
  2509. { Keep the first instruction as mov if ### is a constant }
  2510. if taicpu(p).oper[0]^.typ = top_const then
  2511. taicpu(p).opsize := reg2opsize(taicpu(hp2).oper[1]^.reg)
  2512. else
  2513. begin
  2514. taicpu(p).opcode := taicpu(hp2).opcode;
  2515. taicpu(p).opsize := taicpu(hp2).opsize;
  2516. end;
  2517. DebugMsg(SPeepholeOptimization + 'Removed movs/z instruction and extended earlier write (MovMovs/z2Mov/s/z)', hp2);
  2518. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp2, UsedRegs);
  2519. RemoveInstruction(hp2);
  2520. Result := True;
  2521. Exit;
  2522. end;
  2523. else
  2524. ;
  2525. end;
  2526. end;
  2527. if (aoc_MovAnd2Mov_3 in OptsToCheck) and
  2528. (taicpu(p).oper[1]^.typ = top_reg) and
  2529. (taicpu(p).opsize = S_L) and
  2530. GetNextInstructionUsingRegTrackingUse(p,hp2,taicpu(p).oper[1]^.reg) and
  2531. (taicpu(hp2).opcode = A_AND) and
  2532. (MatchOpType(taicpu(hp2),top_const,top_reg) or
  2533. (MatchOpType(taicpu(hp2),top_reg,top_reg) and
  2534. MatchOperand(taicpu(hp2).oper[0]^,taicpu(hp2).oper[1]^))
  2535. ) then
  2536. begin
  2537. if SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp2).oper[1]^.reg) then
  2538. begin
  2539. if ((taicpu(hp2).oper[0]^.typ=top_const) and (taicpu(hp2).oper[0]^.val = $ffffffff)) or
  2540. ((taicpu(hp2).oper[0]^.typ=top_reg) and (taicpu(hp2).opsize=S_L)) then
  2541. begin
  2542. { Optimize out:
  2543. mov x, %reg
  2544. and ffffffffh, %reg
  2545. }
  2546. DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 3 done',p);
  2547. RemoveInstruction(hp2);
  2548. Result:=true;
  2549. exit;
  2550. end;
  2551. end;
  2552. end;
  2553. { leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  2554. x >= RetOffset) as it doesn't do anything (it writes either to a
  2555. parameter or to the temporary storage room for the function
  2556. result)
  2557. }
  2558. if IsExitCode(hp1) and
  2559. (taicpu(p).oper[1]^.typ = top_ref) and
  2560. (taicpu(p).oper[1]^.ref^.index = NR_NO) and
  2561. (
  2562. (
  2563. (taicpu(p).oper[1]^.ref^.base = current_procinfo.FramePointer) and
  2564. not (
  2565. assigned(current_procinfo.procdef.funcretsym) and
  2566. (taicpu(p).oper[1]^.ref^.offset <= tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)
  2567. )
  2568. ) or
  2569. { Also discard writes to the stack that are below the base pointer,
  2570. as this is temporary storage rather than a function result on the
  2571. stack, say. }
  2572. (
  2573. (taicpu(p).oper[1]^.ref^.base = NR_STACK_POINTER_REG) and
  2574. (taicpu(p).oper[1]^.ref^.offset < current_procinfo.final_localsize)
  2575. )
  2576. ) then
  2577. begin
  2578. RemoveCurrentp(p, hp1);
  2579. DebugMsg(SPeepholeOptimization + 'removed deadstore before leave/ret',p);
  2580. RemoveLastDeallocForFuncRes(p);
  2581. Result:=true;
  2582. exit;
  2583. end;
  2584. if MatchOpType(taicpu(p),top_reg,top_ref) and
  2585. MatchInstruction(hp1,A_CMP,A_TEST,[taicpu(p).opsize]) and
  2586. (taicpu(hp1).oper[1]^.typ = top_ref) and
  2587. RefsEqual(taicpu(p).oper[1]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  2588. begin
  2589. { change
  2590. mov reg1, mem1
  2591. test/cmp x, mem1
  2592. to
  2593. mov reg1, mem1
  2594. test/cmp x, reg1
  2595. }
  2596. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  2597. DebugMsg(SPeepholeOptimization + 'MovTestCmp2MovTestCmp 1',hp1);
  2598. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2599. exit;
  2600. end;
  2601. if MatchInstruction(hp1,A_LEA,[S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  2602. { If the flags register is in use, don't change the instruction to an
  2603. ADD otherwise this will scramble the flags. [Kit] }
  2604. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) then
  2605. begin
  2606. if MatchOpType(Taicpu(p),top_ref,top_reg) and
  2607. ((MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(hp1).oper[1]^.reg,Taicpu(p).oper[1]^.reg) and
  2608. (Taicpu(hp1).oper[0]^.ref^.base<>Taicpu(p).oper[1]^.reg)
  2609. ) or
  2610. (MatchReference(Taicpu(hp1).oper[0]^.ref^,Taicpu(p).oper[1]^.reg,Taicpu(hp1).oper[1]^.reg) and
  2611. (Taicpu(hp1).oper[0]^.ref^.index<>Taicpu(p).oper[1]^.reg)
  2612. )
  2613. ) then
  2614. { mov reg1,ref
  2615. lea reg2,[reg1,reg2]
  2616. to
  2617. add reg2,ref}
  2618. begin
  2619. TransferUsedRegs(TmpUsedRegs);
  2620. { reg1 may not be used afterwards }
  2621. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  2622. begin
  2623. Taicpu(hp1).opcode:=A_ADD;
  2624. Taicpu(hp1).oper[0]^.ref^:=Taicpu(p).oper[0]^.ref^;
  2625. DebugMsg(SPeepholeOptimization + 'MovLea2Add done',hp1);
  2626. RemoveCurrentp(p, hp1);
  2627. result:=true;
  2628. exit;
  2629. end;
  2630. end;
  2631. { If the LEA instruction can be converted into an arithmetic instruction,
  2632. it may be possible to then fold it in the next optimisation, otherwise
  2633. there's nothing more that can be optimised here. }
  2634. if not ConvertLEA(taicpu(hp1)) then
  2635. Exit;
  2636. end;
  2637. if (taicpu(p).oper[1]^.typ = top_reg) and
  2638. (hp1.typ = ait_instruction) and
  2639. GetNextInstruction(hp1, hp2) and
  2640. MatchInstruction(hp2,A_MOV,[]) and
  2641. (SuperRegistersEqual(taicpu(hp2).oper[0]^.reg,taicpu(p).oper[1]^.reg)) and
  2642. (
  2643. IsFoldableArithOp(taicpu(hp1), taicpu(p).oper[1]^.reg)
  2644. {$ifdef x86_64}
  2645. or
  2646. (
  2647. (taicpu(p).opsize=S_L) and (taicpu(hp1).opsize=S_Q) and (taicpu(hp2).opsize=S_L) and
  2648. IsFoldableArithOp(taicpu(hp1), newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[1]^.reg),R_SUBQ))
  2649. )
  2650. {$endif x86_64}
  2651. ) then
  2652. begin
  2653. if OpsEqual(taicpu(hp2).oper[1]^, taicpu(p).oper[0]^) and
  2654. (taicpu(hp2).oper[0]^.typ=top_reg) then
  2655. { change movsX/movzX reg/ref, reg2
  2656. add/sub/or/... reg3/$const, reg2
  2657. mov reg2 reg/ref
  2658. dealloc reg2
  2659. to
  2660. add/sub/or/... reg3/$const, reg/ref }
  2661. begin
  2662. TransferUsedRegs(TmpUsedRegs);
  2663. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2664. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2665. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  2666. begin
  2667. { by example:
  2668. movswl %si,%eax movswl %si,%eax p
  2669. decl %eax addl %edx,%eax hp1
  2670. movw %ax,%si movw %ax,%si hp2
  2671. ->
  2672. movswl %si,%eax movswl %si,%eax p
  2673. decw %eax addw %edx,%eax hp1
  2674. movw %ax,%si movw %ax,%si hp2
  2675. }
  2676. DebugMsg(SPeepholeOptimization + 'MovOpMov2Op ('+
  2677. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  2678. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  2679. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  2680. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  2681. {
  2682. ->
  2683. movswl %si,%eax movswl %si,%eax p
  2684. decw %si addw %dx,%si hp1
  2685. movw %ax,%si movw %ax,%si hp2
  2686. }
  2687. case taicpu(hp1).ops of
  2688. 1:
  2689. begin
  2690. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  2691. if taicpu(hp1).oper[0]^.typ=top_reg then
  2692. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2693. end;
  2694. 2:
  2695. begin
  2696. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  2697. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  2698. (taicpu(hp1).opcode<>A_SHL) and
  2699. (taicpu(hp1).opcode<>A_SHR) and
  2700. (taicpu(hp1).opcode<>A_SAR) then
  2701. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2702. end;
  2703. else
  2704. internalerror(2008042701);
  2705. end;
  2706. {
  2707. ->
  2708. decw %si addw %dx,%si p
  2709. }
  2710. RemoveInstruction(hp2);
  2711. RemoveCurrentP(p, hp1);
  2712. Result:=True;
  2713. Exit;
  2714. end;
  2715. end;
  2716. if MatchOpType(taicpu(hp2),top_reg,top_reg) and
  2717. not(SuperRegistersEqual(taicpu(hp1).oper[0]^.reg,taicpu(hp2).oper[1]^.reg)) and
  2718. ((topsize2memsize[taicpu(hp1).opsize]<= topsize2memsize[taicpu(hp2).opsize]) or
  2719. { opsize matters for these opcodes, we could probably work around this, but it is not worth the effort }
  2720. ((taicpu(hp1).opcode<>A_SHL) and (taicpu(hp1).opcode<>A_SHR) and (taicpu(hp1).opcode<>A_SAR))
  2721. )
  2722. {$ifdef i386}
  2723. { byte registers of esi, edi, ebp, esp are not available on i386 }
  2724. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  2725. and ((taicpu(hp2).opsize<>S_B) or not(getsupreg(taicpu(p).oper[0]^.reg) in [RS_ESI,RS_EDI,RS_EBP,RS_ESP]))
  2726. {$endif i386}
  2727. then
  2728. { change movsX/movzX reg/ref, reg2
  2729. add/sub/or/... regX/$const, reg2
  2730. mov reg2, reg3
  2731. dealloc reg2
  2732. to
  2733. movsX/movzX reg/ref, reg3
  2734. add/sub/or/... reg3/$const, reg3
  2735. }
  2736. begin
  2737. TransferUsedRegs(TmpUsedRegs);
  2738. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2739. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  2740. If not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs)) then
  2741. begin
  2742. { by example:
  2743. movswl %si,%eax movswl %si,%eax p
  2744. decl %eax addl %edx,%eax hp1
  2745. movw %ax,%si movw %ax,%si hp2
  2746. ->
  2747. movswl %si,%eax movswl %si,%eax p
  2748. decw %eax addw %edx,%eax hp1
  2749. movw %ax,%si movw %ax,%si hp2
  2750. }
  2751. DebugMsg(SPeepholeOptimization + 'MovOpMov2MovOp ('+
  2752. debug_op2str(taicpu(p).opcode)+debug_opsize2str(taicpu(p).opsize)+' '+
  2753. debug_op2str(taicpu(hp1).opcode)+debug_opsize2str(taicpu(hp1).opsize)+' '+
  2754. debug_op2str(taicpu(hp2).opcode)+debug_opsize2str(taicpu(hp2).opsize)+')',p);
  2755. { limit size of constants as well to avoid assembler errors, but
  2756. check opsize to avoid overflow when left shifting the 1 }
  2757. if (taicpu(p).oper[0]^.typ=top_const) and (topsize2memsize[taicpu(hp2).opsize]<=63) then
  2758. taicpu(p).oper[0]^.val:=taicpu(p).oper[0]^.val and ((qword(1) shl topsize2memsize[taicpu(hp2).opsize])-1);
  2759. {$ifdef x86_64}
  2760. { Be careful of, for example:
  2761. movl %reg1,%reg2
  2762. addl %reg3,%reg2
  2763. movq %reg2,%reg4
  2764. This will cause problems if the upper 32-bits of %reg3 or %reg4 are non-zero
  2765. }
  2766. if (taicpu(hp1).opsize = S_L) and (taicpu(hp2).opsize = S_Q) then
  2767. begin
  2768. taicpu(hp2).changeopsize(S_L);
  2769. setsubreg(taicpu(hp2).oper[0]^.reg, R_SUBD);
  2770. setsubreg(taicpu(hp2).oper[1]^.reg, R_SUBD);
  2771. end;
  2772. {$endif x86_64}
  2773. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  2774. taicpu(p).changeopsize(taicpu(hp2).opsize);
  2775. if taicpu(p).oper[0]^.typ=top_reg then
  2776. setsubreg(taicpu(p).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2777. taicpu(p).loadoper(1, taicpu(hp2).oper[1]^);
  2778. AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,usedregs);
  2779. {
  2780. ->
  2781. movswl %si,%eax movswl %si,%eax p
  2782. decw %si addw %dx,%si hp1
  2783. movw %ax,%si movw %ax,%si hp2
  2784. }
  2785. case taicpu(hp1).ops of
  2786. 1:
  2787. begin
  2788. taicpu(hp1).loadoper(0, taicpu(hp2).oper[1]^);
  2789. if taicpu(hp1).oper[0]^.typ=top_reg then
  2790. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2791. end;
  2792. 2:
  2793. begin
  2794. taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
  2795. if (taicpu(hp1).oper[0]^.typ=top_reg) and
  2796. (taicpu(hp1).opcode<>A_SHL) and
  2797. (taicpu(hp1).opcode<>A_SHR) and
  2798. (taicpu(hp1).opcode<>A_SAR) then
  2799. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  2800. end;
  2801. else
  2802. internalerror(2018111801);
  2803. end;
  2804. {
  2805. ->
  2806. decw %si addw %dx,%si p
  2807. }
  2808. RemoveInstruction(hp2);
  2809. end;
  2810. end;
  2811. end;
  2812. if MatchInstruction(hp1,A_BTS,A_BTR,[Taicpu(p).opsize]) and
  2813. GetNextInstruction(hp1, hp2) and
  2814. MatchInstruction(hp2,A_OR,[Taicpu(p).opsize]) and
  2815. MatchOperand(Taicpu(p).oper[0]^,0) and
  2816. (Taicpu(p).oper[1]^.typ = top_reg) and
  2817. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp1).oper[1]^) and
  2818. MatchOperand(Taicpu(p).oper[1]^,Taicpu(hp2).oper[1]^) then
  2819. { mov reg1,0
  2820. bts reg1,operand1 --> mov reg1,operand2
  2821. or reg1,operand2 bts reg1,operand1}
  2822. begin
  2823. Taicpu(hp2).opcode:=A_MOV;
  2824. asml.remove(hp1);
  2825. insertllitem(hp2,hp2.next,hp1);
  2826. RemoveCurrentp(p, hp1);
  2827. Result:=true;
  2828. exit;
  2829. end;
  2830. {$ifdef x86_64}
  2831. { Convert:
  2832. movq x(ref),%reg64
  2833. shrq y,%reg64
  2834. To:
  2835. movq x+4(ref),%reg32
  2836. shrq y-32,%reg32 (Remove if y = 32)
  2837. }
  2838. if (taicpu(p).opsize = S_Q) and
  2839. (taicpu(p).oper[0]^.typ = top_ref) and { Second operand will be a register }
  2840. (taicpu(p).oper[0]^.ref^.offset <= $7FFFFFFB) and
  2841. MatchInstruction(hp1, A_SHR, [taicpu(p).opsize]) and
  2842. MatchOpType(taicpu(hp1), top_const, top_reg) and
  2843. (taicpu(hp1).oper[0]^.val >= 32) and
  2844. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  2845. begin
  2846. RegName1 := debug_regname(taicpu(hp1).oper[1]^.reg);
  2847. PreMessage := 'movq ' + debug_operstr(taicpu(p).oper[0]^) + ',' + RegName1 + '; ' +
  2848. 'shrq $' + debug_tostr(taicpu(hp1).oper[0]^.val) + ',' + RegName1 + ' -> movl ';
  2849. { Convert to 32-bit }
  2850. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  2851. taicpu(p).opsize := S_L;
  2852. Inc(taicpu(p).oper[0]^.ref^.offset, 4);
  2853. PreMessage := PreMessage + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg);
  2854. if (taicpu(hp1).oper[0]^.val = 32) then
  2855. begin
  2856. DebugMsg(SPeepholeOptimization + PreMessage + ' (MovShr2Mov)', p);
  2857. RemoveInstruction(hp1);
  2858. end
  2859. else
  2860. begin
  2861. { This will potentially open up more arithmetic operations since
  2862. the peephole optimizer now has a big hint that only the lower
  2863. 32 bits are currently in use (and opcodes are smaller in size) }
  2864. setsubreg(taicpu(hp1).oper[1]^.reg, R_SUBD);
  2865. taicpu(hp1).opsize := S_L;
  2866. Dec(taicpu(hp1).oper[0]^.val, 32);
  2867. DebugMsg(SPeepholeOptimization + PreMessage +
  2868. '; shrl $' + debug_tostr(taicpu(hp1).oper[0]^.val) + ',' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' (MovShr2MovShr)', p);
  2869. end;
  2870. Result := True;
  2871. Exit;
  2872. end;
  2873. {$endif x86_64}
  2874. end;
  2875. function TX86AsmOptimizer.OptPass1MOVXX(var p : tai) : boolean;
  2876. var
  2877. hp1 : tai;
  2878. begin
  2879. Result:=false;
  2880. if taicpu(p).ops <> 2 then
  2881. exit;
  2882. if GetNextInstruction(p,hp1) and
  2883. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  2884. (taicpu(hp1).ops = 2) then
  2885. begin
  2886. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  2887. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  2888. { movXX reg1, mem1 or movXX mem1, reg1
  2889. movXX mem2, reg2 movXX reg2, mem2}
  2890. begin
  2891. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  2892. { movXX reg1, mem1 or movXX mem1, reg1
  2893. movXX mem2, reg1 movXX reg2, mem1}
  2894. begin
  2895. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  2896. begin
  2897. { Removes the second statement from
  2898. movXX reg1, mem1/reg2
  2899. movXX mem1/reg2, reg1
  2900. }
  2901. if taicpu(p).oper[0]^.typ=top_reg then
  2902. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  2903. { Removes the second statement from
  2904. movXX mem1/reg1, reg2
  2905. movXX reg2, mem1/reg1
  2906. }
  2907. if (taicpu(p).oper[1]^.typ=top_reg) and
  2908. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)) then
  2909. begin
  2910. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2Nop 1 done',p);
  2911. RemoveInstruction(hp1);
  2912. RemoveCurrentp(p); { p will now be equal to the instruction that follows what was hp1 }
  2913. end
  2914. else
  2915. begin
  2916. DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
  2917. RemoveInstruction(hp1);
  2918. end;
  2919. Result:=true;
  2920. exit;
  2921. end
  2922. end;
  2923. end;
  2924. end;
  2925. end;
  2926. function TX86AsmOptimizer.OptPass1OP(var p : tai) : boolean;
  2927. var
  2928. hp1 : tai;
  2929. begin
  2930. result:=false;
  2931. { replace
  2932. <Op>X %mreg1,%mreg2 // Op in [ADD,MUL]
  2933. MovX %mreg2,%mreg1
  2934. dealloc %mreg2
  2935. by
  2936. <Op>X %mreg2,%mreg1
  2937. ?
  2938. }
  2939. if GetNextInstruction(p,hp1) and
  2940. { we mix single and double opperations here because we assume that the compiler
  2941. generates vmovapd only after double operations and vmovaps only after single operations }
  2942. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  2943. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  2944. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) and
  2945. (taicpu(p).oper[0]^.typ=top_reg) then
  2946. begin
  2947. TransferUsedRegs(TmpUsedRegs);
  2948. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2949. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2950. begin
  2951. taicpu(p).loadoper(0,taicpu(hp1).oper[0]^);
  2952. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  2953. DebugMsg(SPeepholeOptimization + 'OpMov2Op done',p);
  2954. RemoveInstruction(hp1);
  2955. result:=true;
  2956. end;
  2957. end;
  2958. end;
  2959. function TX86AsmOptimizer.OptPass1Add(var p : tai) : boolean;
  2960. var
  2961. hp1 : tai;
  2962. begin
  2963. result:=false;
  2964. { replace
  2965. addX const,%reg1
  2966. leaX (%reg1,%reg1,Y),%reg2 // Base or index might not be equal to reg1
  2967. dealloc %reg1
  2968. by
  2969. leaX const+const*Y(%reg1,%reg1,Y),%reg2
  2970. }
  2971. if MatchOpType(taicpu(p),top_const,top_reg) and
  2972. GetNextInstruction(p,hp1) and
  2973. MatchInstruction(hp1,A_LEA,[taicpu(p).opsize]) and
  2974. ((taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.base) or
  2975. (taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.index)) then
  2976. begin
  2977. TransferUsedRegs(TmpUsedRegs);
  2978. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  2979. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  2980. begin
  2981. DebugMsg(SPeepholeOptimization + 'AddLea2Lea done',p);
  2982. if taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.base then
  2983. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.val);
  2984. if taicpu(p).oper[1]^.reg=taicpu(hp1).oper[0]^.ref^.index then
  2985. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.val*max(taicpu(hp1).oper[0]^.ref^.scalefactor,1));
  2986. RemoveCurrentP(p);
  2987. result:=true;
  2988. end;
  2989. end;
  2990. end;
  2991. function TX86AsmOptimizer.OptPass1LEA(var p : tai) : boolean;
  2992. var
  2993. hp1: tai;
  2994. ref: Integer;
  2995. saveref: treference;
  2996. TempReg: TRegister;
  2997. Multiple: TCGInt;
  2998. begin
  2999. Result:=false;
  3000. { removes seg register prefixes from LEA operations, as they
  3001. don't do anything}
  3002. taicpu(p).oper[0]^.ref^.Segment:=NR_NO;
  3003. { changes "lea (%reg1), %reg2" into "mov %reg1, %reg2" }
  3004. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  3005. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  3006. { do not mess with leas acessing the stack pointer }
  3007. (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) and
  3008. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  3009. begin
  3010. if (taicpu(p).oper[0]^.ref^.offset = 0) then
  3011. begin
  3012. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) then
  3013. begin
  3014. hp1:=taicpu.op_reg_reg(A_MOV,taicpu(p).opsize,taicpu(p).oper[0]^.ref^.base,
  3015. taicpu(p).oper[1]^.reg);
  3016. InsertLLItem(p.previous,p.next, hp1);
  3017. DebugMsg(SPeepholeOptimization + 'Lea2Mov done',hp1);
  3018. p.free;
  3019. p:=hp1;
  3020. end
  3021. else
  3022. begin
  3023. DebugMsg(SPeepholeOptimization + 'Lea2Nop done',p);
  3024. RemoveCurrentP(p);
  3025. end;
  3026. Result:=true;
  3027. exit;
  3028. end
  3029. else if (
  3030. { continue to use lea to adjust the stack pointer,
  3031. it is the recommended way, but only if not optimizing for size }
  3032. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) or
  3033. (cs_opt_size in current_settings.optimizerswitches)
  3034. ) and
  3035. { If the flags register is in use, don't change the instruction
  3036. to an ADD otherwise this will scramble the flags. [Kit] }
  3037. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  3038. ConvertLEA(taicpu(p)) then
  3039. begin
  3040. Result:=true;
  3041. exit;
  3042. end;
  3043. end;
  3044. if GetNextInstruction(p,hp1) and
  3045. (hp1.typ=ait_instruction) then
  3046. begin
  3047. if MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  3048. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  3049. MatchOpType(Taicpu(hp1),top_reg,top_reg) and
  3050. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) then
  3051. begin
  3052. TransferUsedRegs(TmpUsedRegs);
  3053. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3054. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  3055. begin
  3056. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  3057. DebugMsg(SPeepholeOptimization + 'LeaMov2Lea done',p);
  3058. RemoveInstruction(hp1);
  3059. result:=true;
  3060. exit;
  3061. end;
  3062. end;
  3063. { changes
  3064. lea <ref1>, reg1
  3065. <op> ...,<ref. with reg1>,...
  3066. to
  3067. <op> ...,<ref1>,... }
  3068. if (taicpu(p).oper[1]^.reg<>current_procinfo.framepointer) and
  3069. (taicpu(p).oper[1]^.reg<>NR_STACK_POINTER_REG) and
  3070. not(MatchInstruction(hp1,A_LEA,[])) then
  3071. begin
  3072. { find a reference which uses reg1 }
  3073. 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
  3074. ref:=0
  3075. 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
  3076. ref:=1
  3077. else
  3078. ref:=-1;
  3079. if (ref<>-1) and
  3080. { reg1 must be either the base or the index }
  3081. ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) xor (taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg)) then
  3082. begin
  3083. { reg1 can be removed from the reference }
  3084. saveref:=taicpu(hp1).oper[ref]^.ref^;
  3085. if taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg then
  3086. taicpu(hp1).oper[ref]^.ref^.base:=NR_NO
  3087. else if taicpu(hp1).oper[ref]^.ref^.index=taicpu(p).oper[1]^.reg then
  3088. taicpu(hp1).oper[ref]^.ref^.index:=NR_NO
  3089. else
  3090. Internalerror(2019111201);
  3091. { check if the can insert all data of the lea into the second instruction }
  3092. if ((taicpu(hp1).oper[ref]^.ref^.base=taicpu(p).oper[1]^.reg) or (taicpu(hp1).oper[ref]^.ref^.scalefactor <= 1)) and
  3093. ((taicpu(p).oper[0]^.ref^.base=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.base=NR_NO)) and
  3094. ((taicpu(p).oper[0]^.ref^.index=NR_NO) or (taicpu(hp1).oper[ref]^.ref^.index=NR_NO)) and
  3095. ((taicpu(p).oper[0]^.ref^.symbol=nil) or (taicpu(hp1).oper[ref]^.ref^.symbol=nil)) and
  3096. ((taicpu(p).oper[0]^.ref^.relsymbol=nil) or (taicpu(hp1).oper[ref]^.ref^.relsymbol=nil)) and
  3097. ((taicpu(p).oper[0]^.ref^.scalefactor <= 1) or (taicpu(hp1).oper[ref]^.ref^.scalefactor <= 1)) and
  3098. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.segment=NR_NO)
  3099. {$ifdef x86_64}
  3100. and (abs(taicpu(hp1).oper[ref]^.ref^.offset+taicpu(p).oper[0]^.ref^.offset)<=$7fffffff)
  3101. and (((taicpu(p).oper[0]^.ref^.base<>NR_RIP) and (taicpu(p).oper[0]^.ref^.index<>NR_RIP)) or
  3102. ((taicpu(hp1).oper[ref]^.ref^.base=NR_NO) and (taicpu(hp1).oper[ref]^.ref^.index=NR_NO))
  3103. )
  3104. {$endif x86_64}
  3105. then
  3106. begin
  3107. { reg1 might not used by the second instruction after it is remove from the reference }
  3108. if not(RegInInstruction(taicpu(p).oper[1]^.reg,taicpu(hp1))) then
  3109. begin
  3110. TransferUsedRegs(TmpUsedRegs);
  3111. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3112. { reg1 is not updated so it might not be used afterwards }
  3113. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  3114. begin
  3115. DebugMsg(SPeepholeOptimization + 'LeaOp2Op done',p);
  3116. if taicpu(p).oper[0]^.ref^.base<>NR_NO then
  3117. taicpu(hp1).oper[ref]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  3118. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  3119. taicpu(hp1).oper[ref]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  3120. if taicpu(p).oper[0]^.ref^.symbol<>nil then
  3121. taicpu(hp1).oper[ref]^.ref^.symbol:=taicpu(p).oper[0]^.ref^.symbol;
  3122. if taicpu(p).oper[0]^.ref^.relsymbol<>nil then
  3123. taicpu(hp1).oper[ref]^.ref^.relsymbol:=taicpu(p).oper[0]^.ref^.relsymbol;
  3124. if taicpu(p).oper[0]^.ref^.scalefactor > 1 then
  3125. taicpu(hp1).oper[ref]^.ref^.scalefactor:=taicpu(p).oper[0]^.ref^.scalefactor;
  3126. inc(taicpu(hp1).oper[ref]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  3127. RemoveCurrentP(p, hp1);
  3128. result:=true;
  3129. exit;
  3130. end
  3131. end;
  3132. end;
  3133. { recover }
  3134. taicpu(hp1).oper[ref]^.ref^:=saveref;
  3135. end;
  3136. end;
  3137. end;
  3138. { for now, we do not mess with the stack pointer, thought it might be usefull to remove
  3139. unneeded lea sequences on the stack pointer, it needs to be tested in detail }
  3140. if (taicpu(p).oper[1]^.reg <> NR_STACK_POINTER_REG) and
  3141. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[1]^.reg) then
  3142. begin
  3143. { Check common LEA/LEA conditions }
  3144. if MatchInstruction(hp1,A_LEA,[taicpu(p).opsize]) and
  3145. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
  3146. (taicpu(p).oper[0]^.ref^.relsymbol = nil) and
  3147. (taicpu(p).oper[0]^.ref^.segment = NR_NO) and
  3148. (taicpu(p).oper[0]^.ref^.symbol = nil) and
  3149. (taicpu(hp1).oper[0]^.ref^.relsymbol = nil) and
  3150. (taicpu(hp1).oper[0]^.ref^.segment = NR_NO) and
  3151. (taicpu(hp1).oper[0]^.ref^.symbol = nil) and
  3152. (
  3153. (taicpu(p).oper[0]^.ref^.base = NR_NO) or { Don't call RegModifiedBetween unnecessarily }
  3154. not(RegModifiedBetween(taicpu(p).oper[0]^.ref^.base,p,hp1))
  3155. ) and (
  3156. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) or { Don't call RegModifiedBetween unnecessarily }
  3157. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  3158. not(RegModifiedBetween(taicpu(p).oper[0]^.ref^.index,p,hp1))
  3159. ) then
  3160. begin
  3161. { changes
  3162. lea (regX,scale), reg1
  3163. lea offset(reg1,reg1), reg1
  3164. to
  3165. lea offset(regX,scale*2), reg1
  3166. and
  3167. lea (regX,scale1), reg1
  3168. lea offset(reg1,scale2), reg1
  3169. to
  3170. lea offset(regX,scale1*scale2), reg1
  3171. ... so long as the final scale does not exceed 8
  3172. (Similarly, allow the first instruction to be "lea (regX,regX),reg1")
  3173. }
  3174. if (taicpu(p).oper[0]^.ref^.offset = 0) and
  3175. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  3176. (
  3177. (
  3178. (taicpu(p).oper[0]^.ref^.base = NR_NO)
  3179. ) or (
  3180. (taicpu(p).oper[0]^.ref^.scalefactor <= 1) and
  3181. (
  3182. (taicpu(p).oper[0]^.ref^.base = taicpu(p).oper[0]^.ref^.index) and
  3183. not(RegUsedBetween(taicpu(p).oper[0]^.ref^.index, p, hp1))
  3184. )
  3185. )
  3186. ) and (
  3187. (
  3188. { lea (reg1,scale2), reg1 variant }
  3189. (taicpu(hp1).oper[0]^.ref^.base = NR_NO) and
  3190. (
  3191. (
  3192. (taicpu(p).oper[0]^.ref^.base = NR_NO) and
  3193. (taicpu(hp1).oper[0]^.ref^.scalefactor * taicpu(p).oper[0]^.ref^.scalefactor <= 8)
  3194. ) or (
  3195. { lea (regX,regX), reg1 variant }
  3196. (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  3197. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 4)
  3198. )
  3199. )
  3200. ) or (
  3201. { lea (reg1,reg1), reg1 variant }
  3202. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  3203. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1)
  3204. )
  3205. ) then
  3206. begin
  3207. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea 2 done',p);
  3208. { Make everything homogeneous to make calculations easier }
  3209. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) then
  3210. begin
  3211. if taicpu(p).oper[0]^.ref^.index <> NR_NO then
  3212. { Convert lea (regX,regX),reg1 to lea (regX,2),reg1 }
  3213. taicpu(p).oper[0]^.ref^.scalefactor := 2
  3214. else
  3215. taicpu(p).oper[0]^.ref^.index := taicpu(p).oper[0]^.ref^.base;
  3216. taicpu(p).oper[0]^.ref^.base := NR_NO;
  3217. end;
  3218. if (taicpu(hp1).oper[0]^.ref^.base = NR_NO) then
  3219. begin
  3220. { Just to prevent miscalculations }
  3221. if (taicpu(hp1).oper[0]^.ref^.scalefactor = 0) then
  3222. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(p).oper[0]^.ref^.scalefactor
  3223. else
  3224. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(hp1).oper[0]^.ref^.scalefactor * taicpu(p).oper[0]^.ref^.scalefactor;
  3225. end
  3226. else
  3227. begin
  3228. taicpu(hp1).oper[0]^.ref^.base := NR_NO;
  3229. taicpu(hp1).oper[0]^.ref^.scalefactor := taicpu(p).oper[0]^.ref^.scalefactor * 2;
  3230. end;
  3231. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.ref^.index;
  3232. RemoveCurrentP(p);
  3233. result:=true;
  3234. exit;
  3235. end
  3236. { changes
  3237. lea offset1(regX), reg1
  3238. lea offset2(reg1), reg1
  3239. to
  3240. lea offset1+offset2(regX), reg1 }
  3241. else if
  3242. (
  3243. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  3244. (taicpu(p).oper[0]^.ref^.index = NR_NO)
  3245. ) or (
  3246. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  3247. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1) and
  3248. (
  3249. (
  3250. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  3251. (taicpu(p).oper[0]^.ref^.base = NR_NO)
  3252. ) or (
  3253. (taicpu(p).oper[0]^.ref^.scalefactor <= 1) and
  3254. (
  3255. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  3256. (
  3257. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) and
  3258. (
  3259. (taicpu(hp1).oper[0]^.ref^.index = NR_NO) or
  3260. (taicpu(hp1).oper[0]^.ref^.base = NR_NO)
  3261. )
  3262. )
  3263. )
  3264. )
  3265. )
  3266. ) then
  3267. begin
  3268. DebugMsg(SPeepholeOptimization + 'LeaLea2Lea 1 done',p);
  3269. if taicpu(hp1).oper[0]^.ref^.index=taicpu(p).oper[1]^.reg then
  3270. begin
  3271. taicpu(hp1).oper[0]^.ref^.index:=taicpu(p).oper[0]^.ref^.base;
  3272. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset*max(taicpu(hp1).oper[0]^.ref^.scalefactor,1));
  3273. { if the register is used as index and base, we have to increase for base as well
  3274. and adapt base }
  3275. if taicpu(hp1).oper[0]^.ref^.base=taicpu(p).oper[1]^.reg then
  3276. begin
  3277. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  3278. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  3279. end;
  3280. end
  3281. else
  3282. begin
  3283. inc(taicpu(hp1).oper[0]^.ref^.offset,taicpu(p).oper[0]^.ref^.offset);
  3284. taicpu(hp1).oper[0]^.ref^.base:=taicpu(p).oper[0]^.ref^.base;
  3285. end;
  3286. if taicpu(p).oper[0]^.ref^.index<>NR_NO then
  3287. begin
  3288. taicpu(hp1).oper[0]^.ref^.base:=taicpu(hp1).oper[0]^.ref^.index;
  3289. taicpu(hp1).oper[0]^.ref^.index:=taicpu(p).oper[0]^.ref^.index;
  3290. taicpu(hp1).oper[0]^.ref^.scalefactor:=taicpu(p).oper[0]^.ref^.scalefactor;
  3291. end;
  3292. RemoveCurrentP(p);
  3293. result:=true;
  3294. exit;
  3295. end;
  3296. end;
  3297. { Change:
  3298. leal/q $x(%reg1),%reg2
  3299. ...
  3300. shll/q $y,%reg2
  3301. To:
  3302. leal/q $(x+2^y)(%reg1,2^y),%reg2 (if y <= 3)
  3303. }
  3304. if MatchInstruction(hp1, A_SHL, [taicpu(p).opsize]) and
  3305. MatchOpType(taicpu(hp1), top_const, top_reg) and
  3306. (taicpu(hp1).oper[0]^.val <= 3) then
  3307. begin
  3308. Multiple := 1 shl taicpu(hp1).oper[0]^.val;
  3309. TransferUsedRegs(TmpUsedRegs);
  3310. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  3311. TempReg := taicpu(hp1).oper[1]^.reg; { Store locally to reduce the number of dereferences }
  3312. if
  3313. { This allows the optimisation in some circumstances even if the lea instruction already has a scale factor
  3314. (this works even if scalefactor is zero) }
  3315. ((Multiple * taicpu(p).oper[0]^.ref^.scalefactor) <= 8) and
  3316. { Ensure offset doesn't go out of bounds }
  3317. (abs(taicpu(p).oper[0]^.ref^.offset * Multiple) <= $7FFFFFFF) and
  3318. not (RegInUsedRegs(NR_DEFAULTFLAGS,TmpUsedRegs)) and
  3319. MatchOperand(taicpu(p).oper[1]^, TempReg) and
  3320. (
  3321. (
  3322. not SuperRegistersEqual(taicpu(p).oper[0]^.ref^.base, TempReg) and
  3323. (
  3324. (taicpu(p).oper[0]^.ref^.index = NR_NO) or
  3325. (taicpu(p).oper[0]^.ref^.index = NR_INVALID) or
  3326. (
  3327. { Check for lea $x(%reg1,%reg1),%reg2 and treat as it it were lea $x(%reg1,2),%reg2 }
  3328. (taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[0]^.ref^.base) and
  3329. (taicpu(p).oper[0]^.ref^.scalefactor <= 1)
  3330. )
  3331. )
  3332. ) or (
  3333. (
  3334. (taicpu(p).oper[0]^.ref^.base = NR_NO) or
  3335. (taicpu(p).oper[0]^.ref^.base = NR_INVALID)
  3336. ) and
  3337. not SuperRegistersEqual(taicpu(p).oper[0]^.ref^.index, TempReg)
  3338. )
  3339. ) then
  3340. begin
  3341. repeat
  3342. with taicpu(p).oper[0]^.ref^ do
  3343. begin
  3344. { Convert lea $x(%reg1,%reg1),%reg2 to lea $x(%reg1,2),%reg2 }
  3345. if index = base then
  3346. begin
  3347. if Multiple > 4 then
  3348. { Optimisation will no longer work because resultant
  3349. scale factor will exceed 8 }
  3350. Break;
  3351. base := NR_NO;
  3352. scalefactor := 2;
  3353. DebugMsg(SPeepholeOptimization + 'lea $x(%reg1,%reg1),%reg2 -> lea $x(%reg1,2),%reg2 for following optimisation', p);
  3354. end
  3355. else if (base <> NR_NO) and (base <> NR_INVALID) then
  3356. begin
  3357. { Scale factor only works on the index register }
  3358. index := base;
  3359. base := NR_NO;
  3360. end;
  3361. { For safety }
  3362. if scalefactor <= 1 then
  3363. begin
  3364. DebugMsg(SPeepholeOptimization + 'LeaShl2Lea 1', p);
  3365. scalefactor := Multiple;
  3366. end
  3367. else
  3368. begin
  3369. DebugMsg(SPeepholeOptimization + 'LeaShl2Lea 2', p);
  3370. scalefactor := scalefactor * Multiple;
  3371. end;
  3372. offset := offset * Multiple;
  3373. end;
  3374. RemoveInstruction(hp1);
  3375. Result := True;
  3376. Exit;
  3377. { This repeat..until loop exists for the benefit of Break }
  3378. until True;
  3379. end;
  3380. end;
  3381. end;
  3382. end;
  3383. function TX86AsmOptimizer.DoSubAddOpt(var p: tai): Boolean;
  3384. var
  3385. hp1 : tai;
  3386. begin
  3387. DoSubAddOpt := False;
  3388. if GetLastInstruction(p, hp1) and
  3389. (hp1.typ = ait_instruction) and
  3390. (taicpu(hp1).opsize = taicpu(p).opsize) then
  3391. case taicpu(hp1).opcode Of
  3392. A_DEC:
  3393. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  3394. MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  3395. begin
  3396. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
  3397. RemoveInstruction(hp1);
  3398. end;
  3399. A_SUB:
  3400. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  3401. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  3402. begin
  3403. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
  3404. RemoveInstruction(hp1);
  3405. end;
  3406. A_ADD:
  3407. begin
  3408. if MatchOpType(taicpu(hp1),top_const,top_reg) and
  3409. MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
  3410. begin
  3411. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  3412. RemoveInstruction(hp1);
  3413. if (taicpu(p).oper[0]^.val = 0) then
  3414. begin
  3415. hp1 := tai(p.next);
  3416. RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
  3417. if not GetLastInstruction(hp1, p) then
  3418. p := hp1;
  3419. DoSubAddOpt := True;
  3420. end
  3421. end;
  3422. end;
  3423. else
  3424. ;
  3425. end;
  3426. end;
  3427. function TX86AsmOptimizer.OptPass1Sub(var p : tai) : boolean;
  3428. {$ifdef i386}
  3429. var
  3430. hp1 : tai;
  3431. {$endif i386}
  3432. begin
  3433. Result:=false;
  3434. { * change "subl $2, %esp; pushw x" to "pushl x"}
  3435. { * change "sub/add const1, reg" or "dec reg" followed by
  3436. "sub const2, reg" to one "sub ..., reg" }
  3437. if MatchOpType(taicpu(p),top_const,top_reg) then
  3438. begin
  3439. {$ifdef i386}
  3440. if (taicpu(p).oper[0]^.val = 2) and
  3441. (taicpu(p).oper[1]^.reg = NR_ESP) and
  3442. { Don't do the sub/push optimization if the sub }
  3443. { comes from setting up the stack frame (JM) }
  3444. (not(GetLastInstruction(p,hp1)) or
  3445. not(MatchInstruction(hp1,A_MOV,[S_L]) and
  3446. MatchOperand(taicpu(hp1).oper[0]^,NR_ESP) and
  3447. MatchOperand(taicpu(hp1).oper[0]^,NR_EBP))) then
  3448. begin
  3449. hp1 := tai(p.next);
  3450. while Assigned(hp1) and
  3451. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  3452. not RegReadByInstruction(NR_ESP,hp1) and
  3453. not RegModifiedByInstruction(NR_ESP,hp1) do
  3454. hp1 := tai(hp1.next);
  3455. if Assigned(hp1) and
  3456. MatchInstruction(hp1,A_PUSH,[S_W]) then
  3457. begin
  3458. taicpu(hp1).changeopsize(S_L);
  3459. if taicpu(hp1).oper[0]^.typ=top_reg then
  3460. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  3461. hp1 := tai(p.next);
  3462. RemoveCurrentp(p, hp1);
  3463. Result:=true;
  3464. exit;
  3465. end;
  3466. end;
  3467. {$endif i386}
  3468. if DoSubAddOpt(p) then
  3469. Result:=true;
  3470. end;
  3471. end;
  3472. function TX86AsmOptimizer.OptPass1SHLSAL(var p : tai) : boolean;
  3473. var
  3474. TmpBool1,TmpBool2 : Boolean;
  3475. tmpref : treference;
  3476. hp1,hp2: tai;
  3477. mask: tcgint;
  3478. begin
  3479. Result:=false;
  3480. { All these optimisations work on "shl/sal const,%reg" }
  3481. if not MatchOpType(taicpu(p),top_const,top_reg) then
  3482. Exit;
  3483. if (taicpu(p).opsize in [S_L{$ifdef x86_64},S_Q{$endif x86_64}]) and
  3484. (taicpu(p).oper[0]^.val <= 3) then
  3485. { Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement }
  3486. begin
  3487. { should we check the next instruction? }
  3488. TmpBool1 := True;
  3489. { have we found an add/sub which could be
  3490. integrated in the lea? }
  3491. TmpBool2 := False;
  3492. reference_reset(tmpref,2,[]);
  3493. TmpRef.index := taicpu(p).oper[1]^.reg;
  3494. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  3495. while TmpBool1 and
  3496. GetNextInstruction(p, hp1) and
  3497. (tai(hp1).typ = ait_instruction) and
  3498. ((((taicpu(hp1).opcode = A_ADD) or
  3499. (taicpu(hp1).opcode = A_SUB)) and
  3500. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  3501. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  3502. (((taicpu(hp1).opcode = A_INC) or
  3503. (taicpu(hp1).opcode = A_DEC)) and
  3504. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  3505. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg)) or
  3506. ((taicpu(hp1).opcode = A_LEA) and
  3507. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  3508. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg))) and
  3509. (not GetNextInstruction(hp1,hp2) or
  3510. not instrReadsFlags(hp2)) Do
  3511. begin
  3512. TmpBool1 := False;
  3513. if taicpu(hp1).opcode=A_LEA then
  3514. begin
  3515. if (TmpRef.base = NR_NO) and
  3516. (taicpu(hp1).oper[0]^.ref^.symbol=nil) and
  3517. (taicpu(hp1).oper[0]^.ref^.relsymbol=nil) and
  3518. (taicpu(hp1).oper[0]^.ref^.segment=NR_NO) and
  3519. ((taicpu(hp1).oper[0]^.ref^.scalefactor=0) or
  3520. (taicpu(hp1).oper[0]^.ref^.scalefactor*tmpref.scalefactor<=8)) then
  3521. begin
  3522. TmpBool1 := True;
  3523. TmpBool2 := True;
  3524. inc(TmpRef.offset, taicpu(hp1).oper[0]^.ref^.offset);
  3525. if taicpu(hp1).oper[0]^.ref^.scalefactor<>0 then
  3526. tmpref.scalefactor:=tmpref.scalefactor*taicpu(hp1).oper[0]^.ref^.scalefactor;
  3527. TmpRef.base := taicpu(hp1).oper[0]^.ref^.base;
  3528. RemoveInstruction(hp1);
  3529. end
  3530. end
  3531. else if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  3532. begin
  3533. TmpBool1 := True;
  3534. TmpBool2 := True;
  3535. case taicpu(hp1).opcode of
  3536. A_ADD:
  3537. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  3538. A_SUB:
  3539. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  3540. else
  3541. internalerror(2019050536);
  3542. end;
  3543. RemoveInstruction(hp1);
  3544. end
  3545. else
  3546. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  3547. (((taicpu(hp1).opcode = A_ADD) and
  3548. (TmpRef.base = NR_NO)) or
  3549. (taicpu(hp1).opcode = A_INC) or
  3550. (taicpu(hp1).opcode = A_DEC)) then
  3551. begin
  3552. TmpBool1 := True;
  3553. TmpBool2 := True;
  3554. case taicpu(hp1).opcode of
  3555. A_ADD:
  3556. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  3557. A_INC:
  3558. inc(TmpRef.offset);
  3559. A_DEC:
  3560. dec(TmpRef.offset);
  3561. else
  3562. internalerror(2019050535);
  3563. end;
  3564. RemoveInstruction(hp1);
  3565. end;
  3566. end;
  3567. if TmpBool2
  3568. {$ifndef x86_64}
  3569. or
  3570. ((current_settings.optimizecputype < cpu_Pentium2) and
  3571. (taicpu(p).oper[0]^.val <= 3) and
  3572. not(cs_opt_size in current_settings.optimizerswitches))
  3573. {$endif x86_64}
  3574. then
  3575. begin
  3576. if not(TmpBool2) and
  3577. (taicpu(p).oper[0]^.val=1) then
  3578. begin
  3579. hp1:=taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  3580. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  3581. end
  3582. else
  3583. hp1:=taicpu.op_ref_reg(A_LEA, taicpu(p).opsize, TmpRef,
  3584. taicpu(p).oper[1]^.reg);
  3585. DebugMsg(SPeepholeOptimization + 'ShlAddLeaSubIncDec2Lea',p);
  3586. InsertLLItem(p.previous, p.next, hp1);
  3587. p.free;
  3588. p := hp1;
  3589. end;
  3590. end
  3591. {$ifndef x86_64}
  3592. else if (current_settings.optimizecputype < cpu_Pentium2) then
  3593. begin
  3594. { changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  3595. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  3596. (unlike shl, which is only Tairable in the U pipe) }
  3597. if taicpu(p).oper[0]^.val=1 then
  3598. begin
  3599. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  3600. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  3601. InsertLLItem(p.previous, p.next, hp1);
  3602. p.free;
  3603. p := hp1;
  3604. end
  3605. { changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  3606. "shl $3, %reg" to "lea (,%reg,8), %reg }
  3607. else if (taicpu(p).opsize = S_L) and
  3608. (taicpu(p).oper[0]^.val<= 3) then
  3609. begin
  3610. reference_reset(tmpref,2,[]);
  3611. TmpRef.index := taicpu(p).oper[1]^.reg;
  3612. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  3613. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  3614. InsertLLItem(p.previous, p.next, hp1);
  3615. p.free;
  3616. p := hp1;
  3617. end;
  3618. end
  3619. {$endif x86_64}
  3620. else if
  3621. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  3622. (
  3623. (
  3624. MatchInstruction(hp1, A_AND, [taicpu(p).opsize]) and
  3625. SetAndTest(hp1, hp2)
  3626. {$ifdef x86_64}
  3627. ) or
  3628. (
  3629. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  3630. GetNextInstruction(hp1, hp2) and
  3631. MatchInstruction(hp2, A_AND, [taicpu(p).opsize]) and
  3632. MatchOpType(taicpu(hp2), top_reg, top_reg) and
  3633. (taicpu(hp1).oper[1]^.reg = taicpu(hp2).oper[0]^.reg)
  3634. {$endif x86_64}
  3635. )
  3636. ) and
  3637. (taicpu(p).oper[1]^.reg = taicpu(hp2).oper[1]^.reg) then
  3638. begin
  3639. { Change:
  3640. shl x, %reg1
  3641. mov -(1<<x), %reg2
  3642. and %reg2, %reg1
  3643. Or:
  3644. shl x, %reg1
  3645. and -(1<<x), %reg1
  3646. To just:
  3647. shl x, %reg1
  3648. Since the and operation only zeroes bits that are already zero from the shl operation
  3649. }
  3650. case taicpu(p).oper[0]^.val of
  3651. 8:
  3652. mask:=$FFFFFFFFFFFFFF00;
  3653. 16:
  3654. mask:=$FFFFFFFFFFFF0000;
  3655. 32:
  3656. mask:=$FFFFFFFF00000000;
  3657. 63:
  3658. { Constant pre-calculated to prevent overflow errors with Int64 }
  3659. mask:=$8000000000000000;
  3660. else
  3661. begin
  3662. if taicpu(p).oper[0]^.val >= 64 then
  3663. { Shouldn't happen realistically, since the register
  3664. is guaranteed to be set to zero at this point }
  3665. mask := 0
  3666. else
  3667. mask := -(Int64(1 shl taicpu(p).oper[0]^.val));
  3668. end;
  3669. end;
  3670. if taicpu(hp1).oper[0]^.val = mask then
  3671. begin
  3672. { Everything checks out, perform the optimisation, as long as
  3673. the FLAGS register isn't being used}
  3674. TransferUsedRegs(TmpUsedRegs);
  3675. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  3676. {$ifdef x86_64}
  3677. if (hp1 <> hp2) then
  3678. begin
  3679. { "shl/mov/and" version }
  3680. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  3681. { Don't do the optimisation if the FLAGS register is in use }
  3682. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)) then
  3683. begin
  3684. DebugMsg(SPeepholeOptimization + 'ShlMovAnd2Shl', p);
  3685. { Don't remove the 'mov' instruction if its register is used elsewhere }
  3686. if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp2, TmpUsedRegs)) then
  3687. begin
  3688. RemoveInstruction(hp1);
  3689. Result := True;
  3690. end;
  3691. { Only set Result to True if the 'mov' instruction was removed }
  3692. RemoveInstruction(hp2);
  3693. end;
  3694. end
  3695. else
  3696. {$endif x86_64}
  3697. begin
  3698. { "shl/and" version }
  3699. { Don't do the optimisation if the FLAGS register is in use }
  3700. if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
  3701. begin
  3702. DebugMsg(SPeepholeOptimization + 'ShlAnd2Shl', p);
  3703. RemoveInstruction(hp1);
  3704. Result := True;
  3705. end;
  3706. end;
  3707. Exit;
  3708. end
  3709. else {$ifdef x86_64}if (hp1 = hp2) then{$endif x86_64}
  3710. begin
  3711. { Even if the mask doesn't allow for its removal, we might be
  3712. able to optimise the mask for the "shl/and" version, which
  3713. may permit other peephole optimisations }
  3714. {$ifdef DEBUG_AOPTCPU}
  3715. mask := taicpu(hp1).oper[0]^.val and mask;
  3716. if taicpu(hp1).oper[0]^.val <> mask then
  3717. begin
  3718. DebugMsg(
  3719. SPeepholeOptimization +
  3720. 'Changed mask from $' + debug_tostr(taicpu(hp1).oper[0]^.val) +
  3721. ' to $' + debug_tostr(mask) +
  3722. 'based on previous instruction (ShlAnd2ShlAnd)', hp1);
  3723. taicpu(hp1).oper[0]^.val := mask;
  3724. end;
  3725. {$else DEBUG_AOPTCPU}
  3726. { If debugging is off, just set the operand even if it's the same }
  3727. taicpu(hp1).oper[0]^.val := taicpu(hp1).oper[0]^.val and mask;
  3728. {$endif DEBUG_AOPTCPU}
  3729. end;
  3730. end;
  3731. end;
  3732. function TX86AsmOptimizer.OptPass1SETcc(var p: tai): boolean;
  3733. var
  3734. hp1,hp2,next: tai; SetC, JumpC: TAsmCond; Unconditional: Boolean;
  3735. begin
  3736. Result:=false;
  3737. if MatchOpType(taicpu(p),top_reg) and GetNextInstruction(p, hp1) then
  3738. begin
  3739. if ((MatchInstruction(hp1, A_TEST, [S_B]) and
  3740. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3741. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg)) or
  3742. (MatchInstruction(hp1, A_CMP, [S_B]) and
  3743. MatchOpType(taicpu(hp1),top_const,top_reg) and
  3744. (taicpu(hp1).oper[0]^.val=0))
  3745. ) and
  3746. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
  3747. GetNextInstruction(hp1, hp2) and
  3748. MatchInstruction(hp2, A_Jcc, []) then
  3749. { Change from: To:
  3750. set(C) %reg j(~C) label
  3751. test %reg,%reg/cmp $0,%reg
  3752. je label
  3753. set(C) %reg j(C) label
  3754. test %reg,%reg/cmp $0,%reg
  3755. jne label
  3756. }
  3757. begin
  3758. next := tai(p.Next);
  3759. TransferUsedRegs(TmpUsedRegs);
  3760. UpdateUsedRegs(TmpUsedRegs, next);
  3761. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  3762. JumpC := taicpu(hp2).condition;
  3763. Unconditional := False;
  3764. if conditions_equal(JumpC, C_E) then
  3765. SetC := inverse_cond(taicpu(p).condition)
  3766. else if conditions_equal(JumpC, C_NE) then
  3767. SetC := taicpu(p).condition
  3768. else
  3769. { We've got something weird here (and inefficent) }
  3770. begin
  3771. DebugMsg('DEBUG: Inefficient jump - check code generation', p);
  3772. SetC := C_NONE;
  3773. { JAE/JNB will always branch (use 'condition_in', since C_AE <> C_NB normally) }
  3774. if condition_in(C_AE, JumpC) then
  3775. Unconditional := True
  3776. else
  3777. { Not sure what to do with this jump - drop out }
  3778. Exit;
  3779. end;
  3780. RemoveInstruction(hp1);
  3781. if Unconditional then
  3782. MakeUnconditional(taicpu(hp2))
  3783. else
  3784. begin
  3785. if SetC = C_NONE then
  3786. InternalError(2018061402);
  3787. taicpu(hp2).SetCondition(SetC);
  3788. end;
  3789. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
  3790. begin
  3791. RemoveCurrentp(p, hp2);
  3792. Result := True;
  3793. end;
  3794. DebugMsg(SPeepholeOptimization + 'SETcc/TESTCmp/Jcc -> Jcc',p);
  3795. end
  3796. else if MatchInstruction(hp1, A_MOV, [S_B]) and
  3797. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3798. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) then
  3799. begin
  3800. TransferUsedRegs(TmpUsedRegs);
  3801. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  3802. if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp1, TmpUsedRegs) then
  3803. begin
  3804. AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,UsedRegs);
  3805. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[1]^.reg;
  3806. RemoveInstruction(hp1);
  3807. DebugMsg(SPeepholeOptimization + 'SETcc/Mov -> SETcc',p);
  3808. Result := true;
  3809. end;
  3810. end;
  3811. end;
  3812. end;
  3813. function TX86AsmOptimizer.OptPass1FSTP(var p: tai): boolean;
  3814. { returns true if a "continue" should be done after this optimization }
  3815. var
  3816. hp1, hp2: tai;
  3817. begin
  3818. Result := false;
  3819. if MatchOpType(taicpu(p),top_ref) and
  3820. GetNextInstruction(p, hp1) and
  3821. (hp1.typ = ait_instruction) and
  3822. (((taicpu(hp1).opcode = A_FLD) and
  3823. (taicpu(p).opcode = A_FSTP)) or
  3824. ((taicpu(p).opcode = A_FISTP) and
  3825. (taicpu(hp1).opcode = A_FILD))) and
  3826. MatchOpType(taicpu(hp1),top_ref) and
  3827. (taicpu(hp1).opsize = taicpu(p).opsize) and
  3828. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  3829. begin
  3830. { replacing fstp f;fld f by fst f is only valid for extended because of rounding or if fastmath is on }
  3831. if ((taicpu(p).opsize=S_FX) or (cs_opt_fastmath in current_settings.optimizerswitches)) and
  3832. GetNextInstruction(hp1, hp2) and
  3833. (hp2.typ = ait_instruction) and
  3834. IsExitCode(hp2) and
  3835. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  3836. not(assigned(current_procinfo.procdef.funcretsym) and
  3837. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  3838. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  3839. begin
  3840. RemoveInstruction(hp1);
  3841. RemoveCurrentP(p, hp2);
  3842. RemoveLastDeallocForFuncRes(p);
  3843. Result := true;
  3844. end
  3845. else
  3846. { we can do this only in fast math mode as fstp is rounding ...
  3847. ... still disabled as it breaks the compiler and/or rtl }
  3848. if ({ (cs_opt_fastmath in current_settings.optimizerswitches) or }
  3849. { ... or if another fstp equal to the first one follows }
  3850. (GetNextInstruction(hp1,hp2) and
  3851. (hp2.typ = ait_instruction) and
  3852. (taicpu(p).opcode=taicpu(hp2).opcode) and
  3853. (taicpu(p).opsize=taicpu(hp2).opsize))
  3854. ) and
  3855. { fst can't store an extended/comp value }
  3856. (taicpu(p).opsize <> S_FX) and
  3857. (taicpu(p).opsize <> S_IQ) then
  3858. begin
  3859. if (taicpu(p).opcode = A_FSTP) then
  3860. taicpu(p).opcode := A_FST
  3861. else
  3862. taicpu(p).opcode := A_FIST;
  3863. DebugMsg(SPeepholeOptimization + 'FstpFld2Fst',p);
  3864. RemoveInstruction(hp1);
  3865. end;
  3866. end;
  3867. end;
  3868. function TX86AsmOptimizer.OptPass1FLD(var p : tai) : boolean;
  3869. var
  3870. hp1, hp2: tai;
  3871. begin
  3872. result:=false;
  3873. if MatchOpType(taicpu(p),top_reg) and
  3874. GetNextInstruction(p, hp1) and
  3875. (hp1.typ = Ait_Instruction) and
  3876. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  3877. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  3878. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  3879. { change to
  3880. fld reg fxxx reg,st
  3881. fxxxp st, st1 (hp1)
  3882. Remark: non commutative operations must be reversed!
  3883. }
  3884. begin
  3885. case taicpu(hp1).opcode Of
  3886. A_FMULP,A_FADDP,
  3887. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  3888. begin
  3889. case taicpu(hp1).opcode Of
  3890. A_FADDP: taicpu(hp1).opcode := A_FADD;
  3891. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  3892. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  3893. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  3894. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  3895. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  3896. else
  3897. internalerror(2019050534);
  3898. end;
  3899. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  3900. taicpu(hp1).oper[1]^.reg := NR_ST;
  3901. RemoveCurrentP(p, hp1);
  3902. Result:=true;
  3903. exit;
  3904. end;
  3905. else
  3906. ;
  3907. end;
  3908. end
  3909. else
  3910. if MatchOpType(taicpu(p),top_ref) and
  3911. GetNextInstruction(p, hp2) and
  3912. (hp2.typ = Ait_Instruction) and
  3913. MatchOpType(taicpu(hp2),top_reg,top_reg) and
  3914. (taicpu(p).opsize in [S_FS, S_FL]) and
  3915. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  3916. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  3917. if GetLastInstruction(p, hp1) and
  3918. MatchInstruction(hp1,A_FLD,A_FST,[taicpu(p).opsize]) and
  3919. MatchOpType(taicpu(hp1),top_ref) and
  3920. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  3921. if ((taicpu(hp2).opcode = A_FMULP) or
  3922. (taicpu(hp2).opcode = A_FADDP)) then
  3923. { change to
  3924. fld/fst mem1 (hp1) fld/fst mem1
  3925. fld mem1 (p) fadd/
  3926. faddp/ fmul st, st
  3927. fmulp st, st1 (hp2) }
  3928. begin
  3929. RemoveCurrentP(p, hp1);
  3930. if (taicpu(hp2).opcode = A_FADDP) then
  3931. taicpu(hp2).opcode := A_FADD
  3932. else
  3933. taicpu(hp2).opcode := A_FMUL;
  3934. taicpu(hp2).oper[1]^.reg := NR_ST;
  3935. end
  3936. else
  3937. { change to
  3938. fld/fst mem1 (hp1) fld/fst mem1
  3939. fld mem1 (p) fld st}
  3940. begin
  3941. taicpu(p).changeopsize(S_FL);
  3942. taicpu(p).loadreg(0,NR_ST);
  3943. end
  3944. else
  3945. begin
  3946. case taicpu(hp2).opcode Of
  3947. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  3948. { change to
  3949. fld/fst mem1 (hp1) fld/fst mem1
  3950. fld mem2 (p) fxxx mem2
  3951. fxxxp st, st1 (hp2) }
  3952. begin
  3953. case taicpu(hp2).opcode Of
  3954. A_FADDP: taicpu(p).opcode := A_FADD;
  3955. A_FMULP: taicpu(p).opcode := A_FMUL;
  3956. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  3957. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  3958. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  3959. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  3960. else
  3961. internalerror(2019050533);
  3962. end;
  3963. RemoveInstruction(hp2);
  3964. end
  3965. else
  3966. ;
  3967. end
  3968. end
  3969. end;
  3970. function TX86AsmOptimizer.OptPass1Cmp(var p: tai): boolean;
  3971. var
  3972. v: TCGInt;
  3973. hp1, hp2: tai;
  3974. begin
  3975. Result:=false;
  3976. if taicpu(p).oper[0]^.typ = top_const then
  3977. begin
  3978. { Though GetNextInstruction can be factored out, it is an expensive
  3979. call, so delay calling it until we have first checked cheaper
  3980. conditions that are independent of it. }
  3981. if (taicpu(p).oper[0]^.val = 0) and
  3982. (taicpu(p).oper[1]^.typ = top_reg) and
  3983. GetNextInstruction(p, hp1) and
  3984. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) then
  3985. begin
  3986. hp2 := p;
  3987. { When dealing with "cmp $0,%reg", only ZF and SF contain
  3988. anything meaningful once it's converted to "test %reg,%reg";
  3989. additionally, some jumps will always (or never) branch, so
  3990. evaluate every jump immediately following the
  3991. comparison, optimising the conditions if possible.
  3992. Similarly with SETcc... those that are always set to 0 or 1
  3993. are changed to MOV instructions }
  3994. while GetNextInstruction(hp2, hp1) and
  3995. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) do
  3996. begin
  3997. case taicpu(hp1).condition of
  3998. C_B, C_C, C_NAE, C_O:
  3999. { For B/NAE:
  4000. Will never branch since an unsigned integer can never be below zero
  4001. For C/O:
  4002. Result cannot overflow because 0 is being subtracted
  4003. }
  4004. begin
  4005. if taicpu(hp1).opcode = A_Jcc then
  4006. begin
  4007. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (jump removed)', hp1);
  4008. TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
  4009. RemoveInstruction(hp1);
  4010. { Since hp1 was deleted, hp2 must not be updated }
  4011. Continue;
  4012. end
  4013. else
  4014. begin
  4015. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (set -> mov 0)', hp1);
  4016. { Convert "set(c) %reg" instruction to "movb 0,%reg" }
  4017. taicpu(hp1).opcode := A_MOV;
  4018. taicpu(hp1).ops := 2;
  4019. taicpu(hp1).condition := C_None;
  4020. taicpu(hp1).opsize := S_B;
  4021. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  4022. taicpu(hp1).loadconst(0, 0);
  4023. end;
  4024. end;
  4025. C_BE, C_NA:
  4026. begin
  4027. { Will only branch if equal to zero }
  4028. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition BE/NA --> E', hp1);
  4029. taicpu(hp1).condition := C_E;
  4030. end;
  4031. C_A, C_NBE:
  4032. begin
  4033. { Will only branch if not equal to zero }
  4034. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition A/NBE --> NE', hp1);
  4035. taicpu(hp1).condition := C_NE;
  4036. end;
  4037. C_AE, C_NB, C_NC, C_NO:
  4038. begin
  4039. { Will always branch }
  4040. DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition AE/NB/NC/NO --> Always', hp1);
  4041. if taicpu(hp1).opcode = A_Jcc then
  4042. begin
  4043. MakeUnconditional(taicpu(hp1));
  4044. { Any jumps/set that follow will now be dead code }
  4045. RemoveDeadCodeAfterJump(taicpu(hp1));
  4046. Break;
  4047. end
  4048. else
  4049. begin
  4050. { Convert "set(c) %reg" instruction to "movb 1,%reg" }
  4051. taicpu(hp1).opcode := A_MOV;
  4052. taicpu(hp1).ops := 2;
  4053. taicpu(hp1).condition := C_None;
  4054. taicpu(hp1).opsize := S_B;
  4055. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg);
  4056. taicpu(hp1).loadconst(0, 1);
  4057. end;
  4058. end;
  4059. C_None:
  4060. InternalError(2020012201);
  4061. C_P, C_PE, C_NP, C_PO:
  4062. { We can't handle parity checks and they should never be generated
  4063. after a general-purpose CMP (it's used in some floating-point
  4064. comparisons that don't use CMP) }
  4065. InternalError(2020012202);
  4066. else
  4067. { Zero/Equality, Sign, their complements and all of the
  4068. signed comparisons do not need to be converted };
  4069. end;
  4070. hp2 := hp1;
  4071. end;
  4072. { Convert the instruction to a TEST }
  4073. taicpu(p).opcode := A_TEST;
  4074. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  4075. Result := True;
  4076. Exit;
  4077. end
  4078. else if (taicpu(p).oper[0]^.val = 1) and
  4079. GetNextInstruction(p, hp1) and
  4080. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  4081. (taicpu(hp1).condition in [C_L, C_NGE]) then
  4082. begin
  4083. { Convert; To:
  4084. cmp $1,r/m cmp $0,r/m
  4085. jl @lbl jle @lbl
  4086. }
  4087. DebugMsg(SPeepholeOptimization + 'Cmp1Jl2Cmp0Jle', p);
  4088. taicpu(p).oper[0]^.val := 0;
  4089. taicpu(hp1).condition := C_LE;
  4090. { If the instruction is now "cmp $0,%reg", convert it to a
  4091. TEST (and effectively do the work of the "cmp $0,%reg" in
  4092. the block above)
  4093. If it's a reference, we can get away with not setting
  4094. Result to True because he haven't evaluated the jump
  4095. in this pass yet.
  4096. }
  4097. if (taicpu(p).oper[1]^.typ = top_reg) then
  4098. begin
  4099. taicpu(p).opcode := A_TEST;
  4100. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  4101. Result := True;
  4102. end;
  4103. Exit;
  4104. end
  4105. else if (taicpu(p).oper[1]^.typ = top_reg) then
  4106. begin
  4107. { cmp register,$8000 neg register
  4108. je target --> jo target
  4109. .... only if register is deallocated before jump.}
  4110. case Taicpu(p).opsize of
  4111. S_B: v:=$80;
  4112. S_W: v:=$8000;
  4113. S_L: v:=qword($80000000);
  4114. { S_Q will never happen: cmp with 64 bit constants is not possible }
  4115. S_Q:
  4116. Exit;
  4117. else
  4118. internalerror(2013112905);
  4119. end;
  4120. if (taicpu(p).oper[0]^.val=v) and
  4121. GetNextInstruction(p, hp1) and
  4122. MatchInstruction(hp1,A_Jcc,A_SETcc,[]) and
  4123. (Taicpu(hp1).condition in [C_E,C_NE]) then
  4124. begin
  4125. TransferUsedRegs(TmpUsedRegs);
  4126. UpdateUsedRegs(TmpUsedRegs,tai(p.next));
  4127. if not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, TmpUsedRegs)) then
  4128. begin
  4129. DebugMsg(SPeepholeOptimization + 'CmpJe2NegJo done',p);
  4130. Taicpu(p).opcode:=A_NEG;
  4131. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  4132. Taicpu(p).clearop(1);
  4133. Taicpu(p).ops:=1;
  4134. if Taicpu(hp1).condition=C_E then
  4135. Taicpu(hp1).condition:=C_O
  4136. else
  4137. Taicpu(hp1).condition:=C_NO;
  4138. Result:=true;
  4139. exit;
  4140. end;
  4141. end;
  4142. end;
  4143. end;
  4144. end;
  4145. function TX86AsmOptimizer.OptPass1PXor(var p: tai): boolean;
  4146. var
  4147. hp1: tai;
  4148. begin
  4149. {
  4150. remove the second (v)pxor from
  4151. pxor reg,reg
  4152. ...
  4153. pxor reg,reg
  4154. }
  4155. Result:=false;
  4156. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  4157. MatchOpType(taicpu(p),top_reg,top_reg) and
  4158. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  4159. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  4160. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  4161. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) then
  4162. begin
  4163. DebugMsg(SPeepholeOptimization + 'PXorPXor2PXor done',hp1);
  4164. RemoveInstruction(hp1);
  4165. Result:=true;
  4166. Exit;
  4167. end
  4168. {
  4169. replace
  4170. pxor reg1,reg1
  4171. movapd/s reg1,reg2
  4172. dealloc reg1
  4173. by
  4174. pxor reg2,reg2
  4175. }
  4176. else if GetNextInstruction(p,hp1) and
  4177. { we mix single and double opperations here because we assume that the compiler
  4178. generates vmovapd only after double operations and vmovaps only after single operations }
  4179. MatchInstruction(hp1,A_MOVAPD,A_MOVAPS,[S_NO]) and
  4180. MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) and
  4181. MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) and
  4182. (taicpu(p).oper[0]^.typ=top_reg) then
  4183. begin
  4184. TransferUsedRegs(TmpUsedRegs);
  4185. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4186. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  4187. begin
  4188. taicpu(p).loadoper(0,taicpu(hp1).oper[1]^);
  4189. taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
  4190. DebugMsg(SPeepholeOptimization + 'PXorMovapd2PXor done',p);
  4191. RemoveInstruction(hp1);
  4192. result:=true;
  4193. end;
  4194. end;
  4195. end;
  4196. function TX86AsmOptimizer.OptPass1VPXor(var p: tai): boolean;
  4197. var
  4198. hp1: tai;
  4199. begin
  4200. {
  4201. remove the second (v)pxor from
  4202. (v)pxor reg,reg
  4203. ...
  4204. (v)pxor reg,reg
  4205. }
  4206. Result:=false;
  4207. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^,taicpu(p).oper[2]^) and
  4208. MatchOpType(taicpu(p),top_reg,top_reg,top_reg) and
  4209. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  4210. MatchInstruction(hp1,taicpu(p).opcode,[taicpu(p).opsize]) and
  4211. MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[0]^) and
  4212. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^,taicpu(hp1).oper[2]^) then
  4213. begin
  4214. DebugMsg(SPeepholeOptimization + 'VPXorVPXor2PXor done',hp1);
  4215. RemoveInstruction(hp1);
  4216. Result:=true;
  4217. Exit;
  4218. end
  4219. else
  4220. Result:=OptPass1VOP(p);
  4221. end;
  4222. function TX86AsmOptimizer.OptPass1Imul(var p: tai): boolean;
  4223. var
  4224. hp1 : tai;
  4225. begin
  4226. result:=false;
  4227. { replace
  4228. IMul const,%mreg1,%mreg2
  4229. Mov %reg2,%mreg3
  4230. dealloc %mreg3
  4231. by
  4232. Imul const,%mreg1,%mreg23
  4233. }
  4234. if (taicpu(p).ops=3) and
  4235. GetNextInstruction(p,hp1) and
  4236. MatchInstruction(hp1,A_MOV,[taicpu(p).opsize]) and
  4237. MatchOperand(taicpu(p).oper[2]^,taicpu(hp1).oper[0]^) and
  4238. (taicpu(hp1).oper[1]^.typ=top_reg) then
  4239. begin
  4240. TransferUsedRegs(TmpUsedRegs);
  4241. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4242. if not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg,hp1,TmpUsedRegs)) then
  4243. begin
  4244. taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
  4245. DebugMsg(SPeepholeOptimization + 'ImulMov2Imul done',p);
  4246. RemoveInstruction(hp1);
  4247. result:=true;
  4248. end;
  4249. end;
  4250. end;
  4251. function TX86AsmOptimizer.OptPass2MOV(var p : tai) : boolean;
  4252. function IsXCHGAcceptable: Boolean; inline;
  4253. begin
  4254. { Always accept if optimising for size }
  4255. Result := (cs_opt_size in current_settings.optimizerswitches) or
  4256. (
  4257. {$ifdef x86_64}
  4258. { XCHG takes 3 cycles on AMD Athlon64 }
  4259. (current_settings.optimizecputype >= cpu_core_i)
  4260. {$else x86_64}
  4261. { From the Pentium M onwards, XCHG only has a latency of 2 rather
  4262. than 3, so it becomes a saving compared to three MOVs with two of
  4263. them able to execute simultaneously. [Kit] }
  4264. (current_settings.optimizecputype >= cpu_PentiumM)
  4265. {$endif x86_64}
  4266. );
  4267. end;
  4268. var
  4269. NewRef: TReference;
  4270. hp1,hp2,hp3: tai;
  4271. {$ifndef x86_64}
  4272. hp4: tai;
  4273. OperIdx: Integer;
  4274. {$endif x86_64}
  4275. begin
  4276. Result:=false;
  4277. if not GetNextInstruction(p, hp1) then
  4278. Exit;
  4279. if MatchInstruction(hp1, A_JMP, [S_NO]) then
  4280. begin
  4281. { Sometimes the MOVs that OptPass2JMP produces can be improved
  4282. further, but we can't just put this jump optimisation in pass 1
  4283. because it tends to perform worse when conditional jumps are
  4284. nearby (e.g. when converting CMOV instructions). [Kit] }
  4285. if OptPass2JMP(hp1) then
  4286. { call OptPass1MOV once to potentially merge any MOVs that were created }
  4287. Result := OptPass1MOV(p)
  4288. { OptPass2MOV will now exit but will be called again if OptPass1MOV
  4289. returned True and the instruction is still a MOV, thus checking
  4290. the optimisations below }
  4291. { If OptPass2JMP returned False, no optimisations were done to
  4292. the jump and there are no further optimisations that can be done
  4293. to the MOV instruction on this pass }
  4294. end
  4295. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  4296. (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  4297. MatchInstruction(hp1,A_ADD,A_SUB,[taicpu(p).opsize]) and
  4298. MatchOpType(taicpu(hp1),top_const,top_reg) and
  4299. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) and
  4300. { be lazy, checking separately for sub would be slightly better }
  4301. (abs(taicpu(hp1).oper[0]^.val)<=$7fffffff) then
  4302. begin
  4303. { Change:
  4304. movl/q %reg1,%reg2 movl/q %reg1,%reg2
  4305. addl/q $x,%reg2 subl/q $x,%reg2
  4306. To:
  4307. leal/q x(%reg1),%reg2 leal/q -x(%reg1),%reg2
  4308. }
  4309. TransferUsedRegs(TmpUsedRegs);
  4310. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4311. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  4312. if not GetNextInstruction(hp1, hp2) or
  4313. (
  4314. { The FLAGS register isn't always tracked properly, so do not
  4315. perform this optimisation if a conditional statement follows }
  4316. not RegReadByInstruction(NR_DEFAULTFLAGS, hp2) and
  4317. not RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)
  4318. ) then
  4319. begin
  4320. reference_reset(NewRef, 1, []);
  4321. NewRef.base := taicpu(p).oper[0]^.reg;
  4322. NewRef.scalefactor := 1;
  4323. if taicpu(hp1).opcode = A_ADD then
  4324. begin
  4325. DebugMsg(SPeepholeOptimization + 'MovAdd2Lea', p);
  4326. NewRef.offset := taicpu(hp1).oper[0]^.val;
  4327. end
  4328. else
  4329. begin
  4330. DebugMsg(SPeepholeOptimization + 'MovSub2Lea', p);
  4331. NewRef.offset := -taicpu(hp1).oper[0]^.val;
  4332. end;
  4333. taicpu(p).opcode := A_LEA;
  4334. taicpu(p).loadref(0, NewRef);
  4335. RemoveInstruction(hp1);
  4336. Result := True;
  4337. Exit;
  4338. end;
  4339. end
  4340. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  4341. {$ifdef x86_64}
  4342. MatchInstruction(hp1,A_MOVZX,A_MOVSX,A_MOVSXD,[]) and
  4343. {$else x86_64}
  4344. MatchInstruction(hp1,A_MOVZX,A_MOVSX,[]) and
  4345. {$endif x86_64}
  4346. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  4347. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  4348. { mov reg1, reg2 mov reg1, reg2
  4349. movzx/sx reg2, reg3 to movzx/sx reg1, reg3}
  4350. begin
  4351. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  4352. DebugMsg(SPeepholeOptimization + 'mov %reg1,%reg2; movzx/sx %reg2,%reg3 -> mov %reg1,%reg2;movzx/sx %reg1,%reg3',p);
  4353. { Don't remove the MOV command without first checking that reg2 isn't used afterwards,
  4354. or unless supreg(reg3) = supreg(reg2)). [Kit] }
  4355. TransferUsedRegs(TmpUsedRegs);
  4356. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  4357. if (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) or
  4358. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
  4359. then
  4360. begin
  4361. RemoveCurrentP(p, hp1);
  4362. Result:=true;
  4363. end;
  4364. exit;
  4365. end
  4366. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  4367. IsXCHGAcceptable and
  4368. { XCHG doesn't support 8-byte registers }
  4369. (taicpu(p).opsize <> S_B) and
  4370. MatchInstruction(hp1, A_MOV, []) and
  4371. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  4372. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  4373. GetNextInstruction(hp1, hp2) and
  4374. MatchInstruction(hp2, A_MOV, []) and
  4375. { Don't need to call MatchOpType for hp2 because the operand matches below cover for it }
  4376. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[1]^.reg) and
  4377. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) then
  4378. begin
  4379. { mov %reg1,%reg2
  4380. mov %reg3,%reg1 -> xchg %reg3,%reg1
  4381. mov %reg2,%reg3
  4382. (%reg2 not used afterwards)
  4383. Note that xchg takes 3 cycles to execute, and generally mov's take
  4384. only one cycle apiece, but the first two mov's can be executed in
  4385. parallel, only taking 2 cycles overall. Older processors should
  4386. therefore only optimise for size. [Kit]
  4387. }
  4388. TransferUsedRegs(TmpUsedRegs);
  4389. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4390. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  4391. if not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp2, TmpUsedRegs) then
  4392. begin
  4393. DebugMsg(SPeepholeOptimization + 'MovMovMov2XChg', p);
  4394. AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp1, UsedRegs);
  4395. taicpu(hp1).opcode := A_XCHG;
  4396. RemoveCurrentP(p, hp1);
  4397. RemoveInstruction(hp2);
  4398. Result := True;
  4399. Exit;
  4400. end;
  4401. end
  4402. else if MatchOpType(taicpu(p),top_reg,top_reg) and
  4403. MatchInstruction(hp1, A_SAR, []) then
  4404. begin
  4405. if MatchOperand(taicpu(hp1).oper[0]^, 31) then
  4406. begin
  4407. { the use of %edx also covers the opsize being S_L }
  4408. if MatchOperand(taicpu(hp1).oper[1]^, NR_EDX) then
  4409. begin
  4410. { Note it has to be specifically "movl %eax,%edx", and those specific sub-registers }
  4411. if (taicpu(p).oper[0]^.reg = NR_EAX) and
  4412. (taicpu(p).oper[1]^.reg = NR_EDX) then
  4413. begin
  4414. { Change:
  4415. movl %eax,%edx
  4416. sarl $31,%edx
  4417. To:
  4418. cltd
  4419. }
  4420. DebugMsg(SPeepholeOptimization + 'MovSar2Cltd', p);
  4421. RemoveInstruction(hp1);
  4422. taicpu(p).opcode := A_CDQ;
  4423. taicpu(p).opsize := S_NO;
  4424. taicpu(p).clearop(1);
  4425. taicpu(p).clearop(0);
  4426. taicpu(p).ops:=0;
  4427. Result := True;
  4428. end
  4429. else if (cs_opt_size in current_settings.optimizerswitches) and
  4430. (taicpu(p).oper[0]^.reg = NR_EDX) and
  4431. (taicpu(p).oper[1]^.reg = NR_EAX) then
  4432. begin
  4433. { Change:
  4434. movl %edx,%eax
  4435. sarl $31,%edx
  4436. To:
  4437. movl %edx,%eax
  4438. cltd
  4439. Note that this creates a dependency between the two instructions,
  4440. so only perform if optimising for size.
  4441. }
  4442. DebugMsg(SPeepholeOptimization + 'MovSar2MovCltd', p);
  4443. taicpu(hp1).opcode := A_CDQ;
  4444. taicpu(hp1).opsize := S_NO;
  4445. taicpu(hp1).clearop(1);
  4446. taicpu(hp1).clearop(0);
  4447. taicpu(hp1).ops:=0;
  4448. end;
  4449. {$ifndef x86_64}
  4450. end
  4451. { Don't bother if CMOV is supported, because a more optimal
  4452. sequence would have been generated for the Abs() intrinsic }
  4453. else if not(CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) and
  4454. { the use of %eax also covers the opsize being S_L }
  4455. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) and
  4456. (taicpu(p).oper[0]^.reg = NR_EAX) and
  4457. (taicpu(p).oper[1]^.reg = NR_EDX) and
  4458. GetNextInstruction(hp1, hp2) and
  4459. MatchInstruction(hp2, A_XOR, [S_L]) and
  4460. MatchOperand(taicpu(hp2).oper[0]^, NR_EAX) and
  4461. MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) and
  4462. GetNextInstruction(hp2, hp3) and
  4463. MatchInstruction(hp3, A_SUB, [S_L]) and
  4464. MatchOperand(taicpu(hp3).oper[0]^, NR_EAX) and
  4465. MatchOperand(taicpu(hp3).oper[1]^, NR_EDX) then
  4466. begin
  4467. { Change:
  4468. movl %eax,%edx
  4469. sarl $31,%eax
  4470. xorl %eax,%edx
  4471. subl %eax,%edx
  4472. (Instruction that uses %edx)
  4473. (%eax deallocated)
  4474. (%edx deallocated)
  4475. To:
  4476. cltd
  4477. xorl %edx,%eax <-- Note the registers have swapped
  4478. subl %edx,%eax
  4479. (Instruction that uses %eax) <-- %eax rather than %edx
  4480. }
  4481. TransferUsedRegs(TmpUsedRegs);
  4482. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  4483. UpdateUsedRegs(TmpUsedRegs, tai(hp1.Next));
  4484. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  4485. if not RegUsedAfterInstruction(NR_EAX, hp3, TmpUsedRegs) then
  4486. begin
  4487. if GetNextInstruction(hp3, hp4) and
  4488. not RegModifiedByInstruction(NR_EDX, hp4) and
  4489. not RegUsedAfterInstruction(NR_EDX, hp4, TmpUsedRegs) then
  4490. begin
  4491. DebugMsg(SPeepholeOptimization + 'abs() intrinsic optimisation', p);
  4492. taicpu(p).opcode := A_CDQ;
  4493. taicpu(p).clearop(1);
  4494. taicpu(p).clearop(0);
  4495. taicpu(p).ops:=0;
  4496. RemoveInstruction(hp1);
  4497. taicpu(hp2).loadreg(0, NR_EDX);
  4498. taicpu(hp2).loadreg(1, NR_EAX);
  4499. taicpu(hp3).loadreg(0, NR_EDX);
  4500. taicpu(hp3).loadreg(1, NR_EAX);
  4501. AllocRegBetween(NR_EAX, hp3, hp4, TmpUsedRegs);
  4502. { Convert references in the following instruction (hp4) from %edx to %eax }
  4503. for OperIdx := 0 to taicpu(hp4).ops - 1 do
  4504. with taicpu(hp4).oper[OperIdx]^ do
  4505. case typ of
  4506. top_reg:
  4507. if getsupreg(reg) = RS_EDX then
  4508. reg := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  4509. top_ref:
  4510. begin
  4511. if getsupreg(reg) = RS_EDX then
  4512. ref^.base := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  4513. if getsupreg(reg) = RS_EDX then
  4514. ref^.index := newreg(R_INTREGISTER,RS_EAX,getsubreg(reg));
  4515. end;
  4516. else
  4517. ;
  4518. end;
  4519. end;
  4520. end;
  4521. {$else x86_64}
  4522. end;
  4523. end
  4524. else if MatchOperand(taicpu(hp1).oper[0]^, 63) and
  4525. { the use of %rdx also covers the opsize being S_Q }
  4526. MatchOperand(taicpu(hp1).oper[1]^, NR_RDX) then
  4527. begin
  4528. { Note it has to be specifically "movq %rax,%rdx", and those specific sub-registers }
  4529. if (taicpu(p).oper[0]^.reg = NR_RAX) and
  4530. (taicpu(p).oper[1]^.reg = NR_RDX) then
  4531. begin
  4532. { Change:
  4533. movq %rax,%rdx
  4534. sarq $63,%rdx
  4535. To:
  4536. cqto
  4537. }
  4538. DebugMsg(SPeepholeOptimization + 'MovSar2Cqto', p);
  4539. RemoveInstruction(hp1);
  4540. taicpu(p).opcode := A_CQO;
  4541. taicpu(p).opsize := S_NO;
  4542. taicpu(p).clearop(1);
  4543. taicpu(p).clearop(0);
  4544. taicpu(p).ops:=0;
  4545. Result := True;
  4546. end
  4547. else if (cs_opt_size in current_settings.optimizerswitches) and
  4548. (taicpu(p).oper[0]^.reg = NR_RDX) and
  4549. (taicpu(p).oper[1]^.reg = NR_RAX) then
  4550. begin
  4551. { Change:
  4552. movq %rdx,%rax
  4553. sarq $63,%rdx
  4554. To:
  4555. movq %rdx,%rax
  4556. cqto
  4557. Note that this creates a dependency between the two instructions,
  4558. so only perform if optimising for size.
  4559. }
  4560. DebugMsg(SPeepholeOptimization + 'MovSar2MovCqto', p);
  4561. taicpu(hp1).opcode := A_CQO;
  4562. taicpu(hp1).opsize := S_NO;
  4563. taicpu(hp1).clearop(1);
  4564. taicpu(hp1).clearop(0);
  4565. taicpu(hp1).ops:=0;
  4566. {$endif x86_64}
  4567. end;
  4568. end;
  4569. end
  4570. else if MatchInstruction(hp1, A_MOV, []) and
  4571. (taicpu(hp1).oper[1]^.typ = top_reg) then
  4572. { Though "GetNextInstruction" could be factored out, along with
  4573. the instructions that depend on hp2, it is an expensive call that
  4574. should be delayed for as long as possible, hence we do cheaper
  4575. checks first that are likely to be False. [Kit] }
  4576. begin
  4577. if MatchOperand(taicpu(p).oper[1]^, NR_EDX) and
  4578. (
  4579. (
  4580. (taicpu(hp1).oper[1]^.reg = NR_EAX) and
  4581. (
  4582. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4583. MatchOperand(taicpu(hp1).oper[0]^, NR_EDX)
  4584. )
  4585. ) or
  4586. (
  4587. (taicpu(hp1).oper[1]^.reg = NR_EDX) and
  4588. (
  4589. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4590. MatchOperand(taicpu(hp1).oper[0]^, NR_EAX)
  4591. )
  4592. )
  4593. ) and
  4594. GetNextInstruction(hp1, hp2) and
  4595. MatchInstruction(hp2, A_SAR, []) and
  4596. MatchOperand(taicpu(hp2).oper[0]^, 31) then
  4597. begin
  4598. if MatchOperand(taicpu(hp2).oper[1]^, NR_EDX) then
  4599. begin
  4600. { Change:
  4601. movl r/m,%edx movl r/m,%eax movl r/m,%edx movl r/m,%eax
  4602. movl %edx,%eax or movl %eax,%edx or movl r/m,%eax or movl r/m,%edx
  4603. sarl $31,%edx sarl $31,%edx sarl $31,%edx sarl $31,%edx
  4604. To:
  4605. movl r/m,%eax <- Note the change in register
  4606. cltd
  4607. }
  4608. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCltd', p);
  4609. AllocRegBetween(NR_EAX, p, hp1, UsedRegs);
  4610. taicpu(p).loadreg(1, NR_EAX);
  4611. taicpu(hp1).opcode := A_CDQ;
  4612. taicpu(hp1).clearop(1);
  4613. taicpu(hp1).clearop(0);
  4614. taicpu(hp1).ops:=0;
  4615. RemoveInstruction(hp2);
  4616. (*
  4617. {$ifdef x86_64}
  4618. end
  4619. else if MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) and
  4620. { This code sequence does not get generated - however it might become useful
  4621. if and when 128-bit signed integer types make an appearance, so the code
  4622. is kept here for when it is eventually needed. [Kit] }
  4623. (
  4624. (
  4625. (taicpu(hp1).oper[1]^.reg = NR_RAX) and
  4626. (
  4627. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4628. MatchOperand(taicpu(hp1).oper[0]^, NR_RDX)
  4629. )
  4630. ) or
  4631. (
  4632. (taicpu(hp1).oper[1]^.reg = NR_RDX) and
  4633. (
  4634. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^) or
  4635. MatchOperand(taicpu(hp1).oper[0]^, NR_RAX)
  4636. )
  4637. )
  4638. ) and
  4639. GetNextInstruction(hp1, hp2) and
  4640. MatchInstruction(hp2, A_SAR, [S_Q]) and
  4641. MatchOperand(taicpu(hp2).oper[0]^, 63) and
  4642. MatchOperand(taicpu(hp2).oper[1]^, NR_RDX) then
  4643. begin
  4644. { Change:
  4645. movq r/m,%rdx movq r/m,%rax movq r/m,%rdx movq r/m,%rax
  4646. movq %rdx,%rax or movq %rax,%rdx or movq r/m,%rax or movq r/m,%rdx
  4647. sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx sarq $63,%rdx
  4648. To:
  4649. movq r/m,%rax <- Note the change in register
  4650. cqto
  4651. }
  4652. DebugMsg(SPeepholeOptimization + 'MovMovSar2MovCqto', p);
  4653. AllocRegBetween(NR_RAX, p, hp1, UsedRegs);
  4654. taicpu(p).loadreg(1, NR_RAX);
  4655. taicpu(hp1).opcode := A_CQO;
  4656. taicpu(hp1).clearop(1);
  4657. taicpu(hp1).clearop(0);
  4658. taicpu(hp1).ops:=0;
  4659. RemoveInstruction(hp2);
  4660. {$endif x86_64}
  4661. *)
  4662. end;
  4663. end;
  4664. {$ifdef x86_64}
  4665. end
  4666. else if (taicpu(p).opsize = S_L) and
  4667. (taicpu(p).oper[1]^.typ = top_reg) and
  4668. (
  4669. MatchInstruction(hp1, A_MOV,[]) and
  4670. (taicpu(hp1).opsize = S_L) and
  4671. (taicpu(hp1).oper[1]^.typ = top_reg)
  4672. ) and (
  4673. GetNextInstruction(hp1, hp2) and
  4674. (tai(hp2).typ=ait_instruction) and
  4675. (taicpu(hp2).opsize = S_Q) and
  4676. (
  4677. (
  4678. MatchInstruction(hp2, A_ADD,[]) and
  4679. (taicpu(hp2).opsize = S_Q) and
  4680. (taicpu(hp2).oper[0]^.typ = top_reg) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4681. (
  4682. (
  4683. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4684. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4685. ) or (
  4686. (getsupreg(taicpu(hp2).oper[0]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4687. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4688. )
  4689. )
  4690. ) or (
  4691. MatchInstruction(hp2, A_LEA,[]) and
  4692. (taicpu(hp2).oper[0]^.ref^.offset = 0) and
  4693. (taicpu(hp2).oper[0]^.ref^.scalefactor <= 1) and
  4694. (
  4695. (
  4696. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(p).oper[1]^.reg)) and
  4697. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4698. ) or (
  4699. (getsupreg(taicpu(hp2).oper[0]^.ref^.base) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  4700. (getsupreg(taicpu(hp2).oper[0]^.ref^.index) = getsupreg(taicpu(p).oper[1]^.reg))
  4701. )
  4702. ) and (
  4703. (
  4704. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg))
  4705. ) or (
  4706. (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg))
  4707. )
  4708. )
  4709. )
  4710. )
  4711. ) and (
  4712. GetNextInstruction(hp2, hp3) and
  4713. MatchInstruction(hp3, A_SHR,[]) and
  4714. (taicpu(hp3).opsize = S_Q) and
  4715. (taicpu(hp3).oper[0]^.typ = top_const) and (taicpu(hp2).oper[1]^.typ = top_reg) and
  4716. (taicpu(hp3).oper[0]^.val = 1) and
  4717. (taicpu(hp3).oper[1]^.reg = taicpu(hp2).oper[1]^.reg)
  4718. ) then
  4719. begin
  4720. { Change movl x, reg1d movl x, reg1d
  4721. movl y, reg2d movl y, reg2d
  4722. addq reg2q,reg1q or leaq (reg1q,reg2q),reg1q
  4723. shrq $1, reg1q shrq $1, reg1q
  4724. ( reg1d and reg2d can be switched around in the first two instructions )
  4725. To movl x, reg1d
  4726. addl y, reg1d
  4727. rcrl $1, reg1d
  4728. This corresponds to the common expression (x + y) shr 1, where
  4729. x and y are Cardinals (replacing "shr 1" with "div 2" produces
  4730. smaller code, but won't account for x + y causing an overflow). [Kit]
  4731. }
  4732. if (getsupreg(taicpu(hp2).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) then
  4733. { Change first MOV command to have the same register as the final output }
  4734. taicpu(p).oper[1]^.reg := taicpu(hp1).oper[1]^.reg
  4735. else
  4736. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  4737. { Change second MOV command to an ADD command. This is easier than
  4738. converting the existing command because it means we don't have to
  4739. touch 'y', which might be a complicated reference, and also the
  4740. fact that the third command might either be ADD or LEA. [Kit] }
  4741. taicpu(hp1).opcode := A_ADD;
  4742. { Delete old ADD/LEA instruction }
  4743. RemoveInstruction(hp2);
  4744. { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
  4745. taicpu(hp3).opcode := A_RCR;
  4746. taicpu(hp3).changeopsize(S_L);
  4747. setsubreg(taicpu(hp3).oper[1]^.reg, R_SUBD);
  4748. {$endif x86_64}
  4749. end;
  4750. end;
  4751. function TX86AsmOptimizer.OptPass2Movx(var p : tai) : boolean;
  4752. const
  4753. LIST_STEP_SIZE = 4;
  4754. var
  4755. ThisReg: TRegister;
  4756. MinSize, MaxSize, TrySmaller, TargetSize: TOpSize;
  4757. TargetSubReg: TSubRegister;
  4758. hp1, hp2: tai;
  4759. RegInUse, RegChanged, p_removed: Boolean;
  4760. { Store list of found instructions so we don't have to call
  4761. GetNextInstructionUsingReg multiple times }
  4762. InstrList: array of taicpu;
  4763. InstrMax, Index: Integer;
  4764. UpperLimit, TrySmallerLimit: TCgInt;
  4765. { Data flow analysis }
  4766. TestValMin, TestValMax: TCgInt;
  4767. SmallerOverflow: Boolean;
  4768. begin
  4769. Result := False;
  4770. p_removed := False;
  4771. { This is anything but quick! }
  4772. if not(cs_opt_level2 in current_settings.optimizerswitches) then
  4773. Exit;
  4774. SetLength(InstrList, 0);
  4775. InstrMax := -1;
  4776. ThisReg := taicpu(p).oper[1]^.reg;
  4777. hp1 := p;
  4778. case taicpu(p).opsize of
  4779. S_BW, S_BL:
  4780. begin
  4781. UpperLimit := $FF;
  4782. MinSize := S_B;
  4783. if taicpu(p).opsize = S_BW then
  4784. MaxSize := S_W
  4785. else
  4786. MaxSize := S_L;
  4787. end;
  4788. S_WL:
  4789. begin
  4790. UpperLimit := $FFFF;
  4791. MinSize := S_W;
  4792. MaxSize := S_L;
  4793. end
  4794. else
  4795. InternalError(2020112301);
  4796. end;
  4797. TestValMin := 0;
  4798. TestValMax := UpperLimit;
  4799. TrySmallerLimit := UpperLimit;
  4800. TrySmaller := S_NO;
  4801. SmallerOverflow := False;
  4802. RegChanged := False;
  4803. while GetNextInstructionUsingReg(hp1, hp1, ThisReg) and
  4804. (hp1.typ = ait_instruction) and
  4805. (
  4806. { Under -O1 and -O2, GetNextInstructionUsingReg may return an
  4807. instruction that doesn't actually contain ThisReg }
  4808. (cs_opt_level3 in current_settings.optimizerswitches) or
  4809. RegInInstruction(ThisReg, hp1)
  4810. ) do
  4811. begin
  4812. case taicpu(hp1).opcode of
  4813. A_INC,A_DEC:
  4814. begin
  4815. { Has to be an exact match on the register }
  4816. if not MatchOperand(taicpu(hp1).oper[0]^, ThisReg) then
  4817. Break;
  4818. if taicpu(hp1).opcode = A_INC then
  4819. begin
  4820. Inc(TestValMin);
  4821. Inc(TestValMax);
  4822. end
  4823. else
  4824. begin
  4825. Dec(TestValMin);
  4826. Dec(TestValMax);
  4827. end;
  4828. end;
  4829. { OR and XOR are not included because they can too easily fool
  4830. the data flow analysis (they can cause non-linear behaviour) }
  4831. A_ADD,A_SUB,A_AND,A_SHL,A_SHR:
  4832. begin
  4833. if
  4834. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  4835. { Has to be an exact match on the register }
  4836. (taicpu(hp1).oper[1]^.reg <> ThisReg) or not
  4837. (
  4838. (
  4839. (taicpu(hp1).oper[0]^.typ = top_const) and
  4840. (
  4841. (
  4842. (taicpu(hp1).opcode = A_SHL) and
  4843. (
  4844. ((MinSize = S_B) and (taicpu(hp1).oper[0]^.val < 8)) or
  4845. ((MinSize = S_W) and (taicpu(hp1).oper[0]^.val < 16)) or
  4846. ((MinSize = S_L) and (taicpu(hp1).oper[0]^.val < 32))
  4847. )
  4848. ) or (
  4849. (taicpu(hp1).opcode <> A_SHL) and
  4850. (
  4851. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  4852. { Is it in the negative range? }
  4853. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val))
  4854. )
  4855. )
  4856. )
  4857. ) or (
  4858. MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^.reg) and
  4859. ((taicpu(hp1).opcode = A_ADD) or (taicpu(hp1).opcode = A_AND) or (taicpu(hp1).opcode = A_SUB))
  4860. )
  4861. ) then
  4862. Break;
  4863. case taicpu(hp1).opcode of
  4864. A_ADD:
  4865. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  4866. begin
  4867. TestValMin := TestValMin * 2;
  4868. TestValMax := TestValMax * 2;
  4869. end
  4870. else
  4871. begin
  4872. TestValMin := TestValMin + taicpu(hp1).oper[0]^.val;
  4873. TestValMax := TestValMax + taicpu(hp1).oper[0]^.val;
  4874. end;
  4875. A_SUB:
  4876. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  4877. begin
  4878. TestValMin := 0;
  4879. TestValMax := 0;
  4880. end
  4881. else
  4882. begin
  4883. TestValMin := TestValMin - taicpu(hp1).oper[0]^.val;
  4884. TestValMax := TestValMax - taicpu(hp1).oper[0]^.val;
  4885. end;
  4886. A_AND:
  4887. if (taicpu(hp1).oper[0]^.typ = top_const) then
  4888. begin
  4889. { we might be able to go smaller if AND appears first }
  4890. if InstrMax = -1 then
  4891. case MinSize of
  4892. S_B:
  4893. ;
  4894. S_W:
  4895. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  4896. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  4897. begin
  4898. TrySmaller := S_B;
  4899. TrySmallerLimit := $FF;
  4900. end;
  4901. S_L:
  4902. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  4903. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  4904. begin
  4905. TrySmaller := S_B;
  4906. TrySmallerLimit := $FF;
  4907. end
  4908. else if ((taicpu(hp1).oper[0]^.val and $FFFF) = taicpu(hp1).oper[0]^.val) or
  4909. ((not(taicpu(hp1).oper[0]^.val) and $7FFF) = (not taicpu(hp1).oper[0]^.val)) then
  4910. begin
  4911. TrySmaller := S_W;
  4912. TrySmallerLimit := $FFFF;
  4913. end;
  4914. else
  4915. InternalError(2020112320);
  4916. end;
  4917. TestValMin := TestValMin and taicpu(hp1).oper[0]^.val;
  4918. TestValMax := TestValMax and taicpu(hp1).oper[0]^.val;
  4919. end;
  4920. A_SHL:
  4921. begin
  4922. TestValMin := TestValMin shl taicpu(hp1).oper[0]^.val;
  4923. TestValMax := TestValMax shl taicpu(hp1).oper[0]^.val;
  4924. end;
  4925. A_SHR:
  4926. begin
  4927. { we might be able to go smaller if SHR appears first }
  4928. if InstrMax = -1 then
  4929. case MinSize of
  4930. S_B:
  4931. ;
  4932. S_W:
  4933. if (taicpu(hp1).oper[0]^.val >= 8) then
  4934. begin
  4935. TrySmaller := S_B;
  4936. TrySmallerLimit := $FF;
  4937. end;
  4938. S_L:
  4939. if (taicpu(hp1).oper[0]^.val >= 24) then
  4940. begin
  4941. TrySmaller := S_B;
  4942. TrySmallerLimit := $FF;
  4943. end
  4944. else if (taicpu(hp1).oper[0]^.val >= 16) then
  4945. begin
  4946. TrySmaller := S_W;
  4947. TrySmallerLimit := $FFFF;
  4948. end;
  4949. else
  4950. InternalError(2020112321);
  4951. end;
  4952. TestValMin := TestValMin shr taicpu(hp1).oper[0]^.val;
  4953. TestValMax := TestValMax shr taicpu(hp1).oper[0]^.val;
  4954. end;
  4955. else
  4956. InternalError(2020112303);
  4957. end;
  4958. end;
  4959. (*
  4960. A_IMUL:
  4961. case taicpu(hp1).ops of
  4962. 2:
  4963. begin
  4964. if not MatchOpType(hp1, top_reg, top_reg) or
  4965. { Has to be an exact match on the register }
  4966. (taicpu(hp1).oper[0]^.reg <> ThisReg) or
  4967. (taicpu(hp1).oper[1]^.reg <> ThisReg) then
  4968. Break;
  4969. TestValMin := TestValMin * TestValMin;
  4970. TestValMax := TestValMax * TestValMax;
  4971. end;
  4972. 3:
  4973. begin
  4974. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  4975. { Has to be an exact match on the register }
  4976. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  4977. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  4978. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  4979. { Is it in the negative range? }
  4980. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  4981. Break;
  4982. TestValMin := TestValMin * taicpu(hp1).oper[0]^.val;
  4983. TestValMax := TestValMax * taicpu(hp1).oper[0]^.val;
  4984. end;
  4985. else
  4986. Break;
  4987. end;
  4988. A_IDIV:
  4989. case taicpu(hp1).ops of
  4990. 3:
  4991. begin
  4992. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  4993. { Has to be an exact match on the register }
  4994. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  4995. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  4996. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  4997. { Is it in the negative range? }
  4998. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  4999. Break;
  5000. TestValMin := TestValMin div taicpu(hp1).oper[0]^.val;
  5001. TestValMax := TestValMax div taicpu(hp1).oper[0]^.val;
  5002. end;
  5003. else
  5004. Break;
  5005. end;
  5006. *)
  5007. A_MOVZX:
  5008. begin
  5009. if not MatchOpType(taicpu(hp1), top_reg, top_reg) then
  5010. Break;
  5011. if not SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, ThisReg) then
  5012. begin
  5013. { Because hp1 was obtained via GetNextInstructionUsingReg
  5014. and ThisReg doesn't appear in the first operand, it
  5015. must appear in the second operand and hence gets
  5016. overwritten }
  5017. if (InstrMax = -1) and
  5018. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, ThisReg) then
  5019. begin
  5020. { The two MOVZX instructions are adjacent, so remove the first one }
  5021. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 5', p);
  5022. RemoveCurrentP(p);
  5023. Result := True;
  5024. Exit;
  5025. end;
  5026. Break;
  5027. end;
  5028. { The objective here is to try to find a combination that
  5029. removes one of the MOV/Z instructions. }
  5030. case taicpu(hp1).opsize of
  5031. S_WL:
  5032. if (MinSize in [S_B, S_W]) then
  5033. begin
  5034. TargetSize := S_L;
  5035. TargetSubReg := R_SUBD;
  5036. end
  5037. else if ((TrySmaller in [S_B, S_W]) and not SmallerOverflow) then
  5038. begin
  5039. TargetSize := TrySmaller;
  5040. if TrySmaller = S_B then
  5041. TargetSubReg := R_SUBL
  5042. else
  5043. TargetSubReg := R_SUBW;
  5044. end
  5045. else
  5046. Break;
  5047. S_BW:
  5048. if (MinSize in [S_B, S_W]) then
  5049. begin
  5050. TargetSize := S_W;
  5051. TargetSubReg := R_SUBW;
  5052. end
  5053. else if ((TrySmaller = S_B) and not SmallerOverflow) then
  5054. begin
  5055. TargetSize := S_B;
  5056. TargetSubReg := R_SUBL;
  5057. end
  5058. else
  5059. Break;
  5060. S_BL:
  5061. if (MinSize in [S_B, S_W]) then
  5062. begin
  5063. TargetSize := S_L;
  5064. TargetSubReg := R_SUBD;
  5065. end
  5066. else if ((TrySmaller = S_B) and not SmallerOverflow) then
  5067. begin
  5068. TargetSize := S_B;
  5069. TargetSubReg := R_SUBL;
  5070. end
  5071. else
  5072. Break;
  5073. else
  5074. InternalError(2020112302);
  5075. end;
  5076. { Update the register to its new size }
  5077. ThisReg := newreg(R_INTREGISTER, getsupreg(ThisReg), TargetSubReg);
  5078. if TargetSize = MinSize then
  5079. begin
  5080. { Convert the input MOVZX to a MOV }
  5081. if (taicpu(p).oper[0]^.typ = top_reg) and
  5082. SuperRegistersEqual(taicpu(p).oper[0]^.reg, ThisReg) then
  5083. begin
  5084. { Or remove it completely! }
  5085. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 1', p);
  5086. RemoveCurrentP(p);
  5087. p_removed := True;
  5088. end
  5089. else
  5090. begin
  5091. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 1', p);
  5092. taicpu(p).opcode := A_MOV;
  5093. taicpu(p).oper[1]^.reg := ThisReg;
  5094. taicpu(p).opsize := TargetSize;
  5095. end;
  5096. Result := True;
  5097. end
  5098. else if TargetSize <> MaxSize then
  5099. begin
  5100. case MaxSize of
  5101. S_L:
  5102. if TargetSize = S_W then
  5103. begin
  5104. DebugMsg(SPeepholeOptimization + 'movzbl2movzbw', p);
  5105. taicpu(p).opsize := S_BW;
  5106. taicpu(p).oper[1]^.reg := ThisReg;
  5107. Result := True;
  5108. end
  5109. else
  5110. InternalError(2020112341);
  5111. S_W:
  5112. if TargetSize = S_L then
  5113. begin
  5114. DebugMsg(SPeepholeOptimization + 'movzbw2movzbl', p);
  5115. taicpu(p).opsize := S_BL;
  5116. taicpu(p).oper[1]^.reg := ThisReg;
  5117. Result := True;
  5118. end
  5119. else
  5120. InternalError(2020112342);
  5121. else
  5122. ;
  5123. end;
  5124. end;
  5125. if (MaxSize = TargetSize) or
  5126. ((TargetSize = S_L) and (taicpu(hp1).opsize in [S_L, S_BL, S_WL])) or
  5127. ((TargetSize = S_W) and (taicpu(hp1).opsize in [S_W, S_BW])) then
  5128. begin
  5129. { Convert the output MOVZX to a MOV }
  5130. if SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) then
  5131. begin
  5132. { Or remove it completely! }
  5133. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 2', hp1);
  5134. { Be careful; if p = hp1 and p was also removed, p
  5135. will become a dangling pointer }
  5136. if p = hp1 then
  5137. RemoveCurrentp(p) { p = hp1 and will then become the next instruction }
  5138. else
  5139. RemoveInstruction(hp1);
  5140. end
  5141. else
  5142. begin
  5143. taicpu(hp1).opcode := A_MOV;
  5144. taicpu(hp1).oper[0]^.reg := ThisReg;
  5145. taicpu(hp1).opsize := TargetSize;
  5146. { Check to see if the active register is used afterwards;
  5147. if not, we can change it and make a saving. }
  5148. RegInUse := False;
  5149. TransferUsedRegs(TmpUsedRegs);
  5150. { The target register may be marked as in use to cross
  5151. a jump to a distant label, so exclude it }
  5152. ExcludeRegFromUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs);
  5153. hp2 := p;
  5154. repeat
  5155. UpdateUsedRegs(TmpUsedRegs, tai(hp2.next));
  5156. { Explicitly check for the excluded register (don't include the first
  5157. instruction as it may be reading from here }
  5158. if ((p <> hp2) and (RegInInstruction(taicpu(hp1).oper[1]^.reg, hp2))) or
  5159. RegInUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs) then
  5160. begin
  5161. RegInUse := True;
  5162. Break;
  5163. end;
  5164. if not GetNextInstruction(hp2, hp2) then
  5165. InternalError(2020112340);
  5166. until (hp2 = hp1);
  5167. if not RegInUse and not RegUsedAfterInstruction(ThisReg, hp1, TmpUsedRegs) then
  5168. begin
  5169. DebugMsg(SPeepholeOptimization + 'Simplified register usage so ' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' = ' + debug_regname(taicpu(p).oper[1]^.reg), p);
  5170. ThisReg := taicpu(hp1).oper[1]^.reg;
  5171. RegChanged := True;
  5172. TransferUsedRegs(TmpUsedRegs);
  5173. AllocRegBetween(ThisReg, p, hp1, TmpUsedRegs);
  5174. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 3', hp1);
  5175. if p = hp1 then
  5176. RemoveCurrentp(p) { p = hp1 and will then become the next instruction }
  5177. else
  5178. RemoveInstruction(hp1);
  5179. { Instruction will become "mov %reg,%reg" }
  5180. if not p_removed and (taicpu(p).opcode = A_MOV) and
  5181. MatchOperand(taicpu(p).oper[0]^, ThisReg) then
  5182. begin
  5183. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 6', p);
  5184. RemoveCurrentP(p);
  5185. p_removed := True;
  5186. end
  5187. else
  5188. taicpu(p).oper[1]^.reg := ThisReg;
  5189. Result := True;
  5190. end
  5191. else
  5192. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 2', hp1);
  5193. end;
  5194. end
  5195. else
  5196. InternalError(2020112330);
  5197. { Now go through every instruction we found and change the
  5198. size. If TargetSize = MaxSize, then almost no changes are
  5199. needed and Result can remain False if it hasn't been set
  5200. yet.
  5201. If RegChanged is True, then the register requires changing
  5202. and so the point about TargetSize = MaxSize doesn't apply. }
  5203. if ((TargetSize <> MaxSize) or RegChanged) and (InstrMax >= 0) then
  5204. begin
  5205. for Index := 0 to InstrMax do
  5206. begin
  5207. { If p_removed is true, then the original MOV/Z was removed
  5208. and removing the AND instruction may not be safe if it
  5209. appears first }
  5210. if (InstrList[Index].oper[InstrList[Index].ops - 1]^.typ <> top_reg) then
  5211. InternalError(2020112310);
  5212. if InstrList[Index].oper[0]^.typ = top_reg then
  5213. InstrList[Index].oper[0]^.reg := ThisReg;
  5214. InstrList[Index].oper[InstrList[Index].ops - 1]^.reg := ThisReg;
  5215. InstrList[Index].opsize := TargetSize;
  5216. end;
  5217. Result := True;
  5218. end;
  5219. Exit;
  5220. end;
  5221. else
  5222. { This includes ADC, SBB, IDIV and SAR }
  5223. Break;
  5224. end;
  5225. if (TestValMin < 0) or (TestValMax < 0) or
  5226. (TestValMin > UpperLimit) or (TestValMax > UpperLimit) then
  5227. { Overflow }
  5228. Break
  5229. else if not SmallerOverflow and (TrySmaller <> S_NO) and
  5230. ((TestValMin > TrySmallerLimit) or (TestValMax > TrySmallerLimit)) then
  5231. SmallerOverflow := True;
  5232. { Contains highest index (so instruction count - 1) }
  5233. Inc(InstrMax);
  5234. if InstrMax > High(InstrList) then
  5235. SetLength(InstrList, InstrMax + LIST_STEP_SIZE);
  5236. InstrList[InstrMax] := taicpu(hp1);
  5237. end;
  5238. end;
  5239. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  5240. var
  5241. hp1 : tai;
  5242. begin
  5243. Result:=false;
  5244. if (taicpu(p).ops >= 2) and
  5245. ((taicpu(p).oper[0]^.typ = top_const) or
  5246. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  5247. (taicpu(p).oper[1]^.typ = top_reg) and
  5248. ((taicpu(p).ops = 2) or
  5249. ((taicpu(p).oper[2]^.typ = top_reg) and
  5250. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  5251. GetLastInstruction(p,hp1) and
  5252. MatchInstruction(hp1,A_MOV,[]) and
  5253. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  5254. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  5255. begin
  5256. TransferUsedRegs(TmpUsedRegs);
  5257. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) or
  5258. ((taicpu(p).ops = 3) and (taicpu(p).oper[1]^.reg=taicpu(p).oper[2]^.reg)) then
  5259. { change
  5260. mov reg1,reg2
  5261. imul y,reg2 to imul y,reg1,reg2 }
  5262. begin
  5263. taicpu(p).ops := 3;
  5264. taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
  5265. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  5266. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  5267. RemoveInstruction(hp1);
  5268. result:=true;
  5269. end;
  5270. end;
  5271. end;
  5272. procedure TX86AsmOptimizer.ConvertJumpToRET(const p: tai; const ret_p: tai);
  5273. var
  5274. ThisLabel: TAsmLabel;
  5275. begin
  5276. ThisLabel := tasmlabel(taicpu(p).oper[0]^.ref^.symbol);
  5277. ThisLabel.decrefs;
  5278. taicpu(p).opcode := A_RET;
  5279. taicpu(p).is_jmp := false;
  5280. taicpu(p).ops := taicpu(ret_p).ops;
  5281. case taicpu(ret_p).ops of
  5282. 0:
  5283. taicpu(p).clearop(0);
  5284. 1:
  5285. taicpu(p).loadconst(0,taicpu(ret_p).oper[0]^.val);
  5286. else
  5287. internalerror(2016041301);
  5288. end;
  5289. { If the original label is now dead, it might turn out that the label
  5290. immediately follows p. As a result, everything beyond it, which will
  5291. be just some final register configuration and a RET instruction, is
  5292. now dead code. [Kit] }
  5293. { NOTE: This is much faster than introducing a OptPass2RET routine and
  5294. running RemoveDeadCodeAfterJump for each RET instruction, because
  5295. this optimisation rarely happens and most RETs appear at the end of
  5296. routines where there is nothing that can be stripped. [Kit] }
  5297. if not ThisLabel.is_used then
  5298. RemoveDeadCodeAfterJump(p);
  5299. end;
  5300. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  5301. var
  5302. hp1, hp2, hp3: tai;
  5303. OperIdx: Integer;
  5304. begin
  5305. result:=false;
  5306. 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
  5307. (taicpu(p).oper[0]^.ref^.index=NR_NO) and (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) then
  5308. begin
  5309. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  5310. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and (hp1.typ = ait_instruction) then
  5311. begin
  5312. case taicpu(hp1).opcode of
  5313. A_RET:
  5314. {
  5315. change
  5316. jmp .L1
  5317. ...
  5318. .L1:
  5319. ret
  5320. into
  5321. ret
  5322. }
  5323. begin
  5324. ConvertJumpToRET(p, hp1);
  5325. result:=true;
  5326. end;
  5327. A_MOV:
  5328. {
  5329. change
  5330. jmp .L1
  5331. ...
  5332. .L1:
  5333. mov ##, ##
  5334. ret
  5335. into
  5336. mov ##, ##
  5337. ret
  5338. }
  5339. { This optimisation tends to increase code size if the pass 1 MOV optimisations aren't
  5340. re-run, so only do this particular optimisation if optimising for speed or when
  5341. optimisations are very in-depth. [Kit] }
  5342. if (current_settings.optimizerswitches * [cs_opt_level3, cs_opt_size]) <> [cs_opt_size] then
  5343. begin
  5344. GetNextInstruction(hp1, hp2);
  5345. if not Assigned(hp2) then
  5346. Exit;
  5347. if (hp2.typ in [ait_label, ait_align]) then
  5348. SkipLabels(hp2,hp2);
  5349. if Assigned(hp2) and MatchInstruction(hp2, A_RET, [S_NO]) then
  5350. begin
  5351. { Duplicate the MOV instruction }
  5352. hp3:=tai(hp1.getcopy);
  5353. asml.InsertBefore(hp3, p);
  5354. { Make sure the compiler knows about any final registers written here }
  5355. for OperIdx := 0 to 1 do
  5356. with taicpu(hp3).oper[OperIdx]^ do
  5357. begin
  5358. case typ of
  5359. top_ref:
  5360. begin
  5361. if (ref^.base <> NR_NO) {$ifdef x86_64} and (ref^.base <> NR_RIP) {$endif x86_64} then
  5362. AllocRegBetween(ref^.base, hp3, tai(p.Next), UsedRegs);
  5363. if (ref^.index <> NR_NO) {$ifdef x86_64} and (ref^.index <> NR_RIP) {$endif x86_64} then
  5364. AllocRegBetween(ref^.index, hp3, tai(p.Next), UsedRegs);
  5365. end;
  5366. top_reg:
  5367. AllocRegBetween(reg, hp3, tai(p.Next), UsedRegs);
  5368. else
  5369. ;
  5370. end;
  5371. end;
  5372. { Now change the jump into a RET instruction }
  5373. ConvertJumpToRET(p, hp2);
  5374. result:=true;
  5375. end;
  5376. end;
  5377. else
  5378. ;
  5379. end;
  5380. end;
  5381. end;
  5382. end;
  5383. class function TX86AsmOptimizer.CanBeCMOV(p : tai) : boolean;
  5384. begin
  5385. CanBeCMOV:=assigned(p) and
  5386. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  5387. { we can't use cmov ref,reg because
  5388. ref could be nil and cmov still throws an exception
  5389. if ref=nil but the mov isn't done (FK)
  5390. or ((taicpu(p).oper[0]^.typ = top_ref) and
  5391. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  5392. }
  5393. (taicpu(p).oper[1]^.typ = top_reg) and
  5394. (
  5395. (taicpu(p).oper[0]^.typ = top_reg) or
  5396. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  5397. it is not expected that this can cause a seg. violation }
  5398. (
  5399. (taicpu(p).oper[0]^.typ = top_ref) and
  5400. IsRefSafe(taicpu(p).oper[0]^.ref)
  5401. )
  5402. );
  5403. end;
  5404. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  5405. var
  5406. hp1,hp2: tai;
  5407. {$ifndef i8086}
  5408. hp3,hp4,hpmov2, hp5: tai;
  5409. l : Longint;
  5410. condition : TAsmCond;
  5411. {$endif i8086}
  5412. carryadd_opcode : TAsmOp;
  5413. symbol: TAsmSymbol;
  5414. reg: tsuperregister;
  5415. increg, tmpreg: TRegister;
  5416. begin
  5417. result:=false;
  5418. if GetNextInstruction(p,hp1) and (hp1.typ=ait_instruction) then
  5419. begin
  5420. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  5421. if GetNextInstruction(hp1,hp2) and
  5422. (
  5423. (hp2.typ=ait_label) or
  5424. { trick to skip align }
  5425. ((hp2.typ=ait_align) and GetNextInstruction(hp2,hp2) and (hp2.typ=ait_label))
  5426. ) and
  5427. (Tasmlabel(symbol) = Tai_label(hp2).labsym) and
  5428. (
  5429. (
  5430. ((Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB)) and
  5431. MatchOptype(Taicpu(hp1),top_const,top_reg) and
  5432. (Taicpu(hp1).oper[0]^.val=1)
  5433. ) or
  5434. ((Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC))
  5435. ) then
  5436. { jb @@1 cmc
  5437. inc/dec operand --> adc/sbb operand,0
  5438. @@1:
  5439. ... and ...
  5440. jnb @@1
  5441. inc/dec operand --> adc/sbb operand,0
  5442. @@1: }
  5443. begin
  5444. if Taicpu(p).condition in [C_NAE,C_B,C_C] then
  5445. begin
  5446. case taicpu(hp1).opcode of
  5447. A_INC,
  5448. A_ADD:
  5449. carryadd_opcode:=A_ADC;
  5450. A_DEC,
  5451. A_SUB:
  5452. carryadd_opcode:=A_SBB;
  5453. else
  5454. InternalError(2021011001);
  5455. end;
  5456. Taicpu(p).clearop(0);
  5457. Taicpu(p).ops:=0;
  5458. Taicpu(p).is_jmp:=false;
  5459. Taicpu(p).opcode:=A_CMC;
  5460. Taicpu(p).condition:=C_NONE;
  5461. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2CmcAdc/Sbb',p);
  5462. Taicpu(hp1).ops:=2;
  5463. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  5464. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  5465. else
  5466. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  5467. Taicpu(hp1).loadconst(0,0);
  5468. Taicpu(hp1).opcode:=carryadd_opcode;
  5469. result:=true;
  5470. exit;
  5471. end
  5472. else if Taicpu(p).condition in [C_AE,C_NB,C_NC] then
  5473. begin
  5474. case taicpu(hp1).opcode of
  5475. A_INC,
  5476. A_ADD:
  5477. carryadd_opcode:=A_ADC;
  5478. A_DEC,
  5479. A_SUB:
  5480. carryadd_opcode:=A_SBB;
  5481. else
  5482. InternalError(2021011002);
  5483. end;
  5484. Taicpu(hp1).ops:=2;
  5485. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2Adc/Sbb',p);
  5486. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  5487. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  5488. else
  5489. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  5490. Taicpu(hp1).loadconst(0,0);
  5491. Taicpu(hp1).opcode:=carryadd_opcode;
  5492. RemoveCurrentP(p, hp1);
  5493. result:=true;
  5494. exit;
  5495. end
  5496. {
  5497. jcc @@1 setcc tmpreg
  5498. inc/dec/add/sub operand -> (movzx tmpreg)
  5499. @@1: add/sub tmpreg,operand
  5500. While this increases code size slightly, it makes the code much faster if the
  5501. jump is unpredictable
  5502. }
  5503. else if not(cs_opt_size in current_settings.optimizerswitches) then
  5504. begin
  5505. { search for an available register which is volatile }
  5506. for reg in tcpuregisterset do
  5507. begin
  5508. if
  5509. {$if defined(i386) or defined(i8086)}
  5510. { Only use registers whose lowest 8-bits can Be accessed }
  5511. (reg in [RS_EAX,RS_EBX,RS_ECX,RS_EDX]) and
  5512. {$endif i386 or i8086}
  5513. (reg in paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption)) and
  5514. not(reg in UsedRegs[R_INTREGISTER].GetUsedRegs)
  5515. { We don't need to check if tmpreg is in hp1 or not, because
  5516. it will be marked as in use at p (if not, this is
  5517. indictive of a compiler bug). }
  5518. then
  5519. begin
  5520. TAsmLabel(symbol).decrefs;
  5521. increg := newreg(R_INTREGISTER,reg,R_SUBL);
  5522. Taicpu(p).clearop(0);
  5523. Taicpu(p).ops:=1;
  5524. Taicpu(p).is_jmp:=false;
  5525. Taicpu(p).opcode:=A_SETcc;
  5526. DebugMsg(SPeepholeOptimization+'JccAdd2SetccAdd',p);
  5527. Taicpu(p).condition:=inverse_cond(Taicpu(p).condition);
  5528. Taicpu(p).loadreg(0,increg);
  5529. if getsubreg(Taicpu(hp1).oper[1]^.reg)<>R_SUBL then
  5530. begin
  5531. case getsubreg(Taicpu(hp1).oper[1]^.reg) of
  5532. R_SUBW:
  5533. begin
  5534. tmpreg := newreg(R_INTREGISTER,reg,R_SUBW);
  5535. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BW,increg,tmpreg);
  5536. end;
  5537. R_SUBD:
  5538. begin
  5539. tmpreg := newreg(R_INTREGISTER,reg,R_SUBD);
  5540. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,tmpreg);
  5541. end;
  5542. {$ifdef x86_64}
  5543. R_SUBQ:
  5544. begin
  5545. { MOVZX doesn't have a 64-bit variant, because
  5546. the 32-bit version implicitly zeroes the
  5547. upper 32-bits of the destination register }
  5548. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,
  5549. newreg(R_INTREGISTER,reg,R_SUBD));
  5550. tmpreg := newreg(R_INTREGISTER,reg,R_SUBQ);
  5551. end;
  5552. {$endif x86_64}
  5553. else
  5554. Internalerror(2020030601);
  5555. end;
  5556. taicpu(hp2).fileinfo:=taicpu(hp1).fileinfo;
  5557. asml.InsertAfter(hp2,p);
  5558. end
  5559. else
  5560. tmpreg := increg;
  5561. if (Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC) then
  5562. begin
  5563. Taicpu(hp1).ops:=2;
  5564. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^)
  5565. end;
  5566. Taicpu(hp1).loadreg(0,tmpreg);
  5567. AllocRegBetween(tmpreg,p,hp1,UsedRegs);
  5568. Result := True;
  5569. { p is no longer a Jcc instruction, so exit }
  5570. Exit;
  5571. end;
  5572. end;
  5573. end;
  5574. end;
  5575. { Detect the following:
  5576. jmp<cond> @Lbl1
  5577. jmp @Lbl2
  5578. ...
  5579. @Lbl1:
  5580. ret
  5581. Change to:
  5582. jmp<inv_cond> @Lbl2
  5583. ret
  5584. }
  5585. if MatchInstruction(hp1,A_JMP,[]) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  5586. begin
  5587. hp2:=getlabelwithsym(TAsmLabel(symbol));
  5588. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  5589. MatchInstruction(hp2,A_RET,[S_NO]) then
  5590. begin
  5591. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  5592. { Change label address to that of the unconditional jump }
  5593. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  5594. TAsmLabel(symbol).DecRefs;
  5595. taicpu(hp1).opcode := A_RET;
  5596. taicpu(hp1).is_jmp := false;
  5597. taicpu(hp1).ops := taicpu(hp2).ops;
  5598. DebugMsg(SPeepholeOptimization+'JccJmpRet2J!ccRet',p);
  5599. case taicpu(hp2).ops of
  5600. 0:
  5601. taicpu(hp1).clearop(0);
  5602. 1:
  5603. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  5604. else
  5605. internalerror(2016041302);
  5606. end;
  5607. end;
  5608. {$ifndef i8086}
  5609. end
  5610. {
  5611. convert
  5612. j<c> .L1
  5613. mov 1,reg
  5614. jmp .L2
  5615. .L1
  5616. mov 0,reg
  5617. .L2
  5618. into
  5619. mov 0,reg
  5620. set<not(c)> reg
  5621. take care of alignment and that the mov 0,reg is not converted into a xor as this
  5622. would destroy the flag contents
  5623. }
  5624. else if MatchInstruction(hp1,A_MOV,[]) and
  5625. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5626. {$ifdef i386}
  5627. (
  5628. { Under i386, ESI, EDI, EBP and ESP
  5629. don't have an 8-bit representation }
  5630. not (getsupreg(taicpu(hp1).oper[1]^.reg) in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  5631. ) and
  5632. {$endif i386}
  5633. (taicpu(hp1).oper[0]^.val=1) and
  5634. GetNextInstruction(hp1,hp2) and
  5635. MatchInstruction(hp2,A_JMP,[]) and (taicpu(hp2).oper[0]^.ref^.refaddr=addr_full) and
  5636. GetNextInstruction(hp2,hp3) and
  5637. { skip align }
  5638. ((hp3.typ<>ait_align) or GetNextInstruction(hp3,hp3)) and
  5639. (hp3.typ=ait_label) and
  5640. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol)=tai_label(hp3).labsym) and
  5641. (tai_label(hp3).labsym.getrefs=1) and
  5642. GetNextInstruction(hp3,hp4) and
  5643. MatchInstruction(hp4,A_MOV,[]) and
  5644. MatchOpType(taicpu(hp4),top_const,top_reg) and
  5645. (taicpu(hp4).oper[0]^.val=0) and
  5646. MatchOperand(taicpu(hp1).oper[1]^,taicpu(hp4).oper[1]^) and
  5647. GetNextInstruction(hp4,hp5) and
  5648. (hp5.typ=ait_label) and
  5649. (tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol)=tai_label(hp5).labsym) and
  5650. (tai_label(hp5).labsym.getrefs=1) then
  5651. begin
  5652. AllocRegBetween(NR_FLAGS,p,hp4,UsedRegs);
  5653. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2MovSetcc',p);
  5654. { remove last label }
  5655. RemoveInstruction(hp5);
  5656. { remove second label }
  5657. RemoveInstruction(hp3);
  5658. { if align is present remove it }
  5659. if GetNextInstruction(hp2,hp3) and (hp3.typ=ait_align) then
  5660. RemoveInstruction(hp3);
  5661. { remove jmp }
  5662. RemoveInstruction(hp2);
  5663. if taicpu(hp1).opsize=S_B then
  5664. RemoveInstruction(hp1)
  5665. else
  5666. taicpu(hp1).loadconst(0,0);
  5667. taicpu(hp4).opcode:=A_SETcc;
  5668. taicpu(hp4).opsize:=S_B;
  5669. taicpu(hp4).condition:=inverse_cond(taicpu(p).condition);
  5670. taicpu(hp4).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(hp4).oper[1]^.reg),R_SUBL));
  5671. taicpu(hp4).opercnt:=1;
  5672. taicpu(hp4).ops:=1;
  5673. taicpu(hp4).freeop(1);
  5674. RemoveCurrentP(p);
  5675. Result:=true;
  5676. exit;
  5677. end
  5678. else if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  5679. begin
  5680. { check for
  5681. jCC xxx
  5682. <several movs>
  5683. xxx:
  5684. }
  5685. l:=0;
  5686. while assigned(hp1) and
  5687. CanBeCMOV(hp1) and
  5688. { stop on labels }
  5689. not(hp1.typ=ait_label) do
  5690. begin
  5691. inc(l);
  5692. GetNextInstruction(hp1,hp1);
  5693. end;
  5694. if assigned(hp1) then
  5695. begin
  5696. if FindLabel(tasmlabel(symbol),hp1) then
  5697. begin
  5698. if (l<=4) and (l>0) then
  5699. begin
  5700. condition:=inverse_cond(taicpu(p).condition);
  5701. GetNextInstruction(p,hp1);
  5702. repeat
  5703. if not Assigned(hp1) then
  5704. InternalError(2018062900);
  5705. taicpu(hp1).opcode:=A_CMOVcc;
  5706. taicpu(hp1).condition:=condition;
  5707. UpdateUsedRegs(hp1);
  5708. GetNextInstruction(hp1,hp1);
  5709. until not(CanBeCMOV(hp1));
  5710. { Remember what hp1 is in case there's multiple aligns to get rid of }
  5711. hp2 := hp1;
  5712. repeat
  5713. if not Assigned(hp2) then
  5714. InternalError(2018062910);
  5715. case hp2.typ of
  5716. ait_label:
  5717. { What we expected - break out of the loop (it won't be a dead label at the top of
  5718. a cluster because that was optimised at an earlier stage) }
  5719. Break;
  5720. ait_align:
  5721. { Go to the next entry until a label is found (may be multiple aligns before it) }
  5722. begin
  5723. hp2 := tai(hp2.Next);
  5724. Continue;
  5725. end;
  5726. else
  5727. begin
  5728. { Might be a comment or temporary allocation entry }
  5729. if not (hp2.typ in SkipInstr) then
  5730. InternalError(2018062911);
  5731. hp2 := tai(hp2.Next);
  5732. Continue;
  5733. end;
  5734. end;
  5735. until False;
  5736. { Now we can safely decrement the reference count }
  5737. tasmlabel(symbol).decrefs;
  5738. DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
  5739. { Remove the original jump }
  5740. RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
  5741. GetNextInstruction(hp2, p); { Instruction after the label }
  5742. { Remove the label if this is its final reference }
  5743. if (tasmlabel(symbol).getrefs=0) then
  5744. StripLabelFast(hp1);
  5745. if Assigned(p) then
  5746. begin
  5747. UpdateUsedRegs(p);
  5748. result:=true;
  5749. end;
  5750. exit;
  5751. end;
  5752. end
  5753. else
  5754. begin
  5755. { check further for
  5756. jCC xxx
  5757. <several movs 1>
  5758. jmp yyy
  5759. xxx:
  5760. <several movs 2>
  5761. yyy:
  5762. }
  5763. { hp2 points to jmp yyy }
  5764. hp2:=hp1;
  5765. { skip hp1 to xxx (or an align right before it) }
  5766. GetNextInstruction(hp1, hp1);
  5767. if assigned(hp2) and
  5768. assigned(hp1) and
  5769. (l<=3) and
  5770. (hp2.typ=ait_instruction) and
  5771. (taicpu(hp2).is_jmp) and
  5772. (taicpu(hp2).condition=C_None) and
  5773. { real label and jump, no further references to the
  5774. label are allowed }
  5775. (tasmlabel(symbol).getrefs=1) and
  5776. FindLabel(tasmlabel(symbol),hp1) then
  5777. begin
  5778. l:=0;
  5779. { skip hp1 to <several moves 2> }
  5780. if (hp1.typ = ait_align) then
  5781. GetNextInstruction(hp1, hp1);
  5782. GetNextInstruction(hp1, hpmov2);
  5783. hp1 := hpmov2;
  5784. while assigned(hp1) and
  5785. CanBeCMOV(hp1) do
  5786. begin
  5787. inc(l);
  5788. GetNextInstruction(hp1, hp1);
  5789. end;
  5790. { hp1 points to yyy (or an align right before it) }
  5791. hp3 := hp1;
  5792. if assigned(hp1) and
  5793. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  5794. begin
  5795. condition:=inverse_cond(taicpu(p).condition);
  5796. GetNextInstruction(p,hp1);
  5797. repeat
  5798. taicpu(hp1).opcode:=A_CMOVcc;
  5799. taicpu(hp1).condition:=condition;
  5800. UpdateUsedRegs(hp1);
  5801. GetNextInstruction(hp1,hp1);
  5802. until not(assigned(hp1)) or
  5803. not(CanBeCMOV(hp1));
  5804. condition:=inverse_cond(condition);
  5805. hp1 := hpmov2;
  5806. { hp1 is now at <several movs 2> }
  5807. while Assigned(hp1) and CanBeCMOV(hp1) do
  5808. begin
  5809. taicpu(hp1).opcode:=A_CMOVcc;
  5810. taicpu(hp1).condition:=condition;
  5811. UpdateUsedRegs(hp1);
  5812. GetNextInstruction(hp1,hp1);
  5813. end;
  5814. hp1 := p;
  5815. { Get first instruction after label }
  5816. GetNextInstruction(hp3, p);
  5817. if assigned(p) and (hp3.typ = ait_align) then
  5818. GetNextInstruction(p, p);
  5819. { Don't dereference yet, as doing so will cause
  5820. GetNextInstruction to skip the label and
  5821. optional align marker. [Kit] }
  5822. GetNextInstruction(hp2, hp4);
  5823. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
  5824. { remove jCC }
  5825. RemoveInstruction(hp1);
  5826. { Now we can safely decrement it }
  5827. tasmlabel(symbol).decrefs;
  5828. { Remove label xxx (it will have a ref of zero due to the initial check }
  5829. StripLabelFast(hp4);
  5830. { remove jmp }
  5831. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  5832. RemoveInstruction(hp2);
  5833. { As before, now we can safely decrement it }
  5834. tasmlabel(symbol).decrefs;
  5835. { Remove label yyy (and the optional alignment) if its reference falls to zero }
  5836. if tasmlabel(symbol).getrefs = 0 then
  5837. StripLabelFast(hp3);
  5838. if Assigned(p) then
  5839. begin
  5840. UpdateUsedRegs(p);
  5841. result:=true;
  5842. end;
  5843. exit;
  5844. end;
  5845. end;
  5846. end;
  5847. end;
  5848. {$endif i8086}
  5849. end;
  5850. end;
  5851. end;
  5852. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  5853. var
  5854. hp1,hp2: tai;
  5855. reg_and_hp1_is_instr: Boolean;
  5856. begin
  5857. result:=false;
  5858. reg_and_hp1_is_instr:=(taicpu(p).oper[1]^.typ = top_reg) and
  5859. GetNextInstruction(p,hp1) and
  5860. (hp1.typ = ait_instruction);
  5861. if reg_and_hp1_is_instr and
  5862. (
  5863. (taicpu(hp1).opcode <> A_LEA) or
  5864. { If the LEA instruction can be converted into an arithmetic instruction,
  5865. it may be possible to then fold it. }
  5866. (
  5867. { If the flags register is in use, don't change the instruction
  5868. to an ADD otherwise this will scramble the flags. [Kit] }
  5869. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  5870. ConvertLEA(taicpu(hp1))
  5871. )
  5872. ) and
  5873. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  5874. GetNextInstruction(hp1,hp2) and
  5875. MatchInstruction(hp2,A_MOV,[]) and
  5876. (taicpu(hp2).oper[0]^.typ = top_reg) and
  5877. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  5878. ((taicpu(p).opsize in [S_BW,S_BL]) and (taicpu(hp2).opsize=S_B) or
  5879. (taicpu(p).opsize in [S_WL]) and (taicpu(hp2).opsize=S_W)) and
  5880. {$ifdef i386}
  5881. { not all registers have byte size sub registers on i386 }
  5882. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  5883. {$endif i386}
  5884. (((taicpu(hp1).ops=2) and
  5885. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  5886. ((taicpu(hp1).ops=1) and
  5887. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  5888. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  5889. begin
  5890. { change movsX/movzX reg/ref, reg2
  5891. add/sub/or/... reg3/$const, reg2
  5892. mov reg2 reg/ref
  5893. to add/sub/or/... reg3/$const, reg/ref }
  5894. { by example:
  5895. movswl %si,%eax movswl %si,%eax p
  5896. decl %eax addl %edx,%eax hp1
  5897. movw %ax,%si movw %ax,%si hp2
  5898. ->
  5899. movswl %si,%eax movswl %si,%eax p
  5900. decw %eax addw %edx,%eax hp1
  5901. movw %ax,%si movw %ax,%si hp2
  5902. }
  5903. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  5904. {
  5905. ->
  5906. movswl %si,%eax movswl %si,%eax p
  5907. decw %si addw %dx,%si hp1
  5908. movw %ax,%si movw %ax,%si hp2
  5909. }
  5910. case taicpu(hp1).ops of
  5911. 1:
  5912. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  5913. 2:
  5914. begin
  5915. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  5916. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  5917. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  5918. end;
  5919. else
  5920. internalerror(2008042702);
  5921. end;
  5922. {
  5923. ->
  5924. decw %si addw %dx,%si p
  5925. }
  5926. DebugMsg(SPeepholeOptimization + 'var3',p);
  5927. RemoveCurrentP(p, hp1);
  5928. RemoveInstruction(hp2);
  5929. end
  5930. else if reg_and_hp1_is_instr and
  5931. (taicpu(hp1).opcode = A_MOV) and
  5932. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  5933. (MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^)
  5934. {$ifdef x86_64}
  5935. { check for implicit extension to 64 bit }
  5936. or
  5937. ((taicpu(p).opsize in [S_BL,S_WL]) and
  5938. (taicpu(hp1).opsize=S_Q) and
  5939. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg)
  5940. )
  5941. {$endif x86_64}
  5942. )
  5943. then
  5944. begin
  5945. { change
  5946. movx %reg1,%reg2
  5947. mov %reg2,%reg3
  5948. dealloc %reg2
  5949. into
  5950. movx %reg,%reg3
  5951. }
  5952. TransferUsedRegs(TmpUsedRegs);
  5953. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5954. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  5955. begin
  5956. DebugMsg(SPeepholeOptimization + 'MovxMov2Movx',p);
  5957. {$ifdef x86_64}
  5958. if (taicpu(p).opsize in [S_BL,S_WL]) and
  5959. (taicpu(hp1).opsize=S_Q) then
  5960. taicpu(p).loadreg(1,newreg(R_INTREGISTER,getsupreg(taicpu(hp1).oper[1]^.reg),R_SUBD))
  5961. else
  5962. {$endif x86_64}
  5963. taicpu(p).loadreg(1,taicpu(hp1).oper[1]^.reg);
  5964. RemoveInstruction(hp1);
  5965. end;
  5966. end
  5967. else if reg_and_hp1_is_instr and
  5968. (taicpu(hp1).opcode = A_MOV) and
  5969. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  5970. (((taicpu(p).opsize in [S_BW,S_BL,S_WL{$ifdef x86_64},S_BQ,S_WQ,S_LQ{$endif x86_64}]) and
  5971. (taicpu(hp1).opsize=S_B)) or
  5972. ((taicpu(p).opsize in [S_WL{$ifdef x86_64},S_WQ,S_LQ{$endif x86_64}]) and
  5973. (taicpu(hp1).opsize=S_W))
  5974. {$ifdef x86_64}
  5975. or ((taicpu(p).opsize=S_LQ) and
  5976. (taicpu(hp1).opsize=S_L))
  5977. {$endif x86_64}
  5978. ) and
  5979. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg) then
  5980. begin
  5981. { change
  5982. movx %reg1,%reg2
  5983. mov %reg2,%reg3
  5984. dealloc %reg2
  5985. into
  5986. mov %reg1,%reg3
  5987. if the second mov accesses only the bits stored in reg1
  5988. }
  5989. TransferUsedRegs(TmpUsedRegs);
  5990. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  5991. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  5992. begin
  5993. DebugMsg(SPeepholeOptimization + 'MovxMov2Mov',p);
  5994. if taicpu(p).oper[0]^.typ=top_reg then
  5995. begin
  5996. case taicpu(hp1).opsize of
  5997. S_B:
  5998. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBL));
  5999. S_W:
  6000. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBW));
  6001. S_L:
  6002. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBD));
  6003. else
  6004. Internalerror(2020102301);
  6005. end;
  6006. AllocRegBetween(taicpu(hp1).oper[0]^.reg,p,hp1,UsedRegs);
  6007. end
  6008. else
  6009. taicpu(hp1).loadref(0,taicpu(p).oper[0]^.ref^);
  6010. RemoveCurrentP(p);
  6011. result:=true;
  6012. exit;
  6013. end;
  6014. end
  6015. else if reg_and_hp1_is_instr and
  6016. (taicpu(p).oper[0]^.typ = top_reg) and
  6017. (
  6018. (taicpu(hp1).opcode = A_SHL) or (taicpu(hp1).opcode = A_SAL)
  6019. ) and
  6020. (taicpu(hp1).oper[0]^.typ = top_const) and
  6021. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  6022. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  6023. { Minimum shift value allowed is the bit difference between the sizes }
  6024. (taicpu(hp1).oper[0]^.val >=
  6025. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  6026. 8 * (
  6027. tcgsize2size[reg_cgsize(taicpu(p).oper[1]^.reg)] -
  6028. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  6029. )
  6030. ) then
  6031. begin
  6032. { For:
  6033. movsx/movzx %reg1,%reg1 (same register, just different sizes)
  6034. shl/sal ##, %reg1
  6035. Remove the movsx/movzx instruction if the shift overwrites the
  6036. extended bits of the register (e.g. movslq %eax,%rax; shlq $32,%rax
  6037. }
  6038. DebugMsg(SPeepholeOptimization + 'MovxShl2Shl',p);
  6039. RemoveCurrentP(p, hp1);
  6040. Result := True;
  6041. Exit;
  6042. end
  6043. else if reg_and_hp1_is_instr and
  6044. (taicpu(p).oper[0]^.typ = top_reg) and
  6045. (
  6046. ((taicpu(hp1).opcode = A_SHR) and (taicpu(p).opcode = A_MOVZX)) or
  6047. ((taicpu(hp1).opcode = A_SAR) and (taicpu(p).opcode <> A_MOVZX))
  6048. ) and
  6049. (taicpu(hp1).oper[0]^.typ = top_const) and
  6050. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  6051. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  6052. { Minimum shift value allowed is the bit size of the smallest register - 1 }
  6053. (taicpu(hp1).oper[0]^.val <
  6054. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  6055. 8 * (
  6056. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  6057. )
  6058. ) then
  6059. begin
  6060. { For:
  6061. movsx %reg1,%reg1 movzx %reg1,%reg1 (same register, just different sizes)
  6062. sar ##, %reg1 shr ##, %reg1
  6063. Move the shift to before the movx instruction if the shift value
  6064. is not too large.
  6065. }
  6066. asml.Remove(hp1);
  6067. asml.InsertBefore(hp1, p);
  6068. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  6069. case taicpu(p).opsize of
  6070. s_BW, S_BL{$ifdef x86_64}, S_BQ{$endif}:
  6071. taicpu(hp1).opsize := S_B;
  6072. S_WL{$ifdef x86_64}, S_WQ{$endif}:
  6073. taicpu(hp1).opsize := S_W;
  6074. {$ifdef x86_64}
  6075. S_LQ:
  6076. taicpu(hp1).opsize := S_L;
  6077. {$endif}
  6078. else
  6079. InternalError(2020112401);
  6080. end;
  6081. if (taicpu(hp1).opcode = A_SHR) then
  6082. DebugMsg(SPeepholeOptimization + 'MovzShr2ShrMovz', hp1)
  6083. else
  6084. DebugMsg(SPeepholeOptimization + 'MovsSar2SarMovs', hp1);
  6085. Result := True;
  6086. end
  6087. else if taicpu(p).opcode=A_MOVZX then
  6088. begin
  6089. { removes superfluous And's after movzx's }
  6090. if reg_and_hp1_is_instr and
  6091. (taicpu(hp1).opcode = A_AND) and
  6092. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6093. ((taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)
  6094. {$ifdef x86_64}
  6095. { check for implicit extension to 64 bit }
  6096. or
  6097. ((taicpu(p).opsize in [S_BL,S_WL]) and
  6098. (taicpu(hp1).opsize=S_Q) and
  6099. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg)
  6100. )
  6101. {$endif x86_64}
  6102. )
  6103. then
  6104. begin
  6105. case taicpu(p).opsize Of
  6106. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  6107. if (taicpu(hp1).oper[0]^.val = $ff) then
  6108. begin
  6109. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz1',p);
  6110. RemoveInstruction(hp1);
  6111. Result:=true;
  6112. exit;
  6113. end;
  6114. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  6115. if (taicpu(hp1).oper[0]^.val = $ffff) then
  6116. begin
  6117. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz2',p);
  6118. RemoveInstruction(hp1);
  6119. Result:=true;
  6120. exit;
  6121. end;
  6122. {$ifdef x86_64}
  6123. S_LQ:
  6124. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  6125. begin
  6126. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz3',p);
  6127. RemoveInstruction(hp1);
  6128. Result:=true;
  6129. exit;
  6130. end;
  6131. {$endif x86_64}
  6132. else
  6133. ;
  6134. end;
  6135. { we cannot get rid of the and, but can we get rid of the movz ?}
  6136. if SuperRegistersEqual(taicpu(p).oper[0]^.reg,taicpu(p).oper[1]^.reg) then
  6137. begin
  6138. case taicpu(p).opsize Of
  6139. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  6140. if (taicpu(hp1).oper[0]^.val and $ff)=taicpu(hp1).oper[0]^.val then
  6141. begin
  6142. DebugMsg(SPeepholeOptimization + 'MovzAnd2And1',p);
  6143. RemoveCurrentP(p,hp1);
  6144. Result:=true;
  6145. exit;
  6146. end;
  6147. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  6148. if (taicpu(hp1).oper[0]^.val and $ffff)=taicpu(hp1).oper[0]^.val then
  6149. begin
  6150. DebugMsg(SPeepholeOptimization + 'MovzAnd2And2',p);
  6151. RemoveCurrentP(p,hp1);
  6152. Result:=true;
  6153. exit;
  6154. end;
  6155. {$ifdef x86_64}
  6156. S_LQ:
  6157. if (taicpu(hp1).oper[0]^.val and $ffffffff)=taicpu(hp1).oper[0]^.val then
  6158. begin
  6159. DebugMsg(SPeepholeOptimization + 'MovzAnd2And3',p);
  6160. RemoveCurrentP(p,hp1);
  6161. Result:=true;
  6162. exit;
  6163. end;
  6164. {$endif x86_64}
  6165. else
  6166. ;
  6167. end;
  6168. end;
  6169. end;
  6170. { changes some movzx constructs to faster synonyms (all examples
  6171. are given with eax/ax, but are also valid for other registers)}
  6172. if MatchOpType(taicpu(p),top_reg,top_reg) then
  6173. begin
  6174. case taicpu(p).opsize of
  6175. { Technically, movzbw %al,%ax cannot be encoded in 32/64-bit mode
  6176. (the machine code is equivalent to movzbl %al,%eax), but the
  6177. code generator still generates that assembler instruction and
  6178. it is silently converted. This should probably be checked.
  6179. [Kit] }
  6180. S_BW:
  6181. begin
  6182. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  6183. (
  6184. not IsMOVZXAcceptable
  6185. { and $0xff,%ax has a smaller encoding but risks a partial write penalty }
  6186. or (
  6187. (cs_opt_size in current_settings.optimizerswitches) and
  6188. (taicpu(p).oper[1]^.reg = NR_AX)
  6189. )
  6190. ) then
  6191. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  6192. begin
  6193. DebugMsg(SPeepholeOptimization + 'var7',p);
  6194. taicpu(p).opcode := A_AND;
  6195. taicpu(p).changeopsize(S_W);
  6196. taicpu(p).loadConst(0,$ff);
  6197. Result := True;
  6198. end
  6199. else if not IsMOVZXAcceptable and
  6200. GetNextInstruction(p, hp1) and
  6201. (tai(hp1).typ = ait_instruction) and
  6202. (taicpu(hp1).opcode = A_AND) and
  6203. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6204. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6205. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  6206. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  6207. begin
  6208. DebugMsg(SPeepholeOptimization + 'var8',p);
  6209. taicpu(p).opcode := A_MOV;
  6210. taicpu(p).changeopsize(S_W);
  6211. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  6212. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6213. Result := True;
  6214. end;
  6215. end;
  6216. {$ifndef i8086} { movzbl %al,%eax cannot be encoded in 16-bit mode (the machine code is equivalent to movzbw %al,%ax }
  6217. S_BL:
  6218. begin
  6219. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  6220. (
  6221. not IsMOVZXAcceptable
  6222. { and $0xff,%eax has a smaller encoding but risks a partial write penalty }
  6223. or (
  6224. (cs_opt_size in current_settings.optimizerswitches) and
  6225. (taicpu(p).oper[1]^.reg = NR_EAX)
  6226. )
  6227. ) then
  6228. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  6229. begin
  6230. DebugMsg(SPeepholeOptimization + 'var9',p);
  6231. taicpu(p).opcode := A_AND;
  6232. taicpu(p).changeopsize(S_L);
  6233. taicpu(p).loadConst(0,$ff);
  6234. Result := True;
  6235. end
  6236. else if not IsMOVZXAcceptable and
  6237. GetNextInstruction(p, hp1) and
  6238. (tai(hp1).typ = ait_instruction) and
  6239. (taicpu(hp1).opcode = A_AND) and
  6240. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6241. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6242. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  6243. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  6244. begin
  6245. DebugMsg(SPeepholeOptimization + 'var10',p);
  6246. taicpu(p).opcode := A_MOV;
  6247. taicpu(p).changeopsize(S_L);
  6248. { do not use R_SUBWHOLE
  6249. as movl %rdx,%eax
  6250. is invalid in assembler PM }
  6251. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  6252. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6253. Result := True;
  6254. end;
  6255. end;
  6256. {$endif i8086}
  6257. S_WL:
  6258. if not IsMOVZXAcceptable then
  6259. begin
  6260. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) then
  6261. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  6262. begin
  6263. DebugMsg(SPeepholeOptimization + 'var11',p);
  6264. taicpu(p).opcode := A_AND;
  6265. taicpu(p).changeopsize(S_L);
  6266. taicpu(p).loadConst(0,$ffff);
  6267. Result := True;
  6268. end
  6269. else if GetNextInstruction(p, hp1) and
  6270. (tai(hp1).typ = ait_instruction) and
  6271. (taicpu(hp1).opcode = A_AND) and
  6272. (taicpu(hp1).oper[0]^.typ = top_const) and
  6273. (taicpu(hp1).oper[1]^.typ = top_reg) and
  6274. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6275. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  6276. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  6277. begin
  6278. DebugMsg(SPeepholeOptimization + 'var12',p);
  6279. taicpu(p).opcode := A_MOV;
  6280. taicpu(p).changeopsize(S_L);
  6281. { do not use R_SUBWHOLE
  6282. as movl %rdx,%eax
  6283. is invalid in assembler PM }
  6284. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  6285. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  6286. Result := True;
  6287. end;
  6288. end;
  6289. else
  6290. InternalError(2017050705);
  6291. end;
  6292. end
  6293. else if not IsMOVZXAcceptable and (taicpu(p).oper[0]^.typ = top_ref) then
  6294. begin
  6295. if GetNextInstruction(p, hp1) and
  6296. (tai(hp1).typ = ait_instruction) and
  6297. (taicpu(hp1).opcode = A_AND) and
  6298. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6299. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6300. begin
  6301. //taicpu(p).opcode := A_MOV;
  6302. case taicpu(p).opsize Of
  6303. S_BL:
  6304. begin
  6305. DebugMsg(SPeepholeOptimization + 'var13',p);
  6306. taicpu(hp1).changeopsize(S_L);
  6307. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6308. end;
  6309. S_WL:
  6310. begin
  6311. DebugMsg(SPeepholeOptimization + 'var14',p);
  6312. taicpu(hp1).changeopsize(S_L);
  6313. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  6314. end;
  6315. S_BW:
  6316. begin
  6317. DebugMsg(SPeepholeOptimization + 'var15',p);
  6318. taicpu(hp1).changeopsize(S_W);
  6319. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6320. end;
  6321. else
  6322. Internalerror(2017050704)
  6323. end;
  6324. Result := True;
  6325. end;
  6326. end;
  6327. end;
  6328. end;
  6329. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  6330. var
  6331. hp1, hp2 : tai;
  6332. MaskLength : Cardinal;
  6333. MaskedBits : TCgInt;
  6334. begin
  6335. Result:=false;
  6336. { There are no optimisations for reference targets }
  6337. if (taicpu(p).oper[1]^.typ <> top_reg) then
  6338. Exit;
  6339. while GetNextInstruction(p, hp1) and
  6340. (hp1.typ = ait_instruction) do
  6341. begin
  6342. if (taicpu(p).oper[0]^.typ = top_const) then
  6343. begin
  6344. if (taicpu(hp1).opcode = A_AND) and
  6345. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6346. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  6347. { the second register must contain the first one, so compare their subreg types }
  6348. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  6349. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  6350. { change
  6351. and const1, reg
  6352. and const2, reg
  6353. to
  6354. and (const1 and const2), reg
  6355. }
  6356. begin
  6357. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  6358. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  6359. RemoveCurrentP(p, hp1);
  6360. Result:=true;
  6361. exit;
  6362. end
  6363. else if (taicpu(hp1).opcode = A_MOVZX) and
  6364. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  6365. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg) and
  6366. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  6367. (((taicpu(p).opsize=S_W) and
  6368. (taicpu(hp1).opsize=S_BW)) or
  6369. ((taicpu(p).opsize=S_L) and
  6370. (taicpu(hp1).opsize in [S_WL,S_BL{$ifdef x86_64},S_BQ,S_WQ{$endif x86_64}]))
  6371. {$ifdef x86_64}
  6372. or
  6373. ((taicpu(p).opsize=S_Q) and
  6374. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_BL,S_WL]))
  6375. {$endif x86_64}
  6376. ) then
  6377. begin
  6378. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  6379. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  6380. ) or
  6381. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  6382. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  6383. then
  6384. begin
  6385. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  6386. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  6387. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  6388. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  6389. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  6390. }
  6391. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  6392. RemoveInstruction(hp1);
  6393. { See if there are other optimisations possible }
  6394. Continue;
  6395. end;
  6396. end
  6397. else if (taicpu(hp1).opcode = A_SHL) and
  6398. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6399. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  6400. begin
  6401. {$ifopt R+}
  6402. {$define RANGE_WAS_ON}
  6403. {$R-}
  6404. {$endif}
  6405. { get length of potential and mask }
  6406. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  6407. { really a mask? }
  6408. {$ifdef RANGE_WAS_ON}
  6409. {$R+}
  6410. {$endif}
  6411. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  6412. { unmasked part shifted out? }
  6413. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  6414. begin
  6415. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  6416. RemoveCurrentP(p, hp1);
  6417. Result:=true;
  6418. exit;
  6419. end;
  6420. end
  6421. else if (taicpu(hp1).opcode = A_SHR) and
  6422. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6423. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
  6424. (taicpu(hp1).oper[0]^.val <= 63) then
  6425. begin
  6426. { Does SHR combined with the AND cover all the bits?
  6427. e.g. for "andb $252,%reg; shrb $2,%reg" - the "and" can be removed }
  6428. MaskedBits := taicpu(p).oper[0]^.val or ((TCgInt(1) shl taicpu(hp1).oper[0]^.val) - 1);
  6429. if ((taicpu(p).opsize = S_B) and ((MaskedBits and $FF) = $FF)) or
  6430. ((taicpu(p).opsize = S_W) and ((MaskedBits and $FFFF) = $FFFF)) or
  6431. ((taicpu(p).opsize = S_L) and ((MaskedBits and $FFFFFFFF) = $FFFFFFFF)) then
  6432. begin
  6433. DebugMsg(SPeepholeOptimization + 'AndShrToShr done', p);
  6434. RemoveCurrentP(p, hp1);
  6435. Result := True;
  6436. Exit;
  6437. end;
  6438. end
  6439. else if ((taicpu(hp1).opcode = A_MOVSX){$ifdef x86_64} or (taicpu(hp1).opcode = A_MOVSXD){$endif x86_64}) and
  6440. (taicpu(hp1).oper[0]^.typ = top_reg) and
  6441. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg) then
  6442. begin
  6443. if SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  6444. (
  6445. (
  6446. (taicpu(hp1).opsize in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  6447. ((taicpu(p).oper[0]^.val and $7F) = taicpu(p).oper[0]^.val)
  6448. ) or (
  6449. (taicpu(hp1).opsize in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  6450. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val)
  6451. {$ifdef x86_64}
  6452. ) or (
  6453. (taicpu(hp1).opsize = S_LQ) and
  6454. ((taicpu(p).oper[0]^.val and $7fffffff) = taicpu(p).oper[0]^.val)
  6455. {$endif x86_64}
  6456. )
  6457. ) then
  6458. begin
  6459. if (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg){$ifdef x86_64} or (taicpu(hp1).opsize = S_LQ){$endif x86_64} then
  6460. begin
  6461. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  6462. RemoveInstruction(hp1);
  6463. { See if there are other optimisations possible }
  6464. Continue;
  6465. end;
  6466. { The super-registers are the same though.
  6467. Note that this change by itself doesn't improve
  6468. code speed, but it opens up other optimisations. }
  6469. {$ifdef x86_64}
  6470. { Convert 64-bit register to 32-bit }
  6471. case taicpu(hp1).opsize of
  6472. S_BQ:
  6473. begin
  6474. taicpu(hp1).opsize := S_BL;
  6475. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  6476. end;
  6477. S_WQ:
  6478. begin
  6479. taicpu(hp1).opsize := S_WL;
  6480. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  6481. end
  6482. else
  6483. ;
  6484. end;
  6485. {$endif x86_64}
  6486. DebugMsg(SPeepholeOptimization + 'AndMovsxToAndMovzx', hp1);
  6487. taicpu(hp1).opcode := A_MOVZX;
  6488. { See if there are other optimisations possible }
  6489. Continue;
  6490. end;
  6491. end;
  6492. end;
  6493. if (taicpu(hp1).is_jmp) and
  6494. (taicpu(hp1).opcode<>A_JMP) and
  6495. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  6496. begin
  6497. { change
  6498. and x, reg
  6499. jxx
  6500. to
  6501. test x, reg
  6502. jxx
  6503. if reg is deallocated before the
  6504. jump, but only if it's a conditional jump (PFV)
  6505. }
  6506. taicpu(p).opcode := A_TEST;
  6507. Exit;
  6508. end;
  6509. Break;
  6510. end;
  6511. { Lone AND tests }
  6512. if (taicpu(p).oper[0]^.typ = top_const) then
  6513. begin
  6514. {
  6515. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  6516. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  6517. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  6518. }
  6519. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  6520. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  6521. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  6522. begin
  6523. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg);
  6524. if taicpu(p).opsize = S_L then
  6525. begin
  6526. Include(OptsToCheck,aoc_MovAnd2Mov_3);
  6527. Result := True;
  6528. end;
  6529. end;
  6530. end;
  6531. { Backward check to determine necessity of and %reg,%reg }
  6532. if (taicpu(p).oper[0]^.typ = top_reg) and
  6533. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  6534. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  6535. GetLastInstruction(p, hp2) and
  6536. RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp2) and
  6537. { Check size of adjacent instruction to determine if the AND is
  6538. effectively a null operation }
  6539. (
  6540. (taicpu(p).opsize = taicpu(hp2).opsize) or
  6541. { Note: Don't include S_Q }
  6542. ((taicpu(p).opsize = S_L) and (taicpu(hp2).opsize in [S_BL, S_WL])) or
  6543. ((taicpu(p).opsize = S_W) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_L])) or
  6544. ((taicpu(p).opsize = S_B) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_W, S_L]))
  6545. ) then
  6546. begin
  6547. DebugMsg(SPeepholeOptimization + 'And2Nop', p);
  6548. { If GetNextInstruction returned False, hp1 will be nil }
  6549. RemoveCurrentP(p, hp1);
  6550. Result := True;
  6551. Exit;
  6552. end;
  6553. end;
  6554. function TX86AsmOptimizer.OptPass2ADD(var p : tai) : boolean;
  6555. var
  6556. hp1: tai; NewRef: TReference;
  6557. { This entire nested function is used in an if-statement below, but we
  6558. want to avoid all the used reg transfers and GetNextInstruction calls
  6559. until we really have to check }
  6560. function MemRegisterNotUsedLater: Boolean; inline;
  6561. var
  6562. hp2: tai;
  6563. begin
  6564. TransferUsedRegs(TmpUsedRegs);
  6565. hp2 := p;
  6566. repeat
  6567. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  6568. until not GetNextInstruction(hp2, hp2) or (hp2 = hp1);
  6569. Result := not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs);
  6570. end;
  6571. begin
  6572. Result := False;
  6573. if not GetNextInstruction(p, hp1) or (hp1.typ <> ait_instruction) then
  6574. Exit;
  6575. if (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif}]) then
  6576. begin
  6577. { Change:
  6578. add %reg2,%reg1
  6579. mov/s/z #(%reg1),%reg1 (%reg1 superregisters must be the same)
  6580. To:
  6581. mov/s/z #(%reg1,%reg2),%reg1
  6582. }
  6583. if MatchOpType(taicpu(p), top_reg, top_reg) and
  6584. MatchInstruction(hp1, [A_MOV, A_MOVZX, A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif}], []) and
  6585. MatchOpType(taicpu(hp1), top_ref, top_reg) and
  6586. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1) and
  6587. (
  6588. (
  6589. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  6590. (taicpu(hp1).oper[0]^.ref^.index = NR_NO)
  6591. ) or (
  6592. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  6593. (taicpu(hp1).oper[0]^.ref^.base = NR_NO)
  6594. )
  6595. ) and (
  6596. Reg1WriteOverwritesReg2Entirely(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) or
  6597. (
  6598. { If the super registers ARE equal, then this MOV/S/Z does a partial write }
  6599. not SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  6600. MemRegisterNotUsedLater
  6601. )
  6602. ) then
  6603. begin
  6604. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[1]^.reg;
  6605. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  6606. DebugMsg(SPeepholeOptimization + 'AddMov2Mov done', p);
  6607. RemoveCurrentp(p, hp1);
  6608. Result := True;
  6609. Exit;
  6610. end;
  6611. { Change:
  6612. addl/q $x,%reg1
  6613. movl/q %reg1,%reg2
  6614. To:
  6615. leal/q $x(%reg1),%reg2
  6616. addl/q $x,%reg1 (can be removed if %reg1 or the flags are not used afterwards)
  6617. Breaks the dependency chain.
  6618. }
  6619. if MatchOpType(taicpu(p),top_const,top_reg) and
  6620. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  6621. (taicpu(hp1).oper[1]^.typ = top_reg) and
  6622. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) and
  6623. (
  6624. { Don't do AddMov2LeaAdd under -Os, but do allow AddMov2Lea }
  6625. not (cs_opt_size in current_settings.optimizerswitches) or
  6626. (
  6627. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) and
  6628. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)
  6629. )
  6630. ) then
  6631. begin
  6632. { Change the MOV instruction to a LEA instruction, and update the
  6633. first operand }
  6634. reference_reset(NewRef, 1, []);
  6635. NewRef.base := taicpu(p).oper[1]^.reg;
  6636. NewRef.scalefactor := 1;
  6637. NewRef.offset := taicpu(p).oper[0]^.val;
  6638. taicpu(hp1).opcode := A_LEA;
  6639. taicpu(hp1).loadref(0, NewRef);
  6640. TransferUsedRegs(TmpUsedRegs);
  6641. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  6642. if RegUsedAfterInstruction(NewRef.base, hp1, TmpUsedRegs) or
  6643. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs) then
  6644. begin
  6645. { Move what is now the LEA instruction to before the SUB instruction }
  6646. Asml.Remove(hp1);
  6647. Asml.InsertBefore(hp1, p);
  6648. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  6649. DebugMsg(SPeepholeOptimization + 'AddMov2LeaAdd', p);
  6650. p := hp1;
  6651. end
  6652. else
  6653. begin
  6654. { Since %reg1 or the flags aren't used afterwards, we can delete p completely }
  6655. RemoveCurrentP(p, hp1);
  6656. DebugMsg(SPeepholeOptimization + 'AddMov2Lea', p);
  6657. end;
  6658. Result := True;
  6659. end;
  6660. end;
  6661. end;
  6662. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  6663. begin
  6664. Result:=false;
  6665. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  6666. begin
  6667. if MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  6668. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  6669. begin
  6670. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  6671. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  6672. taicpu(p).opcode:=A_ADD;
  6673. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  6674. result:=true;
  6675. end
  6676. else if MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  6677. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  6678. begin
  6679. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  6680. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  6681. taicpu(p).opcode:=A_ADD;
  6682. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  6683. result:=true;
  6684. end;
  6685. end;
  6686. end;
  6687. function TX86AsmOptimizer.OptPass2SUB(var p: tai): Boolean;
  6688. var
  6689. hp1: tai; NewRef: TReference;
  6690. begin
  6691. { Change:
  6692. subl/q $x,%reg1
  6693. movl/q %reg1,%reg2
  6694. To:
  6695. leal/q $-x(%reg1),%reg2
  6696. subl/q $x,%reg1 (can be removed if %reg1 or the flags are not used afterwards)
  6697. Breaks the dependency chain and potentially permits the removal of
  6698. a CMP instruction if one follows.
  6699. }
  6700. Result := False;
  6701. if (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  6702. MatchOpType(taicpu(p),top_const,top_reg) and
  6703. GetNextInstruction(p, hp1) and
  6704. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  6705. (taicpu(hp1).oper[1]^.typ = top_reg) and
  6706. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) and
  6707. (
  6708. { Don't do SubMov2LeaSub under -Os, but do allow SubMov2Lea }
  6709. not (cs_opt_size in current_settings.optimizerswitches) or
  6710. (
  6711. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) and
  6712. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)
  6713. )
  6714. ) then
  6715. begin
  6716. { Change the MOV instruction to a LEA instruction, and update the
  6717. first operand }
  6718. reference_reset(NewRef, 1, []);
  6719. NewRef.base := taicpu(p).oper[1]^.reg;
  6720. NewRef.scalefactor := 1;
  6721. NewRef.offset := -taicpu(p).oper[0]^.val;
  6722. taicpu(hp1).opcode := A_LEA;
  6723. taicpu(hp1).loadref(0, NewRef);
  6724. TransferUsedRegs(TmpUsedRegs);
  6725. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  6726. if RegUsedAfterInstruction(NewRef.base, hp1, TmpUsedRegs) or
  6727. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs) then
  6728. begin
  6729. { Move what is now the LEA instruction to before the SUB instruction }
  6730. Asml.Remove(hp1);
  6731. Asml.InsertBefore(hp1, p);
  6732. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  6733. DebugMsg(SPeepholeOptimization + 'SubMov2LeaSub', p);
  6734. p := hp1;
  6735. end
  6736. else
  6737. begin
  6738. { Since %reg1 or the flags aren't used afterwards, we can delete p completely }
  6739. RemoveCurrentP(p, hp1);
  6740. DebugMsg(SPeepholeOptimization + 'SubMov2Lea', p);
  6741. end;
  6742. Result := True;
  6743. end;
  6744. end;
  6745. function TX86AsmOptimizer.SkipSimpleInstructions(var hp1 : tai) : Boolean;
  6746. begin
  6747. { we can skip all instructions not messing with the stack pointer }
  6748. while assigned(hp1) and {MatchInstruction(hp1,[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  6749. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  6750. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  6751. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  6752. ({(taicpu(hp1).ops=0) or }
  6753. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  6754. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  6755. ) and }
  6756. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  6757. )
  6758. ) do
  6759. GetNextInstruction(hp1,hp1);
  6760. Result:=assigned(hp1);
  6761. end;
  6762. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  6763. var
  6764. hp1, hp2, hp3, hp4, hp5: tai;
  6765. begin
  6766. Result:=false;
  6767. hp5:=nil;
  6768. { replace
  6769. leal(q) x(<stackpointer>),<stackpointer>
  6770. call procname
  6771. leal(q) -x(<stackpointer>),<stackpointer>
  6772. ret
  6773. by
  6774. jmp procname
  6775. but do it only on level 4 because it destroys stack back traces
  6776. }
  6777. if (cs_opt_level4 in current_settings.optimizerswitches) and
  6778. MatchOpType(taicpu(p),top_ref,top_reg) and
  6779. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  6780. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  6781. { the -8 or -24 are not required, but bail out early if possible,
  6782. higher values are unlikely }
  6783. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  6784. (taicpu(p).oper[0]^.ref^.offset=-24)) and
  6785. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  6786. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  6787. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  6788. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  6789. GetNextInstruction(p, hp1) and
  6790. { Take a copy of hp1 }
  6791. SetAndTest(hp1, hp4) and
  6792. { trick to skip label }
  6793. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  6794. SkipSimpleInstructions(hp1) and
  6795. MatchInstruction(hp1,A_CALL,[S_NO]) and
  6796. GetNextInstruction(hp1, hp2) and
  6797. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  6798. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  6799. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  6800. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  6801. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  6802. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  6803. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  6804. (taicpu(hp2).oper[0]^.ref^.segment=NR_NO) and
  6805. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  6806. GetNextInstruction(hp2, hp3) and
  6807. { trick to skip label }
  6808. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  6809. (MatchInstruction(hp3,A_RET,[S_NO]) or
  6810. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  6811. SetAndTest(hp3,hp5) and
  6812. GetNextInstruction(hp3,hp3) and
  6813. MatchInstruction(hp3,A_RET,[S_NO])
  6814. )
  6815. ) and
  6816. (taicpu(hp3).ops=0) then
  6817. begin
  6818. taicpu(hp1).opcode := A_JMP;
  6819. taicpu(hp1).is_jmp := true;
  6820. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  6821. RemoveCurrentP(p, hp4);
  6822. RemoveInstruction(hp2);
  6823. RemoveInstruction(hp3);
  6824. if Assigned(hp5) then
  6825. begin
  6826. AsmL.Remove(hp5);
  6827. ASmL.InsertBefore(hp5,hp1)
  6828. end;
  6829. Result:=true;
  6830. end;
  6831. end;
  6832. function TX86AsmOptimizer.PostPeepholeOptPush(var p : tai) : Boolean;
  6833. {$ifdef x86_64}
  6834. var
  6835. hp1, hp2, hp3, hp4, hp5: tai;
  6836. {$endif x86_64}
  6837. begin
  6838. Result:=false;
  6839. {$ifdef x86_64}
  6840. hp5:=nil;
  6841. { replace
  6842. push %rax
  6843. call procname
  6844. pop %rcx
  6845. ret
  6846. by
  6847. jmp procname
  6848. but do it only on level 4 because it destroys stack back traces
  6849. It depends on the fact, that the sequence push rax/pop rcx is used for stack alignment as rcx is volatile
  6850. for all supported calling conventions
  6851. }
  6852. if (cs_opt_level4 in current_settings.optimizerswitches) and
  6853. MatchOpType(taicpu(p),top_reg) and
  6854. (taicpu(p).oper[0]^.reg=NR_RAX) and
  6855. GetNextInstruction(p, hp1) and
  6856. { Take a copy of hp1 }
  6857. SetAndTest(hp1, hp4) and
  6858. { trick to skip label }
  6859. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  6860. SkipSimpleInstructions(hp1) and
  6861. MatchInstruction(hp1,A_CALL,[S_NO]) and
  6862. GetNextInstruction(hp1, hp2) and
  6863. MatchInstruction(hp2,A_POP,[taicpu(p).opsize]) and
  6864. MatchOpType(taicpu(hp2),top_reg) and
  6865. (taicpu(hp2).oper[0]^.reg=NR_RCX) and
  6866. GetNextInstruction(hp2, hp3) and
  6867. { trick to skip label }
  6868. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  6869. (MatchInstruction(hp3,A_RET,[S_NO]) or
  6870. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  6871. SetAndTest(hp3,hp5) and
  6872. GetNextInstruction(hp3,hp3) and
  6873. MatchInstruction(hp3,A_RET,[S_NO])
  6874. )
  6875. ) and
  6876. (taicpu(hp3).ops=0) then
  6877. begin
  6878. taicpu(hp1).opcode := A_JMP;
  6879. taicpu(hp1).is_jmp := true;
  6880. DebugMsg(SPeepholeOptimization + 'PushCallPushRet2Jmp done',p);
  6881. RemoveCurrentP(p, hp4);
  6882. RemoveInstruction(hp2);
  6883. RemoveInstruction(hp3);
  6884. if Assigned(hp5) then
  6885. begin
  6886. AsmL.Remove(hp5);
  6887. ASmL.InsertBefore(hp5,hp1)
  6888. end;
  6889. Result:=true;
  6890. end;
  6891. {$endif x86_64}
  6892. end;
  6893. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  6894. var
  6895. Value, RegName: string;
  6896. begin
  6897. Result:=false;
  6898. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  6899. begin
  6900. case taicpu(p).oper[0]^.val of
  6901. 0:
  6902. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  6903. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  6904. begin
  6905. { change "mov $0,%reg" into "xor %reg,%reg" }
  6906. taicpu(p).opcode := A_XOR;
  6907. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  6908. Result := True;
  6909. end;
  6910. $1..$FFFFFFFF:
  6911. begin
  6912. { Code size reduction by J. Gareth "Kit" Moreton }
  6913. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  6914. case taicpu(p).opsize of
  6915. S_Q:
  6916. begin
  6917. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  6918. Value := debug_tostr(taicpu(p).oper[0]^.val);
  6919. { The actual optimization }
  6920. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  6921. taicpu(p).changeopsize(S_L);
  6922. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  6923. Result := True;
  6924. end;
  6925. else
  6926. { Do nothing };
  6927. end;
  6928. end;
  6929. -1:
  6930. { Don't make this optimisation if the CPU flags are required, since OR scrambles them }
  6931. if (cs_opt_size in current_settings.optimizerswitches) and
  6932. (taicpu(p).opsize <> S_B) and
  6933. not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  6934. begin
  6935. { change "mov $-1,%reg" into "or $-1,%reg" }
  6936. { NOTES:
  6937. - No size saving is made when changing a Word-sized assignment unless the register is AX (smaller encoding)
  6938. - This operation creates a false dependency on the register, so only do it when optimising for size
  6939. - 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
  6940. }
  6941. taicpu(p).opcode := A_OR;
  6942. Result := True;
  6943. end;
  6944. end;
  6945. end;
  6946. end;
  6947. function TX86AsmOptimizer.PostPeepholeOptAnd(var p : tai) : boolean;
  6948. var
  6949. hp1: tai;
  6950. begin
  6951. { Detect:
  6952. andw x, %ax (0 <= x < $8000)
  6953. ...
  6954. movzwl %ax,%eax
  6955. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  6956. }
  6957. Result := False;
  6958. if MatchOpType(taicpu(p), top_const, top_reg) and
  6959. (taicpu(p).oper[1]^.reg = NR_AX) and { This is also enough to determine that opsize = S_W }
  6960. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val) and
  6961. GetNextInstructionUsingReg(p, hp1, NR_EAX) and
  6962. MatchInstruction(hp1, A_MOVZX, [S_WL]) and
  6963. MatchOperand(taicpu(hp1).oper[0]^, NR_AX) and
  6964. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) then
  6965. begin
  6966. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via AndMovz2AndCwtl)', hp1);
  6967. taicpu(hp1).opcode := A_CWDE;
  6968. taicpu(hp1).clearop(0);
  6969. taicpu(hp1).clearop(1);
  6970. taicpu(hp1).ops := 0;
  6971. { A change was made, but not with p, so move forward 1 }
  6972. p := tai(p.Next);
  6973. Result := True;
  6974. end;
  6975. end;
  6976. function TX86AsmOptimizer.PostPeepholeOptMOVSX(var p : tai) : boolean;
  6977. begin
  6978. Result := False;
  6979. if not MatchOpType(taicpu(p), top_reg, top_reg) then
  6980. Exit;
  6981. { Convert:
  6982. movswl %ax,%eax -> cwtl
  6983. movslq %eax,%rax -> cdqe
  6984. NOTE: Don't convert movswl %al,%ax to cbw, because cbw and cwde
  6985. refer to the same opcode and depends only on the assembler's
  6986. current operand-size attribute. [Kit]
  6987. }
  6988. with taicpu(p) do
  6989. case opsize of
  6990. S_WL:
  6991. if (oper[0]^.reg = NR_AX) and (oper[1]^.reg = NR_EAX) then
  6992. begin
  6993. DebugMsg(SPeepholeOptimization + 'Converted movswl %ax,%eax to cwtl', p);
  6994. opcode := A_CWDE;
  6995. clearop(0);
  6996. clearop(1);
  6997. ops := 0;
  6998. Result := True;
  6999. end;
  7000. {$ifdef x86_64}
  7001. S_LQ:
  7002. if (oper[0]^.reg = NR_EAX) and (oper[1]^.reg = NR_RAX) then
  7003. begin
  7004. DebugMsg(SPeepholeOptimization + 'Converted movslq %eax,%rax to cltq', p);
  7005. opcode := A_CDQE;
  7006. clearop(0);
  7007. clearop(1);
  7008. ops := 0;
  7009. Result := True;
  7010. end;
  7011. {$endif x86_64}
  7012. else
  7013. ;
  7014. end;
  7015. end;
  7016. function TX86AsmOptimizer.PostPeepholeOptShr(var p : tai) : boolean;
  7017. var
  7018. hp1: tai;
  7019. begin
  7020. { Detect:
  7021. shr x, %ax (x > 0)
  7022. ...
  7023. movzwl %ax,%eax
  7024. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  7025. }
  7026. Result := False;
  7027. if MatchOpType(taicpu(p), top_const, top_reg) and
  7028. (taicpu(p).oper[1]^.reg = NR_AX) and { This is also enough to determine that opsize = S_W }
  7029. (taicpu(p).oper[0]^.val > 0) and
  7030. GetNextInstructionUsingReg(p, hp1, NR_EAX) and
  7031. MatchInstruction(hp1, A_MOVZX, [S_WL]) and
  7032. MatchOperand(taicpu(hp1).oper[0]^, NR_AX) and
  7033. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) then
  7034. begin
  7035. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via ShrMovz2ShrCwtl)', hp1);
  7036. taicpu(hp1).opcode := A_CWDE;
  7037. taicpu(hp1).clearop(0);
  7038. taicpu(hp1).clearop(1);
  7039. taicpu(hp1).ops := 0;
  7040. { A change was made, but not with p, so move forward 1 }
  7041. p := tai(p.Next);
  7042. Result := True;
  7043. end;
  7044. end;
  7045. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  7046. begin
  7047. Result:=false;
  7048. { change "cmp $0, %reg" to "test %reg, %reg" }
  7049. if MatchOpType(taicpu(p),top_const,top_reg) and
  7050. (taicpu(p).oper[0]^.val = 0) then
  7051. begin
  7052. taicpu(p).opcode := A_TEST;
  7053. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  7054. Result:=true;
  7055. end;
  7056. end;
  7057. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  7058. var
  7059. IsTestConstX : Boolean;
  7060. hp1,hp2 : tai;
  7061. begin
  7062. Result:=false;
  7063. { removes the line marked with (x) from the sequence
  7064. and/or/xor/add/sub/... $x, %y
  7065. test/or %y, %y | test $-1, %y (x)
  7066. j(n)z _Label
  7067. as the first instruction already adjusts the ZF
  7068. %y operand may also be a reference }
  7069. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  7070. MatchOperand(taicpu(p).oper[0]^,-1);
  7071. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  7072. GetLastInstruction(p, hp1) and
  7073. (tai(hp1).typ = ait_instruction) and
  7074. GetNextInstruction(p,hp2) and
  7075. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  7076. case taicpu(hp1).opcode Of
  7077. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  7078. begin
  7079. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  7080. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  7081. { and in case of carry for A(E)/B(E)/C/NC }
  7082. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  7083. ((taicpu(hp1).opcode <> A_ADD) and
  7084. (taicpu(hp1).opcode <> A_SUB))) then
  7085. begin
  7086. RemoveCurrentP(p, hp2);
  7087. Result:=true;
  7088. end;
  7089. end;
  7090. A_SHL, A_SAL, A_SHR, A_SAR:
  7091. begin
  7092. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  7093. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  7094. { therefore, it's only safe to do this optimization for }
  7095. { shifts by a (nonzero) constant }
  7096. (taicpu(hp1).oper[0]^.typ = top_const) and
  7097. (taicpu(hp1).oper[0]^.val <> 0) and
  7098. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  7099. { and in case of carry for A(E)/B(E)/C/NC }
  7100. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  7101. begin
  7102. RemoveCurrentP(p, hp2);
  7103. Result:=true;
  7104. end;
  7105. end;
  7106. A_DEC, A_INC, A_NEG:
  7107. begin
  7108. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  7109. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  7110. { and in case of carry for A(E)/B(E)/C/NC }
  7111. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  7112. begin
  7113. case taicpu(hp1).opcode of
  7114. A_DEC, A_INC:
  7115. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  7116. begin
  7117. case taicpu(hp1).opcode Of
  7118. A_DEC: taicpu(hp1).opcode := A_SUB;
  7119. A_INC: taicpu(hp1).opcode := A_ADD;
  7120. else
  7121. ;
  7122. end;
  7123. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  7124. taicpu(hp1).loadConst(0,1);
  7125. taicpu(hp1).ops:=2;
  7126. end;
  7127. else
  7128. ;
  7129. end;
  7130. RemoveCurrentP(p, hp2);
  7131. Result:=true;
  7132. end;
  7133. end
  7134. else
  7135. { change "test $-1,%reg" into "test %reg,%reg" }
  7136. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  7137. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  7138. end { case }
  7139. { change "test $-1,%reg" into "test %reg,%reg" }
  7140. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  7141. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  7142. end;
  7143. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  7144. var
  7145. hp1,hp3 : tai;
  7146. {$ifndef x86_64}
  7147. hp2 : taicpu;
  7148. {$endif x86_64}
  7149. begin
  7150. Result:=false;
  7151. hp3:=nil;
  7152. {$ifndef x86_64}
  7153. { don't do this on modern CPUs, this really hurts them due to
  7154. broken call/ret pairing }
  7155. if (current_settings.optimizecputype < cpu_Pentium2) and
  7156. not(cs_create_pic in current_settings.moduleswitches) and
  7157. GetNextInstruction(p, hp1) and
  7158. MatchInstruction(hp1,A_JMP,[S_NO]) and
  7159. MatchOpType(taicpu(hp1),top_ref) and
  7160. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  7161. begin
  7162. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  7163. InsertLLItem(p.previous, p, hp2);
  7164. taicpu(p).opcode := A_JMP;
  7165. taicpu(p).is_jmp := true;
  7166. RemoveInstruction(hp1);
  7167. Result:=true;
  7168. end
  7169. else
  7170. {$endif x86_64}
  7171. { replace
  7172. call procname
  7173. ret
  7174. by
  7175. jmp procname
  7176. but do it only on level 4 because it destroys stack back traces
  7177. else if the subroutine is marked as no return, remove the ret
  7178. }
  7179. if ((cs_opt_level4 in current_settings.optimizerswitches) or
  7180. (po_noreturn in current_procinfo.procdef.procoptions)) and
  7181. GetNextInstruction(p, hp1) and
  7182. (MatchInstruction(hp1,A_RET,[S_NO]) or
  7183. (MatchInstruction(hp1,A_VZEROUPPER,[S_NO]) and
  7184. SetAndTest(hp1,hp3) and
  7185. GetNextInstruction(hp1,hp1) and
  7186. MatchInstruction(hp1,A_RET,[S_NO])
  7187. )
  7188. ) and
  7189. (taicpu(hp1).ops=0) then
  7190. begin
  7191. if (cs_opt_level4 in current_settings.optimizerswitches) and
  7192. { we might destroy stack alignment here if we do not do a call }
  7193. (target_info.stackalign<=sizeof(SizeUInt)) then
  7194. begin
  7195. taicpu(p).opcode := A_JMP;
  7196. taicpu(p).is_jmp := true;
  7197. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  7198. end
  7199. else
  7200. DebugMsg(SPeepholeOptimization + 'CallRet2Call done',p);
  7201. RemoveInstruction(hp1);
  7202. if Assigned(hp3) then
  7203. begin
  7204. AsmL.Remove(hp3);
  7205. AsmL.InsertBefore(hp3,p)
  7206. end;
  7207. Result:=true;
  7208. end;
  7209. end;
  7210. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  7211. function ConstInRange(const Val: TCGInt; const OpSize: TOpSize): Boolean;
  7212. begin
  7213. case OpSize of
  7214. S_B, S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  7215. Result := (Val <= $FF) and (Val >= -128);
  7216. S_W, S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  7217. Result := (Val <= $FFFF) and (Val >= -32768);
  7218. S_L{$ifdef x86_64}, S_LQ{$endif x86_64}:
  7219. Result := (Val <= $FFFFFFFF) and (Val >= -2147483648);
  7220. else
  7221. Result := True;
  7222. end;
  7223. end;
  7224. var
  7225. hp1, hp2 : tai;
  7226. SizeChange: Boolean;
  7227. PreMessage: string;
  7228. begin
  7229. Result := False;
  7230. if (taicpu(p).oper[0]^.typ = top_reg) and
  7231. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  7232. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) then
  7233. begin
  7234. { Change (using movzbl %al,%eax as an example):
  7235. movzbl %al, %eax movzbl %al, %eax
  7236. cmpl x, %eax testl %eax,%eax
  7237. To:
  7238. cmpb x, %al testb %al, %al (Move one back to avoid a false dependency)
  7239. movzbl %al, %eax movzbl %al, %eax
  7240. Smaller instruction and minimises pipeline stall as the CPU
  7241. doesn't have to wait for the register to get zero-extended. [Kit]
  7242. Also allow if the smaller of the two registers is being checked,
  7243. as this still removes the false dependency.
  7244. }
  7245. if
  7246. (
  7247. (
  7248. (taicpu(hp1).opcode = A_CMP) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  7249. ConstInRange(taicpu(hp1).oper[0]^.val, taicpu(p).opsize)
  7250. ) or (
  7251. { If MatchOperand returns True, they must both be registers }
  7252. (taicpu(hp1).opcode = A_TEST) and MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)
  7253. )
  7254. ) and
  7255. (reg2opsize(taicpu(hp1).oper[1]^.reg) <= reg2opsize(taicpu(p).oper[1]^.reg)) and
  7256. SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) then
  7257. begin
  7258. PreMessage := debug_op2str(taicpu(hp1).opcode) + debug_opsize2str(taicpu(hp1).opsize) + ' ' + debug_operstr(taicpu(hp1).oper[0]^) + ',' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' -> ' + debug_op2str(taicpu(hp1).opcode);
  7259. asml.Remove(hp1);
  7260. asml.InsertBefore(hp1, p);
  7261. { Swap instructions in the case of cmp 0,%reg or test %reg,%reg }
  7262. if (taicpu(hp1).opcode = A_TEST) or (taicpu(hp1).oper[0]^.val = 0) then
  7263. begin
  7264. taicpu(hp1).opcode := A_TEST;
  7265. taicpu(hp1).loadreg(0, taicpu(p).oper[0]^.reg);
  7266. end;
  7267. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  7268. case taicpu(p).opsize of
  7269. S_BW, S_BL:
  7270. begin
  7271. SizeChange := taicpu(hp1).opsize <> S_B;
  7272. taicpu(hp1).changeopsize(S_B);
  7273. end;
  7274. S_WL:
  7275. begin
  7276. SizeChange := taicpu(hp1).opsize <> S_W;
  7277. taicpu(hp1).changeopsize(S_W);
  7278. end
  7279. else
  7280. InternalError(2020112701);
  7281. end;
  7282. UpdateUsedRegs(tai(p.Next));
  7283. { Check if the register is used aferwards - if not, we can
  7284. remove the movzx instruction completely }
  7285. if not RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, p, UsedRegs) then
  7286. begin
  7287. { Hp1 is a better position than p for debugging purposes }
  7288. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4a', hp1);
  7289. RemoveCurrentp(p, hp1);
  7290. Result := True;
  7291. end;
  7292. if SizeChange then
  7293. DebugMsg(SPeepholeOptimization + PreMessage +
  7294. debug_opsize2str(taicpu(hp1).opsize) + ' ' + debug_operstr(taicpu(hp1).oper[0]^) + ',' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' (smaller and minimises pipeline stall - MovzxCmp2CmpMovzx)', hp1)
  7295. else
  7296. DebugMsg(SPeepholeOptimization + 'MovzxCmp2CmpMovzx', hp1);
  7297. Exit;
  7298. end;
  7299. { Change (using movzwl %ax,%eax as an example):
  7300. movzwl %ax, %eax
  7301. movb %al, (dest) (Register is smaller than read register in movz)
  7302. To:
  7303. movb %al, (dest) (Move one back to avoid a false dependency)
  7304. movzwl %ax, %eax
  7305. }
  7306. if (taicpu(hp1).opcode = A_MOV) and
  7307. (taicpu(hp1).oper[0]^.typ = top_reg) and
  7308. not RegInOp(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^) and
  7309. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(p).oper[0]^.reg) and
  7310. (reg2opsize(taicpu(hp1).oper[0]^.reg) <= reg2opsize(taicpu(p).oper[0]^.reg)) then
  7311. begin
  7312. DebugMsg(SPeepholeOptimization + 'MovzxMov2MovMovzx', hp1);
  7313. hp2 := tai(hp1.Previous); { Effectively the old position of hp1 }
  7314. asml.Remove(hp1);
  7315. asml.InsertBefore(hp1, p);
  7316. if taicpu(hp1).oper[1]^.typ = top_reg then
  7317. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, hp2, UsedRegs);
  7318. { Check if the register is used aferwards - if not, we can
  7319. remove the movzx instruction completely }
  7320. if not RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, p, UsedRegs) then
  7321. begin
  7322. { Hp1 is a better position than p for debugging purposes }
  7323. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4b', hp1);
  7324. RemoveCurrentp(p, hp1);
  7325. Result := True;
  7326. end;
  7327. Exit;
  7328. end;
  7329. end;
  7330. {$ifdef x86_64}
  7331. { Code size reduction by J. Gareth "Kit" Moreton }
  7332. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  7333. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  7334. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  7335. then
  7336. begin
  7337. { Has 64-bit register name and opcode suffix }
  7338. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  7339. { The actual optimization }
  7340. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  7341. if taicpu(p).opsize = S_BQ then
  7342. taicpu(p).changeopsize(S_BL)
  7343. else
  7344. taicpu(p).changeopsize(S_WL);
  7345. DebugMsg(SPeepholeOptimization + PreMessage +
  7346. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  7347. end;
  7348. {$endif}
  7349. end;
  7350. {$ifdef x86_64}
  7351. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  7352. var
  7353. PreMessage, RegName: string;
  7354. begin
  7355. { Code size reduction by J. Gareth "Kit" Moreton }
  7356. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  7357. as this removes the REX prefix }
  7358. Result := False;
  7359. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  7360. Exit;
  7361. if taicpu(p).oper[0]^.typ <> top_reg then
  7362. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  7363. InternalError(2018011500);
  7364. case taicpu(p).opsize of
  7365. S_Q:
  7366. begin
  7367. 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
  7368. begin
  7369. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  7370. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  7371. { The actual optimization }
  7372. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  7373. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  7374. taicpu(p).changeopsize(S_L);
  7375. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  7376. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  7377. end;
  7378. end;
  7379. else
  7380. ;
  7381. end;
  7382. end;
  7383. {$endif}
  7384. class procedure TX86AsmOptimizer.OptimizeRefs(var p: taicpu);
  7385. var
  7386. OperIdx: Integer;
  7387. begin
  7388. for OperIdx := 0 to p.ops - 1 do
  7389. if p.oper[OperIdx]^.typ = top_ref then
  7390. optimize_ref(p.oper[OperIdx]^.ref^, False);
  7391. end;
  7392. end.