2016-05-07 19:14:28 +03:00
|
|
|
//! A pass that promotes borrows of constant rvalues.
|
|
|
|
//!
|
|
|
|
//! The rvalues considered constant are trees of temps,
|
|
|
|
//! each with exactly one initialization, and holding
|
|
|
|
//! a constant value with no interior mutability.
|
|
|
|
//! They are placed into a new MIR constant body in
|
|
|
|
//! `promoted` and the borrow rvalue is replaced with
|
|
|
|
//! a `Literal::Promoted` using the index into `promoted`
|
|
|
|
//! of that constant MIR.
|
|
|
|
//!
|
|
|
|
//! This pass assumes that every use is dominated by an
|
|
|
|
//! initialization and can otherwise silence errors, if
|
|
|
|
//! move analysis runs after promotion on broken MIR.
|
|
|
|
|
2020-05-07 10:24:20 -07:00
|
|
|
use rustc_hir as hir;
|
2022-09-19 19:46:53 +02:00
|
|
|
use rustc_middle::mir;
|
2022-04-28 11:31:08 +08:00
|
|
|
use rustc_middle::mir::traversal::ReversePostorderIter;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
|
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::subst::InternalSubsts;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, List, TyCtxt, TypeVisitableExt};
|
2020-10-25 18:06:41 +01:00
|
|
|
use rustc_span::Span;
|
2016-05-07 19:14:28 +03:00
|
|
|
|
2023-03-31 00:32:44 -07:00
|
|
|
use rustc_index::vec::{Idx, IndexSlice, IndexVec};
|
2016-06-07 17:28:36 +03:00
|
|
|
|
2019-10-28 15:32:56 -07:00
|
|
|
use std::cell::Cell;
|
2020-04-04 17:25:45 +02:00
|
|
|
use std::{cmp, iter, mem};
|
2016-05-07 19:14:28 +03:00
|
|
|
|
2021-10-25 17:07:16 +01:00
|
|
|
use crate::transform::check_consts::{qualifs, ConstCx};
|
2019-10-21 21:18:12 +03:00
|
|
|
|
2019-10-28 15:32:56 -07:00
|
|
|
/// A `MirPass` for promotion.
|
|
|
|
///
|
2021-05-09 14:21:33 +02:00
|
|
|
/// Promotion is the extraction of promotable temps into separate MIR bodies so they can have
|
|
|
|
/// `'static` lifetime.
|
2019-10-28 15:32:56 -07:00
|
|
|
///
|
|
|
|
/// After this pass is run, `promoted_fragments` will hold the MIR body corresponding to each
|
2019-12-11 01:53:46 -03:00
|
|
|
/// newly created `Constant`.
|
2019-10-28 15:32:56 -07:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct PromoteTemps<'tcx> {
|
2020-04-12 10:31:00 -07:00
|
|
|
pub promoted_fragments: Cell<IndexVec<Promoted, Body<'tcx>>>,
|
2019-10-28 15:32:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
|
2020-10-04 11:01:38 -07:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2019-10-28 15:32:56 -07:00
|
|
|
// There's not really any point in promoting errorful MIR.
|
|
|
|
//
|
|
|
|
// This does not include MIR that failed const-checking, which we still try to promote.
|
2022-11-03 09:22:08 +08:00
|
|
|
if let Err(_) = body.return_ty().error_reported() {
|
|
|
|
debug!("PromoteTemps: MIR had errors");
|
2019-10-28 15:32:56 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-10-04 11:01:38 -07:00
|
|
|
if body.source.promoted.is_some() {
|
2019-10-28 15:32:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut rpo = traversal::reverse_postorder(body);
|
2020-10-04 15:22:23 -07:00
|
|
|
let ccx = ConstCx::new(tcx, body);
|
2022-05-07 21:02:25 +08:00
|
|
|
let (mut temps, all_candidates) = collect_temps_and_candidates(&ccx, &mut rpo);
|
2019-10-28 15:32:56 -07:00
|
|
|
|
2022-05-07 21:02:25 +08:00
|
|
|
let promotable_candidates = validate_candidates(&ccx, &mut temps, &all_candidates);
|
2019-10-28 15:32:56 -07:00
|
|
|
|
2020-10-04 16:59:19 -07:00
|
|
|
let promoted = promote_candidates(body, tcx, temps, promotable_candidates);
|
2019-11-22 10:58:58 -08:00
|
|
|
self.promoted_fragments.set(promoted);
|
2019-10-28 15:32:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 19:14:28 +03:00
|
|
|
/// State of a temporary during collection and promotion.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum TempState {
|
|
|
|
/// No references to this temp.
|
|
|
|
Undefined,
|
|
|
|
/// One direct assignment and any number of direct uses.
|
|
|
|
/// A borrow of this temp is promotable if the assigned
|
|
|
|
/// value is qualified as constant.
|
2022-05-09 17:13:30 +08:00
|
|
|
Defined { location: Location, uses: usize, valid: Result<(), ()> },
|
2016-05-07 19:14:28 +03:00
|
|
|
/// Any other combination of assignments/uses.
|
|
|
|
Unpromotable,
|
|
|
|
/// This temp was part of an rvalue which got extracted
|
|
|
|
/// during promotion and needs cleanup.
|
|
|
|
PromotedOut,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TempState {
|
|
|
|
pub fn is_promotable(&self) -> bool {
|
2018-10-26 12:11:19 +02:00
|
|
|
debug!("is_promotable: self={:?}", self);
|
2020-12-28 22:44:04 +01:00
|
|
|
matches!(self, TempState::Defined { .. })
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A "root candidate" for promotion, which will become the
|
|
|
|
/// returned value in a promoted MIR, unless it's a subset
|
|
|
|
/// of a larger candidate.
|
2019-08-21 23:31:58 +03:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2021-11-05 00:00:00 +00:00
|
|
|
pub struct Candidate {
|
|
|
|
location: Location,
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 23:31:58 +03:00
|
|
|
struct Collector<'a, 'tcx> {
|
2020-04-02 16:32:01 +02:00
|
|
|
ccx: &'a ConstCx<'a, 'tcx>,
|
2016-09-25 01:38:27 +02:00
|
|
|
temps: IndexVec<Local, TempState>,
|
2019-08-21 23:31:58 +03:00
|
|
|
candidates: Vec<Candidate>,
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 23:31:58 +03:00
|
|
|
impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
|
2022-07-01 16:21:21 +02:00
|
|
|
fn visit_local(&mut self, index: Local, context: PlaceContext, location: Location) {
|
2018-10-26 12:11:19 +02:00
|
|
|
debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
|
2019-04-08 15:28:00 +02:00
|
|
|
// We're only interested in temporaries and the return place
|
2020-04-02 16:32:01 +02:00
|
|
|
match self.ccx.body.local_kind(index) {
|
2023-03-10 10:29:19 +00:00
|
|
|
LocalKind::Arg => return,
|
|
|
|
LocalKind::Temp if self.ccx.body.local_decls[index].is_user_variable() => return,
|
|
|
|
LocalKind::ReturnPointer | LocalKind::Temp => {}
|
2017-09-03 19:14:31 +03:00
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2017-09-03 19:14:31 +03:00
|
|
|
// Ignore drops, if the temp gets promoted,
|
|
|
|
// then it's constant and thus drop is noop.
|
2019-11-26 22:19:54 -05:00
|
|
|
// Non-uses are also irrelevant.
|
2018-10-26 13:22:45 +02:00
|
|
|
if context.is_drop() || !context.is_use() {
|
2018-10-26 12:11:19 +02:00
|
|
|
debug!(
|
2018-10-26 13:22:45 +02:00
|
|
|
"visit_local: context.is_drop={:?} context.is_use={:?}",
|
|
|
|
context.is_drop(),
|
|
|
|
context.is_use(),
|
2018-10-26 12:11:19 +02:00
|
|
|
);
|
2017-09-03 19:14:31 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-07 19:14:28 +03:00
|
|
|
|
2017-09-03 19:14:31 +03:00
|
|
|
let temp = &mut self.temps[index];
|
2018-10-26 12:11:19 +02:00
|
|
|
debug!("visit_local: temp={:?}", temp);
|
2017-09-03 19:14:31 +03:00
|
|
|
if *temp == TempState::Undefined {
|
|
|
|
match context {
|
2018-10-26 13:22:45 +02:00
|
|
|
PlaceContext::MutatingUse(MutatingUseContext::Store)
|
|
|
|
| PlaceContext::MutatingUse(MutatingUseContext::Call) => {
|
2022-05-09 17:13:30 +08:00
|
|
|
*temp = TempState::Defined { location, uses: 0, valid: Err(()) };
|
2017-01-05 02:33:09 +02:00
|
|
|
return;
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2017-09-03 19:14:31 +03:00
|
|
|
_ => { /* mark as unpromotable below */ }
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2022-12-23 15:15:21 +00:00
|
|
|
} else if let TempState::Defined { uses, .. } = temp {
|
2017-09-03 19:14:31 +03:00
|
|
|
// We always allow borrows, even mutable ones, as we need
|
2018-11-27 02:59:49 +00:00
|
|
|
// to promote mutable borrows of some ZSTs e.g., `&mut []`.
|
2018-12-23 19:00:58 +00:00
|
|
|
let allowed_use = match context {
|
|
|
|
PlaceContext::MutatingUse(MutatingUseContext::Borrow)
|
|
|
|
| PlaceContext::NonMutatingUse(_) => true,
|
|
|
|
PlaceContext::MutatingUse(_) | PlaceContext::NonUse(_) => false,
|
|
|
|
};
|
2018-10-26 12:11:19 +02:00
|
|
|
debug!("visit_local: allowed_use={:?}", allowed_use);
|
2017-09-03 19:14:31 +03:00
|
|
|
if allowed_use {
|
|
|
|
*uses += 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* mark as unpromotable below */
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2017-09-03 19:14:31 +03:00
|
|
|
*temp = TempState::Unpromotable;
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 23:31:58 +03:00
|
|
|
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
|
|
|
|
self.super_rvalue(rvalue, location);
|
|
|
|
|
|
|
|
match *rvalue {
|
|
|
|
Rvalue::Ref(..) => {
|
2021-11-05 00:00:00 +00:00
|
|
|
self.candidates.push(Candidate { location });
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2021-12-13 22:34:51 -05:00
|
|
|
pub fn collect_temps_and_candidates<'tcx>(
|
|
|
|
ccx: &ConstCx<'_, 'tcx>,
|
2022-04-28 11:31:08 +08:00
|
|
|
rpo: &mut ReversePostorderIter<'_, 'tcx>,
|
2019-08-21 23:31:58 +03:00
|
|
|
) -> (IndexVec<Local, TempState>, Vec<Candidate>) {
|
|
|
|
let mut collector = Collector {
|
2020-04-02 16:32:01 +02:00
|
|
|
temps: IndexVec::from_elem(TempState::Undefined, &ccx.body.local_decls),
|
2019-08-21 23:31:58 +03:00
|
|
|
candidates: vec![],
|
2020-04-02 16:32:01 +02:00
|
|
|
ccx,
|
2016-05-07 19:14:28 +03:00
|
|
|
};
|
|
|
|
for (bb, data) in rpo {
|
|
|
|
collector.visit_basic_block_data(bb, data);
|
|
|
|
}
|
2019-08-21 23:31:58 +03:00
|
|
|
(collector.temps, collector.candidates)
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:55:04 -07:00
|
|
|
/// Checks whether locals that appear in a promotion context (`Candidate`) are actually promotable.
|
|
|
|
///
|
|
|
|
/// This wraps an `Item`, and has access to all fields of that `Item` via `Deref` coercion.
|
2019-08-21 23:31:58 +03:00
|
|
|
struct Validator<'a, 'tcx> {
|
2020-04-02 16:38:50 +02:00
|
|
|
ccx: &'a ConstCx<'a, 'tcx>,
|
2023-03-31 00:32:44 -07:00
|
|
|
temps: &'a mut IndexSlice<Local, TempState>,
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
|
2021-12-13 22:34:51 -05:00
|
|
|
impl<'a, 'tcx> std::ops::Deref for Validator<'a, 'tcx> {
|
2020-03-23 14:02:58 +01:00
|
|
|
type Target = ConstCx<'a, 'tcx>;
|
2019-10-22 14:55:04 -07:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2020-03-23 14:02:58 +01:00
|
|
|
&self.ccx
|
2019-10-22 14:55:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 23:31:58 +03:00
|
|
|
struct Unpromotable;
|
|
|
|
|
|
|
|
impl<'tcx> Validator<'_, 'tcx> {
|
2022-05-07 21:02:25 +08:00
|
|
|
fn validate_candidate(&mut self, candidate: Candidate) -> Result<(), Unpromotable> {
|
2021-11-05 00:00:00 +00:00
|
|
|
let loc = candidate.location;
|
|
|
|
let statement = &self.body[loc.block].statements[loc.statement_index];
|
|
|
|
match &statement.kind {
|
|
|
|
StatementKind::Assign(box (_, Rvalue::Ref(_, kind, place))) => {
|
|
|
|
// We can only promote interior borrows of promotable temps (non-temps
|
|
|
|
// don't get promoted anyway).
|
|
|
|
self.validate_local(place.local)?;
|
|
|
|
|
|
|
|
// The reference operation itself must be promotable.
|
|
|
|
// (Needs to come after `validate_local` to avoid ICEs.)
|
|
|
|
self.validate_ref(*kind, place)?;
|
2019-08-21 23:31:58 +03:00
|
|
|
|
2021-11-05 00:00:00 +00:00
|
|
|
// We do not check all the projections (they do not get promoted anyway),
|
|
|
|
// but we do stay away from promoting anything involving a dereference.
|
|
|
|
if place.projection.contains(&ProjectionElem::Deref) {
|
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
2019-10-22 14:55:04 -07:00
|
|
|
|
2021-11-05 00:00:00 +00:00
|
|
|
Ok(())
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
2021-11-05 00:00:00 +00:00
|
|
|
_ => bug!(),
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 21:18:12 +03:00
|
|
|
// FIXME(eddyb) maybe cache this?
|
2022-05-07 21:02:25 +08:00
|
|
|
fn qualif_local<Q: qualifs::Qualif>(&mut self, local: Local) -> bool {
|
2019-10-21 21:18:12 +03:00
|
|
|
if let TempState::Defined { location: loc, .. } = self.temps[local] {
|
|
|
|
let num_stmts = self.body[loc.block].statements.len();
|
|
|
|
|
|
|
|
if loc.statement_index < num_stmts {
|
|
|
|
let statement = &self.body[loc.block].statements[loc.statement_index];
|
|
|
|
match &statement.kind {
|
2019-12-13 13:20:16 -08:00
|
|
|
StatementKind::Assign(box (_, rhs)) => qualifs::in_rvalue::<Q, _>(
|
2020-03-23 14:02:58 +01:00
|
|
|
&self.ccx,
|
2019-12-13 13:20:16 -08:00
|
|
|
&mut |l| self.qualif_local::<Q>(l),
|
|
|
|
rhs,
|
|
|
|
),
|
2019-10-21 21:18:12 +03:00
|
|
|
_ => {
|
|
|
|
span_bug!(
|
|
|
|
statement.source_info.span,
|
|
|
|
"{:?} is not an assignment",
|
|
|
|
statement
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let terminator = self.body[loc.block].terminator();
|
|
|
|
match &terminator.kind {
|
2019-12-13 13:20:16 -08:00
|
|
|
TerminatorKind::Call { .. } => {
|
2019-10-21 21:18:12 +03:00
|
|
|
let return_ty = self.body.local_decls[local].ty;
|
2020-03-23 14:02:58 +01:00
|
|
|
Q::in_any_value_of_ty(&self.ccx, return_ty)
|
2019-10-21 21:18:12 +03:00
|
|
|
}
|
|
|
|
kind => {
|
|
|
|
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-30 14:35:23 +00:00
|
|
|
false
|
2019-10-21 21:18:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:02:25 +08:00
|
|
|
fn validate_local(&mut self, local: Local) -> Result<(), Unpromotable> {
|
|
|
|
if let TempState::Defined { location: loc, uses, valid } = self.temps[local] {
|
2022-11-30 14:35:23 +00:00
|
|
|
// We cannot promote things that need dropping, since the promoted value
|
|
|
|
// would not get dropped.
|
|
|
|
if self.qualif_local::<qualifs::NeedsDrop>(local) {
|
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
2022-05-09 17:13:30 +08:00
|
|
|
valid.or_else(|_| {
|
|
|
|
let ok = {
|
|
|
|
let block = &self.body[loc.block];
|
|
|
|
let num_stmts = block.statements.len();
|
|
|
|
|
|
|
|
if loc.statement_index < num_stmts {
|
|
|
|
let statement = &block.statements[loc.statement_index];
|
|
|
|
match &statement.kind {
|
|
|
|
StatementKind::Assign(box (_, rhs)) => self.validate_rvalue(rhs),
|
|
|
|
_ => {
|
|
|
|
span_bug!(
|
|
|
|
statement.source_info.span,
|
|
|
|
"{:?} is not an assignment",
|
|
|
|
statement
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let terminator = block.terminator();
|
|
|
|
match &terminator.kind {
|
|
|
|
TerminatorKind::Call { func, args, .. } => {
|
|
|
|
self.validate_call(func, args)
|
2022-05-07 21:02:25 +08:00
|
|
|
}
|
2022-05-09 17:13:30 +08:00
|
|
|
TerminatorKind::Yield { .. } => Err(Unpromotable),
|
|
|
|
kind => {
|
|
|
|
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
|
2022-05-07 21:02:25 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 17:13:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
self.temps[local] = match ok {
|
|
|
|
Ok(()) => TempState::Defined { location: loc, uses, valid: Ok(()) },
|
|
|
|
Err(_) => TempState::Unpromotable,
|
|
|
|
};
|
|
|
|
ok
|
|
|
|
})
|
2019-08-21 23:31:58 +03:00
|
|
|
} else {
|
|
|
|
Err(Unpromotable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:02:25 +08:00
|
|
|
fn validate_place(&mut self, place: PlaceRef<'tcx>) -> Result<(), Unpromotable> {
|
2021-01-09 23:33:38 -06:00
|
|
|
match place.last_projection() {
|
|
|
|
None => self.validate_local(place.local),
|
|
|
|
Some((place_base, elem)) => {
|
2020-09-06 16:06:39 +02:00
|
|
|
// Validate topmost projection, then recurse.
|
2021-01-09 23:33:38 -06:00
|
|
|
match elem {
|
2020-07-30 18:33:34 +08:00
|
|
|
ProjectionElem::Deref => {
|
2020-09-06 16:06:39 +02:00
|
|
|
let mut promotable = false;
|
2022-12-01 16:41:49 +01:00
|
|
|
// When a static is used by-value, that gets desugared to `*STATIC_ADDR`,
|
|
|
|
// and we need to be able to promote this. So check if this deref matches
|
|
|
|
// that specific pattern.
|
|
|
|
|
2021-01-06 20:03:22 +01:00
|
|
|
// We need to make sure this is a `Deref` of a local with no further projections.
|
2021-01-09 23:33:38 -06:00
|
|
|
// Discussion can be found at
|
|
|
|
// https://github.com/rust-lang/rust/pull/74945#discussion_r463063247
|
2021-01-06 20:03:22 +01:00
|
|
|
if let Some(local) = place_base.as_local() {
|
|
|
|
if let TempState::Defined { location, .. } = self.temps[local] {
|
2021-01-09 23:33:38 -06:00
|
|
|
let def_stmt = self.body[location.block]
|
|
|
|
.statements
|
|
|
|
.get(location.statement_index);
|
|
|
|
if let Some(Statement {
|
|
|
|
kind:
|
|
|
|
StatementKind::Assign(box (
|
|
|
|
_,
|
|
|
|
Rvalue::Use(Operand::Constant(c)),
|
|
|
|
)),
|
|
|
|
..
|
|
|
|
}) = def_stmt
|
|
|
|
{
|
|
|
|
if let Some(did) = c.check_static_ptr(self.tcx) {
|
|
|
|
// Evaluating a promoted may not read statics except if it got
|
|
|
|
// promoted from a static (this is a CTFE check). So we
|
|
|
|
// can only promote static accesses inside statics.
|
|
|
|
if let Some(hir::ConstContext::Static(..)) = self.const_kind
|
2020-07-31 11:58:49 +08:00
|
|
|
{
|
2021-01-09 23:33:38 -06:00
|
|
|
if !self.tcx.is_thread_local_static(did) {
|
|
|
|
promotable = true;
|
|
|
|
}
|
2020-07-30 18:33:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:06:39 +02:00
|
|
|
if !promotable {
|
2020-07-30 18:33:34 +08:00
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 11:58:34 +00:00
|
|
|
ProjectionElem::OpaqueCast(..) | ProjectionElem::Downcast(..) => {
|
2019-08-21 23:31:58 +03:00
|
|
|
return Err(Unpromotable);
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-08-21 23:31:58 +03:00
|
|
|
|
|
|
|
ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {}
|
|
|
|
|
|
|
|
ProjectionElem::Index(local) => {
|
2021-05-09 14:04:34 +02:00
|
|
|
let mut promotable = false;
|
|
|
|
// Only accept if we can predict the index and are indexing an array.
|
2023-02-14 08:51:19 +00:00
|
|
|
let val = if let TempState::Defined { location: loc, .. } =
|
|
|
|
self.temps[local]
|
|
|
|
{
|
|
|
|
let block = &self.body[loc.block];
|
|
|
|
if loc.statement_index < block.statements.len() {
|
|
|
|
let statement = &block.statements[loc.statement_index];
|
|
|
|
match &statement.kind {
|
|
|
|
StatementKind::Assign(box (
|
|
|
|
_,
|
|
|
|
Rvalue::Use(Operand::Constant(c)),
|
|
|
|
)) => c.literal.try_eval_target_usize(self.tcx, self.param_env),
|
|
|
|
_ => None,
|
2021-01-06 20:03:22 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2023-02-14 08:51:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-05-09 14:04:34 +02:00
|
|
|
if let Some(idx) = val {
|
|
|
|
// Determine the type of the thing we are indexing.
|
|
|
|
let ty = place_base.ty(self.body, self.tcx).ty;
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Array(_, len) => {
|
|
|
|
// It's an array; determine its length.
|
2023-02-14 08:51:19 +00:00
|
|
|
if let Some(len) =
|
|
|
|
len.try_eval_target_usize(self.tcx, self.param_env)
|
2021-05-09 14:04:34 +02:00
|
|
|
{
|
|
|
|
// If the index is in-bounds, go ahead.
|
|
|
|
if idx < len {
|
|
|
|
promotable = true;
|
2021-01-06 20:03:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 14:04:34 +02:00
|
|
|
_ => {}
|
2021-01-06 20:03:22 +01:00
|
|
|
}
|
2021-01-01 14:47:45 +01:00
|
|
|
}
|
2021-05-09 14:04:34 +02:00
|
|
|
if !promotable {
|
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
|
|
|
|
2019-08-21 23:31:58 +03:00
|
|
|
self.validate_local(local)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectionElem::Field(..) => {
|
2021-01-09 23:33:38 -06:00
|
|
|
let base_ty = place_base.ty(self.body, self.tcx).ty;
|
2021-06-02 00:00:00 +00:00
|
|
|
if base_ty.is_union() {
|
2020-10-04 15:25:26 +02:00
|
|
|
// No promotion of union field accesses.
|
2021-06-02 00:00:00 +00:00
|
|
|
return Err(Unpromotable);
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 23:33:38 -06:00
|
|
|
self.validate_place(place_base)
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:02:25 +08:00
|
|
|
fn validate_operand(&mut self, operand: &Operand<'tcx>) -> Result<(), Unpromotable> {
|
2019-08-21 23:31:58 +03:00
|
|
|
match operand {
|
|
|
|
Operand::Copy(place) | Operand::Move(place) => self.validate_place(place.as_ref()),
|
|
|
|
|
2019-11-13 16:56:08 -08:00
|
|
|
// The qualifs for a constant (e.g. `HasMutInterior`) are checked in
|
|
|
|
// `validate_rvalue` upon access.
|
2019-11-18 23:04:06 +00:00
|
|
|
Operand::Constant(c) => {
|
|
|
|
if let Some(def_id) = c.check_static_ptr(self.tcx) {
|
2020-09-09 10:00:23 +02:00
|
|
|
// Only allow statics (not consts) to refer to other statics.
|
|
|
|
// FIXME(eddyb) does this matter at all for promotion?
|
2020-09-09 12:57:36 +02:00
|
|
|
// FIXME(RalfJung) it makes little sense to not promote this in `fn`/`const fn`,
|
|
|
|
// and in `const` this cannot occur anyway. The only concern is that we might
|
|
|
|
// promote even `let x = &STATIC` which would be useless, but this applies to
|
|
|
|
// promotion inside statics as well.
|
2020-09-09 10:00:23 +02:00
|
|
|
let is_static = matches!(self.const_kind, Some(hir::ConstContext::Static(_)));
|
|
|
|
if !is_static {
|
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
|
|
|
|
2020-04-17 18:55:23 +02:00
|
|
|
let is_thread_local = self.tcx.is_thread_local_static(def_id);
|
2019-11-18 23:04:06 +00:00
|
|
|
if is_thread_local {
|
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:02:25 +08:00
|
|
|
fn validate_ref(&mut self, kind: BorrowKind, place: &Place<'tcx>) -> Result<(), Unpromotable> {
|
2020-12-28 19:35:16 +01:00
|
|
|
match kind {
|
|
|
|
// Reject these borrow types just to be safe.
|
|
|
|
// FIXME(RalfJung): could we allow them? Should we? No point in it until we have a usecase.
|
|
|
|
BorrowKind::Shallow | BorrowKind::Unique => return Err(Unpromotable),
|
|
|
|
|
|
|
|
BorrowKind::Shared => {
|
|
|
|
let has_mut_interior = self.qualif_local::<qualifs::HasMutInterior>(place.local);
|
|
|
|
if has_mut_interior {
|
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BorrowKind::Mut { .. } => {
|
|
|
|
let ty = place.ty(self.body, self.tcx).ty;
|
|
|
|
|
|
|
|
// In theory, any zero-sized value could be borrowed
|
|
|
|
// mutably without consequences. However, only &mut []
|
|
|
|
// is allowed right now.
|
|
|
|
if let ty::Array(_, len) = ty.kind() {
|
2023-02-14 08:51:19 +00:00
|
|
|
match len.try_eval_target_usize(self.tcx, self.param_env) {
|
2020-12-28 19:35:16 +01:00
|
|
|
Some(0) => {}
|
|
|
|
_ => return Err(Unpromotable),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:02:25 +08:00
|
|
|
fn validate_rvalue(&mut self, rvalue: &Rvalue<'tcx>) -> Result<(), Unpromotable> {
|
2020-12-28 22:44:04 +01:00
|
|
|
match rvalue {
|
2021-01-01 14:47:45 +01:00
|
|
|
Rvalue::Use(operand) | Rvalue::Repeat(operand, _) => {
|
2020-12-28 22:44:04 +01:00
|
|
|
self.validate_operand(operand)?;
|
|
|
|
}
|
2022-06-13 16:37:41 +03:00
|
|
|
Rvalue::CopyForDeref(place) => {
|
|
|
|
let op = &Operand::Copy(*place);
|
|
|
|
self.validate_operand(op)?
|
|
|
|
}
|
2020-12-28 22:44:04 +01:00
|
|
|
|
2020-12-28 23:29:16 +01:00
|
|
|
Rvalue::Discriminant(place) | Rvalue::Len(place) => {
|
|
|
|
self.validate_place(place.as_ref())?
|
|
|
|
}
|
2020-12-28 22:44:04 +01:00
|
|
|
|
|
|
|
Rvalue::ThreadLocalRef(_) => return Err(Unpromotable),
|
|
|
|
|
2022-05-31 00:00:00 +00:00
|
|
|
// ptr-to-int casts are not possible in consts and thus not promotable
|
2022-06-01 13:24:44 -04:00
|
|
|
Rvalue::Cast(CastKind::PointerExposeAddress, _, _) => return Err(Unpromotable),
|
2020-12-28 22:44:04 +01:00
|
|
|
|
2022-06-02 11:10:34 -04:00
|
|
|
// all other casts including int-to-ptr casts are fine, they just use the integer value
|
2022-06-02 09:05:37 -04:00
|
|
|
// at pointer type.
|
2022-05-31 00:00:00 +00:00
|
|
|
Rvalue::Cast(_, operand, _) => {
|
2020-12-28 22:44:04 +01:00
|
|
|
self.validate_operand(operand)?;
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:47:45 +01:00
|
|
|
Rvalue::NullaryOp(op, _) => match op {
|
|
|
|
NullOp::SizeOf => {}
|
2021-09-07 16:06:07 +01:00
|
|
|
NullOp::AlignOf => {}
|
2021-01-01 14:47:45 +01:00
|
|
|
},
|
|
|
|
|
2021-09-06 18:33:23 +01:00
|
|
|
Rvalue::ShallowInitBox(_, _) => return Err(Unpromotable),
|
|
|
|
|
2021-01-01 14:47:45 +01:00
|
|
|
Rvalue::UnaryOp(op, operand) => {
|
|
|
|
match op {
|
|
|
|
// These operations can never fail.
|
|
|
|
UnOp::Neg | UnOp::Not => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.validate_operand(operand)?;
|
|
|
|
}
|
|
|
|
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(op, box (lhs, rhs)) | Rvalue::CheckedBinaryOp(op, box (lhs, rhs)) => {
|
2020-12-28 22:44:04 +01:00
|
|
|
let op = *op;
|
2021-01-01 14:47:45 +01:00
|
|
|
let lhs_ty = lhs.ty(self.body, self.tcx);
|
|
|
|
|
|
|
|
if let ty::RawPtr(_) | ty::FnPtr(..) = lhs_ty.kind() {
|
|
|
|
// Raw and fn pointer operations are not allowed inside consts and thus not promotable.
|
2020-12-28 23:29:16 +01:00
|
|
|
assert!(matches!(
|
|
|
|
op,
|
|
|
|
BinOp::Eq
|
|
|
|
| BinOp::Ne
|
|
|
|
| BinOp::Le
|
|
|
|
| BinOp::Lt
|
|
|
|
| BinOp::Ge
|
|
|
|
| BinOp::Gt
|
|
|
|
| BinOp::Offset
|
|
|
|
));
|
2019-08-21 23:31:58 +03:00
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
|
|
|
|
2020-12-28 23:29:16 +01:00
|
|
|
match op {
|
2021-01-01 14:47:45 +01:00
|
|
|
BinOp::Div | BinOp::Rem => {
|
2021-05-09 14:04:34 +02:00
|
|
|
if lhs_ty.is_integral() {
|
2021-01-01 14:47:45 +01:00
|
|
|
// Integer division: the RHS must be a non-zero const.
|
|
|
|
let const_val = match rhs {
|
|
|
|
Operand::Constant(c) => {
|
|
|
|
c.literal.try_eval_bits(self.tcx, self.param_env, lhs_ty)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
match const_val {
|
|
|
|
Some(x) if x != 0 => {} // okay
|
|
|
|
_ => return Err(Unpromotable), // value not known or 0 -- not okay
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The remaining operations can never fail.
|
2020-12-28 23:29:16 +01:00
|
|
|
BinOp::Eq
|
|
|
|
| BinOp::Ne
|
|
|
|
| BinOp::Le
|
|
|
|
| BinOp::Lt
|
|
|
|
| BinOp::Ge
|
|
|
|
| BinOp::Gt
|
|
|
|
| BinOp::Offset
|
|
|
|
| BinOp::Add
|
|
|
|
| BinOp::Sub
|
|
|
|
| BinOp::Mul
|
|
|
|
| BinOp::BitXor
|
|
|
|
| BinOp::BitAnd
|
|
|
|
| BinOp::BitOr
|
|
|
|
| BinOp::Shl
|
|
|
|
| BinOp::Shr => {}
|
|
|
|
}
|
2019-08-21 23:31:58 +03:00
|
|
|
|
|
|
|
self.validate_operand(lhs)?;
|
2020-12-28 22:44:04 +01:00
|
|
|
self.validate_operand(rhs)?;
|
|
|
|
}
|
|
|
|
|
2018-12-23 19:00:58 +00:00
|
|
|
Rvalue::AddressOf(_, place) => {
|
2020-09-06 16:06:39 +02:00
|
|
|
// We accept `&raw *`, i.e., raw reborrows -- creating a raw pointer is
|
|
|
|
// no problem, only using it is.
|
2021-01-09 23:33:38 -06:00
|
|
|
if let Some((place_base, ProjectionElem::Deref)) = place.as_ref().last_projection()
|
|
|
|
{
|
|
|
|
let base_ty = place_base.ty(self.body, self.tcx).ty;
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Ref(..) = base_ty.kind() {
|
2021-01-09 23:33:38 -06:00
|
|
|
return self.validate_place(place_base);
|
2018-12-23 19:00:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-28 22:44:04 +01:00
|
|
|
return Err(Unpromotable);
|
2018-12-23 19:00:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 23:31:58 +03:00
|
|
|
Rvalue::Ref(_, kind, place) => {
|
|
|
|
// Special-case reborrows to be more like a copy of the reference.
|
2020-12-28 19:35:16 +01:00
|
|
|
let mut place_simplified = place.as_ref();
|
2021-01-09 23:33:38 -06:00
|
|
|
if let Some((place_base, ProjectionElem::Deref)) =
|
|
|
|
place_simplified.last_projection()
|
|
|
|
{
|
|
|
|
let base_ty = place_base.ty(self.body, self.tcx).ty;
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Ref(..) = base_ty.kind() {
|
2021-01-09 23:33:38 -06:00
|
|
|
place_simplified = place_base;
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:35:16 +01:00
|
|
|
self.validate_place(place_simplified)?;
|
2019-10-21 21:18:12 +03:00
|
|
|
|
2020-12-28 19:35:16 +01:00
|
|
|
// Check that the reference is fine (using the original place!).
|
2020-12-29 16:32:38 +01:00
|
|
|
// (Needs to come after `validate_place` to avoid ICEs.)
|
2020-12-28 19:35:16 +01:00
|
|
|
self.validate_ref(*kind, place)?;
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
|
2020-12-28 22:44:04 +01:00
|
|
|
Rvalue::Aggregate(_, operands) => {
|
2019-08-21 23:31:58 +03:00
|
|
|
for o in operands {
|
|
|
|
self.validate_operand(o)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 22:44:04 +01:00
|
|
|
|
|
|
|
Ok(())
|
2019-08-21 23:31:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn validate_call(
|
2022-05-07 21:02:25 +08:00
|
|
|
&mut self,
|
2019-08-21 23:31:58 +03:00
|
|
|
callee: &Operand<'tcx>,
|
|
|
|
args: &[Operand<'tcx>],
|
|
|
|
) -> Result<(), Unpromotable> {
|
2020-04-12 10:28:41 -07:00
|
|
|
let fn_ty = callee.ty(self.body, self.tcx);
|
2019-08-21 23:31:58 +03:00
|
|
|
|
2021-05-09 14:04:34 +02:00
|
|
|
// Inside const/static items, we promote all (eligible) function calls.
|
2020-10-04 15:25:26 +02:00
|
|
|
// Everywhere else, we require `#[rustc_promotable]` on the callee.
|
2021-05-09 14:04:34 +02:00
|
|
|
let promote_all_const_fn = matches!(
|
|
|
|
self.const_kind,
|
|
|
|
Some(hir::ConstContext::Static(_) | hir::ConstContext::Const)
|
|
|
|
);
|
2020-10-04 15:25:26 +02:00
|
|
|
if !promote_all_const_fn {
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::FnDef(def_id, _) = *fn_ty.kind() {
|
2019-08-21 23:31:58 +03:00
|
|
|
// Never promote runtime `const fn` calls of
|
|
|
|
// functions without `#[rustc_promotable]`.
|
|
|
|
if !self.tcx.is_promotable_const_fn(def_id) {
|
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
let is_const_fn = match *fn_ty.kind() {
|
2021-10-25 17:07:16 +01:00
|
|
|
ty::FnDef(def_id, _) => self.tcx.is_const_fn_raw(def_id),
|
2019-08-21 23:31:58 +03:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if !is_const_fn {
|
|
|
|
return Err(Unpromotable);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.validate_operand(callee)?;
|
|
|
|
for arg in args {
|
|
|
|
self.validate_operand(arg)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:55:04 -07:00
|
|
|
// FIXME(eddyb) remove the differences for promotability in `static`, `const`, `const fn`.
|
2019-08-21 23:31:58 +03:00
|
|
|
pub fn validate_candidates(
|
2020-04-02 16:38:50 +02:00
|
|
|
ccx: &ConstCx<'_, '_>,
|
2023-03-31 00:32:44 -07:00
|
|
|
temps: &mut IndexSlice<Local, TempState>,
|
2019-08-21 23:31:58 +03:00
|
|
|
candidates: &[Candidate],
|
|
|
|
) -> Vec<Candidate> {
|
2022-05-07 21:02:25 +08:00
|
|
|
let mut validator = Validator { ccx, temps };
|
2019-08-21 23:31:58 +03:00
|
|
|
|
|
|
|
candidates
|
|
|
|
.iter()
|
|
|
|
.copied()
|
2021-05-09 14:04:34 +02:00
|
|
|
.filter(|&candidate| validator.validate_candidate(candidate).is_ok())
|
2019-08-21 23:31:58 +03:00
|
|
|
.collect()
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
struct Promoter<'a, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-04-12 10:31:00 -07:00
|
|
|
source: &'a mut Body<'tcx>,
|
|
|
|
promoted: Body<'tcx>,
|
2016-09-25 01:38:27 +02:00
|
|
|
temps: &'a mut IndexVec<Local, TempState>,
|
2019-11-22 17:26:09 -03:00
|
|
|
extra_statements: &'a mut Vec<(Location, Statement<'tcx>)>,
|
2016-05-07 19:14:28 +03:00
|
|
|
|
|
|
|
/// If true, all nested temps are also kept in the
|
|
|
|
/// source MIR, not moved to the promoted MIR.
|
2019-06-12 00:11:55 +03:00
|
|
|
keep_original: bool,
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Promoter<'a, 'tcx> {
|
|
|
|
fn new_block(&mut self) -> BasicBlock {
|
2019-11-06 12:00:46 -05:00
|
|
|
let span = self.promoted.span;
|
|
|
|
self.promoted.basic_blocks_mut().push(BasicBlockData {
|
2016-05-07 19:14:28 +03:00
|
|
|
statements: vec![],
|
|
|
|
terminator: Some(Terminator {
|
2020-05-06 10:30:11 +10:00
|
|
|
source_info: SourceInfo::outermost(span),
|
2016-05-07 19:14:28 +03:00
|
|
|
kind: TerminatorKind::Return,
|
|
|
|
}),
|
|
|
|
is_cleanup: false,
|
2016-06-07 17:28:36 +03:00
|
|
|
})
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2016-09-26 22:53:22 +02:00
|
|
|
fn assign(&mut self, dest: Local, rvalue: Rvalue<'tcx>, span: Span) {
|
2023-03-29 00:04:14 -07:00
|
|
|
let last = self.promoted.basic_blocks.last_index().unwrap();
|
2019-11-06 12:00:46 -05:00
|
|
|
let data = &mut self.promoted[last];
|
2016-05-07 19:14:28 +03:00
|
|
|
data.statements.push(Statement {
|
2020-05-06 10:30:11 +10:00
|
|
|
source_info: SourceInfo::outermost(span),
|
2021-08-05 05:36:38 +02:00
|
|
|
kind: StatementKind::Assign(Box::new((Place::from(dest), rvalue))),
|
2016-05-07 19:14:28 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:39:20 -03:00
|
|
|
fn is_temp_kind(&self, local: Local) -> bool {
|
2019-11-06 12:00:46 -05:00
|
|
|
self.source.local_kind(local) == LocalKind::Temp
|
2019-10-07 16:39:20 -03:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Copies the initialization of this temp to the
|
2016-05-07 19:14:28 +03:00
|
|
|
/// promoted MIR, recursing through temps.
|
2016-09-25 01:38:27 +02:00
|
|
|
fn promote_temp(&mut self, temp: Local) -> Local {
|
2016-05-07 19:14:28 +03:00
|
|
|
let old_keep_original = self.keep_original;
|
2017-01-05 01:19:54 +02:00
|
|
|
let loc = match self.temps[temp] {
|
2022-05-07 21:02:25 +08:00
|
|
|
TempState::Defined { location, uses, .. } if uses > 0 => {
|
2016-05-07 19:14:28 +03:00
|
|
|
if uses > 1 {
|
|
|
|
self.keep_original = true;
|
|
|
|
}
|
2017-01-05 01:19:54 +02:00
|
|
|
location
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2016-06-07 17:28:36 +03:00
|
|
|
state => {
|
2019-11-06 12:00:46 -05:00
|
|
|
span_bug!(self.promoted.span, "{:?} not promotable: {:?}", temp, state);
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if !self.keep_original {
|
2016-06-07 17:28:36 +03:00
|
|
|
self.temps[temp] = TempState::PromotedOut;
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
let num_stmts = self.source[loc.block].statements.len();
|
2020-05-06 10:17:38 +10:00
|
|
|
let new_temp = self.promoted.local_decls.push(LocalDecl::new(
|
2019-11-06 12:00:46 -05:00
|
|
|
self.source.local_decls[temp].ty,
|
|
|
|
self.source.local_decls[temp].source_info.span,
|
|
|
|
));
|
2017-01-05 01:19:54 +02:00
|
|
|
|
2019-08-21 23:31:58 +03:00
|
|
|
debug!("promote({:?} @ {:?}/{:?}, {:?})", temp, loc, num_stmts, self.keep_original);
|
2016-05-07 19:14:28 +03:00
|
|
|
|
|
|
|
// First, take the Rvalue or Call out of the source MIR,
|
|
|
|
// or duplicate it, depending on keep_original.
|
2019-08-21 23:31:58 +03:00
|
|
|
if loc.statement_index < num_stmts {
|
2019-09-11 16:05:45 -03:00
|
|
|
let (mut rvalue, source_info) = {
|
2019-11-06 12:00:46 -05:00
|
|
|
let statement = &mut self.source[loc.block].statements[loc.statement_index];
|
2022-12-23 15:15:21 +00:00
|
|
|
let StatementKind::Assign(box (_, rhs)) = &mut statement.kind else {
|
2022-02-19 00:47:43 +01:00
|
|
|
span_bug!(
|
|
|
|
statement.source_info.span,
|
|
|
|
"{:?} is not an assignment",
|
|
|
|
statement
|
|
|
|
);
|
2017-01-05 01:19:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
(
|
|
|
|
if self.keep_original {
|
|
|
|
rhs.clone()
|
|
|
|
} else {
|
2021-08-05 05:36:38 +02:00
|
|
|
let unit = Rvalue::Use(Operand::Constant(Box::new(Constant {
|
2020-04-09 12:24:53 +02:00
|
|
|
span: statement.source_info.span,
|
|
|
|
user_ty: None,
|
2022-02-16 10:56:01 +01:00
|
|
|
literal: ConstantKind::zero_sized(self.tcx.types.unit),
|
2021-08-05 05:36:38 +02:00
|
|
|
})));
|
2017-01-05 01:19:54 +02:00
|
|
|
mem::replace(rhs, unit)
|
|
|
|
},
|
|
|
|
statement.source_info,
|
|
|
|
)
|
2016-08-04 16:14:33 -07:00
|
|
|
};
|
2017-01-05 01:19:54 +02:00
|
|
|
|
|
|
|
self.visit_rvalue(&mut rvalue, loc);
|
|
|
|
self.assign(new_temp, rvalue, source_info.span);
|
2016-05-07 19:14:28 +03:00
|
|
|
} else {
|
2017-01-11 09:50:24 +02:00
|
|
|
let terminator = if self.keep_original {
|
2019-11-06 12:00:46 -05:00
|
|
|
self.source[loc.block].terminator().clone()
|
2017-01-05 01:19:54 +02:00
|
|
|
} else {
|
2019-11-06 12:00:46 -05:00
|
|
|
let terminator = self.source[loc.block].terminator_mut();
|
2022-12-23 15:15:21 +00:00
|
|
|
let target = match &terminator.kind {
|
|
|
|
TerminatorKind::Call { target: Some(target), .. } => *target,
|
|
|
|
kind => {
|
2017-01-05 01:19:54 +02:00
|
|
|
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Terminator {
|
|
|
|
source_info: terminator.source_info,
|
|
|
|
kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto { target }),
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-11 09:50:24 +02:00
|
|
|
match terminator.kind {
|
2020-06-09 15:34:23 -04:00
|
|
|
TerminatorKind::Call { mut func, mut args, from_hir_call, fn_span, .. } => {
|
2017-01-05 01:19:54 +02:00
|
|
|
self.visit_operand(&mut func, loc);
|
|
|
|
for arg in &mut args {
|
|
|
|
self.visit_operand(arg, loc);
|
|
|
|
}
|
2017-01-11 09:50:24 +02:00
|
|
|
|
2023-03-29 00:04:14 -07:00
|
|
|
let last = self.promoted.basic_blocks.last_index().unwrap();
|
2017-01-11 09:50:24 +02:00
|
|
|
let new_target = self.new_block();
|
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
*self.promoted[last].terminator_mut() = Terminator {
|
2017-01-11 09:50:24 +02:00
|
|
|
kind: TerminatorKind::Call {
|
2017-08-06 22:54:09 -07:00
|
|
|
func,
|
|
|
|
args,
|
2017-01-11 09:50:24 +02:00
|
|
|
cleanup: None,
|
2022-04-16 09:27:54 -04:00
|
|
|
destination: Place::from(new_temp),
|
|
|
|
target: Some(new_target),
|
2018-09-29 10:34:12 +01:00
|
|
|
from_hir_call,
|
2020-06-09 15:34:23 -04:00
|
|
|
fn_span,
|
2017-01-11 09:50:24 +02:00
|
|
|
},
|
2020-10-14 00:00:00 +00:00
|
|
|
source_info: SourceInfo::outermost(terminator.source_info.span),
|
2017-01-11 09:50:24 +02:00
|
|
|
..terminator
|
|
|
|
};
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2022-12-23 15:15:21 +00:00
|
|
|
kind => {
|
2017-01-05 01:19:54 +02:00
|
|
|
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2016-05-07 19:14:28 +03:00
|
|
|
|
2017-01-05 01:19:54 +02:00
|
|
|
self.keep_original = old_keep_original;
|
2016-06-07 17:28:36 +03:00
|
|
|
new_temp
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 00:00:00 +00:00
|
|
|
fn promote_candidate(mut self, candidate: Candidate, next_promoted_id: usize) -> Body<'tcx> {
|
2020-10-04 16:59:19 -07:00
|
|
|
let def = self.source.source.with_opt_param();
|
2019-11-22 17:26:09 -03:00
|
|
|
let mut rvalue = {
|
2019-11-06 12:00:46 -05:00
|
|
|
let promoted = &mut self.promoted;
|
2019-08-04 16:20:21 -04:00
|
|
|
let promoted_id = Promoted::new(next_promoted_id);
|
2019-08-15 06:39:31 -04:00
|
|
|
let tcx = self.tcx;
|
2019-12-11 00:11:12 -03:00
|
|
|
let mut promoted_operand = |ty, span| {
|
|
|
|
promoted.span = span;
|
2020-05-06 10:17:38 +10:00
|
|
|
promoted.local_decls[RETURN_PLACE] = LocalDecl::new(ty, span);
|
2022-08-12 03:48:40 +00:00
|
|
|
let substs = tcx.erase_regions(InternalSubsts::identity_for_item(tcx, def.did));
|
2022-09-22 12:34:23 +02:00
|
|
|
let uneval = mir::UnevaluatedConst { def, substs, promoted: Some(promoted_id) };
|
2019-12-11 00:11:12 -03:00
|
|
|
|
|
|
|
Operand::Constant(Box::new(Constant {
|
|
|
|
span,
|
|
|
|
user_ty: None,
|
2022-06-27 16:32:47 +02:00
|
|
|
literal: ConstantKind::Unevaluated(uneval, ty),
|
2019-12-11 00:11:12 -03:00
|
|
|
}))
|
|
|
|
};
|
2022-06-27 16:32:47 +02:00
|
|
|
|
2022-07-04 00:00:00 +00:00
|
|
|
let blocks = self.source.basic_blocks.as_mut();
|
|
|
|
let local_decls = &mut self.source.local_decls;
|
2021-11-05 00:00:00 +00:00
|
|
|
let loc = candidate.location;
|
|
|
|
let statement = &mut blocks[loc.block].statements[loc.statement_index];
|
2023-01-09 20:42:36 +04:00
|
|
|
let StatementKind::Assign(box (_, Rvalue::Ref(region, borrow_kind, place))) = &mut statement.kind else {
|
2022-12-23 15:15:21 +00:00
|
|
|
bug!()
|
|
|
|
};
|
2021-11-05 00:00:00 +00:00
|
|
|
|
2022-12-23 15:15:21 +00:00
|
|
|
// Use the underlying local for this (necessarily interior) borrow.
|
|
|
|
let ty = local_decls[place.local].ty;
|
|
|
|
let span = statement.source_info.span;
|
|
|
|
|
|
|
|
let ref_ty = tcx.mk_ref(
|
|
|
|
tcx.lifetimes.re_erased,
|
|
|
|
ty::TypeAndMut { ty, mutbl: borrow_kind.to_mutbl_lossy() },
|
|
|
|
);
|
|
|
|
|
|
|
|
*region = tcx.lifetimes.re_erased;
|
|
|
|
|
|
|
|
let mut projection = vec![PlaceElem::Deref];
|
|
|
|
projection.extend(place.projection);
|
2023-02-17 14:33:08 +11:00
|
|
|
place.projection = tcx.mk_place_elems(&projection);
|
2022-12-23 15:15:21 +00:00
|
|
|
|
|
|
|
// Create a temp to hold the promoted reference.
|
|
|
|
// This is because `*r` requires `r` to be a local,
|
|
|
|
// otherwise we would use the `promoted` directly.
|
|
|
|
let mut promoted_ref = LocalDecl::new(ref_ty, span);
|
|
|
|
promoted_ref.source_info = statement.source_info;
|
|
|
|
let promoted_ref = local_decls.push(promoted_ref);
|
|
|
|
assert_eq!(self.temps.push(TempState::Unpromotable), promoted_ref);
|
|
|
|
|
|
|
|
let promoted_ref_statement = Statement {
|
|
|
|
source_info: statement.source_info,
|
|
|
|
kind: StatementKind::Assign(Box::new((
|
|
|
|
Place::from(promoted_ref),
|
|
|
|
Rvalue::Use(promoted_operand(ref_ty, span)),
|
|
|
|
))),
|
|
|
|
};
|
|
|
|
self.extra_statements.push((loc, promoted_ref_statement));
|
|
|
|
|
|
|
|
Rvalue::Ref(
|
|
|
|
tcx.lifetimes.re_erased,
|
|
|
|
*borrow_kind,
|
|
|
|
Place {
|
|
|
|
local: mem::replace(&mut place.local, promoted_ref),
|
|
|
|
projection: List::empty(),
|
|
|
|
},
|
|
|
|
)
|
2016-05-07 19:14:28 +03:00
|
|
|
};
|
2018-05-05 13:27:51 +03:00
|
|
|
|
|
|
|
assert_eq!(self.new_block(), START_BLOCK);
|
2019-11-22 17:26:09 -03:00
|
|
|
self.visit_rvalue(
|
|
|
|
&mut rvalue,
|
2023-02-16 18:46:25 -08:00
|
|
|
Location { block: START_BLOCK, statement_index: usize::MAX },
|
2016-08-08 18:46:06 -07:00
|
|
|
);
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2019-11-06 12:00:46 -05:00
|
|
|
let span = self.promoted.span;
|
2019-11-22 17:26:09 -03:00
|
|
|
self.assign(RETURN_PLACE, rvalue, span);
|
2021-10-31 00:00:00 +00:00
|
|
|
self.promoted
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replaces all temporaries with their promoted counterparts.
|
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
|
2019-10-20 16:11:04 -04:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2017-09-03 19:14:31 +03:00
|
|
|
fn visit_local(&mut self, local: &mut Local, _: PlaceContext, _: Location) {
|
2019-10-07 16:39:20 -03:00
|
|
|
if self.is_temp_kind(*local) {
|
2017-09-03 19:14:31 +03:00
|
|
|
*local = self.promote_temp(*local);
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn promote_candidates<'tcx>(
|
2020-04-12 10:31:00 -07:00
|
|
|
body: &mut Body<'tcx>,
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
mut temps: IndexVec<Local, TempState>,
|
|
|
|
candidates: Vec<Candidate>,
|
2020-04-12 10:31:00 -07:00
|
|
|
) -> IndexVec<Promoted, Body<'tcx>> {
|
2016-05-07 19:14:28 +03:00
|
|
|
// Visit candidates in reverse, in case they're nested.
|
2017-01-05 01:19:54 +02:00
|
|
|
debug!("promote_candidates({:?})", candidates);
|
2018-05-05 13:27:51 +03:00
|
|
|
|
2019-08-04 16:20:21 -04:00
|
|
|
let mut promotions = IndexVec::new();
|
|
|
|
|
2019-11-22 17:26:09 -03:00
|
|
|
let mut extra_statements = vec![];
|
2016-05-07 19:14:28 +03:00
|
|
|
for candidate in candidates.into_iter().rev() {
|
2021-11-05 00:00:00 +00:00
|
|
|
let Location { block, statement_index } = candidate.location;
|
|
|
|
if let StatementKind::Assign(box (place, _)) = &body[block].statements[statement_index].kind
|
|
|
|
{
|
|
|
|
if let Some(local) = place.as_local() {
|
|
|
|
if temps[local] == TempState::PromotedOut {
|
|
|
|
// Already promoted.
|
|
|
|
continue;
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-05 13:27:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 00:26:56 +03:00
|
|
|
// Declare return place local so that `mir::Body::new` doesn't complain.
|
2020-05-06 10:17:38 +10:00
|
|
|
let initial_locals = iter::once(LocalDecl::new(tcx.types.never, body.span)).collect();
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2021-11-05 00:00:00 +00:00
|
|
|
let mut scope = body.source_scopes[body.source_info(candidate.location).scope].clone();
|
2020-10-14 00:00:00 +00:00
|
|
|
scope.parent_scope = None;
|
|
|
|
|
2022-07-09 18:04:38 -07:00
|
|
|
let mut promoted = Body::new(
|
2020-10-04 11:01:38 -07:00
|
|
|
body.source, // `promoted` gets filled in below
|
2019-11-22 17:26:09 -03:00
|
|
|
IndexVec::new(),
|
2020-10-14 00:00:00 +00:00
|
|
|
IndexVec::from_elem_n(scope, 1),
|
2019-11-22 17:26:09 -03:00
|
|
|
initial_locals,
|
|
|
|
IndexVec::new(),
|
|
|
|
0,
|
|
|
|
vec![],
|
|
|
|
body.span,
|
2021-01-17 13:27:05 +01:00
|
|
|
body.generator_kind(),
|
2022-02-07 22:00:15 -08:00
|
|
|
body.tainted_by_errors,
|
2019-11-22 17:26:09 -03:00
|
|
|
);
|
2022-07-09 18:04:38 -07:00
|
|
|
promoted.phase = MirPhase::Analysis(AnalysisPhase::Initial);
|
2019-11-22 17:26:09 -03:00
|
|
|
|
2018-07-29 17:40:36 +01:00
|
|
|
let promoter = Promoter {
|
2020-04-12 10:31:00 -07:00
|
|
|
promoted,
|
2018-05-05 13:27:51 +03:00
|
|
|
tcx,
|
2019-11-06 12:00:46 -05:00
|
|
|
source: body,
|
2016-05-07 19:14:28 +03:00
|
|
|
temps: &mut temps,
|
2019-11-22 17:26:09 -03:00
|
|
|
extra_statements: &mut extra_statements,
|
2019-03-23 18:29:02 +05:30
|
|
|
keep_original: false,
|
2016-05-07 19:14:28 +03:00
|
|
|
};
|
2019-08-04 16:20:21 -04:00
|
|
|
|
2021-10-31 00:00:00 +00:00
|
|
|
let mut promoted = promoter.promote_candidate(candidate, promotions.len());
|
|
|
|
promoted.source.promoted = Some(promotions.next_index());
|
|
|
|
promotions.push(promoted);
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:26:09 -03:00
|
|
|
// Insert each of `extra_statements` before its indicated location, which
|
|
|
|
// has to be done in reverse location order, to not invalidate the rest.
|
|
|
|
extra_statements.sort_by_key(|&(loc, _)| cmp::Reverse(loc));
|
|
|
|
for (loc, statement) in extra_statements {
|
|
|
|
body[loc.block].statements.insert(loc.statement_index, statement);
|
|
|
|
}
|
|
|
|
|
2016-05-07 19:14:28 +03:00
|
|
|
// Eliminate assignments to, and drops of promoted temps.
|
2016-09-25 01:38:27 +02:00
|
|
|
let promoted = |index: Local| temps[index] == TempState::PromotedOut;
|
2019-11-06 12:00:46 -05:00
|
|
|
for block in body.basic_blocks_mut() {
|
2019-10-04 00:55:28 -04:00
|
|
|
block.statements.retain(|statement| match &statement.kind {
|
2019-10-20 16:09:36 -04:00
|
|
|
StatementKind::Assign(box (place, _)) => {
|
|
|
|
if let Some(index) = place.as_local() {
|
|
|
|
!promoted(index)
|
|
|
|
} else {
|
|
|
|
true
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-04 08:01:46 +03:00
|
|
|
StatementKind::StorageLive(index) | StatementKind::StorageDead(index) => {
|
2019-10-20 16:09:36 -04:00
|
|
|
!promoted(*index)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
|
|
|
_ => true,
|
2016-05-07 19:14:28 +03:00
|
|
|
});
|
2019-10-04 00:55:28 -04:00
|
|
|
let terminator = block.terminator_mut();
|
2020-06-10 09:56:54 +02:00
|
|
|
if let TerminatorKind::Drop { place, target, .. } = &terminator.kind {
|
2020-03-22 13:36:56 +01:00
|
|
|
if let Some(index) = place.as_local() {
|
|
|
|
if promoted(index) {
|
|
|
|
terminator.kind = TerminatorKind::Goto { target: *target };
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-04 16:20:21 -04:00
|
|
|
|
|
|
|
promotions
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2021-01-29 11:54:19 +05:30
|
|
|
|
|
|
|
/// This function returns `true` if the function being called in the array
|
|
|
|
/// repeat expression is a `const` function.
|
2020-12-30 18:48:40 +01:00
|
|
|
pub fn is_const_fn_in_array_repeat_expression<'tcx>(
|
2021-01-29 11:54:19 +05:30
|
|
|
ccx: &ConstCx<'_, 'tcx>,
|
|
|
|
place: &Place<'tcx>,
|
|
|
|
body: &Body<'tcx>,
|
|
|
|
) -> bool {
|
|
|
|
match place.as_local() {
|
|
|
|
// rule out cases such as: `let my_var = some_fn(); [my_var; N]`
|
|
|
|
Some(local) if body.local_decls[local].is_user_variable() => return false,
|
|
|
|
None => return false,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2022-07-05 00:00:00 +00:00
|
|
|
for block in body.basic_blocks.iter() {
|
2021-01-29 11:54:19 +05:30
|
|
|
if let Some(Terminator { kind: TerminatorKind::Call { func, destination, .. }, .. }) =
|
|
|
|
&block.terminator
|
|
|
|
{
|
2021-03-08 16:18:03 +00:00
|
|
|
if let Operand::Constant(box Constant { literal, .. }) = func {
|
|
|
|
if let ty::FnDef(def_id, _) = *literal.ty().kind() {
|
2022-04-16 09:27:54 -04:00
|
|
|
if destination == place {
|
|
|
|
if ccx.tcx.is_const_fn(def_id) {
|
|
|
|
return true;
|
2021-01-29 11:54:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|