Skip to content

Emit == null instead of <= null for niche check #74533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/librustc_codegen_ssa/mir/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,13 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
bx.sub(tag, bx.cx().const_uint_big(niche_llty, niche_start))
};
let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
let is_niche = {
let relative_max = if relative_max == 0 {
// Avoid calling `const_uint`, which wouldn't work for pointers.
// FIXME(eddyb) check the actual primitive type here.
bx.cx().const_null(niche_llty)
} else {
bx.cx().const_uint(niche_llty, relative_max as u64)
};
let is_niche = if relative_max == 0 {
// Avoid calling `const_uint`, which wouldn't work for pointers.
// Also use canonical == 0 instead of non-canonical u<= 0.
// FIXME(eddyb) check the actual primitive type here.
bx.icmp(IntPredicate::IntEQ, relative_discr, bx.cx().const_null(niche_llty))
} else {
let relative_max = bx.cx().const_uint(niche_llty, relative_max as u64);
bx.icmp(IntPredicate::IntULE, relative_discr, relative_max)
};

Expand Down
25 changes: 25 additions & 0 deletions src/test/codegen/some-global-nonnull.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// compile-flags: -O

#![crate_type = "lib"]

// CHECK-LABEL: @test
// CHECK-NEXT: start:
// CHECK-NEXT: tail call void @ext_fn0()
#[no_mangle]
pub fn test() {
test_inner(Some(inner0));
}

fn test_inner(f_maybe: Option<fn()>) {
if let Some(f) = f_maybe {
f();
}
}

fn inner0() {
unsafe { ext_fn0() };
}

extern "C" {
fn ext_fn0();
}