Skip to content

Commit d70cd49

Browse files
committed
Auto merge of #38854 - Mark-Simulacrum:immediate-refactor, r=eddyb
Simplify type_is_immediate and type_is_fat_ptr r? @eddyb
2 parents 7789881 + 43cf5b9 commit d70cd49

File tree

2 files changed

+27
-47
lines changed

2 files changed

+27
-47
lines changed

src/librustc_trans/common.rs

+19-26
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,30 @@ use rustc_i128::u128;
4545
pub use context::{CrateContext, SharedCrateContext};
4646

4747
pub fn type_is_fat_ptr<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
48-
match ty.sty {
49-
ty::TyRawPtr(ty::TypeAndMut{ty, ..}) |
50-
ty::TyRef(_, ty::TypeAndMut{ty, ..}) |
51-
ty::TyBox(ty) => {
52-
!ccx.shared().type_is_sized(ty)
53-
}
54-
_ => {
55-
false
56-
}
48+
if let Layout::FatPointer { .. } = *ccx.layout_of(ty) {
49+
true
50+
} else {
51+
false
5752
}
5853
}
5954

6055
pub fn type_is_immediate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
61-
use machine::llsize_of_alloc;
62-
use type_of::sizing_type_of;
63-
64-
let simple = ty.is_scalar() ||
65-
ty.is_unique() || ty.is_region_ptr() ||
66-
ty.is_simd();
67-
if simple && !type_is_fat_ptr(ccx, ty) {
68-
return true;
69-
}
70-
if !ccx.shared().type_is_sized(ty) {
71-
return false;
72-
}
73-
match ty.sty {
74-
ty::TyAdt(..) | ty::TyTuple(..) | ty::TyArray(..) | ty::TyClosure(..) => {
75-
let llty = sizing_type_of(ccx, ty);
76-
llsize_of_alloc(ccx, llty) <= llsize_of_alloc(ccx, ccx.int_type())
56+
let layout = ccx.layout_of(ty);
57+
match *layout {
58+
Layout::CEnum { .. } |
59+
Layout::Scalar { .. } |
60+
Layout::Vector { .. } => true,
61+
62+
Layout::FatPointer { .. } => false,
63+
64+
Layout::Array { .. } |
65+
Layout::Univariant { .. } |
66+
Layout::General { .. } |
67+
Layout::UntaggedUnion { .. } |
68+
Layout::RawNullablePointer { .. } |
69+
Layout::StructWrappedNullablePointer { .. } => {
70+
!layout.is_unsized() && layout.size(&ccx.tcx().data_layout).bytes() == 0
7771
}
78-
_ => type_is_zero_size(ccx, ty)
7972
}
8073
}
8174

src/librustc_trans/mir/rvalue.rs

+8-21
Original file line numberDiff line numberDiff line change
@@ -260,37 +260,24 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
260260
let r_t_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
261261
let ll_t_in = type_of::immediate_type_of(bcx.ccx, operand.ty);
262262
let ll_t_out = type_of::immediate_type_of(bcx.ccx, cast_ty);
263-
let (llval, signed) = if let CastTy::Int(IntTy::CEnum) = r_t_in {
264-
let l = bcx.ccx.layout_of(operand.ty);
265-
let discr = match operand.val {
266-
OperandValue::Immediate(llval) => llval,
267-
OperandValue::Ref(llptr) => {
268-
adt::trans_get_discr(&bcx, operand.ty, llptr, None, true)
269-
}
270-
OperandValue::Pair(..) => bug!("Unexpected Pair operand")
271-
};
272-
let (signed, min, max) = match l {
273-
&Layout::CEnum { signed, min, max, .. } => {
274-
(signed, min, max)
275-
}
276-
_ => bug!("CEnum {:?} is not an enum", operand)
277-
};
278-
263+
let llval = operand.immediate();
264+
let l = bcx.ccx.layout_of(operand.ty);
265+
let signed = if let Layout::CEnum { signed, min, max, .. } = *l {
279266
if max > min {
280267
// We want `table[e as usize]` to not
281268
// have bound checks, and this is the most
282269
// convenient place to put the `assume`.
283270

284271
base::call_assume(&bcx, bcx.icmp(
285272
llvm::IntULE,
286-
discr,
287-
C_integral(common::val_ty(discr), max, false)
288-
))
273+
llval,
274+
C_integral(common::val_ty(llval), max, false)
275+
));
289276
}
290277

291-
(discr, signed)
278+
signed
292279
} else {
293-
(operand.immediate(), operand.ty.is_signed())
280+
operand.ty.is_signed()
294281
};
295282

296283
let newval = match (r_t_in, r_t_out) {

0 commit comments

Comments
 (0)