isl_basic_map_eliminate_vars: avoid NULL pointer dereference
[platform/upstream/isl.git] / isl_pw_templ.c
1 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
2 #define FN(TYPE,NAME) xFN(TYPE,NAME)
3 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
4 #define S(TYPE,NAME) xS(TYPE,NAME)
5
6 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim, int n)
7 {
8         struct PW *pw;
9
10         if (!dim)
11                 return NULL;
12         isl_assert(dim->ctx, n >= 0, goto error);
13         pw = isl_alloc(dim->ctx, struct PW,
14                         sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
15         if (!pw)
16                 goto error;
17
18         pw->ref = 1;
19         pw->size = n;
20         pw->n = 0;
21         pw->dim = dim;
22         return pw;
23 error:
24         isl_dim_free(dim);
25         return NULL;
26 }
27
28 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim)
29 {
30         return FN(PW,alloc_)(dim, 0);
31 }
32
33 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
34         __isl_take isl_set *set, __isl_take EL *el)
35 {
36         if (!pw || !set || !el)
37                 goto error;
38
39         if (isl_set_fast_is_empty(set) || FN(EL,IS_ZERO)(el)) {
40                 isl_set_free(set);
41                 FN(EL,free)(el);
42                 return pw;
43         }
44
45         isl_assert(set->ctx, isl_dim_equal(pw->dim, el->dim), goto error);
46         isl_assert(set->ctx, pw->n < pw->size, goto error);
47
48         pw->p[pw->n].set = set;
49         pw->p[pw->n].FIELD = el;
50         pw->n++;
51         
52         return pw;
53 error:
54         FN(PW,free)(pw);
55         isl_set_free(set);
56         FN(EL,free)(el);
57         return NULL;
58 }
59
60 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
61 {
62         PW *pw;
63
64         if (!set || !el)
65                 goto error;
66
67         pw = FN(PW,alloc_)(isl_set_get_dim(set), 1);
68
69         return FN(PW,add_piece)(pw, set, el);
70 error:
71         isl_set_free(set);
72         FN(EL,free)(el);
73         return NULL;
74 }
75
76 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
77 {
78         int i;
79         PW *dup;
80
81         if (!pw)
82                 return NULL;
83
84         dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->n);
85         if (!dup)
86                 return NULL;
87
88         for (i = 0; i < pw->n; ++i)
89                 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
90                                             FN(EL,copy)(pw->p[i].FIELD));
91
92         return dup;
93 error:
94         FN(PW,free)(dup);
95         return NULL;
96 }
97
98 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
99 {
100         if (!pw)
101                 return NULL;
102
103         if (pw->ref == 1)
104                 return pw;
105         pw->ref--;
106         return FN(PW,dup)(pw);
107 }
108
109 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
110 {
111         if (!pw)
112                 return NULL;
113
114         pw->ref++;
115         return pw;
116 }
117
118 void FN(PW,free)(__isl_take PW *pw)
119 {
120         int i;
121
122         if (!pw)
123                 return;
124         if (--pw->ref > 0)
125                 return;
126
127         for (i = 0; i < pw->n; ++i) {
128                 isl_set_free(pw->p[i].set);
129                 FN(EL,free)(pw->p[i].FIELD);
130         }
131         isl_dim_free(pw->dim);
132         free(pw);
133 }
134
135 int FN(PW,is_zero)(__isl_keep PW *pw)
136 {
137         if (!pw)
138                 return -1;
139
140         return pw->n == 0;
141 }
142
143 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
144 {
145         int i, j, n;
146         struct PW *res;
147         isl_set *set;
148
149         if (!pw1 || !pw2)
150                 goto error;
151
152         isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
153
154         if (FN(PW,is_zero)(pw1)) {
155                 FN(PW,free)(pw1);
156                 return pw2;
157         }
158
159         if (FN(PW,is_zero)(pw2)) {
160                 FN(PW,free)(pw2);
161                 return pw1;
162         }
163
164         n = (pw1->n + 1) * (pw2->n + 1);
165         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
166
167         for (i = 0; i < pw1->n; ++i) {
168                 set = isl_set_copy(pw1->p[i].set);
169                 for (j = 0; j < pw2->n; ++j) {
170                         struct isl_set *common;
171                         EL *sum;
172                         set = isl_set_subtract(set,
173                                         isl_set_copy(pw2->p[j].set));
174                         common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
175                                                 isl_set_copy(pw2->p[j].set));
176                         if (isl_set_fast_is_empty(common)) {
177                                 isl_set_free(common);
178                                 continue;
179                         }
180
181                         sum = ADD(common, FN(EL,copy)(pw1->p[i].FIELD),
182                                           FN(EL,copy)(pw2->p[j].FIELD));
183
184                         res = FN(PW,add_piece)(res, common, sum);
185                 }
186                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
187         }
188
189         for (j = 0; j < pw2->n; ++j) {
190                 set = isl_set_copy(pw2->p[j].set);
191                 for (i = 0; i < pw1->n; ++i)
192                         set = isl_set_subtract(set,
193                                         isl_set_copy(pw1->p[i].set));
194                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
195         }
196
197         FN(PW,free)(pw1);
198         FN(PW,free)(pw2);
199
200         return res;
201 error:
202         FN(PW,free)(pw1);
203         FN(PW,free)(pw2);
204         return NULL;
205 }
206
207 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
208 {
209         int i;
210         PW *res;
211
212         if (!pw1 || !pw2)
213                 goto error;
214
215         isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
216
217         if (FN(PW,is_zero)(pw1)) {
218                 FN(PW,free)(pw1);
219                 return pw2;
220         }
221
222         if (FN(PW,is_zero)(pw2)) {
223                 FN(PW,free)(pw2);
224                 return pw1;
225         }
226
227         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
228
229         for (i = 0; i < pw1->n; ++i)
230                 res = FN(PW,add_piece)(res,
231                                 isl_set_copy(pw1->p[i].set),
232                                 FN(EL,copy)(pw1->p[i].FIELD));
233
234         for (i = 0; i < pw2->n; ++i)
235                 res = FN(PW,add_piece)(res,
236                                 isl_set_copy(pw2->p[i].set),
237                                 FN(EL,copy)(pw2->p[i].FIELD));
238
239         FN(PW,free)(pw1);
240         FN(PW,free)(pw2);
241
242         return res;
243 error:
244         FN(PW,free)(pw1);
245         FN(PW,free)(pw2);
246         return NULL;
247 }
248
249 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
250         __isl_take isl_point *pnt)
251 {
252         int i;
253         int found = 0;
254         isl_qpolynomial *qp;
255
256         if (!pw || !pnt)
257                 goto error;
258         isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, pw->dim), goto error);
259
260         for (i = 0; i < pw->n; ++i) {
261                 found = isl_set_contains_point(pw->p[i].set, pnt);
262                 if (found < 0)
263                         goto error;
264                 if (found)
265                         break;
266         }
267         if (found)
268                 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
269                                             isl_point_copy(pnt));
270         else
271                 qp = isl_qpolynomial_zero(isl_dim_copy(pw->dim));
272         FN(PW,free)(pw);
273         isl_point_free(pnt);
274         return qp;
275 error:
276         FN(PW,free)(pw);
277         isl_point_free(pnt);
278         return NULL;
279 }
280
281 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
282 {
283         int i;
284         isl_set *dom;
285
286         if (!pw)
287                 return NULL;
288
289         dom = isl_set_empty(isl_dim_copy(pw->dim));
290         for (i = 0; i < pw->n; ++i)
291                 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
292
293         FN(PW,free)(pw);
294
295         return dom;
296 }
297
298 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *set)
299 {
300         int i;
301
302         if (!pw || !set)
303                 goto error;
304
305         if (pw->n == 0) {
306                 isl_set_free(set);
307                 return pw;
308         }
309
310         pw = FN(PW,cow)(pw);
311         if (!pw)
312                 goto error;
313
314         for (i = 0; i < pw->n; ++i) {
315                 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
316                 if (!pw->p[i].set)
317                         goto error;
318         }
319         
320         isl_set_free(set);
321         return pw;
322 error:
323         isl_set_free(set);
324         FN(PW,free)(pw);
325         return NULL;
326 }
327
328 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
329 {
330         int i;
331         isl_basic_set *hull = NULL;
332
333         if (!pw || !context)
334                 goto error;
335
336         if (pw->n == 0) {
337                 isl_set_free(context);
338                 return pw;
339         }
340
341         hull = isl_set_simple_hull(isl_set_copy(context));
342
343         pw = FN(PW,cow)(pw);
344         if (!pw)
345                 goto error;
346
347         for (i = 0; i < pw->n; ++i) {
348                 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
349                                                 isl_basic_set_copy(hull));
350                 if (!pw->p[i].set)
351                         goto error;
352         }
353
354         isl_basic_set_free(hull);
355         isl_set_free(context);
356
357         return pw;
358 error:
359         FN(PW,free)(pw);
360         isl_basic_set_free(hull);
361         isl_set_free(context);
362         return NULL;
363 }
364
365 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
366 {
367         int i, j;
368
369         if (!pw)
370                 return NULL;
371         if (pw->n == 0)
372                 return pw;
373
374         for (i = pw->n - 1; i >= 0; --i) {
375                 for (j = i - 1; j >= 0; --j) {
376                         if (!FN(EL,is_equal)(pw->p[i].FIELD, pw->p[j].FIELD))
377                                 continue;
378                         pw->p[j].set = isl_set_union(pw->p[j].set,
379                                                         pw->p[i].set);
380                         FN(EL,free)(pw->p[i].FIELD);
381                         if (i != pw->n - 1)
382                                 pw->p[i] = pw->p[pw->n - 1];
383                         pw->n--;
384                         break;
385                 }
386                 if (j >= 0)
387                         continue;
388                 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
389                 if (!pw->p[i].set)
390                         goto error;
391         }
392
393         return pw;
394 error:
395         FN(PW,free)(pw);
396         return NULL;
397 }
398
399 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
400 {
401         return pw ? pw->dim->ctx : NULL;
402 }
403
404 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
405         unsigned first, unsigned n)
406 {
407         int i;
408
409         if (!pw)
410                 return -1;
411         if (pw->n == 0 || n == 0)
412                 return 0;
413         for (i = 0; i < pw->n; ++i) {
414                 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
415                                                         type, first, n);
416                 if (involves < 0 || involves)
417                         return involves;
418         }
419         return 0;
420 }
421
422 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
423         enum isl_dim_type type, unsigned first, unsigned n)
424 {
425         int i;
426
427         if (!pw)
428                 return NULL;
429         if (n == 0)
430                 return pw;
431
432         pw = FN(PW,cow)(pw);
433         if (!pw)
434                 return NULL;
435         pw->dim = isl_dim_drop(pw->dim, type, first, n);
436         if (!pw->dim)
437                 goto error;
438         for (i = 0; i < pw->n; ++i) {
439                 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
440                 if (!pw->p[i].set)
441                         goto error;
442                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
443                 if (!pw->p[i].FIELD)
444                         goto error;
445         }
446
447         return pw;
448 error:
449         FN(PW,free)(pw);
450         return NULL;
451 }
452
453 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
454         enum isl_dim_type type, unsigned pos, isl_int v)
455 {
456         int i;
457
458         if (!pw)
459                 return NULL;
460
461         pw = FN(PW,cow)(pw);
462         if (!pw)
463                 return NULL;
464         for (i = 0; i < pw->n; ++i) {
465                 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
466                 if (!pw->p[i].set)
467                         goto error;
468         }
469
470         return pw;
471 error:
472         FN(PW,free)(pw);
473         return NULL;
474 }
475
476 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
477 {
478         return pw ? isl_dim_size(pw->dim, type) : 0;
479 }
480
481 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
482         enum isl_dim_type type, unsigned first, unsigned n)
483 {
484         int i;
485
486         if (!pw)
487                 return NULL;
488         if (n == 0)
489                 return pw;
490
491         pw = FN(PW,cow)(pw);
492         if (!pw)
493                 return NULL;
494         if (!pw->dim)
495                 goto error;
496         for (i = 0; i < pw->n; ++i) {
497                 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
498                 if (!pw->p[i].set)
499                         goto error;
500         }
501
502         return pw;
503 error:
504         FN(PW,free)(pw);
505         return NULL;
506 }
507
508 /* Compute the maximal value attained by the piecewise quasipolynomial
509  * on its domain or zero if the domain is empty.
510  * In the worst case, the domain is scanned completely,
511  * so the domain is assumed to be bounded.
512  */
513 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
514 {
515         int i;
516         isl_qpolynomial *opt;
517
518         if (!pw)
519                 return NULL;
520
521         if (pw->n == 0) {
522                 isl_dim *dim = isl_dim_copy(pw->dim);
523                 FN(PW,free)(pw);
524                 return isl_qpolynomial_zero(dim);
525         }
526
527         opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
528                                         isl_set_copy(pw->p[0].set), max);
529         for (i = 1; i < pw->n; ++i) {
530                 isl_qpolynomial *opt_i;
531                 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
532                                                 isl_set_copy(pw->p[i].set), max);
533                 if (max)
534                         opt = isl_qpolynomial_max_cst(opt, opt_i);
535                 else
536                         opt = isl_qpolynomial_min_cst(opt, opt_i);
537         }
538
539         FN(PW,free)(pw);
540         return opt;
541 }
542
543 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
544 {
545         return FN(PW,opt)(pw, 1);
546 }
547
548 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
549 {
550         return FN(PW,opt)(pw, 0);
551 }
552
553 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
554 {
555         return pw ? isl_dim_copy(pw->dim) : NULL;
556 }
557
558 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
559 {
560         int i;
561
562         if (!pw || !morph)
563                 goto error;
564
565         isl_assert(pw->dim->ctx, isl_dim_equal(pw->dim, morph->dom->dim),
566                 goto error);
567
568         pw = FN(PW,cow)(pw);
569         if (!pw)
570                 goto error;
571         isl_dim_free(pw->dim);
572         pw->dim = isl_dim_copy(morph->ran->dim);
573         if (!pw->dim)
574                 goto error;
575
576         for (i = 0; i < pw->n; ++i) {
577                 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
578                 if (!pw->p[i].set)
579                         goto error;
580                 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
581                                                 isl_morph_copy(morph));
582                 if (!pw->p[i].FIELD)
583                         goto error;
584         }
585
586         isl_morph_free(morph);
587
588         return pw;
589 error:
590         FN(PW,free)(pw);
591         isl_morph_free(morph);
592         return NULL;
593 }
594
595 int FN(PW,foreach_piece)(__isl_keep PW *pw,
596         int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
597         void *user)
598 {
599         int i;
600
601         if (!pw)
602                 return -1;
603
604         for (i = 0; i < pw->n; ++i)
605                 if (fn(isl_set_copy(pw->p[i].set),
606                                 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
607                         return -1;
608
609         return 0;
610 }
611
612 static int any_divs(__isl_keep isl_set *set)
613 {
614         int i;
615
616         if (!set)
617                 return -1;
618
619         for (i = 0; i < set->n; ++i)
620                 if (set->p[i]->n_div > 0)
621                         return 1;
622
623         return 0;
624 }
625
626 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
627         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
628                     void *user), void *user)
629 {
630         int i;
631
632         if (!set || !el)
633                 goto error;
634
635         for (i = 0; i < set->n; ++i) {
636                 isl_set *lift;
637                 EL *copy;
638
639                 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
640                 lift = isl_set_lift(lift);
641
642                 copy = FN(EL,copy)(el);
643                 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
644
645                 if (fn(lift, copy, user) < 0)
646                         goto error;
647         }
648
649         isl_set_free(set);
650         FN(EL,free)(el);
651
652         return 0;
653 error:
654         isl_set_free(set);
655         FN(EL,free)(el);
656         return -1;
657 }
658
659 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
660         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
661                     void *user), void *user)
662 {
663         int i;
664
665         if (!pw)
666                 return -1;
667
668         for (i = 0; i < pw->n; ++i) {
669                 isl_set *set;
670                 EL *el;
671
672                 set = isl_set_copy(pw->p[i].set);
673                 el = FN(EL,copy)(pw->p[i].FIELD);
674                 if (!any_divs(set)) {
675                         if (fn(set, el, user) < 0)
676                                 return -1;
677                         continue;
678                 }
679                 if (foreach_lifted_subset(set, el, fn, user) < 0)
680                         return -1;
681         }
682
683         return 0;
684 }
685
686 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
687         enum isl_dim_type dst_type, unsigned dst_pos,
688         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
689 {
690         int i;
691
692         pw = FN(PW,cow)(pw);
693         if (!pw)
694                 return NULL;
695
696         pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
697         if (!pw->dim)
698                 goto error;
699
700         for (i = 0; i < pw->n; ++i) {
701                 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
702                                                 dst_type, dst_pos,
703                                                 src_type, src_pos, n);
704                 if (!pw->p[i].set)
705                         goto error;
706                 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
707                                         dst_type, dst_pos, src_type, src_pos, n);
708                 if (!pw->p[i].FIELD)
709                         goto error;
710         }
711
712         return pw;
713 error:
714         FN(PW,free)(pw);
715         return NULL;
716 }