]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_codegen_llvm/common.rs
Beginning of moving all backend-agnostic code to rustc_codegen_ssa
[rust.git] / src / librustc_codegen_llvm / common.rs
index c08937fa9b9162669bcb04f021cbde86c2aa6736..7dc4b00f79449cb2ff472da18ffaa43dd1a90cfd 100644 (file)
 
 //! Code that is useful in various codegen modules.
 
-use llvm::{self, TypeKind};
-use llvm::{True, False, Bool, OperandBundleDef};
+use llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef};
 use rustc::hir::def_id::DefId;
 use rustc::middle::lang_items::LangItem;
 use abi;
 use base;
-use builder::Builder;
 use consts;
-use declare;
 use type_::Type;
 use type_of::LayoutLlvmExt;
 use value::Value;
+use interfaces::*;
 
-use rustc::ty::{self, Ty, TyCtxt};
-use rustc::ty::layout::{HasDataLayout, LayoutOf};
+use rustc::ty::{Ty, TyCtxt};
+use rustc::ty::layout::{HasDataLayout, LayoutOf, self, TyLayout, Size};
+use rustc::mir::interpret::{Scalar, AllocType, Allocation};
 use rustc::hir;
+use mir::constant::const_alloc_to_llvm;
+use mir::place::PlaceRef;
+use rustc_codegen_ssa::common::TypeKind;
 
 use libc::{c_uint, c_char};
-use std::iter;
 
-use rustc_target::spec::abi::Abi;
 use syntax::symbol::LocalInternedString;
-use syntax_pos::{Span, DUMMY_SP};
+use syntax::ast::Mutability;
+use syntax_pos::Span;
 
 pub use context::CodegenCx;
 
-pub fn type_needs_drop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
-    ty.needs_drop(tcx, ty::ParamEnv::reveal_all())
-}
-
-pub fn type_is_sized<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
-    ty.is_sized(tcx.at(DUMMY_SP), ty::ParamEnv::reveal_all())
-}
-
-pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
-    ty.is_freeze(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP)
-}
-
 /*
 * A note on nomenclature of linking: "extern", "foreign", and "upcall".
 *
@@ -112,225 +101,311 @@ pub fn bundle(&self) -> &OperandBundleDef<'ll> {
     }
 }
 
-pub fn val_ty(v: &'ll Value) -> &'ll Type {
-    unsafe {
-        llvm::LLVMTypeOf(v)
-    }
-}
+impl BackendTypes for CodegenCx<'ll, 'tcx> {
+    type Value = &'ll Value;
+    type BasicBlock = &'ll BasicBlock;
+    type Type = &'ll Type;
+    type Context = &'ll llvm::Context;
+    type Funclet = Funclet<'ll>;
 
-// LLVM constant constructors.
-pub fn C_null(t: &'ll Type) -> &'ll Value {
-    unsafe {
-        llvm::LLVMConstNull(t)
-    }
+    type DIScope = &'ll llvm::debuginfo::DIScope;
 }
 
-pub fn C_undef(t: &'ll Type) -> &'ll Value {
-    unsafe {
-        llvm::LLVMGetUndef(t)
+impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
+    fn const_null(&self, t: &'ll Type) -> &'ll Value {
+        unsafe {
+            llvm::LLVMConstNull(t)
+        }
     }
-}
 
-pub fn C_int(t: &'ll Type, i: i64) -> &'ll Value {
-    unsafe {
-        llvm::LLVMConstInt(t, i as u64, True)
+    fn const_undef(&self, t: &'ll Type) -> &'ll Value {
+        unsafe {
+            llvm::LLVMGetUndef(t)
+        }
     }
-}
 
-pub fn C_uint(t: &'ll Type, i: u64) -> &'ll Value {
-    unsafe {
-        llvm::LLVMConstInt(t, i, False)
+    fn const_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
+        unsafe {
+            llvm::LLVMConstInt(t, i as u64, True)
+        }
     }
-}
 
-pub fn C_uint_big(t: &'ll Type, u: u128) -> &'ll Value {
-    unsafe {
-        let words = [u as u64, (u >> 64) as u64];
-        llvm::LLVMConstIntOfArbitraryPrecision(t, 2, words.as_ptr())
+    fn const_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
+        unsafe {
+            llvm::LLVMConstInt(t, i, False)
+        }
     }
-}
 
-pub fn C_bool(cx: &CodegenCx<'ll, '_>, val: bool) -> &'ll Value {
-    C_uint(Type::i1(cx), val as u64)
-}
+    fn const_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
+        unsafe {
+            let words = [u as u64, (u >> 64) as u64];
+            llvm::LLVMConstIntOfArbitraryPrecision(t, 2, words.as_ptr())
+        }
+    }
 
-pub fn C_i32(cx: &CodegenCx<'ll, '_>, i: i32) -> &'ll Value {
-    C_int(Type::i32(cx), i as i64)
-}
+    fn const_bool(&self, val: bool) -> &'ll Value {
+        self.const_uint(self.type_i1(), val as u64)
+    }
 
-pub fn C_u32(cx: &CodegenCx<'ll, '_>, i: u32) -> &'ll Value {
-    C_uint(Type::i32(cx), i as u64)
-}
+    fn const_i32(&self, i: i32) -> &'ll Value {
+        self.const_int(self.type_i32(), i as i64)
+    }
 
-pub fn C_u64(cx: &CodegenCx<'ll, '_>, i: u64) -> &'ll Value {
-    C_uint(Type::i64(cx), i)
-}
+    fn const_u32(&self, i: u32) -> &'ll Value {
+        self.const_uint(self.type_i32(), i as u64)
+    }
 
-pub fn C_usize(cx: &CodegenCx<'ll, '_>, i: u64) -> &'ll Value {
-    let bit_size = cx.data_layout().pointer_size.bits();
-    if bit_size < 64 {
-        // make sure it doesn't overflow
-        assert!(i < (1<<bit_size));
+    fn const_u64(&self, i: u64) -> &'ll Value {
+        self.const_uint(self.type_i64(), i)
     }
 
-    C_uint(cx.isize_ty, i)
-}
+    fn const_usize(&self, i: u64) -> &'ll Value {
+        let bit_size = self.data_layout().pointer_size.bits();
+        if bit_size < 64 {
+            // make sure it doesn't overflow
+            assert!(i < (1<<bit_size));
+        }
 
-pub fn C_u8(cx: &CodegenCx<'ll, '_>, i: u8) -> &'ll Value {
-    C_uint(Type::i8(cx), i as u64)
-}
+        self.const_uint(self.isize_ty, i)
+    }
 
+    fn const_u8(&self, i: u8) -> &'ll Value {
+        self.const_uint(self.type_i8(), i as u64)
+    }
 
-// This is a 'c-like' raw string, which differs from
-// our boxed-and-length-annotated strings.
-pub fn C_cstr(
-    cx: &CodegenCx<'ll, '_>,
-    s: LocalInternedString,
-    null_terminated: bool,
-) -> &'ll Value {
-    unsafe {
-        if let Some(&llval) = cx.const_cstr_cache.borrow().get(&s) {
-            return llval;
+    fn const_cstr(
+        &self,
+        s: LocalInternedString,
+        null_terminated: bool,
+    ) -> &'ll Value {
+        unsafe {
+            if let Some(&llval) = self.const_cstr_cache.borrow().get(&s) {
+                return llval;
+            }
+
+            let sc = llvm::LLVMConstStringInContext(self.llcx,
+                                                    s.as_ptr() as *const c_char,
+                                                    s.len() as c_uint,
+                                                    !null_terminated as Bool);
+            let sym = self.generate_local_symbol_name("str");
+            let g = self.define_global(&sym[..], self.val_ty(sc)).unwrap_or_else(||{
+                bug!("symbol `{}` is already defined", sym);
+            });
+            llvm::LLVMSetInitializer(g, sc);
+            llvm::LLVMSetGlobalConstant(g, True);
+            llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
+
+            self.const_cstr_cache.borrow_mut().insert(s, g);
+            g
         }
+    }
 
-        let sc = llvm::LLVMConstStringInContext(cx.llcx,
-                                                s.as_ptr() as *const c_char,
-                                                s.len() as c_uint,
-                                                !null_terminated as Bool);
-        let sym = cx.generate_local_symbol_name("str");
-        let g = declare::define_global(cx, &sym[..], val_ty(sc)).unwrap_or_else(||{
-            bug!("symbol `{}` is already defined", sym);
-        });
-        llvm::LLVMSetInitializer(g, sc);
-        llvm::LLVMSetGlobalConstant(g, True);
-        llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
-
-        cx.const_cstr_cache.borrow_mut().insert(s, g);
-        g
+    fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
+        let len = s.len();
+        let cs = consts::ptrcast(self.const_cstr(s, false),
+            self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
+        self.const_fat_ptr(cs, self.const_usize(len as u64))
     }
-}
 
-// NB: Do not use `do_spill_noroot` to make this into a constant string, or
-// you will be kicked off fast isel. See issue #4352 for an example of this.
-pub fn C_str_slice(cx: &CodegenCx<'ll, '_>, s: LocalInternedString) -> &'ll Value {
-    let len = s.len();
-    let cs = consts::ptrcast(C_cstr(cx, s, false),
-        cx.layout_of(cx.tcx.mk_str()).llvm_type(cx).ptr_to());
-    C_fat_ptr(cx, cs, C_usize(cx, len as u64))
-}
+    fn const_fat_ptr(
+        &self,
+        ptr: &'ll Value,
+        meta: &'ll Value
+    ) -> &'ll Value {
+        assert_eq!(abi::FAT_PTR_ADDR, 0);
+        assert_eq!(abi::FAT_PTR_EXTRA, 1);
+        self.const_struct(&[ptr, meta], false)
+    }
 
-pub fn C_fat_ptr(cx: &CodegenCx<'ll, '_>, ptr: &'ll Value, meta: &'ll Value) -> &'ll Value {
-    assert_eq!(abi::FAT_PTR_ADDR, 0);
-    assert_eq!(abi::FAT_PTR_EXTRA, 1);
-    C_struct(cx, &[ptr, meta], false)
-}
+    fn const_struct(
+        &self,
+        elts: &[&'ll Value],
+        packed: bool
+    ) -> &'ll Value {
+        struct_in_context(self.llcx, elts, packed)
+    }
 
-pub fn C_struct(cx: &CodegenCx<'ll, '_>, elts: &[&'ll Value], packed: bool) -> &'ll Value {
-    C_struct_in_context(cx.llcx, elts, packed)
-}
+    fn const_array(&self, ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
+        unsafe {
+            return llvm::LLVMConstArray(ty, elts.as_ptr(), elts.len() as c_uint);
+        }
+    }
 
-pub fn C_struct_in_context(
-    llcx: &'ll llvm::Context,
-    elts: &[&'ll Value],
-    packed: bool,
-) -> &'ll Value {
-    unsafe {
-        llvm::LLVMConstStructInContext(llcx,
-                                       elts.as_ptr(), elts.len() as c_uint,
-                                       packed as Bool)
+    fn const_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
+        unsafe {
+            return llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint);
+        }
     }
-}
 
-pub fn C_array(ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
-    unsafe {
-        return llvm::LLVMConstArray(ty, elts.as_ptr(), elts.len() as c_uint);
+    fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
+        bytes_in_context(self.llcx, bytes)
     }
-}
 
-pub fn C_vector(elts: &[&'ll Value]) -> &'ll Value {
-    unsafe {
-        return llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint);
+    fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
+        unsafe {
+            assert_eq!(idx as c_uint as u64, idx);
+            let us = &[idx as c_uint];
+            let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
+
+            debug!("const_get_elt(v={:?}, idx={}, r={:?})",
+                   v, idx, r);
+
+            r
+        }
     }
-}
 
-pub fn C_bytes(cx: &CodegenCx<'ll, '_>, bytes: &[u8]) -> &'ll Value {
-    C_bytes_in_context(cx.llcx, bytes)
-}
+    fn const_get_real(&self, v: &'ll Value) -> Option<(f64, bool)> {
+        unsafe {
+            if self.is_const_real(v) {
+                let mut loses_info: llvm::Bool = ::std::mem::uninitialized();
+                let r = llvm::LLVMConstRealGetDouble(v, &mut loses_info);
+                let loses_info = if loses_info == 1 { true } else { false };
+                Some((r, loses_info))
+            } else {
+                None
+            }
+        }
+    }
 
-pub fn C_bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
-    unsafe {
-        let ptr = bytes.as_ptr() as *const c_char;
-        return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
+    fn const_to_uint(&self, v: &'ll Value) -> u64 {
+        unsafe {
+            llvm::LLVMConstIntGetZExtValue(v)
+        }
     }
-}
 
-pub fn const_get_elt(v: &'ll Value, idx: u64) -> &'ll Value {
-    unsafe {
-        assert_eq!(idx as c_uint as u64, idx);
-        let us = &[idx as c_uint];
-        let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
+    fn is_const_integral(&self, v: &'ll Value) -> bool {
+        unsafe {
+            llvm::LLVMIsAConstantInt(v).is_some()
+        }
+    }
 
-        debug!("const_get_elt(v={:?}, idx={}, r={:?})",
-               v, idx, r);
+    fn is_const_real(&self, v: &'ll Value) -> bool {
+        unsafe {
+            llvm::LLVMIsAConstantFP(v).is_some()
+        }
+    }
 
-        r
+    fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
+        unsafe {
+            if self.is_const_integral(v) {
+                let (mut lo, mut hi) = (0u64, 0u64);
+                let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
+                                                           &mut hi, &mut lo);
+                if success {
+                    Some(hi_lo_to_u128(lo, hi))
+                } else {
+                    None
+                }
+            } else {
+                None
+            }
+        }
     }
-}
 
-pub fn const_get_real(v: &'ll Value) -> Option<(f64, bool)> {
-    unsafe {
-        if is_const_real(v) {
-            let mut loses_info: llvm::Bool = ::std::mem::uninitialized();
-            let r = llvm::LLVMConstRealGetDouble(v, &mut loses_info);
-            let loses_info = if loses_info == 1 { true } else { false };
-            Some((r, loses_info))
-        } else {
-            None
+    fn scalar_to_backend(
+        &self,
+        cv: Scalar,
+        layout: &layout::Scalar,
+        llty: &'ll Type,
+    ) -> &'ll Value {
+        let bitsize = if layout.is_bool() { 1 } else { layout.value.size(self).bits() };
+        match cv {
+            Scalar::Bits { size: 0, .. } => {
+                assert_eq!(0, layout.value.size(self).bytes());
+                self.const_undef(self.type_ix(0))
+            },
+            Scalar::Bits { bits, size } => {
+                assert_eq!(size as u64, layout.value.size(self).bytes());
+                let llval = self.const_uint_big(self.type_ix(bitsize), bits);
+                if layout.value == layout::Pointer {
+                    unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
+                } else {
+                    self.static_bitcast(llval, llty)
+                }
+            },
+            Scalar::Ptr(ptr) => {
+                let alloc_type = self.tcx.alloc_map.lock().get(ptr.alloc_id);
+                let base_addr = match alloc_type {
+                    Some(AllocType::Memory(alloc)) => {
+                        let init = const_alloc_to_llvm(self, alloc);
+                        if alloc.mutability == Mutability::Mutable {
+                            self.static_addr_of_mut(init, alloc.align, None)
+                        } else {
+                            self.static_addr_of(init, alloc.align, None)
+                        }
+                    }
+                    Some(AllocType::Function(fn_instance)) => {
+                        self.get_fn(fn_instance)
+                    }
+                    Some(AllocType::Static(def_id)) => {
+                        assert!(self.tcx.is_static(def_id).is_some());
+                        self.get_static(def_id)
+                    }
+                    None => bug!("missing allocation {:?}", ptr.alloc_id),
+                };
+                let llval = unsafe { llvm::LLVMConstInBoundsGEP(
+                    self.static_bitcast(base_addr, self.type_i8p()),
+                    &self.const_usize(ptr.offset.bytes()),
+                    1,
+                ) };
+                if layout.value != layout::Pointer {
+                    unsafe { llvm::LLVMConstPtrToInt(llval, llty) }
+                } else {
+                    self.static_bitcast(llval, llty)
+                }
+            }
         }
     }
+
+    fn from_const_alloc(
+        &self,
+        layout: TyLayout<'tcx>,
+        alloc: &Allocation,
+        offset: Size,
+    ) -> PlaceRef<'tcx, &'ll Value> {
+        let init = const_alloc_to_llvm(self, alloc);
+        let base_addr = self.static_addr_of(init, layout.align, None);
+
+        let llval = unsafe { llvm::LLVMConstInBoundsGEP(
+            self.static_bitcast(base_addr, self.type_i8p()),
+            &self.const_usize(offset.bytes()),
+            1,
+        )};
+        let llval = self.static_bitcast(llval, self.type_ptr_to(layout.llvm_type(self)));
+        PlaceRef::new_sized(llval, layout, alloc.align)
+    }
 }
 
-pub fn const_to_uint(v: &'ll Value) -> u64 {
+pub fn val_ty(v: &'ll Value) -> &'ll Type {
     unsafe {
-        llvm::LLVMConstIntGetZExtValue(v)
+        llvm::LLVMTypeOf(v)
     }
 }
 
-pub fn is_const_integral(v: &'ll Value) -> bool {
+pub fn bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
     unsafe {
-        llvm::LLVMIsAConstantInt(v).is_some()
+        let ptr = bytes.as_ptr() as *const c_char;
+        return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
     }
 }
 
-pub fn is_const_real(v: &'ll Value) -> bool {
+pub fn struct_in_context(
+    llcx: &'a llvm::Context,
+    elts: &[&'a Value],
+    packed: bool,
+) -> &'a Value {
     unsafe {
-        llvm::LLVMIsAConstantFP(v).is_some()
+        llvm::LLVMConstStructInContext(llcx,
+                                       elts.as_ptr(), elts.len() as c_uint,
+                                       packed as Bool)
     }
 }
 
-
 #[inline]
 fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
     ((hi as u128) << 64) | (lo as u128)
 }
 
-pub fn const_to_opt_u128(v: &'ll Value, sign_ext: bool) -> Option<u128> {
-    unsafe {
-        if is_const_integral(v) {
-            let (mut lo, mut hi) = (0u64, 0u64);
-            let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
-                                                       &mut hi, &mut lo);
-            if success {
-                Some(hi_lo_to_u128(lo, hi))
-            } else {
-                None
-            }
-        } else {
-            None
-        }
-    }
-}
-
 pub fn langcall(tcx: TyCtxt,
                 span: Option<Span>,
                 msg: &str,
@@ -350,20 +425,23 @@ pub fn langcall(tcx: TyCtxt,
 // all shifts). For 32- and 64-bit types, this matches the semantics
 // of Java. (See related discussion on #1877 and #10183.)
 
-pub fn build_unchecked_lshift(
-    bx: &Builder<'a, 'll, 'tcx>,
-    lhs: &'ll Value,
-    rhs: &'ll Value
-) -> &'ll Value {
+pub fn build_unchecked_lshift<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
+    bx: &Bx,
+    lhs: Bx::Value,
+    rhs: Bx::Value
+) -> Bx::Value {
     let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shl, lhs, rhs);
     // #1877, #10183: Ensure that input is always valid
     let rhs = shift_mask_rhs(bx, rhs);
     bx.shl(lhs, rhs)
 }
 
-pub fn build_unchecked_rshift(
-    bx: &Builder<'a, 'll, 'tcx>, lhs_t: Ty<'tcx>, lhs: &'ll Value, rhs: &'ll Value
-) -> &'ll Value {
+pub fn build_unchecked_rshift<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
+    bx: &Bx,
+    lhs_t: Ty<'tcx>,
+    lhs: Bx::Value,
+    rhs: Bx::Value
+) -> Bx::Value {
     let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shr, lhs, rhs);
     // #1877, #10183: Ensure that input is always valid
     let rhs = shift_mask_rhs(bx, rhs);
@@ -375,81 +453,40 @@ pub fn build_unchecked_rshift(
     }
 }
 
-fn shift_mask_rhs(bx: &Builder<'a, 'll, 'tcx>, rhs: &'ll Value) -> &'ll Value {
-    let rhs_llty = val_ty(rhs);
+fn shift_mask_rhs<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
+    bx: &Bx,
+    rhs: Bx::Value
+) -> Bx::Value {
+    let rhs_llty = bx.cx().val_ty(rhs);
     bx.and(rhs, shift_mask_val(bx, rhs_llty, rhs_llty, false))
 }
 
-pub fn shift_mask_val(
-    bx: &Builder<'a, 'll, 'tcx>,
-    llty: &'ll Type,
-    mask_llty: &'ll Type,
+pub fn shift_mask_val<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
+    bx: &Bx,
+    llty: Bx::Type,
+    mask_llty: Bx::Type,
     invert: bool
-) -> &'ll Value {
-    let kind = llty.kind();
+) -> Bx::Value {
+    let kind = bx.cx().type_kind(llty);
     match kind {
         TypeKind::Integer => {
             // i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
-            let val = llty.int_width() - 1;
+            let val = bx.cx().int_width(llty) - 1;
             if invert {
-                C_int(mask_llty, !val as i64)
+                bx.cx().const_int(mask_llty, !val as i64)
             } else {
-                C_uint(mask_llty, val)
+                bx.cx().const_uint(mask_llty, val)
             }
         },
         TypeKind::Vector => {
-            let mask = shift_mask_val(bx, llty.element_type(), mask_llty.element_type(), invert);
-            bx.vector_splat(mask_llty.vector_length(), mask)
+            let mask = shift_mask_val(
+                bx,
+                bx.cx().element_type(llty),
+                bx.cx().element_type(mask_llty),
+                invert
+            );
+            bx.vector_splat(bx.cx().vector_length(mask_llty), mask)
         },
         _ => bug!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
     }
 }
-
-pub fn ty_fn_sig<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
-                           ty: Ty<'tcx>)
-                           -> ty::PolyFnSig<'tcx>
-{
-    match ty.sty {
-        ty::FnDef(..) |
-        // Shims currently have type FnPtr. Not sure this should remain.
-        ty::FnPtr(_) => ty.fn_sig(cx.tcx),
-        ty::Closure(def_id, substs) => {
-            let tcx = cx.tcx;
-            let sig = substs.closure_sig(def_id, tcx);
-
-            let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
-            sig.map_bound(|sig| tcx.mk_fn_sig(
-                iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
-                sig.output(),
-                sig.variadic,
-                sig.unsafety,
-                sig.abi
-            ))
-        }
-        ty::Generator(def_id, substs, _) => {
-            let tcx = cx.tcx;
-            let sig = substs.poly_sig(def_id, cx.tcx);
-
-            let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
-            let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
-
-            sig.map_bound(|sig| {
-                let state_did = tcx.lang_items().gen_state().unwrap();
-                let state_adt_ref = tcx.adt_def(state_did);
-                let state_substs = tcx.intern_substs(&[
-                    sig.yield_ty.into(),
-                    sig.return_ty.into(),
-                ]);
-                let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
-
-                tcx.mk_fn_sig(iter::once(env_ty),
-                    ret_ty,
-                    false,
-                    hir::Unsafety::Normal,
-                    Abi::Rust
-                )
-            })
-        }
-        _ => bug!("unexpected type {:?} to ty_fn_sig", ty)
-    }
-}