Skip to content

Commit 3dcc380

Browse files
authored
Check if expression is defined before trying to compute its constant_integer_bounds (#8599)
* Check if expression is defined before trying to compute its constant_integer_bounds * Fix &&
1 parent 813920f commit 3dcc380

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/FindIntrinsics.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -741,16 +741,18 @@ class FindIntrinsics : public IRMutator {
741741
bool is_saturated = op->value.as<Max>() || op->value.as<Min>();
742742
Expr a = lossless_cast(op->type, shift->args[0]);
743743
Expr b = lossless_cast(op->type.with_code(shift->args[1].type().code()), shift->args[1]);
744-
// Doing the shift in the narrower type might introduce UB where
745-
// there was no UB before, so we need to make sure b is bounded.
746-
auto b_bounds = constant_integer_bounds(b);
747-
const int max_shift = op->type.bits() - 1;
748-
749-
if (a.defined() && b.defined() && b_bounds >= -max_shift && b_bounds <= max_shift) {
750-
if (!is_saturated ||
751-
(shift->is_intrinsic(Call::rounding_shift_right) && can_prove(b >= 0)) ||
752-
(shift->is_intrinsic(Call::rounding_shift_left) && can_prove(b <= 0))) {
753-
return mutate(Call::make(op->type, shift->name, {a, b}, Call::PureIntrinsic));
744+
if (b.defined()) {
745+
// Doing the shift in the narrower type might introduce UB where
746+
// there was no UB before, so we need to make sure b is bounded.
747+
auto b_bounds = constant_integer_bounds(b);
748+
const int max_shift = op->type.bits() - 1;
749+
750+
if (a.defined() && b_bounds >= -max_shift && b_bounds <= max_shift) {
751+
if (!is_saturated ||
752+
(shift->is_intrinsic(Call::rounding_shift_right) && can_prove(b >= 0)) ||
753+
(shift->is_intrinsic(Call::rounding_shift_left) && can_prove(b <= 0))) {
754+
return mutate(Call::make(op->type, shift->name, {a, b}, Call::PureIntrinsic));
755+
}
754756
}
755757
}
756758
}

0 commit comments

Comments
 (0)