This is a follow-up to D130058 to fix how we handle the Max value we obtain from getValueRange(...) in IntExprEvaluator::VisitCastExpr(...) which in the case of an enum that contains an enumerator with the max integer value will overflow by one.
The fix is to decrement the value of Max and use slt and ult for comparison Vs sle and ule.
getValueRange(...) is also used by CodeGenFunction::EmitScalarRangeCheck(...) through getRangeForType(...) and this code does generate the correct IR for these values even though it appears to be logically doing the same operations that does not work in IntExprEvaluator::VisitCastExpr(...).
I don't think this is the correct answer. Even though the other use of this seems to 'work', getValueRange is still returning wrapped values here. The fix is to figure out how to fix the math in getValueRange, and change the sanitizer's IR generation if necessary.