ARMBaseInstrInfo.cpp 236 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728
  1. //===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the Base ARM implementation of the TargetInstrInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ARMBaseInstrInfo.h"
  13. #include "ARMBaseRegisterInfo.h"
  14. #include "ARMConstantPoolValue.h"
  15. #include "ARMFeatures.h"
  16. #include "ARMHazardRecognizer.h"
  17. #include "ARMMachineFunctionInfo.h"
  18. #include "ARMSubtarget.h"
  19. #include "MCTargetDesc/ARMAddressingModes.h"
  20. #include "MCTargetDesc/ARMBaseInfo.h"
  21. #include "MVETailPredUtils.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/STLExtras.h"
  24. #include "llvm/ADT/SmallSet.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/ADT/Triple.h"
  27. #include "llvm/CodeGen/LiveVariables.h"
  28. #include "llvm/CodeGen/MachineBasicBlock.h"
  29. #include "llvm/CodeGen/MachineConstantPool.h"
  30. #include "llvm/CodeGen/MachineFrameInfo.h"
  31. #include "llvm/CodeGen/MachineFunction.h"
  32. #include "llvm/CodeGen/MachineInstr.h"
  33. #include "llvm/CodeGen/MachineInstrBuilder.h"
  34. #include "llvm/CodeGen/MachineMemOperand.h"
  35. #include "llvm/CodeGen/MachineModuleInfo.h"
  36. #include "llvm/CodeGen/MachineOperand.h"
  37. #include "llvm/CodeGen/MachineRegisterInfo.h"
  38. #include "llvm/CodeGen/MachineScheduler.h"
  39. #include "llvm/CodeGen/MultiHazardRecognizer.h"
  40. #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
  41. #include "llvm/CodeGen/SelectionDAGNodes.h"
  42. #include "llvm/CodeGen/TargetInstrInfo.h"
  43. #include "llvm/CodeGen/TargetRegisterInfo.h"
  44. #include "llvm/CodeGen/TargetSchedule.h"
  45. #include "llvm/IR/Attributes.h"
  46. #include "llvm/IR/Constants.h"
  47. #include "llvm/IR/DebugLoc.h"
  48. #include "llvm/IR/Function.h"
  49. #include "llvm/IR/GlobalValue.h"
  50. #include "llvm/MC/MCAsmInfo.h"
  51. #include "llvm/MC/MCInstrDesc.h"
  52. #include "llvm/MC/MCInstrItineraries.h"
  53. #include "llvm/Support/BranchProbability.h"
  54. #include "llvm/Support/Casting.h"
  55. #include "llvm/Support/CommandLine.h"
  56. #include "llvm/Support/Compiler.h"
  57. #include "llvm/Support/Debug.h"
  58. #include "llvm/Support/ErrorHandling.h"
  59. #include "llvm/Support/raw_ostream.h"
  60. #include "llvm/Target/TargetMachine.h"
  61. #include <algorithm>
  62. #include <cassert>
  63. #include <cstdint>
  64. #include <iterator>
  65. #include <new>
  66. #include <utility>
  67. #include <vector>
  68. using namespace llvm;
  69. #define DEBUG_TYPE "arm-instrinfo"
  70. #define GET_INSTRINFO_CTOR_DTOR
  71. #include "ARMGenInstrInfo.inc"
  72. static cl::opt<bool>
  73. EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
  74. cl::desc("Enable ARM 2-addr to 3-addr conv"));
  75. /// ARM_MLxEntry - Record information about MLA / MLS instructions.
  76. struct ARM_MLxEntry {
  77. uint16_t MLxOpc; // MLA / MLS opcode
  78. uint16_t MulOpc; // Expanded multiplication opcode
  79. uint16_t AddSubOpc; // Expanded add / sub opcode
  80. bool NegAcc; // True if the acc is negated before the add / sub.
  81. bool HasLane; // True if instruction has an extra "lane" operand.
  82. };
  83. static const ARM_MLxEntry ARM_MLxTable[] = {
  84. // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
  85. // fp scalar ops
  86. { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
  87. { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
  88. { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
  89. { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
  90. { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
  91. { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
  92. { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
  93. { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
  94. // fp SIMD ops
  95. { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
  96. { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
  97. { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
  98. { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
  99. { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
  100. { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
  101. { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
  102. { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
  103. };
  104. ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
  105. : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
  106. Subtarget(STI) {
  107. for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
  108. if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
  109. llvm_unreachable("Duplicated entries?");
  110. MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
  111. MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
  112. }
  113. }
  114. // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
  115. // currently defaults to no prepass hazard recognizer.
  116. ScheduleHazardRecognizer *
  117. ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
  118. const ScheduleDAG *DAG) const {
  119. if (usePreRAHazardRecognizer()) {
  120. const InstrItineraryData *II =
  121. static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
  122. return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
  123. }
  124. return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
  125. }
  126. // Called during:
  127. // - pre-RA scheduling
  128. // - post-RA scheduling when FeatureUseMISched is set
  129. ScheduleHazardRecognizer *ARMBaseInstrInfo::CreateTargetMIHazardRecognizer(
  130. const InstrItineraryData *II, const ScheduleDAGMI *DAG) const {
  131. MultiHazardRecognizer *MHR = new MultiHazardRecognizer();
  132. // We would like to restrict this hazard recognizer to only
  133. // post-RA scheduling; we can tell that we're post-RA because we don't
  134. // track VRegLiveness.
  135. // Cortex-M7: TRM indicates that there is a single ITCM bank and two DTCM
  136. // banks banked on bit 2. Assume that TCMs are in use.
  137. if (Subtarget.isCortexM7() && !DAG->hasVRegLiveness())
  138. MHR->AddHazardRecognizer(
  139. std::make_unique<ARMBankConflictHazardRecognizer>(DAG, 0x4, true));
  140. // Not inserting ARMHazardRecognizerFPMLx because that would change
  141. // legacy behavior
  142. auto BHR = TargetInstrInfo::CreateTargetMIHazardRecognizer(II, DAG);
  143. MHR->AddHazardRecognizer(std::unique_ptr<ScheduleHazardRecognizer>(BHR));
  144. return MHR;
  145. }
  146. // Called during post-RA scheduling when FeatureUseMISched is not set
  147. ScheduleHazardRecognizer *ARMBaseInstrInfo::
  148. CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
  149. const ScheduleDAG *DAG) const {
  150. MultiHazardRecognizer *MHR = new MultiHazardRecognizer();
  151. if (Subtarget.isThumb2() || Subtarget.hasVFP2Base())
  152. MHR->AddHazardRecognizer(std::make_unique<ARMHazardRecognizerFPMLx>());
  153. auto BHR = TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
  154. if (BHR)
  155. MHR->AddHazardRecognizer(std::unique_ptr<ScheduleHazardRecognizer>(BHR));
  156. return MHR;
  157. }
  158. MachineInstr *
  159. ARMBaseInstrInfo::convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
  160. LiveIntervals *LIS) const {
  161. // FIXME: Thumb2 support.
  162. if (!EnableARM3Addr)
  163. return nullptr;
  164. MachineFunction &MF = *MI.getParent()->getParent();
  165. uint64_t TSFlags = MI.getDesc().TSFlags;
  166. bool isPre = false;
  167. switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
  168. default: return nullptr;
  169. case ARMII::IndexModePre:
  170. isPre = true;
  171. break;
  172. case ARMII::IndexModePost:
  173. break;
  174. }
  175. // Try splitting an indexed load/store to an un-indexed one plus an add/sub
  176. // operation.
  177. unsigned MemOpc = getUnindexedOpcode(MI.getOpcode());
  178. if (MemOpc == 0)
  179. return nullptr;
  180. MachineInstr *UpdateMI = nullptr;
  181. MachineInstr *MemMI = nullptr;
  182. unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
  183. const MCInstrDesc &MCID = MI.getDesc();
  184. unsigned NumOps = MCID.getNumOperands();
  185. bool isLoad = !MI.mayStore();
  186. const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0);
  187. const MachineOperand &Base = MI.getOperand(2);
  188. const MachineOperand &Offset = MI.getOperand(NumOps - 3);
  189. Register WBReg = WB.getReg();
  190. Register BaseReg = Base.getReg();
  191. Register OffReg = Offset.getReg();
  192. unsigned OffImm = MI.getOperand(NumOps - 2).getImm();
  193. ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm();
  194. switch (AddrMode) {
  195. default: llvm_unreachable("Unknown indexed op!");
  196. case ARMII::AddrMode2: {
  197. bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
  198. unsigned Amt = ARM_AM::getAM2Offset(OffImm);
  199. if (OffReg == 0) {
  200. if (ARM_AM::getSOImmVal(Amt) == -1)
  201. // Can't encode it in a so_imm operand. This transformation will
  202. // add more than 1 instruction. Abandon!
  203. return nullptr;
  204. UpdateMI = BuildMI(MF, MI.getDebugLoc(),
  205. get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
  206. .addReg(BaseReg)
  207. .addImm(Amt)
  208. .add(predOps(Pred))
  209. .add(condCodeOp());
  210. } else if (Amt != 0) {
  211. ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
  212. unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
  213. UpdateMI = BuildMI(MF, MI.getDebugLoc(),
  214. get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
  215. .addReg(BaseReg)
  216. .addReg(OffReg)
  217. .addReg(0)
  218. .addImm(SOOpc)
  219. .add(predOps(Pred))
  220. .add(condCodeOp());
  221. } else
  222. UpdateMI = BuildMI(MF, MI.getDebugLoc(),
  223. get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
  224. .addReg(BaseReg)
  225. .addReg(OffReg)
  226. .add(predOps(Pred))
  227. .add(condCodeOp());
  228. break;
  229. }
  230. case ARMII::AddrMode3 : {
  231. bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
  232. unsigned Amt = ARM_AM::getAM3Offset(OffImm);
  233. if (OffReg == 0)
  234. // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
  235. UpdateMI = BuildMI(MF, MI.getDebugLoc(),
  236. get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
  237. .addReg(BaseReg)
  238. .addImm(Amt)
  239. .add(predOps(Pred))
  240. .add(condCodeOp());
  241. else
  242. UpdateMI = BuildMI(MF, MI.getDebugLoc(),
  243. get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
  244. .addReg(BaseReg)
  245. .addReg(OffReg)
  246. .add(predOps(Pred))
  247. .add(condCodeOp());
  248. break;
  249. }
  250. }
  251. std::vector<MachineInstr*> NewMIs;
  252. if (isPre) {
  253. if (isLoad)
  254. MemMI =
  255. BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
  256. .addReg(WBReg)
  257. .addImm(0)
  258. .addImm(Pred);
  259. else
  260. MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
  261. .addReg(MI.getOperand(1).getReg())
  262. .addReg(WBReg)
  263. .addReg(0)
  264. .addImm(0)
  265. .addImm(Pred);
  266. NewMIs.push_back(MemMI);
  267. NewMIs.push_back(UpdateMI);
  268. } else {
  269. if (isLoad)
  270. MemMI =
  271. BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
  272. .addReg(BaseReg)
  273. .addImm(0)
  274. .addImm(Pred);
  275. else
  276. MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
  277. .addReg(MI.getOperand(1).getReg())
  278. .addReg(BaseReg)
  279. .addReg(0)
  280. .addImm(0)
  281. .addImm(Pred);
  282. if (WB.isDead())
  283. UpdateMI->getOperand(0).setIsDead();
  284. NewMIs.push_back(UpdateMI);
  285. NewMIs.push_back(MemMI);
  286. }
  287. // Transfer LiveVariables states, kill / dead info.
  288. if (LV) {
  289. for (const MachineOperand &MO : MI.operands()) {
  290. if (MO.isReg() && Register::isVirtualRegister(MO.getReg())) {
  291. Register Reg = MO.getReg();
  292. LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
  293. if (MO.isDef()) {
  294. MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
  295. if (MO.isDead())
  296. LV->addVirtualRegisterDead(Reg, *NewMI);
  297. }
  298. if (MO.isUse() && MO.isKill()) {
  299. for (unsigned j = 0; j < 2; ++j) {
  300. // Look at the two new MI's in reverse order.
  301. MachineInstr *NewMI = NewMIs[j];
  302. if (!NewMI->readsRegister(Reg))
  303. continue;
  304. LV->addVirtualRegisterKilled(Reg, *NewMI);
  305. if (VI.removeKill(MI))
  306. VI.Kills.push_back(NewMI);
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. MachineBasicBlock &MBB = *MI.getParent();
  314. MBB.insert(MI, NewMIs[1]);
  315. MBB.insert(MI, NewMIs[0]);
  316. return NewMIs[0];
  317. }
  318. // Branch analysis.
  319. bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
  320. MachineBasicBlock *&TBB,
  321. MachineBasicBlock *&FBB,
  322. SmallVectorImpl<MachineOperand> &Cond,
  323. bool AllowModify) const {
  324. TBB = nullptr;
  325. FBB = nullptr;
  326. MachineBasicBlock::instr_iterator I = MBB.instr_end();
  327. if (I == MBB.instr_begin())
  328. return false; // Empty blocks are easy.
  329. --I;
  330. // Walk backwards from the end of the basic block until the branch is
  331. // analyzed or we give up.
  332. while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) {
  333. // Flag to be raised on unanalyzeable instructions. This is useful in cases
  334. // where we want to clean up on the end of the basic block before we bail
  335. // out.
  336. bool CantAnalyze = false;
  337. // Skip over DEBUG values, predicated nonterminators and speculation
  338. // barrier terminators.
  339. while (I->isDebugInstr() || !I->isTerminator() ||
  340. isSpeculationBarrierEndBBOpcode(I->getOpcode()) ||
  341. I->getOpcode() == ARM::t2DoLoopStartTP){
  342. if (I == MBB.instr_begin())
  343. return false;
  344. --I;
  345. }
  346. if (isIndirectBranchOpcode(I->getOpcode()) ||
  347. isJumpTableBranchOpcode(I->getOpcode())) {
  348. // Indirect branches and jump tables can't be analyzed, but we still want
  349. // to clean up any instructions at the tail of the basic block.
  350. CantAnalyze = true;
  351. } else if (isUncondBranchOpcode(I->getOpcode())) {
  352. TBB = I->getOperand(0).getMBB();
  353. } else if (isCondBranchOpcode(I->getOpcode())) {
  354. // Bail out if we encounter multiple conditional branches.
  355. if (!Cond.empty())
  356. return true;
  357. assert(!FBB && "FBB should have been null.");
  358. FBB = TBB;
  359. TBB = I->getOperand(0).getMBB();
  360. Cond.push_back(I->getOperand(1));
  361. Cond.push_back(I->getOperand(2));
  362. } else if (I->isReturn()) {
  363. // Returns can't be analyzed, but we should run cleanup.
  364. CantAnalyze = true;
  365. } else {
  366. // We encountered other unrecognized terminator. Bail out immediately.
  367. return true;
  368. }
  369. // Cleanup code - to be run for unpredicated unconditional branches and
  370. // returns.
  371. if (!isPredicated(*I) &&
  372. (isUncondBranchOpcode(I->getOpcode()) ||
  373. isIndirectBranchOpcode(I->getOpcode()) ||
  374. isJumpTableBranchOpcode(I->getOpcode()) ||
  375. I->isReturn())) {
  376. // Forget any previous condition branch information - it no longer applies.
  377. Cond.clear();
  378. FBB = nullptr;
  379. // If we can modify the function, delete everything below this
  380. // unconditional branch.
  381. if (AllowModify) {
  382. MachineBasicBlock::iterator DI = std::next(I);
  383. while (DI != MBB.instr_end()) {
  384. MachineInstr &InstToDelete = *DI;
  385. ++DI;
  386. // Speculation barriers must not be deleted.
  387. if (isSpeculationBarrierEndBBOpcode(InstToDelete.getOpcode()))
  388. continue;
  389. InstToDelete.eraseFromParent();
  390. }
  391. }
  392. }
  393. if (CantAnalyze) {
  394. // We may not be able to analyze the block, but we could still have
  395. // an unconditional branch as the last instruction in the block, which
  396. // just branches to layout successor. If this is the case, then just
  397. // remove it if we're allowed to make modifications.
  398. if (AllowModify && !isPredicated(MBB.back()) &&
  399. isUncondBranchOpcode(MBB.back().getOpcode()) &&
  400. TBB && MBB.isLayoutSuccessor(TBB))
  401. removeBranch(MBB);
  402. return true;
  403. }
  404. if (I == MBB.instr_begin())
  405. return false;
  406. --I;
  407. }
  408. // We made it past the terminators without bailing out - we must have
  409. // analyzed this branch successfully.
  410. return false;
  411. }
  412. unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock &MBB,
  413. int *BytesRemoved) const {
  414. assert(!BytesRemoved && "code size not handled");
  415. MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
  416. if (I == MBB.end())
  417. return 0;
  418. if (!isUncondBranchOpcode(I->getOpcode()) &&
  419. !isCondBranchOpcode(I->getOpcode()))
  420. return 0;
  421. // Remove the branch.
  422. I->eraseFromParent();
  423. I = MBB.end();
  424. if (I == MBB.begin()) return 1;
  425. --I;
  426. if (!isCondBranchOpcode(I->getOpcode()))
  427. return 1;
  428. // Remove the branch.
  429. I->eraseFromParent();
  430. return 2;
  431. }
  432. unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock &MBB,
  433. MachineBasicBlock *TBB,
  434. MachineBasicBlock *FBB,
  435. ArrayRef<MachineOperand> Cond,
  436. const DebugLoc &DL,
  437. int *BytesAdded) const {
  438. assert(!BytesAdded && "code size not handled");
  439. ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
  440. int BOpc = !AFI->isThumbFunction()
  441. ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
  442. int BccOpc = !AFI->isThumbFunction()
  443. ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
  444. bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
  445. // Shouldn't be a fall through.
  446. assert(TBB && "insertBranch must not be told to insert a fallthrough");
  447. assert((Cond.size() == 2 || Cond.size() == 0) &&
  448. "ARM branch conditions have two components!");
  449. // For conditional branches, we use addOperand to preserve CPSR flags.
  450. if (!FBB) {
  451. if (Cond.empty()) { // Unconditional branch?
  452. if (isThumb)
  453. BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).add(predOps(ARMCC::AL));
  454. else
  455. BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
  456. } else
  457. BuildMI(&MBB, DL, get(BccOpc))
  458. .addMBB(TBB)
  459. .addImm(Cond[0].getImm())
  460. .add(Cond[1]);
  461. return 1;
  462. }
  463. // Two-way conditional branch.
  464. BuildMI(&MBB, DL, get(BccOpc))
  465. .addMBB(TBB)
  466. .addImm(Cond[0].getImm())
  467. .add(Cond[1]);
  468. if (isThumb)
  469. BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).add(predOps(ARMCC::AL));
  470. else
  471. BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
  472. return 2;
  473. }
  474. bool ARMBaseInstrInfo::
  475. reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
  476. ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
  477. Cond[0].setImm(ARMCC::getOppositeCondition(CC));
  478. return false;
  479. }
  480. bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
  481. if (MI.isBundle()) {
  482. MachineBasicBlock::const_instr_iterator I = MI.getIterator();
  483. MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
  484. while (++I != E && I->isInsideBundle()) {
  485. int PIdx = I->findFirstPredOperandIdx();
  486. if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
  487. return true;
  488. }
  489. return false;
  490. }
  491. int PIdx = MI.findFirstPredOperandIdx();
  492. return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
  493. }
  494. std::string ARMBaseInstrInfo::createMIROperandComment(
  495. const MachineInstr &MI, const MachineOperand &Op, unsigned OpIdx,
  496. const TargetRegisterInfo *TRI) const {
  497. // First, let's see if there is a generic comment for this operand
  498. std::string GenericComment =
  499. TargetInstrInfo::createMIROperandComment(MI, Op, OpIdx, TRI);
  500. if (!GenericComment.empty())
  501. return GenericComment;
  502. // If not, check if we have an immediate operand.
  503. if (Op.getType() != MachineOperand::MO_Immediate)
  504. return std::string();
  505. // And print its corresponding condition code if the immediate is a
  506. // predicate.
  507. int FirstPredOp = MI.findFirstPredOperandIdx();
  508. if (FirstPredOp != (int) OpIdx)
  509. return std::string();
  510. std::string CC = "CC::";
  511. CC += ARMCondCodeToString((ARMCC::CondCodes)Op.getImm());
  512. return CC;
  513. }
  514. bool ARMBaseInstrInfo::PredicateInstruction(
  515. MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
  516. unsigned Opc = MI.getOpcode();
  517. if (isUncondBranchOpcode(Opc)) {
  518. MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
  519. MachineInstrBuilder(*MI.getParent()->getParent(), MI)
  520. .addImm(Pred[0].getImm())
  521. .addReg(Pred[1].getReg());
  522. return true;
  523. }
  524. int PIdx = MI.findFirstPredOperandIdx();
  525. if (PIdx != -1) {
  526. MachineOperand &PMO = MI.getOperand(PIdx);
  527. PMO.setImm(Pred[0].getImm());
  528. MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
  529. // Thumb 1 arithmetic instructions do not set CPSR when executed inside an
  530. // IT block. This affects how they are printed.
  531. const MCInstrDesc &MCID = MI.getDesc();
  532. if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
  533. assert(MCID.OpInfo[1].isOptionalDef() && "CPSR def isn't expected operand");
  534. assert((MI.getOperand(1).isDead() ||
  535. MI.getOperand(1).getReg() != ARM::CPSR) &&
  536. "if conversion tried to stop defining used CPSR");
  537. MI.getOperand(1).setReg(ARM::NoRegister);
  538. }
  539. return true;
  540. }
  541. return false;
  542. }
  543. bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
  544. ArrayRef<MachineOperand> Pred2) const {
  545. if (Pred1.size() > 2 || Pred2.size() > 2)
  546. return false;
  547. ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
  548. ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
  549. if (CC1 == CC2)
  550. return true;
  551. switch (CC1) {
  552. default:
  553. return false;
  554. case ARMCC::AL:
  555. return true;
  556. case ARMCC::HS:
  557. return CC2 == ARMCC::HI;
  558. case ARMCC::LS:
  559. return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
  560. case ARMCC::GE:
  561. return CC2 == ARMCC::GT;
  562. case ARMCC::LE:
  563. return CC2 == ARMCC::LT;
  564. }
  565. }
  566. bool ARMBaseInstrInfo::ClobbersPredicate(MachineInstr &MI,
  567. std::vector<MachineOperand> &Pred,
  568. bool SkipDead) const {
  569. bool Found = false;
  570. for (const MachineOperand &MO : MI.operands()) {
  571. bool ClobbersCPSR = MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR);
  572. bool IsCPSR = MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR;
  573. if (ClobbersCPSR || IsCPSR) {
  574. // Filter out T1 instructions that have a dead CPSR,
  575. // allowing IT blocks to be generated containing T1 instructions
  576. const MCInstrDesc &MCID = MI.getDesc();
  577. if (MCID.TSFlags & ARMII::ThumbArithFlagSetting && MO.isDead() &&
  578. SkipDead)
  579. continue;
  580. Pred.push_back(MO);
  581. Found = true;
  582. }
  583. }
  584. return Found;
  585. }
  586. bool ARMBaseInstrInfo::isCPSRDefined(const MachineInstr &MI) {
  587. for (const auto &MO : MI.operands())
  588. if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
  589. return true;
  590. return false;
  591. }
  592. static bool isEligibleForITBlock(const MachineInstr *MI) {
  593. switch (MI->getOpcode()) {
  594. default: return true;
  595. case ARM::tADC: // ADC (register) T1
  596. case ARM::tADDi3: // ADD (immediate) T1
  597. case ARM::tADDi8: // ADD (immediate) T2
  598. case ARM::tADDrr: // ADD (register) T1
  599. case ARM::tAND: // AND (register) T1
  600. case ARM::tASRri: // ASR (immediate) T1
  601. case ARM::tASRrr: // ASR (register) T1
  602. case ARM::tBIC: // BIC (register) T1
  603. case ARM::tEOR: // EOR (register) T1
  604. case ARM::tLSLri: // LSL (immediate) T1
  605. case ARM::tLSLrr: // LSL (register) T1
  606. case ARM::tLSRri: // LSR (immediate) T1
  607. case ARM::tLSRrr: // LSR (register) T1
  608. case ARM::tMUL: // MUL T1
  609. case ARM::tMVN: // MVN (register) T1
  610. case ARM::tORR: // ORR (register) T1
  611. case ARM::tROR: // ROR (register) T1
  612. case ARM::tRSB: // RSB (immediate) T1
  613. case ARM::tSBC: // SBC (register) T1
  614. case ARM::tSUBi3: // SUB (immediate) T1
  615. case ARM::tSUBi8: // SUB (immediate) T2
  616. case ARM::tSUBrr: // SUB (register) T1
  617. return !ARMBaseInstrInfo::isCPSRDefined(*MI);
  618. }
  619. }
  620. /// isPredicable - Return true if the specified instruction can be predicated.
  621. /// By default, this returns true for every instruction with a
  622. /// PredicateOperand.
  623. bool ARMBaseInstrInfo::isPredicable(const MachineInstr &MI) const {
  624. if (!MI.isPredicable())
  625. return false;
  626. if (MI.isBundle())
  627. return false;
  628. if (!isEligibleForITBlock(&MI))
  629. return false;
  630. const MachineFunction *MF = MI.getParent()->getParent();
  631. const ARMFunctionInfo *AFI =
  632. MF->getInfo<ARMFunctionInfo>();
  633. // Neon instructions in Thumb2 IT blocks are deprecated, see ARMARM.
  634. // In their ARM encoding, they can't be encoded in a conditional form.
  635. if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
  636. return false;
  637. // Make indirect control flow changes unpredicable when SLS mitigation is
  638. // enabled.
  639. const ARMSubtarget &ST = MF->getSubtarget<ARMSubtarget>();
  640. if (ST.hardenSlsRetBr() && isIndirectControlFlowNotComingBack(MI))
  641. return false;
  642. if (ST.hardenSlsBlr() && isIndirectCall(MI))
  643. return false;
  644. if (AFI->isThumb2Function()) {
  645. if (getSubtarget().restrictIT())
  646. return isV8EligibleForIT(&MI);
  647. }
  648. return true;
  649. }
  650. namespace llvm {
  651. template <> bool IsCPSRDead<MachineInstr>(const MachineInstr *MI) {
  652. for (const MachineOperand &MO : MI->operands()) {
  653. if (!MO.isReg() || MO.isUndef() || MO.isUse())
  654. continue;
  655. if (MO.getReg() != ARM::CPSR)
  656. continue;
  657. if (!MO.isDead())
  658. return false;
  659. }
  660. // all definitions of CPSR are dead
  661. return true;
  662. }
  663. } // end namespace llvm
  664. /// GetInstSize - Return the size of the specified MachineInstr.
  665. ///
  666. unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
  667. const MachineBasicBlock &MBB = *MI.getParent();
  668. const MachineFunction *MF = MBB.getParent();
  669. const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
  670. const MCInstrDesc &MCID = MI.getDesc();
  671. switch (MI.getOpcode()) {
  672. default:
  673. // Return the size specified in .td file. If there's none, return 0, as we
  674. // can't define a default size (Thumb1 instructions are 2 bytes, Thumb2
  675. // instructions are 2-4 bytes, and ARM instructions are 4 bytes), in
  676. // contrast to AArch64 instructions which have a default size of 4 bytes for
  677. // example.
  678. return MCID.getSize();
  679. case TargetOpcode::BUNDLE:
  680. return getInstBundleLength(MI);
  681. case ARM::CONSTPOOL_ENTRY:
  682. case ARM::JUMPTABLE_INSTS:
  683. case ARM::JUMPTABLE_ADDRS:
  684. case ARM::JUMPTABLE_TBB:
  685. case ARM::JUMPTABLE_TBH:
  686. // If this machine instr is a constant pool entry, its size is recorded as
  687. // operand #2.
  688. return MI.getOperand(2).getImm();
  689. case ARM::SPACE:
  690. return MI.getOperand(1).getImm();
  691. case ARM::INLINEASM:
  692. case ARM::INLINEASM_BR: {
  693. // If this machine instr is an inline asm, measure it.
  694. unsigned Size = getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
  695. if (!MF->getInfo<ARMFunctionInfo>()->isThumbFunction())
  696. Size = alignTo(Size, 4);
  697. return Size;
  698. }
  699. }
  700. }
  701. unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
  702. unsigned Size = 0;
  703. MachineBasicBlock::const_instr_iterator I = MI.getIterator();
  704. MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
  705. while (++I != E && I->isInsideBundle()) {
  706. assert(!I->isBundle() && "No nested bundle!");
  707. Size += getInstSizeInBytes(*I);
  708. }
  709. return Size;
  710. }
  711. void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
  712. MachineBasicBlock::iterator I,
  713. unsigned DestReg, bool KillSrc,
  714. const ARMSubtarget &Subtarget) const {
  715. unsigned Opc = Subtarget.isThumb()
  716. ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
  717. : ARM::MRS;
  718. MachineInstrBuilder MIB =
  719. BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
  720. // There is only 1 A/R class MRS instruction, and it always refers to
  721. // APSR. However, there are lots of other possibilities on M-class cores.
  722. if (Subtarget.isMClass())
  723. MIB.addImm(0x800);
  724. MIB.add(predOps(ARMCC::AL))
  725. .addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
  726. }
  727. void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
  728. MachineBasicBlock::iterator I,
  729. unsigned SrcReg, bool KillSrc,
  730. const ARMSubtarget &Subtarget) const {
  731. unsigned Opc = Subtarget.isThumb()
  732. ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
  733. : ARM::MSR;
  734. MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
  735. if (Subtarget.isMClass())
  736. MIB.addImm(0x800);
  737. else
  738. MIB.addImm(8);
  739. MIB.addReg(SrcReg, getKillRegState(KillSrc))
  740. .add(predOps(ARMCC::AL))
  741. .addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
  742. }
  743. void llvm::addUnpredicatedMveVpredNOp(MachineInstrBuilder &MIB) {
  744. MIB.addImm(ARMVCC::None);
  745. MIB.addReg(0);
  746. MIB.addReg(0); // tp_reg
  747. }
  748. void llvm::addUnpredicatedMveVpredROp(MachineInstrBuilder &MIB,
  749. Register DestReg) {
  750. addUnpredicatedMveVpredNOp(MIB);
  751. MIB.addReg(DestReg, RegState::Undef);
  752. }
  753. void llvm::addPredicatedMveVpredNOp(MachineInstrBuilder &MIB, unsigned Cond) {
  754. MIB.addImm(Cond);
  755. MIB.addReg(ARM::VPR, RegState::Implicit);
  756. MIB.addReg(0); // tp_reg
  757. }
  758. void llvm::addPredicatedMveVpredROp(MachineInstrBuilder &MIB,
  759. unsigned Cond, unsigned Inactive) {
  760. addPredicatedMveVpredNOp(MIB, Cond);
  761. MIB.addReg(Inactive);
  762. }
  763. void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
  764. MachineBasicBlock::iterator I,
  765. const DebugLoc &DL, MCRegister DestReg,
  766. MCRegister SrcReg, bool KillSrc) const {
  767. bool GPRDest = ARM::GPRRegClass.contains(DestReg);
  768. bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
  769. if (GPRDest && GPRSrc) {
  770. BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
  771. .addReg(SrcReg, getKillRegState(KillSrc))
  772. .add(predOps(ARMCC::AL))
  773. .add(condCodeOp());
  774. return;
  775. }
  776. bool SPRDest = ARM::SPRRegClass.contains(DestReg);
  777. bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
  778. unsigned Opc = 0;
  779. if (SPRDest && SPRSrc)
  780. Opc = ARM::VMOVS;
  781. else if (GPRDest && SPRSrc)
  782. Opc = ARM::VMOVRS;
  783. else if (SPRDest && GPRSrc)
  784. Opc = ARM::VMOVSR;
  785. else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.hasFP64())
  786. Opc = ARM::VMOVD;
  787. else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
  788. Opc = Subtarget.hasNEON() ? ARM::VORRq : ARM::MQPRCopy;
  789. if (Opc) {
  790. MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
  791. MIB.addReg(SrcReg, getKillRegState(KillSrc));
  792. if (Opc == ARM::VORRq || Opc == ARM::MVE_VORR)
  793. MIB.addReg(SrcReg, getKillRegState(KillSrc));
  794. if (Opc == ARM::MVE_VORR)
  795. addUnpredicatedMveVpredROp(MIB, DestReg);
  796. else if (Opc != ARM::MQPRCopy)
  797. MIB.add(predOps(ARMCC::AL));
  798. return;
  799. }
  800. // Handle register classes that require multiple instructions.
  801. unsigned BeginIdx = 0;
  802. unsigned SubRegs = 0;
  803. int Spacing = 1;
  804. // Use VORRq when possible.
  805. if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
  806. Opc = Subtarget.hasNEON() ? ARM::VORRq : ARM::MVE_VORR;
  807. BeginIdx = ARM::qsub_0;
  808. SubRegs = 2;
  809. } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
  810. Opc = Subtarget.hasNEON() ? ARM::VORRq : ARM::MVE_VORR;
  811. BeginIdx = ARM::qsub_0;
  812. SubRegs = 4;
  813. // Fall back to VMOVD.
  814. } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
  815. Opc = ARM::VMOVD;
  816. BeginIdx = ARM::dsub_0;
  817. SubRegs = 2;
  818. } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
  819. Opc = ARM::VMOVD;
  820. BeginIdx = ARM::dsub_0;
  821. SubRegs = 3;
  822. } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
  823. Opc = ARM::VMOVD;
  824. BeginIdx = ARM::dsub_0;
  825. SubRegs = 4;
  826. } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
  827. Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
  828. BeginIdx = ARM::gsub_0;
  829. SubRegs = 2;
  830. } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
  831. Opc = ARM::VMOVD;
  832. BeginIdx = ARM::dsub_0;
  833. SubRegs = 2;
  834. Spacing = 2;
  835. } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
  836. Opc = ARM::VMOVD;
  837. BeginIdx = ARM::dsub_0;
  838. SubRegs = 3;
  839. Spacing = 2;
  840. } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
  841. Opc = ARM::VMOVD;
  842. BeginIdx = ARM::dsub_0;
  843. SubRegs = 4;
  844. Spacing = 2;
  845. } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) &&
  846. !Subtarget.hasFP64()) {
  847. Opc = ARM::VMOVS;
  848. BeginIdx = ARM::ssub_0;
  849. SubRegs = 2;
  850. } else if (SrcReg == ARM::CPSR) {
  851. copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
  852. return;
  853. } else if (DestReg == ARM::CPSR) {
  854. copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
  855. return;
  856. } else if (DestReg == ARM::VPR) {
  857. assert(ARM::GPRRegClass.contains(SrcReg));
  858. BuildMI(MBB, I, I->getDebugLoc(), get(ARM::VMSR_P0), DestReg)
  859. .addReg(SrcReg, getKillRegState(KillSrc))
  860. .add(predOps(ARMCC::AL));
  861. return;
  862. } else if (SrcReg == ARM::VPR) {
  863. assert(ARM::GPRRegClass.contains(DestReg));
  864. BuildMI(MBB, I, I->getDebugLoc(), get(ARM::VMRS_P0), DestReg)
  865. .addReg(SrcReg, getKillRegState(KillSrc))
  866. .add(predOps(ARMCC::AL));
  867. return;
  868. } else if (DestReg == ARM::FPSCR_NZCV) {
  869. assert(ARM::GPRRegClass.contains(SrcReg));
  870. BuildMI(MBB, I, I->getDebugLoc(), get(ARM::VMSR_FPSCR_NZCVQC), DestReg)
  871. .addReg(SrcReg, getKillRegState(KillSrc))
  872. .add(predOps(ARMCC::AL));
  873. return;
  874. } else if (SrcReg == ARM::FPSCR_NZCV) {
  875. assert(ARM::GPRRegClass.contains(DestReg));
  876. BuildMI(MBB, I, I->getDebugLoc(), get(ARM::VMRS_FPSCR_NZCVQC), DestReg)
  877. .addReg(SrcReg, getKillRegState(KillSrc))
  878. .add(predOps(ARMCC::AL));
  879. return;
  880. }
  881. assert(Opc && "Impossible reg-to-reg copy");
  882. const TargetRegisterInfo *TRI = &getRegisterInfo();
  883. MachineInstrBuilder Mov;
  884. // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
  885. if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
  886. BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
  887. Spacing = -Spacing;
  888. }
  889. #ifndef NDEBUG
  890. SmallSet<unsigned, 4> DstRegs;
  891. #endif
  892. for (unsigned i = 0; i != SubRegs; ++i) {
  893. Register Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
  894. Register Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
  895. assert(Dst && Src && "Bad sub-register");
  896. #ifndef NDEBUG
  897. assert(!DstRegs.count(Src) && "destructive vector copy");
  898. DstRegs.insert(Dst);
  899. #endif
  900. Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
  901. // VORR (NEON or MVE) takes two source operands.
  902. if (Opc == ARM::VORRq || Opc == ARM::MVE_VORR) {
  903. Mov.addReg(Src);
  904. }
  905. // MVE VORR takes predicate operands in place of an ordinary condition.
  906. if (Opc == ARM::MVE_VORR)
  907. addUnpredicatedMveVpredROp(Mov, Dst);
  908. else
  909. Mov = Mov.add(predOps(ARMCC::AL));
  910. // MOVr can set CC.
  911. if (Opc == ARM::MOVr)
  912. Mov = Mov.add(condCodeOp());
  913. }
  914. // Add implicit super-register defs and kills to the last instruction.
  915. Mov->addRegisterDefined(DestReg, TRI);
  916. if (KillSrc)
  917. Mov->addRegisterKilled(SrcReg, TRI);
  918. }
  919. Optional<DestSourcePair>
  920. ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
  921. // VMOVRRD is also a copy instruction but it requires
  922. // special way of handling. It is more complex copy version
  923. // and since that we are not considering it. For recognition
  924. // of such instruction isExtractSubregLike MI interface fuction
  925. // could be used.
  926. // VORRq is considered as a move only if two inputs are
  927. // the same register.
  928. if (!MI.isMoveReg() ||
  929. (MI.getOpcode() == ARM::VORRq &&
  930. MI.getOperand(1).getReg() != MI.getOperand(2).getReg()))
  931. return None;
  932. return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
  933. }
  934. Optional<ParamLoadedValue>
  935. ARMBaseInstrInfo::describeLoadedValue(const MachineInstr &MI,
  936. Register Reg) const {
  937. if (auto DstSrcPair = isCopyInstrImpl(MI)) {
  938. Register DstReg = DstSrcPair->Destination->getReg();
  939. // TODO: We don't handle cases where the forwarding reg is narrower/wider
  940. // than the copy registers. Consider for example:
  941. //
  942. // s16 = VMOVS s0
  943. // s17 = VMOVS s1
  944. // call @callee(d0)
  945. //
  946. // We'd like to describe the call site value of d0 as d8, but this requires
  947. // gathering and merging the descriptions for the two VMOVS instructions.
  948. //
  949. // We also don't handle the reverse situation, where the forwarding reg is
  950. // narrower than the copy destination:
  951. //
  952. // d8 = VMOVD d0
  953. // call @callee(s1)
  954. //
  955. // We need to produce a fragment description (the call site value of s1 is
  956. // /not/ just d8).
  957. if (DstReg != Reg)
  958. return None;
  959. }
  960. return TargetInstrInfo::describeLoadedValue(MI, Reg);
  961. }
  962. const MachineInstrBuilder &
  963. ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
  964. unsigned SubIdx, unsigned State,
  965. const TargetRegisterInfo *TRI) const {
  966. if (!SubIdx)
  967. return MIB.addReg(Reg, State);
  968. if (Register::isPhysicalRegister(Reg))
  969. return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
  970. return MIB.addReg(Reg, State, SubIdx);
  971. }
  972. void ARMBaseInstrInfo::
  973. storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
  974. Register SrcReg, bool isKill, int FI,
  975. const TargetRegisterClass *RC,
  976. const TargetRegisterInfo *TRI) const {
  977. MachineFunction &MF = *MBB.getParent();
  978. MachineFrameInfo &MFI = MF.getFrameInfo();
  979. Align Alignment = MFI.getObjectAlign(FI);
  980. MachineMemOperand *MMO = MF.getMachineMemOperand(
  981. MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
  982. MFI.getObjectSize(FI), Alignment);
  983. switch (TRI->getSpillSize(*RC)) {
  984. case 2:
  985. if (ARM::HPRRegClass.hasSubClassEq(RC)) {
  986. BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRH))
  987. .addReg(SrcReg, getKillRegState(isKill))
  988. .addFrameIndex(FI)
  989. .addImm(0)
  990. .addMemOperand(MMO)
  991. .add(predOps(ARMCC::AL));
  992. } else
  993. llvm_unreachable("Unknown reg class!");
  994. break;
  995. case 4:
  996. if (ARM::GPRRegClass.hasSubClassEq(RC)) {
  997. BuildMI(MBB, I, DebugLoc(), get(ARM::STRi12))
  998. .addReg(SrcReg, getKillRegState(isKill))
  999. .addFrameIndex(FI)
  1000. .addImm(0)
  1001. .addMemOperand(MMO)
  1002. .add(predOps(ARMCC::AL));
  1003. } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
  1004. BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRS))
  1005. .addReg(SrcReg, getKillRegState(isKill))
  1006. .addFrameIndex(FI)
  1007. .addImm(0)
  1008. .addMemOperand(MMO)
  1009. .add(predOps(ARMCC::AL));
  1010. } else if (ARM::VCCRRegClass.hasSubClassEq(RC)) {
  1011. BuildMI(MBB, I, DebugLoc(), get(ARM::VSTR_P0_off))
  1012. .addReg(SrcReg, getKillRegState(isKill))
  1013. .addFrameIndex(FI)
  1014. .addImm(0)
  1015. .addMemOperand(MMO)
  1016. .add(predOps(ARMCC::AL));
  1017. } else
  1018. llvm_unreachable("Unknown reg class!");
  1019. break;
  1020. case 8:
  1021. if (ARM::DPRRegClass.hasSubClassEq(RC)) {
  1022. BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRD))
  1023. .addReg(SrcReg, getKillRegState(isKill))
  1024. .addFrameIndex(FI)
  1025. .addImm(0)
  1026. .addMemOperand(MMO)
  1027. .add(predOps(ARMCC::AL));
  1028. } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
  1029. if (Subtarget.hasV5TEOps()) {
  1030. MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STRD));
  1031. AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
  1032. AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
  1033. MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
  1034. .add(predOps(ARMCC::AL));
  1035. } else {
  1036. // Fallback to STM instruction, which has existed since the dawn of
  1037. // time.
  1038. MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STMIA))
  1039. .addFrameIndex(FI)
  1040. .addMemOperand(MMO)
  1041. .add(predOps(ARMCC::AL));
  1042. AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
  1043. AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
  1044. }
  1045. } else
  1046. llvm_unreachable("Unknown reg class!");
  1047. break;
  1048. case 16:
  1049. if (ARM::DPairRegClass.hasSubClassEq(RC) && Subtarget.hasNEON()) {
  1050. // Use aligned spills if the stack can be realigned.
  1051. if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF)) {
  1052. BuildMI(MBB, I, DebugLoc(), get(ARM::VST1q64))
  1053. .addFrameIndex(FI)
  1054. .addImm(16)
  1055. .addReg(SrcReg, getKillRegState(isKill))
  1056. .addMemOperand(MMO)
  1057. .add(predOps(ARMCC::AL));
  1058. } else {
  1059. BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMQIA))
  1060. .addReg(SrcReg, getKillRegState(isKill))
  1061. .addFrameIndex(FI)
  1062. .addMemOperand(MMO)
  1063. .add(predOps(ARMCC::AL));
  1064. }
  1065. } else if (ARM::QPRRegClass.hasSubClassEq(RC) &&
  1066. Subtarget.hasMVEIntegerOps()) {
  1067. auto MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::MVE_VSTRWU32));
  1068. MIB.addReg(SrcReg, getKillRegState(isKill))
  1069. .addFrameIndex(FI)
  1070. .addImm(0)
  1071. .addMemOperand(MMO);
  1072. addUnpredicatedMveVpredNOp(MIB);
  1073. } else
  1074. llvm_unreachable("Unknown reg class!");
  1075. break;
  1076. case 24:
  1077. if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
  1078. // Use aligned spills if the stack can be realigned.
  1079. if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF) &&
  1080. Subtarget.hasNEON()) {
  1081. BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64TPseudo))
  1082. .addFrameIndex(FI)
  1083. .addImm(16)
  1084. .addReg(SrcReg, getKillRegState(isKill))
  1085. .addMemOperand(MMO)
  1086. .add(predOps(ARMCC::AL));
  1087. } else {
  1088. MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
  1089. get(ARM::VSTMDIA))
  1090. .addFrameIndex(FI)
  1091. .add(predOps(ARMCC::AL))
  1092. .addMemOperand(MMO);
  1093. MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
  1094. MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
  1095. AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
  1096. }
  1097. } else
  1098. llvm_unreachable("Unknown reg class!");
  1099. break;
  1100. case 32:
  1101. if (ARM::QQPRRegClass.hasSubClassEq(RC) ||
  1102. ARM::MQQPRRegClass.hasSubClassEq(RC) ||
  1103. ARM::DQuadRegClass.hasSubClassEq(RC)) {
  1104. if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF) &&
  1105. Subtarget.hasNEON()) {
  1106. // FIXME: It's possible to only store part of the QQ register if the
  1107. // spilled def has a sub-register index.
  1108. BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64QPseudo))
  1109. .addFrameIndex(FI)
  1110. .addImm(16)
  1111. .addReg(SrcReg, getKillRegState(isKill))
  1112. .addMemOperand(MMO)
  1113. .add(predOps(ARMCC::AL));
  1114. } else if (Subtarget.hasMVEIntegerOps()) {
  1115. BuildMI(MBB, I, DebugLoc(), get(ARM::MQQPRStore))
  1116. .addReg(SrcReg, getKillRegState(isKill))
  1117. .addFrameIndex(FI)
  1118. .addMemOperand(MMO);
  1119. } else {
  1120. MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
  1121. get(ARM::VSTMDIA))
  1122. .addFrameIndex(FI)
  1123. .add(predOps(ARMCC::AL))
  1124. .addMemOperand(MMO);
  1125. MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
  1126. MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
  1127. MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
  1128. AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
  1129. }
  1130. } else
  1131. llvm_unreachable("Unknown reg class!");
  1132. break;
  1133. case 64:
  1134. if (ARM::MQQQQPRRegClass.hasSubClassEq(RC) &&
  1135. Subtarget.hasMVEIntegerOps()) {
  1136. BuildMI(MBB, I, DebugLoc(), get(ARM::MQQQQPRStore))
  1137. .addReg(SrcReg, getKillRegState(isKill))
  1138. .addFrameIndex(FI)
  1139. .addMemOperand(MMO);
  1140. } else if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
  1141. MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMDIA))
  1142. .addFrameIndex(FI)
  1143. .add(predOps(ARMCC::AL))
  1144. .addMemOperand(MMO);
  1145. MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
  1146. MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
  1147. MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
  1148. MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
  1149. MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
  1150. MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
  1151. MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
  1152. AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
  1153. } else
  1154. llvm_unreachable("Unknown reg class!");
  1155. break;
  1156. default:
  1157. llvm_unreachable("Unknown reg class!");
  1158. }
  1159. }
  1160. unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
  1161. int &FrameIndex) const {
  1162. switch (MI.getOpcode()) {
  1163. default: break;
  1164. case ARM::STRrs:
  1165. case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
  1166. if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
  1167. MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
  1168. MI.getOperand(3).getImm() == 0) {
  1169. FrameIndex = MI.getOperand(1).getIndex();
  1170. return MI.getOperand(0).getReg();
  1171. }
  1172. break;
  1173. case ARM::STRi12:
  1174. case ARM::t2STRi12:
  1175. case ARM::tSTRspi:
  1176. case ARM::VSTRD:
  1177. case ARM::VSTRS:
  1178. case ARM::VSTR_P0_off:
  1179. case ARM::MVE_VSTRWU32:
  1180. if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
  1181. MI.getOperand(2).getImm() == 0) {
  1182. FrameIndex = MI.getOperand(1).getIndex();
  1183. return MI.getOperand(0).getReg();
  1184. }
  1185. break;
  1186. case ARM::VST1q64:
  1187. case ARM::VST1d64TPseudo:
  1188. case ARM::VST1d64QPseudo:
  1189. if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
  1190. FrameIndex = MI.getOperand(0).getIndex();
  1191. return MI.getOperand(2).getReg();
  1192. }
  1193. break;
  1194. case ARM::VSTMQIA:
  1195. if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
  1196. FrameIndex = MI.getOperand(1).getIndex();
  1197. return MI.getOperand(0).getReg();
  1198. }
  1199. break;
  1200. case ARM::MQQPRStore:
  1201. case ARM::MQQQQPRStore:
  1202. if (MI.getOperand(1).isFI()) {
  1203. FrameIndex = MI.getOperand(1).getIndex();
  1204. return MI.getOperand(0).getReg();
  1205. }
  1206. break;
  1207. }
  1208. return 0;
  1209. }
  1210. unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
  1211. int &FrameIndex) const {
  1212. SmallVector<const MachineMemOperand *, 1> Accesses;
  1213. if (MI.mayStore() && hasStoreToStackSlot(MI, Accesses) &&
  1214. Accesses.size() == 1) {
  1215. FrameIndex =
  1216. cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
  1217. ->getFrameIndex();
  1218. return true;
  1219. }
  1220. return false;
  1221. }
  1222. void ARMBaseInstrInfo::
  1223. loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
  1224. Register DestReg, int FI,
  1225. const TargetRegisterClass *RC,
  1226. const TargetRegisterInfo *TRI) const {
  1227. DebugLoc DL;
  1228. if (I != MBB.end()) DL = I->getDebugLoc();
  1229. MachineFunction &MF = *MBB.getParent();
  1230. MachineFrameInfo &MFI = MF.getFrameInfo();
  1231. const Align Alignment = MFI.getObjectAlign(FI);
  1232. MachineMemOperand *MMO = MF.getMachineMemOperand(
  1233. MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
  1234. MFI.getObjectSize(FI), Alignment);
  1235. switch (TRI->getSpillSize(*RC)) {
  1236. case 2:
  1237. if (ARM::HPRRegClass.hasSubClassEq(RC)) {
  1238. BuildMI(MBB, I, DL, get(ARM::VLDRH), DestReg)
  1239. .addFrameIndex(FI)
  1240. .addImm(0)
  1241. .addMemOperand(MMO)
  1242. .add(predOps(ARMCC::AL));
  1243. } else
  1244. llvm_unreachable("Unknown reg class!");
  1245. break;
  1246. case 4:
  1247. if (ARM::GPRRegClass.hasSubClassEq(RC)) {
  1248. BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
  1249. .addFrameIndex(FI)
  1250. .addImm(0)
  1251. .addMemOperand(MMO)
  1252. .add(predOps(ARMCC::AL));
  1253. } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
  1254. BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
  1255. .addFrameIndex(FI)
  1256. .addImm(0)
  1257. .addMemOperand(MMO)
  1258. .add(predOps(ARMCC::AL));
  1259. } else if (ARM::VCCRRegClass.hasSubClassEq(RC)) {
  1260. BuildMI(MBB, I, DL, get(ARM::VLDR_P0_off), DestReg)
  1261. .addFrameIndex(FI)
  1262. .addImm(0)
  1263. .addMemOperand(MMO)
  1264. .add(predOps(ARMCC::AL));
  1265. } else
  1266. llvm_unreachable("Unknown reg class!");
  1267. break;
  1268. case 8:
  1269. if (ARM::DPRRegClass.hasSubClassEq(RC)) {
  1270. BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
  1271. .addFrameIndex(FI)
  1272. .addImm(0)
  1273. .addMemOperand(MMO)
  1274. .add(predOps(ARMCC::AL));
  1275. } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
  1276. MachineInstrBuilder MIB;
  1277. if (Subtarget.hasV5TEOps()) {
  1278. MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
  1279. AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
  1280. AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
  1281. MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
  1282. .add(predOps(ARMCC::AL));
  1283. } else {
  1284. // Fallback to LDM instruction, which has existed since the dawn of
  1285. // time.
  1286. MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA))
  1287. .addFrameIndex(FI)
  1288. .addMemOperand(MMO)
  1289. .add(predOps(ARMCC::AL));
  1290. MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
  1291. MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
  1292. }
  1293. if (Register::isPhysicalRegister(DestReg))
  1294. MIB.addReg(DestReg, RegState::ImplicitDefine);
  1295. } else
  1296. llvm_unreachable("Unknown reg class!");
  1297. break;
  1298. case 16:
  1299. if (ARM::DPairRegClass.hasSubClassEq(RC) && Subtarget.hasNEON()) {
  1300. if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF)) {
  1301. BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
  1302. .addFrameIndex(FI)
  1303. .addImm(16)
  1304. .addMemOperand(MMO)
  1305. .add(predOps(ARMCC::AL));
  1306. } else {
  1307. BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
  1308. .addFrameIndex(FI)
  1309. .addMemOperand(MMO)
  1310. .add(predOps(ARMCC::AL));
  1311. }
  1312. } else if (ARM::QPRRegClass.hasSubClassEq(RC) &&
  1313. Subtarget.hasMVEIntegerOps()) {
  1314. auto MIB = BuildMI(MBB, I, DL, get(ARM::MVE_VLDRWU32), DestReg);
  1315. MIB.addFrameIndex(FI)
  1316. .addImm(0)
  1317. .addMemOperand(MMO);
  1318. addUnpredicatedMveVpredNOp(MIB);
  1319. } else
  1320. llvm_unreachable("Unknown reg class!");
  1321. break;
  1322. case 24:
  1323. if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
  1324. if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF) &&
  1325. Subtarget.hasNEON()) {
  1326. BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
  1327. .addFrameIndex(FI)
  1328. .addImm(16)
  1329. .addMemOperand(MMO)
  1330. .add(predOps(ARMCC::AL));
  1331. } else {
  1332. MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
  1333. .addFrameIndex(FI)
  1334. .addMemOperand(MMO)
  1335. .add(predOps(ARMCC::AL));
  1336. MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
  1337. MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
  1338. MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
  1339. if (Register::isPhysicalRegister(DestReg))
  1340. MIB.addReg(DestReg, RegState::ImplicitDefine);
  1341. }
  1342. } else
  1343. llvm_unreachable("Unknown reg class!");
  1344. break;
  1345. case 32:
  1346. if (ARM::QQPRRegClass.hasSubClassEq(RC) ||
  1347. ARM::MQQPRRegClass.hasSubClassEq(RC) ||
  1348. ARM::DQuadRegClass.hasSubClassEq(RC)) {
  1349. if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF) &&
  1350. Subtarget.hasNEON()) {
  1351. BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
  1352. .addFrameIndex(FI)
  1353. .addImm(16)
  1354. .addMemOperand(MMO)
  1355. .add(predOps(ARMCC::AL));
  1356. } else if (Subtarget.hasMVEIntegerOps()) {
  1357. BuildMI(MBB, I, DL, get(ARM::MQQPRLoad), DestReg)
  1358. .addFrameIndex(FI)
  1359. .addMemOperand(MMO);
  1360. } else {
  1361. MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
  1362. .addFrameIndex(FI)
  1363. .add(predOps(ARMCC::AL))
  1364. .addMemOperand(MMO);
  1365. MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
  1366. MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
  1367. MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
  1368. MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
  1369. if (Register::isPhysicalRegister(DestReg))
  1370. MIB.addReg(DestReg, RegState::ImplicitDefine);
  1371. }
  1372. } else
  1373. llvm_unreachable("Unknown reg class!");
  1374. break;
  1375. case 64:
  1376. if (ARM::MQQQQPRRegClass.hasSubClassEq(RC) &&
  1377. Subtarget.hasMVEIntegerOps()) {
  1378. BuildMI(MBB, I, DL, get(ARM::MQQQQPRLoad), DestReg)
  1379. .addFrameIndex(FI)
  1380. .addMemOperand(MMO);
  1381. } else if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
  1382. MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
  1383. .addFrameIndex(FI)
  1384. .add(predOps(ARMCC::AL))
  1385. .addMemOperand(MMO);
  1386. MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
  1387. MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
  1388. MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
  1389. MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
  1390. MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
  1391. MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
  1392. MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
  1393. MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
  1394. if (Register::isPhysicalRegister(DestReg))
  1395. MIB.addReg(DestReg, RegState::ImplicitDefine);
  1396. } else
  1397. llvm_unreachable("Unknown reg class!");
  1398. break;
  1399. default:
  1400. llvm_unreachable("Unknown regclass!");
  1401. }
  1402. }
  1403. unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
  1404. int &FrameIndex) const {
  1405. switch (MI.getOpcode()) {
  1406. default: break;
  1407. case ARM::LDRrs:
  1408. case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
  1409. if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
  1410. MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
  1411. MI.getOperand(3).getImm() == 0) {
  1412. FrameIndex = MI.getOperand(1).getIndex();
  1413. return MI.getOperand(0).getReg();
  1414. }
  1415. break;
  1416. case ARM::LDRi12:
  1417. case ARM::t2LDRi12:
  1418. case ARM::tLDRspi:
  1419. case ARM::VLDRD:
  1420. case ARM::VLDRS:
  1421. case ARM::VLDR_P0_off:
  1422. case ARM::MVE_VLDRWU32:
  1423. if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
  1424. MI.getOperand(2).getImm() == 0) {
  1425. FrameIndex = MI.getOperand(1).getIndex();
  1426. return MI.getOperand(0).getReg();
  1427. }
  1428. break;
  1429. case ARM::VLD1q64:
  1430. case ARM::VLD1d8TPseudo:
  1431. case ARM::VLD1d16TPseudo:
  1432. case ARM::VLD1d32TPseudo:
  1433. case ARM::VLD1d64TPseudo:
  1434. case ARM::VLD1d8QPseudo:
  1435. case ARM::VLD1d16QPseudo:
  1436. case ARM::VLD1d32QPseudo:
  1437. case ARM::VLD1d64QPseudo:
  1438. if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
  1439. FrameIndex = MI.getOperand(1).getIndex();
  1440. return MI.getOperand(0).getReg();
  1441. }
  1442. break;
  1443. case ARM::VLDMQIA:
  1444. if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
  1445. FrameIndex = MI.getOperand(1).getIndex();
  1446. return MI.getOperand(0).getReg();
  1447. }
  1448. break;
  1449. case ARM::MQQPRLoad:
  1450. case ARM::MQQQQPRLoad:
  1451. if (MI.getOperand(1).isFI()) {
  1452. FrameIndex = MI.getOperand(1).getIndex();
  1453. return MI.getOperand(0).getReg();
  1454. }
  1455. break;
  1456. }
  1457. return 0;
  1458. }
  1459. unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
  1460. int &FrameIndex) const {
  1461. SmallVector<const MachineMemOperand *, 1> Accesses;
  1462. if (MI.mayLoad() && hasLoadFromStackSlot(MI, Accesses) &&
  1463. Accesses.size() == 1) {
  1464. FrameIndex =
  1465. cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
  1466. ->getFrameIndex();
  1467. return true;
  1468. }
  1469. return false;
  1470. }
  1471. /// Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
  1472. /// depending on whether the result is used.
  1473. void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
  1474. bool isThumb1 = Subtarget.isThumb1Only();
  1475. bool isThumb2 = Subtarget.isThumb2();
  1476. const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
  1477. DebugLoc dl = MI->getDebugLoc();
  1478. MachineBasicBlock *BB = MI->getParent();
  1479. MachineInstrBuilder LDM, STM;
  1480. if (isThumb1 || !MI->getOperand(1).isDead()) {
  1481. MachineOperand LDWb(MI->getOperand(1));
  1482. LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
  1483. : isThumb1 ? ARM::tLDMIA_UPD
  1484. : ARM::LDMIA_UPD))
  1485. .add(LDWb);
  1486. } else {
  1487. LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
  1488. }
  1489. if (isThumb1 || !MI->getOperand(0).isDead()) {
  1490. MachineOperand STWb(MI->getOperand(0));
  1491. STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
  1492. : isThumb1 ? ARM::tSTMIA_UPD
  1493. : ARM::STMIA_UPD))
  1494. .add(STWb);
  1495. } else {
  1496. STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
  1497. }
  1498. MachineOperand LDBase(MI->getOperand(3));
  1499. LDM.add(LDBase).add(predOps(ARMCC::AL));
  1500. MachineOperand STBase(MI->getOperand(2));
  1501. STM.add(STBase).add(predOps(ARMCC::AL));
  1502. // Sort the scratch registers into ascending order.
  1503. const TargetRegisterInfo &TRI = getRegisterInfo();
  1504. SmallVector<unsigned, 6> ScratchRegs;
  1505. for(unsigned I = 5; I < MI->getNumOperands(); ++I)
  1506. ScratchRegs.push_back(MI->getOperand(I).getReg());
  1507. llvm::sort(ScratchRegs,
  1508. [&TRI](const unsigned &Reg1, const unsigned &Reg2) -> bool {
  1509. return TRI.getEncodingValue(Reg1) <
  1510. TRI.getEncodingValue(Reg2);
  1511. });
  1512. for (const auto &Reg : ScratchRegs) {
  1513. LDM.addReg(Reg, RegState::Define);
  1514. STM.addReg(Reg, RegState::Kill);
  1515. }
  1516. BB->erase(MI);
  1517. }
  1518. bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
  1519. if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
  1520. expandLoadStackGuard(MI);
  1521. MI.getParent()->erase(MI);
  1522. return true;
  1523. }
  1524. if (MI.getOpcode() == ARM::MEMCPY) {
  1525. expandMEMCPY(MI);
  1526. return true;
  1527. }
  1528. // This hook gets to expand COPY instructions before they become
  1529. // copyPhysReg() calls. Look for VMOVS instructions that can legally be
  1530. // widened to VMOVD. We prefer the VMOVD when possible because it may be
  1531. // changed into a VORR that can go down the NEON pipeline.
  1532. if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || !Subtarget.hasFP64())
  1533. return false;
  1534. // Look for a copy between even S-registers. That is where we keep floats
  1535. // when using NEON v2f32 instructions for f32 arithmetic.
  1536. Register DstRegS = MI.getOperand(0).getReg();
  1537. Register SrcRegS = MI.getOperand(1).getReg();
  1538. if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
  1539. return false;
  1540. const TargetRegisterInfo *TRI = &getRegisterInfo();
  1541. unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
  1542. &ARM::DPRRegClass);
  1543. unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
  1544. &ARM::DPRRegClass);
  1545. if (!DstRegD || !SrcRegD)
  1546. return false;
  1547. // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
  1548. // legal if the COPY already defines the full DstRegD, and it isn't a
  1549. // sub-register insertion.
  1550. if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
  1551. return false;
  1552. // A dead copy shouldn't show up here, but reject it just in case.
  1553. if (MI.getOperand(0).isDead())
  1554. return false;
  1555. // All clear, widen the COPY.
  1556. LLVM_DEBUG(dbgs() << "widening: " << MI);
  1557. MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
  1558. // Get rid of the old implicit-def of DstRegD. Leave it if it defines a Q-reg
  1559. // or some other super-register.
  1560. int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
  1561. if (ImpDefIdx != -1)
  1562. MI.RemoveOperand(ImpDefIdx);
  1563. // Change the opcode and operands.
  1564. MI.setDesc(get(ARM::VMOVD));
  1565. MI.getOperand(0).setReg(DstRegD);
  1566. MI.getOperand(1).setReg(SrcRegD);
  1567. MIB.add(predOps(ARMCC::AL));
  1568. // We are now reading SrcRegD instead of SrcRegS. This may upset the
  1569. // register scavenger and machine verifier, so we need to indicate that we
  1570. // are reading an undefined value from SrcRegD, but a proper value from
  1571. // SrcRegS.
  1572. MI.getOperand(1).setIsUndef();
  1573. MIB.addReg(SrcRegS, RegState::Implicit);
  1574. // SrcRegD may actually contain an unrelated value in the ssub_1
  1575. // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
  1576. if (MI.getOperand(1).isKill()) {
  1577. MI.getOperand(1).setIsKill(false);
  1578. MI.addRegisterKilled(SrcRegS, TRI, true);
  1579. }
  1580. LLVM_DEBUG(dbgs() << "replaced by: " << MI);
  1581. return true;
  1582. }
  1583. /// Create a copy of a const pool value. Update CPI to the new index and return
  1584. /// the label UID.
  1585. static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
  1586. MachineConstantPool *MCP = MF.getConstantPool();
  1587. ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
  1588. const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
  1589. assert(MCPE.isMachineConstantPoolEntry() &&
  1590. "Expecting a machine constantpool entry!");
  1591. ARMConstantPoolValue *ACPV =
  1592. static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
  1593. unsigned PCLabelId = AFI->createPICLabelUId();
  1594. ARMConstantPoolValue *NewCPV = nullptr;
  1595. // FIXME: The below assumes PIC relocation model and that the function
  1596. // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
  1597. // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
  1598. // instructions, so that's probably OK, but is PIC always correct when
  1599. // we get here?
  1600. if (ACPV->isGlobalValue())
  1601. NewCPV = ARMConstantPoolConstant::Create(
  1602. cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
  1603. 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
  1604. else if (ACPV->isExtSymbol())
  1605. NewCPV = ARMConstantPoolSymbol::
  1606. Create(MF.getFunction().getContext(),
  1607. cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
  1608. else if (ACPV->isBlockAddress())
  1609. NewCPV = ARMConstantPoolConstant::
  1610. Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
  1611. ARMCP::CPBlockAddress, 4);
  1612. else if (ACPV->isLSDA())
  1613. NewCPV = ARMConstantPoolConstant::Create(&MF.getFunction(), PCLabelId,
  1614. ARMCP::CPLSDA, 4);
  1615. else if (ACPV->isMachineBasicBlock())
  1616. NewCPV = ARMConstantPoolMBB::
  1617. Create(MF.getFunction().getContext(),
  1618. cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
  1619. else
  1620. llvm_unreachable("Unexpected ARM constantpool value type!!");
  1621. CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlign());
  1622. return PCLabelId;
  1623. }
  1624. void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
  1625. MachineBasicBlock::iterator I,
  1626. Register DestReg, unsigned SubIdx,
  1627. const MachineInstr &Orig,
  1628. const TargetRegisterInfo &TRI) const {
  1629. unsigned Opcode = Orig.getOpcode();
  1630. switch (Opcode) {
  1631. default: {
  1632. MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
  1633. MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
  1634. MBB.insert(I, MI);
  1635. break;
  1636. }
  1637. case ARM::tLDRpci_pic:
  1638. case ARM::t2LDRpci_pic: {
  1639. MachineFunction &MF = *MBB.getParent();
  1640. unsigned CPI = Orig.getOperand(1).getIndex();
  1641. unsigned PCLabelId = duplicateCPV(MF, CPI);
  1642. BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
  1643. .addConstantPoolIndex(CPI)
  1644. .addImm(PCLabelId)
  1645. .cloneMemRefs(Orig);
  1646. break;
  1647. }
  1648. }
  1649. }
  1650. MachineInstr &
  1651. ARMBaseInstrInfo::duplicate(MachineBasicBlock &MBB,
  1652. MachineBasicBlock::iterator InsertBefore,
  1653. const MachineInstr &Orig) const {
  1654. MachineInstr &Cloned = TargetInstrInfo::duplicate(MBB, InsertBefore, Orig);
  1655. MachineBasicBlock::instr_iterator I = Cloned.getIterator();
  1656. for (;;) {
  1657. switch (I->getOpcode()) {
  1658. case ARM::tLDRpci_pic:
  1659. case ARM::t2LDRpci_pic: {
  1660. MachineFunction &MF = *MBB.getParent();
  1661. unsigned CPI = I->getOperand(1).getIndex();
  1662. unsigned PCLabelId = duplicateCPV(MF, CPI);
  1663. I->getOperand(1).setIndex(CPI);
  1664. I->getOperand(2).setImm(PCLabelId);
  1665. break;
  1666. }
  1667. }
  1668. if (!I->isBundledWithSucc())
  1669. break;
  1670. ++I;
  1671. }
  1672. return Cloned;
  1673. }
  1674. bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
  1675. const MachineInstr &MI1,
  1676. const MachineRegisterInfo *MRI) const {
  1677. unsigned Opcode = MI0.getOpcode();
  1678. if (Opcode == ARM::t2LDRpci || Opcode == ARM::t2LDRpci_pic ||
  1679. Opcode == ARM::tLDRpci || Opcode == ARM::tLDRpci_pic ||
  1680. Opcode == ARM::LDRLIT_ga_pcrel || Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
  1681. Opcode == ARM::tLDRLIT_ga_pcrel || Opcode == ARM::t2LDRLIT_ga_pcrel ||
  1682. Opcode == ARM::MOV_ga_pcrel || Opcode == ARM::MOV_ga_pcrel_ldr ||
  1683. Opcode == ARM::t2MOV_ga_pcrel) {
  1684. if (MI1.getOpcode() != Opcode)
  1685. return false;
  1686. if (MI0.getNumOperands() != MI1.getNumOperands())
  1687. return false;
  1688. const MachineOperand &MO0 = MI0.getOperand(1);
  1689. const MachineOperand &MO1 = MI1.getOperand(1);
  1690. if (MO0.getOffset() != MO1.getOffset())
  1691. return false;
  1692. if (Opcode == ARM::LDRLIT_ga_pcrel || Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
  1693. Opcode == ARM::tLDRLIT_ga_pcrel || Opcode == ARM::t2LDRLIT_ga_pcrel ||
  1694. Opcode == ARM::MOV_ga_pcrel || Opcode == ARM::MOV_ga_pcrel_ldr ||
  1695. Opcode == ARM::t2MOV_ga_pcrel)
  1696. // Ignore the PC labels.
  1697. return MO0.getGlobal() == MO1.getGlobal();
  1698. const MachineFunction *MF = MI0.getParent()->getParent();
  1699. const MachineConstantPool *MCP = MF->getConstantPool();
  1700. int CPI0 = MO0.getIndex();
  1701. int CPI1 = MO1.getIndex();
  1702. const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
  1703. const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
  1704. bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
  1705. bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
  1706. if (isARMCP0 && isARMCP1) {
  1707. ARMConstantPoolValue *ACPV0 =
  1708. static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
  1709. ARMConstantPoolValue *ACPV1 =
  1710. static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
  1711. return ACPV0->hasSameValue(ACPV1);
  1712. } else if (!isARMCP0 && !isARMCP1) {
  1713. return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
  1714. }
  1715. return false;
  1716. } else if (Opcode == ARM::PICLDR) {
  1717. if (MI1.getOpcode() != Opcode)
  1718. return false;
  1719. if (MI0.getNumOperands() != MI1.getNumOperands())
  1720. return false;
  1721. Register Addr0 = MI0.getOperand(1).getReg();
  1722. Register Addr1 = MI1.getOperand(1).getReg();
  1723. if (Addr0 != Addr1) {
  1724. if (!MRI || !Register::isVirtualRegister(Addr0) ||
  1725. !Register::isVirtualRegister(Addr1))
  1726. return false;
  1727. // This assumes SSA form.
  1728. MachineInstr *Def0 = MRI->getVRegDef(Addr0);
  1729. MachineInstr *Def1 = MRI->getVRegDef(Addr1);
  1730. // Check if the loaded value, e.g. a constantpool of a global address, are
  1731. // the same.
  1732. if (!produceSameValue(*Def0, *Def1, MRI))
  1733. return false;
  1734. }
  1735. for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
  1736. // %12 = PICLDR %11, 0, 14, %noreg
  1737. const MachineOperand &MO0 = MI0.getOperand(i);
  1738. const MachineOperand &MO1 = MI1.getOperand(i);
  1739. if (!MO0.isIdenticalTo(MO1))
  1740. return false;
  1741. }
  1742. return true;
  1743. }
  1744. return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
  1745. }
  1746. /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
  1747. /// determine if two loads are loading from the same base address. It should
  1748. /// only return true if the base pointers are the same and the only differences
  1749. /// between the two addresses is the offset. It also returns the offsets by
  1750. /// reference.
  1751. ///
  1752. /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
  1753. /// is permanently disabled.
  1754. bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
  1755. int64_t &Offset1,
  1756. int64_t &Offset2) const {
  1757. // Don't worry about Thumb: just ARM and Thumb2.
  1758. if (Subtarget.isThumb1Only()) return false;
  1759. if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
  1760. return false;
  1761. switch (Load1->getMachineOpcode()) {
  1762. default:
  1763. return false;
  1764. case ARM::LDRi12:
  1765. case ARM::LDRBi12:
  1766. case ARM::LDRD:
  1767. case ARM::LDRH:
  1768. case ARM::LDRSB:
  1769. case ARM::LDRSH:
  1770. case ARM::VLDRD:
  1771. case ARM::VLDRS:
  1772. case ARM::t2LDRi8:
  1773. case ARM::t2LDRBi8:
  1774. case ARM::t2LDRDi8:
  1775. case ARM::t2LDRSHi8:
  1776. case ARM::t2LDRi12:
  1777. case ARM::t2LDRBi12:
  1778. case ARM::t2LDRSHi12:
  1779. break;
  1780. }
  1781. switch (Load2->getMachineOpcode()) {
  1782. default:
  1783. return false;
  1784. case ARM::LDRi12:
  1785. case ARM::LDRBi12:
  1786. case ARM::LDRD:
  1787. case ARM::LDRH:
  1788. case ARM::LDRSB:
  1789. case ARM::LDRSH:
  1790. case ARM::VLDRD:
  1791. case ARM::VLDRS:
  1792. case ARM::t2LDRi8:
  1793. case ARM::t2LDRBi8:
  1794. case ARM::t2LDRSHi8:
  1795. case ARM::t2LDRi12:
  1796. case ARM::t2LDRBi12:
  1797. case ARM::t2LDRSHi12:
  1798. break;
  1799. }
  1800. // Check if base addresses and chain operands match.
  1801. if (Load1->getOperand(0) != Load2->getOperand(0) ||
  1802. Load1->getOperand(4) != Load2->getOperand(4))
  1803. return false;
  1804. // Index should be Reg0.
  1805. if (Load1->getOperand(3) != Load2->getOperand(3))
  1806. return false;
  1807. // Determine the offsets.
  1808. if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
  1809. isa<ConstantSDNode>(Load2->getOperand(1))) {
  1810. Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
  1811. Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
  1812. return true;
  1813. }
  1814. return false;
  1815. }
  1816. /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
  1817. /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
  1818. /// be scheduled togther. On some targets if two loads are loading from
  1819. /// addresses in the same cache line, it's better if they are scheduled
  1820. /// together. This function takes two integers that represent the load offsets
  1821. /// from the common base address. It returns true if it decides it's desirable
  1822. /// to schedule the two loads together. "NumLoads" is the number of loads that
  1823. /// have already been scheduled after Load1.
  1824. ///
  1825. /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
  1826. /// is permanently disabled.
  1827. bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
  1828. int64_t Offset1, int64_t Offset2,
  1829. unsigned NumLoads) const {
  1830. // Don't worry about Thumb: just ARM and Thumb2.
  1831. if (Subtarget.isThumb1Only()) return false;
  1832. assert(Offset2 > Offset1);
  1833. if ((Offset2 - Offset1) / 8 > 64)
  1834. return false;
  1835. // Check if the machine opcodes are different. If they are different
  1836. // then we consider them to not be of the same base address,
  1837. // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
  1838. // In this case, they are considered to be the same because they are different
  1839. // encoding forms of the same basic instruction.
  1840. if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
  1841. !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
  1842. Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
  1843. (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
  1844. Load2->getMachineOpcode() == ARM::t2LDRBi8)))
  1845. return false; // FIXME: overly conservative?
  1846. // Four loads in a row should be sufficient.
  1847. if (NumLoads >= 3)
  1848. return false;
  1849. return true;
  1850. }
  1851. bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
  1852. const MachineBasicBlock *MBB,
  1853. const MachineFunction &MF) const {
  1854. // Debug info is never a scheduling boundary. It's necessary to be explicit
  1855. // due to the special treatment of IT instructions below, otherwise a
  1856. // dbg_value followed by an IT will result in the IT instruction being
  1857. // considered a scheduling hazard, which is wrong. It should be the actual
  1858. // instruction preceding the dbg_value instruction(s), just like it is
  1859. // when debug info is not present.
  1860. if (MI.isDebugInstr())
  1861. return false;
  1862. // Terminators and labels can't be scheduled around.
  1863. if (MI.isTerminator() || MI.isPosition())
  1864. return true;
  1865. // INLINEASM_BR can jump to another block
  1866. if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
  1867. return true;
  1868. // Treat the start of the IT block as a scheduling boundary, but schedule
  1869. // t2IT along with all instructions following it.
  1870. // FIXME: This is a big hammer. But the alternative is to add all potential
  1871. // true and anti dependencies to IT block instructions as implicit operands
  1872. // to the t2IT instruction. The added compile time and complexity does not
  1873. // seem worth it.
  1874. MachineBasicBlock::const_iterator I = MI;
  1875. // Make sure to skip any debug instructions
  1876. while (++I != MBB->end() && I->isDebugInstr())
  1877. ;
  1878. if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
  1879. return true;
  1880. // Don't attempt to schedule around any instruction that defines
  1881. // a stack-oriented pointer, as it's unlikely to be profitable. This
  1882. // saves compile time, because it doesn't require every single
  1883. // stack slot reference to depend on the instruction that does the
  1884. // modification.
  1885. // Calls don't actually change the stack pointer, even if they have imp-defs.
  1886. // No ARM calling conventions change the stack pointer. (X86 calling
  1887. // conventions sometimes do).
  1888. if (!MI.isCall() && MI.definesRegister(ARM::SP))
  1889. return true;
  1890. return false;
  1891. }
  1892. bool ARMBaseInstrInfo::
  1893. isProfitableToIfCvt(MachineBasicBlock &MBB,
  1894. unsigned NumCycles, unsigned ExtraPredCycles,
  1895. BranchProbability Probability) const {
  1896. if (!NumCycles)
  1897. return false;
  1898. // If we are optimizing for size, see if the branch in the predecessor can be
  1899. // lowered to cbn?z by the constant island lowering pass, and return false if
  1900. // so. This results in a shorter instruction sequence.
  1901. if (MBB.getParent()->getFunction().hasOptSize()) {
  1902. MachineBasicBlock *Pred = *MBB.pred_begin();
  1903. if (!Pred->empty()) {
  1904. MachineInstr *LastMI = &*Pred->rbegin();
  1905. if (LastMI->getOpcode() == ARM::t2Bcc) {
  1906. const TargetRegisterInfo *TRI = &getRegisterInfo();
  1907. MachineInstr *CmpMI = findCMPToFoldIntoCBZ(LastMI, TRI);
  1908. if (CmpMI)
  1909. return false;
  1910. }
  1911. }
  1912. }
  1913. return isProfitableToIfCvt(MBB, NumCycles, ExtraPredCycles,
  1914. MBB, 0, 0, Probability);
  1915. }
  1916. bool ARMBaseInstrInfo::
  1917. isProfitableToIfCvt(MachineBasicBlock &TBB,
  1918. unsigned TCycles, unsigned TExtra,
  1919. MachineBasicBlock &FBB,
  1920. unsigned FCycles, unsigned FExtra,
  1921. BranchProbability Probability) const {
  1922. if (!TCycles)
  1923. return false;
  1924. // In thumb code we often end up trading one branch for a IT block, and
  1925. // if we are cloning the instruction can increase code size. Prevent
  1926. // blocks with multiple predecesors from being ifcvted to prevent this
  1927. // cloning.
  1928. if (Subtarget.isThumb2() && TBB.getParent()->getFunction().hasMinSize()) {
  1929. if (TBB.pred_size() != 1 || FBB.pred_size() != 1)
  1930. return false;
  1931. }
  1932. // Attempt to estimate the relative costs of predication versus branching.
  1933. // Here we scale up each component of UnpredCost to avoid precision issue when
  1934. // scaling TCycles/FCycles by Probability.
  1935. const unsigned ScalingUpFactor = 1024;
  1936. unsigned PredCost = (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor;
  1937. unsigned UnpredCost;
  1938. if (!Subtarget.hasBranchPredictor()) {
  1939. // When we don't have a branch predictor it's always cheaper to not take a
  1940. // branch than take it, so we have to take that into account.
  1941. unsigned NotTakenBranchCost = 1;
  1942. unsigned TakenBranchCost = Subtarget.getMispredictionPenalty();
  1943. unsigned TUnpredCycles, FUnpredCycles;
  1944. if (!FCycles) {
  1945. // Triangle: TBB is the fallthrough
  1946. TUnpredCycles = TCycles + NotTakenBranchCost;
  1947. FUnpredCycles = TakenBranchCost;
  1948. } else {
  1949. // Diamond: TBB is the block that is branched to, FBB is the fallthrough
  1950. TUnpredCycles = TCycles + TakenBranchCost;
  1951. FUnpredCycles = FCycles + NotTakenBranchCost;
  1952. // The branch at the end of FBB will disappear when it's predicated, so
  1953. // discount it from PredCost.
  1954. PredCost -= 1 * ScalingUpFactor;
  1955. }
  1956. // The total cost is the cost of each path scaled by their probabilites
  1957. unsigned TUnpredCost = Probability.scale(TUnpredCycles * ScalingUpFactor);
  1958. unsigned FUnpredCost = Probability.getCompl().scale(FUnpredCycles * ScalingUpFactor);
  1959. UnpredCost = TUnpredCost + FUnpredCost;
  1960. // When predicating assume that the first IT can be folded away but later
  1961. // ones cost one cycle each
  1962. if (Subtarget.isThumb2() && TCycles + FCycles > 4) {
  1963. PredCost += ((TCycles + FCycles - 4) / 4) * ScalingUpFactor;
  1964. }
  1965. } else {
  1966. unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
  1967. unsigned FUnpredCost =
  1968. Probability.getCompl().scale(FCycles * ScalingUpFactor);
  1969. UnpredCost = TUnpredCost + FUnpredCost;
  1970. UnpredCost += 1 * ScalingUpFactor; // The branch itself
  1971. UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
  1972. }
  1973. return PredCost <= UnpredCost;
  1974. }
  1975. unsigned
  1976. ARMBaseInstrInfo::extraSizeToPredicateInstructions(const MachineFunction &MF,
  1977. unsigned NumInsts) const {
  1978. // Thumb2 needs a 2-byte IT instruction to predicate up to 4 instructions.
  1979. // ARM has a condition code field in every predicable instruction, using it
  1980. // doesn't change code size.
  1981. if (!Subtarget.isThumb2())
  1982. return 0;
  1983. // It's possible that the size of the IT is restricted to a single block.
  1984. unsigned MaxInsts = Subtarget.restrictIT() ? 1 : 4;
  1985. return divideCeil(NumInsts, MaxInsts) * 2;
  1986. }
  1987. unsigned
  1988. ARMBaseInstrInfo::predictBranchSizeForIfCvt(MachineInstr &MI) const {
  1989. // If this branch is likely to be folded into the comparison to form a
  1990. // CB(N)Z, then removing it won't reduce code size at all, because that will
  1991. // just replace the CB(N)Z with a CMP.
  1992. if (MI.getOpcode() == ARM::t2Bcc &&
  1993. findCMPToFoldIntoCBZ(&MI, &getRegisterInfo()))
  1994. return 0;
  1995. unsigned Size = getInstSizeInBytes(MI);
  1996. // For Thumb2, all branches are 32-bit instructions during the if conversion
  1997. // pass, but may be replaced with 16-bit instructions during size reduction.
  1998. // Since the branches considered by if conversion tend to be forward branches
  1999. // over small basic blocks, they are very likely to be in range for the
  2000. // narrow instructions, so we assume the final code size will be half what it
  2001. // currently is.
  2002. if (Subtarget.isThumb2())
  2003. Size /= 2;
  2004. return Size;
  2005. }
  2006. bool
  2007. ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
  2008. MachineBasicBlock &FMBB) const {
  2009. // Reduce false anti-dependencies to let the target's out-of-order execution
  2010. // engine do its thing.
  2011. return Subtarget.isProfitableToUnpredicate();
  2012. }
  2013. /// getInstrPredicate - If instruction is predicated, returns its predicate
  2014. /// condition, otherwise returns AL. It also returns the condition code
  2015. /// register by reference.
  2016. ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
  2017. Register &PredReg) {
  2018. int PIdx = MI.findFirstPredOperandIdx();
  2019. if (PIdx == -1) {
  2020. PredReg = 0;
  2021. return ARMCC::AL;
  2022. }
  2023. PredReg = MI.getOperand(PIdx+1).getReg();
  2024. return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
  2025. }
  2026. unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
  2027. if (Opc == ARM::B)
  2028. return ARM::Bcc;
  2029. if (Opc == ARM::tB)
  2030. return ARM::tBcc;
  2031. if (Opc == ARM::t2B)
  2032. return ARM::t2Bcc;
  2033. llvm_unreachable("Unknown unconditional branch opcode!");
  2034. }
  2035. MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
  2036. bool NewMI,
  2037. unsigned OpIdx1,
  2038. unsigned OpIdx2) const {
  2039. switch (MI.getOpcode()) {
  2040. case ARM::MOVCCr:
  2041. case ARM::t2MOVCCr: {
  2042. // MOVCC can be commuted by inverting the condition.
  2043. Register PredReg;
  2044. ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
  2045. // MOVCC AL can't be inverted. Shouldn't happen.
  2046. if (CC == ARMCC::AL || PredReg != ARM::CPSR)
  2047. return nullptr;
  2048. MachineInstr *CommutedMI =
  2049. TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
  2050. if (!CommutedMI)
  2051. return nullptr;
  2052. // After swapping the MOVCC operands, also invert the condition.
  2053. CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
  2054. .setImm(ARMCC::getOppositeCondition(CC));
  2055. return CommutedMI;
  2056. }
  2057. }
  2058. return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
  2059. }
  2060. /// Identify instructions that can be folded into a MOVCC instruction, and
  2061. /// return the defining instruction.
  2062. MachineInstr *
  2063. ARMBaseInstrInfo::canFoldIntoMOVCC(Register Reg, const MachineRegisterInfo &MRI,
  2064. const TargetInstrInfo *TII) const {
  2065. if (!Reg.isVirtual())
  2066. return nullptr;
  2067. if (!MRI.hasOneNonDBGUse(Reg))
  2068. return nullptr;
  2069. MachineInstr *MI = MRI.getVRegDef(Reg);
  2070. if (!MI)
  2071. return nullptr;
  2072. // Check if MI can be predicated and folded into the MOVCC.
  2073. if (!isPredicable(*MI))
  2074. return nullptr;
  2075. // Check if MI has any non-dead defs or physreg uses. This also detects
  2076. // predicated instructions which will be reading CPSR.
  2077. for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 1)) {
  2078. // Reject frame index operands, PEI can't handle the predicated pseudos.
  2079. if (MO.isFI() || MO.isCPI() || MO.isJTI())
  2080. return nullptr;
  2081. if (!MO.isReg())
  2082. continue;
  2083. // MI can't have any tied operands, that would conflict with predication.
  2084. if (MO.isTied())
  2085. return nullptr;
  2086. if (Register::isPhysicalRegister(MO.getReg()))
  2087. return nullptr;
  2088. if (MO.isDef() && !MO.isDead())
  2089. return nullptr;
  2090. }
  2091. bool DontMoveAcrossStores = true;
  2092. if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
  2093. return nullptr;
  2094. return MI;
  2095. }
  2096. bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
  2097. SmallVectorImpl<MachineOperand> &Cond,
  2098. unsigned &TrueOp, unsigned &FalseOp,
  2099. bool &Optimizable) const {
  2100. assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
  2101. "Unknown select instruction");
  2102. // MOVCC operands:
  2103. // 0: Def.
  2104. // 1: True use.
  2105. // 2: False use.
  2106. // 3: Condition code.
  2107. // 4: CPSR use.
  2108. TrueOp = 1;
  2109. FalseOp = 2;
  2110. Cond.push_back(MI.getOperand(3));
  2111. Cond.push_back(MI.getOperand(4));
  2112. // We can always fold a def.
  2113. Optimizable = true;
  2114. return false;
  2115. }
  2116. MachineInstr *
  2117. ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
  2118. SmallPtrSetImpl<MachineInstr *> &SeenMIs,
  2119. bool PreferFalse) const {
  2120. assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
  2121. "Unknown select instruction");
  2122. MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
  2123. MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
  2124. bool Invert = !DefMI;
  2125. if (!DefMI)
  2126. DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
  2127. if (!DefMI)
  2128. return nullptr;
  2129. // Find new register class to use.
  2130. MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
  2131. MachineOperand TrueReg = MI.getOperand(Invert ? 1 : 2);
  2132. Register DestReg = MI.getOperand(0).getReg();
  2133. const TargetRegisterClass *FalseClass = MRI.getRegClass(FalseReg.getReg());
  2134. const TargetRegisterClass *TrueClass = MRI.getRegClass(TrueReg.getReg());
  2135. if (!MRI.constrainRegClass(DestReg, FalseClass))
  2136. return nullptr;
  2137. if (!MRI.constrainRegClass(DestReg, TrueClass))
  2138. return nullptr;
  2139. // Create a new predicated version of DefMI.
  2140. // Rfalse is the first use.
  2141. MachineInstrBuilder NewMI =
  2142. BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
  2143. // Copy all the DefMI operands, excluding its (null) predicate.
  2144. const MCInstrDesc &DefDesc = DefMI->getDesc();
  2145. for (unsigned i = 1, e = DefDesc.getNumOperands();
  2146. i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
  2147. NewMI.add(DefMI->getOperand(i));
  2148. unsigned CondCode = MI.getOperand(3).getImm();
  2149. if (Invert)
  2150. NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
  2151. else
  2152. NewMI.addImm(CondCode);
  2153. NewMI.add(MI.getOperand(4));
  2154. // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
  2155. if (NewMI->hasOptionalDef())
  2156. NewMI.add(condCodeOp());
  2157. // The output register value when the predicate is false is an implicit
  2158. // register operand tied to the first def.
  2159. // The tie makes the register allocator ensure the FalseReg is allocated the
  2160. // same register as operand 0.
  2161. FalseReg.setImplicit();
  2162. NewMI.add(FalseReg);
  2163. NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
  2164. // Update SeenMIs set: register newly created MI and erase removed DefMI.
  2165. SeenMIs.insert(NewMI);
  2166. SeenMIs.erase(DefMI);
  2167. // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
  2168. // DefMI would be invalid when tranferred inside the loop. Checking for a
  2169. // loop is expensive, but at least remove kill flags if they are in different
  2170. // BBs.
  2171. if (DefMI->getParent() != MI.getParent())
  2172. NewMI->clearKillInfo();
  2173. // The caller will erase MI, but not DefMI.
  2174. DefMI->eraseFromParent();
  2175. return NewMI;
  2176. }
  2177. /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
  2178. /// instruction is encoded with an 'S' bit is determined by the optional CPSR
  2179. /// def operand.
  2180. ///
  2181. /// This will go away once we can teach tblgen how to set the optional CPSR def
  2182. /// operand itself.
  2183. struct AddSubFlagsOpcodePair {
  2184. uint16_t PseudoOpc;
  2185. uint16_t MachineOpc;
  2186. };
  2187. static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
  2188. {ARM::ADDSri, ARM::ADDri},
  2189. {ARM::ADDSrr, ARM::ADDrr},
  2190. {ARM::ADDSrsi, ARM::ADDrsi},
  2191. {ARM::ADDSrsr, ARM::ADDrsr},
  2192. {ARM::SUBSri, ARM::SUBri},
  2193. {ARM::SUBSrr, ARM::SUBrr},
  2194. {ARM::SUBSrsi, ARM::SUBrsi},
  2195. {ARM::SUBSrsr, ARM::SUBrsr},
  2196. {ARM::RSBSri, ARM::RSBri},
  2197. {ARM::RSBSrsi, ARM::RSBrsi},
  2198. {ARM::RSBSrsr, ARM::RSBrsr},
  2199. {ARM::tADDSi3, ARM::tADDi3},
  2200. {ARM::tADDSi8, ARM::tADDi8},
  2201. {ARM::tADDSrr, ARM::tADDrr},
  2202. {ARM::tADCS, ARM::tADC},
  2203. {ARM::tSUBSi3, ARM::tSUBi3},
  2204. {ARM::tSUBSi8, ARM::tSUBi8},
  2205. {ARM::tSUBSrr, ARM::tSUBrr},
  2206. {ARM::tSBCS, ARM::tSBC},
  2207. {ARM::tRSBS, ARM::tRSB},
  2208. {ARM::tLSLSri, ARM::tLSLri},
  2209. {ARM::t2ADDSri, ARM::t2ADDri},
  2210. {ARM::t2ADDSrr, ARM::t2ADDrr},
  2211. {ARM::t2ADDSrs, ARM::t2ADDrs},
  2212. {ARM::t2SUBSri, ARM::t2SUBri},
  2213. {ARM::t2SUBSrr, ARM::t2SUBrr},
  2214. {ARM::t2SUBSrs, ARM::t2SUBrs},
  2215. {ARM::t2RSBSri, ARM::t2RSBri},
  2216. {ARM::t2RSBSrs, ARM::t2RSBrs},
  2217. };
  2218. unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
  2219. for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
  2220. if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
  2221. return AddSubFlagsOpcodeMap[i].MachineOpc;
  2222. return 0;
  2223. }
  2224. void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
  2225. MachineBasicBlock::iterator &MBBI,
  2226. const DebugLoc &dl, Register DestReg,
  2227. Register BaseReg, int NumBytes,
  2228. ARMCC::CondCodes Pred, Register PredReg,
  2229. const ARMBaseInstrInfo &TII,
  2230. unsigned MIFlags) {
  2231. if (NumBytes == 0 && DestReg != BaseReg) {
  2232. BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
  2233. .addReg(BaseReg, RegState::Kill)
  2234. .add(predOps(Pred, PredReg))
  2235. .add(condCodeOp())
  2236. .setMIFlags(MIFlags);
  2237. return;
  2238. }
  2239. bool isSub = NumBytes < 0;
  2240. if (isSub) NumBytes = -NumBytes;
  2241. while (NumBytes) {
  2242. unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
  2243. unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
  2244. assert(ThisVal && "Didn't extract field correctly");
  2245. // We will handle these bits from offset, clear them.
  2246. NumBytes &= ~ThisVal;
  2247. assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
  2248. // Build the new ADD / SUB.
  2249. unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
  2250. BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
  2251. .addReg(BaseReg, RegState::Kill)
  2252. .addImm(ThisVal)
  2253. .add(predOps(Pred, PredReg))
  2254. .add(condCodeOp())
  2255. .setMIFlags(MIFlags);
  2256. BaseReg = DestReg;
  2257. }
  2258. }
  2259. bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
  2260. MachineFunction &MF, MachineInstr *MI,
  2261. unsigned NumBytes) {
  2262. // This optimisation potentially adds lots of load and store
  2263. // micro-operations, it's only really a great benefit to code-size.
  2264. if (!Subtarget.hasMinSize())
  2265. return false;
  2266. // If only one register is pushed/popped, LLVM can use an LDR/STR
  2267. // instead. We can't modify those so make sure we're dealing with an
  2268. // instruction we understand.
  2269. bool IsPop = isPopOpcode(MI->getOpcode());
  2270. bool IsPush = isPushOpcode(MI->getOpcode());
  2271. if (!IsPush && !IsPop)
  2272. return false;
  2273. bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
  2274. MI->getOpcode() == ARM::VLDMDIA_UPD;
  2275. bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
  2276. MI->getOpcode() == ARM::tPOP ||
  2277. MI->getOpcode() == ARM::tPOP_RET;
  2278. assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
  2279. MI->getOperand(1).getReg() == ARM::SP)) &&
  2280. "trying to fold sp update into non-sp-updating push/pop");
  2281. // The VFP push & pop act on D-registers, so we can only fold an adjustment
  2282. // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
  2283. // if this is violated.
  2284. if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
  2285. return false;
  2286. // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
  2287. // pred) so the list starts at 4. Thumb1 starts after the predicate.
  2288. int RegListIdx = IsT1PushPop ? 2 : 4;
  2289. // Calculate the space we'll need in terms of registers.
  2290. unsigned RegsNeeded;
  2291. const TargetRegisterClass *RegClass;
  2292. if (IsVFPPushPop) {
  2293. RegsNeeded = NumBytes / 8;
  2294. RegClass = &ARM::DPRRegClass;
  2295. } else {
  2296. RegsNeeded = NumBytes / 4;
  2297. RegClass = &ARM::GPRRegClass;
  2298. }
  2299. // We're going to have to strip all list operands off before
  2300. // re-adding them since the order matters, so save the existing ones
  2301. // for later.
  2302. SmallVector<MachineOperand, 4> RegList;
  2303. // We're also going to need the first register transferred by this
  2304. // instruction, which won't necessarily be the first register in the list.
  2305. unsigned FirstRegEnc = -1;
  2306. const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
  2307. for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
  2308. MachineOperand &MO = MI->getOperand(i);
  2309. RegList.push_back(MO);
  2310. if (MO.isReg() && !MO.isImplicit() &&
  2311. TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
  2312. FirstRegEnc = TRI->getEncodingValue(MO.getReg());
  2313. }
  2314. const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
  2315. // Now try to find enough space in the reglist to allocate NumBytes.
  2316. for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
  2317. --CurRegEnc) {
  2318. unsigned CurReg = RegClass->getRegister(CurRegEnc);
  2319. if (IsT1PushPop && CurRegEnc > TRI->getEncodingValue(ARM::R7))
  2320. continue;
  2321. if (!IsPop) {
  2322. // Pushing any register is completely harmless, mark the register involved
  2323. // as undef since we don't care about its value and must not restore it
  2324. // during stack unwinding.
  2325. RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
  2326. false, false, true));
  2327. --RegsNeeded;
  2328. continue;
  2329. }
  2330. // However, we can only pop an extra register if it's not live. For
  2331. // registers live within the function we might clobber a return value
  2332. // register; the other way a register can be live here is if it's
  2333. // callee-saved.
  2334. if (isCalleeSavedRegister(CurReg, CSRegs) ||
  2335. MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
  2336. MachineBasicBlock::LQR_Dead) {
  2337. // VFP pops don't allow holes in the register list, so any skip is fatal
  2338. // for our transformation. GPR pops do, so we should just keep looking.
  2339. if (IsVFPPushPop)
  2340. return false;
  2341. else
  2342. continue;
  2343. }
  2344. // Mark the unimportant registers as <def,dead> in the POP.
  2345. RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
  2346. true));
  2347. --RegsNeeded;
  2348. }
  2349. if (RegsNeeded > 0)
  2350. return false;
  2351. // Finally we know we can profitably perform the optimisation so go
  2352. // ahead: strip all existing registers off and add them back again
  2353. // in the right order.
  2354. for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
  2355. MI->RemoveOperand(i);
  2356. // Add the complete list back in.
  2357. MachineInstrBuilder MIB(MF, &*MI);
  2358. for (const MachineOperand &MO : llvm::reverse(RegList))
  2359. MIB.add(MO);
  2360. return true;
  2361. }
  2362. bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
  2363. Register FrameReg, int &Offset,
  2364. const ARMBaseInstrInfo &TII) {
  2365. unsigned Opcode = MI.getOpcode();
  2366. const MCInstrDesc &Desc = MI.getDesc();
  2367. unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
  2368. bool isSub = false;
  2369. // Memory operands in inline assembly always use AddrMode2.
  2370. if (Opcode == ARM::INLINEASM || Opcode == ARM::INLINEASM_BR)
  2371. AddrMode = ARMII::AddrMode2;
  2372. if (Opcode == ARM::ADDri) {
  2373. Offset += MI.getOperand(FrameRegIdx+1).getImm();
  2374. if (Offset == 0) {
  2375. // Turn it into a move.
  2376. MI.setDesc(TII.get(ARM::MOVr));
  2377. MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
  2378. MI.RemoveOperand(FrameRegIdx+1);
  2379. Offset = 0;
  2380. return true;
  2381. } else if (Offset < 0) {
  2382. Offset = -Offset;
  2383. isSub = true;
  2384. MI.setDesc(TII.get(ARM::SUBri));
  2385. }
  2386. // Common case: small offset, fits into instruction.
  2387. if (ARM_AM::getSOImmVal(Offset) != -1) {
  2388. // Replace the FrameIndex with sp / fp
  2389. MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
  2390. MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
  2391. Offset = 0;
  2392. return true;
  2393. }
  2394. // Otherwise, pull as much of the immedidate into this ADDri/SUBri
  2395. // as possible.
  2396. unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
  2397. unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
  2398. // We will handle these bits from offset, clear them.
  2399. Offset &= ~ThisImmVal;
  2400. // Get the properly encoded SOImmVal field.
  2401. assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
  2402. "Bit extraction didn't work?");
  2403. MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
  2404. } else {
  2405. unsigned ImmIdx = 0;
  2406. int InstrOffs = 0;
  2407. unsigned NumBits = 0;
  2408. unsigned Scale = 1;
  2409. switch (AddrMode) {
  2410. case ARMII::AddrMode_i12:
  2411. ImmIdx = FrameRegIdx + 1;
  2412. InstrOffs = MI.getOperand(ImmIdx).getImm();
  2413. NumBits = 12;
  2414. break;
  2415. case ARMII::AddrMode2:
  2416. ImmIdx = FrameRegIdx+2;
  2417. InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
  2418. if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
  2419. InstrOffs *= -1;
  2420. NumBits = 12;
  2421. break;
  2422. case ARMII::AddrMode3:
  2423. ImmIdx = FrameRegIdx+2;
  2424. InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
  2425. if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
  2426. InstrOffs *= -1;
  2427. NumBits = 8;
  2428. break;
  2429. case ARMII::AddrMode4:
  2430. case ARMII::AddrMode6:
  2431. // Can't fold any offset even if it's zero.
  2432. return false;
  2433. case ARMII::AddrMode5:
  2434. ImmIdx = FrameRegIdx+1;
  2435. InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
  2436. if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
  2437. InstrOffs *= -1;
  2438. NumBits = 8;
  2439. Scale = 4;
  2440. break;
  2441. case ARMII::AddrMode5FP16:
  2442. ImmIdx = FrameRegIdx+1;
  2443. InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
  2444. if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
  2445. InstrOffs *= -1;
  2446. NumBits = 8;
  2447. Scale = 2;
  2448. break;
  2449. case ARMII::AddrModeT2_i7:
  2450. case ARMII::AddrModeT2_i7s2:
  2451. case ARMII::AddrModeT2_i7s4:
  2452. ImmIdx = FrameRegIdx+1;
  2453. InstrOffs = MI.getOperand(ImmIdx).getImm();
  2454. NumBits = 7;
  2455. Scale = (AddrMode == ARMII::AddrModeT2_i7s2 ? 2 :
  2456. AddrMode == ARMII::AddrModeT2_i7s4 ? 4 : 1);
  2457. break;
  2458. default:
  2459. llvm_unreachable("Unsupported addressing mode!");
  2460. }
  2461. Offset += InstrOffs * Scale;
  2462. assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
  2463. if (Offset < 0) {
  2464. Offset = -Offset;
  2465. isSub = true;
  2466. }
  2467. // Attempt to fold address comp. if opcode has offset bits
  2468. if (NumBits > 0) {
  2469. // Common case: small offset, fits into instruction.
  2470. MachineOperand &ImmOp = MI.getOperand(ImmIdx);
  2471. int ImmedOffset = Offset / Scale;
  2472. unsigned Mask = (1 << NumBits) - 1;
  2473. if ((unsigned)Offset <= Mask * Scale) {
  2474. // Replace the FrameIndex with sp
  2475. MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
  2476. // FIXME: When addrmode2 goes away, this will simplify (like the
  2477. // T2 version), as the LDR.i12 versions don't need the encoding
  2478. // tricks for the offset value.
  2479. if (isSub) {
  2480. if (AddrMode == ARMII::AddrMode_i12)
  2481. ImmedOffset = -ImmedOffset;
  2482. else
  2483. ImmedOffset |= 1 << NumBits;
  2484. }
  2485. ImmOp.ChangeToImmediate(ImmedOffset);
  2486. Offset = 0;
  2487. return true;
  2488. }
  2489. // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
  2490. ImmedOffset = ImmedOffset & Mask;
  2491. if (isSub) {
  2492. if (AddrMode == ARMII::AddrMode_i12)
  2493. ImmedOffset = -ImmedOffset;
  2494. else
  2495. ImmedOffset |= 1 << NumBits;
  2496. }
  2497. ImmOp.ChangeToImmediate(ImmedOffset);
  2498. Offset &= ~(Mask*Scale);
  2499. }
  2500. }
  2501. Offset = (isSub) ? -Offset : Offset;
  2502. return Offset == 0;
  2503. }
  2504. /// analyzeCompare - For a comparison instruction, return the source registers
  2505. /// in SrcReg and SrcReg2 if having two register operands, and the value it
  2506. /// compares against in CmpValue. Return true if the comparison instruction
  2507. /// can be analyzed.
  2508. bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
  2509. Register &SrcReg2, int64_t &CmpMask,
  2510. int64_t &CmpValue) const {
  2511. switch (MI.getOpcode()) {
  2512. default: break;
  2513. case ARM::CMPri:
  2514. case ARM::t2CMPri:
  2515. case ARM::tCMPi8:
  2516. SrcReg = MI.getOperand(0).getReg();
  2517. SrcReg2 = 0;
  2518. CmpMask = ~0;
  2519. CmpValue = MI.getOperand(1).getImm();
  2520. return true;
  2521. case ARM::CMPrr:
  2522. case ARM::t2CMPrr:
  2523. case ARM::tCMPr:
  2524. SrcReg = MI.getOperand(0).getReg();
  2525. SrcReg2 = MI.getOperand(1).getReg();
  2526. CmpMask = ~0;
  2527. CmpValue = 0;
  2528. return true;
  2529. case ARM::TSTri:
  2530. case ARM::t2TSTri:
  2531. SrcReg = MI.getOperand(0).getReg();
  2532. SrcReg2 = 0;
  2533. CmpMask = MI.getOperand(1).getImm();
  2534. CmpValue = 0;
  2535. return true;
  2536. }
  2537. return false;
  2538. }
  2539. /// isSuitableForMask - Identify a suitable 'and' instruction that
  2540. /// operates on the given source register and applies the same mask
  2541. /// as a 'tst' instruction. Provide a limited look-through for copies.
  2542. /// When successful, MI will hold the found instruction.
  2543. static bool isSuitableForMask(MachineInstr *&MI, Register SrcReg,
  2544. int CmpMask, bool CommonUse) {
  2545. switch (MI->getOpcode()) {
  2546. case ARM::ANDri:
  2547. case ARM::t2ANDri:
  2548. if (CmpMask != MI->getOperand(2).getImm())
  2549. return false;
  2550. if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
  2551. return true;
  2552. break;
  2553. }
  2554. return false;
  2555. }
  2556. /// getCmpToAddCondition - assume the flags are set by CMP(a,b), return
  2557. /// the condition code if we modify the instructions such that flags are
  2558. /// set by ADD(a,b,X).
  2559. inline static ARMCC::CondCodes getCmpToAddCondition(ARMCC::CondCodes CC) {
  2560. switch (CC) {
  2561. default: return ARMCC::AL;
  2562. case ARMCC::HS: return ARMCC::LO;
  2563. case ARMCC::LO: return ARMCC::HS;
  2564. case ARMCC::VS: return ARMCC::VS;
  2565. case ARMCC::VC: return ARMCC::VC;
  2566. }
  2567. }
  2568. /// isRedundantFlagInstr - check whether the first instruction, whose only
  2569. /// purpose is to update flags, can be made redundant.
  2570. /// CMPrr can be made redundant by SUBrr if the operands are the same.
  2571. /// CMPri can be made redundant by SUBri if the operands are the same.
  2572. /// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X).
  2573. /// This function can be extended later on.
  2574. inline static bool isRedundantFlagInstr(const MachineInstr *CmpI,
  2575. Register SrcReg, Register SrcReg2,
  2576. int64_t ImmValue,
  2577. const MachineInstr *OI,
  2578. bool &IsThumb1) {
  2579. if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
  2580. (OI->getOpcode() == ARM::SUBrr || OI->getOpcode() == ARM::t2SUBrr) &&
  2581. ((OI->getOperand(1).getReg() == SrcReg &&
  2582. OI->getOperand(2).getReg() == SrcReg2) ||
  2583. (OI->getOperand(1).getReg() == SrcReg2 &&
  2584. OI->getOperand(2).getReg() == SrcReg))) {
  2585. IsThumb1 = false;
  2586. return true;
  2587. }
  2588. if (CmpI->getOpcode() == ARM::tCMPr && OI->getOpcode() == ARM::tSUBrr &&
  2589. ((OI->getOperand(2).getReg() == SrcReg &&
  2590. OI->getOperand(3).getReg() == SrcReg2) ||
  2591. (OI->getOperand(2).getReg() == SrcReg2 &&
  2592. OI->getOperand(3).getReg() == SrcReg))) {
  2593. IsThumb1 = true;
  2594. return true;
  2595. }
  2596. if ((CmpI->getOpcode() == ARM::CMPri || CmpI->getOpcode() == ARM::t2CMPri) &&
  2597. (OI->getOpcode() == ARM::SUBri || OI->getOpcode() == ARM::t2SUBri) &&
  2598. OI->getOperand(1).getReg() == SrcReg &&
  2599. OI->getOperand(2).getImm() == ImmValue) {
  2600. IsThumb1 = false;
  2601. return true;
  2602. }
  2603. if (CmpI->getOpcode() == ARM::tCMPi8 &&
  2604. (OI->getOpcode() == ARM::tSUBi8 || OI->getOpcode() == ARM::tSUBi3) &&
  2605. OI->getOperand(2).getReg() == SrcReg &&
  2606. OI->getOperand(3).getImm() == ImmValue) {
  2607. IsThumb1 = true;
  2608. return true;
  2609. }
  2610. if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
  2611. (OI->getOpcode() == ARM::ADDrr || OI->getOpcode() == ARM::t2ADDrr ||
  2612. OI->getOpcode() == ARM::ADDri || OI->getOpcode() == ARM::t2ADDri) &&
  2613. OI->getOperand(0).isReg() && OI->getOperand(1).isReg() &&
  2614. OI->getOperand(0).getReg() == SrcReg &&
  2615. OI->getOperand(1).getReg() == SrcReg2) {
  2616. IsThumb1 = false;
  2617. return true;
  2618. }
  2619. if (CmpI->getOpcode() == ARM::tCMPr &&
  2620. (OI->getOpcode() == ARM::tADDi3 || OI->getOpcode() == ARM::tADDi8 ||
  2621. OI->getOpcode() == ARM::tADDrr) &&
  2622. OI->getOperand(0).getReg() == SrcReg &&
  2623. OI->getOperand(2).getReg() == SrcReg2) {
  2624. IsThumb1 = true;
  2625. return true;
  2626. }
  2627. return false;
  2628. }
  2629. static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) {
  2630. switch (MI->getOpcode()) {
  2631. default: return false;
  2632. case ARM::tLSLri:
  2633. case ARM::tLSRri:
  2634. case ARM::tLSLrr:
  2635. case ARM::tLSRrr:
  2636. case ARM::tSUBrr:
  2637. case ARM::tADDrr:
  2638. case ARM::tADDi3:
  2639. case ARM::tADDi8:
  2640. case ARM::tSUBi3:
  2641. case ARM::tSUBi8:
  2642. case ARM::tMUL:
  2643. case ARM::tADC:
  2644. case ARM::tSBC:
  2645. case ARM::tRSB:
  2646. case ARM::tAND:
  2647. case ARM::tORR:
  2648. case ARM::tEOR:
  2649. case ARM::tBIC:
  2650. case ARM::tMVN:
  2651. case ARM::tASRri:
  2652. case ARM::tASRrr:
  2653. case ARM::tROR:
  2654. IsThumb1 = true;
  2655. LLVM_FALLTHROUGH;
  2656. case ARM::RSBrr:
  2657. case ARM::RSBri:
  2658. case ARM::RSCrr:
  2659. case ARM::RSCri:
  2660. case ARM::ADDrr:
  2661. case ARM::ADDri:
  2662. case ARM::ADCrr:
  2663. case ARM::ADCri:
  2664. case ARM::SUBrr:
  2665. case ARM::SUBri:
  2666. case ARM::SBCrr:
  2667. case ARM::SBCri:
  2668. case ARM::t2RSBri:
  2669. case ARM::t2ADDrr:
  2670. case ARM::t2ADDri:
  2671. case ARM::t2ADCrr:
  2672. case ARM::t2ADCri:
  2673. case ARM::t2SUBrr:
  2674. case ARM::t2SUBri:
  2675. case ARM::t2SBCrr:
  2676. case ARM::t2SBCri:
  2677. case ARM::ANDrr:
  2678. case ARM::ANDri:
  2679. case ARM::t2ANDrr:
  2680. case ARM::t2ANDri:
  2681. case ARM::ORRrr:
  2682. case ARM::ORRri:
  2683. case ARM::t2ORRrr:
  2684. case ARM::t2ORRri:
  2685. case ARM::EORrr:
  2686. case ARM::EORri:
  2687. case ARM::t2EORrr:
  2688. case ARM::t2EORri:
  2689. case ARM::t2LSRri:
  2690. case ARM::t2LSRrr:
  2691. case ARM::t2LSLri:
  2692. case ARM::t2LSLrr:
  2693. return true;
  2694. }
  2695. }
  2696. /// optimizeCompareInstr - Convert the instruction supplying the argument to the
  2697. /// comparison into one that sets the zero bit in the flags register;
  2698. /// Remove a redundant Compare instruction if an earlier instruction can set the
  2699. /// flags in the same way as Compare.
  2700. /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
  2701. /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
  2702. /// condition code of instructions which use the flags.
  2703. bool ARMBaseInstrInfo::optimizeCompareInstr(
  2704. MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask,
  2705. int64_t CmpValue, const MachineRegisterInfo *MRI) const {
  2706. // Get the unique definition of SrcReg.
  2707. MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
  2708. if (!MI) return false;
  2709. // Masked compares sometimes use the same register as the corresponding 'and'.
  2710. if (CmpMask != ~0) {
  2711. if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
  2712. MI = nullptr;
  2713. for (MachineRegisterInfo::use_instr_iterator
  2714. UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
  2715. UI != UE; ++UI) {
  2716. if (UI->getParent() != CmpInstr.getParent())
  2717. continue;
  2718. MachineInstr *PotentialAND = &*UI;
  2719. if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
  2720. isPredicated(*PotentialAND))
  2721. continue;
  2722. MI = PotentialAND;
  2723. break;
  2724. }
  2725. if (!MI) return false;
  2726. }
  2727. }
  2728. // Get ready to iterate backward from CmpInstr.
  2729. MachineBasicBlock::iterator I = CmpInstr, E = MI,
  2730. B = CmpInstr.getParent()->begin();
  2731. // Early exit if CmpInstr is at the beginning of the BB.
  2732. if (I == B) return false;
  2733. // There are two possible candidates which can be changed to set CPSR:
  2734. // One is MI, the other is a SUB or ADD instruction.
  2735. // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or
  2736. // ADDr[ri](r1, r2, X).
  2737. // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
  2738. MachineInstr *SubAdd = nullptr;
  2739. if (SrcReg2 != 0)
  2740. // MI is not a candidate for CMPrr.
  2741. MI = nullptr;
  2742. else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
  2743. // Conservatively refuse to convert an instruction which isn't in the same
  2744. // BB as the comparison.
  2745. // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate.
  2746. // Thus we cannot return here.
  2747. if (CmpInstr.getOpcode() == ARM::CMPri ||
  2748. CmpInstr.getOpcode() == ARM::t2CMPri ||
  2749. CmpInstr.getOpcode() == ARM::tCMPi8)
  2750. MI = nullptr;
  2751. else
  2752. return false;
  2753. }
  2754. bool IsThumb1 = false;
  2755. if (MI && !isOptimizeCompareCandidate(MI, IsThumb1))
  2756. return false;
  2757. // We also want to do this peephole for cases like this: if (a*b == 0),
  2758. // and optimise away the CMP instruction from the generated code sequence:
  2759. // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
  2760. // resulting from the select instruction, but these MOVS instructions for
  2761. // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
  2762. // However, if we only have MOVS instructions in between the CMP and the
  2763. // other instruction (the MULS in this example), then the CPSR is dead so we
  2764. // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
  2765. // reordering and then continue the analysis hoping we can eliminate the
  2766. // CMP. This peephole works on the vregs, so is still in SSA form. As a
  2767. // consequence, the movs won't redefine/kill the MUL operands which would
  2768. // make this reordering illegal.
  2769. const TargetRegisterInfo *TRI = &getRegisterInfo();
  2770. if (MI && IsThumb1) {
  2771. --I;
  2772. if (I != E && !MI->readsRegister(ARM::CPSR, TRI)) {
  2773. bool CanReorder = true;
  2774. for (; I != E; --I) {
  2775. if (I->getOpcode() != ARM::tMOVi8) {
  2776. CanReorder = false;
  2777. break;
  2778. }
  2779. }
  2780. if (CanReorder) {
  2781. MI = MI->removeFromParent();
  2782. E = CmpInstr;
  2783. CmpInstr.getParent()->insert(E, MI);
  2784. }
  2785. }
  2786. I = CmpInstr;
  2787. E = MI;
  2788. }
  2789. // Check that CPSR isn't set between the comparison instruction and the one we
  2790. // want to change. At the same time, search for SubAdd.
  2791. bool SubAddIsThumb1 = false;
  2792. do {
  2793. const MachineInstr &Instr = *--I;
  2794. // Check whether CmpInstr can be made redundant by the current instruction.
  2795. if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &Instr,
  2796. SubAddIsThumb1)) {
  2797. SubAdd = &*I;
  2798. break;
  2799. }
  2800. // Allow E (which was initially MI) to be SubAdd but do not search before E.
  2801. if (I == E)
  2802. break;
  2803. if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
  2804. Instr.readsRegister(ARM::CPSR, TRI))
  2805. // This instruction modifies or uses CPSR after the one we want to
  2806. // change. We can't do this transformation.
  2807. return false;
  2808. if (I == B) {
  2809. // In some cases, we scan the use-list of an instruction for an AND;
  2810. // that AND is in the same BB, but may not be scheduled before the
  2811. // corresponding TST. In that case, bail out.
  2812. //
  2813. // FIXME: We could try to reschedule the AND.
  2814. return false;
  2815. }
  2816. } while (true);
  2817. // Return false if no candidates exist.
  2818. if (!MI && !SubAdd)
  2819. return false;
  2820. // If we found a SubAdd, use it as it will be closer to the CMP
  2821. if (SubAdd) {
  2822. MI = SubAdd;
  2823. IsThumb1 = SubAddIsThumb1;
  2824. }
  2825. // We can't use a predicated instruction - it doesn't always write the flags.
  2826. if (isPredicated(*MI))
  2827. return false;
  2828. // Scan forward for the use of CPSR
  2829. // When checking against MI: if it's a conditional code that requires
  2830. // checking of the V bit or C bit, then this is not safe to do.
  2831. // It is safe to remove CmpInstr if CPSR is redefined or killed.
  2832. // If we are done with the basic block, we need to check whether CPSR is
  2833. // live-out.
  2834. SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
  2835. OperandsToUpdate;
  2836. bool isSafe = false;
  2837. I = CmpInstr;
  2838. E = CmpInstr.getParent()->end();
  2839. while (!isSafe && ++I != E) {
  2840. const MachineInstr &Instr = *I;
  2841. for (unsigned IO = 0, EO = Instr.getNumOperands();
  2842. !isSafe && IO != EO; ++IO) {
  2843. const MachineOperand &MO = Instr.getOperand(IO);
  2844. if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
  2845. isSafe = true;
  2846. break;
  2847. }
  2848. if (!MO.isReg() || MO.getReg() != ARM::CPSR)
  2849. continue;
  2850. if (MO.isDef()) {
  2851. isSafe = true;
  2852. break;
  2853. }
  2854. // Condition code is after the operand before CPSR except for VSELs.
  2855. ARMCC::CondCodes CC;
  2856. bool IsInstrVSel = true;
  2857. switch (Instr.getOpcode()) {
  2858. default:
  2859. IsInstrVSel = false;
  2860. CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
  2861. break;
  2862. case ARM::VSELEQD:
  2863. case ARM::VSELEQS:
  2864. case ARM::VSELEQH:
  2865. CC = ARMCC::EQ;
  2866. break;
  2867. case ARM::VSELGTD:
  2868. case ARM::VSELGTS:
  2869. case ARM::VSELGTH:
  2870. CC = ARMCC::GT;
  2871. break;
  2872. case ARM::VSELGED:
  2873. case ARM::VSELGES:
  2874. case ARM::VSELGEH:
  2875. CC = ARMCC::GE;
  2876. break;
  2877. case ARM::VSELVSD:
  2878. case ARM::VSELVSS:
  2879. case ARM::VSELVSH:
  2880. CC = ARMCC::VS;
  2881. break;
  2882. }
  2883. if (SubAdd) {
  2884. // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
  2885. // on CMP needs to be updated to be based on SUB.
  2886. // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also
  2887. // needs to be modified.
  2888. // Push the condition code operands to OperandsToUpdate.
  2889. // If it is safe to remove CmpInstr, the condition code of these
  2890. // operands will be modified.
  2891. unsigned Opc = SubAdd->getOpcode();
  2892. bool IsSub = Opc == ARM::SUBrr || Opc == ARM::t2SUBrr ||
  2893. Opc == ARM::SUBri || Opc == ARM::t2SUBri ||
  2894. Opc == ARM::tSUBrr || Opc == ARM::tSUBi3 ||
  2895. Opc == ARM::tSUBi8;
  2896. unsigned OpI = Opc != ARM::tSUBrr ? 1 : 2;
  2897. if (!IsSub ||
  2898. (SrcReg2 != 0 && SubAdd->getOperand(OpI).getReg() == SrcReg2 &&
  2899. SubAdd->getOperand(OpI + 1).getReg() == SrcReg)) {
  2900. // VSel doesn't support condition code update.
  2901. if (IsInstrVSel)
  2902. return false;
  2903. // Ensure we can swap the condition.
  2904. ARMCC::CondCodes NewCC = (IsSub ? getSwappedCondition(CC) : getCmpToAddCondition(CC));
  2905. if (NewCC == ARMCC::AL)
  2906. return false;
  2907. OperandsToUpdate.push_back(
  2908. std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
  2909. }
  2910. } else {
  2911. // No SubAdd, so this is x = <op> y, z; cmp x, 0.
  2912. switch (CC) {
  2913. case ARMCC::EQ: // Z
  2914. case ARMCC::NE: // Z
  2915. case ARMCC::MI: // N
  2916. case ARMCC::PL: // N
  2917. case ARMCC::AL: // none
  2918. // CPSR can be used multiple times, we should continue.
  2919. break;
  2920. case ARMCC::HS: // C
  2921. case ARMCC::LO: // C
  2922. case ARMCC::VS: // V
  2923. case ARMCC::VC: // V
  2924. case ARMCC::HI: // C Z
  2925. case ARMCC::LS: // C Z
  2926. case ARMCC::GE: // N V
  2927. case ARMCC::LT: // N V
  2928. case ARMCC::GT: // Z N V
  2929. case ARMCC::LE: // Z N V
  2930. // The instruction uses the V bit or C bit which is not safe.
  2931. return false;
  2932. }
  2933. }
  2934. }
  2935. }
  2936. // If CPSR is not killed nor re-defined, we should check whether it is
  2937. // live-out. If it is live-out, do not optimize.
  2938. if (!isSafe) {
  2939. MachineBasicBlock *MBB = CmpInstr.getParent();
  2940. for (MachineBasicBlock *Succ : MBB->successors())
  2941. if (Succ->isLiveIn(ARM::CPSR))
  2942. return false;
  2943. }
  2944. // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
  2945. // set CPSR so this is represented as an explicit output)
  2946. if (!IsThumb1) {
  2947. MI->getOperand(5).setReg(ARM::CPSR);
  2948. MI->getOperand(5).setIsDef(true);
  2949. }
  2950. assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
  2951. CmpInstr.eraseFromParent();
  2952. // Modify the condition code of operands in OperandsToUpdate.
  2953. // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
  2954. // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
  2955. for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
  2956. OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
  2957. MI->clearRegisterDeads(ARM::CPSR);
  2958. return true;
  2959. }
  2960. bool ARMBaseInstrInfo::shouldSink(const MachineInstr &MI) const {
  2961. // Do not sink MI if it might be used to optimize a redundant compare.
  2962. // We heuristically only look at the instruction immediately following MI to
  2963. // avoid potentially searching the entire basic block.
  2964. if (isPredicated(MI))
  2965. return true;
  2966. MachineBasicBlock::const_iterator Next = &MI;
  2967. ++Next;
  2968. Register SrcReg, SrcReg2;
  2969. int64_t CmpMask, CmpValue;
  2970. bool IsThumb1;
  2971. if (Next != MI.getParent()->end() &&
  2972. analyzeCompare(*Next, SrcReg, SrcReg2, CmpMask, CmpValue) &&
  2973. isRedundantFlagInstr(&*Next, SrcReg, SrcReg2, CmpValue, &MI, IsThumb1))
  2974. return false;
  2975. return true;
  2976. }
  2977. bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
  2978. Register Reg,
  2979. MachineRegisterInfo *MRI) const {
  2980. // Fold large immediates into add, sub, or, xor.
  2981. unsigned DefOpc = DefMI.getOpcode();
  2982. if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
  2983. return false;
  2984. if (!DefMI.getOperand(1).isImm())
  2985. // Could be t2MOVi32imm @xx
  2986. return false;
  2987. if (!MRI->hasOneNonDBGUse(Reg))
  2988. return false;
  2989. const MCInstrDesc &DefMCID = DefMI.getDesc();
  2990. if (DefMCID.hasOptionalDef()) {
  2991. unsigned NumOps = DefMCID.getNumOperands();
  2992. const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
  2993. if (MO.getReg() == ARM::CPSR && !MO.isDead())
  2994. // If DefMI defines CPSR and it is not dead, it's obviously not safe
  2995. // to delete DefMI.
  2996. return false;
  2997. }
  2998. const MCInstrDesc &UseMCID = UseMI.getDesc();
  2999. if (UseMCID.hasOptionalDef()) {
  3000. unsigned NumOps = UseMCID.getNumOperands();
  3001. if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
  3002. // If the instruction sets the flag, do not attempt this optimization
  3003. // since it may change the semantics of the code.
  3004. return false;
  3005. }
  3006. unsigned UseOpc = UseMI.getOpcode();
  3007. unsigned NewUseOpc = 0;
  3008. uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
  3009. uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
  3010. bool Commute = false;
  3011. switch (UseOpc) {
  3012. default: return false;
  3013. case ARM::SUBrr:
  3014. case ARM::ADDrr:
  3015. case ARM::ORRrr:
  3016. case ARM::EORrr:
  3017. case ARM::t2SUBrr:
  3018. case ARM::t2ADDrr:
  3019. case ARM::t2ORRrr:
  3020. case ARM::t2EORrr: {
  3021. Commute = UseMI.getOperand(2).getReg() != Reg;
  3022. switch (UseOpc) {
  3023. default: break;
  3024. case ARM::ADDrr:
  3025. case ARM::SUBrr:
  3026. if (UseOpc == ARM::SUBrr && Commute)
  3027. return false;
  3028. // ADD/SUB are special because they're essentially the same operation, so
  3029. // we can handle a larger range of immediates.
  3030. if (ARM_AM::isSOImmTwoPartVal(ImmVal))
  3031. NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
  3032. else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
  3033. ImmVal = -ImmVal;
  3034. NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
  3035. } else
  3036. return false;
  3037. SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
  3038. SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
  3039. break;
  3040. case ARM::ORRrr:
  3041. case ARM::EORrr:
  3042. if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
  3043. return false;
  3044. SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
  3045. SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
  3046. switch (UseOpc) {
  3047. default: break;
  3048. case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
  3049. case ARM::EORrr: NewUseOpc = ARM::EORri; break;
  3050. }
  3051. break;
  3052. case ARM::t2ADDrr:
  3053. case ARM::t2SUBrr: {
  3054. if (UseOpc == ARM::t2SUBrr && Commute)
  3055. return false;
  3056. // ADD/SUB are special because they're essentially the same operation, so
  3057. // we can handle a larger range of immediates.
  3058. const bool ToSP = DefMI.getOperand(0).getReg() == ARM::SP;
  3059. const unsigned t2ADD = ToSP ? ARM::t2ADDspImm : ARM::t2ADDri;
  3060. const unsigned t2SUB = ToSP ? ARM::t2SUBspImm : ARM::t2SUBri;
  3061. if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
  3062. NewUseOpc = UseOpc == ARM::t2ADDrr ? t2ADD : t2SUB;
  3063. else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
  3064. ImmVal = -ImmVal;
  3065. NewUseOpc = UseOpc == ARM::t2ADDrr ? t2SUB : t2ADD;
  3066. } else
  3067. return false;
  3068. SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
  3069. SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
  3070. break;
  3071. }
  3072. case ARM::t2ORRrr:
  3073. case ARM::t2EORrr:
  3074. if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
  3075. return false;
  3076. SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
  3077. SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
  3078. switch (UseOpc) {
  3079. default: break;
  3080. case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
  3081. case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
  3082. }
  3083. break;
  3084. }
  3085. }
  3086. }
  3087. unsigned OpIdx = Commute ? 2 : 1;
  3088. Register Reg1 = UseMI.getOperand(OpIdx).getReg();
  3089. bool isKill = UseMI.getOperand(OpIdx).isKill();
  3090. const TargetRegisterClass *TRC = MRI->getRegClass(Reg);
  3091. Register NewReg = MRI->createVirtualRegister(TRC);
  3092. BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc),
  3093. NewReg)
  3094. .addReg(Reg1, getKillRegState(isKill))
  3095. .addImm(SOImmValV1)
  3096. .add(predOps(ARMCC::AL))
  3097. .add(condCodeOp());
  3098. UseMI.setDesc(get(NewUseOpc));
  3099. UseMI.getOperand(1).setReg(NewReg);
  3100. UseMI.getOperand(1).setIsKill();
  3101. UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
  3102. DefMI.eraseFromParent();
  3103. // FIXME: t2ADDrr should be split, as different rulles apply when writing to SP.
  3104. // Just as t2ADDri, that was split to [t2ADDri, t2ADDspImm].
  3105. // Then the below code will not be needed, as the input/output register
  3106. // classes will be rgpr or gprSP.
  3107. // For now, we fix the UseMI operand explicitly here:
  3108. switch(NewUseOpc){
  3109. case ARM::t2ADDspImm:
  3110. case ARM::t2SUBspImm:
  3111. case ARM::t2ADDri:
  3112. case ARM::t2SUBri:
  3113. MRI->constrainRegClass(UseMI.getOperand(0).getReg(), TRC);
  3114. }
  3115. return true;
  3116. }
  3117. static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
  3118. const MachineInstr &MI) {
  3119. switch (MI.getOpcode()) {
  3120. default: {
  3121. const MCInstrDesc &Desc = MI.getDesc();
  3122. int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
  3123. assert(UOps >= 0 && "bad # UOps");
  3124. return UOps;
  3125. }
  3126. case ARM::LDRrs:
  3127. case ARM::LDRBrs:
  3128. case ARM::STRrs:
  3129. case ARM::STRBrs: {
  3130. unsigned ShOpVal = MI.getOperand(3).getImm();
  3131. bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
  3132. unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
  3133. if (!isSub &&
  3134. (ShImm == 0 ||
  3135. ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
  3136. ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
  3137. return 1;
  3138. return 2;
  3139. }
  3140. case ARM::LDRH:
  3141. case ARM::STRH: {
  3142. if (!MI.getOperand(2).getReg())
  3143. return 1;
  3144. unsigned ShOpVal = MI.getOperand(3).getImm();
  3145. bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
  3146. unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
  3147. if (!isSub &&
  3148. (ShImm == 0 ||
  3149. ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
  3150. ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
  3151. return 1;
  3152. return 2;
  3153. }
  3154. case ARM::LDRSB:
  3155. case ARM::LDRSH:
  3156. return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
  3157. case ARM::LDRSB_POST:
  3158. case ARM::LDRSH_POST: {
  3159. Register Rt = MI.getOperand(0).getReg();
  3160. Register Rm = MI.getOperand(3).getReg();
  3161. return (Rt == Rm) ? 4 : 3;
  3162. }
  3163. case ARM::LDR_PRE_REG:
  3164. case ARM::LDRB_PRE_REG: {
  3165. Register Rt = MI.getOperand(0).getReg();
  3166. Register Rm = MI.getOperand(3).getReg();
  3167. if (Rt == Rm)
  3168. return 3;
  3169. unsigned ShOpVal = MI.getOperand(4).getImm();
  3170. bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
  3171. unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
  3172. if (!isSub &&
  3173. (ShImm == 0 ||
  3174. ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
  3175. ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
  3176. return 2;
  3177. return 3;
  3178. }
  3179. case ARM::STR_PRE_REG:
  3180. case ARM::STRB_PRE_REG: {
  3181. unsigned ShOpVal = MI.getOperand(4).getImm();
  3182. bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
  3183. unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
  3184. if (!isSub &&
  3185. (ShImm == 0 ||
  3186. ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
  3187. ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
  3188. return 2;
  3189. return 3;
  3190. }
  3191. case ARM::LDRH_PRE:
  3192. case ARM::STRH_PRE: {
  3193. Register Rt = MI.getOperand(0).getReg();
  3194. Register Rm = MI.getOperand(3).getReg();
  3195. if (!Rm)
  3196. return 2;
  3197. if (Rt == Rm)
  3198. return 3;
  3199. return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
  3200. }
  3201. case ARM::LDR_POST_REG:
  3202. case ARM::LDRB_POST_REG:
  3203. case ARM::LDRH_POST: {
  3204. Register Rt = MI.getOperand(0).getReg();
  3205. Register Rm = MI.getOperand(3).getReg();
  3206. return (Rt == Rm) ? 3 : 2;
  3207. }
  3208. case ARM::LDR_PRE_IMM:
  3209. case ARM::LDRB_PRE_IMM:
  3210. case ARM::LDR_POST_IMM:
  3211. case ARM::LDRB_POST_IMM:
  3212. case ARM::STRB_POST_IMM:
  3213. case ARM::STRB_POST_REG:
  3214. case ARM::STRB_PRE_IMM:
  3215. case ARM::STRH_POST:
  3216. case ARM::STR_POST_IMM:
  3217. case ARM::STR_POST_REG:
  3218. case ARM::STR_PRE_IMM:
  3219. return 2;
  3220. case ARM::LDRSB_PRE:
  3221. case ARM::LDRSH_PRE: {
  3222. Register Rm = MI.getOperand(3).getReg();
  3223. if (Rm == 0)
  3224. return 3;
  3225. Register Rt = MI.getOperand(0).getReg();
  3226. if (Rt == Rm)
  3227. return 4;
  3228. unsigned ShOpVal = MI.getOperand(4).getImm();
  3229. bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
  3230. unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
  3231. if (!isSub &&
  3232. (ShImm == 0 ||
  3233. ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
  3234. ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
  3235. return 3;
  3236. return 4;
  3237. }
  3238. case ARM::LDRD: {
  3239. Register Rt = MI.getOperand(0).getReg();
  3240. Register Rn = MI.getOperand(2).getReg();
  3241. Register Rm = MI.getOperand(3).getReg();
  3242. if (Rm)
  3243. return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
  3244. : 3;
  3245. return (Rt == Rn) ? 3 : 2;
  3246. }
  3247. case ARM::STRD: {
  3248. Register Rm = MI.getOperand(3).getReg();
  3249. if (Rm)
  3250. return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
  3251. : 3;
  3252. return 2;
  3253. }
  3254. case ARM::LDRD_POST:
  3255. case ARM::t2LDRD_POST:
  3256. return 3;
  3257. case ARM::STRD_POST:
  3258. case ARM::t2STRD_POST:
  3259. return 4;
  3260. case ARM::LDRD_PRE: {
  3261. Register Rt = MI.getOperand(0).getReg();
  3262. Register Rn = MI.getOperand(3).getReg();
  3263. Register Rm = MI.getOperand(4).getReg();
  3264. if (Rm)
  3265. return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
  3266. : 4;
  3267. return (Rt == Rn) ? 4 : 3;
  3268. }
  3269. case ARM::t2LDRD_PRE: {
  3270. Register Rt = MI.getOperand(0).getReg();
  3271. Register Rn = MI.getOperand(3).getReg();
  3272. return (Rt == Rn) ? 4 : 3;
  3273. }
  3274. case ARM::STRD_PRE: {
  3275. Register Rm = MI.getOperand(4).getReg();
  3276. if (Rm)
  3277. return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
  3278. : 4;
  3279. return 3;
  3280. }
  3281. case ARM::t2STRD_PRE:
  3282. return 3;
  3283. case ARM::t2LDR_POST:
  3284. case ARM::t2LDRB_POST:
  3285. case ARM::t2LDRB_PRE:
  3286. case ARM::t2LDRSBi12:
  3287. case ARM::t2LDRSBi8:
  3288. case ARM::t2LDRSBpci:
  3289. case ARM::t2LDRSBs:
  3290. case ARM::t2LDRH_POST:
  3291. case ARM::t2LDRH_PRE:
  3292. case ARM::t2LDRSBT:
  3293. case ARM::t2LDRSB_POST:
  3294. case ARM::t2LDRSB_PRE:
  3295. case ARM::t2LDRSH_POST:
  3296. case ARM::t2LDRSH_PRE:
  3297. case ARM::t2LDRSHi12:
  3298. case ARM::t2LDRSHi8:
  3299. case ARM::t2LDRSHpci:
  3300. case ARM::t2LDRSHs:
  3301. return 2;
  3302. case ARM::t2LDRDi8: {
  3303. Register Rt = MI.getOperand(0).getReg();
  3304. Register Rn = MI.getOperand(2).getReg();
  3305. return (Rt == Rn) ? 3 : 2;
  3306. }
  3307. case ARM::t2STRB_POST:
  3308. case ARM::t2STRB_PRE:
  3309. case ARM::t2STRBs:
  3310. case ARM::t2STRDi8:
  3311. case ARM::t2STRH_POST:
  3312. case ARM::t2STRH_PRE:
  3313. case ARM::t2STRHs:
  3314. case ARM::t2STR_POST:
  3315. case ARM::t2STR_PRE:
  3316. case ARM::t2STRs:
  3317. return 2;
  3318. }
  3319. }
  3320. // Return the number of 32-bit words loaded by LDM or stored by STM. If this
  3321. // can't be easily determined return 0 (missing MachineMemOperand).
  3322. //
  3323. // FIXME: The current MachineInstr design does not support relying on machine
  3324. // mem operands to determine the width of a memory access. Instead, we expect
  3325. // the target to provide this information based on the instruction opcode and
  3326. // operands. However, using MachineMemOperand is the best solution now for
  3327. // two reasons:
  3328. //
  3329. // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
  3330. // operands. This is much more dangerous than using the MachineMemOperand
  3331. // sizes because CodeGen passes can insert/remove optional machine operands. In
  3332. // fact, it's totally incorrect for preRA passes and appears to be wrong for
  3333. // postRA passes as well.
  3334. //
  3335. // 2) getNumLDMAddresses is only used by the scheduling machine model and any
  3336. // machine model that calls this should handle the unknown (zero size) case.
  3337. //
  3338. // Long term, we should require a target hook that verifies MachineMemOperand
  3339. // sizes during MC lowering. That target hook should be local to MC lowering
  3340. // because we can't ensure that it is aware of other MI forms. Doing this will
  3341. // ensure that MachineMemOperands are correctly propagated through all passes.
  3342. unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
  3343. unsigned Size = 0;
  3344. for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
  3345. E = MI.memoperands_end();
  3346. I != E; ++I) {
  3347. Size += (*I)->getSize();
  3348. }
  3349. // FIXME: The scheduler currently can't handle values larger than 16. But
  3350. // the values can actually go up to 32 for floating-point load/store
  3351. // multiple (VLDMIA etc.). Also, the way this code is reasoning about memory
  3352. // operations isn't right; we could end up with "extra" memory operands for
  3353. // various reasons, like tail merge merging two memory operations.
  3354. return std::min(Size / 4, 16U);
  3355. }
  3356. static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
  3357. unsigned NumRegs) {
  3358. unsigned UOps = 1 + NumRegs; // 1 for address computation.
  3359. switch (Opc) {
  3360. default:
  3361. break;
  3362. case ARM::VLDMDIA_UPD:
  3363. case ARM::VLDMDDB_UPD:
  3364. case ARM::VLDMSIA_UPD:
  3365. case ARM::VLDMSDB_UPD:
  3366. case ARM::VSTMDIA_UPD:
  3367. case ARM::VSTMDDB_UPD:
  3368. case ARM::VSTMSIA_UPD:
  3369. case ARM::VSTMSDB_UPD:
  3370. case ARM::LDMIA_UPD:
  3371. case ARM::LDMDA_UPD:
  3372. case ARM::LDMDB_UPD:
  3373. case ARM::LDMIB_UPD:
  3374. case ARM::STMIA_UPD:
  3375. case ARM::STMDA_UPD:
  3376. case ARM::STMDB_UPD:
  3377. case ARM::STMIB_UPD:
  3378. case ARM::tLDMIA_UPD:
  3379. case ARM::tSTMIA_UPD:
  3380. case ARM::t2LDMIA_UPD:
  3381. case ARM::t2LDMDB_UPD:
  3382. case ARM::t2STMIA_UPD:
  3383. case ARM::t2STMDB_UPD:
  3384. ++UOps; // One for base register writeback.
  3385. break;
  3386. case ARM::LDMIA_RET:
  3387. case ARM::tPOP_RET:
  3388. case ARM::t2LDMIA_RET:
  3389. UOps += 2; // One for base reg wb, one for write to pc.
  3390. break;
  3391. }
  3392. return UOps;
  3393. }
  3394. unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
  3395. const MachineInstr &MI) const {
  3396. if (!ItinData || ItinData->isEmpty())
  3397. return 1;
  3398. const MCInstrDesc &Desc = MI.getDesc();
  3399. unsigned Class = Desc.getSchedClass();
  3400. int ItinUOps = ItinData->getNumMicroOps(Class);
  3401. if (ItinUOps >= 0) {
  3402. if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
  3403. return getNumMicroOpsSwiftLdSt(ItinData, MI);
  3404. return ItinUOps;
  3405. }
  3406. unsigned Opc = MI.getOpcode();
  3407. switch (Opc) {
  3408. default:
  3409. llvm_unreachable("Unexpected multi-uops instruction!");
  3410. case ARM::VLDMQIA:
  3411. case ARM::VSTMQIA:
  3412. return 2;
  3413. // The number of uOps for load / store multiple are determined by the number
  3414. // registers.
  3415. //
  3416. // On Cortex-A8, each pair of register loads / stores can be scheduled on the
  3417. // same cycle. The scheduling for the first load / store must be done
  3418. // separately by assuming the address is not 64-bit aligned.
  3419. //
  3420. // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
  3421. // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
  3422. // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
  3423. case ARM::VLDMDIA:
  3424. case ARM::VLDMDIA_UPD:
  3425. case ARM::VLDMDDB_UPD:
  3426. case ARM::VLDMSIA:
  3427. case ARM::VLDMSIA_UPD:
  3428. case ARM::VLDMSDB_UPD:
  3429. case ARM::VSTMDIA:
  3430. case ARM::VSTMDIA_UPD:
  3431. case ARM::VSTMDDB_UPD:
  3432. case ARM::VSTMSIA:
  3433. case ARM::VSTMSIA_UPD:
  3434. case ARM::VSTMSDB_UPD: {
  3435. unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
  3436. return (NumRegs / 2) + (NumRegs % 2) + 1;
  3437. }
  3438. case ARM::LDMIA_RET:
  3439. case ARM::LDMIA:
  3440. case ARM::LDMDA:
  3441. case ARM::LDMDB:
  3442. case ARM::LDMIB:
  3443. case ARM::LDMIA_UPD:
  3444. case ARM::LDMDA_UPD:
  3445. case ARM::LDMDB_UPD:
  3446. case ARM::LDMIB_UPD:
  3447. case ARM::STMIA:
  3448. case ARM::STMDA:
  3449. case ARM::STMDB:
  3450. case ARM::STMIB:
  3451. case ARM::STMIA_UPD:
  3452. case ARM::STMDA_UPD:
  3453. case ARM::STMDB_UPD:
  3454. case ARM::STMIB_UPD:
  3455. case ARM::tLDMIA:
  3456. case ARM::tLDMIA_UPD:
  3457. case ARM::tSTMIA_UPD:
  3458. case ARM::tPOP_RET:
  3459. case ARM::tPOP:
  3460. case ARM::tPUSH:
  3461. case ARM::t2LDMIA_RET:
  3462. case ARM::t2LDMIA:
  3463. case ARM::t2LDMDB:
  3464. case ARM::t2LDMIA_UPD:
  3465. case ARM::t2LDMDB_UPD:
  3466. case ARM::t2STMIA:
  3467. case ARM::t2STMDB:
  3468. case ARM::t2STMIA_UPD:
  3469. case ARM::t2STMDB_UPD: {
  3470. unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
  3471. switch (Subtarget.getLdStMultipleTiming()) {
  3472. case ARMSubtarget::SingleIssuePlusExtras:
  3473. return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
  3474. case ARMSubtarget::SingleIssue:
  3475. // Assume the worst.
  3476. return NumRegs;
  3477. case ARMSubtarget::DoubleIssue: {
  3478. if (NumRegs < 4)
  3479. return 2;
  3480. // 4 registers would be issued: 2, 2.
  3481. // 5 registers would be issued: 2, 2, 1.
  3482. unsigned UOps = (NumRegs / 2);
  3483. if (NumRegs % 2)
  3484. ++UOps;
  3485. return UOps;
  3486. }
  3487. case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
  3488. unsigned UOps = (NumRegs / 2);
  3489. // If there are odd number of registers or if it's not 64-bit aligned,
  3490. // then it takes an extra AGU (Address Generation Unit) cycle.
  3491. if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
  3492. (*MI.memoperands_begin())->getAlign() < Align(8))
  3493. ++UOps;
  3494. return UOps;
  3495. }
  3496. }
  3497. }
  3498. }
  3499. llvm_unreachable("Didn't find the number of microops");
  3500. }
  3501. int
  3502. ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
  3503. const MCInstrDesc &DefMCID,
  3504. unsigned DefClass,
  3505. unsigned DefIdx, unsigned DefAlign) const {
  3506. int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
  3507. if (RegNo <= 0)
  3508. // Def is the address writeback.
  3509. return ItinData->getOperandCycle(DefClass, DefIdx);
  3510. int DefCycle;
  3511. if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
  3512. // (regno / 2) + (regno % 2) + 1
  3513. DefCycle = RegNo / 2 + 1;
  3514. if (RegNo % 2)
  3515. ++DefCycle;
  3516. } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
  3517. DefCycle = RegNo;
  3518. bool isSLoad = false;
  3519. switch (DefMCID.getOpcode()) {
  3520. default: break;
  3521. case ARM::VLDMSIA:
  3522. case ARM::VLDMSIA_UPD:
  3523. case ARM::VLDMSDB_UPD:
  3524. isSLoad = true;
  3525. break;
  3526. }
  3527. // If there are odd number of 'S' registers or if it's not 64-bit aligned,
  3528. // then it takes an extra cycle.
  3529. if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
  3530. ++DefCycle;
  3531. } else {
  3532. // Assume the worst.
  3533. DefCycle = RegNo + 2;
  3534. }
  3535. return DefCycle;
  3536. }
  3537. int
  3538. ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
  3539. const MCInstrDesc &DefMCID,
  3540. unsigned DefClass,
  3541. unsigned DefIdx, unsigned DefAlign) const {
  3542. int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
  3543. if (RegNo <= 0)
  3544. // Def is the address writeback.
  3545. return ItinData->getOperandCycle(DefClass, DefIdx);
  3546. int DefCycle;
  3547. if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
  3548. // 4 registers would be issued: 1, 2, 1.
  3549. // 5 registers would be issued: 1, 2, 2.
  3550. DefCycle = RegNo / 2;
  3551. if (DefCycle < 1)
  3552. DefCycle = 1;
  3553. // Result latency is issue cycle + 2: E2.
  3554. DefCycle += 2;
  3555. } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
  3556. DefCycle = (RegNo / 2);
  3557. // If there are odd number of registers or if it's not 64-bit aligned,
  3558. // then it takes an extra AGU (Address Generation Unit) cycle.
  3559. if ((RegNo % 2) || DefAlign < 8)
  3560. ++DefCycle;
  3561. // Result latency is AGU cycles + 2.
  3562. DefCycle += 2;
  3563. } else {
  3564. // Assume the worst.
  3565. DefCycle = RegNo + 2;
  3566. }
  3567. return DefCycle;
  3568. }
  3569. int
  3570. ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
  3571. const MCInstrDesc &UseMCID,
  3572. unsigned UseClass,
  3573. unsigned UseIdx, unsigned UseAlign) const {
  3574. int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
  3575. if (RegNo <= 0)
  3576. return ItinData->getOperandCycle(UseClass, UseIdx);
  3577. int UseCycle;
  3578. if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
  3579. // (regno / 2) + (regno % 2) + 1
  3580. UseCycle = RegNo / 2 + 1;
  3581. if (RegNo % 2)
  3582. ++UseCycle;
  3583. } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
  3584. UseCycle = RegNo;
  3585. bool isSStore = false;
  3586. switch (UseMCID.getOpcode()) {
  3587. default: break;
  3588. case ARM::VSTMSIA:
  3589. case ARM::VSTMSIA_UPD:
  3590. case ARM::VSTMSDB_UPD:
  3591. isSStore = true;
  3592. break;
  3593. }
  3594. // If there are odd number of 'S' registers or if it's not 64-bit aligned,
  3595. // then it takes an extra cycle.
  3596. if ((isSStore && (RegNo % 2)) || UseAlign < 8)
  3597. ++UseCycle;
  3598. } else {
  3599. // Assume the worst.
  3600. UseCycle = RegNo + 2;
  3601. }
  3602. return UseCycle;
  3603. }
  3604. int
  3605. ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
  3606. const MCInstrDesc &UseMCID,
  3607. unsigned UseClass,
  3608. unsigned UseIdx, unsigned UseAlign) const {
  3609. int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
  3610. if (RegNo <= 0)
  3611. return ItinData->getOperandCycle(UseClass, UseIdx);
  3612. int UseCycle;
  3613. if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
  3614. UseCycle = RegNo / 2;
  3615. if (UseCycle < 2)
  3616. UseCycle = 2;
  3617. // Read in E3.
  3618. UseCycle += 2;
  3619. } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
  3620. UseCycle = (RegNo / 2);
  3621. // If there are odd number of registers or if it's not 64-bit aligned,
  3622. // then it takes an extra AGU (Address Generation Unit) cycle.
  3623. if ((RegNo % 2) || UseAlign < 8)
  3624. ++UseCycle;
  3625. } else {
  3626. // Assume the worst.
  3627. UseCycle = 1;
  3628. }
  3629. return UseCycle;
  3630. }
  3631. int
  3632. ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
  3633. const MCInstrDesc &DefMCID,
  3634. unsigned DefIdx, unsigned DefAlign,
  3635. const MCInstrDesc &UseMCID,
  3636. unsigned UseIdx, unsigned UseAlign) const {
  3637. unsigned DefClass = DefMCID.getSchedClass();
  3638. unsigned UseClass = UseMCID.getSchedClass();
  3639. if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
  3640. return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
  3641. // This may be a def / use of a variable_ops instruction, the operand
  3642. // latency might be determinable dynamically. Let the target try to
  3643. // figure it out.
  3644. int DefCycle = -1;
  3645. bool LdmBypass = false;
  3646. switch (DefMCID.getOpcode()) {
  3647. default:
  3648. DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
  3649. break;
  3650. case ARM::VLDMDIA:
  3651. case ARM::VLDMDIA_UPD:
  3652. case ARM::VLDMDDB_UPD:
  3653. case ARM::VLDMSIA:
  3654. case ARM::VLDMSIA_UPD:
  3655. case ARM::VLDMSDB_UPD:
  3656. DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
  3657. break;
  3658. case ARM::LDMIA_RET:
  3659. case ARM::LDMIA:
  3660. case ARM::LDMDA:
  3661. case ARM::LDMDB:
  3662. case ARM::LDMIB:
  3663. case ARM::LDMIA_UPD:
  3664. case ARM::LDMDA_UPD:
  3665. case ARM::LDMDB_UPD:
  3666. case ARM::LDMIB_UPD:
  3667. case ARM::tLDMIA:
  3668. case ARM::tLDMIA_UPD:
  3669. case ARM::tPUSH:
  3670. case ARM::t2LDMIA_RET:
  3671. case ARM::t2LDMIA:
  3672. case ARM::t2LDMDB:
  3673. case ARM::t2LDMIA_UPD:
  3674. case ARM::t2LDMDB_UPD:
  3675. LdmBypass = true;
  3676. DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
  3677. break;
  3678. }
  3679. if (DefCycle == -1)
  3680. // We can't seem to determine the result latency of the def, assume it's 2.
  3681. DefCycle = 2;
  3682. int UseCycle = -1;
  3683. switch (UseMCID.getOpcode()) {
  3684. default:
  3685. UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
  3686. break;
  3687. case ARM::VSTMDIA:
  3688. case ARM::VSTMDIA_UPD:
  3689. case ARM::VSTMDDB_UPD:
  3690. case ARM::VSTMSIA:
  3691. case ARM::VSTMSIA_UPD:
  3692. case ARM::VSTMSDB_UPD:
  3693. UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
  3694. break;
  3695. case ARM::STMIA:
  3696. case ARM::STMDA:
  3697. case ARM::STMDB:
  3698. case ARM::STMIB:
  3699. case ARM::STMIA_UPD:
  3700. case ARM::STMDA_UPD:
  3701. case ARM::STMDB_UPD:
  3702. case ARM::STMIB_UPD:
  3703. case ARM::tSTMIA_UPD:
  3704. case ARM::tPOP_RET:
  3705. case ARM::tPOP:
  3706. case ARM::t2STMIA:
  3707. case ARM::t2STMDB:
  3708. case ARM::t2STMIA_UPD:
  3709. case ARM::t2STMDB_UPD:
  3710. UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
  3711. break;
  3712. }
  3713. if (UseCycle == -1)
  3714. // Assume it's read in the first stage.
  3715. UseCycle = 1;
  3716. UseCycle = DefCycle - UseCycle + 1;
  3717. if (UseCycle > 0) {
  3718. if (LdmBypass) {
  3719. // It's a variable_ops instruction so we can't use DefIdx here. Just use
  3720. // first def operand.
  3721. if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
  3722. UseClass, UseIdx))
  3723. --UseCycle;
  3724. } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
  3725. UseClass, UseIdx)) {
  3726. --UseCycle;
  3727. }
  3728. }
  3729. return UseCycle;
  3730. }
  3731. static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
  3732. const MachineInstr *MI, unsigned Reg,
  3733. unsigned &DefIdx, unsigned &Dist) {
  3734. Dist = 0;
  3735. MachineBasicBlock::const_iterator I = MI; ++I;
  3736. MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
  3737. assert(II->isInsideBundle() && "Empty bundle?");
  3738. int Idx = -1;
  3739. while (II->isInsideBundle()) {
  3740. Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
  3741. if (Idx != -1)
  3742. break;
  3743. --II;
  3744. ++Dist;
  3745. }
  3746. assert(Idx != -1 && "Cannot find bundled definition!");
  3747. DefIdx = Idx;
  3748. return &*II;
  3749. }
  3750. static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
  3751. const MachineInstr &MI, unsigned Reg,
  3752. unsigned &UseIdx, unsigned &Dist) {
  3753. Dist = 0;
  3754. MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
  3755. assert(II->isInsideBundle() && "Empty bundle?");
  3756. MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
  3757. // FIXME: This doesn't properly handle multiple uses.
  3758. int Idx = -1;
  3759. while (II != E && II->isInsideBundle()) {
  3760. Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
  3761. if (Idx != -1)
  3762. break;
  3763. if (II->getOpcode() != ARM::t2IT)
  3764. ++Dist;
  3765. ++II;
  3766. }
  3767. if (Idx == -1) {
  3768. Dist = 0;
  3769. return nullptr;
  3770. }
  3771. UseIdx = Idx;
  3772. return &*II;
  3773. }
  3774. /// Return the number of cycles to add to (or subtract from) the static
  3775. /// itinerary based on the def opcode and alignment. The caller will ensure that
  3776. /// adjusted latency is at least one cycle.
  3777. static int adjustDefLatency(const ARMSubtarget &Subtarget,
  3778. const MachineInstr &DefMI,
  3779. const MCInstrDesc &DefMCID, unsigned DefAlign) {
  3780. int Adjust = 0;
  3781. if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
  3782. // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
  3783. // variants are one cycle cheaper.
  3784. switch (DefMCID.getOpcode()) {
  3785. default: break;
  3786. case ARM::LDRrs:
  3787. case ARM::LDRBrs: {
  3788. unsigned ShOpVal = DefMI.getOperand(3).getImm();
  3789. unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
  3790. if (ShImm == 0 ||
  3791. (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
  3792. --Adjust;
  3793. break;
  3794. }
  3795. case ARM::t2LDRs:
  3796. case ARM::t2LDRBs:
  3797. case ARM::t2LDRHs:
  3798. case ARM::t2LDRSHs: {
  3799. // Thumb2 mode: lsl only.
  3800. unsigned ShAmt = DefMI.getOperand(3).getImm();
  3801. if (ShAmt == 0 || ShAmt == 2)
  3802. --Adjust;
  3803. break;
  3804. }
  3805. }
  3806. } else if (Subtarget.isSwift()) {
  3807. // FIXME: Properly handle all of the latency adjustments for address
  3808. // writeback.
  3809. switch (DefMCID.getOpcode()) {
  3810. default: break;
  3811. case ARM::LDRrs:
  3812. case ARM::LDRBrs: {
  3813. unsigned ShOpVal = DefMI.getOperand(3).getImm();
  3814. bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
  3815. unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
  3816. if (!isSub &&
  3817. (ShImm == 0 ||
  3818. ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
  3819. ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
  3820. Adjust -= 2;
  3821. else if (!isSub &&
  3822. ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
  3823. --Adjust;
  3824. break;
  3825. }
  3826. case ARM::t2LDRs:
  3827. case ARM::t2LDRBs:
  3828. case ARM::t2LDRHs:
  3829. case ARM::t2LDRSHs: {
  3830. // Thumb2 mode: lsl only.
  3831. unsigned ShAmt = DefMI.getOperand(3).getImm();
  3832. if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
  3833. Adjust -= 2;
  3834. break;
  3835. }
  3836. }
  3837. }
  3838. if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
  3839. switch (DefMCID.getOpcode()) {
  3840. default: break;
  3841. case ARM::VLD1q8:
  3842. case ARM::VLD1q16:
  3843. case ARM::VLD1q32:
  3844. case ARM::VLD1q64:
  3845. case ARM::VLD1q8wb_fixed:
  3846. case ARM::VLD1q16wb_fixed:
  3847. case ARM::VLD1q32wb_fixed:
  3848. case ARM::VLD1q64wb_fixed:
  3849. case ARM::VLD1q8wb_register:
  3850. case ARM::VLD1q16wb_register:
  3851. case ARM::VLD1q32wb_register:
  3852. case ARM::VLD1q64wb_register:
  3853. case ARM::VLD2d8:
  3854. case ARM::VLD2d16:
  3855. case ARM::VLD2d32:
  3856. case ARM::VLD2q8:
  3857. case ARM::VLD2q16:
  3858. case ARM::VLD2q32:
  3859. case ARM::VLD2d8wb_fixed:
  3860. case ARM::VLD2d16wb_fixed:
  3861. case ARM::VLD2d32wb_fixed:
  3862. case ARM::VLD2q8wb_fixed:
  3863. case ARM::VLD2q16wb_fixed:
  3864. case ARM::VLD2q32wb_fixed:
  3865. case ARM::VLD2d8wb_register:
  3866. case ARM::VLD2d16wb_register:
  3867. case ARM::VLD2d32wb_register:
  3868. case ARM::VLD2q8wb_register:
  3869. case ARM::VLD2q16wb_register:
  3870. case ARM::VLD2q32wb_register:
  3871. case ARM::VLD3d8:
  3872. case ARM::VLD3d16:
  3873. case ARM::VLD3d32:
  3874. case ARM::VLD1d64T:
  3875. case ARM::VLD3d8_UPD:
  3876. case ARM::VLD3d16_UPD:
  3877. case ARM::VLD3d32_UPD:
  3878. case ARM::VLD1d64Twb_fixed:
  3879. case ARM::VLD1d64Twb_register:
  3880. case ARM::VLD3q8_UPD:
  3881. case ARM::VLD3q16_UPD:
  3882. case ARM::VLD3q32_UPD:
  3883. case ARM::VLD4d8:
  3884. case ARM::VLD4d16:
  3885. case ARM::VLD4d32:
  3886. case ARM::VLD1d64Q:
  3887. case ARM::VLD4d8_UPD:
  3888. case ARM::VLD4d16_UPD:
  3889. case ARM::VLD4d32_UPD:
  3890. case ARM::VLD1d64Qwb_fixed:
  3891. case ARM::VLD1d64Qwb_register:
  3892. case ARM::VLD4q8_UPD:
  3893. case ARM::VLD4q16_UPD:
  3894. case ARM::VLD4q32_UPD:
  3895. case ARM::VLD1DUPq8:
  3896. case ARM::VLD1DUPq16:
  3897. case ARM::VLD1DUPq32:
  3898. case ARM::VLD1DUPq8wb_fixed:
  3899. case ARM::VLD1DUPq16wb_fixed:
  3900. case ARM::VLD1DUPq32wb_fixed:
  3901. case ARM::VLD1DUPq8wb_register:
  3902. case ARM::VLD1DUPq16wb_register:
  3903. case ARM::VLD1DUPq32wb_register:
  3904. case ARM::VLD2DUPd8:
  3905. case ARM::VLD2DUPd16:
  3906. case ARM::VLD2DUPd32:
  3907. case ARM::VLD2DUPd8wb_fixed:
  3908. case ARM::VLD2DUPd16wb_fixed:
  3909. case ARM::VLD2DUPd32wb_fixed:
  3910. case ARM::VLD2DUPd8wb_register:
  3911. case ARM::VLD2DUPd16wb_register:
  3912. case ARM::VLD2DUPd32wb_register:
  3913. case ARM::VLD4DUPd8:
  3914. case ARM::VLD4DUPd16:
  3915. case ARM::VLD4DUPd32:
  3916. case ARM::VLD4DUPd8_UPD:
  3917. case ARM::VLD4DUPd16_UPD:
  3918. case ARM::VLD4DUPd32_UPD:
  3919. case ARM::VLD1LNd8:
  3920. case ARM::VLD1LNd16:
  3921. case ARM::VLD1LNd32:
  3922. case ARM::VLD1LNd8_UPD:
  3923. case ARM::VLD1LNd16_UPD:
  3924. case ARM::VLD1LNd32_UPD:
  3925. case ARM::VLD2LNd8:
  3926. case ARM::VLD2LNd16:
  3927. case ARM::VLD2LNd32:
  3928. case ARM::VLD2LNq16:
  3929. case ARM::VLD2LNq32:
  3930. case ARM::VLD2LNd8_UPD:
  3931. case ARM::VLD2LNd16_UPD:
  3932. case ARM::VLD2LNd32_UPD:
  3933. case ARM::VLD2LNq16_UPD:
  3934. case ARM::VLD2LNq32_UPD:
  3935. case ARM::VLD4LNd8:
  3936. case ARM::VLD4LNd16:
  3937. case ARM::VLD4LNd32:
  3938. case ARM::VLD4LNq16:
  3939. case ARM::VLD4LNq32:
  3940. case ARM::VLD4LNd8_UPD:
  3941. case ARM::VLD4LNd16_UPD:
  3942. case ARM::VLD4LNd32_UPD:
  3943. case ARM::VLD4LNq16_UPD:
  3944. case ARM::VLD4LNq32_UPD:
  3945. // If the address is not 64-bit aligned, the latencies of these
  3946. // instructions increases by one.
  3947. ++Adjust;
  3948. break;
  3949. }
  3950. }
  3951. return Adjust;
  3952. }
  3953. int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
  3954. const MachineInstr &DefMI,
  3955. unsigned DefIdx,
  3956. const MachineInstr &UseMI,
  3957. unsigned UseIdx) const {
  3958. // No operand latency. The caller may fall back to getInstrLatency.
  3959. if (!ItinData || ItinData->isEmpty())
  3960. return -1;
  3961. const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
  3962. Register Reg = DefMO.getReg();
  3963. const MachineInstr *ResolvedDefMI = &DefMI;
  3964. unsigned DefAdj = 0;
  3965. if (DefMI.isBundle())
  3966. ResolvedDefMI =
  3967. getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
  3968. if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
  3969. ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
  3970. return 1;
  3971. }
  3972. const MachineInstr *ResolvedUseMI = &UseMI;
  3973. unsigned UseAdj = 0;
  3974. if (UseMI.isBundle()) {
  3975. ResolvedUseMI =
  3976. getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
  3977. if (!ResolvedUseMI)
  3978. return -1;
  3979. }
  3980. return getOperandLatencyImpl(
  3981. ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
  3982. Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
  3983. }
  3984. int ARMBaseInstrInfo::getOperandLatencyImpl(
  3985. const InstrItineraryData *ItinData, const MachineInstr &DefMI,
  3986. unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
  3987. const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
  3988. unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
  3989. if (Reg == ARM::CPSR) {
  3990. if (DefMI.getOpcode() == ARM::FMSTAT) {
  3991. // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
  3992. return Subtarget.isLikeA9() ? 1 : 20;
  3993. }
  3994. // CPSR set and branch can be paired in the same cycle.
  3995. if (UseMI.isBranch())
  3996. return 0;
  3997. // Otherwise it takes the instruction latency (generally one).
  3998. unsigned Latency = getInstrLatency(ItinData, DefMI);
  3999. // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
  4000. // its uses. Instructions which are otherwise scheduled between them may
  4001. // incur a code size penalty (not able to use the CPSR setting 16-bit
  4002. // instructions).
  4003. if (Latency > 0 && Subtarget.isThumb2()) {
  4004. const MachineFunction *MF = DefMI.getParent()->getParent();
  4005. // FIXME: Use Function::hasOptSize().
  4006. if (MF->getFunction().hasFnAttribute(Attribute::OptimizeForSize))
  4007. --Latency;
  4008. }
  4009. return Latency;
  4010. }
  4011. if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
  4012. return -1;
  4013. unsigned DefAlign = DefMI.hasOneMemOperand()
  4014. ? (*DefMI.memoperands_begin())->getAlign().value()
  4015. : 0;
  4016. unsigned UseAlign = UseMI.hasOneMemOperand()
  4017. ? (*UseMI.memoperands_begin())->getAlign().value()
  4018. : 0;
  4019. // Get the itinerary's latency if possible, and handle variable_ops.
  4020. int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
  4021. UseIdx, UseAlign);
  4022. // Unable to find operand latency. The caller may resort to getInstrLatency.
  4023. if (Latency < 0)
  4024. return Latency;
  4025. // Adjust for IT block position.
  4026. int Adj = DefAdj + UseAdj;
  4027. // Adjust for dynamic def-side opcode variants not captured by the itinerary.
  4028. Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
  4029. if (Adj >= 0 || (int)Latency > -Adj) {
  4030. return Latency + Adj;
  4031. }
  4032. // Return the itinerary latency, which may be zero but not less than zero.
  4033. return Latency;
  4034. }
  4035. int
  4036. ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
  4037. SDNode *DefNode, unsigned DefIdx,
  4038. SDNode *UseNode, unsigned UseIdx) const {
  4039. if (!DefNode->isMachineOpcode())
  4040. return 1;
  4041. const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
  4042. if (isZeroCost(DefMCID.Opcode))
  4043. return 0;
  4044. if (!ItinData || ItinData->isEmpty())
  4045. return DefMCID.mayLoad() ? 3 : 1;
  4046. if (!UseNode->isMachineOpcode()) {
  4047. int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
  4048. int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
  4049. int Threshold = 1 + Adj;
  4050. return Latency <= Threshold ? 1 : Latency - Adj;
  4051. }
  4052. const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
  4053. auto *DefMN = cast<MachineSDNode>(DefNode);
  4054. unsigned DefAlign = !DefMN->memoperands_empty()
  4055. ? (*DefMN->memoperands_begin())->getAlign().value()
  4056. : 0;
  4057. auto *UseMN = cast<MachineSDNode>(UseNode);
  4058. unsigned UseAlign = !UseMN->memoperands_empty()
  4059. ? (*UseMN->memoperands_begin())->getAlign().value()
  4060. : 0;
  4061. int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
  4062. UseMCID, UseIdx, UseAlign);
  4063. if (Latency > 1 &&
  4064. (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
  4065. Subtarget.isCortexA7())) {
  4066. // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
  4067. // variants are one cycle cheaper.
  4068. switch (DefMCID.getOpcode()) {
  4069. default: break;
  4070. case ARM::LDRrs:
  4071. case ARM::LDRBrs: {
  4072. unsigned ShOpVal =
  4073. cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
  4074. unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
  4075. if (ShImm == 0 ||
  4076. (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
  4077. --Latency;
  4078. break;
  4079. }
  4080. case ARM::t2LDRs:
  4081. case ARM::t2LDRBs:
  4082. case ARM::t2LDRHs:
  4083. case ARM::t2LDRSHs: {
  4084. // Thumb2 mode: lsl only.
  4085. unsigned ShAmt =
  4086. cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
  4087. if (ShAmt == 0 || ShAmt == 2)
  4088. --Latency;
  4089. break;
  4090. }
  4091. }
  4092. } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
  4093. // FIXME: Properly handle all of the latency adjustments for address
  4094. // writeback.
  4095. switch (DefMCID.getOpcode()) {
  4096. default: break;
  4097. case ARM::LDRrs:
  4098. case ARM::LDRBrs: {
  4099. unsigned ShOpVal =
  4100. cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
  4101. unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
  4102. if (ShImm == 0 ||
  4103. ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
  4104. ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
  4105. Latency -= 2;
  4106. else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
  4107. --Latency;
  4108. break;
  4109. }
  4110. case ARM::t2LDRs:
  4111. case ARM::t2LDRBs:
  4112. case ARM::t2LDRHs:
  4113. case ARM::t2LDRSHs:
  4114. // Thumb2 mode: lsl 0-3 only.
  4115. Latency -= 2;
  4116. break;
  4117. }
  4118. }
  4119. if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
  4120. switch (DefMCID.getOpcode()) {
  4121. default: break;
  4122. case ARM::VLD1q8:
  4123. case ARM::VLD1q16:
  4124. case ARM::VLD1q32:
  4125. case ARM::VLD1q64:
  4126. case ARM::VLD1q8wb_register:
  4127. case ARM::VLD1q16wb_register:
  4128. case ARM::VLD1q32wb_register:
  4129. case ARM::VLD1q64wb_register:
  4130. case ARM::VLD1q8wb_fixed:
  4131. case ARM::VLD1q16wb_fixed:
  4132. case ARM::VLD1q32wb_fixed:
  4133. case ARM::VLD1q64wb_fixed:
  4134. case ARM::VLD2d8:
  4135. case ARM::VLD2d16:
  4136. case ARM::VLD2d32:
  4137. case ARM::VLD2q8Pseudo:
  4138. case ARM::VLD2q16Pseudo:
  4139. case ARM::VLD2q32Pseudo:
  4140. case ARM::VLD2d8wb_fixed:
  4141. case ARM::VLD2d16wb_fixed:
  4142. case ARM::VLD2d32wb_fixed:
  4143. case ARM::VLD2q8PseudoWB_fixed:
  4144. case ARM::VLD2q16PseudoWB_fixed:
  4145. case ARM::VLD2q32PseudoWB_fixed:
  4146. case ARM::VLD2d8wb_register:
  4147. case ARM::VLD2d16wb_register:
  4148. case ARM::VLD2d32wb_register:
  4149. case ARM::VLD2q8PseudoWB_register:
  4150. case ARM::VLD2q16PseudoWB_register:
  4151. case ARM::VLD2q32PseudoWB_register:
  4152. case ARM::VLD3d8Pseudo:
  4153. case ARM::VLD3d16Pseudo:
  4154. case ARM::VLD3d32Pseudo:
  4155. case ARM::VLD1d8TPseudo:
  4156. case ARM::VLD1d16TPseudo:
  4157. case ARM::VLD1d32TPseudo:
  4158. case ARM::VLD1d64TPseudo:
  4159. case ARM::VLD1d64TPseudoWB_fixed:
  4160. case ARM::VLD1d64TPseudoWB_register:
  4161. case ARM::VLD3d8Pseudo_UPD:
  4162. case ARM::VLD3d16Pseudo_UPD:
  4163. case ARM::VLD3d32Pseudo_UPD:
  4164. case ARM::VLD3q8Pseudo_UPD:
  4165. case ARM::VLD3q16Pseudo_UPD:
  4166. case ARM::VLD3q32Pseudo_UPD:
  4167. case ARM::VLD3q8oddPseudo:
  4168. case ARM::VLD3q16oddPseudo:
  4169. case ARM::VLD3q32oddPseudo:
  4170. case ARM::VLD3q8oddPseudo_UPD:
  4171. case ARM::VLD3q16oddPseudo_UPD:
  4172. case ARM::VLD3q32oddPseudo_UPD:
  4173. case ARM::VLD4d8Pseudo:
  4174. case ARM::VLD4d16Pseudo:
  4175. case ARM::VLD4d32Pseudo:
  4176. case ARM::VLD1d8QPseudo:
  4177. case ARM::VLD1d16QPseudo:
  4178. case ARM::VLD1d32QPseudo:
  4179. case ARM::VLD1d64QPseudo:
  4180. case ARM::VLD1d64QPseudoWB_fixed:
  4181. case ARM::VLD1d64QPseudoWB_register:
  4182. case ARM::VLD1q8HighQPseudo:
  4183. case ARM::VLD1q8LowQPseudo_UPD:
  4184. case ARM::VLD1q8HighTPseudo:
  4185. case ARM::VLD1q8LowTPseudo_UPD:
  4186. case ARM::VLD1q16HighQPseudo:
  4187. case ARM::VLD1q16LowQPseudo_UPD:
  4188. case ARM::VLD1q16HighTPseudo:
  4189. case ARM::VLD1q16LowTPseudo_UPD:
  4190. case ARM::VLD1q32HighQPseudo:
  4191. case ARM::VLD1q32LowQPseudo_UPD:
  4192. case ARM::VLD1q32HighTPseudo:
  4193. case ARM::VLD1q32LowTPseudo_UPD:
  4194. case ARM::VLD1q64HighQPseudo:
  4195. case ARM::VLD1q64LowQPseudo_UPD:
  4196. case ARM::VLD1q64HighTPseudo:
  4197. case ARM::VLD1q64LowTPseudo_UPD:
  4198. case ARM::VLD4d8Pseudo_UPD:
  4199. case ARM::VLD4d16Pseudo_UPD:
  4200. case ARM::VLD4d32Pseudo_UPD:
  4201. case ARM::VLD4q8Pseudo_UPD:
  4202. case ARM::VLD4q16Pseudo_UPD:
  4203. case ARM::VLD4q32Pseudo_UPD:
  4204. case ARM::VLD4q8oddPseudo:
  4205. case ARM::VLD4q16oddPseudo:
  4206. case ARM::VLD4q32oddPseudo:
  4207. case ARM::VLD4q8oddPseudo_UPD:
  4208. case ARM::VLD4q16oddPseudo_UPD:
  4209. case ARM::VLD4q32oddPseudo_UPD:
  4210. case ARM::VLD1DUPq8:
  4211. case ARM::VLD1DUPq16:
  4212. case ARM::VLD1DUPq32:
  4213. case ARM::VLD1DUPq8wb_fixed:
  4214. case ARM::VLD1DUPq16wb_fixed:
  4215. case ARM::VLD1DUPq32wb_fixed:
  4216. case ARM::VLD1DUPq8wb_register:
  4217. case ARM::VLD1DUPq16wb_register:
  4218. case ARM::VLD1DUPq32wb_register:
  4219. case ARM::VLD2DUPd8:
  4220. case ARM::VLD2DUPd16:
  4221. case ARM::VLD2DUPd32:
  4222. case ARM::VLD2DUPd8wb_fixed:
  4223. case ARM::VLD2DUPd16wb_fixed:
  4224. case ARM::VLD2DUPd32wb_fixed:
  4225. case ARM::VLD2DUPd8wb_register:
  4226. case ARM::VLD2DUPd16wb_register:
  4227. case ARM::VLD2DUPd32wb_register:
  4228. case ARM::VLD2DUPq8EvenPseudo:
  4229. case ARM::VLD2DUPq8OddPseudo:
  4230. case ARM::VLD2DUPq16EvenPseudo:
  4231. case ARM::VLD2DUPq16OddPseudo:
  4232. case ARM::VLD2DUPq32EvenPseudo:
  4233. case ARM::VLD2DUPq32OddPseudo:
  4234. case ARM::VLD3DUPq8EvenPseudo:
  4235. case ARM::VLD3DUPq8OddPseudo:
  4236. case ARM::VLD3DUPq16EvenPseudo:
  4237. case ARM::VLD3DUPq16OddPseudo:
  4238. case ARM::VLD3DUPq32EvenPseudo:
  4239. case ARM::VLD3DUPq32OddPseudo:
  4240. case ARM::VLD4DUPd8Pseudo:
  4241. case ARM::VLD4DUPd16Pseudo:
  4242. case ARM::VLD4DUPd32Pseudo:
  4243. case ARM::VLD4DUPd8Pseudo_UPD:
  4244. case ARM::VLD4DUPd16Pseudo_UPD:
  4245. case ARM::VLD4DUPd32Pseudo_UPD:
  4246. case ARM::VLD4DUPq8EvenPseudo:
  4247. case ARM::VLD4DUPq8OddPseudo:
  4248. case ARM::VLD4DUPq16EvenPseudo:
  4249. case ARM::VLD4DUPq16OddPseudo:
  4250. case ARM::VLD4DUPq32EvenPseudo:
  4251. case ARM::VLD4DUPq32OddPseudo:
  4252. case ARM::VLD1LNq8Pseudo:
  4253. case ARM::VLD1LNq16Pseudo:
  4254. case ARM::VLD1LNq32Pseudo:
  4255. case ARM::VLD1LNq8Pseudo_UPD:
  4256. case ARM::VLD1LNq16Pseudo_UPD:
  4257. case ARM::VLD1LNq32Pseudo_UPD:
  4258. case ARM::VLD2LNd8Pseudo:
  4259. case ARM::VLD2LNd16Pseudo:
  4260. case ARM::VLD2LNd32Pseudo:
  4261. case ARM::VLD2LNq16Pseudo:
  4262. case ARM::VLD2LNq32Pseudo:
  4263. case ARM::VLD2LNd8Pseudo_UPD:
  4264. case ARM::VLD2LNd16Pseudo_UPD:
  4265. case ARM::VLD2LNd32Pseudo_UPD:
  4266. case ARM::VLD2LNq16Pseudo_UPD:
  4267. case ARM::VLD2LNq32Pseudo_UPD:
  4268. case ARM::VLD4LNd8Pseudo:
  4269. case ARM::VLD4LNd16Pseudo:
  4270. case ARM::VLD4LNd32Pseudo:
  4271. case ARM::VLD4LNq16Pseudo:
  4272. case ARM::VLD4LNq32Pseudo:
  4273. case ARM::VLD4LNd8Pseudo_UPD:
  4274. case ARM::VLD4LNd16Pseudo_UPD:
  4275. case ARM::VLD4LNd32Pseudo_UPD:
  4276. case ARM::VLD4LNq16Pseudo_UPD:
  4277. case ARM::VLD4LNq32Pseudo_UPD:
  4278. // If the address is not 64-bit aligned, the latencies of these
  4279. // instructions increases by one.
  4280. ++Latency;
  4281. break;
  4282. }
  4283. return Latency;
  4284. }
  4285. unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
  4286. if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
  4287. MI.isImplicitDef())
  4288. return 0;
  4289. if (MI.isBundle())
  4290. return 0;
  4291. const MCInstrDesc &MCID = MI.getDesc();
  4292. if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
  4293. !Subtarget.cheapPredicableCPSRDef())) {
  4294. // When predicated, CPSR is an additional source operand for CPSR updating
  4295. // instructions, this apparently increases their latencies.
  4296. return 1;
  4297. }
  4298. return 0;
  4299. }
  4300. unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
  4301. const MachineInstr &MI,
  4302. unsigned *PredCost) const {
  4303. if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
  4304. MI.isImplicitDef())
  4305. return 1;
  4306. // An instruction scheduler typically runs on unbundled instructions, however
  4307. // other passes may query the latency of a bundled instruction.
  4308. if (MI.isBundle()) {
  4309. unsigned Latency = 0;
  4310. MachineBasicBlock::const_instr_iterator I = MI.getIterator();
  4311. MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
  4312. while (++I != E && I->isInsideBundle()) {
  4313. if (I->getOpcode() != ARM::t2IT)
  4314. Latency += getInstrLatency(ItinData, *I, PredCost);
  4315. }
  4316. return Latency;
  4317. }
  4318. const MCInstrDesc &MCID = MI.getDesc();
  4319. if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
  4320. !Subtarget.cheapPredicableCPSRDef()))) {
  4321. // When predicated, CPSR is an additional source operand for CPSR updating
  4322. // instructions, this apparently increases their latencies.
  4323. *PredCost = 1;
  4324. }
  4325. // Be sure to call getStageLatency for an empty itinerary in case it has a
  4326. // valid MinLatency property.
  4327. if (!ItinData)
  4328. return MI.mayLoad() ? 3 : 1;
  4329. unsigned Class = MCID.getSchedClass();
  4330. // For instructions with variable uops, use uops as latency.
  4331. if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
  4332. return getNumMicroOps(ItinData, MI);
  4333. // For the common case, fall back on the itinerary's latency.
  4334. unsigned Latency = ItinData->getStageLatency(Class);
  4335. // Adjust for dynamic def-side opcode variants not captured by the itinerary.
  4336. unsigned DefAlign =
  4337. MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlign().value() : 0;
  4338. int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
  4339. if (Adj >= 0 || (int)Latency > -Adj) {
  4340. return Latency + Adj;
  4341. }
  4342. return Latency;
  4343. }
  4344. int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
  4345. SDNode *Node) const {
  4346. if (!Node->isMachineOpcode())
  4347. return 1;
  4348. if (!ItinData || ItinData->isEmpty())
  4349. return 1;
  4350. unsigned Opcode = Node->getMachineOpcode();
  4351. switch (Opcode) {
  4352. default:
  4353. return ItinData->getStageLatency(get(Opcode).getSchedClass());
  4354. case ARM::VLDMQIA:
  4355. case ARM::VSTMQIA:
  4356. return 2;
  4357. }
  4358. }
  4359. bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
  4360. const MachineRegisterInfo *MRI,
  4361. const MachineInstr &DefMI,
  4362. unsigned DefIdx,
  4363. const MachineInstr &UseMI,
  4364. unsigned UseIdx) const {
  4365. unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
  4366. unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
  4367. if (Subtarget.nonpipelinedVFP() &&
  4368. (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
  4369. return true;
  4370. // Hoist VFP / NEON instructions with 4 or higher latency.
  4371. unsigned Latency =
  4372. SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
  4373. if (Latency <= 3)
  4374. return false;
  4375. return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
  4376. UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
  4377. }
  4378. bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
  4379. const MachineInstr &DefMI,
  4380. unsigned DefIdx) const {
  4381. const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
  4382. if (!ItinData || ItinData->isEmpty())
  4383. return false;
  4384. unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
  4385. if (DDomain == ARMII::DomainGeneral) {
  4386. unsigned DefClass = DefMI.getDesc().getSchedClass();
  4387. int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
  4388. return (DefCycle != -1 && DefCycle <= 2);
  4389. }
  4390. return false;
  4391. }
  4392. bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
  4393. StringRef &ErrInfo) const {
  4394. if (convertAddSubFlagsOpcode(MI.getOpcode())) {
  4395. ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
  4396. return false;
  4397. }
  4398. if (MI.getOpcode() == ARM::tMOVr && !Subtarget.hasV6Ops()) {
  4399. // Make sure we don't generate a lo-lo mov that isn't supported.
  4400. if (!ARM::hGPRRegClass.contains(MI.getOperand(0).getReg()) &&
  4401. !ARM::hGPRRegClass.contains(MI.getOperand(1).getReg())) {
  4402. ErrInfo = "Non-flag-setting Thumb1 mov is v6-only";
  4403. return false;
  4404. }
  4405. }
  4406. if (MI.getOpcode() == ARM::tPUSH ||
  4407. MI.getOpcode() == ARM::tPOP ||
  4408. MI.getOpcode() == ARM::tPOP_RET) {
  4409. for (const MachineOperand &MO : llvm::drop_begin(MI.operands(), 2)) {
  4410. if (MO.isImplicit() || !MO.isReg())
  4411. continue;
  4412. Register Reg = MO.getReg();
  4413. if (Reg < ARM::R0 || Reg > ARM::R7) {
  4414. if (!(MI.getOpcode() == ARM::tPUSH && Reg == ARM::LR) &&
  4415. !(MI.getOpcode() == ARM::tPOP_RET && Reg == ARM::PC)) {
  4416. ErrInfo = "Unsupported register in Thumb1 push/pop";
  4417. return false;
  4418. }
  4419. }
  4420. }
  4421. }
  4422. if (MI.getOpcode() == ARM::MVE_VMOV_q_rr) {
  4423. assert(MI.getOperand(4).isImm() && MI.getOperand(5).isImm());
  4424. if ((MI.getOperand(4).getImm() != 2 && MI.getOperand(4).getImm() != 3) ||
  4425. MI.getOperand(4).getImm() != MI.getOperand(5).getImm() + 2) {
  4426. ErrInfo = "Incorrect array index for MVE_VMOV_q_rr";
  4427. return false;
  4428. }
  4429. }
  4430. // Check the address model by taking the first Imm operand and checking it is
  4431. // legal for that addressing mode.
  4432. ARMII::AddrMode AddrMode =
  4433. (ARMII::AddrMode)(MI.getDesc().TSFlags & ARMII::AddrModeMask);
  4434. switch (AddrMode) {
  4435. default:
  4436. break;
  4437. case ARMII::AddrModeT2_i7:
  4438. case ARMII::AddrModeT2_i7s2:
  4439. case ARMII::AddrModeT2_i7s4:
  4440. case ARMII::AddrModeT2_i8:
  4441. case ARMII::AddrModeT2_i8pos:
  4442. case ARMII::AddrModeT2_i8neg:
  4443. case ARMII::AddrModeT2_i8s4:
  4444. case ARMII::AddrModeT2_i12: {
  4445. uint32_t Imm = 0;
  4446. for (auto Op : MI.operands()) {
  4447. if (Op.isImm()) {
  4448. Imm = Op.getImm();
  4449. break;
  4450. }
  4451. }
  4452. if (!isLegalAddressImm(MI.getOpcode(), Imm, this)) {
  4453. ErrInfo = "Incorrect AddrMode Imm for instruction";
  4454. return false;
  4455. }
  4456. break;
  4457. }
  4458. }
  4459. return true;
  4460. }
  4461. void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
  4462. unsigned LoadImmOpc,
  4463. unsigned LoadOpc) const {
  4464. assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
  4465. "ROPI/RWPI not currently supported with stack guard");
  4466. MachineBasicBlock &MBB = *MI->getParent();
  4467. DebugLoc DL = MI->getDebugLoc();
  4468. Register Reg = MI->getOperand(0).getReg();
  4469. MachineInstrBuilder MIB;
  4470. unsigned int Offset = 0;
  4471. if (LoadImmOpc == ARM::MRC || LoadImmOpc == ARM::t2MRC) {
  4472. assert(Subtarget.isReadTPHard() &&
  4473. "TLS stack protector requires hardware TLS register");
  4474. BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
  4475. .addImm(15)
  4476. .addImm(0)
  4477. .addImm(13)
  4478. .addImm(0)
  4479. .addImm(3)
  4480. .add(predOps(ARMCC::AL));
  4481. Module &M = *MBB.getParent()->getFunction().getParent();
  4482. Offset = M.getStackProtectorGuardOffset();
  4483. if (Offset & ~0xfffU) {
  4484. // The offset won't fit in the LDR's 12-bit immediate field, so emit an
  4485. // extra ADD to cover the delta. This gives us a guaranteed 8 additional
  4486. // bits, resulting in a range of 0 to +1 MiB for the guard offset.
  4487. unsigned AddOpc = (LoadImmOpc == ARM::MRC) ? ARM::ADDri : ARM::t2ADDri;
  4488. BuildMI(MBB, MI, DL, get(AddOpc), Reg)
  4489. .addReg(Reg, RegState::Kill)
  4490. .addImm(Offset & ~0xfffU)
  4491. .add(predOps(ARMCC::AL))
  4492. .addReg(0);
  4493. Offset &= 0xfffU;
  4494. }
  4495. } else {
  4496. const GlobalValue *GV =
  4497. cast<GlobalValue>((*MI->memoperands_begin())->getValue());
  4498. bool IsIndirect = Subtarget.isGVIndirectSymbol(GV);
  4499. unsigned TargetFlags = ARMII::MO_NO_FLAG;
  4500. if (Subtarget.isTargetMachO()) {
  4501. TargetFlags |= ARMII::MO_NONLAZY;
  4502. } else if (Subtarget.isTargetCOFF()) {
  4503. if (GV->hasDLLImportStorageClass())
  4504. TargetFlags |= ARMII::MO_DLLIMPORT;
  4505. else if (IsIndirect)
  4506. TargetFlags |= ARMII::MO_COFFSTUB;
  4507. } else if (Subtarget.isGVInGOT(GV)) {
  4508. TargetFlags |= ARMII::MO_GOT;
  4509. }
  4510. BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
  4511. .addGlobalAddress(GV, 0, TargetFlags);
  4512. if (IsIndirect) {
  4513. MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
  4514. MIB.addReg(Reg, RegState::Kill).addImm(0);
  4515. auto Flags = MachineMemOperand::MOLoad |
  4516. MachineMemOperand::MODereferenceable |
  4517. MachineMemOperand::MOInvariant;
  4518. MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
  4519. MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, Align(4));
  4520. MIB.addMemOperand(MMO).add(predOps(ARMCC::AL));
  4521. }
  4522. }
  4523. MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
  4524. MIB.addReg(Reg, RegState::Kill)
  4525. .addImm(Offset)
  4526. .cloneMemRefs(*MI)
  4527. .add(predOps(ARMCC::AL));
  4528. }
  4529. bool
  4530. ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
  4531. unsigned &AddSubOpc,
  4532. bool &NegAcc, bool &HasLane) const {
  4533. DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
  4534. if (I == MLxEntryMap.end())
  4535. return false;
  4536. const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
  4537. MulOpc = Entry.MulOpc;
  4538. AddSubOpc = Entry.AddSubOpc;
  4539. NegAcc = Entry.NegAcc;
  4540. HasLane = Entry.HasLane;
  4541. return true;
  4542. }
  4543. //===----------------------------------------------------------------------===//
  4544. // Execution domains.
  4545. //===----------------------------------------------------------------------===//
  4546. //
  4547. // Some instructions go down the NEON pipeline, some go down the VFP pipeline,
  4548. // and some can go down both. The vmov instructions go down the VFP pipeline,
  4549. // but they can be changed to vorr equivalents that are executed by the NEON
  4550. // pipeline.
  4551. //
  4552. // We use the following execution domain numbering:
  4553. //
  4554. enum ARMExeDomain {
  4555. ExeGeneric = 0,
  4556. ExeVFP = 1,
  4557. ExeNEON = 2
  4558. };
  4559. //
  4560. // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
  4561. //
  4562. std::pair<uint16_t, uint16_t>
  4563. ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
  4564. // If we don't have access to NEON instructions then we won't be able
  4565. // to swizzle anything to the NEON domain. Check to make sure.
  4566. if (Subtarget.hasNEON()) {
  4567. // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
  4568. // if they are not predicated.
  4569. if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
  4570. return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
  4571. // CortexA9 is particularly picky about mixing the two and wants these
  4572. // converted.
  4573. if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
  4574. (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
  4575. MI.getOpcode() == ARM::VMOVS))
  4576. return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
  4577. }
  4578. // No other instructions can be swizzled, so just determine their domain.
  4579. unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
  4580. if (Domain & ARMII::DomainNEON)
  4581. return std::make_pair(ExeNEON, 0);
  4582. // Certain instructions can go either way on Cortex-A8.
  4583. // Treat them as NEON instructions.
  4584. if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
  4585. return std::make_pair(ExeNEON, 0);
  4586. if (Domain & ARMII::DomainVFP)
  4587. return std::make_pair(ExeVFP, 0);
  4588. return std::make_pair(ExeGeneric, 0);
  4589. }
  4590. static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
  4591. unsigned SReg, unsigned &Lane) {
  4592. unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
  4593. Lane = 0;
  4594. if (DReg != ARM::NoRegister)
  4595. return DReg;
  4596. Lane = 1;
  4597. DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
  4598. assert(DReg && "S-register with no D super-register?");
  4599. return DReg;
  4600. }
  4601. /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
  4602. /// set ImplicitSReg to a register number that must be marked as implicit-use or
  4603. /// zero if no register needs to be defined as implicit-use.
  4604. ///
  4605. /// If the function cannot determine if an SPR should be marked implicit use or
  4606. /// not, it returns false.
  4607. ///
  4608. /// This function handles cases where an instruction is being modified from taking
  4609. /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
  4610. /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
  4611. /// lane of the DPR).
  4612. ///
  4613. /// If the other SPR is defined, an implicit-use of it should be added. Else,
  4614. /// (including the case where the DPR itself is defined), it should not.
  4615. ///
  4616. static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
  4617. MachineInstr &MI, unsigned DReg,
  4618. unsigned Lane, unsigned &ImplicitSReg) {
  4619. // If the DPR is defined or used already, the other SPR lane will be chained
  4620. // correctly, so there is nothing to be done.
  4621. if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
  4622. ImplicitSReg = 0;
  4623. return true;
  4624. }
  4625. // Otherwise we need to go searching to see if the SPR is set explicitly.
  4626. ImplicitSReg = TRI->getSubReg(DReg,
  4627. (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
  4628. MachineBasicBlock::LivenessQueryResult LQR =
  4629. MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
  4630. if (LQR == MachineBasicBlock::LQR_Live)
  4631. return true;
  4632. else if (LQR == MachineBasicBlock::LQR_Unknown)
  4633. return false;
  4634. // If the register is known not to be live, there is no need to add an
  4635. // implicit-use.
  4636. ImplicitSReg = 0;
  4637. return true;
  4638. }
  4639. void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
  4640. unsigned Domain) const {
  4641. unsigned DstReg, SrcReg, DReg;
  4642. unsigned Lane;
  4643. MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
  4644. const TargetRegisterInfo *TRI = &getRegisterInfo();
  4645. switch (MI.getOpcode()) {
  4646. default:
  4647. llvm_unreachable("cannot handle opcode!");
  4648. break;
  4649. case ARM::VMOVD:
  4650. if (Domain != ExeNEON)
  4651. break;
  4652. // Zap the predicate operands.
  4653. assert(!isPredicated(MI) && "Cannot predicate a VORRd");
  4654. // Make sure we've got NEON instructions.
  4655. assert(Subtarget.hasNEON() && "VORRd requires NEON");
  4656. // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
  4657. DstReg = MI.getOperand(0).getReg();
  4658. SrcReg = MI.getOperand(1).getReg();
  4659. for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
  4660. MI.RemoveOperand(i - 1);
  4661. // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
  4662. MI.setDesc(get(ARM::VORRd));
  4663. MIB.addReg(DstReg, RegState::Define)
  4664. .addReg(SrcReg)
  4665. .addReg(SrcReg)
  4666. .add(predOps(ARMCC::AL));
  4667. break;
  4668. case ARM::VMOVRS:
  4669. if (Domain != ExeNEON)
  4670. break;
  4671. assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
  4672. // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
  4673. DstReg = MI.getOperand(0).getReg();
  4674. SrcReg = MI.getOperand(1).getReg();
  4675. for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
  4676. MI.RemoveOperand(i - 1);
  4677. DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
  4678. // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
  4679. // Note that DSrc has been widened and the other lane may be undef, which
  4680. // contaminates the entire register.
  4681. MI.setDesc(get(ARM::VGETLNi32));
  4682. MIB.addReg(DstReg, RegState::Define)
  4683. .addReg(DReg, RegState::Undef)
  4684. .addImm(Lane)
  4685. .add(predOps(ARMCC::AL));
  4686. // The old source should be an implicit use, otherwise we might think it
  4687. // was dead before here.
  4688. MIB.addReg(SrcReg, RegState::Implicit);
  4689. break;
  4690. case ARM::VMOVSR: {
  4691. if (Domain != ExeNEON)
  4692. break;
  4693. assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
  4694. // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
  4695. DstReg = MI.getOperand(0).getReg();
  4696. SrcReg = MI.getOperand(1).getReg();
  4697. DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
  4698. unsigned ImplicitSReg;
  4699. if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
  4700. break;
  4701. for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
  4702. MI.RemoveOperand(i - 1);
  4703. // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
  4704. // Again DDst may be undefined at the beginning of this instruction.
  4705. MI.setDesc(get(ARM::VSETLNi32));
  4706. MIB.addReg(DReg, RegState::Define)
  4707. .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
  4708. .addReg(SrcReg)
  4709. .addImm(Lane)
  4710. .add(predOps(ARMCC::AL));
  4711. // The narrower destination must be marked as set to keep previous chains
  4712. // in place.
  4713. MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
  4714. if (ImplicitSReg != 0)
  4715. MIB.addReg(ImplicitSReg, RegState::Implicit);
  4716. break;
  4717. }
  4718. case ARM::VMOVS: {
  4719. if (Domain != ExeNEON)
  4720. break;
  4721. // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
  4722. DstReg = MI.getOperand(0).getReg();
  4723. SrcReg = MI.getOperand(1).getReg();
  4724. unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
  4725. DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
  4726. DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
  4727. unsigned ImplicitSReg;
  4728. if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
  4729. break;
  4730. for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
  4731. MI.RemoveOperand(i - 1);
  4732. if (DSrc == DDst) {
  4733. // Destination can be:
  4734. // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
  4735. MI.setDesc(get(ARM::VDUPLN32d));
  4736. MIB.addReg(DDst, RegState::Define)
  4737. .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
  4738. .addImm(SrcLane)
  4739. .add(predOps(ARMCC::AL));
  4740. // Neither the source or the destination are naturally represented any
  4741. // more, so add them in manually.
  4742. MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
  4743. MIB.addReg(SrcReg, RegState::Implicit);
  4744. if (ImplicitSReg != 0)
  4745. MIB.addReg(ImplicitSReg, RegState::Implicit);
  4746. break;
  4747. }
  4748. // In general there's no single instruction that can perform an S <-> S
  4749. // move in NEON space, but a pair of VEXT instructions *can* do the
  4750. // job. It turns out that the VEXTs needed will only use DSrc once, with
  4751. // the position based purely on the combination of lane-0 and lane-1
  4752. // involved. For example
  4753. // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
  4754. // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
  4755. // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
  4756. // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
  4757. //
  4758. // Pattern of the MachineInstrs is:
  4759. // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
  4760. MachineInstrBuilder NewMIB;
  4761. NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
  4762. DDst);
  4763. // On the first instruction, both DSrc and DDst may be undef if present.
  4764. // Specifically when the original instruction didn't have them as an
  4765. // <imp-use>.
  4766. unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
  4767. bool CurUndef = !MI.readsRegister(CurReg, TRI);
  4768. NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
  4769. CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
  4770. CurUndef = !MI.readsRegister(CurReg, TRI);
  4771. NewMIB.addReg(CurReg, getUndefRegState(CurUndef))
  4772. .addImm(1)
  4773. .add(predOps(ARMCC::AL));
  4774. if (SrcLane == DstLane)
  4775. NewMIB.addReg(SrcReg, RegState::Implicit);
  4776. MI.setDesc(get(ARM::VEXTd32));
  4777. MIB.addReg(DDst, RegState::Define);
  4778. // On the second instruction, DDst has definitely been defined above, so
  4779. // it is not undef. DSrc, if present, can be undef as above.
  4780. CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
  4781. CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
  4782. MIB.addReg(CurReg, getUndefRegState(CurUndef));
  4783. CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
  4784. CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
  4785. MIB.addReg(CurReg, getUndefRegState(CurUndef))
  4786. .addImm(1)
  4787. .add(predOps(ARMCC::AL));
  4788. if (SrcLane != DstLane)
  4789. MIB.addReg(SrcReg, RegState::Implicit);
  4790. // As before, the original destination is no longer represented, add it
  4791. // implicitly.
  4792. MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
  4793. if (ImplicitSReg != 0)
  4794. MIB.addReg(ImplicitSReg, RegState::Implicit);
  4795. break;
  4796. }
  4797. }
  4798. }
  4799. //===----------------------------------------------------------------------===//
  4800. // Partial register updates
  4801. //===----------------------------------------------------------------------===//
  4802. //
  4803. // Swift renames NEON registers with 64-bit granularity. That means any
  4804. // instruction writing an S-reg implicitly reads the containing D-reg. The
  4805. // problem is mostly avoided by translating f32 operations to v2f32 operations
  4806. // on D-registers, but f32 loads are still a problem.
  4807. //
  4808. // These instructions can load an f32 into a NEON register:
  4809. //
  4810. // VLDRS - Only writes S, partial D update.
  4811. // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
  4812. // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
  4813. //
  4814. // FCONSTD can be used as a dependency-breaking instruction.
  4815. unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
  4816. const MachineInstr &MI, unsigned OpNum,
  4817. const TargetRegisterInfo *TRI) const {
  4818. auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
  4819. if (!PartialUpdateClearance)
  4820. return 0;
  4821. assert(TRI && "Need TRI instance");
  4822. const MachineOperand &MO = MI.getOperand(OpNum);
  4823. if (MO.readsReg())
  4824. return 0;
  4825. Register Reg = MO.getReg();
  4826. int UseOp = -1;
  4827. switch (MI.getOpcode()) {
  4828. // Normal instructions writing only an S-register.
  4829. case ARM::VLDRS:
  4830. case ARM::FCONSTS:
  4831. case ARM::VMOVSR:
  4832. case ARM::VMOVv8i8:
  4833. case ARM::VMOVv4i16:
  4834. case ARM::VMOVv2i32:
  4835. case ARM::VMOVv2f32:
  4836. case ARM::VMOVv1i64:
  4837. UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
  4838. break;
  4839. // Explicitly reads the dependency.
  4840. case ARM::VLD1LNd32:
  4841. UseOp = 3;
  4842. break;
  4843. default:
  4844. return 0;
  4845. }
  4846. // If this instruction actually reads a value from Reg, there is no unwanted
  4847. // dependency.
  4848. if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
  4849. return 0;
  4850. // We must be able to clobber the whole D-reg.
  4851. if (Register::isVirtualRegister(Reg)) {
  4852. // Virtual register must be a def undef foo:ssub_0 operand.
  4853. if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
  4854. return 0;
  4855. } else if (ARM::SPRRegClass.contains(Reg)) {
  4856. // Physical register: MI must define the full D-reg.
  4857. unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
  4858. &ARM::DPRRegClass);
  4859. if (!DReg || !MI.definesRegister(DReg, TRI))
  4860. return 0;
  4861. }
  4862. // MI has an unwanted D-register dependency.
  4863. // Avoid defs in the previous N instructrions.
  4864. return PartialUpdateClearance;
  4865. }
  4866. // Break a partial register dependency after getPartialRegUpdateClearance
  4867. // returned non-zero.
  4868. void ARMBaseInstrInfo::breakPartialRegDependency(
  4869. MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
  4870. assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
  4871. assert(TRI && "Need TRI instance");
  4872. const MachineOperand &MO = MI.getOperand(OpNum);
  4873. Register Reg = MO.getReg();
  4874. assert(Register::isPhysicalRegister(Reg) &&
  4875. "Can't break virtual register dependencies.");
  4876. unsigned DReg = Reg;
  4877. // If MI defines an S-reg, find the corresponding D super-register.
  4878. if (ARM::SPRRegClass.contains(Reg)) {
  4879. DReg = ARM::D0 + (Reg - ARM::S0) / 2;
  4880. assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
  4881. }
  4882. assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
  4883. assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
  4884. // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
  4885. // the full D-register by loading the same value to both lanes. The
  4886. // instruction is micro-coded with 2 uops, so don't do this until we can
  4887. // properly schedule micro-coded instructions. The dispatcher stalls cause
  4888. // too big regressions.
  4889. // Insert the dependency-breaking FCONSTD before MI.
  4890. // 96 is the encoding of 0.5, but the actual value doesn't matter here.
  4891. BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
  4892. .addImm(96)
  4893. .add(predOps(ARMCC::AL));
  4894. MI.addRegisterKilled(DReg, TRI, true);
  4895. }
  4896. bool ARMBaseInstrInfo::hasNOP() const {
  4897. return Subtarget.getFeatureBits()[ARM::HasV6KOps];
  4898. }
  4899. bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
  4900. if (MI->getNumOperands() < 4)
  4901. return true;
  4902. unsigned ShOpVal = MI->getOperand(3).getImm();
  4903. unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
  4904. // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
  4905. if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
  4906. ((ShImm == 1 || ShImm == 2) &&
  4907. ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
  4908. return true;
  4909. return false;
  4910. }
  4911. bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
  4912. const MachineInstr &MI, unsigned DefIdx,
  4913. SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
  4914. assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
  4915. assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
  4916. switch (MI.getOpcode()) {
  4917. case ARM::VMOVDRR:
  4918. // dX = VMOVDRR rY, rZ
  4919. // is the same as:
  4920. // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
  4921. // Populate the InputRegs accordingly.
  4922. // rY
  4923. const MachineOperand *MOReg = &MI.getOperand(1);
  4924. if (!MOReg->isUndef())
  4925. InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
  4926. MOReg->getSubReg(), ARM::ssub_0));
  4927. // rZ
  4928. MOReg = &MI.getOperand(2);
  4929. if (!MOReg->isUndef())
  4930. InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
  4931. MOReg->getSubReg(), ARM::ssub_1));
  4932. return true;
  4933. }
  4934. llvm_unreachable("Target dependent opcode missing");
  4935. }
  4936. bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
  4937. const MachineInstr &MI, unsigned DefIdx,
  4938. RegSubRegPairAndIdx &InputReg) const {
  4939. assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
  4940. assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
  4941. switch (MI.getOpcode()) {
  4942. case ARM::VMOVRRD:
  4943. // rX, rY = VMOVRRD dZ
  4944. // is the same as:
  4945. // rX = EXTRACT_SUBREG dZ, ssub_0
  4946. // rY = EXTRACT_SUBREG dZ, ssub_1
  4947. const MachineOperand &MOReg = MI.getOperand(2);
  4948. if (MOReg.isUndef())
  4949. return false;
  4950. InputReg.Reg = MOReg.getReg();
  4951. InputReg.SubReg = MOReg.getSubReg();
  4952. InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
  4953. return true;
  4954. }
  4955. llvm_unreachable("Target dependent opcode missing");
  4956. }
  4957. bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
  4958. const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
  4959. RegSubRegPairAndIdx &InsertedReg) const {
  4960. assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
  4961. assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
  4962. switch (MI.getOpcode()) {
  4963. case ARM::VSETLNi32:
  4964. case ARM::MVE_VMOV_to_lane_32:
  4965. // dX = VSETLNi32 dY, rZ, imm
  4966. // qX = MVE_VMOV_to_lane_32 qY, rZ, imm
  4967. const MachineOperand &MOBaseReg = MI.getOperand(1);
  4968. const MachineOperand &MOInsertedReg = MI.getOperand(2);
  4969. if (MOInsertedReg.isUndef())
  4970. return false;
  4971. const MachineOperand &MOIndex = MI.getOperand(3);
  4972. BaseReg.Reg = MOBaseReg.getReg();
  4973. BaseReg.SubReg = MOBaseReg.getSubReg();
  4974. InsertedReg.Reg = MOInsertedReg.getReg();
  4975. InsertedReg.SubReg = MOInsertedReg.getSubReg();
  4976. InsertedReg.SubIdx = ARM::ssub_0 + MOIndex.getImm();
  4977. return true;
  4978. }
  4979. llvm_unreachable("Target dependent opcode missing");
  4980. }
  4981. std::pair<unsigned, unsigned>
  4982. ARMBaseInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
  4983. const unsigned Mask = ARMII::MO_OPTION_MASK;
  4984. return std::make_pair(TF & Mask, TF & ~Mask);
  4985. }
  4986. ArrayRef<std::pair<unsigned, const char *>>
  4987. ARMBaseInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
  4988. using namespace ARMII;
  4989. static const std::pair<unsigned, const char *> TargetFlags[] = {
  4990. {MO_LO16, "arm-lo16"}, {MO_HI16, "arm-hi16"}};
  4991. return makeArrayRef(TargetFlags);
  4992. }
  4993. ArrayRef<std::pair<unsigned, const char *>>
  4994. ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
  4995. using namespace ARMII;
  4996. static const std::pair<unsigned, const char *> TargetFlags[] = {
  4997. {MO_COFFSTUB, "arm-coffstub"},
  4998. {MO_GOT, "arm-got"},
  4999. {MO_SBREL, "arm-sbrel"},
  5000. {MO_DLLIMPORT, "arm-dllimport"},
  5001. {MO_SECREL, "arm-secrel"},
  5002. {MO_NONLAZY, "arm-nonlazy"}};
  5003. return makeArrayRef(TargetFlags);
  5004. }
  5005. Optional<RegImmPair> ARMBaseInstrInfo::isAddImmediate(const MachineInstr &MI,
  5006. Register Reg) const {
  5007. int Sign = 1;
  5008. unsigned Opcode = MI.getOpcode();
  5009. int64_t Offset = 0;
  5010. // TODO: Handle cases where Reg is a super- or sub-register of the
  5011. // destination register.
  5012. const MachineOperand &Op0 = MI.getOperand(0);
  5013. if (!Op0.isReg() || Reg != Op0.getReg())
  5014. return None;
  5015. // We describe SUBri or ADDri instructions.
  5016. if (Opcode == ARM::SUBri)
  5017. Sign = -1;
  5018. else if (Opcode != ARM::ADDri)
  5019. return None;
  5020. // TODO: Third operand can be global address (usually some string). Since
  5021. // strings can be relocated we cannot calculate their offsets for
  5022. // now.
  5023. if (!MI.getOperand(1).isReg() || !MI.getOperand(2).isImm())
  5024. return None;
  5025. Offset = MI.getOperand(2).getImm() * Sign;
  5026. return RegImmPair{MI.getOperand(1).getReg(), Offset};
  5027. }
  5028. bool llvm::registerDefinedBetween(unsigned Reg,
  5029. MachineBasicBlock::iterator From,
  5030. MachineBasicBlock::iterator To,
  5031. const TargetRegisterInfo *TRI) {
  5032. for (auto I = From; I != To; ++I)
  5033. if (I->modifiesRegister(Reg, TRI))
  5034. return true;
  5035. return false;
  5036. }
  5037. MachineInstr *llvm::findCMPToFoldIntoCBZ(MachineInstr *Br,
  5038. const TargetRegisterInfo *TRI) {
  5039. // Search backwards to the instruction that defines CSPR. This may or not
  5040. // be a CMP, we check that after this loop. If we find another instruction
  5041. // that reads cpsr, we return nullptr.
  5042. MachineBasicBlock::iterator CmpMI = Br;
  5043. while (CmpMI != Br->getParent()->begin()) {
  5044. --CmpMI;
  5045. if (CmpMI->modifiesRegister(ARM::CPSR, TRI))
  5046. break;
  5047. if (CmpMI->readsRegister(ARM::CPSR, TRI))
  5048. break;
  5049. }
  5050. // Check that this inst is a CMP r[0-7], #0 and that the register
  5051. // is not redefined between the cmp and the br.
  5052. if (CmpMI->getOpcode() != ARM::tCMPi8 && CmpMI->getOpcode() != ARM::t2CMPri)
  5053. return nullptr;
  5054. Register Reg = CmpMI->getOperand(0).getReg();
  5055. Register PredReg;
  5056. ARMCC::CondCodes Pred = getInstrPredicate(*CmpMI, PredReg);
  5057. if (Pred != ARMCC::AL || CmpMI->getOperand(1).getImm() != 0)
  5058. return nullptr;
  5059. if (!isARMLowRegister(Reg))
  5060. return nullptr;
  5061. if (registerDefinedBetween(Reg, CmpMI->getNextNode(), Br, TRI))
  5062. return nullptr;
  5063. return &*CmpMI;
  5064. }
  5065. unsigned llvm::ConstantMaterializationCost(unsigned Val,
  5066. const ARMSubtarget *Subtarget,
  5067. bool ForCodesize) {
  5068. if (Subtarget->isThumb()) {
  5069. if (Val <= 255) // MOV
  5070. return ForCodesize ? 2 : 1;
  5071. if (Subtarget->hasV6T2Ops() && (Val <= 0xffff || // MOV
  5072. ARM_AM::getT2SOImmVal(Val) != -1 || // MOVW
  5073. ARM_AM::getT2SOImmVal(~Val) != -1)) // MVN
  5074. return ForCodesize ? 4 : 1;
  5075. if (Val <= 510) // MOV + ADDi8
  5076. return ForCodesize ? 4 : 2;
  5077. if (~Val <= 255) // MOV + MVN
  5078. return ForCodesize ? 4 : 2;
  5079. if (ARM_AM::isThumbImmShiftedVal(Val)) // MOV + LSL
  5080. return ForCodesize ? 4 : 2;
  5081. } else {
  5082. if (ARM_AM::getSOImmVal(Val) != -1) // MOV
  5083. return ForCodesize ? 4 : 1;
  5084. if (ARM_AM::getSOImmVal(~Val) != -1) // MVN
  5085. return ForCodesize ? 4 : 1;
  5086. if (Subtarget->hasV6T2Ops() && Val <= 0xffff) // MOVW
  5087. return ForCodesize ? 4 : 1;
  5088. if (ARM_AM::isSOImmTwoPartVal(Val)) // two instrs
  5089. return ForCodesize ? 8 : 2;
  5090. if (ARM_AM::isSOImmTwoPartValNeg(Val)) // two instrs
  5091. return ForCodesize ? 8 : 2;
  5092. }
  5093. if (Subtarget->useMovt()) // MOVW + MOVT
  5094. return ForCodesize ? 8 : 2;
  5095. return ForCodesize ? 8 : 3; // Literal pool load
  5096. }
  5097. bool llvm::HasLowerConstantMaterializationCost(unsigned Val1, unsigned Val2,
  5098. const ARMSubtarget *Subtarget,
  5099. bool ForCodesize) {
  5100. // Check with ForCodesize
  5101. unsigned Cost1 = ConstantMaterializationCost(Val1, Subtarget, ForCodesize);
  5102. unsigned Cost2 = ConstantMaterializationCost(Val2, Subtarget, ForCodesize);
  5103. if (Cost1 < Cost2)
  5104. return true;
  5105. if (Cost1 > Cost2)
  5106. return false;
  5107. // If they are equal, try with !ForCodesize
  5108. return ConstantMaterializationCost(Val1, Subtarget, !ForCodesize) <
  5109. ConstantMaterializationCost(Val2, Subtarget, !ForCodesize);
  5110. }
  5111. /// Constants defining how certain sequences should be outlined.
  5112. /// This encompasses how an outlined function should be called, and what kind of
  5113. /// frame should be emitted for that outlined function.
  5114. ///
  5115. /// \p MachineOutlinerTailCall implies that the function is being created from
  5116. /// a sequence of instructions ending in a return.
  5117. ///
  5118. /// That is,
  5119. ///
  5120. /// I1 OUTLINED_FUNCTION:
  5121. /// I2 --> B OUTLINED_FUNCTION I1
  5122. /// BX LR I2
  5123. /// BX LR
  5124. ///
  5125. /// +-------------------------+--------+-----+
  5126. /// | | Thumb2 | ARM |
  5127. /// +-------------------------+--------+-----+
  5128. /// | Call overhead in Bytes | 4 | 4 |
  5129. /// | Frame overhead in Bytes | 0 | 0 |
  5130. /// | Stack fixup required | No | No |
  5131. /// +-------------------------+--------+-----+
  5132. ///
  5133. /// \p MachineOutlinerThunk implies that the function is being created from
  5134. /// a sequence of instructions ending in a call. The outlined function is
  5135. /// called with a BL instruction, and the outlined function tail-calls the
  5136. /// original call destination.
  5137. ///
  5138. /// That is,
  5139. ///
  5140. /// I1 OUTLINED_FUNCTION:
  5141. /// I2 --> BL OUTLINED_FUNCTION I1
  5142. /// BL f I2
  5143. /// B f
  5144. ///
  5145. /// +-------------------------+--------+-----+
  5146. /// | | Thumb2 | ARM |
  5147. /// +-------------------------+--------+-----+
  5148. /// | Call overhead in Bytes | 4 | 4 |
  5149. /// | Frame overhead in Bytes | 0 | 0 |
  5150. /// | Stack fixup required | No | No |
  5151. /// +-------------------------+--------+-----+
  5152. ///
  5153. /// \p MachineOutlinerNoLRSave implies that the function should be called using
  5154. /// a BL instruction, but doesn't require LR to be saved and restored. This
  5155. /// happens when LR is known to be dead.
  5156. ///
  5157. /// That is,
  5158. ///
  5159. /// I1 OUTLINED_FUNCTION:
  5160. /// I2 --> BL OUTLINED_FUNCTION I1
  5161. /// I3 I2
  5162. /// I3
  5163. /// BX LR
  5164. ///
  5165. /// +-------------------------+--------+-----+
  5166. /// | | Thumb2 | ARM |
  5167. /// +-------------------------+--------+-----+
  5168. /// | Call overhead in Bytes | 4 | 4 |
  5169. /// | Frame overhead in Bytes | 2 | 4 |
  5170. /// | Stack fixup required | No | No |
  5171. /// +-------------------------+--------+-----+
  5172. ///
  5173. /// \p MachineOutlinerRegSave implies that the function should be called with a
  5174. /// save and restore of LR to an available register. This allows us to avoid
  5175. /// stack fixups. Note that this outlining variant is compatible with the
  5176. /// NoLRSave case.
  5177. ///
  5178. /// That is,
  5179. ///
  5180. /// I1 Save LR OUTLINED_FUNCTION:
  5181. /// I2 --> BL OUTLINED_FUNCTION I1
  5182. /// I3 Restore LR I2
  5183. /// I3
  5184. /// BX LR
  5185. ///
  5186. /// +-------------------------+--------+-----+
  5187. /// | | Thumb2 | ARM |
  5188. /// +-------------------------+--------+-----+
  5189. /// | Call overhead in Bytes | 8 | 12 |
  5190. /// | Frame overhead in Bytes | 2 | 4 |
  5191. /// | Stack fixup required | No | No |
  5192. /// +-------------------------+--------+-----+
  5193. ///
  5194. /// \p MachineOutlinerDefault implies that the function should be called with
  5195. /// a save and restore of LR to the stack.
  5196. ///
  5197. /// That is,
  5198. ///
  5199. /// I1 Save LR OUTLINED_FUNCTION:
  5200. /// I2 --> BL OUTLINED_FUNCTION I1
  5201. /// I3 Restore LR I2
  5202. /// I3
  5203. /// BX LR
  5204. ///
  5205. /// +-------------------------+--------+-----+
  5206. /// | | Thumb2 | ARM |
  5207. /// +-------------------------+--------+-----+
  5208. /// | Call overhead in Bytes | 8 | 12 |
  5209. /// | Frame overhead in Bytes | 2 | 4 |
  5210. /// | Stack fixup required | Yes | Yes |
  5211. /// +-------------------------+--------+-----+
  5212. enum MachineOutlinerClass {
  5213. MachineOutlinerTailCall,
  5214. MachineOutlinerThunk,
  5215. MachineOutlinerNoLRSave,
  5216. MachineOutlinerRegSave,
  5217. MachineOutlinerDefault
  5218. };
  5219. enum MachineOutlinerMBBFlags {
  5220. LRUnavailableSomewhere = 0x2,
  5221. HasCalls = 0x4,
  5222. UnsafeRegsDead = 0x8
  5223. };
  5224. struct OutlinerCosts {
  5225. int CallTailCall;
  5226. int FrameTailCall;
  5227. int CallThunk;
  5228. int FrameThunk;
  5229. int CallNoLRSave;
  5230. int FrameNoLRSave;
  5231. int CallRegSave;
  5232. int FrameRegSave;
  5233. int CallDefault;
  5234. int FrameDefault;
  5235. int SaveRestoreLROnStack;
  5236. OutlinerCosts(const ARMSubtarget &target)
  5237. : CallTailCall(target.isThumb() ? 4 : 4),
  5238. FrameTailCall(target.isThumb() ? 0 : 0),
  5239. CallThunk(target.isThumb() ? 4 : 4),
  5240. FrameThunk(target.isThumb() ? 0 : 0),
  5241. CallNoLRSave(target.isThumb() ? 4 : 4),
  5242. FrameNoLRSave(target.isThumb() ? 2 : 4),
  5243. CallRegSave(target.isThumb() ? 8 : 12),
  5244. FrameRegSave(target.isThumb() ? 2 : 4),
  5245. CallDefault(target.isThumb() ? 8 : 12),
  5246. FrameDefault(target.isThumb() ? 2 : 4),
  5247. SaveRestoreLROnStack(target.isThumb() ? 8 : 8) {}
  5248. };
  5249. unsigned
  5250. ARMBaseInstrInfo::findRegisterToSaveLRTo(const outliner::Candidate &C) const {
  5251. assert(C.LRUWasSet && "LRU wasn't set?");
  5252. MachineFunction *MF = C.getMF();
  5253. const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo *>(
  5254. MF->getSubtarget().getRegisterInfo());
  5255. BitVector regsReserved = ARI->getReservedRegs(*MF);
  5256. // Check if there is an available register across the sequence that we can
  5257. // use.
  5258. for (unsigned Reg : ARM::rGPRRegClass) {
  5259. if (!(Reg < regsReserved.size() && regsReserved.test(Reg)) &&
  5260. Reg != ARM::LR && // LR is not reserved, but don't use it.
  5261. Reg != ARM::R12 && // R12 is not guaranteed to be preserved.
  5262. C.LRU.available(Reg) && C.UsedInSequence.available(Reg))
  5263. return Reg;
  5264. }
  5265. // No suitable register. Return 0.
  5266. return 0u;
  5267. }
  5268. // Compute liveness of LR at the point after the interval [I, E), which
  5269. // denotes a *backward* iteration through instructions. Used only for return
  5270. // basic blocks, which do not end with a tail call.
  5271. static bool isLRAvailable(const TargetRegisterInfo &TRI,
  5272. MachineBasicBlock::reverse_iterator I,
  5273. MachineBasicBlock::reverse_iterator E) {
  5274. // At the end of the function LR dead.
  5275. bool Live = false;
  5276. for (; I != E; ++I) {
  5277. const MachineInstr &MI = *I;
  5278. // Check defs of LR.
  5279. if (MI.modifiesRegister(ARM::LR, &TRI))
  5280. Live = false;
  5281. // Check uses of LR.
  5282. unsigned Opcode = MI.getOpcode();
  5283. if (Opcode == ARM::BX_RET || Opcode == ARM::MOVPCLR ||
  5284. Opcode == ARM::SUBS_PC_LR || Opcode == ARM::tBX_RET ||
  5285. Opcode == ARM::tBXNS_RET) {
  5286. // These instructions use LR, but it's not an (explicit or implicit)
  5287. // operand.
  5288. Live = true;
  5289. continue;
  5290. }
  5291. if (MI.readsRegister(ARM::LR, &TRI))
  5292. Live = true;
  5293. }
  5294. return !Live;
  5295. }
  5296. outliner::OutlinedFunction ARMBaseInstrInfo::getOutliningCandidateInfo(
  5297. std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
  5298. outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
  5299. unsigned SequenceSize =
  5300. std::accumulate(FirstCand.front(), std::next(FirstCand.back()), 0,
  5301. [this](unsigned Sum, const MachineInstr &MI) {
  5302. return Sum + getInstSizeInBytes(MI);
  5303. });
  5304. // Properties about candidate MBBs that hold for all of them.
  5305. unsigned FlagsSetInAll = 0xF;
  5306. // Compute liveness information for each candidate, and set FlagsSetInAll.
  5307. const TargetRegisterInfo &TRI = getRegisterInfo();
  5308. std::for_each(
  5309. RepeatedSequenceLocs.begin(), RepeatedSequenceLocs.end(),
  5310. [&FlagsSetInAll](outliner::Candidate &C) { FlagsSetInAll &= C.Flags; });
  5311. // According to the ARM Procedure Call Standard, the following are
  5312. // undefined on entry/exit from a function call:
  5313. //
  5314. // * Register R12(IP),
  5315. // * Condition codes (and thus the CPSR register)
  5316. //
  5317. // Since we control the instructions which are part of the outlined regions
  5318. // we don't need to be fully compliant with the AAPCS, but we have to
  5319. // guarantee that if a veneer is inserted at link time the code is still
  5320. // correct. Because of this, we can't outline any sequence of instructions
  5321. // where one of these registers is live into/across it. Thus, we need to
  5322. // delete those candidates.
  5323. auto CantGuaranteeValueAcrossCall = [&TRI](outliner::Candidate &C) {
  5324. // If the unsafe registers in this block are all dead, then we don't need
  5325. // to compute liveness here.
  5326. if (C.Flags & UnsafeRegsDead)
  5327. return false;
  5328. C.initLRU(TRI);
  5329. LiveRegUnits LRU = C.LRU;
  5330. return (!LRU.available(ARM::R12) || !LRU.available(ARM::CPSR));
  5331. };
  5332. // Are there any candidates where those registers are live?
  5333. if (!(FlagsSetInAll & UnsafeRegsDead)) {
  5334. // Erase every candidate that violates the restrictions above. (It could be
  5335. // true that we have viable candidates, so it's not worth bailing out in
  5336. // the case that, say, 1 out of 20 candidates violate the restructions.)
  5337. llvm::erase_if(RepeatedSequenceLocs, CantGuaranteeValueAcrossCall);
  5338. // If the sequence doesn't have enough candidates left, then we're done.
  5339. if (RepeatedSequenceLocs.size() < 2)
  5340. return outliner::OutlinedFunction();
  5341. }
  5342. // We expect the majority of the outlining candidates to be in consensus with
  5343. // regard to return address sign and authentication, and branch target
  5344. // enforcement, in other words, partitioning according to all the four
  5345. // possible combinations of PAC-RET and BTI is going to yield one big subset
  5346. // and three small (likely empty) subsets. That allows us to cull incompatible
  5347. // candidates separately for PAC-RET and BTI.
  5348. // Partition the candidates in two sets: one with BTI enabled and one with BTI
  5349. // disabled. Remove the candidates from the smaller set. If they are the same
  5350. // number prefer the non-BTI ones for outlining, since they have less
  5351. // overhead.
  5352. auto NoBTI =
  5353. llvm::partition(RepeatedSequenceLocs, [](const outliner::Candidate &C) {
  5354. const ARMFunctionInfo &AFI = *C.getMF()->getInfo<ARMFunctionInfo>();
  5355. return AFI.branchTargetEnforcement();
  5356. });
  5357. if (std::distance(RepeatedSequenceLocs.begin(), NoBTI) >
  5358. std::distance(NoBTI, RepeatedSequenceLocs.end()))
  5359. RepeatedSequenceLocs.erase(NoBTI, RepeatedSequenceLocs.end());
  5360. else
  5361. RepeatedSequenceLocs.erase(RepeatedSequenceLocs.begin(), NoBTI);
  5362. if (RepeatedSequenceLocs.size() < 2)
  5363. return outliner::OutlinedFunction();
  5364. // Likewise, partition the candidates according to PAC-RET enablement.
  5365. auto NoPAC =
  5366. llvm::partition(RepeatedSequenceLocs, [](const outliner::Candidate &C) {
  5367. const ARMFunctionInfo &AFI = *C.getMF()->getInfo<ARMFunctionInfo>();
  5368. // If the function happens to not spill the LR, do not disqualify it
  5369. // from the outlining.
  5370. return AFI.shouldSignReturnAddress(true);
  5371. });
  5372. if (std::distance(RepeatedSequenceLocs.begin(), NoPAC) >
  5373. std::distance(NoPAC, RepeatedSequenceLocs.end()))
  5374. RepeatedSequenceLocs.erase(NoPAC, RepeatedSequenceLocs.end());
  5375. else
  5376. RepeatedSequenceLocs.erase(RepeatedSequenceLocs.begin(), NoPAC);
  5377. if (RepeatedSequenceLocs.size() < 2)
  5378. return outliner::OutlinedFunction();
  5379. // At this point, we have only "safe" candidates to outline. Figure out
  5380. // frame + call instruction information.
  5381. unsigned LastInstrOpcode = RepeatedSequenceLocs[0].back()->getOpcode();
  5382. // Helper lambda which sets call information for every candidate.
  5383. auto SetCandidateCallInfo =
  5384. [&RepeatedSequenceLocs](unsigned CallID, unsigned NumBytesForCall) {
  5385. for (outliner::Candidate &C : RepeatedSequenceLocs)
  5386. C.setCallInfo(CallID, NumBytesForCall);
  5387. };
  5388. OutlinerCosts Costs(Subtarget);
  5389. const auto &SomeMFI =
  5390. *RepeatedSequenceLocs.front().getMF()->getInfo<ARMFunctionInfo>();
  5391. // Adjust costs to account for the BTI instructions.
  5392. if (SomeMFI.branchTargetEnforcement()) {
  5393. Costs.FrameDefault += 4;
  5394. Costs.FrameNoLRSave += 4;
  5395. Costs.FrameRegSave += 4;
  5396. Costs.FrameTailCall += 4;
  5397. Costs.FrameThunk += 4;
  5398. }
  5399. // Adjust costs to account for sign and authentication instructions.
  5400. if (SomeMFI.shouldSignReturnAddress(true)) {
  5401. Costs.CallDefault += 8; // +PAC instr, +AUT instr
  5402. Costs.SaveRestoreLROnStack += 8; // +PAC instr, +AUT instr
  5403. }
  5404. unsigned FrameID = MachineOutlinerDefault;
  5405. unsigned NumBytesToCreateFrame = Costs.FrameDefault;
  5406. // If the last instruction in any candidate is a terminator, then we should
  5407. // tail call all of the candidates.
  5408. if (RepeatedSequenceLocs[0].back()->isTerminator()) {
  5409. FrameID = MachineOutlinerTailCall;
  5410. NumBytesToCreateFrame = Costs.FrameTailCall;
  5411. SetCandidateCallInfo(MachineOutlinerTailCall, Costs.CallTailCall);
  5412. } else if (LastInstrOpcode == ARM::BL || LastInstrOpcode == ARM::BLX ||
  5413. LastInstrOpcode == ARM::BLX_noip || LastInstrOpcode == ARM::tBL ||
  5414. LastInstrOpcode == ARM::tBLXr ||
  5415. LastInstrOpcode == ARM::tBLXr_noip ||
  5416. LastInstrOpcode == ARM::tBLXi) {
  5417. FrameID = MachineOutlinerThunk;
  5418. NumBytesToCreateFrame = Costs.FrameThunk;
  5419. SetCandidateCallInfo(MachineOutlinerThunk, Costs.CallThunk);
  5420. } else {
  5421. // We need to decide how to emit calls + frames. We can always emit the same
  5422. // frame if we don't need to save to the stack. If we have to save to the
  5423. // stack, then we need a different frame.
  5424. unsigned NumBytesNoStackCalls = 0;
  5425. std::vector<outliner::Candidate> CandidatesWithoutStackFixups;
  5426. for (outliner::Candidate &C : RepeatedSequenceLocs) {
  5427. C.initLRU(TRI);
  5428. // LR liveness is overestimated in return blocks, unless they end with a
  5429. // tail call.
  5430. const auto Last = C.getMBB()->rbegin();
  5431. const bool LRIsAvailable =
  5432. C.getMBB()->isReturnBlock() && !Last->isCall()
  5433. ? isLRAvailable(TRI, Last,
  5434. (MachineBasicBlock::reverse_iterator)C.front())
  5435. : C.LRU.available(ARM::LR);
  5436. if (LRIsAvailable) {
  5437. FrameID = MachineOutlinerNoLRSave;
  5438. NumBytesNoStackCalls += Costs.CallNoLRSave;
  5439. C.setCallInfo(MachineOutlinerNoLRSave, Costs.CallNoLRSave);
  5440. CandidatesWithoutStackFixups.push_back(C);
  5441. }
  5442. // Is an unused register available? If so, we won't modify the stack, so
  5443. // we can outline with the same frame type as those that don't save LR.
  5444. else if (findRegisterToSaveLRTo(C)) {
  5445. FrameID = MachineOutlinerRegSave;
  5446. NumBytesNoStackCalls += Costs.CallRegSave;
  5447. C.setCallInfo(MachineOutlinerRegSave, Costs.CallRegSave);
  5448. CandidatesWithoutStackFixups.push_back(C);
  5449. }
  5450. // Is SP used in the sequence at all? If not, we don't have to modify
  5451. // the stack, so we are guaranteed to get the same frame.
  5452. else if (C.UsedInSequence.available(ARM::SP)) {
  5453. NumBytesNoStackCalls += Costs.CallDefault;
  5454. C.setCallInfo(MachineOutlinerDefault, Costs.CallDefault);
  5455. CandidatesWithoutStackFixups.push_back(C);
  5456. }
  5457. // If we outline this, we need to modify the stack. Pretend we don't
  5458. // outline this by saving all of its bytes.
  5459. else
  5460. NumBytesNoStackCalls += SequenceSize;
  5461. }
  5462. // If there are no places where we have to save LR, then note that we don't
  5463. // have to update the stack. Otherwise, give every candidate the default
  5464. // call type
  5465. if (NumBytesNoStackCalls <=
  5466. RepeatedSequenceLocs.size() * Costs.CallDefault) {
  5467. RepeatedSequenceLocs = CandidatesWithoutStackFixups;
  5468. FrameID = MachineOutlinerNoLRSave;
  5469. } else
  5470. SetCandidateCallInfo(MachineOutlinerDefault, Costs.CallDefault);
  5471. }
  5472. // Does every candidate's MBB contain a call? If so, then we might have a
  5473. // call in the range.
  5474. if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
  5475. // check if the range contains a call. These require a save + restore of
  5476. // the link register.
  5477. if (std::any_of(FirstCand.front(), FirstCand.back(),
  5478. [](const MachineInstr &MI) { return MI.isCall(); }))
  5479. NumBytesToCreateFrame += Costs.SaveRestoreLROnStack;
  5480. // Handle the last instruction separately. If it is tail call, then the
  5481. // last instruction is a call, we don't want to save + restore in this
  5482. // case. However, it could be possible that the last instruction is a
  5483. // call without it being valid to tail call this sequence. We should
  5484. // consider this as well.
  5485. else if (FrameID != MachineOutlinerThunk &&
  5486. FrameID != MachineOutlinerTailCall && FirstCand.back()->isCall())
  5487. NumBytesToCreateFrame += Costs.SaveRestoreLROnStack;
  5488. }
  5489. return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize,
  5490. NumBytesToCreateFrame, FrameID);
  5491. }
  5492. bool ARMBaseInstrInfo::checkAndUpdateStackOffset(MachineInstr *MI,
  5493. int64_t Fixup,
  5494. bool Updt) const {
  5495. int SPIdx = MI->findRegisterUseOperandIdx(ARM::SP);
  5496. unsigned AddrMode = (MI->getDesc().TSFlags & ARMII::AddrModeMask);
  5497. if (SPIdx < 0)
  5498. // No SP operand
  5499. return true;
  5500. else if (SPIdx != 1 && (AddrMode != ARMII::AddrModeT2_i8s4 || SPIdx != 2))
  5501. // If SP is not the base register we can't do much
  5502. return false;
  5503. // Stack might be involved but addressing mode doesn't handle any offset.
  5504. // Rq: AddrModeT1_[1|2|4] don't operate on SP
  5505. if (AddrMode == ARMII::AddrMode1 || // Arithmetic instructions
  5506. AddrMode == ARMII::AddrMode4 || // Load/Store Multiple
  5507. AddrMode == ARMII::AddrMode6 || // Neon Load/Store Multiple
  5508. AddrMode == ARMII::AddrModeT2_so || // SP can't be used as based register
  5509. AddrMode == ARMII::AddrModeT2_pc || // PCrel access
  5510. AddrMode == ARMII::AddrMode2 || // Used by PRE and POST indexed LD/ST
  5511. AddrMode == ARMII::AddrModeT2_i7 || // v8.1-M MVE
  5512. AddrMode == ARMII::AddrModeT2_i7s2 || // v8.1-M MVE
  5513. AddrMode == ARMII::AddrModeT2_i7s4 || // v8.1-M sys regs VLDR/VSTR
  5514. AddrMode == ARMII::AddrModeNone ||
  5515. AddrMode == ARMII::AddrModeT2_i8 || // Pre/Post inc instructions
  5516. AddrMode == ARMII::AddrModeT2_i8neg) // Always negative imm
  5517. return false;
  5518. unsigned NumOps = MI->getDesc().getNumOperands();
  5519. unsigned ImmIdx = NumOps - 3;
  5520. const MachineOperand &Offset = MI->getOperand(ImmIdx);
  5521. assert(Offset.isImm() && "Is not an immediate");
  5522. int64_t OffVal = Offset.getImm();
  5523. if (OffVal < 0)
  5524. // Don't override data if the are below SP.
  5525. return false;
  5526. unsigned NumBits = 0;
  5527. unsigned Scale = 1;
  5528. switch (AddrMode) {
  5529. case ARMII::AddrMode3:
  5530. if (ARM_AM::getAM3Op(OffVal) == ARM_AM::sub)
  5531. return false;
  5532. OffVal = ARM_AM::getAM3Offset(OffVal);
  5533. NumBits = 8;
  5534. break;
  5535. case ARMII::AddrMode5:
  5536. if (ARM_AM::getAM5Op(OffVal) == ARM_AM::sub)
  5537. return false;
  5538. OffVal = ARM_AM::getAM5Offset(OffVal);
  5539. NumBits = 8;
  5540. Scale = 4;
  5541. break;
  5542. case ARMII::AddrMode5FP16:
  5543. if (ARM_AM::getAM5FP16Op(OffVal) == ARM_AM::sub)
  5544. return false;
  5545. OffVal = ARM_AM::getAM5FP16Offset(OffVal);
  5546. NumBits = 8;
  5547. Scale = 2;
  5548. break;
  5549. case ARMII::AddrModeT2_i8pos:
  5550. NumBits = 8;
  5551. break;
  5552. case ARMII::AddrModeT2_i8s4:
  5553. // FIXME: Values are already scaled in this addressing mode.
  5554. assert((Fixup & 3) == 0 && "Can't encode this offset!");
  5555. NumBits = 10;
  5556. break;
  5557. case ARMII::AddrModeT2_ldrex:
  5558. NumBits = 8;
  5559. Scale = 4;
  5560. break;
  5561. case ARMII::AddrModeT2_i12:
  5562. case ARMII::AddrMode_i12:
  5563. NumBits = 12;
  5564. break;
  5565. case ARMII::AddrModeT1_s: // SP-relative LD/ST
  5566. NumBits = 8;
  5567. Scale = 4;
  5568. break;
  5569. default:
  5570. llvm_unreachable("Unsupported addressing mode!");
  5571. }
  5572. // Make sure the offset is encodable for instructions that scale the
  5573. // immediate.
  5574. assert(((OffVal * Scale + Fixup) & (Scale - 1)) == 0 &&
  5575. "Can't encode this offset!");
  5576. OffVal += Fixup / Scale;
  5577. unsigned Mask = (1 << NumBits) - 1;
  5578. if (OffVal <= Mask) {
  5579. if (Updt)
  5580. MI->getOperand(ImmIdx).setImm(OffVal);
  5581. return true;
  5582. }
  5583. return false;
  5584. }
  5585. void ARMBaseInstrInfo::mergeOutliningCandidateAttributes(
  5586. Function &F, std::vector<outliner::Candidate> &Candidates) const {
  5587. outliner::Candidate &C = Candidates.front();
  5588. // branch-target-enforcement is guaranteed to be consistent between all
  5589. // candidates, so we only need to look at one.
  5590. const Function &CFn = C.getMF()->getFunction();
  5591. if (CFn.hasFnAttribute("branch-target-enforcement"))
  5592. F.addFnAttr(CFn.getFnAttribute("branch-target-enforcement"));
  5593. ARMGenInstrInfo::mergeOutliningCandidateAttributes(F, Candidates);
  5594. }
  5595. bool ARMBaseInstrInfo::isFunctionSafeToOutlineFrom(
  5596. MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
  5597. const Function &F = MF.getFunction();
  5598. // Can F be deduplicated by the linker? If it can, don't outline from it.
  5599. if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
  5600. return false;
  5601. // Don't outline from functions with section markings; the program could
  5602. // expect that all the code is in the named section.
  5603. // FIXME: Allow outlining from multiple functions with the same section
  5604. // marking.
  5605. if (F.hasSection())
  5606. return false;
  5607. // FIXME: Thumb1 outlining is not handled
  5608. if (MF.getInfo<ARMFunctionInfo>()->isThumb1OnlyFunction())
  5609. return false;
  5610. // It's safe to outline from MF.
  5611. return true;
  5612. }
  5613. bool ARMBaseInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
  5614. unsigned &Flags) const {
  5615. // Check if LR is available through all of the MBB. If it's not, then set
  5616. // a flag.
  5617. assert(MBB.getParent()->getRegInfo().tracksLiveness() &&
  5618. "Suitable Machine Function for outlining must track liveness");
  5619. LiveRegUnits LRU(getRegisterInfo());
  5620. std::for_each(MBB.rbegin(), MBB.rend(),
  5621. [&LRU](MachineInstr &MI) { LRU.accumulate(MI); });
  5622. // Check if each of the unsafe registers are available...
  5623. bool R12AvailableInBlock = LRU.available(ARM::R12);
  5624. bool CPSRAvailableInBlock = LRU.available(ARM::CPSR);
  5625. // If all of these are dead (and not live out), we know we don't have to check
  5626. // them later.
  5627. if (R12AvailableInBlock && CPSRAvailableInBlock)
  5628. Flags |= MachineOutlinerMBBFlags::UnsafeRegsDead;
  5629. // Now, add the live outs to the set.
  5630. LRU.addLiveOuts(MBB);
  5631. // If any of these registers is available in the MBB, but also a live out of
  5632. // the block, then we know outlining is unsafe.
  5633. if (R12AvailableInBlock && !LRU.available(ARM::R12))
  5634. return false;
  5635. if (CPSRAvailableInBlock && !LRU.available(ARM::CPSR))
  5636. return false;
  5637. // Check if there's a call inside this MachineBasicBlock. If there is, then
  5638. // set a flag.
  5639. if (any_of(MBB, [](MachineInstr &MI) { return MI.isCall(); }))
  5640. Flags |= MachineOutlinerMBBFlags::HasCalls;
  5641. // LR liveness is overestimated in return blocks.
  5642. bool LRIsAvailable =
  5643. MBB.isReturnBlock() && !MBB.back().isCall()
  5644. ? isLRAvailable(getRegisterInfo(), MBB.rbegin(), MBB.rend())
  5645. : LRU.available(ARM::LR);
  5646. if (!LRIsAvailable)
  5647. Flags |= MachineOutlinerMBBFlags::LRUnavailableSomewhere;
  5648. return true;
  5649. }
  5650. outliner::InstrType
  5651. ARMBaseInstrInfo::getOutliningType(MachineBasicBlock::iterator &MIT,
  5652. unsigned Flags) const {
  5653. MachineInstr &MI = *MIT;
  5654. const TargetRegisterInfo *TRI = &getRegisterInfo();
  5655. // Be conservative with inline ASM
  5656. if (MI.isInlineAsm())
  5657. return outliner::InstrType::Illegal;
  5658. // Don't allow debug values to impact outlining type.
  5659. if (MI.isDebugInstr() || MI.isIndirectDebugValue())
  5660. return outliner::InstrType::Invisible;
  5661. // At this point, KILL or IMPLICIT_DEF instructions don't really tell us much
  5662. // so we can go ahead and skip over them.
  5663. if (MI.isKill() || MI.isImplicitDef())
  5664. return outliner::InstrType::Invisible;
  5665. // PIC instructions contain labels, outlining them would break offset
  5666. // computing. unsigned Opc = MI.getOpcode();
  5667. unsigned Opc = MI.getOpcode();
  5668. if (Opc == ARM::tPICADD || Opc == ARM::PICADD || Opc == ARM::PICSTR ||
  5669. Opc == ARM::PICSTRB || Opc == ARM::PICSTRH || Opc == ARM::PICLDR ||
  5670. Opc == ARM::PICLDRB || Opc == ARM::PICLDRH || Opc == ARM::PICLDRSB ||
  5671. Opc == ARM::PICLDRSH || Opc == ARM::t2LDRpci_pic ||
  5672. Opc == ARM::t2MOVi16_ga_pcrel || Opc == ARM::t2MOVTi16_ga_pcrel ||
  5673. Opc == ARM::t2MOV_ga_pcrel)
  5674. return outliner::InstrType::Illegal;
  5675. // Be conservative with ARMv8.1 MVE instructions.
  5676. if (Opc == ARM::t2BF_LabelPseudo || Opc == ARM::t2DoLoopStart ||
  5677. Opc == ARM::t2DoLoopStartTP || Opc == ARM::t2WhileLoopStart ||
  5678. Opc == ARM::t2WhileLoopStartLR || Opc == ARM::t2WhileLoopStartTP ||
  5679. Opc == ARM::t2LoopDec || Opc == ARM::t2LoopEnd ||
  5680. Opc == ARM::t2LoopEndDec)
  5681. return outliner::InstrType::Illegal;
  5682. const MCInstrDesc &MCID = MI.getDesc();
  5683. uint64_t MIFlags = MCID.TSFlags;
  5684. if ((MIFlags & ARMII::DomainMask) == ARMII::DomainMVE)
  5685. return outliner::InstrType::Illegal;
  5686. // Is this a terminator for a basic block?
  5687. if (MI.isTerminator()) {
  5688. // Don't outline if the branch is not unconditional.
  5689. if (isPredicated(MI))
  5690. return outliner::InstrType::Illegal;
  5691. // Is this the end of a function?
  5692. if (MI.getParent()->succ_empty())
  5693. return outliner::InstrType::Legal;
  5694. // It's not, so don't outline it.
  5695. return outliner::InstrType::Illegal;
  5696. }
  5697. // Make sure none of the operands are un-outlinable.
  5698. for (const MachineOperand &MOP : MI.operands()) {
  5699. if (MOP.isCPI() || MOP.isJTI() || MOP.isCFIIndex() || MOP.isFI() ||
  5700. MOP.isTargetIndex())
  5701. return outliner::InstrType::Illegal;
  5702. }
  5703. // Don't outline if link register or program counter value are used.
  5704. if (MI.readsRegister(ARM::LR, TRI) || MI.readsRegister(ARM::PC, TRI))
  5705. return outliner::InstrType::Illegal;
  5706. if (MI.isCall()) {
  5707. // Get the function associated with the call. Look at each operand and find
  5708. // the one that represents the calle and get its name.
  5709. const Function *Callee = nullptr;
  5710. for (const MachineOperand &MOP : MI.operands()) {
  5711. if (MOP.isGlobal()) {
  5712. Callee = dyn_cast<Function>(MOP.getGlobal());
  5713. break;
  5714. }
  5715. }
  5716. // Dont't outline calls to "mcount" like functions, in particular Linux
  5717. // kernel function tracing relies on it.
  5718. if (Callee &&
  5719. (Callee->getName() == "\01__gnu_mcount_nc" ||
  5720. Callee->getName() == "\01mcount" || Callee->getName() == "__mcount"))
  5721. return outliner::InstrType::Illegal;
  5722. // If we don't know anything about the callee, assume it depends on the
  5723. // stack layout of the caller. In that case, it's only legal to outline
  5724. // as a tail-call. Explicitly list the call instructions we know about so
  5725. // we don't get unexpected results with call pseudo-instructions.
  5726. auto UnknownCallOutlineType = outliner::InstrType::Illegal;
  5727. if (Opc == ARM::BL || Opc == ARM::tBL || Opc == ARM::BLX ||
  5728. Opc == ARM::BLX_noip || Opc == ARM::tBLXr || Opc == ARM::tBLXr_noip ||
  5729. Opc == ARM::tBLXi)
  5730. UnknownCallOutlineType = outliner::InstrType::LegalTerminator;
  5731. if (!Callee)
  5732. return UnknownCallOutlineType;
  5733. // We have a function we have information about. Check if it's something we
  5734. // can safely outline.
  5735. MachineFunction *MF = MI.getParent()->getParent();
  5736. MachineFunction *CalleeMF = MF->getMMI().getMachineFunction(*Callee);
  5737. // We don't know what's going on with the callee at all. Don't touch it.
  5738. if (!CalleeMF)
  5739. return UnknownCallOutlineType;
  5740. // Check if we know anything about the callee saves on the function. If we
  5741. // don't, then don't touch it, since that implies that we haven't computed
  5742. // anything about its stack frame yet.
  5743. MachineFrameInfo &MFI = CalleeMF->getFrameInfo();
  5744. if (!MFI.isCalleeSavedInfoValid() || MFI.getStackSize() > 0 ||
  5745. MFI.getNumObjects() > 0)
  5746. return UnknownCallOutlineType;
  5747. // At this point, we can say that CalleeMF ought to not pass anything on the
  5748. // stack. Therefore, we can outline it.
  5749. return outliner::InstrType::Legal;
  5750. }
  5751. // Since calls are handled, don't touch LR or PC
  5752. if (MI.modifiesRegister(ARM::LR, TRI) || MI.modifiesRegister(ARM::PC, TRI))
  5753. return outliner::InstrType::Illegal;
  5754. // Does this use the stack?
  5755. if (MI.modifiesRegister(ARM::SP, TRI) || MI.readsRegister(ARM::SP, TRI)) {
  5756. // True if there is no chance that any outlined candidate from this range
  5757. // could require stack fixups. That is, both
  5758. // * LR is available in the range (No save/restore around call)
  5759. // * The range doesn't include calls (No save/restore in outlined frame)
  5760. // are true.
  5761. // These conditions also ensure correctness of the return address
  5762. // authentication - we insert sign and authentication instructions only if
  5763. // we save/restore LR on stack, but then this condition ensures that the
  5764. // outlined range does not modify the SP, therefore the SP value used for
  5765. // signing is the same as the one used for authentication.
  5766. // FIXME: This is very restrictive; the flags check the whole block,
  5767. // not just the bit we will try to outline.
  5768. bool MightNeedStackFixUp =
  5769. (Flags & (MachineOutlinerMBBFlags::LRUnavailableSomewhere |
  5770. MachineOutlinerMBBFlags::HasCalls));
  5771. if (!MightNeedStackFixUp)
  5772. return outliner::InstrType::Legal;
  5773. // Any modification of SP will break our code to save/restore LR.
  5774. // FIXME: We could handle some instructions which add a constant offset to
  5775. // SP, with a bit more work.
  5776. if (MI.modifiesRegister(ARM::SP, TRI))
  5777. return outliner::InstrType::Illegal;
  5778. // At this point, we have a stack instruction that we might need to fix up.
  5779. // up. We'll handle it if it's a load or store.
  5780. if (checkAndUpdateStackOffset(&MI, Subtarget.getStackAlignment().value(),
  5781. false))
  5782. return outliner::InstrType::Legal;
  5783. // We can't fix it up, so don't outline it.
  5784. return outliner::InstrType::Illegal;
  5785. }
  5786. // Be conservative with IT blocks.
  5787. if (MI.readsRegister(ARM::ITSTATE, TRI) ||
  5788. MI.modifiesRegister(ARM::ITSTATE, TRI))
  5789. return outliner::InstrType::Illegal;
  5790. // Don't outline positions.
  5791. if (MI.isPosition())
  5792. return outliner::InstrType::Illegal;
  5793. return outliner::InstrType::Legal;
  5794. }
  5795. void ARMBaseInstrInfo::fixupPostOutline(MachineBasicBlock &MBB) const {
  5796. for (MachineInstr &MI : MBB) {
  5797. checkAndUpdateStackOffset(&MI, Subtarget.getStackAlignment().value(), true);
  5798. }
  5799. }
  5800. void ARMBaseInstrInfo::saveLROnStack(MachineBasicBlock &MBB,
  5801. MachineBasicBlock::iterator It, bool CFI,
  5802. bool Auth) const {
  5803. int Align = std::max(Subtarget.getStackAlignment().value(), uint64_t(8));
  5804. assert(Align >= 8 && Align <= 256);
  5805. if (Auth) {
  5806. assert(Subtarget.isThumb2());
  5807. // Compute PAC in R12. Outlining ensures R12 is dead across the outlined
  5808. // sequence.
  5809. BuildMI(MBB, It, DebugLoc(), get(ARM::t2PAC))
  5810. .setMIFlags(MachineInstr::FrameSetup);
  5811. BuildMI(MBB, It, DebugLoc(), get(ARM::t2STRD_PRE), ARM::SP)
  5812. .addReg(ARM::R12, RegState::Kill)
  5813. .addReg(ARM::LR, RegState::Kill)
  5814. .addReg(ARM::SP)
  5815. .addImm(-Align)
  5816. .add(predOps(ARMCC::AL))
  5817. .setMIFlags(MachineInstr::FrameSetup);
  5818. } else {
  5819. unsigned Opc = Subtarget.isThumb() ? ARM::t2STR_PRE : ARM::STR_PRE_IMM;
  5820. BuildMI(MBB, It, DebugLoc(), get(Opc), ARM::SP)
  5821. .addReg(ARM::LR, RegState::Kill)
  5822. .addReg(ARM::SP)
  5823. .addImm(-Align)
  5824. .add(predOps(ARMCC::AL))
  5825. .setMIFlags(MachineInstr::FrameSetup);
  5826. }
  5827. if (!CFI)
  5828. return;
  5829. MachineFunction &MF = *MBB.getParent();
  5830. // Add a CFI, saying CFA is offset by Align bytes from SP.
  5831. int64_t StackPosEntry =
  5832. MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Align));
  5833. BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
  5834. .addCFIIndex(StackPosEntry)
  5835. .setMIFlags(MachineInstr::FrameSetup);
  5836. // Add a CFI saying that the LR that we want to find is now higher than
  5837. // before.
  5838. int LROffset = Auth ? Align - 4 : Align;
  5839. const MCRegisterInfo *MRI = Subtarget.getRegisterInfo();
  5840. unsigned DwarfLR = MRI->getDwarfRegNum(ARM::LR, true);
  5841. int64_t LRPosEntry = MF.addFrameInst(
  5842. MCCFIInstruction::createOffset(nullptr, DwarfLR, -LROffset));
  5843. BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
  5844. .addCFIIndex(LRPosEntry)
  5845. .setMIFlags(MachineInstr::FrameSetup);
  5846. if (Auth) {
  5847. // Add a CFI for the location of the return adddress PAC.
  5848. unsigned DwarfRAC = MRI->getDwarfRegNum(ARM::RA_AUTH_CODE, true);
  5849. int64_t RACPosEntry = MF.addFrameInst(
  5850. MCCFIInstruction::createOffset(nullptr, DwarfRAC, -Align));
  5851. BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
  5852. .addCFIIndex(RACPosEntry)
  5853. .setMIFlags(MachineInstr::FrameSetup);
  5854. }
  5855. }
  5856. void ARMBaseInstrInfo::emitCFIForLRSaveToReg(MachineBasicBlock &MBB,
  5857. MachineBasicBlock::iterator It,
  5858. Register Reg) const {
  5859. MachineFunction &MF = *MBB.getParent();
  5860. const MCRegisterInfo *MRI = Subtarget.getRegisterInfo();
  5861. unsigned DwarfLR = MRI->getDwarfRegNum(ARM::LR, true);
  5862. unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
  5863. int64_t LRPosEntry = MF.addFrameInst(
  5864. MCCFIInstruction::createRegister(nullptr, DwarfLR, DwarfReg));
  5865. BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
  5866. .addCFIIndex(LRPosEntry)
  5867. .setMIFlags(MachineInstr::FrameSetup);
  5868. }
  5869. void ARMBaseInstrInfo::restoreLRFromStack(MachineBasicBlock &MBB,
  5870. MachineBasicBlock::iterator It,
  5871. bool CFI, bool Auth) const {
  5872. int Align = Subtarget.getStackAlignment().value();
  5873. if (Auth) {
  5874. assert(Subtarget.isThumb2());
  5875. // Restore return address PAC and LR.
  5876. BuildMI(MBB, It, DebugLoc(), get(ARM::t2LDRD_POST))
  5877. .addReg(ARM::R12, RegState::Define)
  5878. .addReg(ARM::LR, RegState::Define)
  5879. .addReg(ARM::SP, RegState::Define)
  5880. .addReg(ARM::SP)
  5881. .addImm(Align)
  5882. .add(predOps(ARMCC::AL))
  5883. .setMIFlags(MachineInstr::FrameDestroy);
  5884. // LR authentication is after the CFI instructions, below.
  5885. } else {
  5886. unsigned Opc = Subtarget.isThumb() ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
  5887. MachineInstrBuilder MIB = BuildMI(MBB, It, DebugLoc(), get(Opc), ARM::LR)
  5888. .addReg(ARM::SP, RegState::Define)
  5889. .addReg(ARM::SP);
  5890. if (!Subtarget.isThumb())
  5891. MIB.addReg(0);
  5892. MIB.addImm(Subtarget.getStackAlignment().value())
  5893. .add(predOps(ARMCC::AL))
  5894. .setMIFlags(MachineInstr::FrameDestroy);
  5895. }
  5896. if (CFI) {
  5897. // Now stack has moved back up...
  5898. MachineFunction &MF = *MBB.getParent();
  5899. const MCRegisterInfo *MRI = Subtarget.getRegisterInfo();
  5900. unsigned DwarfLR = MRI->getDwarfRegNum(ARM::LR, true);
  5901. int64_t StackPosEntry =
  5902. MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 0));
  5903. BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
  5904. .addCFIIndex(StackPosEntry)
  5905. .setMIFlags(MachineInstr::FrameDestroy);
  5906. // ... and we have restored LR.
  5907. int64_t LRPosEntry =
  5908. MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, DwarfLR));
  5909. BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
  5910. .addCFIIndex(LRPosEntry)
  5911. .setMIFlags(MachineInstr::FrameDestroy);
  5912. if (Auth) {
  5913. unsigned DwarfRAC = MRI->getDwarfRegNum(ARM::RA_AUTH_CODE, true);
  5914. int64_t Entry =
  5915. MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, DwarfRAC));
  5916. BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
  5917. .addCFIIndex(Entry)
  5918. .setMIFlags(MachineInstr::FrameDestroy);
  5919. }
  5920. }
  5921. if (Auth)
  5922. BuildMI(MBB, It, DebugLoc(), get(ARM::t2AUT));
  5923. }
  5924. void ARMBaseInstrInfo::emitCFIForLRRestoreFromReg(
  5925. MachineBasicBlock &MBB, MachineBasicBlock::iterator It) const {
  5926. MachineFunction &MF = *MBB.getParent();
  5927. const MCRegisterInfo *MRI = Subtarget.getRegisterInfo();
  5928. unsigned DwarfLR = MRI->getDwarfRegNum(ARM::LR, true);
  5929. int64_t LRPosEntry =
  5930. MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, DwarfLR));
  5931. BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
  5932. .addCFIIndex(LRPosEntry)
  5933. .setMIFlags(MachineInstr::FrameDestroy);
  5934. }
  5935. void ARMBaseInstrInfo::buildOutlinedFrame(
  5936. MachineBasicBlock &MBB, MachineFunction &MF,
  5937. const outliner::OutlinedFunction &OF) const {
  5938. // For thunk outlining, rewrite the last instruction from a call to a
  5939. // tail-call.
  5940. if (OF.FrameConstructionID == MachineOutlinerThunk) {
  5941. MachineInstr *Call = &*--MBB.instr_end();
  5942. bool isThumb = Subtarget.isThumb();
  5943. unsigned FuncOp = isThumb ? 2 : 0;
  5944. unsigned Opc = Call->getOperand(FuncOp).isReg()
  5945. ? isThumb ? ARM::tTAILJMPr : ARM::TAILJMPr
  5946. : isThumb ? Subtarget.isTargetMachO() ? ARM::tTAILJMPd
  5947. : ARM::tTAILJMPdND
  5948. : ARM::TAILJMPd;
  5949. MachineInstrBuilder MIB = BuildMI(MBB, MBB.end(), DebugLoc(), get(Opc))
  5950. .add(Call->getOperand(FuncOp));
  5951. if (isThumb && !Call->getOperand(FuncOp).isReg())
  5952. MIB.add(predOps(ARMCC::AL));
  5953. Call->eraseFromParent();
  5954. }
  5955. // Is there a call in the outlined range?
  5956. auto IsNonTailCall = [](MachineInstr &MI) {
  5957. return MI.isCall() && !MI.isReturn();
  5958. };
  5959. if (llvm::any_of(MBB.instrs(), IsNonTailCall)) {
  5960. MachineBasicBlock::iterator It = MBB.begin();
  5961. MachineBasicBlock::iterator Et = MBB.end();
  5962. if (OF.FrameConstructionID == MachineOutlinerTailCall ||
  5963. OF.FrameConstructionID == MachineOutlinerThunk)
  5964. Et = std::prev(MBB.end());
  5965. // We have to save and restore LR, we need to add it to the liveins if it
  5966. // is not already part of the set. This is suffient since outlined
  5967. // functions only have one block.
  5968. if (!MBB.isLiveIn(ARM::LR))
  5969. MBB.addLiveIn(ARM::LR);
  5970. // Insert a save before the outlined region
  5971. bool Auth = OF.Candidates.front()
  5972. .getMF()
  5973. ->getInfo<ARMFunctionInfo>()
  5974. ->shouldSignReturnAddress(true);
  5975. saveLROnStack(MBB, It, true, Auth);
  5976. // Fix up the instructions in the range, since we're going to modify the
  5977. // stack.
  5978. assert(OF.FrameConstructionID != MachineOutlinerDefault &&
  5979. "Can only fix up stack references once");
  5980. fixupPostOutline(MBB);
  5981. // Insert a restore before the terminator for the function. Restore LR.
  5982. restoreLRFromStack(MBB, Et, true, Auth);
  5983. }
  5984. // If this is a tail call outlined function, then there's already a return.
  5985. if (OF.FrameConstructionID == MachineOutlinerTailCall ||
  5986. OF.FrameConstructionID == MachineOutlinerThunk)
  5987. return;
  5988. // Here we have to insert the return ourselves. Get the correct opcode from
  5989. // current feature set.
  5990. BuildMI(MBB, MBB.end(), DebugLoc(), get(Subtarget.getReturnOpcode()))
  5991. .add(predOps(ARMCC::AL));
  5992. // Did we have to modify the stack by saving the link register?
  5993. if (OF.FrameConstructionID != MachineOutlinerDefault &&
  5994. OF.Candidates[0].CallConstructionID != MachineOutlinerDefault)
  5995. return;
  5996. // We modified the stack.
  5997. // Walk over the basic block and fix up all the stack accesses.
  5998. fixupPostOutline(MBB);
  5999. }
  6000. MachineBasicBlock::iterator ARMBaseInstrInfo::insertOutlinedCall(
  6001. Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
  6002. MachineFunction &MF, const outliner::Candidate &C) const {
  6003. MachineInstrBuilder MIB;
  6004. MachineBasicBlock::iterator CallPt;
  6005. unsigned Opc;
  6006. bool isThumb = Subtarget.isThumb();
  6007. // Are we tail calling?
  6008. if (C.CallConstructionID == MachineOutlinerTailCall) {
  6009. // If yes, then we can just branch to the label.
  6010. Opc = isThumb
  6011. ? Subtarget.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND
  6012. : ARM::TAILJMPd;
  6013. MIB = BuildMI(MF, DebugLoc(), get(Opc))
  6014. .addGlobalAddress(M.getNamedValue(MF.getName()));
  6015. if (isThumb)
  6016. MIB.add(predOps(ARMCC::AL));
  6017. It = MBB.insert(It, MIB);
  6018. return It;
  6019. }
  6020. // Create the call instruction.
  6021. Opc = isThumb ? ARM::tBL : ARM::BL;
  6022. MachineInstrBuilder CallMIB = BuildMI(MF, DebugLoc(), get(Opc));
  6023. if (isThumb)
  6024. CallMIB.add(predOps(ARMCC::AL));
  6025. CallMIB.addGlobalAddress(M.getNamedValue(MF.getName()));
  6026. if (C.CallConstructionID == MachineOutlinerNoLRSave ||
  6027. C.CallConstructionID == MachineOutlinerThunk) {
  6028. // No, so just insert the call.
  6029. It = MBB.insert(It, CallMIB);
  6030. return It;
  6031. }
  6032. const ARMFunctionInfo &AFI = *C.getMF()->getInfo<ARMFunctionInfo>();
  6033. // Can we save to a register?
  6034. if (C.CallConstructionID == MachineOutlinerRegSave) {
  6035. unsigned Reg = findRegisterToSaveLRTo(C);
  6036. assert(Reg != 0 && "No callee-saved register available?");
  6037. // Save and restore LR from that register.
  6038. copyPhysReg(MBB, It, DebugLoc(), Reg, ARM::LR, true);
  6039. if (!AFI.isLRSpilled())
  6040. emitCFIForLRSaveToReg(MBB, It, Reg);
  6041. CallPt = MBB.insert(It, CallMIB);
  6042. copyPhysReg(MBB, It, DebugLoc(), ARM::LR, Reg, true);
  6043. if (!AFI.isLRSpilled())
  6044. emitCFIForLRRestoreFromReg(MBB, It);
  6045. It--;
  6046. return CallPt;
  6047. }
  6048. // We have the default case. Save and restore from SP.
  6049. if (!MBB.isLiveIn(ARM::LR))
  6050. MBB.addLiveIn(ARM::LR);
  6051. bool Auth = !AFI.isLRSpilled() && AFI.shouldSignReturnAddress(true);
  6052. saveLROnStack(MBB, It, !AFI.isLRSpilled(), Auth);
  6053. CallPt = MBB.insert(It, CallMIB);
  6054. restoreLRFromStack(MBB, It, !AFI.isLRSpilled(), Auth);
  6055. It--;
  6056. return CallPt;
  6057. }
  6058. bool ARMBaseInstrInfo::shouldOutlineFromFunctionByDefault(
  6059. MachineFunction &MF) const {
  6060. return Subtarget.isMClass() && MF.getFunction().hasMinSize();
  6061. }
  6062. bool ARMBaseInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI,
  6063. AAResults *AA) const {
  6064. // Try hard to rematerialize any VCTPs because if we spill P0, it will block
  6065. // the tail predication conversion. This means that the element count
  6066. // register has to be live for longer, but that has to be better than
  6067. // spill/restore and VPT predication.
  6068. return isVCTP(&MI) && !isPredicated(MI);
  6069. }
  6070. unsigned llvm::getBLXOpcode(const MachineFunction &MF) {
  6071. return (MF.getSubtarget<ARMSubtarget>().hardenSlsBlr()) ? ARM::BLX_noip
  6072. : ARM::BLX;
  6073. }
  6074. unsigned llvm::gettBLXrOpcode(const MachineFunction &MF) {
  6075. return (MF.getSubtarget<ARMSubtarget>().hardenSlsBlr()) ? ARM::tBLXr_noip
  6076. : ARM::tBLXr;
  6077. }
  6078. unsigned llvm::getBLXpredOpcode(const MachineFunction &MF) {
  6079. return (MF.getSubtarget<ARMSubtarget>().hardenSlsBlr()) ? ARM::BLX_pred_noip
  6080. : ARM::BLX_pred;
  6081. }