Author: jvesely Date: Mon Oct 2 11:39:00 2017 New Revision: 314702 URL: http://llvm.org/viewvc/llvm-project?rev=314702&view=rev Log: integer/add_sat: Use clang builtin instead of llvm asm
reviewer: Tom Stellard Signed-off-by: Jan Vesely <jan.ves...@rutgers.edu> Removed: libclc/trunk/generic/lib/integer/add_sat_if.ll libclc/trunk/generic/lib/integer/add_sat_impl.ll libclc/trunk/ptx/lib/integer/add_sat.ll Modified: libclc/trunk/generic/lib/SOURCES libclc/trunk/generic/lib/integer/add_sat.cl libclc/trunk/ptx/lib/OVERRIDES libclc/trunk/ptx/lib/SOURCES Modified: libclc/trunk/generic/lib/SOURCES URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/SOURCES?rev=314702&r1=314701&r2=314702&view=diff ============================================================================== --- libclc/trunk/generic/lib/SOURCES (original) +++ libclc/trunk/generic/lib/SOURCES Mon Oct 2 11:39:00 2017 @@ -64,8 +64,6 @@ geometric/normalize.cl integer/abs.cl integer/abs_diff.cl integer/add_sat.cl -integer/add_sat_if.ll -integer/add_sat_impl.ll integer/clz.cl integer/hadd.cl integer/mad24.cl Modified: libclc/trunk/generic/lib/integer/add_sat.cl URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/integer/add_sat.cl?rev=314702&r1=314701&r2=314702&view=diff ============================================================================== --- libclc/trunk/generic/lib/integer/add_sat.cl (original) +++ libclc/trunk/generic/lib/integer/add_sat.cl Mon Oct 2 11:39:00 2017 @@ -12,35 +12,55 @@ _CLC_DECL long __clc_add_sat_s64(long, _CLC_DECL ulong __clc_add_sat_u64(ulong, ulong); _CLC_OVERLOAD _CLC_DEF char add_sat(char x, char y) { - return __clc_add_sat_s8(x, y); + short r = x + y; + return convert_char_sat(r); } _CLC_OVERLOAD _CLC_DEF uchar add_sat(uchar x, uchar y) { - return __clc_add_sat_u8(x, y); + ushort r = x + y; + return convert_uchar_sat(r); } _CLC_OVERLOAD _CLC_DEF short add_sat(short x, short y) { - return __clc_add_sat_s16(x, y); + int r = x + y; + return convert_short_sat(r); } _CLC_OVERLOAD _CLC_DEF ushort add_sat(ushort x, ushort y) { - return __clc_add_sat_u16(x, y); + uint r = x + y; + return convert_ushort_sat(r); } _CLC_OVERLOAD _CLC_DEF int add_sat(int x, int y) { - return __clc_add_sat_s32(x, y); + int r; + if (__builtin_sadd_overflow(x, y, &r)) + // The oveflow can only occur if both are pos or both are neg, + // thus we only need to check one operand + return x > 0 ? INT_MAX : INT_MIN; + return r; } _CLC_OVERLOAD _CLC_DEF uint add_sat(uint x, uint y) { - return __clc_add_sat_u32(x, y); + uint r; + if (__builtin_uadd_overflow(x, y, &r)) + return UINT_MAX; + return r; } _CLC_OVERLOAD _CLC_DEF long add_sat(long x, long y) { - return __clc_add_sat_s64(x, y); + long r; + if (__builtin_saddl_overflow(x, y, &r)) + // The oveflow can only occur if both are pos or both are neg, + // thus we only need to check one operand + return x > 0 ? LONG_MAX : LONG_MIN; + return r; } _CLC_OVERLOAD _CLC_DEF ulong add_sat(ulong x, ulong y) { - return __clc_add_sat_u64(x, y); + ulong r; + if (__builtin_uaddl_overflow(x, y, &r)) + return ULONG_MAX; + return r; } _CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, char, add_sat, char, char) Removed: libclc/trunk/generic/lib/integer/add_sat_if.ll URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/integer/add_sat_if.ll?rev=314701&view=auto ============================================================================== --- libclc/trunk/generic/lib/integer/add_sat_if.ll (original) +++ libclc/trunk/generic/lib/integer/add_sat_if.ll (removed) @@ -1,55 +0,0 @@ -declare i8 @__clc_add_sat_impl_s8(i8 %x, i8 %y) - -define i8 @__clc_add_sat_s8(i8 %x, i8 %y) nounwind readnone alwaysinline { - %call = call i8 @__clc_add_sat_impl_s8(i8 %x, i8 %y) - ret i8 %call -} - -declare i8 @__clc_add_sat_impl_u8(i8 %x, i8 %y) - -define i8 @__clc_add_sat_u8(i8 %x, i8 %y) nounwind readnone alwaysinline { - %call = call i8 @__clc_add_sat_impl_u8(i8 %x, i8 %y) - ret i8 %call -} - -declare i16 @__clc_add_sat_impl_s16(i16 %x, i16 %y) - -define i16 @__clc_add_sat_s16(i16 %x, i16 %y) nounwind readnone alwaysinline { - %call = call i16 @__clc_add_sat_impl_s16(i16 %x, i16 %y) - ret i16 %call -} - -declare i16 @__clc_add_sat_impl_u16(i16 %x, i16 %y) - -define i16 @__clc_add_sat_u16(i16 %x, i16 %y) nounwind readnone alwaysinline { - %call = call i16 @__clc_add_sat_impl_u16(i16 %x, i16 %y) - ret i16 %call -} - -declare i32 @__clc_add_sat_impl_s32(i32 %x, i32 %y) - -define i32 @__clc_add_sat_s32(i32 %x, i32 %y) nounwind readnone alwaysinline { - %call = call i32 @__clc_add_sat_impl_s32(i32 %x, i32 %y) - ret i32 %call -} - -declare i32 @__clc_add_sat_impl_u32(i32 %x, i32 %y) - -define i32 @__clc_add_sat_u32(i32 %x, i32 %y) nounwind readnone alwaysinline { - %call = call i32 @__clc_add_sat_impl_u32(i32 %x, i32 %y) - ret i32 %call -} - -declare i64 @__clc_add_sat_impl_s64(i64 %x, i64 %y) - -define i64 @__clc_add_sat_s64(i64 %x, i64 %y) nounwind readnone alwaysinline { - %call = call i64 @__clc_add_sat_impl_s64(i64 %x, i64 %y) - ret i64 %call -} - -declare i64 @__clc_add_sat_impl_u64(i64 %x, i64 %y) - -define i64 @__clc_add_sat_u64(i64 %x, i64 %y) nounwind readnone alwaysinline { - %call = call i64 @__clc_add_sat_impl_u64(i64 %x, i64 %y) - ret i64 %call -} Removed: libclc/trunk/generic/lib/integer/add_sat_impl.ll URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/integer/add_sat_impl.ll?rev=314701&view=auto ============================================================================== --- libclc/trunk/generic/lib/integer/add_sat_impl.ll (original) +++ libclc/trunk/generic/lib/integer/add_sat_impl.ll (removed) @@ -1,83 +0,0 @@ -declare {i8, i1} @llvm.sadd.with.overflow.i8(i8, i8) -declare {i8, i1} @llvm.uadd.with.overflow.i8(i8, i8) - -define i8 @__clc_add_sat_impl_s8(i8 %x, i8 %y) nounwind readnone alwaysinline { - %call = call {i8, i1} @llvm.sadd.with.overflow.i8(i8 %x, i8 %y) - %res = extractvalue {i8, i1} %call, 0 - %over = extractvalue {i8, i1} %call, 1 - %x.msb = ashr i8 %x, 7 - %x.limit = xor i8 %x.msb, 127 - %sat = select i1 %over, i8 %x.limit, i8 %res - ret i8 %sat -} - -define i8 @__clc_add_sat_impl_u8(i8 %x, i8 %y) nounwind readnone alwaysinline { - %call = call {i8, i1} @llvm.uadd.with.overflow.i8(i8 %x, i8 %y) - %res = extractvalue {i8, i1} %call, 0 - %over = extractvalue {i8, i1} %call, 1 - %sat = select i1 %over, i8 -1, i8 %res - ret i8 %sat -} - -declare {i16, i1} @llvm.sadd.with.overflow.i16(i16, i16) -declare {i16, i1} @llvm.uadd.with.overflow.i16(i16, i16) - -define i16 @__clc_add_sat_impl_s16(i16 %x, i16 %y) nounwind readnone alwaysinline { - %call = call {i16, i1} @llvm.sadd.with.overflow.i16(i16 %x, i16 %y) - %res = extractvalue {i16, i1} %call, 0 - %over = extractvalue {i16, i1} %call, 1 - %x.msb = ashr i16 %x, 15 - %x.limit = xor i16 %x.msb, 32767 - %sat = select i1 %over, i16 %x.limit, i16 %res - ret i16 %sat -} - -define i16 @__clc_add_sat_impl_u16(i16 %x, i16 %y) nounwind readnone alwaysinline { - %call = call {i16, i1} @llvm.uadd.with.overflow.i16(i16 %x, i16 %y) - %res = extractvalue {i16, i1} %call, 0 - %over = extractvalue {i16, i1} %call, 1 - %sat = select i1 %over, i16 -1, i16 %res - ret i16 %sat -} - -declare {i32, i1} @llvm.sadd.with.overflow.i32(i32, i32) -declare {i32, i1} @llvm.uadd.with.overflow.i32(i32, i32) - -define i32 @__clc_add_sat_impl_s32(i32 %x, i32 %y) nounwind readnone alwaysinline { - %call = call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %x, i32 %y) - %res = extractvalue {i32, i1} %call, 0 - %over = extractvalue {i32, i1} %call, 1 - %x.msb = ashr i32 %x, 31 - %x.limit = xor i32 %x.msb, 2147483647 - %sat = select i1 %over, i32 %x.limit, i32 %res - ret i32 %sat -} - -define i32 @__clc_add_sat_impl_u32(i32 %x, i32 %y) nounwind readnone alwaysinline { - %call = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y) - %res = extractvalue {i32, i1} %call, 0 - %over = extractvalue {i32, i1} %call, 1 - %sat = select i1 %over, i32 -1, i32 %res - ret i32 %sat -} - -declare {i64, i1} @llvm.sadd.with.overflow.i64(i64, i64) -declare {i64, i1} @llvm.uadd.with.overflow.i64(i64, i64) - -define i64 @__clc_add_sat_impl_s64(i64 %x, i64 %y) nounwind readnone alwaysinline { - %call = call {i64, i1} @llvm.sadd.with.overflow.i64(i64 %x, i64 %y) - %res = extractvalue {i64, i1} %call, 0 - %over = extractvalue {i64, i1} %call, 1 - %x.msb = ashr i64 %x, 63 - %x.limit = xor i64 %x.msb, 9223372036854775807 - %sat = select i1 %over, i64 %x.limit, i64 %res - ret i64 %sat -} - -define i64 @__clc_add_sat_impl_u64(i64 %x, i64 %y) nounwind readnone alwaysinline { - %call = call {i64, i1} @llvm.uadd.with.overflow.i64(i64 %x, i64 %y) - %res = extractvalue {i64, i1} %call, 0 - %over = extractvalue {i64, i1} %call, 1 - %sat = select i1 %over, i64 -1, i64 %res - ret i64 %sat -} Modified: libclc/trunk/ptx/lib/OVERRIDES URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/ptx/lib/OVERRIDES?rev=314702&r1=314701&r2=314702&view=diff ============================================================================== --- libclc/trunk/ptx/lib/OVERRIDES (original) +++ libclc/trunk/ptx/lib/OVERRIDES Mon Oct 2 11:39:00 2017 @@ -1,2 +1 @@ -integer/add_sat_if.ll integer/sub_sat_if.ll Modified: libclc/trunk/ptx/lib/SOURCES URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/ptx/lib/SOURCES?rev=314702&r1=314701&r2=314702&view=diff ============================================================================== --- libclc/trunk/ptx/lib/SOURCES (original) +++ libclc/trunk/ptx/lib/SOURCES Mon Oct 2 11:39:00 2017 @@ -1,2 +1 @@ -integer/add_sat.ll -integer/sub_sat.ll \ No newline at end of file +integer/sub_sat.ll Removed: libclc/trunk/ptx/lib/integer/add_sat.ll URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/ptx/lib/integer/add_sat.ll?rev=314701&view=auto ============================================================================== --- libclc/trunk/ptx/lib/integer/add_sat.ll (original) +++ libclc/trunk/ptx/lib/integer/add_sat.ll (removed) @@ -1,55 +0,0 @@ -declare i8 @__clc_add_sat_impl_s8(i8 %x, i8 %y) - -define ptx_device i8 @__clc_add_sat_s8(i8 %x, i8 %y) nounwind readnone alwaysinline { - %call = call i8 @__clc_add_sat_impl_s8(i8 %x, i8 %y) - ret i8 %call -} - -declare i8 @__clc_add_sat_impl_u8(i8 %x, i8 %y) - -define ptx_device i8 @__clc_add_sat_u8(i8 %x, i8 %y) nounwind readnone alwaysinline { - %call = call i8 @__clc_add_sat_impl_u8(i8 %x, i8 %y) - ret i8 %call -} - -declare i16 @__clc_add_sat_impl_s16(i16 %x, i16 %y) - -define ptx_device i16 @__clc_add_sat_s16(i16 %x, i16 %y) nounwind readnone alwaysinline { - %call = call i16 @__clc_add_sat_impl_s16(i16 %x, i16 %y) - ret i16 %call -} - -declare i16 @__clc_add_sat_impl_u16(i16 %x, i16 %y) - -define ptx_device i16 @__clc_add_sat_u16(i16 %x, i16 %y) nounwind readnone alwaysinline { - %call = call i16 @__clc_add_sat_impl_u16(i16 %x, i16 %y) - ret i16 %call -} - -declare i32 @__clc_add_sat_impl_s32(i32 %x, i32 %y) - -define ptx_device i32 @__clc_add_sat_s32(i32 %x, i32 %y) nounwind readnone alwaysinline { - %call = call i32 @__clc_add_sat_impl_s32(i32 %x, i32 %y) - ret i32 %call -} - -declare i32 @__clc_add_sat_impl_u32(i32 %x, i32 %y) - -define ptx_device i32 @__clc_add_sat_u32(i32 %x, i32 %y) nounwind readnone alwaysinline { - %call = call i32 @__clc_add_sat_impl_u32(i32 %x, i32 %y) - ret i32 %call -} - -declare i64 @__clc_add_sat_impl_s64(i64 %x, i64 %y) - -define ptx_device i64 @__clc_add_sat_s64(i64 %x, i64 %y) nounwind readnone alwaysinline { - %call = call i64 @__clc_add_sat_impl_s64(i64 %x, i64 %y) - ret i64 %call -} - -declare i64 @__clc_add_sat_impl_u64(i64 %x, i64 %y) - -define ptx_device i64 @__clc_add_sat_u64(i64 %x, i64 %y) nounwind readnone alwaysinline { - %call = call i64 @__clc_add_sat_impl_u64(i64 %x, i64 %y) - ret i64 %call -} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits