diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index 782a7ba455984..31a4e380a3d9b 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -1240,6 +1240,21 @@ extern "rust-intrinsic" { /// y < 0 or y >= N, where N is the width of T in bits. pub fn unchecked_shr(x: T, y: T) -> T; + /// Returns the result of an unchecked addition, resulting in + /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`. + #[cfg(not(stage0))] + pub fn unchecked_add(x: T, y: T) -> T; + + /// Returns the result of an unchecked substraction, resulting in + /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`. + #[cfg(not(stage0))] + pub fn unchecked_sub(x: T, y: T) -> T; + + /// Returns the result of an unchecked multiplication, resulting in + /// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`. + #[cfg(not(stage0))] + pub fn unchecked_mul(x: T, y: T) -> T; + /// Performs rotate left. /// The stabilized versions of this intrinsic are available on the integer /// primitives via the `rotate_left` method. For example, diff --git a/src/librustc_codegen_llvm/builder.rs b/src/librustc_codegen_llvm/builder.rs index f37fd0cb83382..102e9e38612e0 100644 --- a/src/librustc_codegen_llvm/builder.rs +++ b/src/librustc_codegen_llvm/builder.rs @@ -265,6 +265,12 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { neg(x) => LLVMBuildNeg, fneg(x) => LLVMBuildFNeg, not(x) => LLVMBuildNot, + unchecked_sadd(x, y) => LLVMBuildNSWAdd, + unchecked_uadd(x, y) => LLVMBuildNUWAdd, + unchecked_ssub(x, y) => LLVMBuildNSWSub, + unchecked_usub(x, y) => LLVMBuildNUWSub, + unchecked_smul(x, y) => LLVMBuildNSWMul, + unchecked_umul(x, y) => LLVMBuildNUWMul, } fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value { diff --git a/src/librustc_codegen_llvm/intrinsic.rs b/src/librustc_codegen_llvm/intrinsic.rs index 98a829b2affec..42aa9989346fc 100644 --- a/src/librustc_codegen_llvm/intrinsic.rs +++ b/src/librustc_codegen_llvm/intrinsic.rs @@ -334,7 +334,8 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> { "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" | "ctpop" | "bswap" | "bitreverse" | "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" | "overflowing_add" | "overflowing_sub" | "overflowing_mul" | - "unchecked_div" | "unchecked_rem" | "unchecked_shl" | "unchecked_shr" | "exact_div" | + "unchecked_div" | "unchecked_rem" | "unchecked_shl" | "unchecked_shr" | + "unchecked_add" | "unchecked_sub" | "unchecked_mul" | "exact_div" | "rotate_left" | "rotate_right" | "saturating_add" | "saturating_sub" => { let ty = arg_tys[0]; match int_type_width_signed(ty, self) { @@ -430,6 +431,27 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> { } else { self.lshr(args[0].immediate(), args[1].immediate()) }, + "unchecked_add" => { + if signed { + self.unchecked_sadd(args[0].immediate(), args[1].immediate()) + } else { + self.unchecked_uadd(args[0].immediate(), args[1].immediate()) + } + }, + "unchecked_sub" => { + if signed { + self.unchecked_ssub(args[0].immediate(), args[1].immediate()) + } else { + self.unchecked_usub(args[0].immediate(), args[1].immediate()) + } + }, + "unchecked_mul" => { + if signed { + self.unchecked_smul(args[0].immediate(), args[1].immediate()) + } else { + self.unchecked_umul(args[0].immediate(), args[1].immediate()) + } + }, "rotate_left" | "rotate_right" => { let is_left = name == "rotate_left"; let val = args[0].immediate(); diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs index f88923fc9f1c5..a71243c7c826b 100644 --- a/src/librustc_codegen_llvm/llvm/ffi.rs +++ b/src/librustc_codegen_llvm/llvm/ffi.rs @@ -1002,6 +1002,36 @@ extern "C" { RHS: &'a Value, Name: *const c_char) -> &'a Value; + pub fn LLVMBuildNSWAdd(B: &Builder<'a>, + LHS: &'a Value, + RHS: &'a Value, + Name: *const c_char) + -> &'a Value; + pub fn LLVMBuildNUWAdd(B: &Builder<'a>, + LHS: &'a Value, + RHS: &'a Value, + Name: *const c_char) + -> &'a Value; + pub fn LLVMBuildNSWSub(B: &Builder<'a>, + LHS: &'a Value, + RHS: &'a Value, + Name: *const c_char) + -> &'a Value; + pub fn LLVMBuildNUWSub(B: &Builder<'a>, + LHS: &'a Value, + RHS: &'a Value, + Name: *const c_char) + -> &'a Value; + pub fn LLVMBuildNSWMul(B: &Builder<'a>, + LHS: &'a Value, + RHS: &'a Value, + Name: *const c_char) + -> &'a Value; + pub fn LLVMBuildNUWMul(B: &Builder<'a>, + LHS: &'a Value, + RHS: &'a Value, + Name: *const c_char) + -> &'a Value; pub fn LLVMBuildAnd(B: &Builder<'a>, LHS: &'a Value, RHS: &'a Value, diff --git a/src/librustc_codegen_ssa/traits/builder.rs b/src/librustc_codegen_ssa/traits/builder.rs index 0c4c4547a7955..a11d1ba9231cc 100644 --- a/src/librustc_codegen_ssa/traits/builder.rs +++ b/src/librustc_codegen_ssa/traits/builder.rs @@ -88,6 +88,12 @@ pub trait BuilderMethods<'a, 'tcx: 'a>: fn shl(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; fn lshr(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; fn ashr(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; + fn unchecked_sadd(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; + fn unchecked_uadd(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; + fn unchecked_ssub(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; + fn unchecked_usub(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; + fn unchecked_smul(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; + fn unchecked_umul(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; fn and(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; fn or(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; fn xor(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value; diff --git a/src/librustc_typeck/check/intrinsic.rs b/src/librustc_typeck/check/intrinsic.rs index c6191e6b579ce..0b14ff1db59d4 100644 --- a/src/librustc_typeck/check/intrinsic.rs +++ b/src/librustc_typeck/check/intrinsic.rs @@ -305,7 +305,8 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, "unchecked_shl" | "unchecked_shr" | "rotate_left" | "rotate_right" => (1, vec![param(0), param(0)], param(0)), - + "unchecked_add" | "unchecked_sub" | "unchecked_mul" => + (1, vec![param(0), param(0)], param(0)), "overflowing_add" | "overflowing_sub" | "overflowing_mul" => (1, vec![param(0), param(0)], param(0)), "saturating_add" | "saturating_sub" => diff --git a/src/test/codegen/unchecked_math.rs b/src/test/codegen/unchecked_math.rs new file mode 100644 index 0000000000000..419c120ede9ec --- /dev/null +++ b/src/test/codegen/unchecked_math.rs @@ -0,0 +1,46 @@ +#![crate_type = "lib"] +#![feature(core_intrinsics)] + +use std::intrinsics::*; + +// CHECK-LABEL: @unchecked_add_signed +#[no_mangle] +pub unsafe fn unchecked_add_signed(a: i32, b: i32) -> i32 { + // CHECK: add nsw + unchecked_add(a, b) +} + +// CHECK-LABEL: @unchecked_add_unsigned +#[no_mangle] +pub unsafe fn unchecked_add_unsigned(a: u32, b: u32) -> u32 { + // CHECK: add nuw + unchecked_add(a, b) +} + +// CHECK-LABEL: @unchecked_sub_signed +#[no_mangle] +pub unsafe fn unchecked_sub_signed(a: i32, b: i32) -> i32 { + // CHECK: sub nsw + unchecked_sub(a, b) +} + +// CHECK-LABEL: @unchecked_sub_unsigned +#[no_mangle] +pub unsafe fn unchecked_sub_unsigned(a: u32, b: u32) -> u32 { + // CHECK: sub nuw + unchecked_sub(a, b) +} + +// CHECK-LABEL: @unchecked_mul_signed +#[no_mangle] +pub unsafe fn unchecked_mul_signed(a: i32, b: i32) -> i32 { + // CHECK: mul nsw + unchecked_mul(a, b) +} + +// CHECK-LABEL: @unchecked_mul_unsigned +#[no_mangle] +pub unsafe fn unchecked_mul_unsigned(a: u32, b: u32) -> u32 { + // CHECK: mul nuw + unchecked_mul(a, b) +} diff --git a/src/test/ui/intrinsics/unchecked_math_unsafe.rs b/src/test/ui/intrinsics/unchecked_math_unsafe.rs new file mode 100644 index 0000000000000..a034b45f5308c --- /dev/null +++ b/src/test/ui/intrinsics/unchecked_math_unsafe.rs @@ -0,0 +1,8 @@ +#![feature(core_intrinsics)] + +fn main() { + let (x, y) = (1u32, 2u32); + let add = std::intrinsics::unchecked_add(x, y); //~ ERROR call to unsafe function + let sub = std::intrinsics::unchecked_sub(x, y); //~ ERROR call to unsafe function + let mul = std::intrinsics::unchecked_mul(x, y); //~ ERROR call to unsafe function +} diff --git a/src/test/ui/intrinsics/unchecked_math_unsafe.stderr b/src/test/ui/intrinsics/unchecked_math_unsafe.stderr new file mode 100644 index 0000000000000..4066cf8efb8c1 --- /dev/null +++ b/src/test/ui/intrinsics/unchecked_math_unsafe.stderr @@ -0,0 +1,27 @@ +error[E0133]: call to unsafe function is unsafe and requires unsafe function or block + --> $DIR/unchecked_math_unsafe.rs:5:15 + | +LL | let add = std::intrinsics::unchecked_add(x, y); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior + +error[E0133]: call to unsafe function is unsafe and requires unsafe function or block + --> $DIR/unchecked_math_unsafe.rs:6:15 + | +LL | let sub = std::intrinsics::unchecked_sub(x, y); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior + +error[E0133]: call to unsafe function is unsafe and requires unsafe function or block + --> $DIR/unchecked_math_unsafe.rs:7:15 + | +LL | let mul = std::intrinsics::unchecked_mul(x, y); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0133`. diff --git a/src/test/ui/intrinsics/unchecked_math_unstable.rs b/src/test/ui/intrinsics/unchecked_math_unstable.rs new file mode 100644 index 0000000000000..8869063d1cc3d --- /dev/null +++ b/src/test/ui/intrinsics/unchecked_math_unstable.rs @@ -0,0 +1,8 @@ +fn main() { + let (x, y) = (1u32, 2u32); + unsafe { + let add = std::intrinsics::unchecked_add(x, y); //~ ERROR use of unstable library feature + let sub = std::intrinsics::unchecked_sub(x, y); //~ ERROR use of unstable library feature + let mul = std::intrinsics::unchecked_mul(x, y); //~ ERROR use of unstable library feature + } +} diff --git a/src/test/ui/intrinsics/unchecked_math_unstable.stderr b/src/test/ui/intrinsics/unchecked_math_unstable.stderr new file mode 100644 index 0000000000000..6f5429127c690 --- /dev/null +++ b/src/test/ui/intrinsics/unchecked_math_unstable.stderr @@ -0,0 +1,27 @@ +error[E0658]: use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library + --> $DIR/unchecked_math_unstable.rs:4:19 + | +LL | let add = std::intrinsics::unchecked_add(x, y); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(core_intrinsics)] to the crate attributes to enable + +error[E0658]: use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library + --> $DIR/unchecked_math_unstable.rs:5:19 + | +LL | let sub = std::intrinsics::unchecked_sub(x, y); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(core_intrinsics)] to the crate attributes to enable + +error[E0658]: use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library + --> $DIR/unchecked_math_unstable.rs:6:19 + | +LL | let mul = std::intrinsics::unchecked_mul(x, y); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(core_intrinsics)] to the crate attributes to enable + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0658`.