diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 772502cc800e..60ecb3ffb6ca 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -88,7 +88,9 @@ assert_eq!(size_of::>(), size_of::<", st #[stable(feature = "nonzero", since = "1.28.0")] #[inline] pub fn get(self) -> $Int { - self.0 .0 + let r = self.0 .0; + unsafe { intrinsics::assume(r != 0) }; + r } } diff --git a/src/test/codegen/nonzero-assume.rs b/src/test/codegen/nonzero-assume.rs new file mode 100644 index 000000000000..a14e8740789b --- /dev/null +++ b/src/test/codegen/nonzero-assume.rs @@ -0,0 +1,23 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -O + +#![crate_type = "lib"] + +use std::num::NonZeroU64; + +// CHECK-LABEL: @assume_nonzero +#[no_mangle] +pub fn assume_nonzero(x: u64, y: NonZeroU64) -> u64 { + x / y.get() + // CHECK: icmp ne i64 %y, 0 + // CHECK: @llvm.assume +}