optabs.c 258 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699
  1. /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "diagnostic-core.h"
  20. /* Include insn-config.h before expr.h so that HAVE_conditional_move
  21. is properly defined. */
  22. #include "insn-config.h"
  23. #include "rtl.h"
  24. #include "hash-set.h"
  25. #include "machmode.h"
  26. #include "vec.h"
  27. #include "double-int.h"
  28. #include "input.h"
  29. #include "alias.h"
  30. #include "symtab.h"
  31. #include "wide-int.h"
  32. #include "inchash.h"
  33. #include "tree.h"
  34. #include "tree-hasher.h"
  35. #include "stor-layout.h"
  36. #include "stringpool.h"
  37. #include "varasm.h"
  38. #include "tm_p.h"
  39. #include "flags.h"
  40. #include "hard-reg-set.h"
  41. #include "function.h"
  42. #include "except.h"
  43. #include "hashtab.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 "calls.h"
  51. #include "emit-rtl.h"
  52. #include "stmt.h"
  53. #include "expr.h"
  54. #include "insn-codes.h"
  55. #include "optabs.h"
  56. #include "libfuncs.h"
  57. #include "recog.h"
  58. #include "reload.h"
  59. #include "ggc.h"
  60. #include "predict.h"
  61. #include "dominance.h"
  62. #include "cfg.h"
  63. #include "basic-block.h"
  64. #include "target.h"
  65. struct target_optabs default_target_optabs;
  66. struct target_libfuncs default_target_libfuncs;
  67. struct target_optabs *this_fn_optabs = &default_target_optabs;
  68. #if SWITCHABLE_TARGET
  69. struct target_optabs *this_target_optabs = &default_target_optabs;
  70. struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
  71. #endif
  72. #define libfunc_hash \
  73. (this_target_libfuncs->x_libfunc_hash)
  74. static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
  75. machine_mode *);
  76. static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
  77. static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
  78. /* Debug facility for use in GDB. */
  79. void debug_optab_libfuncs (void);
  80. /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
  81. #if ENABLE_DECIMAL_BID_FORMAT
  82. #define DECIMAL_PREFIX "bid_"
  83. #else
  84. #define DECIMAL_PREFIX "dpd_"
  85. #endif
  86. /* Used for libfunc_hash. */
  87. hashval_t
  88. libfunc_hasher::hash (libfunc_entry *e)
  89. {
  90. return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
  91. }
  92. /* Used for libfunc_hash. */
  93. bool
  94. libfunc_hasher::equal (libfunc_entry *e1, libfunc_entry *e2)
  95. {
  96. return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
  97. }
  98. /* Return libfunc corresponding operation defined by OPTAB converting
  99. from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
  100. if no libfunc is available. */
  101. rtx
  102. convert_optab_libfunc (convert_optab optab, machine_mode mode1,
  103. machine_mode mode2)
  104. {
  105. struct libfunc_entry e;
  106. struct libfunc_entry **slot;
  107. /* ??? This ought to be an assert, but not all of the places
  108. that we expand optabs know about the optabs that got moved
  109. to being direct. */
  110. if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
  111. return NULL_RTX;
  112. e.op = optab;
  113. e.mode1 = mode1;
  114. e.mode2 = mode2;
  115. slot = libfunc_hash->find_slot (&e, NO_INSERT);
  116. if (!slot)
  117. {
  118. const struct convert_optab_libcall_d *d
  119. = &convlib_def[optab - FIRST_CONV_OPTAB];
  120. if (d->libcall_gen == NULL)
  121. return NULL;
  122. d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
  123. slot = libfunc_hash->find_slot (&e, NO_INSERT);
  124. if (!slot)
  125. return NULL;
  126. }
  127. return (*slot)->libfunc;
  128. }
  129. /* Return libfunc corresponding operation defined by OPTAB in MODE.
  130. Trigger lazy initialization if needed, return NULL if no libfunc is
  131. available. */
  132. rtx
  133. optab_libfunc (optab optab, machine_mode mode)
  134. {
  135. struct libfunc_entry e;
  136. struct libfunc_entry **slot;
  137. /* ??? This ought to be an assert, but not all of the places
  138. that we expand optabs know about the optabs that got moved
  139. to being direct. */
  140. if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
  141. return NULL_RTX;
  142. e.op = optab;
  143. e.mode1 = mode;
  144. e.mode2 = VOIDmode;
  145. slot = libfunc_hash->find_slot (&e, NO_INSERT);
  146. if (!slot)
  147. {
  148. const struct optab_libcall_d *d
  149. = &normlib_def[optab - FIRST_NORM_OPTAB];
  150. if (d->libcall_gen == NULL)
  151. return NULL;
  152. d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
  153. slot = libfunc_hash->find_slot (&e, NO_INSERT);
  154. if (!slot)
  155. return NULL;
  156. }
  157. return (*slot)->libfunc;
  158. }
  159. /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
  160. the result of operation CODE applied to OP0 (and OP1 if it is a binary
  161. operation).
  162. If the last insn does not set TARGET, don't do anything, but return 1.
  163. If the last insn or a previous insn sets TARGET and TARGET is one of OP0
  164. or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
  165. try again, ensuring that TARGET is not one of the operands. */
  166. static int
  167. add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
  168. {
  169. rtx_insn *last_insn;
  170. rtx set;
  171. rtx note;
  172. gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
  173. if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
  174. && GET_RTX_CLASS (code) != RTX_BIN_ARITH
  175. && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
  176. && GET_RTX_CLASS (code) != RTX_COMPARE
  177. && GET_RTX_CLASS (code) != RTX_UNARY)
  178. return 1;
  179. if (GET_CODE (target) == ZERO_EXTRACT)
  180. return 1;
  181. for (last_insn = insns;
  182. NEXT_INSN (last_insn) != NULL_RTX;
  183. last_insn = NEXT_INSN (last_insn))
  184. ;
  185. /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
  186. a value changing in the insn, so the note would be invalid for CSE. */
  187. if (reg_overlap_mentioned_p (target, op0)
  188. || (op1 && reg_overlap_mentioned_p (target, op1)))
  189. {
  190. if (MEM_P (target)
  191. && (rtx_equal_p (target, op0)
  192. || (op1 && rtx_equal_p (target, op1))))
  193. {
  194. /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
  195. over expanding it as temp = MEM op X, MEM = temp. If the target
  196. supports MEM = MEM op X instructions, it is sometimes too hard
  197. to reconstruct that form later, especially if X is also a memory,
  198. and due to multiple occurrences of addresses the address might
  199. be forced into register unnecessarily.
  200. Note that not emitting the REG_EQUIV note might inhibit
  201. CSE in some cases. */
  202. set = single_set (last_insn);
  203. if (set
  204. && GET_CODE (SET_SRC (set)) == code
  205. && MEM_P (SET_DEST (set))
  206. && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
  207. || (op1 && rtx_equal_p (SET_DEST (set),
  208. XEXP (SET_SRC (set), 1)))))
  209. return 1;
  210. }
  211. return 0;
  212. }
  213. set = set_for_reg_notes (last_insn);
  214. if (set == NULL_RTX)
  215. return 1;
  216. if (! rtx_equal_p (SET_DEST (set), target)
  217. /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
  218. && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
  219. || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
  220. return 1;
  221. if (GET_RTX_CLASS (code) == RTX_UNARY)
  222. switch (code)
  223. {
  224. case FFS:
  225. case CLZ:
  226. case CTZ:
  227. case CLRSB:
  228. case POPCOUNT:
  229. case PARITY:
  230. case BSWAP:
  231. if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
  232. {
  233. note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
  234. if (GET_MODE_SIZE (GET_MODE (op0))
  235. > GET_MODE_SIZE (GET_MODE (target)))
  236. note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
  237. note, GET_MODE (op0));
  238. else
  239. note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
  240. note, GET_MODE (op0));
  241. break;
  242. }
  243. /* FALLTHRU */
  244. default:
  245. note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
  246. break;
  247. }
  248. else
  249. note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
  250. set_unique_reg_note (last_insn, REG_EQUAL, note);
  251. return 1;
  252. }
  253. /* Given two input operands, OP0 and OP1, determine what the correct from_mode
  254. for a widening operation would be. In most cases this would be OP0, but if
  255. that's a constant it'll be VOIDmode, which isn't useful. */
  256. static machine_mode
  257. widened_mode (machine_mode to_mode, rtx op0, rtx op1)
  258. {
  259. machine_mode m0 = GET_MODE (op0);
  260. machine_mode m1 = GET_MODE (op1);
  261. machine_mode result;
  262. if (m0 == VOIDmode && m1 == VOIDmode)
  263. return to_mode;
  264. else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
  265. result = m1;
  266. else
  267. result = m0;
  268. if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
  269. return to_mode;
  270. return result;
  271. }
  272. /* Like optab_handler, but for widening_operations that have a
  273. TO_MODE and a FROM_MODE. */
  274. enum insn_code
  275. widening_optab_handler (optab op, machine_mode to_mode,
  276. machine_mode from_mode)
  277. {
  278. unsigned scode = (op << 16) | to_mode;
  279. if (to_mode != from_mode && from_mode != VOIDmode)
  280. {
  281. /* ??? Why does find_widening_optab_handler_and_mode attempt to
  282. widen things that can't be widened? E.g. add_optab... */
  283. if (op > LAST_CONV_OPTAB)
  284. return CODE_FOR_nothing;
  285. scode |= from_mode << 8;
  286. }
  287. return raw_optab_handler (scode);
  288. }
  289. /* Find a widening optab even if it doesn't widen as much as we want.
  290. E.g. if from_mode is HImode, and to_mode is DImode, and there is no
  291. direct HI->SI insn, then return SI->DI, if that exists.
  292. If PERMIT_NON_WIDENING is non-zero then this can be used with
  293. non-widening optabs also. */
  294. enum insn_code
  295. find_widening_optab_handler_and_mode (optab op, machine_mode to_mode,
  296. machine_mode from_mode,
  297. int permit_non_widening,
  298. machine_mode *found_mode)
  299. {
  300. for (; (permit_non_widening || from_mode != to_mode)
  301. && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
  302. && from_mode != VOIDmode;
  303. from_mode = GET_MODE_WIDER_MODE (from_mode))
  304. {
  305. enum insn_code handler = widening_optab_handler (op, to_mode,
  306. from_mode);
  307. if (handler != CODE_FOR_nothing)
  308. {
  309. if (found_mode)
  310. *found_mode = from_mode;
  311. return handler;
  312. }
  313. }
  314. return CODE_FOR_nothing;
  315. }
  316. /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
  317. says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
  318. not actually do a sign-extend or zero-extend, but can leave the
  319. higher-order bits of the result rtx undefined, for example, in the case
  320. of logical operations, but not right shifts. */
  321. static rtx
  322. widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
  323. int unsignedp, int no_extend)
  324. {
  325. rtx result;
  326. /* If we don't have to extend and this is a constant, return it. */
  327. if (no_extend && GET_MODE (op) == VOIDmode)
  328. return op;
  329. /* If we must extend do so. If OP is a SUBREG for a promoted object, also
  330. extend since it will be more efficient to do so unless the signedness of
  331. a promoted object differs from our extension. */
  332. if (! no_extend
  333. || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
  334. && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
  335. return convert_modes (mode, oldmode, op, unsignedp);
  336. /* If MODE is no wider than a single word, we return a lowpart or paradoxical
  337. SUBREG. */
  338. if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
  339. return gen_lowpart (mode, force_reg (GET_MODE (op), op));
  340. /* Otherwise, get an object of MODE, clobber it, and set the low-order
  341. part to OP. */
  342. result = gen_reg_rtx (mode);
  343. emit_clobber (result);
  344. emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
  345. return result;
  346. }
  347. /* Return the optab used for computing the operation given by the tree code,
  348. CODE and the tree EXP. This function is not always usable (for example, it
  349. cannot give complete results for multiplication or division) but probably
  350. ought to be relied on more widely throughout the expander. */
  351. optab
  352. optab_for_tree_code (enum tree_code code, const_tree type,
  353. enum optab_subtype subtype)
  354. {
  355. bool trapv;
  356. switch (code)
  357. {
  358. case BIT_AND_EXPR:
  359. return and_optab;
  360. case BIT_IOR_EXPR:
  361. return ior_optab;
  362. case BIT_NOT_EXPR:
  363. return one_cmpl_optab;
  364. case BIT_XOR_EXPR:
  365. return xor_optab;
  366. case MULT_HIGHPART_EXPR:
  367. return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
  368. case TRUNC_MOD_EXPR:
  369. case CEIL_MOD_EXPR:
  370. case FLOOR_MOD_EXPR:
  371. case ROUND_MOD_EXPR:
  372. return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
  373. case RDIV_EXPR:
  374. case TRUNC_DIV_EXPR:
  375. case CEIL_DIV_EXPR:
  376. case FLOOR_DIV_EXPR:
  377. case ROUND_DIV_EXPR:
  378. case EXACT_DIV_EXPR:
  379. if (TYPE_SATURATING (type))
  380. return TYPE_UNSIGNED (type) ? usdiv_optab : ssdiv_optab;
  381. return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
  382. case LSHIFT_EXPR:
  383. if (TREE_CODE (type) == VECTOR_TYPE)
  384. {
  385. if (subtype == optab_vector)
  386. return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
  387. gcc_assert (subtype == optab_scalar);
  388. }
  389. if (TYPE_SATURATING (type))
  390. return TYPE_UNSIGNED (type) ? usashl_optab : ssashl_optab;
  391. return ashl_optab;
  392. case RSHIFT_EXPR:
  393. if (TREE_CODE (type) == VECTOR_TYPE)
  394. {
  395. if (subtype == optab_vector)
  396. return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
  397. gcc_assert (subtype == optab_scalar);
  398. }
  399. return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
  400. case LROTATE_EXPR:
  401. if (TREE_CODE (type) == VECTOR_TYPE)
  402. {
  403. if (subtype == optab_vector)
  404. return vrotl_optab;
  405. gcc_assert (subtype == optab_scalar);
  406. }
  407. return rotl_optab;
  408. case RROTATE_EXPR:
  409. if (TREE_CODE (type) == VECTOR_TYPE)
  410. {
  411. if (subtype == optab_vector)
  412. return vrotr_optab;
  413. gcc_assert (subtype == optab_scalar);
  414. }
  415. return rotr_optab;
  416. case MAX_EXPR:
  417. return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
  418. case MIN_EXPR:
  419. return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
  420. case REALIGN_LOAD_EXPR:
  421. return vec_realign_load_optab;
  422. case WIDEN_SUM_EXPR:
  423. return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
  424. case DOT_PROD_EXPR:
  425. return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
  426. case SAD_EXPR:
  427. return TYPE_UNSIGNED (type) ? usad_optab : ssad_optab;
  428. case WIDEN_MULT_PLUS_EXPR:
  429. return (TYPE_UNSIGNED (type)
  430. ? (TYPE_SATURATING (type)
  431. ? usmadd_widen_optab : umadd_widen_optab)
  432. : (TYPE_SATURATING (type)
  433. ? ssmadd_widen_optab : smadd_widen_optab));
  434. case WIDEN_MULT_MINUS_EXPR:
  435. return (TYPE_UNSIGNED (type)
  436. ? (TYPE_SATURATING (type)
  437. ? usmsub_widen_optab : umsub_widen_optab)
  438. : (TYPE_SATURATING (type)
  439. ? ssmsub_widen_optab : smsub_widen_optab));
  440. case FMA_EXPR:
  441. return fma_optab;
  442. case REDUC_MAX_EXPR:
  443. return TYPE_UNSIGNED (type)
  444. ? reduc_umax_scal_optab : reduc_smax_scal_optab;
  445. case REDUC_MIN_EXPR:
  446. return TYPE_UNSIGNED (type)
  447. ? reduc_umin_scal_optab : reduc_smin_scal_optab;
  448. case REDUC_PLUS_EXPR:
  449. return reduc_plus_scal_optab;
  450. case VEC_WIDEN_MULT_HI_EXPR:
  451. return TYPE_UNSIGNED (type) ?
  452. vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
  453. case VEC_WIDEN_MULT_LO_EXPR:
  454. return TYPE_UNSIGNED (type) ?
  455. vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
  456. case VEC_WIDEN_MULT_EVEN_EXPR:
  457. return TYPE_UNSIGNED (type) ?
  458. vec_widen_umult_even_optab : vec_widen_smult_even_optab;
  459. case VEC_WIDEN_MULT_ODD_EXPR:
  460. return TYPE_UNSIGNED (type) ?
  461. vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
  462. case VEC_WIDEN_LSHIFT_HI_EXPR:
  463. return TYPE_UNSIGNED (type) ?
  464. vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
  465. case VEC_WIDEN_LSHIFT_LO_EXPR:
  466. return TYPE_UNSIGNED (type) ?
  467. vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
  468. case VEC_UNPACK_HI_EXPR:
  469. return TYPE_UNSIGNED (type) ?
  470. vec_unpacku_hi_optab : vec_unpacks_hi_optab;
  471. case VEC_UNPACK_LO_EXPR:
  472. return TYPE_UNSIGNED (type) ?
  473. vec_unpacku_lo_optab : vec_unpacks_lo_optab;
  474. case VEC_UNPACK_FLOAT_HI_EXPR:
  475. /* The signedness is determined from input operand. */
  476. return TYPE_UNSIGNED (type) ?
  477. vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
  478. case VEC_UNPACK_FLOAT_LO_EXPR:
  479. /* The signedness is determined from input operand. */
  480. return TYPE_UNSIGNED (type) ?
  481. vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
  482. case VEC_PACK_TRUNC_EXPR:
  483. return vec_pack_trunc_optab;
  484. case VEC_PACK_SAT_EXPR:
  485. return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
  486. case VEC_PACK_FIX_TRUNC_EXPR:
  487. /* The signedness is determined from output operand. */
  488. return TYPE_UNSIGNED (type) ?
  489. vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
  490. default:
  491. break;
  492. }
  493. trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
  494. switch (code)
  495. {
  496. case POINTER_PLUS_EXPR:
  497. case PLUS_EXPR:
  498. if (TYPE_SATURATING (type))
  499. return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab;
  500. return trapv ? addv_optab : add_optab;
  501. case MINUS_EXPR:
  502. if (TYPE_SATURATING (type))
  503. return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;
  504. return trapv ? subv_optab : sub_optab;
  505. case MULT_EXPR:
  506. if (TYPE_SATURATING (type))
  507. return TYPE_UNSIGNED (type) ? usmul_optab : ssmul_optab;
  508. return trapv ? smulv_optab : smul_optab;
  509. case NEGATE_EXPR:
  510. if (TYPE_SATURATING (type))
  511. return TYPE_UNSIGNED (type) ? usneg_optab : ssneg_optab;
  512. return trapv ? negv_optab : neg_optab;
  513. case ABS_EXPR:
  514. return trapv ? absv_optab : abs_optab;
  515. default:
  516. return unknown_optab;
  517. }
  518. }
  519. /* Given optab UNOPTAB that reduces a vector to a scalar, find instead the old
  520. optab that produces a vector with the reduction result in one element,
  521. for a tree with type TYPE. */
  522. optab
  523. scalar_reduc_to_vector (optab unoptab, const_tree type)
  524. {
  525. switch (unoptab)
  526. {
  527. case reduc_plus_scal_optab:
  528. return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
  529. case reduc_smin_scal_optab: return reduc_smin_optab;
  530. case reduc_umin_scal_optab: return reduc_umin_optab;
  531. case reduc_smax_scal_optab: return reduc_smax_optab;
  532. case reduc_umax_scal_optab: return reduc_umax_optab;
  533. default: return unknown_optab;
  534. }
  535. }
  536. /* Expand vector widening operations.
  537. There are two different classes of operations handled here:
  538. 1) Operations whose result is wider than all the arguments to the operation.
  539. Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
  540. In this case OP0 and optionally OP1 would be initialized,
  541. but WIDE_OP wouldn't (not relevant for this case).
  542. 2) Operations whose result is of the same size as the last argument to the
  543. operation, but wider than all the other arguments to the operation.
  544. Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
  545. In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
  546. E.g, when called to expand the following operations, this is how
  547. the arguments will be initialized:
  548. nops OP0 OP1 WIDE_OP
  549. widening-sum 2 oprnd0 - oprnd1
  550. widening-dot-product 3 oprnd0 oprnd1 oprnd2
  551. widening-mult 2 oprnd0 oprnd1 -
  552. type-promotion (vec-unpack) 1 oprnd0 - - */
  553. rtx
  554. expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
  555. rtx target, int unsignedp)
  556. {
  557. struct expand_operand eops[4];
  558. tree oprnd0, oprnd1, oprnd2;
  559. machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
  560. optab widen_pattern_optab;
  561. enum insn_code icode;
  562. int nops = TREE_CODE_LENGTH (ops->code);
  563. int op;
  564. oprnd0 = ops->op0;
  565. tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
  566. widen_pattern_optab =
  567. optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
  568. if (ops->code == WIDEN_MULT_PLUS_EXPR
  569. || ops->code == WIDEN_MULT_MINUS_EXPR)
  570. icode = find_widening_optab_handler (widen_pattern_optab,
  571. TYPE_MODE (TREE_TYPE (ops->op2)),
  572. tmode0, 0);
  573. else
  574. icode = optab_handler (widen_pattern_optab, tmode0);
  575. gcc_assert (icode != CODE_FOR_nothing);
  576. if (nops >= 2)
  577. {
  578. oprnd1 = ops->op1;
  579. tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
  580. }
  581. /* The last operand is of a wider mode than the rest of the operands. */
  582. if (nops == 2)
  583. wmode = tmode1;
  584. else if (nops == 3)
  585. {
  586. gcc_assert (tmode1 == tmode0);
  587. gcc_assert (op1);
  588. oprnd2 = ops->op2;
  589. wmode = TYPE_MODE (TREE_TYPE (oprnd2));
  590. }
  591. op = 0;
  592. create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
  593. create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
  594. if (op1)
  595. create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
  596. if (wide_op)
  597. create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
  598. expand_insn (icode, op, eops);
  599. return eops[0].value;
  600. }
  601. /* Generate code to perform an operation specified by TERNARY_OPTAB
  602. on operands OP0, OP1 and OP2, with result having machine-mode MODE.
  603. UNSIGNEDP is for the case where we have to widen the operands
  604. to perform the operation. It says to use zero-extension.
  605. If TARGET is nonzero, the value
  606. is generated there, if it is convenient to do so.
  607. In all cases an rtx is returned for the locus of the value;
  608. this may or may not be TARGET. */
  609. rtx
  610. expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
  611. rtx op1, rtx op2, rtx target, int unsignedp)
  612. {
  613. struct expand_operand ops[4];
  614. enum insn_code icode = optab_handler (ternary_optab, mode);
  615. gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
  616. create_output_operand (&ops[0], target, mode);
  617. create_convert_operand_from (&ops[1], op0, mode, unsignedp);
  618. create_convert_operand_from (&ops[2], op1, mode, unsignedp);
  619. create_convert_operand_from (&ops[3], op2, mode, unsignedp);
  620. expand_insn (icode, 4, ops);
  621. return ops[0].value;
  622. }
  623. /* Like expand_binop, but return a constant rtx if the result can be
  624. calculated at compile time. The arguments and return value are
  625. otherwise the same as for expand_binop. */
  626. rtx
  627. simplify_expand_binop (machine_mode mode, optab binoptab,
  628. rtx op0, rtx op1, rtx target, int unsignedp,
  629. enum optab_methods methods)
  630. {
  631. if (CONSTANT_P (op0) && CONSTANT_P (op1))
  632. {
  633. rtx x = simplify_binary_operation (optab_to_code (binoptab),
  634. mode, op0, op1);
  635. if (x)
  636. return x;
  637. }
  638. return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
  639. }
  640. /* Like simplify_expand_binop, but always put the result in TARGET.
  641. Return true if the expansion succeeded. */
  642. bool
  643. force_expand_binop (machine_mode mode, optab binoptab,
  644. rtx op0, rtx op1, rtx target, int unsignedp,
  645. enum optab_methods methods)
  646. {
  647. rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
  648. target, unsignedp, methods);
  649. if (x == 0)
  650. return false;
  651. if (x != target)
  652. emit_move_insn (target, x);
  653. return true;
  654. }
  655. /* Create a new vector value in VMODE with all elements set to OP. The
  656. mode of OP must be the element mode of VMODE. If OP is a constant,
  657. then the return value will be a constant. */
  658. static rtx
  659. expand_vector_broadcast (machine_mode vmode, rtx op)
  660. {
  661. enum insn_code icode;
  662. rtvec vec;
  663. rtx ret;
  664. int i, n;
  665. gcc_checking_assert (VECTOR_MODE_P (vmode));
  666. n = GET_MODE_NUNITS (vmode);
  667. vec = rtvec_alloc (n);
  668. for (i = 0; i < n; ++i)
  669. RTVEC_ELT (vec, i) = op;
  670. if (CONSTANT_P (op))
  671. return gen_rtx_CONST_VECTOR (vmode, vec);
  672. /* ??? If the target doesn't have a vec_init, then we have no easy way
  673. of performing this operation. Most of this sort of generic support
  674. is hidden away in the vector lowering support in gimple. */
  675. icode = optab_handler (vec_init_optab, vmode);
  676. if (icode == CODE_FOR_nothing)
  677. return NULL;
  678. ret = gen_reg_rtx (vmode);
  679. emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
  680. return ret;
  681. }
  682. /* This subroutine of expand_doubleword_shift handles the cases in which
  683. the effective shift value is >= BITS_PER_WORD. The arguments and return
  684. value are the same as for the parent routine, except that SUPERWORD_OP1
  685. is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
  686. INTO_TARGET may be null if the caller has decided to calculate it. */
  687. static bool
  688. expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
  689. rtx outof_target, rtx into_target,
  690. int unsignedp, enum optab_methods methods)
  691. {
  692. if (into_target != 0)
  693. if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
  694. into_target, unsignedp, methods))
  695. return false;
  696. if (outof_target != 0)
  697. {
  698. /* For a signed right shift, we must fill OUTOF_TARGET with copies
  699. of the sign bit, otherwise we must fill it with zeros. */
  700. if (binoptab != ashr_optab)
  701. emit_move_insn (outof_target, CONST0_RTX (word_mode));
  702. else
  703. if (!force_expand_binop (word_mode, binoptab,
  704. outof_input, GEN_INT (BITS_PER_WORD - 1),
  705. outof_target, unsignedp, methods))
  706. return false;
  707. }
  708. return true;
  709. }
  710. /* This subroutine of expand_doubleword_shift handles the cases in which
  711. the effective shift value is < BITS_PER_WORD. The arguments and return
  712. value are the same as for the parent routine. */
  713. static bool
  714. expand_subword_shift (machine_mode op1_mode, optab binoptab,
  715. rtx outof_input, rtx into_input, rtx op1,
  716. rtx outof_target, rtx into_target,
  717. int unsignedp, enum optab_methods methods,
  718. unsigned HOST_WIDE_INT shift_mask)
  719. {
  720. optab reverse_unsigned_shift, unsigned_shift;
  721. rtx tmp, carries;
  722. reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
  723. unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
  724. /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
  725. We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
  726. the opposite direction to BINOPTAB. */
  727. if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
  728. {
  729. carries = outof_input;
  730. tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
  731. op1_mode), op1_mode);
  732. tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
  733. 0, true, methods);
  734. }
  735. else
  736. {
  737. /* We must avoid shifting by BITS_PER_WORD bits since that is either
  738. the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
  739. has unknown behavior. Do a single shift first, then shift by the
  740. remainder. It's OK to use ~OP1 as the remainder if shift counts
  741. are truncated to the mode size. */
  742. carries = expand_binop (word_mode, reverse_unsigned_shift,
  743. outof_input, const1_rtx, 0, unsignedp, methods);
  744. if (shift_mask == BITS_PER_WORD - 1)
  745. {
  746. tmp = immed_wide_int_const
  747. (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
  748. tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
  749. 0, true, methods);
  750. }
  751. else
  752. {
  753. tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
  754. op1_mode), op1_mode);
  755. tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
  756. 0, true, methods);
  757. }
  758. }
  759. if (tmp == 0 || carries == 0)
  760. return false;
  761. carries = expand_binop (word_mode, reverse_unsigned_shift,
  762. carries, tmp, 0, unsignedp, methods);
  763. if (carries == 0)
  764. return false;
  765. /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
  766. so the result can go directly into INTO_TARGET if convenient. */
  767. tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
  768. into_target, unsignedp, methods);
  769. if (tmp == 0)
  770. return false;
  771. /* Now OR in the bits carried over from OUTOF_INPUT. */
  772. if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
  773. into_target, unsignedp, methods))
  774. return false;
  775. /* Use a standard word_mode shift for the out-of half. */
  776. if (outof_target != 0)
  777. if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
  778. outof_target, unsignedp, methods))
  779. return false;
  780. return true;
  781. }
  782. #ifdef HAVE_conditional_move
  783. /* Try implementing expand_doubleword_shift using conditional moves.
  784. The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
  785. otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
  786. are the shift counts to use in the former and latter case. All other
  787. arguments are the same as the parent routine. */
  788. static bool
  789. expand_doubleword_shift_condmove (machine_mode op1_mode, optab binoptab,
  790. enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
  791. rtx outof_input, rtx into_input,
  792. rtx subword_op1, rtx superword_op1,
  793. rtx outof_target, rtx into_target,
  794. int unsignedp, enum optab_methods methods,
  795. unsigned HOST_WIDE_INT shift_mask)
  796. {
  797. rtx outof_superword, into_superword;
  798. /* Put the superword version of the output into OUTOF_SUPERWORD and
  799. INTO_SUPERWORD. */
  800. outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
  801. if (outof_target != 0 && subword_op1 == superword_op1)
  802. {
  803. /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
  804. OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
  805. into_superword = outof_target;
  806. if (!expand_superword_shift (binoptab, outof_input, superword_op1,
  807. outof_superword, 0, unsignedp, methods))
  808. return false;
  809. }
  810. else
  811. {
  812. into_superword = gen_reg_rtx (word_mode);
  813. if (!expand_superword_shift (binoptab, outof_input, superword_op1,
  814. outof_superword, into_superword,
  815. unsignedp, methods))
  816. return false;
  817. }
  818. /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
  819. if (!expand_subword_shift (op1_mode, binoptab,
  820. outof_input, into_input, subword_op1,
  821. outof_target, into_target,
  822. unsignedp, methods, shift_mask))
  823. return false;
  824. /* Select between them. Do the INTO half first because INTO_SUPERWORD
  825. might be the current value of OUTOF_TARGET. */
  826. if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
  827. into_target, into_superword, word_mode, false))
  828. return false;
  829. if (outof_target != 0)
  830. if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
  831. outof_target, outof_superword,
  832. word_mode, false))
  833. return false;
  834. return true;
  835. }
  836. #endif
  837. /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
  838. OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
  839. input operand; the shift moves bits in the direction OUTOF_INPUT->
  840. INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
  841. of the target. OP1 is the shift count and OP1_MODE is its mode.
  842. If OP1 is constant, it will have been truncated as appropriate
  843. and is known to be nonzero.
  844. If SHIFT_MASK is zero, the result of word shifts is undefined when the
  845. shift count is outside the range [0, BITS_PER_WORD). This routine must
  846. avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
  847. If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
  848. masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
  849. fill with zeros or sign bits as appropriate.
  850. If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
  851. a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
  852. Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
  853. In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
  854. are undefined.
  855. BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
  856. may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
  857. OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
  858. function wants to calculate it itself.
  859. Return true if the shift could be successfully synthesized. */
  860. static bool
  861. expand_doubleword_shift (machine_mode op1_mode, optab binoptab,
  862. rtx outof_input, rtx into_input, rtx op1,
  863. rtx outof_target, rtx into_target,
  864. int unsignedp, enum optab_methods methods,
  865. unsigned HOST_WIDE_INT shift_mask)
  866. {
  867. rtx superword_op1, tmp, cmp1, cmp2;
  868. enum rtx_code cmp_code;
  869. /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
  870. fill the result with sign or zero bits as appropriate. If so, the value
  871. of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
  872. this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
  873. and INTO_INPUT), then emit code to set up OUTOF_TARGET.
  874. This isn't worthwhile for constant shifts since the optimizers will
  875. cope better with in-range shift counts. */
  876. if (shift_mask >= BITS_PER_WORD
  877. && outof_target != 0
  878. && !CONSTANT_P (op1))
  879. {
  880. if (!expand_doubleword_shift (op1_mode, binoptab,
  881. outof_input, into_input, op1,
  882. 0, into_target,
  883. unsignedp, methods, shift_mask))
  884. return false;
  885. if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
  886. outof_target, unsignedp, methods))
  887. return false;
  888. return true;
  889. }
  890. /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
  891. is true when the effective shift value is less than BITS_PER_WORD.
  892. Set SUPERWORD_OP1 to the shift count that should be used to shift
  893. OUTOF_INPUT into INTO_TARGET when the condition is false. */
  894. tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
  895. if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
  896. {
  897. /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
  898. is a subword shift count. */
  899. cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
  900. 0, true, methods);
  901. cmp2 = CONST0_RTX (op1_mode);
  902. cmp_code = EQ;
  903. superword_op1 = op1;
  904. }
  905. else
  906. {
  907. /* Set CMP1 to OP1 - BITS_PER_WORD. */
  908. cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
  909. 0, true, methods);
  910. cmp2 = CONST0_RTX (op1_mode);
  911. cmp_code = LT;
  912. superword_op1 = cmp1;
  913. }
  914. if (cmp1 == 0)
  915. return false;
  916. /* If we can compute the condition at compile time, pick the
  917. appropriate subroutine. */
  918. tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
  919. if (tmp != 0 && CONST_INT_P (tmp))
  920. {
  921. if (tmp == const0_rtx)
  922. return expand_superword_shift (binoptab, outof_input, superword_op1,
  923. outof_target, into_target,
  924. unsignedp, methods);
  925. else
  926. return expand_subword_shift (op1_mode, binoptab,
  927. outof_input, into_input, op1,
  928. outof_target, into_target,
  929. unsignedp, methods, shift_mask);
  930. }
  931. #ifdef HAVE_conditional_move
  932. /* Try using conditional moves to generate straight-line code. */
  933. {
  934. rtx_insn *start = get_last_insn ();
  935. if (expand_doubleword_shift_condmove (op1_mode, binoptab,
  936. cmp_code, cmp1, cmp2,
  937. outof_input, into_input,
  938. op1, superword_op1,
  939. outof_target, into_target,
  940. unsignedp, methods, shift_mask))
  941. return true;
  942. delete_insns_since (start);
  943. }
  944. #endif
  945. /* As a last resort, use branches to select the correct alternative. */
  946. rtx_code_label *subword_label = gen_label_rtx ();
  947. rtx_code_label *done_label = gen_label_rtx ();
  948. NO_DEFER_POP;
  949. do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
  950. 0, 0, subword_label, -1);
  951. OK_DEFER_POP;
  952. if (!expand_superword_shift (binoptab, outof_input, superword_op1,
  953. outof_target, into_target,
  954. unsignedp, methods))
  955. return false;
  956. emit_jump_insn (gen_jump (done_label));
  957. emit_barrier ();
  958. emit_label (subword_label);
  959. if (!expand_subword_shift (op1_mode, binoptab,
  960. outof_input, into_input, op1,
  961. outof_target, into_target,
  962. unsignedp, methods, shift_mask))
  963. return false;
  964. emit_label (done_label);
  965. return true;
  966. }
  967. /* Subroutine of expand_binop. Perform a double word multiplication of
  968. operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
  969. as the target's word_mode. This function return NULL_RTX if anything
  970. goes wrong, in which case it may have already emitted instructions
  971. which need to be deleted.
  972. If we want to multiply two two-word values and have normal and widening
  973. multiplies of single-word values, we can do this with three smaller
  974. multiplications.
  975. The multiplication proceeds as follows:
  976. _______________________
  977. [__op0_high_|__op0_low__]
  978. _______________________
  979. * [__op1_high_|__op1_low__]
  980. _______________________________________________
  981. _______________________
  982. (1) [__op0_low__*__op1_low__]
  983. _______________________
  984. (2a) [__op0_low__*__op1_high_]
  985. _______________________
  986. (2b) [__op0_high_*__op1_low__]
  987. _______________________
  988. (3) [__op0_high_*__op1_high_]
  989. This gives a 4-word result. Since we are only interested in the
  990. lower 2 words, partial result (3) and the upper words of (2a) and
  991. (2b) don't need to be calculated. Hence (2a) and (2b) can be
  992. calculated using non-widening multiplication.
  993. (1), however, needs to be calculated with an unsigned widening
  994. multiplication. If this operation is not directly supported we
  995. try using a signed widening multiplication and adjust the result.
  996. This adjustment works as follows:
  997. If both operands are positive then no adjustment is needed.
  998. If the operands have different signs, for example op0_low < 0 and
  999. op1_low >= 0, the instruction treats the most significant bit of
  1000. op0_low as a sign bit instead of a bit with significance
  1001. 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
  1002. with 2**BITS_PER_WORD - op0_low, and two's complements the
  1003. result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
  1004. the result.
  1005. Similarly, if both operands are negative, we need to add
  1006. (op0_low + op1_low) * 2**BITS_PER_WORD.
  1007. We use a trick to adjust quickly. We logically shift op0_low right
  1008. (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
  1009. op0_high (op1_high) before it is used to calculate 2b (2a). If no
  1010. logical shift exists, we do an arithmetic right shift and subtract
  1011. the 0 or -1. */
  1012. static rtx
  1013. expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
  1014. bool umulp, enum optab_methods methods)
  1015. {
  1016. int low = (WORDS_BIG_ENDIAN ? 1 : 0);
  1017. int high = (WORDS_BIG_ENDIAN ? 0 : 1);
  1018. rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
  1019. rtx product, adjust, product_high, temp;
  1020. rtx op0_high = operand_subword_force (op0, high, mode);
  1021. rtx op0_low = operand_subword_force (op0, low, mode);
  1022. rtx op1_high = operand_subword_force (op1, high, mode);
  1023. rtx op1_low = operand_subword_force (op1, low, mode);
  1024. /* If we're using an unsigned multiply to directly compute the product
  1025. of the low-order words of the operands and perform any required
  1026. adjustments of the operands, we begin by trying two more multiplications
  1027. and then computing the appropriate sum.
  1028. We have checked above that the required addition is provided.
  1029. Full-word addition will normally always succeed, especially if
  1030. it is provided at all, so we don't worry about its failure. The
  1031. multiplication may well fail, however, so we do handle that. */
  1032. if (!umulp)
  1033. {
  1034. /* ??? This could be done with emit_store_flag where available. */
  1035. temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
  1036. NULL_RTX, 1, methods);
  1037. if (temp)
  1038. op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
  1039. NULL_RTX, 0, OPTAB_DIRECT);
  1040. else
  1041. {
  1042. temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
  1043. NULL_RTX, 0, methods);
  1044. if (!temp)
  1045. return NULL_RTX;
  1046. op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
  1047. NULL_RTX, 0, OPTAB_DIRECT);
  1048. }
  1049. if (!op0_high)
  1050. return NULL_RTX;
  1051. }
  1052. adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
  1053. NULL_RTX, 0, OPTAB_DIRECT);
  1054. if (!adjust)
  1055. return NULL_RTX;
  1056. /* OP0_HIGH should now be dead. */
  1057. if (!umulp)
  1058. {
  1059. /* ??? This could be done with emit_store_flag where available. */
  1060. temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
  1061. NULL_RTX, 1, methods);
  1062. if (temp)
  1063. op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
  1064. NULL_RTX, 0, OPTAB_DIRECT);
  1065. else
  1066. {
  1067. temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
  1068. NULL_RTX, 0, methods);
  1069. if (!temp)
  1070. return NULL_RTX;
  1071. op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
  1072. NULL_RTX, 0, OPTAB_DIRECT);
  1073. }
  1074. if (!op1_high)
  1075. return NULL_RTX;
  1076. }
  1077. temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
  1078. NULL_RTX, 0, OPTAB_DIRECT);
  1079. if (!temp)
  1080. return NULL_RTX;
  1081. /* OP1_HIGH should now be dead. */
  1082. adjust = expand_binop (word_mode, add_optab, adjust, temp,
  1083. NULL_RTX, 0, OPTAB_DIRECT);
  1084. if (target && !REG_P (target))
  1085. target = NULL_RTX;
  1086. if (umulp)
  1087. product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
  1088. target, 1, OPTAB_DIRECT);
  1089. else
  1090. product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
  1091. target, 1, OPTAB_DIRECT);
  1092. if (!product)
  1093. return NULL_RTX;
  1094. product_high = operand_subword (product, high, 1, mode);
  1095. adjust = expand_binop (word_mode, add_optab, product_high, adjust,
  1096. NULL_RTX, 0, OPTAB_DIRECT);
  1097. emit_move_insn (product_high, adjust);
  1098. return product;
  1099. }
  1100. /* Wrapper around expand_binop which takes an rtx code to specify
  1101. the operation to perform, not an optab pointer. All other
  1102. arguments are the same. */
  1103. rtx
  1104. expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
  1105. rtx op1, rtx target, int unsignedp,
  1106. enum optab_methods methods)
  1107. {
  1108. optab binop = code_to_optab (code);
  1109. gcc_assert (binop);
  1110. return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
  1111. }
  1112. /* Return whether OP0 and OP1 should be swapped when expanding a commutative
  1113. binop. Order them according to commutative_operand_precedence and, if
  1114. possible, try to put TARGET or a pseudo first. */
  1115. static bool
  1116. swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
  1117. {
  1118. int op0_prec = commutative_operand_precedence (op0);
  1119. int op1_prec = commutative_operand_precedence (op1);
  1120. if (op0_prec < op1_prec)
  1121. return true;
  1122. if (op0_prec > op1_prec)
  1123. return false;
  1124. /* With equal precedence, both orders are ok, but it is better if the
  1125. first operand is TARGET, or if both TARGET and OP0 are pseudos. */
  1126. if (target == 0 || REG_P (target))
  1127. return (REG_P (op1) && !REG_P (op0)) || target == op1;
  1128. else
  1129. return rtx_equal_p (op1, target);
  1130. }
  1131. /* Return true if BINOPTAB implements a shift operation. */
  1132. static bool
  1133. shift_optab_p (optab binoptab)
  1134. {
  1135. switch (optab_to_code (binoptab))
  1136. {
  1137. case ASHIFT:
  1138. case SS_ASHIFT:
  1139. case US_ASHIFT:
  1140. case ASHIFTRT:
  1141. case LSHIFTRT:
  1142. case ROTATE:
  1143. case ROTATERT:
  1144. return true;
  1145. default:
  1146. return false;
  1147. }
  1148. }
  1149. /* Return true if BINOPTAB implements a commutative binary operation. */
  1150. static bool
  1151. commutative_optab_p (optab binoptab)
  1152. {
  1153. return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
  1154. || binoptab == smul_widen_optab
  1155. || binoptab == umul_widen_optab
  1156. || binoptab == smul_highpart_optab
  1157. || binoptab == umul_highpart_optab);
  1158. }
  1159. /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
  1160. optimizing, and if the operand is a constant that costs more than
  1161. 1 instruction, force the constant into a register and return that
  1162. register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
  1163. static rtx
  1164. avoid_expensive_constant (machine_mode mode, optab binoptab,
  1165. int opn, rtx x, bool unsignedp)
  1166. {
  1167. bool speed = optimize_insn_for_speed_p ();
  1168. if (mode != VOIDmode
  1169. && optimize
  1170. && CONSTANT_P (x)
  1171. && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
  1172. > set_src_cost (x, speed)))
  1173. {
  1174. if (CONST_INT_P (x))
  1175. {
  1176. HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
  1177. if (intval != INTVAL (x))
  1178. x = GEN_INT (intval);
  1179. }
  1180. else
  1181. x = convert_modes (mode, VOIDmode, x, unsignedp);
  1182. x = force_reg (mode, x);
  1183. }
  1184. return x;
  1185. }
  1186. /* Helper function for expand_binop: handle the case where there
  1187. is an insn that directly implements the indicated operation.
  1188. Returns null if this is not possible. */
  1189. static rtx
  1190. expand_binop_directly (machine_mode mode, optab binoptab,
  1191. rtx op0, rtx op1,
  1192. rtx target, int unsignedp, enum optab_methods methods,
  1193. rtx_insn *last)
  1194. {
  1195. machine_mode from_mode = widened_mode (mode, op0, op1);
  1196. enum insn_code icode = find_widening_optab_handler (binoptab, mode,
  1197. from_mode, 1);
  1198. machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
  1199. machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
  1200. machine_mode mode0, mode1, tmp_mode;
  1201. struct expand_operand ops[3];
  1202. bool commutative_p;
  1203. rtx pat;
  1204. rtx xop0 = op0, xop1 = op1;
  1205. rtx swap;
  1206. /* If it is a commutative operator and the modes would match
  1207. if we would swap the operands, we can save the conversions. */
  1208. commutative_p = commutative_optab_p (binoptab);
  1209. if (commutative_p
  1210. && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
  1211. && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
  1212. {
  1213. swap = xop0;
  1214. xop0 = xop1;
  1215. xop1 = swap;
  1216. }
  1217. /* If we are optimizing, force expensive constants into a register. */
  1218. xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
  1219. if (!shift_optab_p (binoptab))
  1220. xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
  1221. /* In case the insn wants input operands in modes different from
  1222. those of the actual operands, convert the operands. It would
  1223. seem that we don't need to convert CONST_INTs, but we do, so
  1224. that they're properly zero-extended, sign-extended or truncated
  1225. for their mode. */
  1226. mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
  1227. if (xmode0 != VOIDmode && xmode0 != mode0)
  1228. {
  1229. xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
  1230. mode0 = xmode0;
  1231. }
  1232. mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
  1233. if (xmode1 != VOIDmode && xmode1 != mode1)
  1234. {
  1235. xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
  1236. mode1 = xmode1;
  1237. }
  1238. /* If operation is commutative,
  1239. try to make the first operand a register.
  1240. Even better, try to make it the same as the target.
  1241. Also try to make the last operand a constant. */
  1242. if (commutative_p
  1243. && swap_commutative_operands_with_target (target, xop0, xop1))
  1244. {
  1245. swap = xop1;
  1246. xop1 = xop0;
  1247. xop0 = swap;
  1248. }
  1249. /* Now, if insn's predicates don't allow our operands, put them into
  1250. pseudo regs. */
  1251. if (binoptab == vec_pack_trunc_optab
  1252. || binoptab == vec_pack_usat_optab
  1253. || binoptab == vec_pack_ssat_optab
  1254. || binoptab == vec_pack_ufix_trunc_optab
  1255. || binoptab == vec_pack_sfix_trunc_optab)
  1256. {
  1257. /* The mode of the result is different then the mode of the
  1258. arguments. */
  1259. tmp_mode = insn_data[(int) icode].operand[0].mode;
  1260. if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
  1261. {
  1262. delete_insns_since (last);
  1263. return NULL_RTX;
  1264. }
  1265. }
  1266. else
  1267. tmp_mode = mode;
  1268. create_output_operand (&ops[0], target, tmp_mode);
  1269. create_input_operand (&ops[1], xop0, mode0);
  1270. create_input_operand (&ops[2], xop1, mode1);
  1271. pat = maybe_gen_insn (icode, 3, ops);
  1272. if (pat)
  1273. {
  1274. /* If PAT is composed of more than one insn, try to add an appropriate
  1275. REG_EQUAL note to it. If we can't because TEMP conflicts with an
  1276. operand, call expand_binop again, this time without a target. */
  1277. if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
  1278. && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
  1279. optab_to_code (binoptab),
  1280. ops[1].value, ops[2].value))
  1281. {
  1282. delete_insns_since (last);
  1283. return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
  1284. unsignedp, methods);
  1285. }
  1286. emit_insn (pat);
  1287. return ops[0].value;
  1288. }
  1289. delete_insns_since (last);
  1290. return NULL_RTX;
  1291. }
  1292. /* Generate code to perform an operation specified by BINOPTAB
  1293. on operands OP0 and OP1, with result having machine-mode MODE.
  1294. UNSIGNEDP is for the case where we have to widen the operands
  1295. to perform the operation. It says to use zero-extension.
  1296. If TARGET is nonzero, the value
  1297. is generated there, if it is convenient to do so.
  1298. In all cases an rtx is returned for the locus of the value;
  1299. this may or may not be TARGET. */
  1300. rtx
  1301. expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
  1302. rtx target, int unsignedp, enum optab_methods methods)
  1303. {
  1304. enum optab_methods next_methods
  1305. = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
  1306. ? OPTAB_WIDEN : methods);
  1307. enum mode_class mclass;
  1308. machine_mode wider_mode;
  1309. rtx libfunc;
  1310. rtx temp;
  1311. rtx_insn *entry_last = get_last_insn ();
  1312. rtx_insn *last;
  1313. mclass = GET_MODE_CLASS (mode);
  1314. /* If subtracting an integer constant, convert this into an addition of
  1315. the negated constant. */
  1316. if (binoptab == sub_optab && CONST_INT_P (op1))
  1317. {
  1318. op1 = negate_rtx (mode, op1);
  1319. binoptab = add_optab;
  1320. }
  1321. /* Record where to delete back to if we backtrack. */
  1322. last = get_last_insn ();
  1323. /* If we can do it with a three-operand insn, do so. */
  1324. if (methods != OPTAB_MUST_WIDEN
  1325. && find_widening_optab_handler (binoptab, mode,
  1326. widened_mode (mode, op0, op1), 1)
  1327. != CODE_FOR_nothing)
  1328. {
  1329. temp = expand_binop_directly (mode, binoptab, op0, op1, target,
  1330. unsignedp, methods, last);
  1331. if (temp)
  1332. return temp;
  1333. }
  1334. /* If we were trying to rotate, and that didn't work, try rotating
  1335. the other direction before falling back to shifts and bitwise-or. */
  1336. if (((binoptab == rotl_optab
  1337. && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
  1338. || (binoptab == rotr_optab
  1339. && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
  1340. && mclass == MODE_INT)
  1341. {
  1342. optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
  1343. rtx newop1;
  1344. unsigned int bits = GET_MODE_PRECISION (mode);
  1345. if (CONST_INT_P (op1))
  1346. newop1 = GEN_INT (bits - INTVAL (op1));
  1347. else if (targetm.shift_truncation_mask (mode) == bits - 1)
  1348. newop1 = negate_rtx (GET_MODE (op1), op1);
  1349. else
  1350. newop1 = expand_binop (GET_MODE (op1), sub_optab,
  1351. gen_int_mode (bits, GET_MODE (op1)), op1,
  1352. NULL_RTX, unsignedp, OPTAB_DIRECT);
  1353. temp = expand_binop_directly (mode, otheroptab, op0, newop1,
  1354. target, unsignedp, methods, last);
  1355. if (temp)
  1356. return temp;
  1357. }
  1358. /* If this is a multiply, see if we can do a widening operation that
  1359. takes operands of this mode and makes a wider mode. */
  1360. if (binoptab == smul_optab
  1361. && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
  1362. && (widening_optab_handler ((unsignedp ? umul_widen_optab
  1363. : smul_widen_optab),
  1364. GET_MODE_2XWIDER_MODE (mode), mode)
  1365. != CODE_FOR_nothing))
  1366. {
  1367. temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
  1368. unsignedp ? umul_widen_optab : smul_widen_optab,
  1369. op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
  1370. if (temp != 0)
  1371. {
  1372. if (GET_MODE_CLASS (mode) == MODE_INT
  1373. && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
  1374. return gen_lowpart (mode, temp);
  1375. else
  1376. return convert_to_mode (mode, temp, unsignedp);
  1377. }
  1378. }
  1379. /* If this is a vector shift by a scalar, see if we can do a vector
  1380. shift by a vector. If so, broadcast the scalar into a vector. */
  1381. if (mclass == MODE_VECTOR_INT)
  1382. {
  1383. optab otheroptab = unknown_optab;
  1384. if (binoptab == ashl_optab)
  1385. otheroptab = vashl_optab;
  1386. else if (binoptab == ashr_optab)
  1387. otheroptab = vashr_optab;
  1388. else if (binoptab == lshr_optab)
  1389. otheroptab = vlshr_optab;
  1390. else if (binoptab == rotl_optab)
  1391. otheroptab = vrotl_optab;
  1392. else if (binoptab == rotr_optab)
  1393. otheroptab = vrotr_optab;
  1394. if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
  1395. {
  1396. rtx vop1 = expand_vector_broadcast (mode, op1);
  1397. if (vop1)
  1398. {
  1399. temp = expand_binop_directly (mode, otheroptab, op0, vop1,
  1400. target, unsignedp, methods, last);
  1401. if (temp)
  1402. return temp;
  1403. }
  1404. }
  1405. }
  1406. /* Look for a wider mode of the same class for which we think we
  1407. can open-code the operation. Check for a widening multiply at the
  1408. wider mode as well. */
  1409. if (CLASS_HAS_WIDER_MODES_P (mclass)
  1410. && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
  1411. for (wider_mode = GET_MODE_WIDER_MODE (mode);
  1412. wider_mode != VOIDmode;
  1413. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  1414. {
  1415. if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
  1416. || (binoptab == smul_optab
  1417. && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
  1418. && (find_widening_optab_handler ((unsignedp
  1419. ? umul_widen_optab
  1420. : smul_widen_optab),
  1421. GET_MODE_WIDER_MODE (wider_mode),
  1422. mode, 0)
  1423. != CODE_FOR_nothing)))
  1424. {
  1425. rtx xop0 = op0, xop1 = op1;
  1426. int no_extend = 0;
  1427. /* For certain integer operations, we need not actually extend
  1428. the narrow operands, as long as we will truncate
  1429. the results to the same narrowness. */
  1430. if ((binoptab == ior_optab || binoptab == and_optab
  1431. || binoptab == xor_optab
  1432. || binoptab == add_optab || binoptab == sub_optab
  1433. || binoptab == smul_optab || binoptab == ashl_optab)
  1434. && mclass == MODE_INT)
  1435. {
  1436. no_extend = 1;
  1437. xop0 = avoid_expensive_constant (mode, binoptab, 0,
  1438. xop0, unsignedp);
  1439. if (binoptab != ashl_optab)
  1440. xop1 = avoid_expensive_constant (mode, binoptab, 1,
  1441. xop1, unsignedp);
  1442. }
  1443. xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
  1444. /* The second operand of a shift must always be extended. */
  1445. xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
  1446. no_extend && binoptab != ashl_optab);
  1447. temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
  1448. unsignedp, OPTAB_DIRECT);
  1449. if (temp)
  1450. {
  1451. if (mclass != MODE_INT
  1452. || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
  1453. {
  1454. if (target == 0)
  1455. target = gen_reg_rtx (mode);
  1456. convert_move (target, temp, 0);
  1457. return target;
  1458. }
  1459. else
  1460. return gen_lowpart (mode, temp);
  1461. }
  1462. else
  1463. delete_insns_since (last);
  1464. }
  1465. }
  1466. /* If operation is commutative,
  1467. try to make the first operand a register.
  1468. Even better, try to make it the same as the target.
  1469. Also try to make the last operand a constant. */
  1470. if (commutative_optab_p (binoptab)
  1471. && swap_commutative_operands_with_target (target, op0, op1))
  1472. {
  1473. temp = op1;
  1474. op1 = op0;
  1475. op0 = temp;
  1476. }
  1477. /* These can be done a word at a time. */
  1478. if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
  1479. && mclass == MODE_INT
  1480. && GET_MODE_SIZE (mode) > UNITS_PER_WORD
  1481. && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
  1482. {
  1483. int i;
  1484. rtx_insn *insns;
  1485. /* If TARGET is the same as one of the operands, the REG_EQUAL note
  1486. won't be accurate, so use a new target. */
  1487. if (target == 0
  1488. || target == op0
  1489. || target == op1
  1490. || !valid_multiword_target_p (target))
  1491. target = gen_reg_rtx (mode);
  1492. start_sequence ();
  1493. /* Do the actual arithmetic. */
  1494. for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
  1495. {
  1496. rtx target_piece = operand_subword (target, i, 1, mode);
  1497. rtx x = expand_binop (word_mode, binoptab,
  1498. operand_subword_force (op0, i, mode),
  1499. operand_subword_force (op1, i, mode),
  1500. target_piece, unsignedp, next_methods);
  1501. if (x == 0)
  1502. break;
  1503. if (target_piece != x)
  1504. emit_move_insn (target_piece, x);
  1505. }
  1506. insns = get_insns ();
  1507. end_sequence ();
  1508. if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
  1509. {
  1510. emit_insn (insns);
  1511. return target;
  1512. }
  1513. }
  1514. /* Synthesize double word shifts from single word shifts. */
  1515. if ((binoptab == lshr_optab || binoptab == ashl_optab
  1516. || binoptab == ashr_optab)
  1517. && mclass == MODE_INT
  1518. && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
  1519. && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
  1520. && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
  1521. && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
  1522. && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
  1523. && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
  1524. {
  1525. unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
  1526. machine_mode op1_mode;
  1527. double_shift_mask = targetm.shift_truncation_mask (mode);
  1528. shift_mask = targetm.shift_truncation_mask (word_mode);
  1529. op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
  1530. /* Apply the truncation to constant shifts. */
  1531. if (double_shift_mask > 0 && CONST_INT_P (op1))
  1532. op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
  1533. if (op1 == CONST0_RTX (op1_mode))
  1534. return op0;
  1535. /* Make sure that this is a combination that expand_doubleword_shift
  1536. can handle. See the comments there for details. */
  1537. if (double_shift_mask == 0
  1538. || (shift_mask == BITS_PER_WORD - 1
  1539. && double_shift_mask == BITS_PER_WORD * 2 - 1))
  1540. {
  1541. rtx_insn *insns;
  1542. rtx into_target, outof_target;
  1543. rtx into_input, outof_input;
  1544. int left_shift, outof_word;
  1545. /* If TARGET is the same as one of the operands, the REG_EQUAL note
  1546. won't be accurate, so use a new target. */
  1547. if (target == 0
  1548. || target == op0
  1549. || target == op1
  1550. || !valid_multiword_target_p (target))
  1551. target = gen_reg_rtx (mode);
  1552. start_sequence ();
  1553. /* OUTOF_* is the word we are shifting bits away from, and
  1554. INTO_* is the word that we are shifting bits towards, thus
  1555. they differ depending on the direction of the shift and
  1556. WORDS_BIG_ENDIAN. */
  1557. left_shift = binoptab == ashl_optab;
  1558. outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
  1559. outof_target = operand_subword (target, outof_word, 1, mode);
  1560. into_target = operand_subword (target, 1 - outof_word, 1, mode);
  1561. outof_input = operand_subword_force (op0, outof_word, mode);
  1562. into_input = operand_subword_force (op0, 1 - outof_word, mode);
  1563. if (expand_doubleword_shift (op1_mode, binoptab,
  1564. outof_input, into_input, op1,
  1565. outof_target, into_target,
  1566. unsignedp, next_methods, shift_mask))
  1567. {
  1568. insns = get_insns ();
  1569. end_sequence ();
  1570. emit_insn (insns);
  1571. return target;
  1572. }
  1573. end_sequence ();
  1574. }
  1575. }
  1576. /* Synthesize double word rotates from single word shifts. */
  1577. if ((binoptab == rotl_optab || binoptab == rotr_optab)
  1578. && mclass == MODE_INT
  1579. && CONST_INT_P (op1)
  1580. && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
  1581. && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
  1582. && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
  1583. {
  1584. rtx_insn *insns;
  1585. rtx into_target, outof_target;
  1586. rtx into_input, outof_input;
  1587. rtx inter;
  1588. int shift_count, left_shift, outof_word;
  1589. /* If TARGET is the same as one of the operands, the REG_EQUAL note
  1590. won't be accurate, so use a new target. Do this also if target is not
  1591. a REG, first because having a register instead may open optimization
  1592. opportunities, and second because if target and op0 happen to be MEMs
  1593. designating the same location, we would risk clobbering it too early
  1594. in the code sequence we generate below. */
  1595. if (target == 0
  1596. || target == op0
  1597. || target == op1
  1598. || !REG_P (target)
  1599. || !valid_multiword_target_p (target))
  1600. target = gen_reg_rtx (mode);
  1601. start_sequence ();
  1602. shift_count = INTVAL (op1);
  1603. /* OUTOF_* is the word we are shifting bits away from, and
  1604. INTO_* is the word that we are shifting bits towards, thus
  1605. they differ depending on the direction of the shift and
  1606. WORDS_BIG_ENDIAN. */
  1607. left_shift = (binoptab == rotl_optab);
  1608. outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
  1609. outof_target = operand_subword (target, outof_word, 1, mode);
  1610. into_target = operand_subword (target, 1 - outof_word, 1, mode);
  1611. outof_input = operand_subword_force (op0, outof_word, mode);
  1612. into_input = operand_subword_force (op0, 1 - outof_word, mode);
  1613. if (shift_count == BITS_PER_WORD)
  1614. {
  1615. /* This is just a word swap. */
  1616. emit_move_insn (outof_target, into_input);
  1617. emit_move_insn (into_target, outof_input);
  1618. inter = const0_rtx;
  1619. }
  1620. else
  1621. {
  1622. rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
  1623. rtx first_shift_count, second_shift_count;
  1624. optab reverse_unsigned_shift, unsigned_shift;
  1625. reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
  1626. ? lshr_optab : ashl_optab);
  1627. unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
  1628. ? ashl_optab : lshr_optab);
  1629. if (shift_count > BITS_PER_WORD)
  1630. {
  1631. first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
  1632. second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
  1633. }
  1634. else
  1635. {
  1636. first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
  1637. second_shift_count = GEN_INT (shift_count);
  1638. }
  1639. into_temp1 = expand_binop (word_mode, unsigned_shift,
  1640. outof_input, first_shift_count,
  1641. NULL_RTX, unsignedp, next_methods);
  1642. into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
  1643. into_input, second_shift_count,
  1644. NULL_RTX, unsignedp, next_methods);
  1645. if (into_temp1 != 0 && into_temp2 != 0)
  1646. inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
  1647. into_target, unsignedp, next_methods);
  1648. else
  1649. inter = 0;
  1650. if (inter != 0 && inter != into_target)
  1651. emit_move_insn (into_target, inter);
  1652. outof_temp1 = expand_binop (word_mode, unsigned_shift,
  1653. into_input, first_shift_count,
  1654. NULL_RTX, unsignedp, next_methods);
  1655. outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
  1656. outof_input, second_shift_count,
  1657. NULL_RTX, unsignedp, next_methods);
  1658. if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
  1659. inter = expand_binop (word_mode, ior_optab,
  1660. outof_temp1, outof_temp2,
  1661. outof_target, unsignedp, next_methods);
  1662. if (inter != 0 && inter != outof_target)
  1663. emit_move_insn (outof_target, inter);
  1664. }
  1665. insns = get_insns ();
  1666. end_sequence ();
  1667. if (inter != 0)
  1668. {
  1669. emit_insn (insns);
  1670. return target;
  1671. }
  1672. }
  1673. /* These can be done a word at a time by propagating carries. */
  1674. if ((binoptab == add_optab || binoptab == sub_optab)
  1675. && mclass == MODE_INT
  1676. && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
  1677. && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
  1678. {
  1679. unsigned int i;
  1680. optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
  1681. const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
  1682. rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
  1683. rtx xop0, xop1, xtarget;
  1684. /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
  1685. value is one of those, use it. Otherwise, use 1 since it is the
  1686. one easiest to get. */
  1687. #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
  1688. int normalizep = STORE_FLAG_VALUE;
  1689. #else
  1690. int normalizep = 1;
  1691. #endif
  1692. /* Prepare the operands. */
  1693. xop0 = force_reg (mode, op0);
  1694. xop1 = force_reg (mode, op1);
  1695. xtarget = gen_reg_rtx (mode);
  1696. if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
  1697. target = xtarget;
  1698. /* Indicate for flow that the entire target reg is being set. */
  1699. if (REG_P (target))
  1700. emit_clobber (xtarget);
  1701. /* Do the actual arithmetic. */
  1702. for (i = 0; i < nwords; i++)
  1703. {
  1704. int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
  1705. rtx target_piece = operand_subword (xtarget, index, 1, mode);
  1706. rtx op0_piece = operand_subword_force (xop0, index, mode);
  1707. rtx op1_piece = operand_subword_force (xop1, index, mode);
  1708. rtx x;
  1709. /* Main add/subtract of the input operands. */
  1710. x = expand_binop (word_mode, binoptab,
  1711. op0_piece, op1_piece,
  1712. target_piece, unsignedp, next_methods);
  1713. if (x == 0)
  1714. break;
  1715. if (i + 1 < nwords)
  1716. {
  1717. /* Store carry from main add/subtract. */
  1718. carry_out = gen_reg_rtx (word_mode);
  1719. carry_out = emit_store_flag_force (carry_out,
  1720. (binoptab == add_optab
  1721. ? LT : GT),
  1722. x, op0_piece,
  1723. word_mode, 1, normalizep);
  1724. }
  1725. if (i > 0)
  1726. {
  1727. rtx newx;
  1728. /* Add/subtract previous carry to main result. */
  1729. newx = expand_binop (word_mode,
  1730. normalizep == 1 ? binoptab : otheroptab,
  1731. x, carry_in,
  1732. NULL_RTX, 1, next_methods);
  1733. if (i + 1 < nwords)
  1734. {
  1735. /* Get out carry from adding/subtracting carry in. */
  1736. rtx carry_tmp = gen_reg_rtx (word_mode);
  1737. carry_tmp = emit_store_flag_force (carry_tmp,
  1738. (binoptab == add_optab
  1739. ? LT : GT),
  1740. newx, x,
  1741. word_mode, 1, normalizep);
  1742. /* Logical-ior the two poss. carry together. */
  1743. carry_out = expand_binop (word_mode, ior_optab,
  1744. carry_out, carry_tmp,
  1745. carry_out, 0, next_methods);
  1746. if (carry_out == 0)
  1747. break;
  1748. }
  1749. emit_move_insn (target_piece, newx);
  1750. }
  1751. else
  1752. {
  1753. if (x != target_piece)
  1754. emit_move_insn (target_piece, x);
  1755. }
  1756. carry_in = carry_out;
  1757. }
  1758. if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
  1759. {
  1760. if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
  1761. || ! rtx_equal_p (target, xtarget))
  1762. {
  1763. rtx temp = emit_move_insn (target, xtarget);
  1764. set_dst_reg_note (temp, REG_EQUAL,
  1765. gen_rtx_fmt_ee (optab_to_code (binoptab),
  1766. mode, copy_rtx (xop0),
  1767. copy_rtx (xop1)),
  1768. target);
  1769. }
  1770. else
  1771. target = xtarget;
  1772. return target;
  1773. }
  1774. else
  1775. delete_insns_since (last);
  1776. }
  1777. /* Attempt to synthesize double word multiplies using a sequence of word
  1778. mode multiplications. We first attempt to generate a sequence using a
  1779. more efficient unsigned widening multiply, and if that fails we then
  1780. try using a signed widening multiply. */
  1781. if (binoptab == smul_optab
  1782. && mclass == MODE_INT
  1783. && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
  1784. && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
  1785. && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
  1786. {
  1787. rtx product = NULL_RTX;
  1788. if (widening_optab_handler (umul_widen_optab, mode, word_mode)
  1789. != CODE_FOR_nothing)
  1790. {
  1791. product = expand_doubleword_mult (mode, op0, op1, target,
  1792. true, methods);
  1793. if (!product)
  1794. delete_insns_since (last);
  1795. }
  1796. if (product == NULL_RTX
  1797. && widening_optab_handler (smul_widen_optab, mode, word_mode)
  1798. != CODE_FOR_nothing)
  1799. {
  1800. product = expand_doubleword_mult (mode, op0, op1, target,
  1801. false, methods);
  1802. if (!product)
  1803. delete_insns_since (last);
  1804. }
  1805. if (product != NULL_RTX)
  1806. {
  1807. if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
  1808. {
  1809. temp = emit_move_insn (target ? target : product, product);
  1810. set_dst_reg_note (temp,
  1811. REG_EQUAL,
  1812. gen_rtx_fmt_ee (MULT, mode,
  1813. copy_rtx (op0),
  1814. copy_rtx (op1)),
  1815. target ? target : product);
  1816. }
  1817. return product;
  1818. }
  1819. }
  1820. /* It can't be open-coded in this mode.
  1821. Use a library call if one is available and caller says that's ok. */
  1822. libfunc = optab_libfunc (binoptab, mode);
  1823. if (libfunc
  1824. && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
  1825. {
  1826. rtx_insn *insns;
  1827. rtx op1x = op1;
  1828. machine_mode op1_mode = mode;
  1829. rtx value;
  1830. start_sequence ();
  1831. if (shift_optab_p (binoptab))
  1832. {
  1833. op1_mode = targetm.libgcc_shift_count_mode ();
  1834. /* Specify unsigned here,
  1835. since negative shift counts are meaningless. */
  1836. op1x = convert_to_mode (op1_mode, op1, 1);
  1837. }
  1838. if (GET_MODE (op0) != VOIDmode
  1839. && GET_MODE (op0) != mode)
  1840. op0 = convert_to_mode (mode, op0, unsignedp);
  1841. /* Pass 1 for NO_QUEUE so we don't lose any increments
  1842. if the libcall is cse'd or moved. */
  1843. value = emit_library_call_value (libfunc,
  1844. NULL_RTX, LCT_CONST, mode, 2,
  1845. op0, mode, op1x, op1_mode);
  1846. insns = get_insns ();
  1847. end_sequence ();
  1848. target = gen_reg_rtx (mode);
  1849. emit_libcall_block_1 (insns, target, value,
  1850. gen_rtx_fmt_ee (optab_to_code (binoptab),
  1851. mode, op0, op1),
  1852. trapv_binoptab_p (binoptab));
  1853. return target;
  1854. }
  1855. delete_insns_since (last);
  1856. /* It can't be done in this mode. Can we do it in a wider mode? */
  1857. if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
  1858. || methods == OPTAB_MUST_WIDEN))
  1859. {
  1860. /* Caller says, don't even try. */
  1861. delete_insns_since (entry_last);
  1862. return 0;
  1863. }
  1864. /* Compute the value of METHODS to pass to recursive calls.
  1865. Don't allow widening to be tried recursively. */
  1866. methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
  1867. /* Look for a wider mode of the same class for which it appears we can do
  1868. the operation. */
  1869. if (CLASS_HAS_WIDER_MODES_P (mclass))
  1870. {
  1871. for (wider_mode = GET_MODE_WIDER_MODE (mode);
  1872. wider_mode != VOIDmode;
  1873. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  1874. {
  1875. if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
  1876. != CODE_FOR_nothing
  1877. || (methods == OPTAB_LIB
  1878. && optab_libfunc (binoptab, wider_mode)))
  1879. {
  1880. rtx xop0 = op0, xop1 = op1;
  1881. int no_extend = 0;
  1882. /* For certain integer operations, we need not actually extend
  1883. the narrow operands, as long as we will truncate
  1884. the results to the same narrowness. */
  1885. if ((binoptab == ior_optab || binoptab == and_optab
  1886. || binoptab == xor_optab
  1887. || binoptab == add_optab || binoptab == sub_optab
  1888. || binoptab == smul_optab || binoptab == ashl_optab)
  1889. && mclass == MODE_INT)
  1890. no_extend = 1;
  1891. xop0 = widen_operand (xop0, wider_mode, mode,
  1892. unsignedp, no_extend);
  1893. /* The second operand of a shift must always be extended. */
  1894. xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
  1895. no_extend && binoptab != ashl_optab);
  1896. temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
  1897. unsignedp, methods);
  1898. if (temp)
  1899. {
  1900. if (mclass != MODE_INT
  1901. || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
  1902. {
  1903. if (target == 0)
  1904. target = gen_reg_rtx (mode);
  1905. convert_move (target, temp, 0);
  1906. return target;
  1907. }
  1908. else
  1909. return gen_lowpart (mode, temp);
  1910. }
  1911. else
  1912. delete_insns_since (last);
  1913. }
  1914. }
  1915. }
  1916. delete_insns_since (entry_last);
  1917. return 0;
  1918. }
  1919. /* Expand a binary operator which has both signed and unsigned forms.
  1920. UOPTAB is the optab for unsigned operations, and SOPTAB is for
  1921. signed operations.
  1922. If we widen unsigned operands, we may use a signed wider operation instead
  1923. of an unsigned wider operation, since the result would be the same. */
  1924. rtx
  1925. sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
  1926. rtx op0, rtx op1, rtx target, int unsignedp,
  1927. enum optab_methods methods)
  1928. {
  1929. rtx temp;
  1930. optab direct_optab = unsignedp ? uoptab : soptab;
  1931. bool save_enable;
  1932. /* Do it without widening, if possible. */
  1933. temp = expand_binop (mode, direct_optab, op0, op1, target,
  1934. unsignedp, OPTAB_DIRECT);
  1935. if (temp || methods == OPTAB_DIRECT)
  1936. return temp;
  1937. /* Try widening to a signed int. Disable any direct use of any
  1938. signed insn in the current mode. */
  1939. save_enable = swap_optab_enable (soptab, mode, false);
  1940. temp = expand_binop (mode, soptab, op0, op1, target,
  1941. unsignedp, OPTAB_WIDEN);
  1942. /* For unsigned operands, try widening to an unsigned int. */
  1943. if (!temp && unsignedp)
  1944. temp = expand_binop (mode, uoptab, op0, op1, target,
  1945. unsignedp, OPTAB_WIDEN);
  1946. if (temp || methods == OPTAB_WIDEN)
  1947. goto egress;
  1948. /* Use the right width libcall if that exists. */
  1949. temp = expand_binop (mode, direct_optab, op0, op1, target,
  1950. unsignedp, OPTAB_LIB);
  1951. if (temp || methods == OPTAB_LIB)
  1952. goto egress;
  1953. /* Must widen and use a libcall, use either signed or unsigned. */
  1954. temp = expand_binop (mode, soptab, op0, op1, target,
  1955. unsignedp, methods);
  1956. if (!temp && unsignedp)
  1957. temp = expand_binop (mode, uoptab, op0, op1, target,
  1958. unsignedp, methods);
  1959. egress:
  1960. /* Undo the fiddling above. */
  1961. if (save_enable)
  1962. swap_optab_enable (soptab, mode, true);
  1963. return temp;
  1964. }
  1965. /* Generate code to perform an operation specified by UNOPPTAB
  1966. on operand OP0, with two results to TARG0 and TARG1.
  1967. We assume that the order of the operands for the instruction
  1968. is TARG0, TARG1, OP0.
  1969. Either TARG0 or TARG1 may be zero, but what that means is that
  1970. the result is not actually wanted. We will generate it into
  1971. a dummy pseudo-reg and discard it. They may not both be zero.
  1972. Returns 1 if this operation can be performed; 0 if not. */
  1973. int
  1974. expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
  1975. int unsignedp)
  1976. {
  1977. machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
  1978. enum mode_class mclass;
  1979. machine_mode wider_mode;
  1980. rtx_insn *entry_last = get_last_insn ();
  1981. rtx_insn *last;
  1982. mclass = GET_MODE_CLASS (mode);
  1983. if (!targ0)
  1984. targ0 = gen_reg_rtx (mode);
  1985. if (!targ1)
  1986. targ1 = gen_reg_rtx (mode);
  1987. /* Record where to go back to if we fail. */
  1988. last = get_last_insn ();
  1989. if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
  1990. {
  1991. struct expand_operand ops[3];
  1992. enum insn_code icode = optab_handler (unoptab, mode);
  1993. create_fixed_operand (&ops[0], targ0);
  1994. create_fixed_operand (&ops[1], targ1);
  1995. create_convert_operand_from (&ops[2], op0, mode, unsignedp);
  1996. if (maybe_expand_insn (icode, 3, ops))
  1997. return 1;
  1998. }
  1999. /* It can't be done in this mode. Can we do it in a wider mode? */
  2000. if (CLASS_HAS_WIDER_MODES_P (mclass))
  2001. {
  2002. for (wider_mode = GET_MODE_WIDER_MODE (mode);
  2003. wider_mode != VOIDmode;
  2004. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  2005. {
  2006. if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
  2007. {
  2008. rtx t0 = gen_reg_rtx (wider_mode);
  2009. rtx t1 = gen_reg_rtx (wider_mode);
  2010. rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
  2011. if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
  2012. {
  2013. convert_move (targ0, t0, unsignedp);
  2014. convert_move (targ1, t1, unsignedp);
  2015. return 1;
  2016. }
  2017. else
  2018. delete_insns_since (last);
  2019. }
  2020. }
  2021. }
  2022. delete_insns_since (entry_last);
  2023. return 0;
  2024. }
  2025. /* Generate code to perform an operation specified by BINOPTAB
  2026. on operands OP0 and OP1, with two results to TARG1 and TARG2.
  2027. We assume that the order of the operands for the instruction
  2028. is TARG0, OP0, OP1, TARG1, which would fit a pattern like
  2029. [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
  2030. Either TARG0 or TARG1 may be zero, but what that means is that
  2031. the result is not actually wanted. We will generate it into
  2032. a dummy pseudo-reg and discard it. They may not both be zero.
  2033. Returns 1 if this operation can be performed; 0 if not. */
  2034. int
  2035. expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
  2036. int unsignedp)
  2037. {
  2038. machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
  2039. enum mode_class mclass;
  2040. machine_mode wider_mode;
  2041. rtx_insn *entry_last = get_last_insn ();
  2042. rtx_insn *last;
  2043. mclass = GET_MODE_CLASS (mode);
  2044. if (!targ0)
  2045. targ0 = gen_reg_rtx (mode);
  2046. if (!targ1)
  2047. targ1 = gen_reg_rtx (mode);
  2048. /* Record where to go back to if we fail. */
  2049. last = get_last_insn ();
  2050. if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
  2051. {
  2052. struct expand_operand ops[4];
  2053. enum insn_code icode = optab_handler (binoptab, mode);
  2054. machine_mode mode0 = insn_data[icode].operand[1].mode;
  2055. machine_mode mode1 = insn_data[icode].operand[2].mode;
  2056. rtx xop0 = op0, xop1 = op1;
  2057. /* If we are optimizing, force expensive constants into a register. */
  2058. xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
  2059. xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
  2060. create_fixed_operand (&ops[0], targ0);
  2061. create_convert_operand_from (&ops[1], op0, mode, unsignedp);
  2062. create_convert_operand_from (&ops[2], op1, mode, unsignedp);
  2063. create_fixed_operand (&ops[3], targ1);
  2064. if (maybe_expand_insn (icode, 4, ops))
  2065. return 1;
  2066. delete_insns_since (last);
  2067. }
  2068. /* It can't be done in this mode. Can we do it in a wider mode? */
  2069. if (CLASS_HAS_WIDER_MODES_P (mclass))
  2070. {
  2071. for (wider_mode = GET_MODE_WIDER_MODE (mode);
  2072. wider_mode != VOIDmode;
  2073. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  2074. {
  2075. if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
  2076. {
  2077. rtx t0 = gen_reg_rtx (wider_mode);
  2078. rtx t1 = gen_reg_rtx (wider_mode);
  2079. rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
  2080. rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
  2081. if (expand_twoval_binop (binoptab, cop0, cop1,
  2082. t0, t1, unsignedp))
  2083. {
  2084. convert_move (targ0, t0, unsignedp);
  2085. convert_move (targ1, t1, unsignedp);
  2086. return 1;
  2087. }
  2088. else
  2089. delete_insns_since (last);
  2090. }
  2091. }
  2092. }
  2093. delete_insns_since (entry_last);
  2094. return 0;
  2095. }
  2096. /* Expand the two-valued library call indicated by BINOPTAB, but
  2097. preserve only one of the values. If TARG0 is non-NULL, the first
  2098. value is placed into TARG0; otherwise the second value is placed
  2099. into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
  2100. value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
  2101. This routine assumes that the value returned by the library call is
  2102. as if the return value was of an integral mode twice as wide as the
  2103. mode of OP0. Returns 1 if the call was successful. */
  2104. bool
  2105. expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
  2106. rtx targ0, rtx targ1, enum rtx_code code)
  2107. {
  2108. machine_mode mode;
  2109. machine_mode libval_mode;
  2110. rtx libval;
  2111. rtx_insn *insns;
  2112. rtx libfunc;
  2113. /* Exactly one of TARG0 or TARG1 should be non-NULL. */
  2114. gcc_assert (!targ0 != !targ1);
  2115. mode = GET_MODE (op0);
  2116. libfunc = optab_libfunc (binoptab, mode);
  2117. if (!libfunc)
  2118. return false;
  2119. /* The value returned by the library function will have twice as
  2120. many bits as the nominal MODE. */
  2121. libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
  2122. MODE_INT);
  2123. start_sequence ();
  2124. libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
  2125. libval_mode, 2,
  2126. op0, mode,
  2127. op1, mode);
  2128. /* Get the part of VAL containing the value that we want. */
  2129. libval = simplify_gen_subreg (mode, libval, libval_mode,
  2130. targ0 ? 0 : GET_MODE_SIZE (mode));
  2131. insns = get_insns ();
  2132. end_sequence ();
  2133. /* Move the into the desired location. */
  2134. emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
  2135. gen_rtx_fmt_ee (code, mode, op0, op1));
  2136. return true;
  2137. }
  2138. /* Wrapper around expand_unop which takes an rtx code to specify
  2139. the operation to perform, not an optab pointer. All other
  2140. arguments are the same. */
  2141. rtx
  2142. expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
  2143. rtx target, int unsignedp)
  2144. {
  2145. optab unop = code_to_optab (code);
  2146. gcc_assert (unop);
  2147. return expand_unop (mode, unop, op0, target, unsignedp);
  2148. }
  2149. /* Try calculating
  2150. (clz:narrow x)
  2151. as
  2152. (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
  2153. A similar operation can be used for clrsb. UNOPTAB says which operation
  2154. we are trying to expand. */
  2155. static rtx
  2156. widen_leading (machine_mode mode, rtx op0, rtx target, optab unoptab)
  2157. {
  2158. enum mode_class mclass = GET_MODE_CLASS (mode);
  2159. if (CLASS_HAS_WIDER_MODES_P (mclass))
  2160. {
  2161. machine_mode wider_mode;
  2162. for (wider_mode = GET_MODE_WIDER_MODE (mode);
  2163. wider_mode != VOIDmode;
  2164. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  2165. {
  2166. if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
  2167. {
  2168. rtx xop0, temp;
  2169. rtx_insn *last;
  2170. last = get_last_insn ();
  2171. if (target == 0)
  2172. target = gen_reg_rtx (mode);
  2173. xop0 = widen_operand (op0, wider_mode, mode,
  2174. unoptab != clrsb_optab, false);
  2175. temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
  2176. unoptab != clrsb_optab);
  2177. if (temp != 0)
  2178. temp = expand_binop
  2179. (wider_mode, sub_optab, temp,
  2180. gen_int_mode (GET_MODE_PRECISION (wider_mode)
  2181. - GET_MODE_PRECISION (mode),
  2182. wider_mode),
  2183. target, true, OPTAB_DIRECT);
  2184. if (temp == 0)
  2185. delete_insns_since (last);
  2186. return temp;
  2187. }
  2188. }
  2189. }
  2190. return 0;
  2191. }
  2192. /* Try calculating clz of a double-word quantity as two clz's of word-sized
  2193. quantities, choosing which based on whether the high word is nonzero. */
  2194. static rtx
  2195. expand_doubleword_clz (machine_mode mode, rtx op0, rtx target)
  2196. {
  2197. rtx xop0 = force_reg (mode, op0);
  2198. rtx subhi = gen_highpart (word_mode, xop0);
  2199. rtx sublo = gen_lowpart (word_mode, xop0);
  2200. rtx_code_label *hi0_label = gen_label_rtx ();
  2201. rtx_code_label *after_label = gen_label_rtx ();
  2202. rtx_insn *seq;
  2203. rtx temp, result;
  2204. /* If we were not given a target, use a word_mode register, not a
  2205. 'mode' register. The result will fit, and nobody is expecting
  2206. anything bigger (the return type of __builtin_clz* is int). */
  2207. if (!target)
  2208. target = gen_reg_rtx (word_mode);
  2209. /* In any case, write to a word_mode scratch in both branches of the
  2210. conditional, so we can ensure there is a single move insn setting
  2211. 'target' to tag a REG_EQUAL note on. */
  2212. result = gen_reg_rtx (word_mode);
  2213. start_sequence ();
  2214. /* If the high word is not equal to zero,
  2215. then clz of the full value is clz of the high word. */
  2216. emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
  2217. word_mode, true, hi0_label);
  2218. temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
  2219. if (!temp)
  2220. goto fail;
  2221. if (temp != result)
  2222. convert_move (result, temp, true);
  2223. emit_jump_insn (gen_jump (after_label));
  2224. emit_barrier ();
  2225. /* Else clz of the full value is clz of the low word plus the number
  2226. of bits in the high word. */
  2227. emit_label (hi0_label);
  2228. temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
  2229. if (!temp)
  2230. goto fail;
  2231. temp = expand_binop (word_mode, add_optab, temp,
  2232. gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
  2233. result, true, OPTAB_DIRECT);
  2234. if (!temp)
  2235. goto fail;
  2236. if (temp != result)
  2237. convert_move (result, temp, true);
  2238. emit_label (after_label);
  2239. convert_move (target, result, true);
  2240. seq = get_insns ();
  2241. end_sequence ();
  2242. add_equal_note (seq, target, CLZ, xop0, 0);
  2243. emit_insn (seq);
  2244. return target;
  2245. fail:
  2246. end_sequence ();
  2247. return 0;
  2248. }
  2249. /* Try calculating
  2250. (bswap:narrow x)
  2251. as
  2252. (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
  2253. static rtx
  2254. widen_bswap (machine_mode mode, rtx op0, rtx target)
  2255. {
  2256. enum mode_class mclass = GET_MODE_CLASS (mode);
  2257. machine_mode wider_mode;
  2258. rtx x;
  2259. rtx_insn *last;
  2260. if (!CLASS_HAS_WIDER_MODES_P (mclass))
  2261. return NULL_RTX;
  2262. for (wider_mode = GET_MODE_WIDER_MODE (mode);
  2263. wider_mode != VOIDmode;
  2264. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  2265. if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
  2266. goto found;
  2267. return NULL_RTX;
  2268. found:
  2269. last = get_last_insn ();
  2270. x = widen_operand (op0, wider_mode, mode, true, true);
  2271. x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
  2272. gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
  2273. && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
  2274. if (x != 0)
  2275. x = expand_shift (RSHIFT_EXPR, wider_mode, x,
  2276. GET_MODE_BITSIZE (wider_mode)
  2277. - GET_MODE_BITSIZE (mode),
  2278. NULL_RTX, true);
  2279. if (x != 0)
  2280. {
  2281. if (target == 0)
  2282. target = gen_reg_rtx (mode);
  2283. emit_move_insn (target, gen_lowpart (mode, x));
  2284. }
  2285. else
  2286. delete_insns_since (last);
  2287. return target;
  2288. }
  2289. /* Try calculating bswap as two bswaps of two word-sized operands. */
  2290. static rtx
  2291. expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
  2292. {
  2293. rtx t0, t1;
  2294. t1 = expand_unop (word_mode, bswap_optab,
  2295. operand_subword_force (op, 0, mode), NULL_RTX, true);
  2296. t0 = expand_unop (word_mode, bswap_optab,
  2297. operand_subword_force (op, 1, mode), NULL_RTX, true);
  2298. if (target == 0 || !valid_multiword_target_p (target))
  2299. target = gen_reg_rtx (mode);
  2300. if (REG_P (target))
  2301. emit_clobber (target);
  2302. emit_move_insn (operand_subword (target, 0, 1, mode), t0);
  2303. emit_move_insn (operand_subword (target, 1, 1, mode), t1);
  2304. return target;
  2305. }
  2306. /* Try calculating (parity x) as (and (popcount x) 1), where
  2307. popcount can also be done in a wider mode. */
  2308. static rtx
  2309. expand_parity (machine_mode mode, rtx op0, rtx target)
  2310. {
  2311. enum mode_class mclass = GET_MODE_CLASS (mode);
  2312. if (CLASS_HAS_WIDER_MODES_P (mclass))
  2313. {
  2314. machine_mode wider_mode;
  2315. for (wider_mode = mode; wider_mode != VOIDmode;
  2316. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  2317. {
  2318. if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
  2319. {
  2320. rtx xop0, temp;
  2321. rtx_insn *last;
  2322. last = get_last_insn ();
  2323. if (target == 0)
  2324. target = gen_reg_rtx (mode);
  2325. xop0 = widen_operand (op0, wider_mode, mode, true, false);
  2326. temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
  2327. true);
  2328. if (temp != 0)
  2329. temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
  2330. target, true, OPTAB_DIRECT);
  2331. if (temp == 0)
  2332. delete_insns_since (last);
  2333. return temp;
  2334. }
  2335. }
  2336. }
  2337. return 0;
  2338. }
  2339. /* Try calculating ctz(x) as K - clz(x & -x) ,
  2340. where K is GET_MODE_PRECISION(mode) - 1.
  2341. Both __builtin_ctz and __builtin_clz are undefined at zero, so we
  2342. don't have to worry about what the hardware does in that case. (If
  2343. the clz instruction produces the usual value at 0, which is K, the
  2344. result of this code sequence will be -1; expand_ffs, below, relies
  2345. on this. It might be nice to have it be K instead, for consistency
  2346. with the (very few) processors that provide a ctz with a defined
  2347. value, but that would take one more instruction, and it would be
  2348. less convenient for expand_ffs anyway. */
  2349. static rtx
  2350. expand_ctz (machine_mode mode, rtx op0, rtx target)
  2351. {
  2352. rtx_insn *seq;
  2353. rtx temp;
  2354. if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
  2355. return 0;
  2356. start_sequence ();
  2357. temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
  2358. if (temp)
  2359. temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
  2360. true, OPTAB_DIRECT);
  2361. if (temp)
  2362. temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
  2363. if (temp)
  2364. temp = expand_binop (mode, sub_optab,
  2365. gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
  2366. temp, target,
  2367. true, OPTAB_DIRECT);
  2368. if (temp == 0)
  2369. {
  2370. end_sequence ();
  2371. return 0;
  2372. }
  2373. seq = get_insns ();
  2374. end_sequence ();
  2375. add_equal_note (seq, temp, CTZ, op0, 0);
  2376. emit_insn (seq);
  2377. return temp;
  2378. }
  2379. /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
  2380. else with the sequence used by expand_clz.
  2381. The ffs builtin promises to return zero for a zero value and ctz/clz
  2382. may have an undefined value in that case. If they do not give us a
  2383. convenient value, we have to generate a test and branch. */
  2384. static rtx
  2385. expand_ffs (machine_mode mode, rtx op0, rtx target)
  2386. {
  2387. HOST_WIDE_INT val = 0;
  2388. bool defined_at_zero = false;
  2389. rtx temp;
  2390. rtx_insn *seq;
  2391. if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
  2392. {
  2393. start_sequence ();
  2394. temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
  2395. if (!temp)
  2396. goto fail;
  2397. defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
  2398. }
  2399. else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
  2400. {
  2401. start_sequence ();
  2402. temp = expand_ctz (mode, op0, 0);
  2403. if (!temp)
  2404. goto fail;
  2405. if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
  2406. {
  2407. defined_at_zero = true;
  2408. val = (GET_MODE_PRECISION (mode) - 1) - val;
  2409. }
  2410. }
  2411. else
  2412. return 0;
  2413. if (defined_at_zero && val == -1)
  2414. /* No correction needed at zero. */;
  2415. else
  2416. {
  2417. /* We don't try to do anything clever with the situation found
  2418. on some processors (eg Alpha) where ctz(0:mode) ==
  2419. bitsize(mode). If someone can think of a way to send N to -1
  2420. and leave alone all values in the range 0..N-1 (where N is a
  2421. power of two), cheaper than this test-and-branch, please add it.
  2422. The test-and-branch is done after the operation itself, in case
  2423. the operation sets condition codes that can be recycled for this.
  2424. (This is true on i386, for instance.) */
  2425. rtx_code_label *nonzero_label = gen_label_rtx ();
  2426. emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
  2427. mode, true, nonzero_label);
  2428. convert_move (temp, GEN_INT (-1), false);
  2429. emit_label (nonzero_label);
  2430. }
  2431. /* temp now has a value in the range -1..bitsize-1. ffs is supposed
  2432. to produce a value in the range 0..bitsize. */
  2433. temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
  2434. target, false, OPTAB_DIRECT);
  2435. if (!temp)
  2436. goto fail;
  2437. seq = get_insns ();
  2438. end_sequence ();
  2439. add_equal_note (seq, temp, FFS, op0, 0);
  2440. emit_insn (seq);
  2441. return temp;
  2442. fail:
  2443. end_sequence ();
  2444. return 0;
  2445. }
  2446. /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
  2447. conditions, VAL may already be a SUBREG against which we cannot generate
  2448. a further SUBREG. In this case, we expect forcing the value into a
  2449. register will work around the situation. */
  2450. static rtx
  2451. lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
  2452. machine_mode imode)
  2453. {
  2454. rtx ret;
  2455. ret = lowpart_subreg (omode, val, imode);
  2456. if (ret == NULL)
  2457. {
  2458. val = force_reg (imode, val);
  2459. ret = lowpart_subreg (omode, val, imode);
  2460. gcc_assert (ret != NULL);
  2461. }
  2462. return ret;
  2463. }
  2464. /* Expand a floating point absolute value or negation operation via a
  2465. logical operation on the sign bit. */
  2466. static rtx
  2467. expand_absneg_bit (enum rtx_code code, machine_mode mode,
  2468. rtx op0, rtx target)
  2469. {
  2470. const struct real_format *fmt;
  2471. int bitpos, word, nwords, i;
  2472. machine_mode imode;
  2473. rtx temp;
  2474. rtx_insn *insns;
  2475. /* The format has to have a simple sign bit. */
  2476. fmt = REAL_MODE_FORMAT (mode);
  2477. if (fmt == NULL)
  2478. return NULL_RTX;
  2479. bitpos = fmt->signbit_rw;
  2480. if (bitpos < 0)
  2481. return NULL_RTX;
  2482. /* Don't create negative zeros if the format doesn't support them. */
  2483. if (code == NEG && !fmt->has_signed_zero)
  2484. return NULL_RTX;
  2485. if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
  2486. {
  2487. imode = int_mode_for_mode (mode);
  2488. if (imode == BLKmode)
  2489. return NULL_RTX;
  2490. word = 0;
  2491. nwords = 1;
  2492. }
  2493. else
  2494. {
  2495. imode = word_mode;
  2496. if (FLOAT_WORDS_BIG_ENDIAN)
  2497. word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
  2498. else
  2499. word = bitpos / BITS_PER_WORD;
  2500. bitpos = bitpos % BITS_PER_WORD;
  2501. nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
  2502. }
  2503. wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
  2504. if (code == ABS)
  2505. mask = ~mask;
  2506. if (target == 0
  2507. || target == op0
  2508. || (nwords > 1 && !valid_multiword_target_p (target)))
  2509. target = gen_reg_rtx (mode);
  2510. if (nwords > 1)
  2511. {
  2512. start_sequence ();
  2513. for (i = 0; i < nwords; ++i)
  2514. {
  2515. rtx targ_piece = operand_subword (target, i, 1, mode);
  2516. rtx op0_piece = operand_subword_force (op0, i, mode);
  2517. if (i == word)
  2518. {
  2519. temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
  2520. op0_piece,
  2521. immed_wide_int_const (mask, imode),
  2522. targ_piece, 1, OPTAB_LIB_WIDEN);
  2523. if (temp != targ_piece)
  2524. emit_move_insn (targ_piece, temp);
  2525. }
  2526. else
  2527. emit_move_insn (targ_piece, op0_piece);
  2528. }
  2529. insns = get_insns ();
  2530. end_sequence ();
  2531. emit_insn (insns);
  2532. }
  2533. else
  2534. {
  2535. temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
  2536. gen_lowpart (imode, op0),
  2537. immed_wide_int_const (mask, imode),
  2538. gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
  2539. target = lowpart_subreg_maybe_copy (mode, temp, imode);
  2540. set_dst_reg_note (get_last_insn (), REG_EQUAL,
  2541. gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
  2542. target);
  2543. }
  2544. return target;
  2545. }
  2546. /* As expand_unop, but will fail rather than attempt the operation in a
  2547. different mode or with a libcall. */
  2548. static rtx
  2549. expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
  2550. int unsignedp)
  2551. {
  2552. if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
  2553. {
  2554. struct expand_operand ops[2];
  2555. enum insn_code icode = optab_handler (unoptab, mode);
  2556. rtx_insn *last = get_last_insn ();
  2557. rtx pat;
  2558. create_output_operand (&ops[0], target, mode);
  2559. create_convert_operand_from (&ops[1], op0, mode, unsignedp);
  2560. pat = maybe_gen_insn (icode, 2, ops);
  2561. if (pat)
  2562. {
  2563. if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
  2564. && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
  2565. optab_to_code (unoptab),
  2566. ops[1].value, NULL_RTX))
  2567. {
  2568. delete_insns_since (last);
  2569. return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
  2570. }
  2571. emit_insn (pat);
  2572. return ops[0].value;
  2573. }
  2574. }
  2575. return 0;
  2576. }
  2577. /* Generate code to perform an operation specified by UNOPTAB
  2578. on operand OP0, with result having machine-mode MODE.
  2579. UNSIGNEDP is for the case where we have to widen the operands
  2580. to perform the operation. It says to use zero-extension.
  2581. If TARGET is nonzero, the value
  2582. is generated there, if it is convenient to do so.
  2583. In all cases an rtx is returned for the locus of the value;
  2584. this may or may not be TARGET. */
  2585. rtx
  2586. expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
  2587. int unsignedp)
  2588. {
  2589. enum mode_class mclass = GET_MODE_CLASS (mode);
  2590. machine_mode wider_mode;
  2591. rtx temp;
  2592. rtx libfunc;
  2593. temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
  2594. if (temp)
  2595. return temp;
  2596. /* It can't be done in this mode. Can we open-code it in a wider mode? */
  2597. /* Widening (or narrowing) clz needs special treatment. */
  2598. if (unoptab == clz_optab)
  2599. {
  2600. temp = widen_leading (mode, op0, target, unoptab);
  2601. if (temp)
  2602. return temp;
  2603. if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
  2604. && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
  2605. {
  2606. temp = expand_doubleword_clz (mode, op0, target);
  2607. if (temp)
  2608. return temp;
  2609. }
  2610. goto try_libcall;
  2611. }
  2612. if (unoptab == clrsb_optab)
  2613. {
  2614. temp = widen_leading (mode, op0, target, unoptab);
  2615. if (temp)
  2616. return temp;
  2617. goto try_libcall;
  2618. }
  2619. /* Widening (or narrowing) bswap needs special treatment. */
  2620. if (unoptab == bswap_optab)
  2621. {
  2622. /* HImode is special because in this mode BSWAP is equivalent to ROTATE
  2623. or ROTATERT. First try these directly; if this fails, then try the
  2624. obvious pair of shifts with allowed widening, as this will probably
  2625. be always more efficient than the other fallback methods. */
  2626. if (mode == HImode)
  2627. {
  2628. rtx_insn *last;
  2629. rtx temp1, temp2;
  2630. if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
  2631. {
  2632. temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
  2633. unsignedp, OPTAB_DIRECT);
  2634. if (temp)
  2635. return temp;
  2636. }
  2637. if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
  2638. {
  2639. temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
  2640. unsignedp, OPTAB_DIRECT);
  2641. if (temp)
  2642. return temp;
  2643. }
  2644. last = get_last_insn ();
  2645. temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
  2646. unsignedp, OPTAB_WIDEN);
  2647. temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
  2648. unsignedp, OPTAB_WIDEN);
  2649. if (temp1 && temp2)
  2650. {
  2651. temp = expand_binop (mode, ior_optab, temp1, temp2, target,
  2652. unsignedp, OPTAB_WIDEN);
  2653. if (temp)
  2654. return temp;
  2655. }
  2656. delete_insns_since (last);
  2657. }
  2658. temp = widen_bswap (mode, op0, target);
  2659. if (temp)
  2660. return temp;
  2661. if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
  2662. && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
  2663. {
  2664. temp = expand_doubleword_bswap (mode, op0, target);
  2665. if (temp)
  2666. return temp;
  2667. }
  2668. goto try_libcall;
  2669. }
  2670. if (CLASS_HAS_WIDER_MODES_P (mclass))
  2671. for (wider_mode = GET_MODE_WIDER_MODE (mode);
  2672. wider_mode != VOIDmode;
  2673. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  2674. {
  2675. if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
  2676. {
  2677. rtx xop0 = op0;
  2678. rtx_insn *last = get_last_insn ();
  2679. /* For certain operations, we need not actually extend
  2680. the narrow operand, as long as we will truncate the
  2681. results to the same narrowness. */
  2682. xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
  2683. (unoptab == neg_optab
  2684. || unoptab == one_cmpl_optab)
  2685. && mclass == MODE_INT);
  2686. temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
  2687. unsignedp);
  2688. if (temp)
  2689. {
  2690. if (mclass != MODE_INT
  2691. || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
  2692. {
  2693. if (target == 0)
  2694. target = gen_reg_rtx (mode);
  2695. convert_move (target, temp, 0);
  2696. return target;
  2697. }
  2698. else
  2699. return gen_lowpart (mode, temp);
  2700. }
  2701. else
  2702. delete_insns_since (last);
  2703. }
  2704. }
  2705. /* These can be done a word at a time. */
  2706. if (unoptab == one_cmpl_optab
  2707. && mclass == MODE_INT
  2708. && GET_MODE_SIZE (mode) > UNITS_PER_WORD
  2709. && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
  2710. {
  2711. int i;
  2712. rtx_insn *insns;
  2713. if (target == 0 || target == op0 || !valid_multiword_target_p (target))
  2714. target = gen_reg_rtx (mode);
  2715. start_sequence ();
  2716. /* Do the actual arithmetic. */
  2717. for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
  2718. {
  2719. rtx target_piece = operand_subword (target, i, 1, mode);
  2720. rtx x = expand_unop (word_mode, unoptab,
  2721. operand_subword_force (op0, i, mode),
  2722. target_piece, unsignedp);
  2723. if (target_piece != x)
  2724. emit_move_insn (target_piece, x);
  2725. }
  2726. insns = get_insns ();
  2727. end_sequence ();
  2728. emit_insn (insns);
  2729. return target;
  2730. }
  2731. if (optab_to_code (unoptab) == NEG)
  2732. {
  2733. /* Try negating floating point values by flipping the sign bit. */
  2734. if (SCALAR_FLOAT_MODE_P (mode))
  2735. {
  2736. temp = expand_absneg_bit (NEG, mode, op0, target);
  2737. if (temp)
  2738. return temp;
  2739. }
  2740. /* If there is no negation pattern, and we have no negative zero,
  2741. try subtracting from zero. */
  2742. if (!HONOR_SIGNED_ZEROS (mode))
  2743. {
  2744. temp = expand_binop (mode, (unoptab == negv_optab
  2745. ? subv_optab : sub_optab),
  2746. CONST0_RTX (mode), op0, target,
  2747. unsignedp, OPTAB_DIRECT);
  2748. if (temp)
  2749. return temp;
  2750. }
  2751. }
  2752. /* Try calculating parity (x) as popcount (x) % 2. */
  2753. if (unoptab == parity_optab)
  2754. {
  2755. temp = expand_parity (mode, op0, target);
  2756. if (temp)
  2757. return temp;
  2758. }
  2759. /* Try implementing ffs (x) in terms of clz (x). */
  2760. if (unoptab == ffs_optab)
  2761. {
  2762. temp = expand_ffs (mode, op0, target);
  2763. if (temp)
  2764. return temp;
  2765. }
  2766. /* Try implementing ctz (x) in terms of clz (x). */
  2767. if (unoptab == ctz_optab)
  2768. {
  2769. temp = expand_ctz (mode, op0, target);
  2770. if (temp)
  2771. return temp;
  2772. }
  2773. try_libcall:
  2774. /* Now try a library call in this mode. */
  2775. libfunc = optab_libfunc (unoptab, mode);
  2776. if (libfunc)
  2777. {
  2778. rtx_insn *insns;
  2779. rtx value;
  2780. rtx eq_value;
  2781. machine_mode outmode = mode;
  2782. /* All of these functions return small values. Thus we choose to
  2783. have them return something that isn't a double-word. */
  2784. if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
  2785. || unoptab == clrsb_optab || unoptab == popcount_optab
  2786. || unoptab == parity_optab)
  2787. outmode
  2788. = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
  2789. optab_libfunc (unoptab, mode)));
  2790. start_sequence ();
  2791. /* Pass 1 for NO_QUEUE so we don't lose any increments
  2792. if the libcall is cse'd or moved. */
  2793. value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
  2794. 1, op0, mode);
  2795. insns = get_insns ();
  2796. end_sequence ();
  2797. target = gen_reg_rtx (outmode);
  2798. eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
  2799. if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
  2800. eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
  2801. else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
  2802. eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
  2803. emit_libcall_block_1 (insns, target, value, eq_value,
  2804. trapv_unoptab_p (unoptab));
  2805. return target;
  2806. }
  2807. /* It can't be done in this mode. Can we do it in a wider mode? */
  2808. if (CLASS_HAS_WIDER_MODES_P (mclass))
  2809. {
  2810. for (wider_mode = GET_MODE_WIDER_MODE (mode);
  2811. wider_mode != VOIDmode;
  2812. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  2813. {
  2814. if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
  2815. || optab_libfunc (unoptab, wider_mode))
  2816. {
  2817. rtx xop0 = op0;
  2818. rtx_insn *last = get_last_insn ();
  2819. /* For certain operations, we need not actually extend
  2820. the narrow operand, as long as we will truncate the
  2821. results to the same narrowness. */
  2822. xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
  2823. (unoptab == neg_optab
  2824. || unoptab == one_cmpl_optab
  2825. || unoptab == bswap_optab)
  2826. && mclass == MODE_INT);
  2827. temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
  2828. unsignedp);
  2829. /* If we are generating clz using wider mode, adjust the
  2830. result. Similarly for clrsb. */
  2831. if ((unoptab == clz_optab || unoptab == clrsb_optab)
  2832. && temp != 0)
  2833. temp = expand_binop
  2834. (wider_mode, sub_optab, temp,
  2835. gen_int_mode (GET_MODE_PRECISION (wider_mode)
  2836. - GET_MODE_PRECISION (mode),
  2837. wider_mode),
  2838. target, true, OPTAB_DIRECT);
  2839. /* Likewise for bswap. */
  2840. if (unoptab == bswap_optab && temp != 0)
  2841. {
  2842. gcc_assert (GET_MODE_PRECISION (wider_mode)
  2843. == GET_MODE_BITSIZE (wider_mode)
  2844. && GET_MODE_PRECISION (mode)
  2845. == GET_MODE_BITSIZE (mode));
  2846. temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
  2847. GET_MODE_BITSIZE (wider_mode)
  2848. - GET_MODE_BITSIZE (mode),
  2849. NULL_RTX, true);
  2850. }
  2851. if (temp)
  2852. {
  2853. if (mclass != MODE_INT)
  2854. {
  2855. if (target == 0)
  2856. target = gen_reg_rtx (mode);
  2857. convert_move (target, temp, 0);
  2858. return target;
  2859. }
  2860. else
  2861. return gen_lowpart (mode, temp);
  2862. }
  2863. else
  2864. delete_insns_since (last);
  2865. }
  2866. }
  2867. }
  2868. /* One final attempt at implementing negation via subtraction,
  2869. this time allowing widening of the operand. */
  2870. if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
  2871. {
  2872. rtx temp;
  2873. temp = expand_binop (mode,
  2874. unoptab == negv_optab ? subv_optab : sub_optab,
  2875. CONST0_RTX (mode), op0,
  2876. target, unsignedp, OPTAB_LIB_WIDEN);
  2877. if (temp)
  2878. return temp;
  2879. }
  2880. return 0;
  2881. }
  2882. /* Emit code to compute the absolute value of OP0, with result to
  2883. TARGET if convenient. (TARGET may be 0.) The return value says
  2884. where the result actually is to be found.
  2885. MODE is the mode of the operand; the mode of the result is
  2886. different but can be deduced from MODE.
  2887. */
  2888. rtx
  2889. expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
  2890. int result_unsignedp)
  2891. {
  2892. rtx temp;
  2893. if (GET_MODE_CLASS (mode) != MODE_INT
  2894. || ! flag_trapv)
  2895. result_unsignedp = 1;
  2896. /* First try to do it with a special abs instruction. */
  2897. temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
  2898. op0, target, 0);
  2899. if (temp != 0)
  2900. return temp;
  2901. /* For floating point modes, try clearing the sign bit. */
  2902. if (SCALAR_FLOAT_MODE_P (mode))
  2903. {
  2904. temp = expand_absneg_bit (ABS, mode, op0, target);
  2905. if (temp)
  2906. return temp;
  2907. }
  2908. /* If we have a MAX insn, we can do this as MAX (x, -x). */
  2909. if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
  2910. && !HONOR_SIGNED_ZEROS (mode))
  2911. {
  2912. rtx_insn *last = get_last_insn ();
  2913. temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
  2914. op0, NULL_RTX, 0);
  2915. if (temp != 0)
  2916. temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
  2917. OPTAB_WIDEN);
  2918. if (temp != 0)
  2919. return temp;
  2920. delete_insns_since (last);
  2921. }
  2922. /* If this machine has expensive jumps, we can do integer absolute
  2923. value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
  2924. where W is the width of MODE. */
  2925. if (GET_MODE_CLASS (mode) == MODE_INT
  2926. && BRANCH_COST (optimize_insn_for_speed_p (),
  2927. false) >= 2)
  2928. {
  2929. rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
  2930. GET_MODE_PRECISION (mode) - 1,
  2931. NULL_RTX, 0);
  2932. temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
  2933. OPTAB_LIB_WIDEN);
  2934. if (temp != 0)
  2935. temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
  2936. temp, extended, target, 0, OPTAB_LIB_WIDEN);
  2937. if (temp != 0)
  2938. return temp;
  2939. }
  2940. return NULL_RTX;
  2941. }
  2942. rtx
  2943. expand_abs (machine_mode mode, rtx op0, rtx target,
  2944. int result_unsignedp, int safe)
  2945. {
  2946. rtx temp;
  2947. rtx_code_label *op1;
  2948. if (GET_MODE_CLASS (mode) != MODE_INT
  2949. || ! flag_trapv)
  2950. result_unsignedp = 1;
  2951. temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
  2952. if (temp != 0)
  2953. return temp;
  2954. /* If that does not win, use conditional jump and negate. */
  2955. /* It is safe to use the target if it is the same
  2956. as the source if this is also a pseudo register */
  2957. if (op0 == target && REG_P (op0)
  2958. && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
  2959. safe = 1;
  2960. op1 = gen_label_rtx ();
  2961. if (target == 0 || ! safe
  2962. || GET_MODE (target) != mode
  2963. || (MEM_P (target) && MEM_VOLATILE_P (target))
  2964. || (REG_P (target)
  2965. && REGNO (target) < FIRST_PSEUDO_REGISTER))
  2966. target = gen_reg_rtx (mode);
  2967. emit_move_insn (target, op0);
  2968. NO_DEFER_POP;
  2969. do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
  2970. NULL_RTX, NULL_RTX, op1, -1);
  2971. op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
  2972. target, target, 0);
  2973. if (op0 != target)
  2974. emit_move_insn (target, op0);
  2975. emit_label (op1);
  2976. OK_DEFER_POP;
  2977. return target;
  2978. }
  2979. /* Emit code to compute the one's complement absolute value of OP0
  2980. (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
  2981. (TARGET may be NULL_RTX.) The return value says where the result
  2982. actually is to be found.
  2983. MODE is the mode of the operand; the mode of the result is
  2984. different but can be deduced from MODE. */
  2985. rtx
  2986. expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
  2987. {
  2988. rtx temp;
  2989. /* Not applicable for floating point modes. */
  2990. if (FLOAT_MODE_P (mode))
  2991. return NULL_RTX;
  2992. /* If we have a MAX insn, we can do this as MAX (x, ~x). */
  2993. if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
  2994. {
  2995. rtx_insn *last = get_last_insn ();
  2996. temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
  2997. if (temp != 0)
  2998. temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
  2999. OPTAB_WIDEN);
  3000. if (temp != 0)
  3001. return temp;
  3002. delete_insns_since (last);
  3003. }
  3004. /* If this machine has expensive jumps, we can do one's complement
  3005. absolute value of X as (((signed) x >> (W-1)) ^ x). */
  3006. if (GET_MODE_CLASS (mode) == MODE_INT
  3007. && BRANCH_COST (optimize_insn_for_speed_p (),
  3008. false) >= 2)
  3009. {
  3010. rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
  3011. GET_MODE_PRECISION (mode) - 1,
  3012. NULL_RTX, 0);
  3013. temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
  3014. OPTAB_LIB_WIDEN);
  3015. if (temp != 0)
  3016. return temp;
  3017. }
  3018. return NULL_RTX;
  3019. }
  3020. /* A subroutine of expand_copysign, perform the copysign operation using the
  3021. abs and neg primitives advertised to exist on the target. The assumption
  3022. is that we have a split register file, and leaving op0 in fp registers,
  3023. and not playing with subregs so much, will help the register allocator. */
  3024. static rtx
  3025. expand_copysign_absneg (machine_mode mode, rtx op0, rtx op1, rtx target,
  3026. int bitpos, bool op0_is_abs)
  3027. {
  3028. machine_mode imode;
  3029. enum insn_code icode;
  3030. rtx sign;
  3031. rtx_code_label *label;
  3032. if (target == op1)
  3033. target = NULL_RTX;
  3034. /* Check if the back end provides an insn that handles signbit for the
  3035. argument's mode. */
  3036. icode = optab_handler (signbit_optab, mode);
  3037. if (icode != CODE_FOR_nothing)
  3038. {
  3039. imode = insn_data[(int) icode].operand[0].mode;
  3040. sign = gen_reg_rtx (imode);
  3041. emit_unop_insn (icode, sign, op1, UNKNOWN);
  3042. }
  3043. else
  3044. {
  3045. if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
  3046. {
  3047. imode = int_mode_for_mode (mode);
  3048. if (imode == BLKmode)
  3049. return NULL_RTX;
  3050. op1 = gen_lowpart (imode, op1);
  3051. }
  3052. else
  3053. {
  3054. int word;
  3055. imode = word_mode;
  3056. if (FLOAT_WORDS_BIG_ENDIAN)
  3057. word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
  3058. else
  3059. word = bitpos / BITS_PER_WORD;
  3060. bitpos = bitpos % BITS_PER_WORD;
  3061. op1 = operand_subword_force (op1, word, mode);
  3062. }
  3063. wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
  3064. sign = expand_binop (imode, and_optab, op1,
  3065. immed_wide_int_const (mask, imode),
  3066. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3067. }
  3068. if (!op0_is_abs)
  3069. {
  3070. op0 = expand_unop (mode, abs_optab, op0, target, 0);
  3071. if (op0 == NULL)
  3072. return NULL_RTX;
  3073. target = op0;
  3074. }
  3075. else
  3076. {
  3077. if (target == NULL_RTX)
  3078. target = copy_to_reg (op0);
  3079. else
  3080. emit_move_insn (target, op0);
  3081. }
  3082. label = gen_label_rtx ();
  3083. emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
  3084. if (CONST_DOUBLE_AS_FLOAT_P (op0))
  3085. op0 = simplify_unary_operation (NEG, mode, op0, mode);
  3086. else
  3087. op0 = expand_unop (mode, neg_optab, op0, target, 0);
  3088. if (op0 != target)
  3089. emit_move_insn (target, op0);
  3090. emit_label (label);
  3091. return target;
  3092. }
  3093. /* A subroutine of expand_copysign, perform the entire copysign operation
  3094. with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
  3095. is true if op0 is known to have its sign bit clear. */
  3096. static rtx
  3097. expand_copysign_bit (machine_mode mode, rtx op0, rtx op1, rtx target,
  3098. int bitpos, bool op0_is_abs)
  3099. {
  3100. machine_mode imode;
  3101. int word, nwords, i;
  3102. rtx temp;
  3103. rtx_insn *insns;
  3104. if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
  3105. {
  3106. imode = int_mode_for_mode (mode);
  3107. if (imode == BLKmode)
  3108. return NULL_RTX;
  3109. word = 0;
  3110. nwords = 1;
  3111. }
  3112. else
  3113. {
  3114. imode = word_mode;
  3115. if (FLOAT_WORDS_BIG_ENDIAN)
  3116. word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
  3117. else
  3118. word = bitpos / BITS_PER_WORD;
  3119. bitpos = bitpos % BITS_PER_WORD;
  3120. nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
  3121. }
  3122. wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
  3123. if (target == 0
  3124. || target == op0
  3125. || target == op1
  3126. || (nwords > 1 && !valid_multiword_target_p (target)))
  3127. target = gen_reg_rtx (mode);
  3128. if (nwords > 1)
  3129. {
  3130. start_sequence ();
  3131. for (i = 0; i < nwords; ++i)
  3132. {
  3133. rtx targ_piece = operand_subword (target, i, 1, mode);
  3134. rtx op0_piece = operand_subword_force (op0, i, mode);
  3135. if (i == word)
  3136. {
  3137. if (!op0_is_abs)
  3138. op0_piece
  3139. = expand_binop (imode, and_optab, op0_piece,
  3140. immed_wide_int_const (~mask, imode),
  3141. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3142. op1 = expand_binop (imode, and_optab,
  3143. operand_subword_force (op1, i, mode),
  3144. immed_wide_int_const (mask, imode),
  3145. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3146. temp = expand_binop (imode, ior_optab, op0_piece, op1,
  3147. targ_piece, 1, OPTAB_LIB_WIDEN);
  3148. if (temp != targ_piece)
  3149. emit_move_insn (targ_piece, temp);
  3150. }
  3151. else
  3152. emit_move_insn (targ_piece, op0_piece);
  3153. }
  3154. insns = get_insns ();
  3155. end_sequence ();
  3156. emit_insn (insns);
  3157. }
  3158. else
  3159. {
  3160. op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
  3161. immed_wide_int_const (mask, imode),
  3162. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3163. op0 = gen_lowpart (imode, op0);
  3164. if (!op0_is_abs)
  3165. op0 = expand_binop (imode, and_optab, op0,
  3166. immed_wide_int_const (~mask, imode),
  3167. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3168. temp = expand_binop (imode, ior_optab, op0, op1,
  3169. gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
  3170. target = lowpart_subreg_maybe_copy (mode, temp, imode);
  3171. }
  3172. return target;
  3173. }
  3174. /* Expand the C99 copysign operation. OP0 and OP1 must be the same
  3175. scalar floating point mode. Return NULL if we do not know how to
  3176. expand the operation inline. */
  3177. rtx
  3178. expand_copysign (rtx op0, rtx op1, rtx target)
  3179. {
  3180. machine_mode mode = GET_MODE (op0);
  3181. const struct real_format *fmt;
  3182. bool op0_is_abs;
  3183. rtx temp;
  3184. gcc_assert (SCALAR_FLOAT_MODE_P (mode));
  3185. gcc_assert (GET_MODE (op1) == mode);
  3186. /* First try to do it with a special instruction. */
  3187. temp = expand_binop (mode, copysign_optab, op0, op1,
  3188. target, 0, OPTAB_DIRECT);
  3189. if (temp)
  3190. return temp;
  3191. fmt = REAL_MODE_FORMAT (mode);
  3192. if (fmt == NULL || !fmt->has_signed_zero)
  3193. return NULL_RTX;
  3194. op0_is_abs = false;
  3195. if (CONST_DOUBLE_AS_FLOAT_P (op0))
  3196. {
  3197. if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
  3198. op0 = simplify_unary_operation (ABS, mode, op0, mode);
  3199. op0_is_abs = true;
  3200. }
  3201. if (fmt->signbit_ro >= 0
  3202. && (CONST_DOUBLE_AS_FLOAT_P (op0)
  3203. || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
  3204. && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
  3205. {
  3206. temp = expand_copysign_absneg (mode, op0, op1, target,
  3207. fmt->signbit_ro, op0_is_abs);
  3208. if (temp)
  3209. return temp;
  3210. }
  3211. if (fmt->signbit_rw < 0)
  3212. return NULL_RTX;
  3213. return expand_copysign_bit (mode, op0, op1, target,
  3214. fmt->signbit_rw, op0_is_abs);
  3215. }
  3216. /* Generate an instruction whose insn-code is INSN_CODE,
  3217. with two operands: an output TARGET and an input OP0.
  3218. TARGET *must* be nonzero, and the output is always stored there.
  3219. CODE is an rtx code such that (CODE OP0) is an rtx that describes
  3220. the value that is stored into TARGET.
  3221. Return false if expansion failed. */
  3222. bool
  3223. maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
  3224. enum rtx_code code)
  3225. {
  3226. struct expand_operand ops[2];
  3227. rtx pat;
  3228. create_output_operand (&ops[0], target, GET_MODE (target));
  3229. create_input_operand (&ops[1], op0, GET_MODE (op0));
  3230. pat = maybe_gen_insn (icode, 2, ops);
  3231. if (!pat)
  3232. return false;
  3233. if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
  3234. && code != UNKNOWN)
  3235. add_equal_note (as_a <rtx_insn *> (pat), ops[0].value, code, ops[1].value,
  3236. NULL_RTX);
  3237. emit_insn (pat);
  3238. if (ops[0].value != target)
  3239. emit_move_insn (target, ops[0].value);
  3240. return true;
  3241. }
  3242. /* Generate an instruction whose insn-code is INSN_CODE,
  3243. with two operands: an output TARGET and an input OP0.
  3244. TARGET *must* be nonzero, and the output is always stored there.
  3245. CODE is an rtx code such that (CODE OP0) is an rtx that describes
  3246. the value that is stored into TARGET. */
  3247. void
  3248. emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
  3249. {
  3250. bool ok = maybe_emit_unop_insn (icode, target, op0, code);
  3251. gcc_assert (ok);
  3252. }
  3253. struct no_conflict_data
  3254. {
  3255. rtx target;
  3256. rtx_insn *first, *insn;
  3257. bool must_stay;
  3258. };
  3259. /* Called via note_stores by emit_libcall_block. Set P->must_stay if
  3260. the currently examined clobber / store has to stay in the list of
  3261. insns that constitute the actual libcall block. */
  3262. static void
  3263. no_conflict_move_test (rtx dest, const_rtx set, void *p0)
  3264. {
  3265. struct no_conflict_data *p= (struct no_conflict_data *) p0;
  3266. /* If this inns directly contributes to setting the target, it must stay. */
  3267. if (reg_overlap_mentioned_p (p->target, dest))
  3268. p->must_stay = true;
  3269. /* If we haven't committed to keeping any other insns in the list yet,
  3270. there is nothing more to check. */
  3271. else if (p->insn == p->first)
  3272. return;
  3273. /* If this insn sets / clobbers a register that feeds one of the insns
  3274. already in the list, this insn has to stay too. */
  3275. else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
  3276. || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
  3277. || reg_used_between_p (dest, p->first, p->insn)
  3278. /* Likewise if this insn depends on a register set by a previous
  3279. insn in the list, or if it sets a result (presumably a hard
  3280. register) that is set or clobbered by a previous insn.
  3281. N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
  3282. SET_DEST perform the former check on the address, and the latter
  3283. check on the MEM. */
  3284. || (GET_CODE (set) == SET
  3285. && (modified_in_p (SET_SRC (set), p->first)
  3286. || modified_in_p (SET_DEST (set), p->first)
  3287. || modified_between_p (SET_SRC (set), p->first, p->insn)
  3288. || modified_between_p (SET_DEST (set), p->first, p->insn))))
  3289. p->must_stay = true;
  3290. }
  3291. /* Emit code to make a call to a constant function or a library call.
  3292. INSNS is a list containing all insns emitted in the call.
  3293. These insns leave the result in RESULT. Our block is to copy RESULT
  3294. to TARGET, which is logically equivalent to EQUIV.
  3295. We first emit any insns that set a pseudo on the assumption that these are
  3296. loading constants into registers; doing so allows them to be safely cse'ed
  3297. between blocks. Then we emit all the other insns in the block, followed by
  3298. an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
  3299. note with an operand of EQUIV. */
  3300. static void
  3301. emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
  3302. bool equiv_may_trap)
  3303. {
  3304. rtx final_dest = target;
  3305. rtx_insn *next, *last, *insn;
  3306. /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
  3307. into a MEM later. Protect the libcall block from this change. */
  3308. if (! REG_P (target) || REG_USERVAR_P (target))
  3309. target = gen_reg_rtx (GET_MODE (target));
  3310. /* If we're using non-call exceptions, a libcall corresponding to an
  3311. operation that may trap may also trap. */
  3312. /* ??? See the comment in front of make_reg_eh_region_note. */
  3313. if (cfun->can_throw_non_call_exceptions
  3314. && (equiv_may_trap || may_trap_p (equiv)))
  3315. {
  3316. for (insn = insns; insn; insn = NEXT_INSN (insn))
  3317. if (CALL_P (insn))
  3318. {
  3319. rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
  3320. if (note)
  3321. {
  3322. int lp_nr = INTVAL (XEXP (note, 0));
  3323. if (lp_nr == 0 || lp_nr == INT_MIN)
  3324. remove_note (insn, note);
  3325. }
  3326. }
  3327. }
  3328. else
  3329. {
  3330. /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
  3331. reg note to indicate that this call cannot throw or execute a nonlocal
  3332. goto (unless there is already a REG_EH_REGION note, in which case
  3333. we update it). */
  3334. for (insn = insns; insn; insn = NEXT_INSN (insn))
  3335. if (CALL_P (insn))
  3336. make_reg_eh_region_note_nothrow_nononlocal (insn);
  3337. }
  3338. /* First emit all insns that set pseudos. Remove them from the list as
  3339. we go. Avoid insns that set pseudos which were referenced in previous
  3340. insns. These can be generated by move_by_pieces, for example,
  3341. to update an address. Similarly, avoid insns that reference things
  3342. set in previous insns. */
  3343. for (insn = insns; insn; insn = next)
  3344. {
  3345. rtx set = single_set (insn);
  3346. next = NEXT_INSN (insn);
  3347. if (set != 0 && REG_P (SET_DEST (set))
  3348. && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
  3349. {
  3350. struct no_conflict_data data;
  3351. data.target = const0_rtx;
  3352. data.first = insns;
  3353. data.insn = insn;
  3354. data.must_stay = 0;
  3355. note_stores (PATTERN (insn), no_conflict_move_test, &data);
  3356. if (! data.must_stay)
  3357. {
  3358. if (PREV_INSN (insn))
  3359. SET_NEXT_INSN (PREV_INSN (insn)) = next;
  3360. else
  3361. insns = next;
  3362. if (next)
  3363. SET_PREV_INSN (next) = PREV_INSN (insn);
  3364. add_insn (insn);
  3365. }
  3366. }
  3367. /* Some ports use a loop to copy large arguments onto the stack.
  3368. Don't move anything outside such a loop. */
  3369. if (LABEL_P (insn))
  3370. break;
  3371. }
  3372. /* Write the remaining insns followed by the final copy. */
  3373. for (insn = insns; insn; insn = next)
  3374. {
  3375. next = NEXT_INSN (insn);
  3376. add_insn (insn);
  3377. }
  3378. last = emit_move_insn (target, result);
  3379. set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
  3380. if (final_dest != target)
  3381. emit_move_insn (final_dest, target);
  3382. }
  3383. void
  3384. emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
  3385. {
  3386. emit_libcall_block_1 (safe_as_a <rtx_insn *> (insns),
  3387. target, result, equiv, false);
  3388. }
  3389. /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
  3390. PURPOSE describes how this comparison will be used. CODE is the rtx
  3391. comparison code we will be using.
  3392. ??? Actually, CODE is slightly weaker than that. A target is still
  3393. required to implement all of the normal bcc operations, but not
  3394. required to implement all (or any) of the unordered bcc operations. */
  3395. int
  3396. can_compare_p (enum rtx_code code, machine_mode mode,
  3397. enum can_compare_purpose purpose)
  3398. {
  3399. rtx test;
  3400. test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
  3401. do
  3402. {
  3403. enum insn_code icode;
  3404. if (purpose == ccp_jump
  3405. && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
  3406. && insn_operand_matches (icode, 0, test))
  3407. return 1;
  3408. if (purpose == ccp_store_flag
  3409. && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
  3410. && insn_operand_matches (icode, 1, test))
  3411. return 1;
  3412. if (purpose == ccp_cmov
  3413. && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
  3414. return 1;
  3415. mode = GET_MODE_WIDER_MODE (mode);
  3416. PUT_MODE (test, mode);
  3417. }
  3418. while (mode != VOIDmode);
  3419. return 0;
  3420. }
  3421. /* This function is called when we are going to emit a compare instruction that
  3422. compares the values found in *PX and *PY, using the rtl operator COMPARISON.
  3423. *PMODE is the mode of the inputs (in case they are const_int).
  3424. *PUNSIGNEDP nonzero says that the operands are unsigned;
  3425. this matters if they need to be widened (as given by METHODS).
  3426. If they have mode BLKmode, then SIZE specifies the size of both operands.
  3427. This function performs all the setup necessary so that the caller only has
  3428. to emit a single comparison insn. This setup can involve doing a BLKmode
  3429. comparison or emitting a library call to perform the comparison if no insn
  3430. is available to handle it.
  3431. The values which are passed in through pointers can be modified; the caller
  3432. should perform the comparison on the modified values. Constant
  3433. comparisons must have already been folded. */
  3434. static void
  3435. prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
  3436. int unsignedp, enum optab_methods methods,
  3437. rtx *ptest, machine_mode *pmode)
  3438. {
  3439. machine_mode mode = *pmode;
  3440. rtx libfunc, test;
  3441. machine_mode cmp_mode;
  3442. enum mode_class mclass;
  3443. /* The other methods are not needed. */
  3444. gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
  3445. || methods == OPTAB_LIB_WIDEN);
  3446. /* If we are optimizing, force expensive constants into a register. */
  3447. if (CONSTANT_P (x) && optimize
  3448. && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
  3449. > COSTS_N_INSNS (1)))
  3450. x = force_reg (mode, x);
  3451. if (CONSTANT_P (y) && optimize
  3452. && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
  3453. > COSTS_N_INSNS (1)))
  3454. y = force_reg (mode, y);
  3455. #ifdef HAVE_cc0
  3456. /* Make sure if we have a canonical comparison. The RTL
  3457. documentation states that canonical comparisons are required only
  3458. for targets which have cc0. */
  3459. gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
  3460. #endif
  3461. /* Don't let both operands fail to indicate the mode. */
  3462. if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
  3463. x = force_reg (mode, x);
  3464. if (mode == VOIDmode)
  3465. mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
  3466. /* Handle all BLKmode compares. */
  3467. if (mode == BLKmode)
  3468. {
  3469. machine_mode result_mode;
  3470. enum insn_code cmp_code;
  3471. tree length_type;
  3472. rtx libfunc;
  3473. rtx result;
  3474. rtx opalign
  3475. = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
  3476. gcc_assert (size);
  3477. /* Try to use a memory block compare insn - either cmpstr
  3478. or cmpmem will do. */
  3479. for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
  3480. cmp_mode != VOIDmode;
  3481. cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
  3482. {
  3483. cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
  3484. if (cmp_code == CODE_FOR_nothing)
  3485. cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
  3486. if (cmp_code == CODE_FOR_nothing)
  3487. cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
  3488. if (cmp_code == CODE_FOR_nothing)
  3489. continue;
  3490. /* Must make sure the size fits the insn's mode. */
  3491. if ((CONST_INT_P (size)
  3492. && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
  3493. || (GET_MODE_BITSIZE (GET_MODE (size))
  3494. > GET_MODE_BITSIZE (cmp_mode)))
  3495. continue;
  3496. result_mode = insn_data[cmp_code].operand[0].mode;
  3497. result = gen_reg_rtx (result_mode);
  3498. size = convert_to_mode (cmp_mode, size, 1);
  3499. emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
  3500. *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
  3501. *pmode = result_mode;
  3502. return;
  3503. }
  3504. if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
  3505. goto fail;
  3506. /* Otherwise call a library function, memcmp. */
  3507. libfunc = memcmp_libfunc;
  3508. length_type = sizetype;
  3509. result_mode = TYPE_MODE (integer_type_node);
  3510. cmp_mode = TYPE_MODE (length_type);
  3511. size = convert_to_mode (TYPE_MODE (length_type), size,
  3512. TYPE_UNSIGNED (length_type));
  3513. result = emit_library_call_value (libfunc, 0, LCT_PURE,
  3514. result_mode, 3,
  3515. XEXP (x, 0), Pmode,
  3516. XEXP (y, 0), Pmode,
  3517. size, cmp_mode);
  3518. x = result;
  3519. y = const0_rtx;
  3520. mode = result_mode;
  3521. methods = OPTAB_LIB_WIDEN;
  3522. unsignedp = false;
  3523. }
  3524. /* Don't allow operands to the compare to trap, as that can put the
  3525. compare and branch in different basic blocks. */
  3526. if (cfun->can_throw_non_call_exceptions)
  3527. {
  3528. if (may_trap_p (x))
  3529. x = force_reg (mode, x);
  3530. if (may_trap_p (y))
  3531. y = force_reg (mode, y);
  3532. }
  3533. if (GET_MODE_CLASS (mode) == MODE_CC)
  3534. {
  3535. enum insn_code icode = optab_handler (cbranch_optab, CCmode);
  3536. test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
  3537. gcc_assert (icode != CODE_FOR_nothing
  3538. && insn_operand_matches (icode, 0, test));
  3539. *ptest = test;
  3540. return;
  3541. }
  3542. mclass = GET_MODE_CLASS (mode);
  3543. test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
  3544. cmp_mode = mode;
  3545. do
  3546. {
  3547. enum insn_code icode;
  3548. icode = optab_handler (cbranch_optab, cmp_mode);
  3549. if (icode != CODE_FOR_nothing
  3550. && insn_operand_matches (icode, 0, test))
  3551. {
  3552. rtx_insn *last = get_last_insn ();
  3553. rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
  3554. rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
  3555. if (op0 && op1
  3556. && insn_operand_matches (icode, 1, op0)
  3557. && insn_operand_matches (icode, 2, op1))
  3558. {
  3559. XEXP (test, 0) = op0;
  3560. XEXP (test, 1) = op1;
  3561. *ptest = test;
  3562. *pmode = cmp_mode;
  3563. return;
  3564. }
  3565. delete_insns_since (last);
  3566. }
  3567. if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
  3568. break;
  3569. cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
  3570. }
  3571. while (cmp_mode != VOIDmode);
  3572. if (methods != OPTAB_LIB_WIDEN)
  3573. goto fail;
  3574. if (!SCALAR_FLOAT_MODE_P (mode))
  3575. {
  3576. rtx result;
  3577. machine_mode ret_mode;
  3578. /* Handle a libcall just for the mode we are using. */
  3579. libfunc = optab_libfunc (cmp_optab, mode);
  3580. gcc_assert (libfunc);
  3581. /* If we want unsigned, and this mode has a distinct unsigned
  3582. comparison routine, use that. */
  3583. if (unsignedp)
  3584. {
  3585. rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
  3586. if (ulibfunc)
  3587. libfunc = ulibfunc;
  3588. }
  3589. ret_mode = targetm.libgcc_cmp_return_mode ();
  3590. result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
  3591. ret_mode, 2, x, mode, y, mode);
  3592. /* There are two kinds of comparison routines. Biased routines
  3593. return 0/1/2, and unbiased routines return -1/0/1. Other parts
  3594. of gcc expect that the comparison operation is equivalent
  3595. to the modified comparison. For signed comparisons compare the
  3596. result against 1 in the biased case, and zero in the unbiased
  3597. case. For unsigned comparisons always compare against 1 after
  3598. biasing the unbiased result by adding 1. This gives us a way to
  3599. represent LTU.
  3600. The comparisons in the fixed-point helper library are always
  3601. biased. */
  3602. x = result;
  3603. y = const1_rtx;
  3604. if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
  3605. {
  3606. if (unsignedp)
  3607. x = plus_constant (ret_mode, result, 1);
  3608. else
  3609. y = const0_rtx;
  3610. }
  3611. *pmode = ret_mode;
  3612. prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
  3613. ptest, pmode);
  3614. }
  3615. else
  3616. prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
  3617. return;
  3618. fail:
  3619. *ptest = NULL_RTX;
  3620. }
  3621. /* Before emitting an insn with code ICODE, make sure that X, which is going
  3622. to be used for operand OPNUM of the insn, is converted from mode MODE to
  3623. WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
  3624. that it is accepted by the operand predicate. Return the new value. */
  3625. rtx
  3626. prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
  3627. machine_mode wider_mode, int unsignedp)
  3628. {
  3629. if (mode != wider_mode)
  3630. x = convert_modes (wider_mode, mode, x, unsignedp);
  3631. if (!insn_operand_matches (icode, opnum, x))
  3632. {
  3633. machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
  3634. if (reload_completed)
  3635. return NULL_RTX;
  3636. if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
  3637. return NULL_RTX;
  3638. x = copy_to_mode_reg (op_mode, x);
  3639. }
  3640. return x;
  3641. }
  3642. /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
  3643. we can do the branch. */
  3644. static void
  3645. emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label, int prob)
  3646. {
  3647. machine_mode optab_mode;
  3648. enum mode_class mclass;
  3649. enum insn_code icode;
  3650. rtx_insn *insn;
  3651. mclass = GET_MODE_CLASS (mode);
  3652. optab_mode = (mclass == MODE_CC) ? CCmode : mode;
  3653. icode = optab_handler (cbranch_optab, optab_mode);
  3654. gcc_assert (icode != CODE_FOR_nothing);
  3655. gcc_assert (insn_operand_matches (icode, 0, test));
  3656. insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
  3657. XEXP (test, 1), label));
  3658. if (prob != -1
  3659. && profile_status_for_fn (cfun) != PROFILE_ABSENT
  3660. && insn
  3661. && JUMP_P (insn)
  3662. && any_condjump_p (insn)
  3663. && !find_reg_note (insn, REG_BR_PROB, 0))
  3664. add_int_reg_note (insn, REG_BR_PROB, prob);
  3665. }
  3666. /* Generate code to compare X with Y so that the condition codes are
  3667. set and to jump to LABEL if the condition is true. If X is a
  3668. constant and Y is not a constant, then the comparison is swapped to
  3669. ensure that the comparison RTL has the canonical form.
  3670. UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
  3671. need to be widened. UNSIGNEDP is also used to select the proper
  3672. branch condition code.
  3673. If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
  3674. MODE is the mode of the inputs (in case they are const_int).
  3675. COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
  3676. It will be potentially converted into an unsigned variant based on
  3677. UNSIGNEDP to select a proper jump instruction.
  3678. PROB is the probability of jumping to LABEL. */
  3679. void
  3680. emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
  3681. machine_mode mode, int unsignedp, rtx label,
  3682. int prob)
  3683. {
  3684. rtx op0 = x, op1 = y;
  3685. rtx test;
  3686. /* Swap operands and condition to ensure canonical RTL. */
  3687. if (swap_commutative_operands_p (x, y)
  3688. && can_compare_p (swap_condition (comparison), mode, ccp_jump))
  3689. {
  3690. op0 = y, op1 = x;
  3691. comparison = swap_condition (comparison);
  3692. }
  3693. /* If OP0 is still a constant, then both X and Y must be constants
  3694. or the opposite comparison is not supported. Force X into a register
  3695. to create canonical RTL. */
  3696. if (CONSTANT_P (op0))
  3697. op0 = force_reg (mode, op0);
  3698. if (unsignedp)
  3699. comparison = unsigned_condition (comparison);
  3700. prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
  3701. &test, &mode);
  3702. emit_cmp_and_jump_insn_1 (test, mode, label, prob);
  3703. }
  3704. /* Emit a library call comparison between floating point X and Y.
  3705. COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
  3706. static void
  3707. prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
  3708. rtx *ptest, machine_mode *pmode)
  3709. {
  3710. enum rtx_code swapped = swap_condition (comparison);
  3711. enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
  3712. machine_mode orig_mode = GET_MODE (x);
  3713. machine_mode mode, cmp_mode;
  3714. rtx true_rtx, false_rtx;
  3715. rtx value, target, equiv;
  3716. rtx_insn *insns;
  3717. rtx libfunc = 0;
  3718. bool reversed_p = false;
  3719. cmp_mode = targetm.libgcc_cmp_return_mode ();
  3720. for (mode = orig_mode;
  3721. mode != VOIDmode;
  3722. mode = GET_MODE_WIDER_MODE (mode))
  3723. {
  3724. if (code_to_optab (comparison)
  3725. && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
  3726. break;
  3727. if (code_to_optab (swapped)
  3728. && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
  3729. {
  3730. rtx tmp;
  3731. tmp = x; x = y; y = tmp;
  3732. comparison = swapped;
  3733. break;
  3734. }
  3735. if (code_to_optab (reversed)
  3736. && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
  3737. {
  3738. comparison = reversed;
  3739. reversed_p = true;
  3740. break;
  3741. }
  3742. }
  3743. gcc_assert (mode != VOIDmode);
  3744. if (mode != orig_mode)
  3745. {
  3746. x = convert_to_mode (mode, x, 0);
  3747. y = convert_to_mode (mode, y, 0);
  3748. }
  3749. /* Attach a REG_EQUAL note describing the semantics of the libcall to
  3750. the RTL. The allows the RTL optimizers to delete the libcall if the
  3751. condition can be determined at compile-time. */
  3752. if (comparison == UNORDERED
  3753. || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
  3754. {
  3755. true_rtx = const_true_rtx;
  3756. false_rtx = const0_rtx;
  3757. }
  3758. else
  3759. {
  3760. switch (comparison)
  3761. {
  3762. case EQ:
  3763. true_rtx = const0_rtx;
  3764. false_rtx = const_true_rtx;
  3765. break;
  3766. case NE:
  3767. true_rtx = const_true_rtx;
  3768. false_rtx = const0_rtx;
  3769. break;
  3770. case GT:
  3771. true_rtx = const1_rtx;
  3772. false_rtx = const0_rtx;
  3773. break;
  3774. case GE:
  3775. true_rtx = const0_rtx;
  3776. false_rtx = constm1_rtx;
  3777. break;
  3778. case LT:
  3779. true_rtx = constm1_rtx;
  3780. false_rtx = const0_rtx;
  3781. break;
  3782. case LE:
  3783. true_rtx = const0_rtx;
  3784. false_rtx = const1_rtx;
  3785. break;
  3786. default:
  3787. gcc_unreachable ();
  3788. }
  3789. }
  3790. if (comparison == UNORDERED)
  3791. {
  3792. rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
  3793. equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
  3794. equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
  3795. temp, const_true_rtx, equiv);
  3796. }
  3797. else
  3798. {
  3799. equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
  3800. if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
  3801. equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
  3802. equiv, true_rtx, false_rtx);
  3803. }
  3804. start_sequence ();
  3805. value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
  3806. cmp_mode, 2, x, mode, y, mode);
  3807. insns = get_insns ();
  3808. end_sequence ();
  3809. target = gen_reg_rtx (cmp_mode);
  3810. emit_libcall_block (insns, target, value, equiv);
  3811. if (comparison == UNORDERED
  3812. || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
  3813. || reversed_p)
  3814. *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
  3815. else
  3816. *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
  3817. *pmode = cmp_mode;
  3818. }
  3819. /* Generate code to indirectly jump to a location given in the rtx LOC. */
  3820. void
  3821. emit_indirect_jump (rtx loc ATTRIBUTE_UNUSED)
  3822. {
  3823. #ifndef HAVE_indirect_jump
  3824. sorry ("indirect jumps are not available on this target");
  3825. #else
  3826. struct expand_operand ops[1];
  3827. create_address_operand (&ops[0], loc);
  3828. expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
  3829. emit_barrier ();
  3830. #endif
  3831. }
  3832. #ifdef HAVE_conditional_move
  3833. /* Emit a conditional move instruction if the machine supports one for that
  3834. condition and machine mode.
  3835. OP0 and OP1 are the operands that should be compared using CODE. CMODE is
  3836. the mode to use should they be constants. If it is VOIDmode, they cannot
  3837. both be constants.
  3838. OP2 should be stored in TARGET if the comparison is true, otherwise OP3
  3839. should be stored there. MODE is the mode to use should they be constants.
  3840. If it is VOIDmode, they cannot both be constants.
  3841. The result is either TARGET (perhaps modified) or NULL_RTX if the operation
  3842. is not supported. */
  3843. rtx
  3844. emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
  3845. machine_mode cmode, rtx op2, rtx op3,
  3846. machine_mode mode, int unsignedp)
  3847. {
  3848. rtx tem, comparison;
  3849. rtx_insn *last;
  3850. enum insn_code icode;
  3851. enum rtx_code reversed;
  3852. /* If one operand is constant, make it the second one. Only do this
  3853. if the other operand is not constant as well. */
  3854. if (swap_commutative_operands_p (op0, op1))
  3855. {
  3856. tem = op0;
  3857. op0 = op1;
  3858. op1 = tem;
  3859. code = swap_condition (code);
  3860. }
  3861. /* get_condition will prefer to generate LT and GT even if the old
  3862. comparison was against zero, so undo that canonicalization here since
  3863. comparisons against zero are cheaper. */
  3864. if (code == LT && op1 == const1_rtx)
  3865. code = LE, op1 = const0_rtx;
  3866. else if (code == GT && op1 == constm1_rtx)
  3867. code = GE, op1 = const0_rtx;
  3868. if (cmode == VOIDmode)
  3869. cmode = GET_MODE (op0);
  3870. if (swap_commutative_operands_p (op2, op3)
  3871. && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
  3872. != UNKNOWN))
  3873. {
  3874. tem = op2;
  3875. op2 = op3;
  3876. op3 = tem;
  3877. code = reversed;
  3878. }
  3879. if (mode == VOIDmode)
  3880. mode = GET_MODE (op2);
  3881. icode = direct_optab_handler (movcc_optab, mode);
  3882. if (icode == CODE_FOR_nothing)
  3883. return 0;
  3884. if (!target)
  3885. target = gen_reg_rtx (mode);
  3886. code = unsignedp ? unsigned_condition (code) : code;
  3887. comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
  3888. /* We can get const0_rtx or const_true_rtx in some circumstances. Just
  3889. return NULL and let the caller figure out how best to deal with this
  3890. situation. */
  3891. if (!COMPARISON_P (comparison))
  3892. return NULL_RTX;
  3893. saved_pending_stack_adjust save;
  3894. save_pending_stack_adjust (&save);
  3895. last = get_last_insn ();
  3896. do_pending_stack_adjust ();
  3897. prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
  3898. GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
  3899. &comparison, &cmode);
  3900. if (comparison)
  3901. {
  3902. struct expand_operand ops[4];
  3903. create_output_operand (&ops[0], target, mode);
  3904. create_fixed_operand (&ops[1], comparison);
  3905. create_input_operand (&ops[2], op2, mode);
  3906. create_input_operand (&ops[3], op3, mode);
  3907. if (maybe_expand_insn (icode, 4, ops))
  3908. {
  3909. if (ops[0].value != target)
  3910. convert_move (target, ops[0].value, false);
  3911. return target;
  3912. }
  3913. }
  3914. delete_insns_since (last);
  3915. restore_pending_stack_adjust (&save);
  3916. return NULL_RTX;
  3917. }
  3918. /* Return nonzero if a conditional move of mode MODE is supported.
  3919. This function is for combine so it can tell whether an insn that looks
  3920. like a conditional move is actually supported by the hardware. If we
  3921. guess wrong we lose a bit on optimization, but that's it. */
  3922. /* ??? sparc64 supports conditionally moving integers values based on fp
  3923. comparisons, and vice versa. How do we handle them? */
  3924. int
  3925. can_conditionally_move_p (machine_mode mode)
  3926. {
  3927. if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
  3928. return 1;
  3929. return 0;
  3930. }
  3931. #endif /* HAVE_conditional_move */
  3932. /* Emit a conditional addition instruction if the machine supports one for that
  3933. condition and machine mode.
  3934. OP0 and OP1 are the operands that should be compared using CODE. CMODE is
  3935. the mode to use should they be constants. If it is VOIDmode, they cannot
  3936. both be constants.
  3937. OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
  3938. should be stored there. MODE is the mode to use should they be constants.
  3939. If it is VOIDmode, they cannot both be constants.
  3940. The result is either TARGET (perhaps modified) or NULL_RTX if the operation
  3941. is not supported. */
  3942. rtx
  3943. emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
  3944. machine_mode cmode, rtx op2, rtx op3,
  3945. machine_mode mode, int unsignedp)
  3946. {
  3947. rtx tem, comparison;
  3948. rtx_insn *last;
  3949. enum insn_code icode;
  3950. /* If one operand is constant, make it the second one. Only do this
  3951. if the other operand is not constant as well. */
  3952. if (swap_commutative_operands_p (op0, op1))
  3953. {
  3954. tem = op0;
  3955. op0 = op1;
  3956. op1 = tem;
  3957. code = swap_condition (code);
  3958. }
  3959. /* get_condition will prefer to generate LT and GT even if the old
  3960. comparison was against zero, so undo that canonicalization here since
  3961. comparisons against zero are cheaper. */
  3962. if (code == LT && op1 == const1_rtx)
  3963. code = LE, op1 = const0_rtx;
  3964. else if (code == GT && op1 == constm1_rtx)
  3965. code = GE, op1 = const0_rtx;
  3966. if (cmode == VOIDmode)
  3967. cmode = GET_MODE (op0);
  3968. if (mode == VOIDmode)
  3969. mode = GET_MODE (op2);
  3970. icode = optab_handler (addcc_optab, mode);
  3971. if (icode == CODE_FOR_nothing)
  3972. return 0;
  3973. if (!target)
  3974. target = gen_reg_rtx (mode);
  3975. code = unsignedp ? unsigned_condition (code) : code;
  3976. comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
  3977. /* We can get const0_rtx or const_true_rtx in some circumstances. Just
  3978. return NULL and let the caller figure out how best to deal with this
  3979. situation. */
  3980. if (!COMPARISON_P (comparison))
  3981. return NULL_RTX;
  3982. do_pending_stack_adjust ();
  3983. last = get_last_insn ();
  3984. prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
  3985. GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
  3986. &comparison, &cmode);
  3987. if (comparison)
  3988. {
  3989. struct expand_operand ops[4];
  3990. create_output_operand (&ops[0], target, mode);
  3991. create_fixed_operand (&ops[1], comparison);
  3992. create_input_operand (&ops[2], op2, mode);
  3993. create_input_operand (&ops[3], op3, mode);
  3994. if (maybe_expand_insn (icode, 4, ops))
  3995. {
  3996. if (ops[0].value != target)
  3997. convert_move (target, ops[0].value, false);
  3998. return target;
  3999. }
  4000. }
  4001. delete_insns_since (last);
  4002. return NULL_RTX;
  4003. }
  4004. /* These functions attempt to generate an insn body, rather than
  4005. emitting the insn, but if the gen function already emits them, we
  4006. make no attempt to turn them back into naked patterns. */
  4007. /* Generate and return an insn body to add Y to X. */
  4008. rtx
  4009. gen_add2_insn (rtx x, rtx y)
  4010. {
  4011. enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
  4012. gcc_assert (insn_operand_matches (icode, 0, x));
  4013. gcc_assert (insn_operand_matches (icode, 1, x));
  4014. gcc_assert (insn_operand_matches (icode, 2, y));
  4015. return GEN_FCN (icode) (x, x, y);
  4016. }
  4017. /* Generate and return an insn body to add r1 and c,
  4018. storing the result in r0. */
  4019. rtx
  4020. gen_add3_insn (rtx r0, rtx r1, rtx c)
  4021. {
  4022. enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
  4023. if (icode == CODE_FOR_nothing
  4024. || !insn_operand_matches (icode, 0, r0)
  4025. || !insn_operand_matches (icode, 1, r1)
  4026. || !insn_operand_matches (icode, 2, c))
  4027. return NULL_RTX;
  4028. return GEN_FCN (icode) (r0, r1, c);
  4029. }
  4030. int
  4031. have_add2_insn (rtx x, rtx y)
  4032. {
  4033. enum insn_code icode;
  4034. gcc_assert (GET_MODE (x) != VOIDmode);
  4035. icode = optab_handler (add_optab, GET_MODE (x));
  4036. if (icode == CODE_FOR_nothing)
  4037. return 0;
  4038. if (!insn_operand_matches (icode, 0, x)
  4039. || !insn_operand_matches (icode, 1, x)
  4040. || !insn_operand_matches (icode, 2, y))
  4041. return 0;
  4042. return 1;
  4043. }
  4044. /* Generate and return an insn body to add Y to X. */
  4045. rtx
  4046. gen_addptr3_insn (rtx x, rtx y, rtx z)
  4047. {
  4048. enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
  4049. gcc_assert (insn_operand_matches (icode, 0, x));
  4050. gcc_assert (insn_operand_matches (icode, 1, y));
  4051. gcc_assert (insn_operand_matches (icode, 2, z));
  4052. return GEN_FCN (icode) (x, y, z);
  4053. }
  4054. /* Return true if the target implements an addptr pattern and X, Y,
  4055. and Z are valid for the pattern predicates. */
  4056. int
  4057. have_addptr3_insn (rtx x, rtx y, rtx z)
  4058. {
  4059. enum insn_code icode;
  4060. gcc_assert (GET_MODE (x) != VOIDmode);
  4061. icode = optab_handler (addptr3_optab, GET_MODE (x));
  4062. if (icode == CODE_FOR_nothing)
  4063. return 0;
  4064. if (!insn_operand_matches (icode, 0, x)
  4065. || !insn_operand_matches (icode, 1, y)
  4066. || !insn_operand_matches (icode, 2, z))
  4067. return 0;
  4068. return 1;
  4069. }
  4070. /* Generate and return an insn body to subtract Y from X. */
  4071. rtx
  4072. gen_sub2_insn (rtx x, rtx y)
  4073. {
  4074. enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
  4075. gcc_assert (insn_operand_matches (icode, 0, x));
  4076. gcc_assert (insn_operand_matches (icode, 1, x));
  4077. gcc_assert (insn_operand_matches (icode, 2, y));
  4078. return GEN_FCN (icode) (x, x, y);
  4079. }
  4080. /* Generate and return an insn body to subtract r1 and c,
  4081. storing the result in r0. */
  4082. rtx
  4083. gen_sub3_insn (rtx r0, rtx r1, rtx c)
  4084. {
  4085. enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
  4086. if (icode == CODE_FOR_nothing
  4087. || !insn_operand_matches (icode, 0, r0)
  4088. || !insn_operand_matches (icode, 1, r1)
  4089. || !insn_operand_matches (icode, 2, c))
  4090. return NULL_RTX;
  4091. return GEN_FCN (icode) (r0, r1, c);
  4092. }
  4093. int
  4094. have_sub2_insn (rtx x, rtx y)
  4095. {
  4096. enum insn_code icode;
  4097. gcc_assert (GET_MODE (x) != VOIDmode);
  4098. icode = optab_handler (sub_optab, GET_MODE (x));
  4099. if (icode == CODE_FOR_nothing)
  4100. return 0;
  4101. if (!insn_operand_matches (icode, 0, x)
  4102. || !insn_operand_matches (icode, 1, x)
  4103. || !insn_operand_matches (icode, 2, y))
  4104. return 0;
  4105. return 1;
  4106. }
  4107. /* Return the insn code used to extend FROM_MODE to TO_MODE.
  4108. UNSIGNEDP specifies zero-extension instead of sign-extension. If
  4109. no such operation exists, CODE_FOR_nothing will be returned. */
  4110. enum insn_code
  4111. can_extend_p (machine_mode to_mode, machine_mode from_mode,
  4112. int unsignedp)
  4113. {
  4114. convert_optab tab;
  4115. #ifdef HAVE_ptr_extend
  4116. if (unsignedp < 0)
  4117. return CODE_FOR_ptr_extend;
  4118. #endif
  4119. tab = unsignedp ? zext_optab : sext_optab;
  4120. return convert_optab_handler (tab, to_mode, from_mode);
  4121. }
  4122. /* Generate the body of an insn to extend Y (with mode MFROM)
  4123. into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
  4124. rtx
  4125. gen_extend_insn (rtx x, rtx y, machine_mode mto,
  4126. machine_mode mfrom, int unsignedp)
  4127. {
  4128. enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
  4129. return GEN_FCN (icode) (x, y);
  4130. }
  4131. /* can_fix_p and can_float_p say whether the target machine
  4132. can directly convert a given fixed point type to
  4133. a given floating point type, or vice versa.
  4134. The returned value is the CODE_FOR_... value to use,
  4135. or CODE_FOR_nothing if these modes cannot be directly converted.
  4136. *TRUNCP_PTR is set to 1 if it is necessary to output
  4137. an explicit FTRUNC insn before the fix insn; otherwise 0. */
  4138. static enum insn_code
  4139. can_fix_p (machine_mode fixmode, machine_mode fltmode,
  4140. int unsignedp, int *truncp_ptr)
  4141. {
  4142. convert_optab tab;
  4143. enum insn_code icode;
  4144. tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
  4145. icode = convert_optab_handler (tab, fixmode, fltmode);
  4146. if (icode != CODE_FOR_nothing)
  4147. {
  4148. *truncp_ptr = 0;
  4149. return icode;
  4150. }
  4151. /* FIXME: This requires a port to define both FIX and FTRUNC pattern
  4152. for this to work. We need to rework the fix* and ftrunc* patterns
  4153. and documentation. */
  4154. tab = unsignedp ? ufix_optab : sfix_optab;
  4155. icode = convert_optab_handler (tab, fixmode, fltmode);
  4156. if (icode != CODE_FOR_nothing
  4157. && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
  4158. {
  4159. *truncp_ptr = 1;
  4160. return icode;
  4161. }
  4162. *truncp_ptr = 0;
  4163. return CODE_FOR_nothing;
  4164. }
  4165. enum insn_code
  4166. can_float_p (machine_mode fltmode, machine_mode fixmode,
  4167. int unsignedp)
  4168. {
  4169. convert_optab tab;
  4170. tab = unsignedp ? ufloat_optab : sfloat_optab;
  4171. return convert_optab_handler (tab, fltmode, fixmode);
  4172. }
  4173. /* Function supportable_convert_operation
  4174. Check whether an operation represented by the code CODE is a
  4175. convert operation that is supported by the target platform in
  4176. vector form (i.e., when operating on arguments of type VECTYPE_IN
  4177. producing a result of type VECTYPE_OUT).
  4178. Convert operations we currently support directly are FIX_TRUNC and FLOAT.
  4179. This function checks if these operations are supported
  4180. by the target platform either directly (via vector tree-codes), or via
  4181. target builtins.
  4182. Output:
  4183. - CODE1 is code of vector operation to be used when
  4184. vectorizing the operation, if available.
  4185. - DECL is decl of target builtin functions to be used
  4186. when vectorizing the operation, if available. In this case,
  4187. CODE1 is CALL_EXPR. */
  4188. bool
  4189. supportable_convert_operation (enum tree_code code,
  4190. tree vectype_out, tree vectype_in,
  4191. tree *decl, enum tree_code *code1)
  4192. {
  4193. machine_mode m1,m2;
  4194. int truncp;
  4195. m1 = TYPE_MODE (vectype_out);
  4196. m2 = TYPE_MODE (vectype_in);
  4197. /* First check if we can done conversion directly. */
  4198. if ((code == FIX_TRUNC_EXPR
  4199. && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
  4200. != CODE_FOR_nothing)
  4201. || (code == FLOAT_EXPR
  4202. && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
  4203. != CODE_FOR_nothing))
  4204. {
  4205. *code1 = code;
  4206. return true;
  4207. }
  4208. /* Now check for builtin. */
  4209. if (targetm.vectorize.builtin_conversion
  4210. && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
  4211. {
  4212. *code1 = CALL_EXPR;
  4213. *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
  4214. return true;
  4215. }
  4216. return false;
  4217. }
  4218. /* Generate code to convert FROM to floating point
  4219. and store in TO. FROM must be fixed point and not VOIDmode.
  4220. UNSIGNEDP nonzero means regard FROM as unsigned.
  4221. Normally this is done by correcting the final value
  4222. if it is negative. */
  4223. void
  4224. expand_float (rtx to, rtx from, int unsignedp)
  4225. {
  4226. enum insn_code icode;
  4227. rtx target = to;
  4228. machine_mode fmode, imode;
  4229. bool can_do_signed = false;
  4230. /* Crash now, because we won't be able to decide which mode to use. */
  4231. gcc_assert (GET_MODE (from) != VOIDmode);
  4232. /* Look for an insn to do the conversion. Do it in the specified
  4233. modes if possible; otherwise convert either input, output or both to
  4234. wider mode. If the integer mode is wider than the mode of FROM,
  4235. we can do the conversion signed even if the input is unsigned. */
  4236. for (fmode = GET_MODE (to); fmode != VOIDmode;
  4237. fmode = GET_MODE_WIDER_MODE (fmode))
  4238. for (imode = GET_MODE (from); imode != VOIDmode;
  4239. imode = GET_MODE_WIDER_MODE (imode))
  4240. {
  4241. int doing_unsigned = unsignedp;
  4242. if (fmode != GET_MODE (to)
  4243. && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
  4244. continue;
  4245. icode = can_float_p (fmode, imode, unsignedp);
  4246. if (icode == CODE_FOR_nothing && unsignedp)
  4247. {
  4248. enum insn_code scode = can_float_p (fmode, imode, 0);
  4249. if (scode != CODE_FOR_nothing)
  4250. can_do_signed = true;
  4251. if (imode != GET_MODE (from))
  4252. icode = scode, doing_unsigned = 0;
  4253. }
  4254. if (icode != CODE_FOR_nothing)
  4255. {
  4256. if (imode != GET_MODE (from))
  4257. from = convert_to_mode (imode, from, unsignedp);
  4258. if (fmode != GET_MODE (to))
  4259. target = gen_reg_rtx (fmode);
  4260. emit_unop_insn (icode, target, from,
  4261. doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
  4262. if (target != to)
  4263. convert_move (to, target, 0);
  4264. return;
  4265. }
  4266. }
  4267. /* Unsigned integer, and no way to convert directly. Convert as signed,
  4268. then unconditionally adjust the result. */
  4269. if (unsignedp && can_do_signed)
  4270. {
  4271. rtx_code_label *label = gen_label_rtx ();
  4272. rtx temp;
  4273. REAL_VALUE_TYPE offset;
  4274. /* Look for a usable floating mode FMODE wider than the source and at
  4275. least as wide as the target. Using FMODE will avoid rounding woes
  4276. with unsigned values greater than the signed maximum value. */
  4277. for (fmode = GET_MODE (to); fmode != VOIDmode;
  4278. fmode = GET_MODE_WIDER_MODE (fmode))
  4279. if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
  4280. && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
  4281. break;
  4282. if (fmode == VOIDmode)
  4283. {
  4284. /* There is no such mode. Pretend the target is wide enough. */
  4285. fmode = GET_MODE (to);
  4286. /* Avoid double-rounding when TO is narrower than FROM. */
  4287. if ((significand_size (fmode) + 1)
  4288. < GET_MODE_PRECISION (GET_MODE (from)))
  4289. {
  4290. rtx temp1;
  4291. rtx_code_label *neglabel = gen_label_rtx ();
  4292. /* Don't use TARGET if it isn't a register, is a hard register,
  4293. or is the wrong mode. */
  4294. if (!REG_P (target)
  4295. || REGNO (target) < FIRST_PSEUDO_REGISTER
  4296. || GET_MODE (target) != fmode)
  4297. target = gen_reg_rtx (fmode);
  4298. imode = GET_MODE (from);
  4299. do_pending_stack_adjust ();
  4300. /* Test whether the sign bit is set. */
  4301. emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
  4302. 0, neglabel);
  4303. /* The sign bit is not set. Convert as signed. */
  4304. expand_float (target, from, 0);
  4305. emit_jump_insn (gen_jump (label));
  4306. emit_barrier ();
  4307. /* The sign bit is set.
  4308. Convert to a usable (positive signed) value by shifting right
  4309. one bit, while remembering if a nonzero bit was shifted
  4310. out; i.e., compute (from & 1) | (from >> 1). */
  4311. emit_label (neglabel);
  4312. temp = expand_binop (imode, and_optab, from, const1_rtx,
  4313. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  4314. temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
  4315. temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
  4316. OPTAB_LIB_WIDEN);
  4317. expand_float (target, temp, 0);
  4318. /* Multiply by 2 to undo the shift above. */
  4319. temp = expand_binop (fmode, add_optab, target, target,
  4320. target, 0, OPTAB_LIB_WIDEN);
  4321. if (temp != target)
  4322. emit_move_insn (target, temp);
  4323. do_pending_stack_adjust ();
  4324. emit_label (label);
  4325. goto done;
  4326. }
  4327. }
  4328. /* If we are about to do some arithmetic to correct for an
  4329. unsigned operand, do it in a pseudo-register. */
  4330. if (GET_MODE (to) != fmode
  4331. || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
  4332. target = gen_reg_rtx (fmode);
  4333. /* Convert as signed integer to floating. */
  4334. expand_float (target, from, 0);
  4335. /* If FROM is negative (and therefore TO is negative),
  4336. correct its value by 2**bitwidth. */
  4337. do_pending_stack_adjust ();
  4338. emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
  4339. 0, label);
  4340. real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
  4341. temp = expand_binop (fmode, add_optab, target,
  4342. CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
  4343. target, 0, OPTAB_LIB_WIDEN);
  4344. if (temp != target)
  4345. emit_move_insn (target, temp);
  4346. do_pending_stack_adjust ();
  4347. emit_label (label);
  4348. goto done;
  4349. }
  4350. /* No hardware instruction available; call a library routine. */
  4351. {
  4352. rtx libfunc;
  4353. rtx_insn *insns;
  4354. rtx value;
  4355. convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
  4356. if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
  4357. from = convert_to_mode (SImode, from, unsignedp);
  4358. libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
  4359. gcc_assert (libfunc);
  4360. start_sequence ();
  4361. value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
  4362. GET_MODE (to), 1, from,
  4363. GET_MODE (from));
  4364. insns = get_insns ();
  4365. end_sequence ();
  4366. emit_libcall_block (insns, target, value,
  4367. gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
  4368. GET_MODE (to), from));
  4369. }
  4370. done:
  4371. /* Copy result to requested destination
  4372. if we have been computing in a temp location. */
  4373. if (target != to)
  4374. {
  4375. if (GET_MODE (target) == GET_MODE (to))
  4376. emit_move_insn (to, target);
  4377. else
  4378. convert_move (to, target, 0);
  4379. }
  4380. }
  4381. /* Generate code to convert FROM to fixed point and store in TO. FROM
  4382. must be floating point. */
  4383. void
  4384. expand_fix (rtx to, rtx from, int unsignedp)
  4385. {
  4386. enum insn_code icode;
  4387. rtx target = to;
  4388. machine_mode fmode, imode;
  4389. int must_trunc = 0;
  4390. /* We first try to find a pair of modes, one real and one integer, at
  4391. least as wide as FROM and TO, respectively, in which we can open-code
  4392. this conversion. If the integer mode is wider than the mode of TO,
  4393. we can do the conversion either signed or unsigned. */
  4394. for (fmode = GET_MODE (from); fmode != VOIDmode;
  4395. fmode = GET_MODE_WIDER_MODE (fmode))
  4396. for (imode = GET_MODE (to); imode != VOIDmode;
  4397. imode = GET_MODE_WIDER_MODE (imode))
  4398. {
  4399. int doing_unsigned = unsignedp;
  4400. icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
  4401. if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
  4402. icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
  4403. if (icode != CODE_FOR_nothing)
  4404. {
  4405. rtx_insn *last = get_last_insn ();
  4406. if (fmode != GET_MODE (from))
  4407. from = convert_to_mode (fmode, from, 0);
  4408. if (must_trunc)
  4409. {
  4410. rtx temp = gen_reg_rtx (GET_MODE (from));
  4411. from = expand_unop (GET_MODE (from), ftrunc_optab, from,
  4412. temp, 0);
  4413. }
  4414. if (imode != GET_MODE (to))
  4415. target = gen_reg_rtx (imode);
  4416. if (maybe_emit_unop_insn (icode, target, from,
  4417. doing_unsigned ? UNSIGNED_FIX : FIX))
  4418. {
  4419. if (target != to)
  4420. convert_move (to, target, unsignedp);
  4421. return;
  4422. }
  4423. delete_insns_since (last);
  4424. }
  4425. }
  4426. /* For an unsigned conversion, there is one more way to do it.
  4427. If we have a signed conversion, we generate code that compares
  4428. the real value to the largest representable positive number. If if
  4429. is smaller, the conversion is done normally. Otherwise, subtract
  4430. one plus the highest signed number, convert, and add it back.
  4431. We only need to check all real modes, since we know we didn't find
  4432. anything with a wider integer mode.
  4433. This code used to extend FP value into mode wider than the destination.
  4434. This is needed for decimal float modes which cannot accurately
  4435. represent one plus the highest signed number of the same size, but
  4436. not for binary modes. Consider, for instance conversion from SFmode
  4437. into DImode.
  4438. The hot path through the code is dealing with inputs smaller than 2^63
  4439. and doing just the conversion, so there is no bits to lose.
  4440. In the other path we know the value is positive in the range 2^63..2^64-1
  4441. inclusive. (as for other input overflow happens and result is undefined)
  4442. So we know that the most important bit set in mantissa corresponds to
  4443. 2^63. The subtraction of 2^63 should not generate any rounding as it
  4444. simply clears out that bit. The rest is trivial. */
  4445. if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
  4446. for (fmode = GET_MODE (from); fmode != VOIDmode;
  4447. fmode = GET_MODE_WIDER_MODE (fmode))
  4448. if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
  4449. && (!DECIMAL_FLOAT_MODE_P (fmode)
  4450. || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
  4451. {
  4452. int bitsize;
  4453. REAL_VALUE_TYPE offset;
  4454. rtx limit;
  4455. rtx_code_label *lab1, *lab2;
  4456. rtx_insn *insn;
  4457. bitsize = GET_MODE_PRECISION (GET_MODE (to));
  4458. real_2expN (&offset, bitsize - 1, fmode);
  4459. limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
  4460. lab1 = gen_label_rtx ();
  4461. lab2 = gen_label_rtx ();
  4462. if (fmode != GET_MODE (from))
  4463. from = convert_to_mode (fmode, from, 0);
  4464. /* See if we need to do the subtraction. */
  4465. do_pending_stack_adjust ();
  4466. emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
  4467. 0, lab1);
  4468. /* If not, do the signed "fix" and branch around fixup code. */
  4469. expand_fix (to, from, 0);
  4470. emit_jump_insn (gen_jump (lab2));
  4471. emit_barrier ();
  4472. /* Otherwise, subtract 2**(N-1), convert to signed number,
  4473. then add 2**(N-1). Do the addition using XOR since this
  4474. will often generate better code. */
  4475. emit_label (lab1);
  4476. target = expand_binop (GET_MODE (from), sub_optab, from, limit,
  4477. NULL_RTX, 0, OPTAB_LIB_WIDEN);
  4478. expand_fix (to, target, 0);
  4479. target = expand_binop (GET_MODE (to), xor_optab, to,
  4480. gen_int_mode
  4481. ((HOST_WIDE_INT) 1 << (bitsize - 1),
  4482. GET_MODE (to)),
  4483. to, 1, OPTAB_LIB_WIDEN);
  4484. if (target != to)
  4485. emit_move_insn (to, target);
  4486. emit_label (lab2);
  4487. if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
  4488. {
  4489. /* Make a place for a REG_NOTE and add it. */
  4490. insn = emit_move_insn (to, to);
  4491. set_dst_reg_note (insn, REG_EQUAL,
  4492. gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
  4493. copy_rtx (from)),
  4494. to);
  4495. }
  4496. return;
  4497. }
  4498. /* We can't do it with an insn, so use a library call. But first ensure
  4499. that the mode of TO is at least as wide as SImode, since those are the
  4500. only library calls we know about. */
  4501. if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
  4502. {
  4503. target = gen_reg_rtx (SImode);
  4504. expand_fix (target, from, unsignedp);
  4505. }
  4506. else
  4507. {
  4508. rtx_insn *insns;
  4509. rtx value;
  4510. rtx libfunc;
  4511. convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
  4512. libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
  4513. gcc_assert (libfunc);
  4514. start_sequence ();
  4515. value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
  4516. GET_MODE (to), 1, from,
  4517. GET_MODE (from));
  4518. insns = get_insns ();
  4519. end_sequence ();
  4520. emit_libcall_block (insns, target, value,
  4521. gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
  4522. GET_MODE (to), from));
  4523. }
  4524. if (target != to)
  4525. {
  4526. if (GET_MODE (to) == GET_MODE (target))
  4527. emit_move_insn (to, target);
  4528. else
  4529. convert_move (to, target, 0);
  4530. }
  4531. }
  4532. /* Generate code to convert FROM or TO a fixed-point.
  4533. If UINTP is true, either TO or FROM is an unsigned integer.
  4534. If SATP is true, we need to saturate the result. */
  4535. void
  4536. expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
  4537. {
  4538. machine_mode to_mode = GET_MODE (to);
  4539. machine_mode from_mode = GET_MODE (from);
  4540. convert_optab tab;
  4541. enum rtx_code this_code;
  4542. enum insn_code code;
  4543. rtx_insn *insns;
  4544. rtx value;
  4545. rtx libfunc;
  4546. if (to_mode == from_mode)
  4547. {
  4548. emit_move_insn (to, from);
  4549. return;
  4550. }
  4551. if (uintp)
  4552. {
  4553. tab = satp ? satfractuns_optab : fractuns_optab;
  4554. this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
  4555. }
  4556. else
  4557. {
  4558. tab = satp ? satfract_optab : fract_optab;
  4559. this_code = satp ? SAT_FRACT : FRACT_CONVERT;
  4560. }
  4561. code = convert_optab_handler (tab, to_mode, from_mode);
  4562. if (code != CODE_FOR_nothing)
  4563. {
  4564. emit_unop_insn (code, to, from, this_code);
  4565. return;
  4566. }
  4567. libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
  4568. gcc_assert (libfunc);
  4569. start_sequence ();
  4570. value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
  4571. 1, from, from_mode);
  4572. insns = get_insns ();
  4573. end_sequence ();
  4574. emit_libcall_block (insns, to, value,
  4575. gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
  4576. }
  4577. /* Generate code to convert FROM to fixed point and store in TO. FROM
  4578. must be floating point, TO must be signed. Use the conversion optab
  4579. TAB to do the conversion. */
  4580. bool
  4581. expand_sfix_optab (rtx to, rtx from, convert_optab tab)
  4582. {
  4583. enum insn_code icode;
  4584. rtx target = to;
  4585. machine_mode fmode, imode;
  4586. /* We first try to find a pair of modes, one real and one integer, at
  4587. least as wide as FROM and TO, respectively, in which we can open-code
  4588. this conversion. If the integer mode is wider than the mode of TO,
  4589. we can do the conversion either signed or unsigned. */
  4590. for (fmode = GET_MODE (from); fmode != VOIDmode;
  4591. fmode = GET_MODE_WIDER_MODE (fmode))
  4592. for (imode = GET_MODE (to); imode != VOIDmode;
  4593. imode = GET_MODE_WIDER_MODE (imode))
  4594. {
  4595. icode = convert_optab_handler (tab, imode, fmode);
  4596. if (icode != CODE_FOR_nothing)
  4597. {
  4598. rtx_insn *last = get_last_insn ();
  4599. if (fmode != GET_MODE (from))
  4600. from = convert_to_mode (fmode, from, 0);
  4601. if (imode != GET_MODE (to))
  4602. target = gen_reg_rtx (imode);
  4603. if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
  4604. {
  4605. delete_insns_since (last);
  4606. continue;
  4607. }
  4608. if (target != to)
  4609. convert_move (to, target, 0);
  4610. return true;
  4611. }
  4612. }
  4613. return false;
  4614. }
  4615. /* Report whether we have an instruction to perform the operation
  4616. specified by CODE on operands of mode MODE. */
  4617. int
  4618. have_insn_for (enum rtx_code code, machine_mode mode)
  4619. {
  4620. return (code_to_optab (code)
  4621. && (optab_handler (code_to_optab (code), mode)
  4622. != CODE_FOR_nothing));
  4623. }
  4624. /* Initialize the libfunc fields of an entire group of entries in some
  4625. optab. Each entry is set equal to a string consisting of a leading
  4626. pair of underscores followed by a generic operation name followed by
  4627. a mode name (downshifted to lowercase) followed by a single character
  4628. representing the number of operands for the given operation (which is
  4629. usually one of the characters '2', '3', or '4').
  4630. OPTABLE is the table in which libfunc fields are to be initialized.
  4631. OPNAME is the generic (string) name of the operation.
  4632. SUFFIX is the character which specifies the number of operands for
  4633. the given generic operation.
  4634. MODE is the mode to generate for.
  4635. */
  4636. static void
  4637. gen_libfunc (optab optable, const char *opname, int suffix,
  4638. machine_mode mode)
  4639. {
  4640. unsigned opname_len = strlen (opname);
  4641. const char *mname = GET_MODE_NAME (mode);
  4642. unsigned mname_len = strlen (mname);
  4643. int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
  4644. int len = prefix_len + opname_len + mname_len + 1 + 1;
  4645. char *libfunc_name = XALLOCAVEC (char, len);
  4646. char *p;
  4647. const char *q;
  4648. p = libfunc_name;
  4649. *p++ = '_';
  4650. *p++ = '_';
  4651. if (targetm.libfunc_gnu_prefix)
  4652. {
  4653. *p++ = 'g';
  4654. *p++ = 'n';
  4655. *p++ = 'u';
  4656. *p++ = '_';
  4657. }
  4658. for (q = opname; *q; )
  4659. *p++ = *q++;
  4660. for (q = mname; *q; q++)
  4661. *p++ = TOLOWER (*q);
  4662. *p++ = suffix;
  4663. *p = '\0';
  4664. set_optab_libfunc (optable, mode,
  4665. ggc_alloc_string (libfunc_name, p - libfunc_name));
  4666. }
  4667. /* Like gen_libfunc, but verify that integer operation is involved. */
  4668. void
  4669. gen_int_libfunc (optab optable, const char *opname, char suffix,
  4670. machine_mode mode)
  4671. {
  4672. int maxsize = 2 * BITS_PER_WORD;
  4673. int minsize = BITS_PER_WORD;
  4674. if (GET_MODE_CLASS (mode) != MODE_INT)
  4675. return;
  4676. if (maxsize < LONG_LONG_TYPE_SIZE)
  4677. maxsize = LONG_LONG_TYPE_SIZE;
  4678. if (minsize > INT_TYPE_SIZE
  4679. && (trapv_binoptab_p (optable)
  4680. || trapv_unoptab_p (optable)))
  4681. minsize = INT_TYPE_SIZE;
  4682. if (GET_MODE_BITSIZE (mode) < minsize
  4683. || GET_MODE_BITSIZE (mode) > maxsize)
  4684. return;
  4685. gen_libfunc (optable, opname, suffix, mode);
  4686. }
  4687. /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
  4688. void
  4689. gen_fp_libfunc (optab optable, const char *opname, char suffix,
  4690. machine_mode mode)
  4691. {
  4692. char *dec_opname;
  4693. if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  4694. gen_libfunc (optable, opname, suffix, mode);
  4695. if (DECIMAL_FLOAT_MODE_P (mode))
  4696. {
  4697. dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
  4698. /* For BID support, change the name to have either a bid_ or dpd_ prefix
  4699. depending on the low level floating format used. */
  4700. memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
  4701. strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
  4702. gen_libfunc (optable, dec_opname, suffix, mode);
  4703. }
  4704. }
  4705. /* Like gen_libfunc, but verify that fixed-point operation is involved. */
  4706. void
  4707. gen_fixed_libfunc (optab optable, const char *opname, char suffix,
  4708. machine_mode mode)
  4709. {
  4710. if (!ALL_FIXED_POINT_MODE_P (mode))
  4711. return;
  4712. gen_libfunc (optable, opname, suffix, mode);
  4713. }
  4714. /* Like gen_libfunc, but verify that signed fixed-point operation is
  4715. involved. */
  4716. void
  4717. gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
  4718. machine_mode mode)
  4719. {
  4720. if (!SIGNED_FIXED_POINT_MODE_P (mode))
  4721. return;
  4722. gen_libfunc (optable, opname, suffix, mode);
  4723. }
  4724. /* Like gen_libfunc, but verify that unsigned fixed-point operation is
  4725. involved. */
  4726. void
  4727. gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
  4728. machine_mode mode)
  4729. {
  4730. if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
  4731. return;
  4732. gen_libfunc (optable, opname, suffix, mode);
  4733. }
  4734. /* Like gen_libfunc, but verify that FP or INT operation is involved. */
  4735. void
  4736. gen_int_fp_libfunc (optab optable, const char *name, char suffix,
  4737. machine_mode mode)
  4738. {
  4739. if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
  4740. gen_fp_libfunc (optable, name, suffix, mode);
  4741. if (INTEGRAL_MODE_P (mode))
  4742. gen_int_libfunc (optable, name, suffix, mode);
  4743. }
  4744. /* Like gen_libfunc, but verify that FP or INT operation is involved
  4745. and add 'v' suffix for integer operation. */
  4746. void
  4747. gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
  4748. machine_mode mode)
  4749. {
  4750. if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
  4751. gen_fp_libfunc (optable, name, suffix, mode);
  4752. if (GET_MODE_CLASS (mode) == MODE_INT)
  4753. {
  4754. int len = strlen (name);
  4755. char *v_name = XALLOCAVEC (char, len + 2);
  4756. strcpy (v_name, name);
  4757. v_name[len] = 'v';
  4758. v_name[len + 1] = 0;
  4759. gen_int_libfunc (optable, v_name, suffix, mode);
  4760. }
  4761. }
  4762. /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
  4763. involved. */
  4764. void
  4765. gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
  4766. machine_mode mode)
  4767. {
  4768. if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
  4769. gen_fp_libfunc (optable, name, suffix, mode);
  4770. if (INTEGRAL_MODE_P (mode))
  4771. gen_int_libfunc (optable, name, suffix, mode);
  4772. if (ALL_FIXED_POINT_MODE_P (mode))
  4773. gen_fixed_libfunc (optable, name, suffix, mode);
  4774. }
  4775. /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
  4776. involved. */
  4777. void
  4778. gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
  4779. machine_mode mode)
  4780. {
  4781. if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
  4782. gen_fp_libfunc (optable, name, suffix, mode);
  4783. if (INTEGRAL_MODE_P (mode))
  4784. gen_int_libfunc (optable, name, suffix, mode);
  4785. if (SIGNED_FIXED_POINT_MODE_P (mode))
  4786. gen_signed_fixed_libfunc (optable, name, suffix, mode);
  4787. }
  4788. /* Like gen_libfunc, but verify that INT or FIXED operation is
  4789. involved. */
  4790. void
  4791. gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
  4792. machine_mode mode)
  4793. {
  4794. if (INTEGRAL_MODE_P (mode))
  4795. gen_int_libfunc (optable, name, suffix, mode);
  4796. if (ALL_FIXED_POINT_MODE_P (mode))
  4797. gen_fixed_libfunc (optable, name, suffix, mode);
  4798. }
  4799. /* Like gen_libfunc, but verify that INT or signed FIXED operation is
  4800. involved. */
  4801. void
  4802. gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
  4803. machine_mode mode)
  4804. {
  4805. if (INTEGRAL_MODE_P (mode))
  4806. gen_int_libfunc (optable, name, suffix, mode);
  4807. if (SIGNED_FIXED_POINT_MODE_P (mode))
  4808. gen_signed_fixed_libfunc (optable, name, suffix, mode);
  4809. }
  4810. /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
  4811. involved. */
  4812. void
  4813. gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
  4814. machine_mode mode)
  4815. {
  4816. if (INTEGRAL_MODE_P (mode))
  4817. gen_int_libfunc (optable, name, suffix, mode);
  4818. if (UNSIGNED_FIXED_POINT_MODE_P (mode))
  4819. gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
  4820. }
  4821. /* Initialize the libfunc fields of an entire group of entries of an
  4822. inter-mode-class conversion optab. The string formation rules are
  4823. similar to the ones for init_libfuncs, above, but instead of having
  4824. a mode name and an operand count these functions have two mode names
  4825. and no operand count. */
  4826. void
  4827. gen_interclass_conv_libfunc (convert_optab tab,
  4828. const char *opname,
  4829. machine_mode tmode,
  4830. machine_mode fmode)
  4831. {
  4832. size_t opname_len = strlen (opname);
  4833. size_t mname_len = 0;
  4834. const char *fname, *tname;
  4835. const char *q;
  4836. int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
  4837. char *libfunc_name, *suffix;
  4838. char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
  4839. char *p;
  4840. /* If this is a decimal conversion, add the current BID vs. DPD prefix that
  4841. depends on which underlying decimal floating point format is used. */
  4842. const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
  4843. mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
  4844. nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
  4845. nondec_name[0] = '_';
  4846. nondec_name[1] = '_';
  4847. if (targetm.libfunc_gnu_prefix)
  4848. {
  4849. nondec_name[2] = 'g';
  4850. nondec_name[3] = 'n';
  4851. nondec_name[4] = 'u';
  4852. nondec_name[5] = '_';
  4853. }
  4854. memcpy (&nondec_name[prefix_len], opname, opname_len);
  4855. nondec_suffix = nondec_name + opname_len + prefix_len;
  4856. dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
  4857. dec_name[0] = '_';
  4858. dec_name[1] = '_';
  4859. memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
  4860. memcpy (&dec_name[2+dec_len], opname, opname_len);
  4861. dec_suffix = dec_name + dec_len + opname_len + 2;
  4862. fname = GET_MODE_NAME (fmode);
  4863. tname = GET_MODE_NAME (tmode);
  4864. if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
  4865. {
  4866. libfunc_name = dec_name;
  4867. suffix = dec_suffix;
  4868. }
  4869. else
  4870. {
  4871. libfunc_name = nondec_name;
  4872. suffix = nondec_suffix;
  4873. }
  4874. p = suffix;
  4875. for (q = fname; *q; p++, q++)
  4876. *p = TOLOWER (*q);
  4877. for (q = tname; *q; p++, q++)
  4878. *p = TOLOWER (*q);
  4879. *p = '\0';
  4880. set_conv_libfunc (tab, tmode, fmode,
  4881. ggc_alloc_string (libfunc_name, p - libfunc_name));
  4882. }
  4883. /* Same as gen_interclass_conv_libfunc but verify that we are producing
  4884. int->fp conversion. */
  4885. void
  4886. gen_int_to_fp_conv_libfunc (convert_optab tab,
  4887. const char *opname,
  4888. machine_mode tmode,
  4889. machine_mode fmode)
  4890. {
  4891. if (GET_MODE_CLASS (fmode) != MODE_INT)
  4892. return;
  4893. if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
  4894. return;
  4895. gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
  4896. }
  4897. /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
  4898. naming scheme. */
  4899. void
  4900. gen_ufloat_conv_libfunc (convert_optab tab,
  4901. const char *opname ATTRIBUTE_UNUSED,
  4902. machine_mode tmode,
  4903. machine_mode fmode)
  4904. {
  4905. if (DECIMAL_FLOAT_MODE_P (tmode))
  4906. gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
  4907. else
  4908. gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
  4909. }
  4910. /* Same as gen_interclass_conv_libfunc but verify that we are producing
  4911. fp->int conversion. */
  4912. void
  4913. gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
  4914. const char *opname,
  4915. machine_mode tmode,
  4916. machine_mode fmode)
  4917. {
  4918. if (GET_MODE_CLASS (fmode) != MODE_INT)
  4919. return;
  4920. if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
  4921. return;
  4922. gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
  4923. }
  4924. /* Same as gen_interclass_conv_libfunc but verify that we are producing
  4925. fp->int conversion with no decimal floating point involved. */
  4926. void
  4927. gen_fp_to_int_conv_libfunc (convert_optab tab,
  4928. const char *opname,
  4929. machine_mode tmode,
  4930. machine_mode fmode)
  4931. {
  4932. if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
  4933. return;
  4934. if (GET_MODE_CLASS (tmode) != MODE_INT)
  4935. return;
  4936. gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
  4937. }
  4938. /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
  4939. The string formation rules are
  4940. similar to the ones for init_libfunc, above. */
  4941. void
  4942. gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
  4943. machine_mode tmode, machine_mode fmode)
  4944. {
  4945. size_t opname_len = strlen (opname);
  4946. size_t mname_len = 0;
  4947. const char *fname, *tname;
  4948. const char *q;
  4949. int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
  4950. char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
  4951. char *libfunc_name, *suffix;
  4952. char *p;
  4953. /* If this is a decimal conversion, add the current BID vs. DPD prefix that
  4954. depends on which underlying decimal floating point format is used. */
  4955. const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
  4956. mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
  4957. nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
  4958. nondec_name[0] = '_';
  4959. nondec_name[1] = '_';
  4960. if (targetm.libfunc_gnu_prefix)
  4961. {
  4962. nondec_name[2] = 'g';
  4963. nondec_name[3] = 'n';
  4964. nondec_name[4] = 'u';
  4965. nondec_name[5] = '_';
  4966. }
  4967. memcpy (&nondec_name[prefix_len], opname, opname_len);
  4968. nondec_suffix = nondec_name + opname_len + prefix_len;
  4969. dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
  4970. dec_name[0] = '_';
  4971. dec_name[1] = '_';
  4972. memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
  4973. memcpy (&dec_name[2 + dec_len], opname, opname_len);
  4974. dec_suffix = dec_name + dec_len + opname_len + 2;
  4975. fname = GET_MODE_NAME (fmode);
  4976. tname = GET_MODE_NAME (tmode);
  4977. if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
  4978. {
  4979. libfunc_name = dec_name;
  4980. suffix = dec_suffix;
  4981. }
  4982. else
  4983. {
  4984. libfunc_name = nondec_name;
  4985. suffix = nondec_suffix;
  4986. }
  4987. p = suffix;
  4988. for (q = fname; *q; p++, q++)
  4989. *p = TOLOWER (*q);
  4990. for (q = tname; *q; p++, q++)
  4991. *p = TOLOWER (*q);
  4992. *p++ = '2';
  4993. *p = '\0';
  4994. set_conv_libfunc (tab, tmode, fmode,
  4995. ggc_alloc_string (libfunc_name, p - libfunc_name));
  4996. }
  4997. /* Pick proper libcall for trunc_optab. We need to chose if we do
  4998. truncation or extension and interclass or intraclass. */
  4999. void
  5000. gen_trunc_conv_libfunc (convert_optab tab,
  5001. const char *opname,
  5002. machine_mode tmode,
  5003. machine_mode fmode)
  5004. {
  5005. if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
  5006. return;
  5007. if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
  5008. return;
  5009. if (tmode == fmode)
  5010. return;
  5011. if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
  5012. || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
  5013. gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
  5014. if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
  5015. return;
  5016. if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
  5017. && GET_MODE_CLASS (fmode) == MODE_FLOAT)
  5018. || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
  5019. gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
  5020. }
  5021. /* Pick proper libcall for extend_optab. We need to chose if we do
  5022. truncation or extension and interclass or intraclass. */
  5023. void
  5024. gen_extend_conv_libfunc (convert_optab tab,
  5025. const char *opname ATTRIBUTE_UNUSED,
  5026. machine_mode tmode,
  5027. machine_mode fmode)
  5028. {
  5029. if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
  5030. return;
  5031. if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
  5032. return;
  5033. if (tmode == fmode)
  5034. return;
  5035. if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
  5036. || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
  5037. gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
  5038. if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
  5039. return;
  5040. if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
  5041. && GET_MODE_CLASS (fmode) == MODE_FLOAT)
  5042. || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
  5043. gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
  5044. }
  5045. /* Pick proper libcall for fract_optab. We need to chose if we do
  5046. interclass or intraclass. */
  5047. void
  5048. gen_fract_conv_libfunc (convert_optab tab,
  5049. const char *opname,
  5050. machine_mode tmode,
  5051. machine_mode fmode)
  5052. {
  5053. if (tmode == fmode)
  5054. return;
  5055. if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
  5056. return;
  5057. if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
  5058. gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
  5059. else
  5060. gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
  5061. }
  5062. /* Pick proper libcall for fractuns_optab. */
  5063. void
  5064. gen_fractuns_conv_libfunc (convert_optab tab,
  5065. const char *opname,
  5066. machine_mode tmode,
  5067. machine_mode fmode)
  5068. {
  5069. if (tmode == fmode)
  5070. return;
  5071. /* One mode must be a fixed-point mode, and the other must be an integer
  5072. mode. */
  5073. if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
  5074. || (ALL_FIXED_POINT_MODE_P (fmode)
  5075. && GET_MODE_CLASS (tmode) == MODE_INT)))
  5076. return;
  5077. gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
  5078. }
  5079. /* Pick proper libcall for satfract_optab. We need to chose if we do
  5080. interclass or intraclass. */
  5081. void
  5082. gen_satfract_conv_libfunc (convert_optab tab,
  5083. const char *opname,
  5084. machine_mode tmode,
  5085. machine_mode fmode)
  5086. {
  5087. if (tmode == fmode)
  5088. return;
  5089. /* TMODE must be a fixed-point mode. */
  5090. if (!ALL_FIXED_POINT_MODE_P (tmode))
  5091. return;
  5092. if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
  5093. gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
  5094. else
  5095. gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
  5096. }
  5097. /* Pick proper libcall for satfractuns_optab. */
  5098. void
  5099. gen_satfractuns_conv_libfunc (convert_optab tab,
  5100. const char *opname,
  5101. machine_mode tmode,
  5102. machine_mode fmode)
  5103. {
  5104. if (tmode == fmode)
  5105. return;
  5106. /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
  5107. if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
  5108. return;
  5109. gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
  5110. }
  5111. /* Hashtable callbacks for libfunc_decls. */
  5112. struct libfunc_decl_hasher : ggc_hasher<tree>
  5113. {
  5114. static hashval_t
  5115. hash (tree entry)
  5116. {
  5117. return IDENTIFIER_HASH_VALUE (DECL_NAME (entry));
  5118. }
  5119. static bool
  5120. equal (tree decl, tree name)
  5121. {
  5122. return DECL_NAME (decl) == name;
  5123. }
  5124. };
  5125. /* A table of previously-created libfuncs, hashed by name. */
  5126. static GTY (()) hash_table<libfunc_decl_hasher> *libfunc_decls;
  5127. /* Build a decl for a libfunc named NAME. */
  5128. tree
  5129. build_libfunc_function (const char *name)
  5130. {
  5131. tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
  5132. get_identifier (name),
  5133. build_function_type (integer_type_node, NULL_TREE));
  5134. /* ??? We don't have any type information except for this is
  5135. a function. Pretend this is "int foo()". */
  5136. DECL_ARTIFICIAL (decl) = 1;
  5137. DECL_EXTERNAL (decl) = 1;
  5138. TREE_PUBLIC (decl) = 1;
  5139. gcc_assert (DECL_ASSEMBLER_NAME (decl));
  5140. /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
  5141. are the flags assigned by targetm.encode_section_info. */
  5142. SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
  5143. return decl;
  5144. }
  5145. rtx
  5146. init_one_libfunc (const char *name)
  5147. {
  5148. tree id, decl;
  5149. hashval_t hash;
  5150. if (libfunc_decls == NULL)
  5151. libfunc_decls = hash_table<libfunc_decl_hasher>::create_ggc (37);
  5152. /* See if we have already created a libfunc decl for this function. */
  5153. id = get_identifier (name);
  5154. hash = IDENTIFIER_HASH_VALUE (id);
  5155. tree *slot = libfunc_decls->find_slot_with_hash (id, hash, INSERT);
  5156. decl = *slot;
  5157. if (decl == NULL)
  5158. {
  5159. /* Create a new decl, so that it can be passed to
  5160. targetm.encode_section_info. */
  5161. decl = build_libfunc_function (name);
  5162. *slot = decl;
  5163. }
  5164. return XEXP (DECL_RTL (decl), 0);
  5165. }
  5166. /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
  5167. rtx
  5168. set_user_assembler_libfunc (const char *name, const char *asmspec)
  5169. {
  5170. tree id, decl;
  5171. hashval_t hash;
  5172. id = get_identifier (name);
  5173. hash = IDENTIFIER_HASH_VALUE (id);
  5174. tree *slot = libfunc_decls->find_slot_with_hash (id, hash, NO_INSERT);
  5175. gcc_assert (slot);
  5176. decl = (tree) *slot;
  5177. set_user_assembler_name (decl, asmspec);
  5178. return XEXP (DECL_RTL (decl), 0);
  5179. }
  5180. /* Call this to reset the function entry for one optab (OPTABLE) in mode
  5181. MODE to NAME, which should be either 0 or a string constant. */
  5182. void
  5183. set_optab_libfunc (optab op, machine_mode mode, const char *name)
  5184. {
  5185. rtx val;
  5186. struct libfunc_entry e;
  5187. struct libfunc_entry **slot;
  5188. e.op = op;
  5189. e.mode1 = mode;
  5190. e.mode2 = VOIDmode;
  5191. if (name)
  5192. val = init_one_libfunc (name);
  5193. else
  5194. val = 0;
  5195. slot = libfunc_hash->find_slot (&e, INSERT);
  5196. if (*slot == NULL)
  5197. *slot = ggc_alloc<libfunc_entry> ();
  5198. (*slot)->op = op;
  5199. (*slot)->mode1 = mode;
  5200. (*slot)->mode2 = VOIDmode;
  5201. (*slot)->libfunc = val;
  5202. }
  5203. /* Call this to reset the function entry for one conversion optab
  5204. (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
  5205. either 0 or a string constant. */
  5206. void
  5207. set_conv_libfunc (convert_optab optab, machine_mode tmode,
  5208. machine_mode fmode, const char *name)
  5209. {
  5210. rtx val;
  5211. struct libfunc_entry e;
  5212. struct libfunc_entry **slot;
  5213. e.op = optab;
  5214. e.mode1 = tmode;
  5215. e.mode2 = fmode;
  5216. if (name)
  5217. val = init_one_libfunc (name);
  5218. else
  5219. val = 0;
  5220. slot = libfunc_hash->find_slot (&e, INSERT);
  5221. if (*slot == NULL)
  5222. *slot = ggc_alloc<libfunc_entry> ();
  5223. (*slot)->op = optab;
  5224. (*slot)->mode1 = tmode;
  5225. (*slot)->mode2 = fmode;
  5226. (*slot)->libfunc = val;
  5227. }
  5228. /* Call this to initialize the contents of the optabs
  5229. appropriately for the current target machine. */
  5230. void
  5231. init_optabs (void)
  5232. {
  5233. if (libfunc_hash)
  5234. libfunc_hash->empty ();
  5235. else
  5236. libfunc_hash = hash_table<libfunc_hasher>::create_ggc (10);
  5237. /* Fill in the optabs with the insns we support. */
  5238. init_all_optabs (this_fn_optabs);
  5239. /* The ffs function operates on `int'. Fall back on it if we do not
  5240. have a libgcc2 function for that width. */
  5241. if (INT_TYPE_SIZE < BITS_PER_WORD)
  5242. set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
  5243. "ffs");
  5244. /* Explicitly initialize the bswap libfuncs since we need them to be
  5245. valid for things other than word_mode. */
  5246. if (targetm.libfunc_gnu_prefix)
  5247. {
  5248. set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
  5249. set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
  5250. }
  5251. else
  5252. {
  5253. set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
  5254. set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
  5255. }
  5256. /* Use cabs for double complex abs, since systems generally have cabs.
  5257. Don't define any libcall for float complex, so that cabs will be used. */
  5258. if (complex_double_type_node)
  5259. set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
  5260. "cabs");
  5261. abort_libfunc = init_one_libfunc ("abort");
  5262. memcpy_libfunc = init_one_libfunc ("memcpy");
  5263. memmove_libfunc = init_one_libfunc ("memmove");
  5264. memcmp_libfunc = init_one_libfunc ("memcmp");
  5265. memset_libfunc = init_one_libfunc ("memset");
  5266. setbits_libfunc = init_one_libfunc ("__setbits");
  5267. #ifndef DONT_USE_BUILTIN_SETJMP
  5268. setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
  5269. longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
  5270. #else
  5271. setjmp_libfunc = init_one_libfunc ("setjmp");
  5272. longjmp_libfunc = init_one_libfunc ("longjmp");
  5273. #endif
  5274. unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
  5275. unwind_sjlj_unregister_libfunc
  5276. = init_one_libfunc ("_Unwind_SjLj_Unregister");
  5277. /* For function entry/exit instrumentation. */
  5278. profile_function_entry_libfunc
  5279. = init_one_libfunc ("__cyg_profile_func_enter");
  5280. profile_function_exit_libfunc
  5281. = init_one_libfunc ("__cyg_profile_func_exit");
  5282. gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
  5283. /* Allow the target to add more libcalls or rename some, etc. */
  5284. targetm.init_libfuncs ();
  5285. }
  5286. /* Use the current target and options to initialize
  5287. TREE_OPTIMIZATION_OPTABS (OPTNODE). */
  5288. void
  5289. init_tree_optimization_optabs (tree optnode)
  5290. {
  5291. /* Quick exit if we have already computed optabs for this target. */
  5292. if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
  5293. return;
  5294. /* Forget any previous information and set up for the current target. */
  5295. TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
  5296. struct target_optabs *tmp_optabs = (struct target_optabs *)
  5297. TREE_OPTIMIZATION_OPTABS (optnode);
  5298. if (tmp_optabs)
  5299. memset (tmp_optabs, 0, sizeof (struct target_optabs));
  5300. else
  5301. tmp_optabs = ggc_alloc<target_optabs> ();
  5302. /* Generate a new set of optabs into tmp_optabs. */
  5303. init_all_optabs (tmp_optabs);
  5304. /* If the optabs changed, record it. */
  5305. if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
  5306. TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
  5307. else
  5308. {
  5309. TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
  5310. ggc_free (tmp_optabs);
  5311. }
  5312. }
  5313. /* A helper function for init_sync_libfuncs. Using the basename BASE,
  5314. install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
  5315. static void
  5316. init_sync_libfuncs_1 (optab tab, const char *base, int max)
  5317. {
  5318. machine_mode mode;
  5319. char buf[64];
  5320. size_t len = strlen (base);
  5321. int i;
  5322. gcc_assert (max <= 8);
  5323. gcc_assert (len + 3 < sizeof (buf));
  5324. memcpy (buf, base, len);
  5325. buf[len] = '_';
  5326. buf[len + 1] = '0';
  5327. buf[len + 2] = '\0';
  5328. mode = QImode;
  5329. for (i = 1; i <= max; i *= 2)
  5330. {
  5331. buf[len + 1] = '0' + i;
  5332. set_optab_libfunc (tab, mode, buf);
  5333. mode = GET_MODE_2XWIDER_MODE (mode);
  5334. }
  5335. }
  5336. void
  5337. init_sync_libfuncs (int max)
  5338. {
  5339. if (!flag_sync_libcalls)
  5340. return;
  5341. init_sync_libfuncs_1 (sync_compare_and_swap_optab,
  5342. "__sync_val_compare_and_swap", max);
  5343. init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
  5344. "__sync_lock_test_and_set", max);
  5345. init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
  5346. init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
  5347. init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
  5348. init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
  5349. init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
  5350. init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
  5351. init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
  5352. init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
  5353. init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
  5354. init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
  5355. init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
  5356. init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
  5357. }
  5358. /* Print information about the current contents of the optabs on
  5359. STDERR. */
  5360. DEBUG_FUNCTION void
  5361. debug_optab_libfuncs (void)
  5362. {
  5363. int i, j, k;
  5364. /* Dump the arithmetic optabs. */
  5365. for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
  5366. for (j = 0; j < NUM_MACHINE_MODES; ++j)
  5367. {
  5368. rtx l = optab_libfunc ((optab) i, (machine_mode) j);
  5369. if (l)
  5370. {
  5371. gcc_assert (GET_CODE (l) == SYMBOL_REF);
  5372. fprintf (stderr, "%s\t%s:\t%s\n",
  5373. GET_RTX_NAME (optab_to_code ((optab) i)),
  5374. GET_MODE_NAME (j),
  5375. XSTR (l, 0));
  5376. }
  5377. }
  5378. /* Dump the conversion optabs. */
  5379. for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
  5380. for (j = 0; j < NUM_MACHINE_MODES; ++j)
  5381. for (k = 0; k < NUM_MACHINE_MODES; ++k)
  5382. {
  5383. rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
  5384. (machine_mode) k);
  5385. if (l)
  5386. {
  5387. gcc_assert (GET_CODE (l) == SYMBOL_REF);
  5388. fprintf (stderr, "%s\t%s\t%s:\t%s\n",
  5389. GET_RTX_NAME (optab_to_code ((optab) i)),
  5390. GET_MODE_NAME (j),
  5391. GET_MODE_NAME (k),
  5392. XSTR (l, 0));
  5393. }
  5394. }
  5395. }
  5396. /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
  5397. CODE. Return 0 on failure. */
  5398. rtx
  5399. gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
  5400. {
  5401. machine_mode mode = GET_MODE (op1);
  5402. enum insn_code icode;
  5403. rtx insn;
  5404. rtx trap_rtx;
  5405. if (mode == VOIDmode)
  5406. return 0;
  5407. icode = optab_handler (ctrap_optab, mode);
  5408. if (icode == CODE_FOR_nothing)
  5409. return 0;
  5410. /* Some targets only accept a zero trap code. */
  5411. if (!insn_operand_matches (icode, 3, tcode))
  5412. return 0;
  5413. do_pending_stack_adjust ();
  5414. start_sequence ();
  5415. prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
  5416. &trap_rtx, &mode);
  5417. if (!trap_rtx)
  5418. insn = NULL_RTX;
  5419. else
  5420. insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
  5421. tcode);
  5422. /* If that failed, then give up. */
  5423. if (insn == 0)
  5424. {
  5425. end_sequence ();
  5426. return 0;
  5427. }
  5428. emit_insn (insn);
  5429. insn = get_insns ();
  5430. end_sequence ();
  5431. return insn;
  5432. }
  5433. /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
  5434. or unsigned operation code. */
  5435. enum rtx_code
  5436. get_rtx_code (enum tree_code tcode, bool unsignedp)
  5437. {
  5438. enum rtx_code code;
  5439. switch (tcode)
  5440. {
  5441. case EQ_EXPR:
  5442. code = EQ;
  5443. break;
  5444. case NE_EXPR:
  5445. code = NE;
  5446. break;
  5447. case LT_EXPR:
  5448. code = unsignedp ? LTU : LT;
  5449. break;
  5450. case LE_EXPR:
  5451. code = unsignedp ? LEU : LE;
  5452. break;
  5453. case GT_EXPR:
  5454. code = unsignedp ? GTU : GT;
  5455. break;
  5456. case GE_EXPR:
  5457. code = unsignedp ? GEU : GE;
  5458. break;
  5459. case UNORDERED_EXPR:
  5460. code = UNORDERED;
  5461. break;
  5462. case ORDERED_EXPR:
  5463. code = ORDERED;
  5464. break;
  5465. case UNLT_EXPR:
  5466. code = UNLT;
  5467. break;
  5468. case UNLE_EXPR:
  5469. code = UNLE;
  5470. break;
  5471. case UNGT_EXPR:
  5472. code = UNGT;
  5473. break;
  5474. case UNGE_EXPR:
  5475. code = UNGE;
  5476. break;
  5477. case UNEQ_EXPR:
  5478. code = UNEQ;
  5479. break;
  5480. case LTGT_EXPR:
  5481. code = LTGT;
  5482. break;
  5483. case BIT_AND_EXPR:
  5484. code = AND;
  5485. break;
  5486. case BIT_IOR_EXPR:
  5487. code = IOR;
  5488. break;
  5489. default:
  5490. gcc_unreachable ();
  5491. }
  5492. return code;
  5493. }
  5494. /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
  5495. unsigned operators. Do not generate compare instruction. */
  5496. static rtx
  5497. vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
  5498. bool unsignedp, enum insn_code icode)
  5499. {
  5500. struct expand_operand ops[2];
  5501. rtx rtx_op0, rtx_op1;
  5502. enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
  5503. gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
  5504. /* Expand operands. */
  5505. rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
  5506. EXPAND_STACK_PARM);
  5507. rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
  5508. EXPAND_STACK_PARM);
  5509. create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
  5510. create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
  5511. if (!maybe_legitimize_operands (icode, 4, 2, ops))
  5512. gcc_unreachable ();
  5513. return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
  5514. }
  5515. /* Return true if VEC_PERM_EXPR of arbitrary input vectors can be expanded using
  5516. SIMD extensions of the CPU. SEL may be NULL, which stands for an unknown
  5517. constant. Note that additional permutations representing whole-vector shifts
  5518. may also be handled via the vec_shr optab, but only where the second input
  5519. vector is entirely constant zeroes; this case is not dealt with here. */
  5520. bool
  5521. can_vec_perm_p (machine_mode mode, bool variable,
  5522. const unsigned char *sel)
  5523. {
  5524. machine_mode qimode;
  5525. /* If the target doesn't implement a vector mode for the vector type,
  5526. then no operations are supported. */
  5527. if (!VECTOR_MODE_P (mode))
  5528. return false;
  5529. if (!variable)
  5530. {
  5531. if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
  5532. && (sel == NULL
  5533. || targetm.vectorize.vec_perm_const_ok == NULL
  5534. || targetm.vectorize.vec_perm_const_ok (mode, sel)))
  5535. return true;
  5536. }
  5537. if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
  5538. return true;
  5539. /* We allow fallback to a QI vector mode, and adjust the mask. */
  5540. if (GET_MODE_INNER (mode) == QImode)
  5541. return false;
  5542. qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
  5543. if (!VECTOR_MODE_P (qimode))
  5544. return false;
  5545. /* ??? For completeness, we ought to check the QImode version of
  5546. vec_perm_const_optab. But all users of this implicit lowering
  5547. feature implement the variable vec_perm_optab. */
  5548. if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
  5549. return false;
  5550. /* In order to support the lowering of variable permutations,
  5551. we need to support shifts and adds. */
  5552. if (variable)
  5553. {
  5554. if (GET_MODE_UNIT_SIZE (mode) > 2
  5555. && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
  5556. && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
  5557. return false;
  5558. if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
  5559. return false;
  5560. }
  5561. return true;
  5562. }
  5563. /* Checks if vec_perm mask SEL is a constant equivalent to a shift of the first
  5564. vec_perm operand, assuming the second operand is a constant vector of zeroes.
  5565. Return the shift distance in bits if so, or NULL_RTX if the vec_perm is not a
  5566. shift. */
  5567. static rtx
  5568. shift_amt_for_vec_perm_mask (rtx sel)
  5569. {
  5570. unsigned int i, first, nelt = GET_MODE_NUNITS (GET_MODE (sel));
  5571. unsigned int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (sel)));
  5572. if (GET_CODE (sel) != CONST_VECTOR)
  5573. return NULL_RTX;
  5574. first = INTVAL (CONST_VECTOR_ELT (sel, 0));
  5575. if (first >= 2*nelt)
  5576. return NULL_RTX;
  5577. for (i = 1; i < nelt; i++)
  5578. {
  5579. int idx = INTVAL (CONST_VECTOR_ELT (sel, i));
  5580. unsigned int expected = (i + first) & (2 * nelt - 1);
  5581. /* Indices into the second vector are all equivalent. */
  5582. if (idx < 0 || (MIN (nelt, (unsigned) idx) != MIN (nelt, expected)))
  5583. return NULL_RTX;
  5584. }
  5585. return GEN_INT (first * bitsize);
  5586. }
  5587. /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
  5588. static rtx
  5589. expand_vec_perm_1 (enum insn_code icode, rtx target,
  5590. rtx v0, rtx v1, rtx sel)
  5591. {
  5592. machine_mode tmode = GET_MODE (target);
  5593. machine_mode smode = GET_MODE (sel);
  5594. struct expand_operand ops[4];
  5595. create_output_operand (&ops[0], target, tmode);
  5596. create_input_operand (&ops[3], sel, smode);
  5597. /* Make an effort to preserve v0 == v1. The target expander is able to
  5598. rely on this to determine if we're permuting a single input operand. */
  5599. if (rtx_equal_p (v0, v1))
  5600. {
  5601. if (!insn_operand_matches (icode, 1, v0))
  5602. v0 = force_reg (tmode, v0);
  5603. gcc_checking_assert (insn_operand_matches (icode, 1, v0));
  5604. gcc_checking_assert (insn_operand_matches (icode, 2, v0));
  5605. create_fixed_operand (&ops[1], v0);
  5606. create_fixed_operand (&ops[2], v0);
  5607. }
  5608. else
  5609. {
  5610. create_input_operand (&ops[1], v0, tmode);
  5611. /* See if this can be handled with a vec_shr. We only do this if the
  5612. second vector is all zeroes. */
  5613. enum insn_code shift_code = optab_handler (vec_shr_optab, GET_MODE (v0));
  5614. if (v1 == CONST0_RTX (GET_MODE (v1)) && shift_code)
  5615. if (rtx shift_amt = shift_amt_for_vec_perm_mask (sel))
  5616. {
  5617. create_convert_operand_from_type (&ops[2], shift_amt,
  5618. sizetype_tab[(int) stk_sizetype]);
  5619. if (maybe_expand_insn (shift_code, 3, ops))
  5620. return ops[0].value;
  5621. }
  5622. create_input_operand (&ops[2], v1, tmode);
  5623. }
  5624. if (maybe_expand_insn (icode, 4, ops))
  5625. return ops[0].value;
  5626. return NULL_RTX;
  5627. }
  5628. /* Generate instructions for vec_perm optab given its mode
  5629. and three operands. */
  5630. rtx
  5631. expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
  5632. {
  5633. enum insn_code icode;
  5634. machine_mode qimode;
  5635. unsigned int i, w, e, u;
  5636. rtx tmp, sel_qi = NULL;
  5637. rtvec vec;
  5638. if (!target || GET_MODE (target) != mode)
  5639. target = gen_reg_rtx (mode);
  5640. w = GET_MODE_SIZE (mode);
  5641. e = GET_MODE_NUNITS (mode);
  5642. u = GET_MODE_UNIT_SIZE (mode);
  5643. /* Set QIMODE to a different vector mode with byte elements.
  5644. If no such mode, or if MODE already has byte elements, use VOIDmode. */
  5645. qimode = VOIDmode;
  5646. if (GET_MODE_INNER (mode) != QImode)
  5647. {
  5648. qimode = mode_for_vector (QImode, w);
  5649. if (!VECTOR_MODE_P (qimode))
  5650. qimode = VOIDmode;
  5651. }
  5652. /* If the input is a constant, expand it specially. */
  5653. gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
  5654. if (GET_CODE (sel) == CONST_VECTOR)
  5655. {
  5656. icode = direct_optab_handler (vec_perm_const_optab, mode);
  5657. if (icode != CODE_FOR_nothing)
  5658. {
  5659. tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
  5660. if (tmp)
  5661. return tmp;
  5662. }
  5663. /* Fall back to a constant byte-based permutation. */
  5664. if (qimode != VOIDmode)
  5665. {
  5666. vec = rtvec_alloc (w);
  5667. for (i = 0; i < e; ++i)
  5668. {
  5669. unsigned int j, this_e;
  5670. this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
  5671. this_e &= 2 * e - 1;
  5672. this_e *= u;
  5673. for (j = 0; j < u; ++j)
  5674. RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
  5675. }
  5676. sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
  5677. icode = direct_optab_handler (vec_perm_const_optab, qimode);
  5678. if (icode != CODE_FOR_nothing)
  5679. {
  5680. tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
  5681. tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
  5682. gen_lowpart (qimode, v1), sel_qi);
  5683. if (tmp)
  5684. return gen_lowpart (mode, tmp);
  5685. }
  5686. }
  5687. }
  5688. /* Otherwise expand as a fully variable permuation. */
  5689. icode = direct_optab_handler (vec_perm_optab, mode);
  5690. if (icode != CODE_FOR_nothing)
  5691. {
  5692. tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
  5693. if (tmp)
  5694. return tmp;
  5695. }
  5696. /* As a special case to aid several targets, lower the element-based
  5697. permutation to a byte-based permutation and try again. */
  5698. if (qimode == VOIDmode)
  5699. return NULL_RTX;
  5700. icode = direct_optab_handler (vec_perm_optab, qimode);
  5701. if (icode == CODE_FOR_nothing)
  5702. return NULL_RTX;
  5703. if (sel_qi == NULL)
  5704. {
  5705. /* Multiply each element by its byte size. */
  5706. machine_mode selmode = GET_MODE (sel);
  5707. if (u == 2)
  5708. sel = expand_simple_binop (selmode, PLUS, sel, sel,
  5709. NULL, 0, OPTAB_DIRECT);
  5710. else
  5711. sel = expand_simple_binop (selmode, ASHIFT, sel,
  5712. GEN_INT (exact_log2 (u)),
  5713. NULL, 0, OPTAB_DIRECT);
  5714. gcc_assert (sel != NULL);
  5715. /* Broadcast the low byte each element into each of its bytes. */
  5716. vec = rtvec_alloc (w);
  5717. for (i = 0; i < w; ++i)
  5718. {
  5719. int this_e = i / u * u;
  5720. if (BYTES_BIG_ENDIAN)
  5721. this_e += u - 1;
  5722. RTVEC_ELT (vec, i) = GEN_INT (this_e);
  5723. }
  5724. tmp = gen_rtx_CONST_VECTOR (qimode, vec);
  5725. sel = gen_lowpart (qimode, sel);
  5726. sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
  5727. gcc_assert (sel != NULL);
  5728. /* Add the byte offset to each byte element. */
  5729. /* Note that the definition of the indicies here is memory ordering,
  5730. so there should be no difference between big and little endian. */
  5731. vec = rtvec_alloc (w);
  5732. for (i = 0; i < w; ++i)
  5733. RTVEC_ELT (vec, i) = GEN_INT (i % u);
  5734. tmp = gen_rtx_CONST_VECTOR (qimode, vec);
  5735. sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
  5736. sel, 0, OPTAB_DIRECT);
  5737. gcc_assert (sel_qi != NULL);
  5738. }
  5739. tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
  5740. tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
  5741. gen_lowpart (qimode, v1), sel_qi);
  5742. if (tmp)
  5743. tmp = gen_lowpart (mode, tmp);
  5744. return tmp;
  5745. }
  5746. /* Return insn code for a conditional operator with a comparison in
  5747. mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
  5748. static inline enum insn_code
  5749. get_vcond_icode (machine_mode vmode, machine_mode cmode, bool uns)
  5750. {
  5751. enum insn_code icode = CODE_FOR_nothing;
  5752. if (uns)
  5753. icode = convert_optab_handler (vcondu_optab, vmode, cmode);
  5754. else
  5755. icode = convert_optab_handler (vcond_optab, vmode, cmode);
  5756. return icode;
  5757. }
  5758. /* Return TRUE iff, appropriate vector insns are available
  5759. for vector cond expr with vector type VALUE_TYPE and a comparison
  5760. with operand vector types in CMP_OP_TYPE. */
  5761. bool
  5762. expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
  5763. {
  5764. machine_mode value_mode = TYPE_MODE (value_type);
  5765. machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
  5766. if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
  5767. || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
  5768. || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
  5769. TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
  5770. return false;
  5771. return true;
  5772. }
  5773. /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
  5774. three operands. */
  5775. rtx
  5776. expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
  5777. rtx target)
  5778. {
  5779. struct expand_operand ops[6];
  5780. enum insn_code icode;
  5781. rtx comparison, rtx_op1, rtx_op2;
  5782. machine_mode mode = TYPE_MODE (vec_cond_type);
  5783. machine_mode cmp_op_mode;
  5784. bool unsignedp;
  5785. tree op0a, op0b;
  5786. enum tree_code tcode;
  5787. if (COMPARISON_CLASS_P (op0))
  5788. {
  5789. op0a = TREE_OPERAND (op0, 0);
  5790. op0b = TREE_OPERAND (op0, 1);
  5791. tcode = TREE_CODE (op0);
  5792. }
  5793. else
  5794. {
  5795. /* Fake op0 < 0. */
  5796. gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
  5797. op0a = op0;
  5798. op0b = build_zero_cst (TREE_TYPE (op0));
  5799. tcode = LT_EXPR;
  5800. }
  5801. unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
  5802. cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
  5803. gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
  5804. && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
  5805. icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
  5806. if (icode == CODE_FOR_nothing)
  5807. return 0;
  5808. comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
  5809. rtx_op1 = expand_normal (op1);
  5810. rtx_op2 = expand_normal (op2);
  5811. create_output_operand (&ops[0], target, mode);
  5812. create_input_operand (&ops[1], rtx_op1, mode);
  5813. create_input_operand (&ops[2], rtx_op2, mode);
  5814. create_fixed_operand (&ops[3], comparison);
  5815. create_fixed_operand (&ops[4], XEXP (comparison, 0));
  5816. create_fixed_operand (&ops[5], XEXP (comparison, 1));
  5817. expand_insn (icode, 6, ops);
  5818. return ops[0].value;
  5819. }
  5820. /* Return non-zero if a highpart multiply is supported of can be synthisized.
  5821. For the benefit of expand_mult_highpart, the return value is 1 for direct,
  5822. 2 for even/odd widening, and 3 for hi/lo widening. */
  5823. int
  5824. can_mult_highpart_p (machine_mode mode, bool uns_p)
  5825. {
  5826. optab op;
  5827. unsigned char *sel;
  5828. unsigned i, nunits;
  5829. op = uns_p ? umul_highpart_optab : smul_highpart_optab;
  5830. if (optab_handler (op, mode) != CODE_FOR_nothing)
  5831. return 1;
  5832. /* If the mode is an integral vector, synth from widening operations. */
  5833. if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
  5834. return 0;
  5835. nunits = GET_MODE_NUNITS (mode);
  5836. sel = XALLOCAVEC (unsigned char, nunits);
  5837. op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
  5838. if (optab_handler (op, mode) != CODE_FOR_nothing)
  5839. {
  5840. op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
  5841. if (optab_handler (op, mode) != CODE_FOR_nothing)
  5842. {
  5843. for (i = 0; i < nunits; ++i)
  5844. sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
  5845. if (can_vec_perm_p (mode, false, sel))
  5846. return 2;
  5847. }
  5848. }
  5849. op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
  5850. if (optab_handler (op, mode) != CODE_FOR_nothing)
  5851. {
  5852. op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
  5853. if (optab_handler (op, mode) != CODE_FOR_nothing)
  5854. {
  5855. for (i = 0; i < nunits; ++i)
  5856. sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
  5857. if (can_vec_perm_p (mode, false, sel))
  5858. return 3;
  5859. }
  5860. }
  5861. return 0;
  5862. }
  5863. /* Expand a highpart multiply. */
  5864. rtx
  5865. expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
  5866. rtx target, bool uns_p)
  5867. {
  5868. struct expand_operand eops[3];
  5869. enum insn_code icode;
  5870. int method, i, nunits;
  5871. machine_mode wmode;
  5872. rtx m1, m2, perm;
  5873. optab tab1, tab2;
  5874. rtvec v;
  5875. method = can_mult_highpart_p (mode, uns_p);
  5876. switch (method)
  5877. {
  5878. case 0:
  5879. return NULL_RTX;
  5880. case 1:
  5881. tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
  5882. return expand_binop (mode, tab1, op0, op1, target, uns_p,
  5883. OPTAB_LIB_WIDEN);
  5884. case 2:
  5885. tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
  5886. tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
  5887. break;
  5888. case 3:
  5889. tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
  5890. tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
  5891. if (BYTES_BIG_ENDIAN)
  5892. {
  5893. optab t = tab1;
  5894. tab1 = tab2;
  5895. tab2 = t;
  5896. }
  5897. break;
  5898. default:
  5899. gcc_unreachable ();
  5900. }
  5901. icode = optab_handler (tab1, mode);
  5902. nunits = GET_MODE_NUNITS (mode);
  5903. wmode = insn_data[icode].operand[0].mode;
  5904. gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
  5905. gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
  5906. create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
  5907. create_input_operand (&eops[1], op0, mode);
  5908. create_input_operand (&eops[2], op1, mode);
  5909. expand_insn (icode, 3, eops);
  5910. m1 = gen_lowpart (mode, eops[0].value);
  5911. create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
  5912. create_input_operand (&eops[1], op0, mode);
  5913. create_input_operand (&eops[2], op1, mode);
  5914. expand_insn (optab_handler (tab2, mode), 3, eops);
  5915. m2 = gen_lowpart (mode, eops[0].value);
  5916. v = rtvec_alloc (nunits);
  5917. if (method == 2)
  5918. {
  5919. for (i = 0; i < nunits; ++i)
  5920. RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
  5921. + ((i & 1) ? nunits : 0));
  5922. }
  5923. else
  5924. {
  5925. for (i = 0; i < nunits; ++i)
  5926. RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
  5927. }
  5928. perm = gen_rtx_CONST_VECTOR (mode, v);
  5929. return expand_vec_perm (mode, m1, m2, perm, target);
  5930. }
  5931. /* Return true if target supports vector masked load/store for mode. */
  5932. bool
  5933. can_vec_mask_load_store_p (machine_mode mode, bool is_load)
  5934. {
  5935. optab op = is_load ? maskload_optab : maskstore_optab;
  5936. machine_mode vmode;
  5937. unsigned int vector_sizes;
  5938. /* If mode is vector mode, check it directly. */
  5939. if (VECTOR_MODE_P (mode))
  5940. return optab_handler (op, mode) != CODE_FOR_nothing;
  5941. /* Otherwise, return true if there is some vector mode with
  5942. the mask load/store supported. */
  5943. /* See if there is any chance the mask load or store might be
  5944. vectorized. If not, punt. */
  5945. vmode = targetm.vectorize.preferred_simd_mode (mode);
  5946. if (!VECTOR_MODE_P (vmode))
  5947. return false;
  5948. if (optab_handler (op, vmode) != CODE_FOR_nothing)
  5949. return true;
  5950. vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
  5951. while (vector_sizes != 0)
  5952. {
  5953. unsigned int cur = 1 << floor_log2 (vector_sizes);
  5954. vector_sizes &= ~cur;
  5955. if (cur <= GET_MODE_SIZE (mode))
  5956. continue;
  5957. vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
  5958. if (VECTOR_MODE_P (vmode)
  5959. && optab_handler (op, vmode) != CODE_FOR_nothing)
  5960. return true;
  5961. }
  5962. return false;
  5963. }
  5964. /* Return true if there is a compare_and_swap pattern. */
  5965. bool
  5966. can_compare_and_swap_p (machine_mode mode, bool allow_libcall)
  5967. {
  5968. enum insn_code icode;
  5969. /* Check for __atomic_compare_and_swap. */
  5970. icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
  5971. if (icode != CODE_FOR_nothing)
  5972. return true;
  5973. /* Check for __sync_compare_and_swap. */
  5974. icode = optab_handler (sync_compare_and_swap_optab, mode);
  5975. if (icode != CODE_FOR_nothing)
  5976. return true;
  5977. if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
  5978. return true;
  5979. /* No inline compare and swap. */
  5980. return false;
  5981. }
  5982. /* Return true if an atomic exchange can be performed. */
  5983. bool
  5984. can_atomic_exchange_p (machine_mode mode, bool allow_libcall)
  5985. {
  5986. enum insn_code icode;
  5987. /* Check for __atomic_exchange. */
  5988. icode = direct_optab_handler (atomic_exchange_optab, mode);
  5989. if (icode != CODE_FOR_nothing)
  5990. return true;
  5991. /* Don't check __sync_test_and_set, as on some platforms that
  5992. has reduced functionality. Targets that really do support
  5993. a proper exchange should simply be updated to the __atomics. */
  5994. return can_compare_and_swap_p (mode, allow_libcall);
  5995. }
  5996. /* Helper function to find the MODE_CC set in a sync_compare_and_swap
  5997. pattern. */
  5998. static void
  5999. find_cc_set (rtx x, const_rtx pat, void *data)
  6000. {
  6001. if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
  6002. && GET_CODE (pat) == SET)
  6003. {
  6004. rtx *p_cc_reg = (rtx *) data;
  6005. gcc_assert (!*p_cc_reg);
  6006. *p_cc_reg = x;
  6007. }
  6008. }
  6009. /* This is a helper function for the other atomic operations. This function
  6010. emits a loop that contains SEQ that iterates until a compare-and-swap
  6011. operation at the end succeeds. MEM is the memory to be modified. SEQ is
  6012. a set of instructions that takes a value from OLD_REG as an input and
  6013. produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
  6014. set to the current contents of MEM. After SEQ, a compare-and-swap will
  6015. attempt to update MEM with NEW_REG. The function returns true when the
  6016. loop was generated successfully. */
  6017. static bool
  6018. expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
  6019. {
  6020. machine_mode mode = GET_MODE (mem);
  6021. rtx_code_label *label;
  6022. rtx cmp_reg, success, oldval;
  6023. /* The loop we want to generate looks like
  6024. cmp_reg = mem;
  6025. label:
  6026. old_reg = cmp_reg;
  6027. seq;
  6028. (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
  6029. if (success)
  6030. goto label;
  6031. Note that we only do the plain load from memory once. Subsequent
  6032. iterations use the value loaded by the compare-and-swap pattern. */
  6033. label = gen_label_rtx ();
  6034. cmp_reg = gen_reg_rtx (mode);
  6035. emit_move_insn (cmp_reg, mem);
  6036. emit_label (label);
  6037. emit_move_insn (old_reg, cmp_reg);
  6038. if (seq)
  6039. emit_insn (seq);
  6040. success = NULL_RTX;
  6041. oldval = cmp_reg;
  6042. if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
  6043. new_reg, false, MEMMODEL_SEQ_CST,
  6044. MEMMODEL_RELAXED))
  6045. return false;
  6046. if (oldval != cmp_reg)
  6047. emit_move_insn (cmp_reg, oldval);
  6048. /* Mark this jump predicted not taken. */
  6049. emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
  6050. GET_MODE (success), 1, label, 0);
  6051. return true;
  6052. }
  6053. /* This function tries to emit an atomic_exchange intruction. VAL is written
  6054. to *MEM using memory model MODEL. The previous contents of *MEM are returned,
  6055. using TARGET if possible. */
  6056. static rtx
  6057. maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
  6058. {
  6059. machine_mode mode = GET_MODE (mem);
  6060. enum insn_code icode;
  6061. /* If the target supports the exchange directly, great. */
  6062. icode = direct_optab_handler (atomic_exchange_optab, mode);
  6063. if (icode != CODE_FOR_nothing)
  6064. {
  6065. struct expand_operand ops[4];
  6066. create_output_operand (&ops[0], target, mode);
  6067. create_fixed_operand (&ops[1], mem);
  6068. create_input_operand (&ops[2], val, mode);
  6069. create_integer_operand (&ops[3], model);
  6070. if (maybe_expand_insn (icode, 4, ops))
  6071. return ops[0].value;
  6072. }
  6073. return NULL_RTX;
  6074. }
  6075. /* This function tries to implement an atomic exchange operation using
  6076. __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
  6077. The previous contents of *MEM are returned, using TARGET if possible.
  6078. Since this instructionn is an acquire barrier only, stronger memory
  6079. models may require additional barriers to be emitted. */
  6080. static rtx
  6081. maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
  6082. enum memmodel model)
  6083. {
  6084. machine_mode mode = GET_MODE (mem);
  6085. enum insn_code icode;
  6086. rtx_insn *last_insn = get_last_insn ();
  6087. icode = optab_handler (sync_lock_test_and_set_optab, mode);
  6088. /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
  6089. exists, and the memory model is stronger than acquire, add a release
  6090. barrier before the instruction. */
  6091. if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST
  6092. || (model & MEMMODEL_MASK) == MEMMODEL_RELEASE
  6093. || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
  6094. expand_mem_thread_fence (model);
  6095. if (icode != CODE_FOR_nothing)
  6096. {
  6097. struct expand_operand ops[3];
  6098. create_output_operand (&ops[0], target, mode);
  6099. create_fixed_operand (&ops[1], mem);
  6100. create_input_operand (&ops[2], val, mode);
  6101. if (maybe_expand_insn (icode, 3, ops))
  6102. return ops[0].value;
  6103. }
  6104. /* If an external test-and-set libcall is provided, use that instead of
  6105. any external compare-and-swap that we might get from the compare-and-
  6106. swap-loop expansion later. */
  6107. if (!can_compare_and_swap_p (mode, false))
  6108. {
  6109. rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
  6110. if (libfunc != NULL)
  6111. {
  6112. rtx addr;
  6113. addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
  6114. return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
  6115. mode, 2, addr, ptr_mode,
  6116. val, mode);
  6117. }
  6118. }
  6119. /* If the test_and_set can't be emitted, eliminate any barrier that might
  6120. have been emitted. */
  6121. delete_insns_since (last_insn);
  6122. return NULL_RTX;
  6123. }
  6124. /* This function tries to implement an atomic exchange operation using a
  6125. compare_and_swap loop. VAL is written to *MEM. The previous contents of
  6126. *MEM are returned, using TARGET if possible. No memory model is required
  6127. since a compare_and_swap loop is seq-cst. */
  6128. static rtx
  6129. maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
  6130. {
  6131. machine_mode mode = GET_MODE (mem);
  6132. if (can_compare_and_swap_p (mode, true))
  6133. {
  6134. if (!target || !register_operand (target, mode))
  6135. target = gen_reg_rtx (mode);
  6136. if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
  6137. return target;
  6138. }
  6139. return NULL_RTX;
  6140. }
  6141. /* This function tries to implement an atomic test-and-set operation
  6142. using the atomic_test_and_set instruction pattern. A boolean value
  6143. is returned from the operation, using TARGET if possible. */
  6144. #ifndef HAVE_atomic_test_and_set
  6145. #define HAVE_atomic_test_and_set 0
  6146. #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
  6147. #endif
  6148. static rtx
  6149. maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
  6150. {
  6151. machine_mode pat_bool_mode;
  6152. struct expand_operand ops[3];
  6153. if (!HAVE_atomic_test_and_set)
  6154. return NULL_RTX;
  6155. /* While we always get QImode from __atomic_test_and_set, we get
  6156. other memory modes from __sync_lock_test_and_set. Note that we
  6157. use no endian adjustment here. This matches the 4.6 behavior
  6158. in the Sparc backend. */
  6159. gcc_checking_assert
  6160. (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
  6161. if (GET_MODE (mem) != QImode)
  6162. mem = adjust_address_nv (mem, QImode, 0);
  6163. pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
  6164. create_output_operand (&ops[0], target, pat_bool_mode);
  6165. create_fixed_operand (&ops[1], mem);
  6166. create_integer_operand (&ops[2], model);
  6167. if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
  6168. return ops[0].value;
  6169. return NULL_RTX;
  6170. }
  6171. /* This function expands the legacy _sync_lock test_and_set operation which is
  6172. generally an atomic exchange. Some limited targets only allow the
  6173. constant 1 to be stored. This is an ACQUIRE operation.
  6174. TARGET is an optional place to stick the return value.
  6175. MEM is where VAL is stored. */
  6176. rtx
  6177. expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
  6178. {
  6179. rtx ret;
  6180. /* Try an atomic_exchange first. */
  6181. ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
  6182. if (ret)
  6183. return ret;
  6184. ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
  6185. if (ret)
  6186. return ret;
  6187. ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
  6188. if (ret)
  6189. return ret;
  6190. /* If there are no other options, try atomic_test_and_set if the value
  6191. being stored is 1. */
  6192. if (val == const1_rtx)
  6193. ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
  6194. return ret;
  6195. }
  6196. /* This function expands the atomic test_and_set operation:
  6197. atomically store a boolean TRUE into MEM and return the previous value.
  6198. MEMMODEL is the memory model variant to use.
  6199. TARGET is an optional place to stick the return value. */
  6200. rtx
  6201. expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
  6202. {
  6203. machine_mode mode = GET_MODE (mem);
  6204. rtx ret, trueval, subtarget;
  6205. ret = maybe_emit_atomic_test_and_set (target, mem, model);
  6206. if (ret)
  6207. return ret;
  6208. /* Be binary compatible with non-default settings of trueval, and different
  6209. cpu revisions. E.g. one revision may have atomic-test-and-set, but
  6210. another only has atomic-exchange. */
  6211. if (targetm.atomic_test_and_set_trueval == 1)
  6212. {
  6213. trueval = const1_rtx;
  6214. subtarget = target ? target : gen_reg_rtx (mode);
  6215. }
  6216. else
  6217. {
  6218. trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
  6219. subtarget = gen_reg_rtx (mode);
  6220. }
  6221. /* Try the atomic-exchange optab... */
  6222. ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
  6223. /* ... then an atomic-compare-and-swap loop ... */
  6224. if (!ret)
  6225. ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
  6226. /* ... before trying the vaguely defined legacy lock_test_and_set. */
  6227. if (!ret)
  6228. ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
  6229. /* Recall that the legacy lock_test_and_set optab was allowed to do magic
  6230. things with the value 1. Thus we try again without trueval. */
  6231. if (!ret && targetm.atomic_test_and_set_trueval != 1)
  6232. ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
  6233. /* Failing all else, assume a single threaded environment and simply
  6234. perform the operation. */
  6235. if (!ret)
  6236. {
  6237. /* If the result is ignored skip the move to target. */
  6238. if (subtarget != const0_rtx)
  6239. emit_move_insn (subtarget, mem);
  6240. emit_move_insn (mem, trueval);
  6241. ret = subtarget;
  6242. }
  6243. /* Recall that have to return a boolean value; rectify if trueval
  6244. is not exactly one. */
  6245. if (targetm.atomic_test_and_set_trueval != 1)
  6246. ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
  6247. return ret;
  6248. }
  6249. /* This function expands the atomic exchange operation:
  6250. atomically store VAL in MEM and return the previous value in MEM.
  6251. MEMMODEL is the memory model variant to use.
  6252. TARGET is an optional place to stick the return value. */
  6253. rtx
  6254. expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
  6255. {
  6256. rtx ret;
  6257. ret = maybe_emit_atomic_exchange (target, mem, val, model);
  6258. /* Next try a compare-and-swap loop for the exchange. */
  6259. if (!ret)
  6260. ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
  6261. return ret;
  6262. }
  6263. /* This function expands the atomic compare exchange operation:
  6264. *PTARGET_BOOL is an optional place to store the boolean success/failure.
  6265. *PTARGET_OVAL is an optional place to store the old value from memory.
  6266. Both target parameters may be NULL to indicate that we do not care about
  6267. that return value. Both target parameters are updated on success to
  6268. the actual location of the corresponding result.
  6269. MEMMODEL is the memory model variant to use.
  6270. The return value of the function is true for success. */
  6271. bool
  6272. expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
  6273. rtx mem, rtx expected, rtx desired,
  6274. bool is_weak, enum memmodel succ_model,
  6275. enum memmodel fail_model)
  6276. {
  6277. machine_mode mode = GET_MODE (mem);
  6278. struct expand_operand ops[8];
  6279. enum insn_code icode;
  6280. rtx target_oval, target_bool = NULL_RTX;
  6281. rtx libfunc;
  6282. /* Load expected into a register for the compare and swap. */
  6283. if (MEM_P (expected))
  6284. expected = copy_to_reg (expected);
  6285. /* Make sure we always have some place to put the return oldval.
  6286. Further, make sure that place is distinct from the input expected,
  6287. just in case we need that path down below. */
  6288. if (ptarget_oval == NULL
  6289. || (target_oval = *ptarget_oval) == NULL
  6290. || reg_overlap_mentioned_p (expected, target_oval))
  6291. target_oval = gen_reg_rtx (mode);
  6292. icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
  6293. if (icode != CODE_FOR_nothing)
  6294. {
  6295. machine_mode bool_mode = insn_data[icode].operand[0].mode;
  6296. /* Make sure we always have a place for the bool operand. */
  6297. if (ptarget_bool == NULL
  6298. || (target_bool = *ptarget_bool) == NULL
  6299. || GET_MODE (target_bool) != bool_mode)
  6300. target_bool = gen_reg_rtx (bool_mode);
  6301. /* Emit the compare_and_swap. */
  6302. create_output_operand (&ops[0], target_bool, bool_mode);
  6303. create_output_operand (&ops[1], target_oval, mode);
  6304. create_fixed_operand (&ops[2], mem);
  6305. create_input_operand (&ops[3], expected, mode);
  6306. create_input_operand (&ops[4], desired, mode);
  6307. create_integer_operand (&ops[5], is_weak);
  6308. create_integer_operand (&ops[6], succ_model);
  6309. create_integer_operand (&ops[7], fail_model);
  6310. if (maybe_expand_insn (icode, 8, ops))
  6311. {
  6312. /* Return success/failure. */
  6313. target_bool = ops[0].value;
  6314. target_oval = ops[1].value;
  6315. goto success;
  6316. }
  6317. }
  6318. /* Otherwise fall back to the original __sync_val_compare_and_swap
  6319. which is always seq-cst. */
  6320. icode = optab_handler (sync_compare_and_swap_optab, mode);
  6321. if (icode != CODE_FOR_nothing)
  6322. {
  6323. rtx cc_reg;
  6324. create_output_operand (&ops[0], target_oval, mode);
  6325. create_fixed_operand (&ops[1], mem);
  6326. create_input_operand (&ops[2], expected, mode);
  6327. create_input_operand (&ops[3], desired, mode);
  6328. if (!maybe_expand_insn (icode, 4, ops))
  6329. return false;
  6330. target_oval = ops[0].value;
  6331. /* If the caller isn't interested in the boolean return value,
  6332. skip the computation of it. */
  6333. if (ptarget_bool == NULL)
  6334. goto success;
  6335. /* Otherwise, work out if the compare-and-swap succeeded. */
  6336. cc_reg = NULL_RTX;
  6337. if (have_insn_for (COMPARE, CCmode))
  6338. note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
  6339. if (cc_reg)
  6340. {
  6341. target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
  6342. const0_rtx, VOIDmode, 0, 1);
  6343. goto success;
  6344. }
  6345. goto success_bool_from_val;
  6346. }
  6347. /* Also check for library support for __sync_val_compare_and_swap. */
  6348. libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
  6349. if (libfunc != NULL)
  6350. {
  6351. rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
  6352. target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
  6353. mode, 3, addr, ptr_mode,
  6354. expected, mode, desired, mode);
  6355. /* Compute the boolean return value only if requested. */
  6356. if (ptarget_bool)
  6357. goto success_bool_from_val;
  6358. else
  6359. goto success;
  6360. }
  6361. /* Failure. */
  6362. return false;
  6363. success_bool_from_val:
  6364. target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
  6365. expected, VOIDmode, 1, 1);
  6366. success:
  6367. /* Make sure that the oval output winds up where the caller asked. */
  6368. if (ptarget_oval)
  6369. *ptarget_oval = target_oval;
  6370. if (ptarget_bool)
  6371. *ptarget_bool = target_bool;
  6372. return true;
  6373. }
  6374. /* Generate asm volatile("" : : : "memory") as the memory barrier. */
  6375. static void
  6376. expand_asm_memory_barrier (void)
  6377. {
  6378. rtx asm_op, clob;
  6379. asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
  6380. rtvec_alloc (0), rtvec_alloc (0),
  6381. rtvec_alloc (0), UNKNOWN_LOCATION);
  6382. MEM_VOLATILE_P (asm_op) = 1;
  6383. clob = gen_rtx_SCRATCH (VOIDmode);
  6384. clob = gen_rtx_MEM (BLKmode, clob);
  6385. clob = gen_rtx_CLOBBER (VOIDmode, clob);
  6386. emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
  6387. }
  6388. /* This routine will either emit the mem_thread_fence pattern or issue a
  6389. sync_synchronize to generate a fence for memory model MEMMODEL. */
  6390. #ifndef HAVE_mem_thread_fence
  6391. # define HAVE_mem_thread_fence 0
  6392. # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
  6393. #endif
  6394. #ifndef HAVE_memory_barrier
  6395. # define HAVE_memory_barrier 0
  6396. # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
  6397. #endif
  6398. void
  6399. expand_mem_thread_fence (enum memmodel model)
  6400. {
  6401. if (HAVE_mem_thread_fence)
  6402. emit_insn (gen_mem_thread_fence (GEN_INT (model)));
  6403. else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
  6404. {
  6405. if (HAVE_memory_barrier)
  6406. emit_insn (gen_memory_barrier ());
  6407. else if (synchronize_libfunc != NULL_RTX)
  6408. emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
  6409. else
  6410. expand_asm_memory_barrier ();
  6411. }
  6412. }
  6413. /* This routine will either emit the mem_signal_fence pattern or issue a
  6414. sync_synchronize to generate a fence for memory model MEMMODEL. */
  6415. #ifndef HAVE_mem_signal_fence
  6416. # define HAVE_mem_signal_fence 0
  6417. # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
  6418. #endif
  6419. void
  6420. expand_mem_signal_fence (enum memmodel model)
  6421. {
  6422. if (HAVE_mem_signal_fence)
  6423. emit_insn (gen_mem_signal_fence (GEN_INT (model)));
  6424. else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
  6425. {
  6426. /* By default targets are coherent between a thread and the signal
  6427. handler running on the same thread. Thus this really becomes a
  6428. compiler barrier, in that stores must not be sunk past
  6429. (or raised above) a given point. */
  6430. expand_asm_memory_barrier ();
  6431. }
  6432. }
  6433. /* This function expands the atomic load operation:
  6434. return the atomically loaded value in MEM.
  6435. MEMMODEL is the memory model variant to use.
  6436. TARGET is an option place to stick the return value. */
  6437. rtx
  6438. expand_atomic_load (rtx target, rtx mem, enum memmodel model)
  6439. {
  6440. machine_mode mode = GET_MODE (mem);
  6441. enum insn_code icode;
  6442. /* If the target supports the load directly, great. */
  6443. icode = direct_optab_handler (atomic_load_optab, mode);
  6444. if (icode != CODE_FOR_nothing)
  6445. {
  6446. struct expand_operand ops[3];
  6447. create_output_operand (&ops[0], target, mode);
  6448. create_fixed_operand (&ops[1], mem);
  6449. create_integer_operand (&ops[2], model);
  6450. if (maybe_expand_insn (icode, 3, ops))
  6451. return ops[0].value;
  6452. }
  6453. /* If the size of the object is greater than word size on this target,
  6454. then we assume that a load will not be atomic. */
  6455. if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
  6456. {
  6457. /* Issue val = compare_and_swap (mem, 0, 0).
  6458. This may cause the occasional harmless store of 0 when the value is
  6459. already 0, but it seems to be OK according to the standards guys. */
  6460. if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
  6461. const0_rtx, false, model, model))
  6462. return target;
  6463. else
  6464. /* Otherwise there is no atomic load, leave the library call. */
  6465. return NULL_RTX;
  6466. }
  6467. /* Otherwise assume loads are atomic, and emit the proper barriers. */
  6468. if (!target || target == const0_rtx)
  6469. target = gen_reg_rtx (mode);
  6470. /* For SEQ_CST, emit a barrier before the load. */
  6471. if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
  6472. expand_mem_thread_fence (model);
  6473. emit_move_insn (target, mem);
  6474. /* Emit the appropriate barrier after the load. */
  6475. expand_mem_thread_fence (model);
  6476. return target;
  6477. }
  6478. /* This function expands the atomic store operation:
  6479. Atomically store VAL in MEM.
  6480. MEMMODEL is the memory model variant to use.
  6481. USE_RELEASE is true if __sync_lock_release can be used as a fall back.
  6482. function returns const0_rtx if a pattern was emitted. */
  6483. rtx
  6484. expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
  6485. {
  6486. machine_mode mode = GET_MODE (mem);
  6487. enum insn_code icode;
  6488. struct expand_operand ops[3];
  6489. /* If the target supports the store directly, great. */
  6490. icode = direct_optab_handler (atomic_store_optab, mode);
  6491. if (icode != CODE_FOR_nothing)
  6492. {
  6493. create_fixed_operand (&ops[0], mem);
  6494. create_input_operand (&ops[1], val, mode);
  6495. create_integer_operand (&ops[2], model);
  6496. if (maybe_expand_insn (icode, 3, ops))
  6497. return const0_rtx;
  6498. }
  6499. /* If using __sync_lock_release is a viable alternative, try it. */
  6500. if (use_release)
  6501. {
  6502. icode = direct_optab_handler (sync_lock_release_optab, mode);
  6503. if (icode != CODE_FOR_nothing)
  6504. {
  6505. create_fixed_operand (&ops[0], mem);
  6506. create_input_operand (&ops[1], const0_rtx, mode);
  6507. if (maybe_expand_insn (icode, 2, ops))
  6508. {
  6509. /* lock_release is only a release barrier. */
  6510. if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
  6511. expand_mem_thread_fence (model);
  6512. return const0_rtx;
  6513. }
  6514. }
  6515. }
  6516. /* If the size of the object is greater than word size on this target,
  6517. a default store will not be atomic, Try a mem_exchange and throw away
  6518. the result. If that doesn't work, don't do anything. */
  6519. if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
  6520. {
  6521. rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
  6522. if (!target)
  6523. target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
  6524. if (target)
  6525. return const0_rtx;
  6526. else
  6527. return NULL_RTX;
  6528. }
  6529. /* Otherwise assume stores are atomic, and emit the proper barriers. */
  6530. expand_mem_thread_fence (model);
  6531. emit_move_insn (mem, val);
  6532. /* For SEQ_CST, also emit a barrier after the store. */
  6533. if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
  6534. expand_mem_thread_fence (model);
  6535. return const0_rtx;
  6536. }
  6537. /* Structure containing the pointers and values required to process the
  6538. various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
  6539. struct atomic_op_functions
  6540. {
  6541. direct_optab mem_fetch_before;
  6542. direct_optab mem_fetch_after;
  6543. direct_optab mem_no_result;
  6544. optab fetch_before;
  6545. optab fetch_after;
  6546. direct_optab no_result;
  6547. enum rtx_code reverse_code;
  6548. };
  6549. /* Fill in structure pointed to by OP with the various optab entries for an
  6550. operation of type CODE. */
  6551. static void
  6552. get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
  6553. {
  6554. gcc_assert (op!= NULL);
  6555. /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
  6556. in the source code during compilation, and the optab entries are not
  6557. computable until runtime. Fill in the values at runtime. */
  6558. switch (code)
  6559. {
  6560. case PLUS:
  6561. op->mem_fetch_before = atomic_fetch_add_optab;
  6562. op->mem_fetch_after = atomic_add_fetch_optab;
  6563. op->mem_no_result = atomic_add_optab;
  6564. op->fetch_before = sync_old_add_optab;
  6565. op->fetch_after = sync_new_add_optab;
  6566. op->no_result = sync_add_optab;
  6567. op->reverse_code = MINUS;
  6568. break;
  6569. case MINUS:
  6570. op->mem_fetch_before = atomic_fetch_sub_optab;
  6571. op->mem_fetch_after = atomic_sub_fetch_optab;
  6572. op->mem_no_result = atomic_sub_optab;
  6573. op->fetch_before = sync_old_sub_optab;
  6574. op->fetch_after = sync_new_sub_optab;
  6575. op->no_result = sync_sub_optab;
  6576. op->reverse_code = PLUS;
  6577. break;
  6578. case XOR:
  6579. op->mem_fetch_before = atomic_fetch_xor_optab;
  6580. op->mem_fetch_after = atomic_xor_fetch_optab;
  6581. op->mem_no_result = atomic_xor_optab;
  6582. op->fetch_before = sync_old_xor_optab;
  6583. op->fetch_after = sync_new_xor_optab;
  6584. op->no_result = sync_xor_optab;
  6585. op->reverse_code = XOR;
  6586. break;
  6587. case AND:
  6588. op->mem_fetch_before = atomic_fetch_and_optab;
  6589. op->mem_fetch_after = atomic_and_fetch_optab;
  6590. op->mem_no_result = atomic_and_optab;
  6591. op->fetch_before = sync_old_and_optab;
  6592. op->fetch_after = sync_new_and_optab;
  6593. op->no_result = sync_and_optab;
  6594. op->reverse_code = UNKNOWN;
  6595. break;
  6596. case IOR:
  6597. op->mem_fetch_before = atomic_fetch_or_optab;
  6598. op->mem_fetch_after = atomic_or_fetch_optab;
  6599. op->mem_no_result = atomic_or_optab;
  6600. op->fetch_before = sync_old_ior_optab;
  6601. op->fetch_after = sync_new_ior_optab;
  6602. op->no_result = sync_ior_optab;
  6603. op->reverse_code = UNKNOWN;
  6604. break;
  6605. case NOT:
  6606. op->mem_fetch_before = atomic_fetch_nand_optab;
  6607. op->mem_fetch_after = atomic_nand_fetch_optab;
  6608. op->mem_no_result = atomic_nand_optab;
  6609. op->fetch_before = sync_old_nand_optab;
  6610. op->fetch_after = sync_new_nand_optab;
  6611. op->no_result = sync_nand_optab;
  6612. op->reverse_code = UNKNOWN;
  6613. break;
  6614. default:
  6615. gcc_unreachable ();
  6616. }
  6617. }
  6618. /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
  6619. using memory order MODEL. If AFTER is true the operation needs to return
  6620. the value of *MEM after the operation, otherwise the previous value.
  6621. TARGET is an optional place to place the result. The result is unused if
  6622. it is const0_rtx.
  6623. Return the result if there is a better sequence, otherwise NULL_RTX. */
  6624. static rtx
  6625. maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
  6626. enum memmodel model, bool after)
  6627. {
  6628. /* If the value is prefetched, or not used, it may be possible to replace
  6629. the sequence with a native exchange operation. */
  6630. if (!after || target == const0_rtx)
  6631. {
  6632. /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
  6633. if (code == AND && val == const0_rtx)
  6634. {
  6635. if (target == const0_rtx)
  6636. target = gen_reg_rtx (GET_MODE (mem));
  6637. return maybe_emit_atomic_exchange (target, mem, val, model);
  6638. }
  6639. /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
  6640. if (code == IOR && val == constm1_rtx)
  6641. {
  6642. if (target == const0_rtx)
  6643. target = gen_reg_rtx (GET_MODE (mem));
  6644. return maybe_emit_atomic_exchange (target, mem, val, model);
  6645. }
  6646. }
  6647. return NULL_RTX;
  6648. }
  6649. /* Try to emit an instruction for a specific operation varaition.
  6650. OPTAB contains the OP functions.
  6651. TARGET is an optional place to return the result. const0_rtx means unused.
  6652. MEM is the memory location to operate on.
  6653. VAL is the value to use in the operation.
  6654. USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
  6655. MODEL is the memory model, if used.
  6656. AFTER is true if the returned result is the value after the operation. */
  6657. static rtx
  6658. maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
  6659. rtx val, bool use_memmodel, enum memmodel model, bool after)
  6660. {
  6661. machine_mode mode = GET_MODE (mem);
  6662. struct expand_operand ops[4];
  6663. enum insn_code icode;
  6664. int op_counter = 0;
  6665. int num_ops;
  6666. /* Check to see if there is a result returned. */
  6667. if (target == const0_rtx)
  6668. {
  6669. if (use_memmodel)
  6670. {
  6671. icode = direct_optab_handler (optab->mem_no_result, mode);
  6672. create_integer_operand (&ops[2], model);
  6673. num_ops = 3;
  6674. }
  6675. else
  6676. {
  6677. icode = direct_optab_handler (optab->no_result, mode);
  6678. num_ops = 2;
  6679. }
  6680. }
  6681. /* Otherwise, we need to generate a result. */
  6682. else
  6683. {
  6684. if (use_memmodel)
  6685. {
  6686. icode = direct_optab_handler (after ? optab->mem_fetch_after
  6687. : optab->mem_fetch_before, mode);
  6688. create_integer_operand (&ops[3], model);
  6689. num_ops = 4;
  6690. }
  6691. else
  6692. {
  6693. icode = optab_handler (after ? optab->fetch_after
  6694. : optab->fetch_before, mode);
  6695. num_ops = 3;
  6696. }
  6697. create_output_operand (&ops[op_counter++], target, mode);
  6698. }
  6699. if (icode == CODE_FOR_nothing)
  6700. return NULL_RTX;
  6701. create_fixed_operand (&ops[op_counter++], mem);
  6702. /* VAL may have been promoted to a wider mode. Shrink it if so. */
  6703. create_convert_operand_to (&ops[op_counter++], val, mode, true);
  6704. if (maybe_expand_insn (icode, num_ops, ops))
  6705. return (target == const0_rtx ? const0_rtx : ops[0].value);
  6706. return NULL_RTX;
  6707. }
  6708. /* This function expands an atomic fetch_OP or OP_fetch operation:
  6709. TARGET is an option place to stick the return value. const0_rtx indicates
  6710. the result is unused.
  6711. atomically fetch MEM, perform the operation with VAL and return it to MEM.
  6712. CODE is the operation being performed (OP)
  6713. MEMMODEL is the memory model variant to use.
  6714. AFTER is true to return the result of the operation (OP_fetch).
  6715. AFTER is false to return the value before the operation (fetch_OP).
  6716. This function will *only* generate instructions if there is a direct
  6717. optab. No compare and swap loops or libcalls will be generated. */
  6718. static rtx
  6719. expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
  6720. enum rtx_code code, enum memmodel model,
  6721. bool after)
  6722. {
  6723. machine_mode mode = GET_MODE (mem);
  6724. struct atomic_op_functions optab;
  6725. rtx result;
  6726. bool unused_result = (target == const0_rtx);
  6727. get_atomic_op_for_code (&optab, code);
  6728. /* Check to see if there are any better instructions. */
  6729. result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
  6730. if (result)
  6731. return result;
  6732. /* Check for the case where the result isn't used and try those patterns. */
  6733. if (unused_result)
  6734. {
  6735. /* Try the memory model variant first. */
  6736. result = maybe_emit_op (&optab, target, mem, val, true, model, true);
  6737. if (result)
  6738. return result;
  6739. /* Next try the old style withuot a memory model. */
  6740. result = maybe_emit_op (&optab, target, mem, val, false, model, true);
  6741. if (result)
  6742. return result;
  6743. /* There is no no-result pattern, so try patterns with a result. */
  6744. target = NULL_RTX;
  6745. }
  6746. /* Try the __atomic version. */
  6747. result = maybe_emit_op (&optab, target, mem, val, true, model, after);
  6748. if (result)
  6749. return result;
  6750. /* Try the older __sync version. */
  6751. result = maybe_emit_op (&optab, target, mem, val, false, model, after);
  6752. if (result)
  6753. return result;
  6754. /* If the fetch value can be calculated from the other variation of fetch,
  6755. try that operation. */
  6756. if (after || unused_result || optab.reverse_code != UNKNOWN)
  6757. {
  6758. /* Try the __atomic version, then the older __sync version. */
  6759. result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
  6760. if (!result)
  6761. result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
  6762. if (result)
  6763. {
  6764. /* If the result isn't used, no need to do compensation code. */
  6765. if (unused_result)
  6766. return result;
  6767. /* Issue compensation code. Fetch_after == fetch_before OP val.
  6768. Fetch_before == after REVERSE_OP val. */
  6769. if (!after)
  6770. code = optab.reverse_code;
  6771. if (code == NOT)
  6772. {
  6773. result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
  6774. true, OPTAB_LIB_WIDEN);
  6775. result = expand_simple_unop (mode, NOT, result, target, true);
  6776. }
  6777. else
  6778. result = expand_simple_binop (mode, code, result, val, target,
  6779. true, OPTAB_LIB_WIDEN);
  6780. return result;
  6781. }
  6782. }
  6783. /* No direct opcode can be generated. */
  6784. return NULL_RTX;
  6785. }
  6786. /* This function expands an atomic fetch_OP or OP_fetch operation:
  6787. TARGET is an option place to stick the return value. const0_rtx indicates
  6788. the result is unused.
  6789. atomically fetch MEM, perform the operation with VAL and return it to MEM.
  6790. CODE is the operation being performed (OP)
  6791. MEMMODEL is the memory model variant to use.
  6792. AFTER is true to return the result of the operation (OP_fetch).
  6793. AFTER is false to return the value before the operation (fetch_OP). */
  6794. rtx
  6795. expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
  6796. enum memmodel model, bool after)
  6797. {
  6798. machine_mode mode = GET_MODE (mem);
  6799. rtx result;
  6800. bool unused_result = (target == const0_rtx);
  6801. result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
  6802. after);
  6803. if (result)
  6804. return result;
  6805. /* Add/sub can be implemented by doing the reverse operation with -(val). */
  6806. if (code == PLUS || code == MINUS)
  6807. {
  6808. rtx tmp;
  6809. enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
  6810. start_sequence ();
  6811. tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
  6812. result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
  6813. model, after);
  6814. if (result)
  6815. {
  6816. /* PLUS worked so emit the insns and return. */
  6817. tmp = get_insns ();
  6818. end_sequence ();
  6819. emit_insn (tmp);
  6820. return result;
  6821. }
  6822. /* PLUS did not work, so throw away the negation code and continue. */
  6823. end_sequence ();
  6824. }
  6825. /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
  6826. if (!can_compare_and_swap_p (mode, false))
  6827. {
  6828. rtx libfunc;
  6829. bool fixup = false;
  6830. enum rtx_code orig_code = code;
  6831. struct atomic_op_functions optab;
  6832. get_atomic_op_for_code (&optab, code);
  6833. libfunc = optab_libfunc (after ? optab.fetch_after
  6834. : optab.fetch_before, mode);
  6835. if (libfunc == NULL
  6836. && (after || unused_result || optab.reverse_code != UNKNOWN))
  6837. {
  6838. fixup = true;
  6839. if (!after)
  6840. code = optab.reverse_code;
  6841. libfunc = optab_libfunc (after ? optab.fetch_before
  6842. : optab.fetch_after, mode);
  6843. }
  6844. if (libfunc != NULL)
  6845. {
  6846. rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
  6847. result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
  6848. 2, addr, ptr_mode, val, mode);
  6849. if (!unused_result && fixup)
  6850. result = expand_simple_binop (mode, code, result, val, target,
  6851. true, OPTAB_LIB_WIDEN);
  6852. return result;
  6853. }
  6854. /* We need the original code for any further attempts. */
  6855. code = orig_code;
  6856. }
  6857. /* If nothing else has succeeded, default to a compare and swap loop. */
  6858. if (can_compare_and_swap_p (mode, true))
  6859. {
  6860. rtx_insn *insn;
  6861. rtx t0 = gen_reg_rtx (mode), t1;
  6862. start_sequence ();
  6863. /* If the result is used, get a register for it. */
  6864. if (!unused_result)
  6865. {
  6866. if (!target || !register_operand (target, mode))
  6867. target = gen_reg_rtx (mode);
  6868. /* If fetch_before, copy the value now. */
  6869. if (!after)
  6870. emit_move_insn (target, t0);
  6871. }
  6872. else
  6873. target = const0_rtx;
  6874. t1 = t0;
  6875. if (code == NOT)
  6876. {
  6877. t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
  6878. true, OPTAB_LIB_WIDEN);
  6879. t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
  6880. }
  6881. else
  6882. t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
  6883. OPTAB_LIB_WIDEN);
  6884. /* For after, copy the value now. */
  6885. if (!unused_result && after)
  6886. emit_move_insn (target, t1);
  6887. insn = get_insns ();
  6888. end_sequence ();
  6889. if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
  6890. return target;
  6891. }
  6892. return NULL_RTX;
  6893. }
  6894. /* Return true if OPERAND is suitable for operand number OPNO of
  6895. instruction ICODE. */
  6896. bool
  6897. insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
  6898. {
  6899. return (!insn_data[(int) icode].operand[opno].predicate
  6900. || (insn_data[(int) icode].operand[opno].predicate
  6901. (operand, insn_data[(int) icode].operand[opno].mode)));
  6902. }
  6903. /* TARGET is a target of a multiword operation that we are going to
  6904. implement as a series of word-mode operations. Return true if
  6905. TARGET is suitable for this purpose. */
  6906. bool
  6907. valid_multiword_target_p (rtx target)
  6908. {
  6909. machine_mode mode;
  6910. int i;
  6911. mode = GET_MODE (target);
  6912. for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
  6913. if (!validate_subreg (word_mode, mode, target, i))
  6914. return false;
  6915. return true;
  6916. }
  6917. /* Like maybe_legitimize_operand, but do not change the code of the
  6918. current rtx value. */
  6919. static bool
  6920. maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
  6921. struct expand_operand *op)
  6922. {
  6923. /* See if the operand matches in its current form. */
  6924. if (insn_operand_matches (icode, opno, op->value))
  6925. return true;
  6926. /* If the operand is a memory whose address has no side effects,
  6927. try forcing the address into a non-virtual pseudo register.
  6928. The check for side effects is important because copy_to_mode_reg
  6929. cannot handle things like auto-modified addresses. */
  6930. if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
  6931. {
  6932. rtx addr, mem;
  6933. mem = op->value;
  6934. addr = XEXP (mem, 0);
  6935. if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
  6936. && !side_effects_p (addr))
  6937. {
  6938. rtx_insn *last;
  6939. machine_mode mode;
  6940. last = get_last_insn ();
  6941. mode = get_address_mode (mem);
  6942. mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
  6943. if (insn_operand_matches (icode, opno, mem))
  6944. {
  6945. op->value = mem;
  6946. return true;
  6947. }
  6948. delete_insns_since (last);
  6949. }
  6950. }
  6951. return false;
  6952. }
  6953. /* Try to make OP match operand OPNO of instruction ICODE. Return true
  6954. on success, storing the new operand value back in OP. */
  6955. static bool
  6956. maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
  6957. struct expand_operand *op)
  6958. {
  6959. machine_mode mode, imode;
  6960. bool old_volatile_ok, result;
  6961. mode = op->mode;
  6962. switch (op->type)
  6963. {
  6964. case EXPAND_FIXED:
  6965. old_volatile_ok = volatile_ok;
  6966. volatile_ok = true;
  6967. result = maybe_legitimize_operand_same_code (icode, opno, op);
  6968. volatile_ok = old_volatile_ok;
  6969. return result;
  6970. case EXPAND_OUTPUT:
  6971. gcc_assert (mode != VOIDmode);
  6972. if (op->value
  6973. && op->value != const0_rtx
  6974. && GET_MODE (op->value) == mode
  6975. && maybe_legitimize_operand_same_code (icode, opno, op))
  6976. return true;
  6977. op->value = gen_reg_rtx (mode);
  6978. break;
  6979. case EXPAND_INPUT:
  6980. input:
  6981. gcc_assert (mode != VOIDmode);
  6982. gcc_assert (GET_MODE (op->value) == VOIDmode
  6983. || GET_MODE (op->value) == mode);
  6984. if (maybe_legitimize_operand_same_code (icode, opno, op))
  6985. return true;
  6986. op->value = copy_to_mode_reg (mode, op->value);
  6987. break;
  6988. case EXPAND_CONVERT_TO:
  6989. gcc_assert (mode != VOIDmode);
  6990. op->value = convert_to_mode (mode, op->value, op->unsigned_p);
  6991. goto input;
  6992. case EXPAND_CONVERT_FROM:
  6993. if (GET_MODE (op->value) != VOIDmode)
  6994. mode = GET_MODE (op->value);
  6995. else
  6996. /* The caller must tell us what mode this value has. */
  6997. gcc_assert (mode != VOIDmode);
  6998. imode = insn_data[(int) icode].operand[opno].mode;
  6999. if (imode != VOIDmode && imode != mode)
  7000. {
  7001. op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
  7002. mode = imode;
  7003. }
  7004. goto input;
  7005. case EXPAND_ADDRESS:
  7006. gcc_assert (mode != VOIDmode);
  7007. op->value = convert_memory_address (mode, op->value);
  7008. goto input;
  7009. case EXPAND_INTEGER:
  7010. mode = insn_data[(int) icode].operand[opno].mode;
  7011. if (mode != VOIDmode && const_int_operand (op->value, mode))
  7012. goto input;
  7013. break;
  7014. }
  7015. return insn_operand_matches (icode, opno, op->value);
  7016. }
  7017. /* Make OP describe an input operand that should have the same value
  7018. as VALUE, after any mode conversion that the target might request.
  7019. TYPE is the type of VALUE. */
  7020. void
  7021. create_convert_operand_from_type (struct expand_operand *op,
  7022. rtx value, tree type)
  7023. {
  7024. create_convert_operand_from (op, value, TYPE_MODE (type),
  7025. TYPE_UNSIGNED (type));
  7026. }
  7027. /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
  7028. of instruction ICODE. Return true on success, leaving the new operand
  7029. values in the OPS themselves. Emit no code on failure. */
  7030. bool
  7031. maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
  7032. unsigned int nops, struct expand_operand *ops)
  7033. {
  7034. rtx_insn *last;
  7035. unsigned int i;
  7036. last = get_last_insn ();
  7037. for (i = 0; i < nops; i++)
  7038. if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
  7039. {
  7040. delete_insns_since (last);
  7041. return false;
  7042. }
  7043. return true;
  7044. }
  7045. /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
  7046. as its operands. Return the instruction pattern on success,
  7047. and emit any necessary set-up code. Return null and emit no
  7048. code on failure. */
  7049. rtx
  7050. maybe_gen_insn (enum insn_code icode, unsigned int nops,
  7051. struct expand_operand *ops)
  7052. {
  7053. gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
  7054. if (!maybe_legitimize_operands (icode, 0, nops, ops))
  7055. return NULL_RTX;
  7056. switch (nops)
  7057. {
  7058. case 1:
  7059. return GEN_FCN (icode) (ops[0].value);
  7060. case 2:
  7061. return GEN_FCN (icode) (ops[0].value, ops[1].value);
  7062. case 3:
  7063. return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
  7064. case 4:
  7065. return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
  7066. ops[3].value);
  7067. case 5:
  7068. return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
  7069. ops[3].value, ops[4].value);
  7070. case 6:
  7071. return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
  7072. ops[3].value, ops[4].value, ops[5].value);
  7073. case 7:
  7074. return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
  7075. ops[3].value, ops[4].value, ops[5].value,
  7076. ops[6].value);
  7077. case 8:
  7078. return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
  7079. ops[3].value, ops[4].value, ops[5].value,
  7080. ops[6].value, ops[7].value);
  7081. case 9:
  7082. return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
  7083. ops[3].value, ops[4].value, ops[5].value,
  7084. ops[6].value, ops[7].value, ops[8].value);
  7085. }
  7086. gcc_unreachable ();
  7087. }
  7088. /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
  7089. as its operands. Return true on success and emit no code on failure. */
  7090. bool
  7091. maybe_expand_insn (enum insn_code icode, unsigned int nops,
  7092. struct expand_operand *ops)
  7093. {
  7094. rtx pat = maybe_gen_insn (icode, nops, ops);
  7095. if (pat)
  7096. {
  7097. emit_insn (pat);
  7098. return true;
  7099. }
  7100. return false;
  7101. }
  7102. /* Like maybe_expand_insn, but for jumps. */
  7103. bool
  7104. maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
  7105. struct expand_operand *ops)
  7106. {
  7107. rtx pat = maybe_gen_insn (icode, nops, ops);
  7108. if (pat)
  7109. {
  7110. emit_jump_insn (pat);
  7111. return true;
  7112. }
  7113. return false;
  7114. }
  7115. /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
  7116. as its operands. */
  7117. void
  7118. expand_insn (enum insn_code icode, unsigned int nops,
  7119. struct expand_operand *ops)
  7120. {
  7121. if (!maybe_expand_insn (icode, nops, ops))
  7122. gcc_unreachable ();
  7123. }
  7124. /* Like expand_insn, but for jumps. */
  7125. void
  7126. expand_jump_insn (enum insn_code icode, unsigned int nops,
  7127. struct expand_operand *ops)
  7128. {
  7129. if (!maybe_expand_jump_insn (icode, nops, ops))
  7130. gcc_unreachable ();
  7131. }
  7132. /* Reduce conditional compilation elsewhere. */
  7133. #ifndef HAVE_insv
  7134. #define HAVE_insv 0
  7135. #define CODE_FOR_insv CODE_FOR_nothing
  7136. #endif
  7137. #ifndef HAVE_extv
  7138. #define HAVE_extv 0
  7139. #define CODE_FOR_extv CODE_FOR_nothing
  7140. #endif
  7141. #ifndef HAVE_extzv
  7142. #define HAVE_extzv 0
  7143. #define CODE_FOR_extzv CODE_FOR_nothing
  7144. #endif
  7145. /* Enumerates the possible types of structure operand to an
  7146. extraction_insn. */
  7147. enum extraction_type { ET_unaligned_mem, ET_reg };
  7148. /* Check whether insv, extv or extzv pattern ICODE can be used for an
  7149. insertion or extraction of type TYPE on a structure of mode MODE.
  7150. Return true if so and fill in *INSN accordingly. STRUCT_OP is the
  7151. operand number of the structure (the first sign_extract or zero_extract
  7152. operand) and FIELD_OP is the operand number of the field (the other
  7153. side of the set from the sign_extract or zero_extract). */
  7154. static bool
  7155. get_traditional_extraction_insn (extraction_insn *insn,
  7156. enum extraction_type type,
  7157. machine_mode mode,
  7158. enum insn_code icode,
  7159. int struct_op, int field_op)
  7160. {
  7161. const struct insn_data_d *data = &insn_data[icode];
  7162. machine_mode struct_mode = data->operand[struct_op].mode;
  7163. if (struct_mode == VOIDmode)
  7164. struct_mode = word_mode;
  7165. if (mode != struct_mode)
  7166. return false;
  7167. machine_mode field_mode = data->operand[field_op].mode;
  7168. if (field_mode == VOIDmode)
  7169. field_mode = word_mode;
  7170. machine_mode pos_mode = data->operand[struct_op + 2].mode;
  7171. if (pos_mode == VOIDmode)
  7172. pos_mode = word_mode;
  7173. insn->icode = icode;
  7174. insn->field_mode = field_mode;
  7175. insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
  7176. insn->pos_mode = pos_mode;
  7177. return true;
  7178. }
  7179. /* Return true if an optab exists to perform an insertion or extraction
  7180. of type TYPE in mode MODE. Describe the instruction in *INSN if so.
  7181. REG_OPTAB is the optab to use for register structures and
  7182. MISALIGN_OPTAB is the optab to use for misaligned memory structures.
  7183. POS_OP is the operand number of the bit position. */
  7184. static bool
  7185. get_optab_extraction_insn (struct extraction_insn *insn,
  7186. enum extraction_type type,
  7187. machine_mode mode, direct_optab reg_optab,
  7188. direct_optab misalign_optab, int pos_op)
  7189. {
  7190. direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
  7191. enum insn_code icode = direct_optab_handler (optab, mode);
  7192. if (icode == CODE_FOR_nothing)
  7193. return false;
  7194. const struct insn_data_d *data = &insn_data[icode];
  7195. insn->icode = icode;
  7196. insn->field_mode = mode;
  7197. insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
  7198. insn->pos_mode = data->operand[pos_op].mode;
  7199. if (insn->pos_mode == VOIDmode)
  7200. insn->pos_mode = word_mode;
  7201. return true;
  7202. }
  7203. /* Return true if an instruction exists to perform an insertion or
  7204. extraction (PATTERN says which) of type TYPE in mode MODE.
  7205. Describe the instruction in *INSN if so. */
  7206. static bool
  7207. get_extraction_insn (extraction_insn *insn,
  7208. enum extraction_pattern pattern,
  7209. enum extraction_type type,
  7210. machine_mode mode)
  7211. {
  7212. switch (pattern)
  7213. {
  7214. case EP_insv:
  7215. if (HAVE_insv
  7216. && get_traditional_extraction_insn (insn, type, mode,
  7217. CODE_FOR_insv, 0, 3))
  7218. return true;
  7219. return get_optab_extraction_insn (insn, type, mode, insv_optab,
  7220. insvmisalign_optab, 2);
  7221. case EP_extv:
  7222. if (HAVE_extv
  7223. && get_traditional_extraction_insn (insn, type, mode,
  7224. CODE_FOR_extv, 1, 0))
  7225. return true;
  7226. return get_optab_extraction_insn (insn, type, mode, extv_optab,
  7227. extvmisalign_optab, 3);
  7228. case EP_extzv:
  7229. if (HAVE_extzv
  7230. && get_traditional_extraction_insn (insn, type, mode,
  7231. CODE_FOR_extzv, 1, 0))
  7232. return true;
  7233. return get_optab_extraction_insn (insn, type, mode, extzv_optab,
  7234. extzvmisalign_optab, 3);
  7235. default:
  7236. gcc_unreachable ();
  7237. }
  7238. }
  7239. /* Return true if an instruction exists to access a field of mode
  7240. FIELDMODE in a structure that has STRUCT_BITS significant bits.
  7241. Describe the "best" such instruction in *INSN if so. PATTERN and
  7242. TYPE describe the type of insertion or extraction we want to perform.
  7243. For an insertion, the number of significant structure bits includes
  7244. all bits of the target. For an extraction, it need only include the
  7245. most significant bit of the field. Larger widths are acceptable
  7246. in both cases. */
  7247. static bool
  7248. get_best_extraction_insn (extraction_insn *insn,
  7249. enum extraction_pattern pattern,
  7250. enum extraction_type type,
  7251. unsigned HOST_WIDE_INT struct_bits,
  7252. machine_mode field_mode)
  7253. {
  7254. machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
  7255. while (mode != VOIDmode)
  7256. {
  7257. if (get_extraction_insn (insn, pattern, type, mode))
  7258. {
  7259. while (mode != VOIDmode
  7260. && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
  7261. && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
  7262. field_mode))
  7263. {
  7264. get_extraction_insn (insn, pattern, type, mode);
  7265. mode = GET_MODE_WIDER_MODE (mode);
  7266. }
  7267. return true;
  7268. }
  7269. mode = GET_MODE_WIDER_MODE (mode);
  7270. }
  7271. return false;
  7272. }
  7273. /* Return true if an instruction exists to access a field of mode
  7274. FIELDMODE in a register structure that has STRUCT_BITS significant bits.
  7275. Describe the "best" such instruction in *INSN if so. PATTERN describes
  7276. the type of insertion or extraction we want to perform.
  7277. For an insertion, the number of significant structure bits includes
  7278. all bits of the target. For an extraction, it need only include the
  7279. most significant bit of the field. Larger widths are acceptable
  7280. in both cases. */
  7281. bool
  7282. get_best_reg_extraction_insn (extraction_insn *insn,
  7283. enum extraction_pattern pattern,
  7284. unsigned HOST_WIDE_INT struct_bits,
  7285. machine_mode field_mode)
  7286. {
  7287. return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
  7288. field_mode);
  7289. }
  7290. /* Return true if an instruction exists to access a field of BITSIZE
  7291. bits starting BITNUM bits into a memory structure. Describe the
  7292. "best" such instruction in *INSN if so. PATTERN describes the type
  7293. of insertion or extraction we want to perform and FIELDMODE is the
  7294. natural mode of the extracted field.
  7295. The instructions considered here only access bytes that overlap
  7296. the bitfield; they do not touch any surrounding bytes. */
  7297. bool
  7298. get_best_mem_extraction_insn (extraction_insn *insn,
  7299. enum extraction_pattern pattern,
  7300. HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
  7301. machine_mode field_mode)
  7302. {
  7303. unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
  7304. + bitsize
  7305. + BITS_PER_UNIT - 1);
  7306. struct_bits -= struct_bits % BITS_PER_UNIT;
  7307. return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
  7308. struct_bits, field_mode);
  7309. }
  7310. /* Determine whether "1 << x" is relatively cheap in word_mode. */
  7311. bool
  7312. lshift_cheap_p (bool speed_p)
  7313. {
  7314. /* FIXME: This should be made target dependent via this "this_target"
  7315. mechanism, similar to e.g. can_copy_init_p in gcse.c. */
  7316. static bool init[2] = { false, false };
  7317. static bool cheap[2] = { true, true };
  7318. /* If the targer has no lshift in word_mode, the operation will most
  7319. probably not be cheap. ??? Does GCC even work for such targets? */
  7320. if (optab_handler (ashl_optab, word_mode) == CODE_FOR_nothing)
  7321. return false;
  7322. if (!init[speed_p])
  7323. {
  7324. rtx reg = gen_raw_REG (word_mode, 10000);
  7325. int cost = set_src_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg),
  7326. speed_p);
  7327. cheap[speed_p] = cost < COSTS_N_INSNS (3);
  7328. init[speed_p] = true;
  7329. }
  7330. return cheap[speed_p];
  7331. }
  7332. #include "gt-optabs.h"