-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBounds.cpp
More file actions
3864 lines (3434 loc) · 154 KB
/
Bounds.cpp
File metadata and controls
3864 lines (3434 loc) · 154 KB
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
#include <iostream>
#include <utility>
#include "Bounds.h"
#include "CSE.h"
#include "ConciseCasts.h"
#include "Debug.h"
#include "Deinterleave.h"
#include "ExprUsesVar.h"
#include "FindIntrinsics.h"
#include "Func.h"
#include "IR.h"
#include "IREquality.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "IRPrinter.h"
#include "IRVisitor.h"
#include "InlineReductions.h"
#include "Param.h"
#include "PurifyIndexMath.h"
#include "Simplify.h"
#include "SimplifyCorrelatedDifferences.h"
#include "Solve.h"
#include "StrictifyFloat.h"
#include "Util.h"
#include "Var.h"
#ifndef DO_TRACK_BOUNDS_INTERVALS
#define DO_TRACK_BOUNDS_INTERVALS 0
#endif
#ifndef DO_DUMP_BOXES_TOUCHED
#define DO_DUMP_BOXES_TOUCHED 0
#endif
namespace Halide {
namespace Internal {
using std::map;
using std::pair;
using std::set;
using std::string;
using std::vector;
namespace {
bool can_widen(const Expr &e) {
// We don't want to widen Xtensa 48-bit integers
return e.type().bits() <= 32;
}
bool can_widen_all(const std::vector<Expr> &args) {
for (const auto &e : args) {
if (!can_widen(e)) {
return false;
}
}
return true;
}
Expr widen(Expr a) {
internal_assert(can_widen(a));
Type result_type = a.type().widen();
return Cast::make(result_type, std::move(a));
}
Expr narrow(Expr a) {
Type result_type = a.type().narrow();
return Cast::make(result_type, std::move(a));
}
Expr saturating_narrow(const Expr &a) {
Type narrow = a.type().narrow();
return saturating_cast(narrow, a);
}
int static_sign(const Expr &x) {
if (is_positive_const(x)) {
return 1;
} else if (is_negative_const(x)) {
return -1;
} else {
Expr zero = make_zero(x.type());
if (is_const_one(simplify(x > zero))) {
return 1;
} else if (is_const_one(simplify(x < zero))) {
return -1;
}
}
return 0;
}
Interval simplify(const Interval &i) {
Interval result;
result.min = simplify(i.min);
if (i.is_single_point()) {
result.max = result.min;
} else {
result.max = simplify(i.max);
}
return result;
}
} // anonymous namespace
const FuncValueBounds &empty_func_value_bounds() {
static FuncValueBounds empty;
return empty;
}
Expr find_constant_bound(const Expr &e, Direction d, const Scope<Interval> &scope) {
Interval interval = find_constant_bounds(e, scope);
Expr bound;
if (interval.has_lower_bound() && (d == Direction::Lower)) {
bound = interval.min;
} else if (interval.has_upper_bound() && (d == Direction::Upper)) {
bound = interval.max;
}
return bound;
}
Interval find_constant_bounds(const Expr &e, const Scope<Interval> &scope) {
Expr expr = bound_correlated_differences(simplify(remove_likelies(e)));
Interval interval = bounds_of_expr_in_scope(expr, scope, FuncValueBounds(), true);
interval = simplify(interval);
// Note that we can get non-const but well-defined results (e.g. signed_integer_overflow);
// for our purposes here, treat anything non-const as no-bound.
if (!is_const(interval.min)) {
interval.min = Interval::neg_inf();
}
if (!is_const(interval.max)) {
interval.max = Interval::pos_inf();
}
return interval;
}
bool Box::maybe_unused() const {
return used.defined() && !is_const_one(used);
}
std::ostream &operator<<(std::ostream &stream, const Box &b) {
stream << "{";
for (size_t dim = 0; dim < b.size(); dim++) {
if (dim > 0) {
stream << ", ";
}
stream << "[" << b[dim].min << ", " << b[dim].max << "]";
}
stream << "}";
if (b.used.defined()) {
stream << " if " << b.used;
}
return stream;
}
namespace {
class Bounds : public IRVisitor {
public:
Interval interval;
Scope<Interval> scope;
const FuncValueBounds &func_bounds;
// If set to true, attempt to return an interval with constant upper
// and lower bounds. If the bound is not constant, it is set to
// unbounded.
bool const_bound;
Bounds(const Scope<Interval> *s, const FuncValueBounds &fb, bool const_bound)
: func_bounds(fb), const_bound(const_bound) {
scope.set_containing_scope(s);
}
#if DO_TRACK_BOUNDS_INTERVALS
public:
int log_indent = 0;
private:
std::string log_spaces() const {
return std::string(log_indent, ' ');
}
struct BoundsLogger final {
Bounds *const self;
BoundsLogger(Bounds *self, const char *pretty_function)
: self(self) {
string name = pretty_function;
name = replace_all(std::move(name), "(anonymous namespace)::", "");
name = replace_all(std::move(name), "virtual void Halide::Internal::", "");
name = replace_all(std::move(name), "(const Halide::Internal::", "(");
name = replace_all(std::move(name), "::visit", "");
name = replace_all(std::move(name), " *)", ")");
log_line(name, " {");
self->log_indent++;
}
template<typename... Args>
void log_line(Args &&...args) {
debug(0) << self->log_spaces();
// C++17 right fold
auto dump = [](auto &&arg) {
debug(0) << arg;
};
(dump(args), ...);
debug(0) << "\n";
}
~BoundsLogger() {
log_line("mn=", self->interval.min);
log_line("mx=", self->interval.max);
self->log_indent--;
log_line('}');
}
};
#define TRACK_BOUNDS_INTERVAL BoundsLogger log_me_here_(this, __PRETTY_FUNCTION__)
#define TRACK_BOUNDS_INFO(...) \
do { \
log_me_here_.log_line(__VA_ARGS__); \
} while (0)
#else
#define TRACK_BOUNDS_INTERVAL \
do { \
} while (0)
#define TRACK_BOUNDS_INFO(...) \
do { \
} while (0)
#endif // DO_TRACK_BOUNDS_INTERVALS
private:
// Compute the intrinsic bounds of a function.
void bounds_of_func(const string &name, int value_index, Type t) {
// if we can't get a good bound from the function, fall back to the bounds of the type.
bounds_of_type(t);
pair<string, int> key = {name, value_index};
FuncValueBounds::const_iterator iter = func_bounds.find(key);
if (iter != func_bounds.end()) {
if (iter->second.has_lower_bound()) {
interval.min = iter->second.min;
}
if (iter->second.has_upper_bound()) {
interval.max = iter->second.max;
}
}
}
void bounds_of_type(Type t) {
t = t.element_of();
if ((t.is_uint() || t.is_int()) && t.bits() <= 16) {
interval = Interval(t.min(), t.max());
} else {
interval = Interval::everything();
}
}
using IRVisitor::visit;
void visit(const IntImm *op) override {
TRACK_BOUNDS_INTERVAL;
interval = Interval::single_point(op);
}
void visit(const UIntImm *op) override {
TRACK_BOUNDS_INTERVAL;
interval = Interval::single_point(op);
}
void visit(const FloatImm *op) override {
TRACK_BOUNDS_INTERVAL;
interval = Interval::single_point(op);
}
void visit(const StringImm *op) override {
TRACK_BOUNDS_INTERVAL;
interval = Interval::single_point(op);
}
void visit(const Reinterpret *op) override {
TRACK_BOUNDS_INTERVAL;
Type t = op->type.element_of();
if (t.is_handle()) {
interval = Interval::everything();
return;
}
// Just use the bounds of the type
bounds_of_type(t);
}
void visit(const Cast *op) override {
TRACK_BOUNDS_INTERVAL;
op->value.accept(this);
Interval a = interval;
if (a.is_single_point(op->value)) {
interval = Interval::single_point(op);
return;
}
Type to = op->type.element_of();
Type from = op->value.type().element_of();
if (a.is_single_point()) {
interval = Interval::single_point(Cast::make(to, a.min));
return;
}
// If overflow is impossible, cast the min and max. If it's
// possible, use the bounds of the destination type.
bool could_overflow = true;
if (to.can_represent(from) || to.is_float()) {
could_overflow = false;
} else if (to.is_int() && to.bits() >= 32) {
// If we cast to an int32 or greater, assume that it won't
// overflow. Signed 32-bit integer overflow is undefined.
could_overflow = false;
} else if (a.is_bounded()) {
if (from.can_represent(to)) {
// The other case to consider is narrowing where the
// bounds of the original fit into the narrower type. We
// can only really prove that this is the case if they're
// constants, so try to make the constants first.
// First constant-fold
a = simplify(a);
// Then try to strip off junk mins and maxes.
bool old_constant_bound = const_bound;
const_bound = true;
a.min.accept(this);
Expr lower_bound = interval.has_lower_bound() ? interval.min : Expr();
a.max.accept(this);
Expr upper_bound = interval.has_upper_bound() ? interval.max : Expr();
const_bound = old_constant_bound;
if (lower_bound.defined() && upper_bound.defined()) {
// Cast them to the narrow type and back and see if
// they're provably unchanged.
Expr test =
(cast(from, cast(to, lower_bound)) == lower_bound &&
cast(from, cast(to, upper_bound)) == upper_bound);
if (can_prove(test)) {
could_overflow = false;
// Relax the bounds to the constants we found. Not
// strictly necessary, but probably helpful to
// keep the expressions small.
a = Interval(lower_bound, upper_bound);
}
}
} else {
// a is bounded, but from and to can't necessarily represent
// each other; however, if the bounds can be simplified to
// constants, they might fit regardless of types.
a = simplify(a);
auto umin = as_const_uint(a.min);
auto umax = as_const_uint(a.max);
if (umin && umax && to.can_represent(*umin) && to.can_represent(*umax)) {
could_overflow = false;
} else {
auto imin = as_const_int(a.min);
auto imax = as_const_int(a.max);
if (imin && imax && to.can_represent(*imin) && to.can_represent(*imax)) {
could_overflow = false;
} else {
auto fmin = as_const_float(a.min);
auto fmax = as_const_float(a.max);
if (fmin && fmax && to.can_represent(*fmin) && to.can_represent(*fmax)) {
could_overflow = false;
}
}
}
}
}
if (!could_overflow) {
// Start with the bounds of the narrow type.
bounds_of_type(from);
// If we have a better min or max for the arg use that.
if (a.has_lower_bound()) {
interval.min = a.min;
}
if (a.has_upper_bound()) {
interval.max = a.max;
}
// Then cast those bounds to the wider type.
if (interval.has_lower_bound()) {
interval.min = Cast::make(to, interval.min);
}
if (interval.has_upper_bound()) {
interval.max = Cast::make(to, interval.max);
}
} else {
// This might overflow, so use the bounds of the destination type.
bounds_of_type(to);
}
}
void visit(const Variable *op) override {
TRACK_BOUNDS_INTERVAL;
TRACK_BOUNDS_INFO("name:", op->name);
if (const_bound) {
bounds_of_type(op->type);
if (const Interval *scope_interval = scope.find(op->name)) {
if (scope_interval->has_upper_bound() && is_const(scope_interval->max)) {
interval.max = Interval::make_min(interval.max, scope_interval->max);
}
if (scope_interval->has_lower_bound() && is_const(scope_interval->min)) {
interval.min = Interval::make_max(interval.min, scope_interval->min);
}
}
if (op->param.defined() &&
!op->param.is_buffer() &&
(op->param.min_value().defined() ||
op->param.max_value().defined())) {
if (op->param.max_value().defined() && is_const(op->param.max_value())) {
interval.max = Interval::make_min(interval.max, op->param.max_value());
}
if (op->param.min_value().defined() && is_const(op->param.min_value())) {
interval.min = Interval::make_max(interval.min, op->param.min_value());
}
}
} else {
if (const Interval *in = scope.find(op->name)) {
interval = *in;
} else if (op->type.is_vector()) {
// Uh oh, we need to take the min/max lane of some unknown vector. Treat as unbounded.
bounds_of_type(op->type);
} else {
interval = Interval::single_point(op);
}
}
}
void visit(const Add *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min + b.min);
} else {
bounds_of_type(op->type);
if (a.has_lower_bound() && b.has_lower_bound()) {
interval.min = a.min + b.min;
}
if (a.has_upper_bound() && b.has_upper_bound()) {
interval.max = a.max + b.max;
}
// Assume no overflow for float, int32, and int64
if (op->type.can_overflow()) {
if (!interval.is_bounded()) {
// Possibly infinite things that wrap can be anything.
bounds_of_type(op->type);
return;
}
// TODO(5682): Can't catch overflow of UInt(64) currently.
Type t = op->type.is_uint() ? UInt(64) : Int(32);
Expr no_overflow_max = (cast(t, a.max) + cast(t, b.max) == cast(t, interval.max));
Expr no_overflow_min = (cast(t, a.min) + cast(t, b.min) == cast(t, interval.min));
if (!can_prove(no_overflow_max && no_overflow_min)) {
bounds_of_type(op->type);
return;
}
}
}
}
void visit(const Sub *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min - b.min);
} else {
bounds_of_type(op->type);
if (a.has_lower_bound() && b.has_upper_bound()) {
interval.min = a.min - b.max;
}
if (a.has_upper_bound() && b.has_lower_bound()) {
interval.max = a.max - b.min;
}
// Assume no overflow for float, int32, and int64
if (op->type.can_overflow()) {
if (!interval.is_bounded()) {
// Possibly infinite things that wrap can be anything.
bounds_of_type(op->type);
return;
}
Expr no_overflow_max = (cast<int>(a.max) - cast<int>(b.min) == cast<int>(interval.max));
Expr no_overflow_min = (cast<int>(a.min) - cast<int>(b.max) == cast<int>(interval.min));
if (!can_prove(no_overflow_max && no_overflow_min)) {
bounds_of_type(op->type);
return;
}
}
// Check underflow for uint
if (op->type.is_uint() &&
interval.has_lower_bound() &&
!can_prove(b.max <= a.min)) {
bounds_of_type(op->type);
}
}
}
void visit(const Mul *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
// Move constants to the right
if (a.is_single_point() && !b.is_single_point()) {
std::swap(a, b);
}
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
return;
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min * b.min);
return;
} else if (b.is_single_point()) {
Expr e1 = a.has_lower_bound() ? a.min * b.min : a.min;
Expr e2 = a.has_upper_bound() ? a.max * b.min : a.max;
if (is_const_zero(b.min)) {
interval = b;
} else if (is_positive_const(b.min) || op->type.is_uint()) {
interval = Interval(e1, e2);
} else if (is_negative_const(b.min)) {
if (e1.same_as(Interval::neg_inf())) {
e1 = Interval::pos_inf();
}
if (e2.same_as(Interval::pos_inf())) {
e2 = Interval::neg_inf();
}
interval = Interval(e2, e1);
} else if (a.is_bounded()) {
// Sign of b is unknown
Expr cmp = b.min >= make_zero(b.min.type().element_of());
interval = Interval(select(cmp, e1, e2), select(cmp, e2, e1));
} else {
bounds_of_type(op->type);
}
} else if (a.is_bounded() && b.is_bounded()) {
interval = Interval::nothing();
interval.include(a.min * b.min);
interval.include(a.min * b.max);
interval.include(a.max * b.min);
interval.include(a.max * b.max);
} else {
bounds_of_type(op->type);
}
// Assume no overflow for float, int32, and int64
if (op->type.can_overflow()) {
if (a.is_bounded() && b.is_bounded()) {
// Try to prove it can't overflow. (Be sure to use uint32 for unsigned
// types so that the case of 65535*65535 won't misleadingly fail.)
// TODO(5682): Can't catch overflow of UInt(64) currently.
Type t = op->type.is_uint() ? UInt(64) : Int(32);
Expr test1 = (cast(t, a.min) * cast(t, b.min) == cast(t, a.min * b.min));
Expr test2 = (cast(t, a.min) * cast(t, b.max) == cast(t, a.min * b.max));
Expr test3 = (cast(t, a.max) * cast(t, b.min) == cast(t, a.max * b.min));
Expr test4 = (cast(t, a.max) * cast(t, b.max) == cast(t, a.max * b.max));
if (!can_prove(test1 && test2 && test3 && test4)) {
bounds_of_type(op->type);
}
} else {
bounds_of_type(op->type);
}
}
}
bool div_cannot_overflow(const Interval &a, const Interval &b, Type t) {
// No overflow if: not an allowed overflow int type, or `a` cannot be t.min() or
// `b` cannot be -1, because t.min() / -1 overflows for int16 and int8.
Expr neg_one = make_const(t, -1);
return !t.can_overflow_int() ||
(a.has_lower_bound() && can_prove(a.min != t.min())) ||
(b.has_upper_bound() && can_prove(b.max < neg_one)) ||
(b.has_lower_bound() && can_prove(b.min > neg_one));
}
void visit(const Div *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (!b.is_bounded()) {
// Integer division can only make things smaller in
// magnitude (but can flip the sign).
if (a.is_bounded() && op->type.is_int() && op->type.bits() >= 32) {
// Restrict to no-overflow types to avoid worrying
// about overflow due to negating the most negative int.
if (can_prove(a.min >= 0)) {
interval.min = -a.max;
interval.max = a.max;
} else if (can_prove(a.max <= 0)) {
interval.min = a.min;
interval.max = -a.min;
} else if (a.is_single_point()) {
// The following case would also be correct, but
// would duplicate the expression, which is
// generally a bad thing for any later interval
// arithmetic.
interval.min = -cast(a.min.type(), abs(a.min));
interval.max = cast(a.min.type(), abs(a.max));
} else {
// div by 0 is 0 and the magnitude cannot increase by integer division
interval.min = min(-a.max, a.min);
interval.max = max(-a.min, a.max);
}
} else {
bounds_of_type(op->type);
}
} else if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min / b.min);
} else if (can_prove(b.min == b.max)) {
Expr e1 = a.has_lower_bound() ? a.min / b.min : a.min;
Expr e2 = a.has_upper_bound() ? a.max / b.max : a.max;
Type t = op->type.element_of();
if (div_cannot_overflow(a, b, t)) {
// TODO: handle real numbers with can_prove(b.min > 0) and can_prove(b.min < 0) as well - treating floating point as
// reals can be error prone when dealing with division near 0, so for now we only consider integers in the can_prove() path
if (op->type.is_uint() || is_positive_const(b.min) || (op->type.is_int() && can_prove(b.min >= 0))) {
interval = Interval(e1, e2);
} else if (is_negative_const(b.min) || (op->type.is_int() && can_prove(b.min <= 0))) {
if (e1.same_as(Interval::neg_inf())) {
e1 = Interval::pos_inf();
}
if (e2.same_as(Interval::pos_inf())) {
e2 = Interval::neg_inf();
}
interval = Interval(e2, e1);
} else if (a.is_bounded()) {
// Sign of b is unknown.
Expr cmp = b.min > make_zero(b.min.type().element_of());
interval = Interval(select(cmp, e1, e2), select(cmp, e2, e1));
} else {
bounds_of_type(op->type);
}
} else {
// Overflow is possible because a can be min value of type t and
// b can be -1.
bounds_of_type(op->type);
}
} else if (a.is_bounded()) {
// if we can't statically prove that the divisor can't span zero, then we're unbounded
int min_sign = static_sign(b.min);
int max_sign = static_sign(b.max);
if (min_sign != max_sign || min_sign == 0 || max_sign == 0) {
if (op->type.is_int() && op->type.bits() >= 32) {
// Division can't make signed integers larger
// Restricted to 32-bits or greater to ensure the
// negation can't overflow.
interval = Interval::nothing();
interval.include(a.min);
interval.include(a.max);
interval.include(-a.min);
interval.include(-a.max);
} else if (op->type.is_uint()) {
// Division can't make unsigned integers large,
// but could make them arbitrarily small.
interval.min = make_zero(a.min.type());
interval.max = a.max;
} else {
// Division can make floats arbitrarily large, and
// we can't easily negate narrow bit-width signed
// integers because they just wrap.
bounds_of_type(op->type);
}
} else {
Type t = op->type.element_of();
if (div_cannot_overflow(a, b, t)) {
// Divisor is either strictly positive or strictly
// negative, so we can just take the extrema.
interval = Interval::nothing();
interval.include(a.min / b.min);
interval.include(a.max / b.min);
interval.include(a.min / b.max);
interval.include(a.max / b.max);
} else {
// Overflow is possible because a can be min value of type t and
// b can be -1.
bounds_of_type(op->type);
}
}
} else {
bounds_of_type(op->type);
}
}
void visit(const Mod *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
return;
}
Type t = op->type.element_of();
// Mod is always positive
interval.min = make_zero(t);
interval.max = Interval::pos_inf();
if (!b.is_bounded()) {
if (a.has_lower_bound() && can_prove(a.min >= 0)) {
// Mod cannot make positive values larger
interval.max = a.max;
}
} else {
// b is bounded
if (b.max.type().is_int_or_uint() && is_positive_const(b.min)) {
// If the RHS is >= 1, the result is in [0, max_b-1]
interval.max = b.max - make_one(t);
} else if (b.max.type().is_uint()) {
// if b.max = 0 then result is [0, 0], else [0, b.max - 1]
interval.max = select(b.max == make_zero(t), make_zero(t), b.max - make_one(t));
} else if (b.max.type().is_int()) {
// x % [4,10] -> [0,9]
// x % [-8,-3] -> [0,7]
// x % [-8, 10] -> [0,9]
interval.max = Max::make(interval.min, b.max - make_one(t));
interval.max = Max::make(interval.max, make_const(t, -1) - b.min);
} else if (b.max.type().is_float()) {
// The floating point version has the same sign rules,
// but can reach all the way up to the original value,
// so there's no -1.
interval.max = Max::make(b.max, -b.min);
}
}
}
void visit(const Min *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(Interval::make_min(a.min, b.min));
} else {
interval = Interval(Interval::make_min(a.min, b.min),
Interval::make_min(a.max, b.max));
}
}
void visit(const Max *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(Interval::make_max(a.min, b.min));
} else {
interval = Interval(Interval::make_max(a.min, b.min),
Interval::make_max(a.max, b.max));
}
}
// only used for LT and LE - GT and GE normalize to LT and LTE
template<typename Cmp>
void visit_compare(const Expr &a_expr, const Expr &b_expr) {
a_expr.accept(this);
if (!interval.has_upper_bound() && !interval.has_lower_bound()) {
bounds_of_type(Bool());
return;
}
Interval a = interval;
b_expr.accept(this);
if (!interval.has_upper_bound() && !interval.has_lower_bound()) {
bounds_of_type(Bool());
return;
}
Interval b = interval;
bounds_of_type(Bool());
// The returned interval should have the property that min <=
// val <= max. For integers it's clear what this means. For
// bools, treating false < true, '<=' is in fact
// implication. So we want conditions min and max such that
// min implies val implies max. So min should be a sufficient
// condition, and max should be a necessary condition.
// a.max <(=) b.min implies a <(=) b, so a <(=) b is at least
// as true as a.max <(=) b.min. This does not depend on a's
// lower bound or b's upper bound.
if (a.has_upper_bound() && b.has_lower_bound()) {
interval.min = Cmp::make(a.max, b.min);
}
// a <(=) b implies a.min <(=) b.max, so a <(=) b is at most
// as true as a.min <(=) b.max. This does not depend on a's
// upper bound or b's lower bound.
if (a.has_lower_bound() && b.has_upper_bound()) {
interval.max = Cmp::make(a.min, b.max);
}
}
void visit(const LT *op) override {
TRACK_BOUNDS_INTERVAL;
visit_compare<LT>(op->a, op->b);
}
void visit(const LE *op) override {
TRACK_BOUNDS_INTERVAL;
visit_compare<LE>(op->a, op->b);
}
void visit(const GT *op) override {
TRACK_BOUNDS_INTERVAL;
visit_compare<LT>(op->b, op->a);
}
void visit(const GE *op) override {
TRACK_BOUNDS_INTERVAL;
visit_compare<LE>(op->b, op->a);
}
void visit(const EQ *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min == b.min);
} else {
// If either vary, it could always be false, so we have no
// good sufficient condition.
bounds_of_type(op->type);
// But could it be true? A necessary condition is that the
// ranges overlap.
if (a.is_bounded() && b.is_bounded()) {
interval.max = a.min <= b.max && b.min <= a.max;
} else if (a.has_upper_bound() && b.has_lower_bound()) {
// a.min <= b.max is implied if a.min = -inf or b.max = +inf.
interval.max = b.min <= a.max;
} else if (a.has_lower_bound() && b.has_upper_bound()) {
// b.min <= a.max is implied if a.max = +inf or b.min = -inf.
interval.max = a.min <= b.max;
}
}
}
void visit(const NE *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min != b.min);
} else {
// If either vary, it could always be true that they're
// not equal, so we have no good necessary condition.
bounds_of_type(op->type);
// But we do have a sufficient condition. If the ranges of
// a and b do not overlap, then they must be not equal.
if (a.is_bounded() && b.is_bounded()) {
interval.min = a.min > b.max || b.min > a.max;
} else if (a.has_upper_bound() && b.has_lower_bound()) {
// a.min > b.max is false if a.min = -inf or b.max = +inf.
// a does not need a lower bound nor does b need
// an upper bound for this condition.
interval.min = b.min > a.max;
} else if (a.has_lower_bound() && b.has_upper_bound()) {
// b.min > a.max is false if a.max = +inf or b.min = -inf.
// a does not need an upper bound nor does b need
// a lower bound for this condition.
interval.min = a.min > b.max;
}
}
}
Expr make_and(Expr a, Expr b) {
if (is_const_one(a)) {
return b;
}
if (is_const_one(b)) {
return a;
}
if (is_const_zero(a)) {
return a;
}
if (is_const_zero(b)) {
return b;
}
return a && b;
}
void visit(const And *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min && b.min);
} else {
// And is monotonic increasing in both args
interval.min = make_and(a.min, b.min);
interval.max = make_and(a.max, b.max);
}
}
Expr make_or(Expr a, Expr b) {
if (is_const_one(a)) {
return a;
}
if (is_const_one(b)) {
return b;
}
if (is_const_zero(a)) {
return b;
}
if (is_const_zero(b)) {
return a;
}
return a || b;
}
void visit(const Or *op) override {
TRACK_BOUNDS_INTERVAL;
op->a.accept(this);
Interval a = interval;
op->b.accept(this);
Interval b = interval;
if (a.is_single_point(op->a) && b.is_single_point(op->b)) {
interval = Interval::single_point(op);
} else if (a.is_single_point() && b.is_single_point()) {
interval = Interval::single_point(a.min || b.min);