implement contains_zero method

This commit is contained in:
Andreas Liljeqvist 2021-08-23 14:20:38 +02:00
parent d50abd0249
commit 70433955f4
3 changed files with 12 additions and 9 deletions

View file

@ -462,7 +462,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
load: &'ll Value,
scalar: &abi::Scalar,
) {
let vr = scalar.valid_range;
match scalar.value {
abi::Int(..) => {
let range = scalar.valid_range_exclusive(bx);
@ -470,7 +469,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
bx.range_metadata(load, range);
}
}
abi::Pointer if vr.start < vr.end && !vr.contains(0) => {
abi::Pointer if !scalar.valid_range.contains_zero() => {
bx.nonnull_metadata(load);
}
_ => {}

View file

@ -2857,10 +2857,8 @@ where
return;
}
if scalar.valid_range.start < scalar.valid_range.end {
if scalar.valid_range.start > 0 {
attrs.set(ArgAttribute::NonNull);
}
if !scalar.valid_range.contains_zero() {
attrs.set(ArgAttribute::NonNull);
}
if let Some(pointee) = layout.pointee_info_at(cx, offset) {

View file

@ -688,7 +688,7 @@ impl Primitive {
///
/// This is intended specifically to mirror LLVMs `!range` metadata,
/// semantics.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[derive(HashStable_Generic)]
pub struct AllocationRange {
pub start: u128,
@ -705,6 +705,13 @@ impl AllocationRange {
self.start <= v || v <= self.end
}
}
/// Returns `true` if zero is contained in the range.
/// Equal to `range.contains(0)` but should be faster.
#[inline]
pub fn contains_zero(&self) -> bool {
!(self.start <= self.end && self.start != 0)
}
}
/// Information about one scalar component of a Rust type.
@ -1222,9 +1229,8 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
{
let scalar_allows_raw_init = move |s: &Scalar| -> bool {
if zero {
let range = &s.valid_range;
// The range must contain 0.
range.contains(0) || (range.start > range.end) // wrap-around allows 0
s.valid_range.contains_zero()
} else {
// The range must include all values. `valid_range_exclusive` handles
// the wrap-around using target arithmetic; with wrap-around then the full