expmed.c 186 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872
  1. /* Medium-level subroutines: convert bit-field store and extract
  2. and shifts, multiplies and divides to rtl instructions.
  3. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "diagnostic-core.h"
  21. #include "rtl.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h"
  32. #include "fold-const.h"
  33. #include "stor-layout.h"
  34. #include "tm_p.h"
  35. #include "flags.h"
  36. #include "insn-config.h"
  37. #include "hashtab.h"
  38. #include "hard-reg-set.h"
  39. #include "function.h"
  40. #include "statistics.h"
  41. #include "real.h"
  42. #include "fixed-value.h"
  43. #include "expmed.h"
  44. #include "dojump.h"
  45. #include "explow.h"
  46. #include "calls.h"
  47. #include "emit-rtl.h"
  48. #include "varasm.h"
  49. #include "stmt.h"
  50. #include "expr.h"
  51. #include "insn-codes.h"
  52. #include "optabs.h"
  53. #include "recog.h"
  54. #include "langhooks.h"
  55. #include "predict.h"
  56. #include "basic-block.h"
  57. #include "df.h"
  58. #include "target.h"
  59. struct target_expmed default_target_expmed;
  60. #if SWITCHABLE_TARGET
  61. struct target_expmed *this_target_expmed = &default_target_expmed;
  62. #endif
  63. static void store_fixed_bit_field (rtx, unsigned HOST_WIDE_INT,
  64. unsigned HOST_WIDE_INT,
  65. unsigned HOST_WIDE_INT,
  66. unsigned HOST_WIDE_INT,
  67. rtx);
  68. static void store_fixed_bit_field_1 (rtx, unsigned HOST_WIDE_INT,
  69. unsigned HOST_WIDE_INT,
  70. rtx);
  71. static void store_split_bit_field (rtx, unsigned HOST_WIDE_INT,
  72. unsigned HOST_WIDE_INT,
  73. unsigned HOST_WIDE_INT,
  74. unsigned HOST_WIDE_INT,
  75. rtx);
  76. static rtx extract_fixed_bit_field (machine_mode, rtx,
  77. unsigned HOST_WIDE_INT,
  78. unsigned HOST_WIDE_INT, rtx, int);
  79. static rtx extract_fixed_bit_field_1 (machine_mode, rtx,
  80. unsigned HOST_WIDE_INT,
  81. unsigned HOST_WIDE_INT, rtx, int);
  82. static rtx lshift_value (machine_mode, unsigned HOST_WIDE_INT, int);
  83. static rtx extract_split_bit_field (rtx, unsigned HOST_WIDE_INT,
  84. unsigned HOST_WIDE_INT, int);
  85. static void do_cmp_and_jump (rtx, rtx, enum rtx_code, machine_mode, rtx_code_label *);
  86. static rtx expand_smod_pow2 (machine_mode, rtx, HOST_WIDE_INT);
  87. static rtx expand_sdiv_pow2 (machine_mode, rtx, HOST_WIDE_INT);
  88. /* Return a constant integer mask value of mode MODE with BITSIZE ones
  89. followed by BITPOS zeros, or the complement of that if COMPLEMENT.
  90. The mask is truncated if necessary to the width of mode MODE. The
  91. mask is zero-extended if BITSIZE+BITPOS is too small for MODE. */
  92. static inline rtx
  93. mask_rtx (machine_mode mode, int bitpos, int bitsize, bool complement)
  94. {
  95. return immed_wide_int_const
  96. (wi::shifted_mask (bitpos, bitsize, complement,
  97. GET_MODE_PRECISION (mode)), mode);
  98. }
  99. /* Test whether a value is zero of a power of two. */
  100. #define EXACT_POWER_OF_2_OR_ZERO_P(x) \
  101. (((x) & ((x) - (unsigned HOST_WIDE_INT) 1)) == 0)
  102. struct init_expmed_rtl
  103. {
  104. rtx reg;
  105. rtx plus;
  106. rtx neg;
  107. rtx mult;
  108. rtx sdiv;
  109. rtx udiv;
  110. rtx sdiv_32;
  111. rtx smod_32;
  112. rtx wide_mult;
  113. rtx wide_lshr;
  114. rtx wide_trunc;
  115. rtx shift;
  116. rtx shift_mult;
  117. rtx shift_add;
  118. rtx shift_sub0;
  119. rtx shift_sub1;
  120. rtx zext;
  121. rtx trunc;
  122. rtx pow2[MAX_BITS_PER_WORD];
  123. rtx cint[MAX_BITS_PER_WORD];
  124. };
  125. static void
  126. init_expmed_one_conv (struct init_expmed_rtl *all, machine_mode to_mode,
  127. machine_mode from_mode, bool speed)
  128. {
  129. int to_size, from_size;
  130. rtx which;
  131. to_size = GET_MODE_PRECISION (to_mode);
  132. from_size = GET_MODE_PRECISION (from_mode);
  133. /* Most partial integers have a precision less than the "full"
  134. integer it requires for storage. In case one doesn't, for
  135. comparison purposes here, reduce the bit size by one in that
  136. case. */
  137. if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT
  138. && exact_log2 (to_size) != -1)
  139. to_size --;
  140. if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT
  141. && exact_log2 (from_size) != -1)
  142. from_size --;
  143. /* Assume cost of zero-extend and sign-extend is the same. */
  144. which = (to_size < from_size ? all->trunc : all->zext);
  145. PUT_MODE (all->reg, from_mode);
  146. set_convert_cost (to_mode, from_mode, speed, set_src_cost (which, speed));
  147. }
  148. static void
  149. init_expmed_one_mode (struct init_expmed_rtl *all,
  150. machine_mode mode, int speed)
  151. {
  152. int m, n, mode_bitsize;
  153. machine_mode mode_from;
  154. mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
  155. PUT_MODE (all->reg, mode);
  156. PUT_MODE (all->plus, mode);
  157. PUT_MODE (all->neg, mode);
  158. PUT_MODE (all->mult, mode);
  159. PUT_MODE (all->sdiv, mode);
  160. PUT_MODE (all->udiv, mode);
  161. PUT_MODE (all->sdiv_32, mode);
  162. PUT_MODE (all->smod_32, mode);
  163. PUT_MODE (all->wide_trunc, mode);
  164. PUT_MODE (all->shift, mode);
  165. PUT_MODE (all->shift_mult, mode);
  166. PUT_MODE (all->shift_add, mode);
  167. PUT_MODE (all->shift_sub0, mode);
  168. PUT_MODE (all->shift_sub1, mode);
  169. PUT_MODE (all->zext, mode);
  170. PUT_MODE (all->trunc, mode);
  171. set_add_cost (speed, mode, set_src_cost (all->plus, speed));
  172. set_neg_cost (speed, mode, set_src_cost (all->neg, speed));
  173. set_mul_cost (speed, mode, set_src_cost (all->mult, speed));
  174. set_sdiv_cost (speed, mode, set_src_cost (all->sdiv, speed));
  175. set_udiv_cost (speed, mode, set_src_cost (all->udiv, speed));
  176. set_sdiv_pow2_cheap (speed, mode, (set_src_cost (all->sdiv_32, speed)
  177. <= 2 * add_cost (speed, mode)));
  178. set_smod_pow2_cheap (speed, mode, (set_src_cost (all->smod_32, speed)
  179. <= 4 * add_cost (speed, mode)));
  180. set_shift_cost (speed, mode, 0, 0);
  181. {
  182. int cost = add_cost (speed, mode);
  183. set_shiftadd_cost (speed, mode, 0, cost);
  184. set_shiftsub0_cost (speed, mode, 0, cost);
  185. set_shiftsub1_cost (speed, mode, 0, cost);
  186. }
  187. n = MIN (MAX_BITS_PER_WORD, mode_bitsize);
  188. for (m = 1; m < n; m++)
  189. {
  190. XEXP (all->shift, 1) = all->cint[m];
  191. XEXP (all->shift_mult, 1) = all->pow2[m];
  192. set_shift_cost (speed, mode, m, set_src_cost (all->shift, speed));
  193. set_shiftadd_cost (speed, mode, m, set_src_cost (all->shift_add, speed));
  194. set_shiftsub0_cost (speed, mode, m, set_src_cost (all->shift_sub0, speed));
  195. set_shiftsub1_cost (speed, mode, m, set_src_cost (all->shift_sub1, speed));
  196. }
  197. if (SCALAR_INT_MODE_P (mode))
  198. {
  199. for (mode_from = MIN_MODE_INT; mode_from <= MAX_MODE_INT;
  200. mode_from = (machine_mode)(mode_from + 1))
  201. init_expmed_one_conv (all, mode, mode_from, speed);
  202. }
  203. if (GET_MODE_CLASS (mode) == MODE_INT)
  204. {
  205. machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
  206. if (wider_mode != VOIDmode)
  207. {
  208. PUT_MODE (all->zext, wider_mode);
  209. PUT_MODE (all->wide_mult, wider_mode);
  210. PUT_MODE (all->wide_lshr, wider_mode);
  211. XEXP (all->wide_lshr, 1) = GEN_INT (mode_bitsize);
  212. set_mul_widen_cost (speed, wider_mode,
  213. set_src_cost (all->wide_mult, speed));
  214. set_mul_highpart_cost (speed, mode,
  215. set_src_cost (all->wide_trunc, speed));
  216. }
  217. }
  218. }
  219. void
  220. init_expmed (void)
  221. {
  222. struct init_expmed_rtl all;
  223. machine_mode mode = QImode;
  224. int m, speed;
  225. memset (&all, 0, sizeof all);
  226. for (m = 1; m < MAX_BITS_PER_WORD; m++)
  227. {
  228. all.pow2[m] = GEN_INT ((HOST_WIDE_INT) 1 << m);
  229. all.cint[m] = GEN_INT (m);
  230. }
  231. /* Avoid using hard regs in ways which may be unsupported. */
  232. all.reg = gen_rtx_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
  233. all.plus = gen_rtx_PLUS (mode, all.reg, all.reg);
  234. all.neg = gen_rtx_NEG (mode, all.reg);
  235. all.mult = gen_rtx_MULT (mode, all.reg, all.reg);
  236. all.sdiv = gen_rtx_DIV (mode, all.reg, all.reg);
  237. all.udiv = gen_rtx_UDIV (mode, all.reg, all.reg);
  238. all.sdiv_32 = gen_rtx_DIV (mode, all.reg, all.pow2[5]);
  239. all.smod_32 = gen_rtx_MOD (mode, all.reg, all.pow2[5]);
  240. all.zext = gen_rtx_ZERO_EXTEND (mode, all.reg);
  241. all.wide_mult = gen_rtx_MULT (mode, all.zext, all.zext);
  242. all.wide_lshr = gen_rtx_LSHIFTRT (mode, all.wide_mult, all.reg);
  243. all.wide_trunc = gen_rtx_TRUNCATE (mode, all.wide_lshr);
  244. all.shift = gen_rtx_ASHIFT (mode, all.reg, all.reg);
  245. all.shift_mult = gen_rtx_MULT (mode, all.reg, all.reg);
  246. all.shift_add = gen_rtx_PLUS (mode, all.shift_mult, all.reg);
  247. all.shift_sub0 = gen_rtx_MINUS (mode, all.shift_mult, all.reg);
  248. all.shift_sub1 = gen_rtx_MINUS (mode, all.reg, all.shift_mult);
  249. all.trunc = gen_rtx_TRUNCATE (mode, all.reg);
  250. for (speed = 0; speed < 2; speed++)
  251. {
  252. crtl->maybe_hot_insn_p = speed;
  253. set_zero_cost (speed, set_src_cost (const0_rtx, speed));
  254. for (mode = MIN_MODE_INT; mode <= MAX_MODE_INT;
  255. mode = (machine_mode)(mode + 1))
  256. init_expmed_one_mode (&all, mode, speed);
  257. if (MIN_MODE_PARTIAL_INT != VOIDmode)
  258. for (mode = MIN_MODE_PARTIAL_INT; mode <= MAX_MODE_PARTIAL_INT;
  259. mode = (machine_mode)(mode + 1))
  260. init_expmed_one_mode (&all, mode, speed);
  261. if (MIN_MODE_VECTOR_INT != VOIDmode)
  262. for (mode = MIN_MODE_VECTOR_INT; mode <= MAX_MODE_VECTOR_INT;
  263. mode = (machine_mode)(mode + 1))
  264. init_expmed_one_mode (&all, mode, speed);
  265. }
  266. if (alg_hash_used_p ())
  267. {
  268. struct alg_hash_entry *p = alg_hash_entry_ptr (0);
  269. memset (p, 0, sizeof (*p) * NUM_ALG_HASH_ENTRIES);
  270. }
  271. else
  272. set_alg_hash_used_p (true);
  273. default_rtl_profile ();
  274. ggc_free (all.trunc);
  275. ggc_free (all.shift_sub1);
  276. ggc_free (all.shift_sub0);
  277. ggc_free (all.shift_add);
  278. ggc_free (all.shift_mult);
  279. ggc_free (all.shift);
  280. ggc_free (all.wide_trunc);
  281. ggc_free (all.wide_lshr);
  282. ggc_free (all.wide_mult);
  283. ggc_free (all.zext);
  284. ggc_free (all.smod_32);
  285. ggc_free (all.sdiv_32);
  286. ggc_free (all.udiv);
  287. ggc_free (all.sdiv);
  288. ggc_free (all.mult);
  289. ggc_free (all.neg);
  290. ggc_free (all.plus);
  291. ggc_free (all.reg);
  292. }
  293. /* Return an rtx representing minus the value of X.
  294. MODE is the intended mode of the result,
  295. useful if X is a CONST_INT. */
  296. rtx
  297. negate_rtx (machine_mode mode, rtx x)
  298. {
  299. rtx result = simplify_unary_operation (NEG, mode, x, mode);
  300. if (result == 0)
  301. result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
  302. return result;
  303. }
  304. /* Adjust bitfield memory MEM so that it points to the first unit of mode
  305. MODE that contains a bitfield of size BITSIZE at bit position BITNUM.
  306. If MODE is BLKmode, return a reference to every byte in the bitfield.
  307. Set *NEW_BITNUM to the bit position of the field within the new memory. */
  308. static rtx
  309. narrow_bit_field_mem (rtx mem, machine_mode mode,
  310. unsigned HOST_WIDE_INT bitsize,
  311. unsigned HOST_WIDE_INT bitnum,
  312. unsigned HOST_WIDE_INT *new_bitnum)
  313. {
  314. if (mode == BLKmode)
  315. {
  316. *new_bitnum = bitnum % BITS_PER_UNIT;
  317. HOST_WIDE_INT offset = bitnum / BITS_PER_UNIT;
  318. HOST_WIDE_INT size = ((*new_bitnum + bitsize + BITS_PER_UNIT - 1)
  319. / BITS_PER_UNIT);
  320. return adjust_bitfield_address_size (mem, mode, offset, size);
  321. }
  322. else
  323. {
  324. unsigned int unit = GET_MODE_BITSIZE (mode);
  325. *new_bitnum = bitnum % unit;
  326. HOST_WIDE_INT offset = (bitnum - *new_bitnum) / BITS_PER_UNIT;
  327. return adjust_bitfield_address (mem, mode, offset);
  328. }
  329. }
  330. /* The caller wants to perform insertion or extraction PATTERN on a
  331. bitfield of size BITSIZE at BITNUM bits into memory operand OP0.
  332. BITREGION_START and BITREGION_END are as for store_bit_field
  333. and FIELDMODE is the natural mode of the field.
  334. Search for a mode that is compatible with the memory access
  335. restrictions and (where applicable) with a register insertion or
  336. extraction. Return the new memory on success, storing the adjusted
  337. bit position in *NEW_BITNUM. Return null otherwise. */
  338. static rtx
  339. adjust_bit_field_mem_for_reg (enum extraction_pattern pattern,
  340. rtx op0, HOST_WIDE_INT bitsize,
  341. HOST_WIDE_INT bitnum,
  342. unsigned HOST_WIDE_INT bitregion_start,
  343. unsigned HOST_WIDE_INT bitregion_end,
  344. machine_mode fieldmode,
  345. unsigned HOST_WIDE_INT *new_bitnum)
  346. {
  347. bit_field_mode_iterator iter (bitsize, bitnum, bitregion_start,
  348. bitregion_end, MEM_ALIGN (op0),
  349. MEM_VOLATILE_P (op0));
  350. machine_mode best_mode;
  351. if (iter.next_mode (&best_mode))
  352. {
  353. /* We can use a memory in BEST_MODE. See whether this is true for
  354. any wider modes. All other things being equal, we prefer to
  355. use the widest mode possible because it tends to expose more
  356. CSE opportunities. */
  357. if (!iter.prefer_smaller_modes ())
  358. {
  359. /* Limit the search to the mode required by the corresponding
  360. register insertion or extraction instruction, if any. */
  361. machine_mode limit_mode = word_mode;
  362. extraction_insn insn;
  363. if (get_best_reg_extraction_insn (&insn, pattern,
  364. GET_MODE_BITSIZE (best_mode),
  365. fieldmode))
  366. limit_mode = insn.field_mode;
  367. machine_mode wider_mode;
  368. while (iter.next_mode (&wider_mode)
  369. && GET_MODE_SIZE (wider_mode) <= GET_MODE_SIZE (limit_mode))
  370. best_mode = wider_mode;
  371. }
  372. return narrow_bit_field_mem (op0, best_mode, bitsize, bitnum,
  373. new_bitnum);
  374. }
  375. return NULL_RTX;
  376. }
  377. /* Return true if a bitfield of size BITSIZE at bit number BITNUM within
  378. a structure of mode STRUCT_MODE represents a lowpart subreg. The subreg
  379. offset is then BITNUM / BITS_PER_UNIT. */
  380. static bool
  381. lowpart_bit_field_p (unsigned HOST_WIDE_INT bitnum,
  382. unsigned HOST_WIDE_INT bitsize,
  383. machine_mode struct_mode)
  384. {
  385. if (BYTES_BIG_ENDIAN)
  386. return (bitnum % BITS_PER_UNIT == 0
  387. && (bitnum + bitsize == GET_MODE_BITSIZE (struct_mode)
  388. || (bitnum + bitsize) % BITS_PER_WORD == 0));
  389. else
  390. return bitnum % BITS_PER_WORD == 0;
  391. }
  392. /* Return true if -fstrict-volatile-bitfields applies to an access of OP0
  393. containing BITSIZE bits starting at BITNUM, with field mode FIELDMODE.
  394. Return false if the access would touch memory outside the range
  395. BITREGION_START to BITREGION_END for conformance to the C++ memory
  396. model. */
  397. static bool
  398. strict_volatile_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
  399. unsigned HOST_WIDE_INT bitnum,
  400. machine_mode fieldmode,
  401. unsigned HOST_WIDE_INT bitregion_start,
  402. unsigned HOST_WIDE_INT bitregion_end)
  403. {
  404. unsigned HOST_WIDE_INT modesize = GET_MODE_BITSIZE (fieldmode);
  405. /* -fstrict-volatile-bitfields must be enabled and we must have a
  406. volatile MEM. */
  407. if (!MEM_P (op0)
  408. || !MEM_VOLATILE_P (op0)
  409. || flag_strict_volatile_bitfields <= 0)
  410. return false;
  411. /* Non-integral modes likely only happen with packed structures.
  412. Punt. */
  413. if (!SCALAR_INT_MODE_P (fieldmode))
  414. return false;
  415. /* The bit size must not be larger than the field mode, and
  416. the field mode must not be larger than a word. */
  417. if (bitsize > modesize || modesize > BITS_PER_WORD)
  418. return false;
  419. /* Check for cases of unaligned fields that must be split. */
  420. if (bitnum % modesize + bitsize > modesize)
  421. return false;
  422. /* The memory must be sufficiently aligned for a MODESIZE access.
  423. This condition guarantees, that the memory access will not
  424. touch anything after the end of the structure. */
  425. if (MEM_ALIGN (op0) < modesize)
  426. return false;
  427. /* Check for cases where the C++ memory model applies. */
  428. if (bitregion_end != 0
  429. && (bitnum - bitnum % modesize < bitregion_start
  430. || bitnum - bitnum % modesize + modesize - 1 > bitregion_end))
  431. return false;
  432. return true;
  433. }
  434. /* Return true if OP is a memory and if a bitfield of size BITSIZE at
  435. bit number BITNUM can be treated as a simple value of mode MODE. */
  436. static bool
  437. simple_mem_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
  438. unsigned HOST_WIDE_INT bitnum, machine_mode mode)
  439. {
  440. return (MEM_P (op0)
  441. && bitnum % BITS_PER_UNIT == 0
  442. && bitsize == GET_MODE_BITSIZE (mode)
  443. && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
  444. || (bitnum % GET_MODE_ALIGNMENT (mode) == 0
  445. && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode))));
  446. }
  447. /* Try to use instruction INSV to store VALUE into a field of OP0.
  448. BITSIZE and BITNUM are as for store_bit_field. */
  449. static bool
  450. store_bit_field_using_insv (const extraction_insn *insv, rtx op0,
  451. unsigned HOST_WIDE_INT bitsize,
  452. unsigned HOST_WIDE_INT bitnum,
  453. rtx value)
  454. {
  455. struct expand_operand ops[4];
  456. rtx value1;
  457. rtx xop0 = op0;
  458. rtx_insn *last = get_last_insn ();
  459. bool copy_back = false;
  460. machine_mode op_mode = insv->field_mode;
  461. unsigned int unit = GET_MODE_BITSIZE (op_mode);
  462. if (bitsize == 0 || bitsize > unit)
  463. return false;
  464. if (MEM_P (xop0))
  465. /* Get a reference to the first byte of the field. */
  466. xop0 = narrow_bit_field_mem (xop0, insv->struct_mode, bitsize, bitnum,
  467. &bitnum);
  468. else
  469. {
  470. /* Convert from counting within OP0 to counting in OP_MODE. */
  471. if (BYTES_BIG_ENDIAN)
  472. bitnum += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  473. /* If xop0 is a register, we need it in OP_MODE
  474. to make it acceptable to the format of insv. */
  475. if (GET_CODE (xop0) == SUBREG)
  476. /* We can't just change the mode, because this might clobber op0,
  477. and we will need the original value of op0 if insv fails. */
  478. xop0 = gen_rtx_SUBREG (op_mode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
  479. if (REG_P (xop0) && GET_MODE (xop0) != op_mode)
  480. xop0 = gen_lowpart_SUBREG (op_mode, xop0);
  481. }
  482. /* If the destination is a paradoxical subreg such that we need a
  483. truncate to the inner mode, perform the insertion on a temporary and
  484. truncate the result to the original destination. Note that we can't
  485. just truncate the paradoxical subreg as (truncate:N (subreg:W (reg:N
  486. X) 0)) is (reg:N X). */
  487. if (GET_CODE (xop0) == SUBREG
  488. && REG_P (SUBREG_REG (xop0))
  489. && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (SUBREG_REG (xop0)),
  490. op_mode))
  491. {
  492. rtx tem = gen_reg_rtx (op_mode);
  493. emit_move_insn (tem, xop0);
  494. xop0 = tem;
  495. copy_back = true;
  496. }
  497. /* There are similar overflow check at the start of store_bit_field_1,
  498. but that only check the situation where the field lies completely
  499. outside the register, while there do have situation where the field
  500. lies partialy in the register, we need to adjust bitsize for this
  501. partial overflow situation. Without this fix, pr48335-2.c on big-endian
  502. will broken on those arch support bit insert instruction, like arm, aarch64
  503. etc. */
  504. if (bitsize + bitnum > unit && bitnum < unit)
  505. {
  506. warning (OPT_Wextra, "write of %wu-bit data outside the bound of "
  507. "destination object, data truncated into %wu-bit",
  508. bitsize, unit - bitnum);
  509. bitsize = unit - bitnum;
  510. }
  511. /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
  512. "backwards" from the size of the unit we are inserting into.
  513. Otherwise, we count bits from the most significant on a
  514. BYTES/BITS_BIG_ENDIAN machine. */
  515. if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
  516. bitnum = unit - bitsize - bitnum;
  517. /* Convert VALUE to op_mode (which insv insn wants) in VALUE1. */
  518. value1 = value;
  519. if (GET_MODE (value) != op_mode)
  520. {
  521. if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
  522. {
  523. /* Optimization: Don't bother really extending VALUE
  524. if it has all the bits we will actually use. However,
  525. if we must narrow it, be sure we do it correctly. */
  526. if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (op_mode))
  527. {
  528. rtx tmp;
  529. tmp = simplify_subreg (op_mode, value1, GET_MODE (value), 0);
  530. if (! tmp)
  531. tmp = simplify_gen_subreg (op_mode,
  532. force_reg (GET_MODE (value),
  533. value1),
  534. GET_MODE (value), 0);
  535. value1 = tmp;
  536. }
  537. else
  538. value1 = gen_lowpart (op_mode, value1);
  539. }
  540. else if (CONST_INT_P (value))
  541. value1 = gen_int_mode (INTVAL (value), op_mode);
  542. else
  543. /* Parse phase is supposed to make VALUE's data type
  544. match that of the component reference, which is a type
  545. at least as wide as the field; so VALUE should have
  546. a mode that corresponds to that type. */
  547. gcc_assert (CONSTANT_P (value));
  548. }
  549. create_fixed_operand (&ops[0], xop0);
  550. create_integer_operand (&ops[1], bitsize);
  551. create_integer_operand (&ops[2], bitnum);
  552. create_input_operand (&ops[3], value1, op_mode);
  553. if (maybe_expand_insn (insv->icode, 4, ops))
  554. {
  555. if (copy_back)
  556. convert_move (op0, xop0, true);
  557. return true;
  558. }
  559. delete_insns_since (last);
  560. return false;
  561. }
  562. /* A subroutine of store_bit_field, with the same arguments. Return true
  563. if the operation could be implemented.
  564. If FALLBACK_P is true, fall back to store_fixed_bit_field if we have
  565. no other way of implementing the operation. If FALLBACK_P is false,
  566. return false instead. */
  567. static bool
  568. store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
  569. unsigned HOST_WIDE_INT bitnum,
  570. unsigned HOST_WIDE_INT bitregion_start,
  571. unsigned HOST_WIDE_INT bitregion_end,
  572. machine_mode fieldmode,
  573. rtx value, bool fallback_p)
  574. {
  575. rtx op0 = str_rtx;
  576. rtx orig_value;
  577. while (GET_CODE (op0) == SUBREG)
  578. {
  579. /* The following line once was done only if WORDS_BIG_ENDIAN,
  580. but I think that is a mistake. WORDS_BIG_ENDIAN is
  581. meaningful at a much higher level; when structures are copied
  582. between memory and regs, the higher-numbered regs
  583. always get higher addresses. */
  584. int inner_mode_size = GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)));
  585. int outer_mode_size = GET_MODE_SIZE (GET_MODE (op0));
  586. int byte_offset = 0;
  587. /* Paradoxical subregs need special handling on big endian machines. */
  588. if (SUBREG_BYTE (op0) == 0 && inner_mode_size < outer_mode_size)
  589. {
  590. int difference = inner_mode_size - outer_mode_size;
  591. if (WORDS_BIG_ENDIAN)
  592. byte_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
  593. if (BYTES_BIG_ENDIAN)
  594. byte_offset += difference % UNITS_PER_WORD;
  595. }
  596. else
  597. byte_offset = SUBREG_BYTE (op0);
  598. bitnum += byte_offset * BITS_PER_UNIT;
  599. op0 = SUBREG_REG (op0);
  600. }
  601. /* No action is needed if the target is a register and if the field
  602. lies completely outside that register. This can occur if the source
  603. code contains an out-of-bounds access to a small array. */
  604. if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
  605. return true;
  606. /* Use vec_set patterns for inserting parts of vectors whenever
  607. available. */
  608. if (VECTOR_MODE_P (GET_MODE (op0))
  609. && !MEM_P (op0)
  610. && optab_handler (vec_set_optab, GET_MODE (op0)) != CODE_FOR_nothing
  611. && fieldmode == GET_MODE_INNER (GET_MODE (op0))
  612. && bitsize == GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
  613. && !(bitnum % GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
  614. {
  615. struct expand_operand ops[3];
  616. machine_mode outermode = GET_MODE (op0);
  617. machine_mode innermode = GET_MODE_INNER (outermode);
  618. enum insn_code icode = optab_handler (vec_set_optab, outermode);
  619. int pos = bitnum / GET_MODE_BITSIZE (innermode);
  620. create_fixed_operand (&ops[0], op0);
  621. create_input_operand (&ops[1], value, innermode);
  622. create_integer_operand (&ops[2], pos);
  623. if (maybe_expand_insn (icode, 3, ops))
  624. return true;
  625. }
  626. /* If the target is a register, overwriting the entire object, or storing
  627. a full-word or multi-word field can be done with just a SUBREG. */
  628. if (!MEM_P (op0)
  629. && bitsize == GET_MODE_BITSIZE (fieldmode)
  630. && ((bitsize == GET_MODE_BITSIZE (GET_MODE (op0)) && bitnum == 0)
  631. || (bitsize % BITS_PER_WORD == 0 && bitnum % BITS_PER_WORD == 0)))
  632. {
  633. /* Use the subreg machinery either to narrow OP0 to the required
  634. words or to cope with mode punning between equal-sized modes.
  635. In the latter case, use subreg on the rhs side, not lhs. */
  636. rtx sub;
  637. if (bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
  638. {
  639. sub = simplify_gen_subreg (GET_MODE (op0), value, fieldmode, 0);
  640. if (sub)
  641. {
  642. emit_move_insn (op0, sub);
  643. return true;
  644. }
  645. }
  646. else
  647. {
  648. sub = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
  649. bitnum / BITS_PER_UNIT);
  650. if (sub)
  651. {
  652. emit_move_insn (sub, value);
  653. return true;
  654. }
  655. }
  656. }
  657. /* If the target is memory, storing any naturally aligned field can be
  658. done with a simple store. For targets that support fast unaligned
  659. memory, any naturally sized, unit aligned field can be done directly. */
  660. if (simple_mem_bitfield_p (op0, bitsize, bitnum, fieldmode))
  661. {
  662. op0 = adjust_bitfield_address (op0, fieldmode, bitnum / BITS_PER_UNIT);
  663. emit_move_insn (op0, value);
  664. return true;
  665. }
  666. /* Make sure we are playing with integral modes. Pun with subregs
  667. if we aren't. This must come after the entire register case above,
  668. since that case is valid for any mode. The following cases are only
  669. valid for integral modes. */
  670. {
  671. machine_mode imode = int_mode_for_mode (GET_MODE (op0));
  672. if (imode != GET_MODE (op0))
  673. {
  674. if (MEM_P (op0))
  675. op0 = adjust_bitfield_address_size (op0, imode, 0, MEM_SIZE (op0));
  676. else
  677. {
  678. gcc_assert (imode != BLKmode);
  679. op0 = gen_lowpart (imode, op0);
  680. }
  681. }
  682. }
  683. /* Storing an lsb-aligned field in a register
  684. can be done with a movstrict instruction. */
  685. if (!MEM_P (op0)
  686. && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
  687. && bitsize == GET_MODE_BITSIZE (fieldmode)
  688. && optab_handler (movstrict_optab, fieldmode) != CODE_FOR_nothing)
  689. {
  690. struct expand_operand ops[2];
  691. enum insn_code icode = optab_handler (movstrict_optab, fieldmode);
  692. rtx arg0 = op0;
  693. unsigned HOST_WIDE_INT subreg_off;
  694. if (GET_CODE (arg0) == SUBREG)
  695. {
  696. /* Else we've got some float mode source being extracted into
  697. a different float mode destination -- this combination of
  698. subregs results in Severe Tire Damage. */
  699. gcc_assert (GET_MODE (SUBREG_REG (arg0)) == fieldmode
  700. || GET_MODE_CLASS (fieldmode) == MODE_INT
  701. || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
  702. arg0 = SUBREG_REG (arg0);
  703. }
  704. subreg_off = bitnum / BITS_PER_UNIT;
  705. if (validate_subreg (fieldmode, GET_MODE (arg0), arg0, subreg_off))
  706. {
  707. arg0 = gen_rtx_SUBREG (fieldmode, arg0, subreg_off);
  708. create_fixed_operand (&ops[0], arg0);
  709. /* Shrink the source operand to FIELDMODE. */
  710. create_convert_operand_to (&ops[1], value, fieldmode, false);
  711. if (maybe_expand_insn (icode, 2, ops))
  712. return true;
  713. }
  714. }
  715. /* Handle fields bigger than a word. */
  716. if (bitsize > BITS_PER_WORD)
  717. {
  718. /* Here we transfer the words of the field
  719. in the order least significant first.
  720. This is because the most significant word is the one which may
  721. be less than full.
  722. However, only do that if the value is not BLKmode. */
  723. unsigned int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
  724. unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
  725. unsigned int i;
  726. rtx_insn *last;
  727. /* This is the mode we must force value to, so that there will be enough
  728. subwords to extract. Note that fieldmode will often (always?) be
  729. VOIDmode, because that is what store_field uses to indicate that this
  730. is a bit field, but passing VOIDmode to operand_subword_force
  731. is not allowed. */
  732. fieldmode = GET_MODE (value);
  733. if (fieldmode == VOIDmode)
  734. fieldmode = smallest_mode_for_size (nwords * BITS_PER_WORD, MODE_INT);
  735. last = get_last_insn ();
  736. for (i = 0; i < nwords; i++)
  737. {
  738. /* If I is 0, use the low-order word in both field and target;
  739. if I is 1, use the next to lowest word; and so on. */
  740. unsigned int wordnum = (backwards
  741. ? GET_MODE_SIZE (fieldmode) / UNITS_PER_WORD
  742. - i - 1
  743. : i);
  744. unsigned int bit_offset = (backwards
  745. ? MAX ((int) bitsize - ((int) i + 1)
  746. * BITS_PER_WORD,
  747. 0)
  748. : (int) i * BITS_PER_WORD);
  749. rtx value_word = operand_subword_force (value, wordnum, fieldmode);
  750. unsigned HOST_WIDE_INT new_bitsize =
  751. MIN (BITS_PER_WORD, bitsize - i * BITS_PER_WORD);
  752. /* If the remaining chunk doesn't have full wordsize we have
  753. to make sure that for big endian machines the higher order
  754. bits are used. */
  755. if (new_bitsize < BITS_PER_WORD && BYTES_BIG_ENDIAN && !backwards)
  756. value_word = simplify_expand_binop (word_mode, lshr_optab,
  757. value_word,
  758. GEN_INT (BITS_PER_WORD
  759. - new_bitsize),
  760. NULL_RTX, true,
  761. OPTAB_LIB_WIDEN);
  762. if (!store_bit_field_1 (op0, new_bitsize,
  763. bitnum + bit_offset,
  764. bitregion_start, bitregion_end,
  765. word_mode,
  766. value_word, fallback_p))
  767. {
  768. delete_insns_since (last);
  769. return false;
  770. }
  771. }
  772. return true;
  773. }
  774. /* If VALUE has a floating-point or complex mode, access it as an
  775. integer of the corresponding size. This can occur on a machine
  776. with 64 bit registers that uses SFmode for float. It can also
  777. occur for unaligned float or complex fields. */
  778. orig_value = value;
  779. if (GET_MODE (value) != VOIDmode
  780. && GET_MODE_CLASS (GET_MODE (value)) != MODE_INT
  781. && GET_MODE_CLASS (GET_MODE (value)) != MODE_PARTIAL_INT)
  782. {
  783. value = gen_reg_rtx (int_mode_for_mode (GET_MODE (value)));
  784. emit_move_insn (gen_lowpart (GET_MODE (orig_value), value), orig_value);
  785. }
  786. /* If OP0 is a multi-word register, narrow it to the affected word.
  787. If the region spans two words, defer to store_split_bit_field. */
  788. if (!MEM_P (op0) && GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
  789. {
  790. op0 = simplify_gen_subreg (word_mode, op0, GET_MODE (op0),
  791. bitnum / BITS_PER_WORD * UNITS_PER_WORD);
  792. gcc_assert (op0);
  793. bitnum %= BITS_PER_WORD;
  794. if (bitnum + bitsize > BITS_PER_WORD)
  795. {
  796. if (!fallback_p)
  797. return false;
  798. store_split_bit_field (op0, bitsize, bitnum, bitregion_start,
  799. bitregion_end, value);
  800. return true;
  801. }
  802. }
  803. /* From here on we can assume that the field to be stored in fits
  804. within a word. If the destination is a register, it too fits
  805. in a word. */
  806. extraction_insn insv;
  807. if (!MEM_P (op0)
  808. && get_best_reg_extraction_insn (&insv, EP_insv,
  809. GET_MODE_BITSIZE (GET_MODE (op0)),
  810. fieldmode)
  811. && store_bit_field_using_insv (&insv, op0, bitsize, bitnum, value))
  812. return true;
  813. /* If OP0 is a memory, try copying it to a register and seeing if a
  814. cheap register alternative is available. */
  815. if (MEM_P (op0))
  816. {
  817. if (get_best_mem_extraction_insn (&insv, EP_insv, bitsize, bitnum,
  818. fieldmode)
  819. && store_bit_field_using_insv (&insv, op0, bitsize, bitnum, value))
  820. return true;
  821. rtx_insn *last = get_last_insn ();
  822. /* Try loading part of OP0 into a register, inserting the bitfield
  823. into that, and then copying the result back to OP0. */
  824. unsigned HOST_WIDE_INT bitpos;
  825. rtx xop0 = adjust_bit_field_mem_for_reg (EP_insv, op0, bitsize, bitnum,
  826. bitregion_start, bitregion_end,
  827. fieldmode, &bitpos);
  828. if (xop0)
  829. {
  830. rtx tempreg = copy_to_reg (xop0);
  831. if (store_bit_field_1 (tempreg, bitsize, bitpos,
  832. bitregion_start, bitregion_end,
  833. fieldmode, orig_value, false))
  834. {
  835. emit_move_insn (xop0, tempreg);
  836. return true;
  837. }
  838. delete_insns_since (last);
  839. }
  840. }
  841. if (!fallback_p)
  842. return false;
  843. store_fixed_bit_field (op0, bitsize, bitnum, bitregion_start,
  844. bitregion_end, value);
  845. return true;
  846. }
  847. /* Generate code to store value from rtx VALUE
  848. into a bit-field within structure STR_RTX
  849. containing BITSIZE bits starting at bit BITNUM.
  850. BITREGION_START is bitpos of the first bitfield in this region.
  851. BITREGION_END is the bitpos of the ending bitfield in this region.
  852. These two fields are 0, if the C++ memory model does not apply,
  853. or we are not interested in keeping track of bitfield regions.
  854. FIELDMODE is the machine-mode of the FIELD_DECL node for this field. */
  855. void
  856. store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
  857. unsigned HOST_WIDE_INT bitnum,
  858. unsigned HOST_WIDE_INT bitregion_start,
  859. unsigned HOST_WIDE_INT bitregion_end,
  860. machine_mode fieldmode,
  861. rtx value)
  862. {
  863. /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
  864. if (strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, fieldmode,
  865. bitregion_start, bitregion_end))
  866. {
  867. /* Storing of a full word can be done with a simple store.
  868. We know here that the field can be accessed with one single
  869. instruction. For targets that support unaligned memory,
  870. an unaligned access may be necessary. */
  871. if (bitsize == GET_MODE_BITSIZE (fieldmode))
  872. {
  873. str_rtx = adjust_bitfield_address (str_rtx, fieldmode,
  874. bitnum / BITS_PER_UNIT);
  875. gcc_assert (bitnum % BITS_PER_UNIT == 0);
  876. emit_move_insn (str_rtx, value);
  877. }
  878. else
  879. {
  880. rtx temp;
  881. str_rtx = narrow_bit_field_mem (str_rtx, fieldmode, bitsize, bitnum,
  882. &bitnum);
  883. gcc_assert (bitnum + bitsize <= GET_MODE_BITSIZE (fieldmode));
  884. temp = copy_to_reg (str_rtx);
  885. if (!store_bit_field_1 (temp, bitsize, bitnum, 0, 0,
  886. fieldmode, value, true))
  887. gcc_unreachable ();
  888. emit_move_insn (str_rtx, temp);
  889. }
  890. return;
  891. }
  892. /* Under the C++0x memory model, we must not touch bits outside the
  893. bit region. Adjust the address to start at the beginning of the
  894. bit region. */
  895. if (MEM_P (str_rtx) && bitregion_start > 0)
  896. {
  897. machine_mode bestmode;
  898. HOST_WIDE_INT offset, size;
  899. gcc_assert ((bitregion_start % BITS_PER_UNIT) == 0);
  900. offset = bitregion_start / BITS_PER_UNIT;
  901. bitnum -= bitregion_start;
  902. size = (bitnum + bitsize + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  903. bitregion_end -= bitregion_start;
  904. bitregion_start = 0;
  905. bestmode = get_best_mode (bitsize, bitnum,
  906. bitregion_start, bitregion_end,
  907. MEM_ALIGN (str_rtx), VOIDmode,
  908. MEM_VOLATILE_P (str_rtx));
  909. str_rtx = adjust_bitfield_address_size (str_rtx, bestmode, offset, size);
  910. }
  911. if (!store_bit_field_1 (str_rtx, bitsize, bitnum,
  912. bitregion_start, bitregion_end,
  913. fieldmode, value, true))
  914. gcc_unreachable ();
  915. }
  916. /* Use shifts and boolean operations to store VALUE into a bit field of
  917. width BITSIZE in OP0, starting at bit BITNUM. */
  918. static void
  919. store_fixed_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
  920. unsigned HOST_WIDE_INT bitnum,
  921. unsigned HOST_WIDE_INT bitregion_start,
  922. unsigned HOST_WIDE_INT bitregion_end,
  923. rtx value)
  924. {
  925. /* There is a case not handled here:
  926. a structure with a known alignment of just a halfword
  927. and a field split across two aligned halfwords within the structure.
  928. Or likewise a structure with a known alignment of just a byte
  929. and a field split across two bytes.
  930. Such cases are not supposed to be able to occur. */
  931. if (MEM_P (op0))
  932. {
  933. machine_mode mode = GET_MODE (op0);
  934. if (GET_MODE_BITSIZE (mode) == 0
  935. || GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (word_mode))
  936. mode = word_mode;
  937. mode = get_best_mode (bitsize, bitnum, bitregion_start, bitregion_end,
  938. MEM_ALIGN (op0), mode, MEM_VOLATILE_P (op0));
  939. if (mode == VOIDmode)
  940. {
  941. /* The only way this should occur is if the field spans word
  942. boundaries. */
  943. store_split_bit_field (op0, bitsize, bitnum, bitregion_start,
  944. bitregion_end, value);
  945. return;
  946. }
  947. op0 = narrow_bit_field_mem (op0, mode, bitsize, bitnum, &bitnum);
  948. }
  949. store_fixed_bit_field_1 (op0, bitsize, bitnum, value);
  950. }
  951. /* Helper function for store_fixed_bit_field, stores
  952. the bit field always using the MODE of OP0. */
  953. static void
  954. store_fixed_bit_field_1 (rtx op0, unsigned HOST_WIDE_INT bitsize,
  955. unsigned HOST_WIDE_INT bitnum,
  956. rtx value)
  957. {
  958. machine_mode mode;
  959. rtx temp;
  960. int all_zero = 0;
  961. int all_one = 0;
  962. mode = GET_MODE (op0);
  963. gcc_assert (SCALAR_INT_MODE_P (mode));
  964. /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
  965. for invalid input, such as f5 from gcc.dg/pr48335-2.c. */
  966. if (BYTES_BIG_ENDIAN)
  967. /* BITNUM is the distance between our msb
  968. and that of the containing datum.
  969. Convert it to the distance from the lsb. */
  970. bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
  971. /* Now BITNUM is always the distance between our lsb
  972. and that of OP0. */
  973. /* Shift VALUE left by BITNUM bits. If VALUE is not constant,
  974. we must first convert its mode to MODE. */
  975. if (CONST_INT_P (value))
  976. {
  977. unsigned HOST_WIDE_INT v = UINTVAL (value);
  978. if (bitsize < HOST_BITS_PER_WIDE_INT)
  979. v &= ((unsigned HOST_WIDE_INT) 1 << bitsize) - 1;
  980. if (v == 0)
  981. all_zero = 1;
  982. else if ((bitsize < HOST_BITS_PER_WIDE_INT
  983. && v == ((unsigned HOST_WIDE_INT) 1 << bitsize) - 1)
  984. || (bitsize == HOST_BITS_PER_WIDE_INT
  985. && v == (unsigned HOST_WIDE_INT) -1))
  986. all_one = 1;
  987. value = lshift_value (mode, v, bitnum);
  988. }
  989. else
  990. {
  991. int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
  992. && bitnum + bitsize != GET_MODE_BITSIZE (mode));
  993. if (GET_MODE (value) != mode)
  994. value = convert_to_mode (mode, value, 1);
  995. if (must_and)
  996. value = expand_binop (mode, and_optab, value,
  997. mask_rtx (mode, 0, bitsize, 0),
  998. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  999. if (bitnum > 0)
  1000. value = expand_shift (LSHIFT_EXPR, mode, value,
  1001. bitnum, NULL_RTX, 1);
  1002. }
  1003. /* Now clear the chosen bits in OP0,
  1004. except that if VALUE is -1 we need not bother. */
  1005. /* We keep the intermediates in registers to allow CSE to combine
  1006. consecutive bitfield assignments. */
  1007. temp = force_reg (mode, op0);
  1008. if (! all_one)
  1009. {
  1010. temp = expand_binop (mode, and_optab, temp,
  1011. mask_rtx (mode, bitnum, bitsize, 1),
  1012. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  1013. temp = force_reg (mode, temp);
  1014. }
  1015. /* Now logical-or VALUE into OP0, unless it is zero. */
  1016. if (! all_zero)
  1017. {
  1018. temp = expand_binop (mode, ior_optab, temp, value,
  1019. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  1020. temp = force_reg (mode, temp);
  1021. }
  1022. if (op0 != temp)
  1023. {
  1024. op0 = copy_rtx (op0);
  1025. emit_move_insn (op0, temp);
  1026. }
  1027. }
  1028. /* Store a bit field that is split across multiple accessible memory objects.
  1029. OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
  1030. BITSIZE is the field width; BITPOS the position of its first bit
  1031. (within the word).
  1032. VALUE is the value to store.
  1033. This does not yet handle fields wider than BITS_PER_WORD. */
  1034. static void
  1035. store_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
  1036. unsigned HOST_WIDE_INT bitpos,
  1037. unsigned HOST_WIDE_INT bitregion_start,
  1038. unsigned HOST_WIDE_INT bitregion_end,
  1039. rtx value)
  1040. {
  1041. unsigned int unit;
  1042. unsigned int bitsdone = 0;
  1043. /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
  1044. much at a time. */
  1045. if (REG_P (op0) || GET_CODE (op0) == SUBREG)
  1046. unit = BITS_PER_WORD;
  1047. else
  1048. unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
  1049. /* If OP0 is a memory with a mode, then UNIT must not be larger than
  1050. OP0's mode as well. Otherwise, store_fixed_bit_field will call us
  1051. again, and we will mutually recurse forever. */
  1052. if (MEM_P (op0) && GET_MODE_BITSIZE (GET_MODE (op0)) > 0)
  1053. unit = MIN (unit, GET_MODE_BITSIZE (GET_MODE (op0)));
  1054. /* If VALUE is a constant other than a CONST_INT, get it into a register in
  1055. WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
  1056. that VALUE might be a floating-point constant. */
  1057. if (CONSTANT_P (value) && !CONST_INT_P (value))
  1058. {
  1059. rtx word = gen_lowpart_common (word_mode, value);
  1060. if (word && (value != word))
  1061. value = word;
  1062. else
  1063. value = gen_lowpart_common (word_mode,
  1064. force_reg (GET_MODE (value) != VOIDmode
  1065. ? GET_MODE (value)
  1066. : word_mode, value));
  1067. }
  1068. while (bitsdone < bitsize)
  1069. {
  1070. unsigned HOST_WIDE_INT thissize;
  1071. rtx part, word;
  1072. unsigned HOST_WIDE_INT thispos;
  1073. unsigned HOST_WIDE_INT offset;
  1074. offset = (bitpos + bitsdone) / unit;
  1075. thispos = (bitpos + bitsdone) % unit;
  1076. /* When region of bytes we can touch is restricted, decrease
  1077. UNIT close to the end of the region as needed. If op0 is a REG
  1078. or SUBREG of REG, don't do this, as there can't be data races
  1079. on a register and we can expand shorter code in some cases. */
  1080. if (bitregion_end
  1081. && unit > BITS_PER_UNIT
  1082. && bitpos + bitsdone - thispos + unit > bitregion_end + 1
  1083. && !REG_P (op0)
  1084. && (GET_CODE (op0) != SUBREG || !REG_P (SUBREG_REG (op0))))
  1085. {
  1086. unit = unit / 2;
  1087. continue;
  1088. }
  1089. /* THISSIZE must not overrun a word boundary. Otherwise,
  1090. store_fixed_bit_field will call us again, and we will mutually
  1091. recurse forever. */
  1092. thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
  1093. thissize = MIN (thissize, unit - thispos);
  1094. if (BYTES_BIG_ENDIAN)
  1095. {
  1096. /* Fetch successively less significant portions. */
  1097. if (CONST_INT_P (value))
  1098. part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
  1099. >> (bitsize - bitsdone - thissize))
  1100. & (((HOST_WIDE_INT) 1 << thissize) - 1));
  1101. else
  1102. {
  1103. int total_bits = GET_MODE_BITSIZE (GET_MODE (value));
  1104. /* The args are chosen so that the last part includes the
  1105. lsb. Give extract_bit_field the value it needs (with
  1106. endianness compensation) to fetch the piece we want. */
  1107. part = extract_fixed_bit_field (word_mode, value, thissize,
  1108. total_bits - bitsize + bitsdone,
  1109. NULL_RTX, 1);
  1110. }
  1111. }
  1112. else
  1113. {
  1114. /* Fetch successively more significant portions. */
  1115. if (CONST_INT_P (value))
  1116. part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
  1117. >> bitsdone)
  1118. & (((HOST_WIDE_INT) 1 << thissize) - 1));
  1119. else
  1120. part = extract_fixed_bit_field (word_mode, value, thissize,
  1121. bitsdone, NULL_RTX, 1);
  1122. }
  1123. /* If OP0 is a register, then handle OFFSET here.
  1124. When handling multiword bitfields, extract_bit_field may pass
  1125. down a word_mode SUBREG of a larger REG for a bitfield that actually
  1126. crosses a word boundary. Thus, for a SUBREG, we must find
  1127. the current word starting from the base register. */
  1128. if (GET_CODE (op0) == SUBREG)
  1129. {
  1130. int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD)
  1131. + (offset * unit / BITS_PER_WORD);
  1132. machine_mode sub_mode = GET_MODE (SUBREG_REG (op0));
  1133. if (sub_mode != BLKmode && GET_MODE_SIZE (sub_mode) < UNITS_PER_WORD)
  1134. word = word_offset ? const0_rtx : op0;
  1135. else
  1136. word = operand_subword_force (SUBREG_REG (op0), word_offset,
  1137. GET_MODE (SUBREG_REG (op0)));
  1138. offset &= BITS_PER_WORD / unit - 1;
  1139. }
  1140. else if (REG_P (op0))
  1141. {
  1142. machine_mode op0_mode = GET_MODE (op0);
  1143. if (op0_mode != BLKmode && GET_MODE_SIZE (op0_mode) < UNITS_PER_WORD)
  1144. word = offset ? const0_rtx : op0;
  1145. else
  1146. word = operand_subword_force (op0, offset * unit / BITS_PER_WORD,
  1147. GET_MODE (op0));
  1148. offset &= BITS_PER_WORD / unit - 1;
  1149. }
  1150. else
  1151. word = op0;
  1152. /* OFFSET is in UNITs, and UNIT is in bits. If WORD is const0_rtx,
  1153. it is just an out-of-bounds access. Ignore it. */
  1154. if (word != const0_rtx)
  1155. store_fixed_bit_field (word, thissize, offset * unit + thispos,
  1156. bitregion_start, bitregion_end, part);
  1157. bitsdone += thissize;
  1158. }
  1159. }
  1160. /* A subroutine of extract_bit_field_1 that converts return value X
  1161. to either MODE or TMODE. MODE, TMODE and UNSIGNEDP are arguments
  1162. to extract_bit_field. */
  1163. static rtx
  1164. convert_extracted_bit_field (rtx x, machine_mode mode,
  1165. machine_mode tmode, bool unsignedp)
  1166. {
  1167. if (GET_MODE (x) == tmode || GET_MODE (x) == mode)
  1168. return x;
  1169. /* If the x mode is not a scalar integral, first convert to the
  1170. integer mode of that size and then access it as a floating-point
  1171. value via a SUBREG. */
  1172. if (!SCALAR_INT_MODE_P (tmode))
  1173. {
  1174. machine_mode smode;
  1175. smode = mode_for_size (GET_MODE_BITSIZE (tmode), MODE_INT, 0);
  1176. x = convert_to_mode (smode, x, unsignedp);
  1177. x = force_reg (smode, x);
  1178. return gen_lowpart (tmode, x);
  1179. }
  1180. return convert_to_mode (tmode, x, unsignedp);
  1181. }
  1182. /* Try to use an ext(z)v pattern to extract a field from OP0.
  1183. Return the extracted value on success, otherwise return null.
  1184. EXT_MODE is the mode of the extraction and the other arguments
  1185. are as for extract_bit_field. */
  1186. static rtx
  1187. extract_bit_field_using_extv (const extraction_insn *extv, rtx op0,
  1188. unsigned HOST_WIDE_INT bitsize,
  1189. unsigned HOST_WIDE_INT bitnum,
  1190. int unsignedp, rtx target,
  1191. machine_mode mode, machine_mode tmode)
  1192. {
  1193. struct expand_operand ops[4];
  1194. rtx spec_target = target;
  1195. rtx spec_target_subreg = 0;
  1196. machine_mode ext_mode = extv->field_mode;
  1197. unsigned unit = GET_MODE_BITSIZE (ext_mode);
  1198. if (bitsize == 0 || unit < bitsize)
  1199. return NULL_RTX;
  1200. if (MEM_P (op0))
  1201. /* Get a reference to the first byte of the field. */
  1202. op0 = narrow_bit_field_mem (op0, extv->struct_mode, bitsize, bitnum,
  1203. &bitnum);
  1204. else
  1205. {
  1206. /* Convert from counting within OP0 to counting in EXT_MODE. */
  1207. if (BYTES_BIG_ENDIAN)
  1208. bitnum += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  1209. /* If op0 is a register, we need it in EXT_MODE to make it
  1210. acceptable to the format of ext(z)v. */
  1211. if (GET_CODE (op0) == SUBREG && GET_MODE (op0) != ext_mode)
  1212. return NULL_RTX;
  1213. if (REG_P (op0) && GET_MODE (op0) != ext_mode)
  1214. op0 = gen_lowpart_SUBREG (ext_mode, op0);
  1215. }
  1216. /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
  1217. "backwards" from the size of the unit we are extracting from.
  1218. Otherwise, we count bits from the most significant on a
  1219. BYTES/BITS_BIG_ENDIAN machine. */
  1220. if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
  1221. bitnum = unit - bitsize - bitnum;
  1222. if (target == 0)
  1223. target = spec_target = gen_reg_rtx (tmode);
  1224. if (GET_MODE (target) != ext_mode)
  1225. {
  1226. /* Don't use LHS paradoxical subreg if explicit truncation is needed
  1227. between the mode of the extraction (word_mode) and the target
  1228. mode. Instead, create a temporary and use convert_move to set
  1229. the target. */
  1230. if (REG_P (target)
  1231. && TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (target), ext_mode))
  1232. {
  1233. target = gen_lowpart (ext_mode, target);
  1234. if (GET_MODE_PRECISION (ext_mode)
  1235. > GET_MODE_PRECISION (GET_MODE (spec_target)))
  1236. spec_target_subreg = target;
  1237. }
  1238. else
  1239. target = gen_reg_rtx (ext_mode);
  1240. }
  1241. create_output_operand (&ops[0], target, ext_mode);
  1242. create_fixed_operand (&ops[1], op0);
  1243. create_integer_operand (&ops[2], bitsize);
  1244. create_integer_operand (&ops[3], bitnum);
  1245. if (maybe_expand_insn (extv->icode, 4, ops))
  1246. {
  1247. target = ops[0].value;
  1248. if (target == spec_target)
  1249. return target;
  1250. if (target == spec_target_subreg)
  1251. return spec_target;
  1252. return convert_extracted_bit_field (target, mode, tmode, unsignedp);
  1253. }
  1254. return NULL_RTX;
  1255. }
  1256. /* A subroutine of extract_bit_field, with the same arguments.
  1257. If FALLBACK_P is true, fall back to extract_fixed_bit_field
  1258. if we can find no other means of implementing the operation.
  1259. if FALLBACK_P is false, return NULL instead. */
  1260. static rtx
  1261. extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
  1262. unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
  1263. machine_mode mode, machine_mode tmode,
  1264. bool fallback_p)
  1265. {
  1266. rtx op0 = str_rtx;
  1267. machine_mode int_mode;
  1268. machine_mode mode1;
  1269. if (tmode == VOIDmode)
  1270. tmode = mode;
  1271. while (GET_CODE (op0) == SUBREG)
  1272. {
  1273. bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
  1274. op0 = SUBREG_REG (op0);
  1275. }
  1276. /* If we have an out-of-bounds access to a register, just return an
  1277. uninitialized register of the required mode. This can occur if the
  1278. source code contains an out-of-bounds access to a small array. */
  1279. if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
  1280. return gen_reg_rtx (tmode);
  1281. if (REG_P (op0)
  1282. && mode == GET_MODE (op0)
  1283. && bitnum == 0
  1284. && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
  1285. {
  1286. /* We're trying to extract a full register from itself. */
  1287. return op0;
  1288. }
  1289. /* See if we can get a better vector mode before extracting. */
  1290. if (VECTOR_MODE_P (GET_MODE (op0))
  1291. && !MEM_P (op0)
  1292. && GET_MODE_INNER (GET_MODE (op0)) != tmode)
  1293. {
  1294. machine_mode new_mode;
  1295. if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
  1296. new_mode = MIN_MODE_VECTOR_FLOAT;
  1297. else if (GET_MODE_CLASS (tmode) == MODE_FRACT)
  1298. new_mode = MIN_MODE_VECTOR_FRACT;
  1299. else if (GET_MODE_CLASS (tmode) == MODE_UFRACT)
  1300. new_mode = MIN_MODE_VECTOR_UFRACT;
  1301. else if (GET_MODE_CLASS (tmode) == MODE_ACCUM)
  1302. new_mode = MIN_MODE_VECTOR_ACCUM;
  1303. else if (GET_MODE_CLASS (tmode) == MODE_UACCUM)
  1304. new_mode = MIN_MODE_VECTOR_UACCUM;
  1305. else
  1306. new_mode = MIN_MODE_VECTOR_INT;
  1307. for (; new_mode != VOIDmode ; new_mode = GET_MODE_WIDER_MODE (new_mode))
  1308. if (GET_MODE_SIZE (new_mode) == GET_MODE_SIZE (GET_MODE (op0))
  1309. && targetm.vector_mode_supported_p (new_mode))
  1310. break;
  1311. if (new_mode != VOIDmode)
  1312. op0 = gen_lowpart (new_mode, op0);
  1313. }
  1314. /* Use vec_extract patterns for extracting parts of vectors whenever
  1315. available. */
  1316. if (VECTOR_MODE_P (GET_MODE (op0))
  1317. && !MEM_P (op0)
  1318. && optab_handler (vec_extract_optab, GET_MODE (op0)) != CODE_FOR_nothing
  1319. && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
  1320. == bitnum / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
  1321. {
  1322. struct expand_operand ops[3];
  1323. machine_mode outermode = GET_MODE (op0);
  1324. machine_mode innermode = GET_MODE_INNER (outermode);
  1325. enum insn_code icode = optab_handler (vec_extract_optab, outermode);
  1326. unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
  1327. create_output_operand (&ops[0], target, innermode);
  1328. create_input_operand (&ops[1], op0, outermode);
  1329. create_integer_operand (&ops[2], pos);
  1330. if (maybe_expand_insn (icode, 3, ops))
  1331. {
  1332. target = ops[0].value;
  1333. if (GET_MODE (target) != mode)
  1334. return gen_lowpart (tmode, target);
  1335. return target;
  1336. }
  1337. }
  1338. /* Make sure we are playing with integral modes. Pun with subregs
  1339. if we aren't. */
  1340. {
  1341. machine_mode imode = int_mode_for_mode (GET_MODE (op0));
  1342. if (imode != GET_MODE (op0))
  1343. {
  1344. if (MEM_P (op0))
  1345. op0 = adjust_bitfield_address_size (op0, imode, 0, MEM_SIZE (op0));
  1346. else if (imode != BLKmode)
  1347. {
  1348. op0 = gen_lowpart (imode, op0);
  1349. /* If we got a SUBREG, force it into a register since we
  1350. aren't going to be able to do another SUBREG on it. */
  1351. if (GET_CODE (op0) == SUBREG)
  1352. op0 = force_reg (imode, op0);
  1353. }
  1354. else if (REG_P (op0))
  1355. {
  1356. rtx reg, subreg;
  1357. imode = smallest_mode_for_size (GET_MODE_BITSIZE (GET_MODE (op0)),
  1358. MODE_INT);
  1359. reg = gen_reg_rtx (imode);
  1360. subreg = gen_lowpart_SUBREG (GET_MODE (op0), reg);
  1361. emit_move_insn (subreg, op0);
  1362. op0 = reg;
  1363. bitnum += SUBREG_BYTE (subreg) * BITS_PER_UNIT;
  1364. }
  1365. else
  1366. {
  1367. HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (op0));
  1368. rtx mem = assign_stack_temp (GET_MODE (op0), size);
  1369. emit_move_insn (mem, op0);
  1370. op0 = adjust_bitfield_address_size (mem, BLKmode, 0, size);
  1371. }
  1372. }
  1373. }
  1374. /* ??? We currently assume TARGET is at least as big as BITSIZE.
  1375. If that's wrong, the solution is to test for it and set TARGET to 0
  1376. if needed. */
  1377. /* Get the mode of the field to use for atomic access or subreg
  1378. conversion. */
  1379. mode1 = mode;
  1380. if (SCALAR_INT_MODE_P (tmode))
  1381. {
  1382. machine_mode try_mode = mode_for_size (bitsize,
  1383. GET_MODE_CLASS (tmode), 0);
  1384. if (try_mode != BLKmode)
  1385. mode1 = try_mode;
  1386. }
  1387. gcc_assert (mode1 != BLKmode);
  1388. /* Extraction of a full MODE1 value can be done with a subreg as long
  1389. as the least significant bit of the value is the least significant
  1390. bit of either OP0 or a word of OP0. */
  1391. if (!MEM_P (op0)
  1392. && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
  1393. && bitsize == GET_MODE_BITSIZE (mode1)
  1394. && TRULY_NOOP_TRUNCATION_MODES_P (mode1, GET_MODE (op0)))
  1395. {
  1396. rtx sub = simplify_gen_subreg (mode1, op0, GET_MODE (op0),
  1397. bitnum / BITS_PER_UNIT);
  1398. if (sub)
  1399. return convert_extracted_bit_field (sub, mode, tmode, unsignedp);
  1400. }
  1401. /* Extraction of a full MODE1 value can be done with a load as long as
  1402. the field is on a byte boundary and is sufficiently aligned. */
  1403. if (simple_mem_bitfield_p (op0, bitsize, bitnum, mode1))
  1404. {
  1405. op0 = adjust_bitfield_address (op0, mode1, bitnum / BITS_PER_UNIT);
  1406. return convert_extracted_bit_field (op0, mode, tmode, unsignedp);
  1407. }
  1408. /* Handle fields bigger than a word. */
  1409. if (bitsize > BITS_PER_WORD)
  1410. {
  1411. /* Here we transfer the words of the field
  1412. in the order least significant first.
  1413. This is because the most significant word is the one which may
  1414. be less than full. */
  1415. unsigned int backwards = WORDS_BIG_ENDIAN;
  1416. unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
  1417. unsigned int i;
  1418. rtx_insn *last;
  1419. if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
  1420. target = gen_reg_rtx (mode);
  1421. /* Indicate for flow that the entire target reg is being set. */
  1422. emit_clobber (target);
  1423. last = get_last_insn ();
  1424. for (i = 0; i < nwords; i++)
  1425. {
  1426. /* If I is 0, use the low-order word in both field and target;
  1427. if I is 1, use the next to lowest word; and so on. */
  1428. /* Word number in TARGET to use. */
  1429. unsigned int wordnum
  1430. = (backwards
  1431. ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
  1432. : i);
  1433. /* Offset from start of field in OP0. */
  1434. unsigned int bit_offset = (backwards
  1435. ? MAX ((int) bitsize - ((int) i + 1)
  1436. * BITS_PER_WORD,
  1437. 0)
  1438. : (int) i * BITS_PER_WORD);
  1439. rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
  1440. rtx result_part
  1441. = extract_bit_field_1 (op0, MIN (BITS_PER_WORD,
  1442. bitsize - i * BITS_PER_WORD),
  1443. bitnum + bit_offset, 1, target_part,
  1444. mode, word_mode, fallback_p);
  1445. gcc_assert (target_part);
  1446. if (!result_part)
  1447. {
  1448. delete_insns_since (last);
  1449. return NULL;
  1450. }
  1451. if (result_part != target_part)
  1452. emit_move_insn (target_part, result_part);
  1453. }
  1454. if (unsignedp)
  1455. {
  1456. /* Unless we've filled TARGET, the upper regs in a multi-reg value
  1457. need to be zero'd out. */
  1458. if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
  1459. {
  1460. unsigned int i, total_words;
  1461. total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
  1462. for (i = nwords; i < total_words; i++)
  1463. emit_move_insn
  1464. (operand_subword (target,
  1465. backwards ? total_words - i - 1 : i,
  1466. 1, VOIDmode),
  1467. const0_rtx);
  1468. }
  1469. return target;
  1470. }
  1471. /* Signed bit field: sign-extend with two arithmetic shifts. */
  1472. target = expand_shift (LSHIFT_EXPR, mode, target,
  1473. GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
  1474. return expand_shift (RSHIFT_EXPR, mode, target,
  1475. GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
  1476. }
  1477. /* If OP0 is a multi-word register, narrow it to the affected word.
  1478. If the region spans two words, defer to extract_split_bit_field. */
  1479. if (!MEM_P (op0) && GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
  1480. {
  1481. op0 = simplify_gen_subreg (word_mode, op0, GET_MODE (op0),
  1482. bitnum / BITS_PER_WORD * UNITS_PER_WORD);
  1483. bitnum %= BITS_PER_WORD;
  1484. if (bitnum + bitsize > BITS_PER_WORD)
  1485. {
  1486. if (!fallback_p)
  1487. return NULL_RTX;
  1488. target = extract_split_bit_field (op0, bitsize, bitnum, unsignedp);
  1489. return convert_extracted_bit_field (target, mode, tmode, unsignedp);
  1490. }
  1491. }
  1492. /* From here on we know the desired field is smaller than a word.
  1493. If OP0 is a register, it too fits within a word. */
  1494. enum extraction_pattern pattern = unsignedp ? EP_extzv : EP_extv;
  1495. extraction_insn extv;
  1496. if (!MEM_P (op0)
  1497. /* ??? We could limit the structure size to the part of OP0 that
  1498. contains the field, with appropriate checks for endianness
  1499. and TRULY_NOOP_TRUNCATION. */
  1500. && get_best_reg_extraction_insn (&extv, pattern,
  1501. GET_MODE_BITSIZE (GET_MODE (op0)),
  1502. tmode))
  1503. {
  1504. rtx result = extract_bit_field_using_extv (&extv, op0, bitsize, bitnum,
  1505. unsignedp, target, mode,
  1506. tmode);
  1507. if (result)
  1508. return result;
  1509. }
  1510. /* If OP0 is a memory, try copying it to a register and seeing if a
  1511. cheap register alternative is available. */
  1512. if (MEM_P (op0))
  1513. {
  1514. if (get_best_mem_extraction_insn (&extv, pattern, bitsize, bitnum,
  1515. tmode))
  1516. {
  1517. rtx result = extract_bit_field_using_extv (&extv, op0, bitsize,
  1518. bitnum, unsignedp,
  1519. target, mode,
  1520. tmode);
  1521. if (result)
  1522. return result;
  1523. }
  1524. rtx_insn *last = get_last_insn ();
  1525. /* Try loading part of OP0 into a register and extracting the
  1526. bitfield from that. */
  1527. unsigned HOST_WIDE_INT bitpos;
  1528. rtx xop0 = adjust_bit_field_mem_for_reg (pattern, op0, bitsize, bitnum,
  1529. 0, 0, tmode, &bitpos);
  1530. if (xop0)
  1531. {
  1532. xop0 = copy_to_reg (xop0);
  1533. rtx result = extract_bit_field_1 (xop0, bitsize, bitpos,
  1534. unsignedp, target,
  1535. mode, tmode, false);
  1536. if (result)
  1537. return result;
  1538. delete_insns_since (last);
  1539. }
  1540. }
  1541. if (!fallback_p)
  1542. return NULL;
  1543. /* Find a correspondingly-sized integer field, so we can apply
  1544. shifts and masks to it. */
  1545. int_mode = int_mode_for_mode (tmode);
  1546. if (int_mode == BLKmode)
  1547. int_mode = int_mode_for_mode (mode);
  1548. /* Should probably push op0 out to memory and then do a load. */
  1549. gcc_assert (int_mode != BLKmode);
  1550. target = extract_fixed_bit_field (int_mode, op0, bitsize, bitnum,
  1551. target, unsignedp);
  1552. return convert_extracted_bit_field (target, mode, tmode, unsignedp);
  1553. }
  1554. /* Generate code to extract a byte-field from STR_RTX
  1555. containing BITSIZE bits, starting at BITNUM,
  1556. and put it in TARGET if possible (if TARGET is nonzero).
  1557. Regardless of TARGET, we return the rtx for where the value is placed.
  1558. STR_RTX is the structure containing the byte (a REG or MEM).
  1559. UNSIGNEDP is nonzero if this is an unsigned bit field.
  1560. MODE is the natural mode of the field value once extracted.
  1561. TMODE is the mode the caller would like the value to have;
  1562. but the value may be returned with type MODE instead.
  1563. If a TARGET is specified and we can store in it at no extra cost,
  1564. we do so, and return TARGET.
  1565. Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
  1566. if they are equally easy. */
  1567. rtx
  1568. extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
  1569. unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
  1570. machine_mode mode, machine_mode tmode)
  1571. {
  1572. machine_mode mode1;
  1573. /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
  1574. if (GET_MODE_BITSIZE (GET_MODE (str_rtx)) > 0)
  1575. mode1 = GET_MODE (str_rtx);
  1576. else if (target && GET_MODE_BITSIZE (GET_MODE (target)) > 0)
  1577. mode1 = GET_MODE (target);
  1578. else
  1579. mode1 = tmode;
  1580. if (strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, mode1, 0, 0))
  1581. {
  1582. /* Extraction of a full MODE1 value can be done with a simple load.
  1583. We know here that the field can be accessed with one single
  1584. instruction. For targets that support unaligned memory,
  1585. an unaligned access may be necessary. */
  1586. if (bitsize == GET_MODE_BITSIZE (mode1))
  1587. {
  1588. rtx result = adjust_bitfield_address (str_rtx, mode1,
  1589. bitnum / BITS_PER_UNIT);
  1590. gcc_assert (bitnum % BITS_PER_UNIT == 0);
  1591. return convert_extracted_bit_field (result, mode, tmode, unsignedp);
  1592. }
  1593. str_rtx = narrow_bit_field_mem (str_rtx, mode1, bitsize, bitnum,
  1594. &bitnum);
  1595. gcc_assert (bitnum + bitsize <= GET_MODE_BITSIZE (mode1));
  1596. str_rtx = copy_to_reg (str_rtx);
  1597. }
  1598. return extract_bit_field_1 (str_rtx, bitsize, bitnum, unsignedp,
  1599. target, mode, tmode, true);
  1600. }
  1601. /* Use shifts and boolean operations to extract a field of BITSIZE bits
  1602. from bit BITNUM of OP0.
  1603. UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
  1604. If TARGET is nonzero, attempts to store the value there
  1605. and return TARGET, but this is not guaranteed.
  1606. If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
  1607. static rtx
  1608. extract_fixed_bit_field (machine_mode tmode, rtx op0,
  1609. unsigned HOST_WIDE_INT bitsize,
  1610. unsigned HOST_WIDE_INT bitnum, rtx target,
  1611. int unsignedp)
  1612. {
  1613. if (MEM_P (op0))
  1614. {
  1615. machine_mode mode
  1616. = get_best_mode (bitsize, bitnum, 0, 0, MEM_ALIGN (op0), word_mode,
  1617. MEM_VOLATILE_P (op0));
  1618. if (mode == VOIDmode)
  1619. /* The only way this should occur is if the field spans word
  1620. boundaries. */
  1621. return extract_split_bit_field (op0, bitsize, bitnum, unsignedp);
  1622. op0 = narrow_bit_field_mem (op0, mode, bitsize, bitnum, &bitnum);
  1623. }
  1624. return extract_fixed_bit_field_1 (tmode, op0, bitsize, bitnum,
  1625. target, unsignedp);
  1626. }
  1627. /* Helper function for extract_fixed_bit_field, extracts
  1628. the bit field always using the MODE of OP0. */
  1629. static rtx
  1630. extract_fixed_bit_field_1 (machine_mode tmode, rtx op0,
  1631. unsigned HOST_WIDE_INT bitsize,
  1632. unsigned HOST_WIDE_INT bitnum, rtx target,
  1633. int unsignedp)
  1634. {
  1635. machine_mode mode = GET_MODE (op0);
  1636. gcc_assert (SCALAR_INT_MODE_P (mode));
  1637. /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
  1638. for invalid input, such as extract equivalent of f5 from
  1639. gcc.dg/pr48335-2.c. */
  1640. if (BYTES_BIG_ENDIAN)
  1641. /* BITNUM is the distance between our msb and that of OP0.
  1642. Convert it to the distance from the lsb. */
  1643. bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
  1644. /* Now BITNUM is always the distance between the field's lsb and that of OP0.
  1645. We have reduced the big-endian case to the little-endian case. */
  1646. if (unsignedp)
  1647. {
  1648. if (bitnum)
  1649. {
  1650. /* If the field does not already start at the lsb,
  1651. shift it so it does. */
  1652. /* Maybe propagate the target for the shift. */
  1653. rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
  1654. if (tmode != mode)
  1655. subtarget = 0;
  1656. op0 = expand_shift (RSHIFT_EXPR, mode, op0, bitnum, subtarget, 1);
  1657. }
  1658. /* Convert the value to the desired mode. */
  1659. if (mode != tmode)
  1660. op0 = convert_to_mode (tmode, op0, 1);
  1661. /* Unless the msb of the field used to be the msb when we shifted,
  1662. mask out the upper bits. */
  1663. if (GET_MODE_BITSIZE (mode) != bitnum + bitsize)
  1664. return expand_binop (GET_MODE (op0), and_optab, op0,
  1665. mask_rtx (GET_MODE (op0), 0, bitsize, 0),
  1666. target, 1, OPTAB_LIB_WIDEN);
  1667. return op0;
  1668. }
  1669. /* To extract a signed bit-field, first shift its msb to the msb of the word,
  1670. then arithmetic-shift its lsb to the lsb of the word. */
  1671. op0 = force_reg (mode, op0);
  1672. /* Find the narrowest integer mode that contains the field. */
  1673. for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
  1674. mode = GET_MODE_WIDER_MODE (mode))
  1675. if (GET_MODE_BITSIZE (mode) >= bitsize + bitnum)
  1676. {
  1677. op0 = convert_to_mode (mode, op0, 0);
  1678. break;
  1679. }
  1680. if (mode != tmode)
  1681. target = 0;
  1682. if (GET_MODE_BITSIZE (mode) != (bitsize + bitnum))
  1683. {
  1684. int amount = GET_MODE_BITSIZE (mode) - (bitsize + bitnum);
  1685. /* Maybe propagate the target for the shift. */
  1686. rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
  1687. op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  1688. }
  1689. return expand_shift (RSHIFT_EXPR, mode, op0,
  1690. GET_MODE_BITSIZE (mode) - bitsize, target, 0);
  1691. }
  1692. /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
  1693. VALUE << BITPOS. */
  1694. static rtx
  1695. lshift_value (machine_mode mode, unsigned HOST_WIDE_INT value,
  1696. int bitpos)
  1697. {
  1698. return immed_wide_int_const (wi::lshift (value, bitpos), mode);
  1699. }
  1700. /* Extract a bit field that is split across two words
  1701. and return an RTX for the result.
  1702. OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  1703. BITSIZE is the field width; BITPOS, position of its first bit, in the word.
  1704. UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
  1705. static rtx
  1706. extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
  1707. unsigned HOST_WIDE_INT bitpos, int unsignedp)
  1708. {
  1709. unsigned int unit;
  1710. unsigned int bitsdone = 0;
  1711. rtx result = NULL_RTX;
  1712. int first = 1;
  1713. /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
  1714. much at a time. */
  1715. if (REG_P (op0) || GET_CODE (op0) == SUBREG)
  1716. unit = BITS_PER_WORD;
  1717. else
  1718. unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
  1719. while (bitsdone < bitsize)
  1720. {
  1721. unsigned HOST_WIDE_INT thissize;
  1722. rtx part, word;
  1723. unsigned HOST_WIDE_INT thispos;
  1724. unsigned HOST_WIDE_INT offset;
  1725. offset = (bitpos + bitsdone) / unit;
  1726. thispos = (bitpos + bitsdone) % unit;
  1727. /* THISSIZE must not overrun a word boundary. Otherwise,
  1728. extract_fixed_bit_field will call us again, and we will mutually
  1729. recurse forever. */
  1730. thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
  1731. thissize = MIN (thissize, unit - thispos);
  1732. /* If OP0 is a register, then handle OFFSET here.
  1733. When handling multiword bitfields, extract_bit_field may pass
  1734. down a word_mode SUBREG of a larger REG for a bitfield that actually
  1735. crosses a word boundary. Thus, for a SUBREG, we must find
  1736. the current word starting from the base register. */
  1737. if (GET_CODE (op0) == SUBREG)
  1738. {
  1739. int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
  1740. word = operand_subword_force (SUBREG_REG (op0), word_offset,
  1741. GET_MODE (SUBREG_REG (op0)));
  1742. offset = 0;
  1743. }
  1744. else if (REG_P (op0))
  1745. {
  1746. word = operand_subword_force (op0, offset, GET_MODE (op0));
  1747. offset = 0;
  1748. }
  1749. else
  1750. word = op0;
  1751. /* Extract the parts in bit-counting order,
  1752. whose meaning is determined by BYTES_PER_UNIT.
  1753. OFFSET is in UNITs, and UNIT is in bits. */
  1754. part = extract_fixed_bit_field (word_mode, word, thissize,
  1755. offset * unit + thispos, 0, 1);
  1756. bitsdone += thissize;
  1757. /* Shift this part into place for the result. */
  1758. if (BYTES_BIG_ENDIAN)
  1759. {
  1760. if (bitsize != bitsdone)
  1761. part = expand_shift (LSHIFT_EXPR, word_mode, part,
  1762. bitsize - bitsdone, 0, 1);
  1763. }
  1764. else
  1765. {
  1766. if (bitsdone != thissize)
  1767. part = expand_shift (LSHIFT_EXPR, word_mode, part,
  1768. bitsdone - thissize, 0, 1);
  1769. }
  1770. if (first)
  1771. result = part;
  1772. else
  1773. /* Combine the parts with bitwise or. This works
  1774. because we extracted each part as an unsigned bit field. */
  1775. result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
  1776. OPTAB_LIB_WIDEN);
  1777. first = 0;
  1778. }
  1779. /* Unsigned bit field: we are done. */
  1780. if (unsignedp)
  1781. return result;
  1782. /* Signed bit field: sign-extend with two arithmetic shifts. */
  1783. result = expand_shift (LSHIFT_EXPR, word_mode, result,
  1784. BITS_PER_WORD - bitsize, NULL_RTX, 0);
  1785. return expand_shift (RSHIFT_EXPR, word_mode, result,
  1786. BITS_PER_WORD - bitsize, NULL_RTX, 0);
  1787. }
  1788. /* Try to read the low bits of SRC as an rvalue of mode MODE, preserving
  1789. the bit pattern. SRC_MODE is the mode of SRC; if this is smaller than
  1790. MODE, fill the upper bits with zeros. Fail if the layout of either
  1791. mode is unknown (as for CC modes) or if the extraction would involve
  1792. unprofitable mode punning. Return the value on success, otherwise
  1793. return null.
  1794. This is different from gen_lowpart* in these respects:
  1795. - the returned value must always be considered an rvalue
  1796. - when MODE is wider than SRC_MODE, the extraction involves
  1797. a zero extension
  1798. - when MODE is smaller than SRC_MODE, the extraction involves
  1799. a truncation (and is thus subject to TRULY_NOOP_TRUNCATION).
  1800. In other words, this routine performs a computation, whereas the
  1801. gen_lowpart* routines are conceptually lvalue or rvalue subreg
  1802. operations. */
  1803. rtx
  1804. extract_low_bits (machine_mode mode, machine_mode src_mode, rtx src)
  1805. {
  1806. machine_mode int_mode, src_int_mode;
  1807. if (mode == src_mode)
  1808. return src;
  1809. if (CONSTANT_P (src))
  1810. {
  1811. /* simplify_gen_subreg can't be used here, as if simplify_subreg
  1812. fails, it will happily create (subreg (symbol_ref)) or similar
  1813. invalid SUBREGs. */
  1814. unsigned int byte = subreg_lowpart_offset (mode, src_mode);
  1815. rtx ret = simplify_subreg (mode, src, src_mode, byte);
  1816. if (ret)
  1817. return ret;
  1818. if (GET_MODE (src) == VOIDmode
  1819. || !validate_subreg (mode, src_mode, src, byte))
  1820. return NULL_RTX;
  1821. src = force_reg (GET_MODE (src), src);
  1822. return gen_rtx_SUBREG (mode, src, byte);
  1823. }
  1824. if (GET_MODE_CLASS (mode) == MODE_CC || GET_MODE_CLASS (src_mode) == MODE_CC)
  1825. return NULL_RTX;
  1826. if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (src_mode)
  1827. && MODES_TIEABLE_P (mode, src_mode))
  1828. {
  1829. rtx x = gen_lowpart_common (mode, src);
  1830. if (x)
  1831. return x;
  1832. }
  1833. src_int_mode = int_mode_for_mode (src_mode);
  1834. int_mode = int_mode_for_mode (mode);
  1835. if (src_int_mode == BLKmode || int_mode == BLKmode)
  1836. return NULL_RTX;
  1837. if (!MODES_TIEABLE_P (src_int_mode, src_mode))
  1838. return NULL_RTX;
  1839. if (!MODES_TIEABLE_P (int_mode, mode))
  1840. return NULL_RTX;
  1841. src = gen_lowpart (src_int_mode, src);
  1842. src = convert_modes (int_mode, src_int_mode, src, true);
  1843. src = gen_lowpart (mode, src);
  1844. return src;
  1845. }
  1846. /* Add INC into TARGET. */
  1847. void
  1848. expand_inc (rtx target, rtx inc)
  1849. {
  1850. rtx value = expand_binop (GET_MODE (target), add_optab,
  1851. target, inc,
  1852. target, 0, OPTAB_LIB_WIDEN);
  1853. if (value != target)
  1854. emit_move_insn (target, value);
  1855. }
  1856. /* Subtract DEC from TARGET. */
  1857. void
  1858. expand_dec (rtx target, rtx dec)
  1859. {
  1860. rtx value = expand_binop (GET_MODE (target), sub_optab,
  1861. target, dec,
  1862. target, 0, OPTAB_LIB_WIDEN);
  1863. if (value != target)
  1864. emit_move_insn (target, value);
  1865. }
  1866. /* Output a shift instruction for expression code CODE,
  1867. with SHIFTED being the rtx for the value to shift,
  1868. and AMOUNT the rtx for the amount to shift by.
  1869. Store the result in the rtx TARGET, if that is convenient.
  1870. If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  1871. Return the rtx for where the value is. */
  1872. static rtx
  1873. expand_shift_1 (enum tree_code code, machine_mode mode, rtx shifted,
  1874. rtx amount, rtx target, int unsignedp)
  1875. {
  1876. rtx op1, temp = 0;
  1877. int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
  1878. int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
  1879. optab lshift_optab = ashl_optab;
  1880. optab rshift_arith_optab = ashr_optab;
  1881. optab rshift_uns_optab = lshr_optab;
  1882. optab lrotate_optab = rotl_optab;
  1883. optab rrotate_optab = rotr_optab;
  1884. machine_mode op1_mode;
  1885. machine_mode scalar_mode = mode;
  1886. int attempt;
  1887. bool speed = optimize_insn_for_speed_p ();
  1888. if (VECTOR_MODE_P (mode))
  1889. scalar_mode = GET_MODE_INNER (mode);
  1890. op1 = amount;
  1891. op1_mode = GET_MODE (op1);
  1892. /* Determine whether the shift/rotate amount is a vector, or scalar. If the
  1893. shift amount is a vector, use the vector/vector shift patterns. */
  1894. if (VECTOR_MODE_P (mode) && VECTOR_MODE_P (op1_mode))
  1895. {
  1896. lshift_optab = vashl_optab;
  1897. rshift_arith_optab = vashr_optab;
  1898. rshift_uns_optab = vlshr_optab;
  1899. lrotate_optab = vrotl_optab;
  1900. rrotate_optab = vrotr_optab;
  1901. }
  1902. /* Previously detected shift-counts computed by NEGATE_EXPR
  1903. and shifted in the other direction; but that does not work
  1904. on all machines. */
  1905. if (SHIFT_COUNT_TRUNCATED)
  1906. {
  1907. if (CONST_INT_P (op1)
  1908. && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
  1909. (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (scalar_mode)))
  1910. op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
  1911. % GET_MODE_BITSIZE (scalar_mode));
  1912. else if (GET_CODE (op1) == SUBREG
  1913. && subreg_lowpart_p (op1)
  1914. && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op1)))
  1915. && SCALAR_INT_MODE_P (GET_MODE (op1)))
  1916. op1 = SUBREG_REG (op1);
  1917. }
  1918. /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
  1919. prefer left rotation, if op1 is from bitsize / 2 + 1 to
  1920. bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
  1921. amount instead. */
  1922. if (rotate
  1923. && CONST_INT_P (op1)
  1924. && IN_RANGE (INTVAL (op1), GET_MODE_BITSIZE (scalar_mode) / 2 + left,
  1925. GET_MODE_BITSIZE (scalar_mode) - 1))
  1926. {
  1927. op1 = GEN_INT (GET_MODE_BITSIZE (scalar_mode) - INTVAL (op1));
  1928. left = !left;
  1929. code = left ? LROTATE_EXPR : RROTATE_EXPR;
  1930. }
  1931. /* Rotation of 16bit values by 8 bits is effectively equivalent to a bswaphi.
  1932. Note that this is not the case for bigger values. For instance a rotation
  1933. of 0x01020304 by 16 bits gives 0x03040102 which is different from
  1934. 0x04030201 (bswapsi). */
  1935. if (rotate
  1936. && CONST_INT_P (op1)
  1937. && INTVAL (op1) == BITS_PER_UNIT
  1938. && GET_MODE_SIZE (scalar_mode) == 2
  1939. && optab_handler (bswap_optab, HImode) != CODE_FOR_nothing)
  1940. return expand_unop (HImode, bswap_optab, shifted, NULL_RTX,
  1941. unsignedp);
  1942. if (op1 == const0_rtx)
  1943. return shifted;
  1944. /* Check whether its cheaper to implement a left shift by a constant
  1945. bit count by a sequence of additions. */
  1946. if (code == LSHIFT_EXPR
  1947. && CONST_INT_P (op1)
  1948. && INTVAL (op1) > 0
  1949. && INTVAL (op1) < GET_MODE_PRECISION (scalar_mode)
  1950. && INTVAL (op1) < MAX_BITS_PER_WORD
  1951. && (shift_cost (speed, mode, INTVAL (op1))
  1952. > INTVAL (op1) * add_cost (speed, mode))
  1953. && shift_cost (speed, mode, INTVAL (op1)) != MAX_COST)
  1954. {
  1955. int i;
  1956. for (i = 0; i < INTVAL (op1); i++)
  1957. {
  1958. temp = force_reg (mode, shifted);
  1959. shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
  1960. unsignedp, OPTAB_LIB_WIDEN);
  1961. }
  1962. return shifted;
  1963. }
  1964. for (attempt = 0; temp == 0 && attempt < 3; attempt++)
  1965. {
  1966. enum optab_methods methods;
  1967. if (attempt == 0)
  1968. methods = OPTAB_DIRECT;
  1969. else if (attempt == 1)
  1970. methods = OPTAB_WIDEN;
  1971. else
  1972. methods = OPTAB_LIB_WIDEN;
  1973. if (rotate)
  1974. {
  1975. /* Widening does not work for rotation. */
  1976. if (methods == OPTAB_WIDEN)
  1977. continue;
  1978. else if (methods == OPTAB_LIB_WIDEN)
  1979. {
  1980. /* If we have been unable to open-code this by a rotation,
  1981. do it as the IOR of two shifts. I.e., to rotate A
  1982. by N bits, compute
  1983. (A << N) | ((unsigned) A >> ((-N) & (C - 1)))
  1984. where C is the bitsize of A.
  1985. It is theoretically possible that the target machine might
  1986. not be able to perform either shift and hence we would
  1987. be making two libcalls rather than just the one for the
  1988. shift (similarly if IOR could not be done). We will allow
  1989. this extremely unlikely lossage to avoid complicating the
  1990. code below. */
  1991. rtx subtarget = target == shifted ? 0 : target;
  1992. rtx new_amount, other_amount;
  1993. rtx temp1;
  1994. new_amount = op1;
  1995. if (op1 == const0_rtx)
  1996. return shifted;
  1997. else if (CONST_INT_P (op1))
  1998. other_amount = GEN_INT (GET_MODE_BITSIZE (scalar_mode)
  1999. - INTVAL (op1));
  2000. else
  2001. {
  2002. other_amount
  2003. = simplify_gen_unary (NEG, GET_MODE (op1),
  2004. op1, GET_MODE (op1));
  2005. HOST_WIDE_INT mask = GET_MODE_PRECISION (scalar_mode) - 1;
  2006. other_amount
  2007. = simplify_gen_binary (AND, GET_MODE (op1), other_amount,
  2008. gen_int_mode (mask, GET_MODE (op1)));
  2009. }
  2010. shifted = force_reg (mode, shifted);
  2011. temp = expand_shift_1 (left ? LSHIFT_EXPR : RSHIFT_EXPR,
  2012. mode, shifted, new_amount, 0, 1);
  2013. temp1 = expand_shift_1 (left ? RSHIFT_EXPR : LSHIFT_EXPR,
  2014. mode, shifted, other_amount,
  2015. subtarget, 1);
  2016. return expand_binop (mode, ior_optab, temp, temp1, target,
  2017. unsignedp, methods);
  2018. }
  2019. temp = expand_binop (mode,
  2020. left ? lrotate_optab : rrotate_optab,
  2021. shifted, op1, target, unsignedp, methods);
  2022. }
  2023. else if (unsignedp)
  2024. temp = expand_binop (mode,
  2025. left ? lshift_optab : rshift_uns_optab,
  2026. shifted, op1, target, unsignedp, methods);
  2027. /* Do arithmetic shifts.
  2028. Also, if we are going to widen the operand, we can just as well
  2029. use an arithmetic right-shift instead of a logical one. */
  2030. if (temp == 0 && ! rotate
  2031. && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
  2032. {
  2033. enum optab_methods methods1 = methods;
  2034. /* If trying to widen a log shift to an arithmetic shift,
  2035. don't accept an arithmetic shift of the same size. */
  2036. if (unsignedp)
  2037. methods1 = OPTAB_MUST_WIDEN;
  2038. /* Arithmetic shift */
  2039. temp = expand_binop (mode,
  2040. left ? lshift_optab : rshift_arith_optab,
  2041. shifted, op1, target, unsignedp, methods1);
  2042. }
  2043. /* We used to try extzv here for logical right shifts, but that was
  2044. only useful for one machine, the VAX, and caused poor code
  2045. generation there for lshrdi3, so the code was deleted and a
  2046. define_expand for lshrsi3 was added to vax.md. */
  2047. }
  2048. gcc_assert (temp);
  2049. return temp;
  2050. }
  2051. /* Output a shift instruction for expression code CODE,
  2052. with SHIFTED being the rtx for the value to shift,
  2053. and AMOUNT the amount to shift by.
  2054. Store the result in the rtx TARGET, if that is convenient.
  2055. If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  2056. Return the rtx for where the value is. */
  2057. rtx
  2058. expand_shift (enum tree_code code, machine_mode mode, rtx shifted,
  2059. int amount, rtx target, int unsignedp)
  2060. {
  2061. return expand_shift_1 (code, mode,
  2062. shifted, GEN_INT (amount), target, unsignedp);
  2063. }
  2064. /* Output a shift instruction for expression code CODE,
  2065. with SHIFTED being the rtx for the value to shift,
  2066. and AMOUNT the tree for the amount to shift by.
  2067. Store the result in the rtx TARGET, if that is convenient.
  2068. If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  2069. Return the rtx for where the value is. */
  2070. rtx
  2071. expand_variable_shift (enum tree_code code, machine_mode mode, rtx shifted,
  2072. tree amount, rtx target, int unsignedp)
  2073. {
  2074. return expand_shift_1 (code, mode,
  2075. shifted, expand_normal (amount), target, unsignedp);
  2076. }
  2077. /* Indicates the type of fixup needed after a constant multiplication.
  2078. BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
  2079. the result should be negated, and ADD_VARIANT means that the
  2080. multiplicand should be added to the result. */
  2081. enum mult_variant {basic_variant, negate_variant, add_variant};
  2082. static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
  2083. const struct mult_cost *, machine_mode mode);
  2084. static bool choose_mult_variant (machine_mode, HOST_WIDE_INT,
  2085. struct algorithm *, enum mult_variant *, int);
  2086. static rtx expand_mult_const (machine_mode, rtx, HOST_WIDE_INT, rtx,
  2087. const struct algorithm *, enum mult_variant);
  2088. static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
  2089. static rtx extract_high_half (machine_mode, rtx);
  2090. static rtx expmed_mult_highpart (machine_mode, rtx, rtx, rtx, int, int);
  2091. static rtx expmed_mult_highpart_optab (machine_mode, rtx, rtx, rtx,
  2092. int, int);
  2093. /* Compute and return the best algorithm for multiplying by T.
  2094. The algorithm must cost less than cost_limit
  2095. If retval.cost >= COST_LIMIT, no algorithm was found and all
  2096. other field of the returned struct are undefined.
  2097. MODE is the machine mode of the multiplication. */
  2098. static void
  2099. synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
  2100. const struct mult_cost *cost_limit, machine_mode mode)
  2101. {
  2102. int m;
  2103. struct algorithm *alg_in, *best_alg;
  2104. struct mult_cost best_cost;
  2105. struct mult_cost new_limit;
  2106. int op_cost, op_latency;
  2107. unsigned HOST_WIDE_INT orig_t = t;
  2108. unsigned HOST_WIDE_INT q;
  2109. int maxm, hash_index;
  2110. bool cache_hit = false;
  2111. enum alg_code cache_alg = alg_zero;
  2112. bool speed = optimize_insn_for_speed_p ();
  2113. machine_mode imode;
  2114. struct alg_hash_entry *entry_ptr;
  2115. /* Indicate that no algorithm is yet found. If no algorithm
  2116. is found, this value will be returned and indicate failure. */
  2117. alg_out->cost.cost = cost_limit->cost + 1;
  2118. alg_out->cost.latency = cost_limit->latency + 1;
  2119. if (cost_limit->cost < 0
  2120. || (cost_limit->cost == 0 && cost_limit->latency <= 0))
  2121. return;
  2122. /* Be prepared for vector modes. */
  2123. imode = GET_MODE_INNER (mode);
  2124. if (imode == VOIDmode)
  2125. imode = mode;
  2126. maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (imode));
  2127. /* Restrict the bits of "t" to the multiplication's mode. */
  2128. t &= GET_MODE_MASK (imode);
  2129. /* t == 1 can be done in zero cost. */
  2130. if (t == 1)
  2131. {
  2132. alg_out->ops = 1;
  2133. alg_out->cost.cost = 0;
  2134. alg_out->cost.latency = 0;
  2135. alg_out->op[0] = alg_m;
  2136. return;
  2137. }
  2138. /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
  2139. fail now. */
  2140. if (t == 0)
  2141. {
  2142. if (MULT_COST_LESS (cost_limit, zero_cost (speed)))
  2143. return;
  2144. else
  2145. {
  2146. alg_out->ops = 1;
  2147. alg_out->cost.cost = zero_cost (speed);
  2148. alg_out->cost.latency = zero_cost (speed);
  2149. alg_out->op[0] = alg_zero;
  2150. return;
  2151. }
  2152. }
  2153. /* We'll be needing a couple extra algorithm structures now. */
  2154. alg_in = XALLOCA (struct algorithm);
  2155. best_alg = XALLOCA (struct algorithm);
  2156. best_cost = *cost_limit;
  2157. /* Compute the hash index. */
  2158. hash_index = (t ^ (unsigned int) mode ^ (speed * 256)) % NUM_ALG_HASH_ENTRIES;
  2159. /* See if we already know what to do for T. */
  2160. entry_ptr = alg_hash_entry_ptr (hash_index);
  2161. if (entry_ptr->t == t
  2162. && entry_ptr->mode == mode
  2163. && entry_ptr->mode == mode
  2164. && entry_ptr->speed == speed
  2165. && entry_ptr->alg != alg_unknown)
  2166. {
  2167. cache_alg = entry_ptr->alg;
  2168. if (cache_alg == alg_impossible)
  2169. {
  2170. /* The cache tells us that it's impossible to synthesize
  2171. multiplication by T within entry_ptr->cost. */
  2172. if (!CHEAPER_MULT_COST (&entry_ptr->cost, cost_limit))
  2173. /* COST_LIMIT is at least as restrictive as the one
  2174. recorded in the hash table, in which case we have no
  2175. hope of synthesizing a multiplication. Just
  2176. return. */
  2177. return;
  2178. /* If we get here, COST_LIMIT is less restrictive than the
  2179. one recorded in the hash table, so we may be able to
  2180. synthesize a multiplication. Proceed as if we didn't
  2181. have the cache entry. */
  2182. }
  2183. else
  2184. {
  2185. if (CHEAPER_MULT_COST (cost_limit, &entry_ptr->cost))
  2186. /* The cached algorithm shows that this multiplication
  2187. requires more cost than COST_LIMIT. Just return. This
  2188. way, we don't clobber this cache entry with
  2189. alg_impossible but retain useful information. */
  2190. return;
  2191. cache_hit = true;
  2192. switch (cache_alg)
  2193. {
  2194. case alg_shift:
  2195. goto do_alg_shift;
  2196. case alg_add_t_m2:
  2197. case alg_sub_t_m2:
  2198. goto do_alg_addsub_t_m2;
  2199. case alg_add_factor:
  2200. case alg_sub_factor:
  2201. goto do_alg_addsub_factor;
  2202. case alg_add_t2_m:
  2203. goto do_alg_add_t2_m;
  2204. case alg_sub_t2_m:
  2205. goto do_alg_sub_t2_m;
  2206. default:
  2207. gcc_unreachable ();
  2208. }
  2209. }
  2210. }
  2211. /* If we have a group of zero bits at the low-order part of T, try
  2212. multiplying by the remaining bits and then doing a shift. */
  2213. if ((t & 1) == 0)
  2214. {
  2215. do_alg_shift:
  2216. m = floor_log2 (t & -t); /* m = number of low zero bits */
  2217. if (m < maxm)
  2218. {
  2219. q = t >> m;
  2220. /* The function expand_shift will choose between a shift and
  2221. a sequence of additions, so the observed cost is given as
  2222. MIN (m * add_cost(speed, mode), shift_cost(speed, mode, m)). */
  2223. op_cost = m * add_cost (speed, mode);
  2224. if (shift_cost (speed, mode, m) < op_cost)
  2225. op_cost = shift_cost (speed, mode, m);
  2226. new_limit.cost = best_cost.cost - op_cost;
  2227. new_limit.latency = best_cost.latency - op_cost;
  2228. synth_mult (alg_in, q, &new_limit, mode);
  2229. alg_in->cost.cost += op_cost;
  2230. alg_in->cost.latency += op_cost;
  2231. if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
  2232. {
  2233. best_cost = alg_in->cost;
  2234. std::swap (alg_in, best_alg);
  2235. best_alg->log[best_alg->ops] = m;
  2236. best_alg->op[best_alg->ops] = alg_shift;
  2237. }
  2238. /* See if treating ORIG_T as a signed number yields a better
  2239. sequence. Try this sequence only for a negative ORIG_T
  2240. as it would be useless for a non-negative ORIG_T. */
  2241. if ((HOST_WIDE_INT) orig_t < 0)
  2242. {
  2243. /* Shift ORIG_T as follows because a right shift of a
  2244. negative-valued signed type is implementation
  2245. defined. */
  2246. q = ~(~orig_t >> m);
  2247. /* The function expand_shift will choose between a shift
  2248. and a sequence of additions, so the observed cost is
  2249. given as MIN (m * add_cost(speed, mode),
  2250. shift_cost(speed, mode, m)). */
  2251. op_cost = m * add_cost (speed, mode);
  2252. if (shift_cost (speed, mode, m) < op_cost)
  2253. op_cost = shift_cost (speed, mode, m);
  2254. new_limit.cost = best_cost.cost - op_cost;
  2255. new_limit.latency = best_cost.latency - op_cost;
  2256. synth_mult (alg_in, q, &new_limit, mode);
  2257. alg_in->cost.cost += op_cost;
  2258. alg_in->cost.latency += op_cost;
  2259. if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
  2260. {
  2261. best_cost = alg_in->cost;
  2262. std::swap (alg_in, best_alg);
  2263. best_alg->log[best_alg->ops] = m;
  2264. best_alg->op[best_alg->ops] = alg_shift;
  2265. }
  2266. }
  2267. }
  2268. if (cache_hit)
  2269. goto done;
  2270. }
  2271. /* If we have an odd number, add or subtract one. */
  2272. if ((t & 1) != 0)
  2273. {
  2274. unsigned HOST_WIDE_INT w;
  2275. do_alg_addsub_t_m2:
  2276. for (w = 1; (w & t) != 0; w <<= 1)
  2277. ;
  2278. /* If T was -1, then W will be zero after the loop. This is another
  2279. case where T ends with ...111. Handling this with (T + 1) and
  2280. subtract 1 produces slightly better code and results in algorithm
  2281. selection much faster than treating it like the ...0111 case
  2282. below. */
  2283. if (w == 0
  2284. || (w > 2
  2285. /* Reject the case where t is 3.
  2286. Thus we prefer addition in that case. */
  2287. && t != 3))
  2288. {
  2289. /* T ends with ...111. Multiply by (T + 1) and subtract T. */
  2290. op_cost = add_cost (speed, mode);
  2291. new_limit.cost = best_cost.cost - op_cost;
  2292. new_limit.latency = best_cost.latency - op_cost;
  2293. synth_mult (alg_in, t + 1, &new_limit, mode);
  2294. alg_in->cost.cost += op_cost;
  2295. alg_in->cost.latency += op_cost;
  2296. if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
  2297. {
  2298. best_cost = alg_in->cost;
  2299. std::swap (alg_in, best_alg);
  2300. best_alg->log[best_alg->ops] = 0;
  2301. best_alg->op[best_alg->ops] = alg_sub_t_m2;
  2302. }
  2303. }
  2304. else
  2305. {
  2306. /* T ends with ...01 or ...011. Multiply by (T - 1) and add T. */
  2307. op_cost = add_cost (speed, mode);
  2308. new_limit.cost = best_cost.cost - op_cost;
  2309. new_limit.latency = best_cost.latency - op_cost;
  2310. synth_mult (alg_in, t - 1, &new_limit, mode);
  2311. alg_in->cost.cost += op_cost;
  2312. alg_in->cost.latency += op_cost;
  2313. if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
  2314. {
  2315. best_cost = alg_in->cost;
  2316. std::swap (alg_in, best_alg);
  2317. best_alg->log[best_alg->ops] = 0;
  2318. best_alg->op[best_alg->ops] = alg_add_t_m2;
  2319. }
  2320. }
  2321. /* We may be able to calculate a * -7, a * -15, a * -31, etc
  2322. quickly with a - a * n for some appropriate constant n. */
  2323. m = exact_log2 (-orig_t + 1);
  2324. if (m >= 0 && m < maxm)
  2325. {
  2326. op_cost = shiftsub1_cost (speed, mode, m);
  2327. new_limit.cost = best_cost.cost - op_cost;
  2328. new_limit.latency = best_cost.latency - op_cost;
  2329. synth_mult (alg_in, (unsigned HOST_WIDE_INT) (-orig_t + 1) >> m,
  2330. &new_limit, mode);
  2331. alg_in->cost.cost += op_cost;
  2332. alg_in->cost.latency += op_cost;
  2333. if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
  2334. {
  2335. best_cost = alg_in->cost;
  2336. std::swap (alg_in, best_alg);
  2337. best_alg->log[best_alg->ops] = m;
  2338. best_alg->op[best_alg->ops] = alg_sub_t_m2;
  2339. }
  2340. }
  2341. if (cache_hit)
  2342. goto done;
  2343. }
  2344. /* Look for factors of t of the form
  2345. t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
  2346. If we find such a factor, we can multiply by t using an algorithm that
  2347. multiplies by q, shift the result by m and add/subtract it to itself.
  2348. We search for large factors first and loop down, even if large factors
  2349. are less probable than small; if we find a large factor we will find a
  2350. good sequence quickly, and therefore be able to prune (by decreasing
  2351. COST_LIMIT) the search. */
  2352. do_alg_addsub_factor:
  2353. for (m = floor_log2 (t - 1); m >= 2; m--)
  2354. {
  2355. unsigned HOST_WIDE_INT d;
  2356. d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
  2357. if (t % d == 0 && t > d && m < maxm
  2358. && (!cache_hit || cache_alg == alg_add_factor))
  2359. {
  2360. /* If the target has a cheap shift-and-add instruction use
  2361. that in preference to a shift insn followed by an add insn.
  2362. Assume that the shift-and-add is "atomic" with a latency
  2363. equal to its cost, otherwise assume that on superscalar
  2364. hardware the shift may be executed concurrently with the
  2365. earlier steps in the algorithm. */
  2366. op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
  2367. if (shiftadd_cost (speed, mode, m) < op_cost)
  2368. {
  2369. op_cost = shiftadd_cost (speed, mode, m);
  2370. op_latency = op_cost;
  2371. }
  2372. else
  2373. op_latency = add_cost (speed, mode);
  2374. new_limit.cost = best_cost.cost - op_cost;
  2375. new_limit.latency = best_cost.latency - op_latency;
  2376. synth_mult (alg_in, t / d, &new_limit, mode);
  2377. alg_in->cost.cost += op_cost;
  2378. alg_in->cost.latency += op_latency;
  2379. if (alg_in->cost.latency < op_cost)
  2380. alg_in->cost.latency = op_cost;
  2381. if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
  2382. {
  2383. best_cost = alg_in->cost;
  2384. std::swap (alg_in, best_alg);
  2385. best_alg->log[best_alg->ops] = m;
  2386. best_alg->op[best_alg->ops] = alg_add_factor;
  2387. }
  2388. /* Other factors will have been taken care of in the recursion. */
  2389. break;
  2390. }
  2391. d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
  2392. if (t % d == 0 && t > d && m < maxm
  2393. && (!cache_hit || cache_alg == alg_sub_factor))
  2394. {
  2395. /* If the target has a cheap shift-and-subtract insn use
  2396. that in preference to a shift insn followed by a sub insn.
  2397. Assume that the shift-and-sub is "atomic" with a latency
  2398. equal to it's cost, otherwise assume that on superscalar
  2399. hardware the shift may be executed concurrently with the
  2400. earlier steps in the algorithm. */
  2401. op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
  2402. if (shiftsub0_cost (speed, mode, m) < op_cost)
  2403. {
  2404. op_cost = shiftsub0_cost (speed, mode, m);
  2405. op_latency = op_cost;
  2406. }
  2407. else
  2408. op_latency = add_cost (speed, mode);
  2409. new_limit.cost = best_cost.cost - op_cost;
  2410. new_limit.latency = best_cost.latency - op_latency;
  2411. synth_mult (alg_in, t / d, &new_limit, mode);
  2412. alg_in->cost.cost += op_cost;
  2413. alg_in->cost.latency += op_latency;
  2414. if (alg_in->cost.latency < op_cost)
  2415. alg_in->cost.latency = op_cost;
  2416. if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
  2417. {
  2418. best_cost = alg_in->cost;
  2419. std::swap (alg_in, best_alg);
  2420. best_alg->log[best_alg->ops] = m;
  2421. best_alg->op[best_alg->ops] = alg_sub_factor;
  2422. }
  2423. break;
  2424. }
  2425. }
  2426. if (cache_hit)
  2427. goto done;
  2428. /* Try shift-and-add (load effective address) instructions,
  2429. i.e. do a*3, a*5, a*9. */
  2430. if ((t & 1) != 0)
  2431. {
  2432. do_alg_add_t2_m:
  2433. q = t - 1;
  2434. q = q & -q;
  2435. m = exact_log2 (q);
  2436. if (m >= 0 && m < maxm)
  2437. {
  2438. op_cost = shiftadd_cost (speed, mode, m);
  2439. new_limit.cost = best_cost.cost - op_cost;
  2440. new_limit.latency = best_cost.latency - op_cost;
  2441. synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
  2442. alg_in->cost.cost += op_cost;
  2443. alg_in->cost.latency += op_cost;
  2444. if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
  2445. {
  2446. best_cost = alg_in->cost;
  2447. std::swap (alg_in, best_alg);
  2448. best_alg->log[best_alg->ops] = m;
  2449. best_alg->op[best_alg->ops] = alg_add_t2_m;
  2450. }
  2451. }
  2452. if (cache_hit)
  2453. goto done;
  2454. do_alg_sub_t2_m:
  2455. q = t + 1;
  2456. q = q & -q;
  2457. m = exact_log2 (q);
  2458. if (m >= 0 && m < maxm)
  2459. {
  2460. op_cost = shiftsub0_cost (speed, mode, m);
  2461. new_limit.cost = best_cost.cost - op_cost;
  2462. new_limit.latency = best_cost.latency - op_cost;
  2463. synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
  2464. alg_in->cost.cost += op_cost;
  2465. alg_in->cost.latency += op_cost;
  2466. if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
  2467. {
  2468. best_cost = alg_in->cost;
  2469. std::swap (alg_in, best_alg);
  2470. best_alg->log[best_alg->ops] = m;
  2471. best_alg->op[best_alg->ops] = alg_sub_t2_m;
  2472. }
  2473. }
  2474. if (cache_hit)
  2475. goto done;
  2476. }
  2477. done:
  2478. /* If best_cost has not decreased, we have not found any algorithm. */
  2479. if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
  2480. {
  2481. /* We failed to find an algorithm. Record alg_impossible for
  2482. this case (that is, <T, MODE, COST_LIMIT>) so that next time
  2483. we are asked to find an algorithm for T within the same or
  2484. lower COST_LIMIT, we can immediately return to the
  2485. caller. */
  2486. entry_ptr->t = t;
  2487. entry_ptr->mode = mode;
  2488. entry_ptr->speed = speed;
  2489. entry_ptr->alg = alg_impossible;
  2490. entry_ptr->cost = *cost_limit;
  2491. return;
  2492. }
  2493. /* Cache the result. */
  2494. if (!cache_hit)
  2495. {
  2496. entry_ptr->t = t;
  2497. entry_ptr->mode = mode;
  2498. entry_ptr->speed = speed;
  2499. entry_ptr->alg = best_alg->op[best_alg->ops];
  2500. entry_ptr->cost.cost = best_cost.cost;
  2501. entry_ptr->cost.latency = best_cost.latency;
  2502. }
  2503. /* If we are getting a too long sequence for `struct algorithm'
  2504. to record, make this search fail. */
  2505. if (best_alg->ops == MAX_BITS_PER_WORD)
  2506. return;
  2507. /* Copy the algorithm from temporary space to the space at alg_out.
  2508. We avoid using structure assignment because the majority of
  2509. best_alg is normally undefined, and this is a critical function. */
  2510. alg_out->ops = best_alg->ops + 1;
  2511. alg_out->cost = best_cost;
  2512. memcpy (alg_out->op, best_alg->op,
  2513. alg_out->ops * sizeof *alg_out->op);
  2514. memcpy (alg_out->log, best_alg->log,
  2515. alg_out->ops * sizeof *alg_out->log);
  2516. }
  2517. /* Find the cheapest way of multiplying a value of mode MODE by VAL.
  2518. Try three variations:
  2519. - a shift/add sequence based on VAL itself
  2520. - a shift/add sequence based on -VAL, followed by a negation
  2521. - a shift/add sequence based on VAL - 1, followed by an addition.
  2522. Return true if the cheapest of these cost less than MULT_COST,
  2523. describing the algorithm in *ALG and final fixup in *VARIANT. */
  2524. static bool
  2525. choose_mult_variant (machine_mode mode, HOST_WIDE_INT val,
  2526. struct algorithm *alg, enum mult_variant *variant,
  2527. int mult_cost)
  2528. {
  2529. struct algorithm alg2;
  2530. struct mult_cost limit;
  2531. int op_cost;
  2532. bool speed = optimize_insn_for_speed_p ();
  2533. /* Fail quickly for impossible bounds. */
  2534. if (mult_cost < 0)
  2535. return false;
  2536. /* Ensure that mult_cost provides a reasonable upper bound.
  2537. Any constant multiplication can be performed with less
  2538. than 2 * bits additions. */
  2539. op_cost = 2 * GET_MODE_UNIT_BITSIZE (mode) * add_cost (speed, mode);
  2540. if (mult_cost > op_cost)
  2541. mult_cost = op_cost;
  2542. *variant = basic_variant;
  2543. limit.cost = mult_cost;
  2544. limit.latency = mult_cost;
  2545. synth_mult (alg, val, &limit, mode);
  2546. /* This works only if the inverted value actually fits in an
  2547. `unsigned int' */
  2548. if (HOST_BITS_PER_INT >= GET_MODE_UNIT_BITSIZE (mode))
  2549. {
  2550. op_cost = neg_cost (speed, mode);
  2551. if (MULT_COST_LESS (&alg->cost, mult_cost))
  2552. {
  2553. limit.cost = alg->cost.cost - op_cost;
  2554. limit.latency = alg->cost.latency - op_cost;
  2555. }
  2556. else
  2557. {
  2558. limit.cost = mult_cost - op_cost;
  2559. limit.latency = mult_cost - op_cost;
  2560. }
  2561. synth_mult (&alg2, -val, &limit, mode);
  2562. alg2.cost.cost += op_cost;
  2563. alg2.cost.latency += op_cost;
  2564. if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
  2565. *alg = alg2, *variant = negate_variant;
  2566. }
  2567. /* This proves very useful for division-by-constant. */
  2568. op_cost = add_cost (speed, mode);
  2569. if (MULT_COST_LESS (&alg->cost, mult_cost))
  2570. {
  2571. limit.cost = alg->cost.cost - op_cost;
  2572. limit.latency = alg->cost.latency - op_cost;
  2573. }
  2574. else
  2575. {
  2576. limit.cost = mult_cost - op_cost;
  2577. limit.latency = mult_cost - op_cost;
  2578. }
  2579. synth_mult (&alg2, val - 1, &limit, mode);
  2580. alg2.cost.cost += op_cost;
  2581. alg2.cost.latency += op_cost;
  2582. if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
  2583. *alg = alg2, *variant = add_variant;
  2584. return MULT_COST_LESS (&alg->cost, mult_cost);
  2585. }
  2586. /* A subroutine of expand_mult, used for constant multiplications.
  2587. Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
  2588. convenient. Use the shift/add sequence described by ALG and apply
  2589. the final fixup specified by VARIANT. */
  2590. static rtx
  2591. expand_mult_const (machine_mode mode, rtx op0, HOST_WIDE_INT val,
  2592. rtx target, const struct algorithm *alg,
  2593. enum mult_variant variant)
  2594. {
  2595. HOST_WIDE_INT val_so_far;
  2596. rtx_insn *insn;
  2597. rtx accum, tem;
  2598. int opno;
  2599. machine_mode nmode;
  2600. /* Avoid referencing memory over and over and invalid sharing
  2601. on SUBREGs. */
  2602. op0 = force_reg (mode, op0);
  2603. /* ACCUM starts out either as OP0 or as a zero, depending on
  2604. the first operation. */
  2605. if (alg->op[0] == alg_zero)
  2606. {
  2607. accum = copy_to_mode_reg (mode, CONST0_RTX (mode));
  2608. val_so_far = 0;
  2609. }
  2610. else if (alg->op[0] == alg_m)
  2611. {
  2612. accum = copy_to_mode_reg (mode, op0);
  2613. val_so_far = 1;
  2614. }
  2615. else
  2616. gcc_unreachable ();
  2617. for (opno = 1; opno < alg->ops; opno++)
  2618. {
  2619. int log = alg->log[opno];
  2620. rtx shift_subtarget = optimize ? 0 : accum;
  2621. rtx add_target
  2622. = (opno == alg->ops - 1 && target != 0 && variant != add_variant
  2623. && !optimize)
  2624. ? target : 0;
  2625. rtx accum_target = optimize ? 0 : accum;
  2626. rtx accum_inner;
  2627. switch (alg->op[opno])
  2628. {
  2629. case alg_shift:
  2630. tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
  2631. /* REG_EQUAL note will be attached to the following insn. */
  2632. emit_move_insn (accum, tem);
  2633. val_so_far <<= log;
  2634. break;
  2635. case alg_add_t_m2:
  2636. tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
  2637. accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
  2638. add_target ? add_target : accum_target);
  2639. val_so_far += (HOST_WIDE_INT) 1 << log;
  2640. break;
  2641. case alg_sub_t_m2:
  2642. tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
  2643. accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
  2644. add_target ? add_target : accum_target);
  2645. val_so_far -= (HOST_WIDE_INT) 1 << log;
  2646. break;
  2647. case alg_add_t2_m:
  2648. accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2649. log, shift_subtarget, 0);
  2650. accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
  2651. add_target ? add_target : accum_target);
  2652. val_so_far = (val_so_far << log) + 1;
  2653. break;
  2654. case alg_sub_t2_m:
  2655. accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2656. log, shift_subtarget, 0);
  2657. accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
  2658. add_target ? add_target : accum_target);
  2659. val_so_far = (val_so_far << log) - 1;
  2660. break;
  2661. case alg_add_factor:
  2662. tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
  2663. accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
  2664. add_target ? add_target : accum_target);
  2665. val_so_far += val_so_far << log;
  2666. break;
  2667. case alg_sub_factor:
  2668. tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
  2669. accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
  2670. (add_target
  2671. ? add_target : (optimize ? 0 : tem)));
  2672. val_so_far = (val_so_far << log) - val_so_far;
  2673. break;
  2674. default:
  2675. gcc_unreachable ();
  2676. }
  2677. if (SCALAR_INT_MODE_P (mode))
  2678. {
  2679. /* Write a REG_EQUAL note on the last insn so that we can cse
  2680. multiplication sequences. Note that if ACCUM is a SUBREG,
  2681. we've set the inner register and must properly indicate that. */
  2682. tem = op0, nmode = mode;
  2683. accum_inner = accum;
  2684. if (GET_CODE (accum) == SUBREG)
  2685. {
  2686. accum_inner = SUBREG_REG (accum);
  2687. nmode = GET_MODE (accum_inner);
  2688. tem = gen_lowpart (nmode, op0);
  2689. }
  2690. insn = get_last_insn ();
  2691. set_dst_reg_note (insn, REG_EQUAL,
  2692. gen_rtx_MULT (nmode, tem,
  2693. gen_int_mode (val_so_far, nmode)),
  2694. accum_inner);
  2695. }
  2696. }
  2697. if (variant == negate_variant)
  2698. {
  2699. val_so_far = -val_so_far;
  2700. accum = expand_unop (mode, neg_optab, accum, target, 0);
  2701. }
  2702. else if (variant == add_variant)
  2703. {
  2704. val_so_far = val_so_far + 1;
  2705. accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
  2706. }
  2707. /* Compare only the bits of val and val_so_far that are significant
  2708. in the result mode, to avoid sign-/zero-extension confusion. */
  2709. nmode = GET_MODE_INNER (mode);
  2710. if (nmode == VOIDmode)
  2711. nmode = mode;
  2712. val &= GET_MODE_MASK (nmode);
  2713. val_so_far &= GET_MODE_MASK (nmode);
  2714. gcc_assert (val == val_so_far);
  2715. return accum;
  2716. }
  2717. /* Perform a multiplication and return an rtx for the result.
  2718. MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  2719. TARGET is a suggestion for where to store the result (an rtx).
  2720. We check specially for a constant integer as OP1.
  2721. If you want this check for OP0 as well, then before calling
  2722. you should swap the two operands if OP0 would be constant. */
  2723. rtx
  2724. expand_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
  2725. int unsignedp)
  2726. {
  2727. enum mult_variant variant;
  2728. struct algorithm algorithm;
  2729. rtx scalar_op1;
  2730. int max_cost;
  2731. bool speed = optimize_insn_for_speed_p ();
  2732. bool do_trapv = flag_trapv && SCALAR_INT_MODE_P (mode) && !unsignedp;
  2733. if (CONSTANT_P (op0))
  2734. std::swap (op0, op1);
  2735. /* For vectors, there are several simplifications that can be made if
  2736. all elements of the vector constant are identical. */
  2737. scalar_op1 = op1;
  2738. if (GET_CODE (op1) == CONST_VECTOR)
  2739. {
  2740. int i, n = CONST_VECTOR_NUNITS (op1);
  2741. scalar_op1 = CONST_VECTOR_ELT (op1, 0);
  2742. for (i = 1; i < n; ++i)
  2743. if (!rtx_equal_p (scalar_op1, CONST_VECTOR_ELT (op1, i)))
  2744. goto skip_scalar;
  2745. }
  2746. if (INTEGRAL_MODE_P (mode))
  2747. {
  2748. rtx fake_reg;
  2749. HOST_WIDE_INT coeff;
  2750. bool is_neg;
  2751. int mode_bitsize;
  2752. if (op1 == CONST0_RTX (mode))
  2753. return op1;
  2754. if (op1 == CONST1_RTX (mode))
  2755. return op0;
  2756. if (op1 == CONSTM1_RTX (mode))
  2757. return expand_unop (mode, do_trapv ? negv_optab : neg_optab,
  2758. op0, target, 0);
  2759. if (do_trapv)
  2760. goto skip_synth;
  2761. /* If mode is integer vector mode, check if the backend supports
  2762. vector lshift (by scalar or vector) at all. If not, we can't use
  2763. synthetized multiply. */
  2764. if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
  2765. && optab_handler (vashl_optab, mode) == CODE_FOR_nothing
  2766. && optab_handler (ashl_optab, mode) == CODE_FOR_nothing)
  2767. goto skip_synth;
  2768. /* These are the operations that are potentially turned into
  2769. a sequence of shifts and additions. */
  2770. mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
  2771. /* synth_mult does an `unsigned int' multiply. As long as the mode is
  2772. less than or equal in size to `unsigned int' this doesn't matter.
  2773. If the mode is larger than `unsigned int', then synth_mult works
  2774. only if the constant value exactly fits in an `unsigned int' without
  2775. any truncation. This means that multiplying by negative values does
  2776. not work; results are off by 2^32 on a 32 bit machine. */
  2777. if (CONST_INT_P (scalar_op1))
  2778. {
  2779. coeff = INTVAL (scalar_op1);
  2780. is_neg = coeff < 0;
  2781. }
  2782. #if TARGET_SUPPORTS_WIDE_INT
  2783. else if (CONST_WIDE_INT_P (scalar_op1))
  2784. #else
  2785. else if (CONST_DOUBLE_AS_INT_P (scalar_op1))
  2786. #endif
  2787. {
  2788. int shift = wi::exact_log2 (std::make_pair (scalar_op1, mode));
  2789. /* Perfect power of 2 (other than 1, which is handled above). */
  2790. if (shift > 0)
  2791. return expand_shift (LSHIFT_EXPR, mode, op0,
  2792. shift, target, unsignedp);
  2793. else
  2794. goto skip_synth;
  2795. }
  2796. else
  2797. goto skip_synth;
  2798. /* We used to test optimize here, on the grounds that it's better to
  2799. produce a smaller program when -O is not used. But this causes
  2800. such a terrible slowdown sometimes that it seems better to always
  2801. use synth_mult. */
  2802. /* Special case powers of two. */
  2803. if (EXACT_POWER_OF_2_OR_ZERO_P (coeff)
  2804. && !(is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT))
  2805. return expand_shift (LSHIFT_EXPR, mode, op0,
  2806. floor_log2 (coeff), target, unsignedp);
  2807. fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
  2808. /* Attempt to handle multiplication of DImode values by negative
  2809. coefficients, by performing the multiplication by a positive
  2810. multiplier and then inverting the result. */
  2811. if (is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT)
  2812. {
  2813. /* Its safe to use -coeff even for INT_MIN, as the
  2814. result is interpreted as an unsigned coefficient.
  2815. Exclude cost of op0 from max_cost to match the cost
  2816. calculation of the synth_mult. */
  2817. coeff = -(unsigned HOST_WIDE_INT) coeff;
  2818. max_cost = (set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), speed)
  2819. - neg_cost (speed, mode));
  2820. if (max_cost <= 0)
  2821. goto skip_synth;
  2822. /* Special case powers of two. */
  2823. if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
  2824. {
  2825. rtx temp = expand_shift (LSHIFT_EXPR, mode, op0,
  2826. floor_log2 (coeff), target, unsignedp);
  2827. return expand_unop (mode, neg_optab, temp, target, 0);
  2828. }
  2829. if (choose_mult_variant (mode, coeff, &algorithm, &variant,
  2830. max_cost))
  2831. {
  2832. rtx temp = expand_mult_const (mode, op0, coeff, NULL_RTX,
  2833. &algorithm, variant);
  2834. return expand_unop (mode, neg_optab, temp, target, 0);
  2835. }
  2836. goto skip_synth;
  2837. }
  2838. /* Exclude cost of op0 from max_cost to match the cost
  2839. calculation of the synth_mult. */
  2840. max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), speed);
  2841. if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
  2842. return expand_mult_const (mode, op0, coeff, target,
  2843. &algorithm, variant);
  2844. }
  2845. skip_synth:
  2846. /* Expand x*2.0 as x+x. */
  2847. if (CONST_DOUBLE_AS_FLOAT_P (scalar_op1))
  2848. {
  2849. REAL_VALUE_TYPE d;
  2850. REAL_VALUE_FROM_CONST_DOUBLE (d, scalar_op1);
  2851. if (REAL_VALUES_EQUAL (d, dconst2))
  2852. {
  2853. op0 = force_reg (GET_MODE (op0), op0);
  2854. return expand_binop (mode, add_optab, op0, op0,
  2855. target, unsignedp, OPTAB_LIB_WIDEN);
  2856. }
  2857. }
  2858. skip_scalar:
  2859. /* This used to use umul_optab if unsigned, but for non-widening multiply
  2860. there is no difference between signed and unsigned. */
  2861. op0 = expand_binop (mode, do_trapv ? smulv_optab : smul_optab,
  2862. op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  2863. gcc_assert (op0);
  2864. return op0;
  2865. }
  2866. /* Return a cost estimate for multiplying a register by the given
  2867. COEFFicient in the given MODE and SPEED. */
  2868. int
  2869. mult_by_coeff_cost (HOST_WIDE_INT coeff, machine_mode mode, bool speed)
  2870. {
  2871. int max_cost;
  2872. struct algorithm algorithm;
  2873. enum mult_variant variant;
  2874. rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
  2875. max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, fake_reg), speed);
  2876. if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
  2877. return algorithm.cost.cost;
  2878. else
  2879. return max_cost;
  2880. }
  2881. /* Perform a widening multiplication and return an rtx for the result.
  2882. MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  2883. TARGET is a suggestion for where to store the result (an rtx).
  2884. THIS_OPTAB is the optab we should use, it must be either umul_widen_optab
  2885. or smul_widen_optab.
  2886. We check specially for a constant integer as OP1, comparing the
  2887. cost of a widening multiply against the cost of a sequence of shifts
  2888. and adds. */
  2889. rtx
  2890. expand_widening_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
  2891. int unsignedp, optab this_optab)
  2892. {
  2893. bool speed = optimize_insn_for_speed_p ();
  2894. rtx cop1;
  2895. if (CONST_INT_P (op1)
  2896. && GET_MODE (op0) != VOIDmode
  2897. && (cop1 = convert_modes (mode, GET_MODE (op0), op1,
  2898. this_optab == umul_widen_optab))
  2899. && CONST_INT_P (cop1)
  2900. && (INTVAL (cop1) >= 0
  2901. || HWI_COMPUTABLE_MODE_P (mode)))
  2902. {
  2903. HOST_WIDE_INT coeff = INTVAL (cop1);
  2904. int max_cost;
  2905. enum mult_variant variant;
  2906. struct algorithm algorithm;
  2907. if (coeff == 0)
  2908. return CONST0_RTX (mode);
  2909. /* Special case powers of two. */
  2910. if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
  2911. {
  2912. op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
  2913. return expand_shift (LSHIFT_EXPR, mode, op0,
  2914. floor_log2 (coeff), target, unsignedp);
  2915. }
  2916. /* Exclude cost of op0 from max_cost to match the cost
  2917. calculation of the synth_mult. */
  2918. max_cost = mul_widen_cost (speed, mode);
  2919. if (choose_mult_variant (mode, coeff, &algorithm, &variant,
  2920. max_cost))
  2921. {
  2922. op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
  2923. return expand_mult_const (mode, op0, coeff, target,
  2924. &algorithm, variant);
  2925. }
  2926. }
  2927. return expand_binop (mode, this_optab, op0, op1, target,
  2928. unsignedp, OPTAB_LIB_WIDEN);
  2929. }
  2930. /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
  2931. replace division by D, and put the least significant N bits of the result
  2932. in *MULTIPLIER_PTR and return the most significant bit.
  2933. The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
  2934. needed precision is in PRECISION (should be <= N).
  2935. PRECISION should be as small as possible so this function can choose
  2936. multiplier more freely.
  2937. The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
  2938. is to be used for a final right shift is placed in *POST_SHIFT_PTR.
  2939. Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
  2940. where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
  2941. unsigned HOST_WIDE_INT
  2942. choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
  2943. unsigned HOST_WIDE_INT *multiplier_ptr,
  2944. int *post_shift_ptr, int *lgup_ptr)
  2945. {
  2946. int lgup, post_shift;
  2947. int pow, pow2;
  2948. /* lgup = ceil(log2(divisor)); */
  2949. lgup = ceil_log2 (d);
  2950. gcc_assert (lgup <= n);
  2951. pow = n + lgup;
  2952. pow2 = n + lgup - precision;
  2953. /* mlow = 2^(N + lgup)/d */
  2954. wide_int val = wi::set_bit_in_zero (pow, HOST_BITS_PER_DOUBLE_INT);
  2955. wide_int mlow = wi::udiv_trunc (val, d);
  2956. /* mhigh = (2^(N + lgup) + 2^(N + lgup - precision))/d */
  2957. val |= wi::set_bit_in_zero (pow2, HOST_BITS_PER_DOUBLE_INT);
  2958. wide_int mhigh = wi::udiv_trunc (val, d);
  2959. /* If precision == N, then mlow, mhigh exceed 2^N
  2960. (but they do not exceed 2^(N+1)). */
  2961. /* Reduce to lowest terms. */
  2962. for (post_shift = lgup; post_shift > 0; post_shift--)
  2963. {
  2964. unsigned HOST_WIDE_INT ml_lo = wi::extract_uhwi (mlow, 1,
  2965. HOST_BITS_PER_WIDE_INT);
  2966. unsigned HOST_WIDE_INT mh_lo = wi::extract_uhwi (mhigh, 1,
  2967. HOST_BITS_PER_WIDE_INT);
  2968. if (ml_lo >= mh_lo)
  2969. break;
  2970. mlow = wi::uhwi (ml_lo, HOST_BITS_PER_DOUBLE_INT);
  2971. mhigh = wi::uhwi (mh_lo, HOST_BITS_PER_DOUBLE_INT);
  2972. }
  2973. *post_shift_ptr = post_shift;
  2974. *lgup_ptr = lgup;
  2975. if (n < HOST_BITS_PER_WIDE_INT)
  2976. {
  2977. unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
  2978. *multiplier_ptr = mhigh.to_uhwi () & mask;
  2979. return mhigh.to_uhwi () >= mask;
  2980. }
  2981. else
  2982. {
  2983. *multiplier_ptr = mhigh.to_uhwi ();
  2984. return wi::extract_uhwi (mhigh, HOST_BITS_PER_WIDE_INT, 1);
  2985. }
  2986. }
  2987. /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
  2988. congruent to 1 (mod 2**N). */
  2989. static unsigned HOST_WIDE_INT
  2990. invert_mod2n (unsigned HOST_WIDE_INT x, int n)
  2991. {
  2992. /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
  2993. /* The algorithm notes that the choice y = x satisfies
  2994. x*y == 1 mod 2^3, since x is assumed odd.
  2995. Each iteration doubles the number of bits of significance in y. */
  2996. unsigned HOST_WIDE_INT mask;
  2997. unsigned HOST_WIDE_INT y = x;
  2998. int nbit = 3;
  2999. mask = (n == HOST_BITS_PER_WIDE_INT
  3000. ? ~(unsigned HOST_WIDE_INT) 0
  3001. : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
  3002. while (nbit < n)
  3003. {
  3004. y = y * (2 - x*y) & mask; /* Modulo 2^N */
  3005. nbit *= 2;
  3006. }
  3007. return y;
  3008. }
  3009. /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
  3010. flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
  3011. product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
  3012. to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
  3013. become signed.
  3014. The result is put in TARGET if that is convenient.
  3015. MODE is the mode of operation. */
  3016. rtx
  3017. expand_mult_highpart_adjust (machine_mode mode, rtx adj_operand, rtx op0,
  3018. rtx op1, rtx target, int unsignedp)
  3019. {
  3020. rtx tem;
  3021. enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
  3022. tem = expand_shift (RSHIFT_EXPR, mode, op0,
  3023. GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
  3024. tem = expand_and (mode, tem, op1, NULL_RTX);
  3025. adj_operand
  3026. = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
  3027. adj_operand);
  3028. tem = expand_shift (RSHIFT_EXPR, mode, op1,
  3029. GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
  3030. tem = expand_and (mode, tem, op0, NULL_RTX);
  3031. target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
  3032. target);
  3033. return target;
  3034. }
  3035. /* Subroutine of expmed_mult_highpart. Return the MODE high part of OP. */
  3036. static rtx
  3037. extract_high_half (machine_mode mode, rtx op)
  3038. {
  3039. machine_mode wider_mode;
  3040. if (mode == word_mode)
  3041. return gen_highpart (mode, op);
  3042. gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
  3043. wider_mode = GET_MODE_WIDER_MODE (mode);
  3044. op = expand_shift (RSHIFT_EXPR, wider_mode, op,
  3045. GET_MODE_BITSIZE (mode), 0, 1);
  3046. return convert_modes (mode, wider_mode, op, 0);
  3047. }
  3048. /* Like expmed_mult_highpart, but only consider using a multiplication
  3049. optab. OP1 is an rtx for the constant operand. */
  3050. static rtx
  3051. expmed_mult_highpart_optab (machine_mode mode, rtx op0, rtx op1,
  3052. rtx target, int unsignedp, int max_cost)
  3053. {
  3054. rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
  3055. machine_mode wider_mode;
  3056. optab moptab;
  3057. rtx tem;
  3058. int size;
  3059. bool speed = optimize_insn_for_speed_p ();
  3060. gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
  3061. wider_mode = GET_MODE_WIDER_MODE (mode);
  3062. size = GET_MODE_BITSIZE (mode);
  3063. /* Firstly, try using a multiplication insn that only generates the needed
  3064. high part of the product, and in the sign flavor of unsignedp. */
  3065. if (mul_highpart_cost (speed, mode) < max_cost)
  3066. {
  3067. moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
  3068. tem = expand_binop (mode, moptab, op0, narrow_op1, target,
  3069. unsignedp, OPTAB_DIRECT);
  3070. if (tem)
  3071. return tem;
  3072. }
  3073. /* Secondly, same as above, but use sign flavor opposite of unsignedp.
  3074. Need to adjust the result after the multiplication. */
  3075. if (size - 1 < BITS_PER_WORD
  3076. && (mul_highpart_cost (speed, mode)
  3077. + 2 * shift_cost (speed, mode, size-1)
  3078. + 4 * add_cost (speed, mode) < max_cost))
  3079. {
  3080. moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
  3081. tem = expand_binop (mode, moptab, op0, narrow_op1, target,
  3082. unsignedp, OPTAB_DIRECT);
  3083. if (tem)
  3084. /* We used the wrong signedness. Adjust the result. */
  3085. return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
  3086. tem, unsignedp);
  3087. }
  3088. /* Try widening multiplication. */
  3089. moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
  3090. if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
  3091. && mul_widen_cost (speed, wider_mode) < max_cost)
  3092. {
  3093. tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
  3094. unsignedp, OPTAB_WIDEN);
  3095. if (tem)
  3096. return extract_high_half (mode, tem);
  3097. }
  3098. /* Try widening the mode and perform a non-widening multiplication. */
  3099. if (optab_handler (smul_optab, wider_mode) != CODE_FOR_nothing
  3100. && size - 1 < BITS_PER_WORD
  3101. && (mul_cost (speed, wider_mode) + shift_cost (speed, mode, size-1)
  3102. < max_cost))
  3103. {
  3104. rtx_insn *insns;
  3105. rtx wop0, wop1;
  3106. /* We need to widen the operands, for example to ensure the
  3107. constant multiplier is correctly sign or zero extended.
  3108. Use a sequence to clean-up any instructions emitted by
  3109. the conversions if things don't work out. */
  3110. start_sequence ();
  3111. wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
  3112. wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
  3113. tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
  3114. unsignedp, OPTAB_WIDEN);
  3115. insns = get_insns ();
  3116. end_sequence ();
  3117. if (tem)
  3118. {
  3119. emit_insn (insns);
  3120. return extract_high_half (mode, tem);
  3121. }
  3122. }
  3123. /* Try widening multiplication of opposite signedness, and adjust. */
  3124. moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
  3125. if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
  3126. && size - 1 < BITS_PER_WORD
  3127. && (mul_widen_cost (speed, wider_mode)
  3128. + 2 * shift_cost (speed, mode, size-1)
  3129. + 4 * add_cost (speed, mode) < max_cost))
  3130. {
  3131. tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
  3132. NULL_RTX, ! unsignedp, OPTAB_WIDEN);
  3133. if (tem != 0)
  3134. {
  3135. tem = extract_high_half (mode, tem);
  3136. /* We used the wrong signedness. Adjust the result. */
  3137. return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
  3138. target, unsignedp);
  3139. }
  3140. }
  3141. return 0;
  3142. }
  3143. /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
  3144. putting the high half of the result in TARGET if that is convenient,
  3145. and return where the result is. If the operation can not be performed,
  3146. 0 is returned.
  3147. MODE is the mode of operation and result.
  3148. UNSIGNEDP nonzero means unsigned multiply.
  3149. MAX_COST is the total allowed cost for the expanded RTL. */
  3150. static rtx
  3151. expmed_mult_highpart (machine_mode mode, rtx op0, rtx op1,
  3152. rtx target, int unsignedp, int max_cost)
  3153. {
  3154. machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
  3155. unsigned HOST_WIDE_INT cnst1;
  3156. int extra_cost;
  3157. bool sign_adjust = false;
  3158. enum mult_variant variant;
  3159. struct algorithm alg;
  3160. rtx tem;
  3161. bool speed = optimize_insn_for_speed_p ();
  3162. gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
  3163. /* We can't support modes wider than HOST_BITS_PER_INT. */
  3164. gcc_assert (HWI_COMPUTABLE_MODE_P (mode));
  3165. cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
  3166. /* We can't optimize modes wider than BITS_PER_WORD.
  3167. ??? We might be able to perform double-word arithmetic if
  3168. mode == word_mode, however all the cost calculations in
  3169. synth_mult etc. assume single-word operations. */
  3170. if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
  3171. return expmed_mult_highpart_optab (mode, op0, op1, target,
  3172. unsignedp, max_cost);
  3173. extra_cost = shift_cost (speed, mode, GET_MODE_BITSIZE (mode) - 1);
  3174. /* Check whether we try to multiply by a negative constant. */
  3175. if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
  3176. {
  3177. sign_adjust = true;
  3178. extra_cost += add_cost (speed, mode);
  3179. }
  3180. /* See whether shift/add multiplication is cheap enough. */
  3181. if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
  3182. max_cost - extra_cost))
  3183. {
  3184. /* See whether the specialized multiplication optabs are
  3185. cheaper than the shift/add version. */
  3186. tem = expmed_mult_highpart_optab (mode, op0, op1, target, unsignedp,
  3187. alg.cost.cost + extra_cost);
  3188. if (tem)
  3189. return tem;
  3190. tem = convert_to_mode (wider_mode, op0, unsignedp);
  3191. tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
  3192. tem = extract_high_half (mode, tem);
  3193. /* Adjust result for signedness. */
  3194. if (sign_adjust)
  3195. tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
  3196. return tem;
  3197. }
  3198. return expmed_mult_highpart_optab (mode, op0, op1, target,
  3199. unsignedp, max_cost);
  3200. }
  3201. /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
  3202. static rtx
  3203. expand_smod_pow2 (machine_mode mode, rtx op0, HOST_WIDE_INT d)
  3204. {
  3205. rtx result, temp, shift;
  3206. rtx_code_label *label;
  3207. int logd;
  3208. int prec = GET_MODE_PRECISION (mode);
  3209. logd = floor_log2 (d);
  3210. result = gen_reg_rtx (mode);
  3211. /* Avoid conditional branches when they're expensive. */
  3212. if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2
  3213. && optimize_insn_for_speed_p ())
  3214. {
  3215. rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
  3216. mode, 0, -1);
  3217. if (signmask)
  3218. {
  3219. HOST_WIDE_INT masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
  3220. signmask = force_reg (mode, signmask);
  3221. shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
  3222. /* Use the rtx_cost of a LSHIFTRT instruction to determine
  3223. which instruction sequence to use. If logical right shifts
  3224. are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
  3225. use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
  3226. temp = gen_rtx_LSHIFTRT (mode, result, shift);
  3227. if (optab_handler (lshr_optab, mode) == CODE_FOR_nothing
  3228. || (set_src_cost (temp, optimize_insn_for_speed_p ())
  3229. > COSTS_N_INSNS (2)))
  3230. {
  3231. temp = expand_binop (mode, xor_optab, op0, signmask,
  3232. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3233. temp = expand_binop (mode, sub_optab, temp, signmask,
  3234. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3235. temp = expand_binop (mode, and_optab, temp,
  3236. gen_int_mode (masklow, mode),
  3237. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3238. temp = expand_binop (mode, xor_optab, temp, signmask,
  3239. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3240. temp = expand_binop (mode, sub_optab, temp, signmask,
  3241. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3242. }
  3243. else
  3244. {
  3245. signmask = expand_binop (mode, lshr_optab, signmask, shift,
  3246. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3247. signmask = force_reg (mode, signmask);
  3248. temp = expand_binop (mode, add_optab, op0, signmask,
  3249. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3250. temp = expand_binop (mode, and_optab, temp,
  3251. gen_int_mode (masklow, mode),
  3252. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3253. temp = expand_binop (mode, sub_optab, temp, signmask,
  3254. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3255. }
  3256. return temp;
  3257. }
  3258. }
  3259. /* Mask contains the mode's signbit and the significant bits of the
  3260. modulus. By including the signbit in the operation, many targets
  3261. can avoid an explicit compare operation in the following comparison
  3262. against zero. */
  3263. wide_int mask = wi::mask (logd, false, prec);
  3264. mask = wi::set_bit (mask, prec - 1);
  3265. temp = expand_binop (mode, and_optab, op0,
  3266. immed_wide_int_const (mask, mode),
  3267. result, 1, OPTAB_LIB_WIDEN);
  3268. if (temp != result)
  3269. emit_move_insn (result, temp);
  3270. label = gen_label_rtx ();
  3271. do_cmp_and_jump (result, const0_rtx, GE, mode, label);
  3272. temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
  3273. 0, OPTAB_LIB_WIDEN);
  3274. mask = wi::mask (logd, true, prec);
  3275. temp = expand_binop (mode, ior_optab, temp,
  3276. immed_wide_int_const (mask, mode),
  3277. result, 1, OPTAB_LIB_WIDEN);
  3278. temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
  3279. 0, OPTAB_LIB_WIDEN);
  3280. if (temp != result)
  3281. emit_move_insn (result, temp);
  3282. emit_label (label);
  3283. return result;
  3284. }
  3285. /* Expand signed division of OP0 by a power of two D in mode MODE.
  3286. This routine is only called for positive values of D. */
  3287. static rtx
  3288. expand_sdiv_pow2 (machine_mode mode, rtx op0, HOST_WIDE_INT d)
  3289. {
  3290. rtx temp;
  3291. rtx_code_label *label;
  3292. int logd;
  3293. logd = floor_log2 (d);
  3294. if (d == 2
  3295. && BRANCH_COST (optimize_insn_for_speed_p (),
  3296. false) >= 1)
  3297. {
  3298. temp = gen_reg_rtx (mode);
  3299. temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
  3300. temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
  3301. 0, OPTAB_LIB_WIDEN);
  3302. return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
  3303. }
  3304. #ifdef HAVE_conditional_move
  3305. if (BRANCH_COST (optimize_insn_for_speed_p (), false)
  3306. >= 2)
  3307. {
  3308. rtx temp2;
  3309. start_sequence ();
  3310. temp2 = copy_to_mode_reg (mode, op0);
  3311. temp = expand_binop (mode, add_optab, temp2, gen_int_mode (d - 1, mode),
  3312. NULL_RTX, 0, OPTAB_LIB_WIDEN);
  3313. temp = force_reg (mode, temp);
  3314. /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
  3315. temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
  3316. mode, temp, temp2, mode, 0);
  3317. if (temp2)
  3318. {
  3319. rtx_insn *seq = get_insns ();
  3320. end_sequence ();
  3321. emit_insn (seq);
  3322. return expand_shift (RSHIFT_EXPR, mode, temp2, logd, NULL_RTX, 0);
  3323. }
  3324. end_sequence ();
  3325. }
  3326. #endif
  3327. if (BRANCH_COST (optimize_insn_for_speed_p (),
  3328. false) >= 2)
  3329. {
  3330. int ushift = GET_MODE_BITSIZE (mode) - logd;
  3331. temp = gen_reg_rtx (mode);
  3332. temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
  3333. if (GET_MODE_BITSIZE (mode) >= BITS_PER_WORD
  3334. || shift_cost (optimize_insn_for_speed_p (), mode, ushift)
  3335. > COSTS_N_INSNS (1))
  3336. temp = expand_binop (mode, and_optab, temp, gen_int_mode (d - 1, mode),
  3337. NULL_RTX, 0, OPTAB_LIB_WIDEN);
  3338. else
  3339. temp = expand_shift (RSHIFT_EXPR, mode, temp,
  3340. ushift, NULL_RTX, 1);
  3341. temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
  3342. 0, OPTAB_LIB_WIDEN);
  3343. return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
  3344. }
  3345. label = gen_label_rtx ();
  3346. temp = copy_to_mode_reg (mode, op0);
  3347. do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
  3348. expand_inc (temp, gen_int_mode (d - 1, mode));
  3349. emit_label (label);
  3350. return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
  3351. }
  3352. /* Emit the code to divide OP0 by OP1, putting the result in TARGET
  3353. if that is convenient, and returning where the result is.
  3354. You may request either the quotient or the remainder as the result;
  3355. specify REM_FLAG nonzero to get the remainder.
  3356. CODE is the expression code for which kind of division this is;
  3357. it controls how rounding is done. MODE is the machine mode to use.
  3358. UNSIGNEDP nonzero means do unsigned division. */
  3359. /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
  3360. and then correct it by or'ing in missing high bits
  3361. if result of ANDI is nonzero.
  3362. For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
  3363. This could optimize to a bfexts instruction.
  3364. But C doesn't use these operations, so their optimizations are
  3365. left for later. */
  3366. /* ??? For modulo, we don't actually need the highpart of the first product,
  3367. the low part will do nicely. And for small divisors, the second multiply
  3368. can also be a low-part only multiply or even be completely left out.
  3369. E.g. to calculate the remainder of a division by 3 with a 32 bit
  3370. multiply, multiply with 0x55555556 and extract the upper two bits;
  3371. the result is exact for inputs up to 0x1fffffff.
  3372. The input range can be reduced by using cross-sum rules.
  3373. For odd divisors >= 3, the following table gives right shift counts
  3374. so that if a number is shifted by an integer multiple of the given
  3375. amount, the remainder stays the same:
  3376. 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
  3377. 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
  3378. 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
  3379. 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
  3380. 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
  3381. Cross-sum rules for even numbers can be derived by leaving as many bits
  3382. to the right alone as the divisor has zeros to the right.
  3383. E.g. if x is an unsigned 32 bit number:
  3384. (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
  3385. */
  3386. rtx
  3387. expand_divmod (int rem_flag, enum tree_code code, machine_mode mode,
  3388. rtx op0, rtx op1, rtx target, int unsignedp)
  3389. {
  3390. machine_mode compute_mode;
  3391. rtx tquotient;
  3392. rtx quotient = 0, remainder = 0;
  3393. rtx_insn *last;
  3394. int size;
  3395. rtx_insn *insn;
  3396. optab optab1, optab2;
  3397. int op1_is_constant, op1_is_pow2 = 0;
  3398. int max_cost, extra_cost;
  3399. static HOST_WIDE_INT last_div_const = 0;
  3400. bool speed = optimize_insn_for_speed_p ();
  3401. op1_is_constant = CONST_INT_P (op1);
  3402. if (op1_is_constant)
  3403. {
  3404. unsigned HOST_WIDE_INT ext_op1 = UINTVAL (op1);
  3405. if (unsignedp)
  3406. ext_op1 &= GET_MODE_MASK (mode);
  3407. op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
  3408. || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
  3409. }
  3410. /*
  3411. This is the structure of expand_divmod:
  3412. First comes code to fix up the operands so we can perform the operations
  3413. correctly and efficiently.
  3414. Second comes a switch statement with code specific for each rounding mode.
  3415. For some special operands this code emits all RTL for the desired
  3416. operation, for other cases, it generates only a quotient and stores it in
  3417. QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
  3418. to indicate that it has not done anything.
  3419. Last comes code that finishes the operation. If QUOTIENT is set and
  3420. REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
  3421. QUOTIENT is not set, it is computed using trunc rounding.
  3422. We try to generate special code for division and remainder when OP1 is a
  3423. constant. If |OP1| = 2**n we can use shifts and some other fast
  3424. operations. For other values of OP1, we compute a carefully selected
  3425. fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
  3426. by m.
  3427. In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
  3428. half of the product. Different strategies for generating the product are
  3429. implemented in expmed_mult_highpart.
  3430. If what we actually want is the remainder, we generate that by another
  3431. by-constant multiplication and a subtraction. */
  3432. /* We shouldn't be called with OP1 == const1_rtx, but some of the
  3433. code below will malfunction if we are, so check here and handle
  3434. the special case if so. */
  3435. if (op1 == const1_rtx)
  3436. return rem_flag ? const0_rtx : op0;
  3437. /* When dividing by -1, we could get an overflow.
  3438. negv_optab can handle overflows. */
  3439. if (! unsignedp && op1 == constm1_rtx)
  3440. {
  3441. if (rem_flag)
  3442. return const0_rtx;
  3443. return expand_unop (mode, flag_trapv && GET_MODE_CLASS (mode) == MODE_INT
  3444. ? negv_optab : neg_optab, op0, target, 0);
  3445. }
  3446. if (target
  3447. /* Don't use the function value register as a target
  3448. since we have to read it as well as write it,
  3449. and function-inlining gets confused by this. */
  3450. && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
  3451. /* Don't clobber an operand while doing a multi-step calculation. */
  3452. || ((rem_flag || op1_is_constant)
  3453. && (reg_mentioned_p (target, op0)
  3454. || (MEM_P (op0) && MEM_P (target))))
  3455. || reg_mentioned_p (target, op1)
  3456. || (MEM_P (op1) && MEM_P (target))))
  3457. target = 0;
  3458. /* Get the mode in which to perform this computation. Normally it will
  3459. be MODE, but sometimes we can't do the desired operation in MODE.
  3460. If so, pick a wider mode in which we can do the operation. Convert
  3461. to that mode at the start to avoid repeated conversions.
  3462. First see what operations we need. These depend on the expression
  3463. we are evaluating. (We assume that divxx3 insns exist under the
  3464. same conditions that modxx3 insns and that these insns don't normally
  3465. fail. If these assumptions are not correct, we may generate less
  3466. efficient code in some cases.)
  3467. Then see if we find a mode in which we can open-code that operation
  3468. (either a division, modulus, or shift). Finally, check for the smallest
  3469. mode for which we can do the operation with a library call. */
  3470. /* We might want to refine this now that we have division-by-constant
  3471. optimization. Since expmed_mult_highpart tries so many variants, it is
  3472. not straightforward to generalize this. Maybe we should make an array
  3473. of possible modes in init_expmed? Save this for GCC 2.7. */
  3474. optab1 = ((op1_is_pow2 && op1 != const0_rtx)
  3475. ? (unsignedp ? lshr_optab : ashr_optab)
  3476. : (unsignedp ? udiv_optab : sdiv_optab));
  3477. optab2 = ((op1_is_pow2 && op1 != const0_rtx)
  3478. ? optab1
  3479. : (unsignedp ? udivmod_optab : sdivmod_optab));
  3480. for (compute_mode = mode; compute_mode != VOIDmode;
  3481. compute_mode = GET_MODE_WIDER_MODE (compute_mode))
  3482. if (optab_handler (optab1, compute_mode) != CODE_FOR_nothing
  3483. || optab_handler (optab2, compute_mode) != CODE_FOR_nothing)
  3484. break;
  3485. if (compute_mode == VOIDmode)
  3486. for (compute_mode = mode; compute_mode != VOIDmode;
  3487. compute_mode = GET_MODE_WIDER_MODE (compute_mode))
  3488. if (optab_libfunc (optab1, compute_mode)
  3489. || optab_libfunc (optab2, compute_mode))
  3490. break;
  3491. /* If we still couldn't find a mode, use MODE, but expand_binop will
  3492. probably die. */
  3493. if (compute_mode == VOIDmode)
  3494. compute_mode = mode;
  3495. if (target && GET_MODE (target) == compute_mode)
  3496. tquotient = target;
  3497. else
  3498. tquotient = gen_reg_rtx (compute_mode);
  3499. size = GET_MODE_BITSIZE (compute_mode);
  3500. #if 0
  3501. /* It should be possible to restrict the precision to GET_MODE_BITSIZE
  3502. (mode), and thereby get better code when OP1 is a constant. Do that
  3503. later. It will require going over all usages of SIZE below. */
  3504. size = GET_MODE_BITSIZE (mode);
  3505. #endif
  3506. /* Only deduct something for a REM if the last divide done was
  3507. for a different constant. Then set the constant of the last
  3508. divide. */
  3509. max_cost = (unsignedp
  3510. ? udiv_cost (speed, compute_mode)
  3511. : sdiv_cost (speed, compute_mode));
  3512. if (rem_flag && ! (last_div_const != 0 && op1_is_constant
  3513. && INTVAL (op1) == last_div_const))
  3514. max_cost -= (mul_cost (speed, compute_mode)
  3515. + add_cost (speed, compute_mode));
  3516. last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
  3517. /* Now convert to the best mode to use. */
  3518. if (compute_mode != mode)
  3519. {
  3520. op0 = convert_modes (compute_mode, mode, op0, unsignedp);
  3521. op1 = convert_modes (compute_mode, mode, op1, unsignedp);
  3522. /* convert_modes may have placed op1 into a register, so we
  3523. must recompute the following. */
  3524. op1_is_constant = CONST_INT_P (op1);
  3525. op1_is_pow2 = (op1_is_constant
  3526. && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
  3527. || (! unsignedp
  3528. && EXACT_POWER_OF_2_OR_ZERO_P (-UINTVAL (op1))))));
  3529. }
  3530. /* If one of the operands is a volatile MEM, copy it into a register. */
  3531. if (MEM_P (op0) && MEM_VOLATILE_P (op0))
  3532. op0 = force_reg (compute_mode, op0);
  3533. if (MEM_P (op1) && MEM_VOLATILE_P (op1))
  3534. op1 = force_reg (compute_mode, op1);
  3535. /* If we need the remainder or if OP1 is constant, we need to
  3536. put OP0 in a register in case it has any queued subexpressions. */
  3537. if (rem_flag || op1_is_constant)
  3538. op0 = force_reg (compute_mode, op0);
  3539. last = get_last_insn ();
  3540. /* Promote floor rounding to trunc rounding for unsigned operations. */
  3541. if (unsignedp)
  3542. {
  3543. if (code == FLOOR_DIV_EXPR)
  3544. code = TRUNC_DIV_EXPR;
  3545. if (code == FLOOR_MOD_EXPR)
  3546. code = TRUNC_MOD_EXPR;
  3547. if (code == EXACT_DIV_EXPR && op1_is_pow2)
  3548. code = TRUNC_DIV_EXPR;
  3549. }
  3550. if (op1 != const0_rtx)
  3551. switch (code)
  3552. {
  3553. case TRUNC_MOD_EXPR:
  3554. case TRUNC_DIV_EXPR:
  3555. if (op1_is_constant)
  3556. {
  3557. if (unsignedp)
  3558. {
  3559. unsigned HOST_WIDE_INT mh, ml;
  3560. int pre_shift, post_shift;
  3561. int dummy;
  3562. unsigned HOST_WIDE_INT d = (INTVAL (op1)
  3563. & GET_MODE_MASK (compute_mode));
  3564. if (EXACT_POWER_OF_2_OR_ZERO_P (d))
  3565. {
  3566. pre_shift = floor_log2 (d);
  3567. if (rem_flag)
  3568. {
  3569. unsigned HOST_WIDE_INT mask
  3570. = ((unsigned HOST_WIDE_INT) 1 << pre_shift) - 1;
  3571. remainder
  3572. = expand_binop (compute_mode, and_optab, op0,
  3573. gen_int_mode (mask, compute_mode),
  3574. remainder, 1,
  3575. OPTAB_LIB_WIDEN);
  3576. if (remainder)
  3577. return gen_lowpart (mode, remainder);
  3578. }
  3579. quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  3580. pre_shift, tquotient, 1);
  3581. }
  3582. else if (size <= HOST_BITS_PER_WIDE_INT)
  3583. {
  3584. if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
  3585. {
  3586. /* Most significant bit of divisor is set; emit an scc
  3587. insn. */
  3588. quotient = emit_store_flag_force (tquotient, GEU, op0, op1,
  3589. compute_mode, 1, 1);
  3590. }
  3591. else
  3592. {
  3593. /* Find a suitable multiplier and right shift count
  3594. instead of multiplying with D. */
  3595. mh = choose_multiplier (d, size, size,
  3596. &ml, &post_shift, &dummy);
  3597. /* If the suggested multiplier is more than SIZE bits,
  3598. we can do better for even divisors, using an
  3599. initial right shift. */
  3600. if (mh != 0 && (d & 1) == 0)
  3601. {
  3602. pre_shift = floor_log2 (d & -d);
  3603. mh = choose_multiplier (d >> pre_shift, size,
  3604. size - pre_shift,
  3605. &ml, &post_shift, &dummy);
  3606. gcc_assert (!mh);
  3607. }
  3608. else
  3609. pre_shift = 0;
  3610. if (mh != 0)
  3611. {
  3612. rtx t1, t2, t3, t4;
  3613. if (post_shift - 1 >= BITS_PER_WORD)
  3614. goto fail1;
  3615. extra_cost
  3616. = (shift_cost (speed, compute_mode, post_shift - 1)
  3617. + shift_cost (speed, compute_mode, 1)
  3618. + 2 * add_cost (speed, compute_mode));
  3619. t1 = expmed_mult_highpart
  3620. (compute_mode, op0,
  3621. gen_int_mode (ml, compute_mode),
  3622. NULL_RTX, 1, max_cost - extra_cost);
  3623. if (t1 == 0)
  3624. goto fail1;
  3625. t2 = force_operand (gen_rtx_MINUS (compute_mode,
  3626. op0, t1),
  3627. NULL_RTX);
  3628. t3 = expand_shift (RSHIFT_EXPR, compute_mode,
  3629. t2, 1, NULL_RTX, 1);
  3630. t4 = force_operand (gen_rtx_PLUS (compute_mode,
  3631. t1, t3),
  3632. NULL_RTX);
  3633. quotient = expand_shift
  3634. (RSHIFT_EXPR, compute_mode, t4,
  3635. post_shift - 1, tquotient, 1);
  3636. }
  3637. else
  3638. {
  3639. rtx t1, t2;
  3640. if (pre_shift >= BITS_PER_WORD
  3641. || post_shift >= BITS_PER_WORD)
  3642. goto fail1;
  3643. t1 = expand_shift
  3644. (RSHIFT_EXPR, compute_mode, op0,
  3645. pre_shift, NULL_RTX, 1);
  3646. extra_cost
  3647. = (shift_cost (speed, compute_mode, pre_shift)
  3648. + shift_cost (speed, compute_mode, post_shift));
  3649. t2 = expmed_mult_highpart
  3650. (compute_mode, t1,
  3651. gen_int_mode (ml, compute_mode),
  3652. NULL_RTX, 1, max_cost - extra_cost);
  3653. if (t2 == 0)
  3654. goto fail1;
  3655. quotient = expand_shift
  3656. (RSHIFT_EXPR, compute_mode, t2,
  3657. post_shift, tquotient, 1);
  3658. }
  3659. }
  3660. }
  3661. else /* Too wide mode to use tricky code */
  3662. break;
  3663. insn = get_last_insn ();
  3664. if (insn != last)
  3665. set_dst_reg_note (insn, REG_EQUAL,
  3666. gen_rtx_UDIV (compute_mode, op0, op1),
  3667. quotient);
  3668. }
  3669. else /* TRUNC_DIV, signed */
  3670. {
  3671. unsigned HOST_WIDE_INT ml;
  3672. int lgup, post_shift;
  3673. rtx mlr;
  3674. HOST_WIDE_INT d = INTVAL (op1);
  3675. unsigned HOST_WIDE_INT abs_d;
  3676. /* Since d might be INT_MIN, we have to cast to
  3677. unsigned HOST_WIDE_INT before negating to avoid
  3678. undefined signed overflow. */
  3679. abs_d = (d >= 0
  3680. ? (unsigned HOST_WIDE_INT) d
  3681. : - (unsigned HOST_WIDE_INT) d);
  3682. /* n rem d = n rem -d */
  3683. if (rem_flag && d < 0)
  3684. {
  3685. d = abs_d;
  3686. op1 = gen_int_mode (abs_d, compute_mode);
  3687. }
  3688. if (d == 1)
  3689. quotient = op0;
  3690. else if (d == -1)
  3691. quotient = expand_unop (compute_mode, neg_optab, op0,
  3692. tquotient, 0);
  3693. else if (HOST_BITS_PER_WIDE_INT >= size
  3694. && abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
  3695. {
  3696. /* This case is not handled correctly below. */
  3697. quotient = emit_store_flag (tquotient, EQ, op0, op1,
  3698. compute_mode, 1, 1);
  3699. if (quotient == 0)
  3700. goto fail1;
  3701. }
  3702. else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
  3703. && (rem_flag
  3704. ? smod_pow2_cheap (speed, compute_mode)
  3705. : sdiv_pow2_cheap (speed, compute_mode))
  3706. /* We assume that cheap metric is true if the
  3707. optab has an expander for this mode. */
  3708. && ((optab_handler ((rem_flag ? smod_optab
  3709. : sdiv_optab),
  3710. compute_mode)
  3711. != CODE_FOR_nothing)
  3712. || (optab_handler (sdivmod_optab,
  3713. compute_mode)
  3714. != CODE_FOR_nothing)))
  3715. ;
  3716. else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
  3717. {
  3718. if (rem_flag)
  3719. {
  3720. remainder = expand_smod_pow2 (compute_mode, op0, d);
  3721. if (remainder)
  3722. return gen_lowpart (mode, remainder);
  3723. }
  3724. if (sdiv_pow2_cheap (speed, compute_mode)
  3725. && ((optab_handler (sdiv_optab, compute_mode)
  3726. != CODE_FOR_nothing)
  3727. || (optab_handler (sdivmod_optab, compute_mode)
  3728. != CODE_FOR_nothing)))
  3729. quotient = expand_divmod (0, TRUNC_DIV_EXPR,
  3730. compute_mode, op0,
  3731. gen_int_mode (abs_d,
  3732. compute_mode),
  3733. NULL_RTX, 0);
  3734. else
  3735. quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
  3736. /* We have computed OP0 / abs(OP1). If OP1 is negative,
  3737. negate the quotient. */
  3738. if (d < 0)
  3739. {
  3740. insn = get_last_insn ();
  3741. if (insn != last
  3742. && abs_d < ((unsigned HOST_WIDE_INT) 1
  3743. << (HOST_BITS_PER_WIDE_INT - 1)))
  3744. set_dst_reg_note (insn, REG_EQUAL,
  3745. gen_rtx_DIV (compute_mode, op0,
  3746. gen_int_mode
  3747. (abs_d,
  3748. compute_mode)),
  3749. quotient);
  3750. quotient = expand_unop (compute_mode, neg_optab,
  3751. quotient, quotient, 0);
  3752. }
  3753. }
  3754. else if (size <= HOST_BITS_PER_WIDE_INT)
  3755. {
  3756. choose_multiplier (abs_d, size, size - 1,
  3757. &ml, &post_shift, &lgup);
  3758. if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
  3759. {
  3760. rtx t1, t2, t3;
  3761. if (post_shift >= BITS_PER_WORD
  3762. || size - 1 >= BITS_PER_WORD)
  3763. goto fail1;
  3764. extra_cost = (shift_cost (speed, compute_mode, post_shift)
  3765. + shift_cost (speed, compute_mode, size - 1)
  3766. + add_cost (speed, compute_mode));
  3767. t1 = expmed_mult_highpart
  3768. (compute_mode, op0, gen_int_mode (ml, compute_mode),
  3769. NULL_RTX, 0, max_cost - extra_cost);
  3770. if (t1 == 0)
  3771. goto fail1;
  3772. t2 = expand_shift
  3773. (RSHIFT_EXPR, compute_mode, t1,
  3774. post_shift, NULL_RTX, 0);
  3775. t3 = expand_shift
  3776. (RSHIFT_EXPR, compute_mode, op0,
  3777. size - 1, NULL_RTX, 0);
  3778. if (d < 0)
  3779. quotient
  3780. = force_operand (gen_rtx_MINUS (compute_mode,
  3781. t3, t2),
  3782. tquotient);
  3783. else
  3784. quotient
  3785. = force_operand (gen_rtx_MINUS (compute_mode,
  3786. t2, t3),
  3787. tquotient);
  3788. }
  3789. else
  3790. {
  3791. rtx t1, t2, t3, t4;
  3792. if (post_shift >= BITS_PER_WORD
  3793. || size - 1 >= BITS_PER_WORD)
  3794. goto fail1;
  3795. ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
  3796. mlr = gen_int_mode (ml, compute_mode);
  3797. extra_cost = (shift_cost (speed, compute_mode, post_shift)
  3798. + shift_cost (speed, compute_mode, size - 1)
  3799. + 2 * add_cost (speed, compute_mode));
  3800. t1 = expmed_mult_highpart (compute_mode, op0, mlr,
  3801. NULL_RTX, 0,
  3802. max_cost - extra_cost);
  3803. if (t1 == 0)
  3804. goto fail1;
  3805. t2 = force_operand (gen_rtx_PLUS (compute_mode,
  3806. t1, op0),
  3807. NULL_RTX);
  3808. t3 = expand_shift
  3809. (RSHIFT_EXPR, compute_mode, t2,
  3810. post_shift, NULL_RTX, 0);
  3811. t4 = expand_shift
  3812. (RSHIFT_EXPR, compute_mode, op0,
  3813. size - 1, NULL_RTX, 0);
  3814. if (d < 0)
  3815. quotient
  3816. = force_operand (gen_rtx_MINUS (compute_mode,
  3817. t4, t3),
  3818. tquotient);
  3819. else
  3820. quotient
  3821. = force_operand (gen_rtx_MINUS (compute_mode,
  3822. t3, t4),
  3823. tquotient);
  3824. }
  3825. }
  3826. else /* Too wide mode to use tricky code */
  3827. break;
  3828. insn = get_last_insn ();
  3829. if (insn != last)
  3830. set_dst_reg_note (insn, REG_EQUAL,
  3831. gen_rtx_DIV (compute_mode, op0, op1),
  3832. quotient);
  3833. }
  3834. break;
  3835. }
  3836. fail1:
  3837. delete_insns_since (last);
  3838. break;
  3839. case FLOOR_DIV_EXPR:
  3840. case FLOOR_MOD_EXPR:
  3841. /* We will come here only for signed operations. */
  3842. if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
  3843. {
  3844. unsigned HOST_WIDE_INT mh, ml;
  3845. int pre_shift, lgup, post_shift;
  3846. HOST_WIDE_INT d = INTVAL (op1);
  3847. if (d > 0)
  3848. {
  3849. /* We could just as easily deal with negative constants here,
  3850. but it does not seem worth the trouble for GCC 2.6. */
  3851. if (EXACT_POWER_OF_2_OR_ZERO_P (d))
  3852. {
  3853. pre_shift = floor_log2 (d);
  3854. if (rem_flag)
  3855. {
  3856. unsigned HOST_WIDE_INT mask
  3857. = ((unsigned HOST_WIDE_INT) 1 << pre_shift) - 1;
  3858. remainder = expand_binop
  3859. (compute_mode, and_optab, op0,
  3860. gen_int_mode (mask, compute_mode),
  3861. remainder, 0, OPTAB_LIB_WIDEN);
  3862. if (remainder)
  3863. return gen_lowpart (mode, remainder);
  3864. }
  3865. quotient = expand_shift
  3866. (RSHIFT_EXPR, compute_mode, op0,
  3867. pre_shift, tquotient, 0);
  3868. }
  3869. else
  3870. {
  3871. rtx t1, t2, t3, t4;
  3872. mh = choose_multiplier (d, size, size - 1,
  3873. &ml, &post_shift, &lgup);
  3874. gcc_assert (!mh);
  3875. if (post_shift < BITS_PER_WORD
  3876. && size - 1 < BITS_PER_WORD)
  3877. {
  3878. t1 = expand_shift
  3879. (RSHIFT_EXPR, compute_mode, op0,
  3880. size - 1, NULL_RTX, 0);
  3881. t2 = expand_binop (compute_mode, xor_optab, op0, t1,
  3882. NULL_RTX, 0, OPTAB_WIDEN);
  3883. extra_cost = (shift_cost (speed, compute_mode, post_shift)
  3884. + shift_cost (speed, compute_mode, size - 1)
  3885. + 2 * add_cost (speed, compute_mode));
  3886. t3 = expmed_mult_highpart
  3887. (compute_mode, t2, gen_int_mode (ml, compute_mode),
  3888. NULL_RTX, 1, max_cost - extra_cost);
  3889. if (t3 != 0)
  3890. {
  3891. t4 = expand_shift
  3892. (RSHIFT_EXPR, compute_mode, t3,
  3893. post_shift, NULL_RTX, 1);
  3894. quotient = expand_binop (compute_mode, xor_optab,
  3895. t4, t1, tquotient, 0,
  3896. OPTAB_WIDEN);
  3897. }
  3898. }
  3899. }
  3900. }
  3901. else
  3902. {
  3903. rtx nsign, t1, t2, t3, t4;
  3904. t1 = force_operand (gen_rtx_PLUS (compute_mode,
  3905. op0, constm1_rtx), NULL_RTX);
  3906. t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
  3907. 0, OPTAB_WIDEN);
  3908. nsign = expand_shift
  3909. (RSHIFT_EXPR, compute_mode, t2,
  3910. size - 1, NULL_RTX, 0);
  3911. t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
  3912. NULL_RTX);
  3913. t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
  3914. NULL_RTX, 0);
  3915. if (t4)
  3916. {
  3917. rtx t5;
  3918. t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
  3919. NULL_RTX, 0);
  3920. quotient = force_operand (gen_rtx_PLUS (compute_mode,
  3921. t4, t5),
  3922. tquotient);
  3923. }
  3924. }
  3925. }
  3926. if (quotient != 0)
  3927. break;
  3928. delete_insns_since (last);
  3929. /* Try using an instruction that produces both the quotient and
  3930. remainder, using truncation. We can easily compensate the quotient
  3931. or remainder to get floor rounding, once we have the remainder.
  3932. Notice that we compute also the final remainder value here,
  3933. and return the result right away. */
  3934. if (target == 0 || GET_MODE (target) != compute_mode)
  3935. target = gen_reg_rtx (compute_mode);
  3936. if (rem_flag)
  3937. {
  3938. remainder
  3939. = REG_P (target) ? target : gen_reg_rtx (compute_mode);
  3940. quotient = gen_reg_rtx (compute_mode);
  3941. }
  3942. else
  3943. {
  3944. quotient
  3945. = REG_P (target) ? target : gen_reg_rtx (compute_mode);
  3946. remainder = gen_reg_rtx (compute_mode);
  3947. }
  3948. if (expand_twoval_binop (sdivmod_optab, op0, op1,
  3949. quotient, remainder, 0))
  3950. {
  3951. /* This could be computed with a branch-less sequence.
  3952. Save that for later. */
  3953. rtx tem;
  3954. rtx_code_label *label = gen_label_rtx ();
  3955. do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
  3956. tem = expand_binop (compute_mode, xor_optab, op0, op1,
  3957. NULL_RTX, 0, OPTAB_WIDEN);
  3958. do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
  3959. expand_dec (quotient, const1_rtx);
  3960. expand_inc (remainder, op1);
  3961. emit_label (label);
  3962. return gen_lowpart (mode, rem_flag ? remainder : quotient);
  3963. }
  3964. /* No luck with division elimination or divmod. Have to do it
  3965. by conditionally adjusting op0 *and* the result. */
  3966. {
  3967. rtx_code_label *label1, *label2, *label3, *label4, *label5;
  3968. rtx adjusted_op0;
  3969. rtx tem;
  3970. quotient = gen_reg_rtx (compute_mode);
  3971. adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
  3972. label1 = gen_label_rtx ();
  3973. label2 = gen_label_rtx ();
  3974. label3 = gen_label_rtx ();
  3975. label4 = gen_label_rtx ();
  3976. label5 = gen_label_rtx ();
  3977. do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
  3978. do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
  3979. tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  3980. quotient, 0, OPTAB_LIB_WIDEN);
  3981. if (tem != quotient)
  3982. emit_move_insn (quotient, tem);
  3983. emit_jump_insn (gen_jump (label5));
  3984. emit_barrier ();
  3985. emit_label (label1);
  3986. expand_inc (adjusted_op0, const1_rtx);
  3987. emit_jump_insn (gen_jump (label4));
  3988. emit_barrier ();
  3989. emit_label (label2);
  3990. do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
  3991. tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  3992. quotient, 0, OPTAB_LIB_WIDEN);
  3993. if (tem != quotient)
  3994. emit_move_insn (quotient, tem);
  3995. emit_jump_insn (gen_jump (label5));
  3996. emit_barrier ();
  3997. emit_label (label3);
  3998. expand_dec (adjusted_op0, const1_rtx);
  3999. emit_label (label4);
  4000. tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  4001. quotient, 0, OPTAB_LIB_WIDEN);
  4002. if (tem != quotient)
  4003. emit_move_insn (quotient, tem);
  4004. expand_dec (quotient, const1_rtx);
  4005. emit_label (label5);
  4006. }
  4007. break;
  4008. case CEIL_DIV_EXPR:
  4009. case CEIL_MOD_EXPR:
  4010. if (unsignedp)
  4011. {
  4012. if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
  4013. {
  4014. rtx t1, t2, t3;
  4015. unsigned HOST_WIDE_INT d = INTVAL (op1);
  4016. t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  4017. floor_log2 (d), tquotient, 1);
  4018. t2 = expand_binop (compute_mode, and_optab, op0,
  4019. gen_int_mode (d - 1, compute_mode),
  4020. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  4021. t3 = gen_reg_rtx (compute_mode);
  4022. t3 = emit_store_flag (t3, NE, t2, const0_rtx,
  4023. compute_mode, 1, 1);
  4024. if (t3 == 0)
  4025. {
  4026. rtx_code_label *lab;
  4027. lab = gen_label_rtx ();
  4028. do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
  4029. expand_inc (t1, const1_rtx);
  4030. emit_label (lab);
  4031. quotient = t1;
  4032. }
  4033. else
  4034. quotient = force_operand (gen_rtx_PLUS (compute_mode,
  4035. t1, t3),
  4036. tquotient);
  4037. break;
  4038. }
  4039. /* Try using an instruction that produces both the quotient and
  4040. remainder, using truncation. We can easily compensate the
  4041. quotient or remainder to get ceiling rounding, once we have the
  4042. remainder. Notice that we compute also the final remainder
  4043. value here, and return the result right away. */
  4044. if (target == 0 || GET_MODE (target) != compute_mode)
  4045. target = gen_reg_rtx (compute_mode);
  4046. if (rem_flag)
  4047. {
  4048. remainder = (REG_P (target)
  4049. ? target : gen_reg_rtx (compute_mode));
  4050. quotient = gen_reg_rtx (compute_mode);
  4051. }
  4052. else
  4053. {
  4054. quotient = (REG_P (target)
  4055. ? target : gen_reg_rtx (compute_mode));
  4056. remainder = gen_reg_rtx (compute_mode);
  4057. }
  4058. if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
  4059. remainder, 1))
  4060. {
  4061. /* This could be computed with a branch-less sequence.
  4062. Save that for later. */
  4063. rtx_code_label *label = gen_label_rtx ();
  4064. do_cmp_and_jump (remainder, const0_rtx, EQ,
  4065. compute_mode, label);
  4066. expand_inc (quotient, const1_rtx);
  4067. expand_dec (remainder, op1);
  4068. emit_label (label);
  4069. return gen_lowpart (mode, rem_flag ? remainder : quotient);
  4070. }
  4071. /* No luck with division elimination or divmod. Have to do it
  4072. by conditionally adjusting op0 *and* the result. */
  4073. {
  4074. rtx_code_label *label1, *label2;
  4075. rtx adjusted_op0, tem;
  4076. quotient = gen_reg_rtx (compute_mode);
  4077. adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
  4078. label1 = gen_label_rtx ();
  4079. label2 = gen_label_rtx ();
  4080. do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
  4081. compute_mode, label1);
  4082. emit_move_insn (quotient, const0_rtx);
  4083. emit_jump_insn (gen_jump (label2));
  4084. emit_barrier ();
  4085. emit_label (label1);
  4086. expand_dec (adjusted_op0, const1_rtx);
  4087. tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
  4088. quotient, 1, OPTAB_LIB_WIDEN);
  4089. if (tem != quotient)
  4090. emit_move_insn (quotient, tem);
  4091. expand_inc (quotient, const1_rtx);
  4092. emit_label (label2);
  4093. }
  4094. }
  4095. else /* signed */
  4096. {
  4097. if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
  4098. && INTVAL (op1) >= 0)
  4099. {
  4100. /* This is extremely similar to the code for the unsigned case
  4101. above. For 2.7 we should merge these variants, but for
  4102. 2.6.1 I don't want to touch the code for unsigned since that
  4103. get used in C. The signed case will only be used by other
  4104. languages (Ada). */
  4105. rtx t1, t2, t3;
  4106. unsigned HOST_WIDE_INT d = INTVAL (op1);
  4107. t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  4108. floor_log2 (d), tquotient, 0);
  4109. t2 = expand_binop (compute_mode, and_optab, op0,
  4110. gen_int_mode (d - 1, compute_mode),
  4111. NULL_RTX, 1, OPTAB_LIB_WIDEN);
  4112. t3 = gen_reg_rtx (compute_mode);
  4113. t3 = emit_store_flag (t3, NE, t2, const0_rtx,
  4114. compute_mode, 1, 1);
  4115. if (t3 == 0)
  4116. {
  4117. rtx_code_label *lab;
  4118. lab = gen_label_rtx ();
  4119. do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
  4120. expand_inc (t1, const1_rtx);
  4121. emit_label (lab);
  4122. quotient = t1;
  4123. }
  4124. else
  4125. quotient = force_operand (gen_rtx_PLUS (compute_mode,
  4126. t1, t3),
  4127. tquotient);
  4128. break;
  4129. }
  4130. /* Try using an instruction that produces both the quotient and
  4131. remainder, using truncation. We can easily compensate the
  4132. quotient or remainder to get ceiling rounding, once we have the
  4133. remainder. Notice that we compute also the final remainder
  4134. value here, and return the result right away. */
  4135. if (target == 0 || GET_MODE (target) != compute_mode)
  4136. target = gen_reg_rtx (compute_mode);
  4137. if (rem_flag)
  4138. {
  4139. remainder= (REG_P (target)
  4140. ? target : gen_reg_rtx (compute_mode));
  4141. quotient = gen_reg_rtx (compute_mode);
  4142. }
  4143. else
  4144. {
  4145. quotient = (REG_P (target)
  4146. ? target : gen_reg_rtx (compute_mode));
  4147. remainder = gen_reg_rtx (compute_mode);
  4148. }
  4149. if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
  4150. remainder, 0))
  4151. {
  4152. /* This could be computed with a branch-less sequence.
  4153. Save that for later. */
  4154. rtx tem;
  4155. rtx_code_label *label = gen_label_rtx ();
  4156. do_cmp_and_jump (remainder, const0_rtx, EQ,
  4157. compute_mode, label);
  4158. tem = expand_binop (compute_mode, xor_optab, op0, op1,
  4159. NULL_RTX, 0, OPTAB_WIDEN);
  4160. do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
  4161. expand_inc (quotient, const1_rtx);
  4162. expand_dec (remainder, op1);
  4163. emit_label (label);
  4164. return gen_lowpart (mode, rem_flag ? remainder : quotient);
  4165. }
  4166. /* No luck with division elimination or divmod. Have to do it
  4167. by conditionally adjusting op0 *and* the result. */
  4168. {
  4169. rtx_code_label *label1, *label2, *label3, *label4, *label5;
  4170. rtx adjusted_op0;
  4171. rtx tem;
  4172. quotient = gen_reg_rtx (compute_mode);
  4173. adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
  4174. label1 = gen_label_rtx ();
  4175. label2 = gen_label_rtx ();
  4176. label3 = gen_label_rtx ();
  4177. label4 = gen_label_rtx ();
  4178. label5 = gen_label_rtx ();
  4179. do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
  4180. do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
  4181. compute_mode, label1);
  4182. tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  4183. quotient, 0, OPTAB_LIB_WIDEN);
  4184. if (tem != quotient)
  4185. emit_move_insn (quotient, tem);
  4186. emit_jump_insn (gen_jump (label5));
  4187. emit_barrier ();
  4188. emit_label (label1);
  4189. expand_dec (adjusted_op0, const1_rtx);
  4190. emit_jump_insn (gen_jump (label4));
  4191. emit_barrier ();
  4192. emit_label (label2);
  4193. do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
  4194. compute_mode, label3);
  4195. tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  4196. quotient, 0, OPTAB_LIB_WIDEN);
  4197. if (tem != quotient)
  4198. emit_move_insn (quotient, tem);
  4199. emit_jump_insn (gen_jump (label5));
  4200. emit_barrier ();
  4201. emit_label (label3);
  4202. expand_inc (adjusted_op0, const1_rtx);
  4203. emit_label (label4);
  4204. tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  4205. quotient, 0, OPTAB_LIB_WIDEN);
  4206. if (tem != quotient)
  4207. emit_move_insn (quotient, tem);
  4208. expand_inc (quotient, const1_rtx);
  4209. emit_label (label5);
  4210. }
  4211. }
  4212. break;
  4213. case EXACT_DIV_EXPR:
  4214. if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
  4215. {
  4216. HOST_WIDE_INT d = INTVAL (op1);
  4217. unsigned HOST_WIDE_INT ml;
  4218. int pre_shift;
  4219. rtx t1;
  4220. pre_shift = floor_log2 (d & -d);
  4221. ml = invert_mod2n (d >> pre_shift, size);
  4222. t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  4223. pre_shift, NULL_RTX, unsignedp);
  4224. quotient = expand_mult (compute_mode, t1,
  4225. gen_int_mode (ml, compute_mode),
  4226. NULL_RTX, 1);
  4227. insn = get_last_insn ();
  4228. set_dst_reg_note (insn, REG_EQUAL,
  4229. gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
  4230. compute_mode, op0, op1),
  4231. quotient);
  4232. }
  4233. break;
  4234. case ROUND_DIV_EXPR:
  4235. case ROUND_MOD_EXPR:
  4236. if (unsignedp)
  4237. {
  4238. rtx tem;
  4239. rtx_code_label *label;
  4240. label = gen_label_rtx ();
  4241. quotient = gen_reg_rtx (compute_mode);
  4242. remainder = gen_reg_rtx (compute_mode);
  4243. if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
  4244. {
  4245. rtx tem;
  4246. quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
  4247. quotient, 1, OPTAB_LIB_WIDEN);
  4248. tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
  4249. remainder = expand_binop (compute_mode, sub_optab, op0, tem,
  4250. remainder, 1, OPTAB_LIB_WIDEN);
  4251. }
  4252. tem = plus_constant (compute_mode, op1, -1);
  4253. tem = expand_shift (RSHIFT_EXPR, compute_mode, tem, 1, NULL_RTX, 1);
  4254. do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
  4255. expand_inc (quotient, const1_rtx);
  4256. expand_dec (remainder, op1);
  4257. emit_label (label);
  4258. }
  4259. else
  4260. {
  4261. rtx abs_rem, abs_op1, tem, mask;
  4262. rtx_code_label *label;
  4263. label = gen_label_rtx ();
  4264. quotient = gen_reg_rtx (compute_mode);
  4265. remainder = gen_reg_rtx (compute_mode);
  4266. if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
  4267. {
  4268. rtx tem;
  4269. quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
  4270. quotient, 0, OPTAB_LIB_WIDEN);
  4271. tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
  4272. remainder = expand_binop (compute_mode, sub_optab, op0, tem,
  4273. remainder, 0, OPTAB_LIB_WIDEN);
  4274. }
  4275. abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
  4276. abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
  4277. tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
  4278. 1, NULL_RTX, 1);
  4279. do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
  4280. tem = expand_binop (compute_mode, xor_optab, op0, op1,
  4281. NULL_RTX, 0, OPTAB_WIDEN);
  4282. mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
  4283. size - 1, NULL_RTX, 0);
  4284. tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
  4285. NULL_RTX, 0, OPTAB_WIDEN);
  4286. tem = expand_binop (compute_mode, sub_optab, tem, mask,
  4287. NULL_RTX, 0, OPTAB_WIDEN);
  4288. expand_inc (quotient, tem);
  4289. tem = expand_binop (compute_mode, xor_optab, mask, op1,
  4290. NULL_RTX, 0, OPTAB_WIDEN);
  4291. tem = expand_binop (compute_mode, sub_optab, tem, mask,
  4292. NULL_RTX, 0, OPTAB_WIDEN);
  4293. expand_dec (remainder, tem);
  4294. emit_label (label);
  4295. }
  4296. return gen_lowpart (mode, rem_flag ? remainder : quotient);
  4297. default:
  4298. gcc_unreachable ();
  4299. }
  4300. if (quotient == 0)
  4301. {
  4302. if (target && GET_MODE (target) != compute_mode)
  4303. target = 0;
  4304. if (rem_flag)
  4305. {
  4306. /* Try to produce the remainder without producing the quotient.
  4307. If we seem to have a divmod pattern that does not require widening,
  4308. don't try widening here. We should really have a WIDEN argument
  4309. to expand_twoval_binop, since what we'd really like to do here is
  4310. 1) try a mod insn in compute_mode
  4311. 2) try a divmod insn in compute_mode
  4312. 3) try a div insn in compute_mode and multiply-subtract to get
  4313. remainder
  4314. 4) try the same things with widening allowed. */
  4315. remainder
  4316. = sign_expand_binop (compute_mode, umod_optab, smod_optab,
  4317. op0, op1, target,
  4318. unsignedp,
  4319. ((optab_handler (optab2, compute_mode)
  4320. != CODE_FOR_nothing)
  4321. ? OPTAB_DIRECT : OPTAB_WIDEN));
  4322. if (remainder == 0)
  4323. {
  4324. /* No luck there. Can we do remainder and divide at once
  4325. without a library call? */
  4326. remainder = gen_reg_rtx (compute_mode);
  4327. if (! expand_twoval_binop ((unsignedp
  4328. ? udivmod_optab
  4329. : sdivmod_optab),
  4330. op0, op1,
  4331. NULL_RTX, remainder, unsignedp))
  4332. remainder = 0;
  4333. }
  4334. if (remainder)
  4335. return gen_lowpart (mode, remainder);
  4336. }
  4337. /* Produce the quotient. Try a quotient insn, but not a library call.
  4338. If we have a divmod in this mode, use it in preference to widening
  4339. the div (for this test we assume it will not fail). Note that optab2
  4340. is set to the one of the two optabs that the call below will use. */
  4341. quotient
  4342. = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
  4343. op0, op1, rem_flag ? NULL_RTX : target,
  4344. unsignedp,
  4345. ((optab_handler (optab2, compute_mode)
  4346. != CODE_FOR_nothing)
  4347. ? OPTAB_DIRECT : OPTAB_WIDEN));
  4348. if (quotient == 0)
  4349. {
  4350. /* No luck there. Try a quotient-and-remainder insn,
  4351. keeping the quotient alone. */
  4352. quotient = gen_reg_rtx (compute_mode);
  4353. if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  4354. op0, op1,
  4355. quotient, NULL_RTX, unsignedp))
  4356. {
  4357. quotient = 0;
  4358. if (! rem_flag)
  4359. /* Still no luck. If we are not computing the remainder,
  4360. use a library call for the quotient. */
  4361. quotient = sign_expand_binop (compute_mode,
  4362. udiv_optab, sdiv_optab,
  4363. op0, op1, target,
  4364. unsignedp, OPTAB_LIB_WIDEN);
  4365. }
  4366. }
  4367. }
  4368. if (rem_flag)
  4369. {
  4370. if (target && GET_MODE (target) != compute_mode)
  4371. target = 0;
  4372. if (quotient == 0)
  4373. {
  4374. /* No divide instruction either. Use library for remainder. */
  4375. remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
  4376. op0, op1, target,
  4377. unsignedp, OPTAB_LIB_WIDEN);
  4378. /* No remainder function. Try a quotient-and-remainder
  4379. function, keeping the remainder. */
  4380. if (!remainder)
  4381. {
  4382. remainder = gen_reg_rtx (compute_mode);
  4383. if (!expand_twoval_binop_libfunc
  4384. (unsignedp ? udivmod_optab : sdivmod_optab,
  4385. op0, op1,
  4386. NULL_RTX, remainder,
  4387. unsignedp ? UMOD : MOD))
  4388. remainder = NULL_RTX;
  4389. }
  4390. }
  4391. else
  4392. {
  4393. /* We divided. Now finish doing X - Y * (X / Y). */
  4394. remainder = expand_mult (compute_mode, quotient, op1,
  4395. NULL_RTX, unsignedp);
  4396. remainder = expand_binop (compute_mode, sub_optab, op0,
  4397. remainder, target, unsignedp,
  4398. OPTAB_LIB_WIDEN);
  4399. }
  4400. }
  4401. return gen_lowpart (mode, rem_flag ? remainder : quotient);
  4402. }
  4403. /* Return a tree node with data type TYPE, describing the value of X.
  4404. Usually this is an VAR_DECL, if there is no obvious better choice.
  4405. X may be an expression, however we only support those expressions
  4406. generated by loop.c. */
  4407. tree
  4408. make_tree (tree type, rtx x)
  4409. {
  4410. tree t;
  4411. switch (GET_CODE (x))
  4412. {
  4413. case CONST_INT:
  4414. case CONST_WIDE_INT:
  4415. t = wide_int_to_tree (type, std::make_pair (x, TYPE_MODE (type)));
  4416. return t;
  4417. case CONST_DOUBLE:
  4418. STATIC_ASSERT (HOST_BITS_PER_WIDE_INT * 2 <= MAX_BITSIZE_MODE_ANY_INT);
  4419. if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
  4420. t = wide_int_to_tree (type,
  4421. wide_int::from_array (&CONST_DOUBLE_LOW (x), 2,
  4422. HOST_BITS_PER_WIDE_INT * 2));
  4423. else
  4424. {
  4425. REAL_VALUE_TYPE d;
  4426. REAL_VALUE_FROM_CONST_DOUBLE (d, x);
  4427. t = build_real (type, d);
  4428. }
  4429. return t;
  4430. case CONST_VECTOR:
  4431. {
  4432. int units = CONST_VECTOR_NUNITS (x);
  4433. tree itype = TREE_TYPE (type);
  4434. tree *elts;
  4435. int i;
  4436. /* Build a tree with vector elements. */
  4437. elts = XALLOCAVEC (tree, units);
  4438. for (i = units - 1; i >= 0; --i)
  4439. {
  4440. rtx elt = CONST_VECTOR_ELT (x, i);
  4441. elts[i] = make_tree (itype, elt);
  4442. }
  4443. return build_vector (type, elts);
  4444. }
  4445. case PLUS:
  4446. return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
  4447. make_tree (type, XEXP (x, 1)));
  4448. case MINUS:
  4449. return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
  4450. make_tree (type, XEXP (x, 1)));
  4451. case NEG:
  4452. return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
  4453. case MULT:
  4454. return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
  4455. make_tree (type, XEXP (x, 1)));
  4456. case ASHIFT:
  4457. return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
  4458. make_tree (type, XEXP (x, 1)));
  4459. case LSHIFTRT:
  4460. t = unsigned_type_for (type);
  4461. return fold_convert (type, build2 (RSHIFT_EXPR, t,
  4462. make_tree (t, XEXP (x, 0)),
  4463. make_tree (type, XEXP (x, 1))));
  4464. case ASHIFTRT:
  4465. t = signed_type_for (type);
  4466. return fold_convert (type, build2 (RSHIFT_EXPR, t,
  4467. make_tree (t, XEXP (x, 0)),
  4468. make_tree (type, XEXP (x, 1))));
  4469. case DIV:
  4470. if (TREE_CODE (type) != REAL_TYPE)
  4471. t = signed_type_for (type);
  4472. else
  4473. t = type;
  4474. return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
  4475. make_tree (t, XEXP (x, 0)),
  4476. make_tree (t, XEXP (x, 1))));
  4477. case UDIV:
  4478. t = unsigned_type_for (type);
  4479. return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
  4480. make_tree (t, XEXP (x, 0)),
  4481. make_tree (t, XEXP (x, 1))));
  4482. case SIGN_EXTEND:
  4483. case ZERO_EXTEND:
  4484. t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
  4485. GET_CODE (x) == ZERO_EXTEND);
  4486. return fold_convert (type, make_tree (t, XEXP (x, 0)));
  4487. case CONST:
  4488. return make_tree (type, XEXP (x, 0));
  4489. case SYMBOL_REF:
  4490. t = SYMBOL_REF_DECL (x);
  4491. if (t)
  4492. return fold_convert (type, build_fold_addr_expr (t));
  4493. /* else fall through. */
  4494. default:
  4495. t = build_decl (RTL_LOCATION (x), VAR_DECL, NULL_TREE, type);
  4496. /* If TYPE is a POINTER_TYPE, we might need to convert X from
  4497. address mode to pointer mode. */
  4498. if (POINTER_TYPE_P (type))
  4499. x = convert_memory_address_addr_space
  4500. (TYPE_MODE (type), x, TYPE_ADDR_SPACE (TREE_TYPE (type)));
  4501. /* Note that we do *not* use SET_DECL_RTL here, because we do not
  4502. want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
  4503. t->decl_with_rtl.rtl = x;
  4504. return t;
  4505. }
  4506. }
  4507. /* Compute the logical-and of OP0 and OP1, storing it in TARGET
  4508. and returning TARGET.
  4509. If TARGET is 0, a pseudo-register or constant is returned. */
  4510. rtx
  4511. expand_and (machine_mode mode, rtx op0, rtx op1, rtx target)
  4512. {
  4513. rtx tem = 0;
  4514. if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
  4515. tem = simplify_binary_operation (AND, mode, op0, op1);
  4516. if (tem == 0)
  4517. tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
  4518. if (target == 0)
  4519. target = tem;
  4520. else if (tem != target)
  4521. emit_move_insn (target, tem);
  4522. return target;
  4523. }
  4524. /* Helper function for emit_store_flag. */
  4525. rtx
  4526. emit_cstore (rtx target, enum insn_code icode, enum rtx_code code,
  4527. machine_mode mode, machine_mode compare_mode,
  4528. int unsignedp, rtx x, rtx y, int normalizep,
  4529. machine_mode target_mode)
  4530. {
  4531. struct expand_operand ops[4];
  4532. rtx op0, comparison, subtarget;
  4533. rtx_insn *last;
  4534. machine_mode result_mode = targetm.cstore_mode (icode);
  4535. last = get_last_insn ();
  4536. x = prepare_operand (icode, x, 2, mode, compare_mode, unsignedp);
  4537. y = prepare_operand (icode, y, 3, mode, compare_mode, unsignedp);
  4538. if (!x || !y)
  4539. {
  4540. delete_insns_since (last);
  4541. return NULL_RTX;
  4542. }
  4543. if (target_mode == VOIDmode)
  4544. target_mode = result_mode;
  4545. if (!target)
  4546. target = gen_reg_rtx (target_mode);
  4547. comparison = gen_rtx_fmt_ee (code, result_mode, x, y);
  4548. create_output_operand (&ops[0], optimize ? NULL_RTX : target, result_mode);
  4549. create_fixed_operand (&ops[1], comparison);
  4550. create_fixed_operand (&ops[2], x);
  4551. create_fixed_operand (&ops[3], y);
  4552. if (!maybe_expand_insn (icode, 4, ops))
  4553. {
  4554. delete_insns_since (last);
  4555. return NULL_RTX;
  4556. }
  4557. subtarget = ops[0].value;
  4558. /* If we are converting to a wider mode, first convert to
  4559. TARGET_MODE, then normalize. This produces better combining
  4560. opportunities on machines that have a SIGN_EXTRACT when we are
  4561. testing a single bit. This mostly benefits the 68k.
  4562. If STORE_FLAG_VALUE does not have the sign bit set when
  4563. interpreted in MODE, we can do this conversion as unsigned, which
  4564. is usually more efficient. */
  4565. if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (result_mode))
  4566. {
  4567. convert_move (target, subtarget,
  4568. val_signbit_known_clear_p (result_mode,
  4569. STORE_FLAG_VALUE));
  4570. op0 = target;
  4571. result_mode = target_mode;
  4572. }
  4573. else
  4574. op0 = subtarget;
  4575. /* If we want to keep subexpressions around, don't reuse our last
  4576. target. */
  4577. if (optimize)
  4578. subtarget = 0;
  4579. /* Now normalize to the proper value in MODE. Sometimes we don't
  4580. have to do anything. */
  4581. if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
  4582. ;
  4583. /* STORE_FLAG_VALUE might be the most negative number, so write
  4584. the comparison this way to avoid a compiler-time warning. */
  4585. else if (- normalizep == STORE_FLAG_VALUE)
  4586. op0 = expand_unop (result_mode, neg_optab, op0, subtarget, 0);
  4587. /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
  4588. it hard to use a value of just the sign bit due to ANSI integer
  4589. constant typing rules. */
  4590. else if (val_signbit_known_set_p (result_mode, STORE_FLAG_VALUE))
  4591. op0 = expand_shift (RSHIFT_EXPR, result_mode, op0,
  4592. GET_MODE_BITSIZE (result_mode) - 1, subtarget,
  4593. normalizep == 1);
  4594. else
  4595. {
  4596. gcc_assert (STORE_FLAG_VALUE & 1);
  4597. op0 = expand_and (result_mode, op0, const1_rtx, subtarget);
  4598. if (normalizep == -1)
  4599. op0 = expand_unop (result_mode, neg_optab, op0, op0, 0);
  4600. }
  4601. /* If we were converting to a smaller mode, do the conversion now. */
  4602. if (target_mode != result_mode)
  4603. {
  4604. convert_move (target, op0, 0);
  4605. return target;
  4606. }
  4607. else
  4608. return op0;
  4609. }
  4610. /* A subroutine of emit_store_flag only including "tricks" that do not
  4611. need a recursive call. These are kept separate to avoid infinite
  4612. loops. */
  4613. static rtx
  4614. emit_store_flag_1 (rtx target, enum rtx_code code, rtx op0, rtx op1,
  4615. machine_mode mode, int unsignedp, int normalizep,
  4616. machine_mode target_mode)
  4617. {
  4618. rtx subtarget;
  4619. enum insn_code icode;
  4620. machine_mode compare_mode;
  4621. enum mode_class mclass;
  4622. enum rtx_code scode;
  4623. rtx tem;
  4624. if (unsignedp)
  4625. code = unsigned_condition (code);
  4626. scode = swap_condition (code);
  4627. /* If one operand is constant, make it the second one. Only do this
  4628. if the other operand is not constant as well. */
  4629. if (swap_commutative_operands_p (op0, op1))
  4630. {
  4631. tem = op0;
  4632. op0 = op1;
  4633. op1 = tem;
  4634. code = swap_condition (code);
  4635. }
  4636. if (mode == VOIDmode)
  4637. mode = GET_MODE (op0);
  4638. /* For some comparisons with 1 and -1, we can convert this to
  4639. comparisons with zero. This will often produce more opportunities for
  4640. store-flag insns. */
  4641. switch (code)
  4642. {
  4643. case LT:
  4644. if (op1 == const1_rtx)
  4645. op1 = const0_rtx, code = LE;
  4646. break;
  4647. case LE:
  4648. if (op1 == constm1_rtx)
  4649. op1 = const0_rtx, code = LT;
  4650. break;
  4651. case GE:
  4652. if (op1 == const1_rtx)
  4653. op1 = const0_rtx, code = GT;
  4654. break;
  4655. case GT:
  4656. if (op1 == constm1_rtx)
  4657. op1 = const0_rtx, code = GE;
  4658. break;
  4659. case GEU:
  4660. if (op1 == const1_rtx)
  4661. op1 = const0_rtx, code = NE;
  4662. break;
  4663. case LTU:
  4664. if (op1 == const1_rtx)
  4665. op1 = const0_rtx, code = EQ;
  4666. break;
  4667. default:
  4668. break;
  4669. }
  4670. /* If we are comparing a double-word integer with zero or -1, we can
  4671. convert the comparison into one involving a single word. */
  4672. if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
  4673. && GET_MODE_CLASS (mode) == MODE_INT
  4674. && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
  4675. {
  4676. if ((code == EQ || code == NE)
  4677. && (op1 == const0_rtx || op1 == constm1_rtx))
  4678. {
  4679. rtx op00, op01;
  4680. /* Do a logical OR or AND of the two words and compare the
  4681. result. */
  4682. op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
  4683. op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
  4684. tem = expand_binop (word_mode,
  4685. op1 == const0_rtx ? ior_optab : and_optab,
  4686. op00, op01, NULL_RTX, unsignedp,
  4687. OPTAB_DIRECT);
  4688. if (tem != 0)
  4689. tem = emit_store_flag (NULL_RTX, code, tem, op1, word_mode,
  4690. unsignedp, normalizep);
  4691. }
  4692. else if ((code == LT || code == GE) && op1 == const0_rtx)
  4693. {
  4694. rtx op0h;
  4695. /* If testing the sign bit, can just test on high word. */
  4696. op0h = simplify_gen_subreg (word_mode, op0, mode,
  4697. subreg_highpart_offset (word_mode,
  4698. mode));
  4699. tem = emit_store_flag (NULL_RTX, code, op0h, op1, word_mode,
  4700. unsignedp, normalizep);
  4701. }
  4702. else
  4703. tem = NULL_RTX;
  4704. if (tem)
  4705. {
  4706. if (target_mode == VOIDmode || GET_MODE (tem) == target_mode)
  4707. return tem;
  4708. if (!target)
  4709. target = gen_reg_rtx (target_mode);
  4710. convert_move (target, tem,
  4711. !val_signbit_known_set_p (word_mode,
  4712. (normalizep ? normalizep
  4713. : STORE_FLAG_VALUE)));
  4714. return target;
  4715. }
  4716. }
  4717. /* If this is A < 0 or A >= 0, we can do this by taking the ones
  4718. complement of A (for GE) and shifting the sign bit to the low bit. */
  4719. if (op1 == const0_rtx && (code == LT || code == GE)
  4720. && GET_MODE_CLASS (mode) == MODE_INT
  4721. && (normalizep || STORE_FLAG_VALUE == 1
  4722. || val_signbit_p (mode, STORE_FLAG_VALUE)))
  4723. {
  4724. subtarget = target;
  4725. if (!target)
  4726. target_mode = mode;
  4727. /* If the result is to be wider than OP0, it is best to convert it
  4728. first. If it is to be narrower, it is *incorrect* to convert it
  4729. first. */
  4730. else if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
  4731. {
  4732. op0 = convert_modes (target_mode, mode, op0, 0);
  4733. mode = target_mode;
  4734. }
  4735. if (target_mode != mode)
  4736. subtarget = 0;
  4737. if (code == GE)
  4738. op0 = expand_unop (mode, one_cmpl_optab, op0,
  4739. ((STORE_FLAG_VALUE == 1 || normalizep)
  4740. ? 0 : subtarget), 0);
  4741. if (STORE_FLAG_VALUE == 1 || normalizep)
  4742. /* If we are supposed to produce a 0/1 value, we want to do
  4743. a logical shift from the sign bit to the low-order bit; for
  4744. a -1/0 value, we do an arithmetic shift. */
  4745. op0 = expand_shift (RSHIFT_EXPR, mode, op0,
  4746. GET_MODE_BITSIZE (mode) - 1,
  4747. subtarget, normalizep != -1);
  4748. if (mode != target_mode)
  4749. op0 = convert_modes (target_mode, mode, op0, 0);
  4750. return op0;
  4751. }
  4752. mclass = GET_MODE_CLASS (mode);
  4753. for (compare_mode = mode; compare_mode != VOIDmode;
  4754. compare_mode = GET_MODE_WIDER_MODE (compare_mode))
  4755. {
  4756. machine_mode optab_mode = mclass == MODE_CC ? CCmode : compare_mode;
  4757. icode = optab_handler (cstore_optab, optab_mode);
  4758. if (icode != CODE_FOR_nothing)
  4759. {
  4760. do_pending_stack_adjust ();
  4761. tem = emit_cstore (target, icode, code, mode, compare_mode,
  4762. unsignedp, op0, op1, normalizep, target_mode);
  4763. if (tem)
  4764. return tem;
  4765. if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  4766. {
  4767. tem = emit_cstore (target, icode, scode, mode, compare_mode,
  4768. unsignedp, op1, op0, normalizep, target_mode);
  4769. if (tem)
  4770. return tem;
  4771. }
  4772. break;
  4773. }
  4774. }
  4775. return 0;
  4776. }
  4777. /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
  4778. and storing in TARGET. Normally return TARGET.
  4779. Return 0 if that cannot be done.
  4780. MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
  4781. it is VOIDmode, they cannot both be CONST_INT.
  4782. UNSIGNEDP is for the case where we have to widen the operands
  4783. to perform the operation. It says to use zero-extension.
  4784. NORMALIZEP is 1 if we should convert the result to be either zero
  4785. or one. Normalize is -1 if we should convert the result to be
  4786. either zero or -1. If NORMALIZEP is zero, the result will be left
  4787. "raw" out of the scc insn. */
  4788. rtx
  4789. emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
  4790. machine_mode mode, int unsignedp, int normalizep)
  4791. {
  4792. machine_mode target_mode = target ? GET_MODE (target) : VOIDmode;
  4793. enum rtx_code rcode;
  4794. rtx subtarget;
  4795. rtx tem, trueval;
  4796. rtx_insn *last;
  4797. /* If we compare constants, we shouldn't use a store-flag operation,
  4798. but a constant load. We can get there via the vanilla route that
  4799. usually generates a compare-branch sequence, but will in this case
  4800. fold the comparison to a constant, and thus elide the branch. */
  4801. if (CONSTANT_P (op0) && CONSTANT_P (op1))
  4802. return NULL_RTX;
  4803. tem = emit_store_flag_1 (target, code, op0, op1, mode, unsignedp, normalizep,
  4804. target_mode);
  4805. if (tem)
  4806. return tem;
  4807. /* If we reached here, we can't do this with a scc insn, however there
  4808. are some comparisons that can be done in other ways. Don't do any
  4809. of these cases if branches are very cheap. */
  4810. if (BRANCH_COST (optimize_insn_for_speed_p (), false) == 0)
  4811. return 0;
  4812. /* See what we need to return. We can only return a 1, -1, or the
  4813. sign bit. */
  4814. if (normalizep == 0)
  4815. {
  4816. if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  4817. normalizep = STORE_FLAG_VALUE;
  4818. else if (val_signbit_p (mode, STORE_FLAG_VALUE))
  4819. ;
  4820. else
  4821. return 0;
  4822. }
  4823. last = get_last_insn ();
  4824. /* If optimizing, use different pseudo registers for each insn, instead
  4825. of reusing the same pseudo. This leads to better CSE, but slows
  4826. down the compiler, since there are more pseudos */
  4827. subtarget = (!optimize
  4828. && (target_mode == mode)) ? target : NULL_RTX;
  4829. trueval = GEN_INT (normalizep ? normalizep : STORE_FLAG_VALUE);
  4830. /* For floating-point comparisons, try the reverse comparison or try
  4831. changing the "orderedness" of the comparison. */
  4832. if (GET_MODE_CLASS (mode) == MODE_FLOAT)
  4833. {
  4834. enum rtx_code first_code;
  4835. bool and_them;
  4836. rcode = reverse_condition_maybe_unordered (code);
  4837. if (can_compare_p (rcode, mode, ccp_store_flag)
  4838. && (code == ORDERED || code == UNORDERED
  4839. || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
  4840. || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
  4841. {
  4842. int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
  4843. || (STORE_FLAG_VALUE == -1 && normalizep == 1));
  4844. /* For the reverse comparison, use either an addition or a XOR. */
  4845. if (want_add
  4846. && rtx_cost (GEN_INT (normalizep), PLUS, 1,
  4847. optimize_insn_for_speed_p ()) == 0)
  4848. {
  4849. tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
  4850. STORE_FLAG_VALUE, target_mode);
  4851. if (tem)
  4852. return expand_binop (target_mode, add_optab, tem,
  4853. gen_int_mode (normalizep, target_mode),
  4854. target, 0, OPTAB_WIDEN);
  4855. }
  4856. else if (!want_add
  4857. && rtx_cost (trueval, XOR, 1,
  4858. optimize_insn_for_speed_p ()) == 0)
  4859. {
  4860. tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
  4861. normalizep, target_mode);
  4862. if (tem)
  4863. return expand_binop (target_mode, xor_optab, tem, trueval,
  4864. target, INTVAL (trueval) >= 0, OPTAB_WIDEN);
  4865. }
  4866. }
  4867. delete_insns_since (last);
  4868. /* Cannot split ORDERED and UNORDERED, only try the above trick. */
  4869. if (code == ORDERED || code == UNORDERED)
  4870. return 0;
  4871. and_them = split_comparison (code, mode, &first_code, &code);
  4872. /* If there are no NaNs, the first comparison should always fall through.
  4873. Effectively change the comparison to the other one. */
  4874. if (!HONOR_NANS (mode))
  4875. {
  4876. gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
  4877. return emit_store_flag_1 (target, code, op0, op1, mode, 0, normalizep,
  4878. target_mode);
  4879. }
  4880. #ifdef HAVE_conditional_move
  4881. /* Try using a setcc instruction for ORDERED/UNORDERED, followed by a
  4882. conditional move. */
  4883. tem = emit_store_flag_1 (subtarget, first_code, op0, op1, mode, 0,
  4884. normalizep, target_mode);
  4885. if (tem == 0)
  4886. return 0;
  4887. if (and_them)
  4888. tem = emit_conditional_move (target, code, op0, op1, mode,
  4889. tem, const0_rtx, GET_MODE (tem), 0);
  4890. else
  4891. tem = emit_conditional_move (target, code, op0, op1, mode,
  4892. trueval, tem, GET_MODE (tem), 0);
  4893. if (tem == 0)
  4894. delete_insns_since (last);
  4895. return tem;
  4896. #else
  4897. return 0;
  4898. #endif
  4899. }
  4900. /* The remaining tricks only apply to integer comparisons. */
  4901. if (GET_MODE_CLASS (mode) != MODE_INT)
  4902. return 0;
  4903. /* If this is an equality comparison of integers, we can try to exclusive-or
  4904. (or subtract) the two operands and use a recursive call to try the
  4905. comparison with zero. Don't do any of these cases if branches are
  4906. very cheap. */
  4907. if ((code == EQ || code == NE) && op1 != const0_rtx)
  4908. {
  4909. tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
  4910. OPTAB_WIDEN);
  4911. if (tem == 0)
  4912. tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
  4913. OPTAB_WIDEN);
  4914. if (tem != 0)
  4915. tem = emit_store_flag (target, code, tem, const0_rtx,
  4916. mode, unsignedp, normalizep);
  4917. if (tem != 0)
  4918. return tem;
  4919. delete_insns_since (last);
  4920. }
  4921. /* For integer comparisons, try the reverse comparison. However, for
  4922. small X and if we'd have anyway to extend, implementing "X != 0"
  4923. as "-(int)X >> 31" is still cheaper than inverting "(int)X == 0". */
  4924. rcode = reverse_condition (code);
  4925. if (can_compare_p (rcode, mode, ccp_store_flag)
  4926. && ! (optab_handler (cstore_optab, mode) == CODE_FOR_nothing
  4927. && code == NE
  4928. && GET_MODE_SIZE (mode) < UNITS_PER_WORD
  4929. && op1 == const0_rtx))
  4930. {
  4931. int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
  4932. || (STORE_FLAG_VALUE == -1 && normalizep == 1));
  4933. /* Again, for the reverse comparison, use either an addition or a XOR. */
  4934. if (want_add
  4935. && rtx_cost (GEN_INT (normalizep), PLUS, 1,
  4936. optimize_insn_for_speed_p ()) == 0)
  4937. {
  4938. tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
  4939. STORE_FLAG_VALUE, target_mode);
  4940. if (tem != 0)
  4941. tem = expand_binop (target_mode, add_optab, tem,
  4942. gen_int_mode (normalizep, target_mode),
  4943. target, 0, OPTAB_WIDEN);
  4944. }
  4945. else if (!want_add
  4946. && rtx_cost (trueval, XOR, 1,
  4947. optimize_insn_for_speed_p ()) == 0)
  4948. {
  4949. tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
  4950. normalizep, target_mode);
  4951. if (tem != 0)
  4952. tem = expand_binop (target_mode, xor_optab, tem, trueval, target,
  4953. INTVAL (trueval) >= 0, OPTAB_WIDEN);
  4954. }
  4955. if (tem != 0)
  4956. return tem;
  4957. delete_insns_since (last);
  4958. }
  4959. /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
  4960. the constant zero. Reject all other comparisons at this point. Only
  4961. do LE and GT if branches are expensive since they are expensive on
  4962. 2-operand machines. */
  4963. if (op1 != const0_rtx
  4964. || (code != EQ && code != NE
  4965. && (BRANCH_COST (optimize_insn_for_speed_p (),
  4966. false) <= 1 || (code != LE && code != GT))))
  4967. return 0;
  4968. /* Try to put the result of the comparison in the sign bit. Assume we can't
  4969. do the necessary operation below. */
  4970. tem = 0;
  4971. /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
  4972. the sign bit set. */
  4973. if (code == LE)
  4974. {
  4975. /* This is destructive, so SUBTARGET can't be OP0. */
  4976. if (rtx_equal_p (subtarget, op0))
  4977. subtarget = 0;
  4978. tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
  4979. OPTAB_WIDEN);
  4980. if (tem)
  4981. tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
  4982. OPTAB_WIDEN);
  4983. }
  4984. /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
  4985. number of bits in the mode of OP0, minus one. */
  4986. if (code == GT)
  4987. {
  4988. if (rtx_equal_p (subtarget, op0))
  4989. subtarget = 0;
  4990. tem = expand_shift (RSHIFT_EXPR, mode, op0,
  4991. GET_MODE_BITSIZE (mode) - 1,
  4992. subtarget, 0);
  4993. tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
  4994. OPTAB_WIDEN);
  4995. }
  4996. if (code == EQ || code == NE)
  4997. {
  4998. /* For EQ or NE, one way to do the comparison is to apply an operation
  4999. that converts the operand into a positive number if it is nonzero
  5000. or zero if it was originally zero. Then, for EQ, we subtract 1 and
  5001. for NE we negate. This puts the result in the sign bit. Then we
  5002. normalize with a shift, if needed.
  5003. Two operations that can do the above actions are ABS and FFS, so try
  5004. them. If that doesn't work, and MODE is smaller than a full word,
  5005. we can use zero-extension to the wider mode (an unsigned conversion)
  5006. as the operation. */
  5007. /* Note that ABS doesn't yield a positive number for INT_MIN, but
  5008. that is compensated by the subsequent overflow when subtracting
  5009. one / negating. */
  5010. if (optab_handler (abs_optab, mode) != CODE_FOR_nothing)
  5011. tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
  5012. else if (optab_handler (ffs_optab, mode) != CODE_FOR_nothing)
  5013. tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
  5014. else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
  5015. {
  5016. tem = convert_modes (word_mode, mode, op0, 1);
  5017. mode = word_mode;
  5018. }
  5019. if (tem != 0)
  5020. {
  5021. if (code == EQ)
  5022. tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
  5023. 0, OPTAB_WIDEN);
  5024. else
  5025. tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
  5026. }
  5027. /* If we couldn't do it that way, for NE we can "or" the two's complement
  5028. of the value with itself. For EQ, we take the one's complement of
  5029. that "or", which is an extra insn, so we only handle EQ if branches
  5030. are expensive. */
  5031. if (tem == 0
  5032. && (code == NE
  5033. || BRANCH_COST (optimize_insn_for_speed_p (),
  5034. false) > 1))
  5035. {
  5036. if (rtx_equal_p (subtarget, op0))
  5037. subtarget = 0;
  5038. tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
  5039. tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
  5040. OPTAB_WIDEN);
  5041. if (tem && code == EQ)
  5042. tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
  5043. }
  5044. }
  5045. if (tem && normalizep)
  5046. tem = expand_shift (RSHIFT_EXPR, mode, tem,
  5047. GET_MODE_BITSIZE (mode) - 1,
  5048. subtarget, normalizep == 1);
  5049. if (tem)
  5050. {
  5051. if (!target)
  5052. ;
  5053. else if (GET_MODE (tem) != target_mode)
  5054. {
  5055. convert_move (target, tem, 0);
  5056. tem = target;
  5057. }
  5058. else if (!subtarget)
  5059. {
  5060. emit_move_insn (target, tem);
  5061. tem = target;
  5062. }
  5063. }
  5064. else
  5065. delete_insns_since (last);
  5066. return tem;
  5067. }
  5068. /* Like emit_store_flag, but always succeeds. */
  5069. rtx
  5070. emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
  5071. machine_mode mode, int unsignedp, int normalizep)
  5072. {
  5073. rtx tem;
  5074. rtx_code_label *label;
  5075. rtx trueval, falseval;
  5076. /* First see if emit_store_flag can do the job. */
  5077. tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
  5078. if (tem != 0)
  5079. return tem;
  5080. if (!target)
  5081. target = gen_reg_rtx (word_mode);
  5082. /* If this failed, we have to do this with set/compare/jump/set code.
  5083. For foo != 0, if foo is in OP0, just replace it with 1 if nonzero. */
  5084. trueval = normalizep ? GEN_INT (normalizep) : const1_rtx;
  5085. if (code == NE
  5086. && GET_MODE_CLASS (mode) == MODE_INT
  5087. && REG_P (target)
  5088. && op0 == target
  5089. && op1 == const0_rtx)
  5090. {
  5091. label = gen_label_rtx ();
  5092. do_compare_rtx_and_jump (target, const0_rtx, EQ, unsignedp,
  5093. mode, NULL_RTX, NULL_RTX, label, -1);
  5094. emit_move_insn (target, trueval);
  5095. emit_label (label);
  5096. return target;
  5097. }
  5098. if (!REG_P (target)
  5099. || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
  5100. target = gen_reg_rtx (GET_MODE (target));
  5101. /* Jump in the right direction if the target cannot implement CODE
  5102. but can jump on its reverse condition. */
  5103. falseval = const0_rtx;
  5104. if (! can_compare_p (code, mode, ccp_jump)
  5105. && (! FLOAT_MODE_P (mode)
  5106. || code == ORDERED || code == UNORDERED
  5107. || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
  5108. || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
  5109. {
  5110. enum rtx_code rcode;
  5111. if (FLOAT_MODE_P (mode))
  5112. rcode = reverse_condition_maybe_unordered (code);
  5113. else
  5114. rcode = reverse_condition (code);
  5115. /* Canonicalize to UNORDERED for the libcall. */
  5116. if (can_compare_p (rcode, mode, ccp_jump)
  5117. || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
  5118. {
  5119. falseval = trueval;
  5120. trueval = const0_rtx;
  5121. code = rcode;
  5122. }
  5123. }
  5124. emit_move_insn (target, trueval);
  5125. label = gen_label_rtx ();
  5126. do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
  5127. NULL_RTX, label, -1);
  5128. emit_move_insn (target, falseval);
  5129. emit_label (label);
  5130. return target;
  5131. }
  5132. /* Perform possibly multi-word comparison and conditional jump to LABEL
  5133. if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
  5134. now a thin wrapper around do_compare_rtx_and_jump. */
  5135. static void
  5136. do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, machine_mode mode,
  5137. rtx_code_label *label)
  5138. {
  5139. int unsignedp = (op == LTU || op == LEU || op == GTU || op == GEU);
  5140. do_compare_rtx_and_jump (arg1, arg2, op, unsignedp, mode,
  5141. NULL_RTX, NULL_RTX, label, -1);
  5142. }