aoptx86.pas 348 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150
  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. PreMessage: string;
  4766. { Data flow analysis }
  4767. TestValMin, TestValMax: TCgInt;
  4768. SmallerOverflow, FirstCheck: Boolean;
  4769. begin
  4770. Result := False;
  4771. p_removed := False;
  4772. SetLength(InstrList, 0);
  4773. InstrMax := -1;
  4774. ThisReg := taicpu(p).oper[1]^.reg;
  4775. case taicpu(p).opsize of
  4776. S_BW, S_BL:
  4777. begin
  4778. UpperLimit := $FF;
  4779. MinSize := S_B;
  4780. if taicpu(p).opsize = S_BW then
  4781. MaxSize := S_W
  4782. else
  4783. MaxSize := S_L;
  4784. end;
  4785. S_WL:
  4786. begin
  4787. UpperLimit := $FFFF;
  4788. MinSize := S_W;
  4789. MaxSize := S_L;
  4790. end
  4791. else
  4792. InternalError(2020112301);
  4793. end;
  4794. { With MinSize and MaxSize set, we can check some other optimisations
  4795. first, before attempting the expensive data flow analysis }
  4796. FirstCheck := GetNextInstructionUsingReg(p, hp1, ThisReg) and
  4797. (hp1.typ = ait_instruction) and
  4798. (
  4799. { Under -O1 and -O2, GetNextInstructionUsingReg may return an
  4800. instruction that doesn't actually contain ThisReg }
  4801. (cs_opt_level3 in current_settings.optimizerswitches) or
  4802. RegInInstruction(ThisReg, hp1)
  4803. );
  4804. { Data-flow analysis won't get anywhere since there was no instruction match }
  4805. if not FirstCheck then
  4806. Exit;
  4807. { Check for:
  4808. movz ##,%reg
  4809. cmp $y,%reg
  4810. (%reg deallocated)
  4811. Change register size so movz becomes mov
  4812. }
  4813. { This is anything but quick! }
  4814. if not(cs_opt_level2 in current_settings.optimizerswitches) then
  4815. Exit;
  4816. TestValMin := 0;
  4817. TestValMax := UpperLimit;
  4818. TrySmallerLimit := UpperLimit;
  4819. TrySmaller := S_NO;
  4820. SmallerOverflow := False;
  4821. RegChanged := False;
  4822. while FirstCheck { No need to waste checking for the next instruction again } or
  4823. (
  4824. GetNextInstructionUsingReg(hp1, hp1, ThisReg) and
  4825. (hp1.typ = ait_instruction) and
  4826. (
  4827. { Under -O1 and -O2, GetNextInstructionUsingReg may return an
  4828. instruction that doesn't actually contain ThisReg }
  4829. (cs_opt_level3 in current_settings.optimizerswitches) or
  4830. RegInInstruction(ThisReg, hp1)
  4831. )
  4832. ) do
  4833. begin
  4834. FirstCheck := False;
  4835. case taicpu(hp1).opcode of
  4836. A_INC,A_DEC:
  4837. begin
  4838. { Has to be an exact match on the register }
  4839. if not MatchOperand(taicpu(hp1).oper[0]^, ThisReg) then
  4840. Break;
  4841. if taicpu(hp1).opcode = A_INC then
  4842. begin
  4843. Inc(TestValMin);
  4844. Inc(TestValMax);
  4845. end
  4846. else
  4847. begin
  4848. Dec(TestValMin);
  4849. Dec(TestValMax);
  4850. end;
  4851. end;
  4852. A_CMP:
  4853. begin
  4854. { Smallest signed value for MinSize }
  4855. TrySmallerLimit := not (UpperLimit shr 1);
  4856. if (taicpu(hp1).oper[1]^.typ <> top_reg) or
  4857. { Has to be an exact match on the register }
  4858. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  4859. (taicpu(hp1).oper[0]^.typ <> top_const) or
  4860. { Make sure the comparison value is not smaller than the
  4861. smallest allowed signed value for the minimum size (e.g.
  4862. -128 for 8-bit) }
  4863. (taicpu(hp1).oper[0]^.val < TrySmallerLimit) then
  4864. Break;
  4865. TestValMin := TestValMin - taicpu(hp1).oper[0]^.val;
  4866. TestValMax := TestValMax - taicpu(hp1).oper[0]^.val;
  4867. if (TestValMin < TrySmallerLimit) or (TestValMax < TrySmallerLimit) or
  4868. (TestValMin > UpperLimit) or (TestValMax > UpperLimit) then
  4869. { Overflow }
  4870. Break;
  4871. { Check to see if the active register is used afterwards }
  4872. TransferUsedRegs(TmpUsedRegs);
  4873. IncludeRegInUsedRegs(ThisReg, TmpUsedRegs);
  4874. if not RegUsedAfterInstruction(ThisReg, hp1, TmpUsedRegs) then
  4875. begin
  4876. case MinSize of
  4877. S_B:
  4878. TargetSubReg := R_SUBL;
  4879. S_W:
  4880. TargetSubReg := R_SUBW;
  4881. else
  4882. InternalError(2021051002);
  4883. end;
  4884. { Update the register to its new size }
  4885. ThisReg := newreg(R_INTREGISTER, getsupreg(ThisReg), TargetSubReg);
  4886. taicpu(hp1).oper[1]^.reg := ThisReg;
  4887. taicpu(hp1).opsize := MinSize;
  4888. { Convert the input MOVZX to a MOV }
  4889. if (taicpu(p).oper[0]^.typ = top_reg) and
  4890. SuperRegistersEqual(taicpu(p).oper[0]^.reg, ThisReg) then
  4891. begin
  4892. { Or remove it completely! }
  4893. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 1a', p);
  4894. RemoveCurrentP(p);
  4895. p_removed := True;
  4896. end
  4897. else
  4898. begin
  4899. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 1a', p);
  4900. taicpu(p).opcode := A_MOV;
  4901. taicpu(p).oper[1]^.reg := ThisReg;
  4902. taicpu(p).opsize := MinSize;
  4903. end;
  4904. if (InstrMax >= 0) then
  4905. begin
  4906. for Index := 0 to InstrMax do
  4907. begin
  4908. { If p_removed is true, then the original MOV/Z was removed
  4909. and removing the AND instruction may not be safe if it
  4910. appears first }
  4911. if (InstrList[Index].oper[InstrList[Index].ops - 1]^.typ <> top_reg) then
  4912. InternalError(2020112311);
  4913. if InstrList[Index].oper[0]^.typ = top_reg then
  4914. InstrList[Index].oper[0]^.reg := ThisReg;
  4915. InstrList[Index].oper[InstrList[Index].ops - 1]^.reg := ThisReg;
  4916. InstrList[Index].opsize := MinSize;
  4917. end;
  4918. end;
  4919. Result := True;
  4920. Exit;
  4921. end;
  4922. end;
  4923. { OR and XOR are not included because they can too easily fool
  4924. the data flow analysis (they can cause non-linear behaviour) }
  4925. A_ADD,A_SUB,A_AND,A_SHL,A_SHR:
  4926. begin
  4927. if
  4928. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  4929. { Has to be an exact match on the register }
  4930. (taicpu(hp1).oper[1]^.reg <> ThisReg) or not
  4931. (
  4932. (
  4933. (taicpu(hp1).oper[0]^.typ = top_const) and
  4934. (
  4935. (
  4936. (taicpu(hp1).opcode = A_SHL) and
  4937. (
  4938. ((MinSize = S_B) and (taicpu(hp1).oper[0]^.val < 8)) or
  4939. ((MinSize = S_W) and (taicpu(hp1).oper[0]^.val < 16)) or
  4940. ((MinSize = S_L) and (taicpu(hp1).oper[0]^.val < 32))
  4941. )
  4942. ) or (
  4943. (taicpu(hp1).opcode <> A_SHL) and
  4944. (
  4945. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  4946. { Is it in the negative range? }
  4947. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val))
  4948. )
  4949. )
  4950. )
  4951. ) or (
  4952. MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^.reg) and
  4953. ((taicpu(hp1).opcode = A_ADD) or (taicpu(hp1).opcode = A_AND) or (taicpu(hp1).opcode = A_SUB))
  4954. )
  4955. ) then
  4956. Break;
  4957. case taicpu(hp1).opcode of
  4958. A_ADD:
  4959. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  4960. begin
  4961. TestValMin := TestValMin * 2;
  4962. TestValMax := TestValMax * 2;
  4963. end
  4964. else
  4965. begin
  4966. TestValMin := TestValMin + taicpu(hp1).oper[0]^.val;
  4967. TestValMax := TestValMax + taicpu(hp1).oper[0]^.val;
  4968. end;
  4969. A_SUB:
  4970. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  4971. begin
  4972. TestValMin := 0;
  4973. TestValMax := 0;
  4974. end
  4975. else
  4976. begin
  4977. TestValMin := TestValMin - taicpu(hp1).oper[0]^.val;
  4978. TestValMax := TestValMax - taicpu(hp1).oper[0]^.val;
  4979. end;
  4980. A_AND:
  4981. if (taicpu(hp1).oper[0]^.typ = top_const) then
  4982. begin
  4983. { we might be able to go smaller if AND appears first }
  4984. if InstrMax = -1 then
  4985. case MinSize of
  4986. S_B:
  4987. ;
  4988. S_W:
  4989. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  4990. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  4991. begin
  4992. TrySmaller := S_B;
  4993. TrySmallerLimit := $FF;
  4994. end;
  4995. S_L:
  4996. if ((taicpu(hp1).oper[0]^.val and $FF) = taicpu(hp1).oper[0]^.val) or
  4997. ((not(taicpu(hp1).oper[0]^.val) and $7F) = (not taicpu(hp1).oper[0]^.val)) then
  4998. begin
  4999. TrySmaller := S_B;
  5000. TrySmallerLimit := $FF;
  5001. end
  5002. else if ((taicpu(hp1).oper[0]^.val and $FFFF) = taicpu(hp1).oper[0]^.val) or
  5003. ((not(taicpu(hp1).oper[0]^.val) and $7FFF) = (not taicpu(hp1).oper[0]^.val)) then
  5004. begin
  5005. TrySmaller := S_W;
  5006. TrySmallerLimit := $FFFF;
  5007. end;
  5008. else
  5009. InternalError(2020112320);
  5010. end;
  5011. TestValMin := TestValMin and taicpu(hp1).oper[0]^.val;
  5012. TestValMax := TestValMax and taicpu(hp1).oper[0]^.val;
  5013. end;
  5014. A_SHL:
  5015. begin
  5016. TestValMin := TestValMin shl taicpu(hp1).oper[0]^.val;
  5017. TestValMax := TestValMax shl taicpu(hp1).oper[0]^.val;
  5018. end;
  5019. A_SHR:
  5020. begin
  5021. { we might be able to go smaller if SHR appears first }
  5022. if InstrMax = -1 then
  5023. case MinSize of
  5024. S_B:
  5025. ;
  5026. S_W:
  5027. if (taicpu(hp1).oper[0]^.val >= 8) then
  5028. begin
  5029. TrySmaller := S_B;
  5030. TrySmallerLimit := $FF;
  5031. end;
  5032. S_L:
  5033. if (taicpu(hp1).oper[0]^.val >= 24) then
  5034. begin
  5035. TrySmaller := S_B;
  5036. TrySmallerLimit := $FF;
  5037. end
  5038. else if (taicpu(hp1).oper[0]^.val >= 16) then
  5039. begin
  5040. TrySmaller := S_W;
  5041. TrySmallerLimit := $FFFF;
  5042. end;
  5043. else
  5044. InternalError(2020112321);
  5045. end;
  5046. TestValMin := TestValMin shr taicpu(hp1).oper[0]^.val;
  5047. TestValMax := TestValMax shr taicpu(hp1).oper[0]^.val;
  5048. end;
  5049. else
  5050. InternalError(2020112303);
  5051. end;
  5052. end;
  5053. (*
  5054. A_IMUL:
  5055. case taicpu(hp1).ops of
  5056. 2:
  5057. begin
  5058. if not MatchOpType(hp1, top_reg, top_reg) or
  5059. { Has to be an exact match on the register }
  5060. (taicpu(hp1).oper[0]^.reg <> ThisReg) or
  5061. (taicpu(hp1).oper[1]^.reg <> ThisReg) then
  5062. Break;
  5063. TestValMin := TestValMin * TestValMin;
  5064. TestValMax := TestValMax * TestValMax;
  5065. end;
  5066. 3:
  5067. begin
  5068. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  5069. { Has to be an exact match on the register }
  5070. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  5071. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  5072. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  5073. { Is it in the negative range? }
  5074. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  5075. Break;
  5076. TestValMin := TestValMin * taicpu(hp1).oper[0]^.val;
  5077. TestValMax := TestValMax * taicpu(hp1).oper[0]^.val;
  5078. end;
  5079. else
  5080. Break;
  5081. end;
  5082. A_IDIV:
  5083. case taicpu(hp1).ops of
  5084. 3:
  5085. begin
  5086. if not MatchOpType(hp1, top_const, top_reg, top_reg) or
  5087. { Has to be an exact match on the register }
  5088. (taicpu(hp1).oper[1]^.reg <> ThisReg) or
  5089. (taicpu(hp1).oper[2]^.reg <> ThisReg) or
  5090. ((taicpu(hp1).oper[0]^.val and UpperLimit) = taicpu(hp1).oper[0]^.val) or
  5091. { Is it in the negative range? }
  5092. (((not taicpu(hp1).oper[0]^.val) and (UpperLimit shr 1)) = (not taicpu(hp1).oper[0]^.val)) then
  5093. Break;
  5094. TestValMin := TestValMin div taicpu(hp1).oper[0]^.val;
  5095. TestValMax := TestValMax div taicpu(hp1).oper[0]^.val;
  5096. end;
  5097. else
  5098. Break;
  5099. end;
  5100. *)
  5101. A_MOVZX:
  5102. begin
  5103. if not MatchOpType(taicpu(hp1), top_reg, top_reg) then
  5104. Break;
  5105. if not SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, ThisReg) then
  5106. begin
  5107. { Because hp1 was obtained via GetNextInstructionUsingReg
  5108. and ThisReg doesn't appear in the first operand, it
  5109. must appear in the second operand and hence gets
  5110. overwritten }
  5111. if (InstrMax = -1) and
  5112. Reg1WriteOverwritesReg2Entirely(taicpu(hp1).oper[1]^.reg, ThisReg) then
  5113. begin
  5114. { The two MOVZX instructions are adjacent, so remove the first one }
  5115. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 5', p);
  5116. RemoveCurrentP(p);
  5117. Result := True;
  5118. Exit;
  5119. end;
  5120. Break;
  5121. end;
  5122. { The objective here is to try to find a combination that
  5123. removes one of the MOV/Z instructions. }
  5124. case taicpu(hp1).opsize of
  5125. S_WL:
  5126. if (MinSize in [S_B, S_W]) then
  5127. begin
  5128. TargetSize := S_L;
  5129. TargetSubReg := R_SUBD;
  5130. end
  5131. else if ((TrySmaller in [S_B, S_W]) and not SmallerOverflow) then
  5132. begin
  5133. TargetSize := TrySmaller;
  5134. if TrySmaller = S_B then
  5135. TargetSubReg := R_SUBL
  5136. else
  5137. TargetSubReg := R_SUBW;
  5138. end
  5139. else
  5140. Break;
  5141. S_BW:
  5142. if (MinSize in [S_B, S_W]) then
  5143. begin
  5144. TargetSize := S_W;
  5145. TargetSubReg := R_SUBW;
  5146. end
  5147. else if ((TrySmaller = S_B) and not SmallerOverflow) then
  5148. begin
  5149. TargetSize := S_B;
  5150. TargetSubReg := R_SUBL;
  5151. end
  5152. else
  5153. Break;
  5154. S_BL:
  5155. if (MinSize in [S_B, S_W]) then
  5156. begin
  5157. TargetSize := S_L;
  5158. TargetSubReg := R_SUBD;
  5159. end
  5160. else if ((TrySmaller = S_B) and not SmallerOverflow) then
  5161. begin
  5162. TargetSize := S_B;
  5163. TargetSubReg := R_SUBL;
  5164. end
  5165. else
  5166. Break;
  5167. else
  5168. InternalError(2020112302);
  5169. end;
  5170. { Update the register to its new size }
  5171. ThisReg := newreg(R_INTREGISTER, getsupreg(ThisReg), TargetSubReg);
  5172. if TargetSize = MinSize then
  5173. begin
  5174. { Convert the input MOVZX to a MOV }
  5175. if (taicpu(p).oper[0]^.typ = top_reg) and
  5176. SuperRegistersEqual(taicpu(p).oper[0]^.reg, ThisReg) then
  5177. begin
  5178. { Or remove it completely! }
  5179. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 1', p);
  5180. RemoveCurrentP(p);
  5181. p_removed := True;
  5182. end
  5183. else
  5184. begin
  5185. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 1', p);
  5186. taicpu(p).opcode := A_MOV;
  5187. taicpu(p).oper[1]^.reg := ThisReg;
  5188. taicpu(p).opsize := TargetSize;
  5189. end;
  5190. Result := True;
  5191. end
  5192. else if TargetSize <> MaxSize then
  5193. begin
  5194. case MaxSize of
  5195. S_L:
  5196. if TargetSize = S_W then
  5197. begin
  5198. DebugMsg(SPeepholeOptimization + 'movzbl2movzbw', p);
  5199. taicpu(p).opsize := S_BW;
  5200. taicpu(p).oper[1]^.reg := ThisReg;
  5201. Result := True;
  5202. end
  5203. else
  5204. InternalError(2020112341);
  5205. S_W:
  5206. if TargetSize = S_L then
  5207. begin
  5208. DebugMsg(SPeepholeOptimization + 'movzbw2movzbl', p);
  5209. taicpu(p).opsize := S_BL;
  5210. taicpu(p).oper[1]^.reg := ThisReg;
  5211. Result := True;
  5212. end
  5213. else
  5214. InternalError(2020112342);
  5215. else
  5216. ;
  5217. end;
  5218. end;
  5219. if (MaxSize = TargetSize) or
  5220. ((TargetSize = S_L) and (taicpu(hp1).opsize in [S_L, S_BL, S_WL])) or
  5221. ((TargetSize = S_W) and (taicpu(hp1).opsize in [S_W, S_BW])) then
  5222. begin
  5223. { Convert the output MOVZX to a MOV }
  5224. if SuperRegistersEqual(taicpu(hp1).oper[1]^.reg, ThisReg) then
  5225. begin
  5226. { Or remove it completely! }
  5227. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 2', hp1);
  5228. { Be careful; if p = hp1 and p was also removed, p
  5229. will become a dangling pointer }
  5230. if p = hp1 then
  5231. RemoveCurrentp(p) { p = hp1 and will then become the next instruction }
  5232. else
  5233. RemoveInstruction(hp1);
  5234. end
  5235. else
  5236. begin
  5237. taicpu(hp1).opcode := A_MOV;
  5238. taicpu(hp1).oper[0]^.reg := ThisReg;
  5239. taicpu(hp1).opsize := TargetSize;
  5240. { Check to see if the active register is used afterwards;
  5241. if not, we can change it and make a saving. }
  5242. RegInUse := False;
  5243. TransferUsedRegs(TmpUsedRegs);
  5244. { The target register may be marked as in use to cross
  5245. a jump to a distant label, so exclude it }
  5246. ExcludeRegFromUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs);
  5247. hp2 := p;
  5248. repeat
  5249. UpdateUsedRegs(TmpUsedRegs, tai(hp2.next));
  5250. { Explicitly check for the excluded register (don't include the first
  5251. instruction as it may be reading from here }
  5252. if ((p <> hp2) and (RegInInstruction(taicpu(hp1).oper[1]^.reg, hp2))) or
  5253. RegInUsedRegs(taicpu(hp1).oper[1]^.reg, TmpUsedRegs) then
  5254. begin
  5255. RegInUse := True;
  5256. Break;
  5257. end;
  5258. if not GetNextInstruction(hp2, hp2) then
  5259. InternalError(2020112340);
  5260. until (hp2 = hp1);
  5261. if not RegInUse and not RegUsedAfterInstruction(ThisReg, hp1, TmpUsedRegs) then
  5262. begin
  5263. DebugMsg(SPeepholeOptimization + 'Simplified register usage so ' + debug_regname(taicpu(hp1).oper[1]^.reg) + ' = ' + debug_regname(taicpu(p).oper[1]^.reg), p);
  5264. ThisReg := taicpu(hp1).oper[1]^.reg;
  5265. RegChanged := True;
  5266. TransferUsedRegs(TmpUsedRegs);
  5267. AllocRegBetween(ThisReg, p, hp1, TmpUsedRegs);
  5268. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 3', hp1);
  5269. if p = hp1 then
  5270. RemoveCurrentp(p) { p = hp1 and will then become the next instruction }
  5271. else
  5272. RemoveInstruction(hp1);
  5273. { Instruction will become "mov %reg,%reg" }
  5274. if not p_removed and (taicpu(p).opcode = A_MOV) and
  5275. MatchOperand(taicpu(p).oper[0]^, ThisReg) then
  5276. begin
  5277. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 6', p);
  5278. RemoveCurrentP(p);
  5279. p_removed := True;
  5280. end
  5281. else
  5282. taicpu(p).oper[1]^.reg := ThisReg;
  5283. Result := True;
  5284. end
  5285. else
  5286. DebugMsg(SPeepholeOptimization + 'Movzx2Mov 2', hp1);
  5287. end;
  5288. end
  5289. else
  5290. InternalError(2020112330);
  5291. { Now go through every instruction we found and change the
  5292. size. If TargetSize = MaxSize, then almost no changes are
  5293. needed and Result can remain False if it hasn't been set
  5294. yet.
  5295. If RegChanged is True, then the register requires changing
  5296. and so the point about TargetSize = MaxSize doesn't apply. }
  5297. if ((TargetSize <> MaxSize) or RegChanged) and (InstrMax >= 0) then
  5298. begin
  5299. for Index := 0 to InstrMax do
  5300. begin
  5301. { If p_removed is true, then the original MOV/Z was removed
  5302. and removing the AND instruction may not be safe if it
  5303. appears first }
  5304. if (InstrList[Index].oper[InstrList[Index].ops - 1]^.typ <> top_reg) then
  5305. InternalError(2020112310);
  5306. if InstrList[Index].oper[0]^.typ = top_reg then
  5307. InstrList[Index].oper[0]^.reg := ThisReg;
  5308. InstrList[Index].oper[InstrList[Index].ops - 1]^.reg := ThisReg;
  5309. InstrList[Index].opsize := TargetSize;
  5310. end;
  5311. Result := True;
  5312. end;
  5313. Exit;
  5314. end;
  5315. else
  5316. { This includes ADC, SBB, IDIV and SAR }
  5317. Break;
  5318. end;
  5319. if (TestValMin < 0) or (TestValMax < 0) or
  5320. (TestValMin > UpperLimit) or (TestValMax > UpperLimit) then
  5321. { Overflow }
  5322. Break
  5323. else if not SmallerOverflow and (TrySmaller <> S_NO) and
  5324. ((TestValMin > TrySmallerLimit) or (TestValMax > TrySmallerLimit)) then
  5325. SmallerOverflow := True;
  5326. { Contains highest index (so instruction count - 1) }
  5327. Inc(InstrMax);
  5328. if InstrMax > High(InstrList) then
  5329. SetLength(InstrList, InstrMax + LIST_STEP_SIZE);
  5330. InstrList[InstrMax] := taicpu(hp1);
  5331. end;
  5332. end;
  5333. function TX86AsmOptimizer.OptPass2Imul(var p : tai) : boolean;
  5334. var
  5335. hp1 : tai;
  5336. begin
  5337. Result:=false;
  5338. if (taicpu(p).ops >= 2) and
  5339. ((taicpu(p).oper[0]^.typ = top_const) or
  5340. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  5341. (taicpu(p).oper[1]^.typ = top_reg) and
  5342. ((taicpu(p).ops = 2) or
  5343. ((taicpu(p).oper[2]^.typ = top_reg) and
  5344. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  5345. GetLastInstruction(p,hp1) and
  5346. MatchInstruction(hp1,A_MOV,[]) and
  5347. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  5348. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  5349. begin
  5350. TransferUsedRegs(TmpUsedRegs);
  5351. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,p,TmpUsedRegs)) or
  5352. ((taicpu(p).ops = 3) and (taicpu(p).oper[1]^.reg=taicpu(p).oper[2]^.reg)) then
  5353. { change
  5354. mov reg1,reg2
  5355. imul y,reg2 to imul y,reg1,reg2 }
  5356. begin
  5357. taicpu(p).ops := 3;
  5358. taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
  5359. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  5360. DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
  5361. RemoveInstruction(hp1);
  5362. result:=true;
  5363. end;
  5364. end;
  5365. end;
  5366. procedure TX86AsmOptimizer.ConvertJumpToRET(const p: tai; const ret_p: tai);
  5367. var
  5368. ThisLabel: TAsmLabel;
  5369. begin
  5370. ThisLabel := tasmlabel(taicpu(p).oper[0]^.ref^.symbol);
  5371. ThisLabel.decrefs;
  5372. taicpu(p).opcode := A_RET;
  5373. taicpu(p).is_jmp := false;
  5374. taicpu(p).ops := taicpu(ret_p).ops;
  5375. case taicpu(ret_p).ops of
  5376. 0:
  5377. taicpu(p).clearop(0);
  5378. 1:
  5379. taicpu(p).loadconst(0,taicpu(ret_p).oper[0]^.val);
  5380. else
  5381. internalerror(2016041301);
  5382. end;
  5383. { If the original label is now dead, it might turn out that the label
  5384. immediately follows p. As a result, everything beyond it, which will
  5385. be just some final register configuration and a RET instruction, is
  5386. now dead code. [Kit] }
  5387. { NOTE: This is much faster than introducing a OptPass2RET routine and
  5388. running RemoveDeadCodeAfterJump for each RET instruction, because
  5389. this optimisation rarely happens and most RETs appear at the end of
  5390. routines where there is nothing that can be stripped. [Kit] }
  5391. if not ThisLabel.is_used then
  5392. RemoveDeadCodeAfterJump(p);
  5393. end;
  5394. function TX86AsmOptimizer.OptPass2Jmp(var p : tai) : boolean;
  5395. var
  5396. hp1, hp2, hp3: tai;
  5397. OperIdx: Integer;
  5398. begin
  5399. result:=false;
  5400. 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
  5401. (taicpu(p).oper[0]^.ref^.index=NR_NO) and (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) then
  5402. begin
  5403. hp1:=getlabelwithsym(tasmlabel(taicpu(p).oper[0]^.ref^.symbol));
  5404. if (taicpu(p).condition=C_None) and assigned(hp1) and SkipLabels(hp1,hp1) and (hp1.typ = ait_instruction) then
  5405. begin
  5406. case taicpu(hp1).opcode of
  5407. A_RET:
  5408. {
  5409. change
  5410. jmp .L1
  5411. ...
  5412. .L1:
  5413. ret
  5414. into
  5415. ret
  5416. }
  5417. begin
  5418. ConvertJumpToRET(p, hp1);
  5419. result:=true;
  5420. end;
  5421. A_MOV:
  5422. {
  5423. change
  5424. jmp .L1
  5425. ...
  5426. .L1:
  5427. mov ##, ##
  5428. ret
  5429. into
  5430. mov ##, ##
  5431. ret
  5432. }
  5433. { This optimisation tends to increase code size if the pass 1 MOV optimisations aren't
  5434. re-run, so only do this particular optimisation if optimising for speed or when
  5435. optimisations are very in-depth. [Kit] }
  5436. if (current_settings.optimizerswitches * [cs_opt_level3, cs_opt_size]) <> [cs_opt_size] then
  5437. begin
  5438. GetNextInstruction(hp1, hp2);
  5439. if not Assigned(hp2) then
  5440. Exit;
  5441. if (hp2.typ in [ait_label, ait_align]) then
  5442. SkipLabels(hp2,hp2);
  5443. if Assigned(hp2) and MatchInstruction(hp2, A_RET, [S_NO]) then
  5444. begin
  5445. { Duplicate the MOV instruction }
  5446. hp3:=tai(hp1.getcopy);
  5447. asml.InsertBefore(hp3, p);
  5448. { Make sure the compiler knows about any final registers written here }
  5449. for OperIdx := 0 to 1 do
  5450. with taicpu(hp3).oper[OperIdx]^ do
  5451. begin
  5452. case typ of
  5453. top_ref:
  5454. begin
  5455. if (ref^.base <> NR_NO) {$ifdef x86_64} and (ref^.base <> NR_RIP) {$endif x86_64} then
  5456. AllocRegBetween(ref^.base, hp3, tai(p.Next), UsedRegs);
  5457. if (ref^.index <> NR_NO) {$ifdef x86_64} and (ref^.index <> NR_RIP) {$endif x86_64} then
  5458. AllocRegBetween(ref^.index, hp3, tai(p.Next), UsedRegs);
  5459. end;
  5460. top_reg:
  5461. AllocRegBetween(reg, hp3, tai(p.Next), UsedRegs);
  5462. else
  5463. ;
  5464. end;
  5465. end;
  5466. { Now change the jump into a RET instruction }
  5467. ConvertJumpToRET(p, hp2);
  5468. result:=true;
  5469. end;
  5470. end;
  5471. else
  5472. ;
  5473. end;
  5474. end;
  5475. end;
  5476. end;
  5477. class function TX86AsmOptimizer.CanBeCMOV(p : tai) : boolean;
  5478. begin
  5479. CanBeCMOV:=assigned(p) and
  5480. MatchInstruction(p,A_MOV,[S_W,S_L,S_Q]) and
  5481. { we can't use cmov ref,reg because
  5482. ref could be nil and cmov still throws an exception
  5483. if ref=nil but the mov isn't done (FK)
  5484. or ((taicpu(p).oper[0]^.typ = top_ref) and
  5485. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  5486. }
  5487. (taicpu(p).oper[1]^.typ = top_reg) and
  5488. (
  5489. (taicpu(p).oper[0]^.typ = top_reg) or
  5490. { allow references, but only pure symbols or got rel. addressing with RIP as based,
  5491. it is not expected that this can cause a seg. violation }
  5492. (
  5493. (taicpu(p).oper[0]^.typ = top_ref) and
  5494. IsRefSafe(taicpu(p).oper[0]^.ref)
  5495. )
  5496. );
  5497. end;
  5498. function TX86AsmOptimizer.OptPass2Jcc(var p : tai) : boolean;
  5499. var
  5500. hp1,hp2: tai;
  5501. {$ifndef i8086}
  5502. hp3,hp4,hpmov2, hp5: tai;
  5503. l : Longint;
  5504. condition : TAsmCond;
  5505. {$endif i8086}
  5506. carryadd_opcode : TAsmOp;
  5507. symbol: TAsmSymbol;
  5508. reg: tsuperregister;
  5509. increg, tmpreg: TRegister;
  5510. begin
  5511. result:=false;
  5512. if GetNextInstruction(p,hp1) and (hp1.typ=ait_instruction) then
  5513. begin
  5514. symbol := TAsmLabel(taicpu(p).oper[0]^.ref^.symbol);
  5515. if GetNextInstruction(hp1,hp2) and
  5516. (
  5517. (hp2.typ=ait_label) or
  5518. { trick to skip align }
  5519. ((hp2.typ=ait_align) and GetNextInstruction(hp2,hp2) and (hp2.typ=ait_label))
  5520. ) and
  5521. (Tasmlabel(symbol) = Tai_label(hp2).labsym) and
  5522. (
  5523. (
  5524. ((Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB)) and
  5525. MatchOptype(Taicpu(hp1),top_const,top_reg) and
  5526. (Taicpu(hp1).oper[0]^.val=1)
  5527. ) or
  5528. ((Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC))
  5529. ) then
  5530. { jb @@1 cmc
  5531. inc/dec operand --> adc/sbb operand,0
  5532. @@1:
  5533. ... and ...
  5534. jnb @@1
  5535. inc/dec operand --> adc/sbb operand,0
  5536. @@1: }
  5537. begin
  5538. if Taicpu(p).condition in [C_NAE,C_B,C_C] then
  5539. begin
  5540. case taicpu(hp1).opcode of
  5541. A_INC,
  5542. A_ADD:
  5543. carryadd_opcode:=A_ADC;
  5544. A_DEC,
  5545. A_SUB:
  5546. carryadd_opcode:=A_SBB;
  5547. else
  5548. InternalError(2021011001);
  5549. end;
  5550. Taicpu(p).clearop(0);
  5551. Taicpu(p).ops:=0;
  5552. Taicpu(p).is_jmp:=false;
  5553. Taicpu(p).opcode:=A_CMC;
  5554. Taicpu(p).condition:=C_NONE;
  5555. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2CmcAdc/Sbb',p);
  5556. Taicpu(hp1).ops:=2;
  5557. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  5558. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  5559. else
  5560. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  5561. Taicpu(hp1).loadconst(0,0);
  5562. Taicpu(hp1).opcode:=carryadd_opcode;
  5563. result:=true;
  5564. exit;
  5565. end
  5566. else if Taicpu(p).condition in [C_AE,C_NB,C_NC] then
  5567. begin
  5568. case taicpu(hp1).opcode of
  5569. A_INC,
  5570. A_ADD:
  5571. carryadd_opcode:=A_ADC;
  5572. A_DEC,
  5573. A_SUB:
  5574. carryadd_opcode:=A_SBB;
  5575. else
  5576. InternalError(2021011002);
  5577. end;
  5578. Taicpu(hp1).ops:=2;
  5579. DebugMsg(SPeepholeOptimization+'JccAdd/Inc/Dec2Adc/Sbb',p);
  5580. if (Taicpu(hp1).opcode=A_ADD) or (Taicpu(hp1).opcode=A_SUB) then
  5581. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[1]^)
  5582. else
  5583. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^);
  5584. Taicpu(hp1).loadconst(0,0);
  5585. Taicpu(hp1).opcode:=carryadd_opcode;
  5586. RemoveCurrentP(p, hp1);
  5587. result:=true;
  5588. exit;
  5589. end
  5590. {
  5591. jcc @@1 setcc tmpreg
  5592. inc/dec/add/sub operand -> (movzx tmpreg)
  5593. @@1: add/sub tmpreg,operand
  5594. While this increases code size slightly, it makes the code much faster if the
  5595. jump is unpredictable
  5596. }
  5597. else if not(cs_opt_size in current_settings.optimizerswitches) then
  5598. begin
  5599. { search for an available register which is volatile }
  5600. for reg in tcpuregisterset do
  5601. begin
  5602. if
  5603. {$if defined(i386) or defined(i8086)}
  5604. { Only use registers whose lowest 8-bits can Be accessed }
  5605. (reg in [RS_EAX,RS_EBX,RS_ECX,RS_EDX]) and
  5606. {$endif i386 or i8086}
  5607. (reg in paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption)) and
  5608. not(reg in UsedRegs[R_INTREGISTER].GetUsedRegs)
  5609. { We don't need to check if tmpreg is in hp1 or not, because
  5610. it will be marked as in use at p (if not, this is
  5611. indictive of a compiler bug). }
  5612. then
  5613. begin
  5614. TAsmLabel(symbol).decrefs;
  5615. increg := newreg(R_INTREGISTER,reg,R_SUBL);
  5616. Taicpu(p).clearop(0);
  5617. Taicpu(p).ops:=1;
  5618. Taicpu(p).is_jmp:=false;
  5619. Taicpu(p).opcode:=A_SETcc;
  5620. DebugMsg(SPeepholeOptimization+'JccAdd2SetccAdd',p);
  5621. Taicpu(p).condition:=inverse_cond(Taicpu(p).condition);
  5622. Taicpu(p).loadreg(0,increg);
  5623. if getsubreg(Taicpu(hp1).oper[1]^.reg)<>R_SUBL then
  5624. begin
  5625. case getsubreg(Taicpu(hp1).oper[1]^.reg) of
  5626. R_SUBW:
  5627. begin
  5628. tmpreg := newreg(R_INTREGISTER,reg,R_SUBW);
  5629. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BW,increg,tmpreg);
  5630. end;
  5631. R_SUBD:
  5632. begin
  5633. tmpreg := newreg(R_INTREGISTER,reg,R_SUBD);
  5634. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,tmpreg);
  5635. end;
  5636. {$ifdef x86_64}
  5637. R_SUBQ:
  5638. begin
  5639. { MOVZX doesn't have a 64-bit variant, because
  5640. the 32-bit version implicitly zeroes the
  5641. upper 32-bits of the destination register }
  5642. hp2:=Taicpu.op_reg_reg(A_MOVZX,S_BL,increg,
  5643. newreg(R_INTREGISTER,reg,R_SUBD));
  5644. tmpreg := newreg(R_INTREGISTER,reg,R_SUBQ);
  5645. end;
  5646. {$endif x86_64}
  5647. else
  5648. Internalerror(2020030601);
  5649. end;
  5650. taicpu(hp2).fileinfo:=taicpu(hp1).fileinfo;
  5651. asml.InsertAfter(hp2,p);
  5652. end
  5653. else
  5654. tmpreg := increg;
  5655. if (Taicpu(hp1).opcode=A_INC) or (Taicpu(hp1).opcode=A_DEC) then
  5656. begin
  5657. Taicpu(hp1).ops:=2;
  5658. Taicpu(hp1).loadoper(1,Taicpu(hp1).oper[0]^)
  5659. end;
  5660. Taicpu(hp1).loadreg(0,tmpreg);
  5661. AllocRegBetween(tmpreg,p,hp1,UsedRegs);
  5662. Result := True;
  5663. { p is no longer a Jcc instruction, so exit }
  5664. Exit;
  5665. end;
  5666. end;
  5667. end;
  5668. end;
  5669. { Detect the following:
  5670. jmp<cond> @Lbl1
  5671. jmp @Lbl2
  5672. ...
  5673. @Lbl1:
  5674. ret
  5675. Change to:
  5676. jmp<inv_cond> @Lbl2
  5677. ret
  5678. }
  5679. if MatchInstruction(hp1,A_JMP,[]) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  5680. begin
  5681. hp2:=getlabelwithsym(TAsmLabel(symbol));
  5682. if Assigned(hp2) and SkipLabels(hp2,hp2) and
  5683. MatchInstruction(hp2,A_RET,[S_NO]) then
  5684. begin
  5685. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  5686. { Change label address to that of the unconditional jump }
  5687. taicpu(p).loadoper(0, taicpu(hp1).oper[0]^);
  5688. TAsmLabel(symbol).DecRefs;
  5689. taicpu(hp1).opcode := A_RET;
  5690. taicpu(hp1).is_jmp := false;
  5691. taicpu(hp1).ops := taicpu(hp2).ops;
  5692. DebugMsg(SPeepholeOptimization+'JccJmpRet2J!ccRet',p);
  5693. case taicpu(hp2).ops of
  5694. 0:
  5695. taicpu(hp1).clearop(0);
  5696. 1:
  5697. taicpu(hp1).loadconst(0,taicpu(hp2).oper[0]^.val);
  5698. else
  5699. internalerror(2016041302);
  5700. end;
  5701. end;
  5702. {$ifndef i8086}
  5703. end
  5704. {
  5705. convert
  5706. j<c> .L1
  5707. mov 1,reg
  5708. jmp .L2
  5709. .L1
  5710. mov 0,reg
  5711. .L2
  5712. into
  5713. mov 0,reg
  5714. set<not(c)> reg
  5715. take care of alignment and that the mov 0,reg is not converted into a xor as this
  5716. would destroy the flag contents
  5717. }
  5718. else if MatchInstruction(hp1,A_MOV,[]) and
  5719. MatchOpType(taicpu(hp1),top_const,top_reg) and
  5720. {$ifdef i386}
  5721. (
  5722. { Under i386, ESI, EDI, EBP and ESP
  5723. don't have an 8-bit representation }
  5724. not (getsupreg(taicpu(hp1).oper[1]^.reg) in [RS_ESI, RS_EDI, RS_EBP, RS_ESP])
  5725. ) and
  5726. {$endif i386}
  5727. (taicpu(hp1).oper[0]^.val=1) and
  5728. GetNextInstruction(hp1,hp2) and
  5729. MatchInstruction(hp2,A_JMP,[]) and (taicpu(hp2).oper[0]^.ref^.refaddr=addr_full) and
  5730. GetNextInstruction(hp2,hp3) and
  5731. { skip align }
  5732. ((hp3.typ<>ait_align) or GetNextInstruction(hp3,hp3)) and
  5733. (hp3.typ=ait_label) and
  5734. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol)=tai_label(hp3).labsym) and
  5735. (tai_label(hp3).labsym.getrefs=1) and
  5736. GetNextInstruction(hp3,hp4) and
  5737. MatchInstruction(hp4,A_MOV,[]) and
  5738. MatchOpType(taicpu(hp4),top_const,top_reg) and
  5739. (taicpu(hp4).oper[0]^.val=0) and
  5740. MatchOperand(taicpu(hp1).oper[1]^,taicpu(hp4).oper[1]^) and
  5741. GetNextInstruction(hp4,hp5) and
  5742. (hp5.typ=ait_label) and
  5743. (tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol)=tai_label(hp5).labsym) and
  5744. (tai_label(hp5).labsym.getrefs=1) then
  5745. begin
  5746. AllocRegBetween(NR_FLAGS,p,hp4,UsedRegs);
  5747. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2MovSetcc',p);
  5748. { remove last label }
  5749. RemoveInstruction(hp5);
  5750. { remove second label }
  5751. RemoveInstruction(hp3);
  5752. { if align is present remove it }
  5753. if GetNextInstruction(hp2,hp3) and (hp3.typ=ait_align) then
  5754. RemoveInstruction(hp3);
  5755. { remove jmp }
  5756. RemoveInstruction(hp2);
  5757. if taicpu(hp1).opsize=S_B then
  5758. RemoveInstruction(hp1)
  5759. else
  5760. taicpu(hp1).loadconst(0,0);
  5761. taicpu(hp4).opcode:=A_SETcc;
  5762. taicpu(hp4).opsize:=S_B;
  5763. taicpu(hp4).condition:=inverse_cond(taicpu(p).condition);
  5764. taicpu(hp4).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(hp4).oper[1]^.reg),R_SUBL));
  5765. taicpu(hp4).opercnt:=1;
  5766. taicpu(hp4).ops:=1;
  5767. taicpu(hp4).freeop(1);
  5768. RemoveCurrentP(p);
  5769. Result:=true;
  5770. exit;
  5771. end
  5772. else if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  5773. begin
  5774. { check for
  5775. jCC xxx
  5776. <several movs>
  5777. xxx:
  5778. }
  5779. l:=0;
  5780. while assigned(hp1) and
  5781. CanBeCMOV(hp1) and
  5782. { stop on labels }
  5783. not(hp1.typ=ait_label) do
  5784. begin
  5785. inc(l);
  5786. GetNextInstruction(hp1,hp1);
  5787. end;
  5788. if assigned(hp1) then
  5789. begin
  5790. if FindLabel(tasmlabel(symbol),hp1) then
  5791. begin
  5792. if (l<=4) and (l>0) then
  5793. begin
  5794. condition:=inverse_cond(taicpu(p).condition);
  5795. GetNextInstruction(p,hp1);
  5796. repeat
  5797. if not Assigned(hp1) then
  5798. InternalError(2018062900);
  5799. taicpu(hp1).opcode:=A_CMOVcc;
  5800. taicpu(hp1).condition:=condition;
  5801. UpdateUsedRegs(hp1);
  5802. GetNextInstruction(hp1,hp1);
  5803. until not(CanBeCMOV(hp1));
  5804. { Remember what hp1 is in case there's multiple aligns to get rid of }
  5805. hp2 := hp1;
  5806. repeat
  5807. if not Assigned(hp2) then
  5808. InternalError(2018062910);
  5809. case hp2.typ of
  5810. ait_label:
  5811. { What we expected - break out of the loop (it won't be a dead label at the top of
  5812. a cluster because that was optimised at an earlier stage) }
  5813. Break;
  5814. ait_align:
  5815. { Go to the next entry until a label is found (may be multiple aligns before it) }
  5816. begin
  5817. hp2 := tai(hp2.Next);
  5818. Continue;
  5819. end;
  5820. else
  5821. begin
  5822. { Might be a comment or temporary allocation entry }
  5823. if not (hp2.typ in SkipInstr) then
  5824. InternalError(2018062911);
  5825. hp2 := tai(hp2.Next);
  5826. Continue;
  5827. end;
  5828. end;
  5829. until False;
  5830. { Now we can safely decrement the reference count }
  5831. tasmlabel(symbol).decrefs;
  5832. DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
  5833. { Remove the original jump }
  5834. RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
  5835. GetNextInstruction(hp2, p); { Instruction after the label }
  5836. { Remove the label if this is its final reference }
  5837. if (tasmlabel(symbol).getrefs=0) then
  5838. StripLabelFast(hp1);
  5839. if Assigned(p) then
  5840. begin
  5841. UpdateUsedRegs(p);
  5842. result:=true;
  5843. end;
  5844. exit;
  5845. end;
  5846. end
  5847. else
  5848. begin
  5849. { check further for
  5850. jCC xxx
  5851. <several movs 1>
  5852. jmp yyy
  5853. xxx:
  5854. <several movs 2>
  5855. yyy:
  5856. }
  5857. { hp2 points to jmp yyy }
  5858. hp2:=hp1;
  5859. { skip hp1 to xxx (or an align right before it) }
  5860. GetNextInstruction(hp1, hp1);
  5861. if assigned(hp2) and
  5862. assigned(hp1) and
  5863. (l<=3) and
  5864. (hp2.typ=ait_instruction) and
  5865. (taicpu(hp2).is_jmp) and
  5866. (taicpu(hp2).condition=C_None) and
  5867. { real label and jump, no further references to the
  5868. label are allowed }
  5869. (tasmlabel(symbol).getrefs=1) and
  5870. FindLabel(tasmlabel(symbol),hp1) then
  5871. begin
  5872. l:=0;
  5873. { skip hp1 to <several moves 2> }
  5874. if (hp1.typ = ait_align) then
  5875. GetNextInstruction(hp1, hp1);
  5876. GetNextInstruction(hp1, hpmov2);
  5877. hp1 := hpmov2;
  5878. while assigned(hp1) and
  5879. CanBeCMOV(hp1) do
  5880. begin
  5881. inc(l);
  5882. GetNextInstruction(hp1, hp1);
  5883. end;
  5884. { hp1 points to yyy (or an align right before it) }
  5885. hp3 := hp1;
  5886. if assigned(hp1) and
  5887. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  5888. begin
  5889. condition:=inverse_cond(taicpu(p).condition);
  5890. GetNextInstruction(p,hp1);
  5891. repeat
  5892. taicpu(hp1).opcode:=A_CMOVcc;
  5893. taicpu(hp1).condition:=condition;
  5894. UpdateUsedRegs(hp1);
  5895. GetNextInstruction(hp1,hp1);
  5896. until not(assigned(hp1)) or
  5897. not(CanBeCMOV(hp1));
  5898. condition:=inverse_cond(condition);
  5899. hp1 := hpmov2;
  5900. { hp1 is now at <several movs 2> }
  5901. while Assigned(hp1) and CanBeCMOV(hp1) do
  5902. begin
  5903. taicpu(hp1).opcode:=A_CMOVcc;
  5904. taicpu(hp1).condition:=condition;
  5905. UpdateUsedRegs(hp1);
  5906. GetNextInstruction(hp1,hp1);
  5907. end;
  5908. hp1 := p;
  5909. { Get first instruction after label }
  5910. GetNextInstruction(hp3, p);
  5911. if assigned(p) and (hp3.typ = ait_align) then
  5912. GetNextInstruction(p, p);
  5913. { Don't dereference yet, as doing so will cause
  5914. GetNextInstruction to skip the label and
  5915. optional align marker. [Kit] }
  5916. GetNextInstruction(hp2, hp4);
  5917. DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
  5918. { remove jCC }
  5919. RemoveInstruction(hp1);
  5920. { Now we can safely decrement it }
  5921. tasmlabel(symbol).decrefs;
  5922. { Remove label xxx (it will have a ref of zero due to the initial check }
  5923. StripLabelFast(hp4);
  5924. { remove jmp }
  5925. symbol := taicpu(hp2).oper[0]^.ref^.symbol;
  5926. RemoveInstruction(hp2);
  5927. { As before, now we can safely decrement it }
  5928. tasmlabel(symbol).decrefs;
  5929. { Remove label yyy (and the optional alignment) if its reference falls to zero }
  5930. if tasmlabel(symbol).getrefs = 0 then
  5931. StripLabelFast(hp3);
  5932. if Assigned(p) then
  5933. begin
  5934. UpdateUsedRegs(p);
  5935. result:=true;
  5936. end;
  5937. exit;
  5938. end;
  5939. end;
  5940. end;
  5941. end;
  5942. {$endif i8086}
  5943. end;
  5944. end;
  5945. end;
  5946. function TX86AsmOptimizer.OptPass1Movx(var p : tai) : boolean;
  5947. var
  5948. hp1,hp2: tai;
  5949. reg_and_hp1_is_instr: Boolean;
  5950. begin
  5951. result:=false;
  5952. reg_and_hp1_is_instr:=(taicpu(p).oper[1]^.typ = top_reg) and
  5953. GetNextInstruction(p,hp1) and
  5954. (hp1.typ = ait_instruction);
  5955. if reg_and_hp1_is_instr and
  5956. (
  5957. (taicpu(hp1).opcode <> A_LEA) or
  5958. { If the LEA instruction can be converted into an arithmetic instruction,
  5959. it may be possible to then fold it. }
  5960. (
  5961. { If the flags register is in use, don't change the instruction
  5962. to an ADD otherwise this will scramble the flags. [Kit] }
  5963. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  5964. ConvertLEA(taicpu(hp1))
  5965. )
  5966. ) and
  5967. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  5968. GetNextInstruction(hp1,hp2) and
  5969. MatchInstruction(hp2,A_MOV,[]) and
  5970. (taicpu(hp2).oper[0]^.typ = top_reg) and
  5971. OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
  5972. ((taicpu(p).opsize in [S_BW,S_BL]) and (taicpu(hp2).opsize=S_B) or
  5973. (taicpu(p).opsize in [S_WL]) and (taicpu(hp2).opsize=S_W)) and
  5974. {$ifdef i386}
  5975. { not all registers have byte size sub registers on i386 }
  5976. ((taicpu(hp2).opsize<>S_B) or (getsupreg(taicpu(hp1).oper[0]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX])) and
  5977. {$endif i386}
  5978. (((taicpu(hp1).ops=2) and
  5979. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg))) or
  5980. ((taicpu(hp1).ops=1) and
  5981. (getsupreg(taicpu(hp2).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[0]^.reg)))) and
  5982. not(RegUsedAfterInstruction(taicpu(hp2).oper[0]^.reg,hp2,UsedRegs)) then
  5983. begin
  5984. { change movsX/movzX reg/ref, reg2
  5985. add/sub/or/... reg3/$const, reg2
  5986. mov reg2 reg/ref
  5987. to add/sub/or/... reg3/$const, reg/ref }
  5988. { by example:
  5989. movswl %si,%eax movswl %si,%eax p
  5990. decl %eax addl %edx,%eax hp1
  5991. movw %ax,%si movw %ax,%si hp2
  5992. ->
  5993. movswl %si,%eax movswl %si,%eax p
  5994. decw %eax addw %edx,%eax hp1
  5995. movw %ax,%si movw %ax,%si hp2
  5996. }
  5997. taicpu(hp1).changeopsize(taicpu(hp2).opsize);
  5998. {
  5999. ->
  6000. movswl %si,%eax movswl %si,%eax p
  6001. decw %si addw %dx,%si hp1
  6002. movw %ax,%si movw %ax,%si hp2
  6003. }
  6004. case taicpu(hp1).ops of
  6005. 1:
  6006. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  6007. 2:
  6008. begin
  6009. taicpu(hp1).loadoper(1,taicpu(hp2).oper[1]^);
  6010. if (taicpu(hp1).oper[0]^.typ = top_reg) then
  6011. setsubreg(taicpu(hp1).oper[0]^.reg,getsubreg(taicpu(hp2).oper[0]^.reg));
  6012. end;
  6013. else
  6014. internalerror(2008042702);
  6015. end;
  6016. {
  6017. ->
  6018. decw %si addw %dx,%si p
  6019. }
  6020. DebugMsg(SPeepholeOptimization + 'var3',p);
  6021. RemoveCurrentP(p, hp1);
  6022. RemoveInstruction(hp2);
  6023. end
  6024. else if reg_and_hp1_is_instr and
  6025. (taicpu(hp1).opcode = A_MOV) and
  6026. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  6027. (MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^)
  6028. {$ifdef x86_64}
  6029. { check for implicit extension to 64 bit }
  6030. or
  6031. ((taicpu(p).opsize in [S_BL,S_WL]) and
  6032. (taicpu(hp1).opsize=S_Q) and
  6033. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg)
  6034. )
  6035. {$endif x86_64}
  6036. )
  6037. then
  6038. begin
  6039. { change
  6040. movx %reg1,%reg2
  6041. mov %reg2,%reg3
  6042. dealloc %reg2
  6043. into
  6044. movx %reg,%reg3
  6045. }
  6046. TransferUsedRegs(TmpUsedRegs);
  6047. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  6048. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  6049. begin
  6050. DebugMsg(SPeepholeOptimization + 'MovxMov2Movx',p);
  6051. {$ifdef x86_64}
  6052. if (taicpu(p).opsize in [S_BL,S_WL]) and
  6053. (taicpu(hp1).opsize=S_Q) then
  6054. taicpu(p).loadreg(1,newreg(R_INTREGISTER,getsupreg(taicpu(hp1).oper[1]^.reg),R_SUBD))
  6055. else
  6056. {$endif x86_64}
  6057. taicpu(p).loadreg(1,taicpu(hp1).oper[1]^.reg);
  6058. RemoveInstruction(hp1);
  6059. end;
  6060. end
  6061. else if reg_and_hp1_is_instr and
  6062. (taicpu(hp1).opcode = A_MOV) and
  6063. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  6064. (((taicpu(p).opsize in [S_BW,S_BL,S_WL{$ifdef x86_64},S_BQ,S_WQ,S_LQ{$endif x86_64}]) and
  6065. (taicpu(hp1).opsize=S_B)) or
  6066. ((taicpu(p).opsize in [S_WL{$ifdef x86_64},S_WQ,S_LQ{$endif x86_64}]) and
  6067. (taicpu(hp1).opsize=S_W))
  6068. {$ifdef x86_64}
  6069. or ((taicpu(p).opsize=S_LQ) and
  6070. (taicpu(hp1).opsize=S_L))
  6071. {$endif x86_64}
  6072. ) and
  6073. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[0]^.reg) then
  6074. begin
  6075. { change
  6076. movx %reg1,%reg2
  6077. mov %reg2,%reg3
  6078. dealloc %reg2
  6079. into
  6080. mov %reg1,%reg3
  6081. if the second mov accesses only the bits stored in reg1
  6082. }
  6083. TransferUsedRegs(TmpUsedRegs);
  6084. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  6085. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
  6086. begin
  6087. DebugMsg(SPeepholeOptimization + 'MovxMov2Mov',p);
  6088. if taicpu(p).oper[0]^.typ=top_reg then
  6089. begin
  6090. case taicpu(hp1).opsize of
  6091. S_B:
  6092. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBL));
  6093. S_W:
  6094. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBW));
  6095. S_L:
  6096. taicpu(hp1).loadreg(0,newreg(R_INTREGISTER,getsupreg(taicpu(p).oper[0]^.reg),R_SUBD));
  6097. else
  6098. Internalerror(2020102301);
  6099. end;
  6100. AllocRegBetween(taicpu(hp1).oper[0]^.reg,p,hp1,UsedRegs);
  6101. end
  6102. else
  6103. taicpu(hp1).loadref(0,taicpu(p).oper[0]^.ref^);
  6104. RemoveCurrentP(p);
  6105. result:=true;
  6106. exit;
  6107. end;
  6108. end
  6109. else if reg_and_hp1_is_instr and
  6110. (taicpu(p).oper[0]^.typ = top_reg) and
  6111. (
  6112. (taicpu(hp1).opcode = A_SHL) or (taicpu(hp1).opcode = A_SAL)
  6113. ) and
  6114. (taicpu(hp1).oper[0]^.typ = top_const) and
  6115. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  6116. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  6117. { Minimum shift value allowed is the bit difference between the sizes }
  6118. (taicpu(hp1).oper[0]^.val >=
  6119. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  6120. 8 * (
  6121. tcgsize2size[reg_cgsize(taicpu(p).oper[1]^.reg)] -
  6122. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  6123. )
  6124. ) then
  6125. begin
  6126. { For:
  6127. movsx/movzx %reg1,%reg1 (same register, just different sizes)
  6128. shl/sal ##, %reg1
  6129. Remove the movsx/movzx instruction if the shift overwrites the
  6130. extended bits of the register (e.g. movslq %eax,%rax; shlq $32,%rax
  6131. }
  6132. DebugMsg(SPeepholeOptimization + 'MovxShl2Shl',p);
  6133. RemoveCurrentP(p, hp1);
  6134. Result := True;
  6135. Exit;
  6136. end
  6137. else if reg_and_hp1_is_instr and
  6138. (taicpu(p).oper[0]^.typ = top_reg) and
  6139. (
  6140. ((taicpu(hp1).opcode = A_SHR) and (taicpu(p).opcode = A_MOVZX)) or
  6141. ((taicpu(hp1).opcode = A_SAR) and (taicpu(p).opcode <> A_MOVZX))
  6142. ) and
  6143. (taicpu(hp1).oper[0]^.typ = top_const) and
  6144. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  6145. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^.reg) and
  6146. { Minimum shift value allowed is the bit size of the smallest register - 1 }
  6147. (taicpu(hp1).oper[0]^.val <
  6148. { Multiply by 8 because tcgsize2size returns bytes, not bits }
  6149. 8 * (
  6150. tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)]
  6151. )
  6152. ) then
  6153. begin
  6154. { For:
  6155. movsx %reg1,%reg1 movzx %reg1,%reg1 (same register, just different sizes)
  6156. sar ##, %reg1 shr ##, %reg1
  6157. Move the shift to before the movx instruction if the shift value
  6158. is not too large.
  6159. }
  6160. asml.Remove(hp1);
  6161. asml.InsertBefore(hp1, p);
  6162. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  6163. case taicpu(p).opsize of
  6164. s_BW, S_BL{$ifdef x86_64}, S_BQ{$endif}:
  6165. taicpu(hp1).opsize := S_B;
  6166. S_WL{$ifdef x86_64}, S_WQ{$endif}:
  6167. taicpu(hp1).opsize := S_W;
  6168. {$ifdef x86_64}
  6169. S_LQ:
  6170. taicpu(hp1).opsize := S_L;
  6171. {$endif}
  6172. else
  6173. InternalError(2020112401);
  6174. end;
  6175. if (taicpu(hp1).opcode = A_SHR) then
  6176. DebugMsg(SPeepholeOptimization + 'MovzShr2ShrMovz', hp1)
  6177. else
  6178. DebugMsg(SPeepholeOptimization + 'MovsSar2SarMovs', hp1);
  6179. Result := True;
  6180. end
  6181. else if taicpu(p).opcode=A_MOVZX then
  6182. begin
  6183. { removes superfluous And's after movzx's }
  6184. if reg_and_hp1_is_instr and
  6185. (taicpu(hp1).opcode = A_AND) and
  6186. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6187. ((taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)
  6188. {$ifdef x86_64}
  6189. { check for implicit extension to 64 bit }
  6190. or
  6191. ((taicpu(p).opsize in [S_BL,S_WL]) and
  6192. (taicpu(hp1).opsize=S_Q) and
  6193. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg)
  6194. )
  6195. {$endif x86_64}
  6196. )
  6197. then
  6198. begin
  6199. case taicpu(p).opsize Of
  6200. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  6201. if (taicpu(hp1).oper[0]^.val = $ff) then
  6202. begin
  6203. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz1',p);
  6204. RemoveInstruction(hp1);
  6205. Result:=true;
  6206. exit;
  6207. end;
  6208. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  6209. if (taicpu(hp1).oper[0]^.val = $ffff) then
  6210. begin
  6211. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz2',p);
  6212. RemoveInstruction(hp1);
  6213. Result:=true;
  6214. exit;
  6215. end;
  6216. {$ifdef x86_64}
  6217. S_LQ:
  6218. if (taicpu(hp1).oper[0]^.val = $ffffffff) then
  6219. begin
  6220. DebugMsg(SPeepholeOptimization + 'MovzAnd2Movz3',p);
  6221. RemoveInstruction(hp1);
  6222. Result:=true;
  6223. exit;
  6224. end;
  6225. {$endif x86_64}
  6226. else
  6227. ;
  6228. end;
  6229. { we cannot get rid of the and, but can we get rid of the movz ?}
  6230. if SuperRegistersEqual(taicpu(p).oper[0]^.reg,taicpu(p).oper[1]^.reg) then
  6231. begin
  6232. case taicpu(p).opsize Of
  6233. S_BL, S_BW{$ifdef x86_64}, S_BQ{$endif x86_64}:
  6234. if (taicpu(hp1).oper[0]^.val and $ff)=taicpu(hp1).oper[0]^.val then
  6235. begin
  6236. DebugMsg(SPeepholeOptimization + 'MovzAnd2And1',p);
  6237. RemoveCurrentP(p,hp1);
  6238. Result:=true;
  6239. exit;
  6240. end;
  6241. S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  6242. if (taicpu(hp1).oper[0]^.val and $ffff)=taicpu(hp1).oper[0]^.val then
  6243. begin
  6244. DebugMsg(SPeepholeOptimization + 'MovzAnd2And2',p);
  6245. RemoveCurrentP(p,hp1);
  6246. Result:=true;
  6247. exit;
  6248. end;
  6249. {$ifdef x86_64}
  6250. S_LQ:
  6251. if (taicpu(hp1).oper[0]^.val and $ffffffff)=taicpu(hp1).oper[0]^.val then
  6252. begin
  6253. DebugMsg(SPeepholeOptimization + 'MovzAnd2And3',p);
  6254. RemoveCurrentP(p,hp1);
  6255. Result:=true;
  6256. exit;
  6257. end;
  6258. {$endif x86_64}
  6259. else
  6260. ;
  6261. end;
  6262. end;
  6263. end;
  6264. { changes some movzx constructs to faster synonyms (all examples
  6265. are given with eax/ax, but are also valid for other registers)}
  6266. if MatchOpType(taicpu(p),top_reg,top_reg) then
  6267. begin
  6268. case taicpu(p).opsize of
  6269. { Technically, movzbw %al,%ax cannot be encoded in 32/64-bit mode
  6270. (the machine code is equivalent to movzbl %al,%eax), but the
  6271. code generator still generates that assembler instruction and
  6272. it is silently converted. This should probably be checked.
  6273. [Kit] }
  6274. S_BW:
  6275. begin
  6276. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  6277. (
  6278. not IsMOVZXAcceptable
  6279. { and $0xff,%ax has a smaller encoding but risks a partial write penalty }
  6280. or (
  6281. (cs_opt_size in current_settings.optimizerswitches) and
  6282. (taicpu(p).oper[1]^.reg = NR_AX)
  6283. )
  6284. ) then
  6285. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  6286. begin
  6287. DebugMsg(SPeepholeOptimization + 'var7',p);
  6288. taicpu(p).opcode := A_AND;
  6289. taicpu(p).changeopsize(S_W);
  6290. taicpu(p).loadConst(0,$ff);
  6291. Result := True;
  6292. end
  6293. else if not IsMOVZXAcceptable and
  6294. GetNextInstruction(p, hp1) and
  6295. (tai(hp1).typ = ait_instruction) and
  6296. (taicpu(hp1).opcode = A_AND) and
  6297. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6298. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6299. { Change "movzbw %reg1, %reg2; andw $const, %reg2"
  6300. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  6301. begin
  6302. DebugMsg(SPeepholeOptimization + 'var8',p);
  6303. taicpu(p).opcode := A_MOV;
  6304. taicpu(p).changeopsize(S_W);
  6305. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  6306. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6307. Result := True;
  6308. end;
  6309. end;
  6310. {$ifndef i8086} { movzbl %al,%eax cannot be encoded in 16-bit mode (the machine code is equivalent to movzbw %al,%ax }
  6311. S_BL:
  6312. begin
  6313. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  6314. (
  6315. not IsMOVZXAcceptable
  6316. { and $0xff,%eax has a smaller encoding but risks a partial write penalty }
  6317. or (
  6318. (cs_opt_size in current_settings.optimizerswitches) and
  6319. (taicpu(p).oper[1]^.reg = NR_EAX)
  6320. )
  6321. ) then
  6322. { Change "movzbl %al, %eax" to "andl $0x0ffh, %eax" }
  6323. begin
  6324. DebugMsg(SPeepholeOptimization + 'var9',p);
  6325. taicpu(p).opcode := A_AND;
  6326. taicpu(p).changeopsize(S_L);
  6327. taicpu(p).loadConst(0,$ff);
  6328. Result := True;
  6329. end
  6330. else if not IsMOVZXAcceptable and
  6331. GetNextInstruction(p, hp1) and
  6332. (tai(hp1).typ = ait_instruction) and
  6333. (taicpu(hp1).opcode = A_AND) and
  6334. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6335. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6336. { Change "movzbl %reg1, %reg2; andl $const, %reg2"
  6337. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  6338. begin
  6339. DebugMsg(SPeepholeOptimization + 'var10',p);
  6340. taicpu(p).opcode := A_MOV;
  6341. taicpu(p).changeopsize(S_L);
  6342. { do not use R_SUBWHOLE
  6343. as movl %rdx,%eax
  6344. is invalid in assembler PM }
  6345. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  6346. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6347. Result := True;
  6348. end;
  6349. end;
  6350. {$endif i8086}
  6351. S_WL:
  6352. if not IsMOVZXAcceptable then
  6353. begin
  6354. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) then
  6355. { Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax" }
  6356. begin
  6357. DebugMsg(SPeepholeOptimization + 'var11',p);
  6358. taicpu(p).opcode := A_AND;
  6359. taicpu(p).changeopsize(S_L);
  6360. taicpu(p).loadConst(0,$ffff);
  6361. Result := True;
  6362. end
  6363. else if GetNextInstruction(p, hp1) and
  6364. (tai(hp1).typ = ait_instruction) and
  6365. (taicpu(hp1).opcode = A_AND) and
  6366. (taicpu(hp1).oper[0]^.typ = top_const) and
  6367. (taicpu(hp1).oper[1]^.typ = top_reg) and
  6368. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6369. { Change "movzwl %reg1, %reg2; andl $const, %reg2"
  6370. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  6371. begin
  6372. DebugMsg(SPeepholeOptimization + 'var12',p);
  6373. taicpu(p).opcode := A_MOV;
  6374. taicpu(p).changeopsize(S_L);
  6375. { do not use R_SUBWHOLE
  6376. as movl %rdx,%eax
  6377. is invalid in assembler PM }
  6378. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  6379. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  6380. Result := True;
  6381. end;
  6382. end;
  6383. else
  6384. InternalError(2017050705);
  6385. end;
  6386. end
  6387. else if not IsMOVZXAcceptable and (taicpu(p).oper[0]^.typ = top_ref) then
  6388. begin
  6389. if GetNextInstruction(p, hp1) and
  6390. (tai(hp1).typ = ait_instruction) and
  6391. (taicpu(hp1).opcode = A_AND) and
  6392. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6393. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  6394. begin
  6395. //taicpu(p).opcode := A_MOV;
  6396. case taicpu(p).opsize Of
  6397. S_BL:
  6398. begin
  6399. DebugMsg(SPeepholeOptimization + 'var13',p);
  6400. taicpu(hp1).changeopsize(S_L);
  6401. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6402. end;
  6403. S_WL:
  6404. begin
  6405. DebugMsg(SPeepholeOptimization + 'var14',p);
  6406. taicpu(hp1).changeopsize(S_L);
  6407. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  6408. end;
  6409. S_BW:
  6410. begin
  6411. DebugMsg(SPeepholeOptimization + 'var15',p);
  6412. taicpu(hp1).changeopsize(S_W);
  6413. taicpu(hp1).loadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  6414. end;
  6415. else
  6416. Internalerror(2017050704)
  6417. end;
  6418. Result := True;
  6419. end;
  6420. end;
  6421. end;
  6422. end;
  6423. function TX86AsmOptimizer.OptPass1AND(var p : tai) : boolean;
  6424. var
  6425. hp1, hp2 : tai;
  6426. MaskLength : Cardinal;
  6427. MaskedBits : TCgInt;
  6428. begin
  6429. Result:=false;
  6430. { There are no optimisations for reference targets }
  6431. if (taicpu(p).oper[1]^.typ <> top_reg) then
  6432. Exit;
  6433. while GetNextInstruction(p, hp1) and
  6434. (hp1.typ = ait_instruction) do
  6435. begin
  6436. if (taicpu(p).oper[0]^.typ = top_const) then
  6437. begin
  6438. if (taicpu(hp1).opcode = A_AND) and
  6439. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6440. (getsupreg(taicpu(p).oper[1]^.reg) = getsupreg(taicpu(hp1).oper[1]^.reg)) and
  6441. { the second register must contain the first one, so compare their subreg types }
  6442. (getsubreg(taicpu(p).oper[1]^.reg)<=getsubreg(taicpu(hp1).oper[1]^.reg)) and
  6443. (abs(taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val)<$80000000) then
  6444. { change
  6445. and const1, reg
  6446. and const2, reg
  6447. to
  6448. and (const1 and const2), reg
  6449. }
  6450. begin
  6451. taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  6452. DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
  6453. RemoveCurrentP(p, hp1);
  6454. Result:=true;
  6455. exit;
  6456. end
  6457. else if (taicpu(hp1).opcode = A_MOVZX) and
  6458. MatchOpType(taicpu(hp1),top_reg,top_reg) and
  6459. SuperRegistersEqual(taicpu(p).oper[1]^.reg,taicpu(hp1).oper[1]^.reg) and
  6460. (getsupreg(taicpu(hp1).oper[0]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) and
  6461. (((taicpu(p).opsize=S_W) and
  6462. (taicpu(hp1).opsize=S_BW)) or
  6463. ((taicpu(p).opsize=S_L) and
  6464. (taicpu(hp1).opsize in [S_WL,S_BL{$ifdef x86_64},S_BQ,S_WQ{$endif x86_64}]))
  6465. {$ifdef x86_64}
  6466. or
  6467. ((taicpu(p).opsize=S_Q) and
  6468. (taicpu(hp1).opsize in [S_BQ,S_WQ,S_BL,S_WL]))
  6469. {$endif x86_64}
  6470. ) then
  6471. begin
  6472. if (((taicpu(hp1).opsize) in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  6473. ((taicpu(p).oper[0]^.val and $ff)=taicpu(p).oper[0]^.val)
  6474. ) or
  6475. (((taicpu(hp1).opsize) in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  6476. ((taicpu(p).oper[0]^.val and $ffff)=taicpu(p).oper[0]^.val))
  6477. then
  6478. begin
  6479. { Unlike MOVSX, MOVZX doesn't actually have a version that zero-extends a
  6480. 32-bit register to a 64-bit register, or even a version called MOVZXD, so
  6481. code that tests for the presence of AND 0xffffffff followed by MOVZX is
  6482. wasted, and is indictive of a compiler bug if it were triggered. [Kit]
  6483. NOTE: To zero-extend from 32 bits to 64 bits, simply use the standard MOV.
  6484. }
  6485. DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
  6486. RemoveInstruction(hp1);
  6487. { See if there are other optimisations possible }
  6488. Continue;
  6489. end;
  6490. end
  6491. else if (taicpu(hp1).opcode = A_SHL) and
  6492. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6493. (getsupreg(taicpu(p).oper[1]^.reg)=getsupreg(taicpu(hp1).oper[1]^.reg)) then
  6494. begin
  6495. {$ifopt R+}
  6496. {$define RANGE_WAS_ON}
  6497. {$R-}
  6498. {$endif}
  6499. { get length of potential and mask }
  6500. MaskLength:=SizeOf(taicpu(p).oper[0]^.val)*8-BsrQWord(taicpu(p).oper[0]^.val)-1;
  6501. { really a mask? }
  6502. {$ifdef RANGE_WAS_ON}
  6503. {$R+}
  6504. {$endif}
  6505. if (((QWord(1) shl MaskLength)-1)=taicpu(p).oper[0]^.val) and
  6506. { unmasked part shifted out? }
  6507. ((MaskLength+taicpu(hp1).oper[0]^.val)>=topsize2memsize[taicpu(hp1).opsize]) then
  6508. begin
  6509. DebugMsg(SPeepholeOptimization + 'AndShlToShl done',p);
  6510. RemoveCurrentP(p, hp1);
  6511. Result:=true;
  6512. exit;
  6513. end;
  6514. end
  6515. else if (taicpu(hp1).opcode = A_SHR) and
  6516. MatchOpType(taicpu(hp1),top_const,top_reg) and
  6517. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
  6518. (taicpu(hp1).oper[0]^.val <= 63) then
  6519. begin
  6520. { Does SHR combined with the AND cover all the bits?
  6521. e.g. for "andb $252,%reg; shrb $2,%reg" - the "and" can be removed }
  6522. MaskedBits := taicpu(p).oper[0]^.val or ((TCgInt(1) shl taicpu(hp1).oper[0]^.val) - 1);
  6523. if ((taicpu(p).opsize = S_B) and ((MaskedBits and $FF) = $FF)) or
  6524. ((taicpu(p).opsize = S_W) and ((MaskedBits and $FFFF) = $FFFF)) or
  6525. ((taicpu(p).opsize = S_L) and ((MaskedBits and $FFFFFFFF) = $FFFFFFFF)) then
  6526. begin
  6527. DebugMsg(SPeepholeOptimization + 'AndShrToShr done', p);
  6528. RemoveCurrentP(p, hp1);
  6529. Result := True;
  6530. Exit;
  6531. end;
  6532. end
  6533. else if ((taicpu(hp1).opcode = A_MOVSX){$ifdef x86_64} or (taicpu(hp1).opcode = A_MOVSXD){$endif x86_64}) and
  6534. (taicpu(hp1).oper[0]^.typ = top_reg) and
  6535. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg) then
  6536. begin
  6537. if SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  6538. (
  6539. (
  6540. (taicpu(hp1).opsize in [S_BW,S_BL{$ifdef x86_64},S_BQ{$endif x86_64}]) and
  6541. ((taicpu(p).oper[0]^.val and $7F) = taicpu(p).oper[0]^.val)
  6542. ) or (
  6543. (taicpu(hp1).opsize in [S_WL{$ifdef x86_64},S_WQ{$endif x86_64}]) and
  6544. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val)
  6545. {$ifdef x86_64}
  6546. ) or (
  6547. (taicpu(hp1).opsize = S_LQ) and
  6548. ((taicpu(p).oper[0]^.val and $7fffffff) = taicpu(p).oper[0]^.val)
  6549. {$endif x86_64}
  6550. )
  6551. ) then
  6552. begin
  6553. if (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg){$ifdef x86_64} or (taicpu(hp1).opsize = S_LQ){$endif x86_64} then
  6554. begin
  6555. DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
  6556. RemoveInstruction(hp1);
  6557. { See if there are other optimisations possible }
  6558. Continue;
  6559. end;
  6560. { The super-registers are the same though.
  6561. Note that this change by itself doesn't improve
  6562. code speed, but it opens up other optimisations. }
  6563. {$ifdef x86_64}
  6564. { Convert 64-bit register to 32-bit }
  6565. case taicpu(hp1).opsize of
  6566. S_BQ:
  6567. begin
  6568. taicpu(hp1).opsize := S_BL;
  6569. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  6570. end;
  6571. S_WQ:
  6572. begin
  6573. taicpu(hp1).opsize := S_WL;
  6574. taicpu(hp1).oper[1]^.reg := newreg(R_INTREGISTER, getsupreg(taicpu(hp1).oper[1]^.reg), R_SUBD);
  6575. end
  6576. else
  6577. ;
  6578. end;
  6579. {$endif x86_64}
  6580. DebugMsg(SPeepholeOptimization + 'AndMovsxToAndMovzx', hp1);
  6581. taicpu(hp1).opcode := A_MOVZX;
  6582. { See if there are other optimisations possible }
  6583. Continue;
  6584. end;
  6585. end;
  6586. end;
  6587. if (taicpu(hp1).is_jmp) and
  6588. (taicpu(hp1).opcode<>A_JMP) and
  6589. not(RegInUsedRegs(taicpu(p).oper[1]^.reg,UsedRegs)) then
  6590. begin
  6591. { change
  6592. and x, reg
  6593. jxx
  6594. to
  6595. test x, reg
  6596. jxx
  6597. if reg is deallocated before the
  6598. jump, but only if it's a conditional jump (PFV)
  6599. }
  6600. taicpu(p).opcode := A_TEST;
  6601. Exit;
  6602. end;
  6603. Break;
  6604. end;
  6605. { Lone AND tests }
  6606. if (taicpu(p).oper[0]^.typ = top_const) then
  6607. begin
  6608. {
  6609. - Convert and $0xFF,reg to and reg,reg if reg is 8-bit
  6610. - Convert and $0xFFFF,reg to and reg,reg if reg is 16-bit
  6611. - Convert and $0xFFFFFFFF,reg to and reg,reg if reg is 32-bit
  6612. }
  6613. if ((taicpu(p).oper[0]^.val = $FF) and (taicpu(p).opsize = S_B)) or
  6614. ((taicpu(p).oper[0]^.val = $FFFF) and (taicpu(p).opsize = S_W)) or
  6615. ((taicpu(p).oper[0]^.val = $FFFFFFFF) and (taicpu(p).opsize = S_L)) then
  6616. begin
  6617. taicpu(p).loadreg(0, taicpu(p).oper[1]^.reg);
  6618. if taicpu(p).opsize = S_L then
  6619. begin
  6620. Include(OptsToCheck,aoc_MovAnd2Mov_3);
  6621. Result := True;
  6622. end;
  6623. end;
  6624. end;
  6625. { Backward check to determine necessity of and %reg,%reg }
  6626. if (taicpu(p).oper[0]^.typ = top_reg) and
  6627. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  6628. not RegInUsedRegs(NR_DEFAULTFLAGS, UsedRegs) and
  6629. GetLastInstruction(p, hp2) and
  6630. RegModifiedByInstruction(taicpu(p).oper[1]^.reg, hp2) and
  6631. { Check size of adjacent instruction to determine if the AND is
  6632. effectively a null operation }
  6633. (
  6634. (taicpu(p).opsize = taicpu(hp2).opsize) or
  6635. { Note: Don't include S_Q }
  6636. ((taicpu(p).opsize = S_L) and (taicpu(hp2).opsize in [S_BL, S_WL])) or
  6637. ((taicpu(p).opsize = S_W) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_L])) or
  6638. ((taicpu(p).opsize = S_B) and (taicpu(hp2).opsize in [S_BW, S_BL, S_WL, S_W, S_L]))
  6639. ) then
  6640. begin
  6641. DebugMsg(SPeepholeOptimization + 'And2Nop', p);
  6642. { If GetNextInstruction returned False, hp1 will be nil }
  6643. RemoveCurrentP(p, hp1);
  6644. Result := True;
  6645. Exit;
  6646. end;
  6647. end;
  6648. function TX86AsmOptimizer.OptPass2ADD(var p : tai) : boolean;
  6649. var
  6650. hp1: tai; NewRef: TReference;
  6651. { This entire nested function is used in an if-statement below, but we
  6652. want to avoid all the used reg transfers and GetNextInstruction calls
  6653. until we really have to check }
  6654. function MemRegisterNotUsedLater: Boolean; inline;
  6655. var
  6656. hp2: tai;
  6657. begin
  6658. TransferUsedRegs(TmpUsedRegs);
  6659. hp2 := p;
  6660. repeat
  6661. UpdateUsedRegs(TmpUsedRegs, tai(hp2.Next));
  6662. until not GetNextInstruction(hp2, hp2) or (hp2 = hp1);
  6663. Result := not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs);
  6664. end;
  6665. begin
  6666. Result := False;
  6667. if not GetNextInstruction(p, hp1) or (hp1.typ <> ait_instruction) then
  6668. Exit;
  6669. if (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif}]) then
  6670. begin
  6671. { Change:
  6672. add %reg2,%reg1
  6673. mov/s/z #(%reg1),%reg1 (%reg1 superregisters must be the same)
  6674. To:
  6675. mov/s/z #(%reg1,%reg2),%reg1
  6676. }
  6677. if MatchOpType(taicpu(p), top_reg, top_reg) and
  6678. MatchInstruction(hp1, [A_MOV, A_MOVZX, A_MOVSX{$ifdef x86_64}, A_MOVSXD{$endif}], []) and
  6679. MatchOpType(taicpu(hp1), top_ref, top_reg) and
  6680. (taicpu(hp1).oper[0]^.ref^.scalefactor <= 1) and
  6681. (
  6682. (
  6683. (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  6684. (taicpu(hp1).oper[0]^.ref^.index = NR_NO)
  6685. ) or (
  6686. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  6687. (taicpu(hp1).oper[0]^.ref^.base = NR_NO)
  6688. )
  6689. ) and (
  6690. Reg1WriteOverwritesReg2Entirely(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) or
  6691. (
  6692. { If the super registers ARE equal, then this MOV/S/Z does a partial write }
  6693. not SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) and
  6694. MemRegisterNotUsedLater
  6695. )
  6696. ) then
  6697. begin
  6698. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[1]^.reg;
  6699. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  6700. DebugMsg(SPeepholeOptimization + 'AddMov2Mov done', p);
  6701. RemoveCurrentp(p, hp1);
  6702. Result := True;
  6703. Exit;
  6704. end;
  6705. { Change:
  6706. addl/q $x,%reg1
  6707. movl/q %reg1,%reg2
  6708. To:
  6709. leal/q $x(%reg1),%reg2
  6710. addl/q $x,%reg1 (can be removed if %reg1 or the flags are not used afterwards)
  6711. Breaks the dependency chain.
  6712. }
  6713. if MatchOpType(taicpu(p),top_const,top_reg) and
  6714. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  6715. (taicpu(hp1).oper[1]^.typ = top_reg) and
  6716. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) and
  6717. (
  6718. { Don't do AddMov2LeaAdd under -Os, but do allow AddMov2Lea }
  6719. not (cs_opt_size in current_settings.optimizerswitches) or
  6720. (
  6721. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) and
  6722. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)
  6723. )
  6724. ) then
  6725. begin
  6726. { Change the MOV instruction to a LEA instruction, and update the
  6727. first operand }
  6728. reference_reset(NewRef, 1, []);
  6729. NewRef.base := taicpu(p).oper[1]^.reg;
  6730. NewRef.scalefactor := 1;
  6731. NewRef.offset := taicpu(p).oper[0]^.val;
  6732. taicpu(hp1).opcode := A_LEA;
  6733. taicpu(hp1).loadref(0, NewRef);
  6734. TransferUsedRegs(TmpUsedRegs);
  6735. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  6736. if RegUsedAfterInstruction(NewRef.base, hp1, TmpUsedRegs) or
  6737. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs) then
  6738. begin
  6739. { Move what is now the LEA instruction to before the SUB instruction }
  6740. Asml.Remove(hp1);
  6741. Asml.InsertBefore(hp1, p);
  6742. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  6743. DebugMsg(SPeepholeOptimization + 'AddMov2LeaAdd', p);
  6744. p := hp1;
  6745. end
  6746. else
  6747. begin
  6748. { Since %reg1 or the flags aren't used afterwards, we can delete p completely }
  6749. RemoveCurrentP(p, hp1);
  6750. DebugMsg(SPeepholeOptimization + 'AddMov2Lea', p);
  6751. end;
  6752. Result := True;
  6753. end;
  6754. end;
  6755. end;
  6756. function TX86AsmOptimizer.OptPass2Lea(var p : tai) : Boolean;
  6757. begin
  6758. Result:=false;
  6759. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  6760. begin
  6761. if MatchReference(taicpu(p).oper[0]^.ref^,taicpu(p).oper[1]^.reg,NR_INVALID) and
  6762. (taicpu(p).oper[0]^.ref^.index<>NR_NO) then
  6763. begin
  6764. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.base);
  6765. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.index);
  6766. taicpu(p).opcode:=A_ADD;
  6767. DebugMsg(SPeepholeOptimization + 'Lea2AddBase done',p);
  6768. result:=true;
  6769. end
  6770. else if MatchReference(taicpu(p).oper[0]^.ref^,NR_INVALID,taicpu(p).oper[1]^.reg) and
  6771. (taicpu(p).oper[0]^.ref^.base<>NR_NO) then
  6772. begin
  6773. taicpu(p).loadreg(1,taicpu(p).oper[0]^.ref^.index);
  6774. taicpu(p).loadreg(0,taicpu(p).oper[0]^.ref^.base);
  6775. taicpu(p).opcode:=A_ADD;
  6776. DebugMsg(SPeepholeOptimization + 'Lea2AddIndex done',p);
  6777. result:=true;
  6778. end;
  6779. end;
  6780. end;
  6781. function TX86AsmOptimizer.OptPass2SUB(var p: tai): Boolean;
  6782. var
  6783. hp1: tai; NewRef: TReference;
  6784. begin
  6785. { Change:
  6786. subl/q $x,%reg1
  6787. movl/q %reg1,%reg2
  6788. To:
  6789. leal/q $-x(%reg1),%reg2
  6790. subl/q $x,%reg1 (can be removed if %reg1 or the flags are not used afterwards)
  6791. Breaks the dependency chain and potentially permits the removal of
  6792. a CMP instruction if one follows.
  6793. }
  6794. Result := False;
  6795. if (taicpu(p).opsize in [S_L{$ifdef x86_64}, S_Q{$endif x86_64}]) and
  6796. MatchOpType(taicpu(p),top_const,top_reg) and
  6797. GetNextInstruction(p, hp1) and
  6798. MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
  6799. (taicpu(hp1).oper[1]^.typ = top_reg) and
  6800. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^.reg) and
  6801. (
  6802. { Don't do SubMov2LeaSub under -Os, but do allow SubMov2Lea }
  6803. not (cs_opt_size in current_settings.optimizerswitches) or
  6804. (
  6805. not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs) and
  6806. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)
  6807. )
  6808. ) then
  6809. begin
  6810. { Change the MOV instruction to a LEA instruction, and update the
  6811. first operand }
  6812. reference_reset(NewRef, 1, []);
  6813. NewRef.base := taicpu(p).oper[1]^.reg;
  6814. NewRef.scalefactor := 1;
  6815. NewRef.offset := -taicpu(p).oper[0]^.val;
  6816. taicpu(hp1).opcode := A_LEA;
  6817. taicpu(hp1).loadref(0, NewRef);
  6818. TransferUsedRegs(TmpUsedRegs);
  6819. UpdateUsedRegs(TmpUsedRegs, tai(p.Next));
  6820. if RegUsedAfterInstruction(NewRef.base, hp1, TmpUsedRegs) or
  6821. RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs) then
  6822. begin
  6823. { Move what is now the LEA instruction to before the SUB instruction }
  6824. Asml.Remove(hp1);
  6825. Asml.InsertBefore(hp1, p);
  6826. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, p, UsedRegs);
  6827. DebugMsg(SPeepholeOptimization + 'SubMov2LeaSub', p);
  6828. p := hp1;
  6829. end
  6830. else
  6831. begin
  6832. { Since %reg1 or the flags aren't used afterwards, we can delete p completely }
  6833. RemoveCurrentP(p, hp1);
  6834. DebugMsg(SPeepholeOptimization + 'SubMov2Lea', p);
  6835. end;
  6836. Result := True;
  6837. end;
  6838. end;
  6839. function TX86AsmOptimizer.SkipSimpleInstructions(var hp1 : tai) : Boolean;
  6840. begin
  6841. { we can skip all instructions not messing with the stack pointer }
  6842. while assigned(hp1) and {MatchInstruction(hp1,[A_LEA,A_MOV,A_MOVQ,A_MOVSQ,A_MOVSX,A_MOVSXD,A_MOVZX,
  6843. A_AND,A_OR,A_XOR,A_ADD,A_SHR,A_SHL,A_IMUL,A_SETcc,A_SAR,A_SUB,A_TEST,A_CMOVcc,
  6844. A_MOVSS,A_MOVSD,A_MOVAPS,A_MOVUPD,A_MOVAPD,A_MOVUPS,
  6845. A_VMOVSS,A_VMOVSD,A_VMOVAPS,A_VMOVUPD,A_VMOVAPD,A_VMOVUPS],[]) and}
  6846. ({(taicpu(hp1).ops=0) or }
  6847. ({(MatchOpType(taicpu(hp1),top_reg,top_reg) or MatchOpType(taicpu(hp1),top_const,top_reg) or
  6848. (MatchOpType(taicpu(hp1),top_ref,top_reg))
  6849. ) and }
  6850. not(RegInInstruction(NR_STACK_POINTER_REG,hp1)) { and not(RegInInstruction(NR_FRAME_POINTER_REG,hp1))}
  6851. )
  6852. ) do
  6853. GetNextInstruction(hp1,hp1);
  6854. Result:=assigned(hp1);
  6855. end;
  6856. function TX86AsmOptimizer.PostPeepholeOptLea(var p : tai) : Boolean;
  6857. var
  6858. hp1, hp2, hp3, hp4, hp5: tai;
  6859. begin
  6860. Result:=false;
  6861. hp5:=nil;
  6862. { replace
  6863. leal(q) x(<stackpointer>),<stackpointer>
  6864. call procname
  6865. leal(q) -x(<stackpointer>),<stackpointer>
  6866. ret
  6867. by
  6868. jmp procname
  6869. but do it only on level 4 because it destroys stack back traces
  6870. }
  6871. if (cs_opt_level4 in current_settings.optimizerswitches) and
  6872. MatchOpType(taicpu(p),top_ref,top_reg) and
  6873. (taicpu(p).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  6874. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  6875. { the -8 or -24 are not required, but bail out early if possible,
  6876. higher values are unlikely }
  6877. ((taicpu(p).oper[0]^.ref^.offset=-8) or
  6878. (taicpu(p).oper[0]^.ref^.offset=-24)) and
  6879. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  6880. (taicpu(p).oper[0]^.ref^.relsymbol=nil) and
  6881. (taicpu(p).oper[0]^.ref^.segment=NR_NO) and
  6882. (taicpu(p).oper[1]^.reg=NR_STACK_POINTER_REG) and
  6883. GetNextInstruction(p, hp1) and
  6884. { Take a copy of hp1 }
  6885. SetAndTest(hp1, hp4) and
  6886. { trick to skip label }
  6887. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  6888. SkipSimpleInstructions(hp1) and
  6889. MatchInstruction(hp1,A_CALL,[S_NO]) and
  6890. GetNextInstruction(hp1, hp2) and
  6891. MatchInstruction(hp2,A_LEA,[taicpu(p).opsize]) and
  6892. MatchOpType(taicpu(hp2),top_ref,top_reg) and
  6893. (taicpu(hp2).oper[0]^.ref^.offset=-taicpu(p).oper[0]^.ref^.offset) and
  6894. (taicpu(hp2).oper[0]^.ref^.base=NR_STACK_POINTER_REG) and
  6895. (taicpu(hp2).oper[0]^.ref^.index=NR_NO) and
  6896. (taicpu(hp2).oper[0]^.ref^.symbol=nil) and
  6897. (taicpu(hp2).oper[0]^.ref^.relsymbol=nil) and
  6898. (taicpu(hp2).oper[0]^.ref^.segment=NR_NO) and
  6899. (taicpu(hp2).oper[1]^.reg=NR_STACK_POINTER_REG) and
  6900. GetNextInstruction(hp2, hp3) and
  6901. { trick to skip label }
  6902. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  6903. (MatchInstruction(hp3,A_RET,[S_NO]) or
  6904. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  6905. SetAndTest(hp3,hp5) and
  6906. GetNextInstruction(hp3,hp3) and
  6907. MatchInstruction(hp3,A_RET,[S_NO])
  6908. )
  6909. ) and
  6910. (taicpu(hp3).ops=0) then
  6911. begin
  6912. taicpu(hp1).opcode := A_JMP;
  6913. taicpu(hp1).is_jmp := true;
  6914. DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
  6915. RemoveCurrentP(p, hp4);
  6916. RemoveInstruction(hp2);
  6917. RemoveInstruction(hp3);
  6918. if Assigned(hp5) then
  6919. begin
  6920. AsmL.Remove(hp5);
  6921. ASmL.InsertBefore(hp5,hp1)
  6922. end;
  6923. Result:=true;
  6924. end;
  6925. end;
  6926. function TX86AsmOptimizer.PostPeepholeOptPush(var p : tai) : Boolean;
  6927. {$ifdef x86_64}
  6928. var
  6929. hp1, hp2, hp3, hp4, hp5: tai;
  6930. {$endif x86_64}
  6931. begin
  6932. Result:=false;
  6933. {$ifdef x86_64}
  6934. hp5:=nil;
  6935. { replace
  6936. push %rax
  6937. call procname
  6938. pop %rcx
  6939. ret
  6940. by
  6941. jmp procname
  6942. but do it only on level 4 because it destroys stack back traces
  6943. It depends on the fact, that the sequence push rax/pop rcx is used for stack alignment as rcx is volatile
  6944. for all supported calling conventions
  6945. }
  6946. if (cs_opt_level4 in current_settings.optimizerswitches) and
  6947. MatchOpType(taicpu(p),top_reg) and
  6948. (taicpu(p).oper[0]^.reg=NR_RAX) and
  6949. GetNextInstruction(p, hp1) and
  6950. { Take a copy of hp1 }
  6951. SetAndTest(hp1, hp4) and
  6952. { trick to skip label }
  6953. ((hp1.typ=ait_instruction) or GetNextInstruction(hp1, hp1)) and
  6954. SkipSimpleInstructions(hp1) and
  6955. MatchInstruction(hp1,A_CALL,[S_NO]) and
  6956. GetNextInstruction(hp1, hp2) and
  6957. MatchInstruction(hp2,A_POP,[taicpu(p).opsize]) and
  6958. MatchOpType(taicpu(hp2),top_reg) and
  6959. (taicpu(hp2).oper[0]^.reg=NR_RCX) and
  6960. GetNextInstruction(hp2, hp3) and
  6961. { trick to skip label }
  6962. ((hp3.typ=ait_instruction) or GetNextInstruction(hp3, hp3)) and
  6963. (MatchInstruction(hp3,A_RET,[S_NO]) or
  6964. (MatchInstruction(hp3,A_VZEROUPPER,[S_NO]) and
  6965. SetAndTest(hp3,hp5) and
  6966. GetNextInstruction(hp3,hp3) and
  6967. MatchInstruction(hp3,A_RET,[S_NO])
  6968. )
  6969. ) and
  6970. (taicpu(hp3).ops=0) then
  6971. begin
  6972. taicpu(hp1).opcode := A_JMP;
  6973. taicpu(hp1).is_jmp := true;
  6974. DebugMsg(SPeepholeOptimization + 'PushCallPushRet2Jmp done',p);
  6975. RemoveCurrentP(p, hp4);
  6976. RemoveInstruction(hp2);
  6977. RemoveInstruction(hp3);
  6978. if Assigned(hp5) then
  6979. begin
  6980. AsmL.Remove(hp5);
  6981. ASmL.InsertBefore(hp5,hp1)
  6982. end;
  6983. Result:=true;
  6984. end;
  6985. {$endif x86_64}
  6986. end;
  6987. function TX86AsmOptimizer.PostPeepholeOptMov(var p : tai) : Boolean;
  6988. var
  6989. Value, RegName: string;
  6990. begin
  6991. Result:=false;
  6992. if (taicpu(p).oper[1]^.typ = top_reg) and (taicpu(p).oper[0]^.typ = top_const) then
  6993. begin
  6994. case taicpu(p).oper[0]^.val of
  6995. 0:
  6996. { Don't make this optimisation if the CPU flags are required, since XOR scrambles them }
  6997. if not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  6998. begin
  6999. { change "mov $0,%reg" into "xor %reg,%reg" }
  7000. taicpu(p).opcode := A_XOR;
  7001. taicpu(p).loadReg(0,taicpu(p).oper[1]^.reg);
  7002. Result := True;
  7003. end;
  7004. $1..$FFFFFFFF:
  7005. begin
  7006. { Code size reduction by J. Gareth "Kit" Moreton }
  7007. { change 64-bit register to 32-bit register to reduce code size (upper 32 bits will be set to zero) }
  7008. case taicpu(p).opsize of
  7009. S_Q:
  7010. begin
  7011. RegName := debug_regname(taicpu(p).oper[1]^.reg); { 64-bit register name }
  7012. Value := debug_tostr(taicpu(p).oper[0]^.val);
  7013. { The actual optimization }
  7014. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  7015. taicpu(p).changeopsize(S_L);
  7016. DebugMsg(SPeepholeOptimization + 'movq $' + Value + ',' + RegName + ' -> movl $' + Value + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (immediate can be represented with just 32 bits)', p);
  7017. Result := True;
  7018. end;
  7019. else
  7020. { Do nothing };
  7021. end;
  7022. end;
  7023. -1:
  7024. { Don't make this optimisation if the CPU flags are required, since OR scrambles them }
  7025. if (cs_opt_size in current_settings.optimizerswitches) and
  7026. (taicpu(p).opsize <> S_B) and
  7027. not (RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  7028. begin
  7029. { change "mov $-1,%reg" into "or $-1,%reg" }
  7030. { NOTES:
  7031. - No size saving is made when changing a Word-sized assignment unless the register is AX (smaller encoding)
  7032. - This operation creates a false dependency on the register, so only do it when optimising for size
  7033. - 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
  7034. }
  7035. taicpu(p).opcode := A_OR;
  7036. Result := True;
  7037. end;
  7038. end;
  7039. end;
  7040. end;
  7041. function TX86AsmOptimizer.PostPeepholeOptAnd(var p : tai) : boolean;
  7042. var
  7043. hp1: tai;
  7044. begin
  7045. { Detect:
  7046. andw x, %ax (0 <= x < $8000)
  7047. ...
  7048. movzwl %ax,%eax
  7049. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  7050. }
  7051. Result := False;
  7052. if MatchOpType(taicpu(p), top_const, top_reg) and
  7053. (taicpu(p).oper[1]^.reg = NR_AX) and { This is also enough to determine that opsize = S_W }
  7054. ((taicpu(p).oper[0]^.val and $7FFF) = taicpu(p).oper[0]^.val) and
  7055. GetNextInstructionUsingReg(p, hp1, NR_EAX) and
  7056. MatchInstruction(hp1, A_MOVZX, [S_WL]) and
  7057. MatchOperand(taicpu(hp1).oper[0]^, NR_AX) and
  7058. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) then
  7059. begin
  7060. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via AndMovz2AndCwtl)', hp1);
  7061. taicpu(hp1).opcode := A_CWDE;
  7062. taicpu(hp1).clearop(0);
  7063. taicpu(hp1).clearop(1);
  7064. taicpu(hp1).ops := 0;
  7065. { A change was made, but not with p, so move forward 1 }
  7066. p := tai(p.Next);
  7067. Result := True;
  7068. end;
  7069. end;
  7070. function TX86AsmOptimizer.PostPeepholeOptMOVSX(var p : tai) : boolean;
  7071. begin
  7072. Result := False;
  7073. if not MatchOpType(taicpu(p), top_reg, top_reg) then
  7074. Exit;
  7075. { Convert:
  7076. movswl %ax,%eax -> cwtl
  7077. movslq %eax,%rax -> cdqe
  7078. NOTE: Don't convert movswl %al,%ax to cbw, because cbw and cwde
  7079. refer to the same opcode and depends only on the assembler's
  7080. current operand-size attribute. [Kit]
  7081. }
  7082. with taicpu(p) do
  7083. case opsize of
  7084. S_WL:
  7085. if (oper[0]^.reg = NR_AX) and (oper[1]^.reg = NR_EAX) then
  7086. begin
  7087. DebugMsg(SPeepholeOptimization + 'Converted movswl %ax,%eax to cwtl', p);
  7088. opcode := A_CWDE;
  7089. clearop(0);
  7090. clearop(1);
  7091. ops := 0;
  7092. Result := True;
  7093. end;
  7094. {$ifdef x86_64}
  7095. S_LQ:
  7096. if (oper[0]^.reg = NR_EAX) and (oper[1]^.reg = NR_RAX) then
  7097. begin
  7098. DebugMsg(SPeepholeOptimization + 'Converted movslq %eax,%rax to cltq', p);
  7099. opcode := A_CDQE;
  7100. clearop(0);
  7101. clearop(1);
  7102. ops := 0;
  7103. Result := True;
  7104. end;
  7105. {$endif x86_64}
  7106. else
  7107. ;
  7108. end;
  7109. end;
  7110. function TX86AsmOptimizer.PostPeepholeOptShr(var p : tai) : boolean;
  7111. var
  7112. hp1: tai;
  7113. begin
  7114. { Detect:
  7115. shr x, %ax (x > 0)
  7116. ...
  7117. movzwl %ax,%eax
  7118. Change movzwl %ax,%eax to cwtl (shorter encoding for movswl %ax,%eax)
  7119. }
  7120. Result := False;
  7121. if MatchOpType(taicpu(p), top_const, top_reg) and
  7122. (taicpu(p).oper[1]^.reg = NR_AX) and { This is also enough to determine that opsize = S_W }
  7123. (taicpu(p).oper[0]^.val > 0) and
  7124. GetNextInstructionUsingReg(p, hp1, NR_EAX) and
  7125. MatchInstruction(hp1, A_MOVZX, [S_WL]) and
  7126. MatchOperand(taicpu(hp1).oper[0]^, NR_AX) and
  7127. MatchOperand(taicpu(hp1).oper[1]^, NR_EAX) then
  7128. begin
  7129. DebugMsg(SPeepholeOptimization + 'Converted movzwl %ax,%eax to cwtl (via ShrMovz2ShrCwtl)', hp1);
  7130. taicpu(hp1).opcode := A_CWDE;
  7131. taicpu(hp1).clearop(0);
  7132. taicpu(hp1).clearop(1);
  7133. taicpu(hp1).ops := 0;
  7134. { A change was made, but not with p, so move forward 1 }
  7135. p := tai(p.Next);
  7136. Result := True;
  7137. end;
  7138. end;
  7139. function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean;
  7140. begin
  7141. Result:=false;
  7142. { change "cmp $0, %reg" to "test %reg, %reg" }
  7143. if MatchOpType(taicpu(p),top_const,top_reg) and
  7144. (taicpu(p).oper[0]^.val = 0) then
  7145. begin
  7146. taicpu(p).opcode := A_TEST;
  7147. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  7148. Result:=true;
  7149. end;
  7150. end;
  7151. function TX86AsmOptimizer.PostPeepholeOptTestOr(var p : tai) : Boolean;
  7152. var
  7153. IsTestConstX : Boolean;
  7154. hp1,hp2 : tai;
  7155. begin
  7156. Result:=false;
  7157. { removes the line marked with (x) from the sequence
  7158. and/or/xor/add/sub/... $x, %y
  7159. test/or %y, %y | test $-1, %y (x)
  7160. j(n)z _Label
  7161. as the first instruction already adjusts the ZF
  7162. %y operand may also be a reference }
  7163. IsTestConstX:=(taicpu(p).opcode=A_TEST) and
  7164. MatchOperand(taicpu(p).oper[0]^,-1);
  7165. if (OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) or IsTestConstX) and
  7166. GetLastInstruction(p, hp1) and
  7167. (tai(hp1).typ = ait_instruction) and
  7168. GetNextInstruction(p,hp2) and
  7169. MatchInstruction(hp2,A_SETcc,A_Jcc,A_CMOVcc,[]) then
  7170. case taicpu(hp1).opcode Of
  7171. A_ADD, A_SUB, A_OR, A_XOR, A_AND:
  7172. begin
  7173. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  7174. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  7175. { and in case of carry for A(E)/B(E)/C/NC }
  7176. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  7177. ((taicpu(hp1).opcode <> A_ADD) and
  7178. (taicpu(hp1).opcode <> A_SUB))) then
  7179. begin
  7180. RemoveCurrentP(p, hp2);
  7181. Result:=true;
  7182. end;
  7183. end;
  7184. A_SHL, A_SAL, A_SHR, A_SAR:
  7185. begin
  7186. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) and
  7187. { SHL/SAL/SHR/SAR with a value of 0 do not change the flags }
  7188. { therefore, it's only safe to do this optimization for }
  7189. { shifts by a (nonzero) constant }
  7190. (taicpu(hp1).oper[0]^.typ = top_const) and
  7191. (taicpu(hp1).oper[0]^.val <> 0) and
  7192. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  7193. { and in case of carry for A(E)/B(E)/C/NC }
  7194. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  7195. begin
  7196. RemoveCurrentP(p, hp2);
  7197. Result:=true;
  7198. end;
  7199. end;
  7200. A_DEC, A_INC, A_NEG:
  7201. begin
  7202. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) and
  7203. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  7204. { and in case of carry for A(E)/B(E)/C/NC }
  7205. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  7206. begin
  7207. case taicpu(hp1).opcode of
  7208. A_DEC, A_INC:
  7209. { replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag }
  7210. begin
  7211. case taicpu(hp1).opcode Of
  7212. A_DEC: taicpu(hp1).opcode := A_SUB;
  7213. A_INC: taicpu(hp1).opcode := A_ADD;
  7214. else
  7215. ;
  7216. end;
  7217. taicpu(hp1).loadoper(1,taicpu(hp1).oper[0]^);
  7218. taicpu(hp1).loadConst(0,1);
  7219. taicpu(hp1).ops:=2;
  7220. end;
  7221. else
  7222. ;
  7223. end;
  7224. RemoveCurrentP(p, hp2);
  7225. Result:=true;
  7226. end;
  7227. end
  7228. else
  7229. { change "test $-1,%reg" into "test %reg,%reg" }
  7230. if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  7231. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  7232. end { case }
  7233. { change "test $-1,%reg" into "test %reg,%reg" }
  7234. else if IsTestConstX and (taicpu(p).oper[1]^.typ=top_reg) then
  7235. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  7236. end;
  7237. function TX86AsmOptimizer.PostPeepholeOptCall(var p : tai) : Boolean;
  7238. var
  7239. hp1,hp3 : tai;
  7240. {$ifndef x86_64}
  7241. hp2 : taicpu;
  7242. {$endif x86_64}
  7243. begin
  7244. Result:=false;
  7245. hp3:=nil;
  7246. {$ifndef x86_64}
  7247. { don't do this on modern CPUs, this really hurts them due to
  7248. broken call/ret pairing }
  7249. if (current_settings.optimizecputype < cpu_Pentium2) and
  7250. not(cs_create_pic in current_settings.moduleswitches) and
  7251. GetNextInstruction(p, hp1) and
  7252. MatchInstruction(hp1,A_JMP,[S_NO]) and
  7253. MatchOpType(taicpu(hp1),top_ref) and
  7254. (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full) then
  7255. begin
  7256. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  7257. InsertLLItem(p.previous, p, hp2);
  7258. taicpu(p).opcode := A_JMP;
  7259. taicpu(p).is_jmp := true;
  7260. RemoveInstruction(hp1);
  7261. Result:=true;
  7262. end
  7263. else
  7264. {$endif x86_64}
  7265. { replace
  7266. call procname
  7267. ret
  7268. by
  7269. jmp procname
  7270. but do it only on level 4 because it destroys stack back traces
  7271. else if the subroutine is marked as no return, remove the ret
  7272. }
  7273. if ((cs_opt_level4 in current_settings.optimizerswitches) or
  7274. (po_noreturn in current_procinfo.procdef.procoptions)) and
  7275. GetNextInstruction(p, hp1) and
  7276. (MatchInstruction(hp1,A_RET,[S_NO]) or
  7277. (MatchInstruction(hp1,A_VZEROUPPER,[S_NO]) and
  7278. SetAndTest(hp1,hp3) and
  7279. GetNextInstruction(hp1,hp1) and
  7280. MatchInstruction(hp1,A_RET,[S_NO])
  7281. )
  7282. ) and
  7283. (taicpu(hp1).ops=0) then
  7284. begin
  7285. if (cs_opt_level4 in current_settings.optimizerswitches) and
  7286. { we might destroy stack alignment here if we do not do a call }
  7287. (target_info.stackalign<=sizeof(SizeUInt)) then
  7288. begin
  7289. taicpu(p).opcode := A_JMP;
  7290. taicpu(p).is_jmp := true;
  7291. DebugMsg(SPeepholeOptimization + 'CallRet2Jmp done',p);
  7292. end
  7293. else
  7294. DebugMsg(SPeepholeOptimization + 'CallRet2Call done',p);
  7295. RemoveInstruction(hp1);
  7296. if Assigned(hp3) then
  7297. begin
  7298. AsmL.Remove(hp3);
  7299. AsmL.InsertBefore(hp3,p)
  7300. end;
  7301. Result:=true;
  7302. end;
  7303. end;
  7304. function TX86AsmOptimizer.PostPeepholeOptMovzx(var p : tai) : Boolean;
  7305. function ConstInRange(const Val: TCGInt; const OpSize: TOpSize): Boolean;
  7306. begin
  7307. case OpSize of
  7308. S_B, S_BW, S_BL{$ifdef x86_64}, S_BQ{$endif x86_64}:
  7309. Result := (Val <= $FF) and (Val >= -128);
  7310. S_W, S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
  7311. Result := (Val <= $FFFF) and (Val >= -32768);
  7312. S_L{$ifdef x86_64}, S_LQ{$endif x86_64}:
  7313. Result := (Val <= $FFFFFFFF) and (Val >= -2147483648);
  7314. else
  7315. Result := True;
  7316. end;
  7317. end;
  7318. var
  7319. hp1, hp2 : tai;
  7320. SizeChange: Boolean;
  7321. PreMessage: string;
  7322. begin
  7323. Result := False;
  7324. if (taicpu(p).oper[0]^.typ = top_reg) and
  7325. SuperRegistersEqual(taicpu(p).oper[0]^.reg, taicpu(p).oper[1]^.reg) and
  7326. GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) then
  7327. begin
  7328. { Change (using movzbl %al,%eax as an example):
  7329. movzbl %al, %eax movzbl %al, %eax
  7330. cmpl x, %eax testl %eax,%eax
  7331. To:
  7332. cmpb x, %al testb %al, %al (Move one back to avoid a false dependency)
  7333. movzbl %al, %eax movzbl %al, %eax
  7334. Smaller instruction and minimises pipeline stall as the CPU
  7335. doesn't have to wait for the register to get zero-extended. [Kit]
  7336. Also allow if the smaller of the two registers is being checked,
  7337. as this still removes the false dependency.
  7338. }
  7339. if
  7340. (
  7341. (
  7342. (taicpu(hp1).opcode = A_CMP) and MatchOpType(taicpu(hp1), top_const, top_reg) and
  7343. ConstInRange(taicpu(hp1).oper[0]^.val, taicpu(p).opsize)
  7344. ) or (
  7345. { If MatchOperand returns True, they must both be registers }
  7346. (taicpu(hp1).opcode = A_TEST) and MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)
  7347. )
  7348. ) and
  7349. (reg2opsize(taicpu(hp1).oper[1]^.reg) <= reg2opsize(taicpu(p).oper[1]^.reg)) and
  7350. SuperRegistersEqual(taicpu(p).oper[1]^.reg, taicpu(hp1).oper[1]^.reg) then
  7351. begin
  7352. 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);
  7353. asml.Remove(hp1);
  7354. asml.InsertBefore(hp1, p);
  7355. { Swap instructions in the case of cmp 0,%reg or test %reg,%reg }
  7356. if (taicpu(hp1).opcode = A_TEST) or (taicpu(hp1).oper[0]^.val = 0) then
  7357. begin
  7358. taicpu(hp1).opcode := A_TEST;
  7359. taicpu(hp1).loadreg(0, taicpu(p).oper[0]^.reg);
  7360. end;
  7361. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[0]^.reg;
  7362. case taicpu(p).opsize of
  7363. S_BW, S_BL:
  7364. begin
  7365. SizeChange := taicpu(hp1).opsize <> S_B;
  7366. taicpu(hp1).changeopsize(S_B);
  7367. end;
  7368. S_WL:
  7369. begin
  7370. SizeChange := taicpu(hp1).opsize <> S_W;
  7371. taicpu(hp1).changeopsize(S_W);
  7372. end
  7373. else
  7374. InternalError(2020112701);
  7375. end;
  7376. UpdateUsedRegs(tai(p.Next));
  7377. { Check if the register is used aferwards - if not, we can
  7378. remove the movzx instruction completely }
  7379. if not RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, p, UsedRegs) then
  7380. begin
  7381. { Hp1 is a better position than p for debugging purposes }
  7382. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4a', hp1);
  7383. RemoveCurrentp(p, hp1);
  7384. Result := True;
  7385. end;
  7386. if SizeChange then
  7387. DebugMsg(SPeepholeOptimization + PreMessage +
  7388. 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)
  7389. else
  7390. DebugMsg(SPeepholeOptimization + 'MovzxCmp2CmpMovzx', hp1);
  7391. Exit;
  7392. end;
  7393. { Change (using movzwl %ax,%eax as an example):
  7394. movzwl %ax, %eax
  7395. movb %al, (dest) (Register is smaller than read register in movz)
  7396. To:
  7397. movb %al, (dest) (Move one back to avoid a false dependency)
  7398. movzwl %ax, %eax
  7399. }
  7400. if (taicpu(hp1).opcode = A_MOV) and
  7401. (taicpu(hp1).oper[0]^.typ = top_reg) and
  7402. not RegInOp(taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^) and
  7403. SuperRegistersEqual(taicpu(hp1).oper[0]^.reg, taicpu(p).oper[0]^.reg) and
  7404. (reg2opsize(taicpu(hp1).oper[0]^.reg) <= reg2opsize(taicpu(p).oper[0]^.reg)) then
  7405. begin
  7406. DebugMsg(SPeepholeOptimization + 'MovzxMov2MovMovzx', hp1);
  7407. hp2 := tai(hp1.Previous); { Effectively the old position of hp1 }
  7408. asml.Remove(hp1);
  7409. asml.InsertBefore(hp1, p);
  7410. if taicpu(hp1).oper[1]^.typ = top_reg then
  7411. AllocRegBetween(taicpu(hp1).oper[1]^.reg, hp1, hp2, UsedRegs);
  7412. { Check if the register is used aferwards - if not, we can
  7413. remove the movzx instruction completely }
  7414. if not RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, p, UsedRegs) then
  7415. begin
  7416. { Hp1 is a better position than p for debugging purposes }
  7417. DebugMsg(SPeepholeOptimization + 'Movzx2Nop 4b', hp1);
  7418. RemoveCurrentp(p, hp1);
  7419. Result := True;
  7420. end;
  7421. Exit;
  7422. end;
  7423. end;
  7424. {$ifdef x86_64}
  7425. { Code size reduction by J. Gareth "Kit" Moreton }
  7426. { Convert MOVZBQ and MOVZWQ to MOVZBL and MOVZWL respectively if it removes the REX prefix }
  7427. if (taicpu(p).opsize in [S_BQ, S_WQ]) and
  7428. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_RAX, RS_RCX, RS_RDX, RS_RBX, RS_RSI, RS_RDI, RS_RBP, RS_RSP])
  7429. then
  7430. begin
  7431. { Has 64-bit register name and opcode suffix }
  7432. PreMessage := 'movz' + debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' -> movz';
  7433. { The actual optimization }
  7434. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  7435. if taicpu(p).opsize = S_BQ then
  7436. taicpu(p).changeopsize(S_BL)
  7437. else
  7438. taicpu(p).changeopsize(S_WL);
  7439. DebugMsg(SPeepholeOptimization + PreMessage +
  7440. debug_opsize2str(taicpu(p).opsize) + ' ' + debug_operstr(taicpu(p).oper[0]^) + ',' + debug_regname(taicpu(p).oper[1]^.reg) + ' (removes REX prefix)', p);
  7441. end;
  7442. {$endif}
  7443. end;
  7444. {$ifdef x86_64}
  7445. function TX86AsmOptimizer.PostPeepholeOptXor(var p : tai) : Boolean;
  7446. var
  7447. PreMessage, RegName: string;
  7448. begin
  7449. { Code size reduction by J. Gareth "Kit" Moreton }
  7450. { change "xorq %reg,%reg" to "xorl %reg,%reg" for %rax, %rcx, %rdx, %rbx, %rsi, %rdi, %rbp and %rsp,
  7451. as this removes the REX prefix }
  7452. Result := False;
  7453. if not OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  7454. Exit;
  7455. if taicpu(p).oper[0]^.typ <> top_reg then
  7456. { Should be impossible if both operands were equal, since one of XOR's operands must be a register }
  7457. InternalError(2018011500);
  7458. case taicpu(p).opsize of
  7459. S_Q:
  7460. begin
  7461. 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
  7462. begin
  7463. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 64-bit register name }
  7464. PreMessage := 'xorq ' + RegName + ',' + RegName + ' -> xorl ';
  7465. { The actual optimization }
  7466. setsubreg(taicpu(p).oper[0]^.reg, R_SUBD);
  7467. setsubreg(taicpu(p).oper[1]^.reg, R_SUBD);
  7468. taicpu(p).changeopsize(S_L);
  7469. RegName := debug_regname(taicpu(p).oper[0]^.reg); { 32-bit register name }
  7470. DebugMsg(SPeepholeOptimization + PreMessage + RegName + ',' + RegName + ' (removes REX prefix)', p);
  7471. end;
  7472. end;
  7473. else
  7474. ;
  7475. end;
  7476. end;
  7477. {$endif}
  7478. class procedure TX86AsmOptimizer.OptimizeRefs(var p: taicpu);
  7479. var
  7480. OperIdx: Integer;
  7481. begin
  7482. for OperIdx := 0 to p.ops - 1 do
  7483. if p.oper[OperIdx]^.typ = top_ref then
  7484. optimize_ref(p.oper[OperIdx]^.ref^, False);
  7485. end;
  7486. end.