]> git.lizzy.rs Git - rust.git/blobdiff - src/common.rs
Rollup merge of #81618 - bjorn3:sync_cg_clif-2021-02-01, r=bjorn3
[rust.git] / src / common.rs
index 27e1b1cc3b0320679a8afde0ade9051e9e9c13fb..fbee84e09f7a6e40625dc9728b3eb4dc73e6d20c 100644 (file)
@@ -1,15 +1,12 @@
+use rustc_index::vec::IndexVec;
+use rustc_target::abi::call::FnAbi;
 use rustc_target::abi::{Integer, Primitive};
 use rustc_target::spec::{HasTargetSpec, Target};
-use rustc_index::vec::IndexVec;
 
 use cranelift_codegen::ir::{InstructionData, Opcode, ValueDef};
 
 use crate::prelude::*;
 
-pub(crate) fn mir_var(loc: Local) -> Variable {
-    Variable::with_u32(loc.index() as u32)
-}
-
 pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type {
     match tcx.data_layout.pointer_size.bits() {
         16 => types::I16,
@@ -35,7 +32,7 @@ pub(crate) fn scalar_to_clif_type(tcx: TyCtxt<'_>, scalar: Scalar) -> Type {
 }
 
 fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Type> {
-    Some(match ty.kind {
+    Some(match ty.kind() {
         ty::Bool => types::I8,
         ty::Uint(size) => match size {
             UintTy::U8 => types::I8,
@@ -59,75 +56,83 @@ fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Typ
             FloatTy::F64 => types::F64,
         },
         ty::FnPtr(_) => pointer_ty(tcx),
-        ty::RawPtr(TypeAndMut { ty: pointee_ty, mutbl: _ }) | ty::Ref(_, pointee_ty, _) => {
+        ty::RawPtr(TypeAndMut {
+            ty: pointee_ty,
+            mutbl: _,
+        })
+        | ty::Ref(_, pointee_ty, _) => {
             if has_ptr_meta(tcx, pointee_ty) {
                 return None;
             } else {
                 pointer_ty(tcx)
             }
         }
+        ty::Adt(adt_def, _) if adt_def.repr.simd() => {
+            let (element, count) = match &tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().abi
+            {
+                Abi::Vector { element, count } => (element.clone(), *count),
+                _ => unreachable!(),
+            };
+
+            match scalar_to_clif_type(tcx, element).by(u16::try_from(count).unwrap()) {
+                // Cranelift currently only implements icmp for 128bit vectors.
+                Some(vector_ty) if vector_ty.bits() == 128 => vector_ty,
+                _ => return None,
+            }
+        }
         ty::Param(_) => bug!("ty param {:?}", ty),
         _ => return None,
     })
 }
 
+fn clif_pair_type_from_ty<'tcx>(
+    tcx: TyCtxt<'tcx>,
+    ty: Ty<'tcx>,
+) -> Option<(types::Type, types::Type)> {
+    Some(match ty.kind() {
+        ty::Tuple(substs) if substs.len() == 2 => {
+            let mut types = substs.types();
+            let a = clif_type_from_ty(tcx, types.next().unwrap())?;
+            let b = clif_type_from_ty(tcx, types.next().unwrap())?;
+            if a.is_vector() || b.is_vector() {
+                return None;
+            }
+            (a, b)
+        }
+        ty::RawPtr(TypeAndMut {
+            ty: pointee_ty,
+            mutbl: _,
+        })
+        | ty::Ref(_, pointee_ty, _) => {
+            if has_ptr_meta(tcx, pointee_ty) {
+                (pointer_ty(tcx), pointer_ty(tcx))
+            } else {
+                return None;
+            }
+        }
+        _ => return None,
+    })
+}
+
 /// Is a pointer to this type a fat ptr?
 pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
-    let ptr_ty = tcx.mk_ptr(TypeAndMut { ty, mutbl: rustc_hir::Mutability::Not });
-    match &tcx.layout_of(ParamEnv::reveal_all().and(ptr_ty)).unwrap().abi {
+    let ptr_ty = tcx.mk_ptr(TypeAndMut {
+        ty,
+        mutbl: rustc_hir::Mutability::Not,
+    });
+    match &tcx
+        .layout_of(ParamEnv::reveal_all().and(ptr_ty))
+        .unwrap()
+        .abi
+    {
         Abi::Scalar(_) => false,
         Abi::ScalarPair(_, _) => true,
         abi => unreachable!("Abi of ptr to {:?} is {:?}???", ty, abi),
     }
 }
 
-pub(crate) fn codegen_icmp(
-    fx: &mut FunctionCx<'_, '_, impl Backend>,
-    intcc: IntCC,
-    lhs: Value,
-    rhs: Value,
-) -> Value {
-    let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
-    let rhs_ty = fx.bcx.func.dfg.value_type(rhs);
-    assert_eq!(lhs_ty, rhs_ty);
-    if lhs_ty == types::I128 {
-        // FIXME legalize `icmp.i128` in Cranelift
-
-        let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
-        let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs);
-
-        match intcc {
-            IntCC::Equal => {
-                let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
-                let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
-                fx.bcx.ins().band(lsb_eq, msb_eq)
-            }
-            IntCC::NotEqual => {
-                let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
-                let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
-                fx.bcx.ins().bor(lsb_ne, msb_ne)
-            }
-            _ => {
-                // if msb_eq {
-                //     lsb_cc
-                // } else {
-                //     msb_cc
-                // }
-
-                let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
-                let lsb_cc = fx.bcx.ins().icmp(intcc, lhs_lsb, rhs_lsb);
-                let msb_cc = fx.bcx.ins().icmp(intcc, lhs_msb, rhs_msb);
-
-                fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
-            }
-        }
-    } else {
-        fx.bcx.ins().icmp(intcc, lhs, rhs)
-    }
-}
-
 pub(crate) fn codegen_icmp_imm(
-    fx: &mut FunctionCx<'_, '_, impl Backend>,
+    fx: &mut FunctionCx<'_, '_, impl Module>,
     intcc: IntCC,
     lhs: Value,
     rhs: i128,
@@ -201,8 +206,8 @@ fn resolve_128bit_value_imm(func: &Function, val: Value) -> Option<u128> {
         return None;
     };
 
-    let lsb = resolve_normal_value_imm(func, lsb)? as u64 as u128;
-    let msb = resolve_normal_value_imm(func, msb)? as u64 as u128;
+    let lsb = u128::from(resolve_normal_value_imm(func, lsb)? as u64);
+    let msb = u128::from(resolve_normal_value_imm(func, msb)? as u64);
 
     Some(msb << 64 | lsb)
 }
@@ -211,42 +216,71 @@ pub(crate) fn resolve_value_imm(func: &Function, val: Value) -> Option<u128> {
     if func.dfg.value_type(val) == types::I128 {
         resolve_128bit_value_imm(func, val)
     } else {
-        resolve_normal_value_imm(func, val).map(|imm| imm as u64 as u128)
+        resolve_normal_value_imm(func, val).map(|imm| u128::from(imm as u64))
     }
 }
 
-pub(crate) fn type_min_max_value(ty: Type, signed: bool) -> (i64, i64) {
+pub(crate) fn type_min_max_value(
+    bcx: &mut FunctionBuilder<'_>,
+    ty: Type,
+    signed: bool,
+) -> (Value, Value) {
     assert!(ty.is_int());
+
+    if ty == types::I128 {
+        if signed {
+            let min = i128::MIN as u128;
+            let min_lsb = bcx.ins().iconst(types::I64, min as u64 as i64);
+            let min_msb = bcx.ins().iconst(types::I64, (min >> 64) as u64 as i64);
+            let min = bcx.ins().iconcat(min_lsb, min_msb);
+
+            let max = i128::MAX as u128;
+            let max_lsb = bcx.ins().iconst(types::I64, max as u64 as i64);
+            let max_msb = bcx.ins().iconst(types::I64, (max >> 64) as u64 as i64);
+            let max = bcx.ins().iconcat(max_lsb, max_msb);
+
+            return (min, max);
+        } else {
+            let min_half = bcx.ins().iconst(types::I64, 0);
+            let min = bcx.ins().iconcat(min_half, min_half);
+
+            let max_half = bcx.ins().iconst(types::I64, u64::MAX as i64);
+            let max = bcx.ins().iconcat(max_half, max_half);
+
+            return (min, max);
+        }
+    }
+
     let min = match (ty, signed) {
         (types::I8, false) | (types::I16, false) | (types::I32, false) | (types::I64, false) => {
             0i64
         }
-        (types::I8, true) => i8::MIN as i64,
-        (types::I16, true) => i16::MIN as i64,
-        (types::I32, true) => i32::MIN as i64,
+        (types::I8, true) => i64::from(i8::MIN),
+        (types::I16, true) => i64::from(i16::MIN),
+        (types::I32, true) => i64::from(i32::MIN),
         (types::I64, true) => i64::MIN,
-        (types::I128, _) => unimplemented!(),
         _ => unreachable!(),
     };
 
     let max = match (ty, signed) {
-        (types::I8, false) => u8::MAX as i64,
-        (types::I16, false) => u16::MAX as i64,
-        (types::I32, false) => u32::MAX as i64,
+        (types::I8, false) => i64::from(u8::MAX),
+        (types::I16, false) => i64::from(u16::MAX),
+        (types::I32, false) => i64::from(u32::MAX),
         (types::I64, false) => u64::MAX as i64,
-        (types::I8, true) => i8::MAX as i64,
-        (types::I16, true) => i16::MAX as i64,
-        (types::I32, true) => i32::MAX as i64,
+        (types::I8, true) => i64::from(i8::MAX),
+        (types::I16, true) => i64::from(i16::MAX),
+        (types::I32, true) => i64::from(i32::MAX),
         (types::I64, true) => i64::MAX,
-        (types::I128, _) => unimplemented!(),
         _ => unreachable!(),
     };
 
+    let (min, max) = (bcx.ins().iconst(ty, min), bcx.ins().iconst(ty, max));
+
     (min, max)
 }
 
 pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
-    match ty.kind {
+    match ty.kind() {
         ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Char | ty::Uint(..) | ty::Bool => false,
         ty::Int(..) => true,
         ty::Float(..) => false, // `signed` is unused for floats
@@ -254,91 +288,74 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
     }
 }
 
-pub(crate) struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
-    // FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
+pub(crate) struct FunctionCx<'clif, 'tcx, M: Module> {
+    pub(crate) cx: &'clif mut crate::CodegenCx<'tcx, M>,
     pub(crate) tcx: TyCtxt<'tcx>,
-    pub(crate) module: &'clif mut Module<B>,
     pub(crate) pointer_type: Type, // Cached from module
 
     pub(crate) instance: Instance<'tcx>,
     pub(crate) mir: &'tcx Body<'tcx>,
+    pub(crate) fn_abi: Option<FnAbi<'tcx, Ty<'tcx>>>,
 
     pub(crate) bcx: FunctionBuilder<'clif>,
     pub(crate) block_map: IndexVec<BasicBlock, Block>,
-    pub(crate) local_map: HashMap<Local, CPlace<'tcx>>,
+    pub(crate) local_map: IndexVec<Local, CPlace<'tcx>>,
 
     /// When `#[track_caller]` is used, the implicit caller location is stored in this variable.
     pub(crate) caller_location: Option<CValue<'tcx>>,
 
-    /// See [crate::optimize::code_layout] for more information.
+    /// See [`crate::optimize::code_layout`] for more information.
     pub(crate) cold_blocks: EntitySet<Block>,
 
     pub(crate) clif_comments: crate::pretty_clif::CommentWriter,
-    pub(crate) constants_cx: &'clif mut crate::constant::ConstantCx,
-    pub(crate) vtables: &'clif mut HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
-
     pub(crate) source_info_set: indexmap::IndexSet<SourceInfo>,
+
+    /// This should only be accessed by `CPlace::new_var`.
+    pub(crate) next_ssa_var: u32,
+
+    pub(crate) inline_asm_index: u32,
 }
 
-impl<'tcx, B: Backend> LayoutOf for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> LayoutOf for FunctionCx<'_, 'tcx, M> {
     type Ty = Ty<'tcx>;
     type TyAndLayout = TyAndLayout<'tcx>;
 
     fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> {
-        assert!(!ty.needs_subst());
-        self.tcx
-            .layout_of(ParamEnv::reveal_all().and(&ty))
-            .unwrap_or_else(|e| {
-                if let layout::LayoutError::SizeOverflow(_) = e {
-                    self.tcx.sess.fatal(&e.to_string())
-                } else {
-                    bug!("failed to get layout for `{}`: {}", ty, e)
-                }
-            })
+        RevealAllLayoutCx(self.tcx).layout_of(ty)
     }
 }
 
-impl<'tcx, B: Backend + 'static> layout::HasTyCtxt<'tcx> for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> layout::HasTyCtxt<'tcx> for FunctionCx<'_, 'tcx, M> {
     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
         self.tcx
     }
 }
 
-impl<'tcx, B: Backend + 'static> rustc_target::abi::HasDataLayout for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> rustc_target::abi::HasDataLayout for FunctionCx<'_, 'tcx, M> {
     fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout {
         &self.tcx.data_layout
     }
 }
 
-impl<'tcx, B: Backend + 'static> layout::HasParamEnv<'tcx> for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> layout::HasParamEnv<'tcx> for FunctionCx<'_, 'tcx, M> {
     fn param_env(&self) -> ParamEnv<'tcx> {
         ParamEnv::reveal_all()
     }
 }
 
-impl<'tcx, B: Backend + 'static> HasTargetSpec for FunctionCx<'_, 'tcx, B> {
+impl<'tcx, M: Module> HasTargetSpec for FunctionCx<'_, 'tcx, M> {
     fn target_spec(&self) -> &Target {
-        &self.tcx.sess.target.target
+        &self.tcx.sess.target
     }
 }
 
-impl<'tcx, B: Backend> BackendTypes for FunctionCx<'_, 'tcx, B> {
-    type Value = Value;
-    type Function = Value;
-    type BasicBlock = Block;
-    type Type = Type;
-    type Funclet = !;
-    type DIScope = !;
-    type DIVariable = !;
-}
-
-impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
-    pub(crate) fn monomorphize<T>(&self, value: &T) -> T
+impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> {
+    pub(crate) fn monomorphize<T>(&self, value: T) -> T
     where
-        T: TypeFoldable<'tcx>,
+        T: TypeFoldable<'tcx> + Copy,
     {
-        self.tcx.subst_and_normalize_erasing_regions(
-            self.instance.substs,
+        self.instance.subst_mir_and_normalize_erasing_regions(
+            self.tcx,
             ty::ParamEnv::reveal_all(),
             value,
         )
@@ -348,12 +365,16 @@ pub(crate) fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
         clif_type_from_ty(self.tcx, ty)
     }
 
+    pub(crate) fn clif_pair_type(&self, ty: Ty<'tcx>) -> Option<(Type, Type)> {
+        clif_pair_type_from_ty(self.tcx, ty)
+    }
+
     pub(crate) fn get_block(&self, bb: BasicBlock) -> Block {
         *self.block_map.get(bb).unwrap()
     }
 
     pub(crate) fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
-        *self.local_map.get(&local).unwrap_or_else(|| {
+        *self.local_map.get(local).unwrap_or_else(|| {
             panic!("Local {:?} doesn't exist", local);
         })
     }
@@ -376,13 +397,85 @@ pub(crate) fn get_caller_location(&mut self, span: Span) -> CValue<'tcx> {
             caller.line as u32,
             caller.col_display as u32 + 1,
         ));
-        crate::constant::trans_const_value(
-            self,
-            ty::Const::from_value(self.tcx, const_loc, self.tcx.caller_location_ty()),
-        )
+        crate::constant::codegen_const_value(self, const_loc, self.tcx.caller_location_ty())
     }
 
     pub(crate) fn triple(&self) -> &target_lexicon::Triple {
-        self.module.isa().triple()
+        self.cx.module.isa().triple()
+    }
+
+    pub(crate) fn anonymous_str(&mut self, prefix: &str, msg: &str) -> Value {
+        use std::collections::hash_map::DefaultHasher;
+        use std::hash::{Hash, Hasher};
+
+        let mut hasher = DefaultHasher::new();
+        msg.hash(&mut hasher);
+        let msg_hash = hasher.finish();
+        let mut data_ctx = DataContext::new();
+        data_ctx.define(msg.as_bytes().to_vec().into_boxed_slice());
+        let msg_id = self
+            .cx
+            .module
+            .declare_data(
+                &format!("__{}_{:08x}", prefix, msg_hash),
+                Linkage::Local,
+                false,
+                false,
+            )
+            .unwrap();
+
+        // Ignore DuplicateDefinition error, as the data will be the same
+        let _ = self.cx.module.define_data(msg_id, &data_ctx);
+
+        let local_msg_id = self.cx.module.declare_data_in_func(msg_id, self.bcx.func);
+        #[cfg(debug_assertions)]
+        {
+            self.add_comment(local_msg_id, msg);
+        }
+        self.bcx.ins().global_value(self.pointer_type, local_msg_id)
+    }
+}
+
+pub(crate) struct RevealAllLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>);
+
+impl<'tcx> LayoutOf for RevealAllLayoutCx<'tcx> {
+    type Ty = Ty<'tcx>;
+    type TyAndLayout = TyAndLayout<'tcx>;
+
+    fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> {
+        assert!(!ty.still_further_specializable());
+        self.0
+            .layout_of(ParamEnv::reveal_all().and(&ty))
+            .unwrap_or_else(|e| {
+                if let layout::LayoutError::SizeOverflow(_) = e {
+                    self.0.sess.fatal(&e.to_string())
+                } else {
+                    bug!("failed to get layout for `{}`: {}", ty, e)
+                }
+            })
+    }
+}
+
+impl<'tcx> layout::HasTyCtxt<'tcx> for RevealAllLayoutCx<'tcx> {
+    fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
+        self.0
+    }
+}
+
+impl<'tcx> rustc_target::abi::HasDataLayout for RevealAllLayoutCx<'tcx> {
+    fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout {
+        &self.0.data_layout
+    }
+}
+
+impl<'tcx> layout::HasParamEnv<'tcx> for RevealAllLayoutCx<'tcx> {
+    fn param_env(&self) -> ParamEnv<'tcx> {
+        ParamEnv::reveal_all()
+    }
+}
+
+impl<'tcx> HasTargetSpec for RevealAllLayoutCx<'tcx> {
+    fn target_spec(&self) -> &Target {
+        &self.0.sess.target
     }
 }