Skip to content

Commit

Permalink
Tell LLVM about NonZero integer types
Browse files Browse the repository at this point in the history
Change the `get` method on NonZero integer types to insert an
`assume`, so that LLVM can optimize better.

Closes rust-lang#54868
  • Loading branch information
tromey committed Oct 11, 2018
1 parent b8b4150 commit 7bcaf1a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), 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
}

}
Expand Down
23 changes: 23 additions & 0 deletions src/test/codegen/nonzero-assume.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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
}

0 comments on commit 7bcaf1a

Please sign in to comment.