-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathisl_ast_build_expr.c
3092 lines (2752 loc) · 88.8 KB
/
isl_ast_build_expr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012-2014 Ecole Normale Superieure
* Copyright 2014 INRIA Rocquencourt
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege,
* Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
* and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
* B.P. 105 - 78153 Le Chesnay, France
*/
#include <isl/id.h>
#include <isl/space.h>
#include <isl/constraint.h>
#include <isl/ilp.h>
#include <isl/val.h>
#include <isl_ast_build_expr.h>
#include <isl_ast_private.h>
#include <isl_ast_build_private.h>
#include <isl_sort.h>
/* Compute the "opposite" of the (numerator of the) argument of a div
* with denominator "d".
*
* In particular, compute
*
* -aff + (d - 1)
*/
static __isl_give isl_aff *oppose_div_arg(__isl_take isl_aff *aff,
__isl_take isl_val *d)
{
aff = isl_aff_neg(aff);
aff = isl_aff_add_constant_val(aff, d);
aff = isl_aff_add_constant_si(aff, -1);
return aff;
}
/* Internal data structure used inside isl_ast_expr_add_term.
* The domain of "build" is used to simplify the expressions.
* "build" needs to be set by the caller of isl_ast_expr_add_term.
* "ls" is the domain local space of the affine expression
* of which a term is being added.
* "cst" is the constant term of the expression in which the added term
* appears. It may be modified by isl_ast_expr_add_term.
*
* "v" is the coefficient of the term that is being constructed and
* is set internally by isl_ast_expr_add_term.
*/
struct isl_ast_add_term_data {
isl_ast_build *build;
isl_local_space *ls;
isl_val *cst;
isl_val *v;
};
/* Given the numerator "aff" of the argument of an integer division
* with denominator "d", check if it can be made non-negative over
* data->build->domain by stealing part of the constant term of
* the expression in which the integer division appears.
*
* In particular, the outer expression is of the form
*
* v * floor(aff/d) + cst
*
* We already know that "aff" itself may attain negative values.
* Here we check if aff + d*floor(cst/v) is non-negative, such
* that we could rewrite the expression to
*
* v * floor((aff + d*floor(cst/v))/d) + cst - v*floor(cst/v)
*
* Note that aff + d*floor(cst/v) can only possibly be non-negative
* if data->cst and data->v have the same sign.
* Similarly, if floor(cst/v) is zero, then there is no point in
* checking again.
*/
static isl_bool is_non_neg_after_stealing(__isl_keep isl_aff *aff,
__isl_keep isl_val *d, struct isl_ast_add_term_data *data)
{
isl_aff *shifted;
isl_val *shift;
isl_bool is_zero;
isl_bool non_neg;
if (isl_val_sgn(data->cst) != isl_val_sgn(data->v))
return isl_bool_false;
shift = isl_val_div(isl_val_copy(data->cst), isl_val_copy(data->v));
shift = isl_val_floor(shift);
is_zero = isl_val_is_zero(shift);
if (is_zero < 0 || is_zero) {
isl_val_free(shift);
return isl_bool_not(is_zero);
}
shift = isl_val_mul(shift, isl_val_copy(d));
shifted = isl_aff_copy(aff);
shifted = isl_aff_add_constant_val(shifted, shift);
non_neg = isl_ast_build_aff_is_nonneg(data->build, shifted);
isl_aff_free(shifted);
return non_neg;
}
/* Given the numerator "aff" of the argument of an integer division
* with denominator "d", steal part of the constant term of
* the expression in which the integer division appears to make it
* non-negative over data->build->domain.
*
* In particular, the outer expression is of the form
*
* v * floor(aff/d) + cst
*
* We know that "aff" itself may attain negative values,
* but that aff + d*floor(cst/v) is non-negative.
* Find the minimal positive value that we need to add to "aff"
* to make it positive and adjust data->cst accordingly.
* That is, compute the minimal value "m" of "aff" over
* data->build->domain and take
*
* s = ceil(-m/d)
*
* such that
*
* aff + d * s >= 0
*
* and rewrite the expression to
*
* v * floor((aff + s*d)/d) + (cst - v*s)
*/
static __isl_give isl_aff *steal_from_cst(__isl_take isl_aff *aff,
__isl_keep isl_val *d, struct isl_ast_add_term_data *data)
{
isl_set *domain;
isl_val *shift, *t;
domain = isl_ast_build_get_domain(data->build);
shift = isl_set_min_val(domain, aff);
isl_set_free(domain);
shift = isl_val_neg(shift);
shift = isl_val_div(shift, isl_val_copy(d));
shift = isl_val_ceil(shift);
t = isl_val_copy(shift);
t = isl_val_mul(t, isl_val_copy(data->v));
data->cst = isl_val_sub(data->cst, t);
shift = isl_val_mul(shift, isl_val_copy(d));
return isl_aff_add_constant_val(aff, shift);
}
/* Construct an expression representing the binary operation "type"
* (some division or modulo) applied to the expressions
* constructed from "aff" and "v".
*/
static __isl_give isl_ast_expr *div_mod(enum isl_ast_expr_op_type type,
__isl_take isl_aff *aff, __isl_take isl_val *v,
__isl_keep isl_ast_build *build)
{
isl_ast_expr *expr1, *expr2;
expr1 = isl_ast_expr_from_aff(aff, build);
expr2 = isl_ast_expr_from_val(v);
return isl_ast_expr_alloc_binary(type, expr1, expr2);
}
/* Create an isl_ast_expr evaluating the div at position "pos" in data->ls.
* The result is simplified in terms of data->build->domain.
* This function may change (the sign of) data->v.
*
* data->ls is known to be non-NULL.
*
* Let the div be of the form floor(e/d).
* If the ast_build_prefer_pdiv option is set then we check if "e"
* is non-negative, so that we can generate
*
* (pdiv_q, expr(e), expr(d))
*
* instead of
*
* (fdiv_q, expr(e), expr(d))
*
* If the ast_build_prefer_pdiv option is set and
* if "e" is not non-negative, then we check if "-e + d - 1" is non-negative.
* If so, we can rewrite
*
* floor(e/d) = -ceil(-e/d) = -floor((-e + d - 1)/d)
*
* and still use pdiv_q, while changing the sign of data->v.
*
* Otherwise, we check if
*
* e + d*floor(cst/v)
*
* is non-negative and if so, replace floor(e/d) by
*
* floor((e + s*d)/d) - s
*
* with s the minimal shift that makes the argument non-negative.
*/
static __isl_give isl_ast_expr *var_div(struct isl_ast_add_term_data *data,
int pos)
{
isl_ctx *ctx = isl_local_space_get_ctx(data->ls);
isl_aff *aff;
isl_val *d;
enum isl_ast_expr_op_type type;
aff = isl_local_space_get_div(data->ls, pos);
d = isl_aff_get_denominator_val(aff);
aff = isl_aff_scale_val(aff, isl_val_copy(d));
type = isl_ast_expr_op_fdiv_q;
if (isl_options_get_ast_build_prefer_pdiv(ctx)) {
isl_bool non_neg;
non_neg = isl_ast_build_aff_is_nonneg(data->build, aff);
if (non_neg >= 0 && !non_neg) {
isl_aff *opp = oppose_div_arg(isl_aff_copy(aff),
isl_val_copy(d));
non_neg = isl_ast_build_aff_is_nonneg(data->build, opp);
if (non_neg >= 0 && non_neg) {
data->v = isl_val_neg(data->v);
isl_aff_free(aff);
aff = opp;
} else
isl_aff_free(opp);
}
if (non_neg >= 0 && !non_neg) {
non_neg = is_non_neg_after_stealing(aff, d, data);
if (non_neg >= 0 && non_neg)
aff = steal_from_cst(aff, d, data);
}
if (non_neg < 0)
aff = isl_aff_free(aff);
else if (non_neg)
type = isl_ast_expr_op_pdiv_q;
}
return div_mod(type, aff, d, data->build);
}
/* Create an isl_ast_expr evaluating the specified dimension of data->ls.
* The result is simplified in terms of data->build->domain.
* This function may change (the sign of) data->v.
*
* The isl_ast_expr is constructed based on the type of the dimension.
* - divs are constructed by var_div
* - set variables are constructed from the iterator isl_ids in data->build
* - parameters are constructed from the isl_ids in data->ls
*/
static __isl_give isl_ast_expr *var(struct isl_ast_add_term_data *data,
enum isl_dim_type type, int pos)
{
isl_ctx *ctx = isl_local_space_get_ctx(data->ls);
isl_id *id;
if (type == isl_dim_div)
return var_div(data, pos);
if (type == isl_dim_set) {
id = isl_ast_build_get_iterator_id(data->build, pos);
return isl_ast_expr_from_id(id);
}
if (!isl_local_space_has_dim_id(data->ls, type, pos))
isl_die(ctx, isl_error_internal, "unnamed dimension",
return NULL);
id = isl_local_space_get_dim_id(data->ls, type, pos);
return isl_ast_expr_from_id(id);
}
/* Does "expr" represent the zero integer?
*/
static isl_bool ast_expr_is_zero(__isl_keep isl_ast_expr *expr)
{
if (!expr)
return isl_bool_error;
if (expr->type != isl_ast_expr_int)
return isl_bool_false;
return isl_val_is_zero(expr->u.v);
}
/* Create an expression representing the sum of "expr1" and "expr2",
* provided neither of the two expressions is identically zero.
*/
static __isl_give isl_ast_expr *ast_expr_add(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
if (!expr1 || !expr2)
goto error;
if (ast_expr_is_zero(expr1)) {
isl_ast_expr_free(expr1);
return expr2;
}
if (ast_expr_is_zero(expr2)) {
isl_ast_expr_free(expr2);
return expr1;
}
return isl_ast_expr_add(expr1, expr2);
error:
isl_ast_expr_free(expr1);
isl_ast_expr_free(expr2);
return NULL;
}
/* Subtract expr2 from expr1.
*
* If expr2 is zero, we simply return expr1.
* If expr1 is zero, we return
*
* (isl_ast_expr_op_minus, expr2)
*
* Otherwise, we return
*
* (isl_ast_expr_op_sub, expr1, expr2)
*/
static __isl_give isl_ast_expr *ast_expr_sub(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
if (!expr1 || !expr2)
goto error;
if (ast_expr_is_zero(expr2)) {
isl_ast_expr_free(expr2);
return expr1;
}
if (ast_expr_is_zero(expr1)) {
isl_ast_expr_free(expr1);
return isl_ast_expr_neg(expr2);
}
return isl_ast_expr_sub(expr1, expr2);
error:
isl_ast_expr_free(expr1);
isl_ast_expr_free(expr2);
return NULL;
}
/* Return an isl_ast_expr that represents
*
* v * (aff mod d)
*
* v is assumed to be non-negative.
* The result is simplified in terms of build->domain.
*/
static __isl_give isl_ast_expr *isl_ast_expr_mod(__isl_keep isl_val *v,
__isl_keep isl_aff *aff, __isl_keep isl_val *d,
__isl_keep isl_ast_build *build)
{
isl_ast_expr *expr;
isl_ast_expr *c;
if (!aff)
return NULL;
expr = div_mod(isl_ast_expr_op_pdiv_r,
isl_aff_copy(aff), isl_val_copy(d), build);
if (!isl_val_is_one(v)) {
c = isl_ast_expr_from_val(isl_val_copy(v));
expr = isl_ast_expr_mul(c, expr);
}
return expr;
}
/* Create an isl_ast_expr that scales "expr" by "v".
*
* If v is 1, we simply return expr.
* If v is -1, we return
*
* (isl_ast_expr_op_minus, expr)
*
* Otherwise, we return
*
* (isl_ast_expr_op_mul, expr(v), expr)
*/
static __isl_give isl_ast_expr *scale(__isl_take isl_ast_expr *expr,
__isl_take isl_val *v)
{
isl_ast_expr *c;
if (!expr || !v)
goto error;
if (isl_val_is_one(v)) {
isl_val_free(v);
return expr;
}
if (isl_val_is_negone(v)) {
isl_val_free(v);
expr = isl_ast_expr_neg(expr);
} else {
c = isl_ast_expr_from_val(v);
expr = isl_ast_expr_mul(c, expr);
}
return expr;
error:
isl_val_free(v);
isl_ast_expr_free(expr);
return NULL;
}
/* Add an expression for "*v" times the specified dimension of data->ls
* to expr.
* If the dimension is an integer division, then this function
* may modify data->cst in order to make the numerator non-negative.
* The result is simplified in terms of data->build->domain.
*
* Let e be the expression for the specified dimension,
* multiplied by the absolute value of "*v".
* If "*v" is negative, we create
*
* (isl_ast_expr_op_sub, expr, e)
*
* except when expr is trivially zero, in which case we create
*
* (isl_ast_expr_op_minus, e)
*
* instead.
*
* If "*v" is positive, we simply create
*
* (isl_ast_expr_op_add, expr, e)
*
*/
static __isl_give isl_ast_expr *isl_ast_expr_add_term(
__isl_take isl_ast_expr *expr, enum isl_dim_type type, int pos,
__isl_take isl_val *v, struct isl_ast_add_term_data *data)
{
isl_ast_expr *term;
if (!expr)
return NULL;
data->v = v;
term = var(data, type, pos);
v = data->v;
if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
v = isl_val_neg(v);
term = scale(term, v);
return ast_expr_sub(expr, term);
} else {
term = scale(term, v);
return ast_expr_add(expr, term);
}
}
/* Add an expression for "v" to expr.
*/
static __isl_give isl_ast_expr *isl_ast_expr_add_int(
__isl_take isl_ast_expr *expr, __isl_take isl_val *v)
{
isl_ast_expr *expr_int;
if (!expr || !v)
goto error;
if (isl_val_is_zero(v)) {
isl_val_free(v);
return expr;
}
if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
v = isl_val_neg(v);
expr_int = isl_ast_expr_from_val(v);
return ast_expr_sub(expr, expr_int);
} else {
expr_int = isl_ast_expr_from_val(v);
return ast_expr_add(expr, expr_int);
}
error:
isl_ast_expr_free(expr);
isl_val_free(v);
return NULL;
}
/* Internal data structure used inside extract_modulos.
*
* If any modulo expressions are detected in "aff", then the
* expression is removed from "aff" and added to either "pos" or "neg"
* depending on the sign of the coefficient of the modulo expression
* inside "aff".
*
* "add" is an expression that needs to be added to "aff" at the end of
* the computation. It is NULL as long as no modulos have been extracted.
*
* "i" is the position in "aff" of the div under investigation
* "v" is the coefficient in "aff" of the div
* "div" is the argument of the div, with the denominator removed
* "d" is the original denominator of the argument of the div
*
* If set, then "partial" is the (positively weighted) sum
* of the affine expressions of one or more previously considered constraints
* that could still be complemented to an expression equal to "div".
* "nonneg" is an affine expression that is non-negative over "build"
* and that can be used to extract a modulo expression from "div".
* In particular, if "sign" is 1, then the coefficients of "nonneg"
* are equal to those of "div" modulo "d". If "sign" is -1, then
* the coefficients of "nonneg" are opposite to those of "div" modulo "d".
* If "sign" is 0, then no such affine expression has been found (yet).
*/
struct isl_extract_mod_data {
isl_ast_build *build;
isl_aff *aff;
isl_ast_expr *pos;
isl_ast_expr *neg;
isl_aff *add;
int i;
isl_val *v;
isl_val *d;
isl_aff *div;
isl_aff *partial;
isl_aff *nonneg;
int sign;
};
/* Does
*
* arg mod data->d
*
* represent (a special case of) a test for some linear expression
* being even?
*
* In particular, is it of the form
*
* (lin - 1) mod 2
*
* ?
*/
static isl_bool is_even_test(struct isl_extract_mod_data *data,
__isl_keep isl_aff *arg)
{
isl_bool res;
isl_val *cst;
res = isl_val_eq_si(data->d, 2);
if (res < 0 || !res)
return res;
cst = isl_aff_get_constant_val(arg);
res = isl_val_eq_si(cst, -1);
isl_val_free(cst);
return res;
}
/* Given that data->v * div_i in data->aff is equal to
*
* f * (term - (arg mod d))
*
* with data->d * f = data->v and "arg" non-negative on data->build, add
*
* f * term
*
* to data->add and
*
* abs(f) * (arg mod d)
*
* to data->neg or data->pos depending on the sign of -f.
*
* In the special case that "arg mod d" is of the form "(lin - 1) mod 2",
* with "lin" some linear expression, first replace
*
* f * (term - ((lin - 1) mod 2))
*
* by
*
* -f * (1 - term - (lin mod 2))
*
* These two are equal because
*
* ((lin - 1) mod 2) + (lin mod 2) = 1
*
* Also, if "lin - 1" is non-negative, then "lin" is non-negative too.
*/
static isl_stat extract_term_and_mod(struct isl_extract_mod_data *data,
__isl_take isl_aff *term, __isl_take isl_aff *arg)
{
isl_bool even;
isl_ast_expr *expr;
int s;
even = is_even_test(data, arg);
if (even < 0) {
arg = isl_aff_free(arg);
} else if (even) {
term = oppose_div_arg(term, isl_val_copy(data->d));
data->v = isl_val_neg(data->v);
arg = isl_aff_set_constant_si(arg, 0);
}
data->v = isl_val_div(data->v, isl_val_copy(data->d));
s = isl_val_sgn(data->v);
data->v = isl_val_abs(data->v);
expr = isl_ast_expr_mod(data->v, arg, data->d, data->build);
isl_aff_free(arg);
if (s > 0)
data->neg = ast_expr_add(data->neg, expr);
else
data->pos = ast_expr_add(data->pos, expr);
data->aff = isl_aff_set_coefficient_si(data->aff,
isl_dim_div, data->i, 0);
if (s < 0)
data->v = isl_val_neg(data->v);
term = isl_aff_scale_val(term, isl_val_copy(data->v));
if (!data->add)
data->add = term;
else
data->add = isl_aff_add(data->add, term);
if (!data->add)
return isl_stat_error;
return isl_stat_ok;
}
/* Given that data->v * div_i in data->aff is of the form
*
* f * d * floor(div/d)
*
* with div nonnegative on data->build, rewrite it as
*
* f * (div - (div mod d)) = f * div - f * (div mod d)
*
* and add
*
* f * div
*
* to data->add and
*
* abs(f) * (div mod d)
*
* to data->neg or data->pos depending on the sign of -f.
*/
static isl_stat extract_mod(struct isl_extract_mod_data *data)
{
return extract_term_and_mod(data, isl_aff_copy(data->div),
isl_aff_copy(data->div));
}
/* Given that data->v * div_i in data->aff is of the form
*
* f * d * floor(div/d) (1)
*
* check if div is non-negative on data->build and, if so,
* extract the corresponding modulo from data->aff.
* If not, then check if
*
* -div + d - 1
*
* is non-negative on data->build. If so, replace (1) by
*
* -f * d * floor((-div + d - 1)/d)
*
* and extract the corresponding modulo from data->aff.
*
* This function may modify data->div.
*/
static isl_stat extract_nonneg_mod(struct isl_extract_mod_data *data)
{
isl_bool mod;
mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
if (mod < 0)
goto error;
if (mod)
return extract_mod(data);
data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
if (mod < 0)
goto error;
if (mod) {
data->v = isl_val_neg(data->v);
return extract_mod(data);
}
return isl_stat_ok;
error:
data->aff = isl_aff_free(data->aff);
return isl_stat_error;
}
/* Does "c" have a constant term that is "too large"?
* Here, "too large" is fairly arbitrarily set to 1 << 15.
*/
static isl_bool has_large_constant_term(__isl_keep isl_constraint *c)
{
isl_val *v;
int sign;
v = isl_val_abs(isl_constraint_get_constant_val(c));
if (!v)
return isl_bool_error;
sign = isl_val_cmp_si(v, 1 << 15);
isl_val_free(v);
return isl_bool_ok(sign > 0);
}
/* Is the affine expression with constant term returned by "get_constant"
* "simpler" than data->nonneg
* for use in extracting a modulo expression?
*
* Currently, only this constant term is considered.
* In particular, we prefer the affine expression with the smallest constant
* term.
* This means that if there are two constraints, say x >= 0 and -x + 10 >= 0,
* then we would pick x >= 0
*
* More detailed heuristics could be used if it turns out that there is a need.
*/
static isl_bool is_simpler(struct isl_extract_mod_data *data,
__isl_give isl_val *get_constant(struct isl_extract_mod_data *data,
void *user), void *user)
{
isl_val *v1, *v2;
isl_bool simpler;
if (!data->nonneg)
return isl_bool_true;
v1 = isl_val_abs(get_constant(data, user));
v2 = isl_val_abs(isl_aff_get_constant_val(data->nonneg));
simpler = isl_val_lt(v1, v2);
isl_val_free(v1);
isl_val_free(v2);
return simpler;
}
/* Return the constant term of "c".
*/
static __isl_give isl_val *get_constraint_constant(
struct isl_extract_mod_data *data, void *user)
{
isl_constraint *c = user;
return isl_constraint_get_constant_val(c);
}
/* Is the affine expression of constraint "c" "simpler" than data->nonneg
* for use in extracting a modulo expression?
*
* The test is based on the constant term of "c".
*/
static isl_bool mod_constraint_is_simpler(struct isl_extract_mod_data *data,
__isl_keep isl_constraint *c)
{
return is_simpler(data, &get_constraint_constant, c);
}
/* Replace data->nonneg by the affine expression "aff" and
* set data->sign to "sign".
*/
static isl_stat replace_nonneg(struct isl_extract_mod_data *data,
__isl_take isl_aff *aff, int sign)
{
isl_aff_free(data->nonneg);
data->nonneg = aff;
data->sign = sign;
return isl_stat_non_null(data->nonneg);
}
/* If "c" is "simpler" than data->nonneg,
* then replace data->nonneg by the affine expression of "c" and
* set data->sign to "sign".
*/
static isl_stat replace_if_simpler(struct isl_extract_mod_data *data,
__isl_keep isl_constraint *c, int sign)
{
isl_bool simpler;
simpler = mod_constraint_is_simpler(data, c);
if (simpler < 0 || !simpler)
return isl_stat_non_error_bool(simpler);
return replace_nonneg(data, isl_constraint_get_aff(c), sign);
}
/* Internal data structure used inside check_parallel_or_opposite.
*
* "data" is the information passed down from the caller.
* "c" is the constraint being inspected.
*
* "n" contains the number of parameters and the number of input dimensions and
* is set by the first call to parallel_or_opposite_scan.
* "parallel" is set as long as the coefficients of "c" are still potentially
* equal to those of data->div modulo data->d.
* "opposite" is set as long as the coefficients of "c" are still potentially
* opposite to those of data->div modulo data->d.
* "partial" is set if the coefficients of "c" are still potentially
* a subset of those of data->div.
* "final" is set is the coefficients in data->partial together with those
* of "c" still cover the coefficients of data->div.
*
* If "f" is set, then it is the factor with which the coefficients
* of "c" need to be multiplied to match those of data->div.
*/
struct isl_parallel_stat {
struct isl_extract_mod_data *data;
isl_constraint *c;
isl_size n[2];
isl_bool parallel;
isl_bool opposite;
isl_bool partial;
int final;
isl_val *f;
};
/* Should the scan of coefficients be continued?
* That is, are the coefficients still (potentially) (partially) equal or
* opposite?
*/
static isl_bool parallel_or_opposite_continue(struct isl_parallel_stat *stat)
{
if (stat->parallel < 0 || stat->opposite < 0 || stat->partial < 0)
return isl_bool_error;
return isl_bool_ok(stat->parallel || stat->opposite || stat->partial);
}
/* Is coefficient "i" of type "c_type" of stat->c potentially equal or
* opposite to coefficient "i" of type "a_type" of stat->data->div
* modulo stat->data->div?
* In particular, are they both zero or both non-zero?
*
* Note that while the coefficients of stat->data->div can be reasonably
* expected not to involve any coefficients that are multiples of stat->data->d,
* "c" may very well involve such coefficients.
* This means that some cases of equal or opposite constraints can be missed
* this way.
*
* If the coefficient of stat->data->div is zero, but that of "c" is not,
* then the coefficients of "c" cannot form a subset of those
* of stat->data->div.
* If the coefficient of stat->data->div is not zero,
* then check that it does not appear in both "c" and stat->data->partial.
* If it does not appear in either, then it must appear in some later constraint
* and "c" can therefore not be the last in the sequence of constraints
* that sum up to stat->data->div.
*/
static isl_bool parallel_or_opposite_feasible(struct isl_parallel_stat *stat,
enum isl_dim_type c_type, enum isl_dim_type a_type, int i)
{
isl_bool a, b;
a = isl_constraint_involves_dims(stat->c, c_type, i, 1);
b = isl_aff_involves_dims(stat->data->div, a_type, i, 1);
if (a < 0 || b < 0)
return isl_bool_error;
if (a != b)
stat->parallel = stat->opposite = isl_bool_false;
if (!stat->partial)
return parallel_or_opposite_continue(stat);
if (!b && a)
stat->partial = isl_bool_false;
if (b && (a || stat->final) && stat->data->partial) {
isl_bool c;
c = isl_aff_involves_dims(stat->data->partial, a_type, i, 1);
if (c < 0)
return isl_bool_error;
if (a && c)
stat->partial = isl_bool_false;
if (!a && !c)
stat->final = 0;
}
return parallel_or_opposite_continue(stat);
}
/* Update stat->partial based on the coefficient "v1" of stat->c and
* "v2" of stat->data->div, where "v2" is known not to be zero.
* "v1" may be modified by this function and the modified value is returned.
* This function may also set stat->f.
*
* If "v1" is zero, then no update needs to be performed.
* Otherwise, stat->partial can only remain set if "c" is part
* of some positively weighted sum that is equal to stat->data->div.
* This means that v2 divided by v1 needs to be a positive integer.
* This quotient is stored in stat->f. If this quotient has already
* been set for a previous coefficient, then it needs to be the same.
*/
static __isl_give isl_val *update_is_partial(struct isl_parallel_stat *stat,
__isl_take isl_val *v1, __isl_keep isl_val *v2)
{
if (!stat->partial)
return v1;
if (isl_val_is_zero(v1))
return v1;
stat->partial = isl_val_is_divisible_by(v2, v1);
if (stat->partial < 0 || !stat->partial)
return v1;
v1 = isl_val_div(isl_val_copy(v2), v1);
stat->partial = isl_val_is_pos(v1);
if (stat->partial < 0 || !stat->partial)
return v1;
if (!stat->f)
stat->f = isl_val_copy(v1);
stat->partial = isl_val_eq(v1, stat->f);
return v1;
}
/* Is coefficient "i" of type "c_type" of stat->c equal or
* opposite to coefficient "i" of type "a_type" of stat->data->div
* modulo stat->data->div, or
* could stat->c be part of a positively weighted sum equal to stat->data->div?
* This function may set stat->f (at most once).
*
* If the coefficient of stat->data->div is zero,
* then parallel_or_opposite_feasible has already checked
* that the coefficient of stat->c is zero as well,
* so no further checks are needed.
*/
static isl_bool is_parallel_or_opposite(struct isl_parallel_stat *stat,
enum isl_dim_type c_type, enum isl_dim_type a_type, int i)
{
isl_val *v1, *v2;
isl_bool b;
b = isl_aff_involves_dims(stat->data->div, a_type, i, 1);
if (b < 0 || !b)
return isl_bool_not(b);
v1 = isl_constraint_get_coefficient_val(stat->c, c_type, i);
v2 = isl_aff_get_coefficient_val(stat->data->div, a_type, i);
if (stat->parallel) {
v1 = isl_val_sub(v1, isl_val_copy(v2));
stat->parallel = isl_val_is_divisible_by(v1, stat->data->d);
v1 = isl_val_add(v1, isl_val_copy(v2));
}
if (stat->opposite) {
v1 = isl_val_add(v1, isl_val_copy(v2));
stat->opposite = isl_val_is_divisible_by(v1, stat->data->d);
}
v1 = update_is_partial(stat, v1, v2);
isl_val_free(v1);
isl_val_free(v2);
return parallel_or_opposite_continue(stat);
}
/* Scan the coefficients of stat->c to see if they are (potentially)
* equal or opposite to those of stat->data->div modulo stat->data->d,
* calling "fn" on each coefficient.
* IF "init" is set, then this is the first call to this function and
* then stat->n is initialized.
*/
static isl_bool parallel_or_opposite_scan(struct isl_parallel_stat *stat,
isl_bool (*fn)(struct isl_parallel_stat *stat,
enum isl_dim_type c_type, enum isl_dim_type a_type, int i),
int init)
{
enum isl_dim_type c_type[2] = { isl_dim_param, isl_dim_set };
enum isl_dim_type a_type[2] = { isl_dim_param, isl_dim_in };
int i, t;
for (t = 0; t < 2; ++t) {
if (init) {
stat->n[t] = isl_constraint_dim(stat->c, c_type[t]);
if (stat->n[t] < 0)
return isl_bool_error;
}
for (i = 0; i < stat->n[t]; ++i) {
isl_bool ok;
ok = fn(stat, c_type[t], a_type[t], i);
if (ok < 0 || !ok)
return ok;
}
}
return isl_bool_true;
}
/* Update stat->data->partial with stat->c.
*
* In particular, if stat->c with weight stat->f turns out
* to potentially be a part of a weighted sum equal to stat->data->div
* (i.e., stat->partial is set), then add this scaled version of stat->c
* to stat->data->partial or initialize stat->data->partial if it has not
* been set yet.
*/