[ubsan] Skip overflow checks on safe arithmetic (fixes PR32874)
authorVedant Kumar <vsk@apple.com>
Tue, 2 May 2017 23:46:56 +0000 (23:46 +0000)
committerVedant Kumar <vsk@apple.com>
Tue, 2 May 2017 23:46:56 +0000 (23:46 +0000)
commitd919115983a85f1b772f90d945a4ee8360771c36
treed9985663621c1964229c3da1a0858b86904cdb0d
parent67736594f7cc1f789060a89f0bc981b58b66bca1
[ubsan] Skip overflow checks on safe arithmetic (fixes PR32874)

Currently, ubsan emits overflow checks for arithmetic that is known to
be safe at compile-time, e.g:

  1 + 1 => CheckedAdd(1, 1)

This leads to breakage when using the __builtin_prefetch intrinsic. LLVM
expects the arguments to @llvm.prefetch to be constant integers, and
when ubsan inserts unnecessary checks on the operands to the intrinsic,
this contract is broken, leading to verifier failures (see PR32874).

Instead of special-casing __builtin_prefetch for ubsan, this patch fixes
the underlying problem, i.e that clang currently emits unnecessary
overflow checks.

Testing: I ran the check-clang and check-ubsan targets with a stage2,
ubsan-enabled build of clang. I added a regression test for PR32874, and
some extra checking to make sure we don't regress runtime checking for
unsafe arithmetic. The existing ubsan-promoted-arithmetic.cpp test also
provides coverage for this change.

llvm-svn: 301988
clang/lib/CodeGen/CGExprScalar.cpp
clang/test/CodeGen/PR32874.c [new file with mode: 0644]