tree-vect-data-refs.c 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862
  1. /* Data References Analysis and Manipulation Utilities for Vectorization.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Dorit Naishlos <dorit@il.ibm.com>
  4. and Ira Rosen <irar@il.ibm.com>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "dumpfile.h"
  21. #include "tm.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 "target.h"
  36. #include "predict.h"
  37. #include "hard-reg-set.h"
  38. #include "function.h"
  39. #include "dominance.h"
  40. #include "cfg.h"
  41. #include "basic-block.h"
  42. #include "gimple-pretty-print.h"
  43. #include "tree-ssa-alias.h"
  44. #include "internal-fn.h"
  45. #include "tree-eh.h"
  46. #include "gimple-expr.h"
  47. #include "is-a.h"
  48. #include "gimple.h"
  49. #include "gimplify.h"
  50. #include "gimple-iterator.h"
  51. #include "gimplify-me.h"
  52. #include "gimple-ssa.h"
  53. #include "tree-phinodes.h"
  54. #include "ssa-iterators.h"
  55. #include "stringpool.h"
  56. #include "tree-ssanames.h"
  57. #include "tree-ssa-loop-ivopts.h"
  58. #include "tree-ssa-loop-manip.h"
  59. #include "tree-ssa-loop.h"
  60. #include "cfgloop.h"
  61. #include "tree-chrec.h"
  62. #include "tree-scalar-evolution.h"
  63. #include "tree-vectorizer.h"
  64. #include "diagnostic-core.h"
  65. #include "hash-map.h"
  66. #include "plugin-api.h"
  67. #include "ipa-ref.h"
  68. #include "cgraph.h"
  69. /* Need to include rtl.h, expr.h, etc. for optabs. */
  70. #include "hashtab.h"
  71. #include "rtl.h"
  72. #include "flags.h"
  73. #include "statistics.h"
  74. #include "real.h"
  75. #include "fixed-value.h"
  76. #include "insn-config.h"
  77. #include "expmed.h"
  78. #include "dojump.h"
  79. #include "explow.h"
  80. #include "calls.h"
  81. #include "emit-rtl.h"
  82. #include "varasm.h"
  83. #include "stmt.h"
  84. #include "expr.h"
  85. #include "insn-codes.h"
  86. #include "optabs.h"
  87. #include "builtins.h"
  88. /* Return true if load- or store-lanes optab OPTAB is implemented for
  89. COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
  90. static bool
  91. vect_lanes_optab_supported_p (const char *name, convert_optab optab,
  92. tree vectype, unsigned HOST_WIDE_INT count)
  93. {
  94. machine_mode mode, array_mode;
  95. bool limit_p;
  96. mode = TYPE_MODE (vectype);
  97. limit_p = !targetm.array_mode_supported_p (mode, count);
  98. array_mode = mode_for_size (count * GET_MODE_BITSIZE (mode),
  99. MODE_INT, limit_p);
  100. if (array_mode == BLKmode)
  101. {
  102. if (dump_enabled_p ())
  103. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  104. "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC "]\n",
  105. GET_MODE_NAME (mode), count);
  106. return false;
  107. }
  108. if (convert_optab_handler (optab, array_mode, mode) == CODE_FOR_nothing)
  109. {
  110. if (dump_enabled_p ())
  111. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  112. "cannot use %s<%s><%s>\n", name,
  113. GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
  114. return false;
  115. }
  116. if (dump_enabled_p ())
  117. dump_printf_loc (MSG_NOTE, vect_location,
  118. "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
  119. GET_MODE_NAME (mode));
  120. return true;
  121. }
  122. /* Return the smallest scalar part of STMT.
  123. This is used to determine the vectype of the stmt. We generally set the
  124. vectype according to the type of the result (lhs). For stmts whose
  125. result-type is different than the type of the arguments (e.g., demotion,
  126. promotion), vectype will be reset appropriately (later). Note that we have
  127. to visit the smallest datatype in this function, because that determines the
  128. VF. If the smallest datatype in the loop is present only as the rhs of a
  129. promotion operation - we'd miss it.
  130. Such a case, where a variable of this datatype does not appear in the lhs
  131. anywhere in the loop, can only occur if it's an invariant: e.g.:
  132. 'int_x = (int) short_inv', which we'd expect to have been optimized away by
  133. invariant motion. However, we cannot rely on invariant motion to always
  134. take invariants out of the loop, and so in the case of promotion we also
  135. have to check the rhs.
  136. LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
  137. types. */
  138. tree
  139. vect_get_smallest_scalar_type (gimple stmt, HOST_WIDE_INT *lhs_size_unit,
  140. HOST_WIDE_INT *rhs_size_unit)
  141. {
  142. tree scalar_type = gimple_expr_type (stmt);
  143. HOST_WIDE_INT lhs, rhs;
  144. lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
  145. if (is_gimple_assign (stmt)
  146. && (gimple_assign_cast_p (stmt)
  147. || gimple_assign_rhs_code (stmt) == WIDEN_MULT_EXPR
  148. || gimple_assign_rhs_code (stmt) == WIDEN_LSHIFT_EXPR
  149. || gimple_assign_rhs_code (stmt) == FLOAT_EXPR))
  150. {
  151. tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
  152. rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
  153. if (rhs < lhs)
  154. scalar_type = rhs_type;
  155. }
  156. *lhs_size_unit = lhs;
  157. *rhs_size_unit = rhs;
  158. return scalar_type;
  159. }
  160. /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
  161. tested at run-time. Return TRUE if DDR was successfully inserted.
  162. Return false if versioning is not supported. */
  163. static bool
  164. vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
  165. {
  166. struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
  167. if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
  168. return false;
  169. if (dump_enabled_p ())
  170. {
  171. dump_printf_loc (MSG_NOTE, vect_location,
  172. "mark for run-time aliasing test between ");
  173. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_A (ddr)));
  174. dump_printf (MSG_NOTE, " and ");
  175. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_B (ddr)));
  176. dump_printf (MSG_NOTE, "\n");
  177. }
  178. if (optimize_loop_nest_for_size_p (loop))
  179. {
  180. if (dump_enabled_p ())
  181. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  182. "versioning not supported when optimizing"
  183. " for size.\n");
  184. return false;
  185. }
  186. /* FORNOW: We don't support versioning with outer-loop vectorization. */
  187. if (loop->inner)
  188. {
  189. if (dump_enabled_p ())
  190. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  191. "versioning not yet supported for outer-loops.\n");
  192. return false;
  193. }
  194. /* FORNOW: We don't support creating runtime alias tests for non-constant
  195. step. */
  196. if (TREE_CODE (DR_STEP (DDR_A (ddr))) != INTEGER_CST
  197. || TREE_CODE (DR_STEP (DDR_B (ddr))) != INTEGER_CST)
  198. {
  199. if (dump_enabled_p ())
  200. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  201. "versioning not yet supported for non-constant "
  202. "step\n");
  203. return false;
  204. }
  205. LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
  206. return true;
  207. }
  208. /* Function vect_analyze_data_ref_dependence.
  209. Return TRUE if there (might) exist a dependence between a memory-reference
  210. DRA and a memory-reference DRB. When versioning for alias may check a
  211. dependence at run-time, return FALSE. Adjust *MAX_VF according to
  212. the data dependence. */
  213. static bool
  214. vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
  215. loop_vec_info loop_vinfo, int *max_vf)
  216. {
  217. unsigned int i;
  218. struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
  219. struct data_reference *dra = DDR_A (ddr);
  220. struct data_reference *drb = DDR_B (ddr);
  221. stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
  222. stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
  223. lambda_vector dist_v;
  224. unsigned int loop_depth;
  225. /* In loop analysis all data references should be vectorizable. */
  226. if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
  227. || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
  228. gcc_unreachable ();
  229. /* Independent data accesses. */
  230. if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
  231. return false;
  232. if (dra == drb
  233. || (DR_IS_READ (dra) && DR_IS_READ (drb)))
  234. return false;
  235. /* Even if we have an anti-dependence then, as the vectorized loop covers at
  236. least two scalar iterations, there is always also a true dependence.
  237. As the vectorizer does not re-order loads and stores we can ignore
  238. the anti-dependence if TBAA can disambiguate both DRs similar to the
  239. case with known negative distance anti-dependences (positive
  240. distance anti-dependences would violate TBAA constraints). */
  241. if (((DR_IS_READ (dra) && DR_IS_WRITE (drb))
  242. || (DR_IS_WRITE (dra) && DR_IS_READ (drb)))
  243. && !alias_sets_conflict_p (get_alias_set (DR_REF (dra)),
  244. get_alias_set (DR_REF (drb))))
  245. return false;
  246. /* Unknown data dependence. */
  247. if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
  248. {
  249. /* If user asserted safelen consecutive iterations can be
  250. executed concurrently, assume independence. */
  251. if (loop->safelen >= 2)
  252. {
  253. if (loop->safelen < *max_vf)
  254. *max_vf = loop->safelen;
  255. LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
  256. return false;
  257. }
  258. if (STMT_VINFO_GATHER_P (stmtinfo_a)
  259. || STMT_VINFO_GATHER_P (stmtinfo_b))
  260. {
  261. if (dump_enabled_p ())
  262. {
  263. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  264. "versioning for alias not supported for: "
  265. "can't determine dependence between ");
  266. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  267. DR_REF (dra));
  268. dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
  269. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  270. DR_REF (drb));
  271. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  272. }
  273. return true;
  274. }
  275. if (dump_enabled_p ())
  276. {
  277. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  278. "versioning for alias required: "
  279. "can't determine dependence between ");
  280. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  281. DR_REF (dra));
  282. dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
  283. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  284. DR_REF (drb));
  285. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  286. }
  287. /* Add to list of ddrs that need to be tested at run-time. */
  288. return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
  289. }
  290. /* Known data dependence. */
  291. if (DDR_NUM_DIST_VECTS (ddr) == 0)
  292. {
  293. /* If user asserted safelen consecutive iterations can be
  294. executed concurrently, assume independence. */
  295. if (loop->safelen >= 2)
  296. {
  297. if (loop->safelen < *max_vf)
  298. *max_vf = loop->safelen;
  299. LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = false;
  300. return false;
  301. }
  302. if (STMT_VINFO_GATHER_P (stmtinfo_a)
  303. || STMT_VINFO_GATHER_P (stmtinfo_b))
  304. {
  305. if (dump_enabled_p ())
  306. {
  307. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  308. "versioning for alias not supported for: "
  309. "bad dist vector for ");
  310. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  311. DR_REF (dra));
  312. dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
  313. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  314. DR_REF (drb));
  315. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  316. }
  317. return true;
  318. }
  319. if (dump_enabled_p ())
  320. {
  321. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  322. "versioning for alias required: "
  323. "bad dist vector for ");
  324. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
  325. dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
  326. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
  327. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  328. }
  329. /* Add to list of ddrs that need to be tested at run-time. */
  330. return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
  331. }
  332. loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
  333. FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
  334. {
  335. int dist = dist_v[loop_depth];
  336. if (dump_enabled_p ())
  337. dump_printf_loc (MSG_NOTE, vect_location,
  338. "dependence distance = %d.\n", dist);
  339. if (dist == 0)
  340. {
  341. if (dump_enabled_p ())
  342. {
  343. dump_printf_loc (MSG_NOTE, vect_location,
  344. "dependence distance == 0 between ");
  345. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
  346. dump_printf (MSG_NOTE, " and ");
  347. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
  348. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  349. }
  350. /* When we perform grouped accesses and perform implicit CSE
  351. by detecting equal accesses and doing disambiguation with
  352. runtime alias tests like for
  353. .. = a[i];
  354. .. = a[i+1];
  355. a[i] = ..;
  356. a[i+1] = ..;
  357. *p = ..;
  358. .. = a[i];
  359. .. = a[i+1];
  360. where we will end up loading { a[i], a[i+1] } once, make
  361. sure that inserting group loads before the first load and
  362. stores after the last store will do the right thing.
  363. Similar for groups like
  364. a[i] = ...;
  365. ... = a[i];
  366. a[i+1] = ...;
  367. where loads from the group interleave with the store. */
  368. if (STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
  369. || STMT_VINFO_GROUPED_ACCESS (stmtinfo_b))
  370. {
  371. gimple earlier_stmt;
  372. earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
  373. if (DR_IS_WRITE
  374. (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
  375. {
  376. if (dump_enabled_p ())
  377. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  378. "READ_WRITE dependence in interleaving."
  379. "\n");
  380. return true;
  381. }
  382. }
  383. continue;
  384. }
  385. if (dist > 0 && DDR_REVERSED_P (ddr))
  386. {
  387. /* If DDR_REVERSED_P the order of the data-refs in DDR was
  388. reversed (to make distance vector positive), and the actual
  389. distance is negative. */
  390. if (dump_enabled_p ())
  391. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  392. "dependence distance negative.\n");
  393. /* Record a negative dependence distance to later limit the
  394. amount of stmt copying / unrolling we can perform.
  395. Only need to handle read-after-write dependence. */
  396. if (DR_IS_READ (drb)
  397. && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) == 0
  398. || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) > (unsigned)dist))
  399. STMT_VINFO_MIN_NEG_DIST (stmtinfo_b) = dist;
  400. continue;
  401. }
  402. if (abs (dist) >= 2
  403. && abs (dist) < *max_vf)
  404. {
  405. /* The dependence distance requires reduction of the maximal
  406. vectorization factor. */
  407. *max_vf = abs (dist);
  408. if (dump_enabled_p ())
  409. dump_printf_loc (MSG_NOTE, vect_location,
  410. "adjusting maximal vectorization factor to %i\n",
  411. *max_vf);
  412. }
  413. if (abs (dist) >= *max_vf)
  414. {
  415. /* Dependence distance does not create dependence, as far as
  416. vectorization is concerned, in this case. */
  417. if (dump_enabled_p ())
  418. dump_printf_loc (MSG_NOTE, vect_location,
  419. "dependence distance >= VF.\n");
  420. continue;
  421. }
  422. if (dump_enabled_p ())
  423. {
  424. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  425. "not vectorized, possible dependence "
  426. "between data-refs ");
  427. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
  428. dump_printf (MSG_NOTE, " and ");
  429. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
  430. dump_printf (MSG_NOTE, "\n");
  431. }
  432. return true;
  433. }
  434. return false;
  435. }
  436. /* Function vect_analyze_data_ref_dependences.
  437. Examine all the data references in the loop, and make sure there do not
  438. exist any data dependences between them. Set *MAX_VF according to
  439. the maximum vectorization factor the data dependences allow. */
  440. bool
  441. vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo, int *max_vf)
  442. {
  443. unsigned int i;
  444. struct data_dependence_relation *ddr;
  445. if (dump_enabled_p ())
  446. dump_printf_loc (MSG_NOTE, vect_location,
  447. "=== vect_analyze_data_ref_dependences ===\n");
  448. LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo) = true;
  449. if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
  450. &LOOP_VINFO_DDRS (loop_vinfo),
  451. LOOP_VINFO_LOOP_NEST (loop_vinfo), true))
  452. return false;
  453. FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
  454. if (vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf))
  455. return false;
  456. return true;
  457. }
  458. /* Function vect_slp_analyze_data_ref_dependence.
  459. Return TRUE if there (might) exist a dependence between a memory-reference
  460. DRA and a memory-reference DRB. When versioning for alias may check a
  461. dependence at run-time, return FALSE. Adjust *MAX_VF according to
  462. the data dependence. */
  463. static bool
  464. vect_slp_analyze_data_ref_dependence (struct data_dependence_relation *ddr)
  465. {
  466. struct data_reference *dra = DDR_A (ddr);
  467. struct data_reference *drb = DDR_B (ddr);
  468. /* We need to check dependences of statements marked as unvectorizable
  469. as well, they still can prohibit vectorization. */
  470. /* Independent data accesses. */
  471. if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
  472. return false;
  473. if (dra == drb)
  474. return false;
  475. /* Read-read is OK. */
  476. if (DR_IS_READ (dra) && DR_IS_READ (drb))
  477. return false;
  478. /* If dra and drb are part of the same interleaving chain consider
  479. them independent. */
  480. if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra)))
  481. && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra)))
  482. == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb)))))
  483. return false;
  484. /* Unknown data dependence. */
  485. if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
  486. {
  487. if (dump_enabled_p ())
  488. {
  489. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  490. "can't determine dependence between ");
  491. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
  492. dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
  493. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
  494. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  495. }
  496. }
  497. else if (dump_enabled_p ())
  498. {
  499. dump_printf_loc (MSG_NOTE, vect_location,
  500. "determined dependence between ");
  501. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
  502. dump_printf (MSG_NOTE, " and ");
  503. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
  504. dump_printf (MSG_NOTE, "\n");
  505. }
  506. /* We do not vectorize basic blocks with write-write dependencies. */
  507. if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
  508. return true;
  509. /* If we have a read-write dependence check that the load is before the store.
  510. When we vectorize basic blocks, vector load can be only before
  511. corresponding scalar load, and vector store can be only after its
  512. corresponding scalar store. So the order of the acceses is preserved in
  513. case the load is before the store. */
  514. gimple earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
  515. if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
  516. {
  517. /* That only holds for load-store pairs taking part in vectorization. */
  518. if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dra)))
  519. && STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (drb))))
  520. return false;
  521. }
  522. return true;
  523. }
  524. /* Function vect_analyze_data_ref_dependences.
  525. Examine all the data references in the basic-block, and make sure there
  526. do not exist any data dependences between them. Set *MAX_VF according to
  527. the maximum vectorization factor the data dependences allow. */
  528. bool
  529. vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo)
  530. {
  531. struct data_dependence_relation *ddr;
  532. unsigned int i;
  533. if (dump_enabled_p ())
  534. dump_printf_loc (MSG_NOTE, vect_location,
  535. "=== vect_slp_analyze_data_ref_dependences ===\n");
  536. if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo),
  537. &BB_VINFO_DDRS (bb_vinfo),
  538. vNULL, true))
  539. return false;
  540. FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo), i, ddr)
  541. if (vect_slp_analyze_data_ref_dependence (ddr))
  542. return false;
  543. return true;
  544. }
  545. /* Function vect_compute_data_ref_alignment
  546. Compute the misalignment of the data reference DR.
  547. Output:
  548. 1. If during the misalignment computation it is found that the data reference
  549. cannot be vectorized then false is returned.
  550. 2. DR_MISALIGNMENT (DR) is defined.
  551. FOR NOW: No analysis is actually performed. Misalignment is calculated
  552. only for trivial cases. TODO. */
  553. static bool
  554. vect_compute_data_ref_alignment (struct data_reference *dr)
  555. {
  556. gimple stmt = DR_STMT (dr);
  557. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  558. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  559. struct loop *loop = NULL;
  560. tree ref = DR_REF (dr);
  561. tree vectype;
  562. tree base, base_addr;
  563. bool base_aligned;
  564. tree misalign;
  565. tree aligned_to;
  566. unsigned HOST_WIDE_INT alignment;
  567. if (dump_enabled_p ())
  568. dump_printf_loc (MSG_NOTE, vect_location,
  569. "vect_compute_data_ref_alignment:\n");
  570. if (loop_vinfo)
  571. loop = LOOP_VINFO_LOOP (loop_vinfo);
  572. /* Initialize misalignment to unknown. */
  573. SET_DR_MISALIGNMENT (dr, -1);
  574. /* Strided loads perform only component accesses, misalignment information
  575. is irrelevant for them. */
  576. if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
  577. return true;
  578. misalign = DR_INIT (dr);
  579. aligned_to = DR_ALIGNED_TO (dr);
  580. base_addr = DR_BASE_ADDRESS (dr);
  581. vectype = STMT_VINFO_VECTYPE (stmt_info);
  582. /* In case the dataref is in an inner-loop of the loop that is being
  583. vectorized (LOOP), we use the base and misalignment information
  584. relative to the outer-loop (LOOP). This is ok only if the misalignment
  585. stays the same throughout the execution of the inner-loop, which is why
  586. we have to check that the stride of the dataref in the inner-loop evenly
  587. divides by the vector size. */
  588. if (loop && nested_in_vect_loop_p (loop, stmt))
  589. {
  590. tree step = DR_STEP (dr);
  591. HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
  592. if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) == 0)
  593. {
  594. if (dump_enabled_p ())
  595. dump_printf_loc (MSG_NOTE, vect_location,
  596. "inner step divides the vector-size.\n");
  597. misalign = STMT_VINFO_DR_INIT (stmt_info);
  598. aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
  599. base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
  600. }
  601. else
  602. {
  603. if (dump_enabled_p ())
  604. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  605. "inner step doesn't divide the vector-size.\n");
  606. misalign = NULL_TREE;
  607. }
  608. }
  609. /* Similarly, if we're doing basic-block vectorization, we can only use
  610. base and misalignment information relative to an innermost loop if the
  611. misalignment stays the same throughout the execution of the loop.
  612. As above, this is the case if the stride of the dataref evenly divides
  613. by the vector size. */
  614. if (!loop)
  615. {
  616. tree step = DR_STEP (dr);
  617. HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
  618. if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0)
  619. {
  620. if (dump_enabled_p ())
  621. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  622. "SLP: step doesn't divide the vector-size.\n");
  623. misalign = NULL_TREE;
  624. }
  625. }
  626. alignment = TYPE_ALIGN_UNIT (vectype);
  627. if ((compare_tree_int (aligned_to, alignment) < 0)
  628. || !misalign)
  629. {
  630. if (dump_enabled_p ())
  631. {
  632. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  633. "Unknown alignment for access: ");
  634. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
  635. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  636. }
  637. return true;
  638. }
  639. /* To look at alignment of the base we have to preserve an inner MEM_REF
  640. as that carries alignment information of the actual access. */
  641. base = ref;
  642. while (handled_component_p (base))
  643. base = TREE_OPERAND (base, 0);
  644. if (TREE_CODE (base) == MEM_REF)
  645. base = build2 (MEM_REF, TREE_TYPE (base), base_addr,
  646. build_int_cst (TREE_TYPE (TREE_OPERAND (base, 1)), 0));
  647. if (get_object_alignment (base) >= TYPE_ALIGN (vectype))
  648. base_aligned = true;
  649. else
  650. base_aligned = false;
  651. if (!base_aligned)
  652. {
  653. /* Strip an inner MEM_REF to a bare decl if possible. */
  654. if (TREE_CODE (base) == MEM_REF
  655. && integer_zerop (TREE_OPERAND (base, 1))
  656. && TREE_CODE (TREE_OPERAND (base, 0)) == ADDR_EXPR)
  657. base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
  658. if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype)))
  659. {
  660. if (dump_enabled_p ())
  661. {
  662. dump_printf_loc (MSG_NOTE, vect_location,
  663. "can't force alignment of ref: ");
  664. dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
  665. dump_printf (MSG_NOTE, "\n");
  666. }
  667. return true;
  668. }
  669. /* Force the alignment of the decl.
  670. NOTE: This is the only change to the code we make during
  671. the analysis phase, before deciding to vectorize the loop. */
  672. if (dump_enabled_p ())
  673. {
  674. dump_printf_loc (MSG_NOTE, vect_location, "force alignment of ");
  675. dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
  676. dump_printf (MSG_NOTE, "\n");
  677. }
  678. ((dataref_aux *)dr->aux)->base_decl = base;
  679. ((dataref_aux *)dr->aux)->base_misaligned = true;
  680. }
  681. /* If this is a backward running DR then first access in the larger
  682. vectype actually is N-1 elements before the address in the DR.
  683. Adjust misalign accordingly. */
  684. if (tree_int_cst_sgn (DR_STEP (dr)) < 0)
  685. {
  686. tree offset = ssize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
  687. /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
  688. otherwise we wouldn't be here. */
  689. offset = fold_build2 (MULT_EXPR, ssizetype, offset, DR_STEP (dr));
  690. /* PLUS because DR_STEP was negative. */
  691. misalign = size_binop (PLUS_EXPR, misalign, offset);
  692. }
  693. SET_DR_MISALIGNMENT (dr,
  694. wi::mod_floor (misalign, alignment, SIGNED).to_uhwi ());
  695. if (dump_enabled_p ())
  696. {
  697. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  698. "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
  699. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
  700. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  701. }
  702. return true;
  703. }
  704. /* Function vect_compute_data_refs_alignment
  705. Compute the misalignment of data references in the loop.
  706. Return FALSE if a data reference is found that cannot be vectorized. */
  707. static bool
  708. vect_compute_data_refs_alignment (loop_vec_info loop_vinfo,
  709. bb_vec_info bb_vinfo)
  710. {
  711. vec<data_reference_p> datarefs;
  712. struct data_reference *dr;
  713. unsigned int i;
  714. if (loop_vinfo)
  715. datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
  716. else
  717. datarefs = BB_VINFO_DATAREFS (bb_vinfo);
  718. FOR_EACH_VEC_ELT (datarefs, i, dr)
  719. if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
  720. && !vect_compute_data_ref_alignment (dr))
  721. {
  722. if (bb_vinfo)
  723. {
  724. /* Mark unsupported statement as unvectorizable. */
  725. STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
  726. continue;
  727. }
  728. else
  729. return false;
  730. }
  731. return true;
  732. }
  733. /* Function vect_update_misalignment_for_peel
  734. DR - the data reference whose misalignment is to be adjusted.
  735. DR_PEEL - the data reference whose misalignment is being made
  736. zero in the vector loop by the peel.
  737. NPEEL - the number of iterations in the peel loop if the misalignment
  738. of DR_PEEL is known at compile time. */
  739. static void
  740. vect_update_misalignment_for_peel (struct data_reference *dr,
  741. struct data_reference *dr_peel, int npeel)
  742. {
  743. unsigned int i;
  744. vec<dr_p> same_align_drs;
  745. struct data_reference *current_dr;
  746. int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
  747. int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
  748. stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
  749. stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
  750. /* For interleaved data accesses the step in the loop must be multiplied by
  751. the size of the interleaving group. */
  752. if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
  753. dr_size *= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
  754. if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info))
  755. dr_peel_size *= GROUP_SIZE (peel_stmt_info);
  756. /* It can be assumed that the data refs with the same alignment as dr_peel
  757. are aligned in the vector loop. */
  758. same_align_drs
  759. = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
  760. FOR_EACH_VEC_ELT (same_align_drs, i, current_dr)
  761. {
  762. if (current_dr != dr)
  763. continue;
  764. gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
  765. DR_MISALIGNMENT (dr_peel) / dr_peel_size);
  766. SET_DR_MISALIGNMENT (dr, 0);
  767. return;
  768. }
  769. if (known_alignment_for_access_p (dr)
  770. && known_alignment_for_access_p (dr_peel))
  771. {
  772. bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
  773. int misal = DR_MISALIGNMENT (dr);
  774. tree vectype = STMT_VINFO_VECTYPE (stmt_info);
  775. misal += negative ? -npeel * dr_size : npeel * dr_size;
  776. misal &= (TYPE_ALIGN (vectype) / BITS_PER_UNIT) - 1;
  777. SET_DR_MISALIGNMENT (dr, misal);
  778. return;
  779. }
  780. if (dump_enabled_p ())
  781. dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment to -1.\n");
  782. SET_DR_MISALIGNMENT (dr, -1);
  783. }
  784. /* Function vect_verify_datarefs_alignment
  785. Return TRUE if all data references in the loop can be
  786. handled with respect to alignment. */
  787. bool
  788. vect_verify_datarefs_alignment (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
  789. {
  790. vec<data_reference_p> datarefs;
  791. struct data_reference *dr;
  792. enum dr_alignment_support supportable_dr_alignment;
  793. unsigned int i;
  794. if (loop_vinfo)
  795. datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
  796. else
  797. datarefs = BB_VINFO_DATAREFS (bb_vinfo);
  798. FOR_EACH_VEC_ELT (datarefs, i, dr)
  799. {
  800. gimple stmt = DR_STMT (dr);
  801. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  802. if (!STMT_VINFO_RELEVANT_P (stmt_info))
  803. continue;
  804. /* For interleaving, only the alignment of the first access matters.
  805. Skip statements marked as not vectorizable. */
  806. if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
  807. && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
  808. || !STMT_VINFO_VECTORIZABLE (stmt_info))
  809. continue;
  810. /* Strided loads perform only component accesses, alignment is
  811. irrelevant for them. */
  812. if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
  813. continue;
  814. supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
  815. if (!supportable_dr_alignment)
  816. {
  817. if (dump_enabled_p ())
  818. {
  819. if (DR_IS_READ (dr))
  820. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  821. "not vectorized: unsupported unaligned load.");
  822. else
  823. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  824. "not vectorized: unsupported unaligned "
  825. "store.");
  826. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  827. DR_REF (dr));
  828. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  829. }
  830. return false;
  831. }
  832. if (supportable_dr_alignment != dr_aligned && dump_enabled_p ())
  833. dump_printf_loc (MSG_NOTE, vect_location,
  834. "Vectorizing an unaligned access.\n");
  835. }
  836. return true;
  837. }
  838. /* Given an memory reference EXP return whether its alignment is less
  839. than its size. */
  840. static bool
  841. not_size_aligned (tree exp)
  842. {
  843. if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
  844. return true;
  845. return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp)))
  846. > get_object_alignment (exp));
  847. }
  848. /* Function vector_alignment_reachable_p
  849. Return true if vector alignment for DR is reachable by peeling
  850. a few loop iterations. Return false otherwise. */
  851. static bool
  852. vector_alignment_reachable_p (struct data_reference *dr)
  853. {
  854. gimple stmt = DR_STMT (dr);
  855. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  856. tree vectype = STMT_VINFO_VECTYPE (stmt_info);
  857. if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
  858. {
  859. /* For interleaved access we peel only if number of iterations in
  860. the prolog loop ({VF - misalignment}), is a multiple of the
  861. number of the interleaved accesses. */
  862. int elem_size, mis_in_elements;
  863. int nelements = TYPE_VECTOR_SUBPARTS (vectype);
  864. /* FORNOW: handle only known alignment. */
  865. if (!known_alignment_for_access_p (dr))
  866. return false;
  867. elem_size = GET_MODE_SIZE (TYPE_MODE (vectype)) / nelements;
  868. mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
  869. if ((nelements - mis_in_elements) % GROUP_SIZE (stmt_info))
  870. return false;
  871. }
  872. /* If misalignment is known at the compile time then allow peeling
  873. only if natural alignment is reachable through peeling. */
  874. if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
  875. {
  876. HOST_WIDE_INT elmsize =
  877. int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
  878. if (dump_enabled_p ())
  879. {
  880. dump_printf_loc (MSG_NOTE, vect_location,
  881. "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
  882. dump_printf (MSG_NOTE,
  883. ". misalignment = %d.\n", DR_MISALIGNMENT (dr));
  884. }
  885. if (DR_MISALIGNMENT (dr) % elmsize)
  886. {
  887. if (dump_enabled_p ())
  888. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  889. "data size does not divide the misalignment.\n");
  890. return false;
  891. }
  892. }
  893. if (!known_alignment_for_access_p (dr))
  894. {
  895. tree type = TREE_TYPE (DR_REF (dr));
  896. bool is_packed = not_size_aligned (DR_REF (dr));
  897. if (dump_enabled_p ())
  898. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  899. "Unknown misalignment, is_packed = %d\n",is_packed);
  900. if ((TYPE_USER_ALIGN (type) && !is_packed)
  901. || targetm.vectorize.vector_alignment_reachable (type, is_packed))
  902. return true;
  903. else
  904. return false;
  905. }
  906. return true;
  907. }
  908. /* Calculate the cost of the memory access represented by DR. */
  909. static void
  910. vect_get_data_access_cost (struct data_reference *dr,
  911. unsigned int *inside_cost,
  912. unsigned int *outside_cost,
  913. stmt_vector_for_cost *body_cost_vec)
  914. {
  915. gimple stmt = DR_STMT (dr);
  916. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  917. int nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
  918. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  919. int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
  920. int ncopies = vf / nunits;
  921. if (DR_IS_READ (dr))
  922. vect_get_load_cost (dr, ncopies, true, inside_cost, outside_cost,
  923. NULL, body_cost_vec, false);
  924. else
  925. vect_get_store_cost (dr, ncopies, inside_cost, body_cost_vec);
  926. if (dump_enabled_p ())
  927. dump_printf_loc (MSG_NOTE, vect_location,
  928. "vect_get_data_access_cost: inside_cost = %d, "
  929. "outside_cost = %d.\n", *inside_cost, *outside_cost);
  930. }
  931. /* Insert DR into peeling hash table with NPEEL as key. */
  932. static void
  933. vect_peeling_hash_insert (loop_vec_info loop_vinfo, struct data_reference *dr,
  934. int npeel)
  935. {
  936. struct _vect_peel_info elem, *slot;
  937. _vect_peel_info **new_slot;
  938. bool supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
  939. elem.npeel = npeel;
  940. slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find (&elem);
  941. if (slot)
  942. slot->count++;
  943. else
  944. {
  945. slot = XNEW (struct _vect_peel_info);
  946. slot->npeel = npeel;
  947. slot->dr = dr;
  948. slot->count = 1;
  949. new_slot
  950. = LOOP_VINFO_PEELING_HTAB (loop_vinfo)->find_slot (slot, INSERT);
  951. *new_slot = slot;
  952. }
  953. if (!supportable_dr_alignment
  954. && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
  955. slot->count += VECT_MAX_COST;
  956. }
  957. /* Traverse peeling hash table to find peeling option that aligns maximum
  958. number of data accesses. */
  959. int
  960. vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
  961. _vect_peel_extended_info *max)
  962. {
  963. vect_peel_info elem = *slot;
  964. if (elem->count > max->peel_info.count
  965. || (elem->count == max->peel_info.count
  966. && max->peel_info.npeel > elem->npeel))
  967. {
  968. max->peel_info.npeel = elem->npeel;
  969. max->peel_info.count = elem->count;
  970. max->peel_info.dr = elem->dr;
  971. }
  972. return 1;
  973. }
  974. /* Traverse peeling hash table and calculate cost for each peeling option.
  975. Find the one with the lowest cost. */
  976. int
  977. vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
  978. _vect_peel_extended_info *min)
  979. {
  980. vect_peel_info elem = *slot;
  981. int save_misalignment, dummy;
  982. unsigned int inside_cost = 0, outside_cost = 0, i;
  983. gimple stmt = DR_STMT (elem->dr);
  984. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  985. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  986. vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
  987. struct data_reference *dr;
  988. stmt_vector_for_cost prologue_cost_vec, body_cost_vec, epilogue_cost_vec;
  989. prologue_cost_vec.create (2);
  990. body_cost_vec.create (2);
  991. epilogue_cost_vec.create (2);
  992. FOR_EACH_VEC_ELT (datarefs, i, dr)
  993. {
  994. stmt = DR_STMT (dr);
  995. stmt_info = vinfo_for_stmt (stmt);
  996. /* For interleaving, only the alignment of the first access
  997. matters. */
  998. if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
  999. && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
  1000. continue;
  1001. save_misalignment = DR_MISALIGNMENT (dr);
  1002. vect_update_misalignment_for_peel (dr, elem->dr, elem->npeel);
  1003. vect_get_data_access_cost (dr, &inside_cost, &outside_cost,
  1004. &body_cost_vec);
  1005. SET_DR_MISALIGNMENT (dr, save_misalignment);
  1006. }
  1007. auto_vec<stmt_info_for_cost> scalar_cost_vec;
  1008. vect_get_single_scalar_iteration_cost (loop_vinfo, &scalar_cost_vec);
  1009. outside_cost += vect_get_known_peeling_cost
  1010. (loop_vinfo, elem->npeel, &dummy,
  1011. &scalar_cost_vec, &prologue_cost_vec, &epilogue_cost_vec);
  1012. /* Prologue and epilogue costs are added to the target model later.
  1013. These costs depend only on the scalar iteration cost, the
  1014. number of peeling iterations finally chosen, and the number of
  1015. misaligned statements. So discard the information found here. */
  1016. prologue_cost_vec.release ();
  1017. epilogue_cost_vec.release ();
  1018. if (inside_cost < min->inside_cost
  1019. || (inside_cost == min->inside_cost && outside_cost < min->outside_cost))
  1020. {
  1021. min->inside_cost = inside_cost;
  1022. min->outside_cost = outside_cost;
  1023. min->body_cost_vec.release ();
  1024. min->body_cost_vec = body_cost_vec;
  1025. min->peel_info.dr = elem->dr;
  1026. min->peel_info.npeel = elem->npeel;
  1027. }
  1028. else
  1029. body_cost_vec.release ();
  1030. return 1;
  1031. }
  1032. /* Choose best peeling option by traversing peeling hash table and either
  1033. choosing an option with the lowest cost (if cost model is enabled) or the
  1034. option that aligns as many accesses as possible. */
  1035. static struct data_reference *
  1036. vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo,
  1037. unsigned int *npeel,
  1038. stmt_vector_for_cost *body_cost_vec)
  1039. {
  1040. struct _vect_peel_extended_info res;
  1041. res.peel_info.dr = NULL;
  1042. res.body_cost_vec = stmt_vector_for_cost ();
  1043. if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
  1044. {
  1045. res.inside_cost = INT_MAX;
  1046. res.outside_cost = INT_MAX;
  1047. LOOP_VINFO_PEELING_HTAB (loop_vinfo)
  1048. ->traverse <_vect_peel_extended_info *,
  1049. vect_peeling_hash_get_lowest_cost> (&res);
  1050. }
  1051. else
  1052. {
  1053. res.peel_info.count = 0;
  1054. LOOP_VINFO_PEELING_HTAB (loop_vinfo)
  1055. ->traverse <_vect_peel_extended_info *,
  1056. vect_peeling_hash_get_most_frequent> (&res);
  1057. }
  1058. *npeel = res.peel_info.npeel;
  1059. *body_cost_vec = res.body_cost_vec;
  1060. return res.peel_info.dr;
  1061. }
  1062. /* Function vect_enhance_data_refs_alignment
  1063. This pass will use loop versioning and loop peeling in order to enhance
  1064. the alignment of data references in the loop.
  1065. FOR NOW: we assume that whatever versioning/peeling takes place, only the
  1066. original loop is to be vectorized. Any other loops that are created by
  1067. the transformations performed in this pass - are not supposed to be
  1068. vectorized. This restriction will be relaxed.
  1069. This pass will require a cost model to guide it whether to apply peeling
  1070. or versioning or a combination of the two. For example, the scheme that
  1071. intel uses when given a loop with several memory accesses, is as follows:
  1072. choose one memory access ('p') which alignment you want to force by doing
  1073. peeling. Then, either (1) generate a loop in which 'p' is aligned and all
  1074. other accesses are not necessarily aligned, or (2) use loop versioning to
  1075. generate one loop in which all accesses are aligned, and another loop in
  1076. which only 'p' is necessarily aligned.
  1077. ("Automatic Intra-Register Vectorization for the Intel Architecture",
  1078. Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
  1079. Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
  1080. Devising a cost model is the most critical aspect of this work. It will
  1081. guide us on which access to peel for, whether to use loop versioning, how
  1082. many versions to create, etc. The cost model will probably consist of
  1083. generic considerations as well as target specific considerations (on
  1084. powerpc for example, misaligned stores are more painful than misaligned
  1085. loads).
  1086. Here are the general steps involved in alignment enhancements:
  1087. -- original loop, before alignment analysis:
  1088. for (i=0; i<N; i++){
  1089. x = q[i]; # DR_MISALIGNMENT(q) = unknown
  1090. p[i] = y; # DR_MISALIGNMENT(p) = unknown
  1091. }
  1092. -- After vect_compute_data_refs_alignment:
  1093. for (i=0; i<N; i++){
  1094. x = q[i]; # DR_MISALIGNMENT(q) = 3
  1095. p[i] = y; # DR_MISALIGNMENT(p) = unknown
  1096. }
  1097. -- Possibility 1: we do loop versioning:
  1098. if (p is aligned) {
  1099. for (i=0; i<N; i++){ # loop 1A
  1100. x = q[i]; # DR_MISALIGNMENT(q) = 3
  1101. p[i] = y; # DR_MISALIGNMENT(p) = 0
  1102. }
  1103. }
  1104. else {
  1105. for (i=0; i<N; i++){ # loop 1B
  1106. x = q[i]; # DR_MISALIGNMENT(q) = 3
  1107. p[i] = y; # DR_MISALIGNMENT(p) = unaligned
  1108. }
  1109. }
  1110. -- Possibility 2: we do loop peeling:
  1111. for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
  1112. x = q[i];
  1113. p[i] = y;
  1114. }
  1115. for (i = 3; i < N; i++){ # loop 2A
  1116. x = q[i]; # DR_MISALIGNMENT(q) = 0
  1117. p[i] = y; # DR_MISALIGNMENT(p) = unknown
  1118. }
  1119. -- Possibility 3: combination of loop peeling and versioning:
  1120. for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
  1121. x = q[i];
  1122. p[i] = y;
  1123. }
  1124. if (p is aligned) {
  1125. for (i = 3; i<N; i++){ # loop 3A
  1126. x = q[i]; # DR_MISALIGNMENT(q) = 0
  1127. p[i] = y; # DR_MISALIGNMENT(p) = 0
  1128. }
  1129. }
  1130. else {
  1131. for (i = 3; i<N; i++){ # loop 3B
  1132. x = q[i]; # DR_MISALIGNMENT(q) = 0
  1133. p[i] = y; # DR_MISALIGNMENT(p) = unaligned
  1134. }
  1135. }
  1136. These loops are later passed to loop_transform to be vectorized. The
  1137. vectorizer will use the alignment information to guide the transformation
  1138. (whether to generate regular loads/stores, or with special handling for
  1139. misalignment). */
  1140. bool
  1141. vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
  1142. {
  1143. vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
  1144. struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
  1145. enum dr_alignment_support supportable_dr_alignment;
  1146. struct data_reference *dr0 = NULL, *first_store = NULL;
  1147. struct data_reference *dr;
  1148. unsigned int i, j;
  1149. bool do_peeling = false;
  1150. bool do_versioning = false;
  1151. bool stat;
  1152. gimple stmt;
  1153. stmt_vec_info stmt_info;
  1154. unsigned int npeel = 0;
  1155. bool all_misalignments_unknown = true;
  1156. unsigned int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
  1157. unsigned possible_npeel_number = 1;
  1158. tree vectype;
  1159. unsigned int nelements, mis, same_align_drs_max = 0;
  1160. stmt_vector_for_cost body_cost_vec = stmt_vector_for_cost ();
  1161. if (dump_enabled_p ())
  1162. dump_printf_loc (MSG_NOTE, vect_location,
  1163. "=== vect_enhance_data_refs_alignment ===\n");
  1164. /* While cost model enhancements are expected in the future, the high level
  1165. view of the code at this time is as follows:
  1166. A) If there is a misaligned access then see if peeling to align
  1167. this access can make all data references satisfy
  1168. vect_supportable_dr_alignment. If so, update data structures
  1169. as needed and return true.
  1170. B) If peeling wasn't possible and there is a data reference with an
  1171. unknown misalignment that does not satisfy vect_supportable_dr_alignment
  1172. then see if loop versioning checks can be used to make all data
  1173. references satisfy vect_supportable_dr_alignment. If so, update
  1174. data structures as needed and return true.
  1175. C) If neither peeling nor versioning were successful then return false if
  1176. any data reference does not satisfy vect_supportable_dr_alignment.
  1177. D) Return true (all data references satisfy vect_supportable_dr_alignment).
  1178. Note, Possibility 3 above (which is peeling and versioning together) is not
  1179. being done at this time. */
  1180. /* (1) Peeling to force alignment. */
  1181. /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
  1182. Considerations:
  1183. + How many accesses will become aligned due to the peeling
  1184. - How many accesses will become unaligned due to the peeling,
  1185. and the cost of misaligned accesses.
  1186. - The cost of peeling (the extra runtime checks, the increase
  1187. in code size). */
  1188. FOR_EACH_VEC_ELT (datarefs, i, dr)
  1189. {
  1190. stmt = DR_STMT (dr);
  1191. stmt_info = vinfo_for_stmt (stmt);
  1192. if (!STMT_VINFO_RELEVANT_P (stmt_info))
  1193. continue;
  1194. /* For interleaving, only the alignment of the first access
  1195. matters. */
  1196. if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
  1197. && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
  1198. continue;
  1199. /* For invariant accesses there is nothing to enhance. */
  1200. if (integer_zerop (DR_STEP (dr)))
  1201. continue;
  1202. /* Strided loads perform only component accesses, alignment is
  1203. irrelevant for them. */
  1204. if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
  1205. continue;
  1206. supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
  1207. do_peeling = vector_alignment_reachable_p (dr);
  1208. if (do_peeling)
  1209. {
  1210. if (known_alignment_for_access_p (dr))
  1211. {
  1212. unsigned int npeel_tmp;
  1213. bool negative = tree_int_cst_compare (DR_STEP (dr),
  1214. size_zero_node) < 0;
  1215. /* Save info about DR in the hash table. */
  1216. if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo))
  1217. LOOP_VINFO_PEELING_HTAB (loop_vinfo)
  1218. = new hash_table<peel_info_hasher> (1);
  1219. vectype = STMT_VINFO_VECTYPE (stmt_info);
  1220. nelements = TYPE_VECTOR_SUBPARTS (vectype);
  1221. mis = DR_MISALIGNMENT (dr) / GET_MODE_SIZE (TYPE_MODE (
  1222. TREE_TYPE (DR_REF (dr))));
  1223. npeel_tmp = (negative
  1224. ? (mis - nelements) : (nelements - mis))
  1225. & (nelements - 1);
  1226. /* For multiple types, it is possible that the bigger type access
  1227. will have more than one peeling option. E.g., a loop with two
  1228. types: one of size (vector size / 4), and the other one of
  1229. size (vector size / 8). Vectorization factor will 8. If both
  1230. access are misaligned by 3, the first one needs one scalar
  1231. iteration to be aligned, and the second one needs 5. But the
  1232. the first one will be aligned also by peeling 5 scalar
  1233. iterations, and in that case both accesses will be aligned.
  1234. Hence, except for the immediate peeling amount, we also want
  1235. to try to add full vector size, while we don't exceed
  1236. vectorization factor.
  1237. We do this automtically for cost model, since we calculate cost
  1238. for every peeling option. */
  1239. if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
  1240. possible_npeel_number = vf /nelements;
  1241. /* Handle the aligned case. We may decide to align some other
  1242. access, making DR unaligned. */
  1243. if (DR_MISALIGNMENT (dr) == 0)
  1244. {
  1245. npeel_tmp = 0;
  1246. if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
  1247. possible_npeel_number++;
  1248. }
  1249. for (j = 0; j < possible_npeel_number; j++)
  1250. {
  1251. gcc_assert (npeel_tmp <= vf);
  1252. vect_peeling_hash_insert (loop_vinfo, dr, npeel_tmp);
  1253. npeel_tmp += nelements;
  1254. }
  1255. all_misalignments_unknown = false;
  1256. /* Data-ref that was chosen for the case that all the
  1257. misalignments are unknown is not relevant anymore, since we
  1258. have a data-ref with known alignment. */
  1259. dr0 = NULL;
  1260. }
  1261. else
  1262. {
  1263. /* If we don't know any misalignment values, we prefer
  1264. peeling for data-ref that has the maximum number of data-refs
  1265. with the same alignment, unless the target prefers to align
  1266. stores over load. */
  1267. if (all_misalignments_unknown)
  1268. {
  1269. unsigned same_align_drs
  1270. = STMT_VINFO_SAME_ALIGN_REFS (stmt_info).length ();
  1271. if (!dr0
  1272. || same_align_drs_max < same_align_drs)
  1273. {
  1274. same_align_drs_max = same_align_drs;
  1275. dr0 = dr;
  1276. }
  1277. /* For data-refs with the same number of related
  1278. accesses prefer the one where the misalign
  1279. computation will be invariant in the outermost loop. */
  1280. else if (same_align_drs_max == same_align_drs)
  1281. {
  1282. struct loop *ivloop0, *ivloop;
  1283. ivloop0 = outermost_invariant_loop_for_expr
  1284. (loop, DR_BASE_ADDRESS (dr0));
  1285. ivloop = outermost_invariant_loop_for_expr
  1286. (loop, DR_BASE_ADDRESS (dr));
  1287. if ((ivloop && !ivloop0)
  1288. || (ivloop && ivloop0
  1289. && flow_loop_nested_p (ivloop, ivloop0)))
  1290. dr0 = dr;
  1291. }
  1292. if (!first_store && DR_IS_WRITE (dr))
  1293. first_store = dr;
  1294. }
  1295. /* If there are both known and unknown misaligned accesses in the
  1296. loop, we choose peeling amount according to the known
  1297. accesses. */
  1298. if (!supportable_dr_alignment)
  1299. {
  1300. dr0 = dr;
  1301. if (!first_store && DR_IS_WRITE (dr))
  1302. first_store = dr;
  1303. }
  1304. }
  1305. }
  1306. else
  1307. {
  1308. if (!aligned_access_p (dr))
  1309. {
  1310. if (dump_enabled_p ())
  1311. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1312. "vector alignment may not be reachable\n");
  1313. break;
  1314. }
  1315. }
  1316. }
  1317. /* Check if we can possibly peel the loop. */
  1318. if (!vect_can_advance_ivs_p (loop_vinfo)
  1319. || !slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
  1320. do_peeling = false;
  1321. /* If we don't know how many times the peeling loop will run
  1322. assume it will run VF-1 times and disable peeling if the remaining
  1323. iters are less than the vectorization factor. */
  1324. if (do_peeling
  1325. && all_misalignments_unknown
  1326. && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
  1327. && (LOOP_VINFO_INT_NITERS (loop_vinfo)
  1328. < 2 * (unsigned) LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1))
  1329. do_peeling = false;
  1330. if (do_peeling
  1331. && all_misalignments_unknown
  1332. && vect_supportable_dr_alignment (dr0, false))
  1333. {
  1334. /* Check if the target requires to prefer stores over loads, i.e., if
  1335. misaligned stores are more expensive than misaligned loads (taking
  1336. drs with same alignment into account). */
  1337. if (first_store && DR_IS_READ (dr0))
  1338. {
  1339. unsigned int load_inside_cost = 0, load_outside_cost = 0;
  1340. unsigned int store_inside_cost = 0, store_outside_cost = 0;
  1341. unsigned int load_inside_penalty = 0, load_outside_penalty = 0;
  1342. unsigned int store_inside_penalty = 0, store_outside_penalty = 0;
  1343. stmt_vector_for_cost dummy;
  1344. dummy.create (2);
  1345. vect_get_data_access_cost (dr0, &load_inside_cost, &load_outside_cost,
  1346. &dummy);
  1347. vect_get_data_access_cost (first_store, &store_inside_cost,
  1348. &store_outside_cost, &dummy);
  1349. dummy.release ();
  1350. /* Calculate the penalty for leaving FIRST_STORE unaligned (by
  1351. aligning the load DR0). */
  1352. load_inside_penalty = store_inside_cost;
  1353. load_outside_penalty = store_outside_cost;
  1354. for (i = 0;
  1355. STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
  1356. DR_STMT (first_store))).iterate (i, &dr);
  1357. i++)
  1358. if (DR_IS_READ (dr))
  1359. {
  1360. load_inside_penalty += load_inside_cost;
  1361. load_outside_penalty += load_outside_cost;
  1362. }
  1363. else
  1364. {
  1365. load_inside_penalty += store_inside_cost;
  1366. load_outside_penalty += store_outside_cost;
  1367. }
  1368. /* Calculate the penalty for leaving DR0 unaligned (by
  1369. aligning the FIRST_STORE). */
  1370. store_inside_penalty = load_inside_cost;
  1371. store_outside_penalty = load_outside_cost;
  1372. for (i = 0;
  1373. STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
  1374. DR_STMT (dr0))).iterate (i, &dr);
  1375. i++)
  1376. if (DR_IS_READ (dr))
  1377. {
  1378. store_inside_penalty += load_inside_cost;
  1379. store_outside_penalty += load_outside_cost;
  1380. }
  1381. else
  1382. {
  1383. store_inside_penalty += store_inside_cost;
  1384. store_outside_penalty += store_outside_cost;
  1385. }
  1386. if (load_inside_penalty > store_inside_penalty
  1387. || (load_inside_penalty == store_inside_penalty
  1388. && load_outside_penalty > store_outside_penalty))
  1389. dr0 = first_store;
  1390. }
  1391. /* In case there are only loads with different unknown misalignments, use
  1392. peeling only if it may help to align other accesses in the loop. */
  1393. if (!first_store
  1394. && !STMT_VINFO_SAME_ALIGN_REFS (
  1395. vinfo_for_stmt (DR_STMT (dr0))).length ()
  1396. && vect_supportable_dr_alignment (dr0, false)
  1397. != dr_unaligned_supported)
  1398. do_peeling = false;
  1399. }
  1400. if (do_peeling && !dr0)
  1401. {
  1402. /* Peeling is possible, but there is no data access that is not supported
  1403. unless aligned. So we try to choose the best possible peeling. */
  1404. /* We should get here only if there are drs with known misalignment. */
  1405. gcc_assert (!all_misalignments_unknown);
  1406. /* Choose the best peeling from the hash table. */
  1407. dr0 = vect_peeling_hash_choose_best_peeling (loop_vinfo, &npeel,
  1408. &body_cost_vec);
  1409. if (!dr0 || !npeel)
  1410. do_peeling = false;
  1411. /* If peeling by npeel will result in a remaining loop not iterating
  1412. enough to be vectorized then do not peel. */
  1413. if (do_peeling
  1414. && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
  1415. && (LOOP_VINFO_INT_NITERS (loop_vinfo)
  1416. < LOOP_VINFO_VECT_FACTOR (loop_vinfo) + npeel))
  1417. do_peeling = false;
  1418. }
  1419. if (do_peeling)
  1420. {
  1421. stmt = DR_STMT (dr0);
  1422. stmt_info = vinfo_for_stmt (stmt);
  1423. vectype = STMT_VINFO_VECTYPE (stmt_info);
  1424. nelements = TYPE_VECTOR_SUBPARTS (vectype);
  1425. if (known_alignment_for_access_p (dr0))
  1426. {
  1427. bool negative = tree_int_cst_compare (DR_STEP (dr0),
  1428. size_zero_node) < 0;
  1429. if (!npeel)
  1430. {
  1431. /* Since it's known at compile time, compute the number of
  1432. iterations in the peeled loop (the peeling factor) for use in
  1433. updating DR_MISALIGNMENT values. The peeling factor is the
  1434. vectorization factor minus the misalignment as an element
  1435. count. */
  1436. mis = DR_MISALIGNMENT (dr0);
  1437. mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
  1438. npeel = ((negative ? mis - nelements : nelements - mis)
  1439. & (nelements - 1));
  1440. }
  1441. /* For interleaved data access every iteration accesses all the
  1442. members of the group, therefore we divide the number of iterations
  1443. by the group size. */
  1444. stmt_info = vinfo_for_stmt (DR_STMT (dr0));
  1445. if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
  1446. npeel /= GROUP_SIZE (stmt_info);
  1447. if (dump_enabled_p ())
  1448. dump_printf_loc (MSG_NOTE, vect_location,
  1449. "Try peeling by %d\n", npeel);
  1450. }
  1451. /* Ensure that all data refs can be vectorized after the peel. */
  1452. FOR_EACH_VEC_ELT (datarefs, i, dr)
  1453. {
  1454. int save_misalignment;
  1455. if (dr == dr0)
  1456. continue;
  1457. stmt = DR_STMT (dr);
  1458. stmt_info = vinfo_for_stmt (stmt);
  1459. /* For interleaving, only the alignment of the first access
  1460. matters. */
  1461. if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
  1462. && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
  1463. continue;
  1464. /* Strided loads perform only component accesses, alignment is
  1465. irrelevant for them. */
  1466. if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
  1467. continue;
  1468. save_misalignment = DR_MISALIGNMENT (dr);
  1469. vect_update_misalignment_for_peel (dr, dr0, npeel);
  1470. supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
  1471. SET_DR_MISALIGNMENT (dr, save_misalignment);
  1472. if (!supportable_dr_alignment)
  1473. {
  1474. do_peeling = false;
  1475. break;
  1476. }
  1477. }
  1478. if (do_peeling && known_alignment_for_access_p (dr0) && npeel == 0)
  1479. {
  1480. stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
  1481. if (!stat)
  1482. do_peeling = false;
  1483. else
  1484. {
  1485. body_cost_vec.release ();
  1486. return stat;
  1487. }
  1488. }
  1489. if (do_peeling)
  1490. {
  1491. unsigned max_allowed_peel
  1492. = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT);
  1493. if (max_allowed_peel != (unsigned)-1)
  1494. {
  1495. unsigned max_peel = npeel;
  1496. if (max_peel == 0)
  1497. {
  1498. gimple dr_stmt = DR_STMT (dr0);
  1499. stmt_vec_info vinfo = vinfo_for_stmt (dr_stmt);
  1500. tree vtype = STMT_VINFO_VECTYPE (vinfo);
  1501. max_peel = TYPE_VECTOR_SUBPARTS (vtype) - 1;
  1502. }
  1503. if (max_peel > max_allowed_peel)
  1504. {
  1505. do_peeling = false;
  1506. if (dump_enabled_p ())
  1507. dump_printf_loc (MSG_NOTE, vect_location,
  1508. "Disable peeling, max peels reached: %d\n", max_peel);
  1509. }
  1510. }
  1511. }
  1512. if (do_peeling)
  1513. {
  1514. /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
  1515. If the misalignment of DR_i is identical to that of dr0 then set
  1516. DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
  1517. dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
  1518. by the peeling factor times the element size of DR_i (MOD the
  1519. vectorization factor times the size). Otherwise, the
  1520. misalignment of DR_i must be set to unknown. */
  1521. FOR_EACH_VEC_ELT (datarefs, i, dr)
  1522. if (dr != dr0)
  1523. vect_update_misalignment_for_peel (dr, dr0, npeel);
  1524. LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
  1525. if (npeel)
  1526. LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
  1527. else
  1528. LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
  1529. = DR_MISALIGNMENT (dr0);
  1530. SET_DR_MISALIGNMENT (dr0, 0);
  1531. if (dump_enabled_p ())
  1532. {
  1533. dump_printf_loc (MSG_NOTE, vect_location,
  1534. "Alignment of access forced using peeling.\n");
  1535. dump_printf_loc (MSG_NOTE, vect_location,
  1536. "Peeling for alignment will be applied.\n");
  1537. }
  1538. /* The inside-loop cost will be accounted for in vectorizable_load
  1539. and vectorizable_store correctly with adjusted alignments.
  1540. Drop the body_cst_vec on the floor here. */
  1541. body_cost_vec.release ();
  1542. stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
  1543. gcc_assert (stat);
  1544. return stat;
  1545. }
  1546. }
  1547. body_cost_vec.release ();
  1548. /* (2) Versioning to force alignment. */
  1549. /* Try versioning if:
  1550. 1) optimize loop for speed
  1551. 2) there is at least one unsupported misaligned data ref with an unknown
  1552. misalignment, and
  1553. 3) all misaligned data refs with a known misalignment are supported, and
  1554. 4) the number of runtime alignment checks is within reason. */
  1555. do_versioning =
  1556. optimize_loop_nest_for_speed_p (loop)
  1557. && (!loop->inner); /* FORNOW */
  1558. if (do_versioning)
  1559. {
  1560. FOR_EACH_VEC_ELT (datarefs, i, dr)
  1561. {
  1562. stmt = DR_STMT (dr);
  1563. stmt_info = vinfo_for_stmt (stmt);
  1564. /* For interleaving, only the alignment of the first access
  1565. matters. */
  1566. if (aligned_access_p (dr)
  1567. || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
  1568. && GROUP_FIRST_ELEMENT (stmt_info) != stmt))
  1569. continue;
  1570. /* Strided loads perform only component accesses, alignment is
  1571. irrelevant for them. */
  1572. if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
  1573. continue;
  1574. supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
  1575. if (!supportable_dr_alignment)
  1576. {
  1577. gimple stmt;
  1578. int mask;
  1579. tree vectype;
  1580. if (known_alignment_for_access_p (dr)
  1581. || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
  1582. >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
  1583. {
  1584. do_versioning = false;
  1585. break;
  1586. }
  1587. stmt = DR_STMT (dr);
  1588. vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
  1589. gcc_assert (vectype);
  1590. /* The rightmost bits of an aligned address must be zeros.
  1591. Construct the mask needed for this test. For example,
  1592. GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
  1593. mask must be 15 = 0xf. */
  1594. mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
  1595. /* FORNOW: use the same mask to test all potentially unaligned
  1596. references in the loop. The vectorizer currently supports
  1597. a single vector size, see the reference to
  1598. GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
  1599. vectorization factor is computed. */
  1600. gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
  1601. || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
  1602. LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
  1603. LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (
  1604. DR_STMT (dr));
  1605. }
  1606. }
  1607. /* Versioning requires at least one misaligned data reference. */
  1608. if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
  1609. do_versioning = false;
  1610. else if (!do_versioning)
  1611. LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
  1612. }
  1613. if (do_versioning)
  1614. {
  1615. vec<gimple> may_misalign_stmts
  1616. = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
  1617. gimple stmt;
  1618. /* It can now be assumed that the data references in the statements
  1619. in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
  1620. of the loop being vectorized. */
  1621. FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt)
  1622. {
  1623. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  1624. dr = STMT_VINFO_DATA_REF (stmt_info);
  1625. SET_DR_MISALIGNMENT (dr, 0);
  1626. if (dump_enabled_p ())
  1627. dump_printf_loc (MSG_NOTE, vect_location,
  1628. "Alignment of access forced using versioning.\n");
  1629. }
  1630. if (dump_enabled_p ())
  1631. dump_printf_loc (MSG_NOTE, vect_location,
  1632. "Versioning for alignment will be applied.\n");
  1633. /* Peeling and versioning can't be done together at this time. */
  1634. gcc_assert (! (do_peeling && do_versioning));
  1635. stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
  1636. gcc_assert (stat);
  1637. return stat;
  1638. }
  1639. /* This point is reached if neither peeling nor versioning is being done. */
  1640. gcc_assert (! (do_peeling || do_versioning));
  1641. stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
  1642. return stat;
  1643. }
  1644. /* Function vect_find_same_alignment_drs.
  1645. Update group and alignment relations according to the chosen
  1646. vectorization factor. */
  1647. static void
  1648. vect_find_same_alignment_drs (struct data_dependence_relation *ddr,
  1649. loop_vec_info loop_vinfo)
  1650. {
  1651. unsigned int i;
  1652. struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
  1653. int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
  1654. struct data_reference *dra = DDR_A (ddr);
  1655. struct data_reference *drb = DDR_B (ddr);
  1656. stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
  1657. stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
  1658. int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
  1659. int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
  1660. lambda_vector dist_v;
  1661. unsigned int loop_depth;
  1662. if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
  1663. return;
  1664. if (dra == drb)
  1665. return;
  1666. if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
  1667. return;
  1668. /* Loop-based vectorization and known data dependence. */
  1669. if (DDR_NUM_DIST_VECTS (ddr) == 0)
  1670. return;
  1671. /* Data-dependence analysis reports a distance vector of zero
  1672. for data-references that overlap only in the first iteration
  1673. but have different sign step (see PR45764).
  1674. So as a sanity check require equal DR_STEP. */
  1675. if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
  1676. return;
  1677. loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
  1678. FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
  1679. {
  1680. int dist = dist_v[loop_depth];
  1681. if (dump_enabled_p ())
  1682. dump_printf_loc (MSG_NOTE, vect_location,
  1683. "dependence distance = %d.\n", dist);
  1684. /* Same loop iteration. */
  1685. if (dist == 0
  1686. || (dist % vectorization_factor == 0 && dra_size == drb_size))
  1687. {
  1688. /* Two references with distance zero have the same alignment. */
  1689. STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a).safe_push (drb);
  1690. STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b).safe_push (dra);
  1691. if (dump_enabled_p ())
  1692. {
  1693. dump_printf_loc (MSG_NOTE, vect_location,
  1694. "accesses have the same alignment.\n");
  1695. dump_printf (MSG_NOTE,
  1696. "dependence distance modulo vf == 0 between ");
  1697. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
  1698. dump_printf (MSG_NOTE, " and ");
  1699. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
  1700. dump_printf (MSG_NOTE, "\n");
  1701. }
  1702. }
  1703. }
  1704. }
  1705. /* Function vect_analyze_data_refs_alignment
  1706. Analyze the alignment of the data-references in the loop.
  1707. Return FALSE if a data reference is found that cannot be vectorized. */
  1708. bool
  1709. vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo,
  1710. bb_vec_info bb_vinfo)
  1711. {
  1712. if (dump_enabled_p ())
  1713. dump_printf_loc (MSG_NOTE, vect_location,
  1714. "=== vect_analyze_data_refs_alignment ===\n");
  1715. /* Mark groups of data references with same alignment using
  1716. data dependence information. */
  1717. if (loop_vinfo)
  1718. {
  1719. vec<ddr_p> ddrs = LOOP_VINFO_DDRS (loop_vinfo);
  1720. struct data_dependence_relation *ddr;
  1721. unsigned int i;
  1722. FOR_EACH_VEC_ELT (ddrs, i, ddr)
  1723. vect_find_same_alignment_drs (ddr, loop_vinfo);
  1724. }
  1725. if (!vect_compute_data_refs_alignment (loop_vinfo, bb_vinfo))
  1726. {
  1727. if (dump_enabled_p ())
  1728. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1729. "not vectorized: can't calculate alignment "
  1730. "for data ref.\n");
  1731. return false;
  1732. }
  1733. return true;
  1734. }
  1735. /* Analyze groups of accesses: check that DR belongs to a group of
  1736. accesses of legal size, step, etc. Detect gaps, single element
  1737. interleaving, and other special cases. Set grouped access info.
  1738. Collect groups of strided stores for further use in SLP analysis. */
  1739. static bool
  1740. vect_analyze_group_access (struct data_reference *dr)
  1741. {
  1742. tree step = DR_STEP (dr);
  1743. tree scalar_type = TREE_TYPE (DR_REF (dr));
  1744. HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
  1745. gimple stmt = DR_STMT (dr);
  1746. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  1747. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  1748. bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
  1749. HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
  1750. HOST_WIDE_INT groupsize, last_accessed_element = 1;
  1751. bool slp_impossible = false;
  1752. struct loop *loop = NULL;
  1753. if (loop_vinfo)
  1754. loop = LOOP_VINFO_LOOP (loop_vinfo);
  1755. /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
  1756. size of the interleaving group (including gaps). */
  1757. groupsize = absu_hwi (dr_step) / type_size;
  1758. /* Not consecutive access is possible only if it is a part of interleaving. */
  1759. if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
  1760. {
  1761. /* Check if it this DR is a part of interleaving, and is a single
  1762. element of the group that is accessed in the loop. */
  1763. /* Gaps are supported only for loads. STEP must be a multiple of the type
  1764. size. The size of the group must be a power of 2. */
  1765. if (DR_IS_READ (dr)
  1766. && (dr_step % type_size) == 0
  1767. && groupsize > 0
  1768. && exact_log2 (groupsize) != -1)
  1769. {
  1770. GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = stmt;
  1771. GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
  1772. if (dump_enabled_p ())
  1773. {
  1774. dump_printf_loc (MSG_NOTE, vect_location,
  1775. "Detected single element interleaving ");
  1776. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
  1777. dump_printf (MSG_NOTE, " step ");
  1778. dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
  1779. dump_printf (MSG_NOTE, "\n");
  1780. }
  1781. if (loop_vinfo)
  1782. {
  1783. if (dump_enabled_p ())
  1784. dump_printf_loc (MSG_NOTE, vect_location,
  1785. "Data access with gaps requires scalar "
  1786. "epilogue loop\n");
  1787. if (loop->inner)
  1788. {
  1789. if (dump_enabled_p ())
  1790. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1791. "Peeling for outer loop is not"
  1792. " supported\n");
  1793. return false;
  1794. }
  1795. LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
  1796. }
  1797. return true;
  1798. }
  1799. if (dump_enabled_p ())
  1800. {
  1801. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1802. "not consecutive access ");
  1803. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  1804. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  1805. }
  1806. if (bb_vinfo)
  1807. {
  1808. /* Mark the statement as unvectorizable. */
  1809. STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
  1810. return true;
  1811. }
  1812. return false;
  1813. }
  1814. if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt)
  1815. {
  1816. /* First stmt in the interleaving chain. Check the chain. */
  1817. gimple next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
  1818. struct data_reference *data_ref = dr;
  1819. unsigned int count = 1;
  1820. tree prev_init = DR_INIT (data_ref);
  1821. gimple prev = stmt;
  1822. HOST_WIDE_INT diff, gaps = 0;
  1823. unsigned HOST_WIDE_INT count_in_bytes;
  1824. while (next)
  1825. {
  1826. /* Skip same data-refs. In case that two or more stmts share
  1827. data-ref (supported only for loads), we vectorize only the first
  1828. stmt, and the rest get their vectorized loads from the first
  1829. one. */
  1830. if (!tree_int_cst_compare (DR_INIT (data_ref),
  1831. DR_INIT (STMT_VINFO_DATA_REF (
  1832. vinfo_for_stmt (next)))))
  1833. {
  1834. if (DR_IS_WRITE (data_ref))
  1835. {
  1836. if (dump_enabled_p ())
  1837. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1838. "Two store stmts share the same dr.\n");
  1839. return false;
  1840. }
  1841. /* For load use the same data-ref load. */
  1842. GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
  1843. prev = next;
  1844. next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
  1845. continue;
  1846. }
  1847. prev = next;
  1848. data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
  1849. /* All group members have the same STEP by construction. */
  1850. gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
  1851. /* Check that the distance between two accesses is equal to the type
  1852. size. Otherwise, we have gaps. */
  1853. diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
  1854. - TREE_INT_CST_LOW (prev_init)) / type_size;
  1855. if (diff != 1)
  1856. {
  1857. /* FORNOW: SLP of accesses with gaps is not supported. */
  1858. slp_impossible = true;
  1859. if (DR_IS_WRITE (data_ref))
  1860. {
  1861. if (dump_enabled_p ())
  1862. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1863. "interleaved store with gaps\n");
  1864. return false;
  1865. }
  1866. gaps += diff - 1;
  1867. }
  1868. last_accessed_element += diff;
  1869. /* Store the gap from the previous member of the group. If there is no
  1870. gap in the access, GROUP_GAP is always 1. */
  1871. GROUP_GAP (vinfo_for_stmt (next)) = diff;
  1872. prev_init = DR_INIT (data_ref);
  1873. next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
  1874. /* Count the number of data-refs in the chain. */
  1875. count++;
  1876. }
  1877. /* COUNT is the number of accesses found, we multiply it by the size of
  1878. the type to get COUNT_IN_BYTES. */
  1879. count_in_bytes = type_size * count;
  1880. /* Check that the size of the interleaving (including gaps) is not
  1881. greater than STEP. */
  1882. if (dr_step != 0
  1883. && absu_hwi (dr_step) < count_in_bytes + gaps * type_size)
  1884. {
  1885. if (dump_enabled_p ())
  1886. {
  1887. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1888. "interleaving size is greater than step for ");
  1889. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  1890. DR_REF (dr));
  1891. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  1892. }
  1893. return false;
  1894. }
  1895. /* Check that the size of the interleaving is equal to STEP for stores,
  1896. i.e., that there are no gaps. */
  1897. if (dr_step != 0
  1898. && absu_hwi (dr_step) != count_in_bytes)
  1899. {
  1900. if (DR_IS_READ (dr))
  1901. {
  1902. slp_impossible = true;
  1903. /* There is a gap after the last load in the group. This gap is a
  1904. difference between the groupsize and the number of elements.
  1905. When there is no gap, this difference should be 0. */
  1906. GROUP_GAP (vinfo_for_stmt (stmt)) = groupsize - count;
  1907. }
  1908. else
  1909. {
  1910. if (dump_enabled_p ())
  1911. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1912. "interleaved store with gaps\n");
  1913. return false;
  1914. }
  1915. }
  1916. /* Check that STEP is a multiple of type size. */
  1917. if (dr_step != 0
  1918. && (dr_step % type_size) != 0)
  1919. {
  1920. if (dump_enabled_p ())
  1921. {
  1922. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1923. "step is not a multiple of type size: step ");
  1924. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, step);
  1925. dump_printf (MSG_MISSED_OPTIMIZATION, " size ");
  1926. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  1927. TYPE_SIZE_UNIT (scalar_type));
  1928. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  1929. }
  1930. return false;
  1931. }
  1932. if (groupsize == 0)
  1933. groupsize = count;
  1934. GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
  1935. if (dump_enabled_p ())
  1936. dump_printf_loc (MSG_NOTE, vect_location,
  1937. "Detected interleaving of size %d\n", (int)groupsize);
  1938. /* SLP: create an SLP data structure for every interleaving group of
  1939. stores for further analysis in vect_analyse_slp. */
  1940. if (DR_IS_WRITE (dr) && !slp_impossible)
  1941. {
  1942. if (loop_vinfo)
  1943. LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt);
  1944. if (bb_vinfo)
  1945. BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt);
  1946. }
  1947. /* There is a gap in the end of the group. */
  1948. if (groupsize - last_accessed_element > 0 && loop_vinfo)
  1949. {
  1950. if (dump_enabled_p ())
  1951. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1952. "Data access with gaps requires scalar "
  1953. "epilogue loop\n");
  1954. if (loop->inner)
  1955. {
  1956. if (dump_enabled_p ())
  1957. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1958. "Peeling for outer loop is not supported\n");
  1959. return false;
  1960. }
  1961. LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
  1962. }
  1963. }
  1964. return true;
  1965. }
  1966. /* Analyze the access pattern of the data-reference DR.
  1967. In case of non-consecutive accesses call vect_analyze_group_access() to
  1968. analyze groups of accesses. */
  1969. static bool
  1970. vect_analyze_data_ref_access (struct data_reference *dr)
  1971. {
  1972. tree step = DR_STEP (dr);
  1973. tree scalar_type = TREE_TYPE (DR_REF (dr));
  1974. gimple stmt = DR_STMT (dr);
  1975. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  1976. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  1977. struct loop *loop = NULL;
  1978. if (loop_vinfo)
  1979. loop = LOOP_VINFO_LOOP (loop_vinfo);
  1980. if (loop_vinfo && !step)
  1981. {
  1982. if (dump_enabled_p ())
  1983. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1984. "bad data-ref access in loop\n");
  1985. return false;
  1986. }
  1987. /* Allow invariant loads in not nested loops. */
  1988. if (loop_vinfo && integer_zerop (step))
  1989. {
  1990. GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
  1991. if (nested_in_vect_loop_p (loop, stmt))
  1992. {
  1993. if (dump_enabled_p ())
  1994. dump_printf_loc (MSG_NOTE, vect_location,
  1995. "zero step in inner loop of nest\n");
  1996. return false;
  1997. }
  1998. return DR_IS_READ (dr);
  1999. }
  2000. if (loop && nested_in_vect_loop_p (loop, stmt))
  2001. {
  2002. /* Interleaved accesses are not yet supported within outer-loop
  2003. vectorization for references in the inner-loop. */
  2004. GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
  2005. /* For the rest of the analysis we use the outer-loop step. */
  2006. step = STMT_VINFO_DR_STEP (stmt_info);
  2007. if (integer_zerop (step))
  2008. {
  2009. if (dump_enabled_p ())
  2010. dump_printf_loc (MSG_NOTE, vect_location,
  2011. "zero step in outer loop.\n");
  2012. if (DR_IS_READ (dr))
  2013. return true;
  2014. else
  2015. return false;
  2016. }
  2017. }
  2018. /* Consecutive? */
  2019. if (TREE_CODE (step) == INTEGER_CST)
  2020. {
  2021. HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
  2022. if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
  2023. || (dr_step < 0
  2024. && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
  2025. {
  2026. /* Mark that it is not interleaving. */
  2027. GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
  2028. return true;
  2029. }
  2030. }
  2031. if (loop && nested_in_vect_loop_p (loop, stmt))
  2032. {
  2033. if (dump_enabled_p ())
  2034. dump_printf_loc (MSG_NOTE, vect_location,
  2035. "grouped access in outer loop.\n");
  2036. return false;
  2037. }
  2038. /* Assume this is a DR handled by non-constant strided load case. */
  2039. if (TREE_CODE (step) != INTEGER_CST)
  2040. return STMT_VINFO_STRIDE_LOAD_P (stmt_info);
  2041. /* Not consecutive access - check if it's a part of interleaving group. */
  2042. return vect_analyze_group_access (dr);
  2043. }
  2044. /* A helper function used in the comparator function to sort data
  2045. references. T1 and T2 are two data references to be compared.
  2046. The function returns -1, 0, or 1. */
  2047. static int
  2048. compare_tree (tree t1, tree t2)
  2049. {
  2050. int i, cmp;
  2051. enum tree_code code;
  2052. char tclass;
  2053. if (t1 == t2)
  2054. return 0;
  2055. if (t1 == NULL)
  2056. return -1;
  2057. if (t2 == NULL)
  2058. return 1;
  2059. if (TREE_CODE (t1) != TREE_CODE (t2))
  2060. return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
  2061. code = TREE_CODE (t1);
  2062. switch (code)
  2063. {
  2064. /* For const values, we can just use hash values for comparisons. */
  2065. case INTEGER_CST:
  2066. case REAL_CST:
  2067. case FIXED_CST:
  2068. case STRING_CST:
  2069. case COMPLEX_CST:
  2070. case VECTOR_CST:
  2071. {
  2072. hashval_t h1 = iterative_hash_expr (t1, 0);
  2073. hashval_t h2 = iterative_hash_expr (t2, 0);
  2074. if (h1 != h2)
  2075. return h1 < h2 ? -1 : 1;
  2076. break;
  2077. }
  2078. case SSA_NAME:
  2079. cmp = compare_tree (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
  2080. if (cmp != 0)
  2081. return cmp;
  2082. if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
  2083. return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
  2084. break;
  2085. default:
  2086. tclass = TREE_CODE_CLASS (code);
  2087. /* For var-decl, we could compare their UIDs. */
  2088. if (tclass == tcc_declaration)
  2089. {
  2090. if (DECL_UID (t1) != DECL_UID (t2))
  2091. return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
  2092. break;
  2093. }
  2094. /* For expressions with operands, compare their operands recursively. */
  2095. for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
  2096. {
  2097. cmp = compare_tree (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
  2098. if (cmp != 0)
  2099. return cmp;
  2100. }
  2101. }
  2102. return 0;
  2103. }
  2104. /* Compare two data-references DRA and DRB to group them into chunks
  2105. suitable for grouping. */
  2106. static int
  2107. dr_group_sort_cmp (const void *dra_, const void *drb_)
  2108. {
  2109. data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
  2110. data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
  2111. int cmp;
  2112. /* Stabilize sort. */
  2113. if (dra == drb)
  2114. return 0;
  2115. /* Ordering of DRs according to base. */
  2116. if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0))
  2117. {
  2118. cmp = compare_tree (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb));
  2119. if (cmp != 0)
  2120. return cmp;
  2121. }
  2122. /* And according to DR_OFFSET. */
  2123. if (!dr_equal_offsets_p (dra, drb))
  2124. {
  2125. cmp = compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
  2126. if (cmp != 0)
  2127. return cmp;
  2128. }
  2129. /* Put reads before writes. */
  2130. if (DR_IS_READ (dra) != DR_IS_READ (drb))
  2131. return DR_IS_READ (dra) ? -1 : 1;
  2132. /* Then sort after access size. */
  2133. if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
  2134. TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))), 0))
  2135. {
  2136. cmp = compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
  2137. TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
  2138. if (cmp != 0)
  2139. return cmp;
  2140. }
  2141. /* And after step. */
  2142. if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
  2143. {
  2144. cmp = compare_tree (DR_STEP (dra), DR_STEP (drb));
  2145. if (cmp != 0)
  2146. return cmp;
  2147. }
  2148. /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
  2149. cmp = tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb));
  2150. if (cmp == 0)
  2151. return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
  2152. return cmp;
  2153. }
  2154. /* Function vect_analyze_data_ref_accesses.
  2155. Analyze the access pattern of all the data references in the loop.
  2156. FORNOW: the only access pattern that is considered vectorizable is a
  2157. simple step 1 (consecutive) access.
  2158. FORNOW: handle only arrays and pointer accesses. */
  2159. bool
  2160. vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
  2161. {
  2162. unsigned int i;
  2163. vec<data_reference_p> datarefs;
  2164. struct data_reference *dr;
  2165. if (dump_enabled_p ())
  2166. dump_printf_loc (MSG_NOTE, vect_location,
  2167. "=== vect_analyze_data_ref_accesses ===\n");
  2168. if (loop_vinfo)
  2169. datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
  2170. else
  2171. datarefs = BB_VINFO_DATAREFS (bb_vinfo);
  2172. if (datarefs.is_empty ())
  2173. return true;
  2174. /* Sort the array of datarefs to make building the interleaving chains
  2175. linear. Don't modify the original vector's order, it is needed for
  2176. determining what dependencies are reversed. */
  2177. vec<data_reference_p> datarefs_copy = datarefs.copy ();
  2178. datarefs_copy.qsort (dr_group_sort_cmp);
  2179. /* Build the interleaving chains. */
  2180. for (i = 0; i < datarefs_copy.length () - 1;)
  2181. {
  2182. data_reference_p dra = datarefs_copy[i];
  2183. stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
  2184. stmt_vec_info lastinfo = NULL;
  2185. for (i = i + 1; i < datarefs_copy.length (); ++i)
  2186. {
  2187. data_reference_p drb = datarefs_copy[i];
  2188. stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
  2189. /* ??? Imperfect sorting (non-compatible types, non-modulo
  2190. accesses, same accesses) can lead to a group to be artificially
  2191. split here as we don't just skip over those. If it really
  2192. matters we can push those to a worklist and re-iterate
  2193. over them. The we can just skip ahead to the next DR here. */
  2194. /* Check that the data-refs have same first location (except init)
  2195. and they are both either store or load (not load and store,
  2196. not masked loads or stores). */
  2197. if (DR_IS_READ (dra) != DR_IS_READ (drb)
  2198. || !operand_equal_p (DR_BASE_ADDRESS (dra),
  2199. DR_BASE_ADDRESS (drb), 0)
  2200. || !dr_equal_offsets_p (dra, drb)
  2201. || !gimple_assign_single_p (DR_STMT (dra))
  2202. || !gimple_assign_single_p (DR_STMT (drb)))
  2203. break;
  2204. /* Check that the data-refs have the same constant size and step. */
  2205. tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
  2206. tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
  2207. if (!tree_fits_uhwi_p (sza)
  2208. || !tree_fits_uhwi_p (szb)
  2209. || !tree_int_cst_equal (sza, szb)
  2210. || !tree_fits_shwi_p (DR_STEP (dra))
  2211. || !tree_fits_shwi_p (DR_STEP (drb))
  2212. || !tree_int_cst_equal (DR_STEP (dra), DR_STEP (drb)))
  2213. break;
  2214. /* Do not place the same access in the interleaving chain twice. */
  2215. if (tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) == 0)
  2216. break;
  2217. /* Check the types are compatible.
  2218. ??? We don't distinguish this during sorting. */
  2219. if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
  2220. TREE_TYPE (DR_REF (drb))))
  2221. break;
  2222. /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
  2223. HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
  2224. HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
  2225. gcc_assert (init_a < init_b);
  2226. /* If init_b == init_a + the size of the type * k, we have an
  2227. interleaving, and DRA is accessed before DRB. */
  2228. HOST_WIDE_INT type_size_a = tree_to_uhwi (sza);
  2229. if ((init_b - init_a) % type_size_a != 0)
  2230. break;
  2231. /* The step (if not zero) is greater than the difference between
  2232. data-refs' inits. This splits groups into suitable sizes. */
  2233. HOST_WIDE_INT step = tree_to_shwi (DR_STEP (dra));
  2234. if (step != 0 && step <= (init_b - init_a))
  2235. break;
  2236. if (dump_enabled_p ())
  2237. {
  2238. dump_printf_loc (MSG_NOTE, vect_location,
  2239. "Detected interleaving ");
  2240. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
  2241. dump_printf (MSG_NOTE, " and ");
  2242. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
  2243. dump_printf (MSG_NOTE, "\n");
  2244. }
  2245. /* Link the found element into the group list. */
  2246. if (!GROUP_FIRST_ELEMENT (stmtinfo_a))
  2247. {
  2248. GROUP_FIRST_ELEMENT (stmtinfo_a) = DR_STMT (dra);
  2249. lastinfo = stmtinfo_a;
  2250. }
  2251. GROUP_FIRST_ELEMENT (stmtinfo_b) = DR_STMT (dra);
  2252. GROUP_NEXT_ELEMENT (lastinfo) = DR_STMT (drb);
  2253. lastinfo = stmtinfo_b;
  2254. }
  2255. }
  2256. FOR_EACH_VEC_ELT (datarefs_copy, i, dr)
  2257. if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
  2258. && !vect_analyze_data_ref_access (dr))
  2259. {
  2260. if (dump_enabled_p ())
  2261. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2262. "not vectorized: complicated access pattern.\n");
  2263. if (bb_vinfo)
  2264. {
  2265. /* Mark the statement as not vectorizable. */
  2266. STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
  2267. continue;
  2268. }
  2269. else
  2270. {
  2271. datarefs_copy.release ();
  2272. return false;
  2273. }
  2274. }
  2275. datarefs_copy.release ();
  2276. return true;
  2277. }
  2278. /* Operator == between two dr_with_seg_len objects.
  2279. This equality operator is used to make sure two data refs
  2280. are the same one so that we will consider to combine the
  2281. aliasing checks of those two pairs of data dependent data
  2282. refs. */
  2283. static bool
  2284. operator == (const dr_with_seg_len& d1,
  2285. const dr_with_seg_len& d2)
  2286. {
  2287. return operand_equal_p (DR_BASE_ADDRESS (d1.dr),
  2288. DR_BASE_ADDRESS (d2.dr), 0)
  2289. && compare_tree (d1.offset, d2.offset) == 0
  2290. && compare_tree (d1.seg_len, d2.seg_len) == 0;
  2291. }
  2292. /* Function comp_dr_with_seg_len_pair.
  2293. Comparison function for sorting objects of dr_with_seg_len_pair_t
  2294. so that we can combine aliasing checks in one scan. */
  2295. static int
  2296. comp_dr_with_seg_len_pair (const void *p1_, const void *p2_)
  2297. {
  2298. const dr_with_seg_len_pair_t* p1 = (const dr_with_seg_len_pair_t *) p1_;
  2299. const dr_with_seg_len_pair_t* p2 = (const dr_with_seg_len_pair_t *) p2_;
  2300. const dr_with_seg_len &p11 = p1->first,
  2301. &p12 = p1->second,
  2302. &p21 = p2->first,
  2303. &p22 = p2->second;
  2304. /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
  2305. if a and c have the same basic address snd step, and b and d have the same
  2306. address and step. Therefore, if any a&c or b&d don't have the same address
  2307. and step, we don't care the order of those two pairs after sorting. */
  2308. int comp_res;
  2309. if ((comp_res = compare_tree (DR_BASE_ADDRESS (p11.dr),
  2310. DR_BASE_ADDRESS (p21.dr))) != 0)
  2311. return comp_res;
  2312. if ((comp_res = compare_tree (DR_BASE_ADDRESS (p12.dr),
  2313. DR_BASE_ADDRESS (p22.dr))) != 0)
  2314. return comp_res;
  2315. if ((comp_res = compare_tree (DR_STEP (p11.dr), DR_STEP (p21.dr))) != 0)
  2316. return comp_res;
  2317. if ((comp_res = compare_tree (DR_STEP (p12.dr), DR_STEP (p22.dr))) != 0)
  2318. return comp_res;
  2319. if ((comp_res = compare_tree (p11.offset, p21.offset)) != 0)
  2320. return comp_res;
  2321. if ((comp_res = compare_tree (p12.offset, p22.offset)) != 0)
  2322. return comp_res;
  2323. return 0;
  2324. }
  2325. /* Function vect_vfa_segment_size.
  2326. Create an expression that computes the size of segment
  2327. that will be accessed for a data reference. The functions takes into
  2328. account that realignment loads may access one more vector.
  2329. Input:
  2330. DR: The data reference.
  2331. LENGTH_FACTOR: segment length to consider.
  2332. Return an expression whose value is the size of segment which will be
  2333. accessed by DR. */
  2334. static tree
  2335. vect_vfa_segment_size (struct data_reference *dr, tree length_factor)
  2336. {
  2337. tree segment_length;
  2338. if (integer_zerop (DR_STEP (dr)))
  2339. segment_length = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
  2340. else
  2341. segment_length = size_binop (MULT_EXPR,
  2342. fold_convert (sizetype, DR_STEP (dr)),
  2343. fold_convert (sizetype, length_factor));
  2344. if (vect_supportable_dr_alignment (dr, false)
  2345. == dr_explicit_realign_optimized)
  2346. {
  2347. tree vector_size = TYPE_SIZE_UNIT
  2348. (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr))));
  2349. segment_length = size_binop (PLUS_EXPR, segment_length, vector_size);
  2350. }
  2351. return segment_length;
  2352. }
  2353. /* Function vect_prune_runtime_alias_test_list.
  2354. Prune a list of ddrs to be tested at run-time by versioning for alias.
  2355. Merge several alias checks into one if possible.
  2356. Return FALSE if resulting list of ddrs is longer then allowed by
  2357. PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
  2358. bool
  2359. vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
  2360. {
  2361. vec<ddr_p> may_alias_ddrs =
  2362. LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
  2363. vec<dr_with_seg_len_pair_t>& comp_alias_ddrs =
  2364. LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo);
  2365. int vect_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
  2366. tree scalar_loop_iters = LOOP_VINFO_NITERS (loop_vinfo);
  2367. ddr_p ddr;
  2368. unsigned int i;
  2369. tree length_factor;
  2370. if (dump_enabled_p ())
  2371. dump_printf_loc (MSG_NOTE, vect_location,
  2372. "=== vect_prune_runtime_alias_test_list ===\n");
  2373. if (may_alias_ddrs.is_empty ())
  2374. return true;
  2375. /* Basically, for each pair of dependent data refs store_ptr_0
  2376. and load_ptr_0, we create an expression:
  2377. ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
  2378. || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
  2379. for aliasing checks. However, in some cases we can decrease
  2380. the number of checks by combining two checks into one. For
  2381. example, suppose we have another pair of data refs store_ptr_0
  2382. and load_ptr_1, and if the following condition is satisfied:
  2383. load_ptr_0 < load_ptr_1 &&
  2384. load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
  2385. (this condition means, in each iteration of vectorized loop,
  2386. the accessed memory of store_ptr_0 cannot be between the memory
  2387. of load_ptr_0 and load_ptr_1.)
  2388. we then can use only the following expression to finish the
  2389. alising checks between store_ptr_0 & load_ptr_0 and
  2390. store_ptr_0 & load_ptr_1:
  2391. ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
  2392. || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
  2393. Note that we only consider that load_ptr_0 and load_ptr_1 have the
  2394. same basic address. */
  2395. comp_alias_ddrs.create (may_alias_ddrs.length ());
  2396. /* First, we collect all data ref pairs for aliasing checks. */
  2397. FOR_EACH_VEC_ELT (may_alias_ddrs, i, ddr)
  2398. {
  2399. struct data_reference *dr_a, *dr_b;
  2400. gimple dr_group_first_a, dr_group_first_b;
  2401. tree segment_length_a, segment_length_b;
  2402. gimple stmt_a, stmt_b;
  2403. dr_a = DDR_A (ddr);
  2404. stmt_a = DR_STMT (DDR_A (ddr));
  2405. dr_group_first_a = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a));
  2406. if (dr_group_first_a)
  2407. {
  2408. stmt_a = dr_group_first_a;
  2409. dr_a = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a));
  2410. }
  2411. dr_b = DDR_B (ddr);
  2412. stmt_b = DR_STMT (DDR_B (ddr));
  2413. dr_group_first_b = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b));
  2414. if (dr_group_first_b)
  2415. {
  2416. stmt_b = dr_group_first_b;
  2417. dr_b = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b));
  2418. }
  2419. if (!operand_equal_p (DR_STEP (dr_a), DR_STEP (dr_b), 0))
  2420. length_factor = scalar_loop_iters;
  2421. else
  2422. length_factor = size_int (vect_factor);
  2423. segment_length_a = vect_vfa_segment_size (dr_a, length_factor);
  2424. segment_length_b = vect_vfa_segment_size (dr_b, length_factor);
  2425. dr_with_seg_len_pair_t dr_with_seg_len_pair
  2426. (dr_with_seg_len (dr_a, segment_length_a),
  2427. dr_with_seg_len (dr_b, segment_length_b));
  2428. if (compare_tree (DR_BASE_ADDRESS (dr_a), DR_BASE_ADDRESS (dr_b)) > 0)
  2429. std::swap (dr_with_seg_len_pair.first, dr_with_seg_len_pair.second);
  2430. comp_alias_ddrs.safe_push (dr_with_seg_len_pair);
  2431. }
  2432. /* Second, we sort the collected data ref pairs so that we can scan
  2433. them once to combine all possible aliasing checks. */
  2434. comp_alias_ddrs.qsort (comp_dr_with_seg_len_pair);
  2435. /* Third, we scan the sorted dr pairs and check if we can combine
  2436. alias checks of two neighbouring dr pairs. */
  2437. for (size_t i = 1; i < comp_alias_ddrs.length (); ++i)
  2438. {
  2439. /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
  2440. dr_with_seg_len *dr_a1 = &comp_alias_ddrs[i-1].first,
  2441. *dr_b1 = &comp_alias_ddrs[i-1].second,
  2442. *dr_a2 = &comp_alias_ddrs[i].first,
  2443. *dr_b2 = &comp_alias_ddrs[i].second;
  2444. /* Remove duplicate data ref pairs. */
  2445. if (*dr_a1 == *dr_a2 && *dr_b1 == *dr_b2)
  2446. {
  2447. if (dump_enabled_p ())
  2448. {
  2449. dump_printf_loc (MSG_NOTE, vect_location,
  2450. "found equal ranges ");
  2451. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  2452. DR_REF (dr_a1->dr));
  2453. dump_printf (MSG_NOTE, ", ");
  2454. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  2455. DR_REF (dr_b1->dr));
  2456. dump_printf (MSG_NOTE, " and ");
  2457. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  2458. DR_REF (dr_a2->dr));
  2459. dump_printf (MSG_NOTE, ", ");
  2460. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  2461. DR_REF (dr_b2->dr));
  2462. dump_printf (MSG_NOTE, "\n");
  2463. }
  2464. comp_alias_ddrs.ordered_remove (i--);
  2465. continue;
  2466. }
  2467. if (*dr_a1 == *dr_a2 || *dr_b1 == *dr_b2)
  2468. {
  2469. /* We consider the case that DR_B1 and DR_B2 are same memrefs,
  2470. and DR_A1 and DR_A2 are two consecutive memrefs. */
  2471. if (*dr_a1 == *dr_a2)
  2472. {
  2473. std::swap (dr_a1, dr_b1);
  2474. std::swap (dr_a2, dr_b2);
  2475. }
  2476. if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1->dr),
  2477. DR_BASE_ADDRESS (dr_a2->dr),
  2478. 0)
  2479. || !tree_fits_shwi_p (dr_a1->offset)
  2480. || !tree_fits_shwi_p (dr_a2->offset))
  2481. continue;
  2482. HOST_WIDE_INT diff = (tree_to_shwi (dr_a2->offset)
  2483. - tree_to_shwi (dr_a1->offset));
  2484. /* Now we check if the following condition is satisfied:
  2485. DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
  2486. where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
  2487. SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
  2488. have to make a best estimation. We can get the minimum value
  2489. of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
  2490. then either of the following two conditions can guarantee the
  2491. one above:
  2492. 1: DIFF <= MIN_SEG_LEN_B
  2493. 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
  2494. */
  2495. HOST_WIDE_INT min_seg_len_b = (tree_fits_shwi_p (dr_b1->seg_len)
  2496. ? tree_to_shwi (dr_b1->seg_len)
  2497. : vect_factor);
  2498. if (diff <= min_seg_len_b
  2499. || (tree_fits_shwi_p (dr_a1->seg_len)
  2500. && diff - tree_to_shwi (dr_a1->seg_len) < min_seg_len_b))
  2501. {
  2502. if (dump_enabled_p ())
  2503. {
  2504. dump_printf_loc (MSG_NOTE, vect_location,
  2505. "merging ranges for ");
  2506. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  2507. DR_REF (dr_a1->dr));
  2508. dump_printf (MSG_NOTE, ", ");
  2509. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  2510. DR_REF (dr_b1->dr));
  2511. dump_printf (MSG_NOTE, " and ");
  2512. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  2513. DR_REF (dr_a2->dr));
  2514. dump_printf (MSG_NOTE, ", ");
  2515. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  2516. DR_REF (dr_b2->dr));
  2517. dump_printf (MSG_NOTE, "\n");
  2518. }
  2519. dr_a1->seg_len = size_binop (PLUS_EXPR,
  2520. dr_a2->seg_len, size_int (diff));
  2521. comp_alias_ddrs.ordered_remove (i--);
  2522. }
  2523. }
  2524. }
  2525. dump_printf_loc (MSG_NOTE, vect_location,
  2526. "improved number of alias checks from %d to %d\n",
  2527. may_alias_ddrs.length (), comp_alias_ddrs.length ());
  2528. if ((int) comp_alias_ddrs.length () >
  2529. PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
  2530. return false;
  2531. return true;
  2532. }
  2533. /* Check whether a non-affine read in stmt is suitable for gather load
  2534. and if so, return a builtin decl for that operation. */
  2535. tree
  2536. vect_check_gather (gimple stmt, loop_vec_info loop_vinfo, tree *basep,
  2537. tree *offp, int *scalep)
  2538. {
  2539. HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
  2540. struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
  2541. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  2542. struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
  2543. tree offtype = NULL_TREE;
  2544. tree decl, base, off;
  2545. machine_mode pmode;
  2546. int punsignedp, pvolatilep;
  2547. base = DR_REF (dr);
  2548. /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
  2549. see if we can use the def stmt of the address. */
  2550. if (is_gimple_call (stmt)
  2551. && gimple_call_internal_p (stmt)
  2552. && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
  2553. || gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
  2554. && TREE_CODE (base) == MEM_REF
  2555. && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
  2556. && integer_zerop (TREE_OPERAND (base, 1))
  2557. && !expr_invariant_in_loop_p (loop, TREE_OPERAND (base, 0)))
  2558. {
  2559. gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
  2560. if (is_gimple_assign (def_stmt)
  2561. && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
  2562. base = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
  2563. }
  2564. /* The gather builtins need address of the form
  2565. loop_invariant + vector * {1, 2, 4, 8}
  2566. or
  2567. loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
  2568. Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
  2569. of loop invariants/SSA_NAMEs defined in the loop, with casts,
  2570. multiplications and additions in it. To get a vector, we need
  2571. a single SSA_NAME that will be defined in the loop and will
  2572. contain everything that is not loop invariant and that can be
  2573. vectorized. The following code attempts to find such a preexistng
  2574. SSA_NAME OFF and put the loop invariants into a tree BASE
  2575. that can be gimplified before the loop. */
  2576. base = get_inner_reference (base, &pbitsize, &pbitpos, &off,
  2577. &pmode, &punsignedp, &pvolatilep, false);
  2578. gcc_assert (base != NULL_TREE && (pbitpos % BITS_PER_UNIT) == 0);
  2579. if (TREE_CODE (base) == MEM_REF)
  2580. {
  2581. if (!integer_zerop (TREE_OPERAND (base, 1)))
  2582. {
  2583. if (off == NULL_TREE)
  2584. {
  2585. offset_int moff = mem_ref_offset (base);
  2586. off = wide_int_to_tree (sizetype, moff);
  2587. }
  2588. else
  2589. off = size_binop (PLUS_EXPR, off,
  2590. fold_convert (sizetype, TREE_OPERAND (base, 1)));
  2591. }
  2592. base = TREE_OPERAND (base, 0);
  2593. }
  2594. else
  2595. base = build_fold_addr_expr (base);
  2596. if (off == NULL_TREE)
  2597. off = size_zero_node;
  2598. /* If base is not loop invariant, either off is 0, then we start with just
  2599. the constant offset in the loop invariant BASE and continue with base
  2600. as OFF, otherwise give up.
  2601. We could handle that case by gimplifying the addition of base + off
  2602. into some SSA_NAME and use that as off, but for now punt. */
  2603. if (!expr_invariant_in_loop_p (loop, base))
  2604. {
  2605. if (!integer_zerop (off))
  2606. return NULL_TREE;
  2607. off = base;
  2608. base = size_int (pbitpos / BITS_PER_UNIT);
  2609. }
  2610. /* Otherwise put base + constant offset into the loop invariant BASE
  2611. and continue with OFF. */
  2612. else
  2613. {
  2614. base = fold_convert (sizetype, base);
  2615. base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
  2616. }
  2617. /* OFF at this point may be either a SSA_NAME or some tree expression
  2618. from get_inner_reference. Try to peel off loop invariants from it
  2619. into BASE as long as possible. */
  2620. STRIP_NOPS (off);
  2621. while (offtype == NULL_TREE)
  2622. {
  2623. enum tree_code code;
  2624. tree op0, op1, add = NULL_TREE;
  2625. if (TREE_CODE (off) == SSA_NAME)
  2626. {
  2627. gimple def_stmt = SSA_NAME_DEF_STMT (off);
  2628. if (expr_invariant_in_loop_p (loop, off))
  2629. return NULL_TREE;
  2630. if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
  2631. break;
  2632. op0 = gimple_assign_rhs1 (def_stmt);
  2633. code = gimple_assign_rhs_code (def_stmt);
  2634. op1 = gimple_assign_rhs2 (def_stmt);
  2635. }
  2636. else
  2637. {
  2638. if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
  2639. return NULL_TREE;
  2640. code = TREE_CODE (off);
  2641. extract_ops_from_tree (off, &code, &op0, &op1);
  2642. }
  2643. switch (code)
  2644. {
  2645. case POINTER_PLUS_EXPR:
  2646. case PLUS_EXPR:
  2647. if (expr_invariant_in_loop_p (loop, op0))
  2648. {
  2649. add = op0;
  2650. off = op1;
  2651. do_add:
  2652. add = fold_convert (sizetype, add);
  2653. if (scale != 1)
  2654. add = size_binop (MULT_EXPR, add, size_int (scale));
  2655. base = size_binop (PLUS_EXPR, base, add);
  2656. continue;
  2657. }
  2658. if (expr_invariant_in_loop_p (loop, op1))
  2659. {
  2660. add = op1;
  2661. off = op0;
  2662. goto do_add;
  2663. }
  2664. break;
  2665. case MINUS_EXPR:
  2666. if (expr_invariant_in_loop_p (loop, op1))
  2667. {
  2668. add = fold_convert (sizetype, op1);
  2669. add = size_binop (MINUS_EXPR, size_zero_node, add);
  2670. off = op0;
  2671. goto do_add;
  2672. }
  2673. break;
  2674. case MULT_EXPR:
  2675. if (scale == 1 && tree_fits_shwi_p (op1))
  2676. {
  2677. scale = tree_to_shwi (op1);
  2678. off = op0;
  2679. continue;
  2680. }
  2681. break;
  2682. case SSA_NAME:
  2683. off = op0;
  2684. continue;
  2685. CASE_CONVERT:
  2686. if (!POINTER_TYPE_P (TREE_TYPE (op0))
  2687. && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
  2688. break;
  2689. if (TYPE_PRECISION (TREE_TYPE (op0))
  2690. == TYPE_PRECISION (TREE_TYPE (off)))
  2691. {
  2692. off = op0;
  2693. continue;
  2694. }
  2695. if (TYPE_PRECISION (TREE_TYPE (op0))
  2696. < TYPE_PRECISION (TREE_TYPE (off)))
  2697. {
  2698. off = op0;
  2699. offtype = TREE_TYPE (off);
  2700. STRIP_NOPS (off);
  2701. continue;
  2702. }
  2703. break;
  2704. default:
  2705. break;
  2706. }
  2707. break;
  2708. }
  2709. /* If at the end OFF still isn't a SSA_NAME or isn't
  2710. defined in the loop, punt. */
  2711. if (TREE_CODE (off) != SSA_NAME
  2712. || expr_invariant_in_loop_p (loop, off))
  2713. return NULL_TREE;
  2714. if (offtype == NULL_TREE)
  2715. offtype = TREE_TYPE (off);
  2716. decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
  2717. offtype, scale);
  2718. if (decl == NULL_TREE)
  2719. return NULL_TREE;
  2720. if (basep)
  2721. *basep = base;
  2722. if (offp)
  2723. *offp = off;
  2724. if (scalep)
  2725. *scalep = scale;
  2726. return decl;
  2727. }
  2728. /* Function vect_analyze_data_refs.
  2729. Find all the data references in the loop or basic block.
  2730. The general structure of the analysis of data refs in the vectorizer is as
  2731. follows:
  2732. 1- vect_analyze_data_refs(loop/bb): call
  2733. compute_data_dependences_for_loop/bb to find and analyze all data-refs
  2734. in the loop/bb and their dependences.
  2735. 2- vect_analyze_dependences(): apply dependence testing using ddrs.
  2736. 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
  2737. 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
  2738. */
  2739. bool
  2740. vect_analyze_data_refs (loop_vec_info loop_vinfo,
  2741. bb_vec_info bb_vinfo,
  2742. int *min_vf, unsigned *n_stmts)
  2743. {
  2744. struct loop *loop = NULL;
  2745. basic_block bb = NULL;
  2746. unsigned int i;
  2747. vec<data_reference_p> datarefs;
  2748. struct data_reference *dr;
  2749. tree scalar_type;
  2750. if (dump_enabled_p ())
  2751. dump_printf_loc (MSG_NOTE, vect_location,
  2752. "=== vect_analyze_data_refs ===\n");
  2753. if (loop_vinfo)
  2754. {
  2755. basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
  2756. loop = LOOP_VINFO_LOOP (loop_vinfo);
  2757. datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
  2758. if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo)))
  2759. {
  2760. if (dump_enabled_p ())
  2761. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2762. "not vectorized: loop contains function calls"
  2763. " or data references that cannot be analyzed\n");
  2764. return false;
  2765. }
  2766. for (i = 0; i < loop->num_nodes; i++)
  2767. {
  2768. gimple_stmt_iterator gsi;
  2769. for (gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi); gsi_next (&gsi))
  2770. {
  2771. gimple stmt = gsi_stmt (gsi);
  2772. if (is_gimple_debug (stmt))
  2773. continue;
  2774. ++*n_stmts;
  2775. if (!find_data_references_in_stmt (loop, stmt, &datarefs))
  2776. {
  2777. if (is_gimple_call (stmt) && loop->safelen)
  2778. {
  2779. tree fndecl = gimple_call_fndecl (stmt), op;
  2780. if (fndecl != NULL_TREE)
  2781. {
  2782. struct cgraph_node *node = cgraph_node::get (fndecl);
  2783. if (node != NULL && node->simd_clones != NULL)
  2784. {
  2785. unsigned int j, n = gimple_call_num_args (stmt);
  2786. for (j = 0; j < n; j++)
  2787. {
  2788. op = gimple_call_arg (stmt, j);
  2789. if (DECL_P (op)
  2790. || (REFERENCE_CLASS_P (op)
  2791. && get_base_address (op)))
  2792. break;
  2793. }
  2794. op = gimple_call_lhs (stmt);
  2795. /* Ignore #pragma omp declare simd functions
  2796. if they don't have data references in the
  2797. call stmt itself. */
  2798. if (j == n
  2799. && !(op
  2800. && (DECL_P (op)
  2801. || (REFERENCE_CLASS_P (op)
  2802. && get_base_address (op)))))
  2803. continue;
  2804. }
  2805. }
  2806. }
  2807. LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
  2808. if (dump_enabled_p ())
  2809. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2810. "not vectorized: loop contains function "
  2811. "calls or data references that cannot "
  2812. "be analyzed\n");
  2813. return false;
  2814. }
  2815. }
  2816. }
  2817. LOOP_VINFO_DATAREFS (loop_vinfo) = datarefs;
  2818. }
  2819. else
  2820. {
  2821. gimple_stmt_iterator gsi;
  2822. bb = BB_VINFO_BB (bb_vinfo);
  2823. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  2824. {
  2825. gimple stmt = gsi_stmt (gsi);
  2826. if (is_gimple_debug (stmt))
  2827. continue;
  2828. ++*n_stmts;
  2829. if (!find_data_references_in_stmt (NULL, stmt,
  2830. &BB_VINFO_DATAREFS (bb_vinfo)))
  2831. {
  2832. /* Mark the rest of the basic-block as unvectorizable. */
  2833. for (; !gsi_end_p (gsi); gsi_next (&gsi))
  2834. {
  2835. stmt = gsi_stmt (gsi);
  2836. STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)) = false;
  2837. }
  2838. break;
  2839. }
  2840. }
  2841. datarefs = BB_VINFO_DATAREFS (bb_vinfo);
  2842. }
  2843. /* Go through the data-refs, check that the analysis succeeded. Update
  2844. pointer from stmt_vec_info struct to DR and vectype. */
  2845. FOR_EACH_VEC_ELT (datarefs, i, dr)
  2846. {
  2847. gimple stmt;
  2848. stmt_vec_info stmt_info;
  2849. tree base, offset, init;
  2850. bool gather = false;
  2851. bool simd_lane_access = false;
  2852. int vf;
  2853. again:
  2854. if (!dr || !DR_REF (dr))
  2855. {
  2856. if (dump_enabled_p ())
  2857. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2858. "not vectorized: unhandled data-ref\n");
  2859. return false;
  2860. }
  2861. stmt = DR_STMT (dr);
  2862. stmt_info = vinfo_for_stmt (stmt);
  2863. /* Discard clobbers from the dataref vector. We will remove
  2864. clobber stmts during vectorization. */
  2865. if (gimple_clobber_p (stmt))
  2866. {
  2867. free_data_ref (dr);
  2868. if (i == datarefs.length () - 1)
  2869. {
  2870. datarefs.pop ();
  2871. break;
  2872. }
  2873. datarefs.ordered_remove (i);
  2874. dr = datarefs[i];
  2875. goto again;
  2876. }
  2877. /* Check that analysis of the data-ref succeeded. */
  2878. if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
  2879. || !DR_STEP (dr))
  2880. {
  2881. bool maybe_gather
  2882. = DR_IS_READ (dr)
  2883. && !TREE_THIS_VOLATILE (DR_REF (dr))
  2884. && targetm.vectorize.builtin_gather != NULL;
  2885. bool maybe_simd_lane_access
  2886. = loop_vinfo && loop->simduid;
  2887. /* If target supports vector gather loads, or if this might be
  2888. a SIMD lane access, see if they can't be used. */
  2889. if (loop_vinfo
  2890. && (maybe_gather || maybe_simd_lane_access)
  2891. && !nested_in_vect_loop_p (loop, stmt))
  2892. {
  2893. struct data_reference *newdr
  2894. = create_data_ref (NULL, loop_containing_stmt (stmt),
  2895. DR_REF (dr), stmt, true);
  2896. gcc_assert (newdr != NULL && DR_REF (newdr));
  2897. if (DR_BASE_ADDRESS (newdr)
  2898. && DR_OFFSET (newdr)
  2899. && DR_INIT (newdr)
  2900. && DR_STEP (newdr)
  2901. && integer_zerop (DR_STEP (newdr)))
  2902. {
  2903. if (maybe_simd_lane_access)
  2904. {
  2905. tree off = DR_OFFSET (newdr);
  2906. STRIP_NOPS (off);
  2907. if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
  2908. && TREE_CODE (off) == MULT_EXPR
  2909. && tree_fits_uhwi_p (TREE_OPERAND (off, 1)))
  2910. {
  2911. tree step = TREE_OPERAND (off, 1);
  2912. off = TREE_OPERAND (off, 0);
  2913. STRIP_NOPS (off);
  2914. if (CONVERT_EXPR_P (off)
  2915. && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
  2916. 0)))
  2917. < TYPE_PRECISION (TREE_TYPE (off)))
  2918. off = TREE_OPERAND (off, 0);
  2919. if (TREE_CODE (off) == SSA_NAME)
  2920. {
  2921. gimple def = SSA_NAME_DEF_STMT (off);
  2922. tree reft = TREE_TYPE (DR_REF (newdr));
  2923. if (is_gimple_call (def)
  2924. && gimple_call_internal_p (def)
  2925. && (gimple_call_internal_fn (def)
  2926. == IFN_GOMP_SIMD_LANE))
  2927. {
  2928. tree arg = gimple_call_arg (def, 0);
  2929. gcc_assert (TREE_CODE (arg) == SSA_NAME);
  2930. arg = SSA_NAME_VAR (arg);
  2931. if (arg == loop->simduid
  2932. /* For now. */
  2933. && tree_int_cst_equal
  2934. (TYPE_SIZE_UNIT (reft),
  2935. step))
  2936. {
  2937. DR_OFFSET (newdr) = ssize_int (0);
  2938. DR_STEP (newdr) = step;
  2939. DR_ALIGNED_TO (newdr)
  2940. = size_int (BIGGEST_ALIGNMENT);
  2941. dr = newdr;
  2942. simd_lane_access = true;
  2943. }
  2944. }
  2945. }
  2946. }
  2947. }
  2948. if (!simd_lane_access && maybe_gather)
  2949. {
  2950. dr = newdr;
  2951. gather = true;
  2952. }
  2953. }
  2954. if (!gather && !simd_lane_access)
  2955. free_data_ref (newdr);
  2956. }
  2957. if (!gather && !simd_lane_access)
  2958. {
  2959. if (dump_enabled_p ())
  2960. {
  2961. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2962. "not vectorized: data ref analysis "
  2963. "failed ");
  2964. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  2965. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  2966. }
  2967. if (bb_vinfo)
  2968. break;
  2969. return false;
  2970. }
  2971. }
  2972. if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
  2973. {
  2974. if (dump_enabled_p ())
  2975. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2976. "not vectorized: base addr of dr is a "
  2977. "constant\n");
  2978. if (bb_vinfo)
  2979. break;
  2980. if (gather || simd_lane_access)
  2981. free_data_ref (dr);
  2982. return false;
  2983. }
  2984. if (TREE_THIS_VOLATILE (DR_REF (dr)))
  2985. {
  2986. if (dump_enabled_p ())
  2987. {
  2988. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2989. "not vectorized: volatile type ");
  2990. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  2991. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  2992. }
  2993. if (bb_vinfo)
  2994. break;
  2995. return false;
  2996. }
  2997. if (stmt_can_throw_internal (stmt))
  2998. {
  2999. if (dump_enabled_p ())
  3000. {
  3001. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3002. "not vectorized: statement can throw an "
  3003. "exception ");
  3004. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  3005. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  3006. }
  3007. if (bb_vinfo)
  3008. break;
  3009. if (gather || simd_lane_access)
  3010. free_data_ref (dr);
  3011. return false;
  3012. }
  3013. if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
  3014. && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
  3015. {
  3016. if (dump_enabled_p ())
  3017. {
  3018. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3019. "not vectorized: statement is bitfield "
  3020. "access ");
  3021. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  3022. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  3023. }
  3024. if (bb_vinfo)
  3025. break;
  3026. if (gather || simd_lane_access)
  3027. free_data_ref (dr);
  3028. return false;
  3029. }
  3030. base = unshare_expr (DR_BASE_ADDRESS (dr));
  3031. offset = unshare_expr (DR_OFFSET (dr));
  3032. init = unshare_expr (DR_INIT (dr));
  3033. if (is_gimple_call (stmt)
  3034. && (!gimple_call_internal_p (stmt)
  3035. || (gimple_call_internal_fn (stmt) != IFN_MASK_LOAD
  3036. && gimple_call_internal_fn (stmt) != IFN_MASK_STORE)))
  3037. {
  3038. if (dump_enabled_p ())
  3039. {
  3040. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3041. "not vectorized: dr in a call ");
  3042. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  3043. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  3044. }
  3045. if (bb_vinfo)
  3046. break;
  3047. if (gather || simd_lane_access)
  3048. free_data_ref (dr);
  3049. return false;
  3050. }
  3051. /* Update DR field in stmt_vec_info struct. */
  3052. /* If the dataref is in an inner-loop of the loop that is considered for
  3053. for vectorization, we also want to analyze the access relative to
  3054. the outer-loop (DR contains information only relative to the
  3055. inner-most enclosing loop). We do that by building a reference to the
  3056. first location accessed by the inner-loop, and analyze it relative to
  3057. the outer-loop. */
  3058. if (loop && nested_in_vect_loop_p (loop, stmt))
  3059. {
  3060. tree outer_step, outer_base, outer_init;
  3061. HOST_WIDE_INT pbitsize, pbitpos;
  3062. tree poffset;
  3063. machine_mode pmode;
  3064. int punsignedp, pvolatilep;
  3065. affine_iv base_iv, offset_iv;
  3066. tree dinit;
  3067. /* Build a reference to the first location accessed by the
  3068. inner-loop: *(BASE+INIT). (The first location is actually
  3069. BASE+INIT+OFFSET, but we add OFFSET separately later). */
  3070. tree inner_base = build_fold_indirect_ref
  3071. (fold_build_pointer_plus (base, init));
  3072. if (dump_enabled_p ())
  3073. {
  3074. dump_printf_loc (MSG_NOTE, vect_location,
  3075. "analyze in outer-loop: ");
  3076. dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
  3077. dump_printf (MSG_NOTE, "\n");
  3078. }
  3079. outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
  3080. &poffset, &pmode, &punsignedp, &pvolatilep, false);
  3081. gcc_assert (outer_base != NULL_TREE);
  3082. if (pbitpos % BITS_PER_UNIT != 0)
  3083. {
  3084. if (dump_enabled_p ())
  3085. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3086. "failed: bit offset alignment.\n");
  3087. return false;
  3088. }
  3089. outer_base = build_fold_addr_expr (outer_base);
  3090. if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
  3091. &base_iv, false))
  3092. {
  3093. if (dump_enabled_p ())
  3094. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3095. "failed: evolution of base is not affine.\n");
  3096. return false;
  3097. }
  3098. if (offset)
  3099. {
  3100. if (poffset)
  3101. poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
  3102. poffset);
  3103. else
  3104. poffset = offset;
  3105. }
  3106. if (!poffset)
  3107. {
  3108. offset_iv.base = ssize_int (0);
  3109. offset_iv.step = ssize_int (0);
  3110. }
  3111. else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
  3112. &offset_iv, false))
  3113. {
  3114. if (dump_enabled_p ())
  3115. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3116. "evolution of offset is not affine.\n");
  3117. return false;
  3118. }
  3119. outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
  3120. split_constant_offset (base_iv.base, &base_iv.base, &dinit);
  3121. outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
  3122. split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
  3123. outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
  3124. outer_step = size_binop (PLUS_EXPR,
  3125. fold_convert (ssizetype, base_iv.step),
  3126. fold_convert (ssizetype, offset_iv.step));
  3127. STMT_VINFO_DR_STEP (stmt_info) = outer_step;
  3128. /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
  3129. STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
  3130. STMT_VINFO_DR_INIT (stmt_info) = outer_init;
  3131. STMT_VINFO_DR_OFFSET (stmt_info) =
  3132. fold_convert (ssizetype, offset_iv.base);
  3133. STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
  3134. size_int (highest_pow2_factor (offset_iv.base));
  3135. if (dump_enabled_p ())
  3136. {
  3137. dump_printf_loc (MSG_NOTE, vect_location,
  3138. "\touter base_address: ");
  3139. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  3140. STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
  3141. dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
  3142. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  3143. STMT_VINFO_DR_OFFSET (stmt_info));
  3144. dump_printf (MSG_NOTE,
  3145. "\n\touter constant offset from base address: ");
  3146. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  3147. STMT_VINFO_DR_INIT (stmt_info));
  3148. dump_printf (MSG_NOTE, "\n\touter step: ");
  3149. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  3150. STMT_VINFO_DR_STEP (stmt_info));
  3151. dump_printf (MSG_NOTE, "\n\touter aligned to: ");
  3152. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  3153. STMT_VINFO_DR_ALIGNED_TO (stmt_info));
  3154. dump_printf (MSG_NOTE, "\n");
  3155. }
  3156. }
  3157. if (STMT_VINFO_DATA_REF (stmt_info))
  3158. {
  3159. if (dump_enabled_p ())
  3160. {
  3161. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3162. "not vectorized: more than one data ref "
  3163. "in stmt: ");
  3164. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  3165. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  3166. }
  3167. if (bb_vinfo)
  3168. break;
  3169. if (gather || simd_lane_access)
  3170. free_data_ref (dr);
  3171. return false;
  3172. }
  3173. STMT_VINFO_DATA_REF (stmt_info) = dr;
  3174. if (simd_lane_access)
  3175. {
  3176. STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
  3177. free_data_ref (datarefs[i]);
  3178. datarefs[i] = dr;
  3179. }
  3180. /* Set vectype for STMT. */
  3181. scalar_type = TREE_TYPE (DR_REF (dr));
  3182. STMT_VINFO_VECTYPE (stmt_info)
  3183. = get_vectype_for_scalar_type (scalar_type);
  3184. if (!STMT_VINFO_VECTYPE (stmt_info))
  3185. {
  3186. if (dump_enabled_p ())
  3187. {
  3188. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3189. "not vectorized: no vectype for stmt: ");
  3190. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  3191. dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
  3192. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
  3193. scalar_type);
  3194. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  3195. }
  3196. if (bb_vinfo)
  3197. break;
  3198. if (gather || simd_lane_access)
  3199. {
  3200. STMT_VINFO_DATA_REF (stmt_info) = NULL;
  3201. if (gather)
  3202. free_data_ref (dr);
  3203. }
  3204. return false;
  3205. }
  3206. else
  3207. {
  3208. if (dump_enabled_p ())
  3209. {
  3210. dump_printf_loc (MSG_NOTE, vect_location,
  3211. "got vectype for stmt: ");
  3212. dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
  3213. dump_generic_expr (MSG_NOTE, TDF_SLIM,
  3214. STMT_VINFO_VECTYPE (stmt_info));
  3215. dump_printf (MSG_NOTE, "\n");
  3216. }
  3217. }
  3218. /* Adjust the minimal vectorization factor according to the
  3219. vector type. */
  3220. vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
  3221. if (vf > *min_vf)
  3222. *min_vf = vf;
  3223. if (gather)
  3224. {
  3225. tree off;
  3226. gather = 0 != vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
  3227. if (gather
  3228. && get_vectype_for_scalar_type (TREE_TYPE (off)) == NULL_TREE)
  3229. gather = false;
  3230. if (!gather)
  3231. {
  3232. STMT_VINFO_DATA_REF (stmt_info) = NULL;
  3233. free_data_ref (dr);
  3234. if (dump_enabled_p ())
  3235. {
  3236. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3237. "not vectorized: not suitable for gather "
  3238. "load ");
  3239. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  3240. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  3241. }
  3242. return false;
  3243. }
  3244. datarefs[i] = dr;
  3245. STMT_VINFO_GATHER_P (stmt_info) = true;
  3246. }
  3247. else if (loop_vinfo
  3248. && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
  3249. {
  3250. if (nested_in_vect_loop_p (loop, stmt)
  3251. || !DR_IS_READ (dr))
  3252. {
  3253. if (dump_enabled_p ())
  3254. {
  3255. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3256. "not vectorized: not suitable for strided "
  3257. "load ");
  3258. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  3259. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  3260. }
  3261. return false;
  3262. }
  3263. STMT_VINFO_STRIDE_LOAD_P (stmt_info) = true;
  3264. }
  3265. }
  3266. /* If we stopped analysis at the first dataref we could not analyze
  3267. when trying to vectorize a basic-block mark the rest of the datarefs
  3268. as not vectorizable and truncate the vector of datarefs. That
  3269. avoids spending useless time in analyzing their dependence. */
  3270. if (i != datarefs.length ())
  3271. {
  3272. gcc_assert (bb_vinfo != NULL);
  3273. for (unsigned j = i; j < datarefs.length (); ++j)
  3274. {
  3275. data_reference_p dr = datarefs[j];
  3276. STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
  3277. free_data_ref (dr);
  3278. }
  3279. datarefs.truncate (i);
  3280. }
  3281. return true;
  3282. }
  3283. /* Function vect_get_new_vect_var.
  3284. Returns a name for a new variable. The current naming scheme appends the
  3285. prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
  3286. the name of vectorizer generated variables, and appends that to NAME if
  3287. provided. */
  3288. tree
  3289. vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
  3290. {
  3291. const char *prefix;
  3292. tree new_vect_var;
  3293. switch (var_kind)
  3294. {
  3295. case vect_simple_var:
  3296. prefix = "vect";
  3297. break;
  3298. case vect_scalar_var:
  3299. prefix = "stmp";
  3300. break;
  3301. case vect_pointer_var:
  3302. prefix = "vectp";
  3303. break;
  3304. default:
  3305. gcc_unreachable ();
  3306. }
  3307. if (name)
  3308. {
  3309. char* tmp = concat (prefix, "_", name, NULL);
  3310. new_vect_var = create_tmp_reg (type, tmp);
  3311. free (tmp);
  3312. }
  3313. else
  3314. new_vect_var = create_tmp_reg (type, prefix);
  3315. return new_vect_var;
  3316. }
  3317. /* Duplicate ptr info and set alignment/misaligment on NAME from DR. */
  3318. static void
  3319. vect_duplicate_ssa_name_ptr_info (tree name, data_reference *dr,
  3320. stmt_vec_info stmt_info)
  3321. {
  3322. duplicate_ssa_name_ptr_info (name, DR_PTR_INFO (dr));
  3323. unsigned int align = TYPE_ALIGN_UNIT (STMT_VINFO_VECTYPE (stmt_info));
  3324. int misalign = DR_MISALIGNMENT (dr);
  3325. if (misalign == -1)
  3326. mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
  3327. else
  3328. set_ptr_info_alignment (SSA_NAME_PTR_INFO (name), align, misalign);
  3329. }
  3330. /* Function vect_create_addr_base_for_vector_ref.
  3331. Create an expression that computes the address of the first memory location
  3332. that will be accessed for a data reference.
  3333. Input:
  3334. STMT: The statement containing the data reference.
  3335. NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
  3336. OFFSET: Optional. If supplied, it is be added to the initial address.
  3337. LOOP: Specify relative to which loop-nest should the address be computed.
  3338. For example, when the dataref is in an inner-loop nested in an
  3339. outer-loop that is now being vectorized, LOOP can be either the
  3340. outer-loop, or the inner-loop. The first memory location accessed
  3341. by the following dataref ('in' points to short):
  3342. for (i=0; i<N; i++)
  3343. for (j=0; j<M; j++)
  3344. s += in[i+j]
  3345. is as follows:
  3346. if LOOP=i_loop: &in (relative to i_loop)
  3347. if LOOP=j_loop: &in+i*2B (relative to j_loop)
  3348. BYTE_OFFSET: Optional, defaulted to NULL. If supplied, it is added to the
  3349. initial address. Unlike OFFSET, which is number of elements to
  3350. be added, BYTE_OFFSET is measured in bytes.
  3351. Output:
  3352. 1. Return an SSA_NAME whose value is the address of the memory location of
  3353. the first vector of the data reference.
  3354. 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
  3355. these statement(s) which define the returned SSA_NAME.
  3356. FORNOW: We are only handling array accesses with step 1. */
  3357. tree
  3358. vect_create_addr_base_for_vector_ref (gimple stmt,
  3359. gimple_seq *new_stmt_list,
  3360. tree offset,
  3361. struct loop *loop,
  3362. tree byte_offset)
  3363. {
  3364. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  3365. struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
  3366. tree data_ref_base;
  3367. const char *base_name;
  3368. tree addr_base;
  3369. tree dest;
  3370. gimple_seq seq = NULL;
  3371. tree base_offset;
  3372. tree init;
  3373. tree vect_ptr_type;
  3374. tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
  3375. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  3376. if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
  3377. {
  3378. struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
  3379. gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
  3380. data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
  3381. base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
  3382. init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
  3383. }
  3384. else
  3385. {
  3386. data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
  3387. base_offset = unshare_expr (DR_OFFSET (dr));
  3388. init = unshare_expr (DR_INIT (dr));
  3389. }
  3390. if (loop_vinfo)
  3391. base_name = get_name (data_ref_base);
  3392. else
  3393. {
  3394. base_offset = ssize_int (0);
  3395. init = ssize_int (0);
  3396. base_name = get_name (DR_REF (dr));
  3397. }
  3398. /* Create base_offset */
  3399. base_offset = size_binop (PLUS_EXPR,
  3400. fold_convert (sizetype, base_offset),
  3401. fold_convert (sizetype, init));
  3402. if (offset)
  3403. {
  3404. offset = fold_build2 (MULT_EXPR, sizetype,
  3405. fold_convert (sizetype, offset), step);
  3406. base_offset = fold_build2 (PLUS_EXPR, sizetype,
  3407. base_offset, offset);
  3408. }
  3409. if (byte_offset)
  3410. {
  3411. byte_offset = fold_convert (sizetype, byte_offset);
  3412. base_offset = fold_build2 (PLUS_EXPR, sizetype,
  3413. base_offset, byte_offset);
  3414. }
  3415. /* base + base_offset */
  3416. if (loop_vinfo)
  3417. addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
  3418. else
  3419. {
  3420. addr_base = build1 (ADDR_EXPR,
  3421. build_pointer_type (TREE_TYPE (DR_REF (dr))),
  3422. unshare_expr (DR_REF (dr)));
  3423. }
  3424. vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
  3425. addr_base = fold_convert (vect_ptr_type, addr_base);
  3426. dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
  3427. addr_base = force_gimple_operand (addr_base, &seq, false, dest);
  3428. gimple_seq_add_seq (new_stmt_list, seq);
  3429. if (DR_PTR_INFO (dr)
  3430. && TREE_CODE (addr_base) == SSA_NAME)
  3431. {
  3432. vect_duplicate_ssa_name_ptr_info (addr_base, dr, stmt_info);
  3433. if (offset || byte_offset)
  3434. mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
  3435. }
  3436. if (dump_enabled_p ())
  3437. {
  3438. dump_printf_loc (MSG_NOTE, vect_location, "created ");
  3439. dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
  3440. dump_printf (MSG_NOTE, "\n");
  3441. }
  3442. return addr_base;
  3443. }
  3444. /* Function vect_create_data_ref_ptr.
  3445. Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
  3446. location accessed in the loop by STMT, along with the def-use update
  3447. chain to appropriately advance the pointer through the loop iterations.
  3448. Also set aliasing information for the pointer. This pointer is used by
  3449. the callers to this function to create a memory reference expression for
  3450. vector load/store access.
  3451. Input:
  3452. 1. STMT: a stmt that references memory. Expected to be of the form
  3453. GIMPLE_ASSIGN <name, data-ref> or
  3454. GIMPLE_ASSIGN <data-ref, name>.
  3455. 2. AGGR_TYPE: the type of the reference, which should be either a vector
  3456. or an array.
  3457. 3. AT_LOOP: the loop where the vector memref is to be created.
  3458. 4. OFFSET (optional): an offset to be added to the initial address accessed
  3459. by the data-ref in STMT.
  3460. 5. BSI: location where the new stmts are to be placed if there is no loop
  3461. 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
  3462. pointing to the initial address.
  3463. 7. BYTE_OFFSET (optional, defaults to NULL): a byte offset to be added
  3464. to the initial address accessed by the data-ref in STMT. This is
  3465. similar to OFFSET, but OFFSET is counted in elements, while BYTE_OFFSET
  3466. in bytes.
  3467. Output:
  3468. 1. Declare a new ptr to vector_type, and have it point to the base of the
  3469. data reference (initial addressed accessed by the data reference).
  3470. For example, for vector of type V8HI, the following code is generated:
  3471. v8hi *ap;
  3472. ap = (v8hi *)initial_address;
  3473. if OFFSET is not supplied:
  3474. initial_address = &a[init];
  3475. if OFFSET is supplied:
  3476. initial_address = &a[init + OFFSET];
  3477. if BYTE_OFFSET is supplied:
  3478. initial_address = &a[init] + BYTE_OFFSET;
  3479. Return the initial_address in INITIAL_ADDRESS.
  3480. 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
  3481. update the pointer in each iteration of the loop.
  3482. Return the increment stmt that updates the pointer in PTR_INCR.
  3483. 3. Set INV_P to true if the access pattern of the data reference in the
  3484. vectorized loop is invariant. Set it to false otherwise.
  3485. 4. Return the pointer. */
  3486. tree
  3487. vect_create_data_ref_ptr (gimple stmt, tree aggr_type, struct loop *at_loop,
  3488. tree offset, tree *initial_address,
  3489. gimple_stmt_iterator *gsi, gimple *ptr_incr,
  3490. bool only_init, bool *inv_p, tree byte_offset)
  3491. {
  3492. const char *base_name;
  3493. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  3494. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  3495. struct loop *loop = NULL;
  3496. bool nested_in_vect_loop = false;
  3497. struct loop *containing_loop = NULL;
  3498. tree aggr_ptr_type;
  3499. tree aggr_ptr;
  3500. tree new_temp;
  3501. gimple vec_stmt;
  3502. gimple_seq new_stmt_list = NULL;
  3503. edge pe = NULL;
  3504. basic_block new_bb;
  3505. tree aggr_ptr_init;
  3506. struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
  3507. tree aptr;
  3508. gimple_stmt_iterator incr_gsi;
  3509. bool insert_after;
  3510. tree indx_before_incr, indx_after_incr;
  3511. gimple incr;
  3512. tree step;
  3513. bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
  3514. gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
  3515. || TREE_CODE (aggr_type) == VECTOR_TYPE);
  3516. if (loop_vinfo)
  3517. {
  3518. loop = LOOP_VINFO_LOOP (loop_vinfo);
  3519. nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
  3520. containing_loop = (gimple_bb (stmt))->loop_father;
  3521. pe = loop_preheader_edge (loop);
  3522. }
  3523. else
  3524. {
  3525. gcc_assert (bb_vinfo);
  3526. only_init = true;
  3527. *ptr_incr = NULL;
  3528. }
  3529. /* Check the step (evolution) of the load in LOOP, and record
  3530. whether it's invariant. */
  3531. if (nested_in_vect_loop)
  3532. step = STMT_VINFO_DR_STEP (stmt_info);
  3533. else
  3534. step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
  3535. if (integer_zerop (step))
  3536. *inv_p = true;
  3537. else
  3538. *inv_p = false;
  3539. /* Create an expression for the first address accessed by this load
  3540. in LOOP. */
  3541. base_name = get_name (DR_BASE_ADDRESS (dr));
  3542. if (dump_enabled_p ())
  3543. {
  3544. tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
  3545. dump_printf_loc (MSG_NOTE, vect_location,
  3546. "create %s-pointer variable to type: ",
  3547. get_tree_code_name (TREE_CODE (aggr_type)));
  3548. dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
  3549. if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
  3550. dump_printf (MSG_NOTE, " vectorizing an array ref: ");
  3551. else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
  3552. dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
  3553. else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
  3554. dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
  3555. else
  3556. dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
  3557. dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
  3558. dump_printf (MSG_NOTE, "\n");
  3559. }
  3560. /* (1) Create the new aggregate-pointer variable.
  3561. Vector and array types inherit the alias set of their component
  3562. type by default so we need to use a ref-all pointer if the data
  3563. reference does not conflict with the created aggregated data
  3564. reference because it is not addressable. */
  3565. bool need_ref_all = false;
  3566. if (!alias_sets_conflict_p (get_alias_set (aggr_type),
  3567. get_alias_set (DR_REF (dr))))
  3568. need_ref_all = true;
  3569. /* Likewise for any of the data references in the stmt group. */
  3570. else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
  3571. {
  3572. gimple orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
  3573. do
  3574. {
  3575. stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
  3576. struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
  3577. if (!alias_sets_conflict_p (get_alias_set (aggr_type),
  3578. get_alias_set (DR_REF (sdr))))
  3579. {
  3580. need_ref_all = true;
  3581. break;
  3582. }
  3583. orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
  3584. }
  3585. while (orig_stmt);
  3586. }
  3587. aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
  3588. need_ref_all);
  3589. aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
  3590. /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
  3591. vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
  3592. def-use update cycles for the pointer: one relative to the outer-loop
  3593. (LOOP), which is what steps (3) and (4) below do. The other is relative
  3594. to the inner-loop (which is the inner-most loop containing the dataref),
  3595. and this is done be step (5) below.
  3596. When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
  3597. inner-most loop, and so steps (3),(4) work the same, and step (5) is
  3598. redundant. Steps (3),(4) create the following:
  3599. vp0 = &base_addr;
  3600. LOOP: vp1 = phi(vp0,vp2)
  3601. ...
  3602. ...
  3603. vp2 = vp1 + step
  3604. goto LOOP
  3605. If there is an inner-loop nested in loop, then step (5) will also be
  3606. applied, and an additional update in the inner-loop will be created:
  3607. vp0 = &base_addr;
  3608. LOOP: vp1 = phi(vp0,vp2)
  3609. ...
  3610. inner: vp3 = phi(vp1,vp4)
  3611. vp4 = vp3 + inner_step
  3612. if () goto inner
  3613. ...
  3614. vp2 = vp1 + step
  3615. if () goto LOOP */
  3616. /* (2) Calculate the initial address of the aggregate-pointer, and set
  3617. the aggregate-pointer to point to it before the loop. */
  3618. /* Create: (&(base[init_val+offset]+byte_offset) in the loop preheader. */
  3619. new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
  3620. offset, loop, byte_offset);
  3621. if (new_stmt_list)
  3622. {
  3623. if (pe)
  3624. {
  3625. new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
  3626. gcc_assert (!new_bb);
  3627. }
  3628. else
  3629. gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
  3630. }
  3631. *initial_address = new_temp;
  3632. /* Create: p = (aggr_type *) initial_base */
  3633. if (TREE_CODE (new_temp) != SSA_NAME
  3634. || !useless_type_conversion_p (aggr_ptr_type, TREE_TYPE (new_temp)))
  3635. {
  3636. vec_stmt = gimple_build_assign (aggr_ptr,
  3637. fold_convert (aggr_ptr_type, new_temp));
  3638. aggr_ptr_init = make_ssa_name (aggr_ptr, vec_stmt);
  3639. /* Copy the points-to information if it exists. */
  3640. if (DR_PTR_INFO (dr))
  3641. vect_duplicate_ssa_name_ptr_info (aggr_ptr_init, dr, stmt_info);
  3642. gimple_assign_set_lhs (vec_stmt, aggr_ptr_init);
  3643. if (pe)
  3644. {
  3645. new_bb = gsi_insert_on_edge_immediate (pe, vec_stmt);
  3646. gcc_assert (!new_bb);
  3647. }
  3648. else
  3649. gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
  3650. }
  3651. else
  3652. aggr_ptr_init = new_temp;
  3653. /* (3) Handle the updating of the aggregate-pointer inside the loop.
  3654. This is needed when ONLY_INIT is false, and also when AT_LOOP is the
  3655. inner-loop nested in LOOP (during outer-loop vectorization). */
  3656. /* No update in loop is required. */
  3657. if (only_init && (!loop_vinfo || at_loop == loop))
  3658. aptr = aggr_ptr_init;
  3659. else
  3660. {
  3661. /* The step of the aggregate pointer is the type size. */
  3662. tree iv_step = TYPE_SIZE_UNIT (aggr_type);
  3663. /* One exception to the above is when the scalar step of the load in
  3664. LOOP is zero. In this case the step here is also zero. */
  3665. if (*inv_p)
  3666. iv_step = size_zero_node;
  3667. else if (tree_int_cst_sgn (step) == -1)
  3668. iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
  3669. standard_iv_increment_position (loop, &incr_gsi, &insert_after);
  3670. create_iv (aggr_ptr_init,
  3671. fold_convert (aggr_ptr_type, iv_step),
  3672. aggr_ptr, loop, &incr_gsi, insert_after,
  3673. &indx_before_incr, &indx_after_incr);
  3674. incr = gsi_stmt (incr_gsi);
  3675. set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
  3676. /* Copy the points-to information if it exists. */
  3677. if (DR_PTR_INFO (dr))
  3678. {
  3679. vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr, stmt_info);
  3680. vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr, stmt_info);
  3681. }
  3682. if (ptr_incr)
  3683. *ptr_incr = incr;
  3684. aptr = indx_before_incr;
  3685. }
  3686. if (!nested_in_vect_loop || only_init)
  3687. return aptr;
  3688. /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
  3689. nested in LOOP, if exists. */
  3690. gcc_assert (nested_in_vect_loop);
  3691. if (!only_init)
  3692. {
  3693. standard_iv_increment_position (containing_loop, &incr_gsi,
  3694. &insert_after);
  3695. create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
  3696. containing_loop, &incr_gsi, insert_after, &indx_before_incr,
  3697. &indx_after_incr);
  3698. incr = gsi_stmt (incr_gsi);
  3699. set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
  3700. /* Copy the points-to information if it exists. */
  3701. if (DR_PTR_INFO (dr))
  3702. {
  3703. vect_duplicate_ssa_name_ptr_info (indx_before_incr, dr, stmt_info);
  3704. vect_duplicate_ssa_name_ptr_info (indx_after_incr, dr, stmt_info);
  3705. }
  3706. if (ptr_incr)
  3707. *ptr_incr = incr;
  3708. return indx_before_incr;
  3709. }
  3710. else
  3711. gcc_unreachable ();
  3712. }
  3713. /* Function bump_vector_ptr
  3714. Increment a pointer (to a vector type) by vector-size. If requested,
  3715. i.e. if PTR-INCR is given, then also connect the new increment stmt
  3716. to the existing def-use update-chain of the pointer, by modifying
  3717. the PTR_INCR as illustrated below:
  3718. The pointer def-use update-chain before this function:
  3719. DATAREF_PTR = phi (p_0, p_2)
  3720. ....
  3721. PTR_INCR: p_2 = DATAREF_PTR + step
  3722. The pointer def-use update-chain after this function:
  3723. DATAREF_PTR = phi (p_0, p_2)
  3724. ....
  3725. NEW_DATAREF_PTR = DATAREF_PTR + BUMP
  3726. ....
  3727. PTR_INCR: p_2 = NEW_DATAREF_PTR + step
  3728. Input:
  3729. DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
  3730. in the loop.
  3731. PTR_INCR - optional. The stmt that updates the pointer in each iteration of
  3732. the loop. The increment amount across iterations is expected
  3733. to be vector_size.
  3734. BSI - location where the new update stmt is to be placed.
  3735. STMT - the original scalar memory-access stmt that is being vectorized.
  3736. BUMP - optional. The offset by which to bump the pointer. If not given,
  3737. the offset is assumed to be vector_size.
  3738. Output: Return NEW_DATAREF_PTR as illustrated above.
  3739. */
  3740. tree
  3741. bump_vector_ptr (tree dataref_ptr, gimple ptr_incr, gimple_stmt_iterator *gsi,
  3742. gimple stmt, tree bump)
  3743. {
  3744. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  3745. struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
  3746. tree vectype = STMT_VINFO_VECTYPE (stmt_info);
  3747. tree update = TYPE_SIZE_UNIT (vectype);
  3748. gassign *incr_stmt;
  3749. ssa_op_iter iter;
  3750. use_operand_p use_p;
  3751. tree new_dataref_ptr;
  3752. if (bump)
  3753. update = bump;
  3754. new_dataref_ptr = copy_ssa_name (dataref_ptr);
  3755. incr_stmt = gimple_build_assign (new_dataref_ptr, POINTER_PLUS_EXPR,
  3756. dataref_ptr, update);
  3757. vect_finish_stmt_generation (stmt, incr_stmt, gsi);
  3758. /* Copy the points-to information if it exists. */
  3759. if (DR_PTR_INFO (dr))
  3760. {
  3761. duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
  3762. mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
  3763. }
  3764. if (!ptr_incr)
  3765. return new_dataref_ptr;
  3766. /* Update the vector-pointer's cross-iteration increment. */
  3767. FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
  3768. {
  3769. tree use = USE_FROM_PTR (use_p);
  3770. if (use == dataref_ptr)
  3771. SET_USE (use_p, new_dataref_ptr);
  3772. else
  3773. gcc_assert (tree_int_cst_compare (use, update) == 0);
  3774. }
  3775. return new_dataref_ptr;
  3776. }
  3777. /* Function vect_create_destination_var.
  3778. Create a new temporary of type VECTYPE. */
  3779. tree
  3780. vect_create_destination_var (tree scalar_dest, tree vectype)
  3781. {
  3782. tree vec_dest;
  3783. const char *name;
  3784. char *new_name;
  3785. tree type;
  3786. enum vect_var_kind kind;
  3787. kind = vectype ? vect_simple_var : vect_scalar_var;
  3788. type = vectype ? vectype : TREE_TYPE (scalar_dest);
  3789. gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
  3790. name = get_name (scalar_dest);
  3791. if (name)
  3792. new_name = xasprintf ("%s_%u", name, SSA_NAME_VERSION (scalar_dest));
  3793. else
  3794. new_name = xasprintf ("_%u", SSA_NAME_VERSION (scalar_dest));
  3795. vec_dest = vect_get_new_vect_var (type, kind, new_name);
  3796. free (new_name);
  3797. return vec_dest;
  3798. }
  3799. /* Function vect_grouped_store_supported.
  3800. Returns TRUE if interleave high and interleave low permutations
  3801. are supported, and FALSE otherwise. */
  3802. bool
  3803. vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
  3804. {
  3805. machine_mode mode = TYPE_MODE (vectype);
  3806. /* vect_permute_store_chain requires the group size to be equal to 3 or
  3807. be a power of two. */
  3808. if (count != 3 && exact_log2 (count) == -1)
  3809. {
  3810. if (dump_enabled_p ())
  3811. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  3812. "the size of the group of accesses"
  3813. " is not a power of 2 or not eqaul to 3\n");
  3814. return false;
  3815. }
  3816. /* Check that the permutation is supported. */
  3817. if (VECTOR_MODE_P (mode))
  3818. {
  3819. unsigned int i, nelt = GET_MODE_NUNITS (mode);
  3820. unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
  3821. if (count == 3)
  3822. {
  3823. unsigned int j0 = 0, j1 = 0, j2 = 0;
  3824. unsigned int i, j;
  3825. for (j = 0; j < 3; j++)
  3826. {
  3827. int nelt0 = ((3 - j) * nelt) % 3;
  3828. int nelt1 = ((3 - j) * nelt + 1) % 3;
  3829. int nelt2 = ((3 - j) * nelt + 2) % 3;
  3830. for (i = 0; i < nelt; i++)
  3831. {
  3832. if (3 * i + nelt0 < nelt)
  3833. sel[3 * i + nelt0] = j0++;
  3834. if (3 * i + nelt1 < nelt)
  3835. sel[3 * i + nelt1] = nelt + j1++;
  3836. if (3 * i + nelt2 < nelt)
  3837. sel[3 * i + nelt2] = 0;
  3838. }
  3839. if (!can_vec_perm_p (mode, false, sel))
  3840. {
  3841. if (dump_enabled_p ())
  3842. dump_printf (MSG_MISSED_OPTIMIZATION,
  3843. "permutaion op not supported by target.\n");
  3844. return false;
  3845. }
  3846. for (i = 0; i < nelt; i++)
  3847. {
  3848. if (3 * i + nelt0 < nelt)
  3849. sel[3 * i + nelt0] = 3 * i + nelt0;
  3850. if (3 * i + nelt1 < nelt)
  3851. sel[3 * i + nelt1] = 3 * i + nelt1;
  3852. if (3 * i + nelt2 < nelt)
  3853. sel[3 * i + nelt2] = nelt + j2++;
  3854. }
  3855. if (!can_vec_perm_p (mode, false, sel))
  3856. {
  3857. if (dump_enabled_p ())
  3858. dump_printf (MSG_MISSED_OPTIMIZATION,
  3859. "permutaion op not supported by target.\n");
  3860. return false;
  3861. }
  3862. }
  3863. return true;
  3864. }
  3865. else
  3866. {
  3867. /* If length is not equal to 3 then only power of 2 is supported. */
  3868. gcc_assert (exact_log2 (count) != -1);
  3869. for (i = 0; i < nelt / 2; i++)
  3870. {
  3871. sel[i * 2] = i;
  3872. sel[i * 2 + 1] = i + nelt;
  3873. }
  3874. if (can_vec_perm_p (mode, false, sel))
  3875. {
  3876. for (i = 0; i < nelt; i++)
  3877. sel[i] += nelt / 2;
  3878. if (can_vec_perm_p (mode, false, sel))
  3879. return true;
  3880. }
  3881. }
  3882. }
  3883. if (dump_enabled_p ())
  3884. dump_printf (MSG_MISSED_OPTIMIZATION,
  3885. "permutaion op not supported by target.\n");
  3886. return false;
  3887. }
  3888. /* Return TRUE if vec_store_lanes is available for COUNT vectors of
  3889. type VECTYPE. */
  3890. bool
  3891. vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
  3892. {
  3893. return vect_lanes_optab_supported_p ("vec_store_lanes",
  3894. vec_store_lanes_optab,
  3895. vectype, count);
  3896. }
  3897. /* Function vect_permute_store_chain.
  3898. Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
  3899. a power of 2 or equal to 3, generate interleave_high/low stmts to reorder
  3900. the data correctly for the stores. Return the final references for stores
  3901. in RESULT_CHAIN.
  3902. E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
  3903. The input is 4 vectors each containing 8 elements. We assign a number to
  3904. each element, the input sequence is:
  3905. 1st vec: 0 1 2 3 4 5 6 7
  3906. 2nd vec: 8 9 10 11 12 13 14 15
  3907. 3rd vec: 16 17 18 19 20 21 22 23
  3908. 4th vec: 24 25 26 27 28 29 30 31
  3909. The output sequence should be:
  3910. 1st vec: 0 8 16 24 1 9 17 25
  3911. 2nd vec: 2 10 18 26 3 11 19 27
  3912. 3rd vec: 4 12 20 28 5 13 21 30
  3913. 4th vec: 6 14 22 30 7 15 23 31
  3914. i.e., we interleave the contents of the four vectors in their order.
  3915. We use interleave_high/low instructions to create such output. The input of
  3916. each interleave_high/low operation is two vectors:
  3917. 1st vec 2nd vec
  3918. 0 1 2 3 4 5 6 7
  3919. the even elements of the result vector are obtained left-to-right from the
  3920. high/low elements of the first vector. The odd elements of the result are
  3921. obtained left-to-right from the high/low elements of the second vector.
  3922. The output of interleave_high will be: 0 4 1 5
  3923. and of interleave_low: 2 6 3 7
  3924. The permutation is done in log LENGTH stages. In each stage interleave_high
  3925. and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
  3926. where the first argument is taken from the first half of DR_CHAIN and the
  3927. second argument from it's second half.
  3928. In our example,
  3929. I1: interleave_high (1st vec, 3rd vec)
  3930. I2: interleave_low (1st vec, 3rd vec)
  3931. I3: interleave_high (2nd vec, 4th vec)
  3932. I4: interleave_low (2nd vec, 4th vec)
  3933. The output for the first stage is:
  3934. I1: 0 16 1 17 2 18 3 19
  3935. I2: 4 20 5 21 6 22 7 23
  3936. I3: 8 24 9 25 10 26 11 27
  3937. I4: 12 28 13 29 14 30 15 31
  3938. The output of the second stage, i.e. the final result is:
  3939. I1: 0 8 16 24 1 9 17 25
  3940. I2: 2 10 18 26 3 11 19 27
  3941. I3: 4 12 20 28 5 13 21 30
  3942. I4: 6 14 22 30 7 15 23 31. */
  3943. void
  3944. vect_permute_store_chain (vec<tree> dr_chain,
  3945. unsigned int length,
  3946. gimple stmt,
  3947. gimple_stmt_iterator *gsi,
  3948. vec<tree> *result_chain)
  3949. {
  3950. tree vect1, vect2, high, low;
  3951. gimple perm_stmt;
  3952. tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
  3953. tree perm_mask_low, perm_mask_high;
  3954. tree data_ref;
  3955. tree perm3_mask_low, perm3_mask_high;
  3956. unsigned int i, n, log_length = exact_log2 (length);
  3957. unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
  3958. unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
  3959. result_chain->quick_grow (length);
  3960. memcpy (result_chain->address (), dr_chain.address (),
  3961. length * sizeof (tree));
  3962. if (length == 3)
  3963. {
  3964. unsigned int j0 = 0, j1 = 0, j2 = 0;
  3965. for (j = 0; j < 3; j++)
  3966. {
  3967. int nelt0 = ((3 - j) * nelt) % 3;
  3968. int nelt1 = ((3 - j) * nelt + 1) % 3;
  3969. int nelt2 = ((3 - j) * nelt + 2) % 3;
  3970. for (i = 0; i < nelt; i++)
  3971. {
  3972. if (3 * i + nelt0 < nelt)
  3973. sel[3 * i + nelt0] = j0++;
  3974. if (3 * i + nelt1 < nelt)
  3975. sel[3 * i + nelt1] = nelt + j1++;
  3976. if (3 * i + nelt2 < nelt)
  3977. sel[3 * i + nelt2] = 0;
  3978. }
  3979. perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
  3980. for (i = 0; i < nelt; i++)
  3981. {
  3982. if (3 * i + nelt0 < nelt)
  3983. sel[3 * i + nelt0] = 3 * i + nelt0;
  3984. if (3 * i + nelt1 < nelt)
  3985. sel[3 * i + nelt1] = 3 * i + nelt1;
  3986. if (3 * i + nelt2 < nelt)
  3987. sel[3 * i + nelt2] = nelt + j2++;
  3988. }
  3989. perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
  3990. vect1 = dr_chain[0];
  3991. vect2 = dr_chain[1];
  3992. /* Create interleaving stmt:
  3993. low = VEC_PERM_EXPR <vect1, vect2,
  3994. {j, nelt, *, j + 1, nelt + j + 1, *,
  3995. j + 2, nelt + j + 2, *, ...}> */
  3996. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
  3997. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
  3998. vect2, perm3_mask_low);
  3999. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4000. vect1 = data_ref;
  4001. vect2 = dr_chain[2];
  4002. /* Create interleaving stmt:
  4003. low = VEC_PERM_EXPR <vect1, vect2,
  4004. {0, 1, nelt + j, 3, 4, nelt + j + 1,
  4005. 6, 7, nelt + j + 2, ...}> */
  4006. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
  4007. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect1,
  4008. vect2, perm3_mask_high);
  4009. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4010. (*result_chain)[j] = data_ref;
  4011. }
  4012. }
  4013. else
  4014. {
  4015. /* If length is not equal to 3 then only power of 2 is supported. */
  4016. gcc_assert (exact_log2 (length) != -1);
  4017. for (i = 0, n = nelt / 2; i < n; i++)
  4018. {
  4019. sel[i * 2] = i;
  4020. sel[i * 2 + 1] = i + nelt;
  4021. }
  4022. perm_mask_high = vect_gen_perm_mask_checked (vectype, sel);
  4023. for (i = 0; i < nelt; i++)
  4024. sel[i] += nelt / 2;
  4025. perm_mask_low = vect_gen_perm_mask_checked (vectype, sel);
  4026. for (i = 0, n = log_length; i < n; i++)
  4027. {
  4028. for (j = 0; j < length/2; j++)
  4029. {
  4030. vect1 = dr_chain[j];
  4031. vect2 = dr_chain[j+length/2];
  4032. /* Create interleaving stmt:
  4033. high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1,
  4034. ...}> */
  4035. high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
  4036. perm_stmt = gimple_build_assign (high, VEC_PERM_EXPR, vect1,
  4037. vect2, perm_mask_high);
  4038. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4039. (*result_chain)[2*j] = high;
  4040. /* Create interleaving stmt:
  4041. low = VEC_PERM_EXPR <vect1, vect2,
  4042. {nelt/2, nelt*3/2, nelt/2+1, nelt*3/2+1,
  4043. ...}> */
  4044. low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
  4045. perm_stmt = gimple_build_assign (low, VEC_PERM_EXPR, vect1,
  4046. vect2, perm_mask_low);
  4047. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4048. (*result_chain)[2*j+1] = low;
  4049. }
  4050. memcpy (dr_chain.address (), result_chain->address (),
  4051. length * sizeof (tree));
  4052. }
  4053. }
  4054. }
  4055. /* Function vect_setup_realignment
  4056. This function is called when vectorizing an unaligned load using
  4057. the dr_explicit_realign[_optimized] scheme.
  4058. This function generates the following code at the loop prolog:
  4059. p = initial_addr;
  4060. x msq_init = *(floor(p)); # prolog load
  4061. realignment_token = call target_builtin;
  4062. loop:
  4063. x msq = phi (msq_init, ---)
  4064. The stmts marked with x are generated only for the case of
  4065. dr_explicit_realign_optimized.
  4066. The code above sets up a new (vector) pointer, pointing to the first
  4067. location accessed by STMT, and a "floor-aligned" load using that pointer.
  4068. It also generates code to compute the "realignment-token" (if the relevant
  4069. target hook was defined), and creates a phi-node at the loop-header bb
  4070. whose arguments are the result of the prolog-load (created by this
  4071. function) and the result of a load that takes place in the loop (to be
  4072. created by the caller to this function).
  4073. For the case of dr_explicit_realign_optimized:
  4074. The caller to this function uses the phi-result (msq) to create the
  4075. realignment code inside the loop, and sets up the missing phi argument,
  4076. as follows:
  4077. loop:
  4078. msq = phi (msq_init, lsq)
  4079. lsq = *(floor(p')); # load in loop
  4080. result = realign_load (msq, lsq, realignment_token);
  4081. For the case of dr_explicit_realign:
  4082. loop:
  4083. msq = *(floor(p)); # load in loop
  4084. p' = p + (VS-1);
  4085. lsq = *(floor(p')); # load in loop
  4086. result = realign_load (msq, lsq, realignment_token);
  4087. Input:
  4088. STMT - (scalar) load stmt to be vectorized. This load accesses
  4089. a memory location that may be unaligned.
  4090. BSI - place where new code is to be inserted.
  4091. ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
  4092. is used.
  4093. Output:
  4094. REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
  4095. target hook, if defined.
  4096. Return value - the result of the loop-header phi node. */
  4097. tree
  4098. vect_setup_realignment (gimple stmt, gimple_stmt_iterator *gsi,
  4099. tree *realignment_token,
  4100. enum dr_alignment_support alignment_support_scheme,
  4101. tree init_addr,
  4102. struct loop **at_loop)
  4103. {
  4104. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  4105. tree vectype = STMT_VINFO_VECTYPE (stmt_info);
  4106. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  4107. struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
  4108. struct loop *loop = NULL;
  4109. edge pe = NULL;
  4110. tree scalar_dest = gimple_assign_lhs (stmt);
  4111. tree vec_dest;
  4112. gimple inc;
  4113. tree ptr;
  4114. tree data_ref;
  4115. basic_block new_bb;
  4116. tree msq_init = NULL_TREE;
  4117. tree new_temp;
  4118. gphi *phi_stmt;
  4119. tree msq = NULL_TREE;
  4120. gimple_seq stmts = NULL;
  4121. bool inv_p;
  4122. bool compute_in_loop = false;
  4123. bool nested_in_vect_loop = false;
  4124. struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
  4125. struct loop *loop_for_initial_load = NULL;
  4126. if (loop_vinfo)
  4127. {
  4128. loop = LOOP_VINFO_LOOP (loop_vinfo);
  4129. nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
  4130. }
  4131. gcc_assert (alignment_support_scheme == dr_explicit_realign
  4132. || alignment_support_scheme == dr_explicit_realign_optimized);
  4133. /* We need to generate three things:
  4134. 1. the misalignment computation
  4135. 2. the extra vector load (for the optimized realignment scheme).
  4136. 3. the phi node for the two vectors from which the realignment is
  4137. done (for the optimized realignment scheme). */
  4138. /* 1. Determine where to generate the misalignment computation.
  4139. If INIT_ADDR is NULL_TREE, this indicates that the misalignment
  4140. calculation will be generated by this function, outside the loop (in the
  4141. preheader). Otherwise, INIT_ADDR had already been computed for us by the
  4142. caller, inside the loop.
  4143. Background: If the misalignment remains fixed throughout the iterations of
  4144. the loop, then both realignment schemes are applicable, and also the
  4145. misalignment computation can be done outside LOOP. This is because we are
  4146. vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
  4147. are a multiple of VS (the Vector Size), and therefore the misalignment in
  4148. different vectorized LOOP iterations is always the same.
  4149. The problem arises only if the memory access is in an inner-loop nested
  4150. inside LOOP, which is now being vectorized using outer-loop vectorization.
  4151. This is the only case when the misalignment of the memory access may not
  4152. remain fixed throughout the iterations of the inner-loop (as explained in
  4153. detail in vect_supportable_dr_alignment). In this case, not only is the
  4154. optimized realignment scheme not applicable, but also the misalignment
  4155. computation (and generation of the realignment token that is passed to
  4156. REALIGN_LOAD) have to be done inside the loop.
  4157. In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
  4158. or not, which in turn determines if the misalignment is computed inside
  4159. the inner-loop, or outside LOOP. */
  4160. if (init_addr != NULL_TREE || !loop_vinfo)
  4161. {
  4162. compute_in_loop = true;
  4163. gcc_assert (alignment_support_scheme == dr_explicit_realign);
  4164. }
  4165. /* 2. Determine where to generate the extra vector load.
  4166. For the optimized realignment scheme, instead of generating two vector
  4167. loads in each iteration, we generate a single extra vector load in the
  4168. preheader of the loop, and in each iteration reuse the result of the
  4169. vector load from the previous iteration. In case the memory access is in
  4170. an inner-loop nested inside LOOP, which is now being vectorized using
  4171. outer-loop vectorization, we need to determine whether this initial vector
  4172. load should be generated at the preheader of the inner-loop, or can be
  4173. generated at the preheader of LOOP. If the memory access has no evolution
  4174. in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
  4175. to be generated inside LOOP (in the preheader of the inner-loop). */
  4176. if (nested_in_vect_loop)
  4177. {
  4178. tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
  4179. bool invariant_in_outerloop =
  4180. (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
  4181. loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
  4182. }
  4183. else
  4184. loop_for_initial_load = loop;
  4185. if (at_loop)
  4186. *at_loop = loop_for_initial_load;
  4187. if (loop_for_initial_load)
  4188. pe = loop_preheader_edge (loop_for_initial_load);
  4189. /* 3. For the case of the optimized realignment, create the first vector
  4190. load at the loop preheader. */
  4191. if (alignment_support_scheme == dr_explicit_realign_optimized)
  4192. {
  4193. /* Create msq_init = *(floor(p1)) in the loop preheader */
  4194. gassign *new_stmt;
  4195. gcc_assert (!compute_in_loop);
  4196. vec_dest = vect_create_destination_var (scalar_dest, vectype);
  4197. ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
  4198. NULL_TREE, &init_addr, NULL, &inc,
  4199. true, &inv_p);
  4200. new_temp = copy_ssa_name (ptr);
  4201. new_stmt = gimple_build_assign
  4202. (new_temp, BIT_AND_EXPR, ptr,
  4203. build_int_cst (TREE_TYPE (ptr),
  4204. -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
  4205. new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
  4206. gcc_assert (!new_bb);
  4207. data_ref
  4208. = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
  4209. build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
  4210. new_stmt = gimple_build_assign (vec_dest, data_ref);
  4211. new_temp = make_ssa_name (vec_dest, new_stmt);
  4212. gimple_assign_set_lhs (new_stmt, new_temp);
  4213. if (pe)
  4214. {
  4215. new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
  4216. gcc_assert (!new_bb);
  4217. }
  4218. else
  4219. gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
  4220. msq_init = gimple_assign_lhs (new_stmt);
  4221. }
  4222. /* 4. Create realignment token using a target builtin, if available.
  4223. It is done either inside the containing loop, or before LOOP (as
  4224. determined above). */
  4225. if (targetm.vectorize.builtin_mask_for_load)
  4226. {
  4227. gcall *new_stmt;
  4228. tree builtin_decl;
  4229. /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
  4230. if (!init_addr)
  4231. {
  4232. /* Generate the INIT_ADDR computation outside LOOP. */
  4233. init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
  4234. NULL_TREE, loop);
  4235. if (loop)
  4236. {
  4237. pe = loop_preheader_edge (loop);
  4238. new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
  4239. gcc_assert (!new_bb);
  4240. }
  4241. else
  4242. gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
  4243. }
  4244. builtin_decl = targetm.vectorize.builtin_mask_for_load ();
  4245. new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
  4246. vec_dest =
  4247. vect_create_destination_var (scalar_dest,
  4248. gimple_call_return_type (new_stmt));
  4249. new_temp = make_ssa_name (vec_dest, new_stmt);
  4250. gimple_call_set_lhs (new_stmt, new_temp);
  4251. if (compute_in_loop)
  4252. gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
  4253. else
  4254. {
  4255. /* Generate the misalignment computation outside LOOP. */
  4256. pe = loop_preheader_edge (loop);
  4257. new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
  4258. gcc_assert (!new_bb);
  4259. }
  4260. *realignment_token = gimple_call_lhs (new_stmt);
  4261. /* The result of the CALL_EXPR to this builtin is determined from
  4262. the value of the parameter and no global variables are touched
  4263. which makes the builtin a "const" function. Requiring the
  4264. builtin to have the "const" attribute makes it unnecessary
  4265. to call mark_call_clobbered. */
  4266. gcc_assert (TREE_READONLY (builtin_decl));
  4267. }
  4268. if (alignment_support_scheme == dr_explicit_realign)
  4269. return msq;
  4270. gcc_assert (!compute_in_loop);
  4271. gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
  4272. /* 5. Create msq = phi <msq_init, lsq> in loop */
  4273. pe = loop_preheader_edge (containing_loop);
  4274. vec_dest = vect_create_destination_var (scalar_dest, vectype);
  4275. msq = make_ssa_name (vec_dest);
  4276. phi_stmt = create_phi_node (msq, containing_loop->header);
  4277. add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
  4278. return msq;
  4279. }
  4280. /* Function vect_grouped_load_supported.
  4281. Returns TRUE if even and odd permutations are supported,
  4282. and FALSE otherwise. */
  4283. bool
  4284. vect_grouped_load_supported (tree vectype, unsigned HOST_WIDE_INT count)
  4285. {
  4286. machine_mode mode = TYPE_MODE (vectype);
  4287. /* vect_permute_load_chain requires the group size to be equal to 3 or
  4288. be a power of two. */
  4289. if (count != 3 && exact_log2 (count) == -1)
  4290. {
  4291. if (dump_enabled_p ())
  4292. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4293. "the size of the group of accesses"
  4294. " is not a power of 2 or not equal to 3\n");
  4295. return false;
  4296. }
  4297. /* Check that the permutation is supported. */
  4298. if (VECTOR_MODE_P (mode))
  4299. {
  4300. unsigned int i, j, nelt = GET_MODE_NUNITS (mode);
  4301. unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
  4302. if (count == 3)
  4303. {
  4304. unsigned int k;
  4305. for (k = 0; k < 3; k++)
  4306. {
  4307. for (i = 0; i < nelt; i++)
  4308. if (3 * i + k < 2 * nelt)
  4309. sel[i] = 3 * i + k;
  4310. else
  4311. sel[i] = 0;
  4312. if (!can_vec_perm_p (mode, false, sel))
  4313. {
  4314. if (dump_enabled_p ())
  4315. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4316. "shuffle of 3 loads is not supported by"
  4317. " target\n");
  4318. return false;
  4319. }
  4320. for (i = 0, j = 0; i < nelt; i++)
  4321. if (3 * i + k < 2 * nelt)
  4322. sel[i] = i;
  4323. else
  4324. sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
  4325. if (!can_vec_perm_p (mode, false, sel))
  4326. {
  4327. if (dump_enabled_p ())
  4328. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4329. "shuffle of 3 loads is not supported by"
  4330. " target\n");
  4331. return false;
  4332. }
  4333. }
  4334. return true;
  4335. }
  4336. else
  4337. {
  4338. /* If length is not equal to 3 then only power of 2 is supported. */
  4339. gcc_assert (exact_log2 (count) != -1);
  4340. for (i = 0; i < nelt; i++)
  4341. sel[i] = i * 2;
  4342. if (can_vec_perm_p (mode, false, sel))
  4343. {
  4344. for (i = 0; i < nelt; i++)
  4345. sel[i] = i * 2 + 1;
  4346. if (can_vec_perm_p (mode, false, sel))
  4347. return true;
  4348. }
  4349. }
  4350. }
  4351. if (dump_enabled_p ())
  4352. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4353. "extract even/odd not supported by target\n");
  4354. return false;
  4355. }
  4356. /* Return TRUE if vec_load_lanes is available for COUNT vectors of
  4357. type VECTYPE. */
  4358. bool
  4359. vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
  4360. {
  4361. return vect_lanes_optab_supported_p ("vec_load_lanes",
  4362. vec_load_lanes_optab,
  4363. vectype, count);
  4364. }
  4365. /* Function vect_permute_load_chain.
  4366. Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
  4367. a power of 2 or equal to 3, generate extract_even/odd stmts to reorder
  4368. the input data correctly. Return the final references for loads in
  4369. RESULT_CHAIN.
  4370. E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
  4371. The input is 4 vectors each containing 8 elements. We assign a number to each
  4372. element, the input sequence is:
  4373. 1st vec: 0 1 2 3 4 5 6 7
  4374. 2nd vec: 8 9 10 11 12 13 14 15
  4375. 3rd vec: 16 17 18 19 20 21 22 23
  4376. 4th vec: 24 25 26 27 28 29 30 31
  4377. The output sequence should be:
  4378. 1st vec: 0 4 8 12 16 20 24 28
  4379. 2nd vec: 1 5 9 13 17 21 25 29
  4380. 3rd vec: 2 6 10 14 18 22 26 30
  4381. 4th vec: 3 7 11 15 19 23 27 31
  4382. i.e., the first output vector should contain the first elements of each
  4383. interleaving group, etc.
  4384. We use extract_even/odd instructions to create such output. The input of
  4385. each extract_even/odd operation is two vectors
  4386. 1st vec 2nd vec
  4387. 0 1 2 3 4 5 6 7
  4388. and the output is the vector of extracted even/odd elements. The output of
  4389. extract_even will be: 0 2 4 6
  4390. and of extract_odd: 1 3 5 7
  4391. The permutation is done in log LENGTH stages. In each stage extract_even
  4392. and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
  4393. their order. In our example,
  4394. E1: extract_even (1st vec, 2nd vec)
  4395. E2: extract_odd (1st vec, 2nd vec)
  4396. E3: extract_even (3rd vec, 4th vec)
  4397. E4: extract_odd (3rd vec, 4th vec)
  4398. The output for the first stage will be:
  4399. E1: 0 2 4 6 8 10 12 14
  4400. E2: 1 3 5 7 9 11 13 15
  4401. E3: 16 18 20 22 24 26 28 30
  4402. E4: 17 19 21 23 25 27 29 31
  4403. In order to proceed and create the correct sequence for the next stage (or
  4404. for the correct output, if the second stage is the last one, as in our
  4405. example), we first put the output of extract_even operation and then the
  4406. output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
  4407. The input for the second stage is:
  4408. 1st vec (E1): 0 2 4 6 8 10 12 14
  4409. 2nd vec (E3): 16 18 20 22 24 26 28 30
  4410. 3rd vec (E2): 1 3 5 7 9 11 13 15
  4411. 4th vec (E4): 17 19 21 23 25 27 29 31
  4412. The output of the second stage:
  4413. E1: 0 4 8 12 16 20 24 28
  4414. E2: 2 6 10 14 18 22 26 30
  4415. E3: 1 5 9 13 17 21 25 29
  4416. E4: 3 7 11 15 19 23 27 31
  4417. And RESULT_CHAIN after reordering:
  4418. 1st vec (E1): 0 4 8 12 16 20 24 28
  4419. 2nd vec (E3): 1 5 9 13 17 21 25 29
  4420. 3rd vec (E2): 2 6 10 14 18 22 26 30
  4421. 4th vec (E4): 3 7 11 15 19 23 27 31. */
  4422. static void
  4423. vect_permute_load_chain (vec<tree> dr_chain,
  4424. unsigned int length,
  4425. gimple stmt,
  4426. gimple_stmt_iterator *gsi,
  4427. vec<tree> *result_chain)
  4428. {
  4429. tree data_ref, first_vect, second_vect;
  4430. tree perm_mask_even, perm_mask_odd;
  4431. tree perm3_mask_low, perm3_mask_high;
  4432. gimple perm_stmt;
  4433. tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
  4434. unsigned int i, j, log_length = exact_log2 (length);
  4435. unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
  4436. unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
  4437. result_chain->quick_grow (length);
  4438. memcpy (result_chain->address (), dr_chain.address (),
  4439. length * sizeof (tree));
  4440. if (length == 3)
  4441. {
  4442. unsigned int k;
  4443. for (k = 0; k < 3; k++)
  4444. {
  4445. for (i = 0; i < nelt; i++)
  4446. if (3 * i + k < 2 * nelt)
  4447. sel[i] = 3 * i + k;
  4448. else
  4449. sel[i] = 0;
  4450. perm3_mask_low = vect_gen_perm_mask_checked (vectype, sel);
  4451. for (i = 0, j = 0; i < nelt; i++)
  4452. if (3 * i + k < 2 * nelt)
  4453. sel[i] = i;
  4454. else
  4455. sel[i] = nelt + ((nelt + k) % 3) + 3 * (j++);
  4456. perm3_mask_high = vect_gen_perm_mask_checked (vectype, sel);
  4457. first_vect = dr_chain[0];
  4458. second_vect = dr_chain[1];
  4459. /* Create interleaving stmt (low part of):
  4460. low = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
  4461. ...}> */
  4462. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_low");
  4463. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
  4464. second_vect, perm3_mask_low);
  4465. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4466. /* Create interleaving stmt (high part of):
  4467. high = VEC_PERM_EXPR <first_vect, second_vect2, {k, 3 + k, 6 + k,
  4468. ...}> */
  4469. first_vect = data_ref;
  4470. second_vect = dr_chain[2];
  4471. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3_high");
  4472. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, first_vect,
  4473. second_vect, perm3_mask_high);
  4474. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4475. (*result_chain)[k] = data_ref;
  4476. }
  4477. }
  4478. else
  4479. {
  4480. /* If length is not equal to 3 then only power of 2 is supported. */
  4481. gcc_assert (exact_log2 (length) != -1);
  4482. for (i = 0; i < nelt; ++i)
  4483. sel[i] = i * 2;
  4484. perm_mask_even = vect_gen_perm_mask_checked (vectype, sel);
  4485. for (i = 0; i < nelt; ++i)
  4486. sel[i] = i * 2 + 1;
  4487. perm_mask_odd = vect_gen_perm_mask_checked (vectype, sel);
  4488. for (i = 0; i < log_length; i++)
  4489. {
  4490. for (j = 0; j < length; j += 2)
  4491. {
  4492. first_vect = dr_chain[j];
  4493. second_vect = dr_chain[j+1];
  4494. /* data_ref = permute_even (first_data_ref, second_data_ref); */
  4495. data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
  4496. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
  4497. first_vect, second_vect,
  4498. perm_mask_even);
  4499. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4500. (*result_chain)[j/2] = data_ref;
  4501. /* data_ref = permute_odd (first_data_ref, second_data_ref); */
  4502. data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
  4503. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
  4504. first_vect, second_vect,
  4505. perm_mask_odd);
  4506. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4507. (*result_chain)[j/2+length/2] = data_ref;
  4508. }
  4509. memcpy (dr_chain.address (), result_chain->address (),
  4510. length * sizeof (tree));
  4511. }
  4512. }
  4513. }
  4514. /* Function vect_shift_permute_load_chain.
  4515. Given a chain of loads in DR_CHAIN of LENGTH 2 or 3, generate
  4516. sequence of stmts to reorder the input data accordingly.
  4517. Return the final references for loads in RESULT_CHAIN.
  4518. Return true if successed, false otherwise.
  4519. E.g., LENGTH is 3 and the scalar type is short, i.e., VF is 8.
  4520. The input is 3 vectors each containing 8 elements. We assign a
  4521. number to each element, the input sequence is:
  4522. 1st vec: 0 1 2 3 4 5 6 7
  4523. 2nd vec: 8 9 10 11 12 13 14 15
  4524. 3rd vec: 16 17 18 19 20 21 22 23
  4525. The output sequence should be:
  4526. 1st vec: 0 3 6 9 12 15 18 21
  4527. 2nd vec: 1 4 7 10 13 16 19 22
  4528. 3rd vec: 2 5 8 11 14 17 20 23
  4529. We use 3 shuffle instructions and 3 * 3 - 1 shifts to create such output.
  4530. First we shuffle all 3 vectors to get correct elements order:
  4531. 1st vec: ( 0 3 6) ( 1 4 7) ( 2 5)
  4532. 2nd vec: ( 8 11 14) ( 9 12 15) (10 13)
  4533. 3rd vec: (16 19 22) (17 20 23) (18 21)
  4534. Next we unite and shift vector 3 times:
  4535. 1st step:
  4536. shift right by 6 the concatenation of:
  4537. "1st vec" and "2nd vec"
  4538. ( 0 3 6) ( 1 4 7) |( 2 5) _ ( 8 11 14) ( 9 12 15)| (10 13)
  4539. "2nd vec" and "3rd vec"
  4540. ( 8 11 14) ( 9 12 15) |(10 13) _ (16 19 22) (17 20 23)| (18 21)
  4541. "3rd vec" and "1st vec"
  4542. (16 19 22) (17 20 23) |(18 21) _ ( 0 3 6) ( 1 4 7)| ( 2 5)
  4543. | New vectors |
  4544. So that now new vectors are:
  4545. 1st vec: ( 2 5) ( 8 11 14) ( 9 12 15)
  4546. 2nd vec: (10 13) (16 19 22) (17 20 23)
  4547. 3rd vec: (18 21) ( 0 3 6) ( 1 4 7)
  4548. 2nd step:
  4549. shift right by 5 the concatenation of:
  4550. "1st vec" and "3rd vec"
  4551. ( 2 5) ( 8 11 14) |( 9 12 15) _ (18 21) ( 0 3 6)| ( 1 4 7)
  4552. "2nd vec" and "1st vec"
  4553. (10 13) (16 19 22) |(17 20 23) _ ( 2 5) ( 8 11 14)| ( 9 12 15)
  4554. "3rd vec" and "2nd vec"
  4555. (18 21) ( 0 3 6) |( 1 4 7) _ (10 13) (16 19 22)| (17 20 23)
  4556. | New vectors |
  4557. So that now new vectors are:
  4558. 1st vec: ( 9 12 15) (18 21) ( 0 3 6)
  4559. 2nd vec: (17 20 23) ( 2 5) ( 8 11 14)
  4560. 3rd vec: ( 1 4 7) (10 13) (16 19 22) READY
  4561. 3rd step:
  4562. shift right by 5 the concatenation of:
  4563. "1st vec" and "1st vec"
  4564. ( 9 12 15) (18 21) |( 0 3 6) _ ( 9 12 15) (18 21)| ( 0 3 6)
  4565. shift right by 3 the concatenation of:
  4566. "2nd vec" and "2nd vec"
  4567. (17 20 23) |( 2 5) ( 8 11 14) _ (17 20 23)| ( 2 5) ( 8 11 14)
  4568. | New vectors |
  4569. So that now all vectors are READY:
  4570. 1st vec: ( 0 3 6) ( 9 12 15) (18 21)
  4571. 2nd vec: ( 2 5) ( 8 11 14) (17 20 23)
  4572. 3rd vec: ( 1 4 7) (10 13) (16 19 22)
  4573. This algorithm is faster than one in vect_permute_load_chain if:
  4574. 1. "shift of a concatination" is faster than general permutation.
  4575. This is usually so.
  4576. 2. The TARGET machine can't execute vector instructions in parallel.
  4577. This is because each step of the algorithm depends on previous.
  4578. The algorithm in vect_permute_load_chain is much more parallel.
  4579. The algorithm is applicable only for LOAD CHAIN LENGTH less than VF.
  4580. */
  4581. static bool
  4582. vect_shift_permute_load_chain (vec<tree> dr_chain,
  4583. unsigned int length,
  4584. gimple stmt,
  4585. gimple_stmt_iterator *gsi,
  4586. vec<tree> *result_chain)
  4587. {
  4588. tree vect[3], vect_shift[3], data_ref, first_vect, second_vect;
  4589. tree perm2_mask1, perm2_mask2, perm3_mask;
  4590. tree select_mask, shift1_mask, shift2_mask, shift3_mask, shift4_mask;
  4591. gimple perm_stmt;
  4592. tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
  4593. unsigned int i;
  4594. unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
  4595. unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
  4596. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  4597. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  4598. result_chain->quick_grow (length);
  4599. memcpy (result_chain->address (), dr_chain.address (),
  4600. length * sizeof (tree));
  4601. if (exact_log2 (length) != -1 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 4)
  4602. {
  4603. unsigned int j, log_length = exact_log2 (length);
  4604. for (i = 0; i < nelt / 2; ++i)
  4605. sel[i] = i * 2;
  4606. for (i = 0; i < nelt / 2; ++i)
  4607. sel[nelt / 2 + i] = i * 2 + 1;
  4608. if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
  4609. {
  4610. if (dump_enabled_p ())
  4611. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4612. "shuffle of 2 fields structure is not \
  4613. supported by target\n");
  4614. return false;
  4615. }
  4616. perm2_mask1 = vect_gen_perm_mask_checked (vectype, sel);
  4617. for (i = 0; i < nelt / 2; ++i)
  4618. sel[i] = i * 2 + 1;
  4619. for (i = 0; i < nelt / 2; ++i)
  4620. sel[nelt / 2 + i] = i * 2;
  4621. if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
  4622. {
  4623. if (dump_enabled_p ())
  4624. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4625. "shuffle of 2 fields structure is not \
  4626. supported by target\n");
  4627. return false;
  4628. }
  4629. perm2_mask2 = vect_gen_perm_mask_checked (vectype, sel);
  4630. /* Generating permutation constant to shift all elements.
  4631. For vector length 8 it is {4 5 6 7 8 9 10 11}. */
  4632. for (i = 0; i < nelt; i++)
  4633. sel[i] = nelt / 2 + i;
  4634. if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
  4635. {
  4636. if (dump_enabled_p ())
  4637. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4638. "shift permutation is not supported by target\n");
  4639. return false;
  4640. }
  4641. shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
  4642. /* Generating permutation constant to select vector from 2.
  4643. For vector length 8 it is {0 1 2 3 12 13 14 15}. */
  4644. for (i = 0; i < nelt / 2; i++)
  4645. sel[i] = i;
  4646. for (i = nelt / 2; i < nelt; i++)
  4647. sel[i] = nelt + i;
  4648. if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
  4649. {
  4650. if (dump_enabled_p ())
  4651. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4652. "select is not supported by target\n");
  4653. return false;
  4654. }
  4655. select_mask = vect_gen_perm_mask_checked (vectype, sel);
  4656. for (i = 0; i < log_length; i++)
  4657. {
  4658. for (j = 0; j < length; j += 2)
  4659. {
  4660. first_vect = dr_chain[j];
  4661. second_vect = dr_chain[j + 1];
  4662. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
  4663. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
  4664. first_vect, first_vect,
  4665. perm2_mask1);
  4666. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4667. vect[0] = data_ref;
  4668. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle2");
  4669. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
  4670. second_vect, second_vect,
  4671. perm2_mask2);
  4672. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4673. vect[1] = data_ref;
  4674. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift");
  4675. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
  4676. vect[0], vect[1], shift1_mask);
  4677. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4678. (*result_chain)[j/2 + length/2] = data_ref;
  4679. data_ref = make_temp_ssa_name (vectype, NULL, "vect_select");
  4680. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
  4681. vect[0], vect[1], select_mask);
  4682. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4683. (*result_chain)[j/2] = data_ref;
  4684. }
  4685. memcpy (dr_chain.address (), result_chain->address (),
  4686. length * sizeof (tree));
  4687. }
  4688. return true;
  4689. }
  4690. if (length == 3 && LOOP_VINFO_VECT_FACTOR (loop_vinfo) > 2)
  4691. {
  4692. unsigned int k = 0, l = 0;
  4693. /* Generating permutation constant to get all elements in rigth order.
  4694. For vector length 8 it is {0 3 6 1 4 7 2 5}. */
  4695. for (i = 0; i < nelt; i++)
  4696. {
  4697. if (3 * k + (l % 3) >= nelt)
  4698. {
  4699. k = 0;
  4700. l += (3 - (nelt % 3));
  4701. }
  4702. sel[i] = 3 * k + (l % 3);
  4703. k++;
  4704. }
  4705. if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
  4706. {
  4707. if (dump_enabled_p ())
  4708. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4709. "shuffle of 3 fields structure is not \
  4710. supported by target\n");
  4711. return false;
  4712. }
  4713. perm3_mask = vect_gen_perm_mask_checked (vectype, sel);
  4714. /* Generating permutation constant to shift all elements.
  4715. For vector length 8 it is {6 7 8 9 10 11 12 13}. */
  4716. for (i = 0; i < nelt; i++)
  4717. sel[i] = 2 * (nelt / 3) + (nelt % 3) + i;
  4718. if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
  4719. {
  4720. if (dump_enabled_p ())
  4721. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4722. "shift permutation is not supported by target\n");
  4723. return false;
  4724. }
  4725. shift1_mask = vect_gen_perm_mask_checked (vectype, sel);
  4726. /* Generating permutation constant to shift all elements.
  4727. For vector length 8 it is {5 6 7 8 9 10 11 12}. */
  4728. for (i = 0; i < nelt; i++)
  4729. sel[i] = 2 * (nelt / 3) + 1 + i;
  4730. if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
  4731. {
  4732. if (dump_enabled_p ())
  4733. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4734. "shift permutation is not supported by target\n");
  4735. return false;
  4736. }
  4737. shift2_mask = vect_gen_perm_mask_checked (vectype, sel);
  4738. /* Generating permutation constant to shift all elements.
  4739. For vector length 8 it is {3 4 5 6 7 8 9 10}. */
  4740. for (i = 0; i < nelt; i++)
  4741. sel[i] = (nelt / 3) + (nelt % 3) / 2 + i;
  4742. if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
  4743. {
  4744. if (dump_enabled_p ())
  4745. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4746. "shift permutation is not supported by target\n");
  4747. return false;
  4748. }
  4749. shift3_mask = vect_gen_perm_mask_checked (vectype, sel);
  4750. /* Generating permutation constant to shift all elements.
  4751. For vector length 8 it is {5 6 7 8 9 10 11 12}. */
  4752. for (i = 0; i < nelt; i++)
  4753. sel[i] = 2 * (nelt / 3) + (nelt % 3) / 2 + i;
  4754. if (!can_vec_perm_p (TYPE_MODE (vectype), false, sel))
  4755. {
  4756. if (dump_enabled_p ())
  4757. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  4758. "shift permutation is not supported by target\n");
  4759. return false;
  4760. }
  4761. shift4_mask = vect_gen_perm_mask_checked (vectype, sel);
  4762. for (k = 0; k < 3; k++)
  4763. {
  4764. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shuffle3");
  4765. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
  4766. dr_chain[k], dr_chain[k],
  4767. perm3_mask);
  4768. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4769. vect[k] = data_ref;
  4770. }
  4771. for (k = 0; k < 3; k++)
  4772. {
  4773. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift1");
  4774. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
  4775. vect[k % 3], vect[(k + 1) % 3],
  4776. shift1_mask);
  4777. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4778. vect_shift[k] = data_ref;
  4779. }
  4780. for (k = 0; k < 3; k++)
  4781. {
  4782. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift2");
  4783. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR,
  4784. vect_shift[(4 - k) % 3],
  4785. vect_shift[(3 - k) % 3],
  4786. shift2_mask);
  4787. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4788. vect[k] = data_ref;
  4789. }
  4790. (*result_chain)[3 - (nelt % 3)] = vect[2];
  4791. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift3");
  4792. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[0],
  4793. vect[0], shift3_mask);
  4794. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4795. (*result_chain)[nelt % 3] = data_ref;
  4796. data_ref = make_temp_ssa_name (vectype, NULL, "vect_shift4");
  4797. perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, vect[1],
  4798. vect[1], shift4_mask);
  4799. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  4800. (*result_chain)[0] = data_ref;
  4801. return true;
  4802. }
  4803. return false;
  4804. }
  4805. /* Function vect_transform_grouped_load.
  4806. Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
  4807. to perform their permutation and ascribe the result vectorized statements to
  4808. the scalar statements.
  4809. */
  4810. void
  4811. vect_transform_grouped_load (gimple stmt, vec<tree> dr_chain, int size,
  4812. gimple_stmt_iterator *gsi)
  4813. {
  4814. machine_mode mode;
  4815. vec<tree> result_chain = vNULL;
  4816. /* DR_CHAIN contains input data-refs that are a part of the interleaving.
  4817. RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
  4818. vectors, that are ready for vector computation. */
  4819. result_chain.create (size);
  4820. /* If reassociation width for vector type is 2 or greater target machine can
  4821. execute 2 or more vector instructions in parallel. Otherwise try to
  4822. get chain for loads group using vect_shift_permute_load_chain. */
  4823. mode = TYPE_MODE (STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt)));
  4824. if (targetm.sched.reassociation_width (VEC_PERM_EXPR, mode) > 1
  4825. || exact_log2 (size) != -1
  4826. || !vect_shift_permute_load_chain (dr_chain, size, stmt,
  4827. gsi, &result_chain))
  4828. vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain);
  4829. vect_record_grouped_load_vectors (stmt, result_chain);
  4830. result_chain.release ();
  4831. }
  4832. /* RESULT_CHAIN contains the output of a group of grouped loads that were
  4833. generated as part of the vectorization of STMT. Assign the statement
  4834. for each vector to the associated scalar statement. */
  4835. void
  4836. vect_record_grouped_load_vectors (gimple stmt, vec<tree> result_chain)
  4837. {
  4838. gimple first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
  4839. gimple next_stmt, new_stmt;
  4840. unsigned int i, gap_count;
  4841. tree tmp_data_ref;
  4842. /* Put a permuted data-ref in the VECTORIZED_STMT field.
  4843. Since we scan the chain starting from it's first node, their order
  4844. corresponds the order of data-refs in RESULT_CHAIN. */
  4845. next_stmt = first_stmt;
  4846. gap_count = 1;
  4847. FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
  4848. {
  4849. if (!next_stmt)
  4850. break;
  4851. /* Skip the gaps. Loads created for the gaps will be removed by dead
  4852. code elimination pass later. No need to check for the first stmt in
  4853. the group, since it always exists.
  4854. GROUP_GAP is the number of steps in elements from the previous
  4855. access (if there is no gap GROUP_GAP is 1). We skip loads that
  4856. correspond to the gaps. */
  4857. if (next_stmt != first_stmt
  4858. && gap_count < GROUP_GAP (vinfo_for_stmt (next_stmt)))
  4859. {
  4860. gap_count++;
  4861. continue;
  4862. }
  4863. while (next_stmt)
  4864. {
  4865. new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
  4866. /* We assume that if VEC_STMT is not NULL, this is a case of multiple
  4867. copies, and we put the new vector statement in the first available
  4868. RELATED_STMT. */
  4869. if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
  4870. STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
  4871. else
  4872. {
  4873. if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
  4874. {
  4875. gimple prev_stmt =
  4876. STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
  4877. gimple rel_stmt =
  4878. STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
  4879. while (rel_stmt)
  4880. {
  4881. prev_stmt = rel_stmt;
  4882. rel_stmt =
  4883. STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
  4884. }
  4885. STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) =
  4886. new_stmt;
  4887. }
  4888. }
  4889. next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
  4890. gap_count = 1;
  4891. /* If NEXT_STMT accesses the same DR as the previous statement,
  4892. put the same TMP_DATA_REF as its vectorized statement; otherwise
  4893. get the next data-ref from RESULT_CHAIN. */
  4894. if (!next_stmt || !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
  4895. break;
  4896. }
  4897. }
  4898. }
  4899. /* Function vect_force_dr_alignment_p.
  4900. Returns whether the alignment of a DECL can be forced to be aligned
  4901. on ALIGNMENT bit boundary. */
  4902. bool
  4903. vect_can_force_dr_alignment_p (const_tree decl, unsigned int alignment)
  4904. {
  4905. if (TREE_CODE (decl) != VAR_DECL)
  4906. return false;
  4907. if (decl_in_symtab_p (decl)
  4908. && !symtab_node::get (decl)->can_increase_alignment_p ())
  4909. return false;
  4910. if (TREE_STATIC (decl))
  4911. return (alignment <= MAX_OFILE_ALIGNMENT);
  4912. else
  4913. return (alignment <= MAX_STACK_ALIGNMENT);
  4914. }
  4915. /* Return whether the data reference DR is supported with respect to its
  4916. alignment.
  4917. If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
  4918. it is aligned, i.e., check if it is possible to vectorize it with different
  4919. alignment. */
  4920. enum dr_alignment_support
  4921. vect_supportable_dr_alignment (struct data_reference *dr,
  4922. bool check_aligned_accesses)
  4923. {
  4924. gimple stmt = DR_STMT (dr);
  4925. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  4926. tree vectype = STMT_VINFO_VECTYPE (stmt_info);
  4927. machine_mode mode = TYPE_MODE (vectype);
  4928. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
  4929. struct loop *vect_loop = NULL;
  4930. bool nested_in_vect_loop = false;
  4931. if (aligned_access_p (dr) && !check_aligned_accesses)
  4932. return dr_aligned;
  4933. /* For now assume all conditional loads/stores support unaligned
  4934. access without any special code. */
  4935. if (is_gimple_call (stmt)
  4936. && gimple_call_internal_p (stmt)
  4937. && (gimple_call_internal_fn (stmt) == IFN_MASK_LOAD
  4938. || gimple_call_internal_fn (stmt) == IFN_MASK_STORE))
  4939. return dr_unaligned_supported;
  4940. if (loop_vinfo)
  4941. {
  4942. vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
  4943. nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt);
  4944. }
  4945. /* Possibly unaligned access. */
  4946. /* We can choose between using the implicit realignment scheme (generating
  4947. a misaligned_move stmt) and the explicit realignment scheme (generating
  4948. aligned loads with a REALIGN_LOAD). There are two variants to the
  4949. explicit realignment scheme: optimized, and unoptimized.
  4950. We can optimize the realignment only if the step between consecutive
  4951. vector loads is equal to the vector size. Since the vector memory
  4952. accesses advance in steps of VS (Vector Size) in the vectorized loop, it
  4953. is guaranteed that the misalignment amount remains the same throughout the
  4954. execution of the vectorized loop. Therefore, we can create the
  4955. "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
  4956. at the loop preheader.
  4957. However, in the case of outer-loop vectorization, when vectorizing a
  4958. memory access in the inner-loop nested within the LOOP that is now being
  4959. vectorized, while it is guaranteed that the misalignment of the
  4960. vectorized memory access will remain the same in different outer-loop
  4961. iterations, it is *not* guaranteed that is will remain the same throughout
  4962. the execution of the inner-loop. This is because the inner-loop advances
  4963. with the original scalar step (and not in steps of VS). If the inner-loop
  4964. step happens to be a multiple of VS, then the misalignment remains fixed
  4965. and we can use the optimized realignment scheme. For example:
  4966. for (i=0; i<N; i++)
  4967. for (j=0; j<M; j++)
  4968. s += a[i+j];
  4969. When vectorizing the i-loop in the above example, the step between
  4970. consecutive vector loads is 1, and so the misalignment does not remain
  4971. fixed across the execution of the inner-loop, and the realignment cannot
  4972. be optimized (as illustrated in the following pseudo vectorized loop):
  4973. for (i=0; i<N; i+=4)
  4974. for (j=0; j<M; j++){
  4975. vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
  4976. // when j is {0,1,2,3,4,5,6,7,...} respectively.
  4977. // (assuming that we start from an aligned address).
  4978. }
  4979. We therefore have to use the unoptimized realignment scheme:
  4980. for (i=0; i<N; i+=4)
  4981. for (j=k; j<M; j+=4)
  4982. vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
  4983. // that the misalignment of the initial address is
  4984. // 0).
  4985. The loop can then be vectorized as follows:
  4986. for (k=0; k<4; k++){
  4987. rt = get_realignment_token (&vp[k]);
  4988. for (i=0; i<N; i+=4){
  4989. v1 = vp[i+k];
  4990. for (j=k; j<M; j+=4){
  4991. v2 = vp[i+j+VS-1];
  4992. va = REALIGN_LOAD <v1,v2,rt>;
  4993. vs += va;
  4994. v1 = v2;
  4995. }
  4996. }
  4997. } */
  4998. if (DR_IS_READ (dr))
  4999. {
  5000. bool is_packed = false;
  5001. tree type = (TREE_TYPE (DR_REF (dr)));
  5002. if (optab_handler (vec_realign_load_optab, mode) != CODE_FOR_nothing
  5003. && (!targetm.vectorize.builtin_mask_for_load
  5004. || targetm.vectorize.builtin_mask_for_load ()))
  5005. {
  5006. tree vectype = STMT_VINFO_VECTYPE (stmt_info);
  5007. if ((nested_in_vect_loop
  5008. && (TREE_INT_CST_LOW (DR_STEP (dr))
  5009. != GET_MODE_SIZE (TYPE_MODE (vectype))))
  5010. || !loop_vinfo)
  5011. return dr_explicit_realign;
  5012. else
  5013. return dr_explicit_realign_optimized;
  5014. }
  5015. if (!known_alignment_for_access_p (dr))
  5016. is_packed = not_size_aligned (DR_REF (dr));
  5017. if ((TYPE_USER_ALIGN (type) && !is_packed)
  5018. || targetm.vectorize.
  5019. support_vector_misalignment (mode, type,
  5020. DR_MISALIGNMENT (dr), is_packed))
  5021. /* Can't software pipeline the loads, but can at least do them. */
  5022. return dr_unaligned_supported;
  5023. }
  5024. else
  5025. {
  5026. bool is_packed = false;
  5027. tree type = (TREE_TYPE (DR_REF (dr)));
  5028. if (!known_alignment_for_access_p (dr))
  5029. is_packed = not_size_aligned (DR_REF (dr));
  5030. if ((TYPE_USER_ALIGN (type) && !is_packed)
  5031. || targetm.vectorize.
  5032. support_vector_misalignment (mode, type,
  5033. DR_MISALIGNMENT (dr), is_packed))
  5034. return dr_unaligned_supported;
  5035. }
  5036. /* Unsupported. */
  5037. return dr_unaligned_unsupported;
  5038. }