]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/constant.rs
cranelift changes
[rust.git] / compiler / rustc_codegen_cranelift / src / constant.rs
1 //! Handling of `static`s, `const`s and promoted allocations
2
3 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
5 use rustc_middle::mir::interpret::{
6     read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
7 };
8 use rustc_middle::ty::ConstKind;
9 use rustc_span::DUMMY_SP;
10
11 use cranelift_codegen::ir::GlobalValueData;
12 use cranelift_module::*;
13
14 use crate::prelude::*;
15
16 pub(crate) struct ConstantCx {
17     todo: Vec<TodoItem>,
18     done: FxHashSet<DataId>,
19     anon_allocs: FxHashMap<AllocId, DataId>,
20 }
21
22 #[derive(Copy, Clone, Debug)]
23 enum TodoItem {
24     Alloc(AllocId),
25     Static(DefId),
26 }
27
28 impl ConstantCx {
29     pub(crate) fn new() -> Self {
30         ConstantCx { todo: vec![], done: FxHashSet::default(), anon_allocs: FxHashMap::default() }
31     }
32
33     pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut dyn Module) {
34         //println!("todo {:?}", self.todo);
35         define_all_allocs(tcx, module, &mut self);
36         //println!("done {:?}", self.done);
37         self.done.clear();
38     }
39 }
40
41 pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
42     let mut all_constants_ok = true;
43     for constant in &fx.mir.required_consts {
44         let unevaluated = match fx.monomorphize(constant.literal) {
45             ConstantKind::Ty(ct) => match ct.kind() {
46                 ConstKind::Unevaluated(uv) => uv.expand(),
47                 ConstKind::Value(_) => continue,
48                 ConstKind::Param(_)
49                 | ConstKind::Infer(_)
50                 | ConstKind::Bound(_, _)
51                 | ConstKind::Placeholder(_)
52                 | ConstKind::Error(_) => unreachable!("{:?}", ct),
53             },
54             ConstantKind::Unevaluated(uv, _) => uv,
55             ConstantKind::Val(..) => continue,
56         };
57
58         if let Err(err) = fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {
59             all_constants_ok = false;
60             match err {
61                 ErrorHandled::Reported(_) | ErrorHandled::Linted => {
62                     fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
63                 }
64                 ErrorHandled::TooGeneric => {
65                     span_bug!(constant.span, "codegen encountered polymorphic constant: {:?}", err);
66                 }
67             }
68         }
69     }
70     all_constants_ok
71 }
72
73 pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) {
74     let mut constants_cx = ConstantCx::new();
75     constants_cx.todo.push(TodoItem::Static(def_id));
76     constants_cx.finalize(tcx, module);
77 }
78
79 pub(crate) fn codegen_tls_ref<'tcx>(
80     fx: &mut FunctionCx<'_, '_, 'tcx>,
81     def_id: DefId,
82     layout: TyAndLayout<'tcx>,
83 ) -> CValue<'tcx> {
84     let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
85     let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
86     if fx.clif_comments.enabled() {
87         fx.add_comment(local_data_id, format!("tls {:?}", def_id));
88     }
89     let tls_ptr = fx.bcx.ins().tls_value(fx.pointer_type, local_data_id);
90     CValue::by_val(tls_ptr, layout)
91 }
92
93 fn codegen_static_ref<'tcx>(
94     fx: &mut FunctionCx<'_, '_, 'tcx>,
95     def_id: DefId,
96     layout: TyAndLayout<'tcx>,
97 ) -> CPlace<'tcx> {
98     let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
99     let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
100     if fx.clif_comments.enabled() {
101         fx.add_comment(local_data_id, format!("{:?}", def_id));
102     }
103     let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
104     assert!(!layout.is_unsized(), "unsized statics aren't supported");
105     assert!(
106         matches!(
107             fx.bcx.func.global_values[local_data_id],
108             GlobalValueData::Symbol { tls: false, .. }
109         ),
110         "tls static referenced without Rvalue::ThreadLocalRef"
111     );
112     CPlace::for_ptr(crate::pointer::Pointer::new(global_ptr), layout)
113 }
114
115 pub(crate) fn codegen_constant<'tcx>(
116     fx: &mut FunctionCx<'_, '_, 'tcx>,
117     constant: &Constant<'tcx>,
118 ) -> CValue<'tcx> {
119     let (const_val, ty) = match fx.monomorphize(constant.literal) {
120         ConstantKind::Ty(const_) => match const_.kind() {
121             ConstKind::Value(valtree) => {
122                 (fx.tcx.valtree_to_const_val((const_.ty(), valtree)), const_.ty())
123             }
124             ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
125                 if fx.tcx.is_static(def.did) =>
126             {
127                 assert!(substs.is_empty());
128                 assert_eq!(promoted, ());
129                 return codegen_static_ref(fx, def.did, fx.layout_of(const_.ty())).to_cvalue(fx);
130             }
131             ConstKind::Unevaluated(unevaluated) => {
132                 match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated.expand(), None)
133                 {
134                     Ok(const_val) => (const_val, const_.ty()),
135                     Err(_) => {
136                         span_bug!(
137                             constant.span,
138                             "erroneous constant not captured by required_consts"
139                         );
140                     }
141                 }
142             }
143             ConstKind::Param(_)
144             | ConstKind::Infer(_)
145             | ConstKind::Bound(_, _)
146             | ConstKind::Placeholder(_)
147             | ConstKind::Error(_) => unreachable!("{:?}", const_),
148         },
149         ConstantKind::Unevaluated(ty::Unevaluated { def, substs, promoted }, ty)
150             if fx.tcx.is_static(def.did) =>
151         {
152             assert!(substs.is_empty());
153             assert!(promoted.is_none());
154
155             return codegen_static_ref(fx, def.did, fx.layout_of(ty)).to_cvalue(fx);
156         }
157         ConstantKind::Unevaluated(unevaluated, ty) => {
158             match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {
159                 Ok(const_val) => (const_val, ty),
160                 Err(_) => {
161                     span_bug!(constant.span, "erroneous constant not captured by required_consts");
162                 }
163             }
164         }
165         ConstantKind::Val(val, ty) => (val, ty),
166     };
167
168     codegen_const_value(fx, const_val, ty)
169 }
170
171 pub(crate) fn codegen_const_value<'tcx>(
172     fx: &mut FunctionCx<'_, '_, 'tcx>,
173     const_val: ConstValue<'tcx>,
174     ty: Ty<'tcx>,
175 ) -> CValue<'tcx> {
176     let layout = fx.layout_of(ty);
177     assert!(!layout.is_unsized(), "sized const value");
178
179     if layout.is_zst() {
180         return CValue::by_ref(crate::Pointer::dangling(layout.align.pref), layout);
181     }
182
183     match const_val {
184         ConstValue::ZeroSized => unreachable!(), // we already handles ZST above
185         ConstValue::Scalar(x) => match x {
186             Scalar::Int(int) => {
187                 if fx.clif_type(layout.ty).is_some() {
188                     return CValue::const_val(fx, layout, int);
189                 } else {
190                     let raw_val = int.to_bits(int.size()).unwrap();
191                     let val = match int.size().bytes() {
192                         1 => fx.bcx.ins().iconst(types::I8, raw_val as i64),
193                         2 => fx.bcx.ins().iconst(types::I16, raw_val as i64),
194                         4 => fx.bcx.ins().iconst(types::I32, raw_val as i64),
195                         8 => fx.bcx.ins().iconst(types::I64, raw_val as i64),
196                         16 => {
197                             let lsb = fx.bcx.ins().iconst(types::I64, raw_val as u64 as i64);
198                             let msb =
199                                 fx.bcx.ins().iconst(types::I64, (raw_val >> 64) as u64 as i64);
200                             fx.bcx.ins().iconcat(lsb, msb)
201                         }
202                         _ => unreachable!(),
203                     };
204
205                     let place = CPlace::new_stack_slot(fx, layout);
206                     place.to_ptr().store(fx, val, MemFlags::trusted());
207                     place.to_cvalue(fx)
208                 }
209             }
210             Scalar::Ptr(ptr, _size) => {
211                 let (alloc_id, offset) = ptr.into_parts(); // we know the `offset` is relative
212                 let base_addr = match fx.tcx.global_alloc(alloc_id) {
213                     GlobalAlloc::Memory(alloc) => {
214                         let data_id = data_id_for_alloc_id(
215                             &mut fx.constants_cx,
216                             fx.module,
217                             alloc_id,
218                             alloc.inner().mutability,
219                         );
220                         let local_data_id =
221                             fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
222                         if fx.clif_comments.enabled() {
223                             fx.add_comment(local_data_id, format!("{:?}", alloc_id));
224                         }
225                         fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
226                     }
227                     GlobalAlloc::Function(instance) => {
228                         let func_id = crate::abi::import_function(fx.tcx, fx.module, instance);
229                         let local_func_id =
230                             fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
231                         fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
232                     }
233                     GlobalAlloc::VTable(ty, trait_ref) => {
234                         let alloc_id = fx.tcx.vtable_allocation((ty, trait_ref));
235                         let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
236                         // FIXME: factor this common code with the `Memory` arm into a function?
237                         let data_id = data_id_for_alloc_id(
238                             &mut fx.constants_cx,
239                             fx.module,
240                             alloc_id,
241                             alloc.inner().mutability,
242                         );
243                         let local_data_id =
244                             fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
245                         fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
246                     }
247                     GlobalAlloc::Static(def_id) => {
248                         assert!(fx.tcx.is_static(def_id));
249                         let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
250                         let local_data_id =
251                             fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
252                         if fx.clif_comments.enabled() {
253                             fx.add_comment(local_data_id, format!("{:?}", def_id));
254                         }
255                         fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
256                     }
257                 };
258                 let val = if offset.bytes() != 0 {
259                     fx.bcx.ins().iadd_imm(base_addr, i64::try_from(offset.bytes()).unwrap())
260                 } else {
261                     base_addr
262                 };
263                 CValue::by_val(val, layout)
264             }
265         },
266         ConstValue::ByRef { alloc, offset } => CValue::by_ref(
267             pointer_for_allocation(fx, alloc)
268                 .offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
269             layout,
270         ),
271         ConstValue::Slice { data, start, end } => {
272             let ptr = pointer_for_allocation(fx, data)
273                 .offset_i64(fx, i64::try_from(start).unwrap())
274                 .get_addr(fx);
275             let len = fx
276                 .bcx
277                 .ins()
278                 .iconst(fx.pointer_type, i64::try_from(end.checked_sub(start).unwrap()).unwrap());
279             CValue::by_val_pair(ptr, len, layout)
280         }
281     }
282 }
283
284 pub(crate) fn pointer_for_allocation<'tcx>(
285     fx: &mut FunctionCx<'_, '_, 'tcx>,
286     alloc: ConstAllocation<'tcx>,
287 ) -> crate::pointer::Pointer {
288     let alloc_id = fx.tcx.create_memory_alloc(alloc);
289     let data_id = data_id_for_alloc_id(
290         &mut fx.constants_cx,
291         &mut *fx.module,
292         alloc_id,
293         alloc.inner().mutability,
294     );
295
296     let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
297     if fx.clif_comments.enabled() {
298         fx.add_comment(local_data_id, format!("{:?}", alloc_id));
299     }
300     let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
301     crate::pointer::Pointer::new(global_ptr)
302 }
303
304 pub(crate) fn data_id_for_alloc_id(
305     cx: &mut ConstantCx,
306     module: &mut dyn Module,
307     alloc_id: AllocId,
308     mutability: rustc_hir::Mutability,
309 ) -> DataId {
310     cx.todo.push(TodoItem::Alloc(alloc_id));
311     *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
312         module.declare_anonymous_data(mutability == rustc_hir::Mutability::Mut, false).unwrap()
313     })
314 }
315
316 fn data_id_for_static(
317     tcx: TyCtxt<'_>,
318     module: &mut dyn Module,
319     def_id: DefId,
320     definition: bool,
321 ) -> DataId {
322     let rlinkage = tcx.codegen_fn_attrs(def_id).linkage;
323     let linkage = if definition {
324         crate::linkage::get_static_linkage(tcx, def_id)
325     } else if rlinkage == Some(rustc_middle::mir::mono::Linkage::ExternalWeak)
326         || rlinkage == Some(rustc_middle::mir::mono::Linkage::WeakAny)
327     {
328         Linkage::Preemptible
329     } else {
330         Linkage::Import
331     };
332
333     let instance = Instance::mono(tcx, def_id).polymorphize(tcx);
334     let symbol_name = tcx.symbol_name(instance).name;
335     let ty = instance.ty(tcx, ParamEnv::reveal_all());
336     let is_mutable = if tcx.is_mutable_static(def_id) {
337         true
338     } else {
339         !ty.is_freeze(tcx.at(DUMMY_SP), ParamEnv::reveal_all())
340     };
341     let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes();
342
343     let attrs = tcx.codegen_fn_attrs(def_id);
344
345     let data_id = match module.declare_data(
346         &*symbol_name,
347         linkage,
348         is_mutable,
349         attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
350     ) {
351         Ok(data_id) => data_id,
352         Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!(
353             "attempt to declare `{symbol_name}` as static, but it was already declared as function"
354         )),
355         Err(err) => Err::<_, _>(err).unwrap(),
356     };
357
358     if rlinkage.is_some() {
359         // Comment copied from https://github.com/rust-lang/rust/blob/45060c2a66dfd667f88bd8b94261b28a58d85bd5/src/librustc_codegen_llvm/consts.rs#L141
360         // Declare an internal global `extern_with_linkage_foo` which
361         // is initialized with the address of `foo`.  If `foo` is
362         // discarded during linking (for example, if `foo` has weak
363         // linkage and there are no definitions), then
364         // `extern_with_linkage_foo` will instead be initialized to
365         // zero.
366
367         let ref_name = format!("_rust_extern_with_linkage_{}", symbol_name);
368         let ref_data_id = module.declare_data(&ref_name, Linkage::Local, false, false).unwrap();
369         let mut data_ctx = DataContext::new();
370         data_ctx.set_align(align);
371         let data = module.declare_data_in_data(data_id, &mut data_ctx);
372         data_ctx.define(std::iter::repeat(0).take(pointer_ty(tcx).bytes() as usize).collect());
373         data_ctx.write_data_addr(0, data, 0);
374         match module.define_data(ref_data_id, &data_ctx) {
375             // Every time the static is referenced there will be another definition of this global,
376             // so duplicate definitions are expected and allowed.
377             Err(ModuleError::DuplicateDefinition(_)) => {}
378             res => res.unwrap(),
379         }
380         ref_data_id
381     } else {
382         data_id
383     }
384 }
385
386 fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut ConstantCx) {
387     while let Some(todo_item) = cx.todo.pop() {
388         let (data_id, alloc, section_name) = match todo_item {
389             TodoItem::Alloc(alloc_id) => {
390                 let alloc = match tcx.global_alloc(alloc_id) {
391                     GlobalAlloc::Memory(alloc) => alloc,
392                     GlobalAlloc::Function(_) | GlobalAlloc::Static(_) | GlobalAlloc::VTable(..) => {
393                         unreachable!()
394                     }
395                 };
396                 let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
397                     module
398                         .declare_anonymous_data(
399                             alloc.inner().mutability == rustc_hir::Mutability::Mut,
400                             false,
401                         )
402                         .unwrap()
403                 });
404                 (data_id, alloc, None)
405             }
406             TodoItem::Static(def_id) => {
407                 //println!("static {:?}", def_id);
408
409                 let section_name = tcx.codegen_fn_attrs(def_id).link_section;
410
411                 let alloc = tcx.eval_static_initializer(def_id).unwrap();
412
413                 let data_id = data_id_for_static(tcx, module, def_id, true);
414                 (data_id, alloc, section_name)
415             }
416         };
417
418         //("data_id {}", data_id);
419         if cx.done.contains(&data_id) {
420             continue;
421         }
422
423         let mut data_ctx = DataContext::new();
424         let alloc = alloc.inner();
425         data_ctx.set_align(alloc.align.bytes());
426
427         if let Some(section_name) = section_name {
428             let (segment_name, section_name) = if tcx.sess.target.is_like_osx {
429                 let section_name = section_name.as_str();
430                 if let Some(names) = section_name.split_once(',') {
431                     names
432                 } else {
433                     tcx.sess.fatal(&format!(
434                         "#[link_section = \"{}\"] is not valid for macos target: must be segment and section separated by comma",
435                         section_name
436                     ));
437                 }
438             } else {
439                 ("", section_name.as_str())
440             };
441             data_ctx.set_segment_section(segment_name, section_name);
442         }
443
444         let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len()).to_vec();
445         data_ctx.define(bytes.into_boxed_slice());
446
447         for &(offset, alloc_id) in alloc.provenance().iter() {
448             let addend = {
449                 let endianness = tcx.data_layout.endian;
450                 let offset = offset.bytes() as usize;
451                 let ptr_size = tcx.data_layout.pointer_size;
452                 let bytes = &alloc.inspect_with_uninit_and_ptr_outside_interpreter(
453                     offset..offset + ptr_size.bytes() as usize,
454                 );
455                 read_target_uint(endianness, bytes).unwrap()
456             };
457
458             let reloc_target_alloc = tcx.global_alloc(alloc_id);
459             let data_id = match reloc_target_alloc {
460                 GlobalAlloc::Function(instance) => {
461                     assert_eq!(addend, 0);
462                     let func_id =
463                         crate::abi::import_function(tcx, module, instance.polymorphize(tcx));
464                     let local_func_id = module.declare_func_in_data(func_id, &mut data_ctx);
465                     data_ctx.write_function_addr(offset.bytes() as u32, local_func_id);
466                     continue;
467                 }
468                 GlobalAlloc::Memory(target_alloc) => {
469                     data_id_for_alloc_id(cx, module, alloc_id, target_alloc.inner().mutability)
470                 }
471                 GlobalAlloc::VTable(ty, trait_ref) => {
472                     let alloc_id = tcx.vtable_allocation((ty, trait_ref));
473                     data_id_for_alloc_id(cx, module, alloc_id, Mutability::Not)
474                 }
475                 GlobalAlloc::Static(def_id) => {
476                     if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
477                     {
478                         tcx.sess.fatal(&format!(
479                             "Allocation {:?} contains reference to TLS value {:?}",
480                             alloc, def_id
481                         ));
482                     }
483
484                     // Don't push a `TodoItem::Static` here, as it will cause statics used by
485                     // multiple crates to be duplicated between them. It isn't necessary anyway,
486                     // as it will get pushed by `codegen_static` when necessary.
487                     data_id_for_static(tcx, module, def_id, false)
488                 }
489             };
490
491             let global_value = module.declare_data_in_data(data_id, &mut data_ctx);
492             data_ctx.write_data_addr(offset.bytes() as u32, global_value, addend as i64);
493         }
494
495         module.define_data(data_id, &data_ctx).unwrap();
496         cx.done.insert(data_id);
497     }
498
499     assert!(cx.todo.is_empty(), "{:?}", cx.todo);
500 }
501
502 pub(crate) fn mir_operand_get_const_val<'tcx>(
503     fx: &FunctionCx<'_, '_, 'tcx>,
504     operand: &Operand<'tcx>,
505 ) -> Option<ConstValue<'tcx>> {
506     match operand {
507         Operand::Constant(const_) => match const_.literal {
508             ConstantKind::Ty(const_) => fx
509                 .monomorphize(const_)
510                 .eval_for_mir(fx.tcx, ParamEnv::reveal_all())
511                 .try_to_value(fx.tcx),
512             ConstantKind::Val(val, _) => Some(val),
513             ConstantKind::Unevaluated(uv, _) => {
514                 fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), uv, None).ok()
515             }
516         },
517         // FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
518         // inside a temporary before being passed to the intrinsic requiring the const argument.
519         // This code tries to find a single constant defining definition of the referenced local.
520         Operand::Copy(place) | Operand::Move(place) => {
521             if !place.projection.is_empty() {
522                 return None;
523             }
524             let mut computed_const_val = None;
525             for bb_data in fx.mir.basic_blocks.iter() {
526                 for stmt in &bb_data.statements {
527                     match &stmt.kind {
528                         StatementKind::Assign(local_and_rvalue) if &local_and_rvalue.0 == place => {
529                             match &local_and_rvalue.1 {
530                                 Rvalue::Cast(CastKind::Misc, operand, ty) => {
531                                     if computed_const_val.is_some() {
532                                         return None; // local assigned twice
533                                     }
534                                     if !matches!(ty.kind(), ty::Uint(_) | ty::Int(_)) {
535                                         return None;
536                                     }
537                                     let const_val = mir_operand_get_const_val(fx, operand)?;
538                                     if fx.layout_of(*ty).size
539                                         != const_val.try_to_scalar_int()?.size()
540                                     {
541                                         return None;
542                                     }
543                                     computed_const_val = Some(const_val);
544                                 }
545                                 Rvalue::Use(operand) => {
546                                     computed_const_val = mir_operand_get_const_val(fx, operand)
547                                 }
548                                 _ => return None,
549                             }
550                         }
551                         StatementKind::SetDiscriminant { place: stmt_place, variant_index: _ }
552                             if &**stmt_place == place =>
553                         {
554                             return None;
555                         }
556                         StatementKind::Intrinsic(ref intrinsic) => match **intrinsic {
557                             NonDivergingIntrinsic::CopyNonOverlapping(..) => return None,
558                             NonDivergingIntrinsic::Assume(..) => {}
559                         },
560                         // conservative handling
561                         StatementKind::Assign(_)
562                         | StatementKind::FakeRead(_)
563                         | StatementKind::SetDiscriminant { .. }
564                         | StatementKind::Deinit(_)
565                         | StatementKind::StorageLive(_)
566                         | StatementKind::StorageDead(_)
567                         | StatementKind::Retag(_, _)
568                         | StatementKind::AscribeUserType(_, _)
569                         | StatementKind::Coverage(_)
570                         | StatementKind::Nop => {}
571                     }
572                 }
573                 match &bb_data.terminator().kind {
574                     TerminatorKind::Goto { .. }
575                     | TerminatorKind::SwitchInt { .. }
576                     | TerminatorKind::Resume
577                     | TerminatorKind::Abort
578                     | TerminatorKind::Return
579                     | TerminatorKind::Unreachable
580                     | TerminatorKind::Drop { .. }
581                     | TerminatorKind::Assert { .. } => {}
582                     TerminatorKind::DropAndReplace { .. }
583                     | TerminatorKind::Yield { .. }
584                     | TerminatorKind::GeneratorDrop
585                     | TerminatorKind::FalseEdge { .. }
586                     | TerminatorKind::FalseUnwind { .. } => unreachable!(),
587                     TerminatorKind::InlineAsm { .. } => return None,
588                     TerminatorKind::Call { destination, target: Some(_), .. }
589                         if destination == place =>
590                     {
591                         return None;
592                     }
593                     TerminatorKind::Call { .. } => {}
594                 }
595             }
596             computed_const_val
597         }
598     }
599 }