aoptx86.pas 369 KB

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