https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103034

            Bug ID: 103034
           Summary: implement multiply with overflow
                    (__muloti4/__mulodi4/__mulosi4)
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libgcc
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ndesaulniers at google dot com
  Target Milestone: ---

LLVM's compiler-rt has support for 3 functions for doing multiplication with
overflow checks (__muloti4/__mulodi4/__mulosi4); libcalls to these were emitted
generally when doing multiplication with operands larger than the target's word
size with UBSAN enabled, or via calls to __builtin_mul_overflow().  These seem
to be a pretty potentially a win for code size.

Unfortunately, I had to scale back LLVM's codegen from emitting libcalls to
these; clang doesn't always use LLVM's compiler-rt for many platforms and
targets, so these libcalls are a bit of a portability mess ATM as they do not
exist in libgcc.

For example:
__int128_t a;
int main (void) {
  a = a * a;
  return 0;
}
compiled with -O2 -fsanitize=undefined comes out to about:
gcc-11: 41 instructions
clang-13: 36 instructions
clang-14: 73 instructions

They're generally useful and might be handy for libgcc to implement (then gcc
to implement libcalls to).

The implementations and tests for LLVM can be found in:
compiler-rt/lib/builtins/mulo{tds}i4.c
compiler-rt/lib/builtins/int_mulo_impl.inc
compiler-rt/test/builtins/Unit/mulo{tds}_test.c

Reply via email to