aoptx86.pas 347 KB

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