Skip to content

Commit ee868e0

Browse files
committed
Use ConstCx in the promoted collector
1 parent 77345cc commit ee868e0

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/librustc_mir/transform/promote_consts.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,16 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
6161

6262
let def_id = src.def_id();
6363

64-
let mut rpo = traversal::reverse_postorder(body);
65-
let (temps, all_candidates) = collect_temps_and_candidates(tcx, body, &mut rpo);
64+
let read_only_body = read_only!(body);
65+
66+
let mut rpo = traversal::reverse_postorder(&read_only_body);
67+
68+
let ccx = ConstCx::new(tcx, def_id, read_only_body);
69+
70+
let (temps, all_candidates) = collect_temps_and_candidates(&ccx, &mut rpo);
6671

6772
let promotable_candidates =
68-
validate_candidates(tcx, read_only!(body), def_id, &temps, &all_candidates);
73+
validate_candidates(tcx, read_only_body, def_id, &temps, &all_candidates);
6974

7075
let promoted = promote_candidates(def_id, body, tcx, temps, promotable_candidates);
7176
self.promoted_fragments.set(promoted);
@@ -140,8 +145,7 @@ fn args_required_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Vec<usize>> {
140145
}
141146

142147
struct Collector<'a, 'tcx> {
143-
tcx: TyCtxt<'tcx>,
144-
body: &'a Body<'tcx>,
148+
ccx: &'a ConstCx<'a, 'tcx>,
145149
temps: IndexVec<Local, TempState>,
146150
candidates: Vec<Candidate>,
147151
span: Span,
@@ -151,7 +155,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
151155
fn visit_local(&mut self, &index: &Local, context: PlaceContext, location: Location) {
152156
debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
153157
// We're only interested in temporaries and the return place
154-
match self.body.local_kind(index) {
158+
match self.ccx.body.local_kind(index) {
155159
LocalKind::Temp | LocalKind::ReturnPointer => {}
156160
LocalKind::Arg | LocalKind::Var => return,
157161
}
@@ -204,7 +208,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
204208
Rvalue::Ref(..) => {
205209
self.candidates.push(Candidate::Ref(location));
206210
}
207-
Rvalue::Repeat(..) if self.tcx.features().const_in_array_repeat_expressions => {
211+
Rvalue::Repeat(..) if self.ccx.tcx.features().const_in_array_repeat_expressions => {
208212
// FIXME(#49147) only promote the element when it isn't `Copy`
209213
// (so that code that can copy it at runtime is unaffected).
210214
self.candidates.push(Candidate::Repeat(location));
@@ -217,10 +221,10 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
217221
self.super_terminator_kind(kind, location);
218222

219223
if let TerminatorKind::Call { ref func, .. } = *kind {
220-
if let ty::FnDef(def_id, _) = func.ty(self.body, self.tcx).kind {
221-
let fn_sig = self.tcx.fn_sig(def_id);
224+
if let ty::FnDef(def_id, _) = func.ty(*self.ccx.body, self.ccx.tcx).kind {
225+
let fn_sig = self.ccx.tcx.fn_sig(def_id);
222226
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = fn_sig.abi() {
223-
let name = self.tcx.item_name(def_id);
227+
let name = self.ccx.tcx.item_name(def_id);
224228
// FIXME(eddyb) use `#[rustc_args_required_const(2)]` for shuffles.
225229
if name.as_str().starts_with("simd_shuffle") {
226230
self.candidates.push(Candidate::Argument { bb: location.block, index: 2 });
@@ -229,7 +233,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
229233
}
230234
}
231235

232-
if let Some(constant_args) = args_required_const(self.tcx, def_id) {
236+
if let Some(constant_args) = args_required_const(self.ccx.tcx, def_id) {
233237
for index in constant_args {
234238
self.candidates.push(Candidate::Argument { bb: location.block, index });
235239
}
@@ -244,16 +248,14 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
244248
}
245249

246250
pub fn collect_temps_and_candidates(
247-
tcx: TyCtxt<'tcx>,
248-
body: &Body<'tcx>,
251+
ccx: &ConstCx<'mir, 'tcx>,
249252
rpo: &mut ReversePostorder<'_, 'tcx>,
250253
) -> (IndexVec<Local, TempState>, Vec<Candidate>) {
251254
let mut collector = Collector {
252-
tcx,
253-
body,
254-
temps: IndexVec::from_elem(TempState::Undefined, &body.local_decls),
255+
temps: IndexVec::from_elem(TempState::Undefined, &ccx.body.local_decls),
255256
candidates: vec![],
256-
span: body.span,
257+
span: ccx.body.span,
258+
ccx,
257259
};
258260
for (bb, data) in rpo {
259261
collector.visit_basic_block_data(bb, data);
@@ -1157,7 +1159,7 @@ crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>(
11571159
operand: &Operand<'tcx>,
11581160
) -> bool {
11591161
let mut rpo = traversal::reverse_postorder(&ccx.body);
1160-
let (temps, _) = collect_temps_and_candidates(ccx.tcx, &ccx.body, &mut rpo);
1162+
let (temps, _) = collect_temps_and_candidates(&ccx, &mut rpo);
11611163
let validator = Validator { ccx, temps: &temps, explicit: false };
11621164

11631165
let should_promote = validator.validate_operand(operand).is_ok();

0 commit comments

Comments
 (0)