[CodeGen] Specialize mixed-sign mul-with-overflow (fix PR34920)
authorVedant Kumar <vsk@apple.com>
Sat, 16 Dec 2017 01:28:25 +0000 (01:28 +0000)
committerVedant Kumar <vsk@apple.com>
Sat, 16 Dec 2017 01:28:25 +0000 (01:28 +0000)
commitfa5a0e59f0a54d6e9aec3a8333c796b743ca4d01
treeb7905bec51743c8c43f3dbd31b6e3d548b8b2c79
parent2ff24731bbf905faab6580de7c3e68092ca1ae17
[CodeGen] Specialize mixed-sign mul-with-overflow (fix PR34920)

This patch introduces a specialized way to lower overflow-checked
multiplications with mixed-sign operands. This fixes link failures and
ICEs on code like this:

  void mul(int64_t a, uint64_t b) {
    int64_t res;
    __builtin_mul_overflow(a, b, &res);
  }

The generic checked-binop irgen would use a 65-bit multiplication
intrinsic here, which requires runtime support for _muloti4 (128-bit
multiplication), and therefore fails to link on i386. To get an ICE
on x86_64, change the example to use __int128_t / __uint128_t.

Adding runtime and backend support for 65-bit or 129-bit checked
multiplication on all of our supported targets is infeasible.

This patch solves the problem by using simpler, specialized irgen for
the mixed-sign case.

llvm.org/PR34920, rdar://34963321

Testing: Apart from check-clang, I compared the output from this fairly
comprehensive test driver using unpatched & patched clangs:
https://gist.github.com/vedantk/3eb9c88f82e5c32f2e590555b4af5081

Differential Revision: https://reviews.llvm.org/D41149

llvm-svn: 320902
clang/lib/CodeGen/CGBuiltin.cpp
clang/test/CodeGen/builtins-overflow.c