Skip to content

Commit 0bc743e

Browse files
committed
Use ConstCx in the promoted collector
1 parent f0f7a59 commit 0bc743e

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/librustc_mir/transform/promote_consts.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
6262
let def_id = src.def_id();
6363

6464
let mut rpo = traversal::reverse_postorder(body);
65-
let (temps, all_candidates) = collect_temps_and_candidates(tcx, body, &mut rpo);
65+
let ccx = ConstCx::new(tcx, def_id, body);
66+
let (temps, all_candidates) = collect_temps_and_candidates(&ccx, &mut rpo);
6667

6768
let promotable_candidates = validate_candidates(tcx, body, def_id, &temps, &all_candidates);
6869

@@ -139,8 +140,7 @@ fn args_required_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Vec<usize>> {
139140
}
140141

141142
struct Collector<'a, 'tcx> {
142-
tcx: TyCtxt<'tcx>,
143-
body: &'a Body<'tcx>,
143+
ccx: &'a ConstCx<'a, 'tcx>,
144144
temps: IndexVec<Local, TempState>,
145145
candidates: Vec<Candidate>,
146146
span: Span,
@@ -150,7 +150,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
150150
fn visit_local(&mut self, &index: &Local, context: PlaceContext, location: Location) {
151151
debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
152152
// We're only interested in temporaries and the return place
153-
match self.body.local_kind(index) {
153+
match self.ccx.body.local_kind(index) {
154154
LocalKind::Temp | LocalKind::ReturnPointer => {}
155155
LocalKind::Arg | LocalKind::Var => return,
156156
}
@@ -203,7 +203,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
203203
Rvalue::Ref(..) => {
204204
self.candidates.push(Candidate::Ref(location));
205205
}
206-
Rvalue::Repeat(..) if self.tcx.features().const_in_array_repeat_expressions => {
206+
Rvalue::Repeat(..) if self.ccx.tcx.features().const_in_array_repeat_expressions => {
207207
// FIXME(#49147) only promote the element when it isn't `Copy`
208208
// (so that code that can copy it at runtime is unaffected).
209209
self.candidates.push(Candidate::Repeat(location));
@@ -216,10 +216,10 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
216216
self.super_terminator_kind(kind, location);
217217

218218
if let TerminatorKind::Call { ref func, .. } = *kind {
219-
if let ty::FnDef(def_id, _) = func.ty(self.body, self.tcx).kind {
220-
let fn_sig = self.tcx.fn_sig(def_id);
219+
if let ty::FnDef(def_id, _) = func.ty(self.ccx.body, self.ccx.tcx).kind {
220+
let fn_sig = self.ccx.tcx.fn_sig(def_id);
221221
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = fn_sig.abi() {
222-
let name = self.tcx.item_name(def_id);
222+
let name = self.ccx.tcx.item_name(def_id);
223223
// FIXME(eddyb) use `#[rustc_args_required_const(2)]` for shuffles.
224224
if name.as_str().starts_with("simd_shuffle") {
225225
self.candidates.push(Candidate::Argument { bb: location.block, index: 2 });
@@ -228,7 +228,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
228228
}
229229
}
230230

231-
if let Some(constant_args) = args_required_const(self.tcx, def_id) {
231+
if let Some(constant_args) = args_required_const(self.ccx.tcx, def_id) {
232232
for index in constant_args {
233233
self.candidates.push(Candidate::Argument { bb: location.block, index });
234234
}
@@ -243,16 +243,14 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
243243
}
244244

245245
pub fn collect_temps_and_candidates(
246-
tcx: TyCtxt<'tcx>,
247-
body: &Body<'tcx>,
246+
ccx: &ConstCx<'mir, 'tcx>,
248247
rpo: &mut ReversePostorder<'_, 'tcx>,
249248
) -> (IndexVec<Local, TempState>, Vec<Candidate>) {
250249
let mut collector = Collector {
251-
tcx,
252-
body,
253-
temps: IndexVec::from_elem(TempState::Undefined, &body.local_decls),
250+
temps: IndexVec::from_elem(TempState::Undefined, &ccx.body.local_decls),
254251
candidates: vec![],
255-
span: body.span,
252+
span: ccx.body.span,
253+
ccx,
256254
};
257255
for (bb, data) in rpo {
258256
collector.visit_basic_block_data(bb, data);
@@ -1151,7 +1149,7 @@ crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>(
11511149
operand: &Operand<'tcx>,
11521150
) -> bool {
11531151
let mut rpo = traversal::reverse_postorder(&ccx.body);
1154-
let (temps, _) = collect_temps_and_candidates(ccx.tcx, &ccx.body, &mut rpo);
1152+
let (temps, _) = collect_temps_and_candidates(&ccx, &mut rpo);
11551153
let validator = Validator { ccx, temps: &temps, explicit: false };
11561154

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

0 commit comments

Comments
 (0)