tree-data-ref.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /* Data references and dependences detectors.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Sebastian Pop <pop@cri.ensmp.fr>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_TREE_DATA_REF_H
  17. #define GCC_TREE_DATA_REF_H
  18. #include "graphds.h"
  19. #include "omega.h"
  20. #include "tree-chrec.h"
  21. /*
  22. innermost_loop_behavior describes the evolution of the address of the memory
  23. reference in the innermost enclosing loop. The address is expressed as
  24. BASE + STEP * # of iteration, and base is further decomposed as the base
  25. pointer (BASE_ADDRESS), loop invariant offset (OFFSET) and
  26. constant offset (INIT). Examples, in loop nest
  27. for (i = 0; i < 100; i++)
  28. for (j = 3; j < 100; j++)
  29. Example 1 Example 2
  30. data-ref a[j].b[i][j] *(p + x + 16B + 4B * j)
  31. innermost_loop_behavior
  32. base_address &a p
  33. offset i * D_i x
  34. init 3 * D_j + offsetof (b) 28
  35. step D_j 4
  36. */
  37. struct innermost_loop_behavior
  38. {
  39. tree base_address;
  40. tree offset;
  41. tree init;
  42. tree step;
  43. /* Alignment information. ALIGNED_TO is set to the largest power of two
  44. that divides OFFSET. */
  45. tree aligned_to;
  46. };
  47. /* Describes the evolutions of indices of the memory reference. The indices
  48. are indices of the ARRAY_REFs, indexes in artificial dimensions
  49. added for member selection of records and the operands of MEM_REFs.
  50. BASE_OBJECT is the part of the reference that is loop-invariant
  51. (note that this reference does not have to cover the whole object
  52. being accessed, in which case UNCONSTRAINED_BASE is set; hence it is
  53. not recommended to use BASE_OBJECT in any code generation).
  54. For the examples above,
  55. base_object: a *(p + x + 4B * j_0)
  56. indices: {j_0, +, 1}_2 {16, +, 4}_2
  57. 4
  58. {i_0, +, 1}_1
  59. {j_0, +, 1}_2
  60. */
  61. struct indices
  62. {
  63. /* The object. */
  64. tree base_object;
  65. /* A list of chrecs. Access functions of the indices. */
  66. vec<tree> access_fns;
  67. /* Whether BASE_OBJECT is an access representing the whole object
  68. or whether the access could not be constrained. */
  69. bool unconstrained_base;
  70. };
  71. struct dr_alias
  72. {
  73. /* The alias information that should be used for new pointers to this
  74. location. */
  75. struct ptr_info_def *ptr_info;
  76. };
  77. /* An integer vector. A vector formally consists of an element of a vector
  78. space. A vector space is a set that is closed under vector addition
  79. and scalar multiplication. In this vector space, an element is a list of
  80. integers. */
  81. typedef int *lambda_vector;
  82. /* An integer matrix. A matrix consists of m vectors of length n (IE
  83. all vectors are the same length). */
  84. typedef lambda_vector *lambda_matrix;
  85. struct data_reference
  86. {
  87. /* A pointer to the statement that contains this DR. */
  88. gimple stmt;
  89. /* A pointer to the memory reference. */
  90. tree ref;
  91. /* Auxiliary info specific to a pass. */
  92. void *aux;
  93. /* True when the data reference is in RHS of a stmt. */
  94. bool is_read;
  95. /* Behavior of the memory reference in the innermost loop. */
  96. struct innermost_loop_behavior innermost;
  97. /* Subscripts of this data reference. */
  98. struct indices indices;
  99. /* Alias information for the data reference. */
  100. struct dr_alias alias;
  101. };
  102. #define DR_STMT(DR) (DR)->stmt
  103. #define DR_REF(DR) (DR)->ref
  104. #define DR_BASE_OBJECT(DR) (DR)->indices.base_object
  105. #define DR_UNCONSTRAINED_BASE(DR) (DR)->indices.unconstrained_base
  106. #define DR_ACCESS_FNS(DR) (DR)->indices.access_fns
  107. #define DR_ACCESS_FN(DR, I) DR_ACCESS_FNS (DR)[I]
  108. #define DR_NUM_DIMENSIONS(DR) DR_ACCESS_FNS (DR).length ()
  109. #define DR_IS_READ(DR) (DR)->is_read
  110. #define DR_IS_WRITE(DR) (!DR_IS_READ (DR))
  111. #define DR_BASE_ADDRESS(DR) (DR)->innermost.base_address
  112. #define DR_OFFSET(DR) (DR)->innermost.offset
  113. #define DR_INIT(DR) (DR)->innermost.init
  114. #define DR_STEP(DR) (DR)->innermost.step
  115. #define DR_PTR_INFO(DR) (DR)->alias.ptr_info
  116. #define DR_ALIGNED_TO(DR) (DR)->innermost.aligned_to
  117. typedef struct data_reference *data_reference_p;
  118. enum data_dependence_direction {
  119. dir_positive,
  120. dir_negative,
  121. dir_equal,
  122. dir_positive_or_negative,
  123. dir_positive_or_equal,
  124. dir_negative_or_equal,
  125. dir_star,
  126. dir_independent
  127. };
  128. /* The description of the grid of iterations that overlap. At most
  129. two loops are considered at the same time just now, hence at most
  130. two functions are needed. For each of the functions, we store
  131. the vector of coefficients, f[0] + x * f[1] + y * f[2] + ...,
  132. where x, y, ... are variables. */
  133. #define MAX_DIM 2
  134. /* Special values of N. */
  135. #define NO_DEPENDENCE 0
  136. #define NOT_KNOWN (MAX_DIM + 1)
  137. #define CF_NONTRIVIAL_P(CF) ((CF)->n != NO_DEPENDENCE && (CF)->n != NOT_KNOWN)
  138. #define CF_NOT_KNOWN_P(CF) ((CF)->n == NOT_KNOWN)
  139. #define CF_NO_DEPENDENCE_P(CF) ((CF)->n == NO_DEPENDENCE)
  140. typedef vec<tree> affine_fn;
  141. struct conflict_function
  142. {
  143. unsigned n;
  144. affine_fn fns[MAX_DIM];
  145. };
  146. /* What is a subscript? Given two array accesses a subscript is the
  147. tuple composed of the access functions for a given dimension.
  148. Example: Given A[f1][f2][f3] and B[g1][g2][g3], there are three
  149. subscripts: (f1, g1), (f2, g2), (f3, g3). These three subscripts
  150. are stored in the data_dependence_relation structure under the form
  151. of an array of subscripts. */
  152. struct subscript
  153. {
  154. /* A description of the iterations for which the elements are
  155. accessed twice. */
  156. conflict_function *conflicting_iterations_in_a;
  157. conflict_function *conflicting_iterations_in_b;
  158. /* This field stores the information about the iteration domain
  159. validity of the dependence relation. */
  160. tree last_conflict;
  161. /* Distance from the iteration that access a conflicting element in
  162. A to the iteration that access this same conflicting element in
  163. B. The distance is a tree scalar expression, i.e. a constant or a
  164. symbolic expression, but certainly not a chrec function. */
  165. tree distance;
  166. };
  167. typedef struct subscript *subscript_p;
  168. #define SUB_CONFLICTS_IN_A(SUB) SUB->conflicting_iterations_in_a
  169. #define SUB_CONFLICTS_IN_B(SUB) SUB->conflicting_iterations_in_b
  170. #define SUB_LAST_CONFLICT(SUB) SUB->last_conflict
  171. #define SUB_DISTANCE(SUB) SUB->distance
  172. /* A data_dependence_relation represents a relation between two
  173. data_references A and B. */
  174. struct data_dependence_relation
  175. {
  176. struct data_reference *a;
  177. struct data_reference *b;
  178. /* A "yes/no/maybe" field for the dependence relation:
  179. - when "ARE_DEPENDENT == NULL_TREE", there exist a dependence
  180. relation between A and B, and the description of this relation
  181. is given in the SUBSCRIPTS array,
  182. - when "ARE_DEPENDENT == chrec_known", there is no dependence and
  183. SUBSCRIPTS is empty,
  184. - when "ARE_DEPENDENT == chrec_dont_know", there may be a dependence,
  185. but the analyzer cannot be more specific. */
  186. tree are_dependent;
  187. /* For each subscript in the dependence test, there is an element in
  188. this array. This is the attribute that labels the edge A->B of
  189. the data_dependence_relation. */
  190. vec<subscript_p> subscripts;
  191. /* The analyzed loop nest. */
  192. vec<loop_p> loop_nest;
  193. /* The classic direction vector. */
  194. vec<lambda_vector> dir_vects;
  195. /* The classic distance vector. */
  196. vec<lambda_vector> dist_vects;
  197. /* An index in loop_nest for the innermost loop that varies for
  198. this data dependence relation. */
  199. unsigned inner_loop;
  200. /* Is the dependence reversed with respect to the lexicographic order? */
  201. bool reversed_p;
  202. /* When the dependence relation is affine, it can be represented by
  203. a distance vector. */
  204. bool affine_p;
  205. /* Set to true when the dependence relation is on the same data
  206. access. */
  207. bool self_reference_p;
  208. };
  209. typedef struct data_dependence_relation *ddr_p;
  210. #define DDR_A(DDR) DDR->a
  211. #define DDR_B(DDR) DDR->b
  212. #define DDR_AFFINE_P(DDR) DDR->affine_p
  213. #define DDR_ARE_DEPENDENT(DDR) DDR->are_dependent
  214. #define DDR_SUBSCRIPTS(DDR) DDR->subscripts
  215. #define DDR_SUBSCRIPT(DDR, I) DDR_SUBSCRIPTS (DDR)[I]
  216. #define DDR_NUM_SUBSCRIPTS(DDR) DDR_SUBSCRIPTS (DDR).length ()
  217. #define DDR_LOOP_NEST(DDR) DDR->loop_nest
  218. /* The size of the direction/distance vectors: the number of loops in
  219. the loop nest. */
  220. #define DDR_NB_LOOPS(DDR) (DDR_LOOP_NEST (DDR).length ())
  221. #define DDR_INNER_LOOP(DDR) DDR->inner_loop
  222. #define DDR_SELF_REFERENCE(DDR) DDR->self_reference_p
  223. #define DDR_DIST_VECTS(DDR) ((DDR)->dist_vects)
  224. #define DDR_DIR_VECTS(DDR) ((DDR)->dir_vects)
  225. #define DDR_NUM_DIST_VECTS(DDR) \
  226. (DDR_DIST_VECTS (DDR).length ())
  227. #define DDR_NUM_DIR_VECTS(DDR) \
  228. (DDR_DIR_VECTS (DDR).length ())
  229. #define DDR_DIR_VECT(DDR, I) \
  230. DDR_DIR_VECTS (DDR)[I]
  231. #define DDR_DIST_VECT(DDR, I) \
  232. DDR_DIST_VECTS (DDR)[I]
  233. #define DDR_REVERSED_P(DDR) DDR->reversed_p
  234. bool dr_analyze_innermost (struct data_reference *, struct loop *);
  235. extern bool compute_data_dependences_for_loop (struct loop *, bool,
  236. vec<loop_p> *,
  237. vec<data_reference_p> *,
  238. vec<ddr_p> *);
  239. extern bool compute_data_dependences_for_bb (basic_block, bool,
  240. vec<data_reference_p> *,
  241. vec<ddr_p> *);
  242. extern void debug_ddrs (vec<ddr_p> );
  243. extern void dump_data_reference (FILE *, struct data_reference *);
  244. extern void debug (data_reference &ref);
  245. extern void debug (data_reference *ptr);
  246. extern void debug_data_reference (struct data_reference *);
  247. extern void debug_data_references (vec<data_reference_p> );
  248. extern void debug (vec<data_reference_p> &ref);
  249. extern void debug (vec<data_reference_p> *ptr);
  250. extern void debug_data_dependence_relation (struct data_dependence_relation *);
  251. extern void dump_data_dependence_relations (FILE *, vec<ddr_p> );
  252. extern void debug (vec<ddr_p> &ref);
  253. extern void debug (vec<ddr_p> *ptr);
  254. extern void debug_data_dependence_relations (vec<ddr_p> );
  255. extern void free_dependence_relation (struct data_dependence_relation *);
  256. extern void free_dependence_relations (vec<ddr_p> );
  257. extern void free_data_ref (data_reference_p);
  258. extern void free_data_refs (vec<data_reference_p> );
  259. extern bool find_data_references_in_stmt (struct loop *, gimple,
  260. vec<data_reference_p> *);
  261. extern bool graphite_find_data_references_in_stmt (loop_p, loop_p, gimple,
  262. vec<data_reference_p> *);
  263. tree find_data_references_in_loop (struct loop *, vec<data_reference_p> *);
  264. struct data_reference *create_data_ref (loop_p, loop_p, tree, gimple, bool);
  265. extern bool find_loop_nest (struct loop *, vec<loop_p> *);
  266. extern struct data_dependence_relation *initialize_data_dependence_relation
  267. (struct data_reference *, struct data_reference *, vec<loop_p>);
  268. extern void compute_affine_dependence (struct data_dependence_relation *,
  269. loop_p);
  270. extern void compute_self_dependence (struct data_dependence_relation *);
  271. extern bool compute_all_dependences (vec<data_reference_p> ,
  272. vec<ddr_p> *,
  273. vec<loop_p>, bool);
  274. extern tree find_data_references_in_bb (struct loop *, basic_block,
  275. vec<data_reference_p> *);
  276. extern bool dr_may_alias_p (const struct data_reference *,
  277. const struct data_reference *, bool);
  278. extern bool dr_equal_offsets_p (struct data_reference *,
  279. struct data_reference *);
  280. extern void tree_check_data_deps (void);
  281. /* Return true when the base objects of data references A and B are
  282. the same memory object. */
  283. static inline bool
  284. same_data_refs_base_objects (data_reference_p a, data_reference_p b)
  285. {
  286. return DR_NUM_DIMENSIONS (a) == DR_NUM_DIMENSIONS (b)
  287. && operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0);
  288. }
  289. /* Return true when the data references A and B are accessing the same
  290. memory object with the same access functions. */
  291. static inline bool
  292. same_data_refs (data_reference_p a, data_reference_p b)
  293. {
  294. unsigned int i;
  295. /* The references are exactly the same. */
  296. if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
  297. return true;
  298. if (!same_data_refs_base_objects (a, b))
  299. return false;
  300. for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
  301. if (!eq_evolutions_p (DR_ACCESS_FN (a, i), DR_ACCESS_FN (b, i)))
  302. return false;
  303. return true;
  304. }
  305. /* Return true when the DDR contains two data references that have the
  306. same access functions. */
  307. static inline bool
  308. same_access_functions (const struct data_dependence_relation *ddr)
  309. {
  310. unsigned i;
  311. for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
  312. if (!eq_evolutions_p (DR_ACCESS_FN (DDR_A (ddr), i),
  313. DR_ACCESS_FN (DDR_B (ddr), i)))
  314. return false;
  315. return true;
  316. }
  317. /* Returns true when all the dependences are computable. */
  318. inline bool
  319. known_dependences_p (vec<ddr_p> dependence_relations)
  320. {
  321. ddr_p ddr;
  322. unsigned int i;
  323. FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
  324. if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
  325. return false;
  326. return true;
  327. }
  328. /* Returns the dependence level for a vector DIST of size LENGTH.
  329. LEVEL = 0 means a lexicographic dependence, i.e. a dependence due
  330. to the sequence of statements, not carried by any loop. */
  331. static inline unsigned
  332. dependence_level (lambda_vector dist_vect, int length)
  333. {
  334. int i;
  335. for (i = 0; i < length; i++)
  336. if (dist_vect[i] != 0)
  337. return i + 1;
  338. return 0;
  339. }
  340. /* Return the dependence level for the DDR relation. */
  341. static inline unsigned
  342. ddr_dependence_level (ddr_p ddr)
  343. {
  344. unsigned vector;
  345. unsigned level = 0;
  346. if (DDR_DIST_VECTS (ddr).exists ())
  347. level = dependence_level (DDR_DIST_VECT (ddr, 0), DDR_NB_LOOPS (ddr));
  348. for (vector = 1; vector < DDR_NUM_DIST_VECTS (ddr); vector++)
  349. level = MIN (level, dependence_level (DDR_DIST_VECT (ddr, vector),
  350. DDR_NB_LOOPS (ddr)));
  351. return level;
  352. }
  353. /* Return the index of the variable VAR in the LOOP_NEST array. */
  354. static inline int
  355. index_in_loop_nest (int var, vec<loop_p> loop_nest)
  356. {
  357. struct loop *loopi;
  358. int var_index;
  359. for (var_index = 0; loop_nest.iterate (var_index, &loopi);
  360. var_index++)
  361. if (loopi->num == var)
  362. break;
  363. return var_index;
  364. }
  365. /* Returns true when the data reference DR the form "A[i] = ..."
  366. with a stride equal to its unit type size. */
  367. static inline bool
  368. adjacent_dr_p (struct data_reference *dr)
  369. {
  370. /* If this is a bitfield store bail out. */
  371. if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
  372. && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
  373. return false;
  374. if (!DR_STEP (dr)
  375. || TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
  376. return false;
  377. return tree_int_cst_equal (fold_unary (ABS_EXPR, TREE_TYPE (DR_STEP (dr)),
  378. DR_STEP (dr)),
  379. TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
  380. }
  381. void split_constant_offset (tree , tree *, tree *);
  382. /* Compute the greatest common divisor of a VECTOR of SIZE numbers. */
  383. static inline int
  384. lambda_vector_gcd (lambda_vector vector, int size)
  385. {
  386. int i;
  387. int gcd1 = 0;
  388. if (size > 0)
  389. {
  390. gcd1 = vector[0];
  391. for (i = 1; i < size; i++)
  392. gcd1 = gcd (gcd1, vector[i]);
  393. }
  394. return gcd1;
  395. }
  396. /* Allocate a new vector of given SIZE. */
  397. static inline lambda_vector
  398. lambda_vector_new (int size)
  399. {
  400. /* ??? We shouldn't abuse the GC allocator here. */
  401. return ggc_cleared_vec_alloc<int> (size);
  402. }
  403. /* Clear out vector VEC1 of length SIZE. */
  404. static inline void
  405. lambda_vector_clear (lambda_vector vec1, int size)
  406. {
  407. memset (vec1, 0, size * sizeof (*vec1));
  408. }
  409. /* Returns true when the vector V is lexicographically positive, in
  410. other words, when the first nonzero element is positive. */
  411. static inline bool
  412. lambda_vector_lexico_pos (lambda_vector v,
  413. unsigned n)
  414. {
  415. unsigned i;
  416. for (i = 0; i < n; i++)
  417. {
  418. if (v[i] == 0)
  419. continue;
  420. if (v[i] < 0)
  421. return false;
  422. if (v[i] > 0)
  423. return true;
  424. }
  425. return true;
  426. }
  427. /* Return true if vector VEC1 of length SIZE is the zero vector. */
  428. static inline bool
  429. lambda_vector_zerop (lambda_vector vec1, int size)
  430. {
  431. int i;
  432. for (i = 0; i < size; i++)
  433. if (vec1[i] != 0)
  434. return false;
  435. return true;
  436. }
  437. /* Allocate a matrix of M rows x N cols. */
  438. static inline lambda_matrix
  439. lambda_matrix_new (int m, int n, struct obstack *lambda_obstack)
  440. {
  441. lambda_matrix mat;
  442. int i;
  443. mat = XOBNEWVEC (lambda_obstack, lambda_vector, m);
  444. for (i = 0; i < m; i++)
  445. mat[i] = XOBNEWVEC (lambda_obstack, int, n);
  446. return mat;
  447. }
  448. #endif /* GCC_TREE_DATA_REF_H */