ubsan emits superfluous overflow checks for promoted arithmetic
| Bugzilla Link | 20193 |
| Version | unspecified |
| OS | Linux |
| CC | @kcc,@zygoloid,@vedantk |
Extended Description
Arithmetic on types that are smaller than 'int' cannot overflow, with the exception of SHRT_MIN * SHRT_MIN. Ubsan emits superfluous with.overflow intrinsics in this case anyway:
char min; int main() { return min + min; }
gives:
define i32 @main() #0 prefix <{ i32, i8* }> <{ i32 1413876459, i8* bitcast ({ i8*, i8* }* @_ZTIFivE to i8*) }> { entry: %0 = load i8* @min, align 1, !tbaa !1 %conv = sext i8 %0 to i32 %1 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %conv, i32 %conv) %2 = extractvalue { i32, i1 } %1, 0 %3 = extractvalue { i32, i1 } %1, 1 br i1 %3, label %handler.add_overflow, label %cont, !prof !4 ; ...
We also emit unnecessary overflow checks when one of the operands is a constant and the other is promoted. It would seem straightforward to look at the input value and check if it's obviously got multiple sign bits, and if so, figure out that the overflow is impossible.
With clang/r296213, ubsan should stop adding unnecessary overflow checks in promoted arithmetic. However, it will still always emit a check for cases in which one operand is a constant int, and the other is promoted.