Skip to content

Commit 7bcaf1a

Browse files
committed
Tell LLVM about NonZero integer types
Change the `get` method on NonZero integer types to insert an `assume`, so that LLVM can optimize better. Closes rust-lang#54868
1 parent b8b4150 commit 7bcaf1a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/libcore/num/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
8888
#[stable(feature = "nonzero", since = "1.28.0")]
8989
#[inline]
9090
pub fn get(self) -> $Int {
91-
self.0 .0
91+
let r = self.0 .0;
92+
unsafe { intrinsics::assume(r != 0) };
93+
r
9294
}
9395

9496
}

src/test/codegen/nonzero-assume.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -O
12+
13+
#![crate_type = "lib"]
14+
15+
use std::num::NonZeroU64;
16+
17+
// CHECK-LABEL: @assume_nonzero
18+
#[no_mangle]
19+
pub fn assume_nonzero(x: u64, y: NonZeroU64) -> u64 {
20+
x / y.get()
21+
// CHECK: icmp ne i64 %y, 0
22+
// CHECK: @llvm.assume
23+
}

0 commit comments

Comments
 (0)