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

[MIR] Avoid some code generation for stores of ZST #30848

Merged
merged 1 commit into from
Jan 14, 2016
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
5 changes: 5 additions & 0 deletions src/librustc_trans/trans/mir/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
operand: OperandRef<'tcx>)
{
debug!("store_operand: operand={}", operand.repr(bcx));
// Avoid generating stores of zero-sized values, because the only way to have a zero-sized
// value is through `undef`, and store itself is useless.
if common::type_is_zero_size(bcx.ccx(), operand.ty) {
return;
}
match operand.val {
OperandValue::Ref(r) => base::memcpy_ty(bcx, lldest, r, operand.ty),
OperandValue::Immediate(s) => base::store_ty(bcx, s, lldest, operand.ty),
Expand Down
14 changes: 9 additions & 5 deletions src/librustc_trans/trans/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
},
_ => {
for (i, operand) in operands.iter().enumerate() {
// Note: perhaps this should be StructGep, but
// note that in some cases the values here will
// not be structs but arrays.
let lldest_i = build::GEPi(bcx, dest.llval, &[0, i]);
self.trans_operand_into(bcx, lldest_i, operand);
let op = self.trans_operand(bcx, operand);
// Do not generate stores and GEPis for zero-sized fields.
if !common::type_is_zero_size(bcx.ccx(), op.ty) {
// Note: perhaps this should be StructGep, but
// note that in some cases the values here will
// not be structs but arrays.
let dest = build::GEPi(bcx, dest.llval, &[0, i]);
self.store_operand(bcx, dest, op);
}
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/codegen/mir_zst_stores.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -C no-prepopulate-passes

#![feature(rustc_attrs)]
#![crate_type = "lib"]
use std::marker::PhantomData;


struct Zst { phantom: PhantomData<Zst> }

// CHECK-LABEL: @mir
#[no_mangle]
#[rustc_mir]
fn mir(){
// CHECK-NOT: getelementptr
// CHECK-NOT: store{{.*}}undef
let x = Zst { phantom: PhantomData };
}