llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.31k stars 12.11k forks source link

Example of missing overflow check folds #42517

Open preames opened 5 years ago

preames commented 5 years ago
Bugzilla Link 43172
Version trunk
OS Linux
CC @LebedevRI,@RKSimon,@nikic,@rofirrim,@rotateright

Extended Description

Here is a collection of overflow check idioms which we don't currently turn into the op.with.overflow equivalents (or more importantly, generate op w/following OF flag check on x86).

add i32, i32 %8 = add i32 %1, %0
%9 = xor i32 %8, %0
%10 = xor i32 %8, %1
%11 = and i32 %9, %10
%12 = icmp sgt i32 %11, -1

sub i32 i32 %8 = sub i32 %0, %1
%9 = xor i32 %1, %0
%10 = xor i32 %8, %0
%11 = and i32 %10, %9
%12 = icmp sgt i32 %11, -1

mul i32 i32 %8 = sext i32 %0 to i64
%9 = sext i32 %1 to i64
%10 = mul nsw i64 %9, %8
%11 = add nsw i64 %10, 2147483648
%12 = icmp ult i64 %11, 4294967296

increment i32 (i.e. add i32 1) %7 = icmp eq i32 %0, 2147483647 ...
%9 = add nsw i32 %0, 1
ret i32 %9

Note that in all cases, there's a branch on the overflow check leading to an exception block. I left that out for simplicity.

All of these are derived from https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/lang/Math.java opExact family of methods.

rotateright commented 5 years ago

cc'ing some potentially interested people from: https://reviews.llvm.org/D47927