tree-loop-distribution.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /* Loop distribution.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. Contributed by Georges-Andre Silber <Georges-Andre.Silber@ensmp.fr>
  4. and Sebastian Pop <sebastian.pop@amd.com>.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 3, or (at your option) any
  9. later version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT
  11. ANY 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. /* This pass performs loop distribution: for example, the loop
  18. |DO I = 2, N
  19. | A(I) = B(I) + C
  20. | D(I) = A(I-1)*E
  21. |ENDDO
  22. is transformed to
  23. |DOALL I = 2, N
  24. | A(I) = B(I) + C
  25. |ENDDO
  26. |
  27. |DOALL I = 2, N
  28. | D(I) = A(I-1)*E
  29. |ENDDO
  30. This pass uses an RDG, Reduced Dependence Graph built on top of the
  31. data dependence relations. The RDG is then topologically sorted to
  32. obtain a map of information producers/consumers based on which it
  33. generates the new loops. */
  34. #include "config.h"
  35. #include "system.h"
  36. #include "coretypes.h"
  37. #include "hash-set.h"
  38. #include "machmode.h"
  39. #include "vec.h"
  40. #include "double-int.h"
  41. #include "input.h"
  42. #include "alias.h"
  43. #include "symtab.h"
  44. #include "options.h"
  45. #include "wide-int.h"
  46. #include "inchash.h"
  47. #include "tree.h"
  48. #include "fold-const.h"
  49. #include "predict.h"
  50. #include "tm.h"
  51. #include "hard-reg-set.h"
  52. #include "input.h"
  53. #include "function.h"
  54. #include "dominance.h"
  55. #include "cfg.h"
  56. #include "cfganal.h"
  57. #include "basic-block.h"
  58. #include "tree-ssa-alias.h"
  59. #include "internal-fn.h"
  60. #include "gimple-expr.h"
  61. #include "is-a.h"
  62. #include "gimple.h"
  63. #include "gimple-iterator.h"
  64. #include "gimplify-me.h"
  65. #include "stor-layout.h"
  66. #include "gimple-ssa.h"
  67. #include "tree-cfg.h"
  68. #include "tree-phinodes.h"
  69. #include "ssa-iterators.h"
  70. #include "stringpool.h"
  71. #include "tree-ssanames.h"
  72. #include "tree-ssa-loop-manip.h"
  73. #include "tree-ssa-loop.h"
  74. #include "tree-into-ssa.h"
  75. #include "tree-ssa.h"
  76. #include "cfgloop.h"
  77. #include "tree-chrec.h"
  78. #include "tree-data-ref.h"
  79. #include "tree-scalar-evolution.h"
  80. #include "tree-pass.h"
  81. #include "gimple-pretty-print.h"
  82. #include "tree-vectorizer.h"
  83. /* A Reduced Dependence Graph (RDG) vertex representing a statement. */
  84. typedef struct rdg_vertex
  85. {
  86. /* The statement represented by this vertex. */
  87. gimple stmt;
  88. /* Vector of data-references in this statement. */
  89. vec<data_reference_p> datarefs;
  90. /* True when the statement contains a write to memory. */
  91. bool has_mem_write;
  92. /* True when the statement contains a read from memory. */
  93. bool has_mem_reads;
  94. } *rdg_vertex_p;
  95. #define RDGV_STMT(V) ((struct rdg_vertex *) ((V)->data))->stmt
  96. #define RDGV_DATAREFS(V) ((struct rdg_vertex *) ((V)->data))->datarefs
  97. #define RDGV_HAS_MEM_WRITE(V) ((struct rdg_vertex *) ((V)->data))->has_mem_write
  98. #define RDGV_HAS_MEM_READS(V) ((struct rdg_vertex *) ((V)->data))->has_mem_reads
  99. #define RDG_STMT(RDG, I) RDGV_STMT (&(RDG->vertices[I]))
  100. #define RDG_DATAREFS(RDG, I) RDGV_DATAREFS (&(RDG->vertices[I]))
  101. #define RDG_MEM_WRITE_STMT(RDG, I) RDGV_HAS_MEM_WRITE (&(RDG->vertices[I]))
  102. #define RDG_MEM_READS_STMT(RDG, I) RDGV_HAS_MEM_READS (&(RDG->vertices[I]))
  103. /* Data dependence type. */
  104. enum rdg_dep_type
  105. {
  106. /* Read After Write (RAW). */
  107. flow_dd = 'f',
  108. /* Control dependence (execute conditional on). */
  109. control_dd = 'c'
  110. };
  111. /* Dependence information attached to an edge of the RDG. */
  112. typedef struct rdg_edge
  113. {
  114. /* Type of the dependence. */
  115. enum rdg_dep_type type;
  116. } *rdg_edge_p;
  117. #define RDGE_TYPE(E) ((struct rdg_edge *) ((E)->data))->type
  118. /* Dump vertex I in RDG to FILE. */
  119. static void
  120. dump_rdg_vertex (FILE *file, struct graph *rdg, int i)
  121. {
  122. struct vertex *v = &(rdg->vertices[i]);
  123. struct graph_edge *e;
  124. fprintf (file, "(vertex %d: (%s%s) (in:", i,
  125. RDG_MEM_WRITE_STMT (rdg, i) ? "w" : "",
  126. RDG_MEM_READS_STMT (rdg, i) ? "r" : "");
  127. if (v->pred)
  128. for (e = v->pred; e; e = e->pred_next)
  129. fprintf (file, " %d", e->src);
  130. fprintf (file, ") (out:");
  131. if (v->succ)
  132. for (e = v->succ; e; e = e->succ_next)
  133. fprintf (file, " %d", e->dest);
  134. fprintf (file, ")\n");
  135. print_gimple_stmt (file, RDGV_STMT (v), 0, TDF_VOPS|TDF_MEMSYMS);
  136. fprintf (file, ")\n");
  137. }
  138. /* Call dump_rdg_vertex on stderr. */
  139. DEBUG_FUNCTION void
  140. debug_rdg_vertex (struct graph *rdg, int i)
  141. {
  142. dump_rdg_vertex (stderr, rdg, i);
  143. }
  144. /* Dump the reduced dependence graph RDG to FILE. */
  145. static void
  146. dump_rdg (FILE *file, struct graph *rdg)
  147. {
  148. fprintf (file, "(rdg\n");
  149. for (int i = 0; i < rdg->n_vertices; i++)
  150. dump_rdg_vertex (file, rdg, i);
  151. fprintf (file, ")\n");
  152. }
  153. /* Call dump_rdg on stderr. */
  154. DEBUG_FUNCTION void
  155. debug_rdg (struct graph *rdg)
  156. {
  157. dump_rdg (stderr, rdg);
  158. }
  159. static void
  160. dot_rdg_1 (FILE *file, struct graph *rdg)
  161. {
  162. int i;
  163. pretty_printer buffer;
  164. pp_needs_newline (&buffer) = false;
  165. buffer.buffer->stream = file;
  166. fprintf (file, "digraph RDG {\n");
  167. for (i = 0; i < rdg->n_vertices; i++)
  168. {
  169. struct vertex *v = &(rdg->vertices[i]);
  170. struct graph_edge *e;
  171. fprintf (file, "%d [label=\"[%d] ", i, i);
  172. pp_gimple_stmt_1 (&buffer, RDGV_STMT (v), 0, TDF_SLIM);
  173. pp_flush (&buffer);
  174. fprintf (file, "\"]\n");
  175. /* Highlight reads from memory. */
  176. if (RDG_MEM_READS_STMT (rdg, i))
  177. fprintf (file, "%d [style=filled, fillcolor=green]\n", i);
  178. /* Highlight stores to memory. */
  179. if (RDG_MEM_WRITE_STMT (rdg, i))
  180. fprintf (file, "%d [style=filled, fillcolor=red]\n", i);
  181. if (v->succ)
  182. for (e = v->succ; e; e = e->succ_next)
  183. switch (RDGE_TYPE (e))
  184. {
  185. case flow_dd:
  186. /* These are the most common dependences: don't print these. */
  187. fprintf (file, "%d -> %d \n", i, e->dest);
  188. break;
  189. case control_dd:
  190. fprintf (file, "%d -> %d [label=control] \n", i, e->dest);
  191. break;
  192. default:
  193. gcc_unreachable ();
  194. }
  195. }
  196. fprintf (file, "}\n\n");
  197. }
  198. /* Display the Reduced Dependence Graph using dotty. */
  199. DEBUG_FUNCTION void
  200. dot_rdg (struct graph *rdg)
  201. {
  202. /* When debugging, you may want to enable the following code. */
  203. #ifdef HAVE_POPEN
  204. FILE *file = popen ("dot -Tx11", "w");
  205. if (!file)
  206. return;
  207. dot_rdg_1 (file, rdg);
  208. fflush (file);
  209. close (fileno (file));
  210. pclose (file);
  211. #else
  212. dot_rdg_1 (stderr, rdg);
  213. #endif
  214. }
  215. /* Returns the index of STMT in RDG. */
  216. static int
  217. rdg_vertex_for_stmt (struct graph *rdg ATTRIBUTE_UNUSED, gimple stmt)
  218. {
  219. int index = gimple_uid (stmt);
  220. gcc_checking_assert (index == -1 || RDG_STMT (rdg, index) == stmt);
  221. return index;
  222. }
  223. /* Creates dependence edges in RDG for all the uses of DEF. IDEF is
  224. the index of DEF in RDG. */
  225. static void
  226. create_rdg_edges_for_scalar (struct graph *rdg, tree def, int idef)
  227. {
  228. use_operand_p imm_use_p;
  229. imm_use_iterator iterator;
  230. FOR_EACH_IMM_USE_FAST (imm_use_p, iterator, def)
  231. {
  232. struct graph_edge *e;
  233. int use = rdg_vertex_for_stmt (rdg, USE_STMT (imm_use_p));
  234. if (use < 0)
  235. continue;
  236. e = add_edge (rdg, idef, use);
  237. e->data = XNEW (struct rdg_edge);
  238. RDGE_TYPE (e) = flow_dd;
  239. }
  240. }
  241. /* Creates an edge for the control dependences of BB to the vertex V. */
  242. static void
  243. create_edge_for_control_dependence (struct graph *rdg, basic_block bb,
  244. int v, control_dependences *cd)
  245. {
  246. bitmap_iterator bi;
  247. unsigned edge_n;
  248. EXECUTE_IF_SET_IN_BITMAP (cd->get_edges_dependent_on (bb->index),
  249. 0, edge_n, bi)
  250. {
  251. basic_block cond_bb = cd->get_edge (edge_n)->src;
  252. gimple stmt = last_stmt (cond_bb);
  253. if (stmt && is_ctrl_stmt (stmt))
  254. {
  255. struct graph_edge *e;
  256. int c = rdg_vertex_for_stmt (rdg, stmt);
  257. if (c < 0)
  258. continue;
  259. e = add_edge (rdg, c, v);
  260. e->data = XNEW (struct rdg_edge);
  261. RDGE_TYPE (e) = control_dd;
  262. }
  263. }
  264. }
  265. /* Creates the edges of the reduced dependence graph RDG. */
  266. static void
  267. create_rdg_flow_edges (struct graph *rdg)
  268. {
  269. int i;
  270. def_operand_p def_p;
  271. ssa_op_iter iter;
  272. for (i = 0; i < rdg->n_vertices; i++)
  273. FOR_EACH_PHI_OR_STMT_DEF (def_p, RDG_STMT (rdg, i),
  274. iter, SSA_OP_DEF)
  275. create_rdg_edges_for_scalar (rdg, DEF_FROM_PTR (def_p), i);
  276. }
  277. /* Creates the edges of the reduced dependence graph RDG. */
  278. static void
  279. create_rdg_cd_edges (struct graph *rdg, control_dependences *cd)
  280. {
  281. int i;
  282. for (i = 0; i < rdg->n_vertices; i++)
  283. {
  284. gimple stmt = RDG_STMT (rdg, i);
  285. if (gimple_code (stmt) == GIMPLE_PHI)
  286. {
  287. edge_iterator ei;
  288. edge e;
  289. FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->preds)
  290. create_edge_for_control_dependence (rdg, e->src, i, cd);
  291. }
  292. else
  293. create_edge_for_control_dependence (rdg, gimple_bb (stmt), i, cd);
  294. }
  295. }
  296. /* Build the vertices of the reduced dependence graph RDG. Return false
  297. if that failed. */
  298. static bool
  299. create_rdg_vertices (struct graph *rdg, vec<gimple> stmts, loop_p loop,
  300. vec<data_reference_p> *datarefs)
  301. {
  302. int i;
  303. gimple stmt;
  304. FOR_EACH_VEC_ELT (stmts, i, stmt)
  305. {
  306. struct vertex *v = &(rdg->vertices[i]);
  307. /* Record statement to vertex mapping. */
  308. gimple_set_uid (stmt, i);
  309. v->data = XNEW (struct rdg_vertex);
  310. RDGV_STMT (v) = stmt;
  311. RDGV_DATAREFS (v).create (0);
  312. RDGV_HAS_MEM_WRITE (v) = false;
  313. RDGV_HAS_MEM_READS (v) = false;
  314. if (gimple_code (stmt) == GIMPLE_PHI)
  315. continue;
  316. unsigned drp = datarefs->length ();
  317. if (!find_data_references_in_stmt (loop, stmt, datarefs))
  318. return false;
  319. for (unsigned j = drp; j < datarefs->length (); ++j)
  320. {
  321. data_reference_p dr = (*datarefs)[j];
  322. if (DR_IS_READ (dr))
  323. RDGV_HAS_MEM_READS (v) = true;
  324. else
  325. RDGV_HAS_MEM_WRITE (v) = true;
  326. RDGV_DATAREFS (v).safe_push (dr);
  327. }
  328. }
  329. return true;
  330. }
  331. /* Initialize STMTS with all the statements of LOOP. The order in
  332. which we discover statements is important as
  333. generate_loops_for_partition is using the same traversal for
  334. identifying statements in loop copies. */
  335. static void
  336. stmts_from_loop (struct loop *loop, vec<gimple> *stmts)
  337. {
  338. unsigned int i;
  339. basic_block *bbs = get_loop_body_in_dom_order (loop);
  340. for (i = 0; i < loop->num_nodes; i++)
  341. {
  342. basic_block bb = bbs[i];
  343. for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
  344. gsi_next (&bsi))
  345. if (!virtual_operand_p (gimple_phi_result (bsi.phi ())))
  346. stmts->safe_push (bsi.phi ());
  347. for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
  348. gsi_next (&bsi))
  349. {
  350. gimple stmt = gsi_stmt (bsi);
  351. if (gimple_code (stmt) != GIMPLE_LABEL && !is_gimple_debug (stmt))
  352. stmts->safe_push (stmt);
  353. }
  354. }
  355. free (bbs);
  356. }
  357. /* Free the reduced dependence graph RDG. */
  358. static void
  359. free_rdg (struct graph *rdg)
  360. {
  361. int i;
  362. for (i = 0; i < rdg->n_vertices; i++)
  363. {
  364. struct vertex *v = &(rdg->vertices[i]);
  365. struct graph_edge *e;
  366. for (e = v->succ; e; e = e->succ_next)
  367. free (e->data);
  368. if (v->data)
  369. {
  370. gimple_set_uid (RDGV_STMT (v), -1);
  371. free_data_refs (RDGV_DATAREFS (v));
  372. free (v->data);
  373. }
  374. }
  375. free_graph (rdg);
  376. }
  377. /* Build the Reduced Dependence Graph (RDG) with one vertex per
  378. statement of the loop nest LOOP_NEST, and one edge per data dependence or
  379. scalar dependence. */
  380. static struct graph *
  381. build_rdg (vec<loop_p> loop_nest, control_dependences *cd)
  382. {
  383. struct graph *rdg;
  384. vec<data_reference_p> datarefs;
  385. /* Create the RDG vertices from the stmts of the loop nest. */
  386. auto_vec<gimple, 10> stmts;
  387. stmts_from_loop (loop_nest[0], &stmts);
  388. rdg = new_graph (stmts.length ());
  389. datarefs.create (10);
  390. if (!create_rdg_vertices (rdg, stmts, loop_nest[0], &datarefs))
  391. {
  392. datarefs.release ();
  393. free_rdg (rdg);
  394. return NULL;
  395. }
  396. stmts.release ();
  397. create_rdg_flow_edges (rdg);
  398. if (cd)
  399. create_rdg_cd_edges (rdg, cd);
  400. datarefs.release ();
  401. return rdg;
  402. }
  403. enum partition_kind {
  404. PKIND_NORMAL, PKIND_MEMSET, PKIND_MEMCPY
  405. };
  406. typedef struct partition_s
  407. {
  408. bitmap stmts;
  409. bitmap loops;
  410. bool reduction_p;
  411. enum partition_kind kind;
  412. /* data-references a kind != PKIND_NORMAL partition is about. */
  413. data_reference_p main_dr;
  414. data_reference_p secondary_dr;
  415. tree niter;
  416. bool plus_one;
  417. } *partition_t;
  418. /* Allocate and initialize a partition from BITMAP. */
  419. static partition_t
  420. partition_alloc (bitmap stmts, bitmap loops)
  421. {
  422. partition_t partition = XCNEW (struct partition_s);
  423. partition->stmts = stmts ? stmts : BITMAP_ALLOC (NULL);
  424. partition->loops = loops ? loops : BITMAP_ALLOC (NULL);
  425. partition->reduction_p = false;
  426. partition->kind = PKIND_NORMAL;
  427. return partition;
  428. }
  429. /* Free PARTITION. */
  430. static void
  431. partition_free (partition_t partition)
  432. {
  433. BITMAP_FREE (partition->stmts);
  434. BITMAP_FREE (partition->loops);
  435. free (partition);
  436. }
  437. /* Returns true if the partition can be generated as a builtin. */
  438. static bool
  439. partition_builtin_p (partition_t partition)
  440. {
  441. return partition->kind != PKIND_NORMAL;
  442. }
  443. /* Returns true if the partition contains a reduction. */
  444. static bool
  445. partition_reduction_p (partition_t partition)
  446. {
  447. return partition->reduction_p;
  448. }
  449. /* Merge PARTITION into the partition DEST. */
  450. static void
  451. partition_merge_into (partition_t dest, partition_t partition)
  452. {
  453. dest->kind = PKIND_NORMAL;
  454. bitmap_ior_into (dest->stmts, partition->stmts);
  455. if (partition_reduction_p (partition))
  456. dest->reduction_p = true;
  457. }
  458. /* Returns true when DEF is an SSA_NAME defined in LOOP and used after
  459. the LOOP. */
  460. static bool
  461. ssa_name_has_uses_outside_loop_p (tree def, loop_p loop)
  462. {
  463. imm_use_iterator imm_iter;
  464. use_operand_p use_p;
  465. FOR_EACH_IMM_USE_FAST (use_p, imm_iter, def)
  466. {
  467. gimple use_stmt = USE_STMT (use_p);
  468. if (!is_gimple_debug (use_stmt)
  469. && loop != loop_containing_stmt (use_stmt))
  470. return true;
  471. }
  472. return false;
  473. }
  474. /* Returns true when STMT defines a scalar variable used after the
  475. loop LOOP. */
  476. static bool
  477. stmt_has_scalar_dependences_outside_loop (loop_p loop, gimple stmt)
  478. {
  479. def_operand_p def_p;
  480. ssa_op_iter op_iter;
  481. if (gimple_code (stmt) == GIMPLE_PHI)
  482. return ssa_name_has_uses_outside_loop_p (gimple_phi_result (stmt), loop);
  483. FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_DEF)
  484. if (ssa_name_has_uses_outside_loop_p (DEF_FROM_PTR (def_p), loop))
  485. return true;
  486. return false;
  487. }
  488. /* Return a copy of LOOP placed before LOOP. */
  489. static struct loop *
  490. copy_loop_before (struct loop *loop)
  491. {
  492. struct loop *res;
  493. edge preheader = loop_preheader_edge (loop);
  494. initialize_original_copy_tables ();
  495. res = slpeel_tree_duplicate_loop_to_edge_cfg (loop, NULL, preheader);
  496. gcc_assert (res != NULL);
  497. free_original_copy_tables ();
  498. delete_update_ssa ();
  499. return res;
  500. }
  501. /* Creates an empty basic block after LOOP. */
  502. static void
  503. create_bb_after_loop (struct loop *loop)
  504. {
  505. edge exit = single_exit (loop);
  506. if (!exit)
  507. return;
  508. split_edge (exit);
  509. }
  510. /* Generate code for PARTITION from the code in LOOP. The loop is
  511. copied when COPY_P is true. All the statements not flagged in the
  512. PARTITION bitmap are removed from the loop or from its copy. The
  513. statements are indexed in sequence inside a basic block, and the
  514. basic blocks of a loop are taken in dom order. */
  515. static void
  516. generate_loops_for_partition (struct loop *loop, partition_t partition,
  517. bool copy_p)
  518. {
  519. unsigned i;
  520. basic_block *bbs;
  521. if (copy_p)
  522. {
  523. loop = copy_loop_before (loop);
  524. gcc_assert (loop != NULL);
  525. create_preheader (loop, CP_SIMPLE_PREHEADERS);
  526. create_bb_after_loop (loop);
  527. }
  528. /* Remove stmts not in the PARTITION bitmap. */
  529. bbs = get_loop_body_in_dom_order (loop);
  530. if (MAY_HAVE_DEBUG_STMTS)
  531. for (i = 0; i < loop->num_nodes; i++)
  532. {
  533. basic_block bb = bbs[i];
  534. for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
  535. gsi_next (&bsi))
  536. {
  537. gphi *phi = bsi.phi ();
  538. if (!virtual_operand_p (gimple_phi_result (phi))
  539. && !bitmap_bit_p (partition->stmts, gimple_uid (phi)))
  540. reset_debug_uses (phi);
  541. }
  542. for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  543. {
  544. gimple stmt = gsi_stmt (bsi);
  545. if (gimple_code (stmt) != GIMPLE_LABEL
  546. && !is_gimple_debug (stmt)
  547. && !bitmap_bit_p (partition->stmts, gimple_uid (stmt)))
  548. reset_debug_uses (stmt);
  549. }
  550. }
  551. for (i = 0; i < loop->num_nodes; i++)
  552. {
  553. basic_block bb = bbs[i];
  554. for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);)
  555. {
  556. gphi *phi = bsi.phi ();
  557. if (!virtual_operand_p (gimple_phi_result (phi))
  558. && !bitmap_bit_p (partition->stmts, gimple_uid (phi)))
  559. remove_phi_node (&bsi, true);
  560. else
  561. gsi_next (&bsi);
  562. }
  563. for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);)
  564. {
  565. gimple stmt = gsi_stmt (bsi);
  566. if (gimple_code (stmt) != GIMPLE_LABEL
  567. && !is_gimple_debug (stmt)
  568. && !bitmap_bit_p (partition->stmts, gimple_uid (stmt)))
  569. {
  570. /* Choose an arbitrary path through the empty CFG part
  571. that this unnecessary control stmt controls. */
  572. if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
  573. {
  574. gimple_cond_make_false (cond_stmt);
  575. update_stmt (stmt);
  576. }
  577. else if (gimple_code (stmt) == GIMPLE_SWITCH)
  578. {
  579. gswitch *switch_stmt = as_a <gswitch *> (stmt);
  580. gimple_switch_set_index
  581. (switch_stmt, CASE_LOW (gimple_switch_label (switch_stmt, 1)));
  582. update_stmt (stmt);
  583. }
  584. else
  585. {
  586. unlink_stmt_vdef (stmt);
  587. gsi_remove (&bsi, true);
  588. release_defs (stmt);
  589. continue;
  590. }
  591. }
  592. gsi_next (&bsi);
  593. }
  594. }
  595. free (bbs);
  596. }
  597. /* Build the size argument for a memory operation call. */
  598. static tree
  599. build_size_arg_loc (location_t loc, data_reference_p dr, tree nb_iter,
  600. bool plus_one)
  601. {
  602. tree size = fold_convert_loc (loc, sizetype, nb_iter);
  603. if (plus_one)
  604. size = size_binop (PLUS_EXPR, size, size_one_node);
  605. size = fold_build2_loc (loc, MULT_EXPR, sizetype, size,
  606. TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
  607. size = fold_convert_loc (loc, size_type_node, size);
  608. return size;
  609. }
  610. /* Build an address argument for a memory operation call. */
  611. static tree
  612. build_addr_arg_loc (location_t loc, data_reference_p dr, tree nb_bytes)
  613. {
  614. tree addr_base;
  615. addr_base = size_binop_loc (loc, PLUS_EXPR, DR_OFFSET (dr), DR_INIT (dr));
  616. addr_base = fold_convert_loc (loc, sizetype, addr_base);
  617. /* Test for a negative stride, iterating over every element. */
  618. if (tree_int_cst_sgn (DR_STEP (dr)) == -1)
  619. {
  620. addr_base = size_binop_loc (loc, MINUS_EXPR, addr_base,
  621. fold_convert_loc (loc, sizetype, nb_bytes));
  622. addr_base = size_binop_loc (loc, PLUS_EXPR, addr_base,
  623. TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
  624. }
  625. return fold_build_pointer_plus_loc (loc, DR_BASE_ADDRESS (dr), addr_base);
  626. }
  627. /* If VAL memory representation contains the same value in all bytes,
  628. return that value, otherwise return -1.
  629. E.g. for 0x24242424 return 0x24, for IEEE double
  630. 747708026454360457216.0 return 0x44, etc. */
  631. static int
  632. const_with_all_bytes_same (tree val)
  633. {
  634. unsigned char buf[64];
  635. int i, len;
  636. if (integer_zerop (val)
  637. || real_zerop (val)
  638. || (TREE_CODE (val) == CONSTRUCTOR
  639. && !TREE_CLOBBER_P (val)
  640. && CONSTRUCTOR_NELTS (val) == 0))
  641. return 0;
  642. if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
  643. return -1;
  644. len = native_encode_expr (val, buf, sizeof (buf));
  645. if (len == 0)
  646. return -1;
  647. for (i = 1; i < len; i++)
  648. if (buf[i] != buf[0])
  649. return -1;
  650. return buf[0];
  651. }
  652. /* Generate a call to memset for PARTITION in LOOP. */
  653. static void
  654. generate_memset_builtin (struct loop *loop, partition_t partition)
  655. {
  656. gimple_stmt_iterator gsi;
  657. gimple stmt, fn_call;
  658. tree mem, fn, nb_bytes;
  659. location_t loc;
  660. tree val;
  661. stmt = DR_STMT (partition->main_dr);
  662. loc = gimple_location (stmt);
  663. /* The new statements will be placed before LOOP. */
  664. gsi = gsi_last_bb (loop_preheader_edge (loop)->src);
  665. nb_bytes = build_size_arg_loc (loc, partition->main_dr, partition->niter,
  666. partition->plus_one);
  667. nb_bytes = force_gimple_operand_gsi (&gsi, nb_bytes, true, NULL_TREE,
  668. false, GSI_CONTINUE_LINKING);
  669. mem = build_addr_arg_loc (loc, partition->main_dr, nb_bytes);
  670. mem = force_gimple_operand_gsi (&gsi, mem, true, NULL_TREE,
  671. false, GSI_CONTINUE_LINKING);
  672. /* This exactly matches the pattern recognition in classify_partition. */
  673. val = gimple_assign_rhs1 (stmt);
  674. /* Handle constants like 0x15151515 and similarly
  675. floating point constants etc. where all bytes are the same. */
  676. int bytev = const_with_all_bytes_same (val);
  677. if (bytev != -1)
  678. val = build_int_cst (integer_type_node, bytev);
  679. else if (TREE_CODE (val) == INTEGER_CST)
  680. val = fold_convert (integer_type_node, val);
  681. else if (!useless_type_conversion_p (integer_type_node, TREE_TYPE (val)))
  682. {
  683. tree tem = make_ssa_name (integer_type_node);
  684. gimple cstmt = gimple_build_assign (tem, NOP_EXPR, val);
  685. gsi_insert_after (&gsi, cstmt, GSI_CONTINUE_LINKING);
  686. val = tem;
  687. }
  688. fn = build_fold_addr_expr (builtin_decl_implicit (BUILT_IN_MEMSET));
  689. fn_call = gimple_build_call (fn, 3, mem, val, nb_bytes);
  690. gsi_insert_after (&gsi, fn_call, GSI_CONTINUE_LINKING);
  691. if (dump_file && (dump_flags & TDF_DETAILS))
  692. {
  693. fprintf (dump_file, "generated memset");
  694. if (bytev == 0)
  695. fprintf (dump_file, " zero\n");
  696. else
  697. fprintf (dump_file, "\n");
  698. }
  699. }
  700. /* Generate a call to memcpy for PARTITION in LOOP. */
  701. static void
  702. generate_memcpy_builtin (struct loop *loop, partition_t partition)
  703. {
  704. gimple_stmt_iterator gsi;
  705. gimple stmt, fn_call;
  706. tree dest, src, fn, nb_bytes;
  707. location_t loc;
  708. enum built_in_function kind;
  709. stmt = DR_STMT (partition->main_dr);
  710. loc = gimple_location (stmt);
  711. /* The new statements will be placed before LOOP. */
  712. gsi = gsi_last_bb (loop_preheader_edge (loop)->src);
  713. nb_bytes = build_size_arg_loc (loc, partition->main_dr, partition->niter,
  714. partition->plus_one);
  715. nb_bytes = force_gimple_operand_gsi (&gsi, nb_bytes, true, NULL_TREE,
  716. false, GSI_CONTINUE_LINKING);
  717. dest = build_addr_arg_loc (loc, partition->main_dr, nb_bytes);
  718. src = build_addr_arg_loc (loc, partition->secondary_dr, nb_bytes);
  719. if (ptr_derefs_may_alias_p (dest, src))
  720. kind = BUILT_IN_MEMMOVE;
  721. else
  722. kind = BUILT_IN_MEMCPY;
  723. dest = force_gimple_operand_gsi (&gsi, dest, true, NULL_TREE,
  724. false, GSI_CONTINUE_LINKING);
  725. src = force_gimple_operand_gsi (&gsi, src, true, NULL_TREE,
  726. false, GSI_CONTINUE_LINKING);
  727. fn = build_fold_addr_expr (builtin_decl_implicit (kind));
  728. fn_call = gimple_build_call (fn, 3, dest, src, nb_bytes);
  729. gsi_insert_after (&gsi, fn_call, GSI_CONTINUE_LINKING);
  730. if (dump_file && (dump_flags & TDF_DETAILS))
  731. {
  732. if (kind == BUILT_IN_MEMCPY)
  733. fprintf (dump_file, "generated memcpy\n");
  734. else
  735. fprintf (dump_file, "generated memmove\n");
  736. }
  737. }
  738. /* Remove and destroy the loop LOOP. */
  739. static void
  740. destroy_loop (struct loop *loop)
  741. {
  742. unsigned nbbs = loop->num_nodes;
  743. edge exit = single_exit (loop);
  744. basic_block src = loop_preheader_edge (loop)->src, dest = exit->dest;
  745. basic_block *bbs;
  746. unsigned i;
  747. bbs = get_loop_body_in_dom_order (loop);
  748. redirect_edge_pred (exit, src);
  749. exit->flags &= ~(EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
  750. exit->flags |= EDGE_FALLTHRU;
  751. cancel_loop_tree (loop);
  752. rescan_loop_exit (exit, false, true);
  753. for (i = 0; i < nbbs; i++)
  754. {
  755. /* We have made sure to not leave any dangling uses of SSA
  756. names defined in the loop. With the exception of virtuals.
  757. Make sure we replace all uses of virtual defs that will remain
  758. outside of the loop with the bare symbol as delete_basic_block
  759. will release them. */
  760. for (gphi_iterator gsi = gsi_start_phis (bbs[i]); !gsi_end_p (gsi);
  761. gsi_next (&gsi))
  762. {
  763. gphi *phi = gsi.phi ();
  764. if (virtual_operand_p (gimple_phi_result (phi)))
  765. mark_virtual_phi_result_for_renaming (phi);
  766. }
  767. for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]); !gsi_end_p (gsi);
  768. gsi_next (&gsi))
  769. {
  770. gimple stmt = gsi_stmt (gsi);
  771. tree vdef = gimple_vdef (stmt);
  772. if (vdef && TREE_CODE (vdef) == SSA_NAME)
  773. mark_virtual_operand_for_renaming (vdef);
  774. }
  775. delete_basic_block (bbs[i]);
  776. }
  777. free (bbs);
  778. set_immediate_dominator (CDI_DOMINATORS, dest,
  779. recompute_dominator (CDI_DOMINATORS, dest));
  780. }
  781. /* Generates code for PARTITION. */
  782. static void
  783. generate_code_for_partition (struct loop *loop,
  784. partition_t partition, bool copy_p)
  785. {
  786. switch (partition->kind)
  787. {
  788. case PKIND_NORMAL:
  789. /* Reductions all have to be in the last partition. */
  790. gcc_assert (!partition_reduction_p (partition)
  791. || !copy_p);
  792. generate_loops_for_partition (loop, partition, copy_p);
  793. return;
  794. case PKIND_MEMSET:
  795. generate_memset_builtin (loop, partition);
  796. break;
  797. case PKIND_MEMCPY:
  798. generate_memcpy_builtin (loop, partition);
  799. break;
  800. default:
  801. gcc_unreachable ();
  802. }
  803. /* Common tail for partitions we turn into a call. If this was the last
  804. partition for which we generate code, we have to destroy the loop. */
  805. if (!copy_p)
  806. destroy_loop (loop);
  807. }
  808. /* Returns a partition with all the statements needed for computing
  809. the vertex V of the RDG, also including the loop exit conditions. */
  810. static partition_t
  811. build_rdg_partition_for_vertex (struct graph *rdg, int v)
  812. {
  813. partition_t partition = partition_alloc (NULL, NULL);
  814. auto_vec<int, 3> nodes;
  815. unsigned i;
  816. int x;
  817. graphds_dfs (rdg, &v, 1, &nodes, false, NULL);
  818. FOR_EACH_VEC_ELT (nodes, i, x)
  819. {
  820. bitmap_set_bit (partition->stmts, x);
  821. bitmap_set_bit (partition->loops,
  822. loop_containing_stmt (RDG_STMT (rdg, x))->num);
  823. }
  824. return partition;
  825. }
  826. /* Classifies the builtin kind we can generate for PARTITION of RDG and LOOP.
  827. For the moment we detect only the memset zero pattern. */
  828. static void
  829. classify_partition (loop_p loop, struct graph *rdg, partition_t partition)
  830. {
  831. bitmap_iterator bi;
  832. unsigned i;
  833. tree nb_iter;
  834. data_reference_p single_load, single_store;
  835. bool volatiles_p = false;
  836. bool plus_one = false;
  837. partition->kind = PKIND_NORMAL;
  838. partition->main_dr = NULL;
  839. partition->secondary_dr = NULL;
  840. partition->niter = NULL_TREE;
  841. partition->plus_one = false;
  842. EXECUTE_IF_SET_IN_BITMAP (partition->stmts, 0, i, bi)
  843. {
  844. gimple stmt = RDG_STMT (rdg, i);
  845. if (gimple_has_volatile_ops (stmt))
  846. volatiles_p = true;
  847. /* If the stmt has uses outside of the loop mark it as reduction. */
  848. if (stmt_has_scalar_dependences_outside_loop (loop, stmt))
  849. {
  850. partition->reduction_p = true;
  851. return;
  852. }
  853. }
  854. /* Perform general partition disqualification for builtins. */
  855. if (volatiles_p
  856. || !flag_tree_loop_distribute_patterns)
  857. return;
  858. /* Detect memset and memcpy. */
  859. single_load = NULL;
  860. single_store = NULL;
  861. EXECUTE_IF_SET_IN_BITMAP (partition->stmts, 0, i, bi)
  862. {
  863. gimple stmt = RDG_STMT (rdg, i);
  864. data_reference_p dr;
  865. unsigned j;
  866. if (gimple_code (stmt) == GIMPLE_PHI)
  867. continue;
  868. /* Any scalar stmts are ok. */
  869. if (!gimple_vuse (stmt))
  870. continue;
  871. /* Otherwise just regular loads/stores. */
  872. if (!gimple_assign_single_p (stmt))
  873. return;
  874. /* But exactly one store and/or load. */
  875. for (j = 0; RDG_DATAREFS (rdg, i).iterate (j, &dr); ++j)
  876. {
  877. if (DR_IS_READ (dr))
  878. {
  879. if (single_load != NULL)
  880. return;
  881. single_load = dr;
  882. }
  883. else
  884. {
  885. if (single_store != NULL)
  886. return;
  887. single_store = dr;
  888. }
  889. }
  890. }
  891. if (!single_store)
  892. return;
  893. nb_iter = number_of_latch_executions (loop);
  894. if (!nb_iter || nb_iter == chrec_dont_know)
  895. return;
  896. if (dominated_by_p (CDI_DOMINATORS, single_exit (loop)->src,
  897. gimple_bb (DR_STMT (single_store))))
  898. plus_one = true;
  899. if (single_store && !single_load)
  900. {
  901. gimple stmt = DR_STMT (single_store);
  902. tree rhs = gimple_assign_rhs1 (stmt);
  903. if (const_with_all_bytes_same (rhs) == -1
  904. && (!INTEGRAL_TYPE_P (TREE_TYPE (rhs))
  905. || (TYPE_MODE (TREE_TYPE (rhs))
  906. != TYPE_MODE (unsigned_char_type_node))))
  907. return;
  908. if (TREE_CODE (rhs) == SSA_NAME
  909. && !SSA_NAME_IS_DEFAULT_DEF (rhs)
  910. && flow_bb_inside_loop_p (loop, gimple_bb (SSA_NAME_DEF_STMT (rhs))))
  911. return;
  912. if (!adjacent_dr_p (single_store)
  913. || !dominated_by_p (CDI_DOMINATORS,
  914. loop->latch, gimple_bb (stmt)))
  915. return;
  916. partition->kind = PKIND_MEMSET;
  917. partition->main_dr = single_store;
  918. partition->niter = nb_iter;
  919. partition->plus_one = plus_one;
  920. }
  921. else if (single_store && single_load)
  922. {
  923. gimple store = DR_STMT (single_store);
  924. gimple load = DR_STMT (single_load);
  925. /* Direct aggregate copy or via an SSA name temporary. */
  926. if (load != store
  927. && gimple_assign_lhs (load) != gimple_assign_rhs1 (store))
  928. return;
  929. if (!adjacent_dr_p (single_store)
  930. || !adjacent_dr_p (single_load)
  931. || !operand_equal_p (DR_STEP (single_store),
  932. DR_STEP (single_load), 0)
  933. || !dominated_by_p (CDI_DOMINATORS,
  934. loop->latch, gimple_bb (store)))
  935. return;
  936. /* Now check that if there is a dependence this dependence is
  937. of a suitable form for memmove. */
  938. vec<loop_p> loops = vNULL;
  939. ddr_p ddr;
  940. loops.safe_push (loop);
  941. ddr = initialize_data_dependence_relation (single_load, single_store,
  942. loops);
  943. compute_affine_dependence (ddr, loop);
  944. if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
  945. {
  946. free_dependence_relation (ddr);
  947. loops.release ();
  948. return;
  949. }
  950. if (DDR_ARE_DEPENDENT (ddr) != chrec_known)
  951. {
  952. if (DDR_NUM_DIST_VECTS (ddr) == 0)
  953. {
  954. free_dependence_relation (ddr);
  955. loops.release ();
  956. return;
  957. }
  958. lambda_vector dist_v;
  959. FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
  960. {
  961. int dist = dist_v[index_in_loop_nest (loop->num,
  962. DDR_LOOP_NEST (ddr))];
  963. if (dist > 0 && !DDR_REVERSED_P (ddr))
  964. {
  965. free_dependence_relation (ddr);
  966. loops.release ();
  967. return;
  968. }
  969. }
  970. }
  971. free_dependence_relation (ddr);
  972. loops.release ();
  973. partition->kind = PKIND_MEMCPY;
  974. partition->main_dr = single_store;
  975. partition->secondary_dr = single_load;
  976. partition->niter = nb_iter;
  977. partition->plus_one = plus_one;
  978. }
  979. }
  980. /* For a data reference REF, return the declaration of its base
  981. address or NULL_TREE if the base is not determined. */
  982. static tree
  983. ref_base_address (data_reference_p dr)
  984. {
  985. tree base_address = DR_BASE_ADDRESS (dr);
  986. if (base_address
  987. && TREE_CODE (base_address) == ADDR_EXPR)
  988. return TREE_OPERAND (base_address, 0);
  989. return base_address;
  990. }
  991. /* Returns true when PARTITION1 and PARTITION2 have similar memory
  992. accesses in RDG. */
  993. static bool
  994. similar_memory_accesses (struct graph *rdg, partition_t partition1,
  995. partition_t partition2)
  996. {
  997. unsigned i, j, k, l;
  998. bitmap_iterator bi, bj;
  999. data_reference_p ref1, ref2;
  1000. /* First check whether in the intersection of the two partitions are
  1001. any loads or stores. Common loads are the situation that happens
  1002. most often. */
  1003. EXECUTE_IF_AND_IN_BITMAP (partition1->stmts, partition2->stmts, 0, i, bi)
  1004. if (RDG_MEM_WRITE_STMT (rdg, i)
  1005. || RDG_MEM_READS_STMT (rdg, i))
  1006. return true;
  1007. /* Then check all data-references against each other. */
  1008. EXECUTE_IF_SET_IN_BITMAP (partition1->stmts, 0, i, bi)
  1009. if (RDG_MEM_WRITE_STMT (rdg, i)
  1010. || RDG_MEM_READS_STMT (rdg, i))
  1011. EXECUTE_IF_SET_IN_BITMAP (partition2->stmts, 0, j, bj)
  1012. if (RDG_MEM_WRITE_STMT (rdg, j)
  1013. || RDG_MEM_READS_STMT (rdg, j))
  1014. {
  1015. FOR_EACH_VEC_ELT (RDG_DATAREFS (rdg, i), k, ref1)
  1016. {
  1017. tree base1 = ref_base_address (ref1);
  1018. if (base1)
  1019. FOR_EACH_VEC_ELT (RDG_DATAREFS (rdg, j), l, ref2)
  1020. if (base1 == ref_base_address (ref2))
  1021. return true;
  1022. }
  1023. }
  1024. return false;
  1025. }
  1026. /* Aggregate several components into a useful partition that is
  1027. registered in the PARTITIONS vector. Partitions will be
  1028. distributed in different loops. */
  1029. static void
  1030. rdg_build_partitions (struct graph *rdg,
  1031. vec<gimple> starting_stmts,
  1032. vec<partition_t> *partitions)
  1033. {
  1034. bitmap processed = BITMAP_ALLOC (NULL);
  1035. int i;
  1036. gimple stmt;
  1037. FOR_EACH_VEC_ELT (starting_stmts, i, stmt)
  1038. {
  1039. int v = rdg_vertex_for_stmt (rdg, stmt);
  1040. if (dump_file && (dump_flags & TDF_DETAILS))
  1041. fprintf (dump_file,
  1042. "ldist asked to generate code for vertex %d\n", v);
  1043. /* If the vertex is already contained in another partition so
  1044. is the partition rooted at it. */
  1045. if (bitmap_bit_p (processed, v))
  1046. continue;
  1047. partition_t partition = build_rdg_partition_for_vertex (rdg, v);
  1048. bitmap_ior_into (processed, partition->stmts);
  1049. if (dump_file && (dump_flags & TDF_DETAILS))
  1050. {
  1051. fprintf (dump_file, "ldist useful partition:\n");
  1052. dump_bitmap (dump_file, partition->stmts);
  1053. }
  1054. partitions->safe_push (partition);
  1055. }
  1056. /* All vertices should have been assigned to at least one partition now,
  1057. other than vertices belonging to dead code. */
  1058. BITMAP_FREE (processed);
  1059. }
  1060. /* Dump to FILE the PARTITIONS. */
  1061. static void
  1062. dump_rdg_partitions (FILE *file, vec<partition_t> partitions)
  1063. {
  1064. int i;
  1065. partition_t partition;
  1066. FOR_EACH_VEC_ELT (partitions, i, partition)
  1067. debug_bitmap_file (file, partition->stmts);
  1068. }
  1069. /* Debug PARTITIONS. */
  1070. extern void debug_rdg_partitions (vec<partition_t> );
  1071. DEBUG_FUNCTION void
  1072. debug_rdg_partitions (vec<partition_t> partitions)
  1073. {
  1074. dump_rdg_partitions (stderr, partitions);
  1075. }
  1076. /* Returns the number of read and write operations in the RDG. */
  1077. static int
  1078. number_of_rw_in_rdg (struct graph *rdg)
  1079. {
  1080. int i, res = 0;
  1081. for (i = 0; i < rdg->n_vertices; i++)
  1082. {
  1083. if (RDG_MEM_WRITE_STMT (rdg, i))
  1084. ++res;
  1085. if (RDG_MEM_READS_STMT (rdg, i))
  1086. ++res;
  1087. }
  1088. return res;
  1089. }
  1090. /* Returns the number of read and write operations in a PARTITION of
  1091. the RDG. */
  1092. static int
  1093. number_of_rw_in_partition (struct graph *rdg, partition_t partition)
  1094. {
  1095. int res = 0;
  1096. unsigned i;
  1097. bitmap_iterator ii;
  1098. EXECUTE_IF_SET_IN_BITMAP (partition->stmts, 0, i, ii)
  1099. {
  1100. if (RDG_MEM_WRITE_STMT (rdg, i))
  1101. ++res;
  1102. if (RDG_MEM_READS_STMT (rdg, i))
  1103. ++res;
  1104. }
  1105. return res;
  1106. }
  1107. /* Returns true when one of the PARTITIONS contains all the read or
  1108. write operations of RDG. */
  1109. static bool
  1110. partition_contains_all_rw (struct graph *rdg,
  1111. vec<partition_t> partitions)
  1112. {
  1113. int i;
  1114. partition_t partition;
  1115. int nrw = number_of_rw_in_rdg (rdg);
  1116. FOR_EACH_VEC_ELT (partitions, i, partition)
  1117. if (nrw == number_of_rw_in_partition (rdg, partition))
  1118. return true;
  1119. return false;
  1120. }
  1121. /* Compute partition dependence created by the data references in DRS1
  1122. and DRS2 and modify and return DIR according to that. */
  1123. static int
  1124. pg_add_dependence_edges (struct graph *rdg, vec<loop_p> loops, int dir,
  1125. vec<data_reference_p> drs1,
  1126. vec<data_reference_p> drs2)
  1127. {
  1128. data_reference_p dr1, dr2;
  1129. /* dependence direction - 0 is no dependence, -1 is back,
  1130. 1 is forth, 2 is both (we can stop then, merging will occur). */
  1131. for (int ii = 0; drs1.iterate (ii, &dr1); ++ii)
  1132. for (int jj = 0; drs2.iterate (jj, &dr2); ++jj)
  1133. {
  1134. data_reference_p saved_dr1 = dr1;
  1135. int this_dir = 1;
  1136. ddr_p ddr;
  1137. /* Re-shuffle data-refs to be in dominator order. */
  1138. if (rdg_vertex_for_stmt (rdg, DR_STMT (dr1))
  1139. > rdg_vertex_for_stmt (rdg, DR_STMT (dr2)))
  1140. {
  1141. data_reference_p tem = dr1;
  1142. dr1 = dr2;
  1143. dr2 = tem;
  1144. this_dir = -this_dir;
  1145. }
  1146. ddr = initialize_data_dependence_relation (dr1, dr2, loops);
  1147. compute_affine_dependence (ddr, loops[0]);
  1148. if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
  1149. this_dir = 2;
  1150. else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
  1151. {
  1152. if (DDR_REVERSED_P (ddr))
  1153. {
  1154. data_reference_p tem = dr1;
  1155. dr1 = dr2;
  1156. dr2 = tem;
  1157. this_dir = -this_dir;
  1158. }
  1159. /* Known dependences can still be unordered througout the
  1160. iteration space, see gcc.dg/tree-ssa/ldist-16.c. */
  1161. if (DDR_NUM_DIST_VECTS (ddr) != 1)
  1162. this_dir = 2;
  1163. /* If the overlap is exact preserve stmt order. */
  1164. else if (lambda_vector_zerop (DDR_DIST_VECT (ddr, 0), 1))
  1165. ;
  1166. else
  1167. {
  1168. /* Else as the distance vector is lexicographic positive
  1169. swap the dependence direction. */
  1170. this_dir = -this_dir;
  1171. }
  1172. }
  1173. else
  1174. this_dir = 0;
  1175. free_dependence_relation (ddr);
  1176. if (dir == 0)
  1177. dir = this_dir;
  1178. else if (dir != this_dir)
  1179. return 2;
  1180. /* Shuffle "back" dr1. */
  1181. dr1 = saved_dr1;
  1182. }
  1183. return dir;
  1184. }
  1185. /* Compare postorder number of the partition graph vertices V1 and V2. */
  1186. static int
  1187. pgcmp (const void *v1_, const void *v2_)
  1188. {
  1189. const vertex *v1 = (const vertex *)v1_;
  1190. const vertex *v2 = (const vertex *)v2_;
  1191. return v2->post - v1->post;
  1192. }
  1193. /* Distributes the code from LOOP in such a way that producer
  1194. statements are placed before consumer statements. Tries to separate
  1195. only the statements from STMTS into separate loops.
  1196. Returns the number of distributed loops. */
  1197. static int
  1198. distribute_loop (struct loop *loop, vec<gimple> stmts,
  1199. control_dependences *cd, int *nb_calls)
  1200. {
  1201. struct graph *rdg;
  1202. partition_t partition;
  1203. bool any_builtin;
  1204. int i, nbp;
  1205. graph *pg = NULL;
  1206. int num_sccs = 1;
  1207. *nb_calls = 0;
  1208. auto_vec<loop_p, 3> loop_nest;
  1209. if (!find_loop_nest (loop, &loop_nest))
  1210. return 0;
  1211. rdg = build_rdg (loop_nest, cd);
  1212. if (!rdg)
  1213. {
  1214. if (dump_file && (dump_flags & TDF_DETAILS))
  1215. fprintf (dump_file,
  1216. "Loop %d not distributed: failed to build the RDG.\n",
  1217. loop->num);
  1218. return 0;
  1219. }
  1220. if (dump_file && (dump_flags & TDF_DETAILS))
  1221. dump_rdg (dump_file, rdg);
  1222. auto_vec<partition_t, 3> partitions;
  1223. rdg_build_partitions (rdg, stmts, &partitions);
  1224. any_builtin = false;
  1225. FOR_EACH_VEC_ELT (partitions, i, partition)
  1226. {
  1227. classify_partition (loop, rdg, partition);
  1228. any_builtin |= partition_builtin_p (partition);
  1229. }
  1230. /* If we are only distributing patterns but did not detect any,
  1231. simply bail out. */
  1232. if (!flag_tree_loop_distribution
  1233. && !any_builtin)
  1234. {
  1235. nbp = 0;
  1236. goto ldist_done;
  1237. }
  1238. /* If we are only distributing patterns fuse all partitions that
  1239. were not classified as builtins. This also avoids chopping
  1240. a loop into pieces, separated by builtin calls. That is, we
  1241. only want no or a single loop body remaining. */
  1242. partition_t into;
  1243. if (!flag_tree_loop_distribution)
  1244. {
  1245. for (i = 0; partitions.iterate (i, &into); ++i)
  1246. if (!partition_builtin_p (into))
  1247. break;
  1248. for (++i; partitions.iterate (i, &partition); ++i)
  1249. if (!partition_builtin_p (partition))
  1250. {
  1251. if (dump_file && (dump_flags & TDF_DETAILS))
  1252. {
  1253. fprintf (dump_file, "fusing non-builtin partitions\n");
  1254. dump_bitmap (dump_file, into->stmts);
  1255. dump_bitmap (dump_file, partition->stmts);
  1256. }
  1257. partition_merge_into (into, partition);
  1258. partitions.unordered_remove (i);
  1259. partition_free (partition);
  1260. i--;
  1261. }
  1262. }
  1263. /* Due to limitations in the transform phase we have to fuse all
  1264. reduction partitions into the last partition so the existing
  1265. loop will contain all loop-closed PHI nodes. */
  1266. for (i = 0; partitions.iterate (i, &into); ++i)
  1267. if (partition_reduction_p (into))
  1268. break;
  1269. for (i = i + 1; partitions.iterate (i, &partition); ++i)
  1270. if (partition_reduction_p (partition))
  1271. {
  1272. if (dump_file && (dump_flags & TDF_DETAILS))
  1273. {
  1274. fprintf (dump_file, "fusing partitions\n");
  1275. dump_bitmap (dump_file, into->stmts);
  1276. dump_bitmap (dump_file, partition->stmts);
  1277. fprintf (dump_file, "because they have reductions\n");
  1278. }
  1279. partition_merge_into (into, partition);
  1280. partitions.unordered_remove (i);
  1281. partition_free (partition);
  1282. i--;
  1283. }
  1284. /* Apply our simple cost model - fuse partitions with similar
  1285. memory accesses. */
  1286. for (i = 0; partitions.iterate (i, &into); ++i)
  1287. {
  1288. if (partition_builtin_p (into))
  1289. continue;
  1290. for (int j = i + 1;
  1291. partitions.iterate (j, &partition); ++j)
  1292. {
  1293. if (!partition_builtin_p (partition)
  1294. && similar_memory_accesses (rdg, into, partition))
  1295. {
  1296. if (dump_file && (dump_flags & TDF_DETAILS))
  1297. {
  1298. fprintf (dump_file, "fusing partitions\n");
  1299. dump_bitmap (dump_file, into->stmts);
  1300. dump_bitmap (dump_file, partition->stmts);
  1301. fprintf (dump_file, "because they have similar "
  1302. "memory accesses\n");
  1303. }
  1304. partition_merge_into (into, partition);
  1305. partitions.unordered_remove (j);
  1306. partition_free (partition);
  1307. j--;
  1308. }
  1309. }
  1310. }
  1311. /* Build the partition dependency graph. */
  1312. if (partitions.length () > 1)
  1313. {
  1314. pg = new_graph (partitions.length ());
  1315. struct pgdata {
  1316. partition_t partition;
  1317. vec<data_reference_p> writes;
  1318. vec<data_reference_p> reads;
  1319. };
  1320. #define PGDATA(i) ((pgdata *)(pg->vertices[i].data))
  1321. for (i = 0; partitions.iterate (i, &partition); ++i)
  1322. {
  1323. vertex *v = &pg->vertices[i];
  1324. pgdata *data = new pgdata;
  1325. data_reference_p dr;
  1326. /* FIXME - leaks. */
  1327. v->data = data;
  1328. bitmap_iterator bi;
  1329. unsigned j;
  1330. data->partition = partition;
  1331. data->reads = vNULL;
  1332. data->writes = vNULL;
  1333. EXECUTE_IF_SET_IN_BITMAP (partition->stmts, 0, j, bi)
  1334. for (int k = 0; RDG_DATAREFS (rdg, j).iterate (k, &dr); ++k)
  1335. if (DR_IS_READ (dr))
  1336. data->reads.safe_push (dr);
  1337. else
  1338. data->writes.safe_push (dr);
  1339. }
  1340. partition_t partition1, partition2;
  1341. for (i = 0; partitions.iterate (i, &partition1); ++i)
  1342. for (int j = i + 1; partitions.iterate (j, &partition2); ++j)
  1343. {
  1344. /* dependence direction - 0 is no dependence, -1 is back,
  1345. 1 is forth, 2 is both (we can stop then, merging will occur). */
  1346. int dir = 0;
  1347. dir = pg_add_dependence_edges (rdg, loop_nest, dir,
  1348. PGDATA(i)->writes,
  1349. PGDATA(j)->reads);
  1350. if (dir != 2)
  1351. dir = pg_add_dependence_edges (rdg, loop_nest, dir,
  1352. PGDATA(i)->reads,
  1353. PGDATA(j)->writes);
  1354. if (dir != 2)
  1355. dir = pg_add_dependence_edges (rdg, loop_nest, dir,
  1356. PGDATA(i)->writes,
  1357. PGDATA(j)->writes);
  1358. if (dir == 1 || dir == 2)
  1359. add_edge (pg, i, j);
  1360. if (dir == -1 || dir == 2)
  1361. add_edge (pg, j, i);
  1362. }
  1363. /* Add edges to the reduction partition (if any) to force it last. */
  1364. unsigned j;
  1365. for (j = 0; partitions.iterate (j, &partition); ++j)
  1366. if (partition_reduction_p (partition))
  1367. break;
  1368. if (j < partitions.length ())
  1369. {
  1370. for (unsigned i = 0; partitions.iterate (i, &partition); ++i)
  1371. if (i != j)
  1372. add_edge (pg, i, j);
  1373. }
  1374. /* Compute partitions we cannot separate and fuse them. */
  1375. num_sccs = graphds_scc (pg, NULL);
  1376. for (i = 0; i < num_sccs; ++i)
  1377. {
  1378. partition_t first;
  1379. int j;
  1380. for (j = 0; partitions.iterate (j, &first); ++j)
  1381. if (pg->vertices[j].component == i)
  1382. break;
  1383. for (j = j + 1; partitions.iterate (j, &partition); ++j)
  1384. if (pg->vertices[j].component == i)
  1385. {
  1386. if (dump_file && (dump_flags & TDF_DETAILS))
  1387. {
  1388. fprintf (dump_file, "fusing partitions\n");
  1389. dump_bitmap (dump_file, first->stmts);
  1390. dump_bitmap (dump_file, partition->stmts);
  1391. fprintf (dump_file, "because they are in the same "
  1392. "dependence SCC\n");
  1393. }
  1394. partition_merge_into (first, partition);
  1395. partitions[j] = NULL;
  1396. partition_free (partition);
  1397. PGDATA (j)->partition = NULL;
  1398. }
  1399. }
  1400. /* Now order the remaining nodes in postorder. */
  1401. qsort (pg->vertices, pg->n_vertices, sizeof (vertex), pgcmp);
  1402. partitions.truncate (0);
  1403. for (i = 0; i < pg->n_vertices; ++i)
  1404. {
  1405. pgdata *data = PGDATA (i);
  1406. if (data->partition)
  1407. partitions.safe_push (data->partition);
  1408. data->reads.release ();
  1409. data->writes.release ();
  1410. delete data;
  1411. }
  1412. gcc_assert (partitions.length () == (unsigned)num_sccs);
  1413. free_graph (pg);
  1414. }
  1415. nbp = partitions.length ();
  1416. if (nbp == 0
  1417. || (nbp == 1 && !partition_builtin_p (partitions[0]))
  1418. || (nbp > 1 && partition_contains_all_rw (rdg, partitions)))
  1419. {
  1420. nbp = 0;
  1421. goto ldist_done;
  1422. }
  1423. if (dump_file && (dump_flags & TDF_DETAILS))
  1424. dump_rdg_partitions (dump_file, partitions);
  1425. FOR_EACH_VEC_ELT (partitions, i, partition)
  1426. {
  1427. if (partition_builtin_p (partition))
  1428. (*nb_calls)++;
  1429. generate_code_for_partition (loop, partition, i < nbp - 1);
  1430. }
  1431. ldist_done:
  1432. FOR_EACH_VEC_ELT (partitions, i, partition)
  1433. partition_free (partition);
  1434. free_rdg (rdg);
  1435. return nbp - *nb_calls;
  1436. }
  1437. /* Distribute all loops in the current function. */
  1438. namespace {
  1439. const pass_data pass_data_loop_distribution =
  1440. {
  1441. GIMPLE_PASS, /* type */
  1442. "ldist", /* name */
  1443. OPTGROUP_LOOP, /* optinfo_flags */
  1444. TV_TREE_LOOP_DISTRIBUTION, /* tv_id */
  1445. ( PROP_cfg | PROP_ssa ), /* properties_required */
  1446. 0, /* properties_provided */
  1447. 0, /* properties_destroyed */
  1448. 0, /* todo_flags_start */
  1449. 0, /* todo_flags_finish */
  1450. };
  1451. class pass_loop_distribution : public gimple_opt_pass
  1452. {
  1453. public:
  1454. pass_loop_distribution (gcc::context *ctxt)
  1455. : gimple_opt_pass (pass_data_loop_distribution, ctxt)
  1456. {}
  1457. /* opt_pass methods: */
  1458. virtual bool gate (function *)
  1459. {
  1460. return flag_tree_loop_distribution
  1461. || flag_tree_loop_distribute_patterns;
  1462. }
  1463. virtual unsigned int execute (function *);
  1464. }; // class pass_loop_distribution
  1465. unsigned int
  1466. pass_loop_distribution::execute (function *fun)
  1467. {
  1468. struct loop *loop;
  1469. bool changed = false;
  1470. basic_block bb;
  1471. control_dependences *cd = NULL;
  1472. FOR_ALL_BB_FN (bb, fun)
  1473. {
  1474. gimple_stmt_iterator gsi;
  1475. for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  1476. gimple_set_uid (gsi_stmt (gsi), -1);
  1477. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  1478. gimple_set_uid (gsi_stmt (gsi), -1);
  1479. }
  1480. /* We can at the moment only distribute non-nested loops, thus restrict
  1481. walking to innermost loops. */
  1482. FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
  1483. {
  1484. auto_vec<gimple> work_list;
  1485. basic_block *bbs;
  1486. int num = loop->num;
  1487. unsigned int i;
  1488. /* If the loop doesn't have a single exit we will fail anyway,
  1489. so do that early. */
  1490. if (!single_exit (loop))
  1491. continue;
  1492. /* Only optimize hot loops. */
  1493. if (!optimize_loop_for_speed_p (loop))
  1494. continue;
  1495. /* Initialize the worklist with stmts we seed the partitions with. */
  1496. bbs = get_loop_body_in_dom_order (loop);
  1497. for (i = 0; i < loop->num_nodes; ++i)
  1498. {
  1499. for (gphi_iterator gsi = gsi_start_phis (bbs[i]);
  1500. !gsi_end_p (gsi);
  1501. gsi_next (&gsi))
  1502. {
  1503. gphi *phi = gsi.phi ();
  1504. if (virtual_operand_p (gimple_phi_result (phi)))
  1505. continue;
  1506. /* Distribute stmts which have defs that are used outside of
  1507. the loop. */
  1508. if (!stmt_has_scalar_dependences_outside_loop (loop, phi))
  1509. continue;
  1510. work_list.safe_push (phi);
  1511. }
  1512. for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
  1513. !gsi_end_p (gsi);
  1514. gsi_next (&gsi))
  1515. {
  1516. gimple stmt = gsi_stmt (gsi);
  1517. /* If there is a stmt with side-effects bail out - we
  1518. cannot and should not distribute this loop. */
  1519. if (gimple_has_side_effects (stmt))
  1520. {
  1521. work_list.truncate (0);
  1522. goto out;
  1523. }
  1524. /* Distribute stmts which have defs that are used outside of
  1525. the loop. */
  1526. if (stmt_has_scalar_dependences_outside_loop (loop, stmt))
  1527. ;
  1528. /* Otherwise only distribute stores for now. */
  1529. else if (!gimple_vdef (stmt))
  1530. continue;
  1531. work_list.safe_push (stmt);
  1532. }
  1533. }
  1534. out:
  1535. free (bbs);
  1536. int nb_generated_loops = 0;
  1537. int nb_generated_calls = 0;
  1538. location_t loc = find_loop_location (loop);
  1539. if (work_list.length () > 0)
  1540. {
  1541. if (!cd)
  1542. {
  1543. calculate_dominance_info (CDI_DOMINATORS);
  1544. calculate_dominance_info (CDI_POST_DOMINATORS);
  1545. cd = new control_dependences (create_edge_list ());
  1546. free_dominance_info (CDI_POST_DOMINATORS);
  1547. }
  1548. nb_generated_loops = distribute_loop (loop, work_list, cd,
  1549. &nb_generated_calls);
  1550. }
  1551. if (nb_generated_loops + nb_generated_calls > 0)
  1552. {
  1553. changed = true;
  1554. dump_printf_loc (MSG_OPTIMIZED_LOCATIONS,
  1555. loc, "Loop %d distributed: split to %d loops "
  1556. "and %d library calls.\n",
  1557. num, nb_generated_loops, nb_generated_calls);
  1558. }
  1559. else if (dump_file && (dump_flags & TDF_DETAILS))
  1560. fprintf (dump_file, "Loop %d is the same.\n", num);
  1561. }
  1562. if (cd)
  1563. delete cd;
  1564. if (changed)
  1565. {
  1566. /* Cached scalar evolutions now may refer to wrong or non-existing
  1567. loops. */
  1568. scev_reset_htab ();
  1569. mark_virtual_operands_for_renaming (fun);
  1570. rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
  1571. }
  1572. #ifdef ENABLE_CHECKING
  1573. verify_loop_structure ();
  1574. #endif
  1575. return 0;
  1576. }
  1577. } // anon namespace
  1578. gimple_opt_pass *
  1579. make_pass_loop_distribution (gcc::context *ctxt)
  1580. {
  1581. return new pass_loop_distribution (ctxt);
  1582. }