Skip to content
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

Simplify type_is_immediate and type_is_fat_ptr #38854

Merged
merged 4 commits into from
Jan 14, 2017
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
45 changes: 19 additions & 26 deletions src/librustc_trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,30 @@ use rustc_i128::u128;
pub use context::{CrateContext, SharedCrateContext};

pub fn type_is_fat_ptr<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
match ty.sty {
ty::TyRawPtr(ty::TypeAndMut{ty, ..}) |
ty::TyRef(_, ty::TypeAndMut{ty, ..}) |
ty::TyBox(ty) => {
!ccx.shared().type_is_sized(ty)
}
_ => {
false
}
if let Layout::FatPointer { .. } = *ccx.layout_of(ty) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check that this does not regress rustc performance - this fn is called quite often.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The layouts are cached and sometime during compilation the layout of any type will be requested anyway, so it seems fine to me?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do some benchmarking with regards to this, but it does feel needless because they are cached.

true
} else {
false
}
}

pub fn type_is_immediate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
use machine::llsize_of_alloc;
use type_of::sizing_type_of;

let simple = ty.is_scalar() ||
ty.is_unique() || ty.is_region_ptr() ||
ty.is_simd();
if simple && !type_is_fat_ptr(ccx, ty) {
return true;
}
if !ccx.shared().type_is_sized(ty) {
return false;
}
match ty.sty {
ty::TyAdt(..) | ty::TyTuple(..) | ty::TyArray(..) | ty::TyClosure(..) => {
let llty = sizing_type_of(ccx, ty);
llsize_of_alloc(ccx, llty) <= llsize_of_alloc(ccx, ccx.int_type())
let layout = ccx.layout_of(ty);
match *layout {
Layout::CEnum { .. } |
Layout::Scalar { .. } |
Layout::Vector { .. } => true,

Layout::FatPointer { .. } => false,

Layout::Array { .. } |
Layout::Univariant { .. } |
Layout::General { .. } |
Layout::UntaggedUnion { .. } |
Layout::RawNullablePointer { .. } |
Layout::StructWrappedNullablePointer { .. } => {
!layout.is_unsized() && layout.size(&ccx.tcx().data_layout).bytes() == 0
}
_ => type_is_zero_size(ccx, ty)
}
}

Expand Down
29 changes: 8 additions & 21 deletions src/librustc_trans/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,37 +260,24 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
let r_t_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
let ll_t_in = type_of::immediate_type_of(bcx.ccx, operand.ty);
let ll_t_out = type_of::immediate_type_of(bcx.ccx, cast_ty);
let (llval, signed) = if let CastTy::Int(IntTy::CEnum) = r_t_in {
let l = bcx.ccx.layout_of(operand.ty);
let discr = match operand.val {
OperandValue::Immediate(llval) => llval,
OperandValue::Ref(llptr) => {
adt::trans_get_discr(&bcx, operand.ty, llptr, None, true)
}
OperandValue::Pair(..) => bug!("Unexpected Pair operand")
};
let (signed, min, max) = match l {
&Layout::CEnum { signed, min, max, .. } => {
(signed, min, max)
}
_ => bug!("CEnum {:?} is not an enum", operand)
};

let llval = operand.immediate();
let l = bcx.ccx.layout_of(operand.ty);
let signed = if let Layout::CEnum { signed, min, max, .. } = *l {
if max > min {
// We want `table[e as usize]` to not
// have bound checks, and this is the most
// convenient place to put the `assume`.

base::call_assume(&bcx, bcx.icmp(
llvm::IntULE,
discr,
C_integral(common::val_ty(discr), max, false)
))
llval,
C_integral(common::val_ty(llval), max, false)
));
}

(discr, signed)
signed
} else {
(operand.immediate(), operand.ty.is_signed())
operand.ty.is_signed()
};

let newval = match (r_t_in, r_t_out) {
Expand Down