2015-10-21 16:42:25 -05:00
|
|
|
// Copyright 2012-2014 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.
|
|
|
|
|
2015-11-19 09:37:34 -06:00
|
|
|
use rustc::mir::repr as mir;
|
2016-06-07 09:28:36 -05:00
|
|
|
|
Various improvements to MIR and LLVM IR Construction
Primarily affects the MIR construction, which indirectly improves LLVM
IR generation, but some LLVM IR changes have been made too.
* Handle "statement expressions" more intelligently. These are
expressions that always evaluate to `()`. Previously a temporary would
be generated as a destination to translate into, which is unnecessary.
This affects assignment, augmented assignment, `return`, `break` and
`continue`.
* Avoid inserting drops for non-drop types in more places. Scheduled
drops were already skipped for types that we knew wouldn't need
dropping at construction time. However manually-inserted drops like
those for `x` in `x = y;` were still generated. `build_drop` now takes
a type parameter like its `schedule_drop` counterpart and checks to
see if the type needs dropping.
* Avoid generating an extra temporary for an assignment where the types
involved don't need dropping. Previously an expression like
`a = b + 1;` would result in a temporary for `b + 1`. This is so the
RHS can be evaluated, then the LHS evaluated and dropped and have
everything work correctly. However, this isn't necessary if the `LHS`
doesn't need a drop, as we can just overwrite the existing value.
* Improves lvalue analysis to allow treating an `Rvalue::Use` as an
operand in certain conditions. The reason for it never being an
operand is so it can be zeroed/drop-filled, but this is only true for
types that need dropping.
The first two changes result in significantly fewer MIR blocks being
generated, as previously almost every statement would end up generating
a new block due to the drop of the `()` temporary being generated.
2016-04-14 19:36:16 -05:00
|
|
|
use common::{self, BlockAndBuilder};
|
2015-10-21 16:42:25 -05:00
|
|
|
|
|
|
|
use super::MirContext;
|
2015-11-02 08:39:59 -06:00
|
|
|
use super::TempRef;
|
2015-10-21 16:42:25 -05:00
|
|
|
|
|
|
|
impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
|
|
|
|
pub fn trans_statement(&mut self,
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx: BlockAndBuilder<'bcx, 'tcx>,
|
2015-10-21 16:42:25 -05:00
|
|
|
statement: &mir::Statement<'tcx>)
|
2016-02-01 04:04:46 -06:00
|
|
|
-> BlockAndBuilder<'bcx, 'tcx> {
|
2015-10-21 16:42:25 -05:00
|
|
|
debug!("trans_statement(statement={:?})", statement);
|
|
|
|
|
2016-06-07 11:21:56 -05:00
|
|
|
let debug_loc = self.debug_loc(statement.source_info);
|
2016-04-07 14:35:11 -05:00
|
|
|
debug_loc.apply_to_bcx(&bcx);
|
|
|
|
debug_loc.apply(bcx.fcx());
|
2015-10-21 16:42:25 -05:00
|
|
|
match statement.kind {
|
|
|
|
mir::StatementKind::Assign(ref lvalue, ref rvalue) => {
|
2015-11-02 08:39:59 -06:00
|
|
|
match *lvalue {
|
|
|
|
mir::Lvalue::Temp(index) => {
|
2016-06-07 09:28:36 -05:00
|
|
|
match self.temps[index] {
|
2015-11-02 08:39:59 -06:00
|
|
|
TempRef::Lvalue(tr_dest) => {
|
2016-04-07 14:35:11 -05:00
|
|
|
self.trans_rvalue(bcx, tr_dest, rvalue, debug_loc)
|
2015-11-02 08:39:59 -06:00
|
|
|
}
|
|
|
|
TempRef::Operand(None) => {
|
2016-04-07 14:35:11 -05:00
|
|
|
let (bcx, operand) = self.trans_rvalue_operand(bcx, rvalue,
|
|
|
|
debug_loc);
|
2015-11-02 08:39:59 -06:00
|
|
|
self.temps[index] = TempRef::Operand(Some(operand));
|
|
|
|
bcx
|
|
|
|
}
|
|
|
|
TempRef::Operand(Some(_)) => {
|
2016-06-05 11:03:30 -05:00
|
|
|
let ty = self.lvalue_ty(lvalue);
|
Various improvements to MIR and LLVM IR Construction
Primarily affects the MIR construction, which indirectly improves LLVM
IR generation, but some LLVM IR changes have been made too.
* Handle "statement expressions" more intelligently. These are
expressions that always evaluate to `()`. Previously a temporary would
be generated as a destination to translate into, which is unnecessary.
This affects assignment, augmented assignment, `return`, `break` and
`continue`.
* Avoid inserting drops for non-drop types in more places. Scheduled
drops were already skipped for types that we knew wouldn't need
dropping at construction time. However manually-inserted drops like
those for `x` in `x = y;` were still generated. `build_drop` now takes
a type parameter like its `schedule_drop` counterpart and checks to
see if the type needs dropping.
* Avoid generating an extra temporary for an assignment where the types
involved don't need dropping. Previously an expression like
`a = b + 1;` would result in a temporary for `b + 1`. This is so the
RHS can be evaluated, then the LHS evaluated and dropped and have
everything work correctly. However, this isn't necessary if the `LHS`
doesn't need a drop, as we can just overwrite the existing value.
* Improves lvalue analysis to allow treating an `Rvalue::Use` as an
operand in certain conditions. The reason for it never being an
operand is so it can be zeroed/drop-filled, but this is only true for
types that need dropping.
The first two changes result in significantly fewer MIR blocks being
generated, as previously almost every statement would end up generating
a new block due to the drop of the `()` temporary being generated.
2016-04-14 19:36:16 -05:00
|
|
|
|
|
|
|
if !common::type_is_zero_size(bcx.ccx(), ty) {
|
2016-06-07 11:21:56 -05:00
|
|
|
span_bug!(statement.source_info.span,
|
Various improvements to MIR and LLVM IR Construction
Primarily affects the MIR construction, which indirectly improves LLVM
IR generation, but some LLVM IR changes have been made too.
* Handle "statement expressions" more intelligently. These are
expressions that always evaluate to `()`. Previously a temporary would
be generated as a destination to translate into, which is unnecessary.
This affects assignment, augmented assignment, `return`, `break` and
`continue`.
* Avoid inserting drops for non-drop types in more places. Scheduled
drops were already skipped for types that we knew wouldn't need
dropping at construction time. However manually-inserted drops like
those for `x` in `x = y;` were still generated. `build_drop` now takes
a type parameter like its `schedule_drop` counterpart and checks to
see if the type needs dropping.
* Avoid generating an extra temporary for an assignment where the types
involved don't need dropping. Previously an expression like
`a = b + 1;` would result in a temporary for `b + 1`. This is so the
RHS can be evaluated, then the LHS evaluated and dropped and have
everything work correctly. However, this isn't necessary if the `LHS`
doesn't need a drop, as we can just overwrite the existing value.
* Improves lvalue analysis to allow treating an `Rvalue::Use` as an
operand in certain conditions. The reason for it never being an
operand is so it can be zeroed/drop-filled, but this is only true for
types that need dropping.
The first two changes result in significantly fewer MIR blocks being
generated, as previously almost every statement would end up generating
a new block due to the drop of the `()` temporary being generated.
2016-04-14 19:36:16 -05:00
|
|
|
"operand {:?} already assigned",
|
|
|
|
rvalue);
|
|
|
|
} else {
|
|
|
|
// If the type is zero-sized, it's already been set here,
|
|
|
|
// but we still need to make sure we translate the operand
|
2016-04-16 00:38:18 -05:00
|
|
|
self.trans_rvalue_operand(bcx, rvalue, debug_loc).0
|
Various improvements to MIR and LLVM IR Construction
Primarily affects the MIR construction, which indirectly improves LLVM
IR generation, but some LLVM IR changes have been made too.
* Handle "statement expressions" more intelligently. These are
expressions that always evaluate to `()`. Previously a temporary would
be generated as a destination to translate into, which is unnecessary.
This affects assignment, augmented assignment, `return`, `break` and
`continue`.
* Avoid inserting drops for non-drop types in more places. Scheduled
drops were already skipped for types that we knew wouldn't need
dropping at construction time. However manually-inserted drops like
those for `x` in `x = y;` were still generated. `build_drop` now takes
a type parameter like its `schedule_drop` counterpart and checks to
see if the type needs dropping.
* Avoid generating an extra temporary for an assignment where the types
involved don't need dropping. Previously an expression like
`a = b + 1;` would result in a temporary for `b + 1`. This is so the
RHS can be evaluated, then the LHS evaluated and dropped and have
everything work correctly. However, this isn't necessary if the `LHS`
doesn't need a drop, as we can just overwrite the existing value.
* Improves lvalue analysis to allow treating an `Rvalue::Use` as an
operand in certain conditions. The reason for it never being an
operand is so it can be zeroed/drop-filled, but this is only true for
types that need dropping.
The first two changes result in significantly fewer MIR blocks being
generated, as previously almost every statement would end up generating
a new block due to the drop of the `()` temporary being generated.
2016-04-14 19:36:16 -05:00
|
|
|
}
|
2015-11-02 08:39:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2016-02-01 04:04:46 -06:00
|
|
|
let tr_dest = self.trans_lvalue(&bcx, lvalue);
|
2016-04-07 14:35:11 -05:00
|
|
|
self.trans_rvalue(bcx, tr_dest, rvalue, debug_loc)
|
2015-11-02 08:39:59 -06:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 16:42:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|