Skip to content

Commit 7043395

Browse files
committed
implement contains_zero method
1 parent d50abd0 commit 7043395

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,14 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
462462
load: &'ll Value,
463463
scalar: &abi::Scalar,
464464
) {
465-
let vr = scalar.valid_range;
466465
match scalar.value {
467466
abi::Int(..) => {
468467
let range = scalar.valid_range_exclusive(bx);
469468
if range.start != range.end {
470469
bx.range_metadata(load, range);
471470
}
472471
}
473-
abi::Pointer if vr.start < vr.end && !vr.contains(0) => {
472+
abi::Pointer if !scalar.valid_range.contains_zero() => {
474473
bx.nonnull_metadata(load);
475474
}
476475
_ => {}

compiler/rustc_middle/src/ty/layout.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2857,10 +2857,8 @@ where
28572857
return;
28582858
}
28592859

2860-
if scalar.valid_range.start < scalar.valid_range.end {
2861-
if scalar.valid_range.start > 0 {
2862-
attrs.set(ArgAttribute::NonNull);
2863-
}
2860+
if !scalar.valid_range.contains_zero() {
2861+
attrs.set(ArgAttribute::NonNull);
28642862
}
28652863

28662864
if let Some(pointee) = layout.pointee_info_at(cx, offset) {

compiler/rustc_target/src/abi/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl Primitive {
688688
///
689689
/// This is intended specifically to mirror LLVM’s `!range` metadata,
690690
/// semantics.
691-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
691+
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
692692
#[derive(HashStable_Generic)]
693693
pub struct AllocationRange {
694694
pub start: u128,
@@ -705,6 +705,13 @@ impl AllocationRange {
705705
self.start <= v || v <= self.end
706706
}
707707
}
708+
709+
/// Returns `true` if zero is contained in the range.
710+
/// Equal to `range.contains(0)` but should be faster.
711+
#[inline]
712+
pub fn contains_zero(&self) -> bool {
713+
!(self.start <= self.end && self.start != 0)
714+
}
708715
}
709716

710717
/// Information about one scalar component of a Rust type.
@@ -1222,9 +1229,8 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
12221229
{
12231230
let scalar_allows_raw_init = move |s: &Scalar| -> bool {
12241231
if zero {
1225-
let range = &s.valid_range;
12261232
// The range must contain 0.
1227-
range.contains(0) || (range.start > range.end) // wrap-around allows 0
1233+
s.valid_range.contains_zero()
12281234
} else {
12291235
// The range must include all values. `valid_range_exclusive` handles
12301236
// the wrap-around using target arithmetic; with wrap-around then the full

0 commit comments

Comments
 (0)