da: only add parameters up to deepest filter
[ppn.git] / convert.cc
blobb60dde24fceec86f21233381170816ddaf27b7ce
1 #include <algorithm>
2 #include <vector>
3 #include <isl/constraint.h>
4 #include <omega.h>
6 #include "convert.h"
8 using namespace omega;
10 typedef std::vector<Variable_ID> varvector;
12 static void max_index(Constraint_Handle c, varvector& vv, varvector& params)
14 for (Constr_Vars_Iter cvi(c); cvi; ++cvi) {
15 Variable_ID var = (*cvi).var;
16 if (find(vv.begin(), vv.end(), var) == vv.end() &&
17 find(params.begin(), params.end(), var) == params.end())
18 vv.push_back(var);
22 static void set_constraint(isl_constraint *c, Constraint_Handle h,
23 varvector &vv, varvector &params)
25 isl_int v;
27 isl_int_init(v);
29 for (int i = 0; i < vv.size(); ++i) {
30 isl_int_set_si(v, h.get_coef(vv[i]));
31 isl_constraint_set_coefficient(c, isl_dim_set, i, v);
33 for (int i = 0; i < params.size(); ++i) {
34 isl_int_set_si(v, h.get_coef(params[i]));
35 isl_constraint_set_coefficient(c, isl_dim_param, i, v);
37 isl_int_set_si(v, h.get_const());
38 isl_constraint_set_constant(c, v);
40 isl_int_clear(v);
43 static isl_basic_set *disjunct2basic_set(DNF_Iterator &di, isl_space *dim,
44 varvector &vv, varvector &params)
46 int d = vv.size();
47 isl_constraint *c;
48 isl_basic_set *bset;
49 isl_local_space *ls;
51 for (EQ_Iterator ei = (*di)->EQs(); ei; ++ei)
52 max_index((*ei), vv, params);
53 for (GEQ_Iterator gi = (*di)->GEQs(); gi; ++gi)
54 max_index((*gi), vv, params);
56 dim = isl_space_add_dims(dim, isl_dim_set, vv.size() - d);
57 bset = isl_basic_set_universe(isl_space_copy(dim));
58 ls = isl_local_space_from_space(dim);
60 for (EQ_Iterator ei = (*di)->EQs(); ei; ++ei) {
61 c = isl_equality_alloc(isl_local_space_copy(ls));
62 set_constraint(c, (*ei), vv, params);
63 bset = isl_basic_set_add_constraint(bset, c);
65 for (GEQ_Iterator gi = (*di)->GEQs(); gi; ++gi) {
66 c = isl_inequality_alloc(isl_local_space_copy(ls));
67 set_constraint(c, (*gi), vv, params);
68 bset = isl_basic_set_add_constraint(bset, c);
71 isl_local_space_free(ls);
73 bset = isl_basic_set_project_out(bset, isl_dim_set, d, vv.size() - d);
74 vv.resize(d);
76 return bset;
79 __isl_give isl_map *relation2map(isl_ctx *ctx, Relation &r)
81 varvector vv;
82 varvector params;
83 isl_space *dim;
84 isl_space *dim_set;
85 isl_map *map;
87 for (int j = 1; j <= r.n_inp(); ++j)
88 vv.push_back(r.input_var(j));
89 for (int j = 1; j <= r.n_out(); ++j)
90 vv.push_back(r.output_var(j));
92 const Variable_ID_Tuple *globals = r.global_decls();
93 dim = isl_space_alloc(ctx, globals->size(), r.n_inp(), r.n_out());
94 for (int i = 0; i < globals->size(); ++i) {
95 Variable_ID v = r.get_local((*globals)[i+1]);
96 params.push_back(v);
97 dim = isl_space_set_dim_name(dim, isl_dim_param, i, v->base_name);
100 dim_set = isl_space_set_alloc(ctx, params.size(), vv.size());
101 map = isl_map_empty(isl_space_copy(dim));
103 for (DNF_Iterator di(r.query_DNF()); di; ++di) {
104 isl_basic_set *bset;
105 isl_basic_map *bmap;
107 bset = disjunct2basic_set(di, isl_space_copy(dim_set), vv, params);
108 bmap = isl_basic_map_from_basic_set(bset, isl_space_copy(dim));
109 map = isl_map_union(map, isl_map_from_basic_map(bmap));
112 isl_space_free(dim_set);
113 isl_space_free(dim);
115 return map;
118 __isl_give isl_set *relation2set(isl_ctx *ctx, Relation &r)
120 varvector vv;
121 varvector params;
122 isl_space *dim;
123 isl_set *set;
125 for (int j = 1; j <= r.n_set(); ++j)
126 vv.push_back(r.set_var(j));
128 const Variable_ID_Tuple *globals = r.global_decls();
129 dim = isl_space_set_alloc(ctx, globals->size(), r.n_set());
130 for (int i = 0; i < globals->size(); ++i) {
131 Variable_ID v = r.get_local((*globals)[i+1]);
132 params.push_back(v);
133 dim = isl_space_set_dim_name(dim, isl_dim_param, i, v->base_name);
136 set = isl_set_empty(isl_space_copy(dim));
138 for (DNF_Iterator di(r.query_DNF()); di; ++di) {
139 isl_basic_set *bset;
141 bset = disjunct2basic_set(di, isl_space_copy(dim), vv, params);
142 set = isl_set_union(set, isl_set_from_basic_set(bset));
145 isl_space_free(dim);
147 return set;