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