Skip to content

Commit 8316701

Browse files
committed
[mir-opt] Handle const-prop for the return place
1 parent 0d5264a commit 8316701

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

src/librustc_mir/transform/const_prop.rs

+30-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::hir::def_id::DefId;
99
use rustc::mir::{
1010
AggregateKind, Constant, Location, Place, PlaceBase, Body, Operand, Rvalue, Local, UnOp,
1111
StatementKind, Statement, LocalKind, TerminatorKind, Terminator, ClearCrossCrate, SourceInfo,
12-
BinOp, SourceScope, SourceScopeLocalData, LocalDecl, BasicBlock,
12+
BinOp, SourceScope, SourceScopeLocalData, LocalDecl, BasicBlock, RETURN_PLACE,
1313
};
1414
use rustc::mir::visit::{
1515
Visitor, PlaceContext, MutatingUseContext, MutVisitor, NonMutatingUseContext,
@@ -25,6 +25,7 @@ use rustc::ty::layout::{
2525
LayoutOf, TyLayout, LayoutError, HasTyCtxt, TargetDataLayout, HasDataLayout,
2626
};
2727

28+
use crate::rustc::ty::subst::Subst;
2829
use crate::interpret::{
2930
self, InterpCx, ScalarMaybeUndef, Immediate, OpTy,
3031
StackPopCleanup, LocalValue, LocalState, AllocId, Frame,
@@ -269,6 +270,7 @@ struct ConstPropagator<'mir, 'tcx> {
269270
param_env: ParamEnv<'tcx>,
270271
source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
271272
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
273+
ret: Option<OpTy<'tcx, ()>>,
272274
}
273275

274276
impl<'mir, 'tcx> LayoutOf for ConstPropagator<'mir, 'tcx> {
@@ -308,11 +310,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
308310
let mut ecx = InterpCx::new(tcx.at(span), param_env, ConstPropMachine, ());
309311
let can_const_prop = CanConstProp::check(body);
310312

313+
let substs = &InternalSubsts::identity_for_item(tcx, def_id);
314+
315+
let ret =
316+
ecx
317+
.layout_of(body.return_ty().subst(tcx, substs))
318+
.ok()
319+
// Don't bother allocating memory for ZST types which have no values.
320+
.filter(|ret_layout| !ret_layout.is_zst())
321+
.map(|ret_layout| ecx.allocate(ret_layout, MemoryKind::Stack));
322+
311323
ecx.push_stack_frame(
312-
Instance::new(def_id, &InternalSubsts::identity_for_item(tcx, def_id)),
324+
Instance::new(def_id, substs),
313325
span,
314326
dummy_body,
315-
None,
327+
ret.map(Into::into),
316328
StackPopCleanup::None {
317329
cleanup: false,
318330
},
@@ -327,6 +339,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
327339
source_scope_local_data,
328340
//FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
329341
local_decls: body.local_decls.clone(),
342+
ret: ret.map(Into::into),
330343
}
331344
}
332345

@@ -335,6 +348,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
335348
}
336349

337350
fn get_const(&self, local: Local) -> Option<Const<'tcx>> {
351+
if local == RETURN_PLACE {
352+
// Try to read the return place as an immediate so that if it is representable as a
353+
// scalar, we can handle it as such, but otherwise, just return the value as is.
354+
return match self.ret.map(|ret| self.ecx.try_read_immediate(ret)) {
355+
Some(Ok(Ok(imm))) => Some(imm.into()),
356+
_ => self.ret,
357+
};
358+
}
359+
338360
self.ecx.access_local(self.ecx.frame(), local, None).ok()
339361
}
340362

@@ -643,7 +665,8 @@ impl CanConstProp {
643665
// lint for x != y
644666
// FIXME(oli-obk): lint variables until they are used in a condition
645667
// FIXME(oli-obk): lint if return value is constant
646-
*val = body.local_kind(local) == LocalKind::Temp;
668+
let local_kind = body.local_kind(local);
669+
*val = local_kind == LocalKind::Temp || local_kind == LocalKind::ReturnPointer;
647670

648671
if !*val {
649672
trace!("local {:?} can't be propagated because it's not a temporary", local);
@@ -731,7 +754,9 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
731754
}
732755
} else {
733756
trace!("can't propagate into {:?}", local);
734-
self.remove_const(local);
757+
if local != RETURN_PLACE {
758+
self.remove_const(local);
759+
}
735760
}
736761
}
737762
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// compile-flags: -C overflow-checks=on
2+
3+
fn add() -> u32 {
4+
2 + 2
5+
}
6+
7+
fn main() {
8+
add();
9+
}
10+
11+
// END RUST SOURCE
12+
// START rustc.add.ConstProp.before.mir
13+
// fn add() -> u32 {
14+
// let mut _0: u32;
15+
// let mut _1: (u32, bool);
16+
// bb0: {
17+
// _1 = CheckedAdd(const 2u32, const 2u32);
18+
// assert(!move (_1.1: bool), "attempt to add with overflow") -> bb1;
19+
// }
20+
// bb1: {
21+
// _0 = move (_1.0: u32);
22+
// return;
23+
// }
24+
// bb2 (cleanup): {
25+
// resume;
26+
// }
27+
// }
28+
// END rustc.add.ConstProp.before.mir
29+
// START rustc.add.ConstProp.after.mir
30+
// fn add() -> u32 {
31+
// let mut _0: u32;
32+
// let mut _1: (u32, bool);
33+
// bb0: {
34+
// _1 = (const 4u32, const false);
35+
// assert(!const false, "attempt to add with overflow") -> bb1;
36+
// }
37+
// bb1: {
38+
// _0 = const 4u32;
39+
// return;
40+
// }
41+
// bb2 (cleanup): {
42+
// resume;
43+
// }
44+
// }
45+
// END rustc.add.ConstProp.after.mir
46+
// START rustc.add.PreCodegen.before.mir
47+
// fn add() -> u32 {
48+
// let mut _0: u32;
49+
// let mut _1: (u32, bool);
50+
// bb0: {
51+
// (_1.0: u32) = const 4u32;
52+
// (_1.1: bool) = const false;
53+
// _0 = const 4u32;
54+
// return;
55+
// }
56+
// }
57+
// END rustc.add.PreCodegen.before.mir

0 commit comments

Comments
 (0)