rustc_codegen_ssa/mir/
statement.rs

1use rustc_middle::mir::{self, NonDivergingIntrinsic};
2use rustc_middle::span_bug;
3use tracing::instrument;
4
5use super::{FunctionCx, LocalRef};
6use crate::traits::*;
7
8impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
9    #[instrument(level = "debug", skip(self, bx))]
10    pub(crate) fn codegen_statement(&mut self, bx: &mut Bx, statement: &mir::Statement<'tcx>) {
11        self.set_debug_loc(bx, statement.source_info);
12        match statement.kind {
13            mir::StatementKind::Assign(box (ref place, ref rvalue)) => {
14                if let Some(index) = place.as_local() {
15                    match self.locals[index] {
16                        LocalRef::Place(cg_dest) => self.codegen_rvalue(bx, cg_dest, rvalue),
17                        LocalRef::UnsizedPlace(cg_indirect_dest) => {
18                            self.codegen_rvalue_unsized(bx, cg_indirect_dest, rvalue)
19                        }
20                        LocalRef::PendingOperand => {
21                            let operand = self.codegen_rvalue_operand(bx, rvalue);
22                            self.overwrite_local(index, LocalRef::Operand(operand));
23                            self.debug_introduce_local(bx, index);
24                        }
25                        LocalRef::Operand(op) => {
26                            if !op.layout.is_zst() {
27                                span_bug!(
28                                    statement.source_info.span,
29                                    "operand {:?} already assigned",
30                                    rvalue
31                                );
32                            }
33
34                            // If the type is zero-sized, it's already been set here,
35                            // but we still need to make sure we codegen the operand
36                            self.codegen_rvalue_operand(bx, rvalue);
37                        }
38                    }
39                } else {
40                    let cg_dest = self.codegen_place(bx, place.as_ref());
41                    self.codegen_rvalue(bx, cg_dest, rvalue);
42                }
43            }
44            mir::StatementKind::SetDiscriminant { box ref place, variant_index } => {
45                self.codegen_place(bx, place.as_ref()).codegen_set_discr(bx, variant_index);
46            }
47            mir::StatementKind::Deinit(..) => {
48                // For now, don't codegen this to anything. In the future it may be worth
49                // experimenting with what kind of information we can emit to LLVM without hurting
50                // perf here
51            }
52            mir::StatementKind::StorageLive(local) => {
53                if let LocalRef::Place(cg_place) = self.locals[local] {
54                    cg_place.storage_live(bx);
55                } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
56                    cg_indirect_place.storage_live(bx);
57                }
58            }
59            mir::StatementKind::StorageDead(local) => {
60                if let LocalRef::Place(cg_place) = self.locals[local] {
61                    cg_place.storage_dead(bx);
62                } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
63                    cg_indirect_place.storage_dead(bx);
64                }
65            }
66            mir::StatementKind::Coverage(ref kind) => {
67                self.codegen_coverage(bx, kind, statement.source_info.scope);
68            }
69            mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(ref op)) => {
70                let op_val = self.codegen_operand(bx, op);
71                bx.assume(op_val.immediate());
72            }
73            mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
74                mir::CopyNonOverlapping { ref count, ref src, ref dst },
75            )) => {
76                let dst_val = self.codegen_operand(bx, dst);
77                let src_val = self.codegen_operand(bx, src);
78                let count = self.codegen_operand(bx, count).immediate();
79                let pointee_layout = dst_val
80                    .layout
81                    .pointee_info_at(bx, rustc_abi::Size::ZERO)
82                    .expect("Expected pointer");
83                let bytes = bx.mul(count, bx.const_usize(pointee_layout.size.bytes()));
84
85                let align = pointee_layout.align;
86                let dst = dst_val.immediate();
87                let src = src_val.immediate();
88                bx.memcpy(dst, align, src, align, bytes, crate::MemFlags::empty());
89            }
90            mir::StatementKind::FakeRead(..)
91            | mir::StatementKind::Retag { .. }
92            | mir::StatementKind::AscribeUserType(..)
93            | mir::StatementKind::ConstEvalCounter
94            | mir::StatementKind::PlaceMention(..)
95            | mir::StatementKind::BackwardIncompatibleDropHint { .. }
96            | mir::StatementKind::Nop => {}
97        }
98    }
99}