Skip to content

Commit 335bd8e

Browse files
committed
rustc: do not track non_zero in Layout.
1 parent 02276e9 commit 335bd8e

File tree

4 files changed

+51
-81
lines changed

4 files changed

+51
-81
lines changed

src/librustc/ty/layout.rs

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -838,27 +838,29 @@ impl<'a, 'tcx> Struct {
838838
-> Result<Option<(Size, Primitive)>, LayoutError<'tcx>> {
839839
let cx = (tcx, param_env);
840840
match (layout.layout, &layout.ty.sty) {
841-
(&Scalar { non_zero: true, value, .. }, _) => {
842-
Ok(Some((Size::from_bytes(0), value)))
841+
(&Scalar(Pointer), _) if !layout.ty.is_unsafe_ptr() => {
842+
Ok(Some((Size::from_bytes(0), Pointer)))
843843
}
844-
(&CEnum { non_zero: true, discr, .. }, _) => {
845-
Ok(Some((Size::from_bytes(0), Int(discr))))
844+
(&CEnum { discr, .. }, &ty::TyAdt(def, _)) => {
845+
if def.discriminants(tcx).all(|d| d.to_u128_unchecked() != 0) {
846+
Ok(Some((Size::from_bytes(0), Int(discr))))
847+
} else {
848+
Ok(None)
849+
}
846850
}
847851

848-
(&FatPointer { non_zero: true, .. }, _) => {
852+
(&FatPointer(_), _) if !layout.ty.is_unsafe_ptr() => {
849853
Ok(Some((layout.fields.offset(FAT_PTR_ADDR), Pointer)))
850854
}
851855

852856
// Is this the NonZero lang item wrapping a pointer or integer type?
853857
(_, &ty::TyAdt(def, _)) if Some(def.did) == tcx.lang_items().non_zero() => {
854858
let field = layout.field(cx, 0)?;
855859
match *field {
856-
// FIXME(eddyb) also allow floating-point types here.
857-
Scalar { value: value @ Int(_), non_zero: false } |
858-
Scalar { value: value @ Pointer, non_zero: false } => {
860+
Scalar(value) => {
859861
Ok(Some((layout.fields.offset(0), value)))
860862
}
861-
FatPointer { non_zero: false, .. } => {
863+
FatPointer(_) => {
862864
Ok(Some((layout.fields.offset(0) +
863865
field.fields.offset(FAT_PTR_ADDR),
864866
Pointer)))
@@ -1070,11 +1072,7 @@ pub enum Abi {
10701072
#[derive(Debug, PartialEq, Eq, Hash)]
10711073
pub enum Layout {
10721074
/// TyBool, TyChar, TyInt, TyUint, TyFloat, TyRawPtr, TyRef or TyFnPtr.
1073-
Scalar {
1074-
value: Primitive,
1075-
// If true, the value cannot represent a bit pattern of all zeroes.
1076-
non_zero: bool
1077-
},
1075+
Scalar(Primitive),
10781076

10791077
/// SIMD vectors, from structs marked with #[repr(simd)].
10801078
Vector {
@@ -1092,20 +1090,15 @@ pub enum Layout {
10921090
count: u64
10931091
},
10941092

1095-
/// TyRawPtr or TyRef with a !Sized pointee.
1096-
FatPointer {
1097-
metadata: Primitive,
1098-
/// If true, the pointer cannot be null.
1099-
non_zero: bool
1100-
},
1093+
/// TyRawPtr or TyRef with a !Sized pointee. The primitive is the metadata.
1094+
FatPointer(Primitive),
11011095

11021096
// Remaining variants are all ADTs such as structs, enums or tuples.
11031097

11041098
/// C-like enums; basically an integer.
11051099
CEnum {
11061100
discr: Integer,
11071101
signed: bool,
1108-
non_zero: bool,
11091102
/// Inclusive discriminant range.
11101103
/// If min > max, it represents min...u64::MAX followed by 0...max.
11111104
// FIXME(eddyb) always use the shortest range, e.g. by finding
@@ -1211,7 +1204,7 @@ impl<'a, 'tcx> Layout {
12111204
let success = |layout| {
12121205
let layout = tcx.intern_layout(layout);
12131206
let fields = match *layout {
1214-
Scalar { .. } |
1207+
Scalar(_) |
12151208
CEnum { .. } => {
12161209
FieldPlacement::union(0)
12171210
}
@@ -1258,7 +1251,7 @@ impl<'a, 'tcx> Layout {
12581251
}
12591252
};
12601253
let abi = match *layout {
1261-
Scalar { value, .. } => Abi::Scalar(value),
1254+
Scalar(value) => Abi::Scalar(value),
12621255
CEnum { discr, .. } => Abi::Scalar(Int(discr)),
12631256

12641257
Vector { .. } => Abi::Vector,
@@ -1286,43 +1279,36 @@ impl<'a, 'tcx> Layout {
12861279
assert!(!ty.has_infer_types());
12871280

12881281
let ptr_layout = |pointee: Ty<'tcx>| {
1289-
let non_zero = !ty.is_unsafe_ptr();
12901282
let pointee = tcx.normalize_associated_type_in_env(&pointee, param_env);
12911283
if pointee.is_sized(tcx, param_env, DUMMY_SP) {
1292-
Ok(Scalar { value: Pointer, non_zero })
1284+
Ok(Scalar(Pointer))
12931285
} else {
12941286
let unsized_part = tcx.struct_tail(pointee);
12951287
let metadata = match unsized_part.sty {
1296-
ty::TyForeign(..) => return Ok(Scalar { value: Pointer, non_zero }),
1288+
ty::TyForeign(..) => return Ok(Scalar(Pointer)),
12971289
ty::TySlice(_) | ty::TyStr => {
12981290
Int(dl.ptr_sized_integer())
12991291
}
13001292
ty::TyDynamic(..) => Pointer,
13011293
_ => return Err(LayoutError::Unknown(unsized_part))
13021294
};
1303-
Ok(FatPointer { metadata, non_zero })
1295+
Ok(FatPointer(metadata))
13041296
}
13051297
};
13061298

13071299
let layout = match ty.sty {
13081300
// Basic scalars.
1309-
ty::TyBool => Scalar { value: Int(I1), non_zero: false },
1310-
ty::TyChar => Scalar { value: Int(I32), non_zero: false },
1301+
ty::TyBool => Scalar(Int(I1)),
1302+
ty::TyChar => Scalar(Int(I32)),
13111303
ty::TyInt(ity) => {
1312-
Scalar {
1313-
value: Int(Integer::from_attr(dl, attr::SignedInt(ity))),
1314-
non_zero: false
1315-
}
1304+
Scalar(Int(Integer::from_attr(dl, attr::SignedInt(ity))))
13161305
}
13171306
ty::TyUint(ity) => {
1318-
Scalar {
1319-
value: Int(Integer::from_attr(dl, attr::UnsignedInt(ity))),
1320-
non_zero: false
1321-
}
1307+
Scalar(Int(Integer::from_attr(dl, attr::UnsignedInt(ity))))
13221308
}
1323-
ty::TyFloat(FloatTy::F32) => Scalar { value: F32, non_zero: false },
1324-
ty::TyFloat(FloatTy::F64) => Scalar { value: F64, non_zero: false },
1325-
ty::TyFnPtr(_) => Scalar { value: Pointer, non_zero: true },
1309+
ty::TyFloat(FloatTy::F32) => Scalar(F32),
1310+
ty::TyFloat(FloatTy::F64) => Scalar(F64),
1311+
ty::TyFnPtr(_) => Scalar(Pointer),
13261312

13271313
// The never type.
13281314
ty::TyNever => {
@@ -1430,7 +1416,7 @@ impl<'a, 'tcx> Layout {
14301416
ty::TyAdt(def, ..) if def.repr.simd() => {
14311417
let element = ty.simd_type(tcx);
14321418
match *cx.layout_of(element)? {
1433-
Scalar { value, .. } => {
1419+
Scalar(value) => {
14341420
return success(Vector {
14351421
element: value,
14361422
count: ty.simd_size(tcx) as u64
@@ -1456,12 +1442,9 @@ impl<'a, 'tcx> Layout {
14561442

14571443
if def.is_enum() && def.variants.iter().all(|v| v.fields.is_empty()) {
14581444
// All bodies empty -> intlike
1459-
let (mut min, mut max, mut non_zero) = (i64::max_value(),
1460-
i64::min_value(),
1461-
true);
1445+
let (mut min, mut max) = (i64::max_value(), i64::min_value());
14621446
for discr in def.discriminants(tcx) {
14631447
let x = discr.to_u128_unchecked() as i64;
1464-
if x == 0 { non_zero = false; }
14651448
if x < min { min = x; }
14661449
if x > max { max = x; }
14671450
}
@@ -1472,7 +1455,6 @@ impl<'a, 'tcx> Layout {
14721455
return success(CEnum {
14731456
discr,
14741457
signed,
1475-
non_zero,
14761458
// FIXME: should be u128?
14771459
min: min as u64,
14781460
max: max as u64
@@ -1699,7 +1681,7 @@ impl<'a, 'tcx> Layout {
16991681
/// Returns true if the layout corresponds to an unsized type.
17001682
pub fn is_unsized(&self) -> bool {
17011683
match *self {
1702-
Scalar {..} | Vector {..} | FatPointer {..} |
1684+
Scalar(_) | Vector {..} | FatPointer {..} |
17031685
CEnum {..} | UntaggedUnion {..} | General {..} |
17041686
NullablePointer {..} => false,
17051687

@@ -1712,7 +1694,7 @@ impl<'a, 'tcx> Layout {
17121694
let dl = cx.data_layout();
17131695

17141696
match *self {
1715-
Scalar { value, .. } => {
1697+
Scalar(value) => {
17161698
value.size(dl)
17171699
}
17181700

@@ -1734,7 +1716,7 @@ impl<'a, 'tcx> Layout {
17341716
}
17351717
}
17361718

1737-
FatPointer { metadata, .. } => {
1719+
FatPointer(metadata) => {
17381720
// Effectively a (ptr, meta) tuple.
17391721
(Pointer.size(dl).abi_align(metadata.align(dl)) +
17401722
metadata.size(dl)).abi_align(self.align(dl))
@@ -1755,7 +1737,7 @@ impl<'a, 'tcx> Layout {
17551737
let dl = cx.data_layout();
17561738

17571739
match *self {
1758-
Scalar { value, .. } => {
1740+
Scalar(value) => {
17591741
value.align(dl)
17601742
}
17611743

@@ -1769,7 +1751,7 @@ impl<'a, 'tcx> Layout {
17691751
dl.vector_align(vec_size)
17701752
}
17711753

1772-
FatPointer { metadata, .. } => {
1754+
FatPointer(metadata) => {
17731755
// Effectively a (ptr, meta) tuple.
17741756
Pointer.align(dl).max(metadata.align(dl))
17751757
}
@@ -1993,7 +1975,7 @@ impl<'a, 'tcx> Layout {
19931975

19941976
// other cases provide little interesting (i.e. adjustable
19951977
// via representation tweaks) size info beyond total size.
1996-
Layout::Scalar { .. } |
1978+
Layout::Scalar(_) |
19971979
Layout::Vector { .. } |
19981980
Layout::Array { .. } |
19991981
Layout::FatPointer { .. } => {
@@ -2421,9 +2403,8 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for Layout
24212403
mem::discriminant(self).hash_stable(hcx, hasher);
24222404

24232405
match *self {
2424-
Scalar { value, non_zero } => {
2406+
Scalar(ref value) => {
24252407
value.hash_stable(hcx, hasher);
2426-
non_zero.hash_stable(hcx, hasher);
24272408
}
24282409
Vector { element, count } => {
24292410
element.hash_stable(hcx, hasher);
@@ -2436,14 +2417,12 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for Layout
24362417
element_size.hash_stable(hcx, hasher);
24372418
count.hash_stable(hcx, hasher);
24382419
}
2439-
FatPointer { ref metadata, non_zero } => {
2420+
FatPointer(ref metadata) => {
24402421
metadata.hash_stable(hcx, hasher);
2441-
non_zero.hash_stable(hcx, hasher);
24422422
}
2443-
CEnum { discr, signed, non_zero, min, max } => {
2423+
CEnum { discr, signed, min, max } => {
24442424
discr.hash_stable(hcx, hasher);
24452425
signed.hash_stable(hcx, hasher);
2446-
non_zero.hash_stable(hcx, hasher);
24472426
min.hash_stable(hcx, hasher);
24482427
max.hash_stable(hcx, hasher);
24492428
}

src/librustc_trans/abi.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -468,17 +468,8 @@ impl<'a, 'tcx> ArgType<'tcx> {
468468
pub fn extend_integer_width_to(&mut self, bits: u64) {
469469
// Only integers have signedness
470470
let (i, signed) = match *self.layout {
471-
Layout::Scalar { value, .. } => {
472-
match value {
473-
layout::Int(i) => {
474-
if self.layout.ty.is_integral() {
475-
(i, self.layout.ty.is_signed())
476-
} else {
477-
return;
478-
}
479-
}
480-
_ => return
481-
}
471+
Layout::Scalar(layout::Int(i)) if self.layout.ty.is_integral() => {
472+
(i, self.layout.ty.is_signed())
482473
}
483474

484475
// Rust enum types that map onto C enums also need to follow

src/librustc_trans/cabi_s390x.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use abi::{FnType, ArgType, LayoutExt, Reg};
1515
use context::CrateContext;
1616

17-
use rustc::ty::layout::{self, Layout, FullLayout};
17+
use rustc::ty::layout::{self, FullLayout};
1818

1919
fn classify_ret_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ret: &mut ArgType<'tcx>) {
2020
if !ret.layout.is_aggregate() && ret.layout.size(ccx).bits() <= 64 {
@@ -26,11 +26,11 @@ fn classify_ret_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ret: &mut ArgType<'tc
2626

2727
fn is_single_fp_element<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
2828
layout: FullLayout<'tcx>) -> bool {
29-
match *layout {
30-
Layout::Scalar { value: layout::F32, .. } |
31-
Layout::Scalar { value: layout::F64, .. } => true,
32-
Layout::Univariant { .. } => {
33-
if layout.fields.count() == 1 {
29+
match layout.abi {
30+
layout::Abi::Scalar(layout::F32) |
31+
layout::Abi::Scalar(layout::F64) => true,
32+
layout::Abi::Aggregate => {
33+
if layout.fields.count() == 1 && layout.fields.offset(0).bytes() == 0 {
3434
is_single_fp_element(ccx, layout.field(ccx, 0))
3535
} else {
3636
false

src/librustc_trans/cabi_x86.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use abi::{ArgAttribute, FnType, LayoutExt, Reg, RegKind};
1212
use common::CrateContext;
1313

14-
use rustc::ty::layout::{self, Layout, FullLayout};
14+
use rustc::ty::layout::{self, FullLayout};
1515

1616
#[derive(PartialEq)]
1717
pub enum Flavor {
@@ -21,11 +21,11 @@ pub enum Flavor {
2121

2222
fn is_single_fp_element<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
2323
layout: FullLayout<'tcx>) -> bool {
24-
match *layout {
25-
Layout::Scalar { value: layout::F32, .. } |
26-
Layout::Scalar { value: layout::F64, .. } => true,
27-
Layout::Univariant { .. } => {
28-
if layout.fields.count() == 1 {
24+
match layout.abi {
25+
layout::Abi::Scalar(layout::F32) |
26+
layout::Abi::Scalar(layout::F64) => true,
27+
layout::Abi::Aggregate => {
28+
if layout.fields.count() == 1 && layout.fields.offset(0).bytes() == 0 {
2929
is_single_fp_element(ccx, layout.field(ccx, 0))
3030
} else {
3131
false

0 commit comments

Comments
 (0)