1 use crate::mir::operand::OperandRef;
4 use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
5 use rustc_middle::ty::layout::HasTyCtxt;
6 use rustc_middle::ty::{self, Ty};
7 use rustc_span::source_map::Span;
8 use rustc_target::abi::Abi;
10 use super::FunctionCx;
12 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13 pub fn eval_mir_constant_to_operand(
16 constant: &mir::Constant<'tcx>,
17 ) -> Result<OperandRef<'tcx, Bx::Value>, ErrorHandled> {
18 let val = self.eval_mir_constant(constant)?;
19 let ty = self.monomorphize(constant.ty());
20 Ok(OperandRef::from_const(bx, val, ty))
23 pub fn eval_mir_constant(
25 constant: &mir::Constant<'tcx>,
26 ) -> Result<ConstValue<'tcx>, ErrorHandled> {
27 let ct = self.monomorphize(constant.literal);
29 mir::ConstantKind::Ty(ct) => ct,
30 mir::ConstantKind::Val(val, _) => return Ok(val),
33 ty::ConstKind::Unevaluated(ct) => self
36 .const_eval_resolve(ty::ParamEnv::reveal_all(), ct, None)
38 self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
41 ty::ConstKind::Value(value) => Ok(value),
44 "encountered bad ConstKind after monomorphizing: {:?}",
50 /// process constant containing SIMD shuffle indices
51 pub fn simd_shuffle_indices(
56 constant: Result<ConstValue<'tcx>, ErrorHandled>,
57 ) -> (Bx::Value, Ty<'tcx>) {
60 let field_ty = ty.builtin_index().unwrap();
61 let c = ty::Const::from_value(bx.tcx(), val, ty);
62 let values: Vec<_> = bx
64 .destructure_const(ty::ParamEnv::reveal_all().and(&c))
68 if let Some(prim) = field.val.try_to_scalar() {
69 let layout = bx.layout_of(field_ty);
70 let scalar = match layout.abi {
72 _ => bug!("from_const: invalid ByVal layout: {:#?}", layout),
74 bx.scalar_to_backend(prim, scalar, bx.immediate_backend_type(layout))
76 bug!("simd shuffle field {:?}", field)
80 let llval = bx.const_struct(&values, false);
84 bx.tcx().sess.span_err(span, "could not evaluate shuffle_indices at compile time");
85 // We've errored, so we don't have to produce working code.
86 let ty = self.monomorphize(ty);
87 let llty = bx.backend_type(bx.layout_of(ty));
88 (bx.const_undef(llty), ty)