1*e4b17023SJohn Marino /* Translation of CLAST (CLooG AST) to Gimple.
2*e4b17023SJohn Marino    Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
3*e4b17023SJohn Marino    Contributed by Sebastian Pop <sebastian.pop@amd.com>.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
8*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
9*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
10*e4b17023SJohn Marino any later version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
13*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*e4b17023SJohn Marino GNU General Public License for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino #include "config.h"
22*e4b17023SJohn Marino #include "system.h"
23*e4b17023SJohn Marino #include "coretypes.h"
24*e4b17023SJohn Marino #include "diagnostic-core.h"
25*e4b17023SJohn Marino #include "tree-flow.h"
26*e4b17023SJohn Marino #include "tree-dump.h"
27*e4b17023SJohn Marino #include "cfgloop.h"
28*e4b17023SJohn Marino #include "tree-chrec.h"
29*e4b17023SJohn Marino #include "tree-data-ref.h"
30*e4b17023SJohn Marino #include "tree-scalar-evolution.h"
31*e4b17023SJohn Marino #include "sese.h"
32*e4b17023SJohn Marino 
33*e4b17023SJohn Marino #ifdef HAVE_cloog
34*e4b17023SJohn Marino #include "cloog/cloog.h"
35*e4b17023SJohn Marino #include "ppl_c.h"
36*e4b17023SJohn Marino #include "graphite-cloog-util.h"
37*e4b17023SJohn Marino #include "graphite-ppl.h"
38*e4b17023SJohn Marino #include "graphite-poly.h"
39*e4b17023SJohn Marino #include "graphite-clast-to-gimple.h"
40*e4b17023SJohn Marino #include "graphite-dependences.h"
41*e4b17023SJohn Marino #include "graphite-cloog-compat.h"
42*e4b17023SJohn Marino 
43*e4b17023SJohn Marino #ifndef CLOOG_LANGUAGE_C
44*e4b17023SJohn Marino #define CLOOG_LANGUAGE_C LANGUAGE_C
45*e4b17023SJohn Marino #endif
46*e4b17023SJohn Marino 
47*e4b17023SJohn Marino /* This flag is set when an error occurred during the translation of
48*e4b17023SJohn Marino    CLAST to Gimple.  */
49*e4b17023SJohn Marino static bool gloog_error;
50*e4b17023SJohn Marino 
51*e4b17023SJohn Marino /* Verifies properties that GRAPHITE should maintain during translation.  */
52*e4b17023SJohn Marino 
53*e4b17023SJohn Marino static inline void
graphite_verify(void)54*e4b17023SJohn Marino graphite_verify (void)
55*e4b17023SJohn Marino {
56*e4b17023SJohn Marino #ifdef ENABLE_CHECKING
57*e4b17023SJohn Marino   verify_loop_structure ();
58*e4b17023SJohn Marino   verify_dominators (CDI_DOMINATORS);
59*e4b17023SJohn Marino   verify_loop_closed_ssa (true);
60*e4b17023SJohn Marino #endif
61*e4b17023SJohn Marino }
62*e4b17023SJohn Marino 
63*e4b17023SJohn Marino /* Stores the INDEX in a vector and the loop nesting LEVEL for a given
64*e4b17023SJohn Marino    clast NAME.  BOUND_ONE and BOUND_TWO represent the exact lower and
65*e4b17023SJohn Marino    upper bounds that can be inferred from the polyhedral representation.  */
66*e4b17023SJohn Marino 
67*e4b17023SJohn Marino typedef struct clast_name_index {
68*e4b17023SJohn Marino   int index;
69*e4b17023SJohn Marino   int level;
70*e4b17023SJohn Marino   mpz_t bound_one, bound_two;
71*e4b17023SJohn Marino   const char *name;
72*e4b17023SJohn Marino } *clast_name_index_p;
73*e4b17023SJohn Marino 
74*e4b17023SJohn Marino /* Returns a pointer to a new element of type clast_name_index_p built
75*e4b17023SJohn Marino    from NAME, INDEX, LEVEL, BOUND_ONE, and BOUND_TWO.  */
76*e4b17023SJohn Marino 
77*e4b17023SJohn Marino static inline clast_name_index_p
new_clast_name_index(const char * name,int index,int level,mpz_t bound_one,mpz_t bound_two)78*e4b17023SJohn Marino new_clast_name_index (const char *name, int index, int level,
79*e4b17023SJohn Marino 		      mpz_t bound_one, mpz_t bound_two)
80*e4b17023SJohn Marino {
81*e4b17023SJohn Marino   clast_name_index_p res = XNEW (struct clast_name_index);
82*e4b17023SJohn Marino 
83*e4b17023SJohn Marino   res->name = name;
84*e4b17023SJohn Marino   res->level = level;
85*e4b17023SJohn Marino   res->index = index;
86*e4b17023SJohn Marino   mpz_init (res->bound_one);
87*e4b17023SJohn Marino   mpz_init (res->bound_two);
88*e4b17023SJohn Marino   mpz_set (res->bound_one, bound_one);
89*e4b17023SJohn Marino   mpz_set (res->bound_two, bound_two);
90*e4b17023SJohn Marino   return res;
91*e4b17023SJohn Marino }
92*e4b17023SJohn Marino 
93*e4b17023SJohn Marino /* Free the memory taken by a clast_name_index struct.  */
94*e4b17023SJohn Marino 
95*e4b17023SJohn Marino static void
free_clast_name_index(void * ptr)96*e4b17023SJohn Marino free_clast_name_index (void *ptr)
97*e4b17023SJohn Marino {
98*e4b17023SJohn Marino   struct clast_name_index *c = (struct clast_name_index *) ptr;
99*e4b17023SJohn Marino   mpz_clear (c->bound_one);
100*e4b17023SJohn Marino   mpz_clear (c->bound_two);
101*e4b17023SJohn Marino   free (ptr);
102*e4b17023SJohn Marino }
103*e4b17023SJohn Marino 
104*e4b17023SJohn Marino /* For a given clast NAME, returns -1 if NAME is not in the
105*e4b17023SJohn Marino    INDEX_TABLE, otherwise returns the loop level for the induction
106*e4b17023SJohn Marino    variable NAME, or if it is a parameter, the parameter number in the
107*e4b17023SJohn Marino    vector of parameters.  */
108*e4b17023SJohn Marino 
109*e4b17023SJohn Marino static inline int
clast_name_to_level(clast_name_p name,htab_t index_table)110*e4b17023SJohn Marino clast_name_to_level (clast_name_p name, htab_t index_table)
111*e4b17023SJohn Marino {
112*e4b17023SJohn Marino   struct clast_name_index tmp;
113*e4b17023SJohn Marino   PTR *slot;
114*e4b17023SJohn Marino 
115*e4b17023SJohn Marino #ifdef CLOOG_ORG
116*e4b17023SJohn Marino   gcc_assert (name->type == clast_expr_name);
117*e4b17023SJohn Marino   tmp.name = ((const struct clast_name *) name)->name;
118*e4b17023SJohn Marino #else
119*e4b17023SJohn Marino   tmp.name = name;
120*e4b17023SJohn Marino #endif
121*e4b17023SJohn Marino 
122*e4b17023SJohn Marino   slot = htab_find_slot (index_table, &tmp, NO_INSERT);
123*e4b17023SJohn Marino 
124*e4b17023SJohn Marino   if (slot && *slot)
125*e4b17023SJohn Marino     return ((struct clast_name_index *) *slot)->level;
126*e4b17023SJohn Marino 
127*e4b17023SJohn Marino   return -1;
128*e4b17023SJohn Marino }
129*e4b17023SJohn Marino 
130*e4b17023SJohn Marino /* For a given clast NAME, returns -1 if it does not correspond to any
131*e4b17023SJohn Marino    parameter, or otherwise, returns the index in the PARAMS or
132*e4b17023SJohn Marino    SCATTERING_DIMENSIONS vector.  */
133*e4b17023SJohn Marino 
134*e4b17023SJohn Marino static inline int
clast_name_to_index(clast_name_p name,htab_t index_table)135*e4b17023SJohn Marino clast_name_to_index (clast_name_p name, htab_t index_table)
136*e4b17023SJohn Marino {
137*e4b17023SJohn Marino   struct clast_name_index tmp;
138*e4b17023SJohn Marino   PTR *slot;
139*e4b17023SJohn Marino 
140*e4b17023SJohn Marino #ifdef CLOOG_ORG
141*e4b17023SJohn Marino   gcc_assert (name->type == clast_expr_name);
142*e4b17023SJohn Marino   tmp.name = ((const struct clast_name *) name)->name;
143*e4b17023SJohn Marino #else
144*e4b17023SJohn Marino   tmp.name = name;
145*e4b17023SJohn Marino #endif
146*e4b17023SJohn Marino 
147*e4b17023SJohn Marino   slot = htab_find_slot (index_table, &tmp, NO_INSERT);
148*e4b17023SJohn Marino 
149*e4b17023SJohn Marino   if (slot && *slot)
150*e4b17023SJohn Marino     return ((struct clast_name_index *) *slot)->index;
151*e4b17023SJohn Marino 
152*e4b17023SJohn Marino   return -1;
153*e4b17023SJohn Marino }
154*e4b17023SJohn Marino 
155*e4b17023SJohn Marino /* For a given clast NAME, initializes the lower and upper bounds BOUND_ONE
156*e4b17023SJohn Marino    and BOUND_TWO stored in the INDEX_TABLE.  Returns true when NAME has been
157*e4b17023SJohn Marino    found in the INDEX_TABLE, false otherwise.  */
158*e4b17023SJohn Marino 
159*e4b17023SJohn Marino static inline bool
clast_name_to_lb_ub(clast_name_p name,htab_t index_table,mpz_t bound_one,mpz_t bound_two)160*e4b17023SJohn Marino clast_name_to_lb_ub (clast_name_p name, htab_t index_table, mpz_t bound_one,
161*e4b17023SJohn Marino 		     mpz_t bound_two)
162*e4b17023SJohn Marino {
163*e4b17023SJohn Marino   struct clast_name_index tmp;
164*e4b17023SJohn Marino   PTR *slot;
165*e4b17023SJohn Marino 
166*e4b17023SJohn Marino #ifdef CLOOG_ORG
167*e4b17023SJohn Marino   gcc_assert (name->type == clast_expr_name);
168*e4b17023SJohn Marino   tmp.name = ((const struct clast_name *) name)->name;
169*e4b17023SJohn Marino #else
170*e4b17023SJohn Marino   tmp.name = name;
171*e4b17023SJohn Marino #endif
172*e4b17023SJohn Marino 
173*e4b17023SJohn Marino   slot = htab_find_slot (index_table, &tmp, NO_INSERT);
174*e4b17023SJohn Marino 
175*e4b17023SJohn Marino   if (slot && *slot)
176*e4b17023SJohn Marino     {
177*e4b17023SJohn Marino       mpz_set (bound_one, ((struct clast_name_index *) *slot)->bound_one);
178*e4b17023SJohn Marino       mpz_set (bound_two, ((struct clast_name_index *) *slot)->bound_two);
179*e4b17023SJohn Marino       return true;
180*e4b17023SJohn Marino     }
181*e4b17023SJohn Marino 
182*e4b17023SJohn Marino   return false;
183*e4b17023SJohn Marino }
184*e4b17023SJohn Marino 
185*e4b17023SJohn Marino /* Records in INDEX_TABLE the INDEX and LEVEL for NAME.  */
186*e4b17023SJohn Marino 
187*e4b17023SJohn Marino static inline void
save_clast_name_index(htab_t index_table,const char * name,int index,int level,mpz_t bound_one,mpz_t bound_two)188*e4b17023SJohn Marino save_clast_name_index (htab_t index_table, const char *name,
189*e4b17023SJohn Marino 		       int index, int level, mpz_t bound_one, mpz_t bound_two)
190*e4b17023SJohn Marino {
191*e4b17023SJohn Marino   struct clast_name_index tmp;
192*e4b17023SJohn Marino   PTR *slot;
193*e4b17023SJohn Marino 
194*e4b17023SJohn Marino   tmp.name = name;
195*e4b17023SJohn Marino   slot = htab_find_slot (index_table, &tmp, INSERT);
196*e4b17023SJohn Marino 
197*e4b17023SJohn Marino   if (slot)
198*e4b17023SJohn Marino     {
199*e4b17023SJohn Marino       free (*slot);
200*e4b17023SJohn Marino 
201*e4b17023SJohn Marino       *slot = new_clast_name_index (name, index, level, bound_one, bound_two);
202*e4b17023SJohn Marino     }
203*e4b17023SJohn Marino }
204*e4b17023SJohn Marino 
205*e4b17023SJohn Marino /* Computes a hash function for database element ELT.  */
206*e4b17023SJohn Marino 
207*e4b17023SJohn Marino static inline hashval_t
clast_name_index_elt_info(const void * elt)208*e4b17023SJohn Marino clast_name_index_elt_info (const void *elt)
209*e4b17023SJohn Marino {
210*e4b17023SJohn Marino   return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
211*e4b17023SJohn Marino }
212*e4b17023SJohn Marino 
213*e4b17023SJohn Marino /* Compares database elements E1 and E2.  */
214*e4b17023SJohn Marino 
215*e4b17023SJohn Marino static inline int
eq_clast_name_indexes(const void * e1,const void * e2)216*e4b17023SJohn Marino eq_clast_name_indexes (const void *e1, const void *e2)
217*e4b17023SJohn Marino {
218*e4b17023SJohn Marino   const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
219*e4b17023SJohn Marino   const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
220*e4b17023SJohn Marino 
221*e4b17023SJohn Marino   return (elt1->name == elt2->name);
222*e4b17023SJohn Marino }
223*e4b17023SJohn Marino 
224*e4b17023SJohn Marino 
225*e4b17023SJohn Marino 
226*e4b17023SJohn Marino /* NEWIVS_INDEX binds CLooG's scattering name to the index of the tree
227*e4b17023SJohn Marino    induction variable in NEWIVS.
228*e4b17023SJohn Marino 
229*e4b17023SJohn Marino    PARAMS_INDEX binds CLooG's parameter name to the index of the tree
230*e4b17023SJohn Marino    parameter in PARAMS.  */
231*e4b17023SJohn Marino 
232*e4b17023SJohn Marino typedef struct ivs_params {
233*e4b17023SJohn Marino   VEC (tree, heap) *params, **newivs;
234*e4b17023SJohn Marino   htab_t newivs_index, params_index;
235*e4b17023SJohn Marino   sese region;
236*e4b17023SJohn Marino } *ivs_params_p;
237*e4b17023SJohn Marino 
238*e4b17023SJohn Marino /* Returns the tree variable from the name NAME that was given in
239*e4b17023SJohn Marino    Cloog representation.  */
240*e4b17023SJohn Marino 
241*e4b17023SJohn Marino static tree
clast_name_to_gcc(clast_name_p name,ivs_params_p ip)242*e4b17023SJohn Marino clast_name_to_gcc (clast_name_p name, ivs_params_p ip)
243*e4b17023SJohn Marino {
244*e4b17023SJohn Marino   int index;
245*e4b17023SJohn Marino 
246*e4b17023SJohn Marino   if (ip->params && ip->params_index)
247*e4b17023SJohn Marino     {
248*e4b17023SJohn Marino       index = clast_name_to_index (name, ip->params_index);
249*e4b17023SJohn Marino 
250*e4b17023SJohn Marino       if (index >= 0)
251*e4b17023SJohn Marino 	return VEC_index (tree, ip->params, index);
252*e4b17023SJohn Marino     }
253*e4b17023SJohn Marino 
254*e4b17023SJohn Marino   gcc_assert (*(ip->newivs) && ip->newivs_index);
255*e4b17023SJohn Marino   index = clast_name_to_index (name, ip->newivs_index);
256*e4b17023SJohn Marino   gcc_assert (index >= 0);
257*e4b17023SJohn Marino 
258*e4b17023SJohn Marino   return VEC_index (tree, *(ip->newivs), index);
259*e4b17023SJohn Marino }
260*e4b17023SJohn Marino 
261*e4b17023SJohn Marino /* Returns the maximal precision type for expressions TYPE1 and TYPE2.  */
262*e4b17023SJohn Marino 
263*e4b17023SJohn Marino static tree
max_precision_type(tree type1,tree type2)264*e4b17023SJohn Marino max_precision_type (tree type1, tree type2)
265*e4b17023SJohn Marino {
266*e4b17023SJohn Marino   enum machine_mode mode;
267*e4b17023SJohn Marino   int p1, p2, precision;
268*e4b17023SJohn Marino   tree type;
269*e4b17023SJohn Marino 
270*e4b17023SJohn Marino   if (POINTER_TYPE_P (type1))
271*e4b17023SJohn Marino     return type1;
272*e4b17023SJohn Marino 
273*e4b17023SJohn Marino   if (POINTER_TYPE_P (type2))
274*e4b17023SJohn Marino     return type2;
275*e4b17023SJohn Marino 
276*e4b17023SJohn Marino   if (TYPE_UNSIGNED (type1)
277*e4b17023SJohn Marino       && TYPE_UNSIGNED (type2))
278*e4b17023SJohn Marino     return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
279*e4b17023SJohn Marino 
280*e4b17023SJohn Marino   p1 = TYPE_PRECISION (type1);
281*e4b17023SJohn Marino   p2 = TYPE_PRECISION (type2);
282*e4b17023SJohn Marino 
283*e4b17023SJohn Marino   if (p1 > p2)
284*e4b17023SJohn Marino     precision = TYPE_UNSIGNED (type1) ? p1 * 2 : p1;
285*e4b17023SJohn Marino   else
286*e4b17023SJohn Marino     precision = TYPE_UNSIGNED (type2) ? p2 * 2 : p2;
287*e4b17023SJohn Marino 
288*e4b17023SJohn Marino   if (precision > BITS_PER_WORD)
289*e4b17023SJohn Marino     {
290*e4b17023SJohn Marino       gloog_error = true;
291*e4b17023SJohn Marino       return integer_type_node;
292*e4b17023SJohn Marino     }
293*e4b17023SJohn Marino 
294*e4b17023SJohn Marino   mode = smallest_mode_for_size (precision, MODE_INT);
295*e4b17023SJohn Marino   precision = GET_MODE_PRECISION (mode);
296*e4b17023SJohn Marino   type = build_nonstandard_integer_type (precision, false);
297*e4b17023SJohn Marino 
298*e4b17023SJohn Marino   if (!type)
299*e4b17023SJohn Marino     {
300*e4b17023SJohn Marino       gloog_error = true;
301*e4b17023SJohn Marino       return integer_type_node;
302*e4b17023SJohn Marino     }
303*e4b17023SJohn Marino 
304*e4b17023SJohn Marino   return type;
305*e4b17023SJohn Marino }
306*e4b17023SJohn Marino 
307*e4b17023SJohn Marino static tree
308*e4b17023SJohn Marino clast_to_gcc_expression (tree, struct clast_expr *, ivs_params_p);
309*e4b17023SJohn Marino 
310*e4b17023SJohn Marino /* Converts a Cloog reduction expression R with reduction operation OP
311*e4b17023SJohn Marino    to a GCC expression tree of type TYPE.  */
312*e4b17023SJohn Marino 
313*e4b17023SJohn Marino static tree
clast_to_gcc_expression_red(tree type,enum tree_code op,struct clast_reduction * r,ivs_params_p ip)314*e4b17023SJohn Marino clast_to_gcc_expression_red (tree type, enum tree_code op,
315*e4b17023SJohn Marino 			     struct clast_reduction *r, ivs_params_p ip)
316*e4b17023SJohn Marino {
317*e4b17023SJohn Marino   int i;
318*e4b17023SJohn Marino   tree res = clast_to_gcc_expression (type, r->elts[0], ip);
319*e4b17023SJohn Marino   tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
320*e4b17023SJohn Marino 
321*e4b17023SJohn Marino   for (i = 1; i < r->n; i++)
322*e4b17023SJohn Marino     {
323*e4b17023SJohn Marino       tree t = clast_to_gcc_expression (operand_type, r->elts[i], ip);
324*e4b17023SJohn Marino       res = fold_build2 (op, type, res, t);
325*e4b17023SJohn Marino     }
326*e4b17023SJohn Marino 
327*e4b17023SJohn Marino   return res;
328*e4b17023SJohn Marino }
329*e4b17023SJohn Marino 
330*e4b17023SJohn Marino /* Converts a Cloog AST expression E back to a GCC expression tree of
331*e4b17023SJohn Marino    type TYPE.  */
332*e4b17023SJohn Marino 
333*e4b17023SJohn Marino static tree
clast_to_gcc_expression(tree type,struct clast_expr * e,ivs_params_p ip)334*e4b17023SJohn Marino clast_to_gcc_expression (tree type, struct clast_expr *e, ivs_params_p ip)
335*e4b17023SJohn Marino {
336*e4b17023SJohn Marino   switch (e->type)
337*e4b17023SJohn Marino     {
338*e4b17023SJohn Marino     case clast_expr_term:
339*e4b17023SJohn Marino       {
340*e4b17023SJohn Marino 	struct clast_term *t = (struct clast_term *) e;
341*e4b17023SJohn Marino 
342*e4b17023SJohn Marino 	if (t->var)
343*e4b17023SJohn Marino 	  {
344*e4b17023SJohn Marino 	    if (mpz_cmp_si (t->val, 1) == 0)
345*e4b17023SJohn Marino 	      {
346*e4b17023SJohn Marino 		tree name = clast_name_to_gcc (t->var, ip);
347*e4b17023SJohn Marino 
348*e4b17023SJohn Marino 		if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
349*e4b17023SJohn Marino 		  name = convert_to_ptrofftype (name);
350*e4b17023SJohn Marino 
351*e4b17023SJohn Marino 		name = fold_convert (type, name);
352*e4b17023SJohn Marino 		return name;
353*e4b17023SJohn Marino 	      }
354*e4b17023SJohn Marino 
355*e4b17023SJohn Marino 	    else if (mpz_cmp_si (t->val, -1) == 0)
356*e4b17023SJohn Marino 	      {
357*e4b17023SJohn Marino 		tree name = clast_name_to_gcc (t->var, ip);
358*e4b17023SJohn Marino 
359*e4b17023SJohn Marino 		if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
360*e4b17023SJohn Marino 		  name = convert_to_ptrofftype (name);
361*e4b17023SJohn Marino 
362*e4b17023SJohn Marino 		name = fold_convert (type, name);
363*e4b17023SJohn Marino 
364*e4b17023SJohn Marino 		return fold_build1 (NEGATE_EXPR, type, name);
365*e4b17023SJohn Marino 	      }
366*e4b17023SJohn Marino 	    else
367*e4b17023SJohn Marino 	      {
368*e4b17023SJohn Marino 		tree name = clast_name_to_gcc (t->var, ip);
369*e4b17023SJohn Marino 		tree cst = gmp_cst_to_tree (type, t->val);
370*e4b17023SJohn Marino 
371*e4b17023SJohn Marino 		if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
372*e4b17023SJohn Marino 		  name = convert_to_ptrofftype (name);
373*e4b17023SJohn Marino 
374*e4b17023SJohn Marino 		name = fold_convert (type, name);
375*e4b17023SJohn Marino 
376*e4b17023SJohn Marino 		if (!POINTER_TYPE_P (type))
377*e4b17023SJohn Marino 		  return fold_build2 (MULT_EXPR, type, cst, name);
378*e4b17023SJohn Marino 
379*e4b17023SJohn Marino 		gloog_error = true;
380*e4b17023SJohn Marino 		return cst;
381*e4b17023SJohn Marino 	      }
382*e4b17023SJohn Marino 	  }
383*e4b17023SJohn Marino 	else
384*e4b17023SJohn Marino 	  return gmp_cst_to_tree (type, t->val);
385*e4b17023SJohn Marino       }
386*e4b17023SJohn Marino 
387*e4b17023SJohn Marino     case clast_expr_red:
388*e4b17023SJohn Marino       {
389*e4b17023SJohn Marino         struct clast_reduction *r = (struct clast_reduction *) e;
390*e4b17023SJohn Marino 
391*e4b17023SJohn Marino         switch (r->type)
392*e4b17023SJohn Marino           {
393*e4b17023SJohn Marino 	  case clast_red_sum:
394*e4b17023SJohn Marino 	    return clast_to_gcc_expression_red
395*e4b17023SJohn Marino 	      (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
396*e4b17023SJohn Marino 	       r, ip);
397*e4b17023SJohn Marino 
398*e4b17023SJohn Marino 	  case clast_red_min:
399*e4b17023SJohn Marino 	    return clast_to_gcc_expression_red (type, MIN_EXPR, r, ip);
400*e4b17023SJohn Marino 
401*e4b17023SJohn Marino 	  case clast_red_max:
402*e4b17023SJohn Marino 	    return clast_to_gcc_expression_red (type, MAX_EXPR, r, ip);
403*e4b17023SJohn Marino 
404*e4b17023SJohn Marino 	  default:
405*e4b17023SJohn Marino 	    gcc_unreachable ();
406*e4b17023SJohn Marino           }
407*e4b17023SJohn Marino         break;
408*e4b17023SJohn Marino       }
409*e4b17023SJohn Marino 
410*e4b17023SJohn Marino     case clast_expr_bin:
411*e4b17023SJohn Marino       {
412*e4b17023SJohn Marino 	struct clast_binary *b = (struct clast_binary *) e;
413*e4b17023SJohn Marino 	struct clast_expr *lhs = (struct clast_expr *) b->LHS;
414*e4b17023SJohn Marino 	tree tl = clast_to_gcc_expression (type, lhs, ip);
415*e4b17023SJohn Marino 	tree tr = gmp_cst_to_tree (type, b->RHS);
416*e4b17023SJohn Marino 
417*e4b17023SJohn Marino 	switch (b->type)
418*e4b17023SJohn Marino 	  {
419*e4b17023SJohn Marino 	  case clast_bin_fdiv:
420*e4b17023SJohn Marino 	    return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
421*e4b17023SJohn Marino 
422*e4b17023SJohn Marino 	  case clast_bin_cdiv:
423*e4b17023SJohn Marino 	    return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
424*e4b17023SJohn Marino 
425*e4b17023SJohn Marino 	  case clast_bin_div:
426*e4b17023SJohn Marino 	    return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
427*e4b17023SJohn Marino 
428*e4b17023SJohn Marino 	  case clast_bin_mod:
429*e4b17023SJohn Marino 	    return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
430*e4b17023SJohn Marino 
431*e4b17023SJohn Marino 	  default:
432*e4b17023SJohn Marino 	    gcc_unreachable ();
433*e4b17023SJohn Marino 	  }
434*e4b17023SJohn Marino       }
435*e4b17023SJohn Marino 
436*e4b17023SJohn Marino     default:
437*e4b17023SJohn Marino       gcc_unreachable ();
438*e4b17023SJohn Marino     }
439*e4b17023SJohn Marino 
440*e4b17023SJohn Marino   return NULL_TREE;
441*e4b17023SJohn Marino }
442*e4b17023SJohn Marino 
443*e4b17023SJohn Marino /* Return a type that could represent the values between BOUND_ONE and
444*e4b17023SJohn Marino    BOUND_TWO.  */
445*e4b17023SJohn Marino 
446*e4b17023SJohn Marino static tree
type_for_interval(mpz_t bound_one,mpz_t bound_two)447*e4b17023SJohn Marino type_for_interval (mpz_t bound_one, mpz_t bound_two)
448*e4b17023SJohn Marino {
449*e4b17023SJohn Marino   bool unsigned_p;
450*e4b17023SJohn Marino   tree type;
451*e4b17023SJohn Marino   enum machine_mode mode;
452*e4b17023SJohn Marino   int wider_precision;
453*e4b17023SJohn Marino   int precision = MAX (mpz_sizeinbase (bound_one, 2),
454*e4b17023SJohn Marino 		       mpz_sizeinbase (bound_two, 2));
455*e4b17023SJohn Marino 
456*e4b17023SJohn Marino   if (precision > BITS_PER_WORD)
457*e4b17023SJohn Marino     {
458*e4b17023SJohn Marino       gloog_error = true;
459*e4b17023SJohn Marino       return integer_type_node;
460*e4b17023SJohn Marino     }
461*e4b17023SJohn Marino 
462*e4b17023SJohn Marino   if (mpz_cmp (bound_one, bound_two) <= 0)
463*e4b17023SJohn Marino     unsigned_p = (mpz_sgn (bound_one) >= 0);
464*e4b17023SJohn Marino   else
465*e4b17023SJohn Marino     unsigned_p = (mpz_sgn (bound_two) >= 0);
466*e4b17023SJohn Marino 
467*e4b17023SJohn Marino   mode = smallest_mode_for_size (precision, MODE_INT);
468*e4b17023SJohn Marino   wider_precision = GET_MODE_PRECISION (mode);
469*e4b17023SJohn Marino 
470*e4b17023SJohn Marino   /* As we want to generate signed types as much as possible, try to
471*e4b17023SJohn Marino      fit the interval [bound_one, bound_two] in a signed type.  For example,
472*e4b17023SJohn Marino      supposing that we have the interval [0, 100], instead of
473*e4b17023SJohn Marino      generating unsigned char, we want to generate a signed char.  */
474*e4b17023SJohn Marino   if (unsigned_p && precision < wider_precision)
475*e4b17023SJohn Marino     unsigned_p = false;
476*e4b17023SJohn Marino 
477*e4b17023SJohn Marino   type = build_nonstandard_integer_type (wider_precision, unsigned_p);
478*e4b17023SJohn Marino 
479*e4b17023SJohn Marino   if (!type)
480*e4b17023SJohn Marino     {
481*e4b17023SJohn Marino       gloog_error = true;
482*e4b17023SJohn Marino       return integer_type_node;
483*e4b17023SJohn Marino     }
484*e4b17023SJohn Marino 
485*e4b17023SJohn Marino   return type;
486*e4b17023SJohn Marino }
487*e4b17023SJohn Marino 
488*e4b17023SJohn Marino /* Return a type that could represent the integer value VAL, or
489*e4b17023SJohn Marino    otherwise return NULL_TREE.  */
490*e4b17023SJohn Marino 
491*e4b17023SJohn Marino static tree
type_for_value(mpz_t val)492*e4b17023SJohn Marino type_for_value (mpz_t val)
493*e4b17023SJohn Marino {
494*e4b17023SJohn Marino   return type_for_interval (val, val);
495*e4b17023SJohn Marino }
496*e4b17023SJohn Marino 
497*e4b17023SJohn Marino /* Return the type for the clast_term T.  Initializes BOUND_ONE and
498*e4b17023SJohn Marino    BOUND_TWO to the bounds of the term.  */
499*e4b17023SJohn Marino 
500*e4b17023SJohn Marino static tree
type_for_clast_term(struct clast_term * t,ivs_params_p ip,mpz_t bound_one,mpz_t bound_two)501*e4b17023SJohn Marino type_for_clast_term (struct clast_term *t, ivs_params_p ip, mpz_t bound_one,
502*e4b17023SJohn Marino 		     mpz_t bound_two)
503*e4b17023SJohn Marino {
504*e4b17023SJohn Marino   clast_name_p name = t->var;
505*e4b17023SJohn Marino   bool found = false;
506*e4b17023SJohn Marino 
507*e4b17023SJohn Marino   gcc_assert (t->expr.type == clast_expr_term);
508*e4b17023SJohn Marino 
509*e4b17023SJohn Marino   if (!name)
510*e4b17023SJohn Marino     {
511*e4b17023SJohn Marino       mpz_set (bound_one, t->val);
512*e4b17023SJohn Marino       mpz_set (bound_two, t->val);
513*e4b17023SJohn Marino       return type_for_value (t->val);
514*e4b17023SJohn Marino     }
515*e4b17023SJohn Marino 
516*e4b17023SJohn Marino   if (ip->params && ip->params_index)
517*e4b17023SJohn Marino     found = clast_name_to_lb_ub (name, ip->params_index, bound_one, bound_two);
518*e4b17023SJohn Marino 
519*e4b17023SJohn Marino   if (!found)
520*e4b17023SJohn Marino     {
521*e4b17023SJohn Marino       gcc_assert (*(ip->newivs) && ip->newivs_index);
522*e4b17023SJohn Marino       found = clast_name_to_lb_ub (name, ip->newivs_index,
523*e4b17023SJohn Marino 				   bound_one, bound_two);
524*e4b17023SJohn Marino       gcc_assert (found);
525*e4b17023SJohn Marino     }
526*e4b17023SJohn Marino 
527*e4b17023SJohn Marino   mpz_mul (bound_one, bound_one, t->val);
528*e4b17023SJohn Marino   mpz_mul (bound_two, bound_two, t->val);
529*e4b17023SJohn Marino 
530*e4b17023SJohn Marino   return TREE_TYPE (clast_name_to_gcc (name, ip));
531*e4b17023SJohn Marino }
532*e4b17023SJohn Marino 
533*e4b17023SJohn Marino static tree
534*e4b17023SJohn Marino type_for_clast_expr (struct clast_expr *, ivs_params_p, mpz_t, mpz_t);
535*e4b17023SJohn Marino 
536*e4b17023SJohn Marino /* Return the type for the clast_reduction R.  Initializes BOUND_ONE
537*e4b17023SJohn Marino    and BOUND_TWO to the bounds of the reduction expression.  */
538*e4b17023SJohn Marino 
539*e4b17023SJohn Marino static tree
type_for_clast_red(struct clast_reduction * r,ivs_params_p ip,mpz_t bound_one,mpz_t bound_two)540*e4b17023SJohn Marino type_for_clast_red (struct clast_reduction *r, ivs_params_p ip,
541*e4b17023SJohn Marino 		    mpz_t bound_one, mpz_t bound_two)
542*e4b17023SJohn Marino {
543*e4b17023SJohn Marino   int i;
544*e4b17023SJohn Marino   tree type = type_for_clast_expr (r->elts[0], ip, bound_one, bound_two);
545*e4b17023SJohn Marino   mpz_t b1, b2, m1, m2;
546*e4b17023SJohn Marino 
547*e4b17023SJohn Marino   if (r->n == 1)
548*e4b17023SJohn Marino     return type;
549*e4b17023SJohn Marino 
550*e4b17023SJohn Marino   mpz_init (b1);
551*e4b17023SJohn Marino   mpz_init (b2);
552*e4b17023SJohn Marino   mpz_init (m1);
553*e4b17023SJohn Marino   mpz_init (m2);
554*e4b17023SJohn Marino 
555*e4b17023SJohn Marino   for (i = 1; i < r->n; i++)
556*e4b17023SJohn Marino     {
557*e4b17023SJohn Marino       tree t = type_for_clast_expr (r->elts[i], ip, b1, b2);
558*e4b17023SJohn Marino       type = max_precision_type (type, t);
559*e4b17023SJohn Marino 
560*e4b17023SJohn Marino       switch (r->type)
561*e4b17023SJohn Marino 	{
562*e4b17023SJohn Marino 	case clast_red_sum:
563*e4b17023SJohn Marino 	  value_min (m1, bound_one, bound_two);
564*e4b17023SJohn Marino 	  value_min (m2, b1, b2);
565*e4b17023SJohn Marino 	  mpz_add (bound_one, m1, m2);
566*e4b17023SJohn Marino 
567*e4b17023SJohn Marino 	  value_max (m1, bound_one, bound_two);
568*e4b17023SJohn Marino 	  value_max (m2, b1, b2);
569*e4b17023SJohn Marino 	  mpz_add (bound_two, m1, m2);
570*e4b17023SJohn Marino 	  break;
571*e4b17023SJohn Marino 
572*e4b17023SJohn Marino 	case clast_red_min:
573*e4b17023SJohn Marino 	  value_min (bound_one, bound_one, bound_two);
574*e4b17023SJohn Marino 	  value_min (bound_two, b1, b2);
575*e4b17023SJohn Marino 	  break;
576*e4b17023SJohn Marino 
577*e4b17023SJohn Marino 	case clast_red_max:
578*e4b17023SJohn Marino 	  value_max (bound_one, bound_one, bound_two);
579*e4b17023SJohn Marino 	  value_max (bound_two, b1, b2);
580*e4b17023SJohn Marino 	  break;
581*e4b17023SJohn Marino 
582*e4b17023SJohn Marino 	default:
583*e4b17023SJohn Marino 	  gcc_unreachable ();
584*e4b17023SJohn Marino 	  break;
585*e4b17023SJohn Marino 	}
586*e4b17023SJohn Marino     }
587*e4b17023SJohn Marino 
588*e4b17023SJohn Marino   mpz_clear (b1);
589*e4b17023SJohn Marino   mpz_clear (b2);
590*e4b17023SJohn Marino   mpz_clear (m1);
591*e4b17023SJohn Marino   mpz_clear (m2);
592*e4b17023SJohn Marino 
593*e4b17023SJohn Marino   /* Return a type that can represent the result of the reduction.  */
594*e4b17023SJohn Marino   return max_precision_type (type, type_for_interval (bound_one, bound_two));
595*e4b17023SJohn Marino }
596*e4b17023SJohn Marino 
597*e4b17023SJohn Marino /* Return the type for the clast_binary B used in STMT.  */
598*e4b17023SJohn Marino 
599*e4b17023SJohn Marino static tree
type_for_clast_bin(struct clast_binary * b,ivs_params_p ip,mpz_t bound_one,mpz_t bound_two)600*e4b17023SJohn Marino type_for_clast_bin (struct clast_binary *b, ivs_params_p ip, mpz_t bound_one,
601*e4b17023SJohn Marino 		    mpz_t bound_two)
602*e4b17023SJohn Marino {
603*e4b17023SJohn Marino   mpz_t one;
604*e4b17023SJohn Marino   tree l = type_for_clast_expr ((struct clast_expr *) b->LHS, ip,
605*e4b17023SJohn Marino 				bound_one, bound_two);
606*e4b17023SJohn Marino   tree r = type_for_value (b->RHS);
607*e4b17023SJohn Marino   tree type = max_precision_type (l, r);
608*e4b17023SJohn Marino 
609*e4b17023SJohn Marino   switch (b->type)
610*e4b17023SJohn Marino     {
611*e4b17023SJohn Marino     case clast_bin_fdiv:
612*e4b17023SJohn Marino       mpz_mdiv (bound_one, bound_one, b->RHS);
613*e4b17023SJohn Marino       mpz_mdiv (bound_two, bound_two, b->RHS);
614*e4b17023SJohn Marino       break;
615*e4b17023SJohn Marino 
616*e4b17023SJohn Marino     case clast_bin_cdiv:
617*e4b17023SJohn Marino       mpz_mdiv (bound_one, bound_one, b->RHS);
618*e4b17023SJohn Marino       mpz_mdiv (bound_two, bound_two, b->RHS);
619*e4b17023SJohn Marino       mpz_init (one);
620*e4b17023SJohn Marino       mpz_add (bound_one, bound_one, one);
621*e4b17023SJohn Marino       mpz_add (bound_two, bound_two, one);
622*e4b17023SJohn Marino       mpz_clear (one);
623*e4b17023SJohn Marino       break;
624*e4b17023SJohn Marino 
625*e4b17023SJohn Marino     case clast_bin_div:
626*e4b17023SJohn Marino       mpz_div (bound_one, bound_one, b->RHS);
627*e4b17023SJohn Marino       mpz_div (bound_two, bound_two, b->RHS);
628*e4b17023SJohn Marino       break;
629*e4b17023SJohn Marino 
630*e4b17023SJohn Marino     case clast_bin_mod:
631*e4b17023SJohn Marino       mpz_mod (bound_one, bound_one, b->RHS);
632*e4b17023SJohn Marino       mpz_mod (bound_two, bound_two, b->RHS);
633*e4b17023SJohn Marino       break;
634*e4b17023SJohn Marino 
635*e4b17023SJohn Marino     default:
636*e4b17023SJohn Marino       gcc_unreachable ();
637*e4b17023SJohn Marino     }
638*e4b17023SJohn Marino 
639*e4b17023SJohn Marino   /* Return a type that can represent the result of the reduction.  */
640*e4b17023SJohn Marino   return max_precision_type (type, type_for_interval (bound_one, bound_two));
641*e4b17023SJohn Marino }
642*e4b17023SJohn Marino 
643*e4b17023SJohn Marino /* Returns the type for the CLAST expression E when used in statement
644*e4b17023SJohn Marino    STMT.  */
645*e4b17023SJohn Marino 
646*e4b17023SJohn Marino static tree
type_for_clast_expr(struct clast_expr * e,ivs_params_p ip,mpz_t bound_one,mpz_t bound_two)647*e4b17023SJohn Marino type_for_clast_expr (struct clast_expr *e, ivs_params_p ip, mpz_t bound_one,
648*e4b17023SJohn Marino 		     mpz_t bound_two)
649*e4b17023SJohn Marino {
650*e4b17023SJohn Marino   switch (e->type)
651*e4b17023SJohn Marino     {
652*e4b17023SJohn Marino     case clast_expr_term:
653*e4b17023SJohn Marino       return type_for_clast_term ((struct clast_term *) e, ip,
654*e4b17023SJohn Marino 				  bound_one, bound_two);
655*e4b17023SJohn Marino 
656*e4b17023SJohn Marino     case clast_expr_red:
657*e4b17023SJohn Marino       return type_for_clast_red ((struct clast_reduction *) e, ip,
658*e4b17023SJohn Marino 				 bound_one, bound_two);
659*e4b17023SJohn Marino 
660*e4b17023SJohn Marino     case clast_expr_bin:
661*e4b17023SJohn Marino       return type_for_clast_bin ((struct clast_binary *) e, ip,
662*e4b17023SJohn Marino 				 bound_one, bound_two);
663*e4b17023SJohn Marino 
664*e4b17023SJohn Marino     default:
665*e4b17023SJohn Marino       gcc_unreachable ();
666*e4b17023SJohn Marino     }
667*e4b17023SJohn Marino 
668*e4b17023SJohn Marino   return NULL_TREE;
669*e4b17023SJohn Marino }
670*e4b17023SJohn Marino 
671*e4b17023SJohn Marino /* Returns the type for the equation CLEQ.  */
672*e4b17023SJohn Marino 
673*e4b17023SJohn Marino static tree
type_for_clast_eq(struct clast_equation * cleq,ivs_params_p ip)674*e4b17023SJohn Marino type_for_clast_eq (struct clast_equation *cleq, ivs_params_p ip)
675*e4b17023SJohn Marino {
676*e4b17023SJohn Marino   mpz_t bound_one, bound_two;
677*e4b17023SJohn Marino   tree l, r;
678*e4b17023SJohn Marino 
679*e4b17023SJohn Marino   mpz_init (bound_one);
680*e4b17023SJohn Marino   mpz_init (bound_two);
681*e4b17023SJohn Marino 
682*e4b17023SJohn Marino   l = type_for_clast_expr (cleq->LHS, ip, bound_one, bound_two);
683*e4b17023SJohn Marino   r = type_for_clast_expr (cleq->RHS, ip, bound_one, bound_two);
684*e4b17023SJohn Marino 
685*e4b17023SJohn Marino   mpz_clear (bound_one);
686*e4b17023SJohn Marino   mpz_clear (bound_two);
687*e4b17023SJohn Marino   return max_precision_type (l, r);
688*e4b17023SJohn Marino }
689*e4b17023SJohn Marino 
690*e4b17023SJohn Marino /* Translates a clast equation CLEQ to a tree.  */
691*e4b17023SJohn Marino 
692*e4b17023SJohn Marino static tree
graphite_translate_clast_equation(struct clast_equation * cleq,ivs_params_p ip)693*e4b17023SJohn Marino graphite_translate_clast_equation (struct clast_equation *cleq,
694*e4b17023SJohn Marino 				   ivs_params_p ip)
695*e4b17023SJohn Marino {
696*e4b17023SJohn Marino   enum tree_code comp;
697*e4b17023SJohn Marino   tree type = type_for_clast_eq (cleq, ip);
698*e4b17023SJohn Marino   tree lhs = clast_to_gcc_expression (type, cleq->LHS, ip);
699*e4b17023SJohn Marino   tree rhs = clast_to_gcc_expression (type, cleq->RHS, ip);
700*e4b17023SJohn Marino 
701*e4b17023SJohn Marino   if (cleq->sign == 0)
702*e4b17023SJohn Marino     comp = EQ_EXPR;
703*e4b17023SJohn Marino 
704*e4b17023SJohn Marino   else if (cleq->sign > 0)
705*e4b17023SJohn Marino     comp = GE_EXPR;
706*e4b17023SJohn Marino 
707*e4b17023SJohn Marino   else
708*e4b17023SJohn Marino     comp = LE_EXPR;
709*e4b17023SJohn Marino 
710*e4b17023SJohn Marino   return fold_build2 (comp, boolean_type_node, lhs, rhs);
711*e4b17023SJohn Marino }
712*e4b17023SJohn Marino 
713*e4b17023SJohn Marino /* Creates the test for the condition in STMT.  */
714*e4b17023SJohn Marino 
715*e4b17023SJohn Marino static tree
graphite_create_guard_cond_expr(struct clast_guard * stmt,ivs_params_p ip)716*e4b17023SJohn Marino graphite_create_guard_cond_expr (struct clast_guard *stmt,
717*e4b17023SJohn Marino 				 ivs_params_p ip)
718*e4b17023SJohn Marino {
719*e4b17023SJohn Marino   tree cond = NULL;
720*e4b17023SJohn Marino   int i;
721*e4b17023SJohn Marino 
722*e4b17023SJohn Marino   for (i = 0; i < stmt->n; i++)
723*e4b17023SJohn Marino     {
724*e4b17023SJohn Marino       tree eq = graphite_translate_clast_equation (&stmt->eq[i], ip);
725*e4b17023SJohn Marino 
726*e4b17023SJohn Marino       if (cond)
727*e4b17023SJohn Marino 	cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
728*e4b17023SJohn Marino       else
729*e4b17023SJohn Marino 	cond = eq;
730*e4b17023SJohn Marino     }
731*e4b17023SJohn Marino 
732*e4b17023SJohn Marino   return cond;
733*e4b17023SJohn Marino }
734*e4b17023SJohn Marino 
735*e4b17023SJohn Marino /* Creates a new if region corresponding to Cloog's guard.  */
736*e4b17023SJohn Marino 
737*e4b17023SJohn Marino static edge
graphite_create_new_guard(edge entry_edge,struct clast_guard * stmt,ivs_params_p ip)738*e4b17023SJohn Marino graphite_create_new_guard (edge entry_edge, struct clast_guard *stmt,
739*e4b17023SJohn Marino 			   ivs_params_p ip)
740*e4b17023SJohn Marino {
741*e4b17023SJohn Marino   tree cond_expr = graphite_create_guard_cond_expr (stmt, ip);
742*e4b17023SJohn Marino   edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
743*e4b17023SJohn Marino   return exit_edge;
744*e4b17023SJohn Marino }
745*e4b17023SJohn Marino 
746*e4b17023SJohn Marino /* Compute the lower bound LOW and upper bound UP for the parameter
747*e4b17023SJohn Marino    PARAM in scop SCOP based on the constraints in the context.  */
748*e4b17023SJohn Marino 
749*e4b17023SJohn Marino static void
compute_bounds_for_param(scop_p scop,int param,mpz_t low,mpz_t up)750*e4b17023SJohn Marino compute_bounds_for_param (scop_p scop, int param, mpz_t low, mpz_t up)
751*e4b17023SJohn Marino {
752*e4b17023SJohn Marino   ppl_Linear_Expression_t le;
753*e4b17023SJohn Marino 
754*e4b17023SJohn Marino   /* Prepare the linear expression corresponding to the parameter that
755*e4b17023SJohn Marino      we want to maximize/minimize.  */
756*e4b17023SJohn Marino   ppl_new_Linear_Expression_with_dimension (&le, scop_nb_params (scop));
757*e4b17023SJohn Marino   ppl_set_coef (le, param, 1);
758*e4b17023SJohn Marino 
759*e4b17023SJohn Marino   ppl_max_for_le_pointset (SCOP_CONTEXT (scop), le, up);
760*e4b17023SJohn Marino   ppl_min_for_le_pointset (SCOP_CONTEXT (scop), le, low);
761*e4b17023SJohn Marino   ppl_delete_Linear_Expression (le);
762*e4b17023SJohn Marino }
763*e4b17023SJohn Marino 
764*e4b17023SJohn Marino /* Compute the lower bound LOW and upper bound UP for the induction
765*e4b17023SJohn Marino    variable at LEVEL for the statement PBB, based on the transformed
766*e4b17023SJohn Marino    scattering of PBB: T|I|G|Cst, with T the scattering transform, I
767*e4b17023SJohn Marino    the iteration domain, and G the context parameters.  */
768*e4b17023SJohn Marino 
769*e4b17023SJohn Marino static void
compute_bounds_for_level(poly_bb_p pbb,int level,mpz_t low,mpz_t up)770*e4b17023SJohn Marino compute_bounds_for_level (poly_bb_p pbb, int level, mpz_t low, mpz_t up)
771*e4b17023SJohn Marino {
772*e4b17023SJohn Marino   ppl_Pointset_Powerset_C_Polyhedron_t ps;
773*e4b17023SJohn Marino   ppl_Linear_Expression_t le;
774*e4b17023SJohn Marino 
775*e4b17023SJohn Marino   combine_context_id_scat (&ps, pbb, false);
776*e4b17023SJohn Marino 
777*e4b17023SJohn Marino   /* Prepare the linear expression corresponding to the level that we
778*e4b17023SJohn Marino      want to maximize/minimize.  */
779*e4b17023SJohn Marino   {
780*e4b17023SJohn Marino     ppl_dimension_type dim = pbb_nb_scattering_transform (pbb)
781*e4b17023SJohn Marino       + pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
782*e4b17023SJohn Marino 
783*e4b17023SJohn Marino     ppl_new_Linear_Expression_with_dimension (&le, dim);
784*e4b17023SJohn Marino     ppl_set_coef (le, psct_dynamic_dim (pbb, level), 1);
785*e4b17023SJohn Marino   }
786*e4b17023SJohn Marino 
787*e4b17023SJohn Marino   ppl_max_for_le_pointset (ps, le, up);
788*e4b17023SJohn Marino   ppl_min_for_le_pointset (ps, le, low);
789*e4b17023SJohn Marino   ppl_delete_Linear_Expression (le);
790*e4b17023SJohn Marino   ppl_delete_Pointset_Powerset_C_Polyhedron (ps);
791*e4b17023SJohn Marino }
792*e4b17023SJohn Marino 
793*e4b17023SJohn Marino /* Walks a CLAST and returns the first statement in the body of a
794*e4b17023SJohn Marino    loop.
795*e4b17023SJohn Marino 
796*e4b17023SJohn Marino    FIXME: This function should not be used to get a PBB in the STMT
797*e4b17023SJohn Marino    loop in order to find out the iteration domain of the loop: the
798*e4b17023SJohn Marino    counter example from Tobias is:
799*e4b17023SJohn Marino 
800*e4b17023SJohn Marino    | for (i = 0; i < 100; i++)
801*e4b17023SJohn Marino    |   {
802*e4b17023SJohn Marino    |     if (i == 0)
803*e4b17023SJohn Marino    |       S1;
804*e4b17023SJohn Marino    |     S2;
805*e4b17023SJohn Marino    |   }
806*e4b17023SJohn Marino 
807*e4b17023SJohn Marino    This function would return S1 whose iteration domain contains only
808*e4b17023SJohn Marino    one point "i = 0", whereas the iteration domain of S2 has 100 points.
809*e4b17023SJohn Marino 
810*e4b17023SJohn Marino    This should be implemented using some functionality existing in
811*e4b17023SJohn Marino    CLooG-ISL.  */
812*e4b17023SJohn Marino 
813*e4b17023SJohn Marino static struct clast_user_stmt *
clast_get_body_of_loop(struct clast_stmt * stmt)814*e4b17023SJohn Marino clast_get_body_of_loop (struct clast_stmt *stmt)
815*e4b17023SJohn Marino {
816*e4b17023SJohn Marino   if (!stmt
817*e4b17023SJohn Marino       || CLAST_STMT_IS_A (stmt, stmt_user))
818*e4b17023SJohn Marino     return (struct clast_user_stmt *) stmt;
819*e4b17023SJohn Marino 
820*e4b17023SJohn Marino   if (CLAST_STMT_IS_A (stmt, stmt_for))
821*e4b17023SJohn Marino     return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
822*e4b17023SJohn Marino 
823*e4b17023SJohn Marino   if (CLAST_STMT_IS_A (stmt, stmt_guard))
824*e4b17023SJohn Marino     return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
825*e4b17023SJohn Marino 
826*e4b17023SJohn Marino   if (CLAST_STMT_IS_A (stmt, stmt_block))
827*e4b17023SJohn Marino     return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
828*e4b17023SJohn Marino 
829*e4b17023SJohn Marino   if (CLAST_STMT_IS_A (stmt, stmt_ass))
830*e4b17023SJohn Marino     return clast_get_body_of_loop (stmt->next);
831*e4b17023SJohn Marino 
832*e4b17023SJohn Marino   gcc_unreachable ();
833*e4b17023SJohn Marino }
834*e4b17023SJohn Marino 
835*e4b17023SJohn Marino /* Returns the type for the induction variable for the loop translated
836*e4b17023SJohn Marino    from STMT_FOR.  */
837*e4b17023SJohn Marino 
838*e4b17023SJohn Marino static tree
type_for_clast_for(struct clast_for * stmt_for,ivs_params_p ip)839*e4b17023SJohn Marino type_for_clast_for (struct clast_for *stmt_for, ivs_params_p ip)
840*e4b17023SJohn Marino {
841*e4b17023SJohn Marino   mpz_t bound_one, bound_two;
842*e4b17023SJohn Marino   tree lb_type, ub_type;
843*e4b17023SJohn Marino 
844*e4b17023SJohn Marino   mpz_init (bound_one);
845*e4b17023SJohn Marino   mpz_init (bound_two);
846*e4b17023SJohn Marino 
847*e4b17023SJohn Marino   lb_type = type_for_clast_expr (stmt_for->LB, ip, bound_one, bound_two);
848*e4b17023SJohn Marino   ub_type = type_for_clast_expr (stmt_for->UB, ip, bound_one, bound_two);
849*e4b17023SJohn Marino 
850*e4b17023SJohn Marino   mpz_clear (bound_one);
851*e4b17023SJohn Marino   mpz_clear (bound_two);
852*e4b17023SJohn Marino 
853*e4b17023SJohn Marino   return max_precision_type (lb_type, ub_type);
854*e4b17023SJohn Marino }
855*e4b17023SJohn Marino 
856*e4b17023SJohn Marino /* Creates a new LOOP corresponding to Cloog's STMT.  Inserts an
857*e4b17023SJohn Marino    induction variable for the new LOOP.  New LOOP is attached to CFG
858*e4b17023SJohn Marino    starting at ENTRY_EDGE.  LOOP is inserted into the loop tree and
859*e4b17023SJohn Marino    becomes the child loop of the OUTER_LOOP.  NEWIVS_INDEX binds
860*e4b17023SJohn Marino    CLooG's scattering name to the induction variable created for the
861*e4b17023SJohn Marino    loop of STMT.  The new induction variable is inserted in the NEWIVS
862*e4b17023SJohn Marino    vector and is of type TYPE.  */
863*e4b17023SJohn Marino 
864*e4b17023SJohn Marino static struct loop *
graphite_create_new_loop(edge entry_edge,struct clast_for * stmt,loop_p outer,tree type,tree lb,tree ub,int level,ivs_params_p ip)865*e4b17023SJohn Marino graphite_create_new_loop (edge entry_edge, struct clast_for *stmt,
866*e4b17023SJohn Marino 			  loop_p outer, tree type, tree lb, tree ub,
867*e4b17023SJohn Marino 			  int level, ivs_params_p ip)
868*e4b17023SJohn Marino {
869*e4b17023SJohn Marino   mpz_t low, up;
870*e4b17023SJohn Marino 
871*e4b17023SJohn Marino   struct clast_user_stmt *body
872*e4b17023SJohn Marino     = clast_get_body_of_loop ((struct clast_stmt *) stmt);
873*e4b17023SJohn Marino   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (body->statement);
874*e4b17023SJohn Marino 
875*e4b17023SJohn Marino   tree stride = gmp_cst_to_tree (type, stmt->stride);
876*e4b17023SJohn Marino   tree ivvar = create_tmp_var (type, "graphite_IV");
877*e4b17023SJohn Marino   tree iv, iv_after_increment;
878*e4b17023SJohn Marino   loop_p loop = create_empty_loop_on_edge
879*e4b17023SJohn Marino     (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
880*e4b17023SJohn Marino      outer ? outer : entry_edge->src->loop_father);
881*e4b17023SJohn Marino 
882*e4b17023SJohn Marino   add_referenced_var (ivvar);
883*e4b17023SJohn Marino 
884*e4b17023SJohn Marino   mpz_init (low);
885*e4b17023SJohn Marino   mpz_init (up);
886*e4b17023SJohn Marino   compute_bounds_for_level (pbb, level, low, up);
887*e4b17023SJohn Marino   save_clast_name_index (ip->newivs_index, stmt->iterator,
888*e4b17023SJohn Marino 			 VEC_length (tree, *(ip->newivs)), level, low, up);
889*e4b17023SJohn Marino   mpz_clear (low);
890*e4b17023SJohn Marino   mpz_clear (up);
891*e4b17023SJohn Marino   VEC_safe_push (tree, heap, *(ip->newivs), iv);
892*e4b17023SJohn Marino   return loop;
893*e4b17023SJohn Marino }
894*e4b17023SJohn Marino 
895*e4b17023SJohn Marino /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
896*e4b17023SJohn Marino    induction variables of the loops around GBB in SESE.  */
897*e4b17023SJohn Marino 
898*e4b17023SJohn Marino static void
build_iv_mapping(VEC (tree,heap)* iv_map,struct clast_user_stmt * user_stmt,ivs_params_p ip)899*e4b17023SJohn Marino build_iv_mapping (VEC (tree, heap) *iv_map, struct clast_user_stmt *user_stmt,
900*e4b17023SJohn Marino 		  ivs_params_p ip)
901*e4b17023SJohn Marino {
902*e4b17023SJohn Marino   struct clast_stmt *t;
903*e4b17023SJohn Marino   int depth = 0;
904*e4b17023SJohn Marino   CloogStatement *cs = user_stmt->statement;
905*e4b17023SJohn Marino   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
906*e4b17023SJohn Marino   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
907*e4b17023SJohn Marino   mpz_t bound_one, bound_two;
908*e4b17023SJohn Marino 
909*e4b17023SJohn Marino   mpz_init (bound_one);
910*e4b17023SJohn Marino   mpz_init (bound_two);
911*e4b17023SJohn Marino 
912*e4b17023SJohn Marino   for (t = user_stmt->substitutions; t; t = t->next, depth++)
913*e4b17023SJohn Marino     {
914*e4b17023SJohn Marino       struct clast_expr *expr = (struct clast_expr *)
915*e4b17023SJohn Marino        ((struct clast_assignment *)t)->RHS;
916*e4b17023SJohn Marino       tree type = type_for_clast_expr (expr, ip, bound_one, bound_two);
917*e4b17023SJohn Marino       tree new_name = clast_to_gcc_expression (type, expr, ip);
918*e4b17023SJohn Marino       loop_p old_loop = gbb_loop_at_index (gbb, ip->region, depth);
919*e4b17023SJohn Marino 
920*e4b17023SJohn Marino       VEC_replace (tree, iv_map, old_loop->num, new_name);
921*e4b17023SJohn Marino     }
922*e4b17023SJohn Marino 
923*e4b17023SJohn Marino   mpz_clear (bound_one);
924*e4b17023SJohn Marino   mpz_clear (bound_two);
925*e4b17023SJohn Marino }
926*e4b17023SJohn Marino 
927*e4b17023SJohn Marino /* Construct bb_pbb_def with BB and PBB.  */
928*e4b17023SJohn Marino 
929*e4b17023SJohn Marino static bb_pbb_def *
new_bb_pbb_def(basic_block bb,poly_bb_p pbb)930*e4b17023SJohn Marino new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
931*e4b17023SJohn Marino {
932*e4b17023SJohn Marino   bb_pbb_def *bb_pbb_p;
933*e4b17023SJohn Marino 
934*e4b17023SJohn Marino   bb_pbb_p = XNEW (bb_pbb_def);
935*e4b17023SJohn Marino   bb_pbb_p->bb = bb;
936*e4b17023SJohn Marino   bb_pbb_p->pbb = pbb;
937*e4b17023SJohn Marino 
938*e4b17023SJohn Marino   return bb_pbb_p;
939*e4b17023SJohn Marino }
940*e4b17023SJohn Marino 
941*e4b17023SJohn Marino /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING.  */
942*e4b17023SJohn Marino 
943*e4b17023SJohn Marino static void
mark_bb_with_pbb(poly_bb_p pbb,basic_block bb,htab_t bb_pbb_mapping)944*e4b17023SJohn Marino mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
945*e4b17023SJohn Marino {
946*e4b17023SJohn Marino   bb_pbb_def tmp;
947*e4b17023SJohn Marino   PTR *x;
948*e4b17023SJohn Marino 
949*e4b17023SJohn Marino   tmp.bb = bb;
950*e4b17023SJohn Marino   x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
951*e4b17023SJohn Marino 
952*e4b17023SJohn Marino   if (x && !*x)
953*e4b17023SJohn Marino     *x = new_bb_pbb_def (bb, pbb);
954*e4b17023SJohn Marino }
955*e4b17023SJohn Marino 
956*e4b17023SJohn Marino /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING.  */
957*e4b17023SJohn Marino 
958*e4b17023SJohn Marino static poly_bb_p
find_pbb_via_hash(htab_t bb_pbb_mapping,basic_block bb)959*e4b17023SJohn Marino find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
960*e4b17023SJohn Marino {
961*e4b17023SJohn Marino   bb_pbb_def tmp;
962*e4b17023SJohn Marino   PTR *slot;
963*e4b17023SJohn Marino 
964*e4b17023SJohn Marino   tmp.bb = bb;
965*e4b17023SJohn Marino   slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
966*e4b17023SJohn Marino 
967*e4b17023SJohn Marino   if (slot && *slot)
968*e4b17023SJohn Marino     return ((bb_pbb_def *) *slot)->pbb;
969*e4b17023SJohn Marino 
970*e4b17023SJohn Marino   return NULL;
971*e4b17023SJohn Marino }
972*e4b17023SJohn Marino 
973*e4b17023SJohn Marino /* Check data dependency in LOOP at level LEVEL.
974*e4b17023SJohn Marino    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
975*e4b17023SJohn Marino    mapping.  */
976*e4b17023SJohn Marino 
977*e4b17023SJohn Marino static bool
dependency_in_loop_p(loop_p loop,htab_t bb_pbb_mapping,int level)978*e4b17023SJohn Marino dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
979*e4b17023SJohn Marino {
980*e4b17023SJohn Marino   unsigned i,j;
981*e4b17023SJohn Marino   basic_block *bbs = get_loop_body_in_dom_order (loop);
982*e4b17023SJohn Marino 
983*e4b17023SJohn Marino   for (i = 0; i < loop->num_nodes; i++)
984*e4b17023SJohn Marino     {
985*e4b17023SJohn Marino       poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
986*e4b17023SJohn Marino 
987*e4b17023SJohn Marino       if (pbb1 == NULL)
988*e4b17023SJohn Marino        continue;
989*e4b17023SJohn Marino 
990*e4b17023SJohn Marino       for (j = 0; j < loop->num_nodes; j++)
991*e4b17023SJohn Marino        {
992*e4b17023SJohn Marino 	 poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
993*e4b17023SJohn Marino 
994*e4b17023SJohn Marino 	 if (pbb2 == NULL)
995*e4b17023SJohn Marino 	   continue;
996*e4b17023SJohn Marino 
997*e4b17023SJohn Marino 	 if (dependency_between_pbbs_p (pbb1, pbb2, level))
998*e4b17023SJohn Marino 	   {
999*e4b17023SJohn Marino 	     free (bbs);
1000*e4b17023SJohn Marino 	     return true;
1001*e4b17023SJohn Marino 	   }
1002*e4b17023SJohn Marino        }
1003*e4b17023SJohn Marino     }
1004*e4b17023SJohn Marino 
1005*e4b17023SJohn Marino   free (bbs);
1006*e4b17023SJohn Marino 
1007*e4b17023SJohn Marino   return false;
1008*e4b17023SJohn Marino }
1009*e4b17023SJohn Marino 
1010*e4b17023SJohn Marino /* Translates a clast user statement STMT to gimple.
1011*e4b17023SJohn Marino 
1012*e4b17023SJohn Marino    - NEXT_E is the edge where new generated code should be attached.
1013*e4b17023SJohn Marino    - CONTEXT_LOOP is the loop in which the generated code will be placed
1014*e4b17023SJohn Marino    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1015*e4b17023SJohn Marino 
1016*e4b17023SJohn Marino static edge
translate_clast_user(struct clast_user_stmt * stmt,edge next_e,htab_t bb_pbb_mapping,ivs_params_p ip)1017*e4b17023SJohn Marino translate_clast_user (struct clast_user_stmt *stmt, edge next_e,
1018*e4b17023SJohn Marino 		      htab_t bb_pbb_mapping, ivs_params_p ip)
1019*e4b17023SJohn Marino {
1020*e4b17023SJohn Marino   int i, nb_loops;
1021*e4b17023SJohn Marino   basic_block new_bb;
1022*e4b17023SJohn Marino   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
1023*e4b17023SJohn Marino   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
1024*e4b17023SJohn Marino   VEC (tree, heap) *iv_map;
1025*e4b17023SJohn Marino 
1026*e4b17023SJohn Marino   if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
1027*e4b17023SJohn Marino     return next_e;
1028*e4b17023SJohn Marino 
1029*e4b17023SJohn Marino   nb_loops = number_of_loops ();
1030*e4b17023SJohn Marino   iv_map = VEC_alloc (tree, heap, nb_loops);
1031*e4b17023SJohn Marino   for (i = 0; i < nb_loops; i++)
1032*e4b17023SJohn Marino     VEC_quick_push (tree, iv_map, NULL_TREE);
1033*e4b17023SJohn Marino 
1034*e4b17023SJohn Marino   build_iv_mapping (iv_map, stmt, ip);
1035*e4b17023SJohn Marino   next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), ip->region,
1036*e4b17023SJohn Marino 					   next_e, iv_map, &gloog_error);
1037*e4b17023SJohn Marino   VEC_free (tree, heap, iv_map);
1038*e4b17023SJohn Marino 
1039*e4b17023SJohn Marino   new_bb = next_e->src;
1040*e4b17023SJohn Marino   mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
1041*e4b17023SJohn Marino   update_ssa (TODO_update_ssa);
1042*e4b17023SJohn Marino 
1043*e4b17023SJohn Marino   return next_e;
1044*e4b17023SJohn Marino }
1045*e4b17023SJohn Marino 
1046*e4b17023SJohn Marino /* Creates a new if region protecting the loop to be executed, if the execution
1047*e4b17023SJohn Marino    count is zero (lb > ub).  */
1048*e4b17023SJohn Marino 
1049*e4b17023SJohn Marino static edge
graphite_create_new_loop_guard(edge entry_edge,struct clast_for * stmt,tree * type,tree * lb,tree * ub,ivs_params_p ip)1050*e4b17023SJohn Marino graphite_create_new_loop_guard (edge entry_edge, struct clast_for *stmt,
1051*e4b17023SJohn Marino 				tree *type, tree *lb, tree *ub,
1052*e4b17023SJohn Marino 				ivs_params_p ip)
1053*e4b17023SJohn Marino {
1054*e4b17023SJohn Marino   tree cond_expr;
1055*e4b17023SJohn Marino   edge exit_edge;
1056*e4b17023SJohn Marino 
1057*e4b17023SJohn Marino   *type = type_for_clast_for (stmt, ip);
1058*e4b17023SJohn Marino   *lb = clast_to_gcc_expression (*type, stmt->LB, ip);
1059*e4b17023SJohn Marino   *ub = clast_to_gcc_expression (*type, stmt->UB, ip);
1060*e4b17023SJohn Marino 
1061*e4b17023SJohn Marino   /* When ub is simply a constant or a parameter, use lb <= ub.  */
1062*e4b17023SJohn Marino   if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME)
1063*e4b17023SJohn Marino     cond_expr = fold_build2 (LE_EXPR, boolean_type_node, *lb, *ub);
1064*e4b17023SJohn Marino   else
1065*e4b17023SJohn Marino     {
1066*e4b17023SJohn Marino       tree one = (POINTER_TYPE_P (*type)
1067*e4b17023SJohn Marino 		  ? convert_to_ptrofftype (integer_one_node)
1068*e4b17023SJohn Marino 		  : fold_convert (*type, integer_one_node));
1069*e4b17023SJohn Marino       /* Adding +1 and using LT_EXPR helps with loop latches that have a
1070*e4b17023SJohn Marino 	 loop iteration count of "PARAMETER - 1".  For PARAMETER == 0 this becomes
1071*e4b17023SJohn Marino 	 2^k-1 due to integer overflow, and the condition lb <= ub is true,
1072*e4b17023SJohn Marino 	 even if we do not want this.  However lb < ub + 1 is false, as
1073*e4b17023SJohn Marino 	 expected.  */
1074*e4b17023SJohn Marino       tree ub_one = fold_build2 (POINTER_TYPE_P (*type) ? POINTER_PLUS_EXPR
1075*e4b17023SJohn Marino 				 : PLUS_EXPR, *type, *ub, one);
1076*e4b17023SJohn Marino 
1077*e4b17023SJohn Marino       cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one);
1078*e4b17023SJohn Marino     }
1079*e4b17023SJohn Marino 
1080*e4b17023SJohn Marino   exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
1081*e4b17023SJohn Marino 
1082*e4b17023SJohn Marino   return exit_edge;
1083*e4b17023SJohn Marino }
1084*e4b17023SJohn Marino 
1085*e4b17023SJohn Marino static edge
1086*e4b17023SJohn Marino translate_clast (loop_p, struct clast_stmt *, edge, htab_t, int, ivs_params_p);
1087*e4b17023SJohn Marino 
1088*e4b17023SJohn Marino /* Create the loop for a clast for statement.
1089*e4b17023SJohn Marino 
1090*e4b17023SJohn Marino    - NEXT_E is the edge where new generated code should be attached.
1091*e4b17023SJohn Marino    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1092*e4b17023SJohn Marino 
1093*e4b17023SJohn Marino static edge
translate_clast_for_loop(loop_p context_loop,struct clast_for * stmt,edge next_e,htab_t bb_pbb_mapping,int level,tree type,tree lb,tree ub,ivs_params_p ip)1094*e4b17023SJohn Marino translate_clast_for_loop (loop_p context_loop, struct clast_for *stmt,
1095*e4b17023SJohn Marino 			  edge next_e, htab_t bb_pbb_mapping, int level,
1096*e4b17023SJohn Marino 			  tree type, tree lb, tree ub, ivs_params_p ip)
1097*e4b17023SJohn Marino {
1098*e4b17023SJohn Marino   struct loop *loop = graphite_create_new_loop (next_e, stmt, context_loop,
1099*e4b17023SJohn Marino 						type, lb, ub, level, ip);
1100*e4b17023SJohn Marino   edge last_e = single_exit (loop);
1101*e4b17023SJohn Marino   edge to_body = single_succ_edge (loop->header);
1102*e4b17023SJohn Marino   basic_block after = to_body->dest;
1103*e4b17023SJohn Marino 
1104*e4b17023SJohn Marino   /* Create a basic block for loop close phi nodes.  */
1105*e4b17023SJohn Marino   last_e = single_succ_edge (split_edge (last_e));
1106*e4b17023SJohn Marino 
1107*e4b17023SJohn Marino   /* Translate the body of the loop.  */
1108*e4b17023SJohn Marino   next_e = translate_clast (loop, stmt->body, to_body, bb_pbb_mapping,
1109*e4b17023SJohn Marino 			    level + 1, ip);
1110*e4b17023SJohn Marino   redirect_edge_succ_nodup (next_e, after);
1111*e4b17023SJohn Marino   set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1112*e4b17023SJohn Marino 
1113*e4b17023SJohn Marino   if (flag_loop_parallelize_all
1114*e4b17023SJohn Marino       && !dependency_in_loop_p (loop, bb_pbb_mapping, level))
1115*e4b17023SJohn Marino     loop->can_be_parallel = true;
1116*e4b17023SJohn Marino 
1117*e4b17023SJohn Marino   return last_e;
1118*e4b17023SJohn Marino }
1119*e4b17023SJohn Marino 
1120*e4b17023SJohn Marino /* Translates a clast for statement STMT to gimple.  First a guard is created
1121*e4b17023SJohn Marino    protecting the loop, if it is executed zero times.  In this guard we create
1122*e4b17023SJohn Marino    the real loop structure.
1123*e4b17023SJohn Marino 
1124*e4b17023SJohn Marino    - NEXT_E is the edge where new generated code should be attached.
1125*e4b17023SJohn Marino    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1126*e4b17023SJohn Marino 
1127*e4b17023SJohn Marino static edge
translate_clast_for(loop_p context_loop,struct clast_for * stmt,edge next_e,htab_t bb_pbb_mapping,int level,ivs_params_p ip)1128*e4b17023SJohn Marino translate_clast_for (loop_p context_loop, struct clast_for *stmt, edge next_e,
1129*e4b17023SJohn Marino 		     htab_t bb_pbb_mapping, int level, ivs_params_p ip)
1130*e4b17023SJohn Marino {
1131*e4b17023SJohn Marino   tree type, lb, ub;
1132*e4b17023SJohn Marino   edge last_e = graphite_create_new_loop_guard (next_e, stmt, &type,
1133*e4b17023SJohn Marino 						&lb, &ub, ip);
1134*e4b17023SJohn Marino   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1135*e4b17023SJohn Marino 
1136*e4b17023SJohn Marino   translate_clast_for_loop (context_loop, stmt, true_e, bb_pbb_mapping, level,
1137*e4b17023SJohn Marino 			    type, lb, ub, ip);
1138*e4b17023SJohn Marino   return last_e;
1139*e4b17023SJohn Marino }
1140*e4b17023SJohn Marino 
1141*e4b17023SJohn Marino /* Translates a clast assignment STMT to gimple.
1142*e4b17023SJohn Marino 
1143*e4b17023SJohn Marino    - NEXT_E is the edge where new generated code should be attached.
1144*e4b17023SJohn Marino    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1145*e4b17023SJohn Marino 
1146*e4b17023SJohn Marino static edge
translate_clast_assignment(struct clast_assignment * stmt,edge next_e,int level,ivs_params_p ip)1147*e4b17023SJohn Marino translate_clast_assignment (struct clast_assignment *stmt, edge next_e,
1148*e4b17023SJohn Marino 			    int level, ivs_params_p ip)
1149*e4b17023SJohn Marino {
1150*e4b17023SJohn Marino   gimple_seq stmts;
1151*e4b17023SJohn Marino   mpz_t bound_one, bound_two;
1152*e4b17023SJohn Marino   tree type, new_name, var;
1153*e4b17023SJohn Marino   edge res = single_succ_edge (split_edge (next_e));
1154*e4b17023SJohn Marino   struct clast_expr *expr = (struct clast_expr *) stmt->RHS;
1155*e4b17023SJohn Marino 
1156*e4b17023SJohn Marino   mpz_init (bound_one);
1157*e4b17023SJohn Marino   mpz_init (bound_two);
1158*e4b17023SJohn Marino   type = type_for_clast_expr (expr, ip, bound_one, bound_two);
1159*e4b17023SJohn Marino   var = create_tmp_var (type, "graphite_var");
1160*e4b17023SJohn Marino   new_name = force_gimple_operand (clast_to_gcc_expression (type, expr, ip),
1161*e4b17023SJohn Marino 				   &stmts, true, var);
1162*e4b17023SJohn Marino   add_referenced_var (var);
1163*e4b17023SJohn Marino   if (stmts)
1164*e4b17023SJohn Marino     {
1165*e4b17023SJohn Marino       gsi_insert_seq_on_edge (next_e, stmts);
1166*e4b17023SJohn Marino       gsi_commit_edge_inserts ();
1167*e4b17023SJohn Marino     }
1168*e4b17023SJohn Marino 
1169*e4b17023SJohn Marino   save_clast_name_index (ip->newivs_index, stmt->LHS,
1170*e4b17023SJohn Marino 			 VEC_length (tree, *(ip->newivs)), level,
1171*e4b17023SJohn Marino 			 bound_one, bound_two);
1172*e4b17023SJohn Marino   VEC_safe_push (tree, heap, *(ip->newivs), new_name);
1173*e4b17023SJohn Marino 
1174*e4b17023SJohn Marino   mpz_clear (bound_one);
1175*e4b17023SJohn Marino   mpz_clear (bound_two);
1176*e4b17023SJohn Marino 
1177*e4b17023SJohn Marino   return res;
1178*e4b17023SJohn Marino }
1179*e4b17023SJohn Marino 
1180*e4b17023SJohn Marino /* Translates a clast guard statement STMT to gimple.
1181*e4b17023SJohn Marino 
1182*e4b17023SJohn Marino    - NEXT_E is the edge where new generated code should be attached.
1183*e4b17023SJohn Marino    - CONTEXT_LOOP is the loop in which the generated code will be placed
1184*e4b17023SJohn Marino    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1185*e4b17023SJohn Marino 
1186*e4b17023SJohn Marino static edge
translate_clast_guard(loop_p context_loop,struct clast_guard * stmt,edge next_e,htab_t bb_pbb_mapping,int level,ivs_params_p ip)1187*e4b17023SJohn Marino translate_clast_guard (loop_p context_loop, struct clast_guard *stmt,
1188*e4b17023SJohn Marino 		       edge next_e, htab_t bb_pbb_mapping, int level,
1189*e4b17023SJohn Marino 		       ivs_params_p ip)
1190*e4b17023SJohn Marino {
1191*e4b17023SJohn Marino   edge last_e = graphite_create_new_guard (next_e, stmt, ip);
1192*e4b17023SJohn Marino   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1193*e4b17023SJohn Marino 
1194*e4b17023SJohn Marino   translate_clast (context_loop, stmt->then, true_e, bb_pbb_mapping, level, ip);
1195*e4b17023SJohn Marino   return last_e;
1196*e4b17023SJohn Marino }
1197*e4b17023SJohn Marino 
1198*e4b17023SJohn Marino /* Translates a CLAST statement STMT to GCC representation in the
1199*e4b17023SJohn Marino    context of a SESE.
1200*e4b17023SJohn Marino 
1201*e4b17023SJohn Marino    - NEXT_E is the edge where new generated code should be attached.
1202*e4b17023SJohn Marino    - CONTEXT_LOOP is the loop in which the generated code will be placed
1203*e4b17023SJohn Marino    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1204*e4b17023SJohn Marino 
1205*e4b17023SJohn Marino static edge
translate_clast(loop_p context_loop,struct clast_stmt * stmt,edge next_e,htab_t bb_pbb_mapping,int level,ivs_params_p ip)1206*e4b17023SJohn Marino translate_clast (loop_p context_loop, struct clast_stmt *stmt, edge next_e,
1207*e4b17023SJohn Marino 		 htab_t bb_pbb_mapping, int level, ivs_params_p ip)
1208*e4b17023SJohn Marino {
1209*e4b17023SJohn Marino   if (!stmt)
1210*e4b17023SJohn Marino     return next_e;
1211*e4b17023SJohn Marino 
1212*e4b17023SJohn Marino   if (CLAST_STMT_IS_A (stmt, stmt_root))
1213*e4b17023SJohn Marino     ; /* Do nothing.  */
1214*e4b17023SJohn Marino 
1215*e4b17023SJohn Marino   else if (CLAST_STMT_IS_A (stmt, stmt_user))
1216*e4b17023SJohn Marino     next_e = translate_clast_user ((struct clast_user_stmt *) stmt,
1217*e4b17023SJohn Marino 				   next_e, bb_pbb_mapping, ip);
1218*e4b17023SJohn Marino 
1219*e4b17023SJohn Marino   else if (CLAST_STMT_IS_A (stmt, stmt_for))
1220*e4b17023SJohn Marino     next_e = translate_clast_for (context_loop, (struct clast_for *) stmt,
1221*e4b17023SJohn Marino 				  next_e, bb_pbb_mapping, level, ip);
1222*e4b17023SJohn Marino 
1223*e4b17023SJohn Marino   else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1224*e4b17023SJohn Marino     next_e = translate_clast_guard (context_loop, (struct clast_guard *) stmt,
1225*e4b17023SJohn Marino 				    next_e, bb_pbb_mapping, level, ip);
1226*e4b17023SJohn Marino 
1227*e4b17023SJohn Marino   else if (CLAST_STMT_IS_A (stmt, stmt_block))
1228*e4b17023SJohn Marino     next_e = translate_clast (context_loop, ((struct clast_block *) stmt)->body,
1229*e4b17023SJohn Marino 			      next_e, bb_pbb_mapping, level, ip);
1230*e4b17023SJohn Marino 
1231*e4b17023SJohn Marino   else if (CLAST_STMT_IS_A (stmt, stmt_ass))
1232*e4b17023SJohn Marino     next_e = translate_clast_assignment ((struct clast_assignment *) stmt,
1233*e4b17023SJohn Marino 					 next_e, level, ip);
1234*e4b17023SJohn Marino   else
1235*e4b17023SJohn Marino     gcc_unreachable();
1236*e4b17023SJohn Marino 
1237*e4b17023SJohn Marino   recompute_all_dominators ();
1238*e4b17023SJohn Marino   graphite_verify ();
1239*e4b17023SJohn Marino 
1240*e4b17023SJohn Marino   return translate_clast (context_loop, stmt->next, next_e, bb_pbb_mapping,
1241*e4b17023SJohn Marino 			  level, ip);
1242*e4b17023SJohn Marino }
1243*e4b17023SJohn Marino 
1244*e4b17023SJohn Marino /* Free the SCATTERING domain list.  */
1245*e4b17023SJohn Marino 
1246*e4b17023SJohn Marino static void
free_scattering(CloogScatteringList * scattering)1247*e4b17023SJohn Marino free_scattering (CloogScatteringList *scattering)
1248*e4b17023SJohn Marino {
1249*e4b17023SJohn Marino   while (scattering)
1250*e4b17023SJohn Marino     {
1251*e4b17023SJohn Marino       CloogScattering *dom = cloog_scattering (scattering);
1252*e4b17023SJohn Marino       CloogScatteringList *next = cloog_next_scattering (scattering);
1253*e4b17023SJohn Marino 
1254*e4b17023SJohn Marino       cloog_scattering_free (dom);
1255*e4b17023SJohn Marino       free (scattering);
1256*e4b17023SJohn Marino       scattering = next;
1257*e4b17023SJohn Marino     }
1258*e4b17023SJohn Marino }
1259*e4b17023SJohn Marino 
1260*e4b17023SJohn Marino /* Initialize Cloog's parameter names from the names used in GIMPLE.
1261*e4b17023SJohn Marino    Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1262*e4b17023SJohn Marino    from 0 to scop_nb_loops (scop).  */
1263*e4b17023SJohn Marino 
1264*e4b17023SJohn Marino static void
initialize_cloog_names(scop_p scop,CloogProgram * prog)1265*e4b17023SJohn Marino initialize_cloog_names (scop_p scop, CloogProgram *prog)
1266*e4b17023SJohn Marino {
1267*e4b17023SJohn Marino   sese region = SCOP_REGION (scop);
1268*e4b17023SJohn Marino   int i;
1269*e4b17023SJohn Marino   int nb_iterators = scop_max_loop_depth (scop);
1270*e4b17023SJohn Marino   int nb_scattering = cloog_program_nb_scattdims (prog);
1271*e4b17023SJohn Marino   int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1272*e4b17023SJohn Marino   char **iterators = XNEWVEC (char *, nb_iterators * 2);
1273*e4b17023SJohn Marino   char **scattering = XNEWVEC (char *, nb_scattering);
1274*e4b17023SJohn Marino   char **parameters= XNEWVEC (char *, nb_parameters);
1275*e4b17023SJohn Marino 
1276*e4b17023SJohn Marino   cloog_program_set_names (prog, cloog_names_malloc ());
1277*e4b17023SJohn Marino 
1278*e4b17023SJohn Marino   for (i = 0; i < nb_parameters; i++)
1279*e4b17023SJohn Marino     {
1280*e4b17023SJohn Marino       tree param = VEC_index (tree, SESE_PARAMS (region), i);
1281*e4b17023SJohn Marino       const char *name = get_name (param);
1282*e4b17023SJohn Marino       int len;
1283*e4b17023SJohn Marino 
1284*e4b17023SJohn Marino       if (!name)
1285*e4b17023SJohn Marino 	name = "T";
1286*e4b17023SJohn Marino 
1287*e4b17023SJohn Marino       len = strlen (name);
1288*e4b17023SJohn Marino       len += 17;
1289*e4b17023SJohn Marino       parameters[i] = XNEWVEC (char, len + 1);
1290*e4b17023SJohn Marino       snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1291*e4b17023SJohn Marino     }
1292*e4b17023SJohn Marino 
1293*e4b17023SJohn Marino   cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1294*e4b17023SJohn Marino   cloog_names_set_parameters (cloog_program_names (prog), parameters);
1295*e4b17023SJohn Marino 
1296*e4b17023SJohn Marino   for (i = 0; i < nb_iterators; i++)
1297*e4b17023SJohn Marino     {
1298*e4b17023SJohn Marino       int len = 4 + 16;
1299*e4b17023SJohn Marino       iterators[i] = XNEWVEC (char, len);
1300*e4b17023SJohn Marino       snprintf (iterators[i], len, "git_%d", i);
1301*e4b17023SJohn Marino     }
1302*e4b17023SJohn Marino 
1303*e4b17023SJohn Marino   cloog_names_set_nb_iterators (cloog_program_names (prog),
1304*e4b17023SJohn Marino 				nb_iterators);
1305*e4b17023SJohn Marino   cloog_names_set_iterators (cloog_program_names (prog),
1306*e4b17023SJohn Marino 			     iterators);
1307*e4b17023SJohn Marino 
1308*e4b17023SJohn Marino   for (i = 0; i < nb_scattering; i++)
1309*e4b17023SJohn Marino     {
1310*e4b17023SJohn Marino       int len = 5 + 16;
1311*e4b17023SJohn Marino       scattering[i] = XNEWVEC (char, len);
1312*e4b17023SJohn Marino       snprintf (scattering[i], len, "scat_%d", i);
1313*e4b17023SJohn Marino     }
1314*e4b17023SJohn Marino 
1315*e4b17023SJohn Marino   cloog_names_set_nb_scattering (cloog_program_names (prog),
1316*e4b17023SJohn Marino 				 nb_scattering);
1317*e4b17023SJohn Marino   cloog_names_set_scattering (cloog_program_names (prog),
1318*e4b17023SJohn Marino 			      scattering);
1319*e4b17023SJohn Marino }
1320*e4b17023SJohn Marino 
1321*e4b17023SJohn Marino /* Initialize a CLooG input file.  */
1322*e4b17023SJohn Marino 
1323*e4b17023SJohn Marino static FILE *
init_cloog_input_file(int scop_number)1324*e4b17023SJohn Marino init_cloog_input_file (int scop_number)
1325*e4b17023SJohn Marino {
1326*e4b17023SJohn Marino   FILE *graphite_out_file;
1327*e4b17023SJohn Marino   int len = strlen (dump_base_name);
1328*e4b17023SJohn Marino   char *dumpname = XNEWVEC (char, len + 25);
1329*e4b17023SJohn Marino   char *s_scop_number = XNEWVEC (char, 15);
1330*e4b17023SJohn Marino 
1331*e4b17023SJohn Marino   memcpy (dumpname, dump_base_name, len + 1);
1332*e4b17023SJohn Marino   strip_off_ending (dumpname, len);
1333*e4b17023SJohn Marino   sprintf (s_scop_number, ".%d", scop_number);
1334*e4b17023SJohn Marino   strcat (dumpname, s_scop_number);
1335*e4b17023SJohn Marino   strcat (dumpname, ".cloog");
1336*e4b17023SJohn Marino   graphite_out_file = fopen (dumpname, "w+b");
1337*e4b17023SJohn Marino 
1338*e4b17023SJohn Marino   if (graphite_out_file == 0)
1339*e4b17023SJohn Marino     fatal_error ("can%'t open %s for writing: %m", dumpname);
1340*e4b17023SJohn Marino 
1341*e4b17023SJohn Marino   free (dumpname);
1342*e4b17023SJohn Marino 
1343*e4b17023SJohn Marino   return graphite_out_file;
1344*e4b17023SJohn Marino }
1345*e4b17023SJohn Marino 
1346*e4b17023SJohn Marino /* Build cloog program for SCoP.  */
1347*e4b17023SJohn Marino 
1348*e4b17023SJohn Marino static void
build_cloog_prog(scop_p scop,CloogProgram * prog,CloogOptions * options)1349*e4b17023SJohn Marino build_cloog_prog (scop_p scop, CloogProgram *prog,
1350*e4b17023SJohn Marino                   CloogOptions *options)
1351*e4b17023SJohn Marino {
1352*e4b17023SJohn Marino   int i;
1353*e4b17023SJohn Marino   int max_nb_loops = scop_max_loop_depth (scop);
1354*e4b17023SJohn Marino   poly_bb_p pbb;
1355*e4b17023SJohn Marino   CloogLoop *loop_list = NULL;
1356*e4b17023SJohn Marino   CloogBlockList *block_list = NULL;
1357*e4b17023SJohn Marino   CloogScatteringList *scattering = NULL;
1358*e4b17023SJohn Marino   int nbs = 2 * max_nb_loops + 1;
1359*e4b17023SJohn Marino   int *scaldims;
1360*e4b17023SJohn Marino 
1361*e4b17023SJohn Marino   cloog_program_set_context
1362*e4b17023SJohn Marino     (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop),
1363*e4b17023SJohn Marino       scop_nb_params (scop), cloog_state));
1364*e4b17023SJohn Marino   nbs = unify_scattering_dimensions (scop);
1365*e4b17023SJohn Marino   scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1366*e4b17023SJohn Marino   cloog_program_set_nb_scattdims (prog, nbs);
1367*e4b17023SJohn Marino   initialize_cloog_names (scop, prog);
1368*e4b17023SJohn Marino 
1369*e4b17023SJohn Marino   FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
1370*e4b17023SJohn Marino     {
1371*e4b17023SJohn Marino       CloogStatement *stmt;
1372*e4b17023SJohn Marino       CloogBlock *block;
1373*e4b17023SJohn Marino       CloogDomain *dom;
1374*e4b17023SJohn Marino 
1375*e4b17023SJohn Marino       /* Dead code elimination: when the domain of a PBB is empty,
1376*e4b17023SJohn Marino 	 don't generate code for the PBB.  */
1377*e4b17023SJohn Marino       if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1378*e4b17023SJohn Marino 	continue;
1379*e4b17023SJohn Marino 
1380*e4b17023SJohn Marino       /* Build the new statement and its block.  */
1381*e4b17023SJohn Marino       stmt = cloog_statement_alloc (cloog_state, pbb_index (pbb));
1382*e4b17023SJohn Marino       dom = new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb),
1383*e4b17023SJohn Marino                                                          scop_nb_params (scop),
1384*e4b17023SJohn Marino                                                          cloog_state);
1385*e4b17023SJohn Marino       block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1386*e4b17023SJohn Marino       cloog_statement_set_usr (stmt, pbb);
1387*e4b17023SJohn Marino 
1388*e4b17023SJohn Marino       /* Build loop list.  */
1389*e4b17023SJohn Marino       {
1390*e4b17023SJohn Marino         CloogLoop *new_loop_list = cloog_loop_malloc (cloog_state);
1391*e4b17023SJohn Marino         cloog_loop_set_next (new_loop_list, loop_list);
1392*e4b17023SJohn Marino         cloog_loop_set_domain (new_loop_list, dom);
1393*e4b17023SJohn Marino         cloog_loop_set_block (new_loop_list, block);
1394*e4b17023SJohn Marino         loop_list = new_loop_list;
1395*e4b17023SJohn Marino       }
1396*e4b17023SJohn Marino 
1397*e4b17023SJohn Marino       /* Build block list.  */
1398*e4b17023SJohn Marino       {
1399*e4b17023SJohn Marino         CloogBlockList *new_block_list = cloog_block_list_malloc ();
1400*e4b17023SJohn Marino 
1401*e4b17023SJohn Marino         cloog_block_list_set_next (new_block_list, block_list);
1402*e4b17023SJohn Marino         cloog_block_list_set_block (new_block_list, block);
1403*e4b17023SJohn Marino         block_list = new_block_list;
1404*e4b17023SJohn Marino       }
1405*e4b17023SJohn Marino 
1406*e4b17023SJohn Marino       /* Build scattering list.  */
1407*e4b17023SJohn Marino       {
1408*e4b17023SJohn Marino         /* XXX: Replace with cloog_domain_list_alloc(), when available.  */
1409*e4b17023SJohn Marino         CloogScatteringList *new_scattering
1410*e4b17023SJohn Marino 	  = (CloogScatteringList *) xmalloc (sizeof (CloogScatteringList));
1411*e4b17023SJohn Marino         ppl_Polyhedron_t scat;
1412*e4b17023SJohn Marino 	CloogScattering *dom;
1413*e4b17023SJohn Marino 
1414*e4b17023SJohn Marino 	scat = PBB_TRANSFORMED_SCATTERING (pbb);
1415*e4b17023SJohn Marino         dom = new_Cloog_Scattering_from_ppl_Polyhedron
1416*e4b17023SJohn Marino           (scat, scop_nb_params (scop), pbb_nb_scattering_transform (pbb),
1417*e4b17023SJohn Marino            cloog_state);
1418*e4b17023SJohn Marino 
1419*e4b17023SJohn Marino         cloog_set_next_scattering (new_scattering, scattering);
1420*e4b17023SJohn Marino         cloog_set_scattering (new_scattering, dom);
1421*e4b17023SJohn Marino         scattering = new_scattering;
1422*e4b17023SJohn Marino       }
1423*e4b17023SJohn Marino     }
1424*e4b17023SJohn Marino 
1425*e4b17023SJohn Marino   cloog_program_set_loop (prog, loop_list);
1426*e4b17023SJohn Marino   cloog_program_set_blocklist (prog, block_list);
1427*e4b17023SJohn Marino 
1428*e4b17023SJohn Marino   for (i = 0; i < nbs; i++)
1429*e4b17023SJohn Marino     scaldims[i] = 0 ;
1430*e4b17023SJohn Marino 
1431*e4b17023SJohn Marino   cloog_program_set_scaldims (prog, scaldims);
1432*e4b17023SJohn Marino 
1433*e4b17023SJohn Marino   /* Extract scalar dimensions to simplify the code generation problem.  */
1434*e4b17023SJohn Marino   cloog_program_extract_scalars (prog, scattering, options);
1435*e4b17023SJohn Marino 
1436*e4b17023SJohn Marino   /* Dump a .cloog input file, if requested.  This feature is only
1437*e4b17023SJohn Marino      enabled in the Graphite branch.  */
1438*e4b17023SJohn Marino   if (0)
1439*e4b17023SJohn Marino     {
1440*e4b17023SJohn Marino       static size_t file_scop_number = 0;
1441*e4b17023SJohn Marino       FILE *cloog_file = init_cloog_input_file (file_scop_number);
1442*e4b17023SJohn Marino 
1443*e4b17023SJohn Marino       cloog_program_dump_cloog (cloog_file, prog, scattering);
1444*e4b17023SJohn Marino       ++file_scop_number;
1445*e4b17023SJohn Marino     }
1446*e4b17023SJohn Marino 
1447*e4b17023SJohn Marino   /* Apply scattering.  */
1448*e4b17023SJohn Marino   cloog_program_scatter (prog, scattering, options);
1449*e4b17023SJohn Marino   free_scattering (scattering);
1450*e4b17023SJohn Marino 
1451*e4b17023SJohn Marino   /* Iterators corresponding to scalar dimensions have to be extracted.  */
1452*e4b17023SJohn Marino   cloog_names_scalarize (cloog_program_names (prog), nbs,
1453*e4b17023SJohn Marino 			 cloog_program_scaldims (prog));
1454*e4b17023SJohn Marino 
1455*e4b17023SJohn Marino   /* Free blocklist.  */
1456*e4b17023SJohn Marino   {
1457*e4b17023SJohn Marino     CloogBlockList *next = cloog_program_blocklist (prog);
1458*e4b17023SJohn Marino 
1459*e4b17023SJohn Marino     while (next)
1460*e4b17023SJohn Marino       {
1461*e4b17023SJohn Marino         CloogBlockList *toDelete = next;
1462*e4b17023SJohn Marino         next = cloog_block_list_next (next);
1463*e4b17023SJohn Marino         cloog_block_list_set_next (toDelete, NULL);
1464*e4b17023SJohn Marino         cloog_block_list_set_block (toDelete, NULL);
1465*e4b17023SJohn Marino         cloog_block_list_free (toDelete);
1466*e4b17023SJohn Marino       }
1467*e4b17023SJohn Marino     cloog_program_set_blocklist (prog, NULL);
1468*e4b17023SJohn Marino   }
1469*e4b17023SJohn Marino }
1470*e4b17023SJohn Marino 
1471*e4b17023SJohn Marino /* Return the options that will be used in GLOOG.  */
1472*e4b17023SJohn Marino 
1473*e4b17023SJohn Marino static CloogOptions *
set_cloog_options(void)1474*e4b17023SJohn Marino set_cloog_options (void)
1475*e4b17023SJohn Marino {
1476*e4b17023SJohn Marino   CloogOptions *options = cloog_options_malloc (cloog_state);
1477*e4b17023SJohn Marino 
1478*e4b17023SJohn Marino   /* Change cloog output language to C.  If we do use FORTRAN instead, cloog
1479*e4b17023SJohn Marino      will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1480*e4b17023SJohn Marino      we pass an incomplete program to cloog.  */
1481*e4b17023SJohn Marino   options->language = CLOOG_LANGUAGE_C;
1482*e4b17023SJohn Marino 
1483*e4b17023SJohn Marino   /* Enable complex equality spreading: removes dummy statements
1484*e4b17023SJohn Marino      (assignments) in the generated code which repeats the
1485*e4b17023SJohn Marino      substitution equations for statements.  This is useless for
1486*e4b17023SJohn Marino      GLooG.  */
1487*e4b17023SJohn Marino   options->esp = 1;
1488*e4b17023SJohn Marino 
1489*e4b17023SJohn Marino #ifdef CLOOG_ORG
1490*e4b17023SJohn Marino   /* Silence CLooG to avoid failing tests due to debug output to stderr.  */
1491*e4b17023SJohn Marino   options->quiet = 1;
1492*e4b17023SJohn Marino #else
1493*e4b17023SJohn Marino   /* Enable C pretty-printing mode: normalizes the substitution
1494*e4b17023SJohn Marino      equations for statements.  */
1495*e4b17023SJohn Marino   options->cpp = 1;
1496*e4b17023SJohn Marino #endif
1497*e4b17023SJohn Marino 
1498*e4b17023SJohn Marino   /* Allow cloog to build strides with a stride width different to one.
1499*e4b17023SJohn Marino      This example has stride = 4:
1500*e4b17023SJohn Marino 
1501*e4b17023SJohn Marino      for (i = 0; i < 20; i += 4)
1502*e4b17023SJohn Marino        A  */
1503*e4b17023SJohn Marino   options->strides = 1;
1504*e4b17023SJohn Marino 
1505*e4b17023SJohn Marino   /* Disable optimizations and make cloog generate source code closer to the
1506*e4b17023SJohn Marino      input.  This is useful for debugging,  but later we want the optimized
1507*e4b17023SJohn Marino      code.
1508*e4b17023SJohn Marino 
1509*e4b17023SJohn Marino      XXX: We can not disable optimizations, as loop blocking is not working
1510*e4b17023SJohn Marino      without them.  */
1511*e4b17023SJohn Marino   if (0)
1512*e4b17023SJohn Marino     {
1513*e4b17023SJohn Marino       options->f = -1;
1514*e4b17023SJohn Marino       options->l = INT_MAX;
1515*e4b17023SJohn Marino     }
1516*e4b17023SJohn Marino 
1517*e4b17023SJohn Marino   return options;
1518*e4b17023SJohn Marino }
1519*e4b17023SJohn Marino 
1520*e4b17023SJohn Marino /* Prints STMT to STDERR.  */
1521*e4b17023SJohn Marino 
1522*e4b17023SJohn Marino void
print_clast_stmt(FILE * file,struct clast_stmt * stmt)1523*e4b17023SJohn Marino print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1524*e4b17023SJohn Marino {
1525*e4b17023SJohn Marino   CloogOptions *options = set_cloog_options ();
1526*e4b17023SJohn Marino 
1527*e4b17023SJohn Marino   clast_pprint (file, stmt, 0, options);
1528*e4b17023SJohn Marino   cloog_options_free (options);
1529*e4b17023SJohn Marino }
1530*e4b17023SJohn Marino 
1531*e4b17023SJohn Marino /* Prints STMT to STDERR.  */
1532*e4b17023SJohn Marino 
1533*e4b17023SJohn Marino DEBUG_FUNCTION void
debug_clast_stmt(struct clast_stmt * stmt)1534*e4b17023SJohn Marino debug_clast_stmt (struct clast_stmt *stmt)
1535*e4b17023SJohn Marino {
1536*e4b17023SJohn Marino   print_clast_stmt (stderr, stmt);
1537*e4b17023SJohn Marino }
1538*e4b17023SJohn Marino 
1539*e4b17023SJohn Marino /* Translate SCOP to a CLooG program and clast.  These two
1540*e4b17023SJohn Marino    representations should be freed together: a clast cannot be used
1541*e4b17023SJohn Marino    without a program.  */
1542*e4b17023SJohn Marino 
1543*e4b17023SJohn Marino cloog_prog_clast
scop_to_clast(scop_p scop)1544*e4b17023SJohn Marino scop_to_clast (scop_p scop)
1545*e4b17023SJohn Marino {
1546*e4b17023SJohn Marino   CloogOptions *options = set_cloog_options ();
1547*e4b17023SJohn Marino   cloog_prog_clast pc;
1548*e4b17023SJohn Marino 
1549*e4b17023SJohn Marino   /* Connect new cloog prog generation to graphite.  */
1550*e4b17023SJohn Marino   pc.prog = cloog_program_malloc ();
1551*e4b17023SJohn Marino   build_cloog_prog (scop, pc.prog, options);
1552*e4b17023SJohn Marino   pc.prog = cloog_program_generate (pc.prog, options);
1553*e4b17023SJohn Marino   pc.stmt = cloog_clast_create (pc.prog, options);
1554*e4b17023SJohn Marino 
1555*e4b17023SJohn Marino   cloog_options_free (options);
1556*e4b17023SJohn Marino   return pc;
1557*e4b17023SJohn Marino }
1558*e4b17023SJohn Marino 
1559*e4b17023SJohn Marino /* Prints to FILE the code generated by CLooG for SCOP.  */
1560*e4b17023SJohn Marino 
1561*e4b17023SJohn Marino void
print_generated_program(FILE * file,scop_p scop)1562*e4b17023SJohn Marino print_generated_program (FILE *file, scop_p scop)
1563*e4b17023SJohn Marino {
1564*e4b17023SJohn Marino   CloogOptions *options = set_cloog_options ();
1565*e4b17023SJohn Marino 
1566*e4b17023SJohn Marino   cloog_prog_clast pc = scop_to_clast (scop);
1567*e4b17023SJohn Marino 
1568*e4b17023SJohn Marino   fprintf (file, "       (prog: \n");
1569*e4b17023SJohn Marino   cloog_program_print (file, pc.prog);
1570*e4b17023SJohn Marino   fprintf (file, "       )\n");
1571*e4b17023SJohn Marino 
1572*e4b17023SJohn Marino   fprintf (file, "       (clast: \n");
1573*e4b17023SJohn Marino   clast_pprint (file, pc.stmt, 0, options);
1574*e4b17023SJohn Marino   fprintf (file, "       )\n");
1575*e4b17023SJohn Marino 
1576*e4b17023SJohn Marino   cloog_options_free (options);
1577*e4b17023SJohn Marino   cloog_clast_free (pc.stmt);
1578*e4b17023SJohn Marino   cloog_program_free (pc.prog);
1579*e4b17023SJohn Marino }
1580*e4b17023SJohn Marino 
1581*e4b17023SJohn Marino /* Prints to STDERR the code generated by CLooG for SCOP.  */
1582*e4b17023SJohn Marino 
1583*e4b17023SJohn Marino DEBUG_FUNCTION void
debug_generated_program(scop_p scop)1584*e4b17023SJohn Marino debug_generated_program (scop_p scop)
1585*e4b17023SJohn Marino {
1586*e4b17023SJohn Marino   print_generated_program (stderr, scop);
1587*e4b17023SJohn Marino }
1588*e4b17023SJohn Marino 
1589*e4b17023SJohn Marino /* Add CLooG names to parameter index.  The index is used to translate
1590*e4b17023SJohn Marino    back from CLooG names to GCC trees.  */
1591*e4b17023SJohn Marino 
1592*e4b17023SJohn Marino static void
create_params_index(scop_p scop,htab_t index_table,CloogProgram * prog)1593*e4b17023SJohn Marino create_params_index (scop_p scop, htab_t index_table, CloogProgram *prog) {
1594*e4b17023SJohn Marino   CloogNames* names = cloog_program_names (prog);
1595*e4b17023SJohn Marino   int nb_parameters = cloog_names_nb_parameters (names);
1596*e4b17023SJohn Marino   char **parameters = cloog_names_parameters (names);
1597*e4b17023SJohn Marino   int i;
1598*e4b17023SJohn Marino   mpz_t bound_one, bound_two;
1599*e4b17023SJohn Marino 
1600*e4b17023SJohn Marino   mpz_init (bound_one);
1601*e4b17023SJohn Marino   mpz_init (bound_two);
1602*e4b17023SJohn Marino 
1603*e4b17023SJohn Marino   for (i = 0; i < nb_parameters; i++)
1604*e4b17023SJohn Marino     {
1605*e4b17023SJohn Marino       compute_bounds_for_param (scop, i, bound_one, bound_two);
1606*e4b17023SJohn Marino       save_clast_name_index (index_table, parameters[i], i, i,
1607*e4b17023SJohn Marino 			     bound_one, bound_two);
1608*e4b17023SJohn Marino     }
1609*e4b17023SJohn Marino 
1610*e4b17023SJohn Marino   mpz_clear (bound_one);
1611*e4b17023SJohn Marino   mpz_clear (bound_two);
1612*e4b17023SJohn Marino }
1613*e4b17023SJohn Marino 
1614*e4b17023SJohn Marino /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1615*e4b17023SJohn Marino    the given SCOP.  Return true if code generation succeeded.
1616*e4b17023SJohn Marino    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1617*e4b17023SJohn Marino */
1618*e4b17023SJohn Marino 
1619*e4b17023SJohn Marino bool
gloog(scop_p scop,htab_t bb_pbb_mapping)1620*e4b17023SJohn Marino gloog (scop_p scop, htab_t bb_pbb_mapping)
1621*e4b17023SJohn Marino {
1622*e4b17023SJohn Marino   VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1623*e4b17023SJohn Marino   loop_p context_loop;
1624*e4b17023SJohn Marino   sese region = SCOP_REGION (scop);
1625*e4b17023SJohn Marino   ifsese if_region = NULL;
1626*e4b17023SJohn Marino   htab_t newivs_index, params_index;
1627*e4b17023SJohn Marino   cloog_prog_clast pc;
1628*e4b17023SJohn Marino   struct ivs_params ip;
1629*e4b17023SJohn Marino 
1630*e4b17023SJohn Marino   timevar_push (TV_GRAPHITE_CODE_GEN);
1631*e4b17023SJohn Marino   gloog_error = false;
1632*e4b17023SJohn Marino 
1633*e4b17023SJohn Marino   pc = scop_to_clast (scop);
1634*e4b17023SJohn Marino 
1635*e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
1636*e4b17023SJohn Marino     {
1637*e4b17023SJohn Marino       fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1638*e4b17023SJohn Marino       print_clast_stmt (dump_file, pc.stmt);
1639*e4b17023SJohn Marino       fprintf (dump_file, "\n");
1640*e4b17023SJohn Marino     }
1641*e4b17023SJohn Marino 
1642*e4b17023SJohn Marino   recompute_all_dominators ();
1643*e4b17023SJohn Marino   graphite_verify ();
1644*e4b17023SJohn Marino 
1645*e4b17023SJohn Marino   if_region = move_sese_in_condition (region);
1646*e4b17023SJohn Marino   sese_insert_phis_for_liveouts (region,
1647*e4b17023SJohn Marino 				 if_region->region->exit->src,
1648*e4b17023SJohn Marino 				 if_region->false_region->exit,
1649*e4b17023SJohn Marino 				 if_region->true_region->exit);
1650*e4b17023SJohn Marino   recompute_all_dominators ();
1651*e4b17023SJohn Marino   graphite_verify ();
1652*e4b17023SJohn Marino 
1653*e4b17023SJohn Marino   context_loop = SESE_ENTRY (region)->src->loop_father;
1654*e4b17023SJohn Marino   newivs_index = htab_create (10, clast_name_index_elt_info,
1655*e4b17023SJohn Marino 			      eq_clast_name_indexes, free_clast_name_index);
1656*e4b17023SJohn Marino   params_index = htab_create (10, clast_name_index_elt_info,
1657*e4b17023SJohn Marino 			      eq_clast_name_indexes, free_clast_name_index);
1658*e4b17023SJohn Marino 
1659*e4b17023SJohn Marino   create_params_index (scop, params_index, pc.prog);
1660*e4b17023SJohn Marino 
1661*e4b17023SJohn Marino   ip.newivs = &newivs;
1662*e4b17023SJohn Marino   ip.newivs_index = newivs_index;
1663*e4b17023SJohn Marino   ip.params = SESE_PARAMS (region);
1664*e4b17023SJohn Marino   ip.params_index = params_index;
1665*e4b17023SJohn Marino   ip.region = region;
1666*e4b17023SJohn Marino 
1667*e4b17023SJohn Marino   translate_clast (context_loop, pc.stmt, if_region->true_region->entry,
1668*e4b17023SJohn Marino 		   bb_pbb_mapping, 0, &ip);
1669*e4b17023SJohn Marino   graphite_verify ();
1670*e4b17023SJohn Marino   scev_reset ();
1671*e4b17023SJohn Marino   recompute_all_dominators ();
1672*e4b17023SJohn Marino   graphite_verify ();
1673*e4b17023SJohn Marino 
1674*e4b17023SJohn Marino   if (gloog_error)
1675*e4b17023SJohn Marino     set_ifsese_condition (if_region, integer_zero_node);
1676*e4b17023SJohn Marino 
1677*e4b17023SJohn Marino   free (if_region->true_region);
1678*e4b17023SJohn Marino   free (if_region->region);
1679*e4b17023SJohn Marino   free (if_region);
1680*e4b17023SJohn Marino 
1681*e4b17023SJohn Marino   htab_delete (newivs_index);
1682*e4b17023SJohn Marino   htab_delete (params_index);
1683*e4b17023SJohn Marino   VEC_free (tree, heap, newivs);
1684*e4b17023SJohn Marino   cloog_clast_free (pc.stmt);
1685*e4b17023SJohn Marino   cloog_program_free (pc.prog);
1686*e4b17023SJohn Marino   timevar_pop (TV_GRAPHITE_CODE_GEN);
1687*e4b17023SJohn Marino 
1688*e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_DETAILS))
1689*e4b17023SJohn Marino     {
1690*e4b17023SJohn Marino       loop_p loop;
1691*e4b17023SJohn Marino       loop_iterator li;
1692*e4b17023SJohn Marino       int num_no_dependency = 0;
1693*e4b17023SJohn Marino 
1694*e4b17023SJohn Marino       FOR_EACH_LOOP (li, loop, 0)
1695*e4b17023SJohn Marino 	if (loop->can_be_parallel)
1696*e4b17023SJohn Marino 	  num_no_dependency++;
1697*e4b17023SJohn Marino 
1698*e4b17023SJohn Marino       fprintf (dump_file, "\n%d loops carried no dependency.\n",
1699*e4b17023SJohn Marino 	       num_no_dependency);
1700*e4b17023SJohn Marino     }
1701*e4b17023SJohn Marino 
1702*e4b17023SJohn Marino   return !gloog_error;
1703*e4b17023SJohn Marino }
1704*e4b17023SJohn Marino #endif
1705