Skip to content

Commit a3ad012

Browse files
committed
Auto merge of #53286 - nagisa:cast-assumes, r=eddyb
Do not generate assumes for plain integer casts I gave up on making anything more elegant for now. r? @eddyb
2 parents 7de3dea + dc02a60 commit a3ad012

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/librustc_codegen_llvm/mir/rvalue.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ impl FunctionCx<'a, 'll, 'tcx> {
304304
// then `i1 1` (i.e. E::B) is effectively `i8 -1`.
305305
signed = !scalar.is_bool() && s;
306306

307-
if scalar.valid_range.end() > scalar.valid_range.start() {
307+
let er = scalar.valid_range_exclusive(bx.cx);
308+
if er.end != er.start &&
309+
scalar.valid_range.end() > scalar.valid_range.start() {
308310
// We want `table[e as usize]` to not
309311
// have bound checks, and this is the most
310312
// convenient place to put the `assume`.

src/librustc_target/abi/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,16 @@ pub struct Scalar {
596596
pub value: Primitive,
597597

598598
/// Inclusive wrap-around range of valid values, that is, if
599-
/// min > max, it represents min..=u128::MAX followed by 0..=max.
599+
/// start > end, it represents `start..=max_value()`,
600+
/// followed by `0..=end`.
601+
///
602+
/// That is, for an i8 primitive, a range of `254..=2` means following
603+
/// sequence:
604+
///
605+
/// 254 (-2), 255 (-1), 0, 1, 2
606+
///
607+
/// This is intended specifically to mirror LLVM’s `!range` metadata,
608+
/// semantics.
600609
// FIXME(eddyb) always use the shortest range, e.g. by finding
601610
// the largest space between two consecutive valid values and
602611
// taking everything else as the (shortest) valid range.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#![crate_type = "lib"]
12+
13+
// compile-flags: -Cno-prepopulate-passes
14+
15+
// CHECK-LABEL: fna
16+
#[no_mangle]
17+
pub fn fna(a: i16) -> i32 {
18+
a as i32
19+
// CHECK-NOT: assume
20+
// CHECK: sext
21+
}
22+
23+
// CHECK-LABEL: fnb
24+
#[no_mangle]
25+
pub fn fnb(a: u16) -> u32 {
26+
a as u32
27+
// CHECK-NOT: assume
28+
// CHECK: zext
29+
}

0 commit comments

Comments
 (0)