rustc_codegen_ssa/mir/
statement.rs

1use rustc_middle::mir::{self, NonDivergingIntrinsic, StmtDebugInfo};
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.codegen_stmt_debuginfos(bx, &statement.debuginfos);
12        self.set_debug_loc(bx, statement.source_info);
13        match statement.kind {
14            mir::StatementKind::Assign(box (ref place, ref rvalue)) => {
15                if let Some(index) = place.as_local() {
16                    match self.locals[index] {
17                        LocalRef::Place(cg_dest) => self.codegen_rvalue(bx, cg_dest, rvalue),
18                        LocalRef::UnsizedPlace(cg_indirect_dest) => {
19                            let ty = cg_indirect_dest.layout.ty;
20                            span_bug!(
21                                statement.source_info.span,
22                                "cannot reallocate from `UnsizedPlace({ty})` \
23                                into `{rvalue:?}`; dynamic alloca is not supported",
24                            );
25                        }
26                        LocalRef::PendingOperand => {
27                            let operand = self.codegen_rvalue_operand(bx, rvalue);
28                            self.overwrite_local(index, LocalRef::Operand(operand));
29                            self.debug_introduce_local(bx, index);
30                        }
31                        LocalRef::Operand(op) => {
32                            if !op.layout.is_zst() {
33                                span_bug!(
34                                    statement.source_info.span,
35                                    "operand {:?} already assigned",
36                                    rvalue
37                                );
38                            }
39
40                            // If the type is zero-sized, it's already been set here,
41                            // but we still need to make sure we codegen the operand
42                            self.codegen_rvalue_operand(bx, rvalue);
43                        }
44                    }
45                } else {
46                    let cg_dest = self.codegen_place(bx, place.as_ref());
47                    self.codegen_rvalue(bx, cg_dest, rvalue);
48                }
49            }
50            mir::StatementKind::SetDiscriminant { box ref place, variant_index } => {
51                self.codegen_place(bx, place.as_ref()).codegen_set_discr(bx, variant_index);
52            }
53            mir::StatementKind::Deinit(..) => {
54                // For now, don't codegen this to anything. In the future it may be worth
55                // experimenting with what kind of information we can emit to LLVM without hurting
56                // perf here
57            }
58            mir::StatementKind::StorageLive(local) => {
59                if let LocalRef::Place(cg_place) = self.locals[local] {
60                    cg_place.storage_live(bx);
61                } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
62                    cg_indirect_place.storage_live(bx);
63                }
64            }
65            mir::StatementKind::StorageDead(local) => {
66                if let LocalRef::Place(cg_place) = self.locals[local] {
67                    cg_place.storage_dead(bx);
68                } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
69                    cg_indirect_place.storage_dead(bx);
70                }
71            }
72            mir::StatementKind::Coverage(ref kind) => {
73                self.codegen_coverage(bx, kind, statement.source_info.scope);
74            }
75            mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(ref op)) => {
76                let op_val = self.codegen_operand(bx, op);
77                bx.assume(op_val.immediate());
78            }
79            mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(
80                mir::CopyNonOverlapping { ref count, ref src, ref dst },
81            )) => {
82                let dst_val = self.codegen_operand(bx, dst);
83                let src_val = self.codegen_operand(bx, src);
84                let count = self.codegen_operand(bx, count).immediate();
85                let pointee_layout = dst_val
86                    .layout
87                    .pointee_info_at(bx, rustc_abi::Size::ZERO)
88                    .expect("Expected pointer");
89                let bytes = bx.mul(count, bx.const_usize(pointee_layout.size.bytes()));
90
91                let align = pointee_layout.align;
92                let dst = dst_val.immediate();
93                let src = src_val.immediate();
94                bx.memcpy(dst, align, src, align, bytes, crate::MemFlags::empty(), None);
95            }
96            mir::StatementKind::FakeRead(..)
97            | mir::StatementKind::Retag { .. }
98            | mir::StatementKind::AscribeUserType(..)
99            | mir::StatementKind::ConstEvalCounter
100            | mir::StatementKind::PlaceMention(..)
101            | mir::StatementKind::BackwardIncompatibleDropHint { .. }
102            | mir::StatementKind::Nop => {}
103        }
104    }
105
106    pub(crate) fn codegen_stmt_debuginfo(&mut self, bx: &mut Bx, debuginfo: &StmtDebugInfo<'tcx>) {
107        match debuginfo {
108            StmtDebugInfo::AssignRef(dest, place) => {
109                let local_ref = match self.locals[place.local] {
110                    // For an rvalue like `&(_1.1)`, when `BackendRepr` is `BackendRepr::Memory`, we allocate a block of memory to this place.
111                    // The place is an indirect pointer, we can refer to it directly.
112                    LocalRef::Place(place_ref) => Some((place_ref, place.projection.as_slice())),
113                    // For an rvalue like `&((*_1).1)`, we are calculating the address of `_1.1`.
114                    // The deref projection is no-op here.
115                    LocalRef::Operand(operand_ref) if place.is_indirect_first_projection() => {
116                        Some((operand_ref.deref(bx.cx()), &place.projection[1..]))
117                    }
118                    // For an rvalue like `&1`, when `BackendRepr` is `BackendRepr::Scalar`,
119                    // we cannot get the address.
120                    // N.B. `non_ssa_locals` returns that this is an SSA local.
121                    LocalRef::Operand(_) => None,
122                    LocalRef::UnsizedPlace(_) | LocalRef::PendingOperand => None,
123                }
124                .filter(|(_, projection)| {
125                    // Drop unsupported projections.
126                    projection.iter().all(|p| p.can_use_in_debuginfo())
127                });
128                if let Some((base, projection)) = local_ref {
129                    self.debug_new_val_to_local(bx, *dest, base, projection);
130                } else {
131                    // If the address cannot be calculated, use poison to indicate that the value has been optimized out.
132                    self.debug_poison_to_local(bx, *dest);
133                }
134            }
135            StmtDebugInfo::InvalidAssign(local) => {
136                self.debug_poison_to_local(bx, *local);
137            }
138        }
139    }
140
141    pub(crate) fn codegen_stmt_debuginfos(
142        &mut self,
143        bx: &mut Bx,
144        debuginfos: &[StmtDebugInfo<'tcx>],
145    ) {
146        for debuginfo in debuginfos {
147            self.codegen_stmt_debuginfo(bx, debuginfo);
148        }
149    }
150}