sh.c 431 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479
  1. /* Output routines for GCC for Renesas / SuperH SH.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Contributed by Steve Chamberlain (sac@cygnus.com).
  4. Improved by Jim Wilson (wilson@cygnus.com).
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include <sstream>
  18. #include <vector>
  19. #include "config.h"
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include "tm.h"
  23. #include "insn-config.h"
  24. #include "rtl.h"
  25. #include "hash-set.h"
  26. #include "machmode.h"
  27. #include "vec.h"
  28. #include "double-int.h"
  29. #include "input.h"
  30. #include "alias.h"
  31. #include "symtab.h"
  32. #include "wide-int.h"
  33. #include "inchash.h"
  34. #include "tree.h"
  35. #include "fold-const.h"
  36. #include "stringpool.h"
  37. #include "stor-layout.h"
  38. #include "calls.h"
  39. #include "varasm.h"
  40. #include "flags.h"
  41. #include "hashtab.h"
  42. #include "hard-reg-set.h"
  43. #include "function.h"
  44. #include "statistics.h"
  45. #include "real.h"
  46. #include "fixed-value.h"
  47. #include "expmed.h"
  48. #include "dojump.h"
  49. #include "explow.h"
  50. #include "emit-rtl.h"
  51. #include "stmt.h"
  52. #include "expr.h"
  53. #include "insn-codes.h"
  54. #include "optabs.h"
  55. #include "reload.h"
  56. #include "regs.h"
  57. #include "output.h"
  58. #include "insn-attr.h"
  59. #include "diagnostic-core.h"
  60. #include "recog.h"
  61. #include "dwarf2.h"
  62. #include "tm_p.h"
  63. #include "target.h"
  64. #include "target-def.h"
  65. #include "langhooks.h"
  66. #include "predict.h"
  67. #include "dominance.h"
  68. #include "cfg.h"
  69. #include "cfgrtl.h"
  70. #include "cfganal.h"
  71. #include "lcm.h"
  72. #include "cfgbuild.h"
  73. #include "cfgcleanup.h"
  74. #include "basic-block.h"
  75. #include "df.h"
  76. #include "intl.h"
  77. #include "sched-int.h"
  78. #include "params.h"
  79. #include "ggc.h"
  80. #include "hash-table.h"
  81. #include "tree-ssa-alias.h"
  82. #include "internal-fn.h"
  83. #include "gimple-fold.h"
  84. #include "tree-eh.h"
  85. #include "gimple-expr.h"
  86. #include "is-a.h"
  87. #include "gimple.h"
  88. #include "gimplify.h"
  89. #include "cfgloop.h"
  90. #include "alloc-pool.h"
  91. #include "tm-constrs.h"
  92. #include "opts.h"
  93. #include "tree-pass.h"
  94. #include "pass_manager.h"
  95. #include "context.h"
  96. #include "builtins.h"
  97. #include "rtl-iter.h"
  98. int code_for_indirect_jump_scratch = CODE_FOR_indirect_jump_scratch;
  99. /* These are some macros to abstract register modes. */
  100. #define CONST_OK_FOR_I10(VALUE) (((HOST_WIDE_INT)(VALUE)) >= -512 \
  101. && ((HOST_WIDE_INT)(VALUE)) <= 511)
  102. #define CONST_OK_FOR_ADD(size) \
  103. (TARGET_SHMEDIA ? CONST_OK_FOR_I10 (size) : CONST_OK_FOR_I08 (size))
  104. #define GEN_MOV (*(TARGET_SHMEDIA64 ? gen_movdi : gen_movsi))
  105. #define GEN_ADD3 (*(TARGET_SHMEDIA64 ? gen_adddi3 : gen_addsi3))
  106. #define GEN_SUB3 (*(TARGET_SHMEDIA64 ? gen_subdi3 : gen_subsi3))
  107. /* Used to simplify the logic below. Find the attributes wherever
  108. they may be. */
  109. #define SH_ATTRIBUTES(decl) \
  110. (TYPE_P (decl)) ? TYPE_ATTRIBUTES (decl) \
  111. : DECL_ATTRIBUTES (decl) \
  112. ? (DECL_ATTRIBUTES (decl)) \
  113. : TYPE_ATTRIBUTES (TREE_TYPE (decl))
  114. /* Set to 1 by expand_prologue() when the function is an interrupt handler. */
  115. int current_function_interrupt;
  116. tree sh_deferred_function_attributes;
  117. tree *sh_deferred_function_attributes_tail = &sh_deferred_function_attributes;
  118. /* Global variables for machine-dependent things. */
  119. /* Which cpu are we scheduling for. */
  120. enum processor_type sh_cpu;
  121. /* Definitions used in ready queue reordering for first scheduling pass. */
  122. /* Reg weights arrays for modes SFmode and SImode, indexed by insn LUID. */
  123. static short *regmode_weight[2];
  124. /* Total SFmode and SImode weights of scheduled insns. */
  125. static int curr_regmode_pressure[2];
  126. /* Number of r0 life regions. */
  127. static int r0_life_regions;
  128. /* If true, skip cycles for Q -> R movement. */
  129. static int skip_cycles = 0;
  130. /* Cached value of can_issue_more. This is cached in sh_variable_issue hook
  131. and returned from sh_reorder2. */
  132. static short cached_can_issue_more;
  133. /* Unique number for UNSPEC_BBR pattern. */
  134. static unsigned int unspec_bbr_uid = 1;
  135. /* Provides the class number of the smallest class containing
  136. reg number. */
  137. enum reg_class regno_reg_class[FIRST_PSEUDO_REGISTER] =
  138. {
  139. R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  140. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  141. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  142. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  143. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  144. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  145. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  146. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  147. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  148. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  149. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  150. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  151. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  152. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  153. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  154. GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  155. FP0_REGS,FP_REGS, FP_REGS, FP_REGS,
  156. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  157. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  158. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  159. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  160. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  161. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  162. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  163. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  164. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  165. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  166. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  167. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  168. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  169. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  170. FP_REGS, FP_REGS, FP_REGS, FP_REGS,
  171. TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
  172. TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
  173. DF_REGS, DF_REGS, DF_REGS, DF_REGS,
  174. DF_REGS, DF_REGS, DF_REGS, DF_REGS,
  175. NO_REGS, GENERAL_REGS, PR_REGS, T_REGS,
  176. MAC_REGS, MAC_REGS, FPUL_REGS, FPSCR_REGS,
  177. GENERAL_REGS, GENERAL_REGS,
  178. };
  179. char sh_register_names[FIRST_PSEUDO_REGISTER] \
  180. [MAX_REGISTER_NAME_LENGTH + 1] = SH_REGISTER_NAMES_INITIALIZER;
  181. char sh_additional_register_names[ADDREGNAMES_SIZE] \
  182. [MAX_ADDITIONAL_REGISTER_NAME_LENGTH + 1]
  183. = SH_ADDITIONAL_REGISTER_NAMES_INITIALIZER;
  184. int assembler_dialect;
  185. static bool shmedia_space_reserved_for_target_registers;
  186. static void split_branches (rtx_insn *);
  187. static int branch_dest (rtx);
  188. static void print_slot (rtx_sequence *);
  189. static rtx_code_label *add_constant (rtx, machine_mode, rtx);
  190. static void dump_table (rtx_insn *, rtx_insn *);
  191. static bool broken_move (rtx_insn *);
  192. static bool mova_p (rtx_insn *);
  193. static rtx_insn *find_barrier (int, rtx_insn *, rtx_insn *);
  194. static bool noncall_uses_reg (rtx, rtx_insn *, rtx *);
  195. static rtx_insn *gen_block_redirect (rtx_insn *, int, int);
  196. static void sh_reorg (void);
  197. static void sh_option_override (void);
  198. static void output_stack_adjust (int, rtx, int, HARD_REG_SET *, bool);
  199. static rtx_insn *frame_insn (rtx);
  200. static rtx push (int);
  201. static void pop (int);
  202. static void push_regs (HARD_REG_SET *, int);
  203. static int calc_live_regs (HARD_REG_SET *);
  204. static HOST_WIDE_INT rounded_frame_size (int);
  205. static bool sh_frame_pointer_required (void);
  206. static void sh_emit_mode_set (int, int, int, HARD_REG_SET);
  207. static int sh_mode_needed (int, rtx_insn *);
  208. static int sh_mode_after (int, int, rtx_insn *);
  209. static int sh_mode_entry (int);
  210. static int sh_mode_exit (int);
  211. static int sh_mode_priority (int entity, int n);
  212. static bool sh_lra_p (void);
  213. static rtx mark_constant_pool_use (rtx);
  214. static tree sh_handle_interrupt_handler_attribute (tree *, tree, tree,
  215. int, bool *);
  216. static tree sh_handle_resbank_handler_attribute (tree *, tree,
  217. tree, int, bool *);
  218. static tree sh2a_handle_function_vector_handler_attribute (tree *, tree,
  219. tree, int, bool *);
  220. static tree sh_handle_sp_switch_attribute (tree *, tree, tree, int, bool *);
  221. static tree sh_handle_trap_exit_attribute (tree *, tree, tree, int, bool *);
  222. static tree sh_handle_renesas_attribute (tree *, tree, tree, int, bool *);
  223. static void sh_print_operand (FILE *, rtx, int);
  224. static void sh_print_operand_address (FILE *, rtx);
  225. static bool sh_print_operand_punct_valid_p (unsigned char code);
  226. static bool sh_asm_output_addr_const_extra (FILE *file, rtx x);
  227. static void sh_output_function_epilogue (FILE *, HOST_WIDE_INT);
  228. static void sh_insert_attributes (tree, tree *);
  229. static const char *sh_check_pch_target_flags (int);
  230. static int sh_register_move_cost (machine_mode, reg_class_t, reg_class_t);
  231. static int sh_adjust_cost (rtx_insn *, rtx, rtx_insn *, int);
  232. static int sh_issue_rate (void);
  233. static int sh_dfa_new_cycle (FILE *, int, rtx_insn *, int, int, int *sort_p);
  234. static short find_set_regmode_weight (rtx, machine_mode);
  235. static short find_insn_regmode_weight (rtx, machine_mode);
  236. static void find_regmode_weight (basic_block, machine_mode);
  237. static int find_r0_life_regions (basic_block);
  238. static void sh_md_init_global (FILE *, int, int);
  239. static void sh_md_finish_global (FILE *, int);
  240. static int rank_for_reorder (const void *, const void *);
  241. static void swap_reorder (rtx_insn **, int);
  242. static void ready_reorder (rtx_insn **, int);
  243. static bool high_pressure (machine_mode);
  244. static int sh_reorder (FILE *, int, rtx_insn **, int *, int);
  245. static int sh_reorder2 (FILE *, int, rtx_insn **, int *, int);
  246. static void sh_md_init (FILE *, int, int);
  247. static int sh_variable_issue (FILE *, int, rtx_insn *, int);
  248. static bool sh_function_ok_for_sibcall (tree, tree);
  249. static bool sh_cannot_modify_jumps_p (void);
  250. static bool sh_can_follow_jump (const rtx_insn *, const rtx_insn *);
  251. static reg_class_t sh_target_reg_class (void);
  252. static bool sh_optimize_target_register_callee_saved (bool);
  253. static bool sh_ms_bitfield_layout_p (const_tree);
  254. static void sh_init_builtins (void);
  255. static tree sh_builtin_decl (unsigned, bool);
  256. static rtx sh_expand_builtin (tree, rtx, rtx, machine_mode, int);
  257. static void sh_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
  258. HOST_WIDE_INT, tree);
  259. static void sh_file_start (void);
  260. static bool flow_dependent_p (rtx, rtx);
  261. static void flow_dependent_p_1 (rtx, const_rtx, void *);
  262. static int shiftcosts (rtx);
  263. static int and_xor_ior_costs (rtx, int);
  264. static int addsubcosts (rtx);
  265. static int multcosts (rtx);
  266. static bool unspec_caller_rtx_p (rtx);
  267. static bool sh_cannot_copy_insn_p (rtx_insn *);
  268. static bool sh_rtx_costs (rtx, int, int, int, int *, bool);
  269. static int sh_address_cost (rtx, machine_mode, addr_space_t, bool);
  270. static int sh_pr_n_sets (void);
  271. static rtx sh_allocate_initial_value (rtx);
  272. static reg_class_t sh_preferred_reload_class (rtx, reg_class_t);
  273. static reg_class_t sh_secondary_reload (bool, rtx, reg_class_t,
  274. machine_mode,
  275. struct secondary_reload_info *);
  276. static bool sh_legitimate_address_p (machine_mode, rtx, bool);
  277. static rtx sh_legitimize_address (rtx, rtx, machine_mode);
  278. static rtx sh_delegitimize_address (rtx);
  279. static bool sh_cannot_substitute_mem_equiv_p (rtx);
  280. static bool sh_legitimize_address_displacement (rtx *, rtx *, machine_mode);
  281. static int shmedia_target_regs_stack_space (HARD_REG_SET *);
  282. static int shmedia_reserve_space_for_target_registers_p (int, HARD_REG_SET *);
  283. static int shmedia_target_regs_stack_adjust (HARD_REG_SET *);
  284. static int scavenge_reg (HARD_REG_SET *s);
  285. struct save_schedule_s;
  286. static struct save_entry_s *sh5_schedule_saves (HARD_REG_SET *,
  287. struct save_schedule_s *, int);
  288. static rtx sh_struct_value_rtx (tree, int);
  289. static rtx sh_function_value (const_tree, const_tree, bool);
  290. static bool sh_function_value_regno_p (const unsigned int);
  291. static rtx sh_libcall_value (machine_mode, const_rtx);
  292. static bool sh_return_in_memory (const_tree, const_tree);
  293. static rtx sh_builtin_saveregs (void);
  294. static void sh_setup_incoming_varargs (cumulative_args_t, machine_mode,
  295. tree, int *, int);
  296. static bool sh_strict_argument_naming (cumulative_args_t);
  297. static bool sh_pretend_outgoing_varargs_named (cumulative_args_t);
  298. static void sh_atomic_assign_expand_fenv (tree *, tree *, tree *);
  299. static tree sh_build_builtin_va_list (void);
  300. static void sh_va_start (tree, rtx);
  301. static tree sh_gimplify_va_arg_expr (tree, tree, gimple_seq *, gimple_seq *);
  302. static bool sh_promote_prototypes (const_tree);
  303. static machine_mode sh_promote_function_mode (const_tree type,
  304. machine_mode,
  305. int *punsignedp,
  306. const_tree funtype,
  307. int for_return);
  308. static bool sh_pass_by_reference (cumulative_args_t, machine_mode,
  309. const_tree, bool);
  310. static bool sh_callee_copies (cumulative_args_t, machine_mode,
  311. const_tree, bool);
  312. static int sh_arg_partial_bytes (cumulative_args_t, machine_mode,
  313. tree, bool);
  314. static void sh_function_arg_advance (cumulative_args_t, machine_mode,
  315. const_tree, bool);
  316. static rtx sh_function_arg (cumulative_args_t, machine_mode,
  317. const_tree, bool);
  318. static bool sh_scalar_mode_supported_p (machine_mode);
  319. static int sh_dwarf_calling_convention (const_tree);
  320. static void sh_encode_section_info (tree, rtx, int);
  321. static bool sh2a_function_vector_p (tree);
  322. static void sh_trampoline_init (rtx, tree, rtx);
  323. static rtx sh_trampoline_adjust_address (rtx);
  324. static void sh_conditional_register_usage (void);
  325. static bool sh_legitimate_constant_p (machine_mode, rtx);
  326. static int mov_insn_size (machine_mode, bool);
  327. static int mov_insn_alignment_mask (machine_mode, bool);
  328. static bool sh_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT,
  329. unsigned int,
  330. enum by_pieces_operation,
  331. bool);
  332. static bool sequence_insn_p (rtx_insn *);
  333. static void sh_canonicalize_comparison (int *, rtx *, rtx *, bool);
  334. static void sh_canonicalize_comparison (enum rtx_code&, rtx&, rtx&,
  335. machine_mode, bool);
  336. static bool sh_legitimate_combined_insn (rtx_insn* insn);
  337. static bool sh_fixed_condition_code_regs (unsigned int* p1, unsigned int* p2);
  338. static void sh_init_sync_libfuncs (void) ATTRIBUTE_UNUSED;
  339. static const struct attribute_spec sh_attribute_table[] =
  340. {
  341. /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
  342. affects_type_identity } */
  343. { "interrupt_handler", 0, 0, true, false, false,
  344. sh_handle_interrupt_handler_attribute, false },
  345. { "sp_switch", 1, 1, true, false, false,
  346. sh_handle_sp_switch_attribute, false },
  347. { "trap_exit", 1, 1, true, false, false,
  348. sh_handle_trap_exit_attribute, false },
  349. { "renesas", 0, 0, false, true, false,
  350. sh_handle_renesas_attribute, false },
  351. { "trapa_handler", 0, 0, true, false, false,
  352. sh_handle_interrupt_handler_attribute, false },
  353. { "nosave_low_regs", 0, 0, true, false, false,
  354. sh_handle_interrupt_handler_attribute, false },
  355. { "resbank", 0, 0, true, false, false,
  356. sh_handle_resbank_handler_attribute, false },
  357. { "function_vector", 1, 1, true, false, false,
  358. sh2a_handle_function_vector_handler_attribute, false },
  359. { NULL, 0, 0, false, false, false, NULL, false }
  360. };
  361. /* Initialize the GCC target structure. */
  362. #undef TARGET_ATTRIBUTE_TABLE
  363. #define TARGET_ATTRIBUTE_TABLE sh_attribute_table
  364. /* The next two are used for debug info when compiling with -gdwarf. */
  365. #undef TARGET_ASM_UNALIGNED_HI_OP
  366. #define TARGET_ASM_UNALIGNED_HI_OP "\t.uaword\t"
  367. #undef TARGET_ASM_UNALIGNED_SI_OP
  368. #define TARGET_ASM_UNALIGNED_SI_OP "\t.ualong\t"
  369. /* These are NULLed out on non-SH5 in TARGET_OPTION_OVERRIDE. */
  370. #undef TARGET_ASM_UNALIGNED_DI_OP
  371. #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaquad\t"
  372. #undef TARGET_ASM_ALIGNED_DI_OP
  373. #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
  374. #undef TARGET_OPTION_OVERRIDE
  375. #define TARGET_OPTION_OVERRIDE sh_option_override
  376. #undef TARGET_PRINT_OPERAND
  377. #define TARGET_PRINT_OPERAND sh_print_operand
  378. #undef TARGET_PRINT_OPERAND_ADDRESS
  379. #define TARGET_PRINT_OPERAND_ADDRESS sh_print_operand_address
  380. #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
  381. #define TARGET_PRINT_OPERAND_PUNCT_VALID_P sh_print_operand_punct_valid_p
  382. #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
  383. #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA sh_asm_output_addr_const_extra
  384. #undef TARGET_ASM_FUNCTION_EPILOGUE
  385. #define TARGET_ASM_FUNCTION_EPILOGUE sh_output_function_epilogue
  386. #undef TARGET_ASM_OUTPUT_MI_THUNK
  387. #define TARGET_ASM_OUTPUT_MI_THUNK sh_output_mi_thunk
  388. #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
  389. #define TARGET_ASM_CAN_OUTPUT_MI_THUNK \
  390. hook_bool_const_tree_hwi_hwi_const_tree_true
  391. #undef TARGET_ASM_FILE_START
  392. #define TARGET_ASM_FILE_START sh_file_start
  393. #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
  394. #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
  395. #undef TARGET_REGISTER_MOVE_COST
  396. #define TARGET_REGISTER_MOVE_COST sh_register_move_cost
  397. #undef TARGET_INSERT_ATTRIBUTES
  398. #define TARGET_INSERT_ATTRIBUTES sh_insert_attributes
  399. #undef TARGET_SCHED_ADJUST_COST
  400. #define TARGET_SCHED_ADJUST_COST sh_adjust_cost
  401. #undef TARGET_SCHED_ISSUE_RATE
  402. #define TARGET_SCHED_ISSUE_RATE sh_issue_rate
  403. /* The next 5 hooks have been implemented for reenabling sched1. With the
  404. help of these macros we are limiting the movement of insns in sched1 to
  405. reduce the register pressure. The overall idea is to keep count of SImode
  406. and SFmode regs required by already scheduled insns. When these counts
  407. cross some threshold values; give priority to insns that free registers.
  408. The insn that frees registers is most likely to be the insn with lowest
  409. LUID (original insn order); but such an insn might be there in the stalled
  410. queue (Q) instead of the ready queue (R). To solve this, we skip cycles
  411. up to a max of 8 cycles so that such insns may move from Q -> R.
  412. The description of the hooks are as below:
  413. TARGET_SCHED_INIT_GLOBAL: Added a new target hook in the generic
  414. scheduler; it is called inside the sched_init function just after
  415. find_insn_reg_weights function call. It is used to calculate the SImode
  416. and SFmode weights of insns of basic blocks; much similar to what
  417. find_insn_reg_weights does.
  418. TARGET_SCHED_FINISH_GLOBAL: Corresponding cleanup hook.
  419. TARGET_SCHED_DFA_NEW_CYCLE: Skip cycles if high register pressure is
  420. indicated by TARGET_SCHED_REORDER2; doing this may move insns from
  421. (Q)->(R).
  422. TARGET_SCHED_REORDER: If the register pressure for SImode or SFmode is
  423. high; reorder the ready queue so that the insn with lowest LUID will be
  424. issued next.
  425. TARGET_SCHED_REORDER2: If the register pressure is high, indicate to
  426. TARGET_SCHED_DFA_NEW_CYCLE to skip cycles.
  427. TARGET_SCHED_VARIABLE_ISSUE: Cache the value of can_issue_more so that it
  428. can be returned from TARGET_SCHED_REORDER2.
  429. TARGET_SCHED_INIT: Reset the register pressure counting variables. */
  430. #undef TARGET_SCHED_DFA_NEW_CYCLE
  431. #define TARGET_SCHED_DFA_NEW_CYCLE sh_dfa_new_cycle
  432. #undef TARGET_SCHED_INIT_GLOBAL
  433. #define TARGET_SCHED_INIT_GLOBAL sh_md_init_global
  434. #undef TARGET_SCHED_FINISH_GLOBAL
  435. #define TARGET_SCHED_FINISH_GLOBAL sh_md_finish_global
  436. #undef TARGET_SCHED_VARIABLE_ISSUE
  437. #define TARGET_SCHED_VARIABLE_ISSUE sh_variable_issue
  438. #undef TARGET_SCHED_REORDER
  439. #define TARGET_SCHED_REORDER sh_reorder
  440. #undef TARGET_SCHED_REORDER2
  441. #define TARGET_SCHED_REORDER2 sh_reorder2
  442. #undef TARGET_SCHED_INIT
  443. #define TARGET_SCHED_INIT sh_md_init
  444. #undef TARGET_DELEGITIMIZE_ADDRESS
  445. #define TARGET_DELEGITIMIZE_ADDRESS sh_delegitimize_address
  446. #undef TARGET_LEGITIMIZE_ADDRESS
  447. #define TARGET_LEGITIMIZE_ADDRESS sh_legitimize_address
  448. #undef TARGET_CANNOT_MODIFY_JUMPS_P
  449. #define TARGET_CANNOT_MODIFY_JUMPS_P sh_cannot_modify_jumps_p
  450. #undef TARGET_CAN_FOLLOW_JUMP
  451. #define TARGET_CAN_FOLLOW_JUMP sh_can_follow_jump
  452. #undef TARGET_BRANCH_TARGET_REGISTER_CLASS
  453. #define TARGET_BRANCH_TARGET_REGISTER_CLASS sh_target_reg_class
  454. #undef TARGET_BRANCH_TARGET_REGISTER_CALLEE_SAVED
  455. #define TARGET_BRANCH_TARGET_REGISTER_CALLEE_SAVED \
  456. sh_optimize_target_register_callee_saved
  457. #undef TARGET_MS_BITFIELD_LAYOUT_P
  458. #define TARGET_MS_BITFIELD_LAYOUT_P sh_ms_bitfield_layout_p
  459. #undef TARGET_INIT_BUILTINS
  460. #define TARGET_INIT_BUILTINS sh_init_builtins
  461. #undef TARGET_BUILTIN_DECL
  462. #define TARGET_BUILTIN_DECL sh_builtin_decl
  463. #undef TARGET_EXPAND_BUILTIN
  464. #define TARGET_EXPAND_BUILTIN sh_expand_builtin
  465. #undef TARGET_FUNCTION_OK_FOR_SIBCALL
  466. #define TARGET_FUNCTION_OK_FOR_SIBCALL sh_function_ok_for_sibcall
  467. #undef TARGET_CANNOT_COPY_INSN_P
  468. #define TARGET_CANNOT_COPY_INSN_P sh_cannot_copy_insn_p
  469. #undef TARGET_RTX_COSTS
  470. #define TARGET_RTX_COSTS sh_rtx_costs
  471. #undef TARGET_ADDRESS_COST
  472. #define TARGET_ADDRESS_COST sh_address_cost
  473. #undef TARGET_ALLOCATE_INITIAL_VALUE
  474. #define TARGET_ALLOCATE_INITIAL_VALUE sh_allocate_initial_value
  475. #undef TARGET_MACHINE_DEPENDENT_REORG
  476. #define TARGET_MACHINE_DEPENDENT_REORG sh_reorg
  477. #undef TARGET_DWARF_REGISTER_SPAN
  478. #define TARGET_DWARF_REGISTER_SPAN sh_dwarf_register_span
  479. #ifdef HAVE_AS_TLS
  480. #undef TARGET_HAVE_TLS
  481. #define TARGET_HAVE_TLS true
  482. #endif
  483. #undef TARGET_PROMOTE_PROTOTYPES
  484. #define TARGET_PROMOTE_PROTOTYPES sh_promote_prototypes
  485. #undef TARGET_PROMOTE_FUNCTION_MODE
  486. #define TARGET_PROMOTE_FUNCTION_MODE sh_promote_function_mode
  487. #undef TARGET_FUNCTION_VALUE
  488. #define TARGET_FUNCTION_VALUE sh_function_value
  489. #undef TARGET_FUNCTION_VALUE_REGNO_P
  490. #define TARGET_FUNCTION_VALUE_REGNO_P sh_function_value_regno_p
  491. #undef TARGET_LIBCALL_VALUE
  492. #define TARGET_LIBCALL_VALUE sh_libcall_value
  493. #undef TARGET_STRUCT_VALUE_RTX
  494. #define TARGET_STRUCT_VALUE_RTX sh_struct_value_rtx
  495. #undef TARGET_RETURN_IN_MEMORY
  496. #define TARGET_RETURN_IN_MEMORY sh_return_in_memory
  497. #undef TARGET_EXPAND_BUILTIN_SAVEREGS
  498. #define TARGET_EXPAND_BUILTIN_SAVEREGS sh_builtin_saveregs
  499. #undef TARGET_SETUP_INCOMING_VARARGS
  500. #define TARGET_SETUP_INCOMING_VARARGS sh_setup_incoming_varargs
  501. #undef TARGET_STRICT_ARGUMENT_NAMING
  502. #define TARGET_STRICT_ARGUMENT_NAMING sh_strict_argument_naming
  503. #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
  504. #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED sh_pretend_outgoing_varargs_named
  505. #undef TARGET_MUST_PASS_IN_STACK
  506. #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
  507. #undef TARGET_PASS_BY_REFERENCE
  508. #define TARGET_PASS_BY_REFERENCE sh_pass_by_reference
  509. #undef TARGET_CALLEE_COPIES
  510. #define TARGET_CALLEE_COPIES sh_callee_copies
  511. #undef TARGET_ARG_PARTIAL_BYTES
  512. #define TARGET_ARG_PARTIAL_BYTES sh_arg_partial_bytes
  513. #undef TARGET_FUNCTION_ARG
  514. #define TARGET_FUNCTION_ARG sh_function_arg
  515. #undef TARGET_FUNCTION_ARG_ADVANCE
  516. #define TARGET_FUNCTION_ARG_ADVANCE sh_function_arg_advance
  517. #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
  518. #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV sh_atomic_assign_expand_fenv
  519. #undef TARGET_BUILD_BUILTIN_VA_LIST
  520. #define TARGET_BUILD_BUILTIN_VA_LIST sh_build_builtin_va_list
  521. #undef TARGET_EXPAND_BUILTIN_VA_START
  522. #define TARGET_EXPAND_BUILTIN_VA_START sh_va_start
  523. #undef TARGET_GIMPLIFY_VA_ARG_EXPR
  524. #define TARGET_GIMPLIFY_VA_ARG_EXPR sh_gimplify_va_arg_expr
  525. #undef TARGET_SCALAR_MODE_SUPPORTED_P
  526. #define TARGET_SCALAR_MODE_SUPPORTED_P sh_scalar_mode_supported_p
  527. #undef TARGET_VECTOR_MODE_SUPPORTED_P
  528. #define TARGET_VECTOR_MODE_SUPPORTED_P sh_vector_mode_supported_p
  529. #undef TARGET_CHECK_PCH_TARGET_FLAGS
  530. #define TARGET_CHECK_PCH_TARGET_FLAGS sh_check_pch_target_flags
  531. #undef TARGET_DWARF_CALLING_CONVENTION
  532. #define TARGET_DWARF_CALLING_CONVENTION sh_dwarf_calling_convention
  533. #undef TARGET_FRAME_POINTER_REQUIRED
  534. #define TARGET_FRAME_POINTER_REQUIRED sh_frame_pointer_required
  535. #undef TARGET_MODE_EMIT
  536. #define TARGET_MODE_EMIT sh_emit_mode_set
  537. #undef TARGET_MODE_NEEDED
  538. #define TARGET_MODE_NEEDED sh_mode_needed
  539. #undef TARGET_MODE_AFTER
  540. #define TARGET_MODE_AFTER sh_mode_after
  541. #undef TARGET_MODE_ENTRY
  542. #define TARGET_MODE_ENTRY sh_mode_entry
  543. #undef TARGET_MODE_EXIT
  544. #define TARGET_MODE_EXIT sh_mode_exit
  545. #undef TARGET_MODE_PRIORITY
  546. #define TARGET_MODE_PRIORITY sh_mode_priority
  547. /* Return regmode weight for insn. */
  548. #define INSN_REGMODE_WEIGHT(INSN, MODE)\
  549. regmode_weight[((MODE) == SImode) ? 0 : 1][INSN_UID (INSN)]
  550. /* Return current register pressure for regmode. */
  551. #define CURR_REGMODE_PRESSURE(MODE)\
  552. curr_regmode_pressure[((MODE) == SImode) ? 0 : 1]
  553. #undef TARGET_ENCODE_SECTION_INFO
  554. #define TARGET_ENCODE_SECTION_INFO sh_encode_section_info
  555. #undef TARGET_LRA_P
  556. #define TARGET_LRA_P sh_lra_p
  557. #undef TARGET_SECONDARY_RELOAD
  558. #define TARGET_SECONDARY_RELOAD sh_secondary_reload
  559. #undef TARGET_PREFERRED_RELOAD_CLASS
  560. #define TARGET_PREFERRED_RELOAD_CLASS sh_preferred_reload_class
  561. #undef TARGET_CONDITIONAL_REGISTER_USAGE
  562. #define TARGET_CONDITIONAL_REGISTER_USAGE sh_conditional_register_usage
  563. #undef TARGET_LEGITIMATE_ADDRESS_P
  564. #define TARGET_LEGITIMATE_ADDRESS_P sh_legitimate_address_p
  565. #undef TARGET_CANNOT_SUBSTITUTE_MEM_EQUIV_P
  566. #define TARGET_CANNOT_SUBSTITUTE_MEM_EQUIV_P sh_cannot_substitute_mem_equiv_p
  567. #undef TARGET_LEGITIMIZE_ADDRESS_DISPLACEMENT
  568. #define TARGET_LEGITIMIZE_ADDRESS_DISPLACEMENT \
  569. sh_legitimize_address_displacement
  570. #undef TARGET_TRAMPOLINE_INIT
  571. #define TARGET_TRAMPOLINE_INIT sh_trampoline_init
  572. #undef TARGET_TRAMPOLINE_ADJUST_ADDRESS
  573. #define TARGET_TRAMPOLINE_ADJUST_ADDRESS sh_trampoline_adjust_address
  574. #undef TARGET_LEGITIMATE_CONSTANT_P
  575. #define TARGET_LEGITIMATE_CONSTANT_P sh_legitimate_constant_p
  576. #undef TARGET_CANONICALIZE_COMPARISON
  577. #define TARGET_CANONICALIZE_COMPARISON sh_canonicalize_comparison
  578. #undef TARGET_LEGITIMATE_COMBINED_INSN
  579. #define TARGET_LEGITIMATE_COMBINED_INSN sh_legitimate_combined_insn
  580. #undef TARGET_FIXED_CONDITION_CODE_REGS
  581. #define TARGET_FIXED_CONDITION_CODE_REGS sh_fixed_condition_code_regs
  582. #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
  583. #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
  584. sh_use_by_pieces_infrastructure_p
  585. /* Machine-specific symbol_ref flags. */
  586. #define SYMBOL_FLAG_FUNCVEC_FUNCTION (SYMBOL_FLAG_MACH_DEP << 0)
  587. /* The tas.b instruction sets the 7th bit in the byte, i.e. 0x80. This value
  588. is used by optabs.c atomic op expansion code as well as in sync.md. */
  589. #undef TARGET_ATOMIC_TEST_AND_SET_TRUEVAL
  590. #define TARGET_ATOMIC_TEST_AND_SET_TRUEVAL 0x80
  591. struct gcc_target targetm = TARGET_INITIALIZER;
  592. /* Information on the currently selected atomic model.
  593. This is initialized in sh_option_override. */
  594. static sh_atomic_model selected_atomic_model_;
  595. const sh_atomic_model&
  596. selected_atomic_model (void)
  597. {
  598. return selected_atomic_model_;
  599. }
  600. static sh_atomic_model
  601. parse_validate_atomic_model_option (const char* str)
  602. {
  603. const char* model_names[sh_atomic_model::num_models];
  604. model_names[sh_atomic_model::none] = "none";
  605. model_names[sh_atomic_model::soft_gusa] = "soft-gusa";
  606. model_names[sh_atomic_model::hard_llcs] = "hard-llcs";
  607. model_names[sh_atomic_model::soft_tcb] = "soft-tcb";
  608. model_names[sh_atomic_model::soft_imask] = "soft-imask";
  609. const char* model_cdef_names[sh_atomic_model::num_models];
  610. model_cdef_names[sh_atomic_model::none] = "NONE";
  611. model_cdef_names[sh_atomic_model::soft_gusa] = "SOFT_GUSA";
  612. model_cdef_names[sh_atomic_model::hard_llcs] = "HARD_LLCS";
  613. model_cdef_names[sh_atomic_model::soft_tcb] = "SOFT_TCB";
  614. model_cdef_names[sh_atomic_model::soft_imask] = "SOFT_IMASK";
  615. sh_atomic_model ret;
  616. ret.type = sh_atomic_model::none;
  617. ret.name = model_names[sh_atomic_model::none];
  618. ret.cdef_name = model_cdef_names[sh_atomic_model::none];
  619. ret.strict = false;
  620. ret.tcb_gbr_offset = -1;
  621. /* Handle empty string as 'none'. */
  622. if (str == NULL || *str == '\0')
  623. return ret;
  624. #define err_ret(...) do { error (__VA_ARGS__); return ret; } while (0)
  625. std::vector<std::string> tokens;
  626. for (std::stringstream ss (str); ss.good (); )
  627. {
  628. tokens.push_back (std::string ());
  629. std::getline (ss, tokens.back (), ',');
  630. }
  631. if (tokens.empty ())
  632. err_ret ("invalid atomic model option");
  633. /* The first token must be the atomic model name. */
  634. {
  635. for (size_t i = 0; i < sh_atomic_model::num_models; ++i)
  636. if (tokens.front () == model_names[i])
  637. {
  638. ret.type = (sh_atomic_model::enum_type)i;
  639. ret.name = model_names[i];
  640. ret.cdef_name = model_cdef_names[i];
  641. goto got_mode_name;
  642. }
  643. err_ret ("invalid atomic model name \"%s\"", tokens.front ().c_str ());
  644. got_mode_name:;
  645. }
  646. /* Go through the remaining tokens. */
  647. for (size_t i = 1; i < tokens.size (); ++i)
  648. {
  649. if (tokens[i] == "strict")
  650. ret.strict = true;
  651. else if (tokens[i].find ("gbr-offset=") == 0)
  652. {
  653. std::string offset_str = tokens[i].substr (strlen ("gbr-offset="));
  654. ret.tcb_gbr_offset = integral_argument (offset_str.c_str ());
  655. if (offset_str.empty () || ret.tcb_gbr_offset == -1)
  656. err_ret ("could not parse gbr-offset value \"%s\" in atomic model "
  657. "option", offset_str.c_str ());
  658. }
  659. else
  660. err_ret ("unknown parameter \"%s\" in atomic model option",
  661. tokens[i].c_str ());
  662. }
  663. /* Check that the selection makes sense. */
  664. if (TARGET_SHMEDIA && ret.type != sh_atomic_model::none)
  665. err_ret ("atomic operations are not supported on SHmedia");
  666. if (ret.type == sh_atomic_model::soft_gusa && !TARGET_SH3)
  667. err_ret ("atomic model %s is only available on SH3 and SH4 targets",
  668. ret.name);
  669. if (ret.type == sh_atomic_model::hard_llcs && !TARGET_SH4A)
  670. err_ret ("atomic model %s is only available on SH4A targets", ret.name);
  671. if (ret.type == sh_atomic_model::soft_tcb && ret.tcb_gbr_offset == -1)
  672. err_ret ("atomic model %s requires gbr-offset parameter", ret.name);
  673. if (ret.type == sh_atomic_model::soft_tcb
  674. && (ret.tcb_gbr_offset < 0 || ret.tcb_gbr_offset > 1020
  675. || (ret.tcb_gbr_offset & 3) != 0))
  676. err_ret ("invalid gbr-offset value \"%d\" for atomic model %s; it must be "
  677. "a multiple of 4 in the range 0-1020", ret.tcb_gbr_offset,
  678. ret.name);
  679. if (ret.type == sh_atomic_model::soft_imask && TARGET_USERMODE)
  680. err_ret ("cannot use atomic model %s in user mode", ret.name);
  681. return ret;
  682. #undef err_ret
  683. }
  684. /* Register SH specific RTL passes. */
  685. extern opt_pass* make_pass_sh_treg_combine (gcc::context* ctx, bool split_insns,
  686. const char* name);
  687. extern opt_pass* make_pass_sh_optimize_sett_clrt (gcc::context* ctx,
  688. const char* name);
  689. static void
  690. register_sh_passes (void)
  691. {
  692. if (!TARGET_SH1)
  693. return;
  694. /* Running the sh_treg_combine pass after ce1 generates better code when
  695. comparisons are combined and reg-reg moves are introduced, because
  696. reg-reg moves will be eliminated afterwards. However, there are quite
  697. some cases where combine will be unable to fold comparison related insns,
  698. thus for now don't do it.
  699. register_pass (make_pass_sh_treg_combine (g, false, "sh_treg_combine1"),
  700. PASS_POS_INSERT_AFTER, "ce1", 1);
  701. */
  702. /* Run sh_treg_combine pass after combine but before register allocation. */
  703. register_pass (make_pass_sh_treg_combine (g, true, "sh_treg_combine2"),
  704. PASS_POS_INSERT_AFTER, "split1", 1);
  705. /* Run sh_treg_combine pass after register allocation and basic block
  706. reordering as this sometimes creates new opportunities. */
  707. register_pass (make_pass_sh_treg_combine (g, true, "sh_treg_combine3"),
  708. PASS_POS_INSERT_AFTER, "split4", 1);
  709. /* Optimize sett and clrt insns, by e.g. removing them if the T bit value
  710. is known after a conditional branch.
  711. This must be done after basic blocks and branch conditions have
  712. stabilized and won't be changed by further passes. */
  713. register_pass (make_pass_sh_optimize_sett_clrt (g, "sh_optimize_sett_clrt"),
  714. PASS_POS_INSERT_BEFORE, "sched2", 1);
  715. }
  716. /* Implement TARGET_OPTION_OVERRIDE macro. Validate and override
  717. various options, and do some machine dependent initialization. */
  718. static void
  719. sh_option_override (void)
  720. {
  721. int regno;
  722. SUBTARGET_OVERRIDE_OPTIONS;
  723. if (optimize > 1 && !optimize_size)
  724. target_flags |= MASK_SAVE_ALL_TARGET_REGS;
  725. /* Set default values of TARGET_CBRANCHDI4 and TARGET_CMPEQDI_T. */
  726. TARGET_CBRANCHDI4 = 1;
  727. TARGET_CMPEQDI_T = 0;
  728. sh_cpu = PROCESSOR_SH1;
  729. assembler_dialect = 0;
  730. if (TARGET_SH2)
  731. sh_cpu = PROCESSOR_SH2;
  732. if (TARGET_SH2E)
  733. sh_cpu = PROCESSOR_SH2E;
  734. if (TARGET_SH2A)
  735. sh_cpu = PROCESSOR_SH2A;
  736. if (TARGET_SH3)
  737. sh_cpu = PROCESSOR_SH3;
  738. if (TARGET_SH3E)
  739. sh_cpu = PROCESSOR_SH3E;
  740. if (TARGET_SH4)
  741. {
  742. assembler_dialect = 1;
  743. sh_cpu = PROCESSOR_SH4;
  744. }
  745. if (TARGET_SH4A)
  746. {
  747. assembler_dialect = 1;
  748. sh_cpu = PROCESSOR_SH4A;
  749. }
  750. if (TARGET_SH5)
  751. {
  752. sh_cpu = PROCESSOR_SH5;
  753. target_flags |= MASK_ALIGN_DOUBLE;
  754. if (TARGET_SHMEDIA_FPU)
  755. target_flags |= MASK_FMOVD;
  756. if (TARGET_SHMEDIA)
  757. {
  758. /* There are no delay slots on SHmedia. */
  759. flag_delayed_branch = 0;
  760. /* Relaxation isn't yet supported for SHmedia */
  761. target_flags &= ~MASK_RELAX;
  762. /* After reload, if conversion does little good but can cause
  763. ICEs:
  764. - find_if_block doesn't do anything for SH because we don't
  765. have conditional execution patterns. (We use conditional
  766. move patterns, which are handled differently, and only
  767. before reload).
  768. - find_cond_trap doesn't do anything for the SH because we
  769. don't have conditional traps.
  770. - find_if_case_1 uses redirect_edge_and_branch_force in
  771. the only path that does an optimization, and this causes
  772. an ICE when branch targets are in registers.
  773. - find_if_case_2 doesn't do anything for the SHmedia after
  774. reload except when it can redirect a tablejump - and
  775. that's rather rare. */
  776. flag_if_conversion2 = 0;
  777. if (! strcmp (sh_div_str, "call"))
  778. sh_div_strategy = SH_DIV_CALL;
  779. else if (! strcmp (sh_div_str, "call2"))
  780. sh_div_strategy = SH_DIV_CALL2;
  781. if (! strcmp (sh_div_str, "fp") && TARGET_FPU_ANY)
  782. sh_div_strategy = SH_DIV_FP;
  783. else if (! strcmp (sh_div_str, "inv"))
  784. sh_div_strategy = SH_DIV_INV;
  785. else if (! strcmp (sh_div_str, "inv:minlat"))
  786. sh_div_strategy = SH_DIV_INV_MINLAT;
  787. else if (! strcmp (sh_div_str, "inv20u"))
  788. sh_div_strategy = SH_DIV_INV20U;
  789. else if (! strcmp (sh_div_str, "inv20l"))
  790. sh_div_strategy = SH_DIV_INV20L;
  791. else if (! strcmp (sh_div_str, "inv:call2"))
  792. sh_div_strategy = SH_DIV_INV_CALL2;
  793. else if (! strcmp (sh_div_str, "inv:call"))
  794. sh_div_strategy = SH_DIV_INV_CALL;
  795. else if (! strcmp (sh_div_str, "inv:fp"))
  796. {
  797. if (TARGET_FPU_ANY)
  798. sh_div_strategy = SH_DIV_INV_FP;
  799. else
  800. sh_div_strategy = SH_DIV_INV;
  801. }
  802. TARGET_CBRANCHDI4 = 0;
  803. /* Assembler CFI isn't yet fully supported for SHmedia. */
  804. flag_dwarf2_cfi_asm = 0;
  805. }
  806. }
  807. else
  808. {
  809. /* Only the sh64-elf assembler fully supports .quad properly. */
  810. targetm.asm_out.aligned_op.di = NULL;
  811. targetm.asm_out.unaligned_op.di = NULL;
  812. }
  813. /* User/priviledged mode is supported only on SH3*, SH4* and SH5*.
  814. Disable it for everything else. */
  815. if (! (TARGET_SH3 || TARGET_SH5) && TARGET_USERMODE)
  816. TARGET_USERMODE = false;
  817. if (TARGET_SH1)
  818. {
  819. if (! strcmp (sh_div_str, "call-div1"))
  820. sh_div_strategy = SH_DIV_CALL_DIV1;
  821. else if (! strcmp (sh_div_str, "call-fp")
  822. && (TARGET_FPU_DOUBLE || TARGET_FPU_SINGLE_ONLY
  823. || (TARGET_SHCOMPACT && TARGET_FPU_ANY)))
  824. sh_div_strategy = SH_DIV_CALL_FP;
  825. else if (! strcmp (sh_div_str, "call-table") && TARGET_DYNSHIFT)
  826. sh_div_strategy = SH_DIV_CALL_TABLE;
  827. else
  828. /* Pick one that makes most sense for the target in general.
  829. It is not much good to use different functions depending
  830. on -Os, since then we'll end up with two different functions
  831. when some of the code is compiled for size, and some for
  832. speed. */
  833. /* SH4 tends to emphasize speed. */
  834. if (TARGET_HARD_SH4)
  835. sh_div_strategy = SH_DIV_CALL_TABLE;
  836. /* These have their own way of doing things. */
  837. else if (TARGET_SH2A)
  838. sh_div_strategy = SH_DIV_INTRINSIC;
  839. /* ??? Should we use the integer SHmedia function instead? */
  840. else if (TARGET_SHCOMPACT && TARGET_FPU_ANY)
  841. sh_div_strategy = SH_DIV_CALL_FP;
  842. /* SH1 .. SH3 cores often go into small-footprint systems, so
  843. default to the smallest implementation available. */
  844. else
  845. sh_div_strategy = SH_DIV_CALL_DIV1;
  846. }
  847. if (!TARGET_SH1)
  848. TARGET_PRETEND_CMOVE = 0;
  849. if (sh_divsi3_libfunc[0])
  850. ; /* User supplied - leave it alone. */
  851. else if (TARGET_DIVIDE_CALL_FP)
  852. sh_divsi3_libfunc = "__sdivsi3_i4";
  853. else if (TARGET_DIVIDE_CALL_TABLE)
  854. sh_divsi3_libfunc = "__sdivsi3_i4i";
  855. else if (TARGET_SH5)
  856. sh_divsi3_libfunc = "__sdivsi3_1";
  857. else
  858. sh_divsi3_libfunc = "__sdivsi3";
  859. if (sh_branch_cost == -1)
  860. {
  861. /* The SH1 does not have delay slots, hence we get a pipeline stall
  862. at every branch. The SH4 is superscalar, so the single delay slot
  863. is not sufficient to keep both pipelines filled.
  864. In any case, set the default branch cost to '2', as it results in
  865. slightly overall smaller code and also enables some if conversions
  866. that are required for matching special T bit related insns. */
  867. sh_branch_cost = 2;
  868. }
  869. /* Set -mzdcbranch for SH4 / SH4A if not otherwise specified by the user. */
  870. if (! global_options_set.x_TARGET_ZDCBRANCH && TARGET_HARD_SH4)
  871. TARGET_ZDCBRANCH = 1;
  872. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
  873. if (! VALID_REGISTER_P (regno))
  874. sh_register_names[regno][0] = '\0';
  875. for (regno = 0; regno < ADDREGNAMES_SIZE; regno++)
  876. if (! VALID_REGISTER_P (ADDREGNAMES_REGNO (regno)))
  877. sh_additional_register_names[regno][0] = '\0';
  878. if ((flag_pic && ! TARGET_PREFERGOT)
  879. || (TARGET_SHMEDIA && !TARGET_PT_FIXED))
  880. flag_no_function_cse = 1;
  881. if (targetm.small_register_classes_for_mode_p (VOIDmode))
  882. {
  883. /* Never run scheduling before reload, since that can
  884. break global alloc, and generates slower code anyway due
  885. to the pressure on R0. */
  886. /* Enable sched1 for SH4 if the user explicitly requests.
  887. When sched1 is enabled, the ready queue will be reordered by
  888. the target hooks if pressure is high. We can not do this for
  889. PIC, SH3 and lower as they give spill failures for R0. */
  890. if (!TARGET_HARD_SH4 || flag_pic)
  891. flag_schedule_insns = 0;
  892. /* ??? Current exception handling places basic block boundaries
  893. after call_insns. It causes the high pressure on R0 and gives
  894. spill failures for R0 in reload. See PR 22553 and the thread
  895. on gcc-patches
  896. <http://gcc.gnu.org/ml/gcc-patches/2005-10/msg00816.html>. */
  897. else if (flag_exceptions)
  898. {
  899. if (flag_schedule_insns && global_options_set.x_flag_schedule_insns)
  900. warning (0, "ignoring -fschedule-insns because of exception "
  901. "handling bug");
  902. flag_schedule_insns = 0;
  903. }
  904. else if (flag_schedule_insns
  905. && !global_options_set.x_flag_schedule_insns)
  906. flag_schedule_insns = 0;
  907. }
  908. /* Unwind info is not correct around the CFG unless either a frame
  909. pointer is present or M_A_O_A is set. Fixing this requires rewriting
  910. unwind info generation to be aware of the CFG and propagating states
  911. around edges. */
  912. if ((flag_unwind_tables || flag_asynchronous_unwind_tables
  913. || flag_exceptions || flag_non_call_exceptions)
  914. && flag_omit_frame_pointer && !TARGET_ACCUMULATE_OUTGOING_ARGS)
  915. {
  916. warning (0, "unwind tables currently require either a frame pointer "
  917. "or -maccumulate-outgoing-args for correctness");
  918. TARGET_ACCUMULATE_OUTGOING_ARGS = 1;
  919. }
  920. /* Adjust loop, jump and function alignment values (in bytes), if those
  921. were not specified by the user using -falign-loops, -falign-jumps
  922. and -falign-functions options.
  923. 32 bit alignment is better for speed, because instructions can be
  924. fetched as a pair from a longword boundary. For size use 16 bit
  925. alignment to get more compact code.
  926. Aligning all jumps increases the code size, even if it might
  927. result in slightly faster code. Thus, it is set to the smallest
  928. alignment possible if not specified by the user. */
  929. if (align_loops == 0)
  930. {
  931. if (TARGET_SH5)
  932. align_loops = 8;
  933. else
  934. align_loops = optimize_size ? 2 : 4;
  935. }
  936. if (align_jumps == 0)
  937. {
  938. if (TARGET_SHMEDIA)
  939. align_jumps = 1 << CACHE_LOG;
  940. else
  941. align_jumps = 2;
  942. }
  943. else if (align_jumps < (TARGET_SHMEDIA ? 4 : 2))
  944. align_jumps = TARGET_SHMEDIA ? 4 : 2;
  945. if (align_functions == 0)
  946. {
  947. if (TARGET_SHMEDIA)
  948. align_functions = optimize_size
  949. ? FUNCTION_BOUNDARY/8 : (1 << CACHE_LOG);
  950. else
  951. align_functions = optimize_size ? 2 : 4;
  952. }
  953. /* The linker relaxation code breaks when a function contains
  954. alignments that are larger than that at the start of a
  955. compilation unit. */
  956. if (TARGET_RELAX)
  957. {
  958. int min_align = align_loops > align_jumps ? align_loops : align_jumps;
  959. /* Also take possible .long constants / mova tables into account. */
  960. if (min_align < 4)
  961. min_align = 4;
  962. if (align_functions < min_align)
  963. align_functions = min_align;
  964. }
  965. if (flag_unsafe_math_optimizations)
  966. {
  967. /* Enable fsca insn for SH4A if not otherwise specified by the user. */
  968. if (global_options_set.x_TARGET_FSCA == 0 && TARGET_SH4A_FP)
  969. TARGET_FSCA = 1;
  970. /* Enable fsrra insn for SH4A if not otherwise specified by the user. */
  971. if (global_options_set.x_TARGET_FSRRA == 0 && TARGET_SH4A_FP)
  972. TARGET_FSRRA = 1;
  973. }
  974. /* Allow fsrra insn only if -funsafe-math-optimizations and
  975. -ffinite-math-only is enabled. */
  976. TARGET_FSRRA = TARGET_FSRRA
  977. && flag_unsafe_math_optimizations
  978. && flag_finite_math_only;
  979. /* If the -mieee option was not explicitly set by the user, turn it on
  980. unless -ffinite-math-only was specified. See also PR 33135. */
  981. if (! global_options_set.x_TARGET_IEEE)
  982. TARGET_IEEE = ! flag_finite_math_only;
  983. if (sh_fixed_range_str)
  984. sh_fix_range (sh_fixed_range_str);
  985. /* This target defaults to strict volatile bitfields. */
  986. if (flag_strict_volatile_bitfields < 0 && abi_version_at_least(2))
  987. flag_strict_volatile_bitfields = 1;
  988. /* Parse atomic model option and make sure it is valid for the current
  989. target CPU. */
  990. selected_atomic_model_
  991. = parse_validate_atomic_model_option (sh_atomic_model_str);
  992. register_sh_passes ();
  993. }
  994. /* Print the operand address in x to the stream. */
  995. static void
  996. sh_print_operand_address (FILE *stream, rtx x)
  997. {
  998. switch (GET_CODE (x))
  999. {
  1000. case REG:
  1001. case SUBREG:
  1002. fprintf (stream, "@%s", reg_names[true_regnum (x)]);
  1003. break;
  1004. case PLUS:
  1005. {
  1006. rtx base = XEXP (x, 0);
  1007. rtx index = XEXP (x, 1);
  1008. switch (GET_CODE (index))
  1009. {
  1010. case CONST_INT:
  1011. fprintf (stream, "@(%d,%s)", (int) INTVAL (index),
  1012. reg_names[true_regnum (base)]);
  1013. break;
  1014. case REG:
  1015. case SUBREG:
  1016. {
  1017. int base_num = true_regnum (base);
  1018. int index_num = true_regnum (index);
  1019. fprintf (stream, "@(r0,%s)",
  1020. reg_names[MAX (base_num, index_num)]);
  1021. break;
  1022. }
  1023. default:
  1024. gcc_unreachable ();
  1025. }
  1026. }
  1027. break;
  1028. case PRE_DEC:
  1029. fprintf (stream, "@-%s", reg_names[true_regnum (XEXP (x, 0))]);
  1030. break;
  1031. case POST_INC:
  1032. fprintf (stream, "@%s+", reg_names[true_regnum (XEXP (x, 0))]);
  1033. break;
  1034. default:
  1035. x = mark_constant_pool_use (x);
  1036. output_addr_const (stream, x);
  1037. break;
  1038. }
  1039. }
  1040. /* Print operand x (an rtx) in assembler syntax to file stream
  1041. according to modifier code.
  1042. '.' print a .s if insn needs delay slot
  1043. ',' print LOCAL_LABEL_PREFIX
  1044. '@' print trap, rte or rts depending upon pragma interruptness
  1045. '#' output a nop if there is nothing to put in the delay slot
  1046. ''' print likelihood suffix (/u for unlikely).
  1047. '>' print branch target if -fverbose-asm
  1048. 'O' print a constant without the #
  1049. 'R' print the LSW of a dp value - changes if in little endian
  1050. 'S' print the MSW of a dp value - changes if in little endian
  1051. 'T' print the next word of a dp value - same as 'R' in big endian mode.
  1052. 'M' SHMEDIA: print an `x' if `m' will print `base,index'.
  1053. otherwise: print .b / .w / .l / .s / .d suffix if operand is a MEM.
  1054. 'N' print 'r63' if the operand is (const_int 0).
  1055. 'd' print a V2SF reg as dN instead of fpN.
  1056. 'm' print a pair `base,offset' or `base,index', for LD and ST.
  1057. 'U' Likewise for {LD,ST}{HI,LO}.
  1058. 'V' print the position of a single bit set.
  1059. 'W' print the position of a single bit cleared.
  1060. 't' print a memory address which is a register.
  1061. 'u' prints the lowest 16 bits of CONST_INT, as an unsigned value.
  1062. 'o' output an operator. */
  1063. static void
  1064. sh_print_operand (FILE *stream, rtx x, int code)
  1065. {
  1066. int regno;
  1067. machine_mode mode;
  1068. switch (code)
  1069. {
  1070. tree trapa_attr;
  1071. case '.':
  1072. if (final_sequence
  1073. && ! INSN_ANNULLED_BRANCH_P (final_sequence->insn (0))
  1074. && get_attr_length (final_sequence->insn (1)))
  1075. fprintf (stream, ASSEMBLER_DIALECT ? "/s" : ".s");
  1076. break;
  1077. case ',':
  1078. fprintf (stream, "%s", LOCAL_LABEL_PREFIX);
  1079. break;
  1080. case '@':
  1081. trapa_attr = lookup_attribute ("trap_exit",
  1082. DECL_ATTRIBUTES (current_function_decl));
  1083. if (trapa_attr)
  1084. fprintf (stream, "trapa #%ld",
  1085. (long) TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (trapa_attr))));
  1086. else if (sh_cfun_interrupt_handler_p ())
  1087. {
  1088. if (sh_cfun_resbank_handler_p ())
  1089. fprintf (stream, "resbank\n");
  1090. fprintf (stream, "rte");
  1091. }
  1092. else
  1093. fprintf (stream, "rts");
  1094. break;
  1095. case '#':
  1096. /* Output a nop if there's nothing in the delay slot. */
  1097. if (dbr_sequence_length () == 0)
  1098. fprintf (stream, "\n\tnop");
  1099. break;
  1100. case '\'':
  1101. {
  1102. rtx note = find_reg_note (current_output_insn, REG_BR_PROB, 0);
  1103. if (note && XINT (note, 0) * 2 < REG_BR_PROB_BASE)
  1104. fputs ("/u", stream);
  1105. break;
  1106. }
  1107. case '>':
  1108. if (flag_verbose_asm && JUMP_LABEL (current_output_insn))
  1109. {
  1110. fputs ("\t! target: ", stream);
  1111. output_addr_const (stream, JUMP_LABEL (current_output_insn));
  1112. }
  1113. break;
  1114. case 'O':
  1115. x = mark_constant_pool_use (x);
  1116. output_addr_const (stream, x);
  1117. break;
  1118. /* N.B.: %R / %S / %T adjust memory addresses by four.
  1119. For SHMEDIA, that means they can be used to access the first and
  1120. second 32 bit part of a 64 bit (or larger) value that
  1121. might be held in floating point registers or memory.
  1122. While they can be used to access 64 bit parts of a larger value
  1123. held in general purpose registers, that won't work with memory -
  1124. neither for fp registers, since the frxx names are used. */
  1125. case 'R':
  1126. if (REG_P (x) || GET_CODE (x) == SUBREG)
  1127. {
  1128. regno = true_regnum (x);
  1129. regno += FP_REGISTER_P (regno) ? 1 : SH_REG_LSW_OFFSET;
  1130. fputs (reg_names[regno], (stream));
  1131. }
  1132. else if (MEM_P (x))
  1133. {
  1134. x = adjust_address (x, SImode, 4 * SH_REG_LSW_OFFSET);
  1135. sh_print_operand_address (stream, XEXP (x, 0));
  1136. }
  1137. else
  1138. {
  1139. rtx sub = NULL_RTX;
  1140. mode = GET_MODE (x);
  1141. if (mode == VOIDmode)
  1142. mode = DImode;
  1143. if (GET_MODE_SIZE (mode) >= 8)
  1144. sub = simplify_subreg (SImode, x, mode, 4 * SH_REG_LSW_OFFSET);
  1145. if (sub)
  1146. sh_print_operand (stream, sub, 0);
  1147. else
  1148. output_operand_lossage ("invalid operand to %%R");
  1149. }
  1150. break;
  1151. case 'S':
  1152. if (REG_P (x) || GET_CODE (x) == SUBREG)
  1153. {
  1154. regno = true_regnum (x);
  1155. regno += FP_REGISTER_P (regno) ? 0 : SH_REG_MSW_OFFSET;
  1156. fputs (reg_names[regno], (stream));
  1157. }
  1158. else if (MEM_P (x))
  1159. {
  1160. x = adjust_address (x, SImode, 4 * SH_REG_MSW_OFFSET);
  1161. sh_print_operand_address (stream, XEXP (x, 0));
  1162. }
  1163. else
  1164. {
  1165. rtx sub = NULL_RTX;
  1166. mode = GET_MODE (x);
  1167. if (mode == VOIDmode)
  1168. mode = DImode;
  1169. if (GET_MODE_SIZE (mode) >= 8)
  1170. sub = simplify_subreg (SImode, x, mode, 4 * SH_REG_MSW_OFFSET);
  1171. if (sub)
  1172. sh_print_operand (stream, sub, 0);
  1173. else
  1174. output_operand_lossage ("invalid operand to %%S");
  1175. }
  1176. break;
  1177. case 'T':
  1178. /* Next word of a double. */
  1179. switch (GET_CODE (x))
  1180. {
  1181. case REG:
  1182. fputs (reg_names[REGNO (x) + 1], (stream));
  1183. break;
  1184. case MEM:
  1185. if (GET_CODE (XEXP (x, 0)) != PRE_DEC
  1186. && GET_CODE (XEXP (x, 0)) != POST_INC)
  1187. x = adjust_address (x, SImode, 4);
  1188. sh_print_operand_address (stream, XEXP (x, 0));
  1189. break;
  1190. default:
  1191. break;
  1192. }
  1193. break;
  1194. case 't':
  1195. gcc_assert (MEM_P (x));
  1196. x = XEXP (x, 0);
  1197. switch (GET_CODE (x))
  1198. {
  1199. case REG:
  1200. case SUBREG:
  1201. sh_print_operand (stream, x, 0);
  1202. break;
  1203. default:
  1204. break;
  1205. }
  1206. break;
  1207. case 'o':
  1208. switch (GET_CODE (x))
  1209. {
  1210. case PLUS: fputs ("add", stream); break;
  1211. case MINUS: fputs ("sub", stream); break;
  1212. case MULT: fputs ("mul", stream); break;
  1213. case DIV: fputs ("div", stream); break;
  1214. case EQ: fputs ("eq", stream); break;
  1215. case NE: fputs ("ne", stream); break;
  1216. case GT: case LT: fputs ("gt", stream); break;
  1217. case GE: case LE: fputs ("ge", stream); break;
  1218. case GTU: case LTU: fputs ("gtu", stream); break;
  1219. case GEU: case LEU: fputs ("geu", stream); break;
  1220. default:
  1221. break;
  1222. }
  1223. break;
  1224. case 'M':
  1225. if (TARGET_SHMEDIA)
  1226. {
  1227. if (MEM_P (x)
  1228. && GET_CODE (XEXP (x, 0)) == PLUS
  1229. && (REG_P (XEXP (XEXP (x, 0), 1))
  1230. || GET_CODE (XEXP (XEXP (x, 0), 1)) == SUBREG))
  1231. fputc ('x', stream);
  1232. }
  1233. else
  1234. {
  1235. if (MEM_P (x))
  1236. {
  1237. switch (GET_MODE (x))
  1238. {
  1239. case QImode: fputs (".b", stream); break;
  1240. case HImode: fputs (".w", stream); break;
  1241. case SImode: fputs (".l", stream); break;
  1242. case SFmode: fputs (".s", stream); break;
  1243. case DFmode: fputs (".d", stream); break;
  1244. default: gcc_unreachable ();
  1245. }
  1246. }
  1247. }
  1248. break;
  1249. case 'm':
  1250. gcc_assert (MEM_P (x));
  1251. x = XEXP (x, 0);
  1252. /* Fall through. */
  1253. case 'U':
  1254. switch (GET_CODE (x))
  1255. {
  1256. case REG:
  1257. case SUBREG:
  1258. sh_print_operand (stream, x, 0);
  1259. fputs (", 0", stream);
  1260. break;
  1261. case PLUS:
  1262. sh_print_operand (stream, XEXP (x, 0), 0);
  1263. fputs (", ", stream);
  1264. sh_print_operand (stream, XEXP (x, 1), 0);
  1265. break;
  1266. default:
  1267. gcc_unreachable ();
  1268. }
  1269. break;
  1270. case 'V':
  1271. {
  1272. int num = exact_log2 (INTVAL (x));
  1273. gcc_assert (num >= 0);
  1274. fprintf (stream, "#%d", num);
  1275. }
  1276. break;
  1277. case 'W':
  1278. {
  1279. int num = exact_log2 (~INTVAL (x));
  1280. gcc_assert (num >= 0);
  1281. fprintf (stream, "#%d", num);
  1282. }
  1283. break;
  1284. case 'd':
  1285. gcc_assert (REG_P (x) && GET_MODE (x) == V2SFmode);
  1286. fprintf ((stream), "d%s", reg_names[REGNO (x)] + 1);
  1287. break;
  1288. case 'N':
  1289. if (x == CONST0_RTX (GET_MODE (x)))
  1290. {
  1291. fprintf ((stream), "r63");
  1292. break;
  1293. }
  1294. goto default_output;
  1295. case 'u':
  1296. if (CONST_INT_P (x))
  1297. {
  1298. fprintf ((stream), "%u", (unsigned) INTVAL (x) & (0x10000 - 1));
  1299. break;
  1300. }
  1301. /* Fall through. */
  1302. default_output:
  1303. default:
  1304. regno = 0;
  1305. mode = GET_MODE (x);
  1306. switch (GET_CODE (x))
  1307. {
  1308. case TRUNCATE:
  1309. {
  1310. rtx inner = XEXP (x, 0);
  1311. int offset = 0;
  1312. machine_mode inner_mode;
  1313. /* We might see SUBREGs with vector mode registers inside. */
  1314. if (GET_CODE (inner) == SUBREG
  1315. && (GET_MODE_SIZE (GET_MODE (inner))
  1316. == GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
  1317. && subreg_lowpart_p (inner))
  1318. inner = SUBREG_REG (inner);
  1319. if (CONST_INT_P (inner))
  1320. {
  1321. x = GEN_INT (trunc_int_for_mode (INTVAL (inner), GET_MODE (x)));
  1322. goto default_output;
  1323. }
  1324. inner_mode = GET_MODE (inner);
  1325. if (GET_CODE (inner) == SUBREG
  1326. && (GET_MODE_SIZE (GET_MODE (inner))
  1327. < GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner))))
  1328. && REG_P (SUBREG_REG (inner)))
  1329. {
  1330. offset = subreg_regno_offset (REGNO (SUBREG_REG (inner)),
  1331. GET_MODE (SUBREG_REG (inner)),
  1332. SUBREG_BYTE (inner),
  1333. GET_MODE (inner));
  1334. inner = SUBREG_REG (inner);
  1335. }
  1336. if (!REG_P (inner) || GET_MODE_SIZE (inner_mode) > 8)
  1337. abort ();
  1338. /* Floating point register pairs are always big endian;
  1339. general purpose registers are 64 bit wide. */
  1340. regno = REGNO (inner);
  1341. regno = (HARD_REGNO_NREGS (regno, inner_mode)
  1342. - HARD_REGNO_NREGS (regno, mode))
  1343. + offset;
  1344. x = inner;
  1345. goto reg;
  1346. }
  1347. case SIGN_EXTEND:
  1348. x = XEXP (x, 0);
  1349. goto reg;
  1350. /* FIXME: We need this on SHmedia32 because reload generates
  1351. some sign-extended HI or QI loads into DImode registers
  1352. but, because Pmode is SImode, the address ends up with a
  1353. subreg:SI of the DImode register. Maybe reload should be
  1354. fixed so as to apply alter_subreg to such loads? */
  1355. case IF_THEN_ELSE:
  1356. gcc_assert (trapping_target_operand (x, VOIDmode));
  1357. x = XEXP (XEXP (x, 2), 0);
  1358. goto default_output;
  1359. case SUBREG:
  1360. gcc_assert (SUBREG_BYTE (x) == 0
  1361. && REG_P (SUBREG_REG (x)));
  1362. x = SUBREG_REG (x);
  1363. /* Fall through. */
  1364. reg:
  1365. case REG:
  1366. regno += REGNO (x);
  1367. if (FP_REGISTER_P (regno)
  1368. && mode == V16SFmode)
  1369. fprintf ((stream), "mtrx%s", reg_names[regno] + 2);
  1370. else if (FP_REGISTER_P (REGNO (x))
  1371. && mode == V4SFmode)
  1372. fprintf ((stream), "fv%s", reg_names[regno] + 2);
  1373. else if (REG_P (x)
  1374. && mode == V2SFmode)
  1375. fprintf ((stream), "fp%s", reg_names[regno] + 2);
  1376. else if (FP_REGISTER_P (REGNO (x))
  1377. && GET_MODE_SIZE (mode) > 4)
  1378. fprintf ((stream), "d%s", reg_names[regno] + 1);
  1379. else
  1380. fputs (reg_names[regno], (stream));
  1381. break;
  1382. case MEM:
  1383. output_address (XEXP (x, 0));
  1384. break;
  1385. default:
  1386. if (TARGET_SH1)
  1387. fputc ('#', stream);
  1388. output_addr_const (stream, x);
  1389. break;
  1390. }
  1391. break;
  1392. }
  1393. }
  1394. static bool
  1395. sh_print_operand_punct_valid_p (unsigned char code)
  1396. {
  1397. return (code == '.' || code == '#' || code == '@' || code == ','
  1398. || code == '$' || code == '\'' || code == '>');
  1399. }
  1400. /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
  1401. static bool
  1402. sh_asm_output_addr_const_extra (FILE *file, rtx x)
  1403. {
  1404. if (GET_CODE (x) == UNSPEC)
  1405. {
  1406. switch (XINT (x, 1))
  1407. {
  1408. case UNSPEC_DATALABEL:
  1409. fputs ("datalabel ", file);
  1410. output_addr_const (file, XVECEXP (x, 0, 0));
  1411. break;
  1412. case UNSPEC_PIC:
  1413. /* GLOBAL_OFFSET_TABLE or local symbols, no suffix. */
  1414. output_addr_const (file, XVECEXP (x, 0, 0));
  1415. break;
  1416. case UNSPEC_GOT:
  1417. output_addr_const (file, XVECEXP (x, 0, 0));
  1418. fputs ("@GOT", file);
  1419. break;
  1420. case UNSPEC_GOTOFF:
  1421. output_addr_const (file, XVECEXP (x, 0, 0));
  1422. fputs ("@GOTOFF", file);
  1423. break;
  1424. case UNSPEC_PLT:
  1425. output_addr_const (file, XVECEXP (x, 0, 0));
  1426. fputs ("@PLT", file);
  1427. break;
  1428. case UNSPEC_GOTPLT:
  1429. output_addr_const (file, XVECEXP (x, 0, 0));
  1430. fputs ("@GOTPLT", file);
  1431. break;
  1432. case UNSPEC_DTPOFF:
  1433. output_addr_const (file, XVECEXP (x, 0, 0));
  1434. fputs ("@DTPOFF", file);
  1435. break;
  1436. case UNSPEC_GOTTPOFF:
  1437. output_addr_const (file, XVECEXP (x, 0, 0));
  1438. fputs ("@GOTTPOFF", file);
  1439. break;
  1440. case UNSPEC_TPOFF:
  1441. output_addr_const (file, XVECEXP (x, 0, 0));
  1442. fputs ("@TPOFF", file);
  1443. break;
  1444. case UNSPEC_CALLER:
  1445. {
  1446. char name[32];
  1447. /* LPCS stands for Label for PIC Call Site. */
  1448. targetm.asm_out.generate_internal_label (name, "LPCS",
  1449. INTVAL (XVECEXP (x, 0, 0)));
  1450. assemble_name (file, name);
  1451. }
  1452. break;
  1453. case UNSPEC_EXTRACT_S16:
  1454. case UNSPEC_EXTRACT_U16:
  1455. {
  1456. rtx val, shift;
  1457. val = XVECEXP (x, 0, 0);
  1458. shift = XVECEXP (x, 0, 1);
  1459. fputc ('(', file);
  1460. if (shift != const0_rtx)
  1461. fputc ('(', file);
  1462. if (GET_CODE (val) == CONST
  1463. || GET_RTX_CLASS (GET_CODE (val)) != RTX_OBJ)
  1464. {
  1465. fputc ('(', file);
  1466. output_addr_const (file, val);
  1467. fputc (')', file);
  1468. }
  1469. else
  1470. output_addr_const (file, val);
  1471. if (shift != const0_rtx)
  1472. {
  1473. fputs (" >> ", file);
  1474. output_addr_const (file, shift);
  1475. fputc (')', file);
  1476. }
  1477. fputs (" & 65535)", file);
  1478. }
  1479. break;
  1480. case UNSPEC_SYMOFF:
  1481. output_addr_const (file, XVECEXP (x, 0, 0));
  1482. fputc ('-', file);
  1483. if (GET_CODE (XVECEXP (x, 0, 1)) == CONST)
  1484. {
  1485. fputc ('(', file);
  1486. output_addr_const (file, XVECEXP (x, 0, 1));
  1487. fputc (')', file);
  1488. }
  1489. else
  1490. output_addr_const (file, XVECEXP (x, 0, 1));
  1491. break;
  1492. case UNSPEC_PCREL_SYMOFF:
  1493. output_addr_const (file, XVECEXP (x, 0, 0));
  1494. fputs ("-(", file);
  1495. output_addr_const (file, XVECEXP (x, 0, 1));
  1496. fputs ("-.)", file);
  1497. break;
  1498. default:
  1499. return false;
  1500. }
  1501. return true;
  1502. }
  1503. else
  1504. return false;
  1505. }
  1506. /* Encode symbol attributes of a SYMBOL_REF into its
  1507. SYMBOL_REF_FLAGS. */
  1508. static void
  1509. sh_encode_section_info (tree decl, rtx rtl, int first)
  1510. {
  1511. default_encode_section_info (decl, rtl, first);
  1512. if (TREE_CODE (decl) == FUNCTION_DECL
  1513. && sh2a_function_vector_p (decl) && TARGET_SH2A)
  1514. SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FUNCVEC_FUNCTION;
  1515. }
  1516. /* Prepare operands for a move define_expand; specifically, one of the
  1517. operands must be in a register. */
  1518. void
  1519. prepare_move_operands (rtx operands[], machine_mode mode)
  1520. {
  1521. if ((mode == SImode || mode == DImode)
  1522. && flag_pic
  1523. && ! ((mode == Pmode || mode == ptr_mode)
  1524. && tls_symbolic_operand (operands[1], Pmode) != TLS_MODEL_NONE))
  1525. {
  1526. rtx temp;
  1527. if (SYMBOLIC_CONST_P (operands[1]))
  1528. {
  1529. if (MEM_P (operands[0]))
  1530. operands[1] = force_reg (Pmode, operands[1]);
  1531. else if (TARGET_SHMEDIA
  1532. && GET_CODE (operands[1]) == LABEL_REF
  1533. && target_reg_operand (operands[0], mode))
  1534. /* It's ok. */;
  1535. else
  1536. {
  1537. temp = (!can_create_pseudo_p ()
  1538. ? operands[0]
  1539. : gen_reg_rtx (Pmode));
  1540. operands[1] = legitimize_pic_address (operands[1], mode, temp);
  1541. }
  1542. }
  1543. else if (GET_CODE (operands[1]) == CONST
  1544. && GET_CODE (XEXP (operands[1], 0)) == PLUS
  1545. && SYMBOLIC_CONST_P (XEXP (XEXP (operands[1], 0), 0)))
  1546. {
  1547. temp = !can_create_pseudo_p () ? operands[0] : gen_reg_rtx (Pmode);
  1548. temp = legitimize_pic_address (XEXP (XEXP (operands[1], 0), 0),
  1549. mode, temp);
  1550. operands[1] = expand_binop (mode, add_optab, temp,
  1551. XEXP (XEXP (operands[1], 0), 1),
  1552. (!can_create_pseudo_p ()
  1553. ? temp
  1554. : gen_reg_rtx (Pmode)),
  1555. 0, OPTAB_LIB_WIDEN);
  1556. }
  1557. }
  1558. if (! reload_in_progress && ! reload_completed)
  1559. {
  1560. /* Copy the source to a register if both operands aren't registers. */
  1561. if (! register_operand (operands[0], mode)
  1562. && ! sh_register_operand (operands[1], mode))
  1563. operands[1] = copy_to_mode_reg (mode, operands[1]);
  1564. if (MEM_P (operands[0]) && ! memory_operand (operands[0], mode))
  1565. {
  1566. /* This is like change_address_1 (operands[0], mode, 0, 1) ,
  1567. except that we can't use that function because it is static. */
  1568. rtx new_rtx = change_address (operands[0], mode, 0);
  1569. MEM_COPY_ATTRIBUTES (new_rtx, operands[0]);
  1570. operands[0] = new_rtx;
  1571. }
  1572. /* This case can happen while generating code to move the result
  1573. of a library call to the target. Reject `st r0,@(rX,rY)' because
  1574. reload will fail to find a spill register for rX, since r0 is already
  1575. being used for the source. */
  1576. else if (TARGET_SH1
  1577. && refers_to_regno_p (R0_REG, operands[1])
  1578. && MEM_P (operands[0])
  1579. && GET_CODE (XEXP (operands[0], 0)) == PLUS
  1580. && REG_P (XEXP (XEXP (operands[0], 0), 1)))
  1581. operands[1] = copy_to_mode_reg (mode, operands[1]);
  1582. /* When the displacement addressing is used, RA will assign r0 to
  1583. the pseudo register operand for the QI/HImode load/store.
  1584. This tends to make a long live range for R0 and might cause
  1585. anomalous register spills in some case with LRA. See PR
  1586. target/55212.
  1587. We split possible load/store to two move insns via r0 so as to
  1588. shorten R0 live range. It will make some codes worse but will
  1589. win on avarage for LRA. */
  1590. else if (sh_lra_p ()
  1591. && TARGET_SH1 && ! TARGET_SH2A
  1592. && (mode == QImode || mode == HImode)
  1593. && ((REG_P (operands[0]) && MEM_P (operands[1]))
  1594. || (REG_P (operands[1]) && MEM_P (operands[0]))))
  1595. {
  1596. bool load_p = REG_P (operands[0]);
  1597. rtx reg = operands[load_p ? 0 : 1];
  1598. rtx adr = XEXP (operands[load_p ? 1 : 0], 0);
  1599. if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
  1600. && GET_CODE (adr) == PLUS
  1601. && REG_P (XEXP (adr, 0))
  1602. && (REGNO (XEXP (adr, 0)) >= FIRST_PSEUDO_REGISTER)
  1603. && CONST_INT_P (XEXP (adr, 1))
  1604. && INTVAL (XEXP (adr, 1)) != 0
  1605. && sh_legitimate_index_p (mode, XEXP (adr, 1), false, true))
  1606. {
  1607. rtx r0_rtx = gen_rtx_REG (mode, R0_REG);
  1608. emit_move_insn (r0_rtx, operands[1]);
  1609. operands[1] = r0_rtx;
  1610. }
  1611. }
  1612. }
  1613. if (mode == Pmode || mode == ptr_mode)
  1614. {
  1615. rtx op0, op1, opc;
  1616. enum tls_model tls_kind;
  1617. op0 = operands[0];
  1618. op1 = operands[1];
  1619. if (GET_CODE (op1) == CONST
  1620. && GET_CODE (XEXP (op1, 0)) == PLUS
  1621. && (tls_symbolic_operand (XEXP (XEXP (op1, 0), 0), Pmode)
  1622. != TLS_MODEL_NONE))
  1623. {
  1624. opc = XEXP (XEXP (op1, 0), 1);
  1625. op1 = XEXP (XEXP (op1, 0), 0);
  1626. }
  1627. else
  1628. opc = NULL_RTX;
  1629. if (! reload_in_progress && ! reload_completed
  1630. && (tls_kind = tls_symbolic_operand (op1, Pmode)) != TLS_MODEL_NONE)
  1631. {
  1632. rtx tga_op1, tga_ret, tmp, tmp2;
  1633. if (! flag_pic
  1634. && (tls_kind == TLS_MODEL_GLOBAL_DYNAMIC
  1635. || tls_kind == TLS_MODEL_LOCAL_DYNAMIC
  1636. || tls_kind == TLS_MODEL_INITIAL_EXEC))
  1637. {
  1638. static int got_labelno;
  1639. /* Don't schedule insns for getting GOT address when
  1640. the first scheduling is enabled, to avoid spill
  1641. failures for R0. */
  1642. if (flag_schedule_insns)
  1643. emit_insn (gen_blockage ());
  1644. emit_insn (gen_GOTaddr2picreg (GEN_INT (++got_labelno)));
  1645. emit_use (gen_rtx_REG (SImode, PIC_REG));
  1646. if (flag_schedule_insns)
  1647. emit_insn (gen_blockage ());
  1648. }
  1649. switch (tls_kind)
  1650. {
  1651. case TLS_MODEL_GLOBAL_DYNAMIC:
  1652. tga_ret = gen_rtx_REG (Pmode, R0_REG);
  1653. emit_call_insn (gen_tls_global_dynamic (tga_ret, op1));
  1654. tmp = gen_reg_rtx (Pmode);
  1655. emit_move_insn (tmp, tga_ret);
  1656. op1 = tmp;
  1657. break;
  1658. case TLS_MODEL_LOCAL_DYNAMIC:
  1659. tga_ret = gen_rtx_REG (Pmode, R0_REG);
  1660. emit_call_insn (gen_tls_local_dynamic (tga_ret, op1));
  1661. tmp = gen_reg_rtx (Pmode);
  1662. emit_move_insn (tmp, tga_ret);
  1663. if (register_operand (op0, Pmode))
  1664. tmp2 = op0;
  1665. else
  1666. tmp2 = gen_reg_rtx (Pmode);
  1667. emit_insn (gen_symDTPOFF2reg (tmp2, op1, tmp));
  1668. op1 = tmp2;
  1669. break;
  1670. case TLS_MODEL_INITIAL_EXEC:
  1671. tga_op1 = !can_create_pseudo_p () ? op0 : gen_reg_rtx (Pmode);
  1672. tmp = gen_sym2GOTTPOFF (op1);
  1673. emit_insn (gen_tls_initial_exec (tga_op1, tmp));
  1674. op1 = tga_op1;
  1675. break;
  1676. case TLS_MODEL_LOCAL_EXEC:
  1677. tmp2 = gen_reg_rtx (Pmode);
  1678. emit_insn (gen_store_gbr (tmp2));
  1679. tmp = gen_reg_rtx (Pmode);
  1680. emit_insn (gen_symTPOFF2reg (tmp, op1));
  1681. if (register_operand (op0, Pmode))
  1682. op1 = op0;
  1683. else
  1684. op1 = gen_reg_rtx (Pmode);
  1685. emit_insn (gen_addsi3 (op1, tmp, tmp2));
  1686. break;
  1687. default:
  1688. gcc_unreachable ();
  1689. }
  1690. if (opc)
  1691. emit_insn (gen_addsi3 (op1, op1, force_reg (SImode, opc)));
  1692. operands[1] = op1;
  1693. }
  1694. }
  1695. }
  1696. /* Implement the canonicalize_comparison target hook for the combine
  1697. pass. For the target hook this function is invoked via
  1698. sh_canonicalize_comparison. This function is also re-used to
  1699. canonicalize comparisons in cbranch pattern expanders. */
  1700. static void
  1701. sh_canonicalize_comparison (enum rtx_code& cmp, rtx& op0, rtx& op1,
  1702. machine_mode mode,
  1703. bool op0_preserve_value)
  1704. {
  1705. /* When invoked from within the combine pass the mode is not specified,
  1706. so try to get it from one of the operands. */
  1707. if (mode == VOIDmode)
  1708. mode = GET_MODE (op0);
  1709. if (mode == VOIDmode)
  1710. mode = GET_MODE (op1);
  1711. // We need to have a mode to do something useful here.
  1712. if (mode == VOIDmode)
  1713. return;
  1714. // Currently, we don't deal with floats here.
  1715. if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  1716. return;
  1717. // Make sure that the constant operand is the second operand.
  1718. if (CONST_INT_P (op0) && !CONST_INT_P (op1))
  1719. {
  1720. if (op0_preserve_value)
  1721. return;
  1722. std::swap (op0, op1);
  1723. cmp = swap_condition (cmp);
  1724. }
  1725. if (CONST_INT_P (op1))
  1726. {
  1727. /* Try to adjust the constant operand in such a way that available
  1728. comparison insns can be utilized better and the constant can be
  1729. loaded with a 'mov #imm,Rm' insn. This avoids a load from the
  1730. constant pool. */
  1731. const HOST_WIDE_INT val = INTVAL (op1);
  1732. /* x > -1 --> x >= 0
  1733. x > 0xFFFFFF7F --> x >= 0xFFFFFF80
  1734. x <= -1 --> x < 0
  1735. x <= 0xFFFFFF7F --> x < 0xFFFFFF80 */
  1736. if ((val == -1 || val == -0x81) && (cmp == GT || cmp == LE))
  1737. {
  1738. cmp = cmp == GT ? GE : LT;
  1739. op1 = gen_int_mode (val + 1, mode);
  1740. }
  1741. /* x >= 1 --> x > 0
  1742. x >= 0x80 --> x > 0x7F
  1743. x < 1 --> x <= 0
  1744. x < 0x80 --> x <= 0x7F */
  1745. else if ((val == 1 || val == 0x80) && (cmp == GE || cmp == LT))
  1746. {
  1747. cmp = cmp == GE ? GT : LE;
  1748. op1 = gen_int_mode (val - 1, mode);
  1749. }
  1750. /* unsigned x >= 1 --> x != 0
  1751. unsigned x < 1 --> x == 0 */
  1752. else if (val == 1 && (cmp == GEU || cmp == LTU))
  1753. {
  1754. cmp = cmp == GEU ? NE : EQ;
  1755. op1 = CONST0_RTX (mode);
  1756. }
  1757. /* unsigned x >= 0x80 --> unsigned x > 0x7F
  1758. unsigned x < 0x80 --> unsigned x < 0x7F */
  1759. else if (val == 0x80 && (cmp == GEU || cmp == LTU))
  1760. {
  1761. cmp = cmp == GEU ? GTU : LEU;
  1762. op1 = gen_int_mode (val - 1, mode);
  1763. }
  1764. /* unsigned x > 0 --> x != 0
  1765. unsigned x <= 0 --> x == 0 */
  1766. else if (val == 0 && (cmp == GTU || cmp == LEU))
  1767. cmp = cmp == GTU ? NE : EQ;
  1768. /* unsigned x > 0x7FFFFFFF --> signed x < 0
  1769. unsigned x <= 0x7FFFFFFF --> signed x >= 0 */
  1770. else if (mode == SImode && (cmp == GTU || cmp == LEU)
  1771. && val == 0x7FFFFFFF)
  1772. {
  1773. cmp = cmp == GTU ? LT : GE;
  1774. op1 = const0_rtx;
  1775. }
  1776. /* unsigned x >= 0x80000000 --> signed x < 0
  1777. unsigned x < 0x80000000 --> signed x >= 0 */
  1778. else if (mode == SImode && (cmp == GEU || cmp == LTU)
  1779. && (unsigned HOST_WIDE_INT)val
  1780. == ((unsigned HOST_WIDE_INT)0x7FFFFFFF + 1))
  1781. {
  1782. cmp = cmp == GEU ? LT : GE;
  1783. op1 = const0_rtx;
  1784. }
  1785. }
  1786. }
  1787. /* This function implements the canonicalize_comparison target hook.
  1788. This wrapper around the internally used sh_canonicalize_comparison
  1789. function is needed to do the enum rtx_code <-> int conversion.
  1790. Target hooks cannot use enum rtx_code in its definition. */
  1791. static void
  1792. sh_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
  1793. bool op0_preserve_value)
  1794. {
  1795. enum rtx_code tmp_code = (enum rtx_code)*code;
  1796. sh_canonicalize_comparison (tmp_code, *op0, *op1,
  1797. VOIDmode, op0_preserve_value);
  1798. *code = (int)tmp_code;
  1799. }
  1800. /* This function implements the legitimate_combined_insn target hook,
  1801. which the combine pass uses to early reject combined insns, before
  1802. it tries to recog the insn and determine its cost. */
  1803. static bool
  1804. sh_legitimate_combined_insn (rtx_insn* insn)
  1805. {
  1806. /* Reject combinations of memory loads and zero extensions, as these
  1807. interfere with other combine patterns such as zero extracts and bit
  1808. tests. The SH2A movu.{b|w} insns are formed later in the
  1809. 'sh_optimize_extu_exts' pass after combine/split1. */
  1810. rtx p = PATTERN (insn);
  1811. if (GET_CODE (p) == SET
  1812. && REG_P (XEXP (p, 0)) && GET_MODE (XEXP (p, 0)) == SImode
  1813. && GET_CODE (XEXP (p, 1)) == ZERO_EXTEND
  1814. && MEM_P (XEXP (XEXP (p, 1), 0)))
  1815. return false;
  1816. return true;
  1817. }
  1818. bool
  1819. sh_fixed_condition_code_regs (unsigned int* p1, unsigned int* p2)
  1820. {
  1821. *p1 = T_REG;
  1822. *p2 = INVALID_REGNUM;
  1823. return true;
  1824. }
  1825. enum rtx_code
  1826. prepare_cbranch_operands (rtx *operands, machine_mode mode,
  1827. enum rtx_code comparison)
  1828. {
  1829. /* The scratch reg is only available when this is invoked from within
  1830. the cbranchdi4_i splitter, through expand_cbranchdi4. */
  1831. rtx scratch = NULL_RTX;
  1832. if (comparison == LAST_AND_UNUSED_RTX_CODE)
  1833. comparison = GET_CODE (operands[0]);
  1834. else
  1835. scratch = operands[4];
  1836. sh_canonicalize_comparison (comparison, operands[1], operands[2],
  1837. mode, false);
  1838. /* Notice that this function is also invoked after reload by
  1839. the cbranchdi4_i pattern, through expand_cbranchdi4. */
  1840. rtx op1 = operands[1];
  1841. if (can_create_pseudo_p ())
  1842. operands[1] = force_reg (mode, op1);
  1843. /* When we are handling DImode comparisons, we want to keep constants so
  1844. that we can optimize the component comparisons; however, memory loads
  1845. are better issued as a whole so that they can be scheduled well.
  1846. SImode equality comparisons allow I08 constants, but only when they
  1847. compare r0. Hence, if operands[1] has to be loaded from somewhere else
  1848. into a register, that register might as well be r0, and we allow the
  1849. constant. If it is already in a register, this is likely to be
  1850. allocated to a different hard register, thus we load the constant into
  1851. a register unless it is zero. */
  1852. if (!REG_P (operands[2])
  1853. && (!CONST_INT_P (operands[2])
  1854. || (mode == SImode && operands[2] != CONST0_RTX (SImode)
  1855. && ((comparison != EQ && comparison != NE)
  1856. || (REG_P (op1) && REGNO (op1) != R0_REG)
  1857. || !satisfies_constraint_I08 (operands[2])))))
  1858. {
  1859. if (scratch && GET_MODE (scratch) == mode)
  1860. {
  1861. emit_move_insn (scratch, operands[2]);
  1862. operands[2] = scratch;
  1863. }
  1864. else if (can_create_pseudo_p ())
  1865. operands[2] = force_reg (mode, operands[2]);
  1866. }
  1867. return comparison;
  1868. }
  1869. void
  1870. expand_cbranchsi4 (rtx *operands, enum rtx_code comparison, int probability)
  1871. {
  1872. rtx (*branch_expander) (rtx) = gen_branch_true;
  1873. comparison = prepare_cbranch_operands (operands, SImode, comparison);
  1874. switch (comparison)
  1875. {
  1876. case NE: case LT: case LE: case LTU: case LEU:
  1877. comparison = reverse_condition (comparison);
  1878. branch_expander = gen_branch_false;
  1879. default: ;
  1880. }
  1881. emit_insn (gen_rtx_SET (VOIDmode, get_t_reg_rtx (),
  1882. gen_rtx_fmt_ee (comparison, SImode,
  1883. operands[1], operands[2])));
  1884. rtx_insn *jump = emit_jump_insn (branch_expander (operands[3]));
  1885. if (probability >= 0)
  1886. add_int_reg_note (jump, REG_BR_PROB, probability);
  1887. }
  1888. /* ??? How should we distribute probabilities when more than one branch
  1889. is generated. So far we only have some ad-hoc observations:
  1890. - If the operands are random, they are likely to differ in both parts.
  1891. - If comparing items in a hash chain, the operands are random or equal;
  1892. operation should be EQ or NE.
  1893. - If items are searched in an ordered tree from the root, we can expect
  1894. the highpart to be unequal about half of the time; operation should be
  1895. an inequality comparison, operands non-constant, and overall probability
  1896. about 50%. Likewise for quicksort.
  1897. - Range checks will be often made against constants. Even if we assume for
  1898. simplicity an even distribution of the non-constant operand over a
  1899. sub-range here, the same probability could be generated with differently
  1900. wide sub-ranges - as long as the ratio of the part of the subrange that
  1901. is before the threshold to the part that comes after the threshold stays
  1902. the same. Thus, we can't really tell anything here;
  1903. assuming random distribution is at least simple.
  1904. */
  1905. bool
  1906. expand_cbranchdi4 (rtx *operands, enum rtx_code comparison)
  1907. {
  1908. enum rtx_code msw_taken, msw_skip, lsw_taken;
  1909. rtx_code_label *skip_label = NULL;
  1910. rtx op1h, op1l, op2h, op2l;
  1911. int num_branches;
  1912. int prob, rev_prob;
  1913. int msw_taken_prob = -1, msw_skip_prob = -1, lsw_taken_prob = -1;
  1914. rtx scratch = operands[4];
  1915. comparison = prepare_cbranch_operands (operands, DImode, comparison);
  1916. op1h = gen_highpart_mode (SImode, DImode, operands[1]);
  1917. op2h = gen_highpart_mode (SImode, DImode, operands[2]);
  1918. op1l = gen_lowpart (SImode, operands[1]);
  1919. op2l = gen_lowpart (SImode, operands[2]);
  1920. msw_taken = msw_skip = lsw_taken = LAST_AND_UNUSED_RTX_CODE;
  1921. prob = split_branch_probability;
  1922. rev_prob = REG_BR_PROB_BASE - prob;
  1923. switch (comparison)
  1924. {
  1925. /* ??? Should we use the cmpeqdi_t pattern for equality comparisons?
  1926. That costs 1 cycle more when the first branch can be predicted taken,
  1927. but saves us mispredicts because only one branch needs prediction.
  1928. It also enables generating the cmpeqdi_t-1 pattern. */
  1929. case EQ:
  1930. if (TARGET_CMPEQDI_T)
  1931. {
  1932. emit_insn (gen_cmpeqdi_t (operands[1], operands[2]));
  1933. emit_jump_insn (gen_branch_true (operands[3]));
  1934. return true;
  1935. }
  1936. msw_skip = NE;
  1937. lsw_taken = EQ;
  1938. if (prob >= 0)
  1939. {
  1940. // If we had more precision, we'd use rev_prob - (rev_prob >> 32) .
  1941. msw_skip_prob = rev_prob;
  1942. if (REG_BR_PROB_BASE <= 65535)
  1943. lsw_taken_prob = prob ? REG_BR_PROB_BASE : 0;
  1944. else
  1945. {
  1946. lsw_taken_prob
  1947. = (prob
  1948. ? (REG_BR_PROB_BASE
  1949. - ((gcov_type) REG_BR_PROB_BASE * rev_prob
  1950. / ((gcov_type) prob << 32)))
  1951. : 0);
  1952. }
  1953. }
  1954. break;
  1955. case NE:
  1956. if (TARGET_CMPEQDI_T)
  1957. {
  1958. emit_insn (gen_cmpeqdi_t (operands[1], operands[2]));
  1959. emit_jump_insn (gen_branch_false (operands[3]));
  1960. return true;
  1961. }
  1962. msw_taken = NE;
  1963. msw_taken_prob = prob;
  1964. lsw_taken = NE;
  1965. lsw_taken_prob = 0;
  1966. break;
  1967. case GTU: case GT:
  1968. msw_taken = comparison;
  1969. if (CONST_INT_P (op2l) && INTVAL (op2l) == -1)
  1970. break;
  1971. if (comparison != GTU || op2h != CONST0_RTX (SImode))
  1972. msw_skip = swap_condition (msw_taken);
  1973. lsw_taken = GTU;
  1974. break;
  1975. case GEU: case GE:
  1976. if (op2l == CONST0_RTX (SImode))
  1977. msw_taken = comparison;
  1978. else
  1979. {
  1980. msw_taken = comparison == GE ? GT : GTU;
  1981. msw_skip = swap_condition (msw_taken);
  1982. lsw_taken = GEU;
  1983. }
  1984. break;
  1985. case LTU: case LT:
  1986. msw_taken = comparison;
  1987. if (op2l == CONST0_RTX (SImode))
  1988. break;
  1989. msw_skip = swap_condition (msw_taken);
  1990. lsw_taken = LTU;
  1991. break;
  1992. case LEU: case LE:
  1993. if (CONST_INT_P (op2l) && INTVAL (op2l) == -1)
  1994. msw_taken = comparison;
  1995. else
  1996. {
  1997. lsw_taken = LEU;
  1998. if (comparison == LE)
  1999. msw_taken = LT;
  2000. else if (op2h != CONST0_RTX (SImode))
  2001. msw_taken = LTU;
  2002. else
  2003. {
  2004. msw_skip = swap_condition (LTU);
  2005. break;
  2006. }
  2007. msw_skip = swap_condition (msw_taken);
  2008. }
  2009. break;
  2010. default: return false;
  2011. }
  2012. num_branches = ((msw_taken != LAST_AND_UNUSED_RTX_CODE)
  2013. + (msw_skip != LAST_AND_UNUSED_RTX_CODE)
  2014. + (lsw_taken != LAST_AND_UNUSED_RTX_CODE));
  2015. if (comparison != EQ && comparison != NE && num_branches > 1)
  2016. {
  2017. if (!CONSTANT_P (operands[2])
  2018. && prob >= (int) (REG_BR_PROB_BASE * 3 / 8U)
  2019. && prob <= (int) (REG_BR_PROB_BASE * 5 / 8U))
  2020. {
  2021. msw_taken_prob = prob / 2U;
  2022. msw_skip_prob
  2023. = REG_BR_PROB_BASE * rev_prob / (REG_BR_PROB_BASE + rev_prob);
  2024. lsw_taken_prob = prob;
  2025. }
  2026. else
  2027. {
  2028. msw_taken_prob = prob;
  2029. msw_skip_prob = REG_BR_PROB_BASE;
  2030. /* ??? If we have a constant op2h, should we use that when
  2031. calculating lsw_taken_prob? */
  2032. lsw_taken_prob = prob;
  2033. }
  2034. }
  2035. operands[1] = op1h;
  2036. operands[2] = op2h;
  2037. operands[4] = NULL_RTX;
  2038. if (reload_completed
  2039. && ! arith_reg_or_0_operand (op2h, SImode)
  2040. && (true_regnum (op1h) || (comparison != EQ && comparison != NE))
  2041. && (msw_taken != LAST_AND_UNUSED_RTX_CODE
  2042. || msw_skip != LAST_AND_UNUSED_RTX_CODE))
  2043. {
  2044. emit_move_insn (scratch, operands[2]);
  2045. operands[2] = scratch;
  2046. }
  2047. if (msw_taken != LAST_AND_UNUSED_RTX_CODE)
  2048. expand_cbranchsi4 (operands, msw_taken, msw_taken_prob);
  2049. if (msw_skip != LAST_AND_UNUSED_RTX_CODE)
  2050. {
  2051. rtx taken_label = operands[3];
  2052. /* Operands were possibly modified, but msw_skip doesn't expect this.
  2053. Always use the original ones. */
  2054. if (msw_taken != LAST_AND_UNUSED_RTX_CODE)
  2055. {
  2056. operands[1] = op1h;
  2057. operands[2] = op2h;
  2058. if (reload_completed
  2059. && ! arith_reg_or_0_operand (op2h, SImode)
  2060. && (true_regnum (op1h) || (comparison != EQ && comparison != NE)))
  2061. {
  2062. emit_move_insn (scratch, operands[2]);
  2063. operands[2] = scratch;
  2064. }
  2065. }
  2066. operands[3] = skip_label = gen_label_rtx ();
  2067. expand_cbranchsi4 (operands, msw_skip, msw_skip_prob);
  2068. operands[3] = taken_label;
  2069. }
  2070. operands[1] = op1l;
  2071. operands[2] = op2l;
  2072. if (lsw_taken != LAST_AND_UNUSED_RTX_CODE)
  2073. {
  2074. if (reload_completed
  2075. && ! arith_reg_or_0_operand (op2l, SImode)
  2076. && (true_regnum (op1l) || (lsw_taken != EQ && lsw_taken != NE)))
  2077. {
  2078. emit_move_insn (scratch, operands[2]);
  2079. operands[2] = scratch;
  2080. }
  2081. expand_cbranchsi4 (operands, lsw_taken, lsw_taken_prob);
  2082. }
  2083. if (msw_skip != LAST_AND_UNUSED_RTX_CODE)
  2084. emit_label (skip_label);
  2085. return true;
  2086. }
  2087. /* Given an operand, return 1 if the evaluated operand plugged into an
  2088. if_then_else will result in a branch_true, 0 if branch_false, or
  2089. -1 if neither nor applies. The truth table goes like this:
  2090. op | cmpval | code | result
  2091. ---------+--------+---------+--------------------
  2092. T (0) | 0 | EQ (1) | 0 = 0 ^ (0 == 1)
  2093. T (0) | 1 | EQ (1) | 1 = 0 ^ (1 == 1)
  2094. T (0) | 0 | NE (0) | 1 = 0 ^ (0 == 0)
  2095. T (0) | 1 | NE (0) | 0 = 0 ^ (1 == 0)
  2096. !T (1) | 0 | EQ (1) | 1 = 1 ^ (0 == 1)
  2097. !T (1) | 1 | EQ (1) | 0 = 1 ^ (1 == 1)
  2098. !T (1) | 0 | NE (0) | 0 = 1 ^ (0 == 0)
  2099. !T (1) | 1 | NE (0) | 1 = 1 ^ (1 == 0) */
  2100. int
  2101. sh_eval_treg_value (rtx op)
  2102. {
  2103. if (t_reg_operand (op, GET_MODE (op)))
  2104. return 1;
  2105. if (negt_reg_operand (op, GET_MODE (op)))
  2106. return 0;
  2107. rtx_code code = GET_CODE (op);
  2108. if ((code != EQ && code != NE) || !CONST_INT_P (XEXP (op, 1)))
  2109. return -1;
  2110. int cmpop = code == EQ ? 1 : 0;
  2111. int cmpval = INTVAL (XEXP (op, 1));
  2112. if (cmpval != 0 && cmpval != 1)
  2113. return -1;
  2114. int t;
  2115. if (t_reg_operand (XEXP (op, 0), GET_MODE (XEXP (op, 0))))
  2116. t = 0;
  2117. else if (negt_reg_operand (XEXP (op, 0), GET_MODE (XEXP (op, 0))))
  2118. t = 1;
  2119. else
  2120. return -1;
  2121. return t ^ (cmpval == cmpop);
  2122. }
  2123. /* Emit INSN, possibly in a PARALLEL with an USE/CLOBBER of FPSCR bits in case
  2124. of floating-point comparisons. */
  2125. static void
  2126. sh_emit_set_t_insn (rtx insn, machine_mode mode)
  2127. {
  2128. if (TARGET_FPU_ANY && GET_MODE_CLASS (mode) == MODE_FLOAT
  2129. && GET_CODE (insn) != PARALLEL)
  2130. {
  2131. insn = gen_rtx_PARALLEL (VOIDmode,
  2132. gen_rtvec (3, insn,
  2133. gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, FPSCR_STAT_REG)),
  2134. gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, FPSCR_MODES_REG))));
  2135. }
  2136. emit_insn (insn);
  2137. }
  2138. /* Prepare the operands for an scc instruction; make sure that the
  2139. compare has been done and the result is in T_REG. */
  2140. void
  2141. sh_emit_scc_to_t (enum rtx_code code, rtx op0, rtx op1)
  2142. {
  2143. rtx t_reg = get_t_reg_rtx ();
  2144. enum rtx_code oldcode = code;
  2145. machine_mode mode;
  2146. /* First need a compare insn. */
  2147. switch (code)
  2148. {
  2149. case NE:
  2150. /* It isn't possible to handle this case. */
  2151. gcc_unreachable ();
  2152. case LT:
  2153. code = GT;
  2154. break;
  2155. case LE:
  2156. code = GE;
  2157. break;
  2158. case LTU:
  2159. code = GTU;
  2160. break;
  2161. case LEU:
  2162. code = GEU;
  2163. break;
  2164. default:
  2165. break;
  2166. }
  2167. if (code != oldcode)
  2168. std::swap (op0, op1);
  2169. mode = GET_MODE (op0);
  2170. if (mode == VOIDmode)
  2171. mode = GET_MODE (op1);
  2172. op0 = force_reg (mode, op0);
  2173. if ((code != EQ && code != NE
  2174. && (op1 != const0_rtx
  2175. || code == GTU || code == GEU || code == LTU || code == LEU))
  2176. || (mode == DImode && op1 != const0_rtx)
  2177. || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
  2178. op1 = force_reg (mode, op1);
  2179. sh_emit_set_t_insn (gen_rtx_SET (VOIDmode, t_reg,
  2180. gen_rtx_fmt_ee (code, SImode, op0, op1)),
  2181. mode);
  2182. }
  2183. rtx
  2184. sh_emit_cheap_store_flag (machine_mode mode, enum rtx_code code,
  2185. rtx op0, rtx op1)
  2186. {
  2187. rtx target = gen_reg_rtx (SImode);
  2188. rtx tmp;
  2189. gcc_assert (TARGET_SHMEDIA);
  2190. switch (code)
  2191. {
  2192. case EQ:
  2193. case GT:
  2194. case LT:
  2195. case UNORDERED:
  2196. case GTU:
  2197. case LTU:
  2198. tmp = gen_rtx_fmt_ee (code, SImode, op0, op1);
  2199. emit_insn (gen_cstore4_media (target, tmp, op0, op1));
  2200. code = NE;
  2201. break;
  2202. case NE:
  2203. case GE:
  2204. case LE:
  2205. case ORDERED:
  2206. case GEU:
  2207. case LEU:
  2208. tmp = gen_rtx_fmt_ee (reverse_condition (code), mode, op0, op1);
  2209. emit_insn (gen_cstore4_media (target, tmp, op0, op1));
  2210. code = EQ;
  2211. break;
  2212. case UNEQ:
  2213. case UNGE:
  2214. case UNGT:
  2215. case UNLE:
  2216. case UNLT:
  2217. case LTGT:
  2218. return NULL_RTX;
  2219. default:
  2220. gcc_unreachable ();
  2221. }
  2222. if (mode == DImode)
  2223. {
  2224. rtx t2 = gen_reg_rtx (DImode);
  2225. emit_insn (gen_extendsidi2 (t2, target));
  2226. target = t2;
  2227. }
  2228. return gen_rtx_fmt_ee (code, VOIDmode, target, const0_rtx);
  2229. }
  2230. /* Called from the md file, set up the operands of a compare instruction. */
  2231. void
  2232. sh_emit_compare_and_branch (rtx *operands, machine_mode mode)
  2233. {
  2234. enum rtx_code code = GET_CODE (operands[0]);
  2235. enum rtx_code branch_code;
  2236. rtx op0 = operands[1];
  2237. rtx op1 = operands[2];
  2238. rtx insn;
  2239. bool need_ccmpeq = false;
  2240. if (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT)
  2241. {
  2242. op0 = force_reg (mode, op0);
  2243. op1 = force_reg (mode, op1);
  2244. }
  2245. else
  2246. {
  2247. if (code != EQ || mode == DImode)
  2248. {
  2249. /* Force args into regs, since we can't use constants here. */
  2250. op0 = force_reg (mode, op0);
  2251. if (op1 != const0_rtx || code == GTU || code == GEU)
  2252. op1 = force_reg (mode, op1);
  2253. }
  2254. }
  2255. if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  2256. {
  2257. if (code == LT
  2258. || (code == LE && TARGET_IEEE && TARGET_SH2E)
  2259. || (code == GE && !(TARGET_IEEE && TARGET_SH2E)))
  2260. {
  2261. std::swap (op0, op1);
  2262. code = swap_condition (code);
  2263. }
  2264. /* GE becomes fcmp/gt+fcmp/eq, for SH2E and TARGET_IEEE only. */
  2265. if (code == GE)
  2266. {
  2267. gcc_assert (TARGET_IEEE && TARGET_SH2E);
  2268. need_ccmpeq = true;
  2269. code = GT;
  2270. }
  2271. /* Now we can have EQ, NE, GT, LE. NE and LE are then transformed
  2272. to EQ/GT respectively. */
  2273. gcc_assert (code == EQ || code == GT || code == NE || code == LE);
  2274. }
  2275. switch (code)
  2276. {
  2277. case EQ:
  2278. case GT:
  2279. case GE:
  2280. case GTU:
  2281. case GEU:
  2282. branch_code = code;
  2283. break;
  2284. case NE:
  2285. case LT:
  2286. case LE:
  2287. case LTU:
  2288. case LEU:
  2289. branch_code = reverse_condition (code);
  2290. break;
  2291. default:
  2292. gcc_unreachable ();
  2293. }
  2294. insn = gen_rtx_SET (VOIDmode,
  2295. get_t_reg_rtx (),
  2296. gen_rtx_fmt_ee (branch_code, SImode, op0, op1));
  2297. sh_emit_set_t_insn (insn, mode);
  2298. if (need_ccmpeq)
  2299. sh_emit_set_t_insn (gen_ieee_ccmpeqsf_t (op0, op1), mode);
  2300. if (branch_code == code)
  2301. emit_jump_insn (gen_branch_true (operands[3]));
  2302. else
  2303. emit_jump_insn (gen_branch_false (operands[3]));
  2304. }
  2305. void
  2306. sh_emit_compare_and_set (rtx *operands, machine_mode mode)
  2307. {
  2308. enum rtx_code code = GET_CODE (operands[1]);
  2309. rtx op0 = operands[2];
  2310. rtx op1 = operands[3];
  2311. rtx_code_label *lab = NULL;
  2312. bool invert = false;
  2313. op0 = force_reg (mode, op0);
  2314. if ((code != EQ && code != NE
  2315. && (op1 != const0_rtx
  2316. || code == GTU || code == GEU || code == LTU || code == LEU))
  2317. || (mode == DImode && op1 != const0_rtx)
  2318. || (TARGET_SH2E && GET_MODE_CLASS (mode) == MODE_FLOAT))
  2319. op1 = force_reg (mode, op1);
  2320. if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  2321. {
  2322. if (code == LT || code == LE)
  2323. {
  2324. std::swap (op0, op1);
  2325. code = swap_condition (code);
  2326. }
  2327. if (code == GE)
  2328. {
  2329. if (TARGET_IEEE)
  2330. {
  2331. lab = gen_label_rtx ();
  2332. sh_emit_scc_to_t (EQ, op0, op1);
  2333. emit_jump_insn (gen_branch_true (lab));
  2334. code = GT;
  2335. }
  2336. else
  2337. {
  2338. code = LT;
  2339. invert = true;
  2340. }
  2341. }
  2342. }
  2343. if (code == NE)
  2344. {
  2345. code = EQ;
  2346. invert = true;
  2347. }
  2348. sh_emit_scc_to_t (code, op0, op1);
  2349. if (lab)
  2350. emit_label (lab);
  2351. if (invert)
  2352. emit_insn (gen_movnegt (operands[0], get_t_reg_rtx ()));
  2353. else
  2354. emit_move_insn (operands[0], get_t_reg_rtx ());
  2355. }
  2356. /* Functions to output assembly code. */
  2357. /* Return a sequence of instructions to perform DI or DF move.
  2358. Since the SH cannot move a DI or DF in one instruction, we have
  2359. to take care when we see overlapping source and dest registers. */
  2360. const char *
  2361. output_movedouble (rtx insn ATTRIBUTE_UNUSED, rtx operands[],
  2362. machine_mode mode)
  2363. {
  2364. rtx dst = operands[0];
  2365. rtx src = operands[1];
  2366. if (MEM_P (dst)
  2367. && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
  2368. return "mov.l %T1,%0" "\n"
  2369. " mov.l %1,%0";
  2370. if (register_operand (dst, mode)
  2371. && register_operand (src, mode))
  2372. {
  2373. if (REGNO (src) == MACH_REG)
  2374. return "sts mach,%S0" "\n"
  2375. " sts macl,%R0";
  2376. /* When mov.d r1,r2 do r2->r3 then r1->r2;
  2377. when mov.d r1,r0 do r1->r0 then r2->r1. */
  2378. if (REGNO (src) + 1 == REGNO (dst))
  2379. return "mov %T1,%T0" "\n"
  2380. " mov %1,%0";
  2381. else
  2382. return "mov %1,%0" "\n"
  2383. " mov %T1,%T0";
  2384. }
  2385. else if (CONST_INT_P (src))
  2386. {
  2387. if (INTVAL (src) < 0)
  2388. output_asm_insn ("mov #-1,%S0", operands);
  2389. else
  2390. output_asm_insn ("mov #0,%S0", operands);
  2391. return "mov %1,%R0";
  2392. }
  2393. else if (MEM_P (src))
  2394. {
  2395. int ptrreg = -1;
  2396. int dreg = REGNO (dst);
  2397. rtx inside = XEXP (src, 0);
  2398. switch (GET_CODE (inside))
  2399. {
  2400. case REG:
  2401. ptrreg = REGNO (inside);
  2402. break;
  2403. case SUBREG:
  2404. ptrreg = subreg_regno (inside);
  2405. break;
  2406. case PLUS:
  2407. ptrreg = REGNO (XEXP (inside, 0));
  2408. /* ??? A r0+REG address shouldn't be possible here, because it isn't
  2409. an offsettable address. Unfortunately, offsettable addresses use
  2410. QImode to check the offset, and a QImode offsettable address
  2411. requires r0 for the other operand, which is not currently
  2412. supported, so we can't use the 'o' constraint.
  2413. Thus we must check for and handle r0+REG addresses here.
  2414. We punt for now, since this is likely very rare. */
  2415. gcc_assert (!REG_P (XEXP (inside, 1)));
  2416. break;
  2417. case LABEL_REF:
  2418. return "mov.l %1,%0" "\n"
  2419. " mov.l %1+4,%T0";
  2420. case POST_INC:
  2421. return "mov.l %1,%0" "\n"
  2422. " mov.l %1,%T0";
  2423. default:
  2424. gcc_unreachable ();
  2425. }
  2426. /* Work out the safe way to copy. Copy into the second half first. */
  2427. if (dreg == ptrreg)
  2428. return "mov.l %T1,%T0" "\n"
  2429. " mov.l %1,%0";
  2430. }
  2431. return "mov.l %1,%0" "\n"
  2432. " mov.l %T1,%T0";
  2433. }
  2434. /* Print an instruction which would have gone into a delay slot after
  2435. another instruction, but couldn't because the other instruction expanded
  2436. into a sequence where putting the slot insn at the end wouldn't work. */
  2437. static void
  2438. print_slot (rtx_sequence *seq)
  2439. {
  2440. final_scan_insn (seq->insn (1), asm_out_file, optimize, 1, NULL);
  2441. seq->insn (1)->set_deleted ();
  2442. }
  2443. const char *
  2444. output_far_jump (rtx_insn *insn, rtx op)
  2445. {
  2446. struct { rtx lab, reg, op; } this_jmp;
  2447. rtx_code_label *braf_base_lab = NULL;
  2448. const char *jump;
  2449. int far;
  2450. int offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
  2451. rtx_insn *prev;
  2452. this_jmp.lab = gen_label_rtx ();
  2453. if (TARGET_SH2
  2454. && offset >= -32764
  2455. && offset - get_attr_length (insn) <= 32766)
  2456. {
  2457. far = 0;
  2458. jump = "mov.w %O0,%1" "\n"
  2459. " braf %1";
  2460. }
  2461. else
  2462. {
  2463. far = 1;
  2464. if (flag_pic)
  2465. {
  2466. if (TARGET_SH2)
  2467. jump = "mov.l %O0,%1" "\n"
  2468. " braf %1";
  2469. else
  2470. jump = "mov.l r0,@-r15" "\n"
  2471. " mova %O0,r0" "\n"
  2472. " mov.l @r0,%1" "\n"
  2473. " add r0,%1" "\n"
  2474. " mov.l @r15+,r0" "\n"
  2475. " jmp @%1";
  2476. }
  2477. else
  2478. jump = "mov.l %O0,%1" "\n"
  2479. " jmp @%1";
  2480. }
  2481. /* If we have a scratch register available, use it. */
  2482. if (NONJUMP_INSN_P ((prev = prev_nonnote_insn (insn)))
  2483. && INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
  2484. {
  2485. this_jmp.reg = SET_DEST (XVECEXP (PATTERN (prev), 0, 0));
  2486. if (REGNO (this_jmp.reg) == R0_REG && flag_pic && ! TARGET_SH2)
  2487. jump = "mov.l r1,@-r15" "\n"
  2488. " mova %O0,r0" "\n"
  2489. " mov.l @r0,r1" "\n"
  2490. " add r1,r0" "\n"
  2491. " mov.l @r15+,r1" "\n"
  2492. " jmp @%1";
  2493. output_asm_insn (jump, &this_jmp.lab);
  2494. if (dbr_sequence_length ())
  2495. print_slot (final_sequence);
  2496. else
  2497. output_asm_insn ("nop", 0);
  2498. }
  2499. else
  2500. {
  2501. /* Output the delay slot insn first if any. */
  2502. if (dbr_sequence_length ())
  2503. print_slot (final_sequence);
  2504. this_jmp.reg = gen_rtx_REG (SImode, 13);
  2505. /* We must keep the stack aligned to 8-byte boundaries on SH5.
  2506. Fortunately, MACL is fixed and call-clobbered, and we never
  2507. need its value across jumps, so save r13 in it instead of in
  2508. the stack. */
  2509. if (TARGET_SH5)
  2510. output_asm_insn ("lds r13,macl", 0);
  2511. else
  2512. output_asm_insn ("mov.l r13,@-r15", 0);
  2513. output_asm_insn (jump, &this_jmp.lab);
  2514. if (TARGET_SH5)
  2515. output_asm_insn ("sts macl,r13", 0);
  2516. else
  2517. output_asm_insn ("mov.l @r15+,r13", 0);
  2518. }
  2519. if (far && flag_pic && TARGET_SH2)
  2520. {
  2521. braf_base_lab = gen_label_rtx ();
  2522. (*targetm.asm_out.internal_label) (asm_out_file, "L",
  2523. CODE_LABEL_NUMBER (braf_base_lab));
  2524. }
  2525. if (far)
  2526. output_asm_insn (".align 2", 0);
  2527. (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (this_jmp.lab));
  2528. this_jmp.op = op;
  2529. if (far && flag_pic)
  2530. {
  2531. if (TARGET_SH2)
  2532. this_jmp.lab = braf_base_lab;
  2533. output_asm_insn (".long %O2-%O0", &this_jmp.lab);
  2534. }
  2535. else
  2536. output_asm_insn (far ? ".long %O2" : ".word %O2-%O0", &this_jmp.lab);
  2537. return "";
  2538. }
  2539. /* Local label counter, used for constants in the pool and inside
  2540. pattern branches. */
  2541. static int lf = 100;
  2542. /* Output code for ordinary branches. */
  2543. const char *
  2544. output_branch (int logic, rtx_insn *insn, rtx *operands)
  2545. {
  2546. switch (get_attr_length (insn))
  2547. {
  2548. case 6:
  2549. /* This can happen if filling the delay slot has caused a forward
  2550. branch to exceed its range (we could reverse it, but only
  2551. when we know we won't overextend other branches; this should
  2552. best be handled by relaxation).
  2553. It can also happen when other condbranches hoist delay slot insn
  2554. from their destination, thus leading to code size increase.
  2555. But the branch will still be in the range -4092..+4098 bytes. */
  2556. if (! TARGET_RELAX)
  2557. {
  2558. int label = lf++;
  2559. /* The call to print_slot will clobber the operands. */
  2560. rtx op0 = operands[0];
  2561. /* If the instruction in the delay slot is annulled (true), then
  2562. there is no delay slot where we can put it now. The only safe
  2563. place for it is after the label. final will do that by default. */
  2564. if (final_sequence
  2565. && ! INSN_ANNULLED_BRANCH_P (final_sequence->insn (0))
  2566. && get_attr_length (final_sequence->insn (1)))
  2567. {
  2568. asm_fprintf (asm_out_file, "\tb%s%ss\t%LLF%d\n", logic ? "f" : "t",
  2569. ASSEMBLER_DIALECT ? "/" : ".", label);
  2570. print_slot (final_sequence);
  2571. }
  2572. else
  2573. asm_fprintf (asm_out_file, "\tb%s\t%LLF%d\n", logic ? "f" : "t", label);
  2574. output_asm_insn ("bra\t%l0", &op0);
  2575. fprintf (asm_out_file, "\tnop\n");
  2576. (*targetm.asm_out.internal_label) (asm_out_file, "LF", label);
  2577. return "";
  2578. }
  2579. /* When relaxing, handle this like a short branch. The linker
  2580. will fix it up if it still doesn't fit after relaxation. */
  2581. case 2:
  2582. return logic ? "bt%.\t%l0" : "bf%.\t%l0";
  2583. /* These are for SH2e, in which we have to account for the
  2584. extra nop because of the hardware bug in annulled branches. */
  2585. case 8:
  2586. if (! TARGET_RELAX)
  2587. {
  2588. int label = lf++;
  2589. gcc_assert (!final_sequence
  2590. || !(INSN_ANNULLED_BRANCH_P
  2591. (XVECEXP (final_sequence, 0, 0))));
  2592. asm_fprintf (asm_out_file, "b%s%ss\t%LLF%d\n",
  2593. logic ? "f" : "t",
  2594. ASSEMBLER_DIALECT ? "/" : ".", label);
  2595. fprintf (asm_out_file, "\tnop\n");
  2596. output_asm_insn ("bra\t%l0", operands);
  2597. fprintf (asm_out_file, "\tnop\n");
  2598. (*targetm.asm_out.internal_label) (asm_out_file, "LF", label);
  2599. return "";
  2600. }
  2601. /* When relaxing, fall through. */
  2602. case 4:
  2603. {
  2604. char buffer[10];
  2605. sprintf (buffer, "b%s%ss\t%%l0",
  2606. logic ? "t" : "f",
  2607. ASSEMBLER_DIALECT ? "/" : ".");
  2608. output_asm_insn (buffer, &operands[0]);
  2609. return "nop";
  2610. }
  2611. default:
  2612. /* There should be no longer branches now - that would
  2613. indicate that something has destroyed the branches set
  2614. up in machine_dependent_reorg. */
  2615. gcc_unreachable ();
  2616. }
  2617. }
  2618. /* Output a code sequence for INSN using TEMPL with OPERANDS; but before,
  2619. fill in operands 9 as a label to the successor insn.
  2620. We try to use jump threading where possible.
  2621. IF CODE matches the comparison in the IF_THEN_ELSE of a following jump,
  2622. we assume the jump is taken. I.e. EQ means follow jmp and bf, NE means
  2623. follow jmp and bt, if the address is in range. */
  2624. const char *
  2625. output_branchy_insn (enum rtx_code code, const char *templ,
  2626. rtx_insn *insn, rtx *operands)
  2627. {
  2628. rtx_insn *next_insn = NEXT_INSN (insn);
  2629. if (next_insn && JUMP_P (next_insn) && condjump_p (next_insn))
  2630. {
  2631. rtx src = SET_SRC (PATTERN (next_insn));
  2632. if (GET_CODE (src) == IF_THEN_ELSE && GET_CODE (XEXP (src, 0)) != code)
  2633. {
  2634. /* Following branch not taken */
  2635. rtx_code_label *lab = gen_label_rtx ();
  2636. emit_label_after (lab, next_insn);
  2637. INSN_ADDRESSES_NEW (lab,
  2638. INSN_ADDRESSES (INSN_UID (next_insn))
  2639. + get_attr_length (next_insn));
  2640. operands[9] = lab;
  2641. return templ;
  2642. }
  2643. else
  2644. {
  2645. int offset = (branch_dest (next_insn)
  2646. - INSN_ADDRESSES (INSN_UID (next_insn)) + 4);
  2647. if (offset >= -252 && offset <= 258)
  2648. {
  2649. if (GET_CODE (src) == IF_THEN_ELSE)
  2650. /* branch_true */
  2651. src = XEXP (src, 1);
  2652. operands[9] = src;
  2653. return templ;
  2654. }
  2655. }
  2656. }
  2657. rtx_code_label *lab = gen_label_rtx ();
  2658. emit_label_after (lab, insn);
  2659. INSN_ADDRESSES_NEW (lab,
  2660. INSN_ADDRESSES (INSN_UID (insn))
  2661. + get_attr_length (insn));
  2662. operands[9] = lab;
  2663. return templ;
  2664. }
  2665. const char *
  2666. output_ieee_ccmpeq (rtx_insn *insn, rtx *operands)
  2667. {
  2668. return output_branchy_insn (NE, "bt %l9" "\n"
  2669. " fcmp/eq %1,%0",
  2670. insn, operands);
  2671. }
  2672. /* Output the start of the assembler file. */
  2673. static void
  2674. sh_file_start (void)
  2675. {
  2676. default_file_start ();
  2677. if (TARGET_ELF)
  2678. /* We need to show the text section with the proper
  2679. attributes as in TEXT_SECTION_ASM_OP, before dwarf2out
  2680. emits it without attributes in TEXT_SECTION_ASM_OP, else GAS
  2681. will complain. We can teach GAS specifically about the
  2682. default attributes for our choice of text section, but
  2683. then we would have to change GAS again if/when we change
  2684. the text section name. */
  2685. fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
  2686. else
  2687. /* Switch to the data section so that the coffsem symbol
  2688. isn't in the text section. */
  2689. switch_to_section (data_section);
  2690. if (TARGET_LITTLE_ENDIAN)
  2691. fputs ("\t.little\n", asm_out_file);
  2692. if (!TARGET_ELF)
  2693. {
  2694. if (TARGET_SHCOMPACT)
  2695. fputs ("\t.mode\tSHcompact\n", asm_out_file);
  2696. else if (TARGET_SHMEDIA)
  2697. fprintf (asm_out_file, "\t.mode\tSHmedia\n\t.abi\t%i\n",
  2698. TARGET_SHMEDIA64 ? 64 : 32);
  2699. }
  2700. }
  2701. /* Check if PAT includes UNSPEC_CALLER unspec pattern. */
  2702. static bool
  2703. unspec_caller_rtx_p (rtx pat)
  2704. {
  2705. rtx base, offset;
  2706. int i;
  2707. split_const (pat, &base, &offset);
  2708. if (GET_CODE (base) == UNSPEC)
  2709. {
  2710. if (XINT (base, 1) == UNSPEC_CALLER)
  2711. return true;
  2712. for (i = 0; i < XVECLEN (base, 0); i++)
  2713. if (unspec_caller_rtx_p (XVECEXP (base, 0, i)))
  2714. return true;
  2715. }
  2716. return false;
  2717. }
  2718. /* Indicate that INSN cannot be duplicated. This is true for insn
  2719. that generates a unique label. */
  2720. static bool
  2721. sh_cannot_copy_insn_p (rtx_insn *insn)
  2722. {
  2723. rtx pat;
  2724. if (!reload_completed || !flag_pic)
  2725. return false;
  2726. if (!NONJUMP_INSN_P (insn))
  2727. return false;
  2728. if (asm_noperands (insn) >= 0)
  2729. return false;
  2730. pat = PATTERN (insn);
  2731. if (GET_CODE (pat) != SET)
  2732. return false;
  2733. pat = SET_SRC (pat);
  2734. if (unspec_caller_rtx_p (pat))
  2735. return true;
  2736. return false;
  2737. }
  2738. /* Number of instructions used to make an arithmetic right shift by N. */
  2739. static const char ashiftrt_insns[] =
  2740. { 0,1,2,3,4,5,8,8,8,8,8,8,8,8,8,8,2,3,4,5,8,8,8,8,8,8,8,8,8,8,8,2};
  2741. /* Description of a logical left or right shift, when expanded to a sequence
  2742. of 1/2/8/16 shifts.
  2743. Notice that one bit right shifts clobber the T bit. One bit left shifts
  2744. are done with an 'add Rn,Rm' insn and thus do not clobber the T bit. */
  2745. enum
  2746. {
  2747. ASHL_CLOBBERS_T = 1 << 0,
  2748. LSHR_CLOBBERS_T = 1 << 1
  2749. };
  2750. struct ashl_lshr_sequence
  2751. {
  2752. char insn_count;
  2753. signed char amount[6];
  2754. char clobbers_t;
  2755. };
  2756. static const struct ashl_lshr_sequence ashl_lshr_seq[32] =
  2757. {
  2758. { 0, { 0 }, 0 }, // 0
  2759. { 1, { 1 }, LSHR_CLOBBERS_T },
  2760. { 1, { 2 }, 0 },
  2761. { 2, { 2, 1 }, LSHR_CLOBBERS_T },
  2762. { 2, { 2, 2 }, 0 }, // 4
  2763. { 3, { 2, 1, 2 }, LSHR_CLOBBERS_T },
  2764. { 3, { 2, 2, 2 }, 0 },
  2765. { 4, { 2, 2, 1, 2 }, LSHR_CLOBBERS_T },
  2766. { 1, { 8 }, 0 }, // 8
  2767. { 2, { 8, 1 }, LSHR_CLOBBERS_T },
  2768. { 2, { 8, 2 }, 0 },
  2769. { 3, { 8, 1, 2 }, LSHR_CLOBBERS_T },
  2770. { 3, { 8, 2, 2 }, 0 }, // 12
  2771. { 4, { 8, 2, 1, 2 }, LSHR_CLOBBERS_T },
  2772. { 3, { 8, -2, 8 }, 0 },
  2773. { 3, { 8, -1, 8 }, ASHL_CLOBBERS_T },
  2774. { 1, { 16 }, 0 }, // 16
  2775. { 2, { 16, 1 }, LSHR_CLOBBERS_T },
  2776. { 2, { 16, 2 }, 0 },
  2777. { 3, { 16, 1, 2 }, LSHR_CLOBBERS_T },
  2778. { 3, { 16, 2, 2 }, 0 }, // 20
  2779. { 4, { 16, 2, 1, 2 }, LSHR_CLOBBERS_T },
  2780. { 3, { 16, -2, 8 }, 0 },
  2781. { 3, { 16, -1, 8 }, ASHL_CLOBBERS_T },
  2782. { 2, { 16, 8 }, 0 }, // 24
  2783. { 3, { 16, 1, 8 }, LSHR_CLOBBERS_T },
  2784. { 3, { 16, 8, 2 }, 0 },
  2785. { 4, { 16, 8, 1, 2 }, LSHR_CLOBBERS_T },
  2786. { 4, { 16, 8, 2, 2 }, 0 }, // 28
  2787. { 4, { 16, -1, -2, 16 }, ASHL_CLOBBERS_T },
  2788. { 3, { 16, -2, 16 }, 0 },
  2789. /* For a right shift by 31 a 2 insn shll-movt sequence can be used.
  2790. For a left shift by 31 a 2 insn and-rotl sequences can be used.
  2791. However, the shift-and combiner code needs this entry here to be in
  2792. terms of real shift insns. */
  2793. { 3, { 16, -1, 16 }, ASHL_CLOBBERS_T }
  2794. };
  2795. /* Individual shift amounts for shift amounts < 16, up to three highmost
  2796. bits might be clobbered. This is typically used when combined with some
  2797. kind of sign or zero extension. */
  2798. static const struct ashl_lshr_sequence ext_ashl_lshr_seq[32] =
  2799. {
  2800. { 0, { 0 }, 0 }, // 0
  2801. { 1, { 1 }, LSHR_CLOBBERS_T },
  2802. { 1, { 2 }, 0 },
  2803. { 2, { 2, 1 }, LSHR_CLOBBERS_T },
  2804. { 2, { 2, 2 }, 0 }, // 4
  2805. { 3, { 2, 1, 2 }, LSHR_CLOBBERS_T },
  2806. { 2, { 8, -2 }, 0 },
  2807. { 2, { 8, -1 }, ASHL_CLOBBERS_T },
  2808. { 1, { 8 }, 0 }, // 8
  2809. { 2, { 8, 1 }, LSHR_CLOBBERS_T },
  2810. { 2, { 8, 2 }, 0 },
  2811. { 3, { 8, 1, 2 }, LSHR_CLOBBERS_T },
  2812. { 3, { 8, 2, 2 }, 0 }, // 12
  2813. { 3, { 16, -2, -1 }, ASHL_CLOBBERS_T },
  2814. { 2, { 16, -2 }, 0 },
  2815. { 2, { 16, -1 }, ASHL_CLOBBERS_T },
  2816. { 1, { 16 }, 0 }, // 16
  2817. { 2, { 16, 1 }, LSHR_CLOBBERS_T },
  2818. { 2, { 16, 2 }, 0 },
  2819. { 3, { 16, 1, 2 }, LSHR_CLOBBERS_T },
  2820. { 3, { 16, 2, 2 }, 0 }, // 20
  2821. { 4, { 16, 2, 1, 2 }, LSHR_CLOBBERS_T },
  2822. { 3, { 16, -2, 8 }, 0 },
  2823. { 3, { 16, -1, 8 }, ASHL_CLOBBERS_T },
  2824. { 2, { 16, 8 }, 0 }, // 24
  2825. { 3, { 16, 1, 8 }, LSHR_CLOBBERS_T },
  2826. { 3, { 16, 8, 2 }, 0 },
  2827. { 4, { 16, 8, 1, 2 }, LSHR_CLOBBERS_T },
  2828. { 4, { 16, 8, 2, 2 }, 0 }, // 28
  2829. { 4, { 16, -1, -2, 16 }, ASHL_CLOBBERS_T },
  2830. { 3, { 16, -2, 16 }, 0 },
  2831. { 3, { 16, -1, 16 }, ASHL_CLOBBERS_T }
  2832. };
  2833. /* Return true if a shift left consisting of 1/2/8/16 shift instructions
  2834. will clobber the T bit. */
  2835. bool
  2836. sh_ashlsi_clobbers_t_reg_p (rtx shift_amount)
  2837. {
  2838. gcc_assert (CONST_INT_P (shift_amount));
  2839. const int shift_amount_i = INTVAL (shift_amount) & 31;
  2840. /* Special case for shift count of 31: use and-rotl sequence. */
  2841. if (shift_amount_i == 31)
  2842. return true;
  2843. return (ashl_lshr_seq[shift_amount_i].clobbers_t
  2844. & ASHL_CLOBBERS_T) != 0;
  2845. }
  2846. /* Return true if a logical right shift consisting of 1/2/8/16 shift
  2847. instructions will clobber the T bit. */
  2848. bool
  2849. sh_lshrsi_clobbers_t_reg_p (rtx shift_amount)
  2850. {
  2851. gcc_assert (CONST_INT_P (shift_amount));
  2852. const int shift_amount_i = INTVAL (shift_amount) & 31;
  2853. /* Special case for shift count of 31: use shll-movt sequence. */
  2854. if (shift_amount_i == 31)
  2855. return true;
  2856. return (ashl_lshr_seq[shift_amount_i].clobbers_t
  2857. & LSHR_CLOBBERS_T) != 0;
  2858. }
  2859. /* Return true if it is potentially beneficial to use a dynamic shift
  2860. instruction (shad / shar) instead of a combination of 1/2/8/16
  2861. shift instructions for the specified shift count.
  2862. If dynamic shifts are not available, always return false. */
  2863. bool
  2864. sh_dynamicalize_shift_p (rtx count)
  2865. {
  2866. gcc_assert (CONST_INT_P (count));
  2867. const int shift_amount_i = INTVAL (count) & 31;
  2868. int insn_count;
  2869. /* For left and right shifts, there are shorter 2 insn sequences for
  2870. shift amounts of 31. */
  2871. if (shift_amount_i == 31)
  2872. insn_count = 2;
  2873. else
  2874. insn_count = ashl_lshr_seq[shift_amount_i].insn_count;
  2875. return TARGET_DYNSHIFT && (insn_count > 1 + SH_DYNAMIC_SHIFT_COST);
  2876. }
  2877. /* Assuming we have a value that has been sign-extended by at least one bit,
  2878. can we use the ext_shift_amounts with the last shift turned to an
  2879. arithmetic shift to shift it by N without data loss, and quicker than by
  2880. other means? */
  2881. #define EXT_SHIFT_SIGNED(n) (((n) | 8) == 15)
  2882. /* Return the cost of a shift. */
  2883. static inline int
  2884. shiftcosts (rtx x)
  2885. {
  2886. int value;
  2887. if (TARGET_SHMEDIA)
  2888. return 1;
  2889. if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
  2890. {
  2891. if (GET_MODE (x) == DImode
  2892. && CONST_INT_P (XEXP (x, 1))
  2893. && INTVAL (XEXP (x, 1)) == 1)
  2894. return 2;
  2895. /* Everything else is invalid, because there is no pattern for it. */
  2896. return -1;
  2897. }
  2898. /* If shift by a non constant, then this will be expensive. */
  2899. if (!CONST_INT_P (XEXP (x, 1)))
  2900. return SH_DYNAMIC_SHIFT_COST;
  2901. /* Otherwise, return the true cost in instructions. Cope with out of range
  2902. shift counts more or less arbitrarily. */
  2903. value = INTVAL (XEXP (x, 1)) & 31;
  2904. if (GET_CODE (x) == ASHIFTRT)
  2905. {
  2906. int cost = ashiftrt_insns[value];
  2907. /* If dynamic shifts are available and profitable in this case, then we
  2908. put the constant in a reg and use shad. */
  2909. if (cost > 1 + SH_DYNAMIC_SHIFT_COST)
  2910. cost = 1 + SH_DYNAMIC_SHIFT_COST;
  2911. return cost;
  2912. }
  2913. else
  2914. return ashl_lshr_seq[value].insn_count;
  2915. }
  2916. /* Return the cost of an AND/XOR/IOR operation. */
  2917. static inline int
  2918. and_xor_ior_costs (rtx x, int code)
  2919. {
  2920. /* On SH1-4 we have only max. SImode operations.
  2921. Double the cost for modes > SImode. */
  2922. const int cost_scale = !TARGET_SHMEDIA
  2923. && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
  2924. ? 2 : 1;
  2925. /* A logical operation with two registers is a single cycle
  2926. instruction. */
  2927. if (!CONST_INT_P (XEXP (x, 1)))
  2928. return 1 * cost_scale;
  2929. int i = INTVAL (XEXP (x, 1));
  2930. if (TARGET_SHMEDIA)
  2931. {
  2932. if (satisfies_constraint_I10 (XEXP (x, 1))
  2933. || satisfies_constraint_J16 (XEXP (x, 1)))
  2934. return 1;
  2935. else
  2936. return 1 + rtx_cost (XEXP (x, 1), AND, 1, !optimize_size);
  2937. }
  2938. /* These constants are single cycle extu.[bw] instructions. */
  2939. if ((i == 0xff || i == 0xffff) && code == AND)
  2940. return 1 * cost_scale;
  2941. /* Constants that can be used in an instruction as an immediate are
  2942. a single cycle, but this requires r0, so make it a little more
  2943. expensive. */
  2944. if (CONST_OK_FOR_K08 (i))
  2945. return 2 * cost_scale;
  2946. /* Constants that can be loaded with a mov immediate need one more cycle.
  2947. This case is probably unnecessary. */
  2948. if (CONST_OK_FOR_I08 (i))
  2949. return 2 * cost_scale;
  2950. /* Any other constant requires an additional 2 cycle pc-relative load.
  2951. This case is probably unnecessary. */
  2952. return 3 * cost_scale;
  2953. }
  2954. /* Return the cost of an addition or a subtraction. */
  2955. static inline int
  2956. addsubcosts (rtx x)
  2957. {
  2958. if (GET_MODE (x) == SImode)
  2959. {
  2960. /* The addc or subc patterns will eventually become one or two
  2961. instructions. Below are some costs for some of the patterns
  2962. which combine would reject because the costs of the individual
  2963. insns in the patterns are lower.
  2964. FIXME: It would be much easier if we had something like insn cost
  2965. attributes and the cost calculation machinery used those attributes
  2966. in the first place. This would eliminate redundant recog-like C
  2967. code to calculate costs of complex patterns. */
  2968. rtx op0 = XEXP (x, 0);
  2969. rtx op1 = XEXP (x, 1);
  2970. if (GET_CODE (x) == PLUS)
  2971. {
  2972. if (GET_CODE (op0) == AND
  2973. && XEXP (op0, 1) == const1_rtx
  2974. && (GET_CODE (op1) == PLUS
  2975. || (GET_CODE (op1) == MULT && XEXP (op1, 1) == const2_rtx)))
  2976. return 1;
  2977. if (GET_CODE (op0) == MULT && XEXP (op0, 1) == const2_rtx
  2978. && GET_CODE (op1) == LSHIFTRT
  2979. && CONST_INT_P (XEXP (op1, 1)) && INTVAL (XEXP (op1, 1)) == 31)
  2980. return 1;
  2981. }
  2982. /* Let's assume that adding the result of an insns that stores into
  2983. the T bit is cheap. */
  2984. if (treg_set_expr (op1, SImode))
  2985. return 1;
  2986. if (treg_set_expr (op0, SImode))
  2987. return 1;
  2988. }
  2989. /* On SH1-4 we have only max. SImode operations.
  2990. Double the cost for modes > SImode. */
  2991. const int cost_scale = !TARGET_SHMEDIA
  2992. && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD
  2993. ? 2 : 1;
  2994. /* Adding a register is a single cycle insn. */
  2995. if (REG_P (XEXP (x, 1))
  2996. || GET_CODE (XEXP (x, 1)) == SUBREG)
  2997. return 1 * cost_scale;
  2998. /* Likewise for small constants. */
  2999. if (CONST_INT_P (XEXP (x, 1))
  3000. && CONST_OK_FOR_ADD (INTVAL (XEXP (x, 1))))
  3001. return 1 * cost_scale;
  3002. if (TARGET_SHMEDIA)
  3003. switch (GET_CODE (XEXP (x, 1)))
  3004. {
  3005. case CONST:
  3006. case LABEL_REF:
  3007. case SYMBOL_REF:
  3008. return TARGET_SHMEDIA64 ? 5 : 3;
  3009. case CONST_INT:
  3010. if (CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1))))
  3011. return 2;
  3012. else if (CONST_OK_FOR_I16 (INTVAL (XEXP (x, 1)) >> 16))
  3013. return 3;
  3014. else if (CONST_OK_FOR_I16 ((INTVAL (XEXP (x, 1)) >> 16) >> 16))
  3015. return 4;
  3016. /* Fall through. */
  3017. default:
  3018. return 5;
  3019. }
  3020. /* Any other constant requires a 2 cycle pc-relative load plus an
  3021. addition. */
  3022. return 3 * cost_scale;
  3023. }
  3024. /* Return the cost of a multiply. */
  3025. static inline int
  3026. multcosts (rtx x ATTRIBUTE_UNUSED)
  3027. {
  3028. if (sh_multcost >= 0)
  3029. return sh_multcost;
  3030. if (TARGET_SHMEDIA)
  3031. /* ??? We have a mul insn, but it has a latency of three, and doesn't
  3032. accept constants. Ideally, we would use a cost of one or two and
  3033. add the cost of the operand, but disregard the latter when inside loops
  3034. and loop invariant code motion is still to follow.
  3035. Using a multiply first and splitting it later if it's a loss
  3036. doesn't work because of different sign / zero extension semantics
  3037. of multiplies vs. shifts. */
  3038. return optimize_size ? 2 : 3;
  3039. if (TARGET_SH2)
  3040. {
  3041. /* We have a mul insn, so we can never take more than the mul and the
  3042. read of the mac reg, but count more because of the latency and extra
  3043. reg usage. */
  3044. if (optimize_size)
  3045. return 2;
  3046. return 3;
  3047. }
  3048. /* If we're aiming at small code, then just count the number of
  3049. insns in a multiply call sequence. */
  3050. if (optimize_size)
  3051. return 5;
  3052. /* Otherwise count all the insns in the routine we'd be calling too. */
  3053. return 20;
  3054. }
  3055. /* Compute a (partial) cost for rtx X. Return true if the complete
  3056. cost has been computed, and false if subexpressions should be
  3057. scanned. In either case, *TOTAL contains the cost result. */
  3058. static bool
  3059. sh_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
  3060. int *total, bool speed ATTRIBUTE_UNUSED)
  3061. {
  3062. switch (code)
  3063. {
  3064. /* The lower-subreg pass decides whether to split multi-word regs
  3065. into individual regs by looking at the cost for a SET of certain
  3066. modes with the following patterns:
  3067. (set (reg) (reg))
  3068. (set (reg) (const_int 0))
  3069. On machines that support vector-move operations a multi-word move
  3070. is the same cost as individual reg move. On SH there is no
  3071. vector-move, so we have to provide the correct cost in the number
  3072. of move insns to load/store the reg of the mode in question. */
  3073. case SET:
  3074. if (register_operand (SET_DEST (x), VOIDmode)
  3075. && (register_operand (SET_SRC (x), VOIDmode)
  3076. || satisfies_constraint_Z (SET_SRC (x))))
  3077. {
  3078. const machine_mode mode = GET_MODE (SET_DEST (x));
  3079. *total = COSTS_N_INSNS (GET_MODE_SIZE (mode)
  3080. / mov_insn_size (mode, TARGET_SH2A));
  3081. return true;
  3082. }
  3083. return false;
  3084. /* The cost of a mem access is mainly the cost of the address mode. */
  3085. case MEM:
  3086. *total = sh_address_cost (XEXP (x, 0), GET_MODE (x), MEM_ADDR_SPACE (x),
  3087. true);
  3088. return true;
  3089. case IF_THEN_ELSE:
  3090. /* This case is required for the if_then_else negc pattern. */
  3091. if (treg_set_expr (XEXP (x, 0), SImode))
  3092. {
  3093. *total = COSTS_N_INSNS (1);
  3094. return true;
  3095. }
  3096. else
  3097. return false;
  3098. /* Zero extracts of single bits are usually combine patterns for the
  3099. tst insns. */
  3100. case ZERO_EXTRACT:
  3101. if (GET_CODE (XEXP (x, 0)) == XOR
  3102. && arith_reg_operand (XEXP (XEXP (x, 0), 0), VOIDmode)
  3103. && XEXP (x, 1) == const1_rtx
  3104. && CONST_INT_P (XEXP (x, 2))
  3105. && CONST_INT_P (XEXP (XEXP (x, 0), 1))
  3106. /* Check that the xor constaint overlaps with the extracted bit. */
  3107. && (INTVAL (XEXP (XEXP (x, 0), 1)) & (1LL << INTVAL (XEXP (x, 2)))))
  3108. {
  3109. *total = 1; //COSTS_N_INSNS (1);
  3110. return true;
  3111. }
  3112. return false;
  3113. /* The cost of a sign or zero extend depends on whether the source is a
  3114. reg or a mem. In case of a mem take the address into acount. */
  3115. case SIGN_EXTEND:
  3116. if (arith_reg_operand (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
  3117. {
  3118. *total = COSTS_N_INSNS (1);
  3119. return true;
  3120. }
  3121. if (MEM_P (XEXP (x, 0)))
  3122. {
  3123. *total = sh_address_cost (XEXP (XEXP (x, 0), 0),
  3124. GET_MODE (XEXP (x, 0)),
  3125. MEM_ADDR_SPACE (XEXP (x, 0)), true);
  3126. return true;
  3127. }
  3128. return false;
  3129. case ZERO_EXTEND:
  3130. if (arith_reg_operand (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
  3131. {
  3132. *total = COSTS_N_INSNS (1);
  3133. return true;
  3134. }
  3135. else if (TARGET_SH2A && MEM_P (XEXP (x, 0))
  3136. && (GET_MODE (XEXP (x, 0)) == QImode
  3137. || GET_MODE (XEXP (x, 0)) == HImode))
  3138. {
  3139. /* Handle SH2A's movu.b and movu.w insn. */
  3140. *total = sh_address_cost (XEXP (XEXP (x, 0), 0),
  3141. GET_MODE (XEXP (x, 0)),
  3142. MEM_ADDR_SPACE (XEXP (x, 0)), true);
  3143. return true;
  3144. }
  3145. return false;
  3146. /* mems for SFmode and DFmode can be inside a parallel due to
  3147. the way the fpscr is handled. */
  3148. case PARALLEL:
  3149. for (int i = 0; i < XVECLEN (x, 0); i++)
  3150. {
  3151. rtx xx = XVECEXP (x, 0, i);
  3152. if (GET_CODE (xx) == SET && MEM_P (XEXP (xx, 0)))
  3153. {
  3154. *total = sh_address_cost (XEXP (XEXP (xx, 0), 0),
  3155. GET_MODE (XEXP (xx, 0)),
  3156. MEM_ADDR_SPACE (XEXP (xx, 0)), true);
  3157. return true;
  3158. }
  3159. if (GET_CODE (xx) == SET && MEM_P (XEXP (xx, 1)))
  3160. {
  3161. *total = sh_address_cost (XEXP (XEXP (xx, 1), 0),
  3162. GET_MODE (XEXP (xx, 1)),
  3163. MEM_ADDR_SPACE (XEXP (xx, 1)), true);
  3164. return true;
  3165. }
  3166. }
  3167. if (sh_1el_vec (x, VOIDmode))
  3168. *total = outer_code != SET;
  3169. else if (sh_rep_vec (x, VOIDmode))
  3170. *total = ((GET_MODE_UNIT_SIZE (GET_MODE (x)) + 3) / 4
  3171. + (outer_code != SET));
  3172. else
  3173. *total = COSTS_N_INSNS (3) + (outer_code != SET);
  3174. return true;
  3175. case CONST_INT:
  3176. if (TARGET_SHMEDIA)
  3177. {
  3178. if (INTVAL (x) == 0)
  3179. *total = 0;
  3180. else if (outer_code == AND && and_operand ((x), DImode))
  3181. *total = 0;
  3182. else if ((outer_code == IOR || outer_code == XOR
  3183. || outer_code == PLUS)
  3184. && CONST_OK_FOR_I10 (INTVAL (x)))
  3185. *total = 0;
  3186. else if (CONST_OK_FOR_I16 (INTVAL (x)))
  3187. *total = COSTS_N_INSNS (outer_code != SET);
  3188. else if (CONST_OK_FOR_I16 (INTVAL (x) >> 16))
  3189. *total = COSTS_N_INSNS ((outer_code != SET) + 1);
  3190. else if (CONST_OK_FOR_I16 ((INTVAL (x) >> 16) >> 16))
  3191. *total = COSTS_N_INSNS ((outer_code != SET) + 2);
  3192. else
  3193. *total = COSTS_N_INSNS ((outer_code != SET) + 3);
  3194. return true;
  3195. }
  3196. if (CONST_OK_FOR_I08 (INTVAL (x)))
  3197. *total = 0;
  3198. else if ((outer_code == AND || outer_code == IOR || outer_code == XOR)
  3199. && CONST_OK_FOR_K08 (INTVAL (x)))
  3200. *total = 1;
  3201. /* prepare_cmp_insn will force costly constants int registers before
  3202. the cbranch[sd]i4 patterns can see them, so preserve potentially
  3203. interesting ones not covered by I08 above. */
  3204. else if (outer_code == COMPARE
  3205. && ((unsigned HOST_WIDE_INT) INTVAL (x)
  3206. == (unsigned HOST_WIDE_INT) 0x7fffffff + 1
  3207. || INTVAL (x) == 0x7fffffff
  3208. || INTVAL (x) == 0x80 || INTVAL (x) == -0x81))
  3209. *total = 1;
  3210. else
  3211. *total = 8;
  3212. return true;
  3213. case EQ:
  3214. /* An and with a constant compared against zero is
  3215. most likely going to be a TST #imm, R0 instruction.
  3216. Notice that this does not catch the zero_extract variants from
  3217. the md file. */
  3218. if (XEXP (x, 1) == const0_rtx
  3219. && (GET_CODE (XEXP (x, 0)) == AND
  3220. || (SUBREG_P (XEXP (x, 0))
  3221. && GET_CODE (SUBREG_REG (XEXP (x, 0))) == AND)))
  3222. {
  3223. *total = 1;
  3224. return true;
  3225. }
  3226. else if (XEXP (x, 1) == const0_rtx
  3227. && GET_CODE (XEXP (x, 0)) == AND
  3228. && CONST_INT_P (XEXP (XEXP (x, 0), 1))
  3229. && GET_CODE (XEXP (XEXP (x, 0), 0)) == ASHIFT
  3230. && arith_reg_operand (XEXP (XEXP (XEXP (x, 0), 0), 0), SImode)
  3231. && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1)))
  3232. {
  3233. *total = 1;
  3234. return true;
  3235. }
  3236. else
  3237. return false;
  3238. case SMIN:
  3239. case SMAX:
  3240. /* This is most likely a clips.b or clips.w insn that is being made up
  3241. by combine. */
  3242. if (TARGET_SH2A
  3243. && (GET_CODE (XEXP (x, 0)) == SMAX || GET_CODE (XEXP (x, 0)) == SMIN)
  3244. && CONST_INT_P (XEXP (XEXP (x, 0), 1))
  3245. && REG_P (XEXP (XEXP (x, 0), 0))
  3246. && CONST_INT_P (XEXP (x, 1)))
  3247. {
  3248. *total = COSTS_N_INSNS (1);
  3249. return true;
  3250. }
  3251. else
  3252. return false;
  3253. case CONST:
  3254. case LABEL_REF:
  3255. case SYMBOL_REF:
  3256. if (TARGET_SHMEDIA64)
  3257. *total = COSTS_N_INSNS (4);
  3258. else if (TARGET_SHMEDIA32)
  3259. *total = COSTS_N_INSNS (2);
  3260. else
  3261. *total = 5;
  3262. return true;
  3263. case CONST_DOUBLE:
  3264. if (TARGET_SHMEDIA)
  3265. *total = COSTS_N_INSNS (4);
  3266. /* prepare_cmp_insn will force costly constants int registers before
  3267. the cbranchdi4 pattern can see them, so preserve potentially
  3268. interesting ones. */
  3269. else if (outer_code == COMPARE && GET_MODE (x) == DImode)
  3270. *total = 1;
  3271. else
  3272. *total = 10;
  3273. return true;
  3274. case CONST_VECTOR:
  3275. /* FIXME: This looks broken. Only the last statement has any effect.
  3276. Probably this could be folded with the PARALLEL case? */
  3277. if (x == CONST0_RTX (GET_MODE (x)))
  3278. *total = 0;
  3279. else if (sh_1el_vec (x, VOIDmode))
  3280. *total = outer_code != SET;
  3281. if (sh_rep_vec (x, VOIDmode))
  3282. *total = ((GET_MODE_UNIT_SIZE (GET_MODE (x)) + 3) / 4
  3283. + (outer_code != SET));
  3284. *total = COSTS_N_INSNS (3) + (outer_code != SET);
  3285. return true;
  3286. case PLUS:
  3287. case MINUS:
  3288. *total = COSTS_N_INSNS (addsubcosts (x));
  3289. return true;
  3290. case AND:
  3291. /* Check for (and (not (reg)) (const_int 1)) which is a tst insn. */
  3292. if (GET_CODE (XEXP (x, 0)) == NOT && XEXP (x, 1) == const1_rtx)
  3293. {
  3294. *total = COSTS_N_INSNS (1);
  3295. return true;
  3296. }
  3297. /* Fall through. */
  3298. case XOR:
  3299. case IOR:
  3300. *total = COSTS_N_INSNS (and_xor_ior_costs (x, code));
  3301. return true;
  3302. case MULT:
  3303. *total = COSTS_N_INSNS (multcosts (x));
  3304. return true;
  3305. case LT:
  3306. case GE:
  3307. /* div0s sign comparison. */
  3308. if (GET_CODE (XEXP (x, 0)) == XOR
  3309. && REG_P ((XEXP (XEXP (x, 0), 0)))
  3310. && REG_P ((XEXP (XEXP (x, 0), 1)))
  3311. && satisfies_constraint_Z (XEXP (x, 1)))
  3312. {
  3313. *total = COSTS_N_INSNS (1);
  3314. return true;
  3315. }
  3316. else
  3317. return false;
  3318. case LSHIFTRT:
  3319. /* div0s sign comparison. */
  3320. if (GET_CODE (XEXP (x, 0)) == XOR
  3321. && REG_P ((XEXP (XEXP (x, 0), 0)))
  3322. && REG_P ((XEXP (XEXP (x, 0), 1)))
  3323. && CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) == 31)
  3324. {
  3325. *total = COSTS_N_INSNS (1);
  3326. return true;
  3327. }
  3328. /* Fall through to shiftcosts. */
  3329. case ASHIFT:
  3330. case ASHIFTRT:
  3331. {
  3332. int cost = shiftcosts (x);
  3333. if (cost < 0)
  3334. return false;
  3335. *total = COSTS_N_INSNS (cost);
  3336. return true;
  3337. }
  3338. case DIV:
  3339. case UDIV:
  3340. case MOD:
  3341. case UMOD:
  3342. *total = COSTS_N_INSNS (20);
  3343. return true;
  3344. case FLOAT:
  3345. case FIX:
  3346. *total = 100;
  3347. return true;
  3348. default:
  3349. return false;
  3350. }
  3351. }
  3352. /* Determine the size of the fundamental move insn that will be used
  3353. for the specified mode. */
  3354. static inline int
  3355. mov_insn_size (machine_mode mode, bool consider_sh2a)
  3356. {
  3357. const int mode_sz = GET_MODE_SIZE (mode);
  3358. if ((consider_sh2a && TARGET_SH2A_DOUBLE && mode == DFmode)
  3359. || (TARGET_FMOVD && mode == DFmode))
  3360. return mode_sz;
  3361. else
  3362. {
  3363. /* The max. available mode for actual move insns is SImode.
  3364. Larger accesses will be split into multiple loads/stores. */
  3365. const int max_mov_sz = GET_MODE_SIZE (SImode);
  3366. return mode_sz >= max_mov_sz ? max_mov_sz : mode_sz;
  3367. }
  3368. }
  3369. /* Determine the maximum possible displacement for a move insn for the
  3370. specified mode. */
  3371. int
  3372. sh_max_mov_insn_displacement (machine_mode mode, bool consider_sh2a)
  3373. {
  3374. /* The 4 byte displacement move insns are the same as the 2 byte
  3375. versions but take a 12 bit displacement. All we need to do is to
  3376. scale the max. displacement value accordingly. */
  3377. const int disp_scale = consider_sh2a ? (4095 / 15) : 1;
  3378. /* SH2A supports FPU move insns with 12 bit displacements.
  3379. Other variants to do not support any kind of displacements for
  3380. FPU move insns. */
  3381. if (! consider_sh2a && TARGET_FPU_ANY && GET_MODE_CLASS (mode) == MODE_FLOAT)
  3382. return 0;
  3383. else
  3384. {
  3385. const int mov_insn_sz = mov_insn_size (mode, consider_sh2a);
  3386. const int mode_sz = GET_MODE_SIZE (mode);
  3387. int r = 15 * mov_insn_sz * disp_scale;
  3388. /* If the mov insn will be split into multiple loads/stores, the
  3389. maximum possible displacement is a bit smaller. */
  3390. if (mode_sz > mov_insn_sz)
  3391. r -= mode_sz - mov_insn_sz;
  3392. return r;
  3393. }
  3394. }
  3395. /* Determine the alignment mask for a move insn of the
  3396. specified mode. */
  3397. static inline int
  3398. mov_insn_alignment_mask (machine_mode mode, bool consider_sh2a)
  3399. {
  3400. const int mov_insn_sz = mov_insn_size (mode, consider_sh2a);
  3401. return mov_insn_sz > 0 ? (mov_insn_sz - 1) : 0;
  3402. }
  3403. /* Return the displacement value of a displacement address. */
  3404. HOST_WIDE_INT
  3405. sh_disp_addr_displacement (rtx x)
  3406. {
  3407. gcc_assert (satisfies_constraint_Sdd (x));
  3408. return INTVAL (XEXP (XEXP (x, 0), 1));
  3409. }
  3410. /* Compute the cost of an address. */
  3411. static int
  3412. sh_address_cost (rtx x, machine_mode mode,
  3413. addr_space_t as ATTRIBUTE_UNUSED, bool speed ATTRIBUTE_UNUSED)
  3414. {
  3415. /* 'GBR + 0'. Account one more because of R0 restriction. */
  3416. if (REG_P (x) && REGNO (x) == GBR_REG)
  3417. return 2;
  3418. /* Simple reg, post-inc, pre-dec addressing. */
  3419. if (REG_P (x) || GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_DEC)
  3420. return 1;
  3421. /* 'reg + disp' addressing. */
  3422. if (GET_CODE (x) == PLUS
  3423. && REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1)))
  3424. {
  3425. /* 'GBR + disp'. Account one more because of R0 restriction. */
  3426. if (REGNO (XEXP (x, 0)) == GBR_REG
  3427. && gbr_displacement (XEXP (x, 1), mode))
  3428. return 2;
  3429. const HOST_WIDE_INT offset = INTVAL (XEXP (x, 1));
  3430. if (offset == 0)
  3431. return 1;
  3432. /* The displacement would fit into a 2 byte move insn.
  3433. HImode and QImode loads/stores with displacement put pressure on
  3434. R0 which will most likely require another reg copy. Thus account
  3435. a higher cost for that. */
  3436. if (offset > 0 && offset <= sh_max_mov_insn_displacement (mode, false))
  3437. return (mode == HImode || mode == QImode) ? 2 : 1;
  3438. /* The displacement would fit into a 4 byte move insn (SH2A). */
  3439. if (TARGET_SH2A
  3440. && offset > 0 && offset <= sh_max_mov_insn_displacement (mode, true))
  3441. return 2;
  3442. /* The displacement is probably out of range and will require extra
  3443. calculations. */
  3444. return 3;
  3445. }
  3446. /* 'reg + reg' addressing. Account a slightly higher cost because of
  3447. increased pressure on R0. */
  3448. if (GET_CODE (x) == PLUS && ! CONSTANT_P (XEXP (x, 1))
  3449. && ! TARGET_SHMEDIA)
  3450. return 3;
  3451. /* Not sure what it is - probably expensive. */
  3452. return 10;
  3453. }
  3454. /* Code to expand a shift. */
  3455. static void
  3456. gen_ashift (int type, int n, rtx reg)
  3457. {
  3458. rtx n_rtx;
  3459. /* Negative values here come from the shift_amounts array. */
  3460. if (n < 0)
  3461. {
  3462. if (type == ASHIFT)
  3463. type = LSHIFTRT;
  3464. else
  3465. type = ASHIFT;
  3466. n = -n;
  3467. }
  3468. n_rtx = GEN_INT (n);
  3469. gcc_assert (satisfies_constraint_P27 (n_rtx));
  3470. switch (type)
  3471. {
  3472. case ASHIFTRT:
  3473. emit_insn (gen_ashrsi3_k (reg, reg, n_rtx));
  3474. break;
  3475. case LSHIFTRT:
  3476. if (n == 1)
  3477. emit_insn (gen_shlr (reg, reg));
  3478. else
  3479. emit_insn (gen_lshrsi3_k (reg, reg, n_rtx));
  3480. break;
  3481. case ASHIFT:
  3482. emit_insn (gen_ashlsi3_k (reg, reg, n_rtx));
  3483. break;
  3484. default:
  3485. gcc_unreachable ();
  3486. }
  3487. }
  3488. /* Code to expand a HImode shift. */
  3489. static void
  3490. gen_ashift_hi (int type, int n, rtx reg)
  3491. {
  3492. /* Negative values here come from the shift_amounts array. */
  3493. if (n < 0)
  3494. {
  3495. if (type == ASHIFT)
  3496. type = LSHIFTRT;
  3497. else
  3498. type = ASHIFT;
  3499. n = -n;
  3500. }
  3501. switch (type)
  3502. {
  3503. case ASHIFTRT:
  3504. case LSHIFTRT:
  3505. /* We don't have HImode right shift operations because using the
  3506. ordinary 32 bit shift instructions for that doesn't generate proper
  3507. zero/sign extension.
  3508. gen_ashift_hi is only called in contexts where we know that the
  3509. sign extension works out correctly. */
  3510. {
  3511. int offset = 0;
  3512. if (GET_CODE (reg) == SUBREG)
  3513. {
  3514. offset = SUBREG_BYTE (reg);
  3515. reg = SUBREG_REG (reg);
  3516. }
  3517. gen_ashift (type, n, gen_rtx_SUBREG (SImode, reg, offset));
  3518. break;
  3519. }
  3520. case ASHIFT:
  3521. emit_insn (gen_ashlhi3_k (reg, reg, GEN_INT (n)));
  3522. break;
  3523. }
  3524. }
  3525. /* Output RTL to split a constant shift into its component SH constant
  3526. shift instructions. */
  3527. void
  3528. gen_shifty_op (int code, rtx *operands)
  3529. {
  3530. int value = INTVAL (operands[2]);
  3531. int max, i;
  3532. /* Truncate the shift count in case it is out of bounds. */
  3533. value = value & 31;
  3534. if (value == 31)
  3535. {
  3536. if (code == LSHIFTRT)
  3537. {
  3538. emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
  3539. emit_insn (gen_movt (operands[0], get_t_reg_rtx ()));
  3540. return;
  3541. }
  3542. else if (code == ASHIFT)
  3543. {
  3544. /* There is a two instruction sequence for 31 bit left shifts,
  3545. but it requires r0. */
  3546. if (REG_P (operands[0]) && REGNO (operands[0]) == 0)
  3547. {
  3548. emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
  3549. emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
  3550. return;
  3551. }
  3552. }
  3553. }
  3554. else if (value == 0)
  3555. {
  3556. /* This can happen even when optimizing, if there were subregs before
  3557. reload. Don't output a nop here, as this is never optimized away;
  3558. use a no-op move instead. */
  3559. emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[0]));
  3560. return;
  3561. }
  3562. max = ashl_lshr_seq[value].insn_count;
  3563. for (i = 0; i < max; i++)
  3564. gen_ashift (code, ashl_lshr_seq[value].amount[i], operands[0]);
  3565. }
  3566. /* Same as gen_shifty_op, but optimized for values where the topmost bits
  3567. don't matter. */
  3568. void
  3569. gen_shifty_hi_op (int code, rtx *operands)
  3570. {
  3571. int value = INTVAL (operands[2]);
  3572. int max, i;
  3573. void (*gen_fun) (int, int, rtx);
  3574. /* This operation is used by and_shl for SImode values with a few
  3575. high bits known to be cleared. */
  3576. value &= 31;
  3577. if (value == 0)
  3578. {
  3579. emit_insn (gen_nop ());
  3580. return;
  3581. }
  3582. gen_fun = GET_MODE (operands[0]) == HImode ? gen_ashift_hi : gen_ashift;
  3583. if (code == ASHIFT)
  3584. {
  3585. max = ext_ashl_lshr_seq[value].insn_count;
  3586. for (i = 0; i < max; i++)
  3587. gen_fun (code, ext_ashl_lshr_seq[value].amount[i], operands[0]);
  3588. }
  3589. else
  3590. /* When shifting right, emit the shifts in reverse order, so that
  3591. solitary negative values come first. */
  3592. for (i = ext_ashl_lshr_seq[value].insn_count - 1; i >= 0; i--)
  3593. gen_fun (code, ext_ashl_lshr_seq[value].amount[i], operands[0]);
  3594. }
  3595. /* Output RTL for an arithmetic right shift.
  3596. ??? Rewrite to use super-optimizer sequences. */
  3597. bool
  3598. expand_ashiftrt (rtx *operands)
  3599. {
  3600. rtx wrk;
  3601. char func[18];
  3602. int value;
  3603. if (TARGET_DYNSHIFT)
  3604. {
  3605. if (!CONST_INT_P (operands[2]))
  3606. {
  3607. rtx count = copy_to_mode_reg (SImode, operands[2]);
  3608. emit_insn (gen_negsi2 (count, count));
  3609. emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
  3610. return true;
  3611. }
  3612. else if (ashiftrt_insns[INTVAL (operands[2]) & 31]
  3613. > 1 + SH_DYNAMIC_SHIFT_COST)
  3614. {
  3615. rtx count
  3616. = force_reg (SImode, GEN_INT (- (INTVAL (operands[2]) & 31)));
  3617. emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
  3618. return true;
  3619. }
  3620. }
  3621. if (!CONST_INT_P (operands[2]))
  3622. return false;
  3623. value = INTVAL (operands[2]) & 31;
  3624. if (value == 31)
  3625. {
  3626. /* If we are called from abs expansion, arrange things so that we
  3627. we can use a single MT instruction that doesn't clobber the source,
  3628. if LICM can hoist out the load of the constant zero. */
  3629. if (currently_expanding_to_rtl)
  3630. {
  3631. emit_insn (gen_cmpgtsi_t (force_reg (SImode, CONST0_RTX (SImode)),
  3632. operands[1]));
  3633. emit_insn (gen_mov_neg_si_t (operands[0], get_t_reg_rtx ()));
  3634. return true;
  3635. }
  3636. emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
  3637. return true;
  3638. }
  3639. else if (value >= 16 && value <= 19)
  3640. {
  3641. wrk = gen_reg_rtx (SImode);
  3642. emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
  3643. value -= 16;
  3644. while (value--)
  3645. gen_ashift (ASHIFTRT, 1, wrk);
  3646. emit_move_insn (operands[0], wrk);
  3647. return true;
  3648. }
  3649. /* Expand a short sequence inline, longer call a magic routine. */
  3650. else if (value <= 5)
  3651. {
  3652. wrk = gen_reg_rtx (SImode);
  3653. emit_move_insn (wrk, operands[1]);
  3654. while (value--)
  3655. gen_ashift (ASHIFTRT, 1, wrk);
  3656. emit_move_insn (operands[0], wrk);
  3657. return true;
  3658. }
  3659. wrk = gen_reg_rtx (Pmode);
  3660. /* Load the value into an arg reg and call a helper. */
  3661. emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
  3662. sprintf (func, "__ashiftrt_r4_%d", value);
  3663. function_symbol (wrk, func, SFUNC_STATIC);
  3664. emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
  3665. emit_move_insn (operands[0], gen_rtx_REG (SImode, 4));
  3666. return true;
  3667. }
  3668. /* Try to find a good way to implement the combiner pattern
  3669. [(set (match_operand:SI 0 "register_operand" "r")
  3670. (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
  3671. (match_operand:SI 2 "const_int_operand" "n"))
  3672. (match_operand:SI 3 "const_int_operand" "n"))) .
  3673. LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
  3674. return 0 for simple right / left or left/right shift combination.
  3675. return 1 for a combination of shifts with zero_extend.
  3676. return 2 for a combination of shifts with an AND that needs r0.
  3677. return 3 for a combination of shifts with an AND that needs an extra
  3678. scratch register, when the three highmost bits of the AND mask are clear.
  3679. return 4 for a combination of shifts with an AND that needs an extra
  3680. scratch register, when any of the three highmost bits of the AND mask
  3681. is set.
  3682. If ATTRP is set, store an initial right shift width in ATTRP[0],
  3683. and the instruction length in ATTRP[1] . These values are not valid
  3684. when returning 0.
  3685. When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
  3686. shift_amounts for the last shift value that is to be used before the
  3687. sign extend. */
  3688. int
  3689. shl_and_kind (rtx left_rtx, rtx mask_rtx, int *attrp)
  3690. {
  3691. unsigned HOST_WIDE_INT mask, lsb, mask2, lsb2;
  3692. int left = INTVAL (left_rtx), right;
  3693. int best = 0;
  3694. int cost, best_cost = 10000;
  3695. int best_right = 0, best_len = 0;
  3696. int i;
  3697. int can_ext;
  3698. if (left < 0 || left > 31)
  3699. return 0;
  3700. if (CONST_INT_P (mask_rtx))
  3701. mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> left;
  3702. else
  3703. mask = (unsigned HOST_WIDE_INT) GET_MODE_MASK (SImode) >> left;
  3704. /* Can this be expressed as a right shift / left shift pair? */
  3705. lsb = ((mask ^ (mask - 1)) >> 1) + 1;
  3706. right = exact_log2 (lsb);
  3707. mask2 = ~(mask + lsb - 1);
  3708. lsb2 = ((mask2 ^ (mask2 - 1)) >> 1) + 1;
  3709. /* mask has no zeroes but trailing zeroes <==> ! mask2 */
  3710. if (! mask2)
  3711. best_cost = ashl_lshr_seq[right].insn_count
  3712. + ashl_lshr_seq[right + left].insn_count;
  3713. /* mask has no trailing zeroes <==> ! right */
  3714. else if (! right && mask2 == ~(lsb2 - 1))
  3715. {
  3716. int late_right = exact_log2 (lsb2);
  3717. best_cost = ashl_lshr_seq[left + late_right].insn_count
  3718. + ashl_lshr_seq[late_right].insn_count;
  3719. }
  3720. /* Try to use zero extend. */
  3721. if (mask2 == ~(lsb2 - 1))
  3722. {
  3723. int width, first;
  3724. for (width = 8; width <= 16; width += 8)
  3725. {
  3726. /* Can we zero-extend right away? */
  3727. if (lsb2 == (unsigned HOST_WIDE_INT) 1 << width)
  3728. {
  3729. cost = 1 + ext_ashl_lshr_seq[right].insn_count
  3730. + ext_ashl_lshr_seq[left + right].insn_count;
  3731. if (cost < best_cost)
  3732. {
  3733. best = 1;
  3734. best_cost = cost;
  3735. best_right = right;
  3736. best_len = cost;
  3737. if (attrp)
  3738. attrp[2] = -1;
  3739. }
  3740. continue;
  3741. }
  3742. /* ??? Could try to put zero extend into initial right shift,
  3743. or even shift a bit left before the right shift. */
  3744. /* Determine value of first part of left shift, to get to the
  3745. zero extend cut-off point. */
  3746. first = width - exact_log2 (lsb2) + right;
  3747. if (first >= 0 && right + left - first >= 0)
  3748. {
  3749. cost = ext_ashl_lshr_seq[right].insn_count
  3750. + ext_ashl_lshr_seq[first].insn_count + 1
  3751. + ext_ashl_lshr_seq[right + left - first].insn_count;
  3752. if (cost < best_cost)
  3753. {
  3754. best = 1;
  3755. best_cost = cost;
  3756. best_right = right;
  3757. best_len = cost;
  3758. if (attrp)
  3759. attrp[2] = first;
  3760. }
  3761. }
  3762. }
  3763. }
  3764. /* Try to use r0 AND pattern */
  3765. for (i = 0; i <= 2; i++)
  3766. {
  3767. if (i > right)
  3768. break;
  3769. if (! CONST_OK_FOR_K08 (mask >> i))
  3770. continue;
  3771. cost = (i != 0) + 2 + ext_ashl_lshr_seq[left + i].insn_count;
  3772. if (cost < best_cost)
  3773. {
  3774. best = 2;
  3775. best_cost = cost;
  3776. best_right = i;
  3777. best_len = cost - 1;
  3778. }
  3779. }
  3780. /* Try to use a scratch register to hold the AND operand. */
  3781. can_ext = ((mask << left) & ((unsigned HOST_WIDE_INT) 3 << 30)) == 0;
  3782. for (i = 0; i <= 2; i++)
  3783. {
  3784. if (i > right)
  3785. break;
  3786. cost = (i != 0) + (CONST_OK_FOR_I08 (mask >> i) ? 2 : 3)
  3787. + (can_ext
  3788. ? ext_ashl_lshr_seq
  3789. : ashl_lshr_seq)[left + i].insn_count;
  3790. if (cost < best_cost)
  3791. {
  3792. best = 4 - can_ext;
  3793. best_cost = cost;
  3794. best_right = i;
  3795. best_len = cost - 1 - ! CONST_OK_FOR_I08 (mask >> i);
  3796. }
  3797. }
  3798. if (attrp)
  3799. {
  3800. attrp[0] = best_right;
  3801. attrp[1] = best_len;
  3802. }
  3803. return best;
  3804. }
  3805. /* This is used in length attributes of the unnamed instructions
  3806. corresponding to shl_and_kind return values of 1 and 2. */
  3807. int
  3808. shl_and_length (rtx insn)
  3809. {
  3810. rtx set_src, left_rtx, mask_rtx;
  3811. int attributes[3];
  3812. set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
  3813. left_rtx = XEXP (XEXP (set_src, 0), 1);
  3814. mask_rtx = XEXP (set_src, 1);
  3815. shl_and_kind (left_rtx, mask_rtx, attributes);
  3816. return attributes[1];
  3817. }
  3818. /* This is used in length attribute of the and_shl_scratch instruction. */
  3819. int
  3820. shl_and_scr_length (rtx insn)
  3821. {
  3822. rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
  3823. int len = ashl_lshr_seq[INTVAL (XEXP (set_src, 1)) & 31].insn_count;
  3824. rtx op = XEXP (set_src, 0);
  3825. len += ashl_lshr_seq[INTVAL (XEXP (op, 1)) & 31].insn_count + 1;
  3826. op = XEXP (XEXP (op, 0), 0);
  3827. return len + ashl_lshr_seq[INTVAL (XEXP (op, 1)) & 31].insn_count;
  3828. }
  3829. /* Generate rtl for instructions for which shl_and_kind advised a particular
  3830. method of generating them, i.e. returned zero. */
  3831. bool
  3832. gen_shl_and (rtx dest, rtx left_rtx, rtx mask_rtx, rtx source)
  3833. {
  3834. int attributes[3];
  3835. unsigned HOST_WIDE_INT mask;
  3836. int kind = shl_and_kind (left_rtx, mask_rtx, attributes);
  3837. int right, total_shift;
  3838. void (*shift_gen_fun) (int, rtx *) = gen_shifty_hi_op;
  3839. right = attributes[0];
  3840. total_shift = INTVAL (left_rtx) + right;
  3841. mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> total_shift;
  3842. switch (kind)
  3843. {
  3844. default:
  3845. return true;
  3846. case 1:
  3847. {
  3848. int first = attributes[2];
  3849. rtx operands[3];
  3850. if (first < 0)
  3851. {
  3852. emit_insn ((mask << right) <= 0xff
  3853. ? gen_zero_extendqisi2 (dest,
  3854. gen_lowpart (QImode, source))
  3855. : gen_zero_extendhisi2 (dest,
  3856. gen_lowpart (HImode, source)));
  3857. source = dest;
  3858. }
  3859. if (source != dest)
  3860. emit_insn (gen_movsi (dest, source));
  3861. operands[0] = dest;
  3862. if (right)
  3863. {
  3864. operands[2] = GEN_INT (right);
  3865. gen_shifty_hi_op (LSHIFTRT, operands);
  3866. }
  3867. if (first > 0)
  3868. {
  3869. operands[2] = GEN_INT (first);
  3870. gen_shifty_hi_op (ASHIFT, operands);
  3871. total_shift -= first;
  3872. mask <<= first;
  3873. }
  3874. if (first >= 0)
  3875. emit_insn (mask <= 0xff
  3876. ? gen_zero_extendqisi2 (dest, gen_lowpart (QImode, dest))
  3877. : gen_zero_extendhisi2 (dest, gen_lowpart (HImode, dest)));
  3878. if (total_shift > 0)
  3879. {
  3880. operands[2] = GEN_INT (total_shift);
  3881. gen_shifty_hi_op (ASHIFT, operands);
  3882. }
  3883. break;
  3884. }
  3885. case 4:
  3886. shift_gen_fun = gen_shifty_op;
  3887. case 3:
  3888. /* If the topmost bit that matters is set, set the topmost bits
  3889. that don't matter. This way, we might be able to get a shorter
  3890. signed constant. */
  3891. if (mask & ((HOST_WIDE_INT) 1 << (31 - total_shift)))
  3892. mask |= (HOST_WIDE_INT) ~0 << (31 - total_shift);
  3893. case 2:
  3894. /* Don't expand fine-grained when combining, because that will
  3895. make the pattern fail. */
  3896. if (currently_expanding_to_rtl
  3897. || reload_in_progress || reload_completed)
  3898. {
  3899. rtx operands[3];
  3900. /* Cases 3 and 4 should be handled by this split
  3901. only while combining */
  3902. gcc_assert (kind <= 2);
  3903. if (right)
  3904. {
  3905. emit_insn (gen_lshrsi3 (dest, source, GEN_INT (right)));
  3906. source = dest;
  3907. }
  3908. emit_insn (gen_andsi3 (dest, source, GEN_INT (mask)));
  3909. if (total_shift)
  3910. {
  3911. operands[0] = dest;
  3912. operands[1] = dest;
  3913. operands[2] = GEN_INT (total_shift);
  3914. shift_gen_fun (ASHIFT, operands);
  3915. }
  3916. break;
  3917. }
  3918. else
  3919. {
  3920. int neg = 0;
  3921. if (kind != 4 && total_shift < 16)
  3922. {
  3923. neg = -ext_ashl_lshr_seq[total_shift].amount[1];
  3924. if (neg > 0)
  3925. neg -= ext_ashl_lshr_seq[total_shift].amount[2];
  3926. else
  3927. neg = 0;
  3928. }
  3929. emit_insn (gen_and_shl_scratch (dest, source,
  3930. GEN_INT (right),
  3931. GEN_INT (mask),
  3932. GEN_INT (total_shift + neg),
  3933. GEN_INT (neg)));
  3934. emit_insn (gen_movsi (dest, dest));
  3935. break;
  3936. }
  3937. }
  3938. return false;
  3939. }
  3940. /* Try to find a good way to implement the combiner pattern
  3941. [(set (match_operand:SI 0 "register_operand" "=r")
  3942. (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
  3943. (match_operand:SI 2 "const_int_operand" "n")
  3944. (match_operand:SI 3 "const_int_operand" "n")
  3945. (const_int 0)))
  3946. (clobber (reg:SI T_REG))]
  3947. LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
  3948. return 0 for simple left / right shift combination.
  3949. return 1 for left shift / 8 bit sign extend / left shift.
  3950. return 2 for left shift / 16 bit sign extend / left shift.
  3951. return 3 for left shift / 8 bit sign extend / shift / sign extend.
  3952. return 4 for left shift / 16 bit sign extend / shift / sign extend.
  3953. return 5 for left shift / 16 bit sign extend / right shift
  3954. return 6 for < 8 bit sign extend / left shift.
  3955. return 7 for < 8 bit sign extend / left shift / single right shift.
  3956. If COSTP is nonzero, assign the calculated cost to *COSTP. */
  3957. int
  3958. shl_sext_kind (rtx left_rtx, rtx size_rtx, int *costp)
  3959. {
  3960. int left, size, insize, ext;
  3961. int cost = 0, best_cost;
  3962. int kind;
  3963. left = INTVAL (left_rtx);
  3964. size = INTVAL (size_rtx);
  3965. insize = size - left;
  3966. gcc_assert (insize > 0);
  3967. /* Default to left / right shift. */
  3968. kind = 0;
  3969. best_cost = ashl_lshr_seq[32 - insize].insn_count
  3970. + ashl_lshr_seq[32 - size].insn_count;
  3971. if (size <= 16)
  3972. {
  3973. /* 16 bit shift / sign extend / 16 bit shift */
  3974. cost = ashl_lshr_seq[16 - insize].insn_count + 1
  3975. + ashl_lshr_seq[16 - size].insn_count;
  3976. /* If ashiftrt_insns[16 - size] is 8, this choice will be overridden
  3977. below, by alternative 3 or something even better. */
  3978. if (cost < best_cost)
  3979. {
  3980. kind = 5;
  3981. best_cost = cost;
  3982. }
  3983. }
  3984. /* Try a plain sign extend between two shifts. */
  3985. for (ext = 16; ext >= insize; ext -= 8)
  3986. {
  3987. if (ext <= size)
  3988. {
  3989. cost = ext_ashl_lshr_seq[ext - insize].insn_count + 1
  3990. + ashl_lshr_seq[size - ext].insn_count;
  3991. if (cost < best_cost)
  3992. {
  3993. kind = ext / (unsigned) 8;
  3994. best_cost = cost;
  3995. }
  3996. }
  3997. /* Check if we can do a sloppy shift with a final signed shift
  3998. restoring the sign. */
  3999. if (EXT_SHIFT_SIGNED (size - ext))
  4000. cost = ext_ashl_lshr_seq[ext - insize].insn_count
  4001. + ext_ashl_lshr_seq[size - ext].insn_count + 1;
  4002. /* If not, maybe it's still cheaper to do the second shift sloppy,
  4003. and do a final sign extend? */
  4004. else if (size <= 16)
  4005. cost = ext_ashl_lshr_seq[ext - insize].insn_count + 1
  4006. + ext_ashl_lshr_seq[size > ext ? size - ext : ext - size].insn_count
  4007. + 1;
  4008. else
  4009. continue;
  4010. if (cost < best_cost)
  4011. {
  4012. kind = ext / (unsigned) 8 + 2;
  4013. best_cost = cost;
  4014. }
  4015. }
  4016. /* Check if we can sign extend in r0 */
  4017. if (insize < 8)
  4018. {
  4019. cost = 3 + ashl_lshr_seq[left].insn_count;
  4020. if (cost < best_cost)
  4021. {
  4022. kind = 6;
  4023. best_cost = cost;
  4024. }
  4025. /* Try the same with a final signed shift. */
  4026. if (left < 31)
  4027. {
  4028. cost = 3 + ext_ashl_lshr_seq[left + 1].insn_count + 1;
  4029. if (cost < best_cost)
  4030. {
  4031. kind = 7;
  4032. best_cost = cost;
  4033. }
  4034. }
  4035. }
  4036. if (TARGET_DYNSHIFT)
  4037. {
  4038. /* Try to use a dynamic shift. */
  4039. cost = ashl_lshr_seq[32 - insize].insn_count + 1 + SH_DYNAMIC_SHIFT_COST;
  4040. if (cost < best_cost)
  4041. {
  4042. kind = 0;
  4043. best_cost = cost;
  4044. }
  4045. }
  4046. if (costp)
  4047. *costp = cost;
  4048. return kind;
  4049. }
  4050. /* Function to be used in the length attribute of the instructions
  4051. implementing this pattern. */
  4052. int
  4053. shl_sext_length (rtx insn)
  4054. {
  4055. rtx set_src, left_rtx, size_rtx;
  4056. int cost;
  4057. set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
  4058. left_rtx = XEXP (XEXP (set_src, 0), 1);
  4059. size_rtx = XEXP (set_src, 1);
  4060. shl_sext_kind (left_rtx, size_rtx, &cost);
  4061. return cost;
  4062. }
  4063. /* Generate rtl for this pattern */
  4064. bool
  4065. gen_shl_sext (rtx dest, rtx left_rtx, rtx size_rtx, rtx source)
  4066. {
  4067. int kind;
  4068. int left, size, insize, cost;
  4069. rtx operands[3];
  4070. kind = shl_sext_kind (left_rtx, size_rtx, &cost);
  4071. left = INTVAL (left_rtx);
  4072. size = INTVAL (size_rtx);
  4073. insize = size - left;
  4074. switch (kind)
  4075. {
  4076. case 1:
  4077. case 2:
  4078. case 3:
  4079. case 4:
  4080. {
  4081. int ext = kind & 1 ? 8 : 16;
  4082. int shift2 = size - ext;
  4083. /* Don't expand fine-grained when combining, because that will
  4084. make the pattern fail. */
  4085. if (! currently_expanding_to_rtl
  4086. && ! reload_in_progress && ! reload_completed)
  4087. {
  4088. emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
  4089. emit_insn (gen_movsi (dest, source));
  4090. break;
  4091. }
  4092. if (dest != source)
  4093. emit_insn (gen_movsi (dest, source));
  4094. operands[0] = dest;
  4095. if (ext - insize)
  4096. {
  4097. operands[2] = GEN_INT (ext - insize);
  4098. gen_shifty_hi_op (ASHIFT, operands);
  4099. }
  4100. emit_insn (kind & 1
  4101. ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
  4102. : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
  4103. if (kind <= 2)
  4104. {
  4105. if (shift2)
  4106. {
  4107. operands[2] = GEN_INT (shift2);
  4108. gen_shifty_op (ASHIFT, operands);
  4109. }
  4110. }
  4111. else
  4112. {
  4113. if (shift2 > 0)
  4114. {
  4115. if (EXT_SHIFT_SIGNED (shift2))
  4116. {
  4117. operands[2] = GEN_INT (shift2 + 1);
  4118. gen_shifty_op (ASHIFT, operands);
  4119. operands[2] = const1_rtx;
  4120. gen_shifty_op (ASHIFTRT, operands);
  4121. break;
  4122. }
  4123. operands[2] = GEN_INT (shift2);
  4124. gen_shifty_hi_op (ASHIFT, operands);
  4125. }
  4126. else if (shift2)
  4127. {
  4128. operands[2] = GEN_INT (-shift2);
  4129. gen_shifty_hi_op (LSHIFTRT, operands);
  4130. }
  4131. emit_insn (size <= 8
  4132. ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
  4133. : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
  4134. }
  4135. break;
  4136. }
  4137. case 5:
  4138. {
  4139. int i = 16 - size;
  4140. if (! currently_expanding_to_rtl
  4141. && ! reload_in_progress && ! reload_completed)
  4142. emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
  4143. else
  4144. {
  4145. operands[0] = dest;
  4146. operands[2] = GEN_INT (16 - insize);
  4147. gen_shifty_hi_op (ASHIFT, operands);
  4148. emit_insn (gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
  4149. }
  4150. /* Don't use gen_ashrsi3 because it generates new pseudos. */
  4151. while (--i >= 0)
  4152. gen_ashift (ASHIFTRT, 1, dest);
  4153. break;
  4154. }
  4155. case 6:
  4156. case 7:
  4157. /* Don't expand fine-grained when combining, because that will
  4158. make the pattern fail. */
  4159. if (! currently_expanding_to_rtl
  4160. && ! reload_in_progress && ! reload_completed)
  4161. {
  4162. emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
  4163. emit_insn (gen_movsi (dest, source));
  4164. break;
  4165. }
  4166. emit_insn (gen_andsi3 (dest, source, GEN_INT ((1 << insize) - 1)));
  4167. emit_insn (gen_xorsi3 (dest, dest, GEN_INT (1 << (insize - 1))));
  4168. emit_insn (gen_addsi3 (dest, dest, GEN_INT (-1 << (insize - 1))));
  4169. operands[0] = dest;
  4170. operands[2] = kind == 7 ? GEN_INT (left + 1) : left_rtx;
  4171. gen_shifty_op (ASHIFT, operands);
  4172. if (kind == 7)
  4173. emit_insn (gen_ashrsi3_k (dest, dest, const1_rtx));
  4174. break;
  4175. default:
  4176. return true;
  4177. }
  4178. return false;
  4179. }
  4180. /* Prefix a symbol_ref name with "datalabel". */
  4181. rtx
  4182. gen_datalabel_ref (rtx sym)
  4183. {
  4184. const char *str;
  4185. if (GET_CODE (sym) == LABEL_REF)
  4186. return gen_rtx_CONST (GET_MODE (sym),
  4187. gen_rtx_UNSPEC (GET_MODE (sym),
  4188. gen_rtvec (1, sym),
  4189. UNSPEC_DATALABEL));
  4190. gcc_assert (GET_CODE (sym) == SYMBOL_REF);
  4191. str = XSTR (sym, 0);
  4192. /* Share all SYMBOL_REF strings with the same value - that is important
  4193. for cse. */
  4194. str = IDENTIFIER_POINTER (get_identifier (str));
  4195. XSTR (sym, 0) = str;
  4196. return sym;
  4197. }
  4198. static alloc_pool label_ref_list_pool;
  4199. typedef struct label_ref_list_d
  4200. {
  4201. rtx_code_label *label;
  4202. struct label_ref_list_d *next;
  4203. } *label_ref_list_t;
  4204. /* The SH cannot load a large constant into a register, constants have to
  4205. come from a pc relative load. The reference of a pc relative load
  4206. instruction must be less than 1k in front of the instruction. This
  4207. means that we often have to dump a constant inside a function, and
  4208. generate code to branch around it.
  4209. It is important to minimize this, since the branches will slow things
  4210. down and make things bigger.
  4211. Worst case code looks like:
  4212. mov.l L1,rn
  4213. bra L2
  4214. nop
  4215. align
  4216. L1: .long value
  4217. L2:
  4218. ..
  4219. mov.l L3,rn
  4220. bra L4
  4221. nop
  4222. align
  4223. L3: .long value
  4224. L4:
  4225. ..
  4226. We fix this by performing a scan before scheduling, which notices which
  4227. instructions need to have their operands fetched from the constant table
  4228. and builds the table.
  4229. The algorithm is:
  4230. scan, find an instruction which needs a pcrel move. Look forward, find the
  4231. last barrier which is within MAX_COUNT bytes of the requirement.
  4232. If there isn't one, make one. Process all the instructions between
  4233. the find and the barrier.
  4234. In the above example, we can tell that L3 is within 1k of L1, so
  4235. the first move can be shrunk from the 3 insn+constant sequence into
  4236. just 1 insn, and the constant moved to L3 to make:
  4237. mov.l L1,rn
  4238. ..
  4239. mov.l L3,rn
  4240. bra L4
  4241. nop
  4242. align
  4243. L3:.long value
  4244. L4:.long value
  4245. Then the second move becomes the target for the shortening process. */
  4246. typedef struct
  4247. {
  4248. rtx value; /* Value in table. */
  4249. rtx_code_label *label; /* Label of value. */
  4250. label_ref_list_t wend; /* End of window. */
  4251. machine_mode mode; /* Mode of value. */
  4252. /* True if this constant is accessed as part of a post-increment
  4253. sequence. Note that HImode constants are never accessed in this way. */
  4254. bool part_of_sequence_p;
  4255. } pool_node;
  4256. /* The maximum number of constants that can fit into one pool, since
  4257. constants in the range 0..510 are at least 2 bytes long, and in the
  4258. range from there to 1018 at least 4 bytes. */
  4259. #define MAX_POOL_SIZE 372
  4260. static pool_node pool_vector[MAX_POOL_SIZE];
  4261. static int pool_size;
  4262. static rtx_code_label *pool_window_label;
  4263. static int pool_window_last;
  4264. static int max_labelno_before_reorg;
  4265. /* ??? If we need a constant in HImode which is the truncated value of a
  4266. constant we need in SImode, we could combine the two entries thus saving
  4267. two bytes. Is this common enough to be worth the effort of implementing
  4268. it? */
  4269. /* ??? This stuff should be done at the same time that we shorten branches.
  4270. As it is now, we must assume that all branches are the maximum size, and
  4271. this causes us to almost always output constant pools sooner than
  4272. necessary. */
  4273. /* Add a constant to the pool and return its label. */
  4274. static rtx_code_label *
  4275. add_constant (rtx x, machine_mode mode, rtx last_value)
  4276. {
  4277. int i;
  4278. rtx_code_label *lab, *new_rtx;
  4279. label_ref_list_t ref, newref;
  4280. /* First see if we've already got it. */
  4281. for (i = 0; i < pool_size; i++)
  4282. {
  4283. if (x->code == pool_vector[i].value->code
  4284. && mode == pool_vector[i].mode)
  4285. {
  4286. if (x->code == CODE_LABEL)
  4287. {
  4288. if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
  4289. continue;
  4290. }
  4291. if (rtx_equal_p (x, pool_vector[i].value))
  4292. {
  4293. lab = new_rtx = 0;
  4294. if (! last_value
  4295. || ! i
  4296. || ! rtx_equal_p (last_value, pool_vector[i-1].value))
  4297. {
  4298. new_rtx = gen_label_rtx ();
  4299. LABEL_REFS (new_rtx) = pool_vector[i].label;
  4300. pool_vector[i].label = lab = new_rtx;
  4301. }
  4302. if (lab && pool_window_label)
  4303. {
  4304. newref = (label_ref_list_t) pool_alloc (label_ref_list_pool);
  4305. newref->label = pool_window_label;
  4306. ref = pool_vector[pool_window_last].wend;
  4307. newref->next = ref;
  4308. pool_vector[pool_window_last].wend = newref;
  4309. }
  4310. if (new_rtx)
  4311. pool_window_label = new_rtx;
  4312. pool_window_last = i;
  4313. return lab;
  4314. }
  4315. }
  4316. }
  4317. /* Need a new one. */
  4318. pool_vector[pool_size].value = x;
  4319. if (last_value && rtx_equal_p (last_value, pool_vector[pool_size - 1].value))
  4320. {
  4321. lab = 0;
  4322. pool_vector[pool_size - 1].part_of_sequence_p = true;
  4323. }
  4324. else
  4325. lab = gen_label_rtx ();
  4326. pool_vector[pool_size].mode = mode;
  4327. pool_vector[pool_size].label = lab;
  4328. pool_vector[pool_size].wend = NULL;
  4329. pool_vector[pool_size].part_of_sequence_p = (lab == 0);
  4330. if (lab && pool_window_label)
  4331. {
  4332. newref = (label_ref_list_t) pool_alloc (label_ref_list_pool);
  4333. newref->label = pool_window_label;
  4334. ref = pool_vector[pool_window_last].wend;
  4335. newref->next = ref;
  4336. pool_vector[pool_window_last].wend = newref;
  4337. }
  4338. if (lab)
  4339. pool_window_label = lab;
  4340. pool_window_last = pool_size;
  4341. pool_size++;
  4342. return lab;
  4343. }
  4344. /* Output the literal table. START, if nonzero, is the first instruction
  4345. this table is needed for, and also indicates that there is at least one
  4346. casesi_worker_2 instruction; We have to emit the operand3 labels from
  4347. these insns at a 4-byte aligned position. BARRIER is the barrier
  4348. after which we are to place the table. */
  4349. static void
  4350. dump_table (rtx_insn *start, rtx_insn *barrier)
  4351. {
  4352. rtx_insn *scan = barrier;
  4353. int i;
  4354. bool need_align = true;
  4355. rtx lab;
  4356. label_ref_list_t ref;
  4357. bool have_df = false;
  4358. /* Do two passes, first time dump out the HI sized constants. */
  4359. for (i = 0; i < pool_size; i++)
  4360. {
  4361. pool_node *p = &pool_vector[i];
  4362. if (p->mode == HImode)
  4363. {
  4364. if (need_align)
  4365. {
  4366. scan = emit_insn_after (gen_align_2 (), scan);
  4367. need_align = false;
  4368. }
  4369. for (lab = p->label; lab; lab = LABEL_REFS (lab))
  4370. scan = emit_label_after (lab, scan);
  4371. scan = emit_insn_after (gen_consttable_2 (p->value, const0_rtx),
  4372. scan);
  4373. for (ref = p->wend; ref; ref = ref->next)
  4374. {
  4375. lab = ref->label;
  4376. scan = emit_insn_after (gen_consttable_window_end (lab), scan);
  4377. }
  4378. }
  4379. else if (p->mode == DFmode)
  4380. have_df = true;
  4381. }
  4382. need_align = true;
  4383. if (start)
  4384. {
  4385. scan = emit_insn_after (gen_align_4 (), scan);
  4386. need_align = false;
  4387. for (; start != barrier; start = NEXT_INSN (start))
  4388. if (NONJUMP_INSN_P (start)
  4389. && recog_memoized (start) == CODE_FOR_casesi_worker_2)
  4390. {
  4391. rtx src = SET_SRC (XVECEXP (PATTERN (start), 0, 0));
  4392. rtx lab = XEXP (XVECEXP (src, 0, 3), 0);
  4393. scan = emit_label_after (lab, scan);
  4394. }
  4395. }
  4396. if (TARGET_FMOVD && TARGET_ALIGN_DOUBLE && have_df)
  4397. {
  4398. rtx_insn *align_insn = NULL;
  4399. scan = emit_label_after (gen_label_rtx (), scan);
  4400. scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
  4401. need_align = false;
  4402. for (i = 0; i < pool_size; i++)
  4403. {
  4404. pool_node *p = &pool_vector[i];
  4405. switch (p->mode)
  4406. {
  4407. case HImode:
  4408. break;
  4409. case SImode:
  4410. case SFmode:
  4411. if (align_insn && !p->part_of_sequence_p)
  4412. {
  4413. for (lab = p->label; lab; lab = LABEL_REFS (lab))
  4414. emit_label_before (lab, align_insn);
  4415. emit_insn_before (gen_consttable_4 (p->value, const0_rtx),
  4416. align_insn);
  4417. for (ref = p->wend; ref; ref = ref->next)
  4418. {
  4419. lab = ref->label;
  4420. emit_insn_before (gen_consttable_window_end (lab),
  4421. align_insn);
  4422. }
  4423. delete_insn (align_insn);
  4424. align_insn = NULL;
  4425. continue;
  4426. }
  4427. else
  4428. {
  4429. for (lab = p->label; lab; lab = LABEL_REFS (lab))
  4430. scan = emit_label_after (lab, scan);
  4431. scan = emit_insn_after (gen_consttable_4 (p->value,
  4432. const0_rtx), scan);
  4433. need_align = ! need_align;
  4434. }
  4435. break;
  4436. case DFmode:
  4437. if (need_align)
  4438. {
  4439. scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
  4440. align_insn = scan;
  4441. need_align = false;
  4442. }
  4443. case DImode:
  4444. for (lab = p->label; lab; lab = LABEL_REFS (lab))
  4445. scan = emit_label_after (lab, scan);
  4446. scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
  4447. scan);
  4448. break;
  4449. default:
  4450. gcc_unreachable ();
  4451. }
  4452. if (p->mode != HImode)
  4453. {
  4454. for (ref = p->wend; ref; ref = ref->next)
  4455. {
  4456. lab = ref->label;
  4457. scan = emit_insn_after (gen_consttable_window_end (lab),
  4458. scan);
  4459. }
  4460. }
  4461. }
  4462. pool_size = 0;
  4463. }
  4464. for (i = 0; i < pool_size; i++)
  4465. {
  4466. pool_node *p = &pool_vector[i];
  4467. switch (p->mode)
  4468. {
  4469. case HImode:
  4470. break;
  4471. case SImode:
  4472. case SFmode:
  4473. if (need_align)
  4474. {
  4475. need_align = false;
  4476. scan = emit_label_after (gen_label_rtx (), scan);
  4477. scan = emit_insn_after (gen_align_4 (), scan);
  4478. }
  4479. for (lab = p->label; lab; lab = LABEL_REFS (lab))
  4480. scan = emit_label_after (lab, scan);
  4481. scan = emit_insn_after (gen_consttable_4 (p->value, const0_rtx),
  4482. scan);
  4483. break;
  4484. case DFmode:
  4485. case DImode:
  4486. if (need_align)
  4487. {
  4488. need_align = false;
  4489. scan = emit_label_after (gen_label_rtx (), scan);
  4490. scan = emit_insn_after (gen_align_4 (), scan);
  4491. }
  4492. for (lab = p->label; lab; lab = LABEL_REFS (lab))
  4493. scan = emit_label_after (lab, scan);
  4494. scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
  4495. scan);
  4496. break;
  4497. default:
  4498. gcc_unreachable ();
  4499. }
  4500. if (p->mode != HImode)
  4501. {
  4502. for (ref = p->wend; ref; ref = ref->next)
  4503. {
  4504. lab = ref->label;
  4505. scan = emit_insn_after (gen_consttable_window_end (lab), scan);
  4506. }
  4507. }
  4508. }
  4509. scan = emit_insn_after (gen_consttable_end (), scan);
  4510. scan = emit_barrier_after (scan);
  4511. pool_size = 0;
  4512. pool_window_label = NULL;
  4513. pool_window_last = 0;
  4514. }
  4515. #define MOVA_LABELREF(mova) XVECEXP (SET_SRC (PATTERN (mova)), 0, 0)
  4516. /* Nonzero if the insn is a move instruction which needs to be fixed. */
  4517. /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
  4518. CONST_DOUBLE input value is CONST_OK_FOR_I08. For a SFmode move, we don't
  4519. need to fix it if the input value is CONST_OK_FOR_I08. */
  4520. static bool
  4521. broken_move (rtx_insn *insn)
  4522. {
  4523. if (NONJUMP_INSN_P (insn))
  4524. {
  4525. rtx pat = PATTERN (insn);
  4526. if (GET_CODE (pat) == PARALLEL)
  4527. pat = XVECEXP (pat, 0, 0);
  4528. if (GET_CODE (pat) == SET
  4529. /* We can load any 8-bit value if we don't care what the high
  4530. order bits end up as. */
  4531. && GET_MODE (SET_DEST (pat)) != QImode
  4532. && (CONSTANT_P (SET_SRC (pat))
  4533. || (GET_CODE (SET_SRC (pat)) == UNSPEC_VOLATILE
  4534. && XINT (SET_SRC (pat), 1) == UNSPECV_SP_SWITCH_B)
  4535. /* Match mova_const. */
  4536. || (GET_CODE (SET_SRC (pat)) == UNSPEC
  4537. && XINT (SET_SRC (pat), 1) == UNSPEC_MOVA
  4538. && GET_CODE (XVECEXP (SET_SRC (pat), 0, 0)) == CONST))
  4539. && ! (TARGET_SH2E
  4540. && GET_CODE (SET_SRC (pat)) == CONST_DOUBLE
  4541. && (fp_zero_operand (SET_SRC (pat))
  4542. || fp_one_operand (SET_SRC (pat)))
  4543. /* In general we don't know the current setting of fpscr, so
  4544. disable fldi.
  4545. There is an exception if this was a register-register move
  4546. before reload - and hence it was ascertained that we have
  4547. single precision setting - and in a post-reload optimization
  4548. we changed this to do a constant load. In that case
  4549. we don't have an r0 clobber, hence we must use fldi. */
  4550. && (TARGET_FMOVD
  4551. || (GET_CODE (XEXP (XVECEXP (PATTERN (insn), 0, 2), 0))
  4552. == SCRATCH))
  4553. && REG_P (SET_DEST (pat))
  4554. && FP_REGISTER_P (REGNO (SET_DEST (pat))))
  4555. && ! (TARGET_SH2A
  4556. && GET_MODE (SET_DEST (pat)) == SImode
  4557. && (satisfies_constraint_I20 (SET_SRC (pat))
  4558. || satisfies_constraint_I28 (SET_SRC (pat))))
  4559. && ! satisfies_constraint_I08 (SET_SRC (pat)))
  4560. return true;
  4561. }
  4562. return false;
  4563. }
  4564. /* Return true if the specified insn is a mova insn. */
  4565. static bool
  4566. mova_p (rtx_insn *insn)
  4567. {
  4568. return (NONJUMP_INSN_P (insn)
  4569. && GET_CODE (PATTERN (insn)) == SET
  4570. && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
  4571. && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_MOVA
  4572. /* Don't match mova_const. */
  4573. && GET_CODE (MOVA_LABELREF (insn)) == LABEL_REF);
  4574. }
  4575. /* Fix up a mova from a switch that went out of range. */
  4576. static void
  4577. fixup_mova (rtx_insn *mova)
  4578. {
  4579. PUT_MODE (XEXP (MOVA_LABELREF (mova), 0), QImode);
  4580. if (! flag_pic)
  4581. {
  4582. SET_SRC (PATTERN (mova)) = MOVA_LABELREF (mova);
  4583. INSN_CODE (mova) = -1;
  4584. }
  4585. else
  4586. {
  4587. rtx_insn *worker = mova;
  4588. rtx_code_label *lab = gen_label_rtx ();
  4589. rtx wpat, wpat0, wpat1, wsrc, target, base, diff;
  4590. do
  4591. {
  4592. worker = NEXT_INSN (worker);
  4593. gcc_assert (worker
  4594. && !LABEL_P (worker)
  4595. && !JUMP_P (worker));
  4596. } while (NOTE_P (worker)
  4597. || recog_memoized (worker) != CODE_FOR_casesi_worker_1);
  4598. wpat = PATTERN (worker);
  4599. wpat0 = XVECEXP (wpat, 0, 0);
  4600. wpat1 = XVECEXP (wpat, 0, 1);
  4601. wsrc = SET_SRC (wpat0);
  4602. PATTERN (worker) = (gen_casesi_worker_2
  4603. (SET_DEST (wpat0), XVECEXP (wsrc, 0, 1),
  4604. XEXP (XVECEXP (wsrc, 0, 2), 0), lab,
  4605. XEXP (wpat1, 0)));
  4606. INSN_CODE (worker) = -1;
  4607. target = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
  4608. base = gen_rtx_LABEL_REF (Pmode, lab);
  4609. diff = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, target, base), UNSPEC_SYMOFF);
  4610. SET_SRC (PATTERN (mova)) = gen_rtx_CONST (Pmode, diff);
  4611. INSN_CODE (mova) = -1;
  4612. }
  4613. }
  4614. /* NEW_MOVA is a mova we've just encountered while scanning forward. Update
  4615. *num_mova, and check if the new mova is not nested within the first one.
  4616. return 0 if *first_mova was replaced, 1 if new_mova was replaced,
  4617. 2 if new_mova has been assigned to *first_mova, -1 otherwise.. */
  4618. static int
  4619. untangle_mova (int *num_mova, rtx_insn **first_mova, rtx_insn *new_mova)
  4620. {
  4621. int n_addr = 0; /* Initialization to shut up spurious warning. */
  4622. int f_target, n_target = 0; /* Likewise. */
  4623. if (optimize)
  4624. {
  4625. /* If NEW_MOVA has no address yet, it will be handled later. */
  4626. if (INSN_ADDRESSES_SIZE() <= (unsigned) INSN_UID (new_mova))
  4627. return -1;
  4628. n_addr = INSN_ADDRESSES (INSN_UID (new_mova));
  4629. n_target = INSN_ADDRESSES (INSN_UID (XEXP (MOVA_LABELREF (new_mova), 0)));
  4630. if (n_addr > n_target || n_addr + 1022 < n_target)
  4631. {
  4632. /* Change the mova into a load.
  4633. broken_move will then return true for it. */
  4634. fixup_mova (new_mova);
  4635. return 1;
  4636. }
  4637. }
  4638. if (!(*num_mova)++)
  4639. {
  4640. *first_mova = new_mova;
  4641. return 2;
  4642. }
  4643. if (!optimize
  4644. || ((f_target
  4645. = INSN_ADDRESSES (INSN_UID (XEXP (MOVA_LABELREF (*first_mova), 0))))
  4646. >= n_target))
  4647. return -1;
  4648. (*num_mova)--;
  4649. if (f_target - INSN_ADDRESSES (INSN_UID (*first_mova))
  4650. > n_target - n_addr)
  4651. {
  4652. fixup_mova (*first_mova);
  4653. return 0;
  4654. }
  4655. else
  4656. {
  4657. fixup_mova (new_mova);
  4658. return 1;
  4659. }
  4660. }
  4661. /* Find the last barrier from insn FROM which is close enough to hold the
  4662. constant pool. If we can't find one, then create one near the end of
  4663. the range. */
  4664. static rtx_insn *
  4665. find_barrier (int num_mova, rtx_insn *mova, rtx_insn *from)
  4666. {
  4667. int count_si = 0;
  4668. int count_hi = 0;
  4669. int found_hi = 0;
  4670. int found_si = 0;
  4671. int found_di = 0;
  4672. int hi_align = 2;
  4673. int si_align = 2;
  4674. int leading_mova = num_mova;
  4675. rtx_insn *barrier_before_mova = NULL;
  4676. rtx_insn *found_barrier = NULL;
  4677. rtx_insn *good_barrier = NULL;
  4678. int si_limit;
  4679. int hi_limit;
  4680. rtx_insn *orig = from;
  4681. rtx_insn *last_got = NULL;
  4682. rtx_insn *last_symoff = NULL;
  4683. /* For HImode: range is 510, add 4 because pc counts from address of
  4684. second instruction after this one, subtract 2 for the jump instruction
  4685. that we may need to emit before the table, subtract 2 for the instruction
  4686. that fills the jump delay slot (in very rare cases, reorg will take an
  4687. instruction from after the constant pool or will leave the delay slot
  4688. empty). This gives 510.
  4689. For SImode: range is 1020, add 4 because pc counts from address of
  4690. second instruction after this one, subtract 2 in case pc is 2 byte
  4691. aligned, subtract 2 for the jump instruction that we may need to emit
  4692. before the table, subtract 2 for the instruction that fills the jump
  4693. delay slot. This gives 1018. */
  4694. /* The branch will always be shortened now that the reference address for
  4695. forward branches is the successor address, thus we need no longer make
  4696. adjustments to the [sh]i_limit for -O0. */
  4697. si_limit = 1018;
  4698. hi_limit = 510;
  4699. while (from && count_si < si_limit && count_hi < hi_limit)
  4700. {
  4701. int inc = get_attr_length (from);
  4702. int new_align = 1;
  4703. /* If this is a label that existed at the time of the compute_alignments
  4704. call, determine the alignment. N.B. When find_barrier recurses for
  4705. an out-of-reach mova, we might see labels at the start of previously
  4706. inserted constant tables. */
  4707. if (LABEL_P (from)
  4708. && CODE_LABEL_NUMBER (from) <= max_labelno_before_reorg)
  4709. {
  4710. if (optimize)
  4711. new_align = 1 << label_to_alignment (from);
  4712. else if (BARRIER_P (prev_nonnote_insn (from)))
  4713. new_align = 1 << barrier_align (from);
  4714. else
  4715. new_align = 1;
  4716. inc = 0;
  4717. }
  4718. /* In case we are scanning a constant table because of recursion, check
  4719. for explicit alignments. If the table is long, we might be forced
  4720. to emit the new table in front of it; the length of the alignment
  4721. might be the last straw. */
  4722. else if (NONJUMP_INSN_P (from)
  4723. && GET_CODE (PATTERN (from)) == UNSPEC_VOLATILE
  4724. && XINT (PATTERN (from), 1) == UNSPECV_ALIGN)
  4725. new_align = INTVAL (XVECEXP (PATTERN (from), 0, 0));
  4726. /* When we find the end of a constant table, paste the new constant
  4727. at the end. That is better than putting it in front because
  4728. this way, we don't need extra alignment for adding a 4-byte-aligned
  4729. mov(a) label to a 2/4 or 8/4 byte aligned table. */
  4730. else if (NONJUMP_INSN_P (from)
  4731. && GET_CODE (PATTERN (from)) == UNSPEC_VOLATILE
  4732. && XINT (PATTERN (from), 1) == UNSPECV_CONST_END)
  4733. return from;
  4734. if (BARRIER_P (from))
  4735. {
  4736. rtx_insn *next;
  4737. found_barrier = from;
  4738. /* If we are at the end of the function, or in front of an alignment
  4739. instruction, we need not insert an extra alignment. We prefer
  4740. this kind of barrier. */
  4741. if (barrier_align (from) > 2)
  4742. good_barrier = from;
  4743. /* If we are at the end of a hot/cold block, dump the constants
  4744. here. */
  4745. next = NEXT_INSN (from);
  4746. if (next
  4747. && NOTE_P (next)
  4748. && NOTE_KIND (next) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
  4749. break;
  4750. }
  4751. if (broken_move (from))
  4752. {
  4753. rtx pat, src, dst;
  4754. machine_mode mode;
  4755. pat = PATTERN (from);
  4756. if (GET_CODE (pat) == PARALLEL)
  4757. pat = XVECEXP (pat, 0, 0);
  4758. src = SET_SRC (pat);
  4759. dst = SET_DEST (pat);
  4760. mode = GET_MODE (dst);
  4761. /* GOT pcrelat setting comes in pair of
  4762. mova .L8,r0
  4763. mov.l .L8,r12
  4764. instructions. (plus add r0,r12).
  4765. Remember if we see one without the other. */
  4766. if (GET_CODE (src) == UNSPEC && PIC_ADDR_P (XVECEXP (src, 0, 0)))
  4767. last_got = last_got ? NULL : from;
  4768. else if (PIC_ADDR_P (src))
  4769. last_got = last_got ? NULL : from;
  4770. /* We must explicitly check the mode, because sometimes the
  4771. front end will generate code to load unsigned constants into
  4772. HImode targets without properly sign extending them. */
  4773. if (mode == HImode
  4774. || (mode == SImode && satisfies_constraint_I16 (src)
  4775. && REGNO (dst) != FPUL_REG))
  4776. {
  4777. found_hi += 2;
  4778. /* We put the short constants before the long constants, so
  4779. we must count the length of short constants in the range
  4780. for the long constants. */
  4781. /* ??? This isn't optimal, but is easy to do. */
  4782. si_limit -= 2;
  4783. }
  4784. else
  4785. {
  4786. /* We dump DF/DI constants before SF/SI ones, because
  4787. the limit is the same, but the alignment requirements
  4788. are higher. We may waste up to 4 additional bytes
  4789. for alignment, and the DF/DI constant may have
  4790. another SF/SI constant placed before it. */
  4791. if (TARGET_SHCOMPACT
  4792. && ! found_di
  4793. && (mode == DFmode || mode == DImode))
  4794. {
  4795. found_di = 1;
  4796. si_limit -= 8;
  4797. }
  4798. while (si_align > 2 && found_si + si_align - 2 > count_si)
  4799. si_align >>= 1;
  4800. if (found_si > count_si)
  4801. count_si = found_si;
  4802. found_si += GET_MODE_SIZE (mode);
  4803. if (num_mova)
  4804. si_limit -= GET_MODE_SIZE (mode);
  4805. }
  4806. }
  4807. if (mova_p (from))
  4808. {
  4809. switch (untangle_mova (&num_mova, &mova, from))
  4810. {
  4811. case 1:
  4812. if (flag_pic)
  4813. {
  4814. rtx src = SET_SRC (PATTERN (from));
  4815. if (GET_CODE (src) == CONST
  4816. && GET_CODE (XEXP (src, 0)) == UNSPEC
  4817. && XINT (XEXP (src, 0), 1) == UNSPEC_SYMOFF)
  4818. last_symoff = from;
  4819. }
  4820. break;
  4821. case 0: return find_barrier (0, 0, mova);
  4822. case 2:
  4823. {
  4824. leading_mova = 0;
  4825. barrier_before_mova
  4826. = good_barrier ? good_barrier : found_barrier;
  4827. }
  4828. default: break;
  4829. }
  4830. if (found_si > count_si)
  4831. count_si = found_si;
  4832. }
  4833. else if (JUMP_TABLE_DATA_P (from)
  4834. && GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC)
  4835. {
  4836. if ((num_mova > 1 && GET_MODE (prev_nonnote_insn (from)) == VOIDmode)
  4837. || (num_mova
  4838. && (prev_nonnote_insn (from)
  4839. == XEXP (MOVA_LABELREF (mova), 0))))
  4840. num_mova--;
  4841. if (barrier_align (next_real_insn (from)) == align_jumps_log)
  4842. {
  4843. /* We have just passed the barrier in front of the
  4844. ADDR_DIFF_VEC, which is stored in found_barrier. Since
  4845. the ADDR_DIFF_VEC is accessed as data, just like our pool
  4846. constants, this is a good opportunity to accommodate what
  4847. we have gathered so far.
  4848. If we waited any longer, we could end up at a barrier in
  4849. front of code, which gives worse cache usage for separated
  4850. instruction / data caches. */
  4851. good_barrier = found_barrier;
  4852. break;
  4853. }
  4854. else
  4855. {
  4856. rtx body = PATTERN (from);
  4857. inc = XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body));
  4858. }
  4859. }
  4860. /* For the SH1, we generate alignments even after jumps-around-jumps. */
  4861. else if (JUMP_P (from)
  4862. && ! TARGET_SH2
  4863. && ! optimize_size)
  4864. new_align = 4;
  4865. /* There is a possibility that a bf is transformed into a bf/s by the
  4866. delay slot scheduler. */
  4867. if (JUMP_P (from)
  4868. && get_attr_type (from) == TYPE_CBRANCH
  4869. && ! sequence_insn_p (from))
  4870. inc += 2;
  4871. if (found_si)
  4872. {
  4873. count_si += inc;
  4874. if (new_align > si_align)
  4875. {
  4876. si_limit -= (count_si - 1) & (new_align - si_align);
  4877. si_align = new_align;
  4878. }
  4879. count_si = (count_si + new_align - 1) & -new_align;
  4880. }
  4881. if (found_hi)
  4882. {
  4883. count_hi += inc;
  4884. if (new_align > hi_align)
  4885. {
  4886. hi_limit -= (count_hi - 1) & (new_align - hi_align);
  4887. hi_align = new_align;
  4888. }
  4889. count_hi = (count_hi + new_align - 1) & -new_align;
  4890. }
  4891. from = NEXT_INSN (from);
  4892. }
  4893. if (num_mova)
  4894. {
  4895. if (leading_mova)
  4896. {
  4897. /* Try as we might, the leading mova is out of range. Change
  4898. it into a load (which will become a pcload) and retry. */
  4899. fixup_mova (mova);
  4900. return find_barrier (0, 0, mova);
  4901. }
  4902. else
  4903. {
  4904. /* Insert the constant pool table before the mova instruction,
  4905. to prevent the mova label reference from going out of range. */
  4906. from = mova;
  4907. good_barrier = found_barrier = barrier_before_mova;
  4908. }
  4909. }
  4910. if (found_barrier)
  4911. {
  4912. if (good_barrier && next_real_insn (found_barrier))
  4913. found_barrier = good_barrier;
  4914. }
  4915. else
  4916. {
  4917. /* We didn't find a barrier in time to dump our stuff,
  4918. so we'll make one. */
  4919. rtx_code_label *label = gen_label_rtx ();
  4920. /* Don't emit a constant table in the middle of insns for
  4921. casesi_worker_2. This is a bit overkill but is enough
  4922. because casesi_worker_2 wouldn't appear so frequently. */
  4923. if (last_symoff)
  4924. from = last_symoff;
  4925. /* If we exceeded the range, then we must back up over the last
  4926. instruction we looked at. Otherwise, we just need to undo the
  4927. NEXT_INSN at the end of the loop. */
  4928. if (PREV_INSN (from) != orig
  4929. && (count_hi > hi_limit || count_si > si_limit))
  4930. from = PREV_INSN (PREV_INSN (from));
  4931. else
  4932. from = PREV_INSN (from);
  4933. /* Don't emit a constant table int the middle of global pointer setting,
  4934. since that that would move the addressing base GOT into another table.
  4935. We need the first mov instruction before the _GLOBAL_OFFSET_TABLE_
  4936. in the pool anyway, so just move up the whole constant pool.
  4937. However, avoid doing so when the last single GOT mov is the starting
  4938. insn itself. Going past above the start insn would create a negative
  4939. offset, causing errors. */
  4940. if (last_got && last_got != orig)
  4941. from = PREV_INSN (last_got);
  4942. /* Don't insert the constant pool table at the position which
  4943. may be the landing pad. */
  4944. if (flag_exceptions
  4945. && CALL_P (from)
  4946. && find_reg_note (from, REG_EH_REGION, NULL_RTX))
  4947. from = PREV_INSN (from);
  4948. /* Walk back to be just before any jump or label.
  4949. Putting it before a label reduces the number of times the branch
  4950. around the constant pool table will be hit. Putting it before
  4951. a jump makes it more likely that the bra delay slot will be
  4952. filled. */
  4953. while (NOTE_P (from) || JUMP_P (from)
  4954. || LABEL_P (from))
  4955. from = PREV_INSN (from);
  4956. /* Make sure we do not split between a call and its corresponding
  4957. CALL_ARG_LOCATION note. */
  4958. if (CALL_P (from))
  4959. {
  4960. rtx_insn *next = NEXT_INSN (from);
  4961. if (next && NOTE_P (next)
  4962. && NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
  4963. from = next;
  4964. }
  4965. from = emit_jump_insn_after (gen_jump (label), from);
  4966. JUMP_LABEL (from) = label;
  4967. LABEL_NUSES (label) = 1;
  4968. found_barrier = emit_barrier_after (from);
  4969. emit_label_after (label, found_barrier);
  4970. }
  4971. return found_barrier;
  4972. }
  4973. /* If the instruction INSN is implemented by a special function, and we can
  4974. positively find the register that is used to call the sfunc, and this
  4975. register is not used anywhere else in this instruction - except as the
  4976. destination of a set, return this register; else, return 0. */
  4977. rtx
  4978. sfunc_uses_reg (rtx_insn *insn)
  4979. {
  4980. int i;
  4981. rtx pattern, part, reg_part, reg;
  4982. if (!NONJUMP_INSN_P (insn))
  4983. return NULL_RTX;
  4984. pattern = PATTERN (insn);
  4985. if (GET_CODE (pattern) != PARALLEL || get_attr_type (insn) != TYPE_SFUNC)
  4986. return NULL_RTX;
  4987. for (reg_part = NULL_RTX, i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
  4988. {
  4989. part = XVECEXP (pattern, 0, i);
  4990. if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == SImode)
  4991. reg_part = part;
  4992. }
  4993. if (! reg_part)
  4994. return NULL_RTX;
  4995. reg = XEXP (reg_part, 0);
  4996. for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
  4997. {
  4998. part = XVECEXP (pattern, 0, i);
  4999. if (part == reg_part || GET_CODE (part) == CLOBBER)
  5000. continue;
  5001. if (reg_mentioned_p (reg, ((GET_CODE (part) == SET
  5002. && REG_P (SET_DEST (part)))
  5003. ? SET_SRC (part) : part)))
  5004. return NULL_RTX;
  5005. }
  5006. return reg;
  5007. }
  5008. /* See if the only way in which INSN uses REG is by calling it, or by
  5009. setting it while calling it. Set *SET to a SET rtx if the register
  5010. is set by INSN. */
  5011. static bool
  5012. noncall_uses_reg (rtx reg, rtx_insn *insn, rtx *set)
  5013. {
  5014. rtx pattern, reg2;
  5015. *set = NULL_RTX;
  5016. reg2 = sfunc_uses_reg (insn);
  5017. if (reg2 && REGNO (reg2) == REGNO (reg))
  5018. {
  5019. pattern = single_set (insn);
  5020. if (pattern
  5021. && REG_P (SET_DEST (pattern))
  5022. && REGNO (reg) == REGNO (SET_DEST (pattern)))
  5023. *set = pattern;
  5024. return false;
  5025. }
  5026. if (!CALL_P (insn))
  5027. {
  5028. /* We don't use rtx_equal_p because we don't care if the mode is
  5029. different. */
  5030. pattern = single_set (insn);
  5031. if (pattern
  5032. && REG_P (SET_DEST (pattern))
  5033. && REGNO (reg) == REGNO (SET_DEST (pattern)))
  5034. {
  5035. rtx par, part;
  5036. int i;
  5037. *set = pattern;
  5038. par = PATTERN (insn);
  5039. if (GET_CODE (par) == PARALLEL)
  5040. for (i = XVECLEN (par, 0) - 1; i >= 0; i--)
  5041. {
  5042. part = XVECEXP (par, 0, i);
  5043. if (GET_CODE (part) != SET && reg_mentioned_p (reg, part))
  5044. return true;
  5045. }
  5046. return reg_mentioned_p (reg, SET_SRC (pattern));
  5047. }
  5048. return true;
  5049. }
  5050. pattern = PATTERN (insn);
  5051. if (GET_CODE (pattern) == PARALLEL)
  5052. {
  5053. int i;
  5054. for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
  5055. if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
  5056. return true;
  5057. pattern = XVECEXP (pattern, 0, 0);
  5058. }
  5059. if (GET_CODE (pattern) == SET)
  5060. {
  5061. if (reg_mentioned_p (reg, SET_DEST (pattern)))
  5062. {
  5063. /* We don't use rtx_equal_p, because we don't care if the
  5064. mode is different. */
  5065. if (!REG_P (SET_DEST (pattern))
  5066. || REGNO (reg) != REGNO (SET_DEST (pattern)))
  5067. return true;
  5068. *set = pattern;
  5069. }
  5070. pattern = SET_SRC (pattern);
  5071. }
  5072. if (GET_CODE (pattern) != CALL
  5073. || !MEM_P (XEXP (pattern, 0))
  5074. || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
  5075. return true;
  5076. return false;
  5077. }
  5078. /* Given a X, a pattern of an insn or a part of it, return a mask of used
  5079. general registers. Bits 0..15 mean that the respective registers
  5080. are used as inputs in the instruction. Bits 16..31 mean that the
  5081. registers 0..15, respectively, are used as outputs, or are clobbered.
  5082. IS_DEST should be set to 16 if X is the destination of a SET, else to 0. */
  5083. int
  5084. regs_used (rtx x, int is_dest)
  5085. {
  5086. enum rtx_code code;
  5087. const char *fmt;
  5088. int i, used = 0;
  5089. if (! x)
  5090. return used;
  5091. code = GET_CODE (x);
  5092. switch (code)
  5093. {
  5094. case REG:
  5095. if (REGNO (x) < 16)
  5096. return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
  5097. << (REGNO (x) + is_dest));
  5098. return 0;
  5099. case SUBREG:
  5100. {
  5101. rtx y = SUBREG_REG (x);
  5102. if (!REG_P (y))
  5103. break;
  5104. if (REGNO (y) < 16)
  5105. return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
  5106. << (REGNO (y) +
  5107. subreg_regno_offset (REGNO (y),
  5108. GET_MODE (y),
  5109. SUBREG_BYTE (x),
  5110. GET_MODE (x)) + is_dest));
  5111. return 0;
  5112. }
  5113. case SET:
  5114. return regs_used (SET_SRC (x), 0) | regs_used (SET_DEST (x), 16);
  5115. case RETURN:
  5116. /* If there was a return value, it must have been indicated with USE. */
  5117. return 0x00ffff00;
  5118. case CLOBBER:
  5119. is_dest = 1;
  5120. break;
  5121. case MEM:
  5122. is_dest = 0;
  5123. break;
  5124. case CALL:
  5125. used |= 0x00ff00f0;
  5126. break;
  5127. default:
  5128. break;
  5129. }
  5130. fmt = GET_RTX_FORMAT (code);
  5131. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  5132. {
  5133. if (fmt[i] == 'E')
  5134. {
  5135. int j;
  5136. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  5137. used |= regs_used (XVECEXP (x, i, j), is_dest);
  5138. }
  5139. else if (fmt[i] == 'e')
  5140. used |= regs_used (XEXP (x, i), is_dest);
  5141. }
  5142. return used;
  5143. }
  5144. /* Create an instruction that prevents redirection of a conditional branch
  5145. to the destination of the JUMP with address ADDR.
  5146. If the branch needs to be implemented as an indirect jump, try to find
  5147. a scratch register for it.
  5148. If NEED_BLOCK is 0, don't do anything unless we need a scratch register.
  5149. If any preceding insn that doesn't fit into a delay slot is good enough,
  5150. pass 1. Pass 2 if a definite blocking insn is needed.
  5151. -1 is used internally to avoid deep recursion.
  5152. If a blocking instruction is made or recognized, return it. */
  5153. static rtx_insn *
  5154. gen_block_redirect (rtx_insn *jump, int addr, int need_block)
  5155. {
  5156. int dead = 0;
  5157. rtx_insn *prev = prev_nonnote_insn (jump);
  5158. rtx dest;
  5159. /* First, check if we already have an instruction that satisfies our need. */
  5160. if (prev && NONJUMP_INSN_P (prev) && ! prev->deleted ())
  5161. {
  5162. if (INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
  5163. return prev;
  5164. if (GET_CODE (PATTERN (prev)) == USE
  5165. || GET_CODE (PATTERN (prev)) == CLOBBER
  5166. || get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
  5167. prev = jump;
  5168. else if ((need_block &= ~1) < 0)
  5169. return prev;
  5170. else if (recog_memoized (prev) == CODE_FOR_block_branch_redirect)
  5171. need_block = 0;
  5172. }
  5173. if (GET_CODE (PATTERN (jump)) == RETURN)
  5174. {
  5175. if (! need_block)
  5176. return prev;
  5177. /* Reorg even does nasty things with return insns that cause branches
  5178. to go out of range - see find_end_label and callers. */
  5179. return emit_insn_before (gen_block_branch_redirect (const0_rtx) , jump);
  5180. }
  5181. /* We can't use JUMP_LABEL here because it might be undefined
  5182. when not optimizing. */
  5183. dest = XEXP (SET_SRC (PATTERN (jump)), 0);
  5184. /* If the branch is out of range, try to find a scratch register for it. */
  5185. if (optimize
  5186. && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
  5187. > 4092 + 4098))
  5188. {
  5189. rtx_insn *scan;
  5190. /* Don't look for the stack pointer as a scratch register,
  5191. it would cause trouble if an interrupt occurred. */
  5192. unsigned attempt = 0x7fff, used;
  5193. int jump_left = flag_expensive_optimizations + 1;
  5194. /* It is likely that the most recent eligible instruction is wanted for
  5195. the delay slot. Therefore, find out which registers it uses, and
  5196. try to avoid using them. */
  5197. for (scan = jump; (scan = PREV_INSN (scan)); )
  5198. {
  5199. enum rtx_code code;
  5200. if (scan->deleted ())
  5201. continue;
  5202. code = GET_CODE (scan);
  5203. if (code == CODE_LABEL || code == JUMP_INSN)
  5204. break;
  5205. if (code == INSN
  5206. && GET_CODE (PATTERN (scan)) != USE
  5207. && GET_CODE (PATTERN (scan)) != CLOBBER
  5208. && get_attr_in_delay_slot (scan) == IN_DELAY_SLOT_YES)
  5209. {
  5210. attempt &= ~regs_used (PATTERN (scan), 0);
  5211. break;
  5212. }
  5213. }
  5214. for (used = dead = 0, scan = JUMP_LABEL_AS_INSN (jump);
  5215. (scan = NEXT_INSN (scan)); )
  5216. {
  5217. enum rtx_code code;
  5218. if (scan->deleted ())
  5219. continue;
  5220. code = GET_CODE (scan);
  5221. if (INSN_P (scan))
  5222. {
  5223. used |= regs_used (PATTERN (scan), 0);
  5224. if (code == CALL_INSN)
  5225. used |= regs_used (CALL_INSN_FUNCTION_USAGE (scan), 0);
  5226. dead |= (used >> 16) & ~used;
  5227. if (dead & attempt)
  5228. {
  5229. dead &= attempt;
  5230. break;
  5231. }
  5232. if (code == JUMP_INSN)
  5233. {
  5234. if (jump_left-- && simplejump_p (scan))
  5235. scan = JUMP_LABEL_AS_INSN (scan);
  5236. else
  5237. break;
  5238. }
  5239. }
  5240. }
  5241. /* Mask out the stack pointer again, in case it was
  5242. the only 'free' register we have found. */
  5243. dead &= 0x7fff;
  5244. }
  5245. /* If the immediate destination is still in range, check for possible
  5246. threading with a jump beyond the delay slot insn.
  5247. Don't check if we are called recursively; the jump has been or will be
  5248. checked in a different invocation then. */
  5249. else if (optimize && need_block >= 0)
  5250. {
  5251. rtx_insn *next = next_active_insn (next_active_insn (dest));
  5252. if (next && JUMP_P (next)
  5253. && GET_CODE (PATTERN (next)) == SET
  5254. && recog_memoized (next) == CODE_FOR_jump_compact)
  5255. {
  5256. dest = JUMP_LABEL (next);
  5257. if (dest
  5258. && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
  5259. > 4092 + 4098))
  5260. gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), -1);
  5261. }
  5262. }
  5263. if (dead)
  5264. {
  5265. rtx reg = gen_rtx_REG (SImode, exact_log2 (dead & -dead));
  5266. /* It would be nice if we could convert the jump into an indirect
  5267. jump / far branch right now, and thus exposing all constituent
  5268. instructions to further optimization. However, reorg uses
  5269. simplejump_p to determine if there is an unconditional jump where
  5270. it should try to schedule instructions from the target of the
  5271. branch; simplejump_p fails for indirect jumps even if they have
  5272. a JUMP_LABEL. */
  5273. rtx_insn *insn = emit_insn_before (gen_indirect_jump_scratch
  5274. (reg, GEN_INT (unspec_bbr_uid++)),
  5275. jump);
  5276. /* ??? We would like this to have the scope of the jump, but that
  5277. scope will change when a delay slot insn of an inner scope is added.
  5278. Hence, after delay slot scheduling, we'll have to expect
  5279. NOTE_INSN_BLOCK_END notes between the indirect_jump_scratch and
  5280. the jump. */
  5281. INSN_LOCATION (insn) = INSN_LOCATION (jump);
  5282. INSN_CODE (insn) = CODE_FOR_indirect_jump_scratch;
  5283. return insn;
  5284. }
  5285. else if (need_block)
  5286. /* We can't use JUMP_LABEL here because it might be undefined
  5287. when not optimizing. */
  5288. return emit_insn_before (gen_block_branch_redirect
  5289. (GEN_INT (unspec_bbr_uid++)),
  5290. jump);
  5291. return prev;
  5292. }
  5293. #define CONDJUMP_MIN -252
  5294. #define CONDJUMP_MAX 262
  5295. struct far_branch
  5296. {
  5297. /* A label (to be placed) in front of the jump
  5298. that jumps to our ultimate destination. */
  5299. rtx_insn *near_label;
  5300. /* Where we are going to insert it if we cannot move the jump any farther,
  5301. or the jump itself if we have picked up an existing jump. */
  5302. rtx_insn *insert_place;
  5303. /* The ultimate destination. */
  5304. rtx_insn *far_label;
  5305. struct far_branch *prev;
  5306. /* If the branch has already been created, its address;
  5307. else the address of its first prospective user. */
  5308. int address;
  5309. };
  5310. static void gen_far_branch (struct far_branch *);
  5311. enum mdep_reorg_phase_e mdep_reorg_phase;
  5312. static void
  5313. gen_far_branch (struct far_branch *bp)
  5314. {
  5315. rtx_insn *insn = bp->insert_place;
  5316. rtx_insn *jump;
  5317. rtx_code_label *label = gen_label_rtx ();
  5318. int ok;
  5319. emit_label_after (label, insn);
  5320. if (bp->far_label)
  5321. {
  5322. jump = emit_jump_insn_after (gen_jump (bp->far_label), insn);
  5323. LABEL_NUSES (bp->far_label)++;
  5324. }
  5325. else
  5326. jump = emit_jump_insn_after (gen_return (), insn);
  5327. /* Emit a barrier so that reorg knows that any following instructions
  5328. are not reachable via a fall-through path.
  5329. But don't do this when not optimizing, since we wouldn't suppress the
  5330. alignment for the barrier then, and could end up with out-of-range
  5331. pc-relative loads. */
  5332. if (optimize)
  5333. emit_barrier_after (jump);
  5334. emit_label_after (bp->near_label, insn);
  5335. if (bp->far_label)
  5336. JUMP_LABEL (jump) = bp->far_label;
  5337. else
  5338. {
  5339. rtx pat = PATTERN (jump);
  5340. gcc_assert (ANY_RETURN_P (pat));
  5341. JUMP_LABEL (jump) = pat;
  5342. }
  5343. ok = invert_jump (insn, label, 1);
  5344. gcc_assert (ok);
  5345. /* If we are branching around a jump (rather than a return), prevent
  5346. reorg from using an insn from the jump target as the delay slot insn -
  5347. when reorg did this, it pessimized code (we rather hide the delay slot)
  5348. and it could cause branches to go out of range. */
  5349. if (bp->far_label)
  5350. (emit_insn_after
  5351. (gen_stuff_delay_slot
  5352. (GEN_INT (unspec_bbr_uid++),
  5353. GEN_INT (recog_memoized (insn) == CODE_FOR_branch_false)),
  5354. insn));
  5355. /* Prevent reorg from undoing our splits. */
  5356. gen_block_redirect (jump, bp->address += 2, 2);
  5357. }
  5358. /* Fix up ADDR_DIFF_VECs. */
  5359. void
  5360. fixup_addr_diff_vecs (rtx_insn *first)
  5361. {
  5362. rtx_insn *insn;
  5363. for (insn = first; insn; insn = NEXT_INSN (insn))
  5364. {
  5365. rtx vec_lab, pat, prevpat, x, braf_label;
  5366. rtx_insn *prev;
  5367. if (! JUMP_TABLE_DATA_P (insn)
  5368. || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
  5369. continue;
  5370. pat = PATTERN (insn);
  5371. vec_lab = XEXP (XEXP (pat, 0), 0);
  5372. /* Search the matching casesi_jump_2. */
  5373. for (prev = as_a <rtx_insn *> (vec_lab); ; prev = PREV_INSN (prev))
  5374. {
  5375. if (!JUMP_P (prev))
  5376. continue;
  5377. prevpat = PATTERN (prev);
  5378. if (GET_CODE (prevpat) != PARALLEL || XVECLEN (prevpat, 0) != 2)
  5379. continue;
  5380. x = XVECEXP (prevpat, 0, 1);
  5381. if (GET_CODE (x) != USE)
  5382. continue;
  5383. x = XEXP (x, 0);
  5384. if (GET_CODE (x) == LABEL_REF && XEXP (x, 0) == vec_lab)
  5385. break;
  5386. }
  5387. /* FIXME: This is a bug in the optimizer, but it seems harmless
  5388. to just avoid panicing. */
  5389. if (!prev)
  5390. continue;
  5391. /* Emit the reference label of the braf where it belongs, right after
  5392. the casesi_jump_2 (i.e. braf). */
  5393. braf_label = XEXP (XEXP (SET_SRC (XVECEXP (prevpat, 0, 0)), 1), 0);
  5394. emit_label_after (braf_label, prev);
  5395. /* Fix up the ADDR_DIF_VEC to be relative
  5396. to the reference address of the braf. */
  5397. XEXP (XEXP (pat, 0), 0) = braf_label;
  5398. }
  5399. }
  5400. /* BARRIER_OR_LABEL is either a BARRIER or a CODE_LABEL immediately following
  5401. a barrier. Return the base 2 logarithm of the desired alignment. */
  5402. int
  5403. barrier_align (rtx_insn *barrier_or_label)
  5404. {
  5405. rtx next, pat;
  5406. if (! barrier_or_label)
  5407. return 0;
  5408. if (LABEL_P (barrier_or_label)
  5409. && NEXT_INSN (barrier_or_label)
  5410. && JUMP_TABLE_DATA_P (NEXT_INSN (barrier_or_label)))
  5411. return 2;
  5412. if (BARRIER_P (barrier_or_label)
  5413. && PREV_INSN (barrier_or_label)
  5414. && JUMP_TABLE_DATA_P (PREV_INSN (barrier_or_label)))
  5415. {
  5416. pat = PATTERN (PREV_INSN (barrier_or_label));
  5417. /* If this is a very small table, we want to keep the alignment after
  5418. the table to the minimum for proper code alignment. */
  5419. return ((optimize_size
  5420. || ((unsigned) XVECLEN (pat, 1) * GET_MODE_SIZE (GET_MODE (pat))
  5421. <= (unsigned) 1 << (CACHE_LOG - 2)))
  5422. ? 1 << TARGET_SHMEDIA : align_jumps_log);
  5423. }
  5424. next = next_active_insn (barrier_or_label);
  5425. if (! next)
  5426. return 0;
  5427. pat = PATTERN (next);
  5428. if (GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == UNSPECV_ALIGN)
  5429. /* This is a barrier in front of a constant table. */
  5430. return 0;
  5431. if (optimize_size)
  5432. return 0;
  5433. if (! TARGET_SH2 || ! optimize)
  5434. return align_jumps_log;
  5435. /* When fixing up pcloads, a constant table might be inserted just before
  5436. the basic block that ends with the barrier. Thus, we can't trust the
  5437. instruction lengths before that. */
  5438. if (mdep_reorg_phase > SH_FIXUP_PCLOAD)
  5439. {
  5440. /* Check if there is an immediately preceding branch to the insn beyond
  5441. the barrier. We must weight the cost of discarding useful information
  5442. from the current cache line when executing this branch and there is
  5443. an alignment, against that of fetching unneeded insn in front of the
  5444. branch target when there is no alignment. */
  5445. /* There are two delay_slot cases to consider. One is the simple case
  5446. where the preceding branch is to the insn beyond the barrier (simple
  5447. delay slot filling), and the other is where the preceding branch has
  5448. a delay slot that is a duplicate of the insn after the barrier
  5449. (fill_eager_delay_slots) and the branch is to the insn after the insn
  5450. after the barrier. */
  5451. int slot, credit;
  5452. bool jump_to_next = false;
  5453. /* Skip to the insn before the JUMP_INSN before the barrier under
  5454. investigation. */
  5455. rtx_insn *prev = prev_real_insn (prev_active_insn (barrier_or_label));
  5456. for (slot = 2, credit = (1 << (CACHE_LOG - 2)) + 2;
  5457. credit >= 0 && prev && NONJUMP_INSN_P (prev);
  5458. prev = prev_real_insn (prev))
  5459. {
  5460. jump_to_next = false;
  5461. if (GET_CODE (PATTERN (prev)) == USE
  5462. || GET_CODE (PATTERN (prev)) == CLOBBER)
  5463. continue;
  5464. if (rtx_sequence *prev_seq = dyn_cast <rtx_sequence *> (PATTERN (prev)))
  5465. {
  5466. prev = prev_seq->insn (1);
  5467. if (INSN_UID (prev) == INSN_UID (next))
  5468. {
  5469. /* Delay slot was filled with insn at jump target. */
  5470. jump_to_next = true;
  5471. continue;
  5472. }
  5473. }
  5474. if (slot &&
  5475. get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
  5476. slot = 0;
  5477. credit -= get_attr_length (prev);
  5478. }
  5479. if (prev && jump_to_label_p (prev))
  5480. {
  5481. rtx_insn *x;
  5482. if (jump_to_next
  5483. || next_real_insn (JUMP_LABEL (prev)) == next
  5484. /* If relax_delay_slots() decides NEXT was redundant
  5485. with some previous instruction, it will have
  5486. redirected PREV's jump to the following insn. */
  5487. || JUMP_LABEL (prev) == next_nonnote_insn (next)
  5488. /* There is no upper bound on redundant instructions
  5489. that might have been skipped, but we must not put an
  5490. alignment where none had been before. */
  5491. || (x = (NEXT_INSN (NEXT_INSN (PREV_INSN (prev)))),
  5492. (INSN_P (x)
  5493. && (INSN_CODE (x) == CODE_FOR_block_branch_redirect
  5494. || INSN_CODE (x) == CODE_FOR_indirect_jump_scratch
  5495. || INSN_CODE (x) == CODE_FOR_stuff_delay_slot))))
  5496. {
  5497. rtx pat = PATTERN (prev);
  5498. if (GET_CODE (pat) == PARALLEL)
  5499. pat = XVECEXP (pat, 0, 0);
  5500. if (credit - slot >= (GET_CODE (SET_SRC (pat)) == PC ? 2 : 0))
  5501. return 0;
  5502. }
  5503. }
  5504. }
  5505. return align_jumps_log;
  5506. }
  5507. /* If we are inside a phony loop, almost any kind of label can turn up as the
  5508. first one in the loop. Aligning a braf label causes incorrect switch
  5509. destination addresses; we can detect braf labels because they are
  5510. followed by a BARRIER.
  5511. Applying loop alignment to small constant or switch tables is a waste
  5512. of space, so we suppress this too. */
  5513. int
  5514. sh_loop_align (rtx_insn *label)
  5515. {
  5516. rtx_insn *next = label;
  5517. if (! optimize || optimize_size)
  5518. return 0;
  5519. do
  5520. next = next_nonnote_insn (next);
  5521. while (next && LABEL_P (next));
  5522. if (! next
  5523. || ! INSN_P (next)
  5524. || recog_memoized (next) == CODE_FOR_consttable_2)
  5525. return 0;
  5526. return align_loops_log;
  5527. }
  5528. /* Do a final pass over the function, just before delayed branch
  5529. scheduling. */
  5530. static void
  5531. sh_reorg (void)
  5532. {
  5533. rtx_insn *first, *insn, *mova = NULL;
  5534. int num_mova;
  5535. rtx r0_rtx = gen_rtx_REG (Pmode, 0);
  5536. rtx r0_inc_rtx = gen_rtx_POST_INC (Pmode, r0_rtx);
  5537. first = get_insns ();
  5538. max_labelno_before_reorg = max_label_num ();
  5539. /* We must split call insns before introducing `mova's. If we're
  5540. optimizing, they'll have already been split. Otherwise, make
  5541. sure we don't split them too late. */
  5542. if (! optimize)
  5543. split_all_insns_noflow ();
  5544. if (TARGET_SHMEDIA)
  5545. return;
  5546. /* If relaxing, generate pseudo-ops to associate function calls with
  5547. the symbols they call. It does no harm to not generate these
  5548. pseudo-ops. However, when we can generate them, it enables the
  5549. linker to potentially relax the jsr to a bsr, and eliminate the
  5550. register load and, possibly, the constant pool entry. */
  5551. mdep_reorg_phase = SH_INSERT_USES_LABELS;
  5552. if (TARGET_RELAX)
  5553. {
  5554. /* Remove all REG_LABEL_OPERAND notes. We want to use them for our
  5555. own purposes. This works because none of the remaining passes
  5556. need to look at them.
  5557. ??? But it may break in the future. We should use a machine
  5558. dependent REG_NOTE, or some other approach entirely. */
  5559. for (insn = first; insn; insn = NEXT_INSN (insn))
  5560. {
  5561. if (INSN_P (insn))
  5562. {
  5563. rtx note;
  5564. while ((note = find_reg_note (insn, REG_LABEL_OPERAND,
  5565. NULL_RTX)) != 0)
  5566. remove_note (insn, note);
  5567. }
  5568. }
  5569. for (insn = first; insn; insn = NEXT_INSN (insn))
  5570. {
  5571. rtx pattern, reg, set, dies;
  5572. rtx_code_label *label;
  5573. rtx_insn *link, *scan;
  5574. int rescan = 0, foundinsn = 0;
  5575. if (CALL_P (insn))
  5576. {
  5577. pattern = PATTERN (insn);
  5578. if (GET_CODE (pattern) == PARALLEL)
  5579. pattern = XVECEXP (pattern, 0, 0);
  5580. if (GET_CODE (pattern) == SET)
  5581. pattern = SET_SRC (pattern);
  5582. if (GET_CODE (pattern) != CALL
  5583. || !MEM_P (XEXP (pattern, 0)))
  5584. continue;
  5585. reg = XEXP (XEXP (pattern, 0), 0);
  5586. }
  5587. else
  5588. {
  5589. reg = sfunc_uses_reg (insn);
  5590. if (! reg)
  5591. continue;
  5592. }
  5593. if (!REG_P (reg))
  5594. continue;
  5595. /* Try scanning backward to find where the register is set. */
  5596. link = NULL;
  5597. for (scan = PREV_INSN (insn);
  5598. scan && !LABEL_P (scan);
  5599. scan = PREV_INSN (scan))
  5600. {
  5601. if (! INSN_P (scan))
  5602. continue;
  5603. if (! reg_mentioned_p (reg, scan))
  5604. continue;
  5605. if (noncall_uses_reg (reg, scan, &set))
  5606. break;
  5607. if (set)
  5608. {
  5609. link = scan;
  5610. break;
  5611. }
  5612. }
  5613. if (! link)
  5614. continue;
  5615. /* The register is set at LINK. */
  5616. /* We can only optimize the function call if the register is
  5617. being set to a symbol. In theory, we could sometimes
  5618. optimize calls to a constant location, but the assembler
  5619. and linker do not support that at present. */
  5620. if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
  5621. && GET_CODE (SET_SRC (set)) != LABEL_REF)
  5622. continue;
  5623. /* Scan forward from LINK to the place where REG dies, and
  5624. make sure that the only insns which use REG are
  5625. themselves function calls. */
  5626. /* ??? This doesn't work for call targets that were allocated
  5627. by reload, since there may not be a REG_DEAD note for the
  5628. register. */
  5629. dies = NULL_RTX;
  5630. for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
  5631. {
  5632. rtx scanset;
  5633. /* Don't try to trace forward past a CODE_LABEL if we haven't
  5634. seen INSN yet. Ordinarily, we will only find the setting insn
  5635. if it is in the same basic block. However,
  5636. cross-jumping can insert code labels in between the load and
  5637. the call, and can result in situations where a single call
  5638. insn may have two targets depending on where we came from. */
  5639. if (LABEL_P (scan) && ! foundinsn)
  5640. break;
  5641. if (! INSN_P (scan))
  5642. continue;
  5643. /* Don't try to trace forward past a JUMP. To optimize
  5644. safely, we would have to check that all the
  5645. instructions at the jump destination did not use REG. */
  5646. if (JUMP_P (scan))
  5647. break;
  5648. if (! reg_mentioned_p (reg, scan))
  5649. continue;
  5650. if (noncall_uses_reg (reg, scan, &scanset))
  5651. break;
  5652. if (scan == insn)
  5653. foundinsn = 1;
  5654. if (scan != insn
  5655. && (CALL_P (scan) || sfunc_uses_reg (scan)))
  5656. {
  5657. /* There is a function call to this register other
  5658. than the one we are checking. If we optimize
  5659. this call, we need to rescan again below. */
  5660. rescan = 1;
  5661. }
  5662. /* ??? We shouldn't have to worry about SCANSET here.
  5663. We should just be able to check for a REG_DEAD note
  5664. on a function call. However, the REG_DEAD notes are
  5665. apparently not dependable around libcalls; c-torture
  5666. execute/920501-2 is a test case. If SCANSET is set,
  5667. then this insn sets the register, so it must have
  5668. died earlier. Unfortunately, this will only handle
  5669. the cases in which the register is, in fact, set in a
  5670. later insn. */
  5671. /* ??? We shouldn't have to use FOUNDINSN here.
  5672. This dates back to when we used LOG_LINKS to find
  5673. the most recent insn which sets the register. */
  5674. if (foundinsn
  5675. && (scanset
  5676. || find_reg_note (scan, REG_DEAD, reg)))
  5677. {
  5678. dies = scan;
  5679. break;
  5680. }
  5681. }
  5682. if (! dies)
  5683. {
  5684. /* Either there was a branch, or some insn used REG
  5685. other than as a function call address. */
  5686. continue;
  5687. }
  5688. /* Create a code label, and put it in a REG_LABEL_OPERAND note
  5689. on the insn which sets the register, and on each call insn
  5690. which uses the register. In final_prescan_insn we look for
  5691. the REG_LABEL_OPERAND notes, and output the appropriate label
  5692. or pseudo-op. */
  5693. label = gen_label_rtx ();
  5694. add_reg_note (link, REG_LABEL_OPERAND, label);
  5695. add_reg_note (insn, REG_LABEL_OPERAND, label);
  5696. if (rescan)
  5697. {
  5698. scan = link;
  5699. do
  5700. {
  5701. rtx reg2;
  5702. scan = NEXT_INSN (scan);
  5703. if (scan != insn
  5704. && ((CALL_P (scan)
  5705. && reg_mentioned_p (reg, scan))
  5706. || ((reg2 = sfunc_uses_reg (scan))
  5707. && REGNO (reg2) == REGNO (reg))))
  5708. add_reg_note (scan, REG_LABEL_OPERAND, label);
  5709. }
  5710. while (scan != dies);
  5711. }
  5712. }
  5713. }
  5714. if (TARGET_SH2)
  5715. fixup_addr_diff_vecs (first);
  5716. if (optimize)
  5717. {
  5718. mdep_reorg_phase = SH_SHORTEN_BRANCHES0;
  5719. shorten_branches (first);
  5720. }
  5721. /* Scan the function looking for move instructions which have to be
  5722. changed to pc-relative loads and insert the literal tables. */
  5723. label_ref_list_pool = create_alloc_pool ("label references list",
  5724. sizeof (struct label_ref_list_d),
  5725. 30);
  5726. mdep_reorg_phase = SH_FIXUP_PCLOAD;
  5727. for (insn = first, num_mova = 0; insn; insn = NEXT_INSN (insn))
  5728. {
  5729. if (mova_p (insn))
  5730. {
  5731. /* ??? basic block reordering can move a switch table dispatch
  5732. below the switch table. Check if that has happened.
  5733. We only have the addresses available when optimizing; but then,
  5734. this check shouldn't be needed when not optimizing. */
  5735. if (!untangle_mova (&num_mova, &mova, insn))
  5736. {
  5737. insn = mova;
  5738. num_mova = 0;
  5739. }
  5740. }
  5741. else if (JUMP_TABLE_DATA_P (insn)
  5742. && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
  5743. && num_mova
  5744. /* ??? loop invariant motion can also move a mova out of a
  5745. loop. Since loop does this code motion anyway, maybe we
  5746. should wrap UNSPEC_MOVA into a CONST, so that reload can
  5747. move it back. */
  5748. && ((num_mova > 1
  5749. && GET_MODE (prev_nonnote_insn (insn)) == VOIDmode)
  5750. || (prev_nonnote_insn (insn)
  5751. == XEXP (MOVA_LABELREF (mova), 0))))
  5752. {
  5753. rtx_insn *scan;
  5754. int total;
  5755. num_mova--;
  5756. /* Some code might have been inserted between the mova and
  5757. its ADDR_DIFF_VEC. Check if the mova is still in range. */
  5758. for (scan = mova, total = 0; scan != insn; scan = NEXT_INSN (scan))
  5759. total += get_attr_length (scan);
  5760. /* range of mova is 1020, add 4 because pc counts from address of
  5761. second instruction after this one, subtract 2 in case pc is 2
  5762. byte aligned. Possible alignment needed for the ADDR_DIFF_VEC
  5763. cancels out with alignment effects of the mova itself. */
  5764. if (total > 1022)
  5765. {
  5766. /* Change the mova into a load, and restart scanning
  5767. there. broken_move will then return true for mova. */
  5768. fixup_mova (mova);
  5769. insn = mova;
  5770. }
  5771. }
  5772. if (broken_move (insn)
  5773. || (NONJUMP_INSN_P (insn)
  5774. && recog_memoized (insn) == CODE_FOR_casesi_worker_2))
  5775. {
  5776. rtx_insn *scan;
  5777. /* Scan ahead looking for a barrier to stick the constant table
  5778. behind. */
  5779. rtx_insn *barrier = find_barrier (num_mova, mova, insn);
  5780. rtx_insn *last_float_move = NULL;
  5781. rtx last_float = 0, *last_float_addr = NULL;
  5782. int need_aligned_label = 0;
  5783. if (num_mova && ! mova_p (mova))
  5784. {
  5785. /* find_barrier had to change the first mova into a
  5786. pcload; thus, we have to start with this new pcload. */
  5787. insn = mova;
  5788. num_mova = 0;
  5789. }
  5790. /* Now find all the moves between the points and modify them. */
  5791. for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
  5792. {
  5793. if (LABEL_P (scan))
  5794. last_float = 0;
  5795. if (NONJUMP_INSN_P (scan)
  5796. && recog_memoized (scan) == CODE_FOR_casesi_worker_2)
  5797. need_aligned_label = 1;
  5798. if (broken_move (scan))
  5799. {
  5800. rtx *patp = &PATTERN (scan), pat = *patp;
  5801. rtx src, dst;
  5802. rtx lab;
  5803. rtx newsrc;
  5804. machine_mode mode;
  5805. if (GET_CODE (pat) == PARALLEL)
  5806. patp = &XVECEXP (pat, 0, 0), pat = *patp;
  5807. src = SET_SRC (pat);
  5808. dst = SET_DEST (pat);
  5809. mode = GET_MODE (dst);
  5810. if (mode == SImode && satisfies_constraint_I16 (src)
  5811. && REGNO (dst) != FPUL_REG)
  5812. {
  5813. int offset = 0;
  5814. mode = HImode;
  5815. while (GET_CODE (dst) == SUBREG)
  5816. {
  5817. offset += subreg_regno_offset (REGNO (SUBREG_REG (dst)),
  5818. GET_MODE (SUBREG_REG (dst)),
  5819. SUBREG_BYTE (dst),
  5820. GET_MODE (dst));
  5821. dst = SUBREG_REG (dst);
  5822. }
  5823. dst = gen_rtx_REG (HImode, REGNO (dst) + offset);
  5824. }
  5825. if (REG_P (dst) && FP_ANY_REGISTER_P (REGNO (dst)))
  5826. {
  5827. /* This must be an insn that clobbers r0. */
  5828. rtx *clobberp = &XVECEXP (PATTERN (scan), 0,
  5829. XVECLEN (PATTERN (scan), 0)
  5830. - 1);
  5831. rtx clobber = *clobberp;
  5832. gcc_assert (GET_CODE (clobber) == CLOBBER
  5833. && rtx_equal_p (XEXP (clobber, 0), r0_rtx));
  5834. if (last_float
  5835. && reg_set_between_p (r0_rtx, last_float_move, scan))
  5836. last_float = 0;
  5837. if (last_float
  5838. && TARGET_SHCOMPACT
  5839. && GET_MODE_SIZE (mode) != 4
  5840. && GET_MODE_SIZE (GET_MODE (last_float)) == 4)
  5841. last_float = 0;
  5842. lab = add_constant (src, mode, last_float);
  5843. if (lab)
  5844. emit_insn_before (gen_mova (lab), scan);
  5845. else
  5846. {
  5847. /* There will be a REG_UNUSED note for r0 on
  5848. LAST_FLOAT_MOVE; we have to change it to REG_INC,
  5849. lest reorg:mark_target_live_regs will not
  5850. consider r0 to be used, and we end up with delay
  5851. slot insn in front of SCAN that clobbers r0. */
  5852. rtx note
  5853. = find_regno_note (last_float_move, REG_UNUSED, 0);
  5854. /* If we are not optimizing, then there may not be
  5855. a note. */
  5856. if (note)
  5857. PUT_REG_NOTE_KIND (note, REG_INC);
  5858. *last_float_addr = r0_inc_rtx;
  5859. }
  5860. last_float_move = scan;
  5861. last_float = src;
  5862. newsrc = gen_const_mem (mode,
  5863. (((TARGET_SH4 && ! TARGET_FMOVD)
  5864. || REGNO (dst) == FPUL_REG)
  5865. ? r0_inc_rtx
  5866. : r0_rtx));
  5867. last_float_addr = &XEXP (newsrc, 0);
  5868. /* Remove the clobber of r0. */
  5869. *clobberp = gen_rtx_CLOBBER (GET_MODE (clobber),
  5870. gen_rtx_SCRATCH (Pmode));
  5871. }
  5872. /* This is a mova needing a label. Create it. */
  5873. else if (GET_CODE (src) == UNSPEC
  5874. && XINT (src, 1) == UNSPEC_MOVA
  5875. && GET_CODE (XVECEXP (src, 0, 0)) == CONST)
  5876. {
  5877. lab = add_constant (XVECEXP (src, 0, 0), mode, 0);
  5878. newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
  5879. newsrc = gen_rtx_UNSPEC (SImode,
  5880. gen_rtvec (1, newsrc),
  5881. UNSPEC_MOVA);
  5882. }
  5883. else if (GET_CODE (src) == UNSPEC_VOLATILE
  5884. && XINT (src, 1) == UNSPECV_SP_SWITCH_B)
  5885. {
  5886. newsrc = XVECEXP (src, 0, 0);
  5887. XVECEXP (src, 0, 0) = gen_const_mem (mode, newsrc);
  5888. INSN_CODE (scan) = -1;
  5889. continue;
  5890. }
  5891. else
  5892. {
  5893. lab = add_constant (src, mode, 0);
  5894. newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
  5895. newsrc = gen_const_mem (mode, newsrc);
  5896. }
  5897. *patp = gen_rtx_SET (VOIDmode, dst, newsrc);
  5898. INSN_CODE (scan) = -1;
  5899. }
  5900. }
  5901. dump_table (need_aligned_label ? insn : 0, barrier);
  5902. insn = barrier;
  5903. }
  5904. }
  5905. free_alloc_pool (label_ref_list_pool);
  5906. for (insn = first; insn; insn = NEXT_INSN (insn))
  5907. PUT_MODE (insn, VOIDmode);
  5908. mdep_reorg_phase = SH_SHORTEN_BRANCHES1;
  5909. INSN_ADDRESSES_FREE ();
  5910. split_branches (first);
  5911. /* The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
  5912. also has an effect on the register that holds the address of the sfunc.
  5913. Insert an extra dummy insn in front of each sfunc that pretends to
  5914. use this register. */
  5915. if (flag_delayed_branch)
  5916. {
  5917. for (insn = first; insn; insn = NEXT_INSN (insn))
  5918. {
  5919. rtx reg = sfunc_uses_reg (insn);
  5920. if (! reg)
  5921. continue;
  5922. emit_insn_before (gen_use_sfunc_addr (reg), insn);
  5923. }
  5924. }
  5925. mdep_reorg_phase = SH_AFTER_MDEP_REORG;
  5926. }
  5927. /* Return the UID of the insn that follows the specified label. */
  5928. int
  5929. get_dest_uid (rtx label, int max_uid)
  5930. {
  5931. rtx_insn *dest = next_real_insn (label);
  5932. int dest_uid;
  5933. if (! dest)
  5934. /* This can happen for an undefined label. */
  5935. return 0;
  5936. dest_uid = INSN_UID (dest);
  5937. /* If this is a newly created branch redirection blocking instruction,
  5938. we cannot index the branch_uid or insn_addresses arrays with its
  5939. uid. But then, we won't need to, because the actual destination is
  5940. the following branch. */
  5941. while (dest_uid >= max_uid)
  5942. {
  5943. dest = NEXT_INSN (dest);
  5944. dest_uid = INSN_UID (dest);
  5945. }
  5946. if (JUMP_P (dest) && GET_CODE (PATTERN (dest)) == RETURN)
  5947. return 0;
  5948. return dest_uid;
  5949. }
  5950. /* Split condbranches that are out of range. Also add clobbers for
  5951. scratch registers that are needed in far jumps.
  5952. We do this before delay slot scheduling, so that it can take our
  5953. newly created instructions into account. It also allows us to
  5954. find branches with common targets more easily. */
  5955. static void
  5956. split_branches (rtx_insn *first)
  5957. {
  5958. rtx_insn *insn;
  5959. struct far_branch **uid_branch, *far_branch_list = 0;
  5960. int max_uid = get_max_uid ();
  5961. int ok;
  5962. /* Find out which branches are out of range. */
  5963. shorten_branches (first);
  5964. uid_branch = (struct far_branch **) alloca (max_uid * sizeof *uid_branch);
  5965. memset ((char *) uid_branch, 0, max_uid * sizeof *uid_branch);
  5966. for (insn = first; insn; insn = NEXT_INSN (insn))
  5967. if (! INSN_P (insn))
  5968. continue;
  5969. else if (insn->deleted ())
  5970. {
  5971. /* Shorten_branches would split this instruction again,
  5972. so transform it into a note. */
  5973. SET_INSN_DELETED (insn);
  5974. }
  5975. else if (JUMP_P (insn))
  5976. {
  5977. enum attr_type type = get_attr_type (insn);
  5978. if (type == TYPE_CBRANCH)
  5979. {
  5980. rtx_insn *next, *beyond;
  5981. if (get_attr_length (insn) > 4)
  5982. {
  5983. rtx src = SET_SRC (PATTERN (insn));
  5984. rtx olabel = XEXP (XEXP (src, 1), 0);
  5985. int addr = INSN_ADDRESSES (INSN_UID (insn));
  5986. rtx_insn *label = 0;
  5987. int dest_uid = get_dest_uid (olabel, max_uid);
  5988. struct far_branch *bp = uid_branch[dest_uid];
  5989. /* redirect_jump needs a valid JUMP_LABEL, and it might delete
  5990. the label if the LABEL_NUSES count drops to zero. There is
  5991. always a jump_optimize pass that sets these values, but it
  5992. proceeds to delete unreferenced code, and then if not
  5993. optimizing, to un-delete the deleted instructions, thus
  5994. leaving labels with too low uses counts. */
  5995. if (! optimize)
  5996. {
  5997. JUMP_LABEL (insn) = olabel;
  5998. LABEL_NUSES (olabel)++;
  5999. }
  6000. if (! bp)
  6001. {
  6002. bp = (struct far_branch *) alloca (sizeof *bp);
  6003. uid_branch[dest_uid] = bp;
  6004. bp->prev = far_branch_list;
  6005. far_branch_list = bp;
  6006. bp->far_label = as_a <rtx_insn *> (
  6007. XEXP (XEXP (SET_SRC (PATTERN (insn)), 1),
  6008. 0));
  6009. LABEL_NUSES (bp->far_label)++;
  6010. }
  6011. else
  6012. {
  6013. label = bp->near_label;
  6014. if (! label && bp->address - addr >= CONDJUMP_MIN)
  6015. {
  6016. rtx_insn *block = bp->insert_place;
  6017. if (GET_CODE (PATTERN (block)) == RETURN)
  6018. block = PREV_INSN (block);
  6019. else
  6020. block = gen_block_redirect (block,
  6021. bp->address, 2);
  6022. label = emit_label_after (gen_label_rtx (),
  6023. PREV_INSN (block));
  6024. bp->near_label = label;
  6025. }
  6026. else if (label && ! NEXT_INSN (label))
  6027. {
  6028. if (addr + 2 - bp->address <= CONDJUMP_MAX)
  6029. bp->insert_place = insn;
  6030. else
  6031. gen_far_branch (bp);
  6032. }
  6033. }
  6034. if (! label
  6035. || (NEXT_INSN (label) && bp->address - addr < CONDJUMP_MIN))
  6036. {
  6037. bp->near_label = label = gen_label_rtx ();
  6038. bp->insert_place = insn;
  6039. bp->address = addr;
  6040. }
  6041. ok = redirect_jump (insn, label, 0);
  6042. gcc_assert (ok);
  6043. }
  6044. else
  6045. {
  6046. /* get_attr_length (insn) == 2 */
  6047. /* Check if we have a pattern where reorg wants to redirect
  6048. the branch to a label from an unconditional branch that
  6049. is too far away. */
  6050. /* We can't use JUMP_LABEL here because it might be undefined
  6051. when not optimizing. */
  6052. /* A syntax error might cause beyond to be NULL_RTX. */
  6053. beyond
  6054. = next_active_insn (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1),
  6055. 0));
  6056. if (beyond
  6057. && (JUMP_P (beyond)
  6058. || ((beyond = next_active_insn (beyond))
  6059. && JUMP_P (beyond)))
  6060. && GET_CODE (PATTERN (beyond)) == SET
  6061. && recog_memoized (beyond) == CODE_FOR_jump_compact
  6062. && ((INSN_ADDRESSES
  6063. (INSN_UID (XEXP (SET_SRC (PATTERN (beyond)), 0)))
  6064. - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
  6065. > 252 + 258 + 2))
  6066. gen_block_redirect (beyond,
  6067. INSN_ADDRESSES (INSN_UID (beyond)), 1);
  6068. }
  6069. next = next_active_insn (insn);
  6070. if (next
  6071. && (JUMP_P (next)
  6072. || ((next = next_active_insn (next))
  6073. && JUMP_P (next)))
  6074. && GET_CODE (PATTERN (next)) == SET
  6075. && recog_memoized (next) == CODE_FOR_jump_compact
  6076. && ((INSN_ADDRESSES
  6077. (INSN_UID (XEXP (SET_SRC (PATTERN (next)), 0)))
  6078. - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
  6079. > 252 + 258 + 2))
  6080. gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), 1);
  6081. }
  6082. else if (type == TYPE_JUMP || type == TYPE_RETURN)
  6083. {
  6084. int addr = INSN_ADDRESSES (INSN_UID (insn));
  6085. rtx_insn *far_label = 0;
  6086. int dest_uid = 0;
  6087. struct far_branch *bp;
  6088. if (type == TYPE_JUMP)
  6089. {
  6090. far_label = as_a <rtx_insn *> (
  6091. XEXP (SET_SRC (PATTERN (insn)), 0));
  6092. dest_uid = get_dest_uid (far_label, max_uid);
  6093. if (! dest_uid)
  6094. {
  6095. /* Parse errors can lead to labels outside
  6096. the insn stream. */
  6097. if (! NEXT_INSN (far_label))
  6098. continue;
  6099. if (! optimize)
  6100. {
  6101. JUMP_LABEL (insn) = far_label;
  6102. LABEL_NUSES (far_label)++;
  6103. }
  6104. redirect_jump (insn, ret_rtx, 1);
  6105. far_label = 0;
  6106. }
  6107. }
  6108. bp = uid_branch[dest_uid];
  6109. if (! bp)
  6110. {
  6111. bp = (struct far_branch *) alloca (sizeof *bp);
  6112. uid_branch[dest_uid] = bp;
  6113. bp->prev = far_branch_list;
  6114. far_branch_list = bp;
  6115. bp->near_label = 0;
  6116. bp->far_label = far_label;
  6117. if (far_label)
  6118. LABEL_NUSES (far_label)++;
  6119. }
  6120. else if (bp->near_label && ! NEXT_INSN (bp->near_label))
  6121. if (addr - bp->address <= CONDJUMP_MAX)
  6122. emit_label_after (bp->near_label, PREV_INSN (insn));
  6123. else
  6124. {
  6125. gen_far_branch (bp);
  6126. bp->near_label = 0;
  6127. }
  6128. else
  6129. bp->near_label = 0;
  6130. bp->address = addr;
  6131. bp->insert_place = insn;
  6132. if (! far_label)
  6133. emit_insn_before (gen_block_branch_redirect (const0_rtx), insn);
  6134. else
  6135. gen_block_redirect (insn, addr, bp->near_label ? 2 : 0);
  6136. }
  6137. }
  6138. /* Generate all pending far branches,
  6139. and free our references to the far labels. */
  6140. while (far_branch_list)
  6141. {
  6142. if (far_branch_list->near_label
  6143. && ! NEXT_INSN (far_branch_list->near_label))
  6144. gen_far_branch (far_branch_list);
  6145. if (optimize
  6146. && far_branch_list->far_label
  6147. && ! --LABEL_NUSES (far_branch_list->far_label))
  6148. delete_insn (far_branch_list->far_label);
  6149. far_branch_list = far_branch_list->prev;
  6150. }
  6151. /* Instruction length information is no longer valid due to the new
  6152. instructions that have been generated. */
  6153. init_insn_lengths ();
  6154. }
  6155. /* Dump out instruction addresses, which is useful for debugging the
  6156. constant pool table stuff.
  6157. If relaxing, output the label and pseudo-ops used to link together
  6158. calls and the instruction which set the registers.
  6159. ??? The addresses printed by this routine for insns are nonsense for
  6160. insns which are inside of a sequence where none of the inner insns have
  6161. variable length. This is because the second pass of shorten_branches
  6162. does not bother to update them. */
  6163. void
  6164. final_prescan_insn (rtx_insn *insn, rtx *opvec ATTRIBUTE_UNUSED,
  6165. int noperands ATTRIBUTE_UNUSED)
  6166. {
  6167. if (TARGET_DUMPISIZE)
  6168. fprintf (asm_out_file, "\n! at %04x\n", INSN_ADDRESSES (INSN_UID (insn)));
  6169. if (TARGET_RELAX)
  6170. {
  6171. rtx note;
  6172. note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX);
  6173. if (note)
  6174. {
  6175. rtx pattern;
  6176. pattern = PATTERN (insn);
  6177. if (GET_CODE (pattern) == PARALLEL)
  6178. pattern = XVECEXP (pattern, 0, 0);
  6179. switch (GET_CODE (pattern))
  6180. {
  6181. case SET:
  6182. if (GET_CODE (SET_SRC (pattern)) != CALL
  6183. && get_attr_type (insn) != TYPE_SFUNC)
  6184. {
  6185. targetm.asm_out.internal_label
  6186. (asm_out_file, "L", CODE_LABEL_NUMBER (XEXP (note, 0)));
  6187. break;
  6188. }
  6189. /* else FALLTHROUGH */
  6190. case CALL:
  6191. asm_fprintf (asm_out_file, "\t.uses %LL%d\n",
  6192. CODE_LABEL_NUMBER (XEXP (note, 0)));
  6193. break;
  6194. default:
  6195. gcc_unreachable ();
  6196. }
  6197. }
  6198. }
  6199. }
  6200. /* Dump out any constants accumulated in the final pass. These will
  6201. only be labels. */
  6202. const char *
  6203. output_jump_label_table (void)
  6204. {
  6205. int i;
  6206. if (pool_size)
  6207. {
  6208. fprintf (asm_out_file, "\t.align 2\n");
  6209. for (i = 0; i < pool_size; i++)
  6210. {
  6211. pool_node *p = &pool_vector[i];
  6212. (*targetm.asm_out.internal_label) (asm_out_file, "L",
  6213. CODE_LABEL_NUMBER (p->label));
  6214. output_asm_insn (".long %O0", &p->value);
  6215. }
  6216. pool_size = 0;
  6217. }
  6218. return "";
  6219. }
  6220. /* A full frame looks like:
  6221. arg-5
  6222. arg-4
  6223. [ if current_function_anonymous_args
  6224. arg-3
  6225. arg-2
  6226. arg-1
  6227. arg-0 ]
  6228. saved-fp
  6229. saved-r10
  6230. saved-r11
  6231. saved-r12
  6232. saved-pr
  6233. local-n
  6234. ..
  6235. local-1
  6236. local-0 <- fp points here.
  6237. Number of bytes pushed for anonymous args, used to pass information
  6238. between expand_prologue and expand_epilogue.
  6239. Adjust the stack by SIZE bytes. REG holds the rtl of the register to be
  6240. adjusted. If epilogue_p is zero, this is for a prologue; otherwise, it's
  6241. for an epilogue and a negative value means that it's for a sibcall
  6242. epilogue. If LIVE_REGS_MASK is nonzero, it points to a HARD_REG_SET of
  6243. all the registers that are about to be restored, and hence dead. */
  6244. static void
  6245. output_stack_adjust (int size, rtx reg, int epilogue_p,
  6246. HARD_REG_SET *live_regs_mask, bool frame_p)
  6247. {
  6248. rtx_insn *(*emit_fn) (rtx) = frame_p ? &frame_insn : &emit_insn;
  6249. if (size)
  6250. {
  6251. HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
  6252. /* This test is bogus, as output_stack_adjust is used to re-align the
  6253. stack. */
  6254. #if 0
  6255. gcc_assert (!(size % align));
  6256. #endif
  6257. if (CONST_OK_FOR_ADD (size))
  6258. emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size)));
  6259. /* Try to do it with two partial adjustments; however, we must make
  6260. sure that the stack is properly aligned at all times, in case
  6261. an interrupt occurs between the two partial adjustments. */
  6262. else if (CONST_OK_FOR_ADD (size / 2 & -align)
  6263. && CONST_OK_FOR_ADD (size - (size / 2 & -align)))
  6264. {
  6265. emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size / 2 & -align)));
  6266. emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size - (size / 2 & -align))));
  6267. }
  6268. else
  6269. {
  6270. rtx const_reg;
  6271. rtx insn;
  6272. int temp = epilogue_p ? 7 : (TARGET_SH5 ? 0 : 1);
  6273. int i;
  6274. /* If TEMP is invalid, we could temporarily save a general
  6275. register to MACL. However, there is currently no need
  6276. to handle this case, so just die when we see it. */
  6277. if (epilogue_p < 0
  6278. || current_function_interrupt
  6279. || ! call_really_used_regs[temp] || fixed_regs[temp])
  6280. temp = -1;
  6281. if (temp < 0 && ! current_function_interrupt
  6282. && (TARGET_SHMEDIA || epilogue_p >= 0))
  6283. {
  6284. HARD_REG_SET temps;
  6285. COPY_HARD_REG_SET (temps, call_used_reg_set);
  6286. AND_COMPL_HARD_REG_SET (temps, call_fixed_reg_set);
  6287. if (epilogue_p > 0)
  6288. {
  6289. int nreg = 0;
  6290. if (crtl->return_rtx)
  6291. {
  6292. machine_mode mode;
  6293. mode = GET_MODE (crtl->return_rtx);
  6294. if (BASE_RETURN_VALUE_REG (mode) == FIRST_RET_REG)
  6295. nreg = HARD_REGNO_NREGS (FIRST_RET_REG, mode);
  6296. }
  6297. for (i = 0; i < nreg; i++)
  6298. CLEAR_HARD_REG_BIT (temps, FIRST_RET_REG + i);
  6299. if (crtl->calls_eh_return)
  6300. {
  6301. CLEAR_HARD_REG_BIT (temps, EH_RETURN_STACKADJ_REGNO);
  6302. for (i = 0; i <= 3; i++)
  6303. CLEAR_HARD_REG_BIT (temps, EH_RETURN_DATA_REGNO (i));
  6304. }
  6305. }
  6306. if (TARGET_SHMEDIA && epilogue_p < 0)
  6307. for (i = FIRST_TARGET_REG; i <= LAST_TARGET_REG; i++)
  6308. CLEAR_HARD_REG_BIT (temps, i);
  6309. if (epilogue_p <= 0)
  6310. {
  6311. for (i = FIRST_PARM_REG;
  6312. i < FIRST_PARM_REG + NPARM_REGS (SImode); i++)
  6313. CLEAR_HARD_REG_BIT (temps, i);
  6314. if (cfun->static_chain_decl != NULL)
  6315. CLEAR_HARD_REG_BIT (temps, STATIC_CHAIN_REGNUM);
  6316. }
  6317. temp = scavenge_reg (&temps);
  6318. }
  6319. if (temp < 0 && live_regs_mask)
  6320. {
  6321. HARD_REG_SET temps;
  6322. COPY_HARD_REG_SET (temps, *live_regs_mask);
  6323. CLEAR_HARD_REG_BIT (temps, REGNO (reg));
  6324. temp = scavenge_reg (&temps);
  6325. }
  6326. if (temp < 0)
  6327. {
  6328. rtx adj_reg, tmp_reg, mem;
  6329. /* If we reached here, the most likely case is the (sibcall)
  6330. epilogue for non SHmedia. Put a special push/pop sequence
  6331. for such case as the last resort. This looks lengthy but
  6332. would not be problem because it seems to be very
  6333. rare. */
  6334. gcc_assert (!TARGET_SHMEDIA && epilogue_p);
  6335. /* ??? There is still the slight possibility that r4 or
  6336. r5 have been reserved as fixed registers or assigned
  6337. as global registers, and they change during an
  6338. interrupt. There are possible ways to handle this:
  6339. - If we are adjusting the frame pointer (r14), we can do
  6340. with a single temp register and an ordinary push / pop
  6341. on the stack.
  6342. - Grab any call-used or call-saved registers (i.e. not
  6343. fixed or globals) for the temps we need. We might
  6344. also grab r14 if we are adjusting the stack pointer.
  6345. If we can't find enough available registers, issue
  6346. a diagnostic and die - the user must have reserved
  6347. way too many registers.
  6348. But since all this is rather unlikely to happen and
  6349. would require extra testing, we just die if r4 / r5
  6350. are not available. */
  6351. gcc_assert (!fixed_regs[4] && !fixed_regs[5]
  6352. && !global_regs[4] && !global_regs[5]);
  6353. adj_reg = gen_rtx_REG (GET_MODE (reg), 4);
  6354. tmp_reg = gen_rtx_REG (GET_MODE (reg), 5);
  6355. emit_move_insn (gen_tmp_stack_mem (Pmode, reg), adj_reg);
  6356. emit_insn (GEN_MOV (adj_reg, GEN_INT (size)));
  6357. emit_insn (GEN_ADD3 (adj_reg, adj_reg, reg));
  6358. mem = gen_tmp_stack_mem (Pmode, gen_rtx_PRE_DEC (Pmode, adj_reg));
  6359. emit_move_insn (mem, tmp_reg);
  6360. emit_move_insn (tmp_reg, gen_tmp_stack_mem (Pmode, reg));
  6361. mem = gen_tmp_stack_mem (Pmode, gen_rtx_PRE_DEC (Pmode, adj_reg));
  6362. emit_move_insn (mem, tmp_reg);
  6363. emit_move_insn (reg, adj_reg);
  6364. mem = gen_tmp_stack_mem (Pmode, gen_rtx_POST_INC (Pmode, reg));
  6365. emit_move_insn (adj_reg, mem);
  6366. mem = gen_tmp_stack_mem (Pmode, gen_rtx_POST_INC (Pmode, reg));
  6367. emit_move_insn (tmp_reg, mem);
  6368. /* Tell flow the insns that pop r4/r5 aren't dead. */
  6369. emit_use (tmp_reg);
  6370. emit_use (adj_reg);
  6371. return;
  6372. }
  6373. const_reg = gen_rtx_REG (GET_MODE (reg), temp);
  6374. /* If SIZE is negative, subtract the positive value.
  6375. This sometimes allows a constant pool entry to be shared
  6376. between prologue and epilogue code. */
  6377. if (size < 0)
  6378. {
  6379. emit_insn (GEN_MOV (const_reg, GEN_INT (-size)));
  6380. insn = emit_fn (GEN_SUB3 (reg, reg, const_reg));
  6381. }
  6382. else
  6383. {
  6384. emit_insn (GEN_MOV (const_reg, GEN_INT (size)));
  6385. insn = emit_fn (GEN_ADD3 (reg, reg, const_reg));
  6386. }
  6387. add_reg_note (insn, REG_FRAME_RELATED_EXPR,
  6388. gen_rtx_SET (VOIDmode, reg,
  6389. gen_rtx_PLUS (SImode, reg,
  6390. GEN_INT (size))));
  6391. }
  6392. }
  6393. }
  6394. /* Emit the specified insn and mark it as frame related.
  6395. FIXME: Rename this to emit_frame_insn. */
  6396. static rtx_insn *
  6397. frame_insn (rtx x)
  6398. {
  6399. rtx_insn *insn = emit_insn (x);
  6400. RTX_FRAME_RELATED_P (insn) = 1;
  6401. return insn;
  6402. }
  6403. /* Output RTL to push register RN onto the stack. */
  6404. static rtx
  6405. push (int rn)
  6406. {
  6407. rtx x;
  6408. if (rn == FPUL_REG)
  6409. x = gen_push_fpul ();
  6410. else if (rn == FPSCR_REG)
  6411. x = gen_push_fpscr ();
  6412. else if ((TARGET_SH4 || TARGET_SH2A_DOUBLE) && TARGET_FMOVD
  6413. && ! TARGET_FPU_SINGLE && FP_OR_XD_REGISTER_P (rn))
  6414. {
  6415. if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
  6416. return NULL_RTX;
  6417. x = gen_push_4 (gen_rtx_REG (DFmode, rn));
  6418. }
  6419. else if (TARGET_SH2E && FP_REGISTER_P (rn))
  6420. x = gen_push_e (gen_rtx_REG (SFmode, rn));
  6421. else
  6422. x = gen_push (gen_rtx_REG (SImode, rn));
  6423. x = frame_insn (x);
  6424. add_reg_note (x, REG_INC, gen_rtx_REG (SImode, STACK_POINTER_REGNUM));
  6425. return x;
  6426. }
  6427. /* Output RTL to pop register RN from the stack. */
  6428. static void
  6429. pop (int rn)
  6430. {
  6431. rtx x, sp_reg, reg;
  6432. if (rn == FPUL_REG)
  6433. x = gen_pop_fpul ();
  6434. else if (rn == FPSCR_REG)
  6435. x = gen_pop_fpscr ();
  6436. else if ((TARGET_SH4 || TARGET_SH2A_DOUBLE) && TARGET_FMOVD
  6437. && ! TARGET_FPU_SINGLE && FP_OR_XD_REGISTER_P (rn))
  6438. {
  6439. if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
  6440. return;
  6441. x = gen_pop_4 (gen_rtx_REG (DFmode, rn));
  6442. }
  6443. else if (TARGET_SH2E && FP_REGISTER_P (rn))
  6444. x = gen_pop_e (gen_rtx_REG (SFmode, rn));
  6445. else
  6446. x = gen_pop (gen_rtx_REG (SImode, rn));
  6447. x = emit_insn (x);
  6448. sp_reg = gen_rtx_REG (SImode, STACK_POINTER_REGNUM);
  6449. reg = copy_rtx (GET_CODE (PATTERN (x)) == PARALLEL
  6450. ? SET_DEST (XVECEXP (PATTERN (x), 0, 0))
  6451. : SET_DEST (PATTERN (x)));
  6452. add_reg_note (x, REG_CFA_RESTORE, reg);
  6453. add_reg_note (x, REG_CFA_ADJUST_CFA,
  6454. gen_rtx_SET (SImode, sp_reg,
  6455. plus_constant (SImode, sp_reg,
  6456. GET_MODE_SIZE (GET_MODE (reg)))));
  6457. add_reg_note (x, REG_INC, gen_rtx_REG (SImode, STACK_POINTER_REGNUM));
  6458. RTX_FRAME_RELATED_P (x) = 1;
  6459. }
  6460. /* Generate code to push the regs specified in the mask. */
  6461. static void
  6462. push_regs (HARD_REG_SET *mask, int interrupt_handler)
  6463. {
  6464. int i = interrupt_handler ? LAST_BANKED_REG + 1 : 0;
  6465. int skip_fpscr = 0;
  6466. /* Push PR last; this gives better latencies after the prologue, and
  6467. candidates for the return delay slot when there are no general
  6468. registers pushed. */
  6469. for (; i < FIRST_PSEUDO_REGISTER; i++)
  6470. {
  6471. /* If this is an interrupt handler, and the SZ bit varies,
  6472. and we have to push any floating point register, we need
  6473. to switch to the correct precision first. */
  6474. if (i == FIRST_FP_REG && interrupt_handler && TARGET_FMOVD
  6475. && hard_reg_set_intersect_p (*mask, reg_class_contents[DF_REGS]))
  6476. {
  6477. HARD_REG_SET unsaved;
  6478. push (FPSCR_REG);
  6479. COMPL_HARD_REG_SET (unsaved, *mask);
  6480. fpscr_set_from_mem (NORMAL_MODE (FP_MODE), unsaved);
  6481. skip_fpscr = 1;
  6482. }
  6483. if (i != PR_REG
  6484. && (i != FPSCR_REG || ! skip_fpscr)
  6485. && TEST_HARD_REG_BIT (*mask, i))
  6486. {
  6487. /* If the ISR has RESBANK attribute assigned, don't push any of
  6488. the following registers - R0-R14, MACH, MACL and GBR. */
  6489. if (! (sh_cfun_resbank_handler_p ()
  6490. && ((i >= FIRST_GENERAL_REG && i < LAST_GENERAL_REG)
  6491. || i == MACH_REG
  6492. || i == MACL_REG
  6493. || i == GBR_REG)))
  6494. push (i);
  6495. }
  6496. }
  6497. /* Push banked registers last to improve delay slot opportunities. */
  6498. if (interrupt_handler)
  6499. {
  6500. bool use_movml = false;
  6501. if (TARGET_SH2A)
  6502. {
  6503. unsigned int count = 0;
  6504. for (i = FIRST_BANKED_REG; i <= LAST_BANKED_REG; i++)
  6505. if (TEST_HARD_REG_BIT (*mask, i))
  6506. count++;
  6507. else
  6508. break;
  6509. /* Use movml when all banked registers are pushed. */
  6510. if (count == LAST_BANKED_REG - FIRST_BANKED_REG + 1)
  6511. use_movml = true;
  6512. }
  6513. if (sh_cfun_resbank_handler_p ())
  6514. ; /* Do nothing. */
  6515. else if (use_movml)
  6516. {
  6517. rtx x, mem, reg, set;
  6518. rtx sp_reg = gen_rtx_REG (SImode, STACK_POINTER_REGNUM);
  6519. /* We must avoid scheduling multiple store insn with another
  6520. insns. */
  6521. emit_insn (gen_blockage ());
  6522. x = gen_movml_push_banked (sp_reg);
  6523. x = frame_insn (x);
  6524. for (i = FIRST_BANKED_REG; i <= LAST_BANKED_REG; i++)
  6525. {
  6526. mem = gen_rtx_MEM (SImode, plus_constant (Pmode, sp_reg, i * 4));
  6527. reg = gen_rtx_REG (SImode, i);
  6528. add_reg_note (x, REG_CFA_OFFSET, gen_rtx_SET (SImode, mem, reg));
  6529. }
  6530. set = gen_rtx_SET (SImode, sp_reg,
  6531. plus_constant (Pmode, sp_reg, - 32));
  6532. add_reg_note (x, REG_CFA_ADJUST_CFA, set);
  6533. emit_insn (gen_blockage ());
  6534. }
  6535. else
  6536. for (i = FIRST_BANKED_REG; i <= LAST_BANKED_REG; i++)
  6537. if (TEST_HARD_REG_BIT (*mask, i))
  6538. push (i);
  6539. }
  6540. /* Don't push PR register for an ISR with RESBANK attribute assigned. */
  6541. if (TEST_HARD_REG_BIT (*mask, PR_REG) && !sh_cfun_resbank_handler_p ())
  6542. push (PR_REG);
  6543. }
  6544. /* Calculate how much extra space is needed to save all callee-saved
  6545. target registers.
  6546. LIVE_REGS_MASK is the register mask calculated by calc_live_regs. */
  6547. static int
  6548. shmedia_target_regs_stack_space (HARD_REG_SET *live_regs_mask)
  6549. {
  6550. int reg;
  6551. int stack_space = 0;
  6552. int interrupt_handler = sh_cfun_interrupt_handler_p ();
  6553. for (reg = LAST_TARGET_REG; reg >= FIRST_TARGET_REG; reg--)
  6554. if ((! call_really_used_regs[reg] || interrupt_handler)
  6555. && ! TEST_HARD_REG_BIT (*live_regs_mask, reg))
  6556. /* Leave space to save this target register on the stack,
  6557. in case target register allocation wants to use it. */
  6558. stack_space += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
  6559. return stack_space;
  6560. }
  6561. /* Decide whether we should reserve space for callee-save target registers,
  6562. in case target register allocation wants to use them. REGS_SAVED is
  6563. the space, in bytes, that is already required for register saves.
  6564. LIVE_REGS_MASK is the register mask calculated by calc_live_regs. */
  6565. static int
  6566. shmedia_reserve_space_for_target_registers_p (int regs_saved,
  6567. HARD_REG_SET *live_regs_mask)
  6568. {
  6569. if (optimize_size)
  6570. return 0;
  6571. return shmedia_target_regs_stack_space (live_regs_mask) <= regs_saved;
  6572. }
  6573. /* Decide how much space to reserve for callee-save target registers
  6574. in case target register allocation wants to use them.
  6575. LIVE_REGS_MASK is the register mask calculated by calc_live_regs. */
  6576. static int
  6577. shmedia_target_regs_stack_adjust (HARD_REG_SET *live_regs_mask)
  6578. {
  6579. if (shmedia_space_reserved_for_target_registers)
  6580. return shmedia_target_regs_stack_space (live_regs_mask);
  6581. else
  6582. return 0;
  6583. }
  6584. /* Work out the registers which need to be saved, both as a mask and a
  6585. count of saved words. Return the count.
  6586. If doing a pragma interrupt function, then push all regs used by the
  6587. function, and if we call another function (we can tell by looking at PR),
  6588. make sure that all the regs it clobbers are safe too. */
  6589. static int
  6590. calc_live_regs (HARD_REG_SET *live_regs_mask)
  6591. {
  6592. unsigned int reg;
  6593. int count;
  6594. tree attrs;
  6595. bool interrupt_or_trapa_handler, trapa_handler, interrupt_handler;
  6596. bool nosave_low_regs;
  6597. int pr_live, has_call;
  6598. attrs = DECL_ATTRIBUTES (current_function_decl);
  6599. interrupt_or_trapa_handler = sh_cfun_interrupt_handler_p ();
  6600. trapa_handler = lookup_attribute ("trapa_handler", attrs) != NULL_TREE;
  6601. interrupt_handler = interrupt_or_trapa_handler && ! trapa_handler;
  6602. nosave_low_regs = lookup_attribute ("nosave_low_regs", attrs) != NULL_TREE;
  6603. CLEAR_HARD_REG_SET (*live_regs_mask);
  6604. if ((TARGET_SH4 || TARGET_SH2A_DOUBLE) && TARGET_FMOVD && interrupt_handler
  6605. && df_regs_ever_live_p (FPSCR_REG))
  6606. target_flags &= ~MASK_FPU_SINGLE;
  6607. /* If we can save a lot of saves by switching to double mode, do that. */
  6608. else if ((TARGET_SH4 || TARGET_SH2A_DOUBLE) && TARGET_FMOVD
  6609. && TARGET_FPU_SINGLE)
  6610. for (count = 0, reg = FIRST_FP_REG; reg <= LAST_FP_REG; reg += 2)
  6611. if (df_regs_ever_live_p (reg) && df_regs_ever_live_p (reg+1)
  6612. && (! call_really_used_regs[reg]
  6613. || interrupt_handler)
  6614. && ++count > 2)
  6615. {
  6616. target_flags &= ~MASK_FPU_SINGLE;
  6617. break;
  6618. }
  6619. /* PR_MEDIA_REG is a general purpose register, thus global_alloc already
  6620. knows how to use it. That means the pseudo originally allocated for
  6621. the initial value can become the PR_MEDIA_REG hard register, as seen for
  6622. execute/20010122-1.c:test9. */
  6623. if (TARGET_SHMEDIA)
  6624. /* ??? this function is called from initial_elimination_offset, hence we
  6625. can't use the result of sh_media_register_for_return here. */
  6626. pr_live = sh_pr_n_sets ();
  6627. else
  6628. {
  6629. rtx pr_initial = has_hard_reg_initial_val (Pmode, PR_REG);
  6630. pr_live = (pr_initial
  6631. ? (!REG_P (pr_initial)
  6632. || REGNO (pr_initial) != (PR_REG))
  6633. : df_regs_ever_live_p (PR_REG));
  6634. /* For Shcompact, if not optimizing, we end up with a memory reference
  6635. using the return address pointer for __builtin_return_address even
  6636. though there is no actual need to put the PR register on the stack. */
  6637. pr_live |= df_regs_ever_live_p (RETURN_ADDRESS_POINTER_REGNUM);
  6638. }
  6639. /* Force PR to be live if the prologue has to call the SHmedia
  6640. argument decoder or register saver. */
  6641. if (TARGET_SHCOMPACT
  6642. && ((crtl->args.info.call_cookie
  6643. & ~ CALL_COOKIE_RET_TRAMP (1))
  6644. || crtl->saves_all_registers))
  6645. pr_live = 1;
  6646. has_call = TARGET_SHMEDIA ? ! leaf_function_p () : pr_live;
  6647. for (count = 0, reg = FIRST_PSEUDO_REGISTER; reg-- != 0; )
  6648. {
  6649. if (reg == (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG)
  6650. ? pr_live
  6651. : interrupt_handler
  6652. ? (/* Need to save all the regs ever live. */
  6653. (df_regs_ever_live_p (reg)
  6654. || (call_really_used_regs[reg]
  6655. && (! fixed_regs[reg] || reg == MACH_REG || reg == MACL_REG
  6656. || reg == PIC_OFFSET_TABLE_REGNUM)
  6657. && has_call)
  6658. || (TARGET_SHMEDIA && has_call
  6659. && REGISTER_NATURAL_MODE (reg) == SImode
  6660. && (GENERAL_REGISTER_P (reg) || TARGET_REGISTER_P (reg))))
  6661. && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
  6662. && reg != RETURN_ADDRESS_POINTER_REGNUM
  6663. && reg != T_REG && reg != GBR_REG
  6664. && reg != FPSCR_MODES_REG && reg != FPSCR_STAT_REG
  6665. /* Push fpscr only on targets which have FPU */
  6666. && (reg != FPSCR_REG || TARGET_FPU_ANY))
  6667. : (/* Only push those regs which are used and need to be saved. */
  6668. (TARGET_SHCOMPACT
  6669. && flag_pic
  6670. && crtl->args.info.call_cookie
  6671. && reg == PIC_OFFSET_TABLE_REGNUM)
  6672. || (df_regs_ever_live_p (reg)
  6673. && ((!call_really_used_regs[reg]
  6674. && !(reg != PIC_OFFSET_TABLE_REGNUM
  6675. && fixed_regs[reg] && call_used_regs[reg]))
  6676. || (trapa_handler && reg == FPSCR_REG && TARGET_FPU_ANY)))
  6677. || (crtl->calls_eh_return
  6678. && (reg == EH_RETURN_DATA_REGNO (0)
  6679. || reg == EH_RETURN_DATA_REGNO (1)
  6680. || reg == EH_RETURN_DATA_REGNO (2)
  6681. || reg == EH_RETURN_DATA_REGNO (3)))
  6682. || ((reg == MACL_REG || reg == MACH_REG)
  6683. && df_regs_ever_live_p (reg)
  6684. && sh_cfun_attr_renesas_p ())
  6685. ))
  6686. {
  6687. SET_HARD_REG_BIT (*live_regs_mask, reg);
  6688. count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
  6689. if ((TARGET_SH4 || TARGET_SH2A_DOUBLE || TARGET_SH5) && TARGET_FMOVD
  6690. && GET_MODE_CLASS (REGISTER_NATURAL_MODE (reg)) == MODE_FLOAT)
  6691. {
  6692. if (FP_REGISTER_P (reg))
  6693. {
  6694. if (! TARGET_FPU_SINGLE && ! df_regs_ever_live_p (reg ^ 1))
  6695. {
  6696. SET_HARD_REG_BIT (*live_regs_mask, (reg ^ 1));
  6697. count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg ^ 1));
  6698. }
  6699. }
  6700. else if (XD_REGISTER_P (reg))
  6701. {
  6702. /* Must switch to double mode to access these registers. */
  6703. target_flags &= ~MASK_FPU_SINGLE;
  6704. }
  6705. }
  6706. }
  6707. if (nosave_low_regs && reg == R8_REG)
  6708. break;
  6709. }
  6710. /* If we have a target register optimization pass after prologue / epilogue
  6711. threading, we need to assume all target registers will be live even if
  6712. they aren't now. */
  6713. if (flag_branch_target_load_optimize2
  6714. && TARGET_SAVE_ALL_TARGET_REGS
  6715. && shmedia_space_reserved_for_target_registers)
  6716. for (reg = LAST_TARGET_REG; reg >= FIRST_TARGET_REG; reg--)
  6717. if ((! call_really_used_regs[reg] || interrupt_handler)
  6718. && ! TEST_HARD_REG_BIT (*live_regs_mask, reg))
  6719. {
  6720. SET_HARD_REG_BIT (*live_regs_mask, reg);
  6721. count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
  6722. }
  6723. /* If this is an interrupt handler, we don't have any call-clobbered
  6724. registers we can conveniently use for target register save/restore.
  6725. Make sure we save at least one general purpose register when we need
  6726. to save target registers. */
  6727. if (interrupt_handler
  6728. && hard_reg_set_intersect_p (*live_regs_mask,
  6729. reg_class_contents[TARGET_REGS])
  6730. && ! hard_reg_set_intersect_p (*live_regs_mask,
  6731. reg_class_contents[GENERAL_REGS]))
  6732. {
  6733. SET_HARD_REG_BIT (*live_regs_mask, R0_REG);
  6734. count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (R0_REG));
  6735. }
  6736. return count;
  6737. }
  6738. /* Code to generate prologue and epilogue sequences */
  6739. /* PUSHED is the number of bytes that are being pushed on the
  6740. stack for register saves. Return the frame size, padded
  6741. appropriately so that the stack stays properly aligned. */
  6742. static HOST_WIDE_INT
  6743. rounded_frame_size (int pushed)
  6744. {
  6745. HOST_WIDE_INT size = get_frame_size ();
  6746. HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
  6747. if (ACCUMULATE_OUTGOING_ARGS)
  6748. size += crtl->outgoing_args_size;
  6749. return ((size + pushed + align - 1) & -align) - pushed;
  6750. }
  6751. /* Choose a call-clobbered target-branch register that remains
  6752. unchanged along the whole function. We set it up as the return
  6753. value in the prologue. */
  6754. int
  6755. sh_media_register_for_return (void)
  6756. {
  6757. int regno;
  6758. int tr0_used;
  6759. if (! crtl->is_leaf)
  6760. return -1;
  6761. if (lookup_attribute ("interrupt_handler",
  6762. DECL_ATTRIBUTES (current_function_decl)))
  6763. return -1;
  6764. if (sh_cfun_interrupt_handler_p ())
  6765. return -1;
  6766. tr0_used = flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM);
  6767. for (regno = FIRST_TARGET_REG + tr0_used; regno <= LAST_TARGET_REG; regno++)
  6768. if (call_really_used_regs[regno] && ! df_regs_ever_live_p (regno))
  6769. return regno;
  6770. return -1;
  6771. }
  6772. /* The maximum registers we need to save are:
  6773. - 62 general purpose registers (r15 is stack pointer, r63 is zero)
  6774. - 32 floating point registers (for each pair, we save none,
  6775. one single precision value, or a double precision value).
  6776. - 8 target registers
  6777. - add 1 entry for a delimiter. */
  6778. #define MAX_SAVED_REGS (62+32+8)
  6779. typedef struct save_entry_s
  6780. {
  6781. unsigned char reg;
  6782. unsigned char mode;
  6783. short offset;
  6784. } save_entry;
  6785. #define MAX_TEMPS 4
  6786. /* There will be a delimiter entry with VOIDmode both at the start and the
  6787. end of a filled in schedule. The end delimiter has the offset of the
  6788. save with the smallest (i.e. most negative) offset. */
  6789. typedef struct save_schedule_s
  6790. {
  6791. save_entry entries[MAX_SAVED_REGS + 2];
  6792. int temps[MAX_TEMPS+1];
  6793. } save_schedule;
  6794. /* Fill in SCHEDULE according to LIVE_REGS_MASK. If RESTORE is nonzero,
  6795. use reverse order. Returns the last entry written to (not counting
  6796. the delimiter). OFFSET_BASE is a number to be added to all offset
  6797. entries. */
  6798. static save_entry *
  6799. sh5_schedule_saves (HARD_REG_SET *live_regs_mask, save_schedule *schedule,
  6800. int offset_base)
  6801. {
  6802. int align, i;
  6803. save_entry *entry = schedule->entries;
  6804. int tmpx = 0;
  6805. int offset;
  6806. if (! current_function_interrupt)
  6807. for (i = FIRST_GENERAL_REG; tmpx < MAX_TEMPS && i <= LAST_GENERAL_REG; i++)
  6808. if (call_really_used_regs[i] && ! fixed_regs[i] && i != PR_MEDIA_REG
  6809. && ! FUNCTION_ARG_REGNO_P (i)
  6810. && i != FIRST_RET_REG
  6811. && ! (cfun->static_chain_decl != NULL && i == STATIC_CHAIN_REGNUM)
  6812. && ! (crtl->calls_eh_return
  6813. && (i == EH_RETURN_STACKADJ_REGNO
  6814. || ((unsigned) i >= EH_RETURN_DATA_REGNO (0)
  6815. && (unsigned) i <= EH_RETURN_DATA_REGNO (3)))))
  6816. schedule->temps[tmpx++] = i;
  6817. entry->reg = -1;
  6818. entry->mode = VOIDmode;
  6819. entry->offset = offset_base;
  6820. entry++;
  6821. /* We loop twice: first, we save 8-byte aligned registers in the
  6822. higher addresses, that are known to be aligned. Then, we
  6823. proceed to saving 32-bit registers that don't need 8-byte
  6824. alignment.
  6825. If this is an interrupt function, all registers that need saving
  6826. need to be saved in full. moreover, we need to postpone saving
  6827. target registers till we have saved some general purpose registers
  6828. we can then use as scratch registers. */
  6829. offset = offset_base;
  6830. for (align = 1; align >= 0; align--)
  6831. {
  6832. for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
  6833. if (TEST_HARD_REG_BIT (*live_regs_mask, i))
  6834. {
  6835. machine_mode mode = REGISTER_NATURAL_MODE (i);
  6836. int reg = i;
  6837. if (current_function_interrupt)
  6838. {
  6839. if (TARGET_REGISTER_P (i))
  6840. continue;
  6841. if (GENERAL_REGISTER_P (i))
  6842. mode = DImode;
  6843. }
  6844. if (mode == SFmode && (i % 2) == 1
  6845. && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
  6846. && (TEST_HARD_REG_BIT (*live_regs_mask, (i ^ 1))))
  6847. {
  6848. mode = DFmode;
  6849. i--;
  6850. reg--;
  6851. }
  6852. /* If we're doing the aligned pass and this is not aligned,
  6853. or we're doing the unaligned pass and this is aligned,
  6854. skip it. */
  6855. if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT) == 0)
  6856. != align)
  6857. continue;
  6858. if (current_function_interrupt
  6859. && GENERAL_REGISTER_P (i)
  6860. && tmpx < MAX_TEMPS)
  6861. schedule->temps[tmpx++] = i;
  6862. offset -= GET_MODE_SIZE (mode);
  6863. entry->reg = i;
  6864. entry->mode = mode;
  6865. entry->offset = offset;
  6866. entry++;
  6867. }
  6868. if (align && current_function_interrupt)
  6869. for (i = LAST_TARGET_REG; i >= FIRST_TARGET_REG; i--)
  6870. if (TEST_HARD_REG_BIT (*live_regs_mask, i))
  6871. {
  6872. offset -= GET_MODE_SIZE (DImode);
  6873. entry->reg = i;
  6874. entry->mode = DImode;
  6875. entry->offset = offset;
  6876. entry++;
  6877. }
  6878. }
  6879. entry->reg = -1;
  6880. entry->mode = VOIDmode;
  6881. entry->offset = offset;
  6882. schedule->temps[tmpx] = -1;
  6883. return entry - 1;
  6884. }
  6885. /* Expand code for the function prologue. */
  6886. void
  6887. sh_expand_prologue (void)
  6888. {
  6889. HARD_REG_SET live_regs_mask;
  6890. int d, i;
  6891. int d_rounding = 0;
  6892. int save_flags = target_flags;
  6893. int pretend_args;
  6894. int stack_usage;
  6895. tree sp_switch_attr
  6896. = lookup_attribute ("sp_switch", DECL_ATTRIBUTES (current_function_decl));
  6897. current_function_interrupt = sh_cfun_interrupt_handler_p ();
  6898. /* We have pretend args if we had an object sent partially in registers
  6899. and partially on the stack, e.g. a large structure. */
  6900. pretend_args = crtl->args.pretend_args_size;
  6901. if (TARGET_VARARGS_PRETEND_ARGS (current_function_decl)
  6902. && (NPARM_REGS(SImode)
  6903. > crtl->args.info.arg_count[(int) SH_ARG_INT]))
  6904. pretend_args = 0;
  6905. output_stack_adjust (-pretend_args
  6906. - crtl->args.info.stack_regs * 8,
  6907. stack_pointer_rtx, 0, NULL, true);
  6908. stack_usage = pretend_args + crtl->args.info.stack_regs * 8;
  6909. if (TARGET_SHCOMPACT && flag_pic && crtl->args.info.call_cookie)
  6910. /* We're going to use the PIC register to load the address of the
  6911. incoming-argument decoder and/or of the return trampoline from
  6912. the GOT, so make sure the PIC register is preserved and
  6913. initialized. */
  6914. df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
  6915. if (TARGET_SHCOMPACT
  6916. && (crtl->args.info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
  6917. {
  6918. int reg;
  6919. /* First, make all registers with incoming arguments that will
  6920. be pushed onto the stack live, so that register renaming
  6921. doesn't overwrite them. */
  6922. for (reg = 0; reg < NPARM_REGS (SImode); reg++)
  6923. if (CALL_COOKIE_STACKSEQ_GET (crtl->args.info.call_cookie)
  6924. >= NPARM_REGS (SImode) - reg)
  6925. for (; reg < NPARM_REGS (SImode); reg++)
  6926. emit_insn (gen_shcompact_preserve_incoming_args
  6927. (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
  6928. else if (CALL_COOKIE_INT_REG_GET
  6929. (crtl->args.info.call_cookie, reg) == 1)
  6930. emit_insn (gen_shcompact_preserve_incoming_args
  6931. (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
  6932. emit_move_insn (gen_rtx_REG (Pmode, MACL_REG),
  6933. stack_pointer_rtx);
  6934. emit_move_insn (gen_rtx_REG (SImode, R0_REG),
  6935. GEN_INT (crtl->args.info.call_cookie));
  6936. emit_move_insn (gen_rtx_REG (SImode, MACH_REG),
  6937. gen_rtx_REG (SImode, R0_REG));
  6938. }
  6939. else if (TARGET_SHMEDIA)
  6940. {
  6941. int tr = sh_media_register_for_return ();
  6942. if (tr >= 0)
  6943. emit_move_insn (gen_rtx_REG (DImode, tr),
  6944. gen_rtx_REG (DImode, PR_MEDIA_REG));
  6945. }
  6946. /* Emit the code for SETUP_VARARGS. */
  6947. if (cfun->stdarg)
  6948. {
  6949. if (TARGET_VARARGS_PRETEND_ARGS (current_function_decl))
  6950. {
  6951. /* Push arg regs as if they'd been provided by caller in stack. */
  6952. for (i = 0; i < NPARM_REGS(SImode); i++)
  6953. {
  6954. int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
  6955. if (i >= (NPARM_REGS(SImode)
  6956. - crtl->args.info.arg_count[(int) SH_ARG_INT]
  6957. ))
  6958. break;
  6959. push (rn);
  6960. stack_usage += GET_MODE_SIZE (SImode);
  6961. }
  6962. }
  6963. }
  6964. /* If we're supposed to switch stacks at function entry, do so now. */
  6965. if (sp_switch_attr)
  6966. {
  6967. rtx lab, newsrc;
  6968. /* The argument specifies a variable holding the address of the
  6969. stack the interrupt function should switch to/from at entry/exit. */
  6970. tree arg = TREE_VALUE ( TREE_VALUE (sp_switch_attr));
  6971. const char *s
  6972. = ggc_strdup (TREE_STRING_POINTER (arg));
  6973. rtx sp_switch = gen_rtx_SYMBOL_REF (Pmode, s);
  6974. lab = add_constant (sp_switch, SImode, 0);
  6975. newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
  6976. emit_insn (gen_sp_switch_1 (newsrc));
  6977. }
  6978. d = calc_live_regs (&live_regs_mask);
  6979. /* ??? Maybe we could save some switching if we can move a mode switch
  6980. that already happens to be at the function start into the prologue. */
  6981. if (target_flags != save_flags && ! current_function_interrupt)
  6982. emit_insn (gen_toggle_sz ());
  6983. if (TARGET_SH5)
  6984. {
  6985. int offset_base, offset;
  6986. rtx r0 = NULL_RTX;
  6987. int offset_in_r0 = -1;
  6988. int sp_in_r0 = 0;
  6989. int tregs_space = shmedia_target_regs_stack_adjust (&live_regs_mask);
  6990. int total_size, save_size;
  6991. save_schedule schedule;
  6992. save_entry *entry;
  6993. int *tmp_pnt;
  6994. if (call_really_used_regs[R0_REG] && ! fixed_regs[R0_REG]
  6995. && ! current_function_interrupt)
  6996. r0 = gen_rtx_REG (Pmode, R0_REG);
  6997. /* D is the actual number of bytes that we need for saving registers,
  6998. however, in initial_elimination_offset we have committed to using
  6999. an additional TREGS_SPACE amount of bytes - in order to keep both
  7000. addresses to arguments supplied by the caller and local variables
  7001. valid, we must keep this gap. Place it between the incoming
  7002. arguments and the actually saved registers in a bid to optimize
  7003. locality of reference. */
  7004. total_size = d + tregs_space;
  7005. total_size += rounded_frame_size (total_size);
  7006. save_size = total_size - rounded_frame_size (d);
  7007. if (save_size % (STACK_BOUNDARY / BITS_PER_UNIT))
  7008. d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
  7009. - save_size % (STACK_BOUNDARY / BITS_PER_UNIT));
  7010. /* If adjusting the stack in a single step costs nothing extra, do so.
  7011. I.e. either if a single addi is enough, or we need a movi anyway,
  7012. and we don't exceed the maximum offset range (the test for the
  7013. latter is conservative for simplicity). */
  7014. if (TARGET_SHMEDIA
  7015. && (CONST_OK_FOR_I10 (-total_size)
  7016. || (! CONST_OK_FOR_I10 (-(save_size + d_rounding))
  7017. && total_size <= 2044)))
  7018. d_rounding = total_size - save_size;
  7019. offset_base = d + d_rounding;
  7020. output_stack_adjust (-(save_size + d_rounding), stack_pointer_rtx,
  7021. 0, NULL, true);
  7022. stack_usage += save_size + d_rounding;
  7023. sh5_schedule_saves (&live_regs_mask, &schedule, offset_base);
  7024. tmp_pnt = schedule.temps;
  7025. for (entry = &schedule.entries[1]; entry->mode != VOIDmode; entry++)
  7026. {
  7027. machine_mode mode = (machine_mode) entry->mode;
  7028. unsigned int reg = entry->reg;
  7029. rtx reg_rtx, mem_rtx, pre_dec = NULL_RTX;
  7030. rtx orig_reg_rtx;
  7031. offset = entry->offset;
  7032. reg_rtx = gen_rtx_REG (mode, reg);
  7033. mem_rtx = gen_frame_mem (mode,
  7034. gen_rtx_PLUS (Pmode,
  7035. stack_pointer_rtx,
  7036. GEN_INT (offset)));
  7037. if (!memory_address_p (mode, XEXP (mem_rtx, 0)))
  7038. {
  7039. gcc_assert (r0);
  7040. mem_rtx = NULL_RTX;
  7041. }
  7042. if (HAVE_PRE_DECREMENT
  7043. && (offset_in_r0 - offset == GET_MODE_SIZE (mode)
  7044. || mem_rtx == NULL_RTX
  7045. || reg == PR_REG || SPECIAL_REGISTER_P (reg)))
  7046. {
  7047. pre_dec = gen_frame_mem (mode, gen_rtx_PRE_DEC (Pmode, r0));
  7048. if (!memory_address_p (mode, XEXP (pre_dec, 0)))
  7049. pre_dec = NULL_RTX;
  7050. else
  7051. {
  7052. mem_rtx = NULL_RTX;
  7053. offset += GET_MODE_SIZE (mode);
  7054. }
  7055. }
  7056. if (mem_rtx != NULL_RTX)
  7057. goto addr_ok;
  7058. if (offset_in_r0 == -1)
  7059. {
  7060. emit_move_insn (r0, GEN_INT (offset));
  7061. offset_in_r0 = offset;
  7062. }
  7063. else if (offset != offset_in_r0)
  7064. {
  7065. emit_move_insn (r0,
  7066. gen_rtx_PLUS
  7067. (Pmode, r0,
  7068. GEN_INT (offset - offset_in_r0)));
  7069. offset_in_r0 += offset - offset_in_r0;
  7070. }
  7071. if (pre_dec != NULL_RTX)
  7072. {
  7073. if (! sp_in_r0)
  7074. {
  7075. emit_move_insn (r0,
  7076. gen_rtx_PLUS
  7077. (Pmode, r0, stack_pointer_rtx));
  7078. sp_in_r0 = 1;
  7079. }
  7080. offset -= GET_MODE_SIZE (mode);
  7081. offset_in_r0 -= GET_MODE_SIZE (mode);
  7082. mem_rtx = pre_dec;
  7083. }
  7084. else if (sp_in_r0)
  7085. mem_rtx = gen_frame_mem (mode, r0);
  7086. else
  7087. mem_rtx = gen_frame_mem (mode,
  7088. gen_rtx_PLUS (Pmode,
  7089. stack_pointer_rtx,
  7090. r0));
  7091. /* We must not use an r0-based address for target-branch
  7092. registers or for special registers without pre-dec
  7093. memory addresses, since we store their values in r0
  7094. first. */
  7095. gcc_assert (!TARGET_REGISTER_P (reg)
  7096. && ((reg != PR_REG && !SPECIAL_REGISTER_P (reg))
  7097. || mem_rtx == pre_dec));
  7098. addr_ok:
  7099. orig_reg_rtx = reg_rtx;
  7100. if (TARGET_REGISTER_P (reg)
  7101. || ((reg == PR_REG || SPECIAL_REGISTER_P (reg))
  7102. && mem_rtx != pre_dec))
  7103. {
  7104. rtx tmp_reg = gen_rtx_REG (GET_MODE (reg_rtx), *tmp_pnt);
  7105. emit_move_insn (tmp_reg, reg_rtx);
  7106. if (REGNO (tmp_reg) == R0_REG)
  7107. {
  7108. offset_in_r0 = -1;
  7109. sp_in_r0 = 0;
  7110. gcc_assert (!refers_to_regno_p (R0_REG, mem_rtx));
  7111. }
  7112. if (*++tmp_pnt <= 0)
  7113. tmp_pnt = schedule.temps;
  7114. reg_rtx = tmp_reg;
  7115. }
  7116. {
  7117. rtx insn;
  7118. /* Mark as interesting for dwarf cfi generator */
  7119. insn = emit_move_insn (mem_rtx, reg_rtx);
  7120. RTX_FRAME_RELATED_P (insn) = 1;
  7121. /* If we use an intermediate register for the save, we can't
  7122. describe this exactly in cfi as a copy of the to-be-saved
  7123. register into the temporary register and then the temporary
  7124. register on the stack, because the temporary register can
  7125. have a different natural size than the to-be-saved register.
  7126. Thus, we gloss over the intermediate copy and pretend we do
  7127. a direct save from the to-be-saved register. */
  7128. if (REGNO (reg_rtx) != reg)
  7129. {
  7130. rtx set;
  7131. set = gen_rtx_SET (VOIDmode, mem_rtx, orig_reg_rtx);
  7132. add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
  7133. }
  7134. if (TARGET_SHCOMPACT && (offset_in_r0 != -1))
  7135. {
  7136. rtx reg_rtx = gen_rtx_REG (mode, reg);
  7137. rtx set;
  7138. rtx mem_rtx = gen_frame_mem (mode,
  7139. gen_rtx_PLUS (Pmode,
  7140. stack_pointer_rtx,
  7141. GEN_INT (offset)));
  7142. set = gen_rtx_SET (VOIDmode, mem_rtx, reg_rtx);
  7143. add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
  7144. }
  7145. }
  7146. }
  7147. gcc_assert (entry->offset == d_rounding);
  7148. }
  7149. else
  7150. {
  7151. push_regs (&live_regs_mask, current_function_interrupt);
  7152. stack_usage += d;
  7153. }
  7154. if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
  7155. emit_insn (gen_GOTaddr2picreg (const0_rtx));
  7156. if (SHMEDIA_REGS_STACK_ADJUST ())
  7157. {
  7158. /* This must NOT go through the PLT, otherwise mach and macl
  7159. may be clobbered. */
  7160. function_symbol (gen_rtx_REG (Pmode, R0_REG),
  7161. (TARGET_FPU_ANY
  7162. ? "__GCC_push_shmedia_regs"
  7163. : "__GCC_push_shmedia_regs_nofpu"), SFUNC_GOT);
  7164. emit_insn (gen_shmedia_save_restore_regs_compact
  7165. (GEN_INT (-SHMEDIA_REGS_STACK_ADJUST ())));
  7166. }
  7167. if (target_flags != save_flags && ! current_function_interrupt)
  7168. emit_insn (gen_toggle_sz ());
  7169. target_flags = save_flags;
  7170. output_stack_adjust (-rounded_frame_size (d) + d_rounding,
  7171. stack_pointer_rtx, 0, NULL, true);
  7172. stack_usage += rounded_frame_size (d) - d_rounding;
  7173. if (frame_pointer_needed)
  7174. frame_insn (GEN_MOV (hard_frame_pointer_rtx, stack_pointer_rtx));
  7175. if (TARGET_SHCOMPACT
  7176. && (crtl->args.info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
  7177. {
  7178. /* This must NOT go through the PLT, otherwise mach and macl
  7179. may be clobbered. */
  7180. function_symbol (gen_rtx_REG (Pmode, R0_REG),
  7181. "__GCC_shcompact_incoming_args", SFUNC_GOT);
  7182. emit_insn (gen_shcompact_incoming_args ());
  7183. }
  7184. /* If we are profiling, make sure no instructions are scheduled before
  7185. the call to mcount. Similarly if some call instructions are swapped
  7186. before frame related insns, it'll confuse the unwinder because
  7187. currently SH has no unwind info for function epilogues. */
  7188. if (crtl->profile || flag_exceptions || flag_unwind_tables)
  7189. emit_insn (gen_blockage ());
  7190. if (flag_stack_usage_info)
  7191. current_function_static_stack_size = stack_usage;
  7192. }
  7193. /* Expand code for the function epilogue. */
  7194. void
  7195. sh_expand_epilogue (bool sibcall_p)
  7196. {
  7197. HARD_REG_SET live_regs_mask;
  7198. int d, i;
  7199. int d_rounding = 0;
  7200. int save_flags = target_flags;
  7201. int frame_size, save_size;
  7202. int fpscr_deferred = 0;
  7203. int e = sibcall_p ? -1 : 1;
  7204. d = calc_live_regs (&live_regs_mask);
  7205. save_size = d;
  7206. frame_size = rounded_frame_size (d);
  7207. if (TARGET_SH5)
  7208. {
  7209. int tregs_space = shmedia_target_regs_stack_adjust (&live_regs_mask);
  7210. int total_size;
  7211. if (d % (STACK_BOUNDARY / BITS_PER_UNIT))
  7212. d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
  7213. - d % (STACK_BOUNDARY / BITS_PER_UNIT));
  7214. total_size = d + tregs_space;
  7215. total_size += rounded_frame_size (total_size);
  7216. save_size = total_size - frame_size;
  7217. /* If adjusting the stack in a single step costs nothing extra, do so.
  7218. I.e. either if a single addi is enough, or we need a movi anyway,
  7219. and we don't exceed the maximum offset range (the test for the
  7220. latter is conservative for simplicity). */
  7221. if (TARGET_SHMEDIA
  7222. && ! frame_pointer_needed
  7223. && (CONST_OK_FOR_I10 (total_size)
  7224. || (! CONST_OK_FOR_I10 (save_size + d_rounding)
  7225. && total_size <= 2044)))
  7226. d_rounding = frame_size;
  7227. frame_size -= d_rounding;
  7228. }
  7229. if (frame_pointer_needed)
  7230. {
  7231. /* We must avoid scheduling the epilogue with previous basic blocks.
  7232. See PR/18032 and PR/40313. */
  7233. emit_insn (gen_blockage ());
  7234. output_stack_adjust (frame_size, hard_frame_pointer_rtx, e,
  7235. &live_regs_mask, true);
  7236. /* We must avoid moving the stack pointer adjustment past code
  7237. which reads from the local frame, else an interrupt could
  7238. occur after the SP adjustment and clobber data in the local
  7239. frame. */
  7240. emit_insn (gen_blockage ());
  7241. frame_insn (GEN_MOV (stack_pointer_rtx, hard_frame_pointer_rtx));
  7242. }
  7243. else if (frame_size)
  7244. {
  7245. /* We must avoid moving the stack pointer adjustment past code
  7246. which reads from the local frame, else an interrupt could
  7247. occur after the SP adjustment and clobber data in the local
  7248. frame. */
  7249. emit_insn (gen_blockage ());
  7250. output_stack_adjust (frame_size, stack_pointer_rtx, e,
  7251. &live_regs_mask, true);
  7252. }
  7253. if (SHMEDIA_REGS_STACK_ADJUST ())
  7254. {
  7255. function_symbol (gen_rtx_REG (Pmode, R0_REG),
  7256. (TARGET_FPU_ANY
  7257. ? "__GCC_pop_shmedia_regs"
  7258. : "__GCC_pop_shmedia_regs_nofpu"), SFUNC_GOT);
  7259. /* This must NOT go through the PLT, otherwise mach and macl
  7260. may be clobbered. */
  7261. emit_insn (gen_shmedia_save_restore_regs_compact
  7262. (GEN_INT (SHMEDIA_REGS_STACK_ADJUST ())));
  7263. }
  7264. /* Pop all the registers. */
  7265. if (target_flags != save_flags && ! current_function_interrupt)
  7266. emit_insn (gen_toggle_sz ());
  7267. if (TARGET_SH5)
  7268. {
  7269. int offset_base, offset;
  7270. int offset_in_r0 = -1;
  7271. int sp_in_r0 = 0;
  7272. rtx r0 = gen_rtx_REG (Pmode, R0_REG);
  7273. save_schedule schedule;
  7274. save_entry *entry;
  7275. int *tmp_pnt;
  7276. entry = sh5_schedule_saves (&live_regs_mask, &schedule, d_rounding);
  7277. offset_base = -entry[1].offset + d_rounding;
  7278. tmp_pnt = schedule.temps;
  7279. for (; entry->mode != VOIDmode; entry--)
  7280. {
  7281. machine_mode mode = (machine_mode) entry->mode;
  7282. int reg = entry->reg;
  7283. rtx reg_rtx, mem_rtx, post_inc = NULL_RTX;
  7284. offset = offset_base + entry->offset;
  7285. reg_rtx = gen_rtx_REG (mode, reg);
  7286. mem_rtx = gen_frame_mem (mode,
  7287. gen_rtx_PLUS (Pmode,
  7288. stack_pointer_rtx,
  7289. GEN_INT (offset)));
  7290. if (!memory_address_p (mode, XEXP (mem_rtx, 0)))
  7291. mem_rtx = NULL_RTX;
  7292. if (HAVE_POST_INCREMENT
  7293. && (offset == offset_in_r0
  7294. || (offset + GET_MODE_SIZE (mode) != d + d_rounding
  7295. && mem_rtx == NULL_RTX)
  7296. || reg == PR_REG || SPECIAL_REGISTER_P (reg)))
  7297. {
  7298. post_inc = gen_frame_mem (mode, gen_rtx_POST_INC (Pmode, r0));
  7299. if (!memory_address_p (mode, XEXP (post_inc, 0)))
  7300. post_inc = NULL_RTX;
  7301. else
  7302. mem_rtx = NULL_RTX;
  7303. }
  7304. if (mem_rtx != NULL_RTX)
  7305. goto addr_ok;
  7306. if (offset_in_r0 == -1)
  7307. {
  7308. emit_move_insn (r0, GEN_INT (offset));
  7309. offset_in_r0 = offset;
  7310. }
  7311. else if (offset != offset_in_r0)
  7312. {
  7313. emit_move_insn (r0,
  7314. gen_rtx_PLUS
  7315. (Pmode, r0,
  7316. GEN_INT (offset - offset_in_r0)));
  7317. offset_in_r0 += offset - offset_in_r0;
  7318. }
  7319. if (post_inc != NULL_RTX)
  7320. {
  7321. if (! sp_in_r0)
  7322. {
  7323. emit_move_insn (r0,
  7324. gen_rtx_PLUS
  7325. (Pmode, r0, stack_pointer_rtx));
  7326. sp_in_r0 = 1;
  7327. }
  7328. mem_rtx = post_inc;
  7329. offset_in_r0 += GET_MODE_SIZE (mode);
  7330. }
  7331. else if (sp_in_r0)
  7332. mem_rtx = gen_frame_mem (mode, r0);
  7333. else
  7334. mem_rtx = gen_frame_mem (mode,
  7335. gen_rtx_PLUS (Pmode,
  7336. stack_pointer_rtx,
  7337. r0));
  7338. gcc_assert ((reg != PR_REG && !SPECIAL_REGISTER_P (reg))
  7339. || mem_rtx == post_inc);
  7340. addr_ok:
  7341. if ((reg == PR_REG || SPECIAL_REGISTER_P (reg))
  7342. && mem_rtx != post_inc)
  7343. {
  7344. emit_move_insn (r0, mem_rtx);
  7345. mem_rtx = r0;
  7346. }
  7347. else if (TARGET_REGISTER_P (reg))
  7348. {
  7349. rtx tmp_reg = gen_rtx_REG (mode, *tmp_pnt);
  7350. /* Give the scheduler a bit of freedom by using up to
  7351. MAX_TEMPS registers in a round-robin fashion. */
  7352. emit_move_insn (tmp_reg, mem_rtx);
  7353. mem_rtx = tmp_reg;
  7354. if (*++tmp_pnt < 0)
  7355. tmp_pnt = schedule.temps;
  7356. }
  7357. emit_move_insn (reg_rtx, mem_rtx);
  7358. }
  7359. gcc_assert (entry->offset + offset_base == d + d_rounding);
  7360. }
  7361. else /* ! TARGET_SH5 */
  7362. {
  7363. int last_reg;
  7364. save_size = 0;
  7365. /* For an ISR with RESBANK attribute assigned, don't pop PR
  7366. register. */
  7367. if (TEST_HARD_REG_BIT (live_regs_mask, PR_REG)
  7368. && !sh_cfun_resbank_handler_p ())
  7369. {
  7370. if (!frame_pointer_needed)
  7371. emit_insn (gen_blockage ());
  7372. pop (PR_REG);
  7373. }
  7374. /* Banked registers are popped first to avoid being scheduled in the
  7375. delay slot. RTE switches banks before the ds instruction. */
  7376. if (current_function_interrupt)
  7377. {
  7378. bool use_movml = false;
  7379. if (TARGET_SH2A)
  7380. {
  7381. unsigned int count = 0;
  7382. for (i = FIRST_BANKED_REG; i <= LAST_BANKED_REG; i++)
  7383. if (TEST_HARD_REG_BIT (live_regs_mask, i))
  7384. count++;
  7385. else
  7386. break;
  7387. /* Use movml when all banked register are poped. */
  7388. if (count == LAST_BANKED_REG - FIRST_BANKED_REG + 1)
  7389. use_movml = true;
  7390. }
  7391. if (sh_cfun_resbank_handler_p ())
  7392. ; /* Do nothing. */
  7393. else if (use_movml)
  7394. {
  7395. rtx sp_reg = gen_rtx_REG (SImode, STACK_POINTER_REGNUM);
  7396. /* We must avoid scheduling multiple load insn with another
  7397. insns. */
  7398. emit_insn (gen_blockage ());
  7399. emit_insn (gen_movml_pop_banked (sp_reg));
  7400. emit_insn (gen_blockage ());
  7401. }
  7402. else
  7403. for (i = LAST_BANKED_REG; i >= FIRST_BANKED_REG; i--)
  7404. if (TEST_HARD_REG_BIT (live_regs_mask, i))
  7405. pop (i);
  7406. last_reg = FIRST_PSEUDO_REGISTER - LAST_BANKED_REG - 1;
  7407. }
  7408. else
  7409. last_reg = FIRST_PSEUDO_REGISTER;
  7410. for (i = 0; i < last_reg; i++)
  7411. {
  7412. int j = (FIRST_PSEUDO_REGISTER - 1) - i;
  7413. if (j == FPSCR_REG && current_function_interrupt && TARGET_FMOVD
  7414. && hard_reg_set_intersect_p (live_regs_mask,
  7415. reg_class_contents[DF_REGS]))
  7416. fpscr_deferred = 1;
  7417. /* For an ISR with RESBANK attribute assigned, don't pop
  7418. following registers, R0-R14, MACH, MACL and GBR. */
  7419. else if (j != PR_REG && TEST_HARD_REG_BIT (live_regs_mask, j)
  7420. && ! (sh_cfun_resbank_handler_p ()
  7421. && ((j >= FIRST_GENERAL_REG
  7422. && j < LAST_GENERAL_REG)
  7423. || j == MACH_REG
  7424. || j == MACL_REG
  7425. || j == GBR_REG)))
  7426. pop (j);
  7427. if (j == FIRST_FP_REG && fpscr_deferred)
  7428. pop (FPSCR_REG);
  7429. }
  7430. }
  7431. if (target_flags != save_flags && ! current_function_interrupt)
  7432. emit_insn (gen_toggle_sz ());
  7433. target_flags = save_flags;
  7434. output_stack_adjust (crtl->args.pretend_args_size
  7435. + save_size + d_rounding
  7436. + crtl->args.info.stack_regs * 8,
  7437. stack_pointer_rtx, e, NULL, true);
  7438. if (crtl->calls_eh_return)
  7439. emit_insn (GEN_ADD3 (stack_pointer_rtx, stack_pointer_rtx,
  7440. EH_RETURN_STACKADJ_RTX));
  7441. /* Switch back to the normal stack if necessary. */
  7442. if (lookup_attribute ("sp_switch", DECL_ATTRIBUTES (current_function_decl)))
  7443. emit_insn (gen_sp_switch_2 ());
  7444. /* Tell flow the insn that pops PR isn't dead. */
  7445. /* PR_REG will never be live in SHmedia mode, and we don't need to
  7446. USE PR_MEDIA_REG, since it will be explicitly copied to TR0_REG
  7447. by the return pattern. */
  7448. if (TEST_HARD_REG_BIT (live_regs_mask, PR_REG))
  7449. emit_use (gen_rtx_REG (SImode, PR_REG));
  7450. }
  7451. /* Emit code to change the current function's return address to RA.
  7452. TEMP is available as a scratch register, if needed. */
  7453. void
  7454. sh_set_return_address (rtx ra, rtx tmp)
  7455. {
  7456. HARD_REG_SET live_regs_mask;
  7457. int d;
  7458. int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
  7459. int pr_offset;
  7460. d = calc_live_regs (&live_regs_mask);
  7461. /* If pr_reg isn't life, we can set it (or the register given in
  7462. sh_media_register_for_return) directly. */
  7463. if (! TEST_HARD_REG_BIT (live_regs_mask, pr_reg))
  7464. {
  7465. rtx rr;
  7466. if (TARGET_SHMEDIA)
  7467. {
  7468. int rr_regno = sh_media_register_for_return ();
  7469. if (rr_regno < 0)
  7470. rr_regno = pr_reg;
  7471. rr = gen_rtx_REG (DImode, rr_regno);
  7472. }
  7473. else
  7474. rr = gen_rtx_REG (SImode, pr_reg);
  7475. emit_insn (GEN_MOV (rr, ra));
  7476. /* Tell flow the register for return isn't dead. */
  7477. emit_use (rr);
  7478. return;
  7479. }
  7480. if (TARGET_SH5)
  7481. {
  7482. int offset;
  7483. save_schedule schedule;
  7484. save_entry *entry;
  7485. entry = sh5_schedule_saves (&live_regs_mask, &schedule, 0);
  7486. offset = entry[1].offset;
  7487. for (; entry->mode != VOIDmode; entry--)
  7488. if (entry->reg == pr_reg)
  7489. goto found;
  7490. /* We can't find pr register. */
  7491. gcc_unreachable ();
  7492. found:
  7493. offset = entry->offset - offset;
  7494. pr_offset = (rounded_frame_size (d) + offset
  7495. + SHMEDIA_REGS_STACK_ADJUST ());
  7496. }
  7497. else
  7498. pr_offset = rounded_frame_size (d);
  7499. emit_insn (GEN_MOV (tmp, GEN_INT (pr_offset)));
  7500. if (frame_pointer_needed)
  7501. emit_insn (GEN_ADD3 (tmp, tmp, hard_frame_pointer_rtx));
  7502. else
  7503. emit_insn (GEN_ADD3 (tmp, tmp, stack_pointer_rtx));
  7504. tmp = gen_frame_mem (Pmode, tmp);
  7505. emit_insn (GEN_MOV (tmp, ra));
  7506. /* Tell this store isn't dead. */
  7507. emit_use (tmp);
  7508. }
  7509. /* Clear variables at function end. */
  7510. static void
  7511. sh_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
  7512. HOST_WIDE_INT size ATTRIBUTE_UNUSED)
  7513. {
  7514. }
  7515. static rtx
  7516. sh_builtin_saveregs (void)
  7517. {
  7518. /* First unnamed integer register. */
  7519. int first_intreg = crtl->args.info.arg_count[(int) SH_ARG_INT];
  7520. /* Number of integer registers we need to save. */
  7521. int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
  7522. /* First unnamed SFmode float reg */
  7523. int first_floatreg = crtl->args.info.arg_count[(int) SH_ARG_FLOAT];
  7524. /* Number of SFmode float regs to save. */
  7525. int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
  7526. rtx regbuf, fpregs;
  7527. int bufsize, regno;
  7528. alias_set_type alias_set;
  7529. if (TARGET_SH5)
  7530. {
  7531. if (n_intregs)
  7532. {
  7533. int pushregs = n_intregs;
  7534. while (pushregs < NPARM_REGS (SImode) - 1
  7535. && (CALL_COOKIE_INT_REG_GET
  7536. (crtl->args.info.call_cookie,
  7537. NPARM_REGS (SImode) - pushregs)
  7538. == 1))
  7539. {
  7540. crtl->args.info.call_cookie
  7541. &= ~ CALL_COOKIE_INT_REG (NPARM_REGS (SImode)
  7542. - pushregs, 1);
  7543. pushregs++;
  7544. }
  7545. if (pushregs == NPARM_REGS (SImode))
  7546. crtl->args.info.call_cookie
  7547. |= (CALL_COOKIE_INT_REG (0, 1)
  7548. | CALL_COOKIE_STACKSEQ (pushregs - 1));
  7549. else
  7550. crtl->args.info.call_cookie
  7551. |= CALL_COOKIE_STACKSEQ (pushregs);
  7552. crtl->args.pretend_args_size += 8 * n_intregs;
  7553. }
  7554. if (TARGET_SHCOMPACT)
  7555. return const0_rtx;
  7556. }
  7557. if (! TARGET_SH2E && ! TARGET_SH4 && ! TARGET_SH5)
  7558. {
  7559. error ("__builtin_saveregs not supported by this subtarget");
  7560. return const0_rtx;
  7561. }
  7562. if (TARGET_SHMEDIA)
  7563. n_floatregs = 0;
  7564. /* Allocate block of memory for the regs. */
  7565. /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
  7566. Or can assign_stack_local accept a 0 SIZE argument? */
  7567. bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
  7568. if (TARGET_SHMEDIA)
  7569. regbuf = gen_frame_mem (BLKmode, gen_rtx_REG (Pmode, ARG_POINTER_REGNUM));
  7570. else if (n_floatregs & 1)
  7571. {
  7572. rtx addr;
  7573. regbuf = assign_stack_local (BLKmode, bufsize + UNITS_PER_WORD, 0);
  7574. addr = copy_to_mode_reg (Pmode, XEXP (regbuf, 0));
  7575. emit_insn (gen_iorsi3 (addr, addr, GEN_INT (UNITS_PER_WORD)));
  7576. regbuf = change_address (regbuf, BLKmode, addr);
  7577. }
  7578. else if (STACK_BOUNDARY < 64 && TARGET_FPU_DOUBLE && n_floatregs)
  7579. {
  7580. rtx addr, mask;
  7581. regbuf = assign_stack_local (BLKmode, bufsize + UNITS_PER_WORD, 0);
  7582. addr = copy_to_mode_reg (Pmode, plus_constant (Pmode,
  7583. XEXP (regbuf, 0), 4));
  7584. mask = copy_to_mode_reg (Pmode, GEN_INT (-8));
  7585. emit_insn (gen_andsi3 (addr, addr, mask));
  7586. regbuf = change_address (regbuf, BLKmode, addr);
  7587. }
  7588. else
  7589. regbuf = assign_stack_local (BLKmode, bufsize, TARGET_FPU_DOUBLE ? 64 : 0);
  7590. alias_set = get_varargs_alias_set ();
  7591. set_mem_alias_set (regbuf, alias_set);
  7592. /* Save int args.
  7593. This is optimized to only save the regs that are necessary. Explicitly
  7594. named args need not be saved. */
  7595. if (n_intregs > 0)
  7596. move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
  7597. adjust_address (regbuf, BLKmode,
  7598. n_floatregs * UNITS_PER_WORD),
  7599. n_intregs);
  7600. if (TARGET_SHMEDIA)
  7601. /* Return the address of the regbuf. */
  7602. return XEXP (regbuf, 0);
  7603. /* Save float args.
  7604. This is optimized to only save the regs that are necessary. Explicitly
  7605. named args need not be saved.
  7606. We explicitly build a pointer to the buffer because it halves the insn
  7607. count when not optimizing (otherwise the pointer is built for each reg
  7608. saved).
  7609. We emit the moves in reverse order so that we can use predecrement. */
  7610. fpregs = copy_to_mode_reg (Pmode,
  7611. plus_constant (Pmode, XEXP (regbuf, 0),
  7612. n_floatregs * UNITS_PER_WORD));
  7613. if (TARGET_SH4 || TARGET_SH2A_DOUBLE)
  7614. {
  7615. rtx mem;
  7616. for (regno = NPARM_REGS (DFmode) - 2; regno >= first_floatreg; regno -= 2)
  7617. {
  7618. emit_insn (gen_addsi3 (fpregs, fpregs,
  7619. GEN_INT (-2 * UNITS_PER_WORD)));
  7620. mem = change_address (regbuf, DFmode, fpregs);
  7621. emit_move_insn (mem,
  7622. gen_rtx_REG (DFmode, BASE_ARG_REG (DFmode) + regno));
  7623. }
  7624. regno = first_floatreg;
  7625. if (regno & 1)
  7626. {
  7627. emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (-UNITS_PER_WORD)));
  7628. mem = change_address (regbuf, SFmode, fpregs);
  7629. emit_move_insn (mem,
  7630. gen_rtx_REG (SFmode, BASE_ARG_REG (SFmode)
  7631. + regno - SH_REG_MSW_OFFSET));
  7632. }
  7633. }
  7634. else
  7635. for (regno = NPARM_REGS (SFmode) - 1; regno >= first_floatreg; regno--)
  7636. {
  7637. rtx mem;
  7638. emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (-UNITS_PER_WORD)));
  7639. mem = change_address (regbuf, SFmode, fpregs);
  7640. emit_move_insn (mem,
  7641. gen_rtx_REG (SFmode, BASE_ARG_REG (SFmode) + regno));
  7642. }
  7643. /* Return the address of the regbuf. */
  7644. return XEXP (regbuf, 0);
  7645. }
  7646. /* Define the `__builtin_va_list' type for the ABI. */
  7647. static tree
  7648. sh_build_builtin_va_list (void)
  7649. {
  7650. tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
  7651. tree record, type_decl;
  7652. if (TARGET_SH5 || (! TARGET_SH2E && ! TARGET_SH4)
  7653. || TARGET_HITACHI || sh_cfun_attr_renesas_p ())
  7654. return ptr_type_node;
  7655. record = (*lang_hooks.types.make_type) (RECORD_TYPE);
  7656. type_decl = build_decl (BUILTINS_LOCATION,
  7657. TYPE_DECL, get_identifier ("__va_list_tag"), record);
  7658. f_next_o = build_decl (BUILTINS_LOCATION,
  7659. FIELD_DECL, get_identifier ("__va_next_o"),
  7660. ptr_type_node);
  7661. f_next_o_limit = build_decl (BUILTINS_LOCATION,
  7662. FIELD_DECL,
  7663. get_identifier ("__va_next_o_limit"),
  7664. ptr_type_node);
  7665. f_next_fp = build_decl (BUILTINS_LOCATION,
  7666. FIELD_DECL, get_identifier ("__va_next_fp"),
  7667. ptr_type_node);
  7668. f_next_fp_limit = build_decl (BUILTINS_LOCATION,
  7669. FIELD_DECL,
  7670. get_identifier ("__va_next_fp_limit"),
  7671. ptr_type_node);
  7672. f_next_stack = build_decl (BUILTINS_LOCATION,
  7673. FIELD_DECL, get_identifier ("__va_next_stack"),
  7674. ptr_type_node);
  7675. DECL_FIELD_CONTEXT (f_next_o) = record;
  7676. DECL_FIELD_CONTEXT (f_next_o_limit) = record;
  7677. DECL_FIELD_CONTEXT (f_next_fp) = record;
  7678. DECL_FIELD_CONTEXT (f_next_fp_limit) = record;
  7679. DECL_FIELD_CONTEXT (f_next_stack) = record;
  7680. TYPE_STUB_DECL (record) = type_decl;
  7681. TYPE_NAME (record) = type_decl;
  7682. TYPE_FIELDS (record) = f_next_o;
  7683. DECL_CHAIN (f_next_o) = f_next_o_limit;
  7684. DECL_CHAIN (f_next_o_limit) = f_next_fp;
  7685. DECL_CHAIN (f_next_fp) = f_next_fp_limit;
  7686. DECL_CHAIN (f_next_fp_limit) = f_next_stack;
  7687. layout_type (record);
  7688. return record;
  7689. }
  7690. /* Implement `va_start' for varargs and stdarg. */
  7691. static void
  7692. sh_va_start (tree valist, rtx nextarg)
  7693. {
  7694. tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
  7695. tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
  7696. tree t, u;
  7697. int nfp, nint;
  7698. if (TARGET_SH5)
  7699. {
  7700. expand_builtin_saveregs ();
  7701. std_expand_builtin_va_start (valist, nextarg);
  7702. return;
  7703. }
  7704. if ((! TARGET_SH2E && ! TARGET_SH4)
  7705. || TARGET_HITACHI || sh_cfun_attr_renesas_p ())
  7706. {
  7707. std_expand_builtin_va_start (valist, nextarg);
  7708. return;
  7709. }
  7710. f_next_o = TYPE_FIELDS (va_list_type_node);
  7711. f_next_o_limit = DECL_CHAIN (f_next_o);
  7712. f_next_fp = DECL_CHAIN (f_next_o_limit);
  7713. f_next_fp_limit = DECL_CHAIN (f_next_fp);
  7714. f_next_stack = DECL_CHAIN (f_next_fp_limit);
  7715. next_o = build3 (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o,
  7716. NULL_TREE);
  7717. next_o_limit = build3 (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
  7718. valist, f_next_o_limit, NULL_TREE);
  7719. next_fp = build3 (COMPONENT_REF, TREE_TYPE (f_next_fp), valist, f_next_fp,
  7720. NULL_TREE);
  7721. next_fp_limit = build3 (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
  7722. valist, f_next_fp_limit, NULL_TREE);
  7723. next_stack = build3 (COMPONENT_REF, TREE_TYPE (f_next_stack),
  7724. valist, f_next_stack, NULL_TREE);
  7725. /* Call __builtin_saveregs. */
  7726. u = make_tree (sizetype, expand_builtin_saveregs ());
  7727. u = fold_convert (ptr_type_node, u);
  7728. t = build2 (MODIFY_EXPR, ptr_type_node, next_fp, u);
  7729. TREE_SIDE_EFFECTS (t) = 1;
  7730. expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
  7731. nfp = crtl->args.info.arg_count[SH_ARG_FLOAT];
  7732. if (nfp < 8)
  7733. nfp = 8 - nfp;
  7734. else
  7735. nfp = 0;
  7736. u = fold_build_pointer_plus_hwi (u, UNITS_PER_WORD * nfp);
  7737. t = build2 (MODIFY_EXPR, ptr_type_node, next_fp_limit, u);
  7738. TREE_SIDE_EFFECTS (t) = 1;
  7739. expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
  7740. t = build2 (MODIFY_EXPR, ptr_type_node, next_o, u);
  7741. TREE_SIDE_EFFECTS (t) = 1;
  7742. expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
  7743. nint = crtl->args.info.arg_count[SH_ARG_INT];
  7744. if (nint < 4)
  7745. nint = 4 - nint;
  7746. else
  7747. nint = 0;
  7748. u = fold_build_pointer_plus_hwi (u, UNITS_PER_WORD * nint);
  7749. t = build2 (MODIFY_EXPR, ptr_type_node, next_o_limit, u);
  7750. TREE_SIDE_EFFECTS (t) = 1;
  7751. expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
  7752. u = make_tree (ptr_type_node, nextarg);
  7753. t = build2 (MODIFY_EXPR, ptr_type_node, next_stack, u);
  7754. TREE_SIDE_EFFECTS (t) = 1;
  7755. expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
  7756. }
  7757. /* TYPE is a RECORD_TYPE. If there is only a single nonzero-sized
  7758. member, return it. */
  7759. static tree
  7760. find_sole_member (tree type)
  7761. {
  7762. tree field, member = NULL_TREE;
  7763. for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  7764. {
  7765. if (TREE_CODE (field) != FIELD_DECL)
  7766. continue;
  7767. if (!DECL_SIZE (field))
  7768. return NULL_TREE;
  7769. if (integer_zerop (DECL_SIZE (field)))
  7770. continue;
  7771. if (member)
  7772. return NULL_TREE;
  7773. member = field;
  7774. }
  7775. return member;
  7776. }
  7777. /* Implement `va_arg'. */
  7778. static tree
  7779. sh_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
  7780. gimple_seq *post_p ATTRIBUTE_UNUSED)
  7781. {
  7782. HOST_WIDE_INT size, rsize;
  7783. tree tmp, pptr_type_node;
  7784. tree addr, lab_over = NULL, result = NULL;
  7785. bool pass_by_ref;
  7786. tree eff_type;
  7787. if (!VOID_TYPE_P (type))
  7788. pass_by_ref = targetm.calls.must_pass_in_stack (TYPE_MODE (type), type);
  7789. else
  7790. pass_by_ref = false;
  7791. if (pass_by_ref)
  7792. type = build_pointer_type (type);
  7793. size = int_size_in_bytes (type);
  7794. rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
  7795. pptr_type_node = build_pointer_type (ptr_type_node);
  7796. if (! TARGET_SH5 && (TARGET_SH2E || TARGET_SH4)
  7797. && ! (TARGET_HITACHI || sh_cfun_attr_renesas_p ()))
  7798. {
  7799. tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
  7800. tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
  7801. int pass_as_float;
  7802. tree lab_false;
  7803. tree member;
  7804. f_next_o = TYPE_FIELDS (va_list_type_node);
  7805. f_next_o_limit = DECL_CHAIN (f_next_o);
  7806. f_next_fp = DECL_CHAIN (f_next_o_limit);
  7807. f_next_fp_limit = DECL_CHAIN (f_next_fp);
  7808. f_next_stack = DECL_CHAIN (f_next_fp_limit);
  7809. next_o = build3 (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o,
  7810. NULL_TREE);
  7811. next_o_limit = build3 (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
  7812. valist, f_next_o_limit, NULL_TREE);
  7813. next_fp = build3 (COMPONENT_REF, TREE_TYPE (f_next_fp),
  7814. valist, f_next_fp, NULL_TREE);
  7815. next_fp_limit = build3 (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
  7816. valist, f_next_fp_limit, NULL_TREE);
  7817. next_stack = build3 (COMPONENT_REF, TREE_TYPE (f_next_stack),
  7818. valist, f_next_stack, NULL_TREE);
  7819. /* Structures with a single member with a distinct mode are passed
  7820. like their member. This is relevant if the latter has a REAL_TYPE
  7821. or COMPLEX_TYPE type. */
  7822. eff_type = type;
  7823. while (TREE_CODE (eff_type) == RECORD_TYPE
  7824. && (member = find_sole_member (eff_type))
  7825. && (TREE_CODE (TREE_TYPE (member)) == REAL_TYPE
  7826. || TREE_CODE (TREE_TYPE (member)) == COMPLEX_TYPE
  7827. || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE))
  7828. {
  7829. tree field_type = TREE_TYPE (member);
  7830. if (TYPE_MODE (eff_type) == TYPE_MODE (field_type))
  7831. eff_type = field_type;
  7832. else
  7833. {
  7834. gcc_assert ((TYPE_ALIGN (eff_type)
  7835. < GET_MODE_ALIGNMENT (TYPE_MODE (field_type)))
  7836. || (TYPE_ALIGN (eff_type)
  7837. > GET_MODE_BITSIZE (TYPE_MODE (field_type))));
  7838. break;
  7839. }
  7840. }
  7841. if (TARGET_SH4 || TARGET_SH2A_DOUBLE)
  7842. {
  7843. pass_as_float = ((TREE_CODE (eff_type) == REAL_TYPE && size <= 8)
  7844. || (TREE_CODE (eff_type) == COMPLEX_TYPE
  7845. && TREE_CODE (TREE_TYPE (eff_type)) == REAL_TYPE
  7846. && size <= 16));
  7847. }
  7848. else
  7849. {
  7850. pass_as_float = (TREE_CODE (eff_type) == REAL_TYPE && size == 4);
  7851. }
  7852. addr = create_tmp_var (pptr_type_node);
  7853. lab_false = create_artificial_label (UNKNOWN_LOCATION);
  7854. lab_over = create_artificial_label (UNKNOWN_LOCATION);
  7855. valist = build_simple_mem_ref (addr);
  7856. if (pass_as_float)
  7857. {
  7858. tree next_fp_tmp = create_tmp_var (TREE_TYPE (f_next_fp));
  7859. tree cmp;
  7860. bool is_double = size == 8 && TREE_CODE (eff_type) == REAL_TYPE;
  7861. tmp = build1 (ADDR_EXPR, pptr_type_node, unshare_expr (next_fp));
  7862. gimplify_assign (unshare_expr (addr), tmp, pre_p);
  7863. gimplify_assign (unshare_expr (next_fp_tmp), valist, pre_p);
  7864. tmp = next_fp_limit;
  7865. if (size > 4 && !is_double)
  7866. tmp = fold_build_pointer_plus_hwi (unshare_expr (tmp), 4 - size);
  7867. tmp = build2 (GE_EXPR, boolean_type_node,
  7868. unshare_expr (next_fp_tmp), unshare_expr (tmp));
  7869. cmp = build3 (COND_EXPR, void_type_node, tmp,
  7870. build1 (GOTO_EXPR, void_type_node,
  7871. unshare_expr (lab_false)), NULL_TREE);
  7872. if (!is_double)
  7873. gimplify_and_add (cmp, pre_p);
  7874. if (TYPE_ALIGN (eff_type) > BITS_PER_WORD
  7875. || (is_double || size == 16))
  7876. {
  7877. tmp = fold_convert (sizetype, next_fp_tmp);
  7878. tmp = build2 (BIT_AND_EXPR, sizetype, tmp,
  7879. size_int (UNITS_PER_WORD));
  7880. tmp = fold_build_pointer_plus (unshare_expr (next_fp_tmp), tmp);
  7881. gimplify_assign (unshare_expr (next_fp_tmp), tmp, pre_p);
  7882. }
  7883. if (is_double)
  7884. gimplify_and_add (cmp, pre_p);
  7885. #ifdef FUNCTION_ARG_SCmode_WART
  7886. if (TYPE_MODE (eff_type) == SCmode
  7887. && TARGET_SH4 && TARGET_LITTLE_ENDIAN)
  7888. {
  7889. tree subtype = TREE_TYPE (eff_type);
  7890. tree real, imag;
  7891. imag
  7892. = std_gimplify_va_arg_expr (next_fp_tmp, subtype, pre_p, NULL);
  7893. imag = get_initialized_tmp_var (imag, pre_p, NULL);
  7894. real
  7895. = std_gimplify_va_arg_expr (next_fp_tmp, subtype, pre_p, NULL);
  7896. real = get_initialized_tmp_var (real, pre_p, NULL);
  7897. result = build2 (COMPLEX_EXPR, eff_type, real, imag);
  7898. if (type != eff_type)
  7899. result = build1 (VIEW_CONVERT_EXPR, type, result);
  7900. result = get_initialized_tmp_var (result, pre_p, NULL);
  7901. }
  7902. #endif /* FUNCTION_ARG_SCmode_WART */
  7903. tmp = build1 (GOTO_EXPR, void_type_node, unshare_expr (lab_over));
  7904. gimplify_and_add (tmp, pre_p);
  7905. tmp = build1 (LABEL_EXPR, void_type_node, unshare_expr (lab_false));
  7906. gimplify_and_add (tmp, pre_p);
  7907. tmp = build1 (ADDR_EXPR, pptr_type_node, unshare_expr (next_stack));
  7908. gimplify_assign (unshare_expr (addr), tmp, pre_p);
  7909. gimplify_assign (unshare_expr (next_fp_tmp),
  7910. unshare_expr (valist), pre_p);
  7911. gimplify_assign (unshare_expr (valist),
  7912. unshare_expr (next_fp_tmp), post_p);
  7913. valist = next_fp_tmp;
  7914. }
  7915. else
  7916. {
  7917. tmp = fold_build_pointer_plus_hwi (unshare_expr (next_o), rsize);
  7918. tmp = build2 (GT_EXPR, boolean_type_node, tmp,
  7919. unshare_expr (next_o_limit));
  7920. tmp = build3 (COND_EXPR, void_type_node, tmp,
  7921. build1 (GOTO_EXPR, void_type_node,
  7922. unshare_expr (lab_false)),
  7923. NULL_TREE);
  7924. gimplify_and_add (tmp, pre_p);
  7925. tmp = build1 (ADDR_EXPR, pptr_type_node, unshare_expr (next_o));
  7926. gimplify_assign (unshare_expr (addr), tmp, pre_p);
  7927. tmp = build1 (GOTO_EXPR, void_type_node, unshare_expr (lab_over));
  7928. gimplify_and_add (tmp, pre_p);
  7929. tmp = build1 (LABEL_EXPR, void_type_node, unshare_expr (lab_false));
  7930. gimplify_and_add (tmp, pre_p);
  7931. if (size > 4 && ! (TARGET_SH4 || TARGET_SH2A))
  7932. gimplify_assign (unshare_expr (next_o),
  7933. unshare_expr (next_o_limit), pre_p);
  7934. tmp = build1 (ADDR_EXPR, pptr_type_node, unshare_expr (next_stack));
  7935. gimplify_assign (unshare_expr (addr), tmp, pre_p);
  7936. }
  7937. if (!result)
  7938. {
  7939. tmp = build1 (LABEL_EXPR, void_type_node, unshare_expr (lab_over));
  7940. gimplify_and_add (tmp, pre_p);
  7941. }
  7942. }
  7943. /* ??? In va-sh.h, there had been code to make values larger than
  7944. size 8 indirect. This does not match the FUNCTION_ARG macros. */
  7945. tmp = std_gimplify_va_arg_expr (valist, type, pre_p, NULL);
  7946. if (result)
  7947. {
  7948. gimplify_assign (result, tmp, pre_p);
  7949. result = build1 (NOP_EXPR, TREE_TYPE (result), result);
  7950. tmp = build1 (LABEL_EXPR, void_type_node, unshare_expr (lab_over));
  7951. gimplify_and_add (tmp, pre_p);
  7952. }
  7953. else
  7954. result = tmp;
  7955. if (pass_by_ref)
  7956. result = build_va_arg_indirect_ref (result);
  7957. return result;
  7958. }
  7959. /* 64 bit floating points memory transfers are paired single precision loads
  7960. or store. So DWARF information needs fixing in little endian (unless
  7961. PR=SZ=1 in FPSCR). */
  7962. rtx
  7963. sh_dwarf_register_span (rtx reg)
  7964. {
  7965. unsigned regno = REGNO (reg);
  7966. if (WORDS_BIG_ENDIAN || GET_MODE (reg) != DFmode)
  7967. return NULL_RTX;
  7968. return
  7969. gen_rtx_PARALLEL (VOIDmode,
  7970. gen_rtvec (2,
  7971. gen_rtx_REG (SFmode, regno + 1),
  7972. gen_rtx_REG (SFmode, regno)));
  7973. }
  7974. static machine_mode
  7975. sh_promote_function_mode (const_tree type, machine_mode mode,
  7976. int *punsignedp, const_tree funtype,
  7977. int for_return)
  7978. {
  7979. if (sh_promote_prototypes (funtype))
  7980. return promote_mode (type, mode, punsignedp);
  7981. else
  7982. return default_promote_function_mode (type, mode, punsignedp, funtype,
  7983. for_return);
  7984. }
  7985. static bool
  7986. sh_promote_prototypes (const_tree type)
  7987. {
  7988. if (TARGET_HITACHI)
  7989. return false;
  7990. if (! type)
  7991. return true;
  7992. return ! sh_attr_renesas_p (type);
  7993. }
  7994. /* Whether an argument must be passed by reference. On SHcompact, we
  7995. pretend arguments wider than 32-bits that would have been passed in
  7996. registers are passed by reference, so that an SHmedia trampoline
  7997. loads them into the full 64-bits registers. */
  7998. static int
  7999. shcompact_byref (const CUMULATIVE_ARGS *cum, machine_mode mode,
  8000. const_tree type, bool named)
  8001. {
  8002. unsigned HOST_WIDE_INT size;
  8003. if (type)
  8004. size = int_size_in_bytes (type);
  8005. else
  8006. size = GET_MODE_SIZE (mode);
  8007. if (cum->arg_count[SH_ARG_INT] < NPARM_REGS (SImode)
  8008. && (!named
  8009. || GET_SH_ARG_CLASS (mode) == SH_ARG_INT
  8010. || (GET_SH_ARG_CLASS (mode) == SH_ARG_FLOAT
  8011. && cum->arg_count[SH_ARG_FLOAT] >= NPARM_REGS (SFmode)))
  8012. && size > 4
  8013. && !SHCOMPACT_FORCE_ON_STACK (mode, type)
  8014. && !SH5_WOULD_BE_PARTIAL_NREGS (*cum, mode, type, named))
  8015. return size;
  8016. else
  8017. return 0;
  8018. }
  8019. static bool
  8020. sh_pass_by_reference (cumulative_args_t cum_v, machine_mode mode,
  8021. const_tree type, bool named)
  8022. {
  8023. CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
  8024. if (targetm.calls.must_pass_in_stack (mode, type))
  8025. return true;
  8026. /* ??? std_gimplify_va_arg_expr passes NULL for cum. That function
  8027. wants to know about pass-by-reference semantics for incoming
  8028. arguments. */
  8029. if (! cum)
  8030. return false;
  8031. if (TARGET_SHCOMPACT)
  8032. {
  8033. cum->byref = shcompact_byref (cum, mode, type, named);
  8034. return cum->byref != 0;
  8035. }
  8036. return false;
  8037. }
  8038. static bool
  8039. sh_callee_copies (cumulative_args_t cum, machine_mode mode,
  8040. const_tree type, bool named ATTRIBUTE_UNUSED)
  8041. {
  8042. /* ??? How can it possibly be correct to return true only on the
  8043. caller side of the equation? Is there someplace else in the
  8044. sh backend that's magically producing the copies? */
  8045. return (get_cumulative_args (cum)->outgoing
  8046. && ((mode == BLKmode ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode))
  8047. % SH_MIN_ALIGN_FOR_CALLEE_COPY == 0));
  8048. }
  8049. /* Round a register number up to a proper boundary for an arg of mode
  8050. MODE.
  8051. The SH doesn't care about double alignment, so we only
  8052. round doubles to even regs when asked to explicitly. */
  8053. static int
  8054. sh_round_reg (const CUMULATIVE_ARGS& cum, machine_mode mode)
  8055. {
  8056. /* FIXME: This used to be a macro and has been copy pasted into this
  8057. function as is. Make this more readable. */
  8058. return
  8059. (((TARGET_ALIGN_DOUBLE
  8060. || ((TARGET_SH4 || TARGET_SH2A_DOUBLE)
  8061. && (mode == DFmode || mode == DCmode)
  8062. && cum.arg_count[(int) SH_ARG_FLOAT] < NPARM_REGS (mode)))
  8063. && GET_MODE_UNIT_SIZE (mode) > UNITS_PER_WORD)
  8064. ? (cum.arg_count[(int) GET_SH_ARG_CLASS (mode)]
  8065. + (cum.arg_count[(int) GET_SH_ARG_CLASS (mode)] & 1))
  8066. : cum.arg_count[(int) GET_SH_ARG_CLASS (mode)]);
  8067. }
  8068. /* Return true if arg of the specified mode should be be passed in a register
  8069. or false otherwise. */
  8070. static bool
  8071. sh_pass_in_reg_p (const CUMULATIVE_ARGS& cum, machine_mode mode,
  8072. const_tree type)
  8073. {
  8074. /* FIXME: This used to be a macro and has been copy pasted into this
  8075. function as is. Make this more readable. */
  8076. return
  8077. ((type == 0
  8078. || (! TREE_ADDRESSABLE (type)
  8079. && (! (TARGET_HITACHI || cum.renesas_abi)
  8080. || ! (AGGREGATE_TYPE_P (type)
  8081. || (!TARGET_FPU_ANY
  8082. && (GET_MODE_CLASS (mode) == MODE_FLOAT
  8083. && GET_MODE_SIZE (mode) > GET_MODE_SIZE (SFmode)))))))
  8084. && ! cum.force_mem
  8085. && (TARGET_SH2E
  8086. ? ((mode) == BLKmode
  8087. ? ((cum.arg_count[(int) SH_ARG_INT] * UNITS_PER_WORD
  8088. + int_size_in_bytes (type))
  8089. <= NPARM_REGS (SImode) * UNITS_PER_WORD)
  8090. : ((sh_round_reg (cum, mode)
  8091. + HARD_REGNO_NREGS (BASE_ARG_REG (mode), mode))
  8092. <= NPARM_REGS (mode)))
  8093. : sh_round_reg (cum, mode) < NPARM_REGS (mode)));
  8094. }
  8095. static int
  8096. sh_arg_partial_bytes (cumulative_args_t cum_v, machine_mode mode,
  8097. tree type, bool named ATTRIBUTE_UNUSED)
  8098. {
  8099. CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
  8100. int words = 0;
  8101. if (!TARGET_SH5
  8102. && sh_pass_in_reg_p (*cum, mode, type)
  8103. && !(TARGET_SH4 || TARGET_SH2A_DOUBLE)
  8104. && (sh_round_reg (*cum, mode)
  8105. + (mode != BLKmode
  8106. ? CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD)
  8107. : CEIL (int_size_in_bytes (type), UNITS_PER_WORD))
  8108. > NPARM_REGS (mode)))
  8109. words = NPARM_REGS (mode) - sh_round_reg (*cum, mode);
  8110. else if (!TARGET_SHCOMPACT
  8111. && SH5_WOULD_BE_PARTIAL_NREGS (*cum, mode, type, named))
  8112. words = NPARM_REGS (SImode) - cum->arg_count[SH_ARG_INT];
  8113. return words * UNITS_PER_WORD;
  8114. }
  8115. /* Define where to put the arguments to a function.
  8116. Value is zero to push the argument on the stack,
  8117. or a hard register in which to store the argument.
  8118. MODE is the argument's machine mode.
  8119. TYPE is the data type of the argument (as a tree).
  8120. This is null for libcalls where that information may
  8121. not be available.
  8122. CUM is a variable of type CUMULATIVE_ARGS which gives info about
  8123. the preceding args and about the function being called.
  8124. NAMED is nonzero if this argument is a named parameter
  8125. (otherwise it is an extra parameter matching an ellipsis).
  8126. On SH the first args are normally in registers
  8127. and the rest are pushed. Any arg that starts within the first
  8128. NPARM_REGS words is at least partially passed in a register unless
  8129. its data type forbids. */
  8130. static rtx
  8131. sh_function_arg (cumulative_args_t ca_v, machine_mode mode,
  8132. const_tree type, bool named)
  8133. {
  8134. CUMULATIVE_ARGS *ca = get_cumulative_args (ca_v);
  8135. if (! TARGET_SH5 && mode == VOIDmode)
  8136. return GEN_INT (ca->renesas_abi ? 1 : 0);
  8137. if (! TARGET_SH5
  8138. && sh_pass_in_reg_p (*ca, mode, type)
  8139. && (named || ! (TARGET_HITACHI || ca->renesas_abi)))
  8140. {
  8141. int regno;
  8142. if (mode == SCmode && TARGET_SH4 && TARGET_LITTLE_ENDIAN
  8143. && (! FUNCTION_ARG_SCmode_WART || (sh_round_reg (*ca, mode) & 1)))
  8144. {
  8145. rtx r1 = gen_rtx_EXPR_LIST (VOIDmode,
  8146. gen_rtx_REG (SFmode,
  8147. BASE_ARG_REG (mode)
  8148. + (sh_round_reg (*ca, mode) ^ 1)),
  8149. const0_rtx);
  8150. rtx r2 = gen_rtx_EXPR_LIST (VOIDmode,
  8151. gen_rtx_REG (SFmode,
  8152. BASE_ARG_REG (mode)
  8153. + ((sh_round_reg (*ca, mode) + 1) ^ 1)),
  8154. GEN_INT (4));
  8155. return gen_rtx_PARALLEL(SCmode, gen_rtvec(2, r1, r2));
  8156. }
  8157. /* If the alignment of a DF value causes an SF register to be
  8158. skipped, we will use that skipped register for the next SF
  8159. value. */
  8160. if ((TARGET_HITACHI || ca->renesas_abi)
  8161. && ca->free_single_fp_reg
  8162. && mode == SFmode)
  8163. return gen_rtx_REG (mode, ca->free_single_fp_reg);
  8164. regno = (BASE_ARG_REG (mode) + sh_round_reg (*ca, mode))
  8165. ^ (mode == SFmode && TARGET_SH4
  8166. && TARGET_LITTLE_ENDIAN
  8167. && ! TARGET_HITACHI && ! ca->renesas_abi);
  8168. return gen_rtx_REG (mode, regno);
  8169. }
  8170. if (TARGET_SH5)
  8171. {
  8172. if (mode == VOIDmode && TARGET_SHCOMPACT)
  8173. return GEN_INT (ca->call_cookie);
  8174. /* The following test assumes unnamed arguments are promoted to
  8175. DFmode. */
  8176. if (mode == SFmode && ca->free_single_fp_reg)
  8177. return SH5_PROTOTYPED_FLOAT_ARG (*ca, mode, ca->free_single_fp_reg);
  8178. if ((GET_SH_ARG_CLASS (mode) == SH_ARG_FLOAT)
  8179. && (named || ! ca->prototype_p)
  8180. && ca->arg_count[(int) SH_ARG_FLOAT] < NPARM_REGS (SFmode))
  8181. {
  8182. if (! ca->prototype_p && TARGET_SHMEDIA)
  8183. return SH5_PROTOTYPELESS_FLOAT_ARG (*ca, mode);
  8184. return SH5_PROTOTYPED_FLOAT_ARG (*ca, mode,
  8185. FIRST_FP_PARM_REG
  8186. + ca->arg_count[(int) SH_ARG_FLOAT]);
  8187. }
  8188. if (ca->arg_count[(int) SH_ARG_INT] < NPARM_REGS (SImode)
  8189. && (! TARGET_SHCOMPACT
  8190. || (! SHCOMPACT_FORCE_ON_STACK (mode, type)
  8191. && ! SH5_WOULD_BE_PARTIAL_NREGS (*ca, mode,
  8192. type, named))))
  8193. {
  8194. return gen_rtx_REG (mode, (FIRST_PARM_REG
  8195. + ca->arg_count[(int) SH_ARG_INT]));
  8196. }
  8197. return NULL_RTX;
  8198. }
  8199. return NULL_RTX;
  8200. }
  8201. /* Update the data in CUM to advance over an argument
  8202. of mode MODE and data type TYPE.
  8203. (TYPE is null for libcalls where that information may not be
  8204. available.) */
  8205. static void
  8206. sh_function_arg_advance (cumulative_args_t ca_v, machine_mode mode,
  8207. const_tree type, bool named)
  8208. {
  8209. CUMULATIVE_ARGS *ca = get_cumulative_args (ca_v);
  8210. if (ca->force_mem)
  8211. ca->force_mem = 0;
  8212. else if (TARGET_SH5)
  8213. {
  8214. const_tree type2 = (ca->byref && type
  8215. ? TREE_TYPE (type)
  8216. : type);
  8217. machine_mode mode2 = (ca->byref && type
  8218. ? TYPE_MODE (type2)
  8219. : mode);
  8220. int dwords = ((ca->byref
  8221. ? ca->byref
  8222. : mode2 == BLKmode
  8223. ? int_size_in_bytes (type2)
  8224. : GET_MODE_SIZE (mode2)) + 7) / 8;
  8225. int numregs = MIN (dwords, NPARM_REGS (SImode)
  8226. - ca->arg_count[(int) SH_ARG_INT]);
  8227. if (numregs)
  8228. {
  8229. ca->arg_count[(int) SH_ARG_INT] += numregs;
  8230. if (TARGET_SHCOMPACT
  8231. && SHCOMPACT_FORCE_ON_STACK (mode2, type2))
  8232. {
  8233. ca->call_cookie
  8234. |= CALL_COOKIE_INT_REG (ca->arg_count[(int) SH_ARG_INT]
  8235. - numregs, 1);
  8236. /* N.B. We want this also for outgoing. */
  8237. ca->stack_regs += numregs;
  8238. }
  8239. else if (ca->byref)
  8240. {
  8241. if (! ca->outgoing)
  8242. ca->stack_regs += numregs;
  8243. ca->byref_regs += numregs;
  8244. ca->byref = 0;
  8245. do
  8246. ca->call_cookie
  8247. |= CALL_COOKIE_INT_REG (ca->arg_count[(int) SH_ARG_INT]
  8248. - numregs, 2);
  8249. while (--numregs);
  8250. ca->call_cookie
  8251. |= CALL_COOKIE_INT_REG (ca->arg_count[(int) SH_ARG_INT]
  8252. - 1, 1);
  8253. }
  8254. else if (dwords > numregs)
  8255. {
  8256. int pushregs = numregs;
  8257. if (TARGET_SHCOMPACT)
  8258. ca->stack_regs += numregs;
  8259. while (pushregs < NPARM_REGS (SImode) - 1
  8260. && (CALL_COOKIE_INT_REG_GET
  8261. (ca->call_cookie,
  8262. NPARM_REGS (SImode) - pushregs)
  8263. == 1))
  8264. {
  8265. ca->call_cookie
  8266. &= ~ CALL_COOKIE_INT_REG (NPARM_REGS (SImode)
  8267. - pushregs, 1);
  8268. pushregs++;
  8269. }
  8270. if (numregs == NPARM_REGS (SImode))
  8271. ca->call_cookie
  8272. |= CALL_COOKIE_INT_REG (0, 1)
  8273. | CALL_COOKIE_STACKSEQ (numregs - 1);
  8274. else
  8275. ca->call_cookie
  8276. |= CALL_COOKIE_STACKSEQ (numregs);
  8277. }
  8278. }
  8279. if (GET_SH_ARG_CLASS (mode2) == SH_ARG_FLOAT
  8280. && (named || ! ca->prototype_p))
  8281. {
  8282. if (mode2 == SFmode && ca->free_single_fp_reg)
  8283. ca->free_single_fp_reg = 0;
  8284. else if (ca->arg_count[(int) SH_ARG_FLOAT]
  8285. < NPARM_REGS (SFmode))
  8286. {
  8287. int numfpregs
  8288. = MIN ((GET_MODE_SIZE (mode2) + 7) / 8 * 2,
  8289. NPARM_REGS (SFmode)
  8290. - ca->arg_count[(int) SH_ARG_FLOAT]);
  8291. ca->arg_count[(int) SH_ARG_FLOAT] += numfpregs;
  8292. if (TARGET_SHCOMPACT && ! ca->prototype_p)
  8293. {
  8294. if (ca->outgoing && numregs > 0)
  8295. do
  8296. {
  8297. ca->call_cookie
  8298. |= (CALL_COOKIE_INT_REG
  8299. (ca->arg_count[(int) SH_ARG_INT]
  8300. - numregs + ((numfpregs - 2) / 2),
  8301. 4 + (ca->arg_count[(int) SH_ARG_FLOAT]
  8302. - numfpregs) / 2));
  8303. }
  8304. while (numfpregs -= 2);
  8305. }
  8306. else if (mode2 == SFmode && (named)
  8307. && (ca->arg_count[(int) SH_ARG_FLOAT]
  8308. < NPARM_REGS (SFmode)))
  8309. ca->free_single_fp_reg
  8310. = FIRST_FP_PARM_REG - numfpregs
  8311. + ca->arg_count[(int) SH_ARG_FLOAT] + 1;
  8312. }
  8313. }
  8314. return;
  8315. }
  8316. if ((TARGET_HITACHI || ca->renesas_abi) && TARGET_FPU_DOUBLE)
  8317. {
  8318. /* Note that we've used the skipped register. */
  8319. if (mode == SFmode && ca->free_single_fp_reg)
  8320. {
  8321. ca->free_single_fp_reg = 0;
  8322. return;
  8323. }
  8324. /* When we have a DF after an SF, there's an SF register that get
  8325. skipped in order to align the DF value. We note this skipped
  8326. register, because the next SF value will use it, and not the
  8327. SF that follows the DF. */
  8328. if (mode == DFmode
  8329. && sh_round_reg (*ca, DFmode) != sh_round_reg (*ca, SFmode))
  8330. {
  8331. ca->free_single_fp_reg = (sh_round_reg (*ca, SFmode)
  8332. + BASE_ARG_REG (mode));
  8333. }
  8334. }
  8335. if (! ((TARGET_SH4 || TARGET_SH2A) || ca->renesas_abi)
  8336. || sh_pass_in_reg_p (*ca, mode, type))
  8337. (ca->arg_count[(int) GET_SH_ARG_CLASS (mode)]
  8338. = (sh_round_reg (*ca, mode)
  8339. + (mode == BLKmode
  8340. ? CEIL (int_size_in_bytes (type), UNITS_PER_WORD)
  8341. : CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD))));
  8342. }
  8343. /* The Renesas calling convention doesn't quite fit into this scheme since
  8344. the address is passed like an invisible argument, but one that is always
  8345. passed in memory. */
  8346. static rtx
  8347. sh_struct_value_rtx (tree fndecl, int incoming ATTRIBUTE_UNUSED)
  8348. {
  8349. if (TARGET_HITACHI || sh_attr_renesas_p (fndecl))
  8350. return NULL_RTX;
  8351. return gen_rtx_REG (Pmode, 2);
  8352. }
  8353. /* Worker function for TARGET_FUNCTION_VALUE.
  8354. For the SH, this is like LIBCALL_VALUE, except that we must change the
  8355. mode like PROMOTE_MODE does.
  8356. ??? PROMOTE_MODE is ignored for non-scalar types. The set of types
  8357. tested here has to be kept in sync with the one in
  8358. explow.c:promote_mode. */
  8359. static rtx
  8360. sh_function_value (const_tree valtype,
  8361. const_tree fn_decl_or_type,
  8362. bool outgoing ATTRIBUTE_UNUSED)
  8363. {
  8364. if (fn_decl_or_type
  8365. && !DECL_P (fn_decl_or_type))
  8366. fn_decl_or_type = NULL;
  8367. return gen_rtx_REG (
  8368. ((GET_MODE_CLASS (TYPE_MODE (valtype)) == MODE_INT
  8369. && GET_MODE_SIZE (TYPE_MODE (valtype)) < 4
  8370. && (TREE_CODE (valtype) == INTEGER_TYPE
  8371. || TREE_CODE (valtype) == ENUMERAL_TYPE
  8372. || TREE_CODE (valtype) == BOOLEAN_TYPE
  8373. || TREE_CODE (valtype) == REAL_TYPE
  8374. || TREE_CODE (valtype) == OFFSET_TYPE))
  8375. && sh_promote_prototypes (fn_decl_or_type)
  8376. ? (TARGET_SHMEDIA64 ? DImode : SImode) : TYPE_MODE (valtype)),
  8377. BASE_RETURN_VALUE_REG (TYPE_MODE (valtype)));
  8378. }
  8379. /* Worker function for TARGET_LIBCALL_VALUE. */
  8380. static rtx
  8381. sh_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
  8382. {
  8383. return gen_rtx_REG (mode, BASE_RETURN_VALUE_REG (mode));
  8384. }
  8385. /* Return true if N is a possible register number of function value. */
  8386. static bool
  8387. sh_function_value_regno_p (const unsigned int regno)
  8388. {
  8389. return ((regno) == FIRST_RET_REG
  8390. || (TARGET_SH2E && (regno) == FIRST_FP_RET_REG)
  8391. || (TARGET_SHMEDIA_FPU && (regno) == FIRST_FP_RET_REG));
  8392. }
  8393. /* Worker function for TARGET_RETURN_IN_MEMORY. */
  8394. static bool
  8395. sh_return_in_memory (const_tree type, const_tree fndecl)
  8396. {
  8397. if (TARGET_SH5)
  8398. {
  8399. if (TYPE_MODE (type) == BLKmode)
  8400. return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)) > 8;
  8401. else
  8402. return GET_MODE_SIZE (TYPE_MODE (type)) > 8;
  8403. }
  8404. else
  8405. {
  8406. return (TYPE_MODE (type) == BLKmode
  8407. || ((TARGET_HITACHI || sh_attr_renesas_p (fndecl))
  8408. && TREE_CODE (type) == RECORD_TYPE));
  8409. }
  8410. }
  8411. /* We actually emit the code in sh_expand_prologue. We used to use
  8412. a static variable to flag that we need to emit this code, but that
  8413. doesn't when inlining, when functions are deferred and then emitted
  8414. later. Fortunately, we already have two flags that are part of struct
  8415. function that tell if a function uses varargs or stdarg. */
  8416. static void
  8417. sh_setup_incoming_varargs (cumulative_args_t ca,
  8418. machine_mode mode,
  8419. tree type,
  8420. int *pretend_arg_size,
  8421. int second_time ATTRIBUTE_UNUSED)
  8422. {
  8423. gcc_assert (cfun->stdarg);
  8424. if (TARGET_VARARGS_PRETEND_ARGS (current_function_decl))
  8425. {
  8426. int named_parm_regs, anon_parm_regs;
  8427. named_parm_regs = (sh_round_reg (*get_cumulative_args (ca), mode)
  8428. + (mode == BLKmode
  8429. ? CEIL (int_size_in_bytes (type), UNITS_PER_WORD)
  8430. : CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD)));
  8431. anon_parm_regs = NPARM_REGS (SImode) - named_parm_regs;
  8432. if (anon_parm_regs > 0)
  8433. *pretend_arg_size = anon_parm_regs * 4;
  8434. }
  8435. }
  8436. static bool
  8437. sh_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
  8438. {
  8439. return TARGET_SH5;
  8440. }
  8441. static bool
  8442. sh_pretend_outgoing_varargs_named (cumulative_args_t ca_v)
  8443. {
  8444. CUMULATIVE_ARGS *ca = get_cumulative_args (ca_v);
  8445. return ! (TARGET_HITACHI || ca->renesas_abi) && ! TARGET_SH5;
  8446. }
  8447. /* Define the offset between two registers, one to be eliminated, and
  8448. the other its replacement, at the start of a routine. */
  8449. int
  8450. initial_elimination_offset (int from, int to)
  8451. {
  8452. int regs_saved;
  8453. int regs_saved_rounding = 0;
  8454. int total_saved_regs_space;
  8455. int total_auto_space;
  8456. int save_flags = target_flags;
  8457. int copy_flags;
  8458. HARD_REG_SET live_regs_mask;
  8459. shmedia_space_reserved_for_target_registers = false;
  8460. regs_saved = calc_live_regs (&live_regs_mask);
  8461. regs_saved += SHMEDIA_REGS_STACK_ADJUST ();
  8462. if (shmedia_reserve_space_for_target_registers_p (regs_saved, &live_regs_mask))
  8463. {
  8464. shmedia_space_reserved_for_target_registers = true;
  8465. regs_saved += shmedia_target_regs_stack_adjust (&live_regs_mask);
  8466. }
  8467. if (TARGET_SH5 && regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT))
  8468. regs_saved_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
  8469. - regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT));
  8470. total_auto_space = rounded_frame_size (regs_saved) - regs_saved_rounding;
  8471. copy_flags = target_flags;
  8472. target_flags = save_flags;
  8473. total_saved_regs_space = regs_saved + regs_saved_rounding;
  8474. if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
  8475. return total_saved_regs_space + total_auto_space
  8476. + crtl->args.info.byref_regs * 8;
  8477. if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
  8478. return total_saved_regs_space + total_auto_space
  8479. + crtl->args.info.byref_regs * 8;
  8480. /* Initial gap between fp and sp is 0. */
  8481. if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
  8482. return 0;
  8483. if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
  8484. return rounded_frame_size (0);
  8485. if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
  8486. return rounded_frame_size (0);
  8487. gcc_assert (from == RETURN_ADDRESS_POINTER_REGNUM
  8488. && (to == HARD_FRAME_POINTER_REGNUM
  8489. || to == STACK_POINTER_REGNUM));
  8490. if (TARGET_SH5)
  8491. {
  8492. int n = total_saved_regs_space;
  8493. int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
  8494. save_schedule schedule;
  8495. save_entry *entry;
  8496. n += total_auto_space;
  8497. /* If it wasn't saved, there's not much we can do. */
  8498. if (! TEST_HARD_REG_BIT (live_regs_mask, pr_reg))
  8499. return n;
  8500. target_flags = copy_flags;
  8501. sh5_schedule_saves (&live_regs_mask, &schedule, n);
  8502. for (entry = &schedule.entries[1]; entry->mode != VOIDmode; entry++)
  8503. if (entry->reg == pr_reg)
  8504. {
  8505. target_flags = save_flags;
  8506. return entry->offset;
  8507. }
  8508. gcc_unreachable ();
  8509. }
  8510. else
  8511. return total_auto_space;
  8512. }
  8513. /* Parse the -mfixed-range= option string. */
  8514. void
  8515. sh_fix_range (const char *const_str)
  8516. {
  8517. int i, first, last;
  8518. char *str, *dash, *comma;
  8519. /* str must be of the form REG1'-'REG2{,REG1'-'REG} where REG1 and
  8520. REG2 are either register names or register numbers. The effect
  8521. of this option is to mark the registers in the range from REG1 to
  8522. REG2 as ``fixed'' so they won't be used by the compiler. */
  8523. i = strlen (const_str);
  8524. str = (char *) alloca (i + 1);
  8525. memcpy (str, const_str, i + 1);
  8526. while (1)
  8527. {
  8528. dash = strchr (str, '-');
  8529. if (!dash)
  8530. {
  8531. warning (0, "value of -mfixed-range must have form REG1-REG2");
  8532. return;
  8533. }
  8534. *dash = '\0';
  8535. comma = strchr (dash + 1, ',');
  8536. if (comma)
  8537. *comma = '\0';
  8538. first = decode_reg_name (str);
  8539. if (first < 0)
  8540. {
  8541. warning (0, "unknown register name: %s", str);
  8542. return;
  8543. }
  8544. last = decode_reg_name (dash + 1);
  8545. if (last < 0)
  8546. {
  8547. warning (0, "unknown register name: %s", dash + 1);
  8548. return;
  8549. }
  8550. *dash = '-';
  8551. if (first > last)
  8552. {
  8553. warning (0, "%s-%s is an empty range", str, dash + 1);
  8554. return;
  8555. }
  8556. for (i = first; i <= last; ++i)
  8557. fixed_regs[i] = call_used_regs[i] = 1;
  8558. if (!comma)
  8559. break;
  8560. *comma = ',';
  8561. str = comma + 1;
  8562. }
  8563. }
  8564. /* Insert any deferred function attributes from earlier pragmas. */
  8565. static void
  8566. sh_insert_attributes (tree node, tree *attributes)
  8567. {
  8568. tree attrs;
  8569. if (TREE_CODE (node) != FUNCTION_DECL)
  8570. return;
  8571. /* We are only interested in fields. */
  8572. if (!DECL_P (node))
  8573. return;
  8574. /* Append the attributes to the deferred attributes. */
  8575. *sh_deferred_function_attributes_tail = *attributes;
  8576. attrs = sh_deferred_function_attributes;
  8577. if (!attrs)
  8578. return;
  8579. /* Some attributes imply or require the interrupt attribute. */
  8580. if (!lookup_attribute ("interrupt_handler", attrs)
  8581. && !lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (node)))
  8582. {
  8583. /* If we have a trapa_handler, but no interrupt_handler attribute,
  8584. insert an interrupt_handler attribute. */
  8585. if (lookup_attribute ("trapa_handler", attrs) != NULL_TREE)
  8586. /* We can't use sh_pr_interrupt here because that's not in the
  8587. java frontend. */
  8588. attrs
  8589. = tree_cons (get_identifier("interrupt_handler"), NULL_TREE, attrs);
  8590. /* However, for sp_switch, trap_exit, nosave_low_regs and resbank,
  8591. if the interrupt attribute is missing, we ignore the attribute
  8592. and warn. */
  8593. else if (lookup_attribute ("sp_switch", attrs)
  8594. || lookup_attribute ("trap_exit", attrs)
  8595. || lookup_attribute ("nosave_low_regs", attrs)
  8596. || lookup_attribute ("resbank", attrs))
  8597. {
  8598. tree *tail;
  8599. for (tail = attributes; attrs; attrs = TREE_CHAIN (attrs))
  8600. {
  8601. if (is_attribute_p ("sp_switch", TREE_PURPOSE (attrs))
  8602. || is_attribute_p ("trap_exit", TREE_PURPOSE (attrs))
  8603. || is_attribute_p ("nosave_low_regs", TREE_PURPOSE (attrs))
  8604. || is_attribute_p ("resbank", TREE_PURPOSE (attrs)))
  8605. warning (OPT_Wattributes,
  8606. "%qE attribute only applies to interrupt functions",
  8607. TREE_PURPOSE (attrs));
  8608. else
  8609. {
  8610. *tail = tree_cons (TREE_PURPOSE (attrs), NULL_TREE,
  8611. NULL_TREE);
  8612. tail = &TREE_CHAIN (*tail);
  8613. }
  8614. }
  8615. attrs = *attributes;
  8616. }
  8617. }
  8618. /* Install the processed list. */
  8619. *attributes = attrs;
  8620. /* Clear deferred attributes. */
  8621. sh_deferred_function_attributes = NULL_TREE;
  8622. sh_deferred_function_attributes_tail = &sh_deferred_function_attributes;
  8623. return;
  8624. }
  8625. /*------------------------------------------------------------------------------
  8626. Target specific attributes
  8627. Supported attributes are:
  8628. * interrupt_handler
  8629. Specifies this function is an interrupt handler.
  8630. * trapa_handler
  8631. Like interrupt_handler, but don't save all registers.
  8632. * sp_switch
  8633. Specifies an alternate stack for an interrupt handler to run on.
  8634. * trap_exit
  8635. Use a trapa to exit an interrupt function instead of rte.
  8636. * nosave_low_regs
  8637. Don't save r0..r7 in an interrupt handler function.
  8638. This is useful on SH3* and SH4*, which have a separate set of low
  8639. regs for user and privileged modes.
  8640. This is mainly to be used for non-reentrant interrupt handlers (i.e.
  8641. those that run with interrupts disabled and thus can't be
  8642. interrupted thenselves).
  8643. * renesas
  8644. Use Renesas calling/layout conventions (functions and structures).
  8645. * resbank
  8646. In case of an interrupt handler function, use a register bank to
  8647. save registers R0-R14, MACH, MACL, GBR and PR.
  8648. This is available only on SH2A targets.
  8649. * function_vector
  8650. Declares a function to be called using the TBR relative addressing
  8651. mode. Takes an argument that specifies the slot number in the table
  8652. where this function can be looked up by the JSR/N @@(disp8,TBR) insn.
  8653. */
  8654. /* Handle a 'resbank' attribute. */
  8655. static tree
  8656. sh_handle_resbank_handler_attribute (tree * node, tree name,
  8657. tree args ATTRIBUTE_UNUSED,
  8658. int flags ATTRIBUTE_UNUSED,
  8659. bool * no_add_attrs)
  8660. {
  8661. if (!TARGET_SH2A)
  8662. {
  8663. warning (OPT_Wattributes, "%qE attribute is supported only for SH2A",
  8664. name);
  8665. *no_add_attrs = true;
  8666. }
  8667. if (TREE_CODE (*node) != FUNCTION_DECL)
  8668. {
  8669. warning (OPT_Wattributes, "%qE attribute only applies to functions",
  8670. name);
  8671. *no_add_attrs = true;
  8672. }
  8673. return NULL_TREE;
  8674. }
  8675. /* Handle an "interrupt_handler" attribute; arguments as in
  8676. struct attribute_spec.handler. */
  8677. static tree
  8678. sh_handle_interrupt_handler_attribute (tree *node, tree name,
  8679. tree args ATTRIBUTE_UNUSED,
  8680. int flags ATTRIBUTE_UNUSED,
  8681. bool *no_add_attrs)
  8682. {
  8683. if (TREE_CODE (*node) != FUNCTION_DECL)
  8684. {
  8685. warning (OPT_Wattributes, "%qE attribute only applies to functions",
  8686. name);
  8687. *no_add_attrs = true;
  8688. }
  8689. else if (TARGET_SHCOMPACT)
  8690. {
  8691. error ("attribute interrupt_handler is not compatible with -m5-compact");
  8692. *no_add_attrs = true;
  8693. }
  8694. return NULL_TREE;
  8695. }
  8696. /* Handle an 'function_vector' attribute; arguments as in
  8697. struct attribute_spec.handler. */
  8698. static tree
  8699. sh2a_handle_function_vector_handler_attribute (tree * node, tree name,
  8700. tree args ATTRIBUTE_UNUSED,
  8701. int flags ATTRIBUTE_UNUSED,
  8702. bool * no_add_attrs)
  8703. {
  8704. if (!TARGET_SH2A)
  8705. {
  8706. warning (OPT_Wattributes, "%qE attribute only applies to SH2A",
  8707. name);
  8708. *no_add_attrs = true;
  8709. }
  8710. else if (TREE_CODE (*node) != FUNCTION_DECL)
  8711. {
  8712. warning (OPT_Wattributes, "%qE attribute only applies to functions",
  8713. name);
  8714. *no_add_attrs = true;
  8715. }
  8716. else if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
  8717. {
  8718. /* The argument must be a constant integer. */
  8719. warning (OPT_Wattributes,
  8720. "%qE attribute argument not an integer constant",
  8721. name);
  8722. *no_add_attrs = true;
  8723. }
  8724. else if (TREE_INT_CST_LOW (TREE_VALUE (args)) > 255)
  8725. {
  8726. /* The argument value must be between 0 to 255. */
  8727. warning (OPT_Wattributes,
  8728. "%qE attribute argument should be between 0 to 255",
  8729. name);
  8730. *no_add_attrs = true;
  8731. }
  8732. return NULL_TREE;
  8733. }
  8734. /* Returns true if current function has been assigned the attribute
  8735. 'function_vector'. */
  8736. bool
  8737. sh2a_is_function_vector_call (rtx x)
  8738. {
  8739. if (GET_CODE (x) == SYMBOL_REF
  8740. && (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_FUNCVEC_FUNCTION))
  8741. {
  8742. tree tr = SYMBOL_REF_DECL (x);
  8743. if (sh2a_function_vector_p (tr))
  8744. return true;
  8745. }
  8746. return false;
  8747. }
  8748. /* Returns the function vector number, if the attribute
  8749. 'function_vector' is assigned, otherwise returns zero. */
  8750. int
  8751. sh2a_get_function_vector_number (rtx x)
  8752. {
  8753. int num;
  8754. tree list, t;
  8755. if ((GET_CODE (x) == SYMBOL_REF)
  8756. && (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_FUNCVEC_FUNCTION))
  8757. {
  8758. t = SYMBOL_REF_DECL (x);
  8759. if (TREE_CODE (t) != FUNCTION_DECL)
  8760. return 0;
  8761. list = SH_ATTRIBUTES (t);
  8762. while (list)
  8763. {
  8764. if (is_attribute_p ("function_vector", TREE_PURPOSE (list)))
  8765. {
  8766. num = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (list)));
  8767. return num;
  8768. }
  8769. list = TREE_CHAIN (list);
  8770. }
  8771. return 0;
  8772. }
  8773. else
  8774. return 0;
  8775. }
  8776. /* Handle an "sp_switch" attribute; arguments as in
  8777. struct attribute_spec.handler. */
  8778. static tree
  8779. sh_handle_sp_switch_attribute (tree *node, tree name, tree args,
  8780. int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
  8781. {
  8782. if (TREE_CODE (*node) != FUNCTION_DECL)
  8783. {
  8784. warning (OPT_Wattributes, "%qE attribute only applies to functions",
  8785. name);
  8786. *no_add_attrs = true;
  8787. }
  8788. else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
  8789. {
  8790. /* The argument must be a constant string. */
  8791. warning (OPT_Wattributes, "%qE attribute argument not a string constant",
  8792. name);
  8793. *no_add_attrs = true;
  8794. }
  8795. return NULL_TREE;
  8796. }
  8797. /* Handle an "trap_exit" attribute; arguments as in
  8798. struct attribute_spec.handler. */
  8799. static tree
  8800. sh_handle_trap_exit_attribute (tree *node, tree name, tree args,
  8801. int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
  8802. {
  8803. if (TREE_CODE (*node) != FUNCTION_DECL)
  8804. {
  8805. warning (OPT_Wattributes, "%qE attribute only applies to functions",
  8806. name);
  8807. *no_add_attrs = true;
  8808. }
  8809. /* The argument specifies a trap number to be used in a trapa instruction
  8810. at function exit (instead of an rte instruction). */
  8811. else if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
  8812. {
  8813. /* The argument must be a constant integer. */
  8814. warning (OPT_Wattributes, "%qE attribute argument not an "
  8815. "integer constant", name);
  8816. *no_add_attrs = true;
  8817. }
  8818. return NULL_TREE;
  8819. }
  8820. static tree
  8821. sh_handle_renesas_attribute (tree *node ATTRIBUTE_UNUSED,
  8822. tree name ATTRIBUTE_UNUSED,
  8823. tree args ATTRIBUTE_UNUSED,
  8824. int flags ATTRIBUTE_UNUSED,
  8825. bool *no_add_attrs ATTRIBUTE_UNUSED)
  8826. {
  8827. return NULL_TREE;
  8828. }
  8829. /* True if __attribute__((renesas)) or -mrenesas. */
  8830. bool
  8831. sh_attr_renesas_p (const_tree td)
  8832. {
  8833. if (TARGET_HITACHI)
  8834. return true;
  8835. if (td == NULL_TREE)
  8836. return false;
  8837. if (DECL_P (td))
  8838. td = TREE_TYPE (td);
  8839. if (td == error_mark_node)
  8840. return false;
  8841. return (lookup_attribute ("renesas", TYPE_ATTRIBUTES (td))
  8842. != NULL_TREE);
  8843. }
  8844. /* True if __attribute__((renesas)) or -mrenesas, for the current
  8845. function. */
  8846. bool
  8847. sh_cfun_attr_renesas_p (void)
  8848. {
  8849. return sh_attr_renesas_p (current_function_decl);
  8850. }
  8851. /* Returns true if the current function has the "interrupt_handler"
  8852. attribute set. */
  8853. bool
  8854. sh_cfun_interrupt_handler_p (void)
  8855. {
  8856. return (lookup_attribute ("interrupt_handler",
  8857. DECL_ATTRIBUTES (current_function_decl))
  8858. != NULL_TREE);
  8859. }
  8860. /* Returns true if FUNC has been assigned the attribute
  8861. "function_vector". */
  8862. bool
  8863. sh2a_function_vector_p (tree func)
  8864. {
  8865. tree list;
  8866. if (TREE_CODE (func) != FUNCTION_DECL)
  8867. return false;
  8868. list = SH_ATTRIBUTES (func);
  8869. while (list)
  8870. {
  8871. if (is_attribute_p ("function_vector", TREE_PURPOSE (list)))
  8872. return true;
  8873. list = TREE_CHAIN (list);
  8874. }
  8875. return false;
  8876. }
  8877. /* Returns true if given tree has the "resbank" attribute set. */
  8878. bool
  8879. sh_cfun_resbank_handler_p (void)
  8880. {
  8881. return ((lookup_attribute ("resbank",
  8882. DECL_ATTRIBUTES (current_function_decl))
  8883. != NULL_TREE)
  8884. && (lookup_attribute ("interrupt_handler",
  8885. DECL_ATTRIBUTES (current_function_decl))
  8886. != NULL_TREE) && TARGET_SH2A);
  8887. }
  8888. /* Returns true if the current function has a "trap_exit" attribute set. */
  8889. bool
  8890. sh_cfun_trap_exit_p (void)
  8891. {
  8892. return lookup_attribute ("trap_exit", DECL_ATTRIBUTES (current_function_decl))
  8893. != NULL_TREE;
  8894. }
  8895. /* Implement TARGET_CHECK_PCH_TARGET_FLAGS. */
  8896. static const char *
  8897. sh_check_pch_target_flags (int old_flags)
  8898. {
  8899. if ((old_flags ^ target_flags) & (MASK_SH1 | MASK_SH2 | MASK_SH3
  8900. | MASK_SH_E | MASK_HARD_SH4
  8901. | MASK_FPU_SINGLE | MASK_SH4))
  8902. return _("created and used with different architectures / ABIs");
  8903. if ((old_flags ^ target_flags) & MASK_HITACHI)
  8904. return _("created and used with different ABIs");
  8905. if ((old_flags ^ target_flags) & MASK_LITTLE_ENDIAN)
  8906. return _("created and used with different endianness");
  8907. return NULL;
  8908. }
  8909. /* Predicates used by the templates. */
  8910. /* Returns true if OP is MACL, MACH or PR. The input must be a REG rtx.
  8911. Used only in general_movsrc_operand. */
  8912. bool
  8913. system_reg_operand (rtx op, machine_mode mode ATTRIBUTE_UNUSED)
  8914. {
  8915. switch (REGNO (op))
  8916. {
  8917. case PR_REG:
  8918. case MACL_REG:
  8919. case MACH_REG:
  8920. return true;
  8921. }
  8922. return false;
  8923. }
  8924. /* Returns true if OP is a floating point value with value 0.0. */
  8925. bool
  8926. fp_zero_operand (rtx op)
  8927. {
  8928. REAL_VALUE_TYPE r;
  8929. if (GET_MODE (op) != SFmode)
  8930. return false;
  8931. REAL_VALUE_FROM_CONST_DOUBLE (r, op);
  8932. return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
  8933. }
  8934. /* Returns true if OP is a floating point value with value 1.0. */
  8935. bool
  8936. fp_one_operand (rtx op)
  8937. {
  8938. REAL_VALUE_TYPE r;
  8939. if (GET_MODE (op) != SFmode)
  8940. return false;
  8941. REAL_VALUE_FROM_CONST_DOUBLE (r, op);
  8942. return REAL_VALUES_EQUAL (r, dconst1);
  8943. }
  8944. /* Return the TLS type for TLS symbols. */
  8945. enum tls_model
  8946. tls_symbolic_operand (rtx op, machine_mode mode ATTRIBUTE_UNUSED)
  8947. {
  8948. if (GET_CODE (op) != SYMBOL_REF)
  8949. return TLS_MODEL_NONE;
  8950. return SYMBOL_REF_TLS_MODEL (op);
  8951. }
  8952. /* Return the destination address of a branch. */
  8953. static int
  8954. branch_dest (rtx branch)
  8955. {
  8956. rtx dest = SET_SRC (PATTERN (branch));
  8957. int dest_uid;
  8958. if (GET_CODE (dest) == IF_THEN_ELSE)
  8959. dest = XEXP (dest, 1);
  8960. dest = XEXP (dest, 0);
  8961. dest_uid = INSN_UID (dest);
  8962. return INSN_ADDRESSES (dest_uid);
  8963. }
  8964. /* Return nonzero if REG is not used after INSN.
  8965. We assume REG is a reload reg, and therefore does
  8966. not live past labels. It may live past calls or jumps though. */
  8967. bool
  8968. reg_unused_after (rtx reg, rtx_insn *insn)
  8969. {
  8970. enum rtx_code code;
  8971. rtx set;
  8972. /* If the reg is set by this instruction, then it is safe for our
  8973. case. Disregard the case where this is a store to memory, since
  8974. we are checking a register used in the store address. */
  8975. set = single_set (insn);
  8976. if (set && !MEM_P (SET_DEST (set))
  8977. && reg_overlap_mentioned_p (reg, SET_DEST (set)))
  8978. return true;
  8979. while ((insn = NEXT_INSN (insn)))
  8980. {
  8981. rtx set;
  8982. if (!INSN_P (insn))
  8983. continue;
  8984. code = GET_CODE (insn);
  8985. #if 0
  8986. /* If this is a label that existed before reload, then the register
  8987. is dead here. However, if this is a label added by reorg, then
  8988. the register may still be live here. We can't tell the difference,
  8989. so we just ignore labels completely. */
  8990. if (code == CODE_LABEL)
  8991. return 1;
  8992. /* else */
  8993. #endif
  8994. if (code == JUMP_INSN)
  8995. return false;
  8996. /* If this is a sequence, we must handle them all at once.
  8997. We could have for instance a call that sets the target register,
  8998. and an insn in a delay slot that uses the register. In this case,
  8999. we must return 0. */
  9000. else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
  9001. {
  9002. rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
  9003. int i;
  9004. int retval = 0;
  9005. for (i = 0; i < seq->len (); i++)
  9006. {
  9007. rtx_insn *this_insn = seq->insn (i);
  9008. rtx set = single_set (this_insn);
  9009. if (CALL_P (this_insn))
  9010. code = CALL_INSN;
  9011. else if (JUMP_P (this_insn))
  9012. {
  9013. if (INSN_ANNULLED_BRANCH_P (this_insn))
  9014. return false;
  9015. code = JUMP_INSN;
  9016. }
  9017. if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
  9018. return false;
  9019. if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
  9020. {
  9021. if (!MEM_P (SET_DEST (set)))
  9022. retval = true;
  9023. else
  9024. return false;
  9025. }
  9026. if (set == NULL_RTX
  9027. && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
  9028. return false;
  9029. }
  9030. if (retval == 1)
  9031. return true;
  9032. else if (code == JUMP_INSN)
  9033. return false;
  9034. }
  9035. set = single_set (insn);
  9036. if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
  9037. return false;
  9038. if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
  9039. return !MEM_P (SET_DEST (set));
  9040. if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
  9041. return false;
  9042. if (code == CALL_INSN && call_really_used_regs[REGNO (reg)])
  9043. return true;
  9044. }
  9045. return true;
  9046. }
  9047. static GTY(()) rtx t_reg_rtx;
  9048. rtx
  9049. get_t_reg_rtx (void)
  9050. {
  9051. if (! t_reg_rtx)
  9052. t_reg_rtx = gen_rtx_REG (SImode, T_REG);
  9053. return t_reg_rtx;
  9054. }
  9055. static GTY(()) tree fpscr_values;
  9056. static void
  9057. emit_fpu_switch (rtx scratch, int index)
  9058. {
  9059. rtx src;
  9060. if (fpscr_values == NULL)
  9061. {
  9062. tree t;
  9063. t = build_index_type (integer_one_node);
  9064. t = build_array_type (integer_type_node, t);
  9065. t = build_decl (BUILTINS_LOCATION,
  9066. VAR_DECL, get_identifier ("__fpscr_values"), t);
  9067. DECL_ARTIFICIAL (t) = 1;
  9068. DECL_IGNORED_P (t) = 1;
  9069. DECL_EXTERNAL (t) = 1;
  9070. TREE_STATIC (t) = 1;
  9071. TREE_PUBLIC (t) = 1;
  9072. TREE_USED (t) = 1;
  9073. fpscr_values = t;
  9074. }
  9075. src = DECL_RTL (fpscr_values);
  9076. if (!can_create_pseudo_p ())
  9077. {
  9078. emit_move_insn (scratch, XEXP (src, 0));
  9079. if (index != 0)
  9080. emit_insn (gen_addsi3 (scratch, scratch, GEN_INT (index * 4)));
  9081. src = adjust_automodify_address (src, SImode, scratch, index * 4);
  9082. }
  9083. else
  9084. src = adjust_address (src, SImode, index * 4);
  9085. emit_insn (gen_lds_fpscr (src));
  9086. }
  9087. static rtx get_free_reg (HARD_REG_SET);
  9088. /* This function returns a register to use to load the address to load
  9089. the fpscr from. Currently it always returns r1 or r7, but when we are
  9090. able to use pseudo registers after combine, or have a better mechanism
  9091. for choosing a register, it should be done here. */
  9092. /* REGS_LIVE is the liveness information for the point for which we
  9093. need this allocation. In some bare-bones exit blocks, r1 is live at the
  9094. start. We can even have all of r0..r3 being live:
  9095. __complex__ long long f (double d) { if (d == 0) return 2; else return 3; }
  9096. INSN before which new insns are placed with will clobber the register
  9097. we return. If a basic block consists only of setting the return value
  9098. register to a pseudo and using that register, the return value is not
  9099. live before or after this block, yet we we'll insert our insns right in
  9100. the middle. */
  9101. static rtx
  9102. get_free_reg (HARD_REG_SET regs_live)
  9103. {
  9104. if (! TEST_HARD_REG_BIT (regs_live, 1))
  9105. return gen_rtx_REG (Pmode, 1);
  9106. /* Hard reg 1 is live; since this is a small register classes target,
  9107. there shouldn't be anything but a jump before the function end. */
  9108. gcc_assert (!TEST_HARD_REG_BIT (regs_live, 7));
  9109. return gen_rtx_REG (Pmode, 7);
  9110. }
  9111. /* This function will set the fpscr from memory.
  9112. MODE is the mode we are setting it to. */
  9113. void
  9114. fpscr_set_from_mem (int mode, HARD_REG_SET regs_live)
  9115. {
  9116. enum attr_fp_mode fp_mode = (enum attr_fp_mode) mode;
  9117. enum attr_fp_mode norm_mode = ACTUAL_NORMAL_MODE (FP_MODE);
  9118. rtx addr_reg;
  9119. addr_reg = !can_create_pseudo_p () ? get_free_reg (regs_live) : NULL_RTX;
  9120. emit_fpu_switch (addr_reg, fp_mode == norm_mode);
  9121. }
  9122. /* Is the given character a logical line separator for the assembler? */
  9123. #ifndef IS_ASM_LOGICAL_LINE_SEPARATOR
  9124. #define IS_ASM_LOGICAL_LINE_SEPARATOR(C, STR) ((C) == ';')
  9125. #endif
  9126. static bool
  9127. sequence_insn_p (rtx_insn *insn)
  9128. {
  9129. rtx_insn *prev, *next;
  9130. prev = PREV_INSN (insn);
  9131. if (prev == NULL)
  9132. return false;
  9133. next = NEXT_INSN (prev);
  9134. if (next == NULL)
  9135. return false;
  9136. return INSN_P (next) && GET_CODE (PATTERN (next)) == SEQUENCE;
  9137. }
  9138. int
  9139. sh_insn_length_adjustment (rtx_insn *insn)
  9140. {
  9141. /* Instructions with unfilled delay slots take up an extra two bytes for
  9142. the nop in the delay slot. */
  9143. if (((NONJUMP_INSN_P (insn)
  9144. && GET_CODE (PATTERN (insn)) != USE
  9145. && GET_CODE (PATTERN (insn)) != CLOBBER)
  9146. || CALL_P (insn) || JUMP_P (insn))
  9147. && ! sequence_insn_p (insn)
  9148. && get_attr_needs_delay_slot (insn) == NEEDS_DELAY_SLOT_YES)
  9149. return 2;
  9150. /* Increase the insn length of a cbranch without a delay slot insn to
  9151. force a delay slot which will be stuffed with a nop. */
  9152. if (TARGET_CBRANCH_FORCE_DELAY_SLOT && TARGET_SH2
  9153. && JUMP_P (insn) && get_attr_type (insn) == TYPE_CBRANCH
  9154. && ! sequence_insn_p (insn))
  9155. return 2;
  9156. /* sh-dsp parallel processing insn take four bytes instead of two. */
  9157. if (NONJUMP_INSN_P (insn))
  9158. {
  9159. int sum = 0;
  9160. rtx body = PATTERN (insn);
  9161. const char *templ;
  9162. char c;
  9163. bool maybe_label = true;
  9164. if (GET_CODE (body) == ASM_INPUT)
  9165. templ = XSTR (body, 0);
  9166. else if (asm_noperands (body) >= 0)
  9167. templ
  9168. = decode_asm_operands (body, NULL, NULL, NULL, NULL, NULL);
  9169. else
  9170. return 0;
  9171. do
  9172. {
  9173. int ppi_adjust = 0;
  9174. do
  9175. c = *templ++;
  9176. while (c == ' ' || c == '\t');
  9177. /* all sh-dsp parallel-processing insns start with p.
  9178. The only non-ppi sh insn starting with p is pref.
  9179. The only ppi starting with pr is prnd. */
  9180. if ((c == 'p' || c == 'P') && strncasecmp ("re", templ, 2))
  9181. ppi_adjust = 2;
  9182. /* The repeat pseudo-insn expands two three insns, a total of
  9183. six bytes in size. */
  9184. else if ((c == 'r' || c == 'R')
  9185. && ! strncasecmp ("epeat", templ, 5))
  9186. ppi_adjust = 4;
  9187. while (c && c != '\n'
  9188. && ! IS_ASM_LOGICAL_LINE_SEPARATOR (c, templ))
  9189. {
  9190. /* If this is a label, it is obviously not a ppi insn. */
  9191. if (c == ':' && maybe_label)
  9192. {
  9193. ppi_adjust = 0;
  9194. break;
  9195. }
  9196. else if (c == '\'' || c == '"')
  9197. maybe_label = false;
  9198. c = *templ++;
  9199. }
  9200. sum += ppi_adjust;
  9201. maybe_label = c != ':';
  9202. }
  9203. while (c);
  9204. return sum;
  9205. }
  9206. return 0;
  9207. }
  9208. /* Return TRUE for a valid displacement for the REG+disp addressing
  9209. with MODE. */
  9210. bool
  9211. sh_legitimate_index_p (machine_mode mode, rtx op, bool consider_sh2a,
  9212. bool allow_zero)
  9213. {
  9214. if (! CONST_INT_P (op))
  9215. return false;
  9216. if (TARGET_SHMEDIA)
  9217. {
  9218. int size;
  9219. /* Check if this is the address of an unaligned load / store. */
  9220. if (mode == VOIDmode)
  9221. return satisfies_constraint_I06 (op);
  9222. size = GET_MODE_SIZE (mode);
  9223. return (!(INTVAL (op) & (size - 1))
  9224. && INTVAL (op) >= -512 * size
  9225. && INTVAL (op) < 512 * size);
  9226. }
  9227. else
  9228. {
  9229. const HOST_WIDE_INT offset = INTVAL (op);
  9230. const int max_disp = sh_max_mov_insn_displacement (mode, consider_sh2a);
  9231. const int align_mask = mov_insn_alignment_mask (mode, consider_sh2a);
  9232. /* If the mode does not support any displacement always return false.
  9233. Even though an index of '0' is actually always valid, it will cause
  9234. troubles when e.g. a DFmode move is split into two SFmode moves,
  9235. where one SFmode move will have index '0' and the other move will
  9236. have index '4'. */
  9237. if (!allow_zero && max_disp < 1)
  9238. return false;
  9239. return offset >= 0 && offset <= max_disp && (offset & align_mask) == 0;
  9240. }
  9241. }
  9242. /* Recognize an RTL expression that is a valid memory address for
  9243. an instruction.
  9244. The MODE argument is the machine mode for the MEM expression
  9245. that wants to use this address.
  9246. Allow REG
  9247. REG+disp
  9248. REG+r0
  9249. REG++
  9250. --REG
  9251. GBR
  9252. GBR+disp */
  9253. static bool
  9254. sh_legitimate_address_p (machine_mode mode, rtx x, bool strict)
  9255. {
  9256. if (! ALLOW_INDEXED_ADDRESS
  9257. && GET_CODE (x) == PLUS && REG_P (XEXP (x, 0)) && REG_P (XEXP (x, 1)))
  9258. return false;
  9259. if (REG_P (x) && REGNO (x) == GBR_REG)
  9260. return true;
  9261. if (MAYBE_BASE_REGISTER_RTX_P (x, strict))
  9262. return true;
  9263. else if ((GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_DEC)
  9264. && ! TARGET_SHMEDIA
  9265. && MAYBE_BASE_REGISTER_RTX_P (XEXP (x, 0), strict))
  9266. return true;
  9267. else if (GET_CODE (x) == PLUS)
  9268. {
  9269. rtx xop0 = XEXP (x, 0);
  9270. rtx xop1 = XEXP (x, 1);
  9271. if (REG_P (xop0) && REGNO (xop0) == GBR_REG)
  9272. return gbr_displacement (xop1, mode);
  9273. if (GET_MODE_SIZE (mode) <= 8
  9274. && MAYBE_BASE_REGISTER_RTX_P (xop0, strict)
  9275. && sh_legitimate_index_p (mode, xop1, TARGET_SH2A, false))
  9276. return true;
  9277. if ((ALLOW_INDEXED_ADDRESS || GET_MODE (x) == DImode
  9278. || ((xop0 == stack_pointer_rtx
  9279. || xop0 == hard_frame_pointer_rtx)
  9280. && REG_P (xop1) && REGNO (xop1) == R0_REG)
  9281. || ((xop1 == stack_pointer_rtx
  9282. || xop1 == hard_frame_pointer_rtx)
  9283. && REG_P (xop0) && REGNO (xop0) == R0_REG))
  9284. && ((!TARGET_SHMEDIA && GET_MODE_SIZE (mode) <= 4)
  9285. || (TARGET_SHMEDIA && GET_MODE_SIZE (mode) <= 8)
  9286. || ((TARGET_SH4 || TARGET_SH2A_DOUBLE)
  9287. && TARGET_FMOVD && mode == DFmode)))
  9288. {
  9289. if (MAYBE_BASE_REGISTER_RTX_P (xop1, strict)
  9290. && MAYBE_INDEX_REGISTER_RTX_P (xop0, strict))
  9291. return true;
  9292. if (MAYBE_INDEX_REGISTER_RTX_P (xop1, strict)
  9293. && MAYBE_BASE_REGISTER_RTX_P (xop0, strict))
  9294. return true;
  9295. }
  9296. }
  9297. return false;
  9298. }
  9299. /* Return TRUE if X references a SYMBOL_REF or LABEL_REF whose symbol
  9300. isn't protected by a PIC unspec. */
  9301. bool
  9302. nonpic_symbol_mentioned_p (rtx x)
  9303. {
  9304. const char *fmt;
  9305. int i;
  9306. if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF
  9307. || GET_CODE (x) == PC)
  9308. return true;
  9309. /* We don't want to look into the possible MEM location of a
  9310. CONST_DOUBLE, since we're not going to use it, in general. */
  9311. if (GET_CODE (x) == CONST_DOUBLE)
  9312. return false;
  9313. if (GET_CODE (x) == UNSPEC
  9314. && (XINT (x, 1) == UNSPEC_PIC
  9315. || XINT (x, 1) == UNSPEC_GOT
  9316. || XINT (x, 1) == UNSPEC_GOTOFF
  9317. || XINT (x, 1) == UNSPEC_GOTPLT
  9318. || XINT (x, 1) == UNSPEC_GOTTPOFF
  9319. || XINT (x, 1) == UNSPEC_DTPOFF
  9320. || XINT (x, 1) == UNSPEC_TPOFF
  9321. || XINT (x, 1) == UNSPEC_PLT
  9322. || XINT (x, 1) == UNSPEC_SYMOFF
  9323. || XINT (x, 1) == UNSPEC_PCREL_SYMOFF))
  9324. return false;
  9325. fmt = GET_RTX_FORMAT (GET_CODE (x));
  9326. for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
  9327. {
  9328. if (fmt[i] == 'E')
  9329. {
  9330. int j;
  9331. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  9332. if (nonpic_symbol_mentioned_p (XVECEXP (x, i, j)))
  9333. return true;
  9334. }
  9335. else if (fmt[i] == 'e' && nonpic_symbol_mentioned_p (XEXP (x, i)))
  9336. return true;
  9337. }
  9338. return false;
  9339. }
  9340. /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
  9341. @GOTOFF in `reg'. */
  9342. rtx
  9343. legitimize_pic_address (rtx orig, machine_mode mode ATTRIBUTE_UNUSED,
  9344. rtx reg)
  9345. {
  9346. if (tls_symbolic_operand (orig, Pmode) != TLS_MODEL_NONE)
  9347. return orig;
  9348. if (GET_CODE (orig) == LABEL_REF
  9349. || (GET_CODE (orig) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (orig)))
  9350. {
  9351. if (reg == NULL_RTX)
  9352. reg = gen_reg_rtx (Pmode);
  9353. emit_insn (gen_symGOTOFF2reg (reg, orig));
  9354. return reg;
  9355. }
  9356. else if (GET_CODE (orig) == SYMBOL_REF)
  9357. {
  9358. if (reg == NULL_RTX)
  9359. reg = gen_reg_rtx (Pmode);
  9360. emit_insn (gen_symGOT2reg (reg, orig));
  9361. return reg;
  9362. }
  9363. return orig;
  9364. }
  9365. /* Given a (logical) mode size and an offset in bytes, try to find a the
  9366. appropriate displacement value for a mov insn. On SH the displacements
  9367. are limited to max. 60 bytes for SImode, max. 30 bytes in HImode and max.
  9368. 15 bytes in QImode. To compensate this we create a new base address by
  9369. adding an adjustment value to it.
  9370. If the originally requested offset is greater than 127 we prefer using
  9371. values 124..127 over 128..131 to increase opportunities to use the
  9372. add #imm, Rn insn.
  9373. In some cases it is possible that a requested offset might seem unaligned
  9374. or inappropriate for the mode size, like offset = 2 and mode size = 4.
  9375. This is compensated by adjusting the base address so that the effective
  9376. address of the displacement move insn will be aligned.
  9377. This is not the best possible way of rebasing the base address, as it
  9378. does not look at other present displacement addressings around it.
  9379. In some cases this can create more base address adjustments than would
  9380. actually be necessary. */
  9381. struct disp_adjust
  9382. {
  9383. rtx offset_adjust;
  9384. rtx mov_disp;
  9385. };
  9386. static struct disp_adjust
  9387. sh_find_mov_disp_adjust (machine_mode mode, HOST_WIDE_INT offset)
  9388. {
  9389. struct disp_adjust res = { NULL_RTX, NULL_RTX };
  9390. /* Do not try to use SH2A's large displacements here, because this would
  9391. effectively disable the small displacement insns. */
  9392. const int mode_sz = GET_MODE_SIZE (mode);
  9393. const int mov_insn_sz = mov_insn_size (mode, false);
  9394. const int max_disp = sh_max_mov_insn_displacement (mode, false);
  9395. const int max_disp_next = max_disp + mov_insn_sz;
  9396. HOST_WIDE_INT align_modifier = offset > 127 ? mov_insn_sz : 0;
  9397. HOST_WIDE_INT offset_adjust;
  9398. /* In some cases this actually does happen and we must check for it. */
  9399. if (mode_sz < 1 || mode_sz > 8 || max_disp < 1)
  9400. return res;
  9401. /* Keeps the previous behavior for QImode displacement addressing.
  9402. This just decides how the offset is re-based. Removing this special
  9403. case will result in slightly bigger code on average, but it's not that
  9404. bad actually. */
  9405. if (mov_insn_sz == 1)
  9406. align_modifier = 0;
  9407. offset_adjust = ((offset + align_modifier) & ~max_disp) - align_modifier;
  9408. if (mode_sz + offset - offset_adjust <= max_disp_next)
  9409. {
  9410. res.offset_adjust = GEN_INT (offset_adjust);
  9411. res.mov_disp = GEN_INT (offset - offset_adjust);
  9412. }
  9413. return res;
  9414. }
  9415. /* Try to modify an illegitimate address and make it legitimate.
  9416. If we find one, return the new, valid address.
  9417. Otherwise, return the original address. */
  9418. static rtx
  9419. sh_legitimize_address (rtx x, rtx oldx, machine_mode mode)
  9420. {
  9421. if (flag_pic)
  9422. x = legitimize_pic_address (oldx, mode, NULL_RTX);
  9423. if (TARGET_SHMEDIA)
  9424. return x;
  9425. if (((TARGET_SH4 || TARGET_SH2A_DOUBLE) && mode == DFmode)
  9426. || (TARGET_SH2E && mode == SFmode))
  9427. return x;
  9428. if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1))
  9429. && BASE_REGISTER_RTX_P (XEXP (x, 0)))
  9430. {
  9431. struct disp_adjust adj = sh_find_mov_disp_adjust (mode,
  9432. INTVAL (XEXP (x, 1)));
  9433. if (adj.offset_adjust != NULL_RTX && adj.mov_disp != NULL_RTX)
  9434. {
  9435. rtx sum = expand_binop (Pmode, add_optab, XEXP (x, 0),
  9436. adj.offset_adjust, NULL_RTX, 0,
  9437. OPTAB_LIB_WIDEN);
  9438. return gen_rtx_PLUS (Pmode, sum, adj.mov_disp);
  9439. }
  9440. }
  9441. return x;
  9442. }
  9443. /* Attempt to replace *p, which is an address that needs reloading, with
  9444. a valid memory address for an operand of mode MODE.
  9445. Like for sh_legitimize_address, for the SH we try to get a normal form
  9446. of the address. That will allow inheritance of the address reloads. */
  9447. bool
  9448. sh_legitimize_reload_address (rtx *p, machine_mode mode, int opnum,
  9449. int itype)
  9450. {
  9451. enum reload_type type = (enum reload_type) itype;
  9452. const int mode_sz = GET_MODE_SIZE (mode);
  9453. if (sh_lra_p ())
  9454. return false;
  9455. if (! ALLOW_INDEXED_ADDRESS
  9456. && GET_CODE (*p) == PLUS
  9457. && REG_P (XEXP (*p, 0)) && REG_P (XEXP (*p, 1)))
  9458. {
  9459. *p = copy_rtx (*p);
  9460. push_reload (*p, NULL_RTX, p, NULL,
  9461. BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
  9462. return true;
  9463. }
  9464. if (! ALLOW_INDEXED_ADDRESS
  9465. && GET_CODE (*p) == PLUS
  9466. && GET_CODE (XEXP (*p, 0)) == PLUS)
  9467. {
  9468. rtx sum = gen_rtx_PLUS (Pmode, XEXP (XEXP (*p, 0), 0),
  9469. XEXP (XEXP (*p, 0), 1));
  9470. *p = gen_rtx_PLUS (Pmode, sum, XEXP (*p, 1));
  9471. push_reload (sum, NULL_RTX, &XEXP (*p, 0), NULL,
  9472. BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
  9473. return true;
  9474. }
  9475. if (TARGET_SHMEDIA)
  9476. return false;
  9477. if (GET_CODE (*p) == PLUS && CONST_INT_P (XEXP (*p, 1))
  9478. && MAYBE_BASE_REGISTER_RTX_P (XEXP (*p, 0), true)
  9479. && (ALLOW_INDEXED_ADDRESS
  9480. || XEXP (*p, 0) == stack_pointer_rtx
  9481. || XEXP (*p, 0) == hard_frame_pointer_rtx))
  9482. {
  9483. const HOST_WIDE_INT offset = INTVAL (XEXP (*p, 1));
  9484. struct disp_adjust adj = sh_find_mov_disp_adjust (mode, offset);
  9485. if (TARGET_SH2A && mode == DFmode && (offset & 0x7))
  9486. {
  9487. push_reload (*p, NULL_RTX, p, NULL,
  9488. BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
  9489. return true;
  9490. }
  9491. if (TARGET_SH2E && mode == SFmode)
  9492. {
  9493. *p = copy_rtx (*p);
  9494. push_reload (*p, NULL_RTX, p, NULL,
  9495. BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
  9496. return true;
  9497. }
  9498. /* FIXME: Do not allow to legitimize QImode and HImode displacement
  9499. moves because then reload has a problem figuring the constraint
  9500. that the move insn target/source reg must be R0.
  9501. Or maybe some handling is wrong in sh_secondary_reload for this
  9502. to work properly? */
  9503. if ((mode_sz == 4 || mode_sz == 8)
  9504. && ! (TARGET_SH4 && mode == DFmode)
  9505. && adj.offset_adjust != NULL_RTX && adj.mov_disp != NULL_RTX)
  9506. {
  9507. rtx sum = gen_rtx_PLUS (Pmode, XEXP (*p, 0), adj.offset_adjust);
  9508. *p = gen_rtx_PLUS (Pmode, sum, adj.mov_disp);
  9509. push_reload (sum, NULL_RTX, &XEXP (*p, 0), NULL,
  9510. BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
  9511. return true;
  9512. }
  9513. }
  9514. /* We must re-recognize what we created before. */
  9515. if (GET_CODE (*p) == PLUS
  9516. && (mode_sz == 4 || mode_sz == 8)
  9517. && GET_CODE (XEXP (*p, 0)) == PLUS
  9518. && CONST_INT_P (XEXP (XEXP (*p, 0), 1))
  9519. && MAYBE_BASE_REGISTER_RTX_P (XEXP (XEXP (*p, 0), 0), true)
  9520. && CONST_INT_P (XEXP (*p, 1))
  9521. && ! (TARGET_SH2E && mode == SFmode))
  9522. {
  9523. /* Because this address is so complex, we know it must have
  9524. been created by LEGITIMIZE_RELOAD_ADDRESS before; thus,
  9525. it is already unshared, and needs no further unsharing. */
  9526. push_reload (XEXP (*p, 0), NULL_RTX, &XEXP (*p, 0), NULL,
  9527. BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
  9528. return true;
  9529. }
  9530. return false;
  9531. }
  9532. /* In the name of slightly smaller debug output, and to cater to
  9533. general assembler lossage, recognize various UNSPEC sequences
  9534. and turn them back into a direct symbol reference. */
  9535. static rtx
  9536. sh_delegitimize_address (rtx orig_x)
  9537. {
  9538. rtx x, y;
  9539. orig_x = delegitimize_mem_from_attrs (orig_x);
  9540. x = orig_x;
  9541. if (MEM_P (x))
  9542. x = XEXP (x, 0);
  9543. if (GET_CODE (x) == CONST)
  9544. {
  9545. y = XEXP (x, 0);
  9546. if (GET_CODE (y) == UNSPEC)
  9547. {
  9548. if (XINT (y, 1) == UNSPEC_GOT
  9549. || XINT (y, 1) == UNSPEC_GOTOFF
  9550. || XINT (y, 1) == UNSPEC_SYMOFF)
  9551. return XVECEXP (y, 0, 0);
  9552. else if (XINT (y, 1) == UNSPEC_PCREL_SYMOFF)
  9553. {
  9554. if (GET_CODE (XVECEXP (y, 0, 0)) == CONST)
  9555. {
  9556. rtx symplt = XEXP (XVECEXP (y, 0, 0), 0);
  9557. if (GET_CODE (symplt) == UNSPEC
  9558. && XINT (symplt, 1) == UNSPEC_PLT)
  9559. return XVECEXP (symplt, 0, 0);
  9560. }
  9561. }
  9562. else if (TARGET_SHMEDIA
  9563. && (XINT (y, 1) == UNSPEC_EXTRACT_S16
  9564. || XINT (y, 1) == UNSPEC_EXTRACT_U16))
  9565. {
  9566. rtx offset = XVECEXP (y, 0, 1);
  9567. x = gen_rtx_PLUS (Pmode, XVECEXP (y, 0, 0), offset);
  9568. if (MEM_P (orig_x))
  9569. x = replace_equiv_address_nv (orig_x, x);
  9570. return x;
  9571. }
  9572. }
  9573. }
  9574. return orig_x;
  9575. }
  9576. /* Mark the use of a constant in the literal table. If the constant
  9577. has multiple labels, make it unique. */
  9578. static rtx
  9579. mark_constant_pool_use (rtx x)
  9580. {
  9581. rtx_insn *insn, *lab;
  9582. rtx pattern;
  9583. if (x == NULL_RTX)
  9584. return x;
  9585. switch (GET_CODE (x))
  9586. {
  9587. case LABEL_REF:
  9588. x = XEXP (x, 0);
  9589. case CODE_LABEL:
  9590. break;
  9591. default:
  9592. return x;
  9593. }
  9594. /* Get the first label in the list of labels for the same constant
  9595. and delete another labels in the list. */
  9596. lab = as_a <rtx_insn *> (x);
  9597. for (insn = PREV_INSN (lab); insn; insn = PREV_INSN (insn))
  9598. {
  9599. if (!LABEL_P (insn)
  9600. || LABEL_REFS (insn) != NEXT_INSN (insn))
  9601. break;
  9602. lab = insn;
  9603. }
  9604. for (rtx insn = LABEL_REFS (lab); insn; insn = LABEL_REFS (insn))
  9605. as_a<rtx_insn *> (insn)->set_deleted ();
  9606. /* Mark constants in a window. */
  9607. for (insn = NEXT_INSN (as_a <rtx_insn *> (x)); insn; insn = NEXT_INSN (insn))
  9608. {
  9609. if (!NONJUMP_INSN_P (insn))
  9610. continue;
  9611. pattern = PATTERN (insn);
  9612. if (GET_CODE (pattern) != UNSPEC_VOLATILE)
  9613. continue;
  9614. switch (XINT (pattern, 1))
  9615. {
  9616. case UNSPECV_CONST2:
  9617. case UNSPECV_CONST4:
  9618. case UNSPECV_CONST8:
  9619. XVECEXP (pattern, 0, 1) = const1_rtx;
  9620. break;
  9621. case UNSPECV_WINDOW_END:
  9622. if (XVECEXP (pattern, 0, 0) == x)
  9623. return lab;
  9624. break;
  9625. case UNSPECV_CONST_END:
  9626. return lab;
  9627. default:
  9628. break;
  9629. }
  9630. }
  9631. return lab;
  9632. }
  9633. /* Return true if it's possible to redirect BRANCH1 to the destination
  9634. of an unconditional jump BRANCH2. We only want to do this if the
  9635. resulting branch will have a short displacement. */
  9636. static bool
  9637. sh_can_follow_jump (const rtx_insn *branch1, const rtx_insn *branch2)
  9638. {
  9639. /* Don't follow if BRANCH2 is possible to be a jump crossing between
  9640. hot and cold partitions. */
  9641. if (TARGET_SH1
  9642. && flag_reorder_blocks_and_partition
  9643. && simplejump_p (branch2)
  9644. && CROSSING_JUMP_P (branch2))
  9645. return false;
  9646. if (flag_expensive_optimizations && simplejump_p (branch2))
  9647. {
  9648. rtx dest = XEXP (SET_SRC (single_set (branch2)), 0);
  9649. rtx_insn *insn;
  9650. int distance;
  9651. for (distance = 0, insn = NEXT_INSN (branch1);
  9652. insn && distance < 256;
  9653. insn = PREV_INSN (insn))
  9654. {
  9655. if (insn == dest)
  9656. return true;
  9657. else
  9658. distance += get_attr_length (insn);
  9659. }
  9660. for (distance = 0, insn = NEXT_INSN (branch1);
  9661. insn && distance < 256;
  9662. insn = NEXT_INSN (insn))
  9663. {
  9664. if (insn == dest)
  9665. return true;
  9666. else
  9667. distance += get_attr_length (insn);
  9668. }
  9669. }
  9670. return false;
  9671. }
  9672. /* Return nonzero if register old_reg can be renamed to register new_reg. */
  9673. bool
  9674. sh_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
  9675. unsigned int new_reg)
  9676. {
  9677. /* Interrupt functions can only use registers that have already been
  9678. saved by the prologue, even if they would normally be
  9679. call-clobbered. */
  9680. if (sh_cfun_interrupt_handler_p () && !df_regs_ever_live_p (new_reg))
  9681. return false;
  9682. return true;
  9683. }
  9684. /* Function to update the integer COST
  9685. based on the relationship between INSN that is dependent on
  9686. DEP_INSN through the dependence LINK. The default is to make no
  9687. adjustment to COST. This can be used for example to specify to
  9688. the scheduler that an output- or anti-dependence does not incur
  9689. the same cost as a data-dependence. The return value should be
  9690. the new value for COST. */
  9691. static int
  9692. sh_adjust_cost (rtx_insn *insn, rtx link ATTRIBUTE_UNUSED,
  9693. rtx_insn *dep_insn, int cost)
  9694. {
  9695. rtx reg, use_pat;
  9696. if (TARGET_SHMEDIA)
  9697. {
  9698. /* On SHmedia, if the dependence is an anti-dependence or
  9699. output-dependence, there is no cost. */
  9700. if (REG_NOTE_KIND (link) != 0)
  9701. {
  9702. /* However, dependencies between target register loads and
  9703. uses of the register in a subsequent block that are separated
  9704. by a conditional branch are not modelled - we have to do with
  9705. the anti-dependency between the target register load and the
  9706. conditional branch that ends the current block. */
  9707. if (REG_NOTE_KIND (link) == REG_DEP_ANTI
  9708. && GET_CODE (PATTERN (dep_insn)) == SET
  9709. && (get_attr_type (dep_insn) == TYPE_PT_MEDIA
  9710. || get_attr_type (dep_insn) == TYPE_PTABS_MEDIA)
  9711. && get_attr_type (insn) == TYPE_CBRANCH_MEDIA)
  9712. {
  9713. int orig_cost = cost;
  9714. rtx note = find_reg_note (insn, REG_BR_PROB, 0);
  9715. rtx target = ((!note || XINT (note, 0) * 2 < REG_BR_PROB_BASE)
  9716. ? insn : JUMP_LABEL (insn));
  9717. /* On the likely path, the branch costs 1, on the unlikely path,
  9718. it costs 3. */
  9719. cost--;
  9720. do
  9721. target = next_active_insn (target);
  9722. while (target && ! flow_dependent_p (target, dep_insn)
  9723. && --cost > 0);
  9724. /* If two branches are executed in immediate succession, with the
  9725. first branch properly predicted, this causes a stall at the
  9726. second branch, hence we won't need the target for the
  9727. second branch for two cycles after the launch of the first
  9728. branch. */
  9729. if (cost > orig_cost - 2)
  9730. cost = orig_cost - 2;
  9731. }
  9732. else
  9733. cost = 0;
  9734. }
  9735. else if (get_attr_is_mac_media (insn)
  9736. && get_attr_is_mac_media (dep_insn))
  9737. cost = 1;
  9738. else if (! reload_completed
  9739. && GET_CODE (PATTERN (insn)) == SET
  9740. && GET_CODE (SET_SRC (PATTERN (insn))) == FLOAT
  9741. && GET_CODE (PATTERN (dep_insn)) == SET
  9742. && fp_arith_reg_operand (SET_SRC (PATTERN (dep_insn)), VOIDmode)
  9743. && cost < 4)
  9744. cost = 4;
  9745. /* Schedule the ptabs for a casesi_jump_media in preference to stuff
  9746. that is needed at the target. */
  9747. else if (get_attr_type (insn) == TYPE_JUMP_MEDIA
  9748. && ! flow_dependent_p (insn, dep_insn))
  9749. cost--;
  9750. }
  9751. else if (REG_NOTE_KIND (link) == 0)
  9752. {
  9753. enum attr_type type;
  9754. rtx dep_set;
  9755. if (recog_memoized (insn) < 0
  9756. || recog_memoized (dep_insn) < 0)
  9757. return cost;
  9758. dep_set = single_set (dep_insn);
  9759. /* The latency that we specify in the scheduling description refers
  9760. to the actual output, not to an auto-increment register; for that,
  9761. the latency is one. */
  9762. if (dep_set && MEM_P (SET_SRC (dep_set)) && cost > 1)
  9763. {
  9764. rtx set = single_set (insn);
  9765. if (set
  9766. && !reg_mentioned_p (SET_DEST (dep_set), SET_SRC (set))
  9767. && (!MEM_P (SET_DEST (set))
  9768. || !reg_mentioned_p (SET_DEST (dep_set),
  9769. XEXP (SET_DEST (set), 0))))
  9770. cost = 1;
  9771. }
  9772. /* The only input for a call that is timing-critical is the
  9773. function's address. */
  9774. if (CALL_P (insn))
  9775. {
  9776. rtx call = get_call_rtx_from (insn);
  9777. if (call
  9778. /* sibcalli_thunk uses a symbol_ref in an unspec. */
  9779. && (GET_CODE (XEXP (XEXP (call, 0), 0)) == UNSPEC
  9780. || ! reg_set_p (XEXP (XEXP (call, 0), 0), dep_insn)))
  9781. cost -= TARGET_SH4_300 ? 3 : 6;
  9782. }
  9783. /* Likewise, the most timing critical input for an sfuncs call
  9784. is the function address. However, sfuncs typically start
  9785. using their arguments pretty quickly.
  9786. Assume a four cycle delay for SH4 before they are needed.
  9787. Cached ST40-300 calls are quicker, so assume only a one
  9788. cycle delay there.
  9789. ??? Maybe we should encode the delays till input registers
  9790. are needed by sfuncs into the sfunc call insn. */
  9791. /* All sfunc calls are parallels with at least four components.
  9792. Exploit this to avoid unnecessary calls to sfunc_uses_reg. */
  9793. else if (GET_CODE (PATTERN (insn)) == PARALLEL
  9794. && XVECLEN (PATTERN (insn), 0) >= 4
  9795. && (reg = sfunc_uses_reg (insn)))
  9796. {
  9797. if (! reg_set_p (reg, dep_insn))
  9798. cost -= TARGET_SH4_300 ? 1 : 4;
  9799. }
  9800. if (TARGET_HARD_SH4 && !TARGET_SH4_300)
  9801. {
  9802. enum attr_type dep_type = get_attr_type (dep_insn);
  9803. if (dep_type == TYPE_FLOAD || dep_type == TYPE_PCFLOAD)
  9804. cost--;
  9805. else if ((dep_type == TYPE_LOAD_SI || dep_type == TYPE_PCLOAD_SI)
  9806. && (type = get_attr_type (insn)) != TYPE_CALL
  9807. && type != TYPE_SFUNC)
  9808. cost--;
  9809. /* When the preceding instruction loads the shift amount of
  9810. the following SHAD/SHLD, the latency of the load is increased
  9811. by 1 cycle. */
  9812. if (get_attr_type (insn) == TYPE_DYN_SHIFT
  9813. && get_attr_any_int_load (dep_insn) == ANY_INT_LOAD_YES
  9814. && reg_overlap_mentioned_p (SET_DEST (dep_set),
  9815. XEXP (SET_SRC (single_set (insn)),
  9816. 1)))
  9817. cost++;
  9818. /* When an LS group instruction with a latency of less than
  9819. 3 cycles is followed by a double-precision floating-point
  9820. instruction, FIPR, or FTRV, the latency of the first
  9821. instruction is increased to 3 cycles. */
  9822. else if (cost < 3
  9823. && get_attr_insn_class (dep_insn) == INSN_CLASS_LS_GROUP
  9824. && get_attr_dfp_comp (insn) == DFP_COMP_YES)
  9825. cost = 3;
  9826. /* The lsw register of a double-precision computation is ready one
  9827. cycle earlier. */
  9828. else if (reload_completed
  9829. && get_attr_dfp_comp (dep_insn) == DFP_COMP_YES
  9830. && (use_pat = single_set (insn))
  9831. && ! regno_use_in (REGNO (SET_DEST (single_set (dep_insn))),
  9832. SET_SRC (use_pat)))
  9833. cost -= 1;
  9834. if (get_attr_any_fp_comp (dep_insn) == ANY_FP_COMP_YES
  9835. && get_attr_late_fp_use (insn) == LATE_FP_USE_YES)
  9836. cost -= 1;
  9837. }
  9838. else if (TARGET_SH4_300)
  9839. {
  9840. /* Stores need their input register two cycles later. */
  9841. if (dep_set && cost >= 1
  9842. && ((type = get_attr_type (insn)) == TYPE_STORE
  9843. || type == TYPE_PSTORE
  9844. || type == TYPE_FSTORE || type == TYPE_MAC_MEM))
  9845. {
  9846. rtx set = single_set (insn);
  9847. if (!reg_mentioned_p (SET_SRC (set), XEXP (SET_DEST (set), 0))
  9848. && rtx_equal_p (SET_SRC (set), SET_DEST (dep_set)))
  9849. {
  9850. cost -= 2;
  9851. /* But don't reduce the cost below 1 if the address depends
  9852. on a side effect of dep_insn. */
  9853. if (cost < 1
  9854. && modified_in_p (XEXP (SET_DEST (set), 0), dep_insn))
  9855. cost = 1;
  9856. }
  9857. }
  9858. }
  9859. }
  9860. /* An anti-dependence penalty of two applies if the first insn is a double
  9861. precision fadd / fsub / fmul. */
  9862. else if (!TARGET_SH4_300
  9863. && REG_NOTE_KIND (link) == REG_DEP_ANTI
  9864. && recog_memoized (dep_insn) >= 0
  9865. && (get_attr_type (dep_insn) == TYPE_DFP_ARITH
  9866. || get_attr_type (dep_insn) == TYPE_DFP_MUL)
  9867. /* A lot of alleged anti-flow dependences are fake,
  9868. so check this one is real. */
  9869. && flow_dependent_p (dep_insn, insn))
  9870. cost = 2;
  9871. return cost;
  9872. }
  9873. /* Check if INSN is flow-dependent on DEP_INSN. Can also be used to check
  9874. if DEP_INSN is anti-flow dependent on INSN. */
  9875. static bool
  9876. flow_dependent_p (rtx insn, rtx dep_insn)
  9877. {
  9878. rtx tmp = PATTERN (insn);
  9879. note_stores (PATTERN (dep_insn), flow_dependent_p_1, &tmp);
  9880. return tmp == NULL_RTX;
  9881. }
  9882. /* A helper function for flow_dependent_p called through note_stores. */
  9883. static void
  9884. flow_dependent_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
  9885. {
  9886. rtx * pinsn = (rtx *) data;
  9887. if (*pinsn && reg_referenced_p (x, *pinsn))
  9888. *pinsn = NULL_RTX;
  9889. }
  9890. /* For use by sh_allocate_initial_value. Note that sh.md contains some
  9891. 'special function' patterns (type sfunc) that clobber pr, but that
  9892. do not look like function calls to leaf_function_p. Hence we must
  9893. do this extra check. */
  9894. static int
  9895. sh_pr_n_sets (void)
  9896. {
  9897. return DF_REG_DEF_COUNT (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG);
  9898. }
  9899. /* Return where to allocate pseudo for a given hard register initial
  9900. value. */
  9901. static rtx
  9902. sh_allocate_initial_value (rtx hard_reg)
  9903. {
  9904. rtx x;
  9905. if (REGNO (hard_reg) == (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG))
  9906. {
  9907. if (crtl->is_leaf
  9908. && ! sh_pr_n_sets ()
  9909. && ! (TARGET_SHCOMPACT
  9910. && ((crtl->args.info.call_cookie
  9911. & ~ CALL_COOKIE_RET_TRAMP (1))
  9912. || crtl->saves_all_registers)))
  9913. x = hard_reg;
  9914. else
  9915. x = gen_frame_mem (Pmode, return_address_pointer_rtx);
  9916. }
  9917. else
  9918. x = NULL_RTX;
  9919. return x;
  9920. }
  9921. /* This function returns "2" to indicate dual issue for the SH4
  9922. processor. To be used by the DFA pipeline description. */
  9923. static int
  9924. sh_issue_rate (void)
  9925. {
  9926. if (TARGET_SUPERSCALAR)
  9927. return 2;
  9928. else
  9929. return 1;
  9930. }
  9931. /* Functions for ready queue reordering for sched1. */
  9932. /* Get weight for mode for a set x. */
  9933. static short
  9934. find_set_regmode_weight (rtx x, machine_mode mode)
  9935. {
  9936. if (GET_CODE (x) == CLOBBER && register_operand (SET_DEST (x), mode))
  9937. return 1;
  9938. if (GET_CODE (x) == SET && register_operand (SET_DEST (x), mode))
  9939. {
  9940. if (REG_P (SET_DEST (x)))
  9941. {
  9942. if (!reg_mentioned_p (SET_DEST (x), SET_SRC (x)))
  9943. return 1;
  9944. else
  9945. return 0;
  9946. }
  9947. return 1;
  9948. }
  9949. return 0;
  9950. }
  9951. /* Get regmode weight for insn. */
  9952. static short
  9953. find_insn_regmode_weight (rtx insn, machine_mode mode)
  9954. {
  9955. short reg_weight = 0;
  9956. rtx x;
  9957. /* Increment weight for each register born here. */
  9958. x = PATTERN (insn);
  9959. reg_weight += find_set_regmode_weight (x, mode);
  9960. if (GET_CODE (x) == PARALLEL)
  9961. {
  9962. int j;
  9963. for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
  9964. {
  9965. x = XVECEXP (PATTERN (insn), 0, j);
  9966. reg_weight += find_set_regmode_weight (x, mode);
  9967. }
  9968. }
  9969. /* Decrement weight for each register that dies here. */
  9970. for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
  9971. {
  9972. if (REG_NOTE_KIND (x) == REG_DEAD || REG_NOTE_KIND (x) == REG_UNUSED)
  9973. {
  9974. rtx note = XEXP (x, 0);
  9975. if (REG_P (note) && GET_MODE (note) == mode)
  9976. reg_weight--;
  9977. }
  9978. }
  9979. return reg_weight;
  9980. }
  9981. /* Calculate regmode weights for all insns of a basic block. */
  9982. static void
  9983. find_regmode_weight (basic_block b, machine_mode mode)
  9984. {
  9985. rtx_insn *insn, *next_tail, *head, *tail;
  9986. get_ebb_head_tail (b, b, &head, &tail);
  9987. next_tail = NEXT_INSN (tail);
  9988. for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
  9989. {
  9990. /* Handle register life information. */
  9991. if (!INSN_P (insn))
  9992. continue;
  9993. if (mode == SFmode)
  9994. INSN_REGMODE_WEIGHT (insn, mode) =
  9995. find_insn_regmode_weight (insn, mode)
  9996. + 2 * find_insn_regmode_weight (insn, DFmode);
  9997. else if (mode == SImode)
  9998. INSN_REGMODE_WEIGHT (insn, mode) =
  9999. find_insn_regmode_weight (insn, mode)
  10000. + 2 * find_insn_regmode_weight (insn, DImode);
  10001. }
  10002. }
  10003. /* Comparison function for ready queue sorting. */
  10004. static int
  10005. rank_for_reorder (const void *x, const void *y)
  10006. {
  10007. rtx_insn *tmp = *(rtx_insn * const *) y;
  10008. rtx_insn *tmp2 = *(rtx_insn * const *) x;
  10009. /* The insn in a schedule group should be issued the first. */
  10010. if (SCHED_GROUP_P (tmp) != SCHED_GROUP_P (tmp2))
  10011. return SCHED_GROUP_P (tmp2) ? 1 : -1;
  10012. /* If insns are equally good, sort by INSN_LUID (original insn order), This
  10013. minimizes instruction movement, thus minimizing sched's effect on
  10014. register pressure. */
  10015. return INSN_LUID (tmp) - INSN_LUID (tmp2);
  10016. }
  10017. /* Resort the array A in which only element at index N may be out of order. */
  10018. static void
  10019. swap_reorder (rtx_insn **a, int n)
  10020. {
  10021. rtx_insn *insn = a[n - 1];
  10022. int i = n - 2;
  10023. while (i >= 0 && rank_for_reorder (a + i, &insn) >= 0)
  10024. {
  10025. a[i + 1] = a[i];
  10026. i -= 1;
  10027. }
  10028. a[i + 1] = insn;
  10029. }
  10030. /* Sort the ready list by ascending priority. */
  10031. static void
  10032. ready_reorder (rtx_insn **ready, int nready)
  10033. {
  10034. if (nready == 2)
  10035. swap_reorder (ready, nready);
  10036. else if (nready > 2)
  10037. qsort (ready, nready, sizeof (rtx_insn *), rank_for_reorder);
  10038. }
  10039. /* Count life regions of r0 for a block. */
  10040. static int
  10041. find_r0_life_regions (basic_block b)
  10042. {
  10043. rtx_insn *end, *insn;
  10044. rtx pset;
  10045. rtx r0_reg;
  10046. int live;
  10047. int set;
  10048. int death = 0;
  10049. if (REGNO_REG_SET_P (df_get_live_in (b), R0_REG))
  10050. {
  10051. set = 1;
  10052. live = 1;
  10053. }
  10054. else
  10055. {
  10056. set = 0;
  10057. live = 0;
  10058. }
  10059. insn = BB_HEAD (b);
  10060. end = BB_END (b);
  10061. r0_reg = gen_rtx_REG (SImode, R0_REG);
  10062. while (1)
  10063. {
  10064. if (INSN_P (insn))
  10065. {
  10066. if (find_regno_note (insn, REG_DEAD, R0_REG))
  10067. {
  10068. death++;
  10069. live = 0;
  10070. }
  10071. if (!live
  10072. && (pset = single_set (insn))
  10073. && reg_overlap_mentioned_p (r0_reg, SET_DEST (pset))
  10074. && !find_regno_note (insn, REG_UNUSED, R0_REG))
  10075. {
  10076. set++;
  10077. live = 1;
  10078. }
  10079. }
  10080. if (insn == end)
  10081. break;
  10082. insn = NEXT_INSN (insn);
  10083. }
  10084. return set - death;
  10085. }
  10086. /* Calculate regmode weights for all insns of all basic block. */
  10087. static void
  10088. sh_md_init_global (FILE *dump ATTRIBUTE_UNUSED,
  10089. int verbose ATTRIBUTE_UNUSED,
  10090. int old_max_uid)
  10091. {
  10092. basic_block b;
  10093. regmode_weight[0] = (short *) xcalloc (old_max_uid, sizeof (short));
  10094. regmode_weight[1] = (short *) xcalloc (old_max_uid, sizeof (short));
  10095. r0_life_regions = 0;
  10096. FOR_EACH_BB_REVERSE_FN (b, cfun)
  10097. {
  10098. find_regmode_weight (b, SImode);
  10099. find_regmode_weight (b, SFmode);
  10100. if (!reload_completed)
  10101. r0_life_regions += find_r0_life_regions (b);
  10102. }
  10103. CURR_REGMODE_PRESSURE (SImode) = 0;
  10104. CURR_REGMODE_PRESSURE (SFmode) = 0;
  10105. }
  10106. /* Cleanup. */
  10107. static void
  10108. sh_md_finish_global (FILE *dump ATTRIBUTE_UNUSED,
  10109. int verbose ATTRIBUTE_UNUSED)
  10110. {
  10111. if (regmode_weight[0])
  10112. {
  10113. free (regmode_weight[0]);
  10114. regmode_weight[0] = NULL;
  10115. }
  10116. if (regmode_weight[1])
  10117. {
  10118. free (regmode_weight[1]);
  10119. regmode_weight[1] = NULL;
  10120. }
  10121. }
  10122. /* The scalar modes supported differs from the default version in TImode
  10123. for 32-bit SHMEDIA. */
  10124. static bool
  10125. sh_scalar_mode_supported_p (machine_mode mode)
  10126. {
  10127. if (TARGET_SHMEDIA32 && mode == TImode)
  10128. return false;
  10129. return default_scalar_mode_supported_p (mode);
  10130. }
  10131. /* Cache the can_issue_more so that we can return it from reorder2. Also,
  10132. keep count of register pressures on SImode and SFmode. */
  10133. static int
  10134. sh_variable_issue (FILE *dump ATTRIBUTE_UNUSED,
  10135. int sched_verbose ATTRIBUTE_UNUSED,
  10136. rtx_insn *insn,
  10137. int can_issue_more)
  10138. {
  10139. if (GET_CODE (PATTERN (insn)) != USE
  10140. && GET_CODE (PATTERN (insn)) != CLOBBER)
  10141. cached_can_issue_more = can_issue_more - 1;
  10142. else
  10143. cached_can_issue_more = can_issue_more;
  10144. if (reload_completed)
  10145. return cached_can_issue_more;
  10146. CURR_REGMODE_PRESSURE (SImode) += INSN_REGMODE_WEIGHT (insn, SImode);
  10147. CURR_REGMODE_PRESSURE (SFmode) += INSN_REGMODE_WEIGHT (insn, SFmode);
  10148. return cached_can_issue_more;
  10149. }
  10150. static void
  10151. sh_md_init (FILE *dump ATTRIBUTE_UNUSED,
  10152. int verbose ATTRIBUTE_UNUSED,
  10153. int veclen ATTRIBUTE_UNUSED)
  10154. {
  10155. CURR_REGMODE_PRESSURE (SImode) = 0;
  10156. CURR_REGMODE_PRESSURE (SFmode) = 0;
  10157. }
  10158. /* Some magic numbers. */
  10159. /* Pressure on register r0 can lead to spill failures. so avoid sched1 for
  10160. functions that already have high pressure on r0. */
  10161. #define R0_MAX_LIFE_REGIONS 2
  10162. /* Register Pressure thresholds for SImode and SFmode registers. */
  10163. #define SIMODE_MAX_WEIGHT 5
  10164. #define SFMODE_MAX_WEIGHT 10
  10165. /* Return true if the pressure is high for MODE. */
  10166. static bool
  10167. high_pressure (machine_mode mode)
  10168. {
  10169. /* Pressure on register r0 can lead to spill failures. so avoid sched1 for
  10170. functions that already have high pressure on r0. */
  10171. if (r0_life_regions >= R0_MAX_LIFE_REGIONS)
  10172. return true;
  10173. if (mode == SFmode)
  10174. return (CURR_REGMODE_PRESSURE (SFmode) > SFMODE_MAX_WEIGHT);
  10175. else
  10176. return (CURR_REGMODE_PRESSURE (SImode) > SIMODE_MAX_WEIGHT);
  10177. }
  10178. /* Reorder ready queue if register pressure is high. */
  10179. static int
  10180. sh_reorder (FILE *dump ATTRIBUTE_UNUSED,
  10181. int sched_verbose ATTRIBUTE_UNUSED,
  10182. rtx_insn **ready,
  10183. int *n_readyp,
  10184. int clock_var ATTRIBUTE_UNUSED)
  10185. {
  10186. if (reload_completed)
  10187. return sh_issue_rate ();
  10188. if (high_pressure (SFmode) || high_pressure (SImode))
  10189. {
  10190. ready_reorder (ready, *n_readyp);
  10191. }
  10192. return sh_issue_rate ();
  10193. }
  10194. /* Skip cycles if the current register pressure is high. */
  10195. static int
  10196. sh_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
  10197. int sched_verbose ATTRIBUTE_UNUSED,
  10198. rtx_insn **ready ATTRIBUTE_UNUSED,
  10199. int *n_readyp ATTRIBUTE_UNUSED,
  10200. int clock_var ATTRIBUTE_UNUSED)
  10201. {
  10202. if (reload_completed)
  10203. return cached_can_issue_more;
  10204. if (high_pressure(SFmode) || high_pressure (SImode))
  10205. skip_cycles = 1;
  10206. return cached_can_issue_more;
  10207. }
  10208. /* Skip cycles without sorting the ready queue. This will move insn from
  10209. Q->R. If this is the last cycle we are skipping; allow sorting of ready
  10210. queue by sh_reorder. */
  10211. /* Generally, skipping these many cycles are sufficient for all insns to move
  10212. from Q -> R. */
  10213. #define MAX_SKIPS 8
  10214. static int
  10215. sh_dfa_new_cycle (FILE *sched_dump ATTRIBUTE_UNUSED,
  10216. int sched_verbose ATTRIBUTE_UNUSED,
  10217. rtx_insn *insn ATTRIBUTE_UNUSED,
  10218. int last_clock_var,
  10219. int clock_var,
  10220. int *sort_p)
  10221. {
  10222. if (reload_completed)
  10223. return 0;
  10224. if (skip_cycles)
  10225. {
  10226. if ((clock_var - last_clock_var) < MAX_SKIPS)
  10227. {
  10228. *sort_p = 0;
  10229. return 1;
  10230. }
  10231. /* If this is the last cycle we are skipping, allow reordering of R. */
  10232. if ((clock_var - last_clock_var) == MAX_SKIPS)
  10233. {
  10234. *sort_p = 1;
  10235. return 1;
  10236. }
  10237. }
  10238. skip_cycles = 0;
  10239. return 0;
  10240. }
  10241. /* SHmedia requires registers for branches, so we can't generate new
  10242. branches past reload. */
  10243. static bool
  10244. sh_cannot_modify_jumps_p (void)
  10245. {
  10246. return (TARGET_SHMEDIA && (reload_in_progress || reload_completed));
  10247. }
  10248. static reg_class_t
  10249. sh_target_reg_class (void)
  10250. {
  10251. return TARGET_SHMEDIA ? TARGET_REGS : NO_REGS;
  10252. }
  10253. static bool
  10254. sh_optimize_target_register_callee_saved (bool after_prologue_epilogue_gen)
  10255. {
  10256. if (! shmedia_space_reserved_for_target_registers)
  10257. return 0;
  10258. if (after_prologue_epilogue_gen && ! TARGET_SAVE_ALL_TARGET_REGS)
  10259. return 0;
  10260. HARD_REG_SET dummy;
  10261. if (calc_live_regs (&dummy) >= 6 * 8)
  10262. return 1;
  10263. return 0;
  10264. }
  10265. static bool
  10266. sh_ms_bitfield_layout_p (const_tree record_type ATTRIBUTE_UNUSED)
  10267. {
  10268. return (TARGET_SH5 || TARGET_HITACHI || sh_attr_renesas_p (record_type));
  10269. }
  10270. /*
  10271. On the SH1..SH4, the trampoline looks like
  10272. 2 0002 D202 mov.l l2,r2
  10273. 1 0000 D301 mov.l l1,r3
  10274. 3 0004 422B jmp @r2
  10275. 4 0006 0009 nop
  10276. 5 0008 00000000 l1: .long area
  10277. 6 000c 00000000 l2: .long function
  10278. SH5 (compact) uses r1 instead of r3 for the static chain. */
  10279. /* Emit RTL insns to initialize the variable parts of a trampoline.
  10280. FNADDR is an RTX for the address of the function's pure code.
  10281. CXT is an RTX for the static chain value for the function. */
  10282. static void
  10283. sh_trampoline_init (rtx tramp_mem, tree fndecl, rtx cxt)
  10284. {
  10285. rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
  10286. rtx tramp = force_reg (Pmode, XEXP (tramp_mem, 0));
  10287. if (TARGET_SHMEDIA64)
  10288. {
  10289. rtx tramp_templ;
  10290. int fixed_len;
  10291. rtx movi1 = GEN_INT (0xcc000010);
  10292. rtx shori1 = GEN_INT (0xc8000010);
  10293. rtx src, dst;
  10294. /* The following trampoline works within a +- 128 KB range for cxt:
  10295. ptb/u cxt,tr1; movi fnaddr >> 48,r0; shori fnaddr >> 32,r0;
  10296. shori fnaddr >> 16,r0; shori fnaddr,r0; ptabs/l r0,tr0
  10297. gettr tr1,r1; blink tr0,r63 */
  10298. /* Address rounding makes it hard to compute the exact bounds of the
  10299. offset for this trampoline, but we have a rather generous offset
  10300. range, so frame_offset should do fine as an upper bound. */
  10301. if (cxt == virtual_stack_vars_rtx && frame_offset < 0x20000)
  10302. {
  10303. /* ??? could optimize this trampoline initialization
  10304. by writing DImode words with two insns each. */
  10305. rtx mask = force_reg (DImode, GEN_INT (0x3fffc00));
  10306. rtx insn = gen_rtx_MINUS (DImode, cxt, tramp);
  10307. insn = gen_rtx_ASHIFT (DImode, insn, GEN_INT (10-2));
  10308. insn = gen_rtx_AND (DImode, insn, mask);
  10309. /* Or in ptb/u .,tr1 pattern */
  10310. insn = gen_rtx_IOR (DImode, insn, gen_int_mode (0xec000010, SImode));
  10311. insn = force_operand (insn, NULL_RTX);
  10312. insn = gen_lowpart (SImode, insn);
  10313. emit_move_insn (change_address (tramp_mem, SImode, NULL_RTX), insn);
  10314. insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (38));
  10315. insn = gen_rtx_AND (DImode, insn, mask);
  10316. insn = force_operand (gen_rtx_IOR (DImode, movi1, insn), NULL_RTX);
  10317. insn = gen_lowpart (SImode, insn);
  10318. emit_move_insn (adjust_address (tramp_mem, SImode, 4), insn);
  10319. insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (22));
  10320. insn = gen_rtx_AND (DImode, insn, mask);
  10321. insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
  10322. insn = gen_lowpart (SImode, insn);
  10323. emit_move_insn (adjust_address (tramp_mem, SImode, 8), insn);
  10324. insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (6));
  10325. insn = gen_rtx_AND (DImode, insn, mask);
  10326. insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
  10327. insn = gen_lowpart (SImode, insn);
  10328. emit_move_insn (adjust_address (tramp_mem, SImode, 12), insn);
  10329. insn = gen_rtx_ASHIFT (DImode, fnaddr, GEN_INT (10));
  10330. insn = gen_rtx_AND (DImode, insn, mask);
  10331. insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
  10332. insn = gen_lowpart (SImode, insn);
  10333. emit_move_insn (adjust_address (tramp_mem, SImode, 16), insn);
  10334. emit_move_insn (adjust_address (tramp_mem, SImode, 20),
  10335. GEN_INT (0x6bf10600));
  10336. emit_move_insn (adjust_address (tramp_mem, SImode, 24),
  10337. GEN_INT (0x4415fc10));
  10338. emit_move_insn (adjust_address (tramp_mem, SImode, 28),
  10339. GEN_INT (0x4401fff0));
  10340. emit_insn (gen_ic_invalidate_line (tramp));
  10341. return;
  10342. }
  10343. tramp_templ = gen_rtx_SYMBOL_REF (Pmode,"__GCC_nested_trampoline");
  10344. fixed_len = TRAMPOLINE_SIZE - 2 * GET_MODE_SIZE (Pmode);
  10345. tramp_templ = gen_datalabel_ref (tramp_templ);
  10346. dst = tramp_mem;
  10347. src = gen_const_mem (BLKmode, tramp_templ);
  10348. set_mem_align (dst, 256);
  10349. set_mem_align (src, 64);
  10350. emit_block_move (dst, src, GEN_INT (fixed_len), BLOCK_OP_NORMAL);
  10351. emit_move_insn (adjust_address (tramp_mem, Pmode, fixed_len), fnaddr);
  10352. emit_move_insn (adjust_address (tramp_mem, Pmode,
  10353. fixed_len + GET_MODE_SIZE (Pmode)),
  10354. cxt);
  10355. emit_insn (gen_ic_invalidate_line (tramp));
  10356. return;
  10357. }
  10358. else if (TARGET_SHMEDIA)
  10359. {
  10360. /* movi fnaddr >> 16,r1; shori fnaddr,r1; ptabs/l r1,tr0
  10361. movi cxt >> 16,r1; shori cxt,r1; blink tr0,r63 */
  10362. rtx quad0 = gen_reg_rtx (DImode), cxtload = gen_reg_rtx (DImode);
  10363. rtx quad1 = gen_reg_rtx (DImode), quad2 = gen_reg_rtx (DImode);
  10364. /* movi 0,r1: 0xcc000010 shori 0,r1: c8000010 concatenated,
  10365. rotated 10 right, and higher 16 bit of every 32 selected. */
  10366. rtx movishori
  10367. = force_reg (V2HImode, (simplify_gen_subreg
  10368. (V2HImode, GEN_INT (0x4330432), SImode, 0)));
  10369. rtx ptabs = force_reg (DImode, GEN_INT (0x6bf10600));
  10370. rtx blink = force_reg (DImode, GEN_INT (0x4401fff0));
  10371. fnaddr = force_reg (SImode, fnaddr);
  10372. cxt = force_reg (SImode, cxt);
  10373. emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, quad0, 0),
  10374. gen_rtx_SUBREG (V2HImode, fnaddr, 0),
  10375. movishori));
  10376. emit_insn (gen_rotrdi3_mextr (quad0, quad0,
  10377. GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
  10378. emit_insn (gen_ashldi3_media (quad0, quad0, const2_rtx));
  10379. emit_move_insn (change_address (tramp_mem, DImode, NULL_RTX), quad0);
  10380. emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, cxtload, 0),
  10381. gen_rtx_SUBREG (V2HImode, cxt, 0),
  10382. movishori));
  10383. emit_insn (gen_rotrdi3_mextr (cxtload, cxtload,
  10384. GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
  10385. emit_insn (gen_ashldi3_media (cxtload, cxtload, const2_rtx));
  10386. if (TARGET_LITTLE_ENDIAN)
  10387. {
  10388. emit_insn (gen_mshflo_l_di (quad1, ptabs, cxtload));
  10389. emit_insn (gen_mextr4 (quad2, cxtload, blink));
  10390. }
  10391. else
  10392. {
  10393. emit_insn (gen_mextr4 (quad1, cxtload, ptabs));
  10394. emit_insn (gen_mshflo_l_di (quad2, blink, cxtload));
  10395. }
  10396. emit_move_insn (adjust_address (tramp_mem, DImode, 8), quad1);
  10397. emit_move_insn (adjust_address (tramp_mem, DImode, 16), quad2);
  10398. emit_insn (gen_ic_invalidate_line (tramp));
  10399. return;
  10400. }
  10401. else if (TARGET_SHCOMPACT)
  10402. {
  10403. emit_insn (gen_initialize_trampoline (tramp, cxt, fnaddr));
  10404. return;
  10405. }
  10406. emit_move_insn (change_address (tramp_mem, SImode, NULL_RTX),
  10407. gen_int_mode (TARGET_LITTLE_ENDIAN ? 0xd301d202 : 0xd202d301,
  10408. SImode));
  10409. emit_move_insn (adjust_address (tramp_mem, SImode, 4),
  10410. gen_int_mode (TARGET_LITTLE_ENDIAN ? 0x0009422b : 0x422b0009,
  10411. SImode));
  10412. emit_move_insn (adjust_address (tramp_mem, SImode, 8), cxt);
  10413. emit_move_insn (adjust_address (tramp_mem, SImode, 12), fnaddr);
  10414. if (TARGET_HARD_SH4 || TARGET_SH5)
  10415. {
  10416. if (!TARGET_INLINE_IC_INVALIDATE
  10417. || (!(TARGET_SH4A || TARGET_SH4_300) && TARGET_USERMODE))
  10418. emit_library_call (function_symbol (NULL, "__ic_invalidate",
  10419. FUNCTION_ORDINARY),
  10420. LCT_NORMAL, VOIDmode, 1, tramp, SImode);
  10421. else
  10422. emit_insn (gen_ic_invalidate_line (tramp));
  10423. }
  10424. }
  10425. /* On SH5, trampolines are SHmedia code, so add 1 to the address. */
  10426. static rtx
  10427. sh_trampoline_adjust_address (rtx tramp)
  10428. {
  10429. if (TARGET_SHMEDIA)
  10430. tramp = expand_simple_binop (Pmode, PLUS, tramp, const1_rtx,
  10431. gen_reg_rtx (Pmode), 0, OPTAB_LIB_WIDEN);
  10432. return tramp;
  10433. }
  10434. /* FIXME: This is overly conservative. A SHcompact function that
  10435. receives arguments ``by reference'' will have them stored in its
  10436. own stack frame, so it must not pass pointers or references to
  10437. these arguments to other functions by means of sibling calls. */
  10438. /* If PIC, we cannot make sibling calls to global functions
  10439. because the PLT requires r12 to be live. */
  10440. static bool
  10441. sh_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
  10442. {
  10443. return (1
  10444. && (! TARGET_SHCOMPACT
  10445. || crtl->args.info.stack_regs == 0)
  10446. && ! sh_cfun_interrupt_handler_p ()
  10447. && (! flag_pic
  10448. || (decl && ! TREE_PUBLIC (decl))
  10449. || (decl && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT)));
  10450. }
  10451. /* Machine specific built-in functions. */
  10452. struct builtin_description
  10453. {
  10454. bool (* const is_enabled) (void);
  10455. const enum insn_code icode;
  10456. const char *const name;
  10457. int signature;
  10458. tree fndecl;
  10459. };
  10460. static bool
  10461. shmedia_builtin_p (void)
  10462. {
  10463. return TARGET_SHMEDIA;
  10464. }
  10465. /* This function can be used if there are any built-ins that are not for
  10466. SHmedia. It's commented out to avoid the defined-but-unused warning. */
  10467. static bool
  10468. sh1_builtin_p (void)
  10469. {
  10470. return TARGET_SH1;
  10471. }
  10472. /* describe number and signedness of arguments; arg[0] == result
  10473. (1: unsigned, 2: signed, 4: don't care, 8: pointer 0: no argument */
  10474. /* 9: 64-bit pointer, 10: 32-bit pointer */
  10475. static const char signature_args[][4] =
  10476. {
  10477. #define SH_BLTIN_V2SI2 0
  10478. { 4, 4 },
  10479. #define SH_BLTIN_V4HI2 1
  10480. { 4, 4 },
  10481. #define SH_BLTIN_V2SI3 2
  10482. { 4, 4, 4 },
  10483. #define SH_BLTIN_V4HI3 3
  10484. { 4, 4, 4 },
  10485. #define SH_BLTIN_V8QI3 4
  10486. { 4, 4, 4 },
  10487. #define SH_BLTIN_MAC_HISI 5
  10488. { 1, 4, 4, 1 },
  10489. #define SH_BLTIN_SH_HI 6
  10490. { 4, 4, 1 },
  10491. #define SH_BLTIN_SH_SI 7
  10492. { 4, 4, 1 },
  10493. #define SH_BLTIN_V4HI2V2SI 8
  10494. { 4, 4, 4 },
  10495. #define SH_BLTIN_V4HI2V8QI 9
  10496. { 4, 4, 4 },
  10497. #define SH_BLTIN_SISF 10
  10498. { 4, 2 },
  10499. #define SH_BLTIN_LDUA_L 11
  10500. { 2, 10 },
  10501. #define SH_BLTIN_LDUA_Q 12
  10502. { 1, 10 },
  10503. #define SH_BLTIN_STUA_L 13
  10504. { 0, 10, 2 },
  10505. #define SH_BLTIN_STUA_Q 14
  10506. { 0, 10, 1 },
  10507. #define SH_BLTIN_LDUA_L64 15
  10508. { 2, 9 },
  10509. #define SH_BLTIN_LDUA_Q64 16
  10510. { 1, 9 },
  10511. #define SH_BLTIN_STUA_L64 17
  10512. { 0, 9, 2 },
  10513. #define SH_BLTIN_STUA_Q64 18
  10514. { 0, 9, 1 },
  10515. #define SH_BLTIN_NUM_SHARED_SIGNATURES 19
  10516. #define SH_BLTIN_2 19
  10517. #define SH_BLTIN_SU 19
  10518. { 1, 2 },
  10519. #define SH_BLTIN_3 20
  10520. #define SH_BLTIN_SUS 20
  10521. { 2, 2, 1 },
  10522. #define SH_BLTIN_PSSV 21
  10523. { 0, 8, 2, 2 },
  10524. #define SH_BLTIN_XXUU 22
  10525. #define SH_BLTIN_UUUU 22
  10526. { 1, 1, 1, 1 },
  10527. #define SH_BLTIN_PV 23
  10528. { 0, 8 },
  10529. #define SH_BLTIN_VP 24
  10530. { 8, 0 },
  10531. #define SH_BLTIN_UV 25
  10532. { 1, 0 },
  10533. #define SH_BLTIN_VU 26
  10534. { 0, 1 },
  10535. };
  10536. /* mcmv: operands considered unsigned. */
  10537. /* mmulsum_wq, msad_ubq: result considered unsigned long long. */
  10538. /* mperm: control value considered unsigned int. */
  10539. /* mshalds, mshard, mshards, mshlld, mshlrd: shift count is unsigned int. */
  10540. /* mshards_q: returns signed short. */
  10541. /* nsb: takes long long arg, returns unsigned char. */
  10542. static struct builtin_description bdesc[] =
  10543. {
  10544. { shmedia_builtin_p,
  10545. CODE_FOR_absv2si2, "__builtin_absv2si2", SH_BLTIN_V2SI2, 0 },
  10546. { shmedia_builtin_p,
  10547. CODE_FOR_absv4hi2, "__builtin_absv4hi2", SH_BLTIN_V4HI2, 0 },
  10548. { shmedia_builtin_p,
  10549. CODE_FOR_addv2si3, "__builtin_addv2si3", SH_BLTIN_V2SI3, 0 },
  10550. { shmedia_builtin_p,
  10551. CODE_FOR_addv4hi3, "__builtin_addv4hi3", SH_BLTIN_V4HI3, 0 },
  10552. { shmedia_builtin_p,
  10553. CODE_FOR_ssaddv2si3,"__builtin_ssaddv2si3", SH_BLTIN_V2SI3, 0 },
  10554. { shmedia_builtin_p,
  10555. CODE_FOR_usaddv8qi3,"__builtin_usaddv8qi3", SH_BLTIN_V8QI3, 0 },
  10556. { shmedia_builtin_p,
  10557. CODE_FOR_ssaddv4hi3,"__builtin_ssaddv4hi3", SH_BLTIN_V4HI3, 0 },
  10558. { shmedia_builtin_p,
  10559. CODE_FOR_alloco_i, "__builtin_sh_media_ALLOCO", SH_BLTIN_PV, 0 },
  10560. { shmedia_builtin_p,
  10561. CODE_FOR_negcmpeqv8qi,"__builtin_sh_media_MCMPEQ_B", SH_BLTIN_V8QI3, 0 },
  10562. { shmedia_builtin_p,
  10563. CODE_FOR_negcmpeqv2si,"__builtin_sh_media_MCMPEQ_L", SH_BLTIN_V2SI3, 0 },
  10564. { shmedia_builtin_p,
  10565. CODE_FOR_negcmpeqv4hi,"__builtin_sh_media_MCMPEQ_W", SH_BLTIN_V4HI3, 0 },
  10566. { shmedia_builtin_p,
  10567. CODE_FOR_negcmpgtuv8qi,"__builtin_sh_media_MCMPGT_UB", SH_BLTIN_V8QI3, 0 },
  10568. { shmedia_builtin_p,
  10569. CODE_FOR_negcmpgtv2si,"__builtin_sh_media_MCMPGT_L", SH_BLTIN_V2SI3, 0 },
  10570. { shmedia_builtin_p,
  10571. CODE_FOR_negcmpgtv4hi,"__builtin_sh_media_MCMPGT_W", SH_BLTIN_V4HI3, 0 },
  10572. { shmedia_builtin_p,
  10573. CODE_FOR_mcmv, "__builtin_sh_media_MCMV", SH_BLTIN_UUUU, 0 },
  10574. { shmedia_builtin_p,
  10575. CODE_FOR_mcnvs_lw, "__builtin_sh_media_MCNVS_LW", SH_BLTIN_3, 0 },
  10576. { shmedia_builtin_p,
  10577. CODE_FOR_mcnvs_wb, "__builtin_sh_media_MCNVS_WB", SH_BLTIN_V4HI2V8QI, 0 },
  10578. { shmedia_builtin_p,
  10579. CODE_FOR_mcnvs_wub, "__builtin_sh_media_MCNVS_WUB", SH_BLTIN_V4HI2V8QI, 0 },
  10580. { shmedia_builtin_p,
  10581. CODE_FOR_mextr1, "__builtin_sh_media_MEXTR1", SH_BLTIN_V8QI3, 0 },
  10582. { shmedia_builtin_p,
  10583. CODE_FOR_mextr2, "__builtin_sh_media_MEXTR2", SH_BLTIN_V8QI3, 0 },
  10584. { shmedia_builtin_p,
  10585. CODE_FOR_mextr3, "__builtin_sh_media_MEXTR3", SH_BLTIN_V8QI3, 0 },
  10586. { shmedia_builtin_p,
  10587. CODE_FOR_mextr4, "__builtin_sh_media_MEXTR4", SH_BLTIN_V8QI3, 0 },
  10588. { shmedia_builtin_p,
  10589. CODE_FOR_mextr5, "__builtin_sh_media_MEXTR5", SH_BLTIN_V8QI3, 0 },
  10590. { shmedia_builtin_p,
  10591. CODE_FOR_mextr6, "__builtin_sh_media_MEXTR6", SH_BLTIN_V8QI3, 0 },
  10592. { shmedia_builtin_p,
  10593. CODE_FOR_mextr7, "__builtin_sh_media_MEXTR7", SH_BLTIN_V8QI3, 0 },
  10594. { shmedia_builtin_p,
  10595. CODE_FOR_mmacfx_wl, "__builtin_sh_media_MMACFX_WL", SH_BLTIN_MAC_HISI, 0 },
  10596. { shmedia_builtin_p,
  10597. CODE_FOR_mmacnfx_wl,"__builtin_sh_media_MMACNFX_WL", SH_BLTIN_MAC_HISI, 0 },
  10598. { shmedia_builtin_p,
  10599. CODE_FOR_mulv2si3, "__builtin_mulv2si3", SH_BLTIN_V2SI3, 0 },
  10600. { shmedia_builtin_p,
  10601. CODE_FOR_mulv4hi3, "__builtin_mulv4hi3", SH_BLTIN_V4HI3, 0 },
  10602. { shmedia_builtin_p,
  10603. CODE_FOR_mmulfx_l, "__builtin_sh_media_MMULFX_L", SH_BLTIN_V2SI3, 0 },
  10604. { shmedia_builtin_p,
  10605. CODE_FOR_mmulfx_w, "__builtin_sh_media_MMULFX_W", SH_BLTIN_V4HI3, 0 },
  10606. { shmedia_builtin_p,
  10607. CODE_FOR_mmulfxrp_w,"__builtin_sh_media_MMULFXRP_W", SH_BLTIN_V4HI3, 0 },
  10608. { shmedia_builtin_p,
  10609. CODE_FOR_mmulhi_wl, "__builtin_sh_media_MMULHI_WL", SH_BLTIN_V4HI2V2SI, 0 },
  10610. { shmedia_builtin_p,
  10611. CODE_FOR_mmullo_wl, "__builtin_sh_media_MMULLO_WL", SH_BLTIN_V4HI2V2SI, 0 },
  10612. { shmedia_builtin_p,
  10613. CODE_FOR_mmulsum_wq,"__builtin_sh_media_MMULSUM_WQ", SH_BLTIN_XXUU, 0 },
  10614. { shmedia_builtin_p,
  10615. CODE_FOR_mperm_w, "__builtin_sh_media_MPERM_W", SH_BLTIN_SH_HI, 0 },
  10616. { shmedia_builtin_p,
  10617. CODE_FOR_msad_ubq, "__builtin_sh_media_MSAD_UBQ", SH_BLTIN_XXUU, 0 },
  10618. { shmedia_builtin_p,
  10619. CODE_FOR_mshalds_l, "__builtin_sh_media_MSHALDS_L", SH_BLTIN_SH_SI, 0 },
  10620. { shmedia_builtin_p,
  10621. CODE_FOR_mshalds_w, "__builtin_sh_media_MSHALDS_W", SH_BLTIN_SH_HI, 0 },
  10622. { shmedia_builtin_p,
  10623. CODE_FOR_ashrv2si3, "__builtin_ashrv2si3", SH_BLTIN_SH_SI, 0 },
  10624. { shmedia_builtin_p,
  10625. CODE_FOR_ashrv4hi3, "__builtin_ashrv4hi3", SH_BLTIN_SH_HI, 0 },
  10626. { shmedia_builtin_p,
  10627. CODE_FOR_mshards_q, "__builtin_sh_media_MSHARDS_Q", SH_BLTIN_SUS, 0 },
  10628. { shmedia_builtin_p,
  10629. CODE_FOR_mshfhi_b, "__builtin_sh_media_MSHFHI_B", SH_BLTIN_V8QI3, 0 },
  10630. { shmedia_builtin_p,
  10631. CODE_FOR_mshfhi_l, "__builtin_sh_media_MSHFHI_L", SH_BLTIN_V2SI3, 0 },
  10632. { shmedia_builtin_p,
  10633. CODE_FOR_mshfhi_w, "__builtin_sh_media_MSHFHI_W", SH_BLTIN_V4HI3, 0 },
  10634. { shmedia_builtin_p,
  10635. CODE_FOR_mshflo_b, "__builtin_sh_media_MSHFLO_B", SH_BLTIN_V8QI3, 0 },
  10636. { shmedia_builtin_p,
  10637. CODE_FOR_mshflo_l, "__builtin_sh_media_MSHFLO_L", SH_BLTIN_V2SI3, 0 },
  10638. { shmedia_builtin_p,
  10639. CODE_FOR_mshflo_w, "__builtin_sh_media_MSHFLO_W", SH_BLTIN_V4HI3, 0 },
  10640. { shmedia_builtin_p,
  10641. CODE_FOR_ashlv2si3, "__builtin_ashlv2si3", SH_BLTIN_SH_SI, 0 },
  10642. { shmedia_builtin_p,
  10643. CODE_FOR_ashlv4hi3, "__builtin_ashlv4hi3", SH_BLTIN_SH_HI, 0 },
  10644. { shmedia_builtin_p,
  10645. CODE_FOR_lshrv2si3, "__builtin_lshrv2si3", SH_BLTIN_SH_SI, 0 },
  10646. { shmedia_builtin_p,
  10647. CODE_FOR_lshrv4hi3, "__builtin_lshrv4hi3", SH_BLTIN_SH_HI, 0 },
  10648. { shmedia_builtin_p,
  10649. CODE_FOR_subv2si3, "__builtin_subv2si3", SH_BLTIN_V2SI3, 0 },
  10650. { shmedia_builtin_p,
  10651. CODE_FOR_subv4hi3, "__builtin_subv4hi3", SH_BLTIN_V4HI3, 0 },
  10652. { shmedia_builtin_p,
  10653. CODE_FOR_sssubv2si3,"__builtin_sssubv2si3", SH_BLTIN_V2SI3, 0 },
  10654. { shmedia_builtin_p,
  10655. CODE_FOR_ussubv8qi3,"__builtin_ussubv8qi3", SH_BLTIN_V8QI3, 0 },
  10656. { shmedia_builtin_p,
  10657. CODE_FOR_sssubv4hi3,"__builtin_sssubv4hi3", SH_BLTIN_V4HI3, 0 },
  10658. { shmedia_builtin_p,
  10659. CODE_FOR_fcosa_s, "__builtin_sh_media_FCOSA_S", SH_BLTIN_SISF, 0 },
  10660. { shmedia_builtin_p,
  10661. CODE_FOR_fsina_s, "__builtin_sh_media_FSINA_S", SH_BLTIN_SISF, 0 },
  10662. { shmedia_builtin_p,
  10663. CODE_FOR_fipr, "__builtin_sh_media_FIPR_S", SH_BLTIN_3, 0 },
  10664. { shmedia_builtin_p,
  10665. CODE_FOR_ftrv, "__builtin_sh_media_FTRV_S", SH_BLTIN_3, 0 },
  10666. { shmedia_builtin_p,
  10667. CODE_FOR_sqrtdf2, "__builtin_sh_media_FSQRT_D", SH_BLTIN_2, 0 },
  10668. { shmedia_builtin_p,
  10669. CODE_FOR_sqrtsf2, "__builtin_sh_media_FSQRT_S", SH_BLTIN_2, 0 },
  10670. { shmedia_builtin_p,
  10671. CODE_FOR_fsrra_s, "__builtin_sh_media_FSRRA_S", SH_BLTIN_2, 0 },
  10672. { shmedia_builtin_p,
  10673. CODE_FOR_ldhi_l, "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L, 0 },
  10674. { shmedia_builtin_p,
  10675. CODE_FOR_ldhi_q, "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q, 0 },
  10676. { shmedia_builtin_p,
  10677. CODE_FOR_ldlo_l, "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L, 0 },
  10678. { shmedia_builtin_p,
  10679. CODE_FOR_ldlo_q, "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q, 0 },
  10680. { shmedia_builtin_p,
  10681. CODE_FOR_sthi_l, "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L, 0 },
  10682. { shmedia_builtin_p,
  10683. CODE_FOR_sthi_q, "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q, 0 },
  10684. { shmedia_builtin_p,
  10685. CODE_FOR_stlo_l, "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L, 0 },
  10686. { shmedia_builtin_p,
  10687. CODE_FOR_stlo_q, "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q, 0 },
  10688. { shmedia_builtin_p,
  10689. CODE_FOR_ldhi_l64, "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L64, 0 },
  10690. { shmedia_builtin_p,
  10691. CODE_FOR_ldhi_q64, "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q64, 0 },
  10692. { shmedia_builtin_p,
  10693. CODE_FOR_ldlo_l64, "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L64, 0 },
  10694. { shmedia_builtin_p,
  10695. CODE_FOR_ldlo_q64, "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q64, 0 },
  10696. { shmedia_builtin_p,
  10697. CODE_FOR_sthi_l64, "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L64, 0 },
  10698. { shmedia_builtin_p,
  10699. CODE_FOR_sthi_q64, "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q64, 0 },
  10700. { shmedia_builtin_p,
  10701. CODE_FOR_stlo_l64, "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L64, 0 },
  10702. { shmedia_builtin_p,
  10703. CODE_FOR_stlo_q64, "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q64, 0 },
  10704. { shmedia_builtin_p,
  10705. CODE_FOR_nsb, "__builtin_sh_media_NSB", SH_BLTIN_SU, 0 },
  10706. { shmedia_builtin_p,
  10707. CODE_FOR_byterev, "__builtin_sh_media_BYTEREV", SH_BLTIN_2, 0 },
  10708. { shmedia_builtin_p,
  10709. CODE_FOR_prefetch, "__builtin_sh_media_PREFO", SH_BLTIN_PSSV, 0 },
  10710. { sh1_builtin_p,
  10711. CODE_FOR_sts_fpscr, "__builtin_sh_get_fpscr", SH_BLTIN_UV, 0 },
  10712. { sh1_builtin_p,
  10713. CODE_FOR_set_fpscr, "__builtin_sh_set_fpscr", SH_BLTIN_VU, 0 },
  10714. };
  10715. static tree sh_builtin_get_fpscr;
  10716. static tree sh_builtin_set_fpscr;
  10717. static void
  10718. sh_init_builtins (void)
  10719. {
  10720. tree shared[SH_BLTIN_NUM_SHARED_SIGNATURES];
  10721. memset (shared, 0, sizeof shared);
  10722. for (unsigned int di = 0; di < ARRAY_SIZE (bdesc); ++di)
  10723. {
  10724. builtin_description* d = &bdesc[di];
  10725. if (!d->is_enabled ())
  10726. continue;
  10727. tree type, arg_type = NULL_TREE;
  10728. int signature = d->signature;
  10729. if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES && shared[signature])
  10730. type = shared[signature];
  10731. else
  10732. {
  10733. int has_result = signature_args[signature][0] != 0;
  10734. tree args[3];
  10735. if ((signature_args[signature][1] & 8)
  10736. && (((signature_args[signature][1] & 1) && TARGET_SHMEDIA32)
  10737. || ((signature_args[signature][1] & 2) && TARGET_SHMEDIA64)))
  10738. continue;
  10739. if (! TARGET_FPU_ANY
  10740. && FLOAT_MODE_P (insn_data[d->icode].operand[0].mode))
  10741. continue;
  10742. for (unsigned int i = 0; i < ARRAY_SIZE (args); i++)
  10743. args[i] = NULL_TREE;
  10744. for (int i = 3; ; i--)
  10745. {
  10746. int arg = signature_args[signature][i];
  10747. int opno = i - 1 + has_result;
  10748. if (arg & 8)
  10749. arg_type = ptr_type_node;
  10750. else if (arg)
  10751. arg_type = (*lang_hooks.types.type_for_mode)
  10752. (insn_data[d->icode].operand[opno].mode, (arg & 1));
  10753. else if (i)
  10754. continue;
  10755. else
  10756. arg_type = void_type_node;
  10757. if (i == 0)
  10758. break;
  10759. args[i-1] = arg_type;
  10760. }
  10761. type = build_function_type_list (arg_type, args[0], args[1],
  10762. args[2], NULL_TREE);
  10763. if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES)
  10764. shared[signature] = type;
  10765. }
  10766. d->fndecl =
  10767. add_builtin_function (d->name, type, d - bdesc, BUILT_IN_MD,
  10768. NULL, NULL_TREE);
  10769. /* Recode {sts,set}_fpscr decls for sh_atomic_assign_expand_fenv. */
  10770. if (d->icode == CODE_FOR_sts_fpscr)
  10771. sh_builtin_get_fpscr = d->fndecl;
  10772. else if (d->icode == CODE_FOR_set_fpscr)
  10773. sh_builtin_set_fpscr = d->fndecl;
  10774. }
  10775. }
  10776. /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
  10777. static void
  10778. sh_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
  10779. {
  10780. const unsigned SH_FE_INVALID = 64;
  10781. const unsigned SH_FE_DIVBYZERO = 32;
  10782. const unsigned SH_FE_OVERFLOW = 16;
  10783. const unsigned SH_FE_UNDERFLOW = 8;
  10784. const unsigned SH_FE_INEXACT = 4;
  10785. const unsigned HOST_WIDE_INT SH_FE_ALL_EXCEPT = (SH_FE_INVALID
  10786. | SH_FE_DIVBYZERO
  10787. | SH_FE_OVERFLOW
  10788. | SH_FE_UNDERFLOW
  10789. | SH_FE_INEXACT);
  10790. const unsigned HOST_WIDE_INT SH_FE_EXCEPT_SHIFT = 5;
  10791. tree fenv_var, mask, ld_fenv, masked_fenv;
  10792. tree new_fenv_var, reload_fenv, restore_fnenv;
  10793. tree update_call, atomic_feraiseexcept, hold_fnclex;
  10794. if (! TARGET_FPU_ANY)
  10795. return;
  10796. /* Generate the equivalent of :
  10797. unsigned int fenv_var;
  10798. fenv_var = __builtin_sh_get_fpscr ();
  10799. unsigned int masked_fenv;
  10800. masked_fenv = fenv_var & mask;
  10801. __builtin_sh_set_fpscr (masked_fenv); */
  10802. fenv_var = create_tmp_var (unsigned_type_node);
  10803. mask = build_int_cst (unsigned_type_node,
  10804. ~((SH_FE_ALL_EXCEPT << SH_FE_EXCEPT_SHIFT)
  10805. | SH_FE_ALL_EXCEPT));
  10806. ld_fenv = build2 (MODIFY_EXPR, unsigned_type_node,
  10807. fenv_var, build_call_expr (sh_builtin_get_fpscr, 0));
  10808. masked_fenv = build2 (BIT_AND_EXPR, unsigned_type_node, fenv_var, mask);
  10809. hold_fnclex = build_call_expr (sh_builtin_set_fpscr, 1, masked_fenv);
  10810. *hold = build2 (COMPOUND_EXPR, void_type_node,
  10811. build2 (COMPOUND_EXPR, void_type_node, masked_fenv, ld_fenv),
  10812. hold_fnclex);
  10813. /* Store the value of masked_fenv to clear the exceptions:
  10814. __builtin_sh_set_fpscr (masked_fenv); */
  10815. *clear = build_call_expr (sh_builtin_set_fpscr, 1, masked_fenv);
  10816. /* Generate the equivalent of :
  10817. unsigned int new_fenv_var;
  10818. new_fenv_var = __builtin_sh_get_fpscr ();
  10819. __builtin_sh_set_fpscr (fenv_var);
  10820. __atomic_feraiseexcept (new_fenv_var); */
  10821. new_fenv_var = create_tmp_var (unsigned_type_node);
  10822. reload_fenv = build2 (MODIFY_EXPR, unsigned_type_node, new_fenv_var,
  10823. build_call_expr (sh_builtin_get_fpscr, 0));
  10824. restore_fnenv = build_call_expr (sh_builtin_set_fpscr, 1, fenv_var);
  10825. atomic_feraiseexcept = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
  10826. update_call = build_call_expr (atomic_feraiseexcept, 1,
  10827. fold_convert (integer_type_node,
  10828. new_fenv_var));
  10829. *update = build2 (COMPOUND_EXPR, void_type_node,
  10830. build2 (COMPOUND_EXPR, void_type_node,
  10831. reload_fenv, restore_fnenv), update_call);
  10832. }
  10833. /* Implements target hook vector_mode_supported_p. */
  10834. bool
  10835. sh_vector_mode_supported_p (machine_mode mode)
  10836. {
  10837. if (TARGET_FPU_ANY
  10838. && ((mode == V2SFmode)
  10839. || (mode == V4SFmode)
  10840. || (mode == V16SFmode)))
  10841. return true;
  10842. else if (TARGET_SHMEDIA
  10843. && ((mode == V8QImode)
  10844. || (mode == V2HImode)
  10845. || (mode == V4HImode)
  10846. || (mode == V2SImode)))
  10847. return true;
  10848. return false;
  10849. }
  10850. bool
  10851. sh_frame_pointer_required (void)
  10852. {
  10853. /* If needed override this in other tm.h files to cope with various OS
  10854. lossage requiring a frame pointer. */
  10855. if (SUBTARGET_FRAME_POINTER_REQUIRED)
  10856. return true;
  10857. if (crtl->profile)
  10858. return true;
  10859. return false;
  10860. }
  10861. /* Implements target hook dwarf_calling_convention. Return an enum
  10862. of dwarf_calling_convention. */
  10863. int
  10864. sh_dwarf_calling_convention (const_tree func)
  10865. {
  10866. if (sh_attr_renesas_p (func))
  10867. return DW_CC_GNU_renesas_sh;
  10868. return DW_CC_normal;
  10869. }
  10870. /* Returns the sh builtin decl for CODE. */
  10871. static tree
  10872. sh_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
  10873. {
  10874. if (code >= ARRAY_SIZE (bdesc))
  10875. return error_mark_node;
  10876. if (!bdesc[code].is_enabled ())
  10877. return error_mark_node;
  10878. return bdesc[code].fndecl;
  10879. }
  10880. /* Expand an expression EXP that calls a built-in function,
  10881. with result going to TARGET if that's convenient
  10882. (and in mode MODE if that's convenient).
  10883. SUBTARGET may be used as the target for computing one of EXP's operands.
  10884. IGNORE is nonzero if the value is to be ignored. */
  10885. static rtx
  10886. sh_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
  10887. machine_mode mode ATTRIBUTE_UNUSED, int ignore)
  10888. {
  10889. tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
  10890. unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
  10891. const struct builtin_description *d = &bdesc[fcode];
  10892. enum insn_code icode = d->icode;
  10893. int signature = d->signature;
  10894. int nop = 0;
  10895. rtx op[4];
  10896. if (signature_args[signature][0])
  10897. {
  10898. if (ignore)
  10899. return NULL_RTX;
  10900. machine_mode tmode = insn_data[icode].operand[0].mode;
  10901. if (! target || GET_MODE (target) != tmode
  10902. || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
  10903. target = gen_reg_rtx (tmode);
  10904. op[nop++] = target;
  10905. }
  10906. else
  10907. target = NULL_RTX;
  10908. for (int i = 1; i <= 3; i++, nop++)
  10909. {
  10910. tree arg;
  10911. machine_mode opmode, argmode;
  10912. tree optype;
  10913. if (! signature_args[signature][i])
  10914. break;
  10915. arg = CALL_EXPR_ARG (exp, i - 1);
  10916. if (arg == error_mark_node)
  10917. return const0_rtx;
  10918. if (signature_args[signature][i] & 8)
  10919. {
  10920. opmode = ptr_mode;
  10921. optype = ptr_type_node;
  10922. }
  10923. else
  10924. {
  10925. opmode = insn_data[icode].operand[nop].mode;
  10926. optype = (*lang_hooks.types.type_for_mode) (opmode, 0);
  10927. }
  10928. argmode = TYPE_MODE (TREE_TYPE (arg));
  10929. if (argmode != opmode)
  10930. arg = build1 (NOP_EXPR, optype, arg);
  10931. op[nop] = expand_expr (arg, NULL_RTX, opmode, EXPAND_NORMAL);
  10932. if (! (*insn_data[icode].operand[nop].predicate) (op[nop], opmode))
  10933. op[nop] = copy_to_mode_reg (opmode, op[nop]);
  10934. }
  10935. rtx pat = NULL_RTX;
  10936. switch (nop)
  10937. {
  10938. case 1:
  10939. pat = (*insn_data[d->icode].genfun) (op[0]);
  10940. break;
  10941. case 2:
  10942. pat = (*insn_data[d->icode].genfun) (op[0], op[1]);
  10943. break;
  10944. case 3:
  10945. pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2]);
  10946. break;
  10947. case 4:
  10948. pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2], op[3]);
  10949. break;
  10950. default:
  10951. gcc_unreachable ();
  10952. }
  10953. if (! pat)
  10954. return NULL_RTX;
  10955. emit_insn (pat);
  10956. return target;
  10957. }
  10958. void
  10959. sh_expand_unop_v2sf (enum rtx_code code, rtx op0, rtx op1)
  10960. {
  10961. rtx sel0 = const0_rtx;
  10962. rtx sel1 = const1_rtx;
  10963. rtx (*fn) (rtx, rtx, rtx, rtx, rtx) = gen_unary_sf_op;
  10964. rtx op = gen_rtx_fmt_e (code, SFmode, op1);
  10965. emit_insn ((*fn) (op0, op1, op, sel0, sel0));
  10966. emit_insn ((*fn) (op0, op1, op, sel1, sel1));
  10967. }
  10968. void
  10969. sh_expand_binop_v2sf (enum rtx_code code, rtx op0, rtx op1, rtx op2)
  10970. {
  10971. rtx op = gen_rtx_fmt_ee (code, SFmode, op1, op2);
  10972. emit_insn (gen_binary_sf_op0 (op0, op1, op2, op));
  10973. emit_insn (gen_binary_sf_op1 (op0, op1, op2, op));
  10974. }
  10975. /* Return true if hard register REGNO can hold a value of machine-mode MODE.
  10976. We can allow any mode in any general register. The special registers
  10977. only allow SImode. Don't allow any mode in the PR.
  10978. We cannot hold DCmode values in the XD registers because alter_reg
  10979. handles subregs of them incorrectly. We could work around this by
  10980. spacing the XD registers like the DR registers, but this would require
  10981. additional memory in every compilation to hold larger register vectors.
  10982. We could hold SFmode / SCmode values in XD registers, but that
  10983. would require a tertiary reload when reloading from / to memory,
  10984. and a secondary reload to reload from / to general regs; that
  10985. seems to be a losing proposition.
  10986. We want to allow TImode FP regs so that when V4SFmode is loaded as TImode,
  10987. it won't be ferried through GP registers first. */
  10988. bool
  10989. sh_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
  10990. {
  10991. if (SPECIAL_REGISTER_P (regno))
  10992. return mode == SImode;
  10993. if (regno == FPUL_REG)
  10994. return (mode == SImode || mode == SFmode);
  10995. if (FP_REGISTER_P (regno) && mode == SFmode)
  10996. return true;
  10997. if (mode == V2SFmode)
  10998. {
  10999. if (((FP_REGISTER_P (regno) && (regno - FIRST_FP_REG) % 2 == 0)
  11000. || GENERAL_REGISTER_P (regno)))
  11001. return true;
  11002. else
  11003. return false;
  11004. }
  11005. if (mode == V4SFmode)
  11006. {
  11007. if ((FP_REGISTER_P (regno) && (regno - FIRST_FP_REG) % 4 == 0)
  11008. || GENERAL_REGISTER_P (regno))
  11009. return true;
  11010. else
  11011. return false;
  11012. }
  11013. if (mode == V16SFmode)
  11014. {
  11015. if (TARGET_SHMEDIA)
  11016. {
  11017. if (FP_REGISTER_P (regno) && (regno - FIRST_FP_REG) % 16 == 0)
  11018. return true;
  11019. else
  11020. return false;
  11021. }
  11022. else
  11023. return regno == FIRST_XD_REG;
  11024. }
  11025. if (FP_REGISTER_P (regno))
  11026. {
  11027. if (mode == SFmode
  11028. || mode == SImode
  11029. || ((TARGET_SH2E || TARGET_SHMEDIA) && mode == SCmode)
  11030. || ((((TARGET_SH4 || TARGET_SH2A_DOUBLE) && mode == DFmode)
  11031. || mode == DCmode
  11032. || (TARGET_SHMEDIA
  11033. && (mode == DFmode || mode == DImode
  11034. || mode == V2SFmode || mode == TImode)))
  11035. && ((regno - FIRST_FP_REG) & 1) == 0)
  11036. || ((TARGET_SH4 || TARGET_SHMEDIA) && mode == TImode
  11037. && ((regno - FIRST_FP_REG) & 3) == 0))
  11038. return true;
  11039. else
  11040. return false;
  11041. }
  11042. if (XD_REGISTER_P (regno))
  11043. return mode == DFmode;
  11044. if (TARGET_REGISTER_P (regno))
  11045. return (mode == DImode || mode == SImode || mode == PDImode);
  11046. if (regno == PR_REG)
  11047. return mode == SImode;
  11048. if (regno == FPSCR_REG)
  11049. return mode == SImode;
  11050. /* FIXME. This works around PR target/37633 for -O0. */
  11051. if (!optimize && TARGET_SHMEDIA32 && GET_MODE_SIZE (mode) > 4)
  11052. {
  11053. unsigned int n = GET_MODE_SIZE (mode) / 8;
  11054. if (regno >= FIRST_GENERAL_REG + 10 - n + 1
  11055. && regno <= FIRST_GENERAL_REG + 14)
  11056. return false;
  11057. }
  11058. return true;
  11059. }
  11060. /* Specify the modes required to caller save a given hard regno.
  11061. choose_hard_reg_mode chooses mode based on HARD_REGNO_MODE_OK
  11062. and returns ?Imode for float regs when sh_hard_regno_mode_ok
  11063. permits integer modes on them. That makes LRA's split process
  11064. unhappy. See PR55212.
  11065. */
  11066. machine_mode
  11067. sh_hard_regno_caller_save_mode (unsigned int regno, unsigned int nregs,
  11068. machine_mode mode)
  11069. {
  11070. if (FP_REGISTER_P (regno)
  11071. && (mode == SFmode
  11072. || mode == SCmode
  11073. || ((mode == DFmode || mode == DCmode)
  11074. && ((regno - FIRST_FP_REG) & 1) == 0)))
  11075. return mode;
  11076. return choose_hard_reg_mode (regno, nregs, false);
  11077. }
  11078. /* Return the class of registers for which a mode change from FROM to TO
  11079. is invalid. */
  11080. bool
  11081. sh_cannot_change_mode_class (machine_mode from, machine_mode to,
  11082. enum reg_class rclass)
  11083. {
  11084. /* We want to enable the use of SUBREGs as a means to
  11085. VEC_SELECT a single element of a vector. */
  11086. /* This effectively disallows using GENERAL_REGS for SFmode vector subregs.
  11087. This can be problematic when SFmode vector subregs need to be accessed
  11088. on the stack with displacement addressing, as it happens with -O0.
  11089. Thus we disallow the mode change for -O0. */
  11090. if (to == SFmode && VECTOR_MODE_P (from) && GET_MODE_INNER (from) == SFmode)
  11091. return optimize ? (reg_classes_intersect_p (GENERAL_REGS, rclass)) : false;
  11092. if (GET_MODE_SIZE (from) != GET_MODE_SIZE (to))
  11093. {
  11094. if (TARGET_LITTLE_ENDIAN)
  11095. {
  11096. if (GET_MODE_SIZE (to) < 8 || GET_MODE_SIZE (from) < 8)
  11097. return reg_classes_intersect_p (DF_REGS, rclass);
  11098. }
  11099. else
  11100. {
  11101. if (GET_MODE_SIZE (from) < 8)
  11102. return reg_classes_intersect_p (DF_REGS, rclass);
  11103. }
  11104. }
  11105. return false;
  11106. }
  11107. /* Return true if registers in machine mode MODE will likely be
  11108. allocated to registers in small register classes. */
  11109. bool
  11110. sh_small_register_classes_for_mode_p (machine_mode mode ATTRIBUTE_UNUSED)
  11111. {
  11112. return (! TARGET_SHMEDIA);
  11113. }
  11114. /* If ADDRESS refers to a CODE_LABEL, add NUSES to the number of times
  11115. that label is used. */
  11116. void
  11117. sh_mark_label (rtx address, int nuses)
  11118. {
  11119. if (GOTOFF_P (address))
  11120. {
  11121. /* Extract the label or symbol. */
  11122. address = XEXP (address, 0);
  11123. if (GET_CODE (address) == PLUS)
  11124. address = XEXP (address, 0);
  11125. address = XVECEXP (address, 0, 0);
  11126. }
  11127. if (GET_CODE (address) == LABEL_REF
  11128. && LABEL_P (XEXP (address, 0)))
  11129. LABEL_NUSES (XEXP (address, 0)) += nuses;
  11130. }
  11131. /* Compute extra cost of moving data between one register class
  11132. and another.
  11133. If SECONDARY*_RELOAD_CLASS says something about the src/dst pair, regclass
  11134. uses this information. Hence, the general register <-> floating point
  11135. register information here is not used for SFmode. */
  11136. static int
  11137. sh_register_move_cost (machine_mode mode,
  11138. reg_class_t srcclass, reg_class_t dstclass)
  11139. {
  11140. if (dstclass == T_REGS || dstclass == PR_REGS)
  11141. return 10;
  11142. if (dstclass == MAC_REGS && srcclass == MAC_REGS)
  11143. return 4;
  11144. if (mode == SImode && ! TARGET_SHMEDIA && TARGET_FMOVD
  11145. && REGCLASS_HAS_FP_REG (srcclass)
  11146. && REGCLASS_HAS_FP_REG (dstclass))
  11147. return 4;
  11148. if (REGCLASS_HAS_FP_REG (dstclass) && srcclass == T_REGS)
  11149. return ((TARGET_HARD_SH4 && !optimize_size) ? 10 : 7);
  11150. if ((REGCLASS_HAS_FP_REG (dstclass) && srcclass == MAC_REGS)
  11151. || (dstclass == MAC_REGS && REGCLASS_HAS_FP_REG (srcclass)))
  11152. return 9;
  11153. if ((REGCLASS_HAS_FP_REG (dstclass)
  11154. && REGCLASS_HAS_GENERAL_REG (srcclass))
  11155. || (REGCLASS_HAS_GENERAL_REG (dstclass)
  11156. && REGCLASS_HAS_FP_REG (srcclass)))
  11157. {
  11158. /* Discourage trying to use fp regs for a pointer. This also
  11159. discourages fp regs with SImode because Pmode is an alias
  11160. of SImode on this target. See PR target/48596. */
  11161. int addend = (mode == Pmode) ? 40 : 0;
  11162. return (((TARGET_SHMEDIA ? 4 : TARGET_FMOVD ? 8 : 12) + addend)
  11163. * ((GET_MODE_SIZE (mode) + 7) / 8U));
  11164. }
  11165. if ((dstclass == FPUL_REGS
  11166. && REGCLASS_HAS_GENERAL_REG (srcclass))
  11167. || (srcclass == FPUL_REGS
  11168. && REGCLASS_HAS_GENERAL_REG (dstclass)))
  11169. return 5;
  11170. if ((dstclass == FPUL_REGS
  11171. && (srcclass == PR_REGS || srcclass == MAC_REGS || srcclass == T_REGS))
  11172. || (srcclass == FPUL_REGS
  11173. && (dstclass == PR_REGS || dstclass == MAC_REGS)))
  11174. return 7;
  11175. if ((srcclass == TARGET_REGS && ! REGCLASS_HAS_GENERAL_REG (dstclass))
  11176. || ((dstclass) == TARGET_REGS && ! REGCLASS_HAS_GENERAL_REG (srcclass)))
  11177. return 20;
  11178. /* ??? ptabs faults on (value & 0x3) == 0x3 */
  11179. if (TARGET_SHMEDIA
  11180. && ((srcclass) == TARGET_REGS || (srcclass) == SIBCALL_REGS))
  11181. {
  11182. if (sh_gettrcost >= 0)
  11183. return sh_gettrcost;
  11184. else if (!TARGET_PT_FIXED)
  11185. return 100;
  11186. }
  11187. if ((srcclass == FPSCR_REGS && ! REGCLASS_HAS_GENERAL_REG (dstclass))
  11188. || (dstclass == FPSCR_REGS && ! REGCLASS_HAS_GENERAL_REG (srcclass)))
  11189. return 4;
  11190. if (TARGET_SHMEDIA
  11191. || (TARGET_FMOVD
  11192. && ! REGCLASS_HAS_GENERAL_REG (srcclass)
  11193. && ! REGCLASS_HAS_GENERAL_REG (dstclass)))
  11194. return 2 * ((GET_MODE_SIZE (mode) + 7) / 8U);
  11195. return 2 * ((GET_MODE_SIZE (mode) + 3) / 4U);
  11196. }
  11197. static rtx
  11198. emit_load_ptr (rtx reg, rtx addr)
  11199. {
  11200. rtx mem = gen_const_mem (ptr_mode, addr);
  11201. if (Pmode != ptr_mode)
  11202. mem = gen_rtx_SIGN_EXTEND (Pmode, mem);
  11203. return emit_move_insn (reg, mem);
  11204. }
  11205. static void
  11206. sh_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
  11207. HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
  11208. tree function)
  11209. {
  11210. CUMULATIVE_ARGS cum;
  11211. int structure_value_byref = 0;
  11212. rtx this_rtx, this_value, sibcall, funexp;
  11213. rtx_insn *insns;
  11214. tree funtype = TREE_TYPE (function);
  11215. int simple_add = CONST_OK_FOR_ADD (delta);
  11216. int did_load = 0;
  11217. rtx scratch0, scratch1, scratch2;
  11218. unsigned i;
  11219. reload_completed = 1;
  11220. epilogue_completed = 1;
  11221. crtl->uses_only_leaf_regs = 1;
  11222. emit_note (NOTE_INSN_PROLOGUE_END);
  11223. /* Find the "this" pointer. We have such a wide range of ABIs for the
  11224. SH that it's best to do this completely machine independently.
  11225. "this" is passed as first argument, unless a structure return pointer
  11226. comes first, in which case "this" comes second. */
  11227. INIT_CUMULATIVE_ARGS (cum, funtype, NULL_RTX, 0, 1);
  11228. #ifndef PCC_STATIC_STRUCT_RETURN
  11229. if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
  11230. structure_value_byref = 1;
  11231. #endif /* not PCC_STATIC_STRUCT_RETURN */
  11232. if (structure_value_byref && sh_struct_value_rtx (function, 0) == 0)
  11233. {
  11234. tree ptype = build_pointer_type (TREE_TYPE (funtype));
  11235. sh_function_arg_advance (pack_cumulative_args (&cum), Pmode, ptype, true);
  11236. }
  11237. this_rtx
  11238. = sh_function_arg (pack_cumulative_args (&cum), Pmode, ptr_type_node, true);
  11239. /* For SHcompact, we only have r0 for a scratch register: r1 is the
  11240. static chain pointer (even if you can't have nested virtual functions
  11241. right now, someone might implement them sometime), and the rest of the
  11242. registers are used for argument passing, are callee-saved, or reserved. */
  11243. /* We need to check call_used_regs / fixed_regs in case -fcall_saved-reg /
  11244. -ffixed-reg has been used. */
  11245. if (! call_used_regs[0] || fixed_regs[0])
  11246. error ("r0 needs to be available as a call-clobbered register");
  11247. scratch0 = scratch1 = scratch2 = gen_rtx_REG (Pmode, 0);
  11248. if (! TARGET_SH5)
  11249. {
  11250. if (call_used_regs[1] && ! fixed_regs[1])
  11251. scratch1 = gen_rtx_REG (ptr_mode, 1);
  11252. /* N.B., if not TARGET_HITACHI, register 2 is used to pass the pointer
  11253. pointing where to return struct values. */
  11254. if (call_used_regs[3] && ! fixed_regs[3])
  11255. scratch2 = gen_rtx_REG (Pmode, 3);
  11256. }
  11257. else if (TARGET_SHMEDIA)
  11258. {
  11259. for (i = FIRST_GENERAL_REG; i <= LAST_GENERAL_REG; i++)
  11260. if (i != REGNO (scratch0) &&
  11261. call_used_regs[i] && ! fixed_regs[i] && ! FUNCTION_ARG_REGNO_P (i))
  11262. {
  11263. scratch1 = gen_rtx_REG (ptr_mode, i);
  11264. break;
  11265. }
  11266. if (scratch1 == scratch0)
  11267. error ("need a second call-clobbered general purpose register");
  11268. for (i = FIRST_TARGET_REG; i <= LAST_TARGET_REG; i++)
  11269. if (call_used_regs[i] && ! fixed_regs[i])
  11270. {
  11271. scratch2 = gen_rtx_REG (Pmode, i);
  11272. break;
  11273. }
  11274. if (scratch2 == scratch0)
  11275. error ("need a call-clobbered target register");
  11276. }
  11277. this_value = plus_constant (Pmode, this_rtx, delta);
  11278. if (vcall_offset
  11279. && (simple_add || scratch0 != scratch1)
  11280. && strict_memory_address_p (ptr_mode, this_value))
  11281. {
  11282. emit_load_ptr (scratch0, this_value);
  11283. did_load = 1;
  11284. }
  11285. if (!delta)
  11286. ; /* Do nothing. */
  11287. else if (simple_add)
  11288. emit_move_insn (this_rtx, this_value);
  11289. else
  11290. {
  11291. emit_move_insn (scratch1, GEN_INT (delta));
  11292. emit_insn (gen_add2_insn (this_rtx, scratch1));
  11293. }
  11294. if (vcall_offset)
  11295. {
  11296. rtx offset_addr;
  11297. if (!did_load)
  11298. emit_load_ptr (scratch0, this_rtx);
  11299. offset_addr = plus_constant (Pmode, scratch0, vcall_offset);
  11300. if (strict_memory_address_p (ptr_mode, offset_addr))
  11301. ; /* Do nothing. */
  11302. else if (! TARGET_SH5 && scratch0 != scratch1)
  11303. {
  11304. /* scratch0 != scratch1, and we have indexed loads. Get better
  11305. schedule by loading the offset into r1 and using an indexed
  11306. load - then the load of r1 can issue before the load from
  11307. (this_rtx + delta) finishes. */
  11308. emit_move_insn (scratch1, GEN_INT (vcall_offset));
  11309. offset_addr = gen_rtx_PLUS (Pmode, scratch0, scratch1);
  11310. }
  11311. else if (CONST_OK_FOR_ADD (vcall_offset))
  11312. {
  11313. emit_insn (gen_add2_insn (scratch0, GEN_INT (vcall_offset)));
  11314. offset_addr = scratch0;
  11315. }
  11316. else if (scratch0 != scratch1)
  11317. {
  11318. emit_move_insn (scratch1, GEN_INT (vcall_offset));
  11319. emit_insn (gen_add2_insn (scratch0, scratch1));
  11320. offset_addr = scratch0;
  11321. }
  11322. else
  11323. gcc_unreachable (); /* FIXME */
  11324. emit_load_ptr (scratch0, offset_addr);
  11325. if (Pmode != ptr_mode)
  11326. scratch0 = gen_rtx_TRUNCATE (ptr_mode, scratch0);
  11327. emit_insn (gen_add2_insn (this_rtx, scratch0));
  11328. }
  11329. /* Generate a tail call to the target function. */
  11330. if (! TREE_USED (function))
  11331. {
  11332. assemble_external (function);
  11333. TREE_USED (function) = 1;
  11334. }
  11335. funexp = XEXP (DECL_RTL (function), 0);
  11336. /* If the function is overridden, so is the thunk, hence we don't
  11337. need GOT addressing even if this is a public symbol. */
  11338. #if 0
  11339. if (TARGET_SH1 && ! flag_weak)
  11340. sibcall = gen_sibcalli_thunk (funexp, const0_rtx);
  11341. else
  11342. #endif
  11343. if (TARGET_SH2 && flag_pic)
  11344. {
  11345. sibcall = gen_sibcall_pcrel (funexp, const0_rtx);
  11346. XEXP (XVECEXP (sibcall, 0, 2), 0) = scratch2;
  11347. }
  11348. else
  11349. {
  11350. if (TARGET_SHMEDIA && flag_pic)
  11351. {
  11352. funexp = gen_sym2PIC (funexp);
  11353. PUT_MODE (funexp, Pmode);
  11354. }
  11355. emit_move_insn (scratch2, funexp);
  11356. funexp = gen_rtx_MEM (FUNCTION_MODE, scratch2);
  11357. sibcall = gen_sibcall (funexp, const0_rtx, NULL_RTX);
  11358. }
  11359. sibcall = emit_call_insn (sibcall);
  11360. SIBLING_CALL_P (sibcall) = 1;
  11361. use_reg (&CALL_INSN_FUNCTION_USAGE (sibcall), this_rtx);
  11362. emit_barrier ();
  11363. /* Run just enough of rest_of_compilation to do scheduling and get
  11364. the insns emitted. Note that use_thunk calls
  11365. assemble_start_function and assemble_end_function. */
  11366. insns = get_insns ();
  11367. if (optimize > 0)
  11368. {
  11369. if (! cfun->cfg)
  11370. init_flow (cfun);
  11371. split_all_insns_noflow ();
  11372. }
  11373. sh_reorg ();
  11374. shorten_branches (insns);
  11375. final_start_function (insns, file, 1);
  11376. final (insns, file, 1);
  11377. final_end_function ();
  11378. reload_completed = 0;
  11379. epilogue_completed = 0;
  11380. }
  11381. rtx
  11382. function_symbol (rtx target, const char *name, enum sh_function_kind kind)
  11383. {
  11384. rtx sym;
  11385. /* If this is not an ordinary function, the name usually comes from a
  11386. string literal or an sprintf buffer. Make sure we use the same
  11387. string consistently, so that cse will be able to unify address loads. */
  11388. if (kind != FUNCTION_ORDINARY)
  11389. name = IDENTIFIER_POINTER (get_identifier (name));
  11390. sym = gen_rtx_SYMBOL_REF (Pmode, name);
  11391. SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_FUNCTION;
  11392. if (flag_pic)
  11393. switch (kind)
  11394. {
  11395. case FUNCTION_ORDINARY:
  11396. break;
  11397. case SFUNC_GOT:
  11398. {
  11399. rtx reg = target ? target : gen_reg_rtx (Pmode);
  11400. emit_insn (gen_symGOT2reg (reg, sym));
  11401. sym = reg;
  11402. break;
  11403. }
  11404. case SFUNC_STATIC:
  11405. {
  11406. /* ??? To allow cse to work, we use GOTOFF relocations.
  11407. We could add combiner patterns to transform this into
  11408. straight pc-relative calls with sym2PIC / bsrf when
  11409. label load and function call are still 1:1 and in the
  11410. same basic block during combine. */
  11411. rtx reg = target ? target : gen_reg_rtx (Pmode);
  11412. emit_insn (gen_symGOTOFF2reg (reg, sym));
  11413. sym = reg;
  11414. break;
  11415. }
  11416. }
  11417. if (target && sym != target)
  11418. {
  11419. emit_move_insn (target, sym);
  11420. return target;
  11421. }
  11422. return sym;
  11423. }
  11424. /* Find the number of a general purpose register in S. */
  11425. static int
  11426. scavenge_reg (HARD_REG_SET *s)
  11427. {
  11428. int r;
  11429. for (r = FIRST_GENERAL_REG; r <= LAST_GENERAL_REG; r++)
  11430. if (TEST_HARD_REG_BIT (*s, r))
  11431. return r;
  11432. return -1;
  11433. }
  11434. rtx
  11435. sh_get_pr_initial_val (void)
  11436. {
  11437. rtx val;
  11438. /* ??? Unfortunately, get_hard_reg_initial_val doesn't always work for the
  11439. PR register on SHcompact, because it might be clobbered by the prologue.
  11440. We check first if that is known to be the case. */
  11441. if (TARGET_SHCOMPACT
  11442. && ((crtl->args.info.call_cookie
  11443. & ~ CALL_COOKIE_RET_TRAMP (1))
  11444. || crtl->saves_all_registers))
  11445. return gen_frame_mem (SImode, return_address_pointer_rtx);
  11446. /* If we haven't finished rtl generation, there might be a nonlocal label
  11447. that we haven't seen yet.
  11448. ??? get_hard_reg_initial_val fails if it is called after register
  11449. allocation has started, unless it has been called before for the
  11450. same register. And even then, we end in trouble if we didn't use
  11451. the register in the same basic block before. So call
  11452. get_hard_reg_initial_val now and wrap it in an unspec if we might
  11453. need to replace it. */
  11454. /* ??? We also must do this for TARGET_SH1 in general, because otherwise
  11455. combine can put the pseudo returned by get_hard_reg_initial_val into
  11456. instructions that need a general purpose registers, which will fail to
  11457. be recognized when the pseudo becomes allocated to PR. */
  11458. val
  11459. = get_hard_reg_initial_val (Pmode, TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG);
  11460. if (TARGET_SH1)
  11461. return gen_rtx_UNSPEC (SImode, gen_rtvec (1, val), UNSPEC_RA);
  11462. return val;
  11463. }
  11464. bool
  11465. sh_expand_t_scc (rtx operands[])
  11466. {
  11467. enum rtx_code code = GET_CODE (operands[1]);
  11468. rtx target = operands[0];
  11469. rtx op0 = operands[2];
  11470. rtx op1 = operands[3];
  11471. rtx result = target;
  11472. HOST_WIDE_INT val;
  11473. if (!REG_P (op0) || REGNO (op0) != T_REG
  11474. || !CONST_INT_P (op1))
  11475. return false;
  11476. if (!REG_P (result))
  11477. result = gen_reg_rtx (SImode);
  11478. val = INTVAL (op1);
  11479. if ((code == EQ && val == 1) || (code == NE && val == 0))
  11480. emit_insn (gen_movt (result, get_t_reg_rtx ()));
  11481. else if ((code == EQ && val == 0) || (code == NE && val == 1))
  11482. emit_insn (gen_movnegt (result, get_t_reg_rtx ()));
  11483. else if (code == EQ || code == NE)
  11484. emit_insn (gen_move_insn (result, GEN_INT (code == NE)));
  11485. else
  11486. return false;
  11487. if (result != target)
  11488. emit_move_insn (target, result);
  11489. return true;
  11490. }
  11491. /* INSN is an sfunc; return the rtx that describes the address used. */
  11492. static rtx
  11493. extract_sfunc_addr (rtx insn)
  11494. {
  11495. rtx pattern, part = NULL_RTX;
  11496. int len, i;
  11497. pattern = PATTERN (insn);
  11498. len = XVECLEN (pattern, 0);
  11499. for (i = 0; i < len; i++)
  11500. {
  11501. part = XVECEXP (pattern, 0, i);
  11502. if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == Pmode
  11503. && GENERAL_REGISTER_P (true_regnum (XEXP (part, 0))))
  11504. return XEXP (part, 0);
  11505. }
  11506. gcc_assert (GET_CODE (XVECEXP (pattern, 0, 0)) == UNSPEC_VOLATILE);
  11507. return XVECEXP (XVECEXP (pattern, 0, 0), 0, 1);
  11508. }
  11509. /* Verify that the register in use_sfunc_addr still agrees with the address
  11510. used in the sfunc. This prevents fill_slots_from_thread from changing
  11511. use_sfunc_addr.
  11512. INSN is the use_sfunc_addr instruction, and REG is the register it
  11513. guards. */
  11514. bool
  11515. check_use_sfunc_addr (rtx_insn *insn, rtx reg)
  11516. {
  11517. /* Search for the sfunc. It should really come right after INSN. */
  11518. while ((insn = NEXT_INSN (insn)))
  11519. {
  11520. if (LABEL_P (insn) || JUMP_P (insn))
  11521. break;
  11522. if (! INSN_P (insn))
  11523. continue;
  11524. if (rtx_sequence *seq = dyn_cast<rtx_sequence *> (PATTERN (insn)))
  11525. insn = seq->insn (0);
  11526. if (GET_CODE (PATTERN (insn)) != PARALLEL
  11527. || get_attr_type (insn) != TYPE_SFUNC)
  11528. continue;
  11529. return rtx_equal_p (extract_sfunc_addr (insn), reg);
  11530. }
  11531. gcc_unreachable ();
  11532. }
  11533. /* This function returns a constant rtx that represents 2**15 / pi in
  11534. SFmode. It's used to scale a fixed-point signed 16.16-bit fraction
  11535. of a full circle back to an SFmode value, i.e. 0x10000 maps to 2*pi. */
  11536. static GTY(()) rtx sh_fsca_sf2int_rtx;
  11537. rtx
  11538. sh_fsca_sf2int (void)
  11539. {
  11540. if (! sh_fsca_sf2int_rtx)
  11541. {
  11542. REAL_VALUE_TYPE rv;
  11543. real_from_string (&rv, "10430.378350470453");
  11544. sh_fsca_sf2int_rtx = const_double_from_real_value (rv, SFmode);
  11545. }
  11546. return sh_fsca_sf2int_rtx;
  11547. }
  11548. /* This function returns a constant rtx that represents pi / 2**15 in
  11549. SFmode. It's used to scale SFmode angles, in radians, to a
  11550. fixed-point signed 16.16-bit fraction of a full circle, i.e. 2*pi
  11551. maps to 0x10000. */
  11552. static GTY(()) rtx sh_fsca_int2sf_rtx;
  11553. rtx
  11554. sh_fsca_int2sf (void)
  11555. {
  11556. if (! sh_fsca_int2sf_rtx)
  11557. {
  11558. REAL_VALUE_TYPE rv;
  11559. real_from_string (&rv, "9.587379924285257e-5");
  11560. sh_fsca_int2sf_rtx = const_double_from_real_value (rv, SFmode);
  11561. }
  11562. return sh_fsca_int2sf_rtx;
  11563. }
  11564. /* Initialize the CUMULATIVE_ARGS structure. */
  11565. void
  11566. sh_init_cumulative_args (CUMULATIVE_ARGS * pcum,
  11567. tree fntype,
  11568. rtx libname ATTRIBUTE_UNUSED,
  11569. tree fndecl,
  11570. signed int n_named_args,
  11571. machine_mode mode)
  11572. {
  11573. pcum->arg_count [(int) SH_ARG_FLOAT] = 0;
  11574. pcum->free_single_fp_reg = 0;
  11575. pcum->stack_regs = 0;
  11576. pcum->byref_regs = 0;
  11577. pcum->byref = 0;
  11578. pcum->outgoing = (n_named_args == -1) ? 0 : 1;
  11579. /* XXX - Should we check TARGET_HITACHI here ??? */
  11580. pcum->renesas_abi = sh_attr_renesas_p (fntype) ? 1 : 0;
  11581. if (fntype)
  11582. {
  11583. pcum->force_mem = ((TARGET_HITACHI || pcum->renesas_abi)
  11584. && aggregate_value_p (TREE_TYPE (fntype), fndecl));
  11585. pcum->prototype_p = prototype_p (fntype);
  11586. pcum->arg_count [(int) SH_ARG_INT]
  11587. = TARGET_SH5 && aggregate_value_p (TREE_TYPE (fntype), fndecl);
  11588. pcum->call_cookie
  11589. = CALL_COOKIE_RET_TRAMP (TARGET_SHCOMPACT
  11590. && pcum->arg_count [(int) SH_ARG_INT] == 0
  11591. && (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
  11592. ? int_size_in_bytes (TREE_TYPE (fntype))
  11593. : GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (fntype)))) > 4
  11594. && (BASE_RETURN_VALUE_REG (TYPE_MODE (TREE_TYPE (fntype)))
  11595. == FIRST_RET_REG));
  11596. }
  11597. else
  11598. {
  11599. pcum->arg_count [(int) SH_ARG_INT] = 0;
  11600. pcum->prototype_p = FALSE;
  11601. if (mode != VOIDmode)
  11602. {
  11603. pcum->call_cookie =
  11604. CALL_COOKIE_RET_TRAMP (TARGET_SHCOMPACT
  11605. && GET_MODE_SIZE (mode) > 4
  11606. && BASE_RETURN_VALUE_REG (mode) == FIRST_RET_REG);
  11607. /* If the default ABI is the Renesas ABI then all library
  11608. calls must assume that the library will be using the
  11609. Renesas ABI. So if the function would return its result
  11610. in memory then we must force the address of this memory
  11611. block onto the stack. Ideally we would like to call
  11612. targetm.calls.return_in_memory() here but we do not have
  11613. the TYPE or the FNDECL available so we synthesize the
  11614. contents of that function as best we can. */
  11615. pcum->force_mem =
  11616. (TARGET_DEFAULT & MASK_HITACHI)
  11617. && (mode == BLKmode
  11618. || (GET_MODE_SIZE (mode) > 4
  11619. && !(mode == DFmode
  11620. && TARGET_FPU_DOUBLE)));
  11621. }
  11622. else
  11623. {
  11624. pcum->call_cookie = 0;
  11625. pcum->force_mem = FALSE;
  11626. }
  11627. }
  11628. }
  11629. rtx
  11630. sh_gen_truncate (machine_mode mode, rtx x, int need_sign_ext)
  11631. {
  11632. enum rtx_code code = TRUNCATE;
  11633. if (GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
  11634. {
  11635. rtx inner = XEXP (x, 0);
  11636. machine_mode inner_mode = GET_MODE (inner);
  11637. if (inner_mode == mode)
  11638. return inner;
  11639. else if (GET_MODE_SIZE (inner_mode) >= GET_MODE_SIZE (mode))
  11640. x = inner;
  11641. else if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode)
  11642. && (! need_sign_ext || GET_CODE (x) == SIGN_EXTEND))
  11643. {
  11644. code = GET_CODE (x);
  11645. x = inner;
  11646. }
  11647. }
  11648. return gen_rtx_fmt_e (code, mode, x);
  11649. }
  11650. /* Look through X cleaning up truncates of registers that span multiple
  11651. actual hard registers. Return the number of changes made. */
  11652. int
  11653. shmedia_cleanup_truncate (rtx x)
  11654. {
  11655. int n_changes = 0;
  11656. subrtx_var_iterator::array_type array;
  11657. FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
  11658. {
  11659. rtx x = *iter;
  11660. if (GET_CODE (x) == TRUNCATE)
  11661. {
  11662. rtx reg = XEXP (x, 0);
  11663. machine_mode reg_mode = GET_MODE (reg);
  11664. if (REG_P (reg) && GET_MODE_SIZE (reg_mode) > 8)
  11665. {
  11666. int offset = subreg_lowpart_offset (DImode, reg_mode);
  11667. XEXP (x, 0) = simplify_subreg (DImode, reg, reg_mode, offset);
  11668. n_changes += 1;
  11669. iter.skip_subrtxes ();
  11670. }
  11671. }
  11672. }
  11673. return n_changes;
  11674. }
  11675. /* Load and store depend on the highpart of the address. However,
  11676. set_attr_alternative does not give well-defined results before reload,
  11677. so we must look at the rtl ourselves to see if any of the feeding
  11678. registers is used in a memref.
  11679. Return true iff INSN contains a MEM. */
  11680. bool
  11681. sh_contains_memref_p (rtx insn)
  11682. {
  11683. subrtx_iterator::array_type array;
  11684. FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
  11685. if (MEM_P (*iter))
  11686. return true;
  11687. return false;
  11688. }
  11689. /* Return true iff INSN loads a banked register. */
  11690. bool
  11691. sh_loads_bankedreg_p (rtx insn)
  11692. {
  11693. if (GET_CODE (PATTERN (insn)) == SET)
  11694. {
  11695. rtx op = SET_DEST (PATTERN(insn));
  11696. if (REG_P (op) && BANKED_REGISTER_P (REGNO (op)))
  11697. return true;
  11698. }
  11699. return false;
  11700. }
  11701. /* FNADDR is the MEM expression from a call expander. Return an address
  11702. to use in an SHmedia insn pattern. */
  11703. rtx
  11704. shmedia_prepare_call_address (rtx fnaddr, int is_sibcall)
  11705. {
  11706. int is_sym;
  11707. fnaddr = XEXP (fnaddr, 0);
  11708. is_sym = GET_CODE (fnaddr) == SYMBOL_REF;
  11709. if (flag_pic && is_sym)
  11710. {
  11711. if (! SYMBOL_REF_LOCAL_P (fnaddr))
  11712. {
  11713. rtx reg = gen_reg_rtx (Pmode);
  11714. /* We must not use GOTPLT for sibcalls, because PIC_REG
  11715. must be restored before the PLT code gets to run. */
  11716. if (is_sibcall)
  11717. emit_insn (gen_symGOT2reg (reg, fnaddr));
  11718. else
  11719. emit_insn (gen_symGOTPLT2reg (reg, fnaddr));
  11720. fnaddr = reg;
  11721. }
  11722. else
  11723. {
  11724. fnaddr = gen_sym2PIC (fnaddr);
  11725. PUT_MODE (fnaddr, Pmode);
  11726. }
  11727. }
  11728. /* If ptabs might trap, make this visible to the rest of the compiler.
  11729. We generally assume that symbols pertain to valid locations, but
  11730. it is possible to generate invalid symbols with asm or linker tricks.
  11731. In a list of functions where each returns its successor, an invalid
  11732. symbol might denote an empty list. */
  11733. if (!TARGET_PT_FIXED
  11734. && (!is_sym || TARGET_INVALID_SYMBOLS)
  11735. && (!REG_P (fnaddr) || ! TARGET_REGISTER_P (REGNO (fnaddr))))
  11736. {
  11737. rtx tr = gen_reg_rtx (PDImode);
  11738. emit_insn (gen_ptabs (tr, fnaddr));
  11739. fnaddr = tr;
  11740. }
  11741. else if (! target_reg_operand (fnaddr, Pmode))
  11742. fnaddr = copy_to_mode_reg (Pmode, fnaddr);
  11743. return fnaddr;
  11744. }
  11745. /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
  11746. static reg_class_t
  11747. sh_preferred_reload_class (rtx x, reg_class_t rclass)
  11748. {
  11749. if (rclass == NO_REGS
  11750. && TARGET_SHMEDIA
  11751. && (CONST_DOUBLE_P (x)
  11752. || GET_CODE (x) == SYMBOL_REF
  11753. || PIC_ADDR_P (x)))
  11754. return GENERAL_REGS;
  11755. return rclass;
  11756. }
  11757. /* Implement TARGET_SECONDARY_RELOAD. */
  11758. static reg_class_t
  11759. sh_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
  11760. machine_mode mode, secondary_reload_info *sri)
  11761. {
  11762. enum reg_class rclass = (enum reg_class) rclass_i;
  11763. if (MEM_P (x) && GET_CODE (XEXP (x, 0)) == PLUS
  11764. && REG_P (XEXP (XEXP (x, 0), 0))
  11765. && REGNO (XEXP (XEXP (x, 0), 0)) == GBR_REG)
  11766. return rclass == R0_REGS ? NO_REGS : R0_REGS;
  11767. if (MEM_P (x) && REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) == GBR_REG)
  11768. return rclass == R0_REGS ? NO_REGS : R0_REGS;
  11769. if (REG_P (x) && REGNO (x) == GBR_REG)
  11770. return NO_REGS;
  11771. if (in_p)
  11772. {
  11773. if (REGCLASS_HAS_FP_REG (rclass)
  11774. && ! TARGET_SHMEDIA
  11775. && immediate_operand ((x), mode)
  11776. && ! ((fp_zero_operand (x) || fp_one_operand (x)) && mode == SFmode))
  11777. switch (mode)
  11778. {
  11779. case SFmode:
  11780. sri->icode = CODE_FOR_reload_insf__frn;
  11781. return NO_REGS;
  11782. case DFmode:
  11783. sri->icode = CODE_FOR_reload_indf__frn;
  11784. return NO_REGS;
  11785. case SImode:
  11786. /* ??? If we knew that we are in the appropriate mode -
  11787. single precision - we could use a reload pattern directly. */
  11788. return FPUL_REGS;
  11789. default:
  11790. abort ();
  11791. }
  11792. if (rclass == FPUL_REGS
  11793. && ((REG_P (x) && (REGNO (x) == MACL_REG || REGNO (x) == MACH_REG
  11794. || REGNO (x) == T_REG))
  11795. || GET_CODE (x) == PLUS))
  11796. return GENERAL_REGS;
  11797. if (rclass == FPUL_REGS && immediate_operand (x, mode))
  11798. {
  11799. if (satisfies_constraint_I08 (x) || fp_zero_operand (x))
  11800. return GENERAL_REGS;
  11801. else if (mode == SFmode)
  11802. return FP_REGS;
  11803. sri->icode = CODE_FOR_reload_insi__i_fpul;
  11804. return NO_REGS;
  11805. }
  11806. if (rclass == FPSCR_REGS
  11807. && ((REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
  11808. || (MEM_P (x) && GET_CODE (XEXP (x, 0)) == PLUS)))
  11809. return GENERAL_REGS;
  11810. if (REGCLASS_HAS_FP_REG (rclass)
  11811. && TARGET_SHMEDIA
  11812. && immediate_operand (x, mode)
  11813. && x != CONST0_RTX (GET_MODE (x))
  11814. && GET_MODE (x) != V4SFmode)
  11815. return GENERAL_REGS;
  11816. if ((mode == QImode || mode == HImode)
  11817. && TARGET_SHMEDIA && inqhi_operand (x, mode))
  11818. {
  11819. sri->icode = ((mode == QImode)
  11820. ? CODE_FOR_reload_inqi : CODE_FOR_reload_inhi);
  11821. return NO_REGS;
  11822. }
  11823. if (TARGET_SHMEDIA && rclass == GENERAL_REGS
  11824. && (GET_CODE (x) == LABEL_REF || PIC_ADDR_P (x)))
  11825. return TARGET_REGS;
  11826. } /* end of input-only processing. */
  11827. if (((REGCLASS_HAS_FP_REG (rclass)
  11828. && (REG_P (x)
  11829. && (GENERAL_OR_AP_REGISTER_P (REGNO (x))
  11830. || (FP_REGISTER_P (REGNO (x)) && mode == SImode
  11831. && TARGET_FMOVD))))
  11832. || (REGCLASS_HAS_GENERAL_REG (rclass)
  11833. && REG_P (x)
  11834. && FP_REGISTER_P (REGNO (x))))
  11835. && ! TARGET_SHMEDIA
  11836. && (mode == SFmode || mode == SImode))
  11837. return FPUL_REGS;
  11838. if ((rclass == FPUL_REGS
  11839. || (REGCLASS_HAS_FP_REG (rclass)
  11840. && ! TARGET_SHMEDIA && mode == SImode))
  11841. && (MEM_P (x)
  11842. || (REG_P (x)
  11843. && (REGNO (x) >= FIRST_PSEUDO_REGISTER
  11844. || REGNO (x) == T_REG
  11845. || system_reg_operand (x, VOIDmode)))))
  11846. {
  11847. if (rclass == FPUL_REGS)
  11848. return GENERAL_REGS;
  11849. return NO_REGS; // LRA wants NO_REGS here, it used to be FPUL_REGS;
  11850. }
  11851. if ((rclass == TARGET_REGS
  11852. || (TARGET_SHMEDIA && rclass == SIBCALL_REGS))
  11853. && !satisfies_constraint_Csy (x)
  11854. && (!REG_P (x) || ! GENERAL_REGISTER_P (REGNO (x))))
  11855. return GENERAL_REGS;
  11856. if ((rclass == MAC_REGS || rclass == PR_REGS)
  11857. && REG_P (x) && ! GENERAL_REGISTER_P (REGNO (x))
  11858. && rclass != REGNO_REG_CLASS (REGNO (x)))
  11859. return GENERAL_REGS;
  11860. if (rclass != GENERAL_REGS && REG_P (x)
  11861. && TARGET_REGISTER_P (REGNO (x)))
  11862. return GENERAL_REGS;
  11863. /* If here fall back to loading FPUL register through general registers.
  11864. This case can happen when movsi_ie insn is picked initially to
  11865. load/store the FPUL register from/to another register, and then the
  11866. other register is allocated on the stack. */
  11867. if (rclass == FPUL_REGS && true_regnum (x) == -1)
  11868. return GENERAL_REGS;
  11869. /* Force mov.b / mov.w displacement addressing insn to use R0 as
  11870. the other operand.
  11871. On SH2A could also just leave it alone here, which would result in a
  11872. 4 byte move insn being generated instead. However, for this to work
  11873. the insns must have the appropriate alternatives. */
  11874. if ((mode == QImode || mode == HImode) && rclass != R0_REGS
  11875. && satisfies_constraint_Sdd (x)
  11876. && sh_disp_addr_displacement (x)
  11877. <= sh_max_mov_insn_displacement (mode, false))
  11878. return R0_REGS;
  11879. /* When reload is trying to address a QImode or HImode subreg on the stack,
  11880. force any subreg byte into R0_REGS, as this is going to become a
  11881. displacement address.
  11882. We could restrict this to SUBREG_BYTE (x) > 0, but if the actual reg
  11883. is on the stack, the memref to it might already require a displacement
  11884. and that has to be added to the final address. At this point we don't
  11885. know the cumulative displacement so we assume the worst case. */
  11886. if ((mode == QImode || mode == HImode) && rclass != R0_REGS
  11887. && GET_CODE (x) == SUBREG && true_regnum (x) == -1)
  11888. return R0_REGS;
  11889. return NO_REGS;
  11890. }
  11891. /* Return true if SUBST can't safely replace its equivalent during RA. */
  11892. static bool
  11893. sh_cannot_substitute_mem_equiv_p (rtx)
  11894. {
  11895. if (TARGET_SHMEDIA)
  11896. return false;
  11897. /* If SUBST is mem[base+index] or QI/HImode mem[base+disp], the insn
  11898. uses R0 and may cause spill failure when R0 is already used.
  11899. We have to return true for that case at least.
  11900. Moreover SH has strong R0 parity and also have not enough numbers of
  11901. the hard registers to make the equiv substitution win in the size
  11902. and the speed on average working sets. The pseudos produced to
  11903. hold the equiv values can't get good hard registers for bad cases
  11904. and end up memory save/restore insns which make the code worse. */
  11905. return true;
  11906. }
  11907. /* Return true if DISP can be legitimized. */
  11908. static bool
  11909. sh_legitimize_address_displacement (rtx *disp, rtx *offs,
  11910. machine_mode mode)
  11911. {
  11912. if (TARGET_SHMEDIA)
  11913. return false;
  11914. if (((TARGET_SH4 || TARGET_SH2A_DOUBLE) && mode == DFmode)
  11915. || (TARGET_SH2E && mode == SFmode))
  11916. return false;
  11917. struct disp_adjust adj = sh_find_mov_disp_adjust (mode, INTVAL (*disp));
  11918. if (adj.offset_adjust != NULL_RTX && adj.mov_disp != NULL_RTX)
  11919. {
  11920. *disp = adj.mov_disp;
  11921. *offs = adj.offset_adjust;
  11922. return true;
  11923. }
  11924. return false;
  11925. }
  11926. /* Return true if movsf insn should be splited with an additional
  11927. register. */
  11928. bool
  11929. sh_movsf_ie_ra_split_p (rtx op0, rtx op1, rtx op2)
  11930. {
  11931. /* op0 == op1 */
  11932. if (rtx_equal_p (op0, op1))
  11933. return true;
  11934. /* fy, FQ, reg */
  11935. if (GET_CODE (op1) == CONST_DOUBLE
  11936. && ! satisfies_constraint_G (op1)
  11937. && ! satisfies_constraint_H (op1)
  11938. && REG_P (op0)
  11939. && REG_P (op2))
  11940. return true;
  11941. /* f, r, y */
  11942. if (REG_P (op0) && FP_REGISTER_P (REGNO (op0))
  11943. && REG_P (op1) && GENERAL_REGISTER_P (REGNO (op1))
  11944. && REG_P (op2) && (REGNO (op2) == FPUL_REG))
  11945. return true;
  11946. /* r, f, y */
  11947. if (REG_P (op1) && FP_REGISTER_P (REGNO (op1))
  11948. && REG_P (op0) && GENERAL_REGISTER_P (REGNO (op0))
  11949. && REG_P (op2) && (REGNO (op2) == FPUL_REG))
  11950. return true;
  11951. return false;
  11952. }
  11953. static void
  11954. sh_conditional_register_usage (void)
  11955. {
  11956. int regno;
  11957. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno ++)
  11958. if (! VALID_REGISTER_P (regno))
  11959. fixed_regs[regno] = call_used_regs[regno] = 1;
  11960. /* R8 and R9 are call-clobbered on SH5, but not on earlier SH ABIs. */
  11961. if (TARGET_SH5)
  11962. {
  11963. call_used_regs[FIRST_GENERAL_REG + 8]
  11964. = call_used_regs[FIRST_GENERAL_REG + 9] = 1;
  11965. call_really_used_regs[FIRST_GENERAL_REG + 8]
  11966. = call_really_used_regs[FIRST_GENERAL_REG + 9] = 1;
  11967. }
  11968. if (TARGET_SHMEDIA)
  11969. {
  11970. regno_reg_class[FIRST_GENERAL_REG] = GENERAL_REGS;
  11971. CLEAR_HARD_REG_SET (reg_class_contents[FP0_REGS]);
  11972. regno_reg_class[FIRST_FP_REG] = FP_REGS;
  11973. }
  11974. if (flag_pic)
  11975. {
  11976. fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
  11977. call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
  11978. }
  11979. /* Renesas saves and restores mac registers on call. */
  11980. if (TARGET_HITACHI && ! TARGET_NOMACSAVE)
  11981. {
  11982. call_really_used_regs[MACH_REG] = 0;
  11983. call_really_used_regs[MACL_REG] = 0;
  11984. }
  11985. if (TARGET_SHMEDIA)
  11986. {
  11987. for (regno = FIRST_TARGET_REG; regno <= LAST_TARGET_REG; regno ++)
  11988. if (! fixed_regs[regno] && call_really_used_regs[regno])
  11989. SET_HARD_REG_BIT (reg_class_contents[SIBCALL_REGS], regno);
  11990. }
  11991. else
  11992. for (regno = FIRST_GENERAL_REG; regno <= LAST_GENERAL_REG; regno++)
  11993. if (! fixed_regs[regno] && call_really_used_regs[regno])
  11994. SET_HARD_REG_BIT (reg_class_contents[SIBCALL_REGS], regno);
  11995. call_really_used_regs[FPSCR_MODES_REG] = 0;
  11996. call_really_used_regs[FPSCR_STAT_REG] = 0;
  11997. }
  11998. /* Implement TARGET_LEGITIMATE_CONSTANT_P
  11999. can_store_by_pieces constructs VOIDmode CONST_DOUBLEs. */
  12000. static bool
  12001. sh_legitimate_constant_p (machine_mode mode, rtx x)
  12002. {
  12003. return (TARGET_SHMEDIA
  12004. ? ((mode != DFmode && GET_MODE_CLASS (mode) != MODE_VECTOR_FLOAT)
  12005. || x == CONST0_RTX (mode)
  12006. || !TARGET_SHMEDIA_FPU
  12007. || TARGET_SHMEDIA64)
  12008. : (GET_CODE (x) != CONST_DOUBLE
  12009. || mode == DFmode || mode == SFmode
  12010. || mode == DImode || GET_MODE (x) == VOIDmode));
  12011. }
  12012. enum sh_divide_strategy_e sh_div_strategy = SH_DIV_STRATEGY_DEFAULT;
  12013. static void
  12014. sh_init_sync_libfuncs (void)
  12015. {
  12016. init_sync_libfuncs (UNITS_PER_WORD);
  12017. }
  12018. /* Return true if it is appropriate to emit `ret' instructions in the
  12019. body of a function. */
  12020. bool
  12021. sh_can_use_simple_return_p (void)
  12022. {
  12023. HARD_REG_SET live_regs_mask;
  12024. int d;
  12025. /* Some targets require special return insns. */
  12026. if (TARGET_SHMEDIA
  12027. || (TARGET_SHCOMPACT
  12028. && (crtl->args.info.call_cookie & CALL_COOKIE_RET_TRAMP (1))))
  12029. return false;
  12030. if (! reload_completed || frame_pointer_needed)
  12031. return false;
  12032. /* Moving prologue around does't reduce the size. */
  12033. if (optimize_function_for_size_p (cfun))
  12034. return false;
  12035. /* Finally, allow for pr save. */
  12036. d = calc_live_regs (&live_regs_mask);
  12037. if (rounded_frame_size (d) > 4)
  12038. return false;
  12039. return true;
  12040. }
  12041. /*------------------------------------------------------------------------------
  12042. Address mode optimization support code
  12043. */
  12044. typedef HOST_WIDE_INT disp_t;
  12045. static const disp_t MIN_DISP = HOST_WIDE_INT_MIN;
  12046. static const disp_t MAX_DISP = HOST_WIDE_INT_MAX;
  12047. static const disp_t INVALID_DISP = MAX_DISP;
  12048. /* A memory reference which is described by a base register and a
  12049. displacement. */
  12050. class base_reg_disp
  12051. {
  12052. public:
  12053. base_reg_disp (rtx br, disp_t d);
  12054. bool is_reg (void) const;
  12055. bool is_disp (void) const;
  12056. rtx reg (void) const;
  12057. disp_t disp (void) const;
  12058. private:
  12059. rtx reg_;
  12060. disp_t disp_;
  12061. };
  12062. inline
  12063. base_reg_disp::base_reg_disp (rtx br, disp_t d)
  12064. : reg_ (br), disp_ (d)
  12065. {
  12066. }
  12067. inline bool
  12068. base_reg_disp::is_reg (void) const
  12069. {
  12070. return reg_ != NULL_RTX && disp_ != INVALID_DISP;
  12071. }
  12072. inline bool
  12073. base_reg_disp::is_disp (void) const
  12074. {
  12075. return reg_ == NULL_RTX && disp_ != INVALID_DISP;
  12076. }
  12077. inline rtx
  12078. base_reg_disp::reg (void) const
  12079. {
  12080. return reg_;
  12081. }
  12082. inline disp_t
  12083. base_reg_disp::disp (void) const
  12084. {
  12085. return disp_;
  12086. }
  12087. /* Find the base register and calculate the displacement for a given
  12088. address rtx 'x'. */
  12089. static base_reg_disp
  12090. sh_find_base_reg_disp (rtx_insn* insn, rtx x, disp_t disp = 0,
  12091. rtx base_reg = NULL)
  12092. {
  12093. if (REG_P (x))
  12094. {
  12095. if (REGNO (x) == GBR_REG)
  12096. return base_reg_disp (x, disp);
  12097. /* We've reached a hard-reg. This is probably the point where
  12098. function args are copied to pseudos. Do not go any further and
  12099. stick to the pseudo. If the original mem addr was in a hard reg
  12100. from the beginning, it will become the base reg. */
  12101. if (REGNO (x) < FIRST_PSEUDO_REGISTER)
  12102. return base_reg_disp (base_reg != NULL ? base_reg : x, disp);
  12103. /* Find the def of the reg and trace it. If there are more than one
  12104. defs and they are not the same, assume it's not safe to proceed. */
  12105. rtx_insn* last_i = NULL;
  12106. rtx last_set = NULL;
  12107. for (df_ref d = DF_REG_DEF_CHAIN (REGNO (x)); d != NULL;
  12108. d = DF_REF_NEXT_REG (d))
  12109. {
  12110. rtx set = const_cast<rtx> (set_of (x, DF_REF_INSN (d)));
  12111. /* Accept multiple defs, as long as they are equal. */
  12112. if (last_set == NULL || rtx_equal_p (last_set, set))
  12113. {
  12114. last_i = DF_REF_INSN (d);
  12115. last_set = set;
  12116. }
  12117. else
  12118. {
  12119. last_i = NULL;
  12120. last_set = NULL;
  12121. break;
  12122. }
  12123. }
  12124. if (last_set != NULL && last_i != NULL)
  12125. return sh_find_base_reg_disp (last_i, XEXP (last_set, 1), disp,
  12126. XEXP (last_set, 0));
  12127. /* When here, no previous insn was found that sets the reg.
  12128. The input reg is already the base reg. */
  12129. return base_reg_disp (x, disp);
  12130. }
  12131. else if (GET_CODE (x) == PLUS)
  12132. {
  12133. base_reg_disp left_val = sh_find_base_reg_disp (insn, XEXP (x, 0));
  12134. base_reg_disp right_val = sh_find_base_reg_disp (insn, XEXP (x, 1));
  12135. /* Either left or right val must be a reg.
  12136. We don't handle the case of 'reg + reg' here. */
  12137. if (left_val.is_reg () && right_val.is_disp ())
  12138. return base_reg_disp (left_val.reg (), left_val.disp ()
  12139. + right_val.disp () + disp);
  12140. else if (right_val.is_reg () && left_val.is_disp ())
  12141. return base_reg_disp (right_val.reg (), right_val.disp ()
  12142. + left_val.disp () + disp);
  12143. else
  12144. return base_reg_disp (base_reg, disp);
  12145. }
  12146. else if (CONST_INT_P (x))
  12147. return base_reg_disp (NULL, disp + INTVAL (x));
  12148. /* Didn't find anything useful. */
  12149. return base_reg_disp (base_reg, disp);
  12150. }
  12151. /* Given an insn and a memory operand, try to find an equivalent GBR
  12152. based memory address and return the corresponding new memory address.
  12153. Return NULL_RTX if not found. */
  12154. rtx
  12155. sh_find_equiv_gbr_addr (rtx_insn* insn, rtx mem)
  12156. {
  12157. if (!MEM_P (mem) || gbr_address_mem (mem, GET_MODE (mem)))
  12158. return NULL_RTX;
  12159. /* Leave post/pre inc/dec or any other side effect addresses alone. */
  12160. if (side_effects_p (XEXP (mem, 0)))
  12161. return NULL_RTX;
  12162. /* When not optimizing there might be no dataflow available. */
  12163. if (df == NULL)
  12164. return NULL_RTX;
  12165. base_reg_disp gbr_disp = sh_find_base_reg_disp (insn, XEXP (mem, 0));
  12166. if (gbr_disp.is_reg () && REGNO (gbr_disp.reg ()) == GBR_REG)
  12167. {
  12168. /* If GBR is marked as call clobbered we bail out if we see a call.
  12169. FIXME: Actually should check if this mem refers to the gbr value
  12170. before or after the call. If there is a store_gbr preceeding this
  12171. mem, it's safe to use GBR for this mem.
  12172. If GBR is not marked as call clobbered, but there is some other
  12173. def than a call, it's probably a load_gbr upon which we also
  12174. bail out to be on the safe side.
  12175. FIXME: Should check if we have a use-after-def case, such as
  12176. the call case above. */
  12177. for (df_ref d = DF_REG_DEF_CHAIN (GBR_REG); d != NULL;
  12178. d = DF_REF_NEXT_REG (d))
  12179. {
  12180. if (CALL_P (DF_REF_INSN (d)))
  12181. {
  12182. if (REGNO_REG_SET_P (regs_invalidated_by_call_regset, GBR_REG))
  12183. return NULL_RTX;
  12184. else
  12185. continue;
  12186. }
  12187. else
  12188. return NULL_RTX;
  12189. }
  12190. rtx disp = GEN_INT (gbr_disp.disp ());
  12191. if (gbr_displacement (disp, GET_MODE (mem)))
  12192. return gen_rtx_PLUS (SImode, gen_rtx_REG (SImode, GBR_REG), disp);
  12193. }
  12194. return NULL_RTX;
  12195. }
  12196. /*------------------------------------------------------------------------------
  12197. Manual insn combine support code.
  12198. */
  12199. /* Return true if the specified insn contains any UNSPECs or
  12200. UNSPEC_VOLATILEs. */
  12201. static bool
  12202. sh_unspec_insn_p (rtx x)
  12203. {
  12204. subrtx_iterator::array_type array;
  12205. FOR_EACH_SUBRTX (i, array, x, ALL)
  12206. if (*i != NULL
  12207. && (GET_CODE (*i) == UNSPEC || GET_CODE (*i) == UNSPEC_VOLATILE))
  12208. return true;
  12209. return false;
  12210. }
  12211. /* Return true if the register operands of the specified insn are modified
  12212. between the specified from and to insns (exclusive of those two). */
  12213. bool
  12214. sh_insn_operands_modified_between_p (rtx_insn* operands_insn,
  12215. const rtx_insn* from,
  12216. const rtx_insn* to)
  12217. {
  12218. /* FIXME: Return true for multiple sets for now. */
  12219. rtx s = single_set (operands_insn);
  12220. if (s == NULL_RTX)
  12221. return true;
  12222. subrtx_iterator::array_type array;
  12223. FOR_EACH_SUBRTX (i, array, SET_SRC (s), ALL)
  12224. if (*i != NULL &&
  12225. ((REG_P (*i) || SUBREG_P (*i)) && reg_set_between_p (*i, from, to)))
  12226. return true;
  12227. return false;
  12228. }
  12229. /* Given an insn, determine whether it's a 'nott' insn, i.e. an insn that
  12230. negates the T bit and stores the result in the T bit. */
  12231. bool
  12232. sh_is_nott_insn (const rtx_insn* i)
  12233. {
  12234. return i != NULL && GET_CODE (PATTERN (i)) == SET
  12235. && t_reg_operand (XEXP (PATTERN (i), 0), VOIDmode)
  12236. && negt_reg_operand (XEXP (PATTERN (i), 1), VOIDmode);
  12237. }
  12238. rtx
  12239. sh_movt_set_dest (const rtx_insn* i)
  12240. {
  12241. if (i == NULL)
  12242. return NULL;
  12243. const_rtx p = PATTERN (i);
  12244. return GET_CODE (p) == SET
  12245. && arith_reg_dest (XEXP (p, 0), SImode)
  12246. && t_reg_operand (XEXP (p, 1), VOIDmode) ? XEXP (p, 0) : NULL;
  12247. }
  12248. /* Given an insn, check whether it's a 'movrt' kind of insn, i.e. an insn
  12249. that stores the negated T bit in a register, and return the destination
  12250. register rtx, or null. */
  12251. rtx
  12252. sh_movrt_set_dest (const rtx_insn* i)
  12253. {
  12254. if (i == NULL)
  12255. return NULL;
  12256. const_rtx p = PATTERN (i);
  12257. /* The negc movrt replacement is inside a parallel. */
  12258. if (GET_CODE (p) == PARALLEL)
  12259. p = XVECEXP (p, 0, 0);
  12260. return GET_CODE (p) == SET
  12261. && arith_reg_dest (XEXP (p, 0), SImode)
  12262. && negt_reg_operand (XEXP (p, 1), VOIDmode) ? XEXP (p, 0) : NULL;
  12263. }
  12264. /* Given an insn and a reg number, tell whether the reg dies or is unused
  12265. after the insn. */
  12266. bool
  12267. sh_reg_dead_or_unused_after_insn (const rtx_insn* i, int regno)
  12268. {
  12269. return find_regno_note (i, REG_DEAD, regno) != NULL
  12270. || find_regno_note (i, REG_UNUSED, regno) != NULL;
  12271. }
  12272. /* Given an insn and a reg number, remove reg dead or reg unused notes to
  12273. mark it as being used after the insn. */
  12274. void
  12275. sh_remove_reg_dead_or_unused_notes (rtx_insn* i, int regno)
  12276. {
  12277. if (rtx n = find_regno_note (i, REG_DEAD, regno))
  12278. remove_note (i, n);
  12279. if (rtx n = find_regno_note (i, REG_UNUSED, regno))
  12280. remove_note (i, n);
  12281. }
  12282. /* Given an insn check if it contains any post/pre inc/dec mem operands and
  12283. add the REG_INC notes accordingly.
  12284. FIXME: This function is very similar to lra.c (add_auto_inc_notes).
  12285. FIXME: This function is currently used by peephole2 patterns because
  12286. the peephole2 pass does not preserve REG_INC notes. If the notes
  12287. are dropped the following passes will do wrong things. */
  12288. rtx_insn*
  12289. sh_check_add_incdec_notes (rtx_insn* i)
  12290. {
  12291. struct for_each_inc_dec_clb
  12292. {
  12293. static int func (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
  12294. rtx dest, rtx src ATTRIBUTE_UNUSED,
  12295. rtx srcoff ATTRIBUTE_UNUSED, void* arg)
  12296. {
  12297. gcc_assert (REG_P (dest));
  12298. rtx_insn* i = (rtx_insn*)arg;
  12299. if (find_regno_note (i, REG_INC, REGNO (dest)) == NULL)
  12300. add_reg_note (i, REG_INC, dest);
  12301. return 0;
  12302. }
  12303. };
  12304. for_each_inc_dec (PATTERN (i), for_each_inc_dec_clb::func, i);
  12305. return i;
  12306. }
  12307. /* Given an op rtx and an insn, try to find out whether the result of the
  12308. specified op consists only of logical operations on T bit stores. */
  12309. bool
  12310. sh_is_logical_t_store_expr (rtx op, rtx_insn* insn)
  12311. {
  12312. if (!logical_operator (op, SImode))
  12313. return false;
  12314. rtx ops[2] = { XEXP (op, 0), XEXP (op, 1) };
  12315. int op_is_t_count = 0;
  12316. for (int i = 0; i < 2; ++i)
  12317. {
  12318. if (t_reg_operand (ops[i], VOIDmode)
  12319. || negt_reg_operand (ops[i], VOIDmode))
  12320. op_is_t_count++;
  12321. else
  12322. {
  12323. set_of_reg op_set = sh_find_set_of_reg (ops[i], insn,
  12324. prev_nonnote_insn_bb);
  12325. if (op_set.set_src == NULL_RTX)
  12326. continue;
  12327. if (t_reg_operand (op_set.set_src, VOIDmode)
  12328. || negt_reg_operand (op_set.set_src, VOIDmode)
  12329. || sh_is_logical_t_store_expr (op_set.set_src, op_set.insn))
  12330. op_is_t_count++;
  12331. }
  12332. }
  12333. return op_is_t_count == 2;
  12334. }
  12335. /* Given the operand that is extended in a sign/zero extend insn, and the
  12336. insn, try to figure out whether the sign/zero extension can be replaced
  12337. by a simple reg-reg copy. If so, the replacement reg rtx is returned,
  12338. NULL_RTX otherwise. */
  12339. rtx
  12340. sh_try_omit_signzero_extend (rtx extended_op, rtx_insn* insn)
  12341. {
  12342. if (REG_P (extended_op))
  12343. extended_op = extended_op;
  12344. else if (GET_CODE (extended_op) == SUBREG && REG_P (SUBREG_REG (extended_op)))
  12345. extended_op = SUBREG_REG (extended_op);
  12346. else
  12347. return NULL_RTX;
  12348. /* Reg moves must be of the same mode. */
  12349. if (GET_MODE (extended_op) != SImode)
  12350. return NULL_RTX;
  12351. set_of_reg s = sh_find_set_of_reg (extended_op, insn, prev_nonnote_insn_bb);
  12352. if (s.set_src == NULL_RTX)
  12353. return NULL_RTX;
  12354. if (t_reg_operand (s.set_src, VOIDmode)
  12355. || negt_reg_operand (s.set_src, VOIDmode))
  12356. return extended_op;
  12357. /* If the zero extended reg was formed by a logical operation, check the
  12358. operands of the logical operation. If both originated from T bit
  12359. stores the zero extension can be eliminated. */
  12360. else if (sh_is_logical_t_store_expr (s.set_src, s.insn))
  12361. return extended_op;
  12362. return NULL_RTX;
  12363. }
  12364. /* Given the current insn, which is assumed to be a movrt_negc insn, try to
  12365. figure out whether it should be converted into a movt-xor sequence in
  12366. the movrt_negc splitter.
  12367. Returns true if insns have been modified and the splitter has succeeded. */
  12368. bool
  12369. sh_split_movrt_negc_to_movt_xor (rtx_insn* curr_insn, rtx operands[])
  12370. {
  12371. /* In cases such as
  12372. tst r4,r4
  12373. mov #-1,r1
  12374. negc r1,r1
  12375. tst r4,r4
  12376. we can replace the T bit clobbering negc with a movt-xor sequence and
  12377. eliminate the redundant comparison.
  12378. Because the xor insn depends on register allocation results, allow this
  12379. only before reload. */
  12380. if (!can_create_pseudo_p ())
  12381. return false;
  12382. set_of_reg t_before_negc = sh_find_set_of_reg (get_t_reg_rtx (), curr_insn,
  12383. prev_nonnote_insn_bb);
  12384. set_of_reg t_after_negc = sh_find_set_of_reg (get_t_reg_rtx (), curr_insn,
  12385. next_nonnote_insn_bb);
  12386. if (t_before_negc.set_rtx != NULL_RTX && t_after_negc.set_rtx != NULL_RTX
  12387. && rtx_equal_p (t_before_negc.set_rtx, t_after_negc.set_rtx)
  12388. && !reg_used_between_p (get_t_reg_rtx (), curr_insn, t_after_negc.insn)
  12389. && !sh_insn_operands_modified_between_p (t_before_negc.insn,
  12390. t_before_negc.insn,
  12391. t_after_negc.insn)
  12392. && !sh_unspec_insn_p (t_after_negc.insn)
  12393. && !volatile_insn_p (PATTERN (t_after_negc.insn))
  12394. && !side_effects_p (PATTERN (t_after_negc.insn))
  12395. && !may_trap_or_fault_p (PATTERN (t_after_negc.insn)))
  12396. {
  12397. emit_insn (gen_movrt_xor (operands[0], get_t_reg_rtx ()));
  12398. set_insn_deleted (t_after_negc.insn);
  12399. return true;
  12400. }
  12401. else
  12402. return false;
  12403. }
  12404. /* Given a reg and the current insn, see if the value of the reg originated
  12405. from a sign or zero extension and return the discovered information. */
  12406. sh_extending_set_of_reg
  12407. sh_find_extending_set_of_reg (rtx reg, rtx_insn* curr_insn)
  12408. {
  12409. if (reg == NULL)
  12410. return sh_extending_set_of_reg (curr_insn);
  12411. if (SUBREG_P (reg))
  12412. reg = SUBREG_REG (reg);
  12413. if (!REG_P (reg))
  12414. return sh_extending_set_of_reg (curr_insn);
  12415. /* FIXME: Also search the predecessor basic blocks. It seems that checking
  12416. only the adjacent predecessor blocks would cover most of the cases.
  12417. Also try to look through the first extension that we hit. There are some
  12418. cases, where a zero_extend is followed an (implicit) sign_extend, and it
  12419. fails to see the sign_extend. */
  12420. sh_extending_set_of_reg result =
  12421. sh_find_set_of_reg (reg, curr_insn, prev_nonnote_insn_bb, true);
  12422. if (result.set_src != NULL)
  12423. {
  12424. if (GET_CODE (result.set_src) == SIGN_EXTEND
  12425. || GET_CODE (result.set_src) == ZERO_EXTEND)
  12426. {
  12427. if (dump_file)
  12428. fprintf (dump_file, "sh_find_extending_set_of_reg: reg %d is "
  12429. "explicitly sign/zero extended in insn %d\n",
  12430. REGNO (reg), INSN_UID (result.insn));
  12431. result.from_mode = GET_MODE (XEXP (result.set_src, 0));
  12432. result.ext_code = GET_CODE (result.set_src);
  12433. }
  12434. else if (MEM_P (result.set_src)
  12435. && (GET_MODE (result.set_src) == QImode
  12436. || GET_MODE (result.set_src) == HImode)
  12437. && !sh_unspec_insn_p (result.insn))
  12438. {
  12439. /* On SH QIHImode memory loads always sign extend. However, in
  12440. some cases where it seems that the higher bits are not
  12441. interesting, the loads will not be expanded as sign extending
  12442. insns, but as QIHImode loads into QIHImode regs. We report that
  12443. the reg has been sign extended by the mem load. When it is used
  12444. as such, we must convert the mem load into a sign extending insn,
  12445. see also sh_extending_set_of_reg::use_as_extended_reg. */
  12446. if (dump_file)
  12447. fprintf (dump_file, "sh_find_extending_set_of_reg: reg %d is "
  12448. "implicitly sign extended in insn %d\n",
  12449. REGNO (reg), INSN_UID (result.insn));
  12450. result.from_mode = GET_MODE (result.set_src);
  12451. result.ext_code = SIGN_EXTEND;
  12452. }
  12453. }
  12454. return result;
  12455. }
  12456. /* Given a reg that is known to be sign or zero extended at some insn,
  12457. take the appropriate measures so that the extended value can be used as
  12458. a reg at the specified insn and return the resulting reg rtx. */
  12459. rtx
  12460. sh_extending_set_of_reg::use_as_extended_reg (rtx_insn* use_at_insn) const
  12461. {
  12462. gcc_assert (insn != NULL && set_src != NULL && set_rtx != NULL);
  12463. gcc_assert (ext_code == SIGN_EXTEND || ext_code == ZERO_EXTEND);
  12464. gcc_assert (from_mode == QImode || from_mode == HImode);
  12465. if (MEM_P (set_src) && ext_code == SIGN_EXTEND)
  12466. {
  12467. if (dump_file)
  12468. fprintf (dump_file,
  12469. "use_as_extended_reg: converting non-extending mem load in "
  12470. "insn %d into sign-extending load\n", INSN_UID (insn));
  12471. rtx r = gen_reg_rtx (SImode);
  12472. rtx_insn* i0;
  12473. if (from_mode == QImode)
  12474. i0 = emit_insn_after (gen_extendqisi2 (r, set_src), insn);
  12475. else if (from_mode == HImode)
  12476. i0 = emit_insn_after (gen_extendhisi2 (r, set_src), insn);
  12477. else
  12478. gcc_unreachable ();
  12479. emit_insn_after (
  12480. gen_move_insn (XEXP (set_rtx, 0),
  12481. gen_lowpart (GET_MODE (set_src), r)), i0);
  12482. set_insn_deleted (insn);
  12483. return r;
  12484. }
  12485. else
  12486. {
  12487. rtx extension_dst = XEXP (set_rtx, 0);
  12488. if (modified_between_p (extension_dst, insn, use_at_insn))
  12489. {
  12490. if (dump_file)
  12491. fprintf (dump_file,
  12492. "use_as_extended_reg: dest reg %d of extending insn %d is "
  12493. "modified, inserting a reg-reg copy\n",
  12494. REGNO (extension_dst), INSN_UID (insn));
  12495. rtx r = gen_reg_rtx (SImode);
  12496. emit_insn_after (gen_move_insn (r, extension_dst), insn);
  12497. return r;
  12498. }
  12499. else
  12500. {
  12501. sh_remove_reg_dead_or_unused_notes (insn, REGNO (extension_dst));
  12502. return extension_dst;
  12503. }
  12504. }
  12505. }
  12506. bool
  12507. sh_extending_set_of_reg::can_use_as_unextended_reg (void) const
  12508. {
  12509. if ((ext_code == SIGN_EXTEND || ext_code == ZERO_EXTEND)
  12510. && (from_mode == QImode || from_mode == HImode)
  12511. && set_src != NULL)
  12512. return arith_reg_operand (XEXP (set_src, 0), from_mode);
  12513. else
  12514. return false;
  12515. }
  12516. rtx
  12517. sh_extending_set_of_reg::use_as_unextended_reg (rtx_insn* use_at_insn) const
  12518. {
  12519. gcc_assert (can_use_as_unextended_reg ());
  12520. rtx r = XEXP (set_src, 0);
  12521. rtx r0 = simplify_gen_subreg (SImode, r, from_mode, 0);
  12522. if (modified_between_p (r, insn, use_at_insn))
  12523. {
  12524. rtx r1 = gen_reg_rtx (SImode);
  12525. emit_insn_after (gen_move_insn (r1, r0), insn);
  12526. return r1;
  12527. }
  12528. else
  12529. {
  12530. sh_remove_reg_dead_or_unused_notes (insn, SUBREG_P (r)
  12531. ? REGNO (SUBREG_REG (r))
  12532. : REGNO (r));
  12533. return r0;
  12534. }
  12535. }
  12536. /* Given the current insn, which is assumed to be the *tst<mode>_t_subregs insn,
  12537. perform the necessary checks on the operands and split it accordingly. */
  12538. void
  12539. sh_split_tst_subregs (rtx_insn* curr_insn, machine_mode subreg_mode,
  12540. int subreg_offset, rtx operands[])
  12541. {
  12542. gcc_assert (subreg_mode == QImode || subreg_mode == HImode);
  12543. sh_extending_set_of_reg eop0 = sh_find_extending_set_of_reg (operands[0],
  12544. curr_insn);
  12545. sh_extending_set_of_reg eop1 = sh_find_extending_set_of_reg (operands[1],
  12546. curr_insn);
  12547. /* If one of the operands is known to be zero extended, that's already
  12548. sufficient to mask out the unwanted high bits. */
  12549. if (eop0.ext_code == ZERO_EXTEND && eop0.from_mode == subreg_mode)
  12550. {
  12551. emit_insn (gen_tstsi_t (eop0.use_as_extended_reg (curr_insn),
  12552. operands[1]));
  12553. return;
  12554. }
  12555. if (eop1.ext_code == ZERO_EXTEND && eop1.from_mode == subreg_mode)
  12556. {
  12557. emit_insn (gen_tstsi_t (operands[0],
  12558. eop1.use_as_extended_reg (curr_insn)));
  12559. return;
  12560. }
  12561. /* None of the operands seem to be zero extended.
  12562. If both are sign extended it's OK, too. */
  12563. if (eop0.ext_code == SIGN_EXTEND && eop1.ext_code == SIGN_EXTEND
  12564. && eop0.from_mode == subreg_mode && eop1.from_mode == subreg_mode)
  12565. {
  12566. emit_insn (gen_tstsi_t (eop0.use_as_extended_reg (curr_insn),
  12567. eop1.use_as_extended_reg (curr_insn)));
  12568. return;
  12569. }
  12570. /* Otherwise we have to insert a zero extension on one of the operands to
  12571. mask out the unwanted high bits.
  12572. Prefer the operand that has no known extension. */
  12573. if (eop0.ext_code != UNKNOWN && eop1.ext_code == UNKNOWN)
  12574. std::swap (operands[0], operands[1]);
  12575. rtx tmp0 = gen_reg_rtx (SImode);
  12576. rtx tmp1 = simplify_gen_subreg (subreg_mode, operands[0],
  12577. GET_MODE (operands[0]), subreg_offset);
  12578. emit_insn (subreg_mode == QImode
  12579. ? gen_zero_extendqisi2 (tmp0, tmp1)
  12580. : gen_zero_extendhisi2 (tmp0, tmp1));
  12581. emit_insn (gen_tstsi_t (tmp0, operands[1]));
  12582. }
  12583. /* A helper class to increment/decrement a counter variable each time a
  12584. function is entered/left. */
  12585. class scope_counter
  12586. {
  12587. public:
  12588. scope_counter (int& counter) : m_counter (counter) { ++m_counter; }
  12589. ~scope_counter (void)
  12590. {
  12591. --m_counter;
  12592. gcc_assert (m_counter >= 0);
  12593. }
  12594. int count (void) const { return m_counter; }
  12595. private:
  12596. int& m_counter;
  12597. };
  12598. /* Given an rtx x, determine whether the expression can be used to create
  12599. an insn that calulates x and stores the result in the T bit.
  12600. This is used by the 'treg_set_expr' predicate to construct insns sequences
  12601. where T bit results are fed into other insns, such as addc, subc, negc
  12602. insns.
  12603. FIXME: The patterns that expand 'treg_set_expr' operands tend to
  12604. distinguish between 'positive' and 'negative' forms. For now this has to
  12605. be done in the preparation code. We could also introduce
  12606. 'pos_treg_set_expr' and 'neg_treg_set_expr' predicates for that and write
  12607. two different patterns for the 'postive' and 'negative' forms. However,
  12608. the total amount of lines of code seems to be about the same and the
  12609. '{pos|neg}_treg_set_expr' predicates would be more expensive, because the
  12610. recog function would need to look inside the expression by temporarily
  12611. splitting it. */
  12612. static int sh_recog_treg_set_expr_reent_count = 0;
  12613. bool
  12614. sh_recog_treg_set_expr (rtx op, machine_mode mode)
  12615. {
  12616. scope_counter recursion (sh_recog_treg_set_expr_reent_count);
  12617. /* Limit the recursion count to avoid nested expressions which we can't
  12618. resolve to a single treg set insn. */
  12619. if (recursion.count () > 1)
  12620. return false;
  12621. /* Early accept known possible operands before doing recog. */
  12622. if (op == const0_rtx || op == const1_rtx || t_reg_operand (op, mode))
  12623. return true;
  12624. /* Early reject impossible operands before doing recog.
  12625. There are some (set ((t) (subreg ...))) patterns, but we must be careful
  12626. not to allow any invalid reg-reg or mem-reg moves, or else other passes
  12627. such as lower-subreg will bail out. Some insns such as SH4A movua are
  12628. done with UNSPEC, so must reject those, too, or else it would result
  12629. in an invalid reg -> treg move. */
  12630. if (register_operand (op, mode) || memory_operand (op, mode)
  12631. || sh_unspec_insn_p (op))
  12632. return false;
  12633. if (!can_create_pseudo_p ())
  12634. return false;
  12635. /* We are going to invoke recog in a re-entrant way and thus
  12636. have to capture its current state and restore it afterwards. */
  12637. recog_data_d prev_recog_data = recog_data;
  12638. rtx_insn* i = make_insn_raw (gen_rtx_SET (VOIDmode, get_t_reg_rtx (), op));
  12639. SET_PREV_INSN (i) = NULL;
  12640. SET_NEXT_INSN (i) = NULL;
  12641. int result = recog (PATTERN (i), i, 0);
  12642. /* It seems there is no insn like that. Create a simple negated
  12643. version and try again. If we hit a negated form, we'll allow that
  12644. and append a nott sequence when splitting out the insns. Insns that
  12645. do the split can then remove the trailing nott if they know how to
  12646. deal with it. */
  12647. if (result < 0 && GET_CODE (op) == EQ)
  12648. {
  12649. PUT_CODE (op, NE);
  12650. result = recog (PATTERN (i), i, 0);
  12651. PUT_CODE (op, EQ);
  12652. }
  12653. if (result < 0 && GET_CODE (op) == NE)
  12654. {
  12655. PUT_CODE (op, EQ);
  12656. result = recog (PATTERN (i), i, 0);
  12657. PUT_CODE (op, NE);
  12658. }
  12659. recog_data = prev_recog_data;
  12660. return result >= 0;
  12661. }
  12662. /* Returns true when recog of a 'treg_set_expr' is currently in progress.
  12663. This can be used as a condition for insn/split patterns to allow certain
  12664. T bit setting patters only to be matched as sub expressions of other
  12665. patterns. */
  12666. bool
  12667. sh_in_recog_treg_set_expr (void)
  12668. {
  12669. return sh_recog_treg_set_expr_reent_count > 0;
  12670. }
  12671. /* Given an rtx x, which is assumed to be some expression that has been
  12672. matched by the 'treg_set_expr' predicate before, split and emit the
  12673. insns that are necessary to calculate the expression and store the result
  12674. in the T bit.
  12675. The splitting is done recursively similar to 'try_split' in emit-rt.c.
  12676. Unfortunately we can't use 'try_split' here directly, as it tries to invoke
  12677. 'delete_insn' which then causes the DF parts to bail out, because we
  12678. currently are inside another gen_split* function and would invoke
  12679. 'try_split' in a reentrant way. */
  12680. static std::pair<rtx_insn*, rtx_insn*>
  12681. sh_try_split_insn_simple (rtx_insn* i, rtx_insn* curr_insn, int n = 0)
  12682. {
  12683. if (dump_file)
  12684. {
  12685. fprintf (dump_file, "sh_try_split_insn_simple n = %d i = \n", n);
  12686. print_rtl_single (dump_file, i);
  12687. fprintf (dump_file, "\n");
  12688. }
  12689. rtx_insn* seq = safe_as_a<rtx_insn*> (split_insns (PATTERN (i), curr_insn));
  12690. if (seq == NULL)
  12691. return std::make_pair (i, i);
  12692. /* Avoid infinite splitter loops if any insn of the result matches
  12693. the original pattern. */
  12694. for (rtx_insn* s = seq; s != NULL; s = NEXT_INSN (s))
  12695. if (INSN_P (s) && rtx_equal_p (PATTERN (s), PATTERN (i)))
  12696. return std::make_pair (i, i);
  12697. unshare_all_rtl_in_chain (seq);
  12698. /* 'seq' is now a replacement for 'i'. Assuming that 'i' is an insn in
  12699. a linked list, replace the single insn with the new insns. */
  12700. rtx_insn* seqlast = seq;
  12701. while (NEXT_INSN (seqlast) != NULL)
  12702. seqlast = NEXT_INSN (seqlast);
  12703. if (rtx_insn* iprev = PREV_INSN (i))
  12704. SET_NEXT_INSN (iprev) = seq;
  12705. if (rtx_insn* inext = NEXT_INSN (i))
  12706. SET_PREV_INSN (inext) = seqlast;
  12707. SET_PREV_INSN (seq) = PREV_INSN (i);
  12708. SET_NEXT_INSN (seqlast) = NEXT_INSN (i);
  12709. SET_PREV_INSN (i) = NULL;
  12710. SET_NEXT_INSN (i) = NULL;
  12711. /* Recursively split all insns. */
  12712. for (i = seq; ; i = NEXT_INSN (i))
  12713. {
  12714. std::pair<rtx_insn*, rtx_insn*> ii =
  12715. sh_try_split_insn_simple (i, curr_insn, n + 1);
  12716. if (i == seq)
  12717. seq = ii.first;
  12718. if (i == seqlast)
  12719. {
  12720. seqlast = ii.second;
  12721. break;
  12722. }
  12723. i = ii.first;
  12724. }
  12725. return std::make_pair (seq, seqlast);
  12726. }
  12727. sh_treg_insns
  12728. sh_split_treg_set_expr (rtx x, rtx_insn* curr_insn)
  12729. {
  12730. if (t_reg_operand (x, VOIDmode))
  12731. return sh_treg_insns ();
  12732. scope_counter in_treg_set_expr (sh_recog_treg_set_expr_reent_count);
  12733. rtx_insn* i = make_insn_raw (gen_rtx_SET (VOIDmode, get_t_reg_rtx (), x));
  12734. SET_PREV_INSN (i) = NULL;
  12735. SET_NEXT_INSN (i) = NULL;
  12736. if (dump_file)
  12737. {
  12738. fprintf (dump_file, "split_treg_set_expr insn:\n");
  12739. print_rtl (dump_file, i);
  12740. fprintf (dump_file, "\n");
  12741. }
  12742. /* We are going to invoke recog/split_insns in a re-entrant way and thus
  12743. have to capture its current state and restore it afterwards. */
  12744. recog_data_d prev_recog_data = recog_data;
  12745. int insn_code = recog (PATTERN (i), i, 0);
  12746. /* If the insn was not found, see if we matched the negated form before
  12747. and append a nott. */
  12748. bool append_nott = false;
  12749. if (insn_code < 0 && GET_CODE (x) == EQ)
  12750. {
  12751. PUT_CODE (x, NE);
  12752. insn_code = recog (PATTERN (i), i, 0);
  12753. if (insn_code >= 0)
  12754. append_nott = true;
  12755. else
  12756. PUT_CODE (x, EQ);
  12757. }
  12758. if (insn_code < 0 && GET_CODE (x) == NE)
  12759. {
  12760. PUT_CODE (x, EQ);
  12761. insn_code = recog (PATTERN (i), i, 0);
  12762. if (insn_code >= 0)
  12763. append_nott = true;
  12764. else
  12765. PUT_CODE (x, NE);
  12766. }
  12767. gcc_assert (insn_code >= 0);
  12768. /* Try to recursively split the insn. Some insns might refuse to split
  12769. any further while we are in the treg_set_expr splitting phase. They
  12770. will be emitted as part of the outer insn and then split again. */
  12771. std::pair<rtx_insn*, rtx_insn*> insnlist =
  12772. sh_try_split_insn_simple (i, curr_insn);
  12773. /* Restore recog state. */
  12774. recog_data = prev_recog_data;
  12775. rtx_insn* nott_insn = sh_is_nott_insn (insnlist.second)
  12776. ? insnlist.second
  12777. : NULL;
  12778. if (dump_file)
  12779. {
  12780. fprintf (dump_file, "split_treg_set_expr insnlist:\n");
  12781. print_rtl (dump_file, insnlist.first);
  12782. fprintf (dump_file, "\n");
  12783. if (nott_insn != NULL)
  12784. fprintf (dump_file, "trailing nott insn %d\n", INSN_UID (nott_insn));
  12785. }
  12786. emit_insn (insnlist.first);
  12787. if (nott_insn != NULL && append_nott)
  12788. {
  12789. if (dump_file)
  12790. fprintf (dump_file, "removing trailing nott\n");
  12791. remove_insn (nott_insn);
  12792. nott_insn = NULL;
  12793. append_nott = false;
  12794. }
  12795. if (append_nott)
  12796. nott_insn = emit_insn (gen_nott (get_t_reg_rtx ()));
  12797. rtx_insn* first_insn = get_insns ();
  12798. if (dump_file)
  12799. {
  12800. fprintf (dump_file, "resulting insns:\n");
  12801. print_rtl (dump_file, first_insn);
  12802. fprintf (dump_file, "\n");
  12803. }
  12804. return sh_treg_insns (first_insn, nott_insn);
  12805. }
  12806. /*------------------------------------------------------------------------------
  12807. Mode switching support code.
  12808. */
  12809. static void
  12810. sh_emit_mode_set (int entity ATTRIBUTE_UNUSED, int mode,
  12811. int prev_mode, HARD_REG_SET regs_live ATTRIBUTE_UNUSED)
  12812. {
  12813. if ((TARGET_SH4A_FP || TARGET_SH4_300)
  12814. && prev_mode != FP_MODE_NONE && prev_mode != mode)
  12815. {
  12816. emit_insn (gen_toggle_pr ());
  12817. if (TARGET_FMOVD)
  12818. emit_insn (gen_toggle_sz ());
  12819. }
  12820. else if (mode != FP_MODE_NONE)
  12821. {
  12822. rtx tmp = gen_reg_rtx (SImode);
  12823. emit_insn (gen_sts_fpscr (tmp));
  12824. rtx i = NULL;
  12825. const unsigned HOST_WIDE_INT fpbits =
  12826. TARGET_FMOVD ? (FPSCR_PR | FPSCR_SZ) : FPSCR_PR;
  12827. if (prev_mode != FP_MODE_NONE && prev_mode != mode)
  12828. i = gen_xorsi3 (tmp, tmp, force_reg (SImode, GEN_INT (fpbits)));
  12829. else if (mode == FP_MODE_SINGLE)
  12830. i = gen_andsi3 (tmp, tmp, force_reg (SImode, GEN_INT (~fpbits)));
  12831. else if (mode == FP_MODE_DOUBLE)
  12832. i = gen_iorsi3 (tmp, tmp, force_reg (SImode, GEN_INT (fpbits)));
  12833. else
  12834. gcc_unreachable ();
  12835. emit_insn (i);
  12836. emit_insn (gen_lds_fpscr (tmp));
  12837. }
  12838. }
  12839. static int
  12840. sh_mode_needed (int entity ATTRIBUTE_UNUSED, rtx_insn *insn)
  12841. {
  12842. return recog_memoized (insn) >= 0 ? get_attr_fp_mode (insn) : FP_MODE_NONE;
  12843. }
  12844. static int
  12845. sh_mode_after (int entity ATTRIBUTE_UNUSED, int mode, rtx_insn *insn)
  12846. {
  12847. if (TARGET_HITACHI && recog_memoized (insn) >= 0 &&
  12848. get_attr_fp_set (insn) != FP_SET_NONE)
  12849. return (int) get_attr_fp_set (insn);
  12850. else
  12851. return mode;
  12852. }
  12853. static int
  12854. sh_mode_entry (int entity ATTRIBUTE_UNUSED)
  12855. {
  12856. return NORMAL_MODE (entity);
  12857. }
  12858. static int
  12859. sh_mode_exit (int entity ATTRIBUTE_UNUSED)
  12860. {
  12861. return sh_cfun_attr_renesas_p () ? FP_MODE_NONE : NORMAL_MODE (entity);
  12862. }
  12863. static int
  12864. sh_mode_priority (int entity ATTRIBUTE_UNUSED, int n)
  12865. {
  12866. return ((TARGET_FPU_SINGLE != 0) ^ (n) ? FP_MODE_SINGLE : FP_MODE_DOUBLE);
  12867. }
  12868. /*------------------------------------------------------------------------------
  12869. Misc
  12870. */
  12871. /* Return true if we use LRA instead of reload pass. */
  12872. static bool
  12873. sh_lra_p (void)
  12874. {
  12875. return sh_lra_flag;
  12876. }
  12877. /* Implement TARGET_USE_BY_PIECES_INFRASTRUCTURE_P. */
  12878. static bool
  12879. sh_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
  12880. unsigned int align,
  12881. enum by_pieces_operation op,
  12882. bool speed_p)
  12883. {
  12884. switch (op)
  12885. {
  12886. case MOVE_BY_PIECES:
  12887. return move_by_pieces_ninsns (size, align, MOVE_MAX_PIECES + 1)
  12888. < (!speed_p ? 2 : (align >= 32) ? 16 : 2);
  12889. case STORE_BY_PIECES:
  12890. case SET_BY_PIECES:
  12891. return move_by_pieces_ninsns (size, align, STORE_MAX_PIECES + 1)
  12892. < (!speed_p ? 2 : (align >= 32) ? 16 : 2);
  12893. default:
  12894. return default_use_by_pieces_infrastructure_p (size, align,
  12895. op, speed_p);
  12896. }
  12897. }
  12898. #include "gt-sh.h"