tree-vect-slp.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433
  1. /* SLP - Basic Block Vectorization
  2. Copyright (C) 2007-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 "target.h"
  35. #include "predict.h"
  36. #include "hard-reg-set.h"
  37. #include "function.h"
  38. #include "basic-block.h"
  39. #include "gimple-pretty-print.h"
  40. #include "tree-ssa-alias.h"
  41. #include "internal-fn.h"
  42. #include "gimple-expr.h"
  43. #include "is-a.h"
  44. #include "gimple.h"
  45. #include "gimple-iterator.h"
  46. #include "gimple-ssa.h"
  47. #include "tree-phinodes.h"
  48. #include "ssa-iterators.h"
  49. #include "stringpool.h"
  50. #include "tree-ssanames.h"
  51. #include "tree-pass.h"
  52. #include "cfgloop.h"
  53. #include "hashtab.h"
  54. #include "rtl.h"
  55. #include "flags.h"
  56. #include "statistics.h"
  57. #include "real.h"
  58. #include "fixed-value.h"
  59. #include "insn-config.h"
  60. #include "expmed.h"
  61. #include "dojump.h"
  62. #include "explow.h"
  63. #include "calls.h"
  64. #include "emit-rtl.h"
  65. #include "varasm.h"
  66. #include "stmt.h"
  67. #include "expr.h"
  68. #include "recog.h" /* FIXME: for insn_data */
  69. #include "insn-codes.h"
  70. #include "optabs.h"
  71. #include "tree-vectorizer.h"
  72. #include "langhooks.h"
  73. #include "gimple-walk.h"
  74. /* Extract the location of the basic block in the source code.
  75. Return the basic block location if succeed and NULL if not. */
  76. source_location
  77. find_bb_location (basic_block bb)
  78. {
  79. gimple stmt = NULL;
  80. gimple_stmt_iterator si;
  81. if (!bb)
  82. return UNKNOWN_LOCATION;
  83. for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
  84. {
  85. stmt = gsi_stmt (si);
  86. if (gimple_location (stmt) != UNKNOWN_LOCATION)
  87. return gimple_location (stmt);
  88. }
  89. return UNKNOWN_LOCATION;
  90. }
  91. /* Recursively free the memory allocated for the SLP tree rooted at NODE. */
  92. static void
  93. vect_free_slp_tree (slp_tree node)
  94. {
  95. int i;
  96. slp_tree child;
  97. if (!node)
  98. return;
  99. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  100. vect_free_slp_tree (child);
  101. SLP_TREE_CHILDREN (node).release ();
  102. SLP_TREE_SCALAR_STMTS (node).release ();
  103. SLP_TREE_VEC_STMTS (node).release ();
  104. SLP_TREE_LOAD_PERMUTATION (node).release ();
  105. free (node);
  106. }
  107. /* Free the memory allocated for the SLP instance. */
  108. void
  109. vect_free_slp_instance (slp_instance instance)
  110. {
  111. vect_free_slp_tree (SLP_INSTANCE_TREE (instance));
  112. SLP_INSTANCE_LOADS (instance).release ();
  113. SLP_INSTANCE_BODY_COST_VEC (instance).release ();
  114. free (instance);
  115. }
  116. /* Create an SLP node for SCALAR_STMTS. */
  117. static slp_tree
  118. vect_create_new_slp_node (vec<gimple> scalar_stmts)
  119. {
  120. slp_tree node;
  121. gimple stmt = scalar_stmts[0];
  122. unsigned int nops;
  123. if (is_gimple_call (stmt))
  124. nops = gimple_call_num_args (stmt);
  125. else if (is_gimple_assign (stmt))
  126. {
  127. nops = gimple_num_ops (stmt) - 1;
  128. if (gimple_assign_rhs_code (stmt) == COND_EXPR)
  129. nops++;
  130. }
  131. else
  132. return NULL;
  133. node = XNEW (struct _slp_tree);
  134. SLP_TREE_SCALAR_STMTS (node) = scalar_stmts;
  135. SLP_TREE_VEC_STMTS (node).create (0);
  136. SLP_TREE_CHILDREN (node).create (nops);
  137. SLP_TREE_LOAD_PERMUTATION (node) = vNULL;
  138. return node;
  139. }
  140. /* Allocate operands info for NOPS operands, and GROUP_SIZE def-stmts for each
  141. operand. */
  142. static vec<slp_oprnd_info>
  143. vect_create_oprnd_info (int nops, int group_size)
  144. {
  145. int i;
  146. slp_oprnd_info oprnd_info;
  147. vec<slp_oprnd_info> oprnds_info;
  148. oprnds_info.create (nops);
  149. for (i = 0; i < nops; i++)
  150. {
  151. oprnd_info = XNEW (struct _slp_oprnd_info);
  152. oprnd_info->def_stmts.create (group_size);
  153. oprnd_info->first_dt = vect_uninitialized_def;
  154. oprnd_info->first_op_type = NULL_TREE;
  155. oprnd_info->first_pattern = false;
  156. oprnds_info.quick_push (oprnd_info);
  157. }
  158. return oprnds_info;
  159. }
  160. /* Free operands info. */
  161. static void
  162. vect_free_oprnd_info (vec<slp_oprnd_info> &oprnds_info)
  163. {
  164. int i;
  165. slp_oprnd_info oprnd_info;
  166. FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
  167. {
  168. oprnd_info->def_stmts.release ();
  169. XDELETE (oprnd_info);
  170. }
  171. oprnds_info.release ();
  172. }
  173. /* Find the place of the data-ref in STMT in the interleaving chain that starts
  174. from FIRST_STMT. Return -1 if the data-ref is not a part of the chain. */
  175. static int
  176. vect_get_place_in_interleaving_chain (gimple stmt, gimple first_stmt)
  177. {
  178. gimple next_stmt = first_stmt;
  179. int result = 0;
  180. if (first_stmt != GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
  181. return -1;
  182. do
  183. {
  184. if (next_stmt == stmt)
  185. return result;
  186. result++;
  187. next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
  188. }
  189. while (next_stmt);
  190. return -1;
  191. }
  192. /* Get the defs for the rhs of STMT (collect them in OPRNDS_INFO), check that
  193. they are of a valid type and that they match the defs of the first stmt of
  194. the SLP group (stored in OPRNDS_INFO). If there was a fatal error
  195. return -1, if the error could be corrected by swapping operands of the
  196. operation return 1, if everything is ok return 0. */
  197. static int
  198. vect_get_and_check_slp_defs (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
  199. gimple stmt, bool first,
  200. vec<slp_oprnd_info> *oprnds_info)
  201. {
  202. tree oprnd;
  203. unsigned int i, number_of_oprnds;
  204. tree def;
  205. gimple def_stmt;
  206. enum vect_def_type dt = vect_uninitialized_def;
  207. struct loop *loop = NULL;
  208. bool pattern = false;
  209. slp_oprnd_info oprnd_info;
  210. int first_op_idx = 1;
  211. bool commutative = false;
  212. bool first_op_cond = false;
  213. if (loop_vinfo)
  214. loop = LOOP_VINFO_LOOP (loop_vinfo);
  215. if (is_gimple_call (stmt))
  216. {
  217. number_of_oprnds = gimple_call_num_args (stmt);
  218. first_op_idx = 3;
  219. }
  220. else if (is_gimple_assign (stmt))
  221. {
  222. enum tree_code code = gimple_assign_rhs_code (stmt);
  223. number_of_oprnds = gimple_num_ops (stmt) - 1;
  224. if (gimple_assign_rhs_code (stmt) == COND_EXPR)
  225. {
  226. first_op_cond = true;
  227. commutative = true;
  228. number_of_oprnds++;
  229. }
  230. else
  231. commutative = commutative_tree_code (code);
  232. }
  233. else
  234. return -1;
  235. bool swapped = false;
  236. for (i = 0; i < number_of_oprnds; i++)
  237. {
  238. again:
  239. if (first_op_cond)
  240. {
  241. if (i == 0 || i == 1)
  242. oprnd = TREE_OPERAND (gimple_op (stmt, first_op_idx),
  243. swapped ? !i : i);
  244. else
  245. oprnd = gimple_op (stmt, first_op_idx + i - 1);
  246. }
  247. else
  248. oprnd = gimple_op (stmt, first_op_idx + (swapped ? !i : i));
  249. oprnd_info = (*oprnds_info)[i];
  250. if (!vect_is_simple_use (oprnd, NULL, loop_vinfo, bb_vinfo, &def_stmt,
  251. &def, &dt)
  252. || (!def_stmt && dt != vect_constant_def))
  253. {
  254. if (dump_enabled_p ())
  255. {
  256. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  257. "Build SLP failed: can't find def for ");
  258. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
  259. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  260. }
  261. return -1;
  262. }
  263. /* Check if DEF_STMT is a part of a pattern in LOOP and get the def stmt
  264. from the pattern. Check that all the stmts of the node are in the
  265. pattern. */
  266. if (def_stmt && gimple_bb (def_stmt)
  267. && ((loop && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
  268. || (!loop && gimple_bb (def_stmt) == BB_VINFO_BB (bb_vinfo)
  269. && gimple_code (def_stmt) != GIMPLE_PHI))
  270. && vinfo_for_stmt (def_stmt)
  271. && STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (def_stmt))
  272. && !STMT_VINFO_RELEVANT (vinfo_for_stmt (def_stmt))
  273. && !STMT_VINFO_LIVE_P (vinfo_for_stmt (def_stmt)))
  274. {
  275. pattern = true;
  276. if (!first && !oprnd_info->first_pattern)
  277. {
  278. if (i == 0
  279. && !swapped
  280. && commutative)
  281. {
  282. swapped = true;
  283. goto again;
  284. }
  285. if (dump_enabled_p ())
  286. {
  287. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  288. "Build SLP failed: some of the stmts"
  289. " are in a pattern, and others are not ");
  290. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, oprnd);
  291. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  292. }
  293. return 1;
  294. }
  295. def_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (def_stmt));
  296. dt = STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt));
  297. if (dt == vect_unknown_def_type)
  298. {
  299. if (dump_enabled_p ())
  300. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  301. "Unsupported pattern.\n");
  302. return -1;
  303. }
  304. switch (gimple_code (def_stmt))
  305. {
  306. case GIMPLE_PHI:
  307. def = gimple_phi_result (def_stmt);
  308. break;
  309. case GIMPLE_ASSIGN:
  310. def = gimple_assign_lhs (def_stmt);
  311. break;
  312. default:
  313. if (dump_enabled_p ())
  314. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  315. "unsupported defining stmt:\n");
  316. return -1;
  317. }
  318. }
  319. if (first)
  320. {
  321. oprnd_info->first_dt = dt;
  322. oprnd_info->first_pattern = pattern;
  323. oprnd_info->first_op_type = TREE_TYPE (oprnd);
  324. }
  325. else
  326. {
  327. /* Not first stmt of the group, check that the def-stmt/s match
  328. the def-stmt/s of the first stmt. Allow different definition
  329. types for reduction chains: the first stmt must be a
  330. vect_reduction_def (a phi node), and the rest
  331. vect_internal_def. */
  332. if (((oprnd_info->first_dt != dt
  333. && !(oprnd_info->first_dt == vect_reduction_def
  334. && dt == vect_internal_def)
  335. && !((oprnd_info->first_dt == vect_external_def
  336. || oprnd_info->first_dt == vect_constant_def)
  337. && (dt == vect_external_def
  338. || dt == vect_constant_def)))
  339. || !types_compatible_p (oprnd_info->first_op_type,
  340. TREE_TYPE (oprnd))))
  341. {
  342. /* Try swapping operands if we got a mismatch. */
  343. if (i == 0
  344. && !swapped
  345. && commutative)
  346. {
  347. swapped = true;
  348. goto again;
  349. }
  350. if (dump_enabled_p ())
  351. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  352. "Build SLP failed: different types\n");
  353. return 1;
  354. }
  355. }
  356. /* Check the types of the definitions. */
  357. switch (dt)
  358. {
  359. case vect_constant_def:
  360. case vect_external_def:
  361. case vect_reduction_def:
  362. break;
  363. case vect_internal_def:
  364. oprnd_info->def_stmts.quick_push (def_stmt);
  365. break;
  366. default:
  367. /* FORNOW: Not supported. */
  368. if (dump_enabled_p ())
  369. {
  370. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  371. "Build SLP failed: illegal type of def ");
  372. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, def);
  373. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  374. }
  375. return -1;
  376. }
  377. }
  378. /* Swap operands. */
  379. if (swapped)
  380. {
  381. if (first_op_cond)
  382. {
  383. tree cond = gimple_assign_rhs1 (stmt);
  384. swap_ssa_operands (stmt, &TREE_OPERAND (cond, 0),
  385. &TREE_OPERAND (cond, 1));
  386. TREE_SET_CODE (cond, swap_tree_comparison (TREE_CODE (cond)));
  387. }
  388. else
  389. swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
  390. gimple_assign_rhs2_ptr (stmt));
  391. }
  392. return 0;
  393. }
  394. /* Verify if the scalar stmts STMTS are isomorphic, require data
  395. permutation or are of unsupported types of operation. Return
  396. true if they are, otherwise return false and indicate in *MATCHES
  397. which stmts are not isomorphic to the first one. If MATCHES[0]
  398. is false then this indicates the comparison could not be
  399. carried out or the stmts will never be vectorized by SLP. */
  400. static bool
  401. vect_build_slp_tree_1 (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
  402. vec<gimple> stmts, unsigned int group_size,
  403. unsigned nops, unsigned int *max_nunits,
  404. unsigned int vectorization_factor, bool *matches)
  405. {
  406. unsigned int i;
  407. gimple stmt = stmts[0];
  408. enum tree_code first_stmt_code = ERROR_MARK, rhs_code = ERROR_MARK;
  409. enum tree_code first_cond_code = ERROR_MARK;
  410. tree lhs;
  411. bool need_same_oprnds = false;
  412. tree vectype, scalar_type, first_op1 = NULL_TREE;
  413. optab optab;
  414. int icode;
  415. machine_mode optab_op2_mode;
  416. machine_mode vec_mode;
  417. struct data_reference *first_dr;
  418. HOST_WIDE_INT dummy;
  419. gimple first_load = NULL, prev_first_load = NULL, old_first_load = NULL;
  420. tree cond;
  421. /* For every stmt in NODE find its def stmt/s. */
  422. FOR_EACH_VEC_ELT (stmts, i, stmt)
  423. {
  424. matches[i] = false;
  425. if (dump_enabled_p ())
  426. {
  427. dump_printf_loc (MSG_NOTE, vect_location, "Build SLP for ");
  428. dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
  429. dump_printf (MSG_NOTE, "\n");
  430. }
  431. /* Fail to vectorize statements marked as unvectorizable. */
  432. if (!STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)))
  433. {
  434. if (dump_enabled_p ())
  435. {
  436. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  437. "Build SLP failed: unvectorizable statement ");
  438. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  439. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  440. }
  441. /* Fatal mismatch. */
  442. matches[0] = false;
  443. return false;
  444. }
  445. lhs = gimple_get_lhs (stmt);
  446. if (lhs == NULL_TREE)
  447. {
  448. if (dump_enabled_p ())
  449. {
  450. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  451. "Build SLP failed: not GIMPLE_ASSIGN nor "
  452. "GIMPLE_CALL ");
  453. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  454. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  455. }
  456. /* Fatal mismatch. */
  457. matches[0] = false;
  458. return false;
  459. }
  460. if (is_gimple_assign (stmt)
  461. && gimple_assign_rhs_code (stmt) == COND_EXPR
  462. && (cond = gimple_assign_rhs1 (stmt))
  463. && !COMPARISON_CLASS_P (cond))
  464. {
  465. if (dump_enabled_p ())
  466. {
  467. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  468. "Build SLP failed: condition is not "
  469. "comparison ");
  470. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  471. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  472. }
  473. /* Fatal mismatch. */
  474. matches[0] = false;
  475. return false;
  476. }
  477. scalar_type = vect_get_smallest_scalar_type (stmt, &dummy, &dummy);
  478. vectype = get_vectype_for_scalar_type (scalar_type);
  479. if (!vectype)
  480. {
  481. if (dump_enabled_p ())
  482. {
  483. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  484. "Build SLP failed: unsupported data-type ");
  485. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  486. scalar_type);
  487. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  488. }
  489. /* Fatal mismatch. */
  490. matches[0] = false;
  491. return false;
  492. }
  493. /* In case of multiple types we need to detect the smallest type. */
  494. if (*max_nunits < TYPE_VECTOR_SUBPARTS (vectype))
  495. {
  496. *max_nunits = TYPE_VECTOR_SUBPARTS (vectype);
  497. if (bb_vinfo)
  498. vectorization_factor = *max_nunits;
  499. }
  500. if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
  501. {
  502. rhs_code = CALL_EXPR;
  503. if (gimple_call_internal_p (call_stmt)
  504. || gimple_call_tail_p (call_stmt)
  505. || gimple_call_noreturn_p (call_stmt)
  506. || !gimple_call_nothrow_p (call_stmt)
  507. || gimple_call_chain (call_stmt))
  508. {
  509. if (dump_enabled_p ())
  510. {
  511. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  512. "Build SLP failed: unsupported call type ");
  513. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  514. call_stmt, 0);
  515. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  516. }
  517. /* Fatal mismatch. */
  518. matches[0] = false;
  519. return false;
  520. }
  521. }
  522. else
  523. rhs_code = gimple_assign_rhs_code (stmt);
  524. /* Check the operation. */
  525. if (i == 0)
  526. {
  527. first_stmt_code = rhs_code;
  528. /* Shift arguments should be equal in all the packed stmts for a
  529. vector shift with scalar shift operand. */
  530. if (rhs_code == LSHIFT_EXPR || rhs_code == RSHIFT_EXPR
  531. || rhs_code == LROTATE_EXPR
  532. || rhs_code == RROTATE_EXPR)
  533. {
  534. vec_mode = TYPE_MODE (vectype);
  535. /* First see if we have a vector/vector shift. */
  536. optab = optab_for_tree_code (rhs_code, vectype,
  537. optab_vector);
  538. if (!optab
  539. || optab_handler (optab, vec_mode) == CODE_FOR_nothing)
  540. {
  541. /* No vector/vector shift, try for a vector/scalar shift. */
  542. optab = optab_for_tree_code (rhs_code, vectype,
  543. optab_scalar);
  544. if (!optab)
  545. {
  546. if (dump_enabled_p ())
  547. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  548. "Build SLP failed: no optab.\n");
  549. /* Fatal mismatch. */
  550. matches[0] = false;
  551. return false;
  552. }
  553. icode = (int) optab_handler (optab, vec_mode);
  554. if (icode == CODE_FOR_nothing)
  555. {
  556. if (dump_enabled_p ())
  557. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  558. "Build SLP failed: "
  559. "op not supported by target.\n");
  560. /* Fatal mismatch. */
  561. matches[0] = false;
  562. return false;
  563. }
  564. optab_op2_mode = insn_data[icode].operand[2].mode;
  565. if (!VECTOR_MODE_P (optab_op2_mode))
  566. {
  567. need_same_oprnds = true;
  568. first_op1 = gimple_assign_rhs2 (stmt);
  569. }
  570. }
  571. }
  572. else if (rhs_code == WIDEN_LSHIFT_EXPR)
  573. {
  574. need_same_oprnds = true;
  575. first_op1 = gimple_assign_rhs2 (stmt);
  576. }
  577. }
  578. else
  579. {
  580. if (first_stmt_code != rhs_code
  581. && (first_stmt_code != IMAGPART_EXPR
  582. || rhs_code != REALPART_EXPR)
  583. && (first_stmt_code != REALPART_EXPR
  584. || rhs_code != IMAGPART_EXPR)
  585. && !(STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
  586. && (first_stmt_code == ARRAY_REF
  587. || first_stmt_code == BIT_FIELD_REF
  588. || first_stmt_code == INDIRECT_REF
  589. || first_stmt_code == COMPONENT_REF
  590. || first_stmt_code == MEM_REF)))
  591. {
  592. if (dump_enabled_p ())
  593. {
  594. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  595. "Build SLP failed: different operation "
  596. "in stmt ");
  597. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  598. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  599. }
  600. /* Mismatch. */
  601. continue;
  602. }
  603. if (need_same_oprnds
  604. && !operand_equal_p (first_op1, gimple_assign_rhs2 (stmt), 0))
  605. {
  606. if (dump_enabled_p ())
  607. {
  608. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  609. "Build SLP failed: different shift "
  610. "arguments in ");
  611. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  612. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  613. }
  614. /* Mismatch. */
  615. continue;
  616. }
  617. if (rhs_code == CALL_EXPR)
  618. {
  619. gimple first_stmt = stmts[0];
  620. if (gimple_call_num_args (stmt) != nops
  621. || !operand_equal_p (gimple_call_fn (first_stmt),
  622. gimple_call_fn (stmt), 0)
  623. || gimple_call_fntype (first_stmt)
  624. != gimple_call_fntype (stmt))
  625. {
  626. if (dump_enabled_p ())
  627. {
  628. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  629. "Build SLP failed: different calls in ");
  630. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  631. stmt, 0);
  632. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  633. }
  634. /* Mismatch. */
  635. continue;
  636. }
  637. }
  638. }
  639. /* Grouped store or load. */
  640. if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt)))
  641. {
  642. if (REFERENCE_CLASS_P (lhs))
  643. {
  644. /* Store. */
  645. ;
  646. }
  647. else
  648. {
  649. /* Load. */
  650. unsigned unrolling_factor
  651. = least_common_multiple
  652. (*max_nunits, group_size) / group_size;
  653. /* FORNOW: Check that there is no gap between the loads
  654. and no gap between the groups when we need to load
  655. multiple groups at once.
  656. ??? We should enhance this to only disallow gaps
  657. inside vectors. */
  658. if ((unrolling_factor > 1
  659. && ((GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt
  660. && GROUP_GAP (vinfo_for_stmt (stmt)) != 0)
  661. /* If the group is split up then GROUP_GAP
  662. isn't correct here, nor is GROUP_FIRST_ELEMENT. */
  663. || GROUP_SIZE (vinfo_for_stmt (stmt)) > group_size))
  664. || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) != stmt
  665. && GROUP_GAP (vinfo_for_stmt (stmt)) != 1))
  666. {
  667. if (dump_enabled_p ())
  668. {
  669. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  670. "Build SLP failed: grouped "
  671. "loads have gaps ");
  672. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  673. stmt, 0);
  674. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  675. }
  676. /* Fatal mismatch. */
  677. matches[0] = false;
  678. return false;
  679. }
  680. /* Check that the size of interleaved loads group is not
  681. greater than the SLP group size. */
  682. unsigned ncopies
  683. = vectorization_factor / TYPE_VECTOR_SUBPARTS (vectype);
  684. if (loop_vinfo
  685. && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt
  686. && ((GROUP_SIZE (vinfo_for_stmt (stmt))
  687. - GROUP_GAP (vinfo_for_stmt (stmt)))
  688. > ncopies * group_size))
  689. {
  690. if (dump_enabled_p ())
  691. {
  692. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  693. "Build SLP failed: the number "
  694. "of interleaved loads is greater than "
  695. "the SLP group size ");
  696. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  697. stmt, 0);
  698. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  699. }
  700. /* Fatal mismatch. */
  701. matches[0] = false;
  702. return false;
  703. }
  704. old_first_load = first_load;
  705. first_load = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
  706. if (prev_first_load)
  707. {
  708. /* Check that there are no loads from different interleaving
  709. chains in the same node. */
  710. if (prev_first_load != first_load)
  711. {
  712. if (dump_enabled_p ())
  713. {
  714. dump_printf_loc (MSG_MISSED_OPTIMIZATION,
  715. vect_location,
  716. "Build SLP failed: different "
  717. "interleaving chains in one node ");
  718. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  719. stmt, 0);
  720. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  721. }
  722. /* Mismatch. */
  723. continue;
  724. }
  725. }
  726. else
  727. prev_first_load = first_load;
  728. /* In some cases a group of loads is just the same load
  729. repeated N times. Only analyze its cost once. */
  730. if (first_load == stmt && old_first_load != first_load)
  731. {
  732. first_dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
  733. if (vect_supportable_dr_alignment (first_dr, false)
  734. == dr_unaligned_unsupported)
  735. {
  736. if (dump_enabled_p ())
  737. {
  738. dump_printf_loc (MSG_MISSED_OPTIMIZATION,
  739. vect_location,
  740. "Build SLP failed: unsupported "
  741. "unaligned load ");
  742. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  743. stmt, 0);
  744. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  745. }
  746. /* Fatal mismatch. */
  747. matches[0] = false;
  748. return false;
  749. }
  750. }
  751. }
  752. } /* Grouped access. */
  753. else
  754. {
  755. if (TREE_CODE_CLASS (rhs_code) == tcc_reference)
  756. {
  757. /* Not grouped load. */
  758. if (dump_enabled_p ())
  759. {
  760. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  761. "Build SLP failed: not grouped load ");
  762. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  763. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  764. }
  765. /* FORNOW: Not grouped loads are not supported. */
  766. /* Fatal mismatch. */
  767. matches[0] = false;
  768. return false;
  769. }
  770. /* Not memory operation. */
  771. if (TREE_CODE_CLASS (rhs_code) != tcc_binary
  772. && TREE_CODE_CLASS (rhs_code) != tcc_unary
  773. && rhs_code != COND_EXPR
  774. && rhs_code != CALL_EXPR)
  775. {
  776. if (dump_enabled_p ())
  777. {
  778. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  779. "Build SLP failed: operation");
  780. dump_printf (MSG_MISSED_OPTIMIZATION, " unsupported ");
  781. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  782. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  783. }
  784. /* Fatal mismatch. */
  785. matches[0] = false;
  786. return false;
  787. }
  788. if (rhs_code == COND_EXPR)
  789. {
  790. tree cond_expr = gimple_assign_rhs1 (stmt);
  791. if (i == 0)
  792. first_cond_code = TREE_CODE (cond_expr);
  793. else if (first_cond_code != TREE_CODE (cond_expr))
  794. {
  795. if (dump_enabled_p ())
  796. {
  797. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  798. "Build SLP failed: different"
  799. " operation");
  800. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  801. stmt, 0);
  802. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  803. }
  804. /* Mismatch. */
  805. continue;
  806. }
  807. }
  808. }
  809. matches[i] = true;
  810. }
  811. for (i = 0; i < group_size; ++i)
  812. if (!matches[i])
  813. return false;
  814. return true;
  815. }
  816. /* Recursively build an SLP tree starting from NODE.
  817. Fail (and return a value not equal to zero) if def-stmts are not
  818. isomorphic, require data permutation or are of unsupported types of
  819. operation. Otherwise, return 0.
  820. The value returned is the depth in the SLP tree where a mismatch
  821. was found. */
  822. static bool
  823. vect_build_slp_tree (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
  824. slp_tree *node, unsigned int group_size,
  825. unsigned int *max_nunits,
  826. vec<slp_tree> *loads,
  827. unsigned int vectorization_factor,
  828. bool *matches, unsigned *npermutes, unsigned *tree_size,
  829. unsigned max_tree_size)
  830. {
  831. unsigned nops, i, this_tree_size = 0;
  832. gimple stmt;
  833. matches[0] = false;
  834. stmt = SLP_TREE_SCALAR_STMTS (*node)[0];
  835. if (is_gimple_call (stmt))
  836. nops = gimple_call_num_args (stmt);
  837. else if (is_gimple_assign (stmt))
  838. {
  839. nops = gimple_num_ops (stmt) - 1;
  840. if (gimple_assign_rhs_code (stmt) == COND_EXPR)
  841. nops++;
  842. }
  843. else
  844. return false;
  845. if (!vect_build_slp_tree_1 (loop_vinfo, bb_vinfo,
  846. SLP_TREE_SCALAR_STMTS (*node), group_size, nops,
  847. max_nunits, vectorization_factor, matches))
  848. return false;
  849. /* If the SLP node is a load, terminate the recursion. */
  850. if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (stmt))
  851. && DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))))
  852. {
  853. loads->safe_push (*node);
  854. return true;
  855. }
  856. /* Get at the operands, verifying they are compatible. */
  857. vec<slp_oprnd_info> oprnds_info = vect_create_oprnd_info (nops, group_size);
  858. slp_oprnd_info oprnd_info;
  859. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (*node), i, stmt)
  860. {
  861. switch (vect_get_and_check_slp_defs (loop_vinfo, bb_vinfo,
  862. stmt, (i == 0), &oprnds_info))
  863. {
  864. case 0:
  865. break;
  866. case -1:
  867. matches[0] = false;
  868. vect_free_oprnd_info (oprnds_info);
  869. return false;
  870. case 1:
  871. matches[i] = false;
  872. break;
  873. }
  874. }
  875. for (i = 0; i < group_size; ++i)
  876. if (!matches[i])
  877. {
  878. vect_free_oprnd_info (oprnds_info);
  879. return false;
  880. }
  881. stmt = SLP_TREE_SCALAR_STMTS (*node)[0];
  882. /* Create SLP_TREE nodes for the definition node/s. */
  883. FOR_EACH_VEC_ELT (oprnds_info, i, oprnd_info)
  884. {
  885. slp_tree child;
  886. unsigned old_nloads = loads->length ();
  887. unsigned old_max_nunits = *max_nunits;
  888. if (oprnd_info->first_dt != vect_internal_def)
  889. continue;
  890. if (++this_tree_size > max_tree_size)
  891. {
  892. vect_free_oprnd_info (oprnds_info);
  893. return false;
  894. }
  895. child = vect_create_new_slp_node (oprnd_info->def_stmts);
  896. if (!child)
  897. {
  898. vect_free_oprnd_info (oprnds_info);
  899. return false;
  900. }
  901. if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &child,
  902. group_size, max_nunits, loads,
  903. vectorization_factor, matches,
  904. npermutes, &this_tree_size, max_tree_size))
  905. {
  906. oprnd_info->def_stmts = vNULL;
  907. SLP_TREE_CHILDREN (*node).quick_push (child);
  908. continue;
  909. }
  910. /* If the SLP build for operand zero failed and operand zero
  911. and one can be commutated try that for the scalar stmts
  912. that failed the match. */
  913. if (i == 0
  914. /* A first scalar stmt mismatch signals a fatal mismatch. */
  915. && matches[0]
  916. /* ??? For COND_EXPRs we can swap the comparison operands
  917. as well as the arms under some constraints. */
  918. && nops == 2
  919. && oprnds_info[1]->first_dt == vect_internal_def
  920. && is_gimple_assign (stmt)
  921. && commutative_tree_code (gimple_assign_rhs_code (stmt))
  922. /* Do so only if the number of not successful permutes was nor more
  923. than a cut-ff as re-trying the recursive match on
  924. possibly each level of the tree would expose exponential
  925. behavior. */
  926. && *npermutes < 4)
  927. {
  928. unsigned int j;
  929. slp_tree grandchild;
  930. /* Roll back. */
  931. *max_nunits = old_max_nunits;
  932. loads->truncate (old_nloads);
  933. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (child), j, grandchild)
  934. vect_free_slp_tree (grandchild);
  935. SLP_TREE_CHILDREN (child).truncate (0);
  936. /* Swap mismatched definition stmts. */
  937. dump_printf_loc (MSG_NOTE, vect_location,
  938. "Re-trying with swapped operands of stmts ");
  939. for (j = 0; j < group_size; ++j)
  940. if (!matches[j])
  941. {
  942. gimple tem = oprnds_info[0]->def_stmts[j];
  943. oprnds_info[0]->def_stmts[j] = oprnds_info[1]->def_stmts[j];
  944. oprnds_info[1]->def_stmts[j] = tem;
  945. dump_printf (MSG_NOTE, "%d ", j);
  946. }
  947. dump_printf (MSG_NOTE, "\n");
  948. /* And try again ... */
  949. if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &child,
  950. group_size, max_nunits, loads,
  951. vectorization_factor,
  952. matches, npermutes, &this_tree_size,
  953. max_tree_size))
  954. {
  955. oprnd_info->def_stmts = vNULL;
  956. SLP_TREE_CHILDREN (*node).quick_push (child);
  957. continue;
  958. }
  959. ++*npermutes;
  960. }
  961. oprnd_info->def_stmts = vNULL;
  962. vect_free_slp_tree (child);
  963. vect_free_oprnd_info (oprnds_info);
  964. return false;
  965. }
  966. if (tree_size)
  967. *tree_size += this_tree_size;
  968. vect_free_oprnd_info (oprnds_info);
  969. return true;
  970. }
  971. /* Dump a slp tree NODE using flags specified in DUMP_KIND. */
  972. static void
  973. vect_print_slp_tree (int dump_kind, slp_tree node)
  974. {
  975. int i;
  976. gimple stmt;
  977. slp_tree child;
  978. if (!node)
  979. return;
  980. dump_printf (dump_kind, "node ");
  981. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
  982. {
  983. dump_printf (dump_kind, "\n\tstmt %d ", i);
  984. dump_gimple_stmt (dump_kind, TDF_SLIM, stmt, 0);
  985. }
  986. dump_printf (dump_kind, "\n");
  987. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  988. vect_print_slp_tree (dump_kind, child);
  989. }
  990. /* Mark the tree rooted at NODE with MARK (PURE_SLP or HYBRID).
  991. If MARK is HYBRID, it refers to a specific stmt in NODE (the stmt at index
  992. J). Otherwise, MARK is PURE_SLP and J is -1, which indicates that all the
  993. stmts in NODE are to be marked. */
  994. static void
  995. vect_mark_slp_stmts (slp_tree node, enum slp_vect_type mark, int j)
  996. {
  997. int i;
  998. gimple stmt;
  999. slp_tree child;
  1000. if (!node)
  1001. return;
  1002. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
  1003. if (j < 0 || i == j)
  1004. STMT_SLP_TYPE (vinfo_for_stmt (stmt)) = mark;
  1005. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  1006. vect_mark_slp_stmts (child, mark, j);
  1007. }
  1008. /* Mark the statements of the tree rooted at NODE as relevant (vect_used). */
  1009. static void
  1010. vect_mark_slp_stmts_relevant (slp_tree node)
  1011. {
  1012. int i;
  1013. gimple stmt;
  1014. stmt_vec_info stmt_info;
  1015. slp_tree child;
  1016. if (!node)
  1017. return;
  1018. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
  1019. {
  1020. stmt_info = vinfo_for_stmt (stmt);
  1021. gcc_assert (!STMT_VINFO_RELEVANT (stmt_info)
  1022. || STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope);
  1023. STMT_VINFO_RELEVANT (stmt_info) = vect_used_in_scope;
  1024. }
  1025. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  1026. vect_mark_slp_stmts_relevant (child);
  1027. }
  1028. /* Rearrange the statements of NODE according to PERMUTATION. */
  1029. static void
  1030. vect_slp_rearrange_stmts (slp_tree node, unsigned int group_size,
  1031. vec<unsigned> permutation)
  1032. {
  1033. gimple stmt;
  1034. vec<gimple> tmp_stmts;
  1035. unsigned int i;
  1036. slp_tree child;
  1037. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  1038. vect_slp_rearrange_stmts (child, group_size, permutation);
  1039. gcc_assert (group_size == SLP_TREE_SCALAR_STMTS (node).length ());
  1040. tmp_stmts.create (group_size);
  1041. tmp_stmts.quick_grow_cleared (group_size);
  1042. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
  1043. tmp_stmts[permutation[i]] = stmt;
  1044. SLP_TREE_SCALAR_STMTS (node).release ();
  1045. SLP_TREE_SCALAR_STMTS (node) = tmp_stmts;
  1046. }
  1047. /* Check if the required load permutations in the SLP instance
  1048. SLP_INSTN are supported. */
  1049. static bool
  1050. vect_supported_load_permutation_p (slp_instance slp_instn)
  1051. {
  1052. unsigned int group_size = SLP_INSTANCE_GROUP_SIZE (slp_instn);
  1053. unsigned int i, j, k, next;
  1054. sbitmap load_index;
  1055. slp_tree node;
  1056. gimple stmt, load, next_load, first_load;
  1057. struct data_reference *dr;
  1058. if (dump_enabled_p ())
  1059. {
  1060. dump_printf_loc (MSG_NOTE, vect_location, "Load permutation ");
  1061. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
  1062. if (node->load_permutation.exists ())
  1063. FOR_EACH_VEC_ELT (node->load_permutation, j, next)
  1064. dump_printf (MSG_NOTE, "%d ", next);
  1065. else
  1066. for (k = 0; k < group_size; ++k)
  1067. dump_printf (MSG_NOTE, "%d ", k);
  1068. dump_printf (MSG_NOTE, "\n");
  1069. }
  1070. /* In case of reduction every load permutation is allowed, since the order
  1071. of the reduction statements is not important (as opposed to the case of
  1072. grouped stores). The only condition we need to check is that all the
  1073. load nodes are of the same size and have the same permutation (and then
  1074. rearrange all the nodes of the SLP instance according to this
  1075. permutation). */
  1076. /* Check that all the load nodes are of the same size. */
  1077. /* ??? Can't we assert this? */
  1078. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
  1079. if (SLP_TREE_SCALAR_STMTS (node).length () != (unsigned) group_size)
  1080. return false;
  1081. node = SLP_INSTANCE_TREE (slp_instn);
  1082. stmt = SLP_TREE_SCALAR_STMTS (node)[0];
  1083. /* Reduction (there are no data-refs in the root).
  1084. In reduction chain the order of the loads is important. */
  1085. if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))
  1086. && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
  1087. {
  1088. slp_tree load;
  1089. unsigned int lidx;
  1090. /* Compare all the permutation sequences to the first one. We know
  1091. that at least one load is permuted. */
  1092. node = SLP_INSTANCE_LOADS (slp_instn)[0];
  1093. if (!node->load_permutation.exists ())
  1094. return false;
  1095. for (i = 1; SLP_INSTANCE_LOADS (slp_instn).iterate (i, &load); ++i)
  1096. {
  1097. if (!load->load_permutation.exists ())
  1098. return false;
  1099. FOR_EACH_VEC_ELT (load->load_permutation, j, lidx)
  1100. if (lidx != node->load_permutation[j])
  1101. return false;
  1102. }
  1103. /* Check that the loads in the first sequence are different and there
  1104. are no gaps between them. */
  1105. load_index = sbitmap_alloc (group_size);
  1106. bitmap_clear (load_index);
  1107. FOR_EACH_VEC_ELT (node->load_permutation, i, lidx)
  1108. {
  1109. if (bitmap_bit_p (load_index, lidx))
  1110. {
  1111. sbitmap_free (load_index);
  1112. return false;
  1113. }
  1114. bitmap_set_bit (load_index, lidx);
  1115. }
  1116. for (i = 0; i < group_size; i++)
  1117. if (!bitmap_bit_p (load_index, i))
  1118. {
  1119. sbitmap_free (load_index);
  1120. return false;
  1121. }
  1122. sbitmap_free (load_index);
  1123. /* This permutation is valid for reduction. Since the order of the
  1124. statements in the nodes is not important unless they are memory
  1125. accesses, we can rearrange the statements in all the nodes
  1126. according to the order of the loads. */
  1127. vect_slp_rearrange_stmts (SLP_INSTANCE_TREE (slp_instn), group_size,
  1128. node->load_permutation);
  1129. /* We are done, no actual permutations need to be generated. */
  1130. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
  1131. SLP_TREE_LOAD_PERMUTATION (node).release ();
  1132. return true;
  1133. }
  1134. /* In basic block vectorization we allow any subchain of an interleaving
  1135. chain.
  1136. FORNOW: not supported in loop SLP because of realignment compications. */
  1137. if (STMT_VINFO_BB_VINFO (vinfo_for_stmt (stmt)))
  1138. {
  1139. /* Check that for every node in the instance the loads
  1140. form a subchain. */
  1141. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
  1142. {
  1143. next_load = NULL;
  1144. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), j, load)
  1145. {
  1146. if (j != 0 && next_load != load)
  1147. return false;
  1148. next_load = GROUP_NEXT_ELEMENT (vinfo_for_stmt (load));
  1149. }
  1150. }
  1151. /* Check that the alignment of the first load in every subchain, i.e.,
  1152. the first statement in every load node, is supported.
  1153. ??? This belongs in alignment checking. */
  1154. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
  1155. {
  1156. first_load = SLP_TREE_SCALAR_STMTS (node)[0];
  1157. if (first_load != GROUP_FIRST_ELEMENT (vinfo_for_stmt (first_load)))
  1158. {
  1159. dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (first_load));
  1160. if (vect_supportable_dr_alignment (dr, false)
  1161. == dr_unaligned_unsupported)
  1162. {
  1163. if (dump_enabled_p ())
  1164. {
  1165. dump_printf_loc (MSG_MISSED_OPTIMIZATION,
  1166. vect_location,
  1167. "unsupported unaligned load ");
  1168. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
  1169. first_load, 0);
  1170. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  1171. }
  1172. return false;
  1173. }
  1174. }
  1175. }
  1176. /* We are done, no actual permutations need to be generated. */
  1177. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
  1178. SLP_TREE_LOAD_PERMUTATION (node).release ();
  1179. return true;
  1180. }
  1181. /* FORNOW: the only supported permutation is 0..01..1.. of length equal to
  1182. GROUP_SIZE and where each sequence of same drs is of GROUP_SIZE length as
  1183. well (unless it's reduction). */
  1184. if (SLP_INSTANCE_LOADS (slp_instn).length () != group_size)
  1185. return false;
  1186. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
  1187. if (!node->load_permutation.exists ())
  1188. return false;
  1189. load_index = sbitmap_alloc (group_size);
  1190. bitmap_clear (load_index);
  1191. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
  1192. {
  1193. unsigned int lidx = node->load_permutation[0];
  1194. if (bitmap_bit_p (load_index, lidx))
  1195. {
  1196. sbitmap_free (load_index);
  1197. return false;
  1198. }
  1199. bitmap_set_bit (load_index, lidx);
  1200. FOR_EACH_VEC_ELT (node->load_permutation, j, k)
  1201. if (k != lidx)
  1202. {
  1203. sbitmap_free (load_index);
  1204. return false;
  1205. }
  1206. }
  1207. for (i = 0; i < group_size; i++)
  1208. if (!bitmap_bit_p (load_index, i))
  1209. {
  1210. sbitmap_free (load_index);
  1211. return false;
  1212. }
  1213. sbitmap_free (load_index);
  1214. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (slp_instn), i, node)
  1215. if (node->load_permutation.exists ()
  1216. && !vect_transform_slp_perm_load
  1217. (node, vNULL, NULL,
  1218. SLP_INSTANCE_UNROLLING_FACTOR (slp_instn), slp_instn, true))
  1219. return false;
  1220. return true;
  1221. }
  1222. /* Find the first load in the loop that belongs to INSTANCE.
  1223. When loads are in several SLP nodes, there can be a case in which the first
  1224. load does not appear in the first SLP node to be transformed, causing
  1225. incorrect order of statements. Since we generate all the loads together,
  1226. they must be inserted before the first load of the SLP instance and not
  1227. before the first load of the first node of the instance. */
  1228. static gimple
  1229. vect_find_first_load_in_slp_instance (slp_instance instance)
  1230. {
  1231. int i, j;
  1232. slp_tree load_node;
  1233. gimple first_load = NULL, load;
  1234. FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), i, load_node)
  1235. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load)
  1236. first_load = get_earlier_stmt (load, first_load);
  1237. return first_load;
  1238. }
  1239. /* Find the last store in SLP INSTANCE. */
  1240. static gimple
  1241. vect_find_last_store_in_slp_instance (slp_instance instance)
  1242. {
  1243. int i;
  1244. slp_tree node;
  1245. gimple last_store = NULL, store;
  1246. node = SLP_INSTANCE_TREE (instance);
  1247. for (i = 0; SLP_TREE_SCALAR_STMTS (node).iterate (i, &store); i++)
  1248. last_store = get_later_stmt (store, last_store);
  1249. return last_store;
  1250. }
  1251. /* Compute the cost for the SLP node NODE in the SLP instance INSTANCE. */
  1252. static void
  1253. vect_analyze_slp_cost_1 (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
  1254. slp_instance instance, slp_tree node,
  1255. stmt_vector_for_cost *prologue_cost_vec,
  1256. unsigned ncopies_for_cost)
  1257. {
  1258. stmt_vector_for_cost *body_cost_vec = &SLP_INSTANCE_BODY_COST_VEC (instance);
  1259. unsigned i;
  1260. slp_tree child;
  1261. gimple stmt, s;
  1262. stmt_vec_info stmt_info;
  1263. tree lhs;
  1264. unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
  1265. /* Recurse down the SLP tree. */
  1266. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  1267. vect_analyze_slp_cost_1 (loop_vinfo, bb_vinfo,
  1268. instance, child, prologue_cost_vec,
  1269. ncopies_for_cost);
  1270. /* Look at the first scalar stmt to determine the cost. */
  1271. stmt = SLP_TREE_SCALAR_STMTS (node)[0];
  1272. stmt_info = vinfo_for_stmt (stmt);
  1273. if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
  1274. {
  1275. if (DR_IS_WRITE (STMT_VINFO_DATA_REF (stmt_info)))
  1276. vect_model_store_cost (stmt_info, ncopies_for_cost, false,
  1277. vect_uninitialized_def,
  1278. node, prologue_cost_vec, body_cost_vec);
  1279. else
  1280. {
  1281. int i;
  1282. gcc_checking_assert (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)));
  1283. vect_model_load_cost (stmt_info, ncopies_for_cost, false,
  1284. node, prologue_cost_vec, body_cost_vec);
  1285. /* If the load is permuted record the cost for the permutation.
  1286. ??? Loads from multiple chains are let through here only
  1287. for a single special case involving complex numbers where
  1288. in the end no permutation is necessary. */
  1289. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, s)
  1290. if ((STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo_for_stmt (s))
  1291. == STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info))
  1292. && vect_get_place_in_interleaving_chain
  1293. (s, STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info)) != i)
  1294. {
  1295. record_stmt_cost (body_cost_vec, group_size, vec_perm,
  1296. stmt_info, 0, vect_body);
  1297. break;
  1298. }
  1299. }
  1300. }
  1301. else
  1302. record_stmt_cost (body_cost_vec, ncopies_for_cost, vector_stmt,
  1303. stmt_info, 0, vect_body);
  1304. /* Scan operands and account for prologue cost of constants/externals.
  1305. ??? This over-estimates cost for multiple uses and should be
  1306. re-engineered. */
  1307. lhs = gimple_get_lhs (stmt);
  1308. for (i = 0; i < gimple_num_ops (stmt); ++i)
  1309. {
  1310. tree def, op = gimple_op (stmt, i);
  1311. gimple def_stmt;
  1312. enum vect_def_type dt;
  1313. if (!op || op == lhs)
  1314. continue;
  1315. if (vect_is_simple_use (op, NULL, loop_vinfo, bb_vinfo,
  1316. &def_stmt, &def, &dt)
  1317. && (dt == vect_constant_def || dt == vect_external_def))
  1318. record_stmt_cost (prologue_cost_vec, 1, vector_stmt,
  1319. stmt_info, 0, vect_prologue);
  1320. }
  1321. }
  1322. /* Compute the cost for the SLP instance INSTANCE. */
  1323. static void
  1324. vect_analyze_slp_cost (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
  1325. slp_instance instance, unsigned nunits)
  1326. {
  1327. stmt_vector_for_cost body_cost_vec, prologue_cost_vec;
  1328. unsigned ncopies_for_cost;
  1329. stmt_info_for_cost *si;
  1330. unsigned i;
  1331. /* Calculate the number of vector stmts to create based on the unrolling
  1332. factor (number of vectors is 1 if NUNITS >= GROUP_SIZE, and is
  1333. GROUP_SIZE / NUNITS otherwise. */
  1334. unsigned group_size = SLP_INSTANCE_GROUP_SIZE (instance);
  1335. ncopies_for_cost = least_common_multiple (nunits, group_size) / nunits;
  1336. prologue_cost_vec.create (10);
  1337. body_cost_vec.create (10);
  1338. SLP_INSTANCE_BODY_COST_VEC (instance) = body_cost_vec;
  1339. vect_analyze_slp_cost_1 (loop_vinfo, bb_vinfo,
  1340. instance, SLP_INSTANCE_TREE (instance),
  1341. &prologue_cost_vec, ncopies_for_cost);
  1342. /* Record the prologue costs, which were delayed until we were
  1343. sure that SLP was successful. Unlike the body costs, we know
  1344. the final values now regardless of the loop vectorization factor. */
  1345. void *data = (loop_vinfo ? LOOP_VINFO_TARGET_COST_DATA (loop_vinfo)
  1346. : BB_VINFO_TARGET_COST_DATA (bb_vinfo));
  1347. FOR_EACH_VEC_ELT (prologue_cost_vec, i, si)
  1348. {
  1349. struct _stmt_vec_info *stmt_info
  1350. = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
  1351. (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
  1352. si->misalign, vect_prologue);
  1353. }
  1354. prologue_cost_vec.release ();
  1355. }
  1356. /* Analyze an SLP instance starting from a group of grouped stores. Call
  1357. vect_build_slp_tree to build a tree of packed stmts if possible.
  1358. Return FALSE if it's impossible to SLP any stmt in the loop. */
  1359. static bool
  1360. vect_analyze_slp_instance (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
  1361. gimple stmt, unsigned max_tree_size)
  1362. {
  1363. slp_instance new_instance;
  1364. slp_tree node;
  1365. unsigned int group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
  1366. unsigned int unrolling_factor = 1, nunits;
  1367. tree vectype, scalar_type = NULL_TREE;
  1368. gimple next;
  1369. unsigned int vectorization_factor = 0;
  1370. int i;
  1371. unsigned int max_nunits = 0;
  1372. vec<slp_tree> loads;
  1373. struct data_reference *dr = STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt));
  1374. vec<gimple> scalar_stmts;
  1375. if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
  1376. {
  1377. if (dr)
  1378. {
  1379. scalar_type = TREE_TYPE (DR_REF (dr));
  1380. vectype = get_vectype_for_scalar_type (scalar_type);
  1381. }
  1382. else
  1383. {
  1384. gcc_assert (loop_vinfo);
  1385. vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
  1386. }
  1387. group_size = GROUP_SIZE (vinfo_for_stmt (stmt));
  1388. }
  1389. else
  1390. {
  1391. gcc_assert (loop_vinfo);
  1392. vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
  1393. group_size = LOOP_VINFO_REDUCTIONS (loop_vinfo).length ();
  1394. }
  1395. if (!vectype)
  1396. {
  1397. if (dump_enabled_p ())
  1398. {
  1399. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1400. "Build SLP failed: unsupported data-type ");
  1401. dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, scalar_type);
  1402. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  1403. }
  1404. return false;
  1405. }
  1406. nunits = TYPE_VECTOR_SUBPARTS (vectype);
  1407. if (loop_vinfo)
  1408. vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
  1409. else
  1410. vectorization_factor = nunits;
  1411. /* Calculate the unrolling factor. */
  1412. unrolling_factor = least_common_multiple (nunits, group_size) / group_size;
  1413. if (unrolling_factor != 1 && !loop_vinfo)
  1414. {
  1415. if (dump_enabled_p ())
  1416. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1417. "Build SLP failed: unrolling required in basic"
  1418. " block SLP\n");
  1419. return false;
  1420. }
  1421. /* Create a node (a root of the SLP tree) for the packed grouped stores. */
  1422. scalar_stmts.create (group_size);
  1423. next = stmt;
  1424. if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
  1425. {
  1426. /* Collect the stores and store them in SLP_TREE_SCALAR_STMTS. */
  1427. while (next)
  1428. {
  1429. if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next))
  1430. && STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)))
  1431. scalar_stmts.safe_push (
  1432. STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next)));
  1433. else
  1434. scalar_stmts.safe_push (next);
  1435. next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
  1436. }
  1437. }
  1438. else
  1439. {
  1440. /* Collect reduction statements. */
  1441. vec<gimple> reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
  1442. for (i = 0; reductions.iterate (i, &next); i++)
  1443. scalar_stmts.safe_push (next);
  1444. }
  1445. node = vect_create_new_slp_node (scalar_stmts);
  1446. loads.create (group_size);
  1447. /* Build the tree for the SLP instance. */
  1448. bool *matches = XALLOCAVEC (bool, group_size);
  1449. unsigned npermutes = 0;
  1450. if (vect_build_slp_tree (loop_vinfo, bb_vinfo, &node, group_size,
  1451. &max_nunits, &loads,
  1452. vectorization_factor, matches, &npermutes, NULL,
  1453. max_tree_size))
  1454. {
  1455. /* Calculate the unrolling factor based on the smallest type. */
  1456. if (max_nunits > nunits)
  1457. unrolling_factor = least_common_multiple (max_nunits, group_size)
  1458. / group_size;
  1459. if (unrolling_factor != 1 && !loop_vinfo)
  1460. {
  1461. if (dump_enabled_p ())
  1462. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1463. "Build SLP failed: unrolling required in basic"
  1464. " block SLP\n");
  1465. vect_free_slp_tree (node);
  1466. loads.release ();
  1467. return false;
  1468. }
  1469. /* Create a new SLP instance. */
  1470. new_instance = XNEW (struct _slp_instance);
  1471. SLP_INSTANCE_TREE (new_instance) = node;
  1472. SLP_INSTANCE_GROUP_SIZE (new_instance) = group_size;
  1473. SLP_INSTANCE_UNROLLING_FACTOR (new_instance) = unrolling_factor;
  1474. SLP_INSTANCE_BODY_COST_VEC (new_instance) = vNULL;
  1475. SLP_INSTANCE_LOADS (new_instance) = loads;
  1476. SLP_INSTANCE_FIRST_LOAD_STMT (new_instance) = NULL;
  1477. /* Compute the load permutation. */
  1478. slp_tree load_node;
  1479. bool loads_permuted = false;
  1480. FOR_EACH_VEC_ELT (loads, i, load_node)
  1481. {
  1482. vec<unsigned> load_permutation;
  1483. int j;
  1484. gimple load, first_stmt;
  1485. bool this_load_permuted = false;
  1486. load_permutation.create (group_size);
  1487. first_stmt = GROUP_FIRST_ELEMENT
  1488. (vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (load_node)[0]));
  1489. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (load_node), j, load)
  1490. {
  1491. int load_place
  1492. = vect_get_place_in_interleaving_chain (load, first_stmt);
  1493. gcc_assert (load_place != -1);
  1494. if (load_place != j)
  1495. this_load_permuted = true;
  1496. load_permutation.safe_push (load_place);
  1497. }
  1498. if (!this_load_permuted)
  1499. {
  1500. load_permutation.release ();
  1501. continue;
  1502. }
  1503. SLP_TREE_LOAD_PERMUTATION (load_node) = load_permutation;
  1504. loads_permuted = true;
  1505. }
  1506. if (loads_permuted)
  1507. {
  1508. if (!vect_supported_load_permutation_p (new_instance))
  1509. {
  1510. if (dump_enabled_p ())
  1511. {
  1512. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1513. "Build SLP failed: unsupported load "
  1514. "permutation ");
  1515. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  1516. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  1517. }
  1518. vect_free_slp_instance (new_instance);
  1519. return false;
  1520. }
  1521. SLP_INSTANCE_FIRST_LOAD_STMT (new_instance)
  1522. = vect_find_first_load_in_slp_instance (new_instance);
  1523. }
  1524. /* Compute the costs of this SLP instance. */
  1525. vect_analyze_slp_cost (loop_vinfo, bb_vinfo,
  1526. new_instance, TYPE_VECTOR_SUBPARTS (vectype));
  1527. if (loop_vinfo)
  1528. LOOP_VINFO_SLP_INSTANCES (loop_vinfo).safe_push (new_instance);
  1529. else
  1530. BB_VINFO_SLP_INSTANCES (bb_vinfo).safe_push (new_instance);
  1531. if (dump_enabled_p ())
  1532. vect_print_slp_tree (MSG_NOTE, node);
  1533. return true;
  1534. }
  1535. /* Failed to SLP. */
  1536. /* Free the allocated memory. */
  1537. vect_free_slp_tree (node);
  1538. loads.release ();
  1539. return false;
  1540. }
  1541. /* Check if there are stmts in the loop can be vectorized using SLP. Build SLP
  1542. trees of packed scalar stmts if SLP is possible. */
  1543. bool
  1544. vect_analyze_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo,
  1545. unsigned max_tree_size)
  1546. {
  1547. unsigned int i;
  1548. vec<gimple> grouped_stores;
  1549. vec<gimple> reductions = vNULL;
  1550. vec<gimple> reduc_chains = vNULL;
  1551. gimple first_element;
  1552. bool ok = false;
  1553. if (dump_enabled_p ())
  1554. dump_printf_loc (MSG_NOTE, vect_location, "=== vect_analyze_slp ===\n");
  1555. if (loop_vinfo)
  1556. {
  1557. grouped_stores = LOOP_VINFO_GROUPED_STORES (loop_vinfo);
  1558. reduc_chains = LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo);
  1559. reductions = LOOP_VINFO_REDUCTIONS (loop_vinfo);
  1560. }
  1561. else
  1562. grouped_stores = BB_VINFO_GROUPED_STORES (bb_vinfo);
  1563. /* Find SLP sequences starting from groups of grouped stores. */
  1564. FOR_EACH_VEC_ELT (grouped_stores, i, first_element)
  1565. if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element,
  1566. max_tree_size))
  1567. ok = true;
  1568. if (bb_vinfo && !ok)
  1569. {
  1570. if (dump_enabled_p ())
  1571. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1572. "Failed to SLP the basic block.\n");
  1573. return false;
  1574. }
  1575. if (loop_vinfo
  1576. && LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo).length () > 0)
  1577. {
  1578. /* Find SLP sequences starting from reduction chains. */
  1579. FOR_EACH_VEC_ELT (reduc_chains, i, first_element)
  1580. if (vect_analyze_slp_instance (loop_vinfo, bb_vinfo, first_element,
  1581. max_tree_size))
  1582. ok = true;
  1583. else
  1584. return false;
  1585. /* Don't try to vectorize SLP reductions if reduction chain was
  1586. detected. */
  1587. return ok;
  1588. }
  1589. /* Find SLP sequences starting from groups of reductions. */
  1590. if (loop_vinfo && LOOP_VINFO_REDUCTIONS (loop_vinfo).length () > 1
  1591. && vect_analyze_slp_instance (loop_vinfo, bb_vinfo, reductions[0],
  1592. max_tree_size))
  1593. ok = true;
  1594. return true;
  1595. }
  1596. /* For each possible SLP instance decide whether to SLP it and calculate overall
  1597. unrolling factor needed to SLP the loop. Return TRUE if decided to SLP at
  1598. least one instance. */
  1599. bool
  1600. vect_make_slp_decision (loop_vec_info loop_vinfo)
  1601. {
  1602. unsigned int i, unrolling_factor = 1;
  1603. vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
  1604. slp_instance instance;
  1605. int decided_to_slp = 0;
  1606. if (dump_enabled_p ())
  1607. dump_printf_loc (MSG_NOTE, vect_location, "=== vect_make_slp_decision ==="
  1608. "\n");
  1609. FOR_EACH_VEC_ELT (slp_instances, i, instance)
  1610. {
  1611. /* FORNOW: SLP if you can. */
  1612. if (unrolling_factor < SLP_INSTANCE_UNROLLING_FACTOR (instance))
  1613. unrolling_factor = SLP_INSTANCE_UNROLLING_FACTOR (instance);
  1614. /* Mark all the stmts that belong to INSTANCE as PURE_SLP stmts. Later we
  1615. call vect_detect_hybrid_slp () to find stmts that need hybrid SLP and
  1616. loop-based vectorization. Such stmts will be marked as HYBRID. */
  1617. vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
  1618. decided_to_slp++;
  1619. }
  1620. LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo) = unrolling_factor;
  1621. if (decided_to_slp && dump_enabled_p ())
  1622. dump_printf_loc (MSG_NOTE, vect_location,
  1623. "Decided to SLP %d instances. Unrolling factor %d\n",
  1624. decided_to_slp, unrolling_factor);
  1625. return (decided_to_slp > 0);
  1626. }
  1627. /* Find stmts that must be both vectorized and SLPed (since they feed stmts that
  1628. can't be SLPed) in the tree rooted at NODE. Mark such stmts as HYBRID. */
  1629. static void
  1630. vect_detect_hybrid_slp_stmts (slp_tree node, unsigned i, slp_vect_type stype)
  1631. {
  1632. gimple stmt = SLP_TREE_SCALAR_STMTS (node)[i];
  1633. imm_use_iterator imm_iter;
  1634. gimple use_stmt;
  1635. stmt_vec_info use_vinfo, stmt_vinfo = vinfo_for_stmt (stmt);
  1636. slp_tree child;
  1637. loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
  1638. struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
  1639. int j;
  1640. /* Propagate hybrid down the SLP tree. */
  1641. if (stype == hybrid)
  1642. ;
  1643. else if (HYBRID_SLP_STMT (stmt_vinfo))
  1644. stype = hybrid;
  1645. else
  1646. {
  1647. /* Check if a pure SLP stmt has uses in non-SLP stmts. */
  1648. gcc_checking_assert (PURE_SLP_STMT (stmt_vinfo));
  1649. /* We always get the pattern stmt here, but for immediate
  1650. uses we have to use the LHS of the original stmt. */
  1651. gcc_checking_assert (!STMT_VINFO_IN_PATTERN_P (stmt_vinfo));
  1652. if (STMT_VINFO_RELATED_STMT (stmt_vinfo))
  1653. stmt = STMT_VINFO_RELATED_STMT (stmt_vinfo);
  1654. if (TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
  1655. FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, gimple_op (stmt, 0))
  1656. {
  1657. if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
  1658. continue;
  1659. use_vinfo = vinfo_for_stmt (use_stmt);
  1660. if (STMT_VINFO_IN_PATTERN_P (use_vinfo)
  1661. && STMT_VINFO_RELATED_STMT (use_vinfo))
  1662. use_vinfo = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (use_vinfo));
  1663. if (!STMT_SLP_TYPE (use_vinfo)
  1664. && (STMT_VINFO_RELEVANT (use_vinfo)
  1665. || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (use_vinfo)))
  1666. && !(gimple_code (use_stmt) == GIMPLE_PHI
  1667. && STMT_VINFO_DEF_TYPE (use_vinfo) == vect_reduction_def))
  1668. stype = hybrid;
  1669. }
  1670. }
  1671. if (stype == hybrid)
  1672. STMT_SLP_TYPE (stmt_vinfo) = hybrid;
  1673. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), j, child)
  1674. vect_detect_hybrid_slp_stmts (child, i, stype);
  1675. }
  1676. /* Helpers for vect_detect_hybrid_slp walking pattern stmt uses. */
  1677. static tree
  1678. vect_detect_hybrid_slp_1 (tree *tp, int *, void *data)
  1679. {
  1680. walk_stmt_info *wi = (walk_stmt_info *)data;
  1681. struct loop *loopp = (struct loop *)wi->info;
  1682. if (wi->is_lhs)
  1683. return NULL_TREE;
  1684. if (TREE_CODE (*tp) == SSA_NAME
  1685. && !SSA_NAME_IS_DEFAULT_DEF (*tp))
  1686. {
  1687. gimple def_stmt = SSA_NAME_DEF_STMT (*tp);
  1688. if (flow_bb_inside_loop_p (loopp, gimple_bb (def_stmt))
  1689. && PURE_SLP_STMT (vinfo_for_stmt (def_stmt)))
  1690. STMT_SLP_TYPE (vinfo_for_stmt (def_stmt)) = hybrid;
  1691. }
  1692. return NULL_TREE;
  1693. }
  1694. static tree
  1695. vect_detect_hybrid_slp_2 (gimple_stmt_iterator *gsi, bool *handled,
  1696. walk_stmt_info *)
  1697. {
  1698. /* If the stmt is in a SLP instance then this isn't a reason
  1699. to mark use definitions in other SLP instances as hybrid. */
  1700. if (STMT_SLP_TYPE (vinfo_for_stmt (gsi_stmt (*gsi))) != loop_vect)
  1701. *handled = true;
  1702. return NULL_TREE;
  1703. }
  1704. /* Find stmts that must be both vectorized and SLPed. */
  1705. void
  1706. vect_detect_hybrid_slp (loop_vec_info loop_vinfo)
  1707. {
  1708. unsigned int i;
  1709. vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
  1710. slp_instance instance;
  1711. if (dump_enabled_p ())
  1712. dump_printf_loc (MSG_NOTE, vect_location, "=== vect_detect_hybrid_slp ==="
  1713. "\n");
  1714. /* First walk all pattern stmt in the loop and mark defs of uses as
  1715. hybrid because immediate uses in them are not recorded. */
  1716. for (i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; ++i)
  1717. {
  1718. basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
  1719. for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
  1720. gsi_next (&gsi))
  1721. {
  1722. gimple stmt = gsi_stmt (gsi);
  1723. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  1724. if (STMT_VINFO_IN_PATTERN_P (stmt_info))
  1725. {
  1726. walk_stmt_info wi;
  1727. memset (&wi, 0, sizeof (wi));
  1728. wi.info = LOOP_VINFO_LOOP (loop_vinfo);
  1729. gimple_stmt_iterator gsi2
  1730. = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
  1731. walk_gimple_stmt (&gsi2, vect_detect_hybrid_slp_2,
  1732. vect_detect_hybrid_slp_1, &wi);
  1733. walk_gimple_seq (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info),
  1734. vect_detect_hybrid_slp_2,
  1735. vect_detect_hybrid_slp_1, &wi);
  1736. }
  1737. }
  1738. }
  1739. /* Then walk the SLP instance trees marking stmts with uses in
  1740. non-SLP stmts as hybrid, also propagating hybrid down the
  1741. SLP tree, collecting the above info on-the-fly. */
  1742. FOR_EACH_VEC_ELT (slp_instances, i, instance)
  1743. {
  1744. for (unsigned i = 0; i < SLP_INSTANCE_GROUP_SIZE (instance); ++i)
  1745. vect_detect_hybrid_slp_stmts (SLP_INSTANCE_TREE (instance),
  1746. i, pure_slp);
  1747. }
  1748. }
  1749. /* Create and initialize a new bb_vec_info struct for BB, as well as
  1750. stmt_vec_info structs for all the stmts in it. */
  1751. static bb_vec_info
  1752. new_bb_vec_info (basic_block bb)
  1753. {
  1754. bb_vec_info res = NULL;
  1755. gimple_stmt_iterator gsi;
  1756. res = (bb_vec_info) xcalloc (1, sizeof (struct _bb_vec_info));
  1757. BB_VINFO_BB (res) = bb;
  1758. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  1759. {
  1760. gimple stmt = gsi_stmt (gsi);
  1761. gimple_set_uid (stmt, 0);
  1762. set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, NULL, res));
  1763. }
  1764. BB_VINFO_GROUPED_STORES (res).create (10);
  1765. BB_VINFO_SLP_INSTANCES (res).create (2);
  1766. BB_VINFO_TARGET_COST_DATA (res) = init_cost (NULL);
  1767. bb->aux = res;
  1768. return res;
  1769. }
  1770. /* Free BB_VINFO struct, as well as all the stmt_vec_info structs of all the
  1771. stmts in the basic block. */
  1772. static void
  1773. destroy_bb_vec_info (bb_vec_info bb_vinfo)
  1774. {
  1775. vec<slp_instance> slp_instances;
  1776. slp_instance instance;
  1777. basic_block bb;
  1778. gimple_stmt_iterator si;
  1779. unsigned i;
  1780. if (!bb_vinfo)
  1781. return;
  1782. bb = BB_VINFO_BB (bb_vinfo);
  1783. for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
  1784. {
  1785. gimple stmt = gsi_stmt (si);
  1786. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  1787. if (stmt_info)
  1788. /* Free stmt_vec_info. */
  1789. free_stmt_vec_info (stmt);
  1790. }
  1791. vect_destroy_datarefs (NULL, bb_vinfo);
  1792. free_dependence_relations (BB_VINFO_DDRS (bb_vinfo));
  1793. BB_VINFO_GROUPED_STORES (bb_vinfo).release ();
  1794. slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
  1795. FOR_EACH_VEC_ELT (slp_instances, i, instance)
  1796. vect_free_slp_instance (instance);
  1797. BB_VINFO_SLP_INSTANCES (bb_vinfo).release ();
  1798. destroy_cost_data (BB_VINFO_TARGET_COST_DATA (bb_vinfo));
  1799. free (bb_vinfo);
  1800. bb->aux = NULL;
  1801. }
  1802. /* Analyze statements contained in SLP tree node after recursively analyzing
  1803. the subtree. Return TRUE if the operations are supported. */
  1804. static bool
  1805. vect_slp_analyze_node_operations (bb_vec_info bb_vinfo, slp_tree node)
  1806. {
  1807. bool dummy;
  1808. int i;
  1809. gimple stmt;
  1810. slp_tree child;
  1811. if (!node)
  1812. return true;
  1813. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  1814. if (!vect_slp_analyze_node_operations (bb_vinfo, child))
  1815. return false;
  1816. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
  1817. {
  1818. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  1819. gcc_assert (stmt_info);
  1820. gcc_assert (PURE_SLP_STMT (stmt_info));
  1821. if (!vect_analyze_stmt (stmt, &dummy, node))
  1822. return false;
  1823. }
  1824. return true;
  1825. }
  1826. /* Analyze statements in SLP instances of the basic block. Return TRUE if the
  1827. operations are supported. */
  1828. static bool
  1829. vect_slp_analyze_operations (bb_vec_info bb_vinfo)
  1830. {
  1831. vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
  1832. slp_instance instance;
  1833. int i;
  1834. for (i = 0; slp_instances.iterate (i, &instance); )
  1835. {
  1836. if (!vect_slp_analyze_node_operations (bb_vinfo,
  1837. SLP_INSTANCE_TREE (instance)))
  1838. {
  1839. vect_free_slp_instance (instance);
  1840. slp_instances.ordered_remove (i);
  1841. }
  1842. else
  1843. i++;
  1844. }
  1845. if (!slp_instances.length ())
  1846. return false;
  1847. return true;
  1848. }
  1849. /* Compute the scalar cost of the SLP node NODE and its children
  1850. and return it. Do not account defs that are marked in LIFE and
  1851. update LIFE according to uses of NODE. */
  1852. static unsigned
  1853. vect_bb_slp_scalar_cost (basic_block bb,
  1854. slp_tree node, vec<bool, va_heap> *life)
  1855. {
  1856. unsigned scalar_cost = 0;
  1857. unsigned i;
  1858. gimple stmt;
  1859. slp_tree child;
  1860. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
  1861. {
  1862. unsigned stmt_cost;
  1863. ssa_op_iter op_iter;
  1864. def_operand_p def_p;
  1865. stmt_vec_info stmt_info;
  1866. if ((*life)[i])
  1867. continue;
  1868. /* If there is a non-vectorized use of the defs then the scalar
  1869. stmt is kept live in which case we do not account it or any
  1870. required defs in the SLP children in the scalar cost. This
  1871. way we make the vectorization more costly when compared to
  1872. the scalar cost. */
  1873. FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF)
  1874. {
  1875. imm_use_iterator use_iter;
  1876. gimple use_stmt;
  1877. FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, DEF_FROM_PTR (def_p))
  1878. if (!is_gimple_debug (use_stmt)
  1879. && (gimple_code (use_stmt) == GIMPLE_PHI
  1880. || gimple_bb (use_stmt) != bb
  1881. || !STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (use_stmt))))
  1882. {
  1883. (*life)[i] = true;
  1884. BREAK_FROM_IMM_USE_STMT (use_iter);
  1885. }
  1886. }
  1887. if ((*life)[i])
  1888. continue;
  1889. stmt_info = vinfo_for_stmt (stmt);
  1890. if (STMT_VINFO_DATA_REF (stmt_info))
  1891. {
  1892. if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
  1893. stmt_cost = vect_get_stmt_cost (scalar_load);
  1894. else
  1895. stmt_cost = vect_get_stmt_cost (scalar_store);
  1896. }
  1897. else
  1898. stmt_cost = vect_get_stmt_cost (scalar_stmt);
  1899. scalar_cost += stmt_cost;
  1900. }
  1901. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  1902. scalar_cost += vect_bb_slp_scalar_cost (bb, child, life);
  1903. return scalar_cost;
  1904. }
  1905. /* Check if vectorization of the basic block is profitable. */
  1906. static bool
  1907. vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
  1908. {
  1909. vec<slp_instance> slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
  1910. slp_instance instance;
  1911. int i, j;
  1912. unsigned int vec_inside_cost = 0, vec_outside_cost = 0, scalar_cost = 0;
  1913. unsigned int vec_prologue_cost = 0, vec_epilogue_cost = 0;
  1914. void *target_cost_data = BB_VINFO_TARGET_COST_DATA (bb_vinfo);
  1915. stmt_vec_info stmt_info = NULL;
  1916. stmt_vector_for_cost body_cost_vec;
  1917. stmt_info_for_cost *ci;
  1918. /* Calculate vector costs. */
  1919. FOR_EACH_VEC_ELT (slp_instances, i, instance)
  1920. {
  1921. body_cost_vec = SLP_INSTANCE_BODY_COST_VEC (instance);
  1922. FOR_EACH_VEC_ELT (body_cost_vec, j, ci)
  1923. {
  1924. stmt_info = ci->stmt ? vinfo_for_stmt (ci->stmt) : NULL;
  1925. (void) add_stmt_cost (target_cost_data, ci->count, ci->kind,
  1926. stmt_info, ci->misalign, vect_body);
  1927. }
  1928. }
  1929. /* Calculate scalar cost. */
  1930. FOR_EACH_VEC_ELT (slp_instances, i, instance)
  1931. {
  1932. auto_vec<bool, 20> life;
  1933. life.safe_grow_cleared (SLP_INSTANCE_GROUP_SIZE (instance));
  1934. scalar_cost += vect_bb_slp_scalar_cost (BB_VINFO_BB (bb_vinfo),
  1935. SLP_INSTANCE_TREE (instance),
  1936. &life);
  1937. }
  1938. /* Complete the target-specific cost calculation. */
  1939. finish_cost (BB_VINFO_TARGET_COST_DATA (bb_vinfo), &vec_prologue_cost,
  1940. &vec_inside_cost, &vec_epilogue_cost);
  1941. vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
  1942. if (dump_enabled_p ())
  1943. {
  1944. dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
  1945. dump_printf (MSG_NOTE, " Vector inside of basic block cost: %d\n",
  1946. vec_inside_cost);
  1947. dump_printf (MSG_NOTE, " Vector prologue cost: %d\n", vec_prologue_cost);
  1948. dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n", vec_epilogue_cost);
  1949. dump_printf (MSG_NOTE, " Scalar cost of basic block: %d\n", scalar_cost);
  1950. }
  1951. /* Vectorization is profitable if its cost is less than the cost of scalar
  1952. version. */
  1953. if (vec_outside_cost + vec_inside_cost >= scalar_cost)
  1954. return false;
  1955. return true;
  1956. }
  1957. /* Check if the basic block can be vectorized. */
  1958. static bb_vec_info
  1959. vect_slp_analyze_bb_1 (basic_block bb)
  1960. {
  1961. bb_vec_info bb_vinfo;
  1962. vec<slp_instance> slp_instances;
  1963. slp_instance instance;
  1964. int i;
  1965. int min_vf = 2;
  1966. unsigned n_stmts = 0;
  1967. bb_vinfo = new_bb_vec_info (bb);
  1968. if (!bb_vinfo)
  1969. return NULL;
  1970. if (!vect_analyze_data_refs (NULL, bb_vinfo, &min_vf, &n_stmts))
  1971. {
  1972. if (dump_enabled_p ())
  1973. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1974. "not vectorized: unhandled data-ref in basic "
  1975. "block.\n");
  1976. destroy_bb_vec_info (bb_vinfo);
  1977. return NULL;
  1978. }
  1979. if (BB_VINFO_DATAREFS (bb_vinfo).length () < 2)
  1980. {
  1981. if (dump_enabled_p ())
  1982. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1983. "not vectorized: not enough data-refs in "
  1984. "basic block.\n");
  1985. destroy_bb_vec_info (bb_vinfo);
  1986. return NULL;
  1987. }
  1988. if (!vect_analyze_data_ref_accesses (NULL, bb_vinfo))
  1989. {
  1990. if (dump_enabled_p ())
  1991. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  1992. "not vectorized: unhandled data access in "
  1993. "basic block.\n");
  1994. destroy_bb_vec_info (bb_vinfo);
  1995. return NULL;
  1996. }
  1997. vect_pattern_recog (NULL, bb_vinfo);
  1998. if (!vect_analyze_data_refs_alignment (NULL, bb_vinfo))
  1999. {
  2000. if (dump_enabled_p ())
  2001. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2002. "not vectorized: bad data alignment in basic "
  2003. "block.\n");
  2004. destroy_bb_vec_info (bb_vinfo);
  2005. return NULL;
  2006. }
  2007. /* Check the SLP opportunities in the basic block, analyze and build SLP
  2008. trees. */
  2009. if (!vect_analyze_slp (NULL, bb_vinfo, n_stmts))
  2010. {
  2011. if (dump_enabled_p ())
  2012. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2013. "not vectorized: failed to find SLP opportunities "
  2014. "in basic block.\n");
  2015. destroy_bb_vec_info (bb_vinfo);
  2016. return NULL;
  2017. }
  2018. slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
  2019. /* Mark all the statements that we want to vectorize as pure SLP and
  2020. relevant. */
  2021. FOR_EACH_VEC_ELT (slp_instances, i, instance)
  2022. {
  2023. vect_mark_slp_stmts (SLP_INSTANCE_TREE (instance), pure_slp, -1);
  2024. vect_mark_slp_stmts_relevant (SLP_INSTANCE_TREE (instance));
  2025. }
  2026. /* Mark all the statements that we do not want to vectorize. */
  2027. for (gimple_stmt_iterator gsi = gsi_start_bb (BB_VINFO_BB (bb_vinfo));
  2028. !gsi_end_p (gsi); gsi_next (&gsi))
  2029. {
  2030. stmt_vec_info vinfo = vinfo_for_stmt (gsi_stmt (gsi));
  2031. if (STMT_SLP_TYPE (vinfo) != pure_slp)
  2032. STMT_VINFO_VECTORIZABLE (vinfo) = false;
  2033. }
  2034. /* Analyze dependences. At this point all stmts not participating in
  2035. vectorization have to be marked. Dependence analysis assumes
  2036. that we either vectorize all SLP instances or none at all. */
  2037. if (!vect_slp_analyze_data_ref_dependences (bb_vinfo))
  2038. {
  2039. if (dump_enabled_p ())
  2040. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2041. "not vectorized: unhandled data dependence "
  2042. "in basic block.\n");
  2043. destroy_bb_vec_info (bb_vinfo);
  2044. return NULL;
  2045. }
  2046. if (!vect_verify_datarefs_alignment (NULL, bb_vinfo))
  2047. {
  2048. if (dump_enabled_p ())
  2049. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2050. "not vectorized: unsupported alignment in basic "
  2051. "block.\n");
  2052. destroy_bb_vec_info (bb_vinfo);
  2053. return NULL;
  2054. }
  2055. if (!vect_slp_analyze_operations (bb_vinfo))
  2056. {
  2057. if (dump_enabled_p ())
  2058. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2059. "not vectorized: bad operation in basic block.\n");
  2060. destroy_bb_vec_info (bb_vinfo);
  2061. return NULL;
  2062. }
  2063. /* Cost model: check if the vectorization is worthwhile. */
  2064. if (!unlimited_cost_model (NULL)
  2065. && !vect_bb_vectorization_profitable_p (bb_vinfo))
  2066. {
  2067. if (dump_enabled_p ())
  2068. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2069. "not vectorized: vectorization is not "
  2070. "profitable.\n");
  2071. destroy_bb_vec_info (bb_vinfo);
  2072. return NULL;
  2073. }
  2074. if (dump_enabled_p ())
  2075. dump_printf_loc (MSG_NOTE, vect_location,
  2076. "Basic block will be vectorized using SLP\n");
  2077. return bb_vinfo;
  2078. }
  2079. bb_vec_info
  2080. vect_slp_analyze_bb (basic_block bb)
  2081. {
  2082. bb_vec_info bb_vinfo;
  2083. int insns = 0;
  2084. gimple_stmt_iterator gsi;
  2085. unsigned int vector_sizes;
  2086. if (dump_enabled_p ())
  2087. dump_printf_loc (MSG_NOTE, vect_location, "===vect_slp_analyze_bb===\n");
  2088. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  2089. {
  2090. gimple stmt = gsi_stmt (gsi);
  2091. if (!is_gimple_debug (stmt)
  2092. && !gimple_nop_p (stmt)
  2093. && gimple_code (stmt) != GIMPLE_LABEL)
  2094. insns++;
  2095. }
  2096. if (insns > PARAM_VALUE (PARAM_SLP_MAX_INSNS_IN_BB))
  2097. {
  2098. if (dump_enabled_p ())
  2099. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2100. "not vectorized: too many instructions in "
  2101. "basic block.\n");
  2102. return NULL;
  2103. }
  2104. /* Autodetect first vector size we try. */
  2105. current_vector_size = 0;
  2106. vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
  2107. while (1)
  2108. {
  2109. bb_vinfo = vect_slp_analyze_bb_1 (bb);
  2110. if (bb_vinfo)
  2111. return bb_vinfo;
  2112. destroy_bb_vec_info (bb_vinfo);
  2113. vector_sizes &= ~current_vector_size;
  2114. if (vector_sizes == 0
  2115. || current_vector_size == 0)
  2116. return NULL;
  2117. /* Try the next biggest vector size. */
  2118. current_vector_size = 1 << floor_log2 (vector_sizes);
  2119. if (dump_enabled_p ())
  2120. dump_printf_loc (MSG_NOTE, vect_location,
  2121. "***** Re-trying analysis with "
  2122. "vector size %d\n", current_vector_size);
  2123. }
  2124. }
  2125. /* SLP costs are calculated according to SLP instance unrolling factor (i.e.,
  2126. the number of created vector stmts depends on the unrolling factor).
  2127. However, the actual number of vector stmts for every SLP node depends on
  2128. VF which is set later in vect_analyze_operations (). Hence, SLP costs
  2129. should be updated. In this function we assume that the inside costs
  2130. calculated in vect_model_xxx_cost are linear in ncopies. */
  2131. void
  2132. vect_update_slp_costs_according_to_vf (loop_vec_info loop_vinfo)
  2133. {
  2134. unsigned int i, j, vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
  2135. vec<slp_instance> slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
  2136. slp_instance instance;
  2137. stmt_vector_for_cost body_cost_vec;
  2138. stmt_info_for_cost *si;
  2139. void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
  2140. if (dump_enabled_p ())
  2141. dump_printf_loc (MSG_NOTE, vect_location,
  2142. "=== vect_update_slp_costs_according_to_vf ===\n");
  2143. FOR_EACH_VEC_ELT (slp_instances, i, instance)
  2144. {
  2145. /* We assume that costs are linear in ncopies. */
  2146. int ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (instance);
  2147. /* Record the instance's instructions in the target cost model.
  2148. This was delayed until here because the count of instructions
  2149. isn't known beforehand. */
  2150. body_cost_vec = SLP_INSTANCE_BODY_COST_VEC (instance);
  2151. FOR_EACH_VEC_ELT (body_cost_vec, j, si)
  2152. (void) add_stmt_cost (data, si->count * ncopies, si->kind,
  2153. vinfo_for_stmt (si->stmt), si->misalign,
  2154. vect_body);
  2155. }
  2156. }
  2157. /* For constant and loop invariant defs of SLP_NODE this function returns
  2158. (vector) defs (VEC_OPRNDS) that will be used in the vectorized stmts.
  2159. OP_NUM determines if we gather defs for operand 0 or operand 1 of the RHS of
  2160. scalar stmts. NUMBER_OF_VECTORS is the number of vector defs to create.
  2161. REDUC_INDEX is the index of the reduction operand in the statements, unless
  2162. it is -1. */
  2163. static void
  2164. vect_get_constant_vectors (tree op, slp_tree slp_node,
  2165. vec<tree> *vec_oprnds,
  2166. unsigned int op_num, unsigned int number_of_vectors,
  2167. int reduc_index)
  2168. {
  2169. vec<gimple> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
  2170. gimple stmt = stmts[0];
  2171. stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
  2172. unsigned nunits;
  2173. tree vec_cst;
  2174. tree *elts;
  2175. unsigned j, number_of_places_left_in_vector;
  2176. tree vector_type;
  2177. tree vop;
  2178. int group_size = stmts.length ();
  2179. unsigned int vec_num, i;
  2180. unsigned number_of_copies = 1;
  2181. vec<tree> voprnds;
  2182. voprnds.create (number_of_vectors);
  2183. bool constant_p, is_store;
  2184. tree neutral_op = NULL;
  2185. enum tree_code code = gimple_expr_code (stmt);
  2186. gimple def_stmt;
  2187. struct loop *loop;
  2188. gimple_seq ctor_seq = NULL;
  2189. if (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
  2190. && reduc_index != -1)
  2191. {
  2192. op_num = reduc_index - 1;
  2193. op = gimple_op (stmt, reduc_index);
  2194. /* For additional copies (see the explanation of NUMBER_OF_COPIES below)
  2195. we need either neutral operands or the original operands. See
  2196. get_initial_def_for_reduction() for details. */
  2197. switch (code)
  2198. {
  2199. case WIDEN_SUM_EXPR:
  2200. case DOT_PROD_EXPR:
  2201. case PLUS_EXPR:
  2202. case MINUS_EXPR:
  2203. case BIT_IOR_EXPR:
  2204. case BIT_XOR_EXPR:
  2205. if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
  2206. neutral_op = build_real (TREE_TYPE (op), dconst0);
  2207. else
  2208. neutral_op = build_int_cst (TREE_TYPE (op), 0);
  2209. break;
  2210. case MULT_EXPR:
  2211. if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
  2212. neutral_op = build_real (TREE_TYPE (op), dconst1);
  2213. else
  2214. neutral_op = build_int_cst (TREE_TYPE (op), 1);
  2215. break;
  2216. case BIT_AND_EXPR:
  2217. neutral_op = build_int_cst (TREE_TYPE (op), -1);
  2218. break;
  2219. /* For MIN/MAX we don't have an easy neutral operand but
  2220. the initial values can be used fine here. Only for
  2221. a reduction chain we have to force a neutral element. */
  2222. case MAX_EXPR:
  2223. case MIN_EXPR:
  2224. if (!GROUP_FIRST_ELEMENT (stmt_vinfo))
  2225. neutral_op = NULL;
  2226. else
  2227. {
  2228. def_stmt = SSA_NAME_DEF_STMT (op);
  2229. loop = (gimple_bb (stmt))->loop_father;
  2230. neutral_op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
  2231. loop_preheader_edge (loop));
  2232. }
  2233. break;
  2234. default:
  2235. neutral_op = NULL;
  2236. }
  2237. }
  2238. if (STMT_VINFO_DATA_REF (stmt_vinfo))
  2239. {
  2240. is_store = true;
  2241. op = gimple_assign_rhs1 (stmt);
  2242. }
  2243. else
  2244. is_store = false;
  2245. gcc_assert (op);
  2246. if (CONSTANT_CLASS_P (op))
  2247. constant_p = true;
  2248. else
  2249. constant_p = false;
  2250. vector_type = get_vectype_for_scalar_type (TREE_TYPE (op));
  2251. gcc_assert (vector_type);
  2252. nunits = TYPE_VECTOR_SUBPARTS (vector_type);
  2253. /* NUMBER_OF_COPIES is the number of times we need to use the same values in
  2254. created vectors. It is greater than 1 if unrolling is performed.
  2255. For example, we have two scalar operands, s1 and s2 (e.g., group of
  2256. strided accesses of size two), while NUNITS is four (i.e., four scalars
  2257. of this type can be packed in a vector). The output vector will contain
  2258. two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
  2259. will be 2).
  2260. If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
  2261. containing the operands.
  2262. For example, NUNITS is four as before, and the group size is 8
  2263. (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
  2264. {s5, s6, s7, s8}. */
  2265. number_of_copies = least_common_multiple (nunits, group_size) / group_size;
  2266. number_of_places_left_in_vector = nunits;
  2267. elts = XALLOCAVEC (tree, nunits);
  2268. for (j = 0; j < number_of_copies; j++)
  2269. {
  2270. for (i = group_size - 1; stmts.iterate (i, &stmt); i--)
  2271. {
  2272. if (is_store)
  2273. op = gimple_assign_rhs1 (stmt);
  2274. else
  2275. {
  2276. switch (code)
  2277. {
  2278. case COND_EXPR:
  2279. if (op_num == 0 || op_num == 1)
  2280. {
  2281. tree cond = gimple_assign_rhs1 (stmt);
  2282. op = TREE_OPERAND (cond, op_num);
  2283. }
  2284. else
  2285. {
  2286. if (op_num == 2)
  2287. op = gimple_assign_rhs2 (stmt);
  2288. else
  2289. op = gimple_assign_rhs3 (stmt);
  2290. }
  2291. break;
  2292. case CALL_EXPR:
  2293. op = gimple_call_arg (stmt, op_num);
  2294. break;
  2295. case LSHIFT_EXPR:
  2296. case RSHIFT_EXPR:
  2297. case LROTATE_EXPR:
  2298. case RROTATE_EXPR:
  2299. op = gimple_op (stmt, op_num + 1);
  2300. /* Unlike the other binary operators, shifts/rotates have
  2301. the shift count being int, instead of the same type as
  2302. the lhs, so make sure the scalar is the right type if
  2303. we are dealing with vectors of
  2304. long long/long/short/char. */
  2305. if (op_num == 1 && TREE_CODE (op) == INTEGER_CST)
  2306. op = fold_convert (TREE_TYPE (vector_type), op);
  2307. break;
  2308. default:
  2309. op = gimple_op (stmt, op_num + 1);
  2310. break;
  2311. }
  2312. }
  2313. if (reduc_index != -1)
  2314. {
  2315. loop = (gimple_bb (stmt))->loop_father;
  2316. def_stmt = SSA_NAME_DEF_STMT (op);
  2317. gcc_assert (loop);
  2318. /* Get the def before the loop. In reduction chain we have only
  2319. one initial value. */
  2320. if ((j != (number_of_copies - 1)
  2321. || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
  2322. && i != 0))
  2323. && neutral_op)
  2324. op = neutral_op;
  2325. else
  2326. op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
  2327. loop_preheader_edge (loop));
  2328. }
  2329. /* Create 'vect_ = {op0,op1,...,opn}'. */
  2330. number_of_places_left_in_vector--;
  2331. if (!types_compatible_p (TREE_TYPE (vector_type), TREE_TYPE (op)))
  2332. {
  2333. if (CONSTANT_CLASS_P (op))
  2334. {
  2335. op = fold_unary (VIEW_CONVERT_EXPR,
  2336. TREE_TYPE (vector_type), op);
  2337. gcc_assert (op && CONSTANT_CLASS_P (op));
  2338. }
  2339. else
  2340. {
  2341. tree new_temp = make_ssa_name (TREE_TYPE (vector_type));
  2342. gimple init_stmt;
  2343. op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vector_type), op);
  2344. init_stmt
  2345. = gimple_build_assign (new_temp, VIEW_CONVERT_EXPR, op);
  2346. gimple_seq_add_stmt (&ctor_seq, init_stmt);
  2347. op = new_temp;
  2348. }
  2349. }
  2350. elts[number_of_places_left_in_vector] = op;
  2351. if (!CONSTANT_CLASS_P (op))
  2352. constant_p = false;
  2353. if (number_of_places_left_in_vector == 0)
  2354. {
  2355. number_of_places_left_in_vector = nunits;
  2356. if (constant_p)
  2357. vec_cst = build_vector (vector_type, elts);
  2358. else
  2359. {
  2360. vec<constructor_elt, va_gc> *v;
  2361. unsigned k;
  2362. vec_alloc (v, nunits);
  2363. for (k = 0; k < nunits; ++k)
  2364. CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]);
  2365. vec_cst = build_constructor (vector_type, v);
  2366. }
  2367. voprnds.quick_push (vect_init_vector (stmt, vec_cst,
  2368. vector_type, NULL));
  2369. if (ctor_seq != NULL)
  2370. {
  2371. gimple init_stmt = SSA_NAME_DEF_STMT (voprnds.last ());
  2372. gimple_stmt_iterator gsi = gsi_for_stmt (init_stmt);
  2373. gsi_insert_seq_before_without_update (&gsi, ctor_seq,
  2374. GSI_SAME_STMT);
  2375. ctor_seq = NULL;
  2376. }
  2377. }
  2378. }
  2379. }
  2380. /* Since the vectors are created in the reverse order, we should invert
  2381. them. */
  2382. vec_num = voprnds.length ();
  2383. for (j = vec_num; j != 0; j--)
  2384. {
  2385. vop = voprnds[j - 1];
  2386. vec_oprnds->quick_push (vop);
  2387. }
  2388. voprnds.release ();
  2389. /* In case that VF is greater than the unrolling factor needed for the SLP
  2390. group of stmts, NUMBER_OF_VECTORS to be created is greater than
  2391. NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
  2392. to replicate the vectors. */
  2393. while (number_of_vectors > vec_oprnds->length ())
  2394. {
  2395. tree neutral_vec = NULL;
  2396. if (neutral_op)
  2397. {
  2398. if (!neutral_vec)
  2399. neutral_vec = build_vector_from_val (vector_type, neutral_op);
  2400. vec_oprnds->quick_push (neutral_vec);
  2401. }
  2402. else
  2403. {
  2404. for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++)
  2405. vec_oprnds->quick_push (vop);
  2406. }
  2407. }
  2408. }
  2409. /* Get vectorized definitions from SLP_NODE that contains corresponding
  2410. vectorized def-stmts. */
  2411. static void
  2412. vect_get_slp_vect_defs (slp_tree slp_node, vec<tree> *vec_oprnds)
  2413. {
  2414. tree vec_oprnd;
  2415. gimple vec_def_stmt;
  2416. unsigned int i;
  2417. gcc_assert (SLP_TREE_VEC_STMTS (slp_node).exists ());
  2418. FOR_EACH_VEC_ELT (SLP_TREE_VEC_STMTS (slp_node), i, vec_def_stmt)
  2419. {
  2420. gcc_assert (vec_def_stmt);
  2421. vec_oprnd = gimple_get_lhs (vec_def_stmt);
  2422. vec_oprnds->quick_push (vec_oprnd);
  2423. }
  2424. }
  2425. /* Get vectorized definitions for SLP_NODE.
  2426. If the scalar definitions are loop invariants or constants, collect them and
  2427. call vect_get_constant_vectors() to create vector stmts.
  2428. Otherwise, the def-stmts must be already vectorized and the vectorized stmts
  2429. must be stored in the corresponding child of SLP_NODE, and we call
  2430. vect_get_slp_vect_defs () to retrieve them. */
  2431. void
  2432. vect_get_slp_defs (vec<tree> ops, slp_tree slp_node,
  2433. vec<vec<tree> > *vec_oprnds, int reduc_index)
  2434. {
  2435. gimple first_stmt;
  2436. int number_of_vects = 0, i;
  2437. unsigned int child_index = 0;
  2438. HOST_WIDE_INT lhs_size_unit, rhs_size_unit;
  2439. slp_tree child = NULL;
  2440. vec<tree> vec_defs;
  2441. tree oprnd;
  2442. bool vectorized_defs;
  2443. first_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[0];
  2444. FOR_EACH_VEC_ELT (ops, i, oprnd)
  2445. {
  2446. /* For each operand we check if it has vectorized definitions in a child
  2447. node or we need to create them (for invariants and constants). We
  2448. check if the LHS of the first stmt of the next child matches OPRND.
  2449. If it does, we found the correct child. Otherwise, we call
  2450. vect_get_constant_vectors (), and not advance CHILD_INDEX in order
  2451. to check this child node for the next operand. */
  2452. vectorized_defs = false;
  2453. if (SLP_TREE_CHILDREN (slp_node).length () > child_index)
  2454. {
  2455. child = SLP_TREE_CHILDREN (slp_node)[child_index];
  2456. /* We have to check both pattern and original def, if available. */
  2457. gimple first_def = SLP_TREE_SCALAR_STMTS (child)[0];
  2458. gimple related = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first_def));
  2459. if (operand_equal_p (oprnd, gimple_get_lhs (first_def), 0)
  2460. || (related
  2461. && operand_equal_p (oprnd, gimple_get_lhs (related), 0)))
  2462. {
  2463. /* The number of vector defs is determined by the number of
  2464. vector statements in the node from which we get those
  2465. statements. */
  2466. number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (child);
  2467. vectorized_defs = true;
  2468. child_index++;
  2469. }
  2470. }
  2471. if (!vectorized_defs)
  2472. {
  2473. if (i == 0)
  2474. {
  2475. number_of_vects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
  2476. /* Number of vector stmts was calculated according to LHS in
  2477. vect_schedule_slp_instance (), fix it by replacing LHS with
  2478. RHS, if necessary. See vect_get_smallest_scalar_type () for
  2479. details. */
  2480. vect_get_smallest_scalar_type (first_stmt, &lhs_size_unit,
  2481. &rhs_size_unit);
  2482. if (rhs_size_unit != lhs_size_unit)
  2483. {
  2484. number_of_vects *= rhs_size_unit;
  2485. number_of_vects /= lhs_size_unit;
  2486. }
  2487. }
  2488. }
  2489. /* Allocate memory for vectorized defs. */
  2490. vec_defs = vNULL;
  2491. vec_defs.create (number_of_vects);
  2492. /* For reduction defs we call vect_get_constant_vectors (), since we are
  2493. looking for initial loop invariant values. */
  2494. if (vectorized_defs && reduc_index == -1)
  2495. /* The defs are already vectorized. */
  2496. vect_get_slp_vect_defs (child, &vec_defs);
  2497. else
  2498. /* Build vectors from scalar defs. */
  2499. vect_get_constant_vectors (oprnd, slp_node, &vec_defs, i,
  2500. number_of_vects, reduc_index);
  2501. vec_oprnds->quick_push (vec_defs);
  2502. /* For reductions, we only need initial values. */
  2503. if (reduc_index != -1)
  2504. return;
  2505. }
  2506. }
  2507. /* Create NCOPIES permutation statements using the mask MASK_BYTES (by
  2508. building a vector of type MASK_TYPE from it) and two input vectors placed in
  2509. DR_CHAIN at FIRST_VEC_INDX and SECOND_VEC_INDX for the first copy and
  2510. shifting by STRIDE elements of DR_CHAIN for every copy.
  2511. (STRIDE is the number of vectorized stmts for NODE divided by the number of
  2512. copies).
  2513. VECT_STMTS_COUNTER specifies the index in the vectorized stmts of NODE, where
  2514. the created stmts must be inserted. */
  2515. static inline void
  2516. vect_create_mask_and_perm (gimple stmt, gimple next_scalar_stmt,
  2517. tree mask, int first_vec_indx, int second_vec_indx,
  2518. gimple_stmt_iterator *gsi, slp_tree node,
  2519. tree vectype, vec<tree> dr_chain,
  2520. int ncopies, int vect_stmts_counter)
  2521. {
  2522. tree perm_dest;
  2523. gimple perm_stmt = NULL;
  2524. stmt_vec_info next_stmt_info;
  2525. int i, stride;
  2526. tree first_vec, second_vec, data_ref;
  2527. stride = SLP_TREE_NUMBER_OF_VEC_STMTS (node) / ncopies;
  2528. /* Initialize the vect stmts of NODE to properly insert the generated
  2529. stmts later. */
  2530. for (i = SLP_TREE_VEC_STMTS (node).length ();
  2531. i < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (node); i++)
  2532. SLP_TREE_VEC_STMTS (node).quick_push (NULL);
  2533. perm_dest = vect_create_destination_var (gimple_assign_lhs (stmt), vectype);
  2534. for (i = 0; i < ncopies; i++)
  2535. {
  2536. first_vec = dr_chain[first_vec_indx];
  2537. second_vec = dr_chain[second_vec_indx];
  2538. /* Generate the permute statement. */
  2539. perm_stmt = gimple_build_assign (perm_dest, VEC_PERM_EXPR,
  2540. first_vec, second_vec, mask);
  2541. data_ref = make_ssa_name (perm_dest, perm_stmt);
  2542. gimple_set_lhs (perm_stmt, data_ref);
  2543. vect_finish_stmt_generation (stmt, perm_stmt, gsi);
  2544. /* Store the vector statement in NODE. */
  2545. SLP_TREE_VEC_STMTS (node)[stride * i + vect_stmts_counter] = perm_stmt;
  2546. first_vec_indx += stride;
  2547. second_vec_indx += stride;
  2548. }
  2549. /* Mark the scalar stmt as vectorized. */
  2550. next_stmt_info = vinfo_for_stmt (next_scalar_stmt);
  2551. STMT_VINFO_VEC_STMT (next_stmt_info) = perm_stmt;
  2552. }
  2553. /* Given FIRST_MASK_ELEMENT - the mask element in element representation,
  2554. return in CURRENT_MASK_ELEMENT its equivalent in target specific
  2555. representation. Check that the mask is valid and return FALSE if not.
  2556. Return TRUE in NEED_NEXT_VECTOR if the permutation requires to move to
  2557. the next vector, i.e., the current first vector is not needed. */
  2558. static bool
  2559. vect_get_mask_element (gimple stmt, int first_mask_element, int m,
  2560. int mask_nunits, bool only_one_vec, int index,
  2561. unsigned char *mask, int *current_mask_element,
  2562. bool *need_next_vector, int *number_of_mask_fixes,
  2563. bool *mask_fixed, bool *needs_first_vector)
  2564. {
  2565. int i;
  2566. /* Convert to target specific representation. */
  2567. *current_mask_element = first_mask_element + m;
  2568. /* Adjust the value in case it's a mask for second and third vectors. */
  2569. *current_mask_element -= mask_nunits * (*number_of_mask_fixes - 1);
  2570. if (*current_mask_element < mask_nunits)
  2571. *needs_first_vector = true;
  2572. /* We have only one input vector to permute but the mask accesses values in
  2573. the next vector as well. */
  2574. if (only_one_vec && *current_mask_element >= mask_nunits)
  2575. {
  2576. if (dump_enabled_p ())
  2577. {
  2578. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2579. "permutation requires at least two vectors ");
  2580. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  2581. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  2582. }
  2583. return false;
  2584. }
  2585. /* The mask requires the next vector. */
  2586. while (*current_mask_element >= mask_nunits * 2)
  2587. {
  2588. if (*needs_first_vector || *mask_fixed)
  2589. {
  2590. /* We either need the first vector too or have already moved to the
  2591. next vector. In both cases, this permutation needs three
  2592. vectors. */
  2593. if (dump_enabled_p ())
  2594. {
  2595. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2596. "permutation requires at "
  2597. "least three vectors ");
  2598. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  2599. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  2600. }
  2601. return false;
  2602. }
  2603. /* We move to the next vector, dropping the first one and working with
  2604. the second and the third - we need to adjust the values of the mask
  2605. accordingly. */
  2606. *current_mask_element -= mask_nunits * *number_of_mask_fixes;
  2607. for (i = 0; i < index; i++)
  2608. mask[i] -= mask_nunits * *number_of_mask_fixes;
  2609. (*number_of_mask_fixes)++;
  2610. *mask_fixed = true;
  2611. }
  2612. *need_next_vector = *mask_fixed;
  2613. /* This was the last element of this mask. Start a new one. */
  2614. if (index == mask_nunits - 1)
  2615. {
  2616. *number_of_mask_fixes = 1;
  2617. *mask_fixed = false;
  2618. *needs_first_vector = false;
  2619. }
  2620. return true;
  2621. }
  2622. /* Generate vector permute statements from a list of loads in DR_CHAIN.
  2623. If ANALYZE_ONLY is TRUE, only check that it is possible to create valid
  2624. permute statements for the SLP node NODE of the SLP instance
  2625. SLP_NODE_INSTANCE. */
  2626. bool
  2627. vect_transform_slp_perm_load (slp_tree node, vec<tree> dr_chain,
  2628. gimple_stmt_iterator *gsi, int vf,
  2629. slp_instance slp_node_instance, bool analyze_only)
  2630. {
  2631. gimple stmt = SLP_TREE_SCALAR_STMTS (node)[0];
  2632. stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
  2633. tree mask_element_type = NULL_TREE, mask_type;
  2634. int i, j, k, nunits, vec_index = 0, scalar_index;
  2635. tree vectype = STMT_VINFO_VECTYPE (stmt_info);
  2636. gimple next_scalar_stmt;
  2637. int group_size = SLP_INSTANCE_GROUP_SIZE (slp_node_instance);
  2638. int first_mask_element;
  2639. int index, unroll_factor, current_mask_element, ncopies;
  2640. unsigned char *mask;
  2641. bool only_one_vec = false, need_next_vector = false;
  2642. int first_vec_index, second_vec_index, orig_vec_stmts_num, vect_stmts_counter;
  2643. int number_of_mask_fixes = 1;
  2644. bool mask_fixed = false;
  2645. bool needs_first_vector = false;
  2646. machine_mode mode;
  2647. mode = TYPE_MODE (vectype);
  2648. if (!can_vec_perm_p (mode, false, NULL))
  2649. {
  2650. if (dump_enabled_p ())
  2651. {
  2652. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  2653. "no vect permute for ");
  2654. dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
  2655. dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
  2656. }
  2657. return false;
  2658. }
  2659. /* The generic VEC_PERM_EXPR code always uses an integral type of the
  2660. same size as the vector element being permuted. */
  2661. mask_element_type = lang_hooks.types.type_for_mode
  2662. (int_mode_for_mode (TYPE_MODE (TREE_TYPE (vectype))), 1);
  2663. mask_type = get_vectype_for_scalar_type (mask_element_type);
  2664. nunits = TYPE_VECTOR_SUBPARTS (vectype);
  2665. mask = XALLOCAVEC (unsigned char, nunits);
  2666. unroll_factor = SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
  2667. /* The number of vector stmts to generate based only on SLP_NODE_INSTANCE
  2668. unrolling factor. */
  2669. orig_vec_stmts_num = group_size *
  2670. SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance) / nunits;
  2671. if (orig_vec_stmts_num == 1)
  2672. only_one_vec = true;
  2673. /* Number of copies is determined by the final vectorization factor
  2674. relatively to SLP_NODE_INSTANCE unrolling factor. */
  2675. ncopies = vf / SLP_INSTANCE_UNROLLING_FACTOR (slp_node_instance);
  2676. if (!STMT_VINFO_GROUPED_ACCESS (stmt_info))
  2677. return false;
  2678. /* Generate permutation masks for every NODE. Number of masks for each NODE
  2679. is equal to GROUP_SIZE.
  2680. E.g., we have a group of three nodes with three loads from the same
  2681. location in each node, and the vector size is 4. I.e., we have a
  2682. a0b0c0a1b1c1... sequence and we need to create the following vectors:
  2683. for a's: a0a0a0a1 a1a1a2a2 a2a3a3a3
  2684. for b's: b0b0b0b1 b1b1b2b2 b2b3b3b3
  2685. ...
  2686. The masks for a's should be: {0,0,0,3} {3,3,6,6} {6,9,9,9}.
  2687. The last mask is illegal since we assume two operands for permute
  2688. operation, and the mask element values can't be outside that range.
  2689. Hence, the last mask must be converted into {2,5,5,5}.
  2690. For the first two permutations we need the first and the second input
  2691. vectors: {a0,b0,c0,a1} and {b1,c1,a2,b2}, and for the last permutation
  2692. we need the second and the third vectors: {b1,c1,a2,b2} and
  2693. {c2,a3,b3,c3}. */
  2694. {
  2695. scalar_index = 0;
  2696. index = 0;
  2697. vect_stmts_counter = 0;
  2698. vec_index = 0;
  2699. first_vec_index = vec_index++;
  2700. if (only_one_vec)
  2701. second_vec_index = first_vec_index;
  2702. else
  2703. second_vec_index = vec_index++;
  2704. for (j = 0; j < unroll_factor; j++)
  2705. {
  2706. for (k = 0; k < group_size; k++)
  2707. {
  2708. i = SLP_TREE_LOAD_PERMUTATION (node)[k];
  2709. first_mask_element = i + j * group_size;
  2710. if (!vect_get_mask_element (stmt, first_mask_element, 0,
  2711. nunits, only_one_vec, index,
  2712. mask, &current_mask_element,
  2713. &need_next_vector,
  2714. &number_of_mask_fixes, &mask_fixed,
  2715. &needs_first_vector))
  2716. return false;
  2717. gcc_assert (current_mask_element < 2 * nunits);
  2718. mask[index++] = current_mask_element;
  2719. if (index == nunits)
  2720. {
  2721. index = 0;
  2722. if (!can_vec_perm_p (mode, false, mask))
  2723. {
  2724. if (dump_enabled_p ())
  2725. {
  2726. dump_printf_loc (MSG_MISSED_OPTIMIZATION,
  2727. vect_location,
  2728. "unsupported vect permute { ");
  2729. for (i = 0; i < nunits; ++i)
  2730. dump_printf (MSG_MISSED_OPTIMIZATION, "%d ",
  2731. mask[i]);
  2732. dump_printf (MSG_MISSED_OPTIMIZATION, "}\n");
  2733. }
  2734. return false;
  2735. }
  2736. if (!analyze_only)
  2737. {
  2738. int l;
  2739. tree mask_vec, *mask_elts;
  2740. mask_elts = XALLOCAVEC (tree, nunits);
  2741. for (l = 0; l < nunits; ++l)
  2742. mask_elts[l] = build_int_cst (mask_element_type,
  2743. mask[l]);
  2744. mask_vec = build_vector (mask_type, mask_elts);
  2745. if (need_next_vector)
  2746. {
  2747. first_vec_index = second_vec_index;
  2748. second_vec_index = vec_index;
  2749. }
  2750. next_scalar_stmt
  2751. = SLP_TREE_SCALAR_STMTS (node)[scalar_index++];
  2752. vect_create_mask_and_perm (stmt, next_scalar_stmt,
  2753. mask_vec, first_vec_index, second_vec_index,
  2754. gsi, node, vectype, dr_chain,
  2755. ncopies, vect_stmts_counter++);
  2756. }
  2757. }
  2758. }
  2759. }
  2760. }
  2761. return true;
  2762. }
  2763. /* Vectorize SLP instance tree in postorder. */
  2764. static bool
  2765. vect_schedule_slp_instance (slp_tree node, slp_instance instance,
  2766. unsigned int vectorization_factor)
  2767. {
  2768. gimple stmt;
  2769. bool grouped_store, is_store;
  2770. gimple_stmt_iterator si;
  2771. stmt_vec_info stmt_info;
  2772. unsigned int vec_stmts_size, nunits, group_size;
  2773. tree vectype;
  2774. int i;
  2775. slp_tree child;
  2776. if (!node)
  2777. return false;
  2778. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  2779. vect_schedule_slp_instance (child, instance, vectorization_factor);
  2780. stmt = SLP_TREE_SCALAR_STMTS (node)[0];
  2781. stmt_info = vinfo_for_stmt (stmt);
  2782. /* VECTYPE is the type of the destination. */
  2783. vectype = STMT_VINFO_VECTYPE (stmt_info);
  2784. nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (vectype);
  2785. group_size = SLP_INSTANCE_GROUP_SIZE (instance);
  2786. /* For each SLP instance calculate number of vector stmts to be created
  2787. for the scalar stmts in each node of the SLP tree. Number of vector
  2788. elements in one vector iteration is the number of scalar elements in
  2789. one scalar iteration (GROUP_SIZE) multiplied by VF divided by vector
  2790. size. */
  2791. vec_stmts_size = (vectorization_factor * group_size) / nunits;
  2792. if (!SLP_TREE_VEC_STMTS (node).exists ())
  2793. {
  2794. SLP_TREE_VEC_STMTS (node).create (vec_stmts_size);
  2795. SLP_TREE_NUMBER_OF_VEC_STMTS (node) = vec_stmts_size;
  2796. }
  2797. if (dump_enabled_p ())
  2798. {
  2799. dump_printf_loc (MSG_NOTE,vect_location,
  2800. "------>vectorizing SLP node starting from: ");
  2801. dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
  2802. dump_printf (MSG_NOTE, "\n");
  2803. }
  2804. /* Loads should be inserted before the first load. */
  2805. if (SLP_INSTANCE_FIRST_LOAD_STMT (instance)
  2806. && STMT_VINFO_GROUPED_ACCESS (stmt_info)
  2807. && !REFERENCE_CLASS_P (gimple_get_lhs (stmt))
  2808. && SLP_TREE_LOAD_PERMUTATION (node).exists ())
  2809. si = gsi_for_stmt (SLP_INSTANCE_FIRST_LOAD_STMT (instance));
  2810. else if (is_pattern_stmt_p (stmt_info))
  2811. si = gsi_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
  2812. else
  2813. si = gsi_for_stmt (stmt);
  2814. /* Stores should be inserted just before the last store. */
  2815. if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
  2816. && REFERENCE_CLASS_P (gimple_get_lhs (stmt)))
  2817. {
  2818. gimple last_store = vect_find_last_store_in_slp_instance (instance);
  2819. if (is_pattern_stmt_p (vinfo_for_stmt (last_store)))
  2820. last_store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (last_store));
  2821. si = gsi_for_stmt (last_store);
  2822. }
  2823. /* Mark the first element of the reduction chain as reduction to properly
  2824. transform the node. In the analysis phase only the last element of the
  2825. chain is marked as reduction. */
  2826. if (GROUP_FIRST_ELEMENT (stmt_info) && !STMT_VINFO_GROUPED_ACCESS (stmt_info)
  2827. && GROUP_FIRST_ELEMENT (stmt_info) == stmt)
  2828. {
  2829. STMT_VINFO_DEF_TYPE (stmt_info) = vect_reduction_def;
  2830. STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
  2831. }
  2832. is_store = vect_transform_stmt (stmt, &si, &grouped_store, node, instance);
  2833. return is_store;
  2834. }
  2835. /* Replace scalar calls from SLP node NODE with setting of their lhs to zero.
  2836. For loop vectorization this is done in vectorizable_call, but for SLP
  2837. it needs to be deferred until end of vect_schedule_slp, because multiple
  2838. SLP instances may refer to the same scalar stmt. */
  2839. static void
  2840. vect_remove_slp_scalar_calls (slp_tree node)
  2841. {
  2842. gimple stmt, new_stmt;
  2843. gimple_stmt_iterator gsi;
  2844. int i;
  2845. slp_tree child;
  2846. tree lhs;
  2847. stmt_vec_info stmt_info;
  2848. if (!node)
  2849. return;
  2850. FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
  2851. vect_remove_slp_scalar_calls (child);
  2852. FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (node), i, stmt)
  2853. {
  2854. if (!is_gimple_call (stmt) || gimple_bb (stmt) == NULL)
  2855. continue;
  2856. stmt_info = vinfo_for_stmt (stmt);
  2857. if (stmt_info == NULL
  2858. || is_pattern_stmt_p (stmt_info)
  2859. || !PURE_SLP_STMT (stmt_info))
  2860. continue;
  2861. lhs = gimple_call_lhs (stmt);
  2862. new_stmt = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
  2863. set_vinfo_for_stmt (new_stmt, stmt_info);
  2864. set_vinfo_for_stmt (stmt, NULL);
  2865. STMT_VINFO_STMT (stmt_info) = new_stmt;
  2866. gsi = gsi_for_stmt (stmt);
  2867. gsi_replace (&gsi, new_stmt, false);
  2868. SSA_NAME_DEF_STMT (gimple_assign_lhs (new_stmt)) = new_stmt;
  2869. }
  2870. }
  2871. /* Generate vector code for all SLP instances in the loop/basic block. */
  2872. bool
  2873. vect_schedule_slp (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
  2874. {
  2875. vec<slp_instance> slp_instances;
  2876. slp_instance instance;
  2877. unsigned int i, vf;
  2878. bool is_store = false;
  2879. if (loop_vinfo)
  2880. {
  2881. slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
  2882. vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
  2883. }
  2884. else
  2885. {
  2886. slp_instances = BB_VINFO_SLP_INSTANCES (bb_vinfo);
  2887. vf = 1;
  2888. }
  2889. FOR_EACH_VEC_ELT (slp_instances, i, instance)
  2890. {
  2891. /* Schedule the tree of INSTANCE. */
  2892. is_store = vect_schedule_slp_instance (SLP_INSTANCE_TREE (instance),
  2893. instance, vf);
  2894. if (dump_enabled_p ())
  2895. dump_printf_loc (MSG_NOTE, vect_location,
  2896. "vectorizing stmts using SLP.\n");
  2897. }
  2898. FOR_EACH_VEC_ELT (slp_instances, i, instance)
  2899. {
  2900. slp_tree root = SLP_INSTANCE_TREE (instance);
  2901. gimple store;
  2902. unsigned int j;
  2903. gimple_stmt_iterator gsi;
  2904. /* Remove scalar call stmts. Do not do this for basic-block
  2905. vectorization as not all uses may be vectorized.
  2906. ??? Why should this be necessary? DCE should be able to
  2907. remove the stmts itself.
  2908. ??? For BB vectorization we can as well remove scalar
  2909. stmts starting from the SLP tree root if they have no
  2910. uses. */
  2911. if (loop_vinfo)
  2912. vect_remove_slp_scalar_calls (root);
  2913. for (j = 0; SLP_TREE_SCALAR_STMTS (root).iterate (j, &store)
  2914. && j < SLP_INSTANCE_GROUP_SIZE (instance); j++)
  2915. {
  2916. if (!STMT_VINFO_DATA_REF (vinfo_for_stmt (store)))
  2917. break;
  2918. if (is_pattern_stmt_p (vinfo_for_stmt (store)))
  2919. store = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (store));
  2920. /* Free the attached stmt_vec_info and remove the stmt. */
  2921. gsi = gsi_for_stmt (store);
  2922. unlink_stmt_vdef (store);
  2923. gsi_remove (&gsi, true);
  2924. release_defs (store);
  2925. free_stmt_vec_info (store);
  2926. }
  2927. }
  2928. return is_store;
  2929. }
  2930. /* Vectorize the basic block. */
  2931. void
  2932. vect_slp_transform_bb (basic_block bb)
  2933. {
  2934. bb_vec_info bb_vinfo = vec_info_for_bb (bb);
  2935. gimple_stmt_iterator si;
  2936. gcc_assert (bb_vinfo);
  2937. if (dump_enabled_p ())
  2938. dump_printf_loc (MSG_NOTE, vect_location, "SLPing BB\n");
  2939. for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
  2940. {
  2941. gimple stmt = gsi_stmt (si);
  2942. stmt_vec_info stmt_info;
  2943. if (dump_enabled_p ())
  2944. {
  2945. dump_printf_loc (MSG_NOTE, vect_location,
  2946. "------>SLPing statement: ");
  2947. dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
  2948. dump_printf (MSG_NOTE, "\n");
  2949. }
  2950. stmt_info = vinfo_for_stmt (stmt);
  2951. gcc_assert (stmt_info);
  2952. /* Schedule all the SLP instances when the first SLP stmt is reached. */
  2953. if (STMT_SLP_TYPE (stmt_info))
  2954. {
  2955. vect_schedule_slp (NULL, bb_vinfo);
  2956. break;
  2957. }
  2958. }
  2959. if (dump_enabled_p ())
  2960. dump_printf_loc (MSG_NOTE, vect_location,
  2961. "BASIC BLOCK VECTORIZED\n");
  2962. destroy_bb_vec_info (bb_vinfo);
  2963. }