-
Notifications
You must be signed in to change notification settings - Fork 25
/
isl_ast_build_expr.c
2757 lines (2457 loc) · 78.2 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
*
* "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 *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;
}
/* Is the affine expression of constraint "c" "simpler" than data->nonneg
* for use in extracting a modulo expression?
*
* We currently only consider the constant term of the affine expression.
* 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 int mod_constraint_is_simpler(struct isl_extract_mod_data *data,
__isl_keep isl_constraint *c)
{
isl_val *v1, *v2;
int simpler;
if (!data->nonneg)
return 1;
v1 = isl_val_abs(isl_constraint_get_constant_val(c));
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;
}
/* Check if the coefficients of "c" are either equal or opposite to those
* of data->div modulo data->d. If so, and if "c" is "simpler" than
* data->nonneg, then replace data->nonneg by the affine expression of "c"
* and set data->sign accordingly.
*
* Both "c" and data->div are assumed not to involve any integer divisions.
*
* Before we start the actual comparison, we first quickly check if
* "c" and data->div have the same non-zero coefficients.
* If not, then we assume that "c" is not of the desired form.
* Note that while the coefficients of data->div can be reasonably expected
* not to involve any coefficients that are multiples of d, "c" may
* very well involve such coefficients. This means that we may actually
* miss some cases.
*
* If the constant term is "too large", then the constraint is rejected,
* where "too large" is fairly arbitrarily set to 1 << 15.
* We do this to avoid picking up constraints that bound a variable
* by a very large number, say the largest or smallest possible
* variable in the representation of some integer type.
*/
static isl_stat check_parallel_or_opposite(__isl_take isl_constraint *c,
void *user)
{
struct isl_extract_mod_data *data = user;
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;
isl_size n[2];
isl_bool parallel = isl_bool_true, opposite = isl_bool_true;
for (t = 0; t < 2; ++t) {
n[t] = isl_constraint_dim(c, c_type[t]);
if (n[t] < 0)
goto error;
for (i = 0; i < n[t]; ++i) {
isl_bool a, b;
a = isl_constraint_involves_dims(c, c_type[t], i, 1);
b = isl_aff_involves_dims(data->div, a_type[t], i, 1);
if (a < 0 || b < 0)
goto error;
if (a != b)
parallel = opposite = isl_bool_false;
}
}
if (parallel || opposite) {
isl_val *v;
v = isl_val_abs(isl_constraint_get_constant_val(c));
if (isl_val_cmp_si(v, 1 << 15) > 0)
parallel = opposite = isl_bool_false;
isl_val_free(v);
}
for (t = 0; t < 2; ++t) {
for (i = 0; i < n[t]; ++i) {
isl_val *v1, *v2;
if (!parallel && !opposite)
break;
v1 = isl_constraint_get_coefficient_val(c,
c_type[t], i);
v2 = isl_aff_get_coefficient_val(data->div,
a_type[t], i);
if (parallel) {
v1 = isl_val_sub(v1, isl_val_copy(v2));
parallel = isl_val_is_divisible_by(v1, data->d);
v1 = isl_val_add(v1, isl_val_copy(v2));
}
if (opposite) {
v1 = isl_val_add(v1, isl_val_copy(v2));
opposite = isl_val_is_divisible_by(v1, data->d);
}
isl_val_free(v1);
isl_val_free(v2);
if (parallel < 0 || opposite < 0)
goto error;
}
}
if ((parallel || opposite) && mod_constraint_is_simpler(data, c)) {
isl_aff_free(data->nonneg);
data->nonneg = isl_constraint_get_aff(c);
data->sign = parallel ? 1 : -1;
}
isl_constraint_free(c);
if (data->sign != 0 && data->nonneg == NULL)
return isl_stat_error;
return isl_stat_ok;
error:
isl_constraint_free(c);
return isl_stat_error;
}
/* Given that data->v * div_i in data->aff is of the form
*
* f * d * floor(div/d) (1)
*
* see if we can find an expression div' that is non-negative over data->build
* and that is related to div through
*
* div' = div + d * e
*
* or
*
* div' = -div + d - 1 + d * e
*
* with e some affine expression.
* If so, we write (1) as
*
* f * div + f * (div' mod d)
*
* or
*
* -f * (-div + d - 1) - f * (div' mod d)
*
* exploiting (in the second case) the fact that
*
* f * d * floor(div/d) = -f * d * floor((-div + d - 1)/d)
*
*
* We first try to find an appropriate expression for div'
* from the constraints of data->build->domain (which is therefore
* guaranteed to be non-negative on data->build), where we remove
* any integer divisions from the constraints and skip this step
* if "div" itself involves any integer divisions.
* If we cannot find an appropriate expression this way, then
* we pass control to extract_nonneg_mod where check
* if div or "-div + d -1" themselves happen to be
* non-negative on data->build.
*
* While looking for an appropriate constraint in data->build->domain,
* we ignore the constant term, so after finding such a constraint,
* we still need to fix up the constant term.
* In particular, if a is the constant term of "div"
* (or d - 1 - the constant term of "div" if data->sign < 0)
* and b is the constant term of the constraint, then we need to find
* a non-negative constant c such that
*
* b + c \equiv a mod d
*
* We therefore take
*
* c = (a - b) mod d
*
* and add it to b to obtain the constant term of div'.
* If this constant term is "too negative", then we add an appropriate
* multiple of d to make it positive.
*
*
* Note that the above is only a very simple heuristic for finding an
* appropriate expression. We could try a bit harder by also considering
* sums of constraints that involve disjoint sets of variables or
* we could consider arbitrary linear combinations of constraints,
* although that could potentially be much more expensive as it involves
* the solution of an LP problem.
*
* In particular, if v_i is a column vector representing constraint i,
* w represents div and e_i is the i-th unit vector, then we are looking
* for a solution of the constraints
*
* \sum_i lambda_i v_i = w + \sum_i alpha_i d e_i
*
* with \lambda_i >= 0 and alpha_i of unrestricted sign.
* If we are not just interested in a non-negative expression, but
* also in one with a minimal range, then we don't just want
* c = \sum_i lambda_i v_i to be non-negative over the domain,
* but also beta - c = \sum_i mu_i v_i, where beta is a scalar
* that we want to minimize and we now also have to take into account
* the constant terms of the constraints.
* Alternatively, we could first compute the dual of the domain
* and plug in the constraints on the coefficients.
*/
static isl_stat try_extract_mod(struct isl_extract_mod_data *data)
{
isl_basic_set *hull;
isl_val *v1, *v2;
isl_stat r;
isl_size n;
if (!data->build)
goto error;
n = isl_aff_dim(data->div, isl_dim_div);
if (n < 0)
goto error;
if (isl_aff_involves_dims(data->div, isl_dim_div, 0, n))
return extract_nonneg_mod(data);
hull = isl_set_simple_hull(isl_set_copy(data->build->domain));
hull = isl_basic_set_remove_divs(hull);
data->sign = 0;
data->nonneg = NULL;
r = isl_basic_set_foreach_constraint(hull, &check_parallel_or_opposite,
data);
isl_basic_set_free(hull);
if (!data->sign || r < 0) {
isl_aff_free(data->nonneg);
if (r < 0)
goto error;
return extract_nonneg_mod(data);
}
v1 = isl_aff_get_constant_val(data->div);
v2 = isl_aff_get_constant_val(data->nonneg);
if (data->sign < 0) {
v1 = isl_val_neg(v1);
v1 = isl_val_add(v1, isl_val_copy(data->d));
v1 = isl_val_sub_ui(v1, 1);
}
v1 = isl_val_sub(v1, isl_val_copy(v2));
v1 = isl_val_mod(v1, isl_val_copy(data->d));
v1 = isl_val_add(v1, v2);
v2 = isl_val_div(isl_val_copy(v1), isl_val_copy(data->d));
v2 = isl_val_ceil(v2);
if (isl_val_is_neg(v2)) {
v2 = isl_val_mul(v2, isl_val_copy(data->d));
v1 = isl_val_sub(v1, isl_val_copy(v2));
}
data->nonneg = isl_aff_set_constant_val(data->nonneg, v1);
isl_val_free(v2);
if (data->sign < 0) {
data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
data->v = isl_val_neg(data->v);
}
return extract_term_and_mod(data,
isl_aff_copy(data->div), data->nonneg);
error:
data->aff = isl_aff_free(data->aff);
return isl_stat_error;
}
/* Check if "data->aff" involves any (implicit) modulo computations based
* on div "data->i".
* If so, remove them from aff and add expressions corresponding
* to those modulo computations to data->pos and/or data->neg.
*
* "aff" is assumed to be an integer affine expression.
*
* In particular, check if (v * div_j) is of the form
*
* f * m * floor(a / m)
*
* and, if so, rewrite it as
*
* f * (a - (a mod m)) = f * a - f * (a mod m)
*
* and extract out -f * (a mod m).
* In particular, if f > 0, we add (f * (a mod m)) to *neg.
* If f < 0, we add ((-f) * (a mod m)) to *pos.
*
* Note that in order to represent "a mod m" as
*
* (isl_ast_expr_op_pdiv_r, a, m)
*
* we need to make sure that a is non-negative.
* If not, we check if "-a + m - 1" is non-negative.
* If so, we can rewrite
*
* floor(a/m) = -ceil(-a/m) = -floor((-a + m - 1)/m)
*
* and still extract a modulo.
*/
static int extract_modulo(struct isl_extract_mod_data *data)
{
data->div = isl_aff_get_div(data->aff, data->i);
data->d = isl_aff_get_denominator_val(data->div);
if (isl_val_is_divisible_by(data->v, data->d)) {
data->div = isl_aff_scale_val(data->div, isl_val_copy(data->d));
if (try_extract_mod(data) < 0)
data->aff = isl_aff_free(data->aff);