|
| 1 | +//! Lowers intrinsic calls |
| 2 | +
|
| 3 | +use crate::transform::MirPass; |
| 4 | +use rustc_middle::mir::*; |
| 5 | +use rustc_middle::ty::subst::SubstsRef; |
| 6 | +use rustc_middle::ty::{self, Ty, TyCtxt}; |
| 7 | +use rustc_span::symbol::{sym, Symbol}; |
| 8 | +use rustc_target::spec::abi::Abi; |
| 9 | + |
| 10 | +pub struct LowerIntrinsics; |
| 11 | + |
| 12 | +impl<'tcx> MirPass<'tcx> for LowerIntrinsics { |
| 13 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
| 14 | + for block in body.basic_blocks_mut() { |
| 15 | + let terminator = block.terminator.as_mut().unwrap(); |
| 16 | + if let TerminatorKind::Call { |
| 17 | + func: Operand::Constant(box Constant { literal: ty::Const { ty: func_ty, .. }, .. }), |
| 18 | + args, |
| 19 | + destination, |
| 20 | + .. |
| 21 | + } = &mut terminator.kind |
| 22 | + { |
| 23 | + let (intrinsic_name, substs) = match resolve_rust_intrinsic(tcx, func_ty) { |
| 24 | + None => continue, |
| 25 | + Some(it) => it, |
| 26 | + }; |
| 27 | + match intrinsic_name { |
| 28 | + sym::unreachable => { |
| 29 | + terminator.kind = TerminatorKind::Unreachable; |
| 30 | + } |
| 31 | + sym::forget => { |
| 32 | + if let Some((destination, target)) = *destination { |
| 33 | + block.statements.push(Statement { |
| 34 | + source_info: terminator.source_info, |
| 35 | + kind: StatementKind::Assign(box ( |
| 36 | + destination, |
| 37 | + Rvalue::Use(Operand::Constant(box Constant { |
| 38 | + span: terminator.source_info.span, |
| 39 | + user_ty: None, |
| 40 | + literal: ty::Const::zero_sized(tcx, tcx.types.unit), |
| 41 | + })), |
| 42 | + )), |
| 43 | + }); |
| 44 | + terminator.kind = TerminatorKind::Goto { target }; |
| 45 | + } |
| 46 | + } |
| 47 | + sym::wrapping_add | sym::wrapping_sub | sym::wrapping_mul => { |
| 48 | + if let Some((destination, target)) = *destination { |
| 49 | + let lhs; |
| 50 | + let rhs; |
| 51 | + { |
| 52 | + let mut args = args.drain(..); |
| 53 | + lhs = args.next().unwrap(); |
| 54 | + rhs = args.next().unwrap(); |
| 55 | + } |
| 56 | + let bin_op = match intrinsic_name { |
| 57 | + sym::wrapping_add => BinOp::Add, |
| 58 | + sym::wrapping_sub => BinOp::Sub, |
| 59 | + sym::wrapping_mul => BinOp::Mul, |
| 60 | + _ => bug!("unexpected intrinsic"), |
| 61 | + }; |
| 62 | + block.statements.push(Statement { |
| 63 | + source_info: terminator.source_info, |
| 64 | + kind: StatementKind::Assign(box ( |
| 65 | + destination, |
| 66 | + Rvalue::BinaryOp(bin_op, lhs, rhs), |
| 67 | + )), |
| 68 | + }); |
| 69 | + terminator.kind = TerminatorKind::Goto { target }; |
| 70 | + } |
| 71 | + } |
| 72 | + sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => { |
| 73 | + // The checked binary operations are not suitable target for lowering here, |
| 74 | + // since their semantics depend on the value of overflow-checks flag used |
| 75 | + // during codegen. Issue #35310. |
| 76 | + } |
| 77 | + sym::size_of => { |
| 78 | + if let Some((destination, target)) = *destination { |
| 79 | + let tp_ty = substs.type_at(0); |
| 80 | + block.statements.push(Statement { |
| 81 | + source_info: terminator.source_info, |
| 82 | + kind: StatementKind::Assign(box ( |
| 83 | + destination, |
| 84 | + Rvalue::NullaryOp(NullOp::SizeOf, tp_ty), |
| 85 | + )), |
| 86 | + }); |
| 87 | + terminator.kind = TerminatorKind::Goto { target }; |
| 88 | + } |
| 89 | + } |
| 90 | + _ => {} |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn resolve_rust_intrinsic( |
| 98 | + tcx: TyCtxt<'tcx>, |
| 99 | + func_ty: Ty<'tcx>, |
| 100 | +) -> Option<(Symbol, SubstsRef<'tcx>)> { |
| 101 | + if let ty::FnDef(def_id, substs) = *func_ty.kind() { |
| 102 | + let fn_sig = func_ty.fn_sig(tcx); |
| 103 | + if fn_sig.abi() == Abi::RustIntrinsic { |
| 104 | + return Some((tcx.item_name(def_id), substs)); |
| 105 | + } |
| 106 | + } |
| 107 | + None |
| 108 | +} |
0 commit comments