]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/base.rs
Generalized base.rs#call_memcpy and everything that it uses
[rust.git] / src / librustc_codegen_llvm / base.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Codegen the completed AST to the LLVM IR.
12 //!
13 //! Some functions here, such as codegen_block and codegen_expr, return a value --
14 //! the result of the codegen to LLVM -- while others, such as codegen_fn
15 //! and mono_item, are called only for the side effect of adding a
16 //! particular definition to the LLVM IR output we're producing.
17 //!
18 //! Hopefully useful general knowledge about codegen:
19 //!
20 //!   * There's no way to find out the Ty type of a Value.  Doing so
21 //!     would be "trying to get the eggs out of an omelette" (credit:
22 //!     pcwalton).  You can, instead, find out its llvm::Type by calling val_ty,
23 //!     but one llvm::Type corresponds to many `Ty`s; for instance, tup(int, int,
24 //!     int) and rec(x=int, y=int, z=int) will have the same llvm::Type.
25
26 use super::ModuleLlvm;
27 use super::ModuleCodegen;
28 use super::ModuleKind;
29 use super::CachedModuleCodegen;
30
31 use abi;
32 use back::write::{self, OngoingCodegen};
33 use llvm::{self, TypeKind, get_param};
34 use metadata;
35 use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
36 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
37 use rustc::middle::lang_items::StartFnLangItem;
38 use rustc::middle::weak_lang_items;
39 use rustc::mir::mono::{Linkage, Visibility, Stats, CodegenUnitNameBuilder};
40 use rustc::middle::cstore::{EncodedMetadata};
41 use rustc::ty::{self, Ty, TyCtxt};
42 use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx};
43 use rustc::ty::query::Providers;
44 use rustc::middle::cstore::{self, LinkagePreference};
45 use rustc::middle::exported_symbols;
46 use rustc::util::common::{time, print_time_passes_entry};
47 use rustc::util::profiling::ProfileCategory;
48 use rustc::session::config::{self, DebugInfo, EntryFnType, Lto};
49 use rustc::session::Session;
50 use rustc_incremental;
51 use allocator;
52 use mir::place::PlaceRef;
53 use attributes;
54 use builder::{Builder, MemFlags};
55 use callee;
56 use common::{C_bool, C_bytes_in_context, C_usize};
57 use rustc_mir::monomorphize::item::DefPathBasedNames;
58 use common::{C_struct_in_context, C_array, val_ty};
59 use consts;
60 use context::CodegenCx;
61 use debuginfo;
62 use declare;
63 use meth;
64 use mir;
65 use monomorphize::Instance;
66 use monomorphize::partitioning::{CodegenUnit, CodegenUnitExt};
67 use rustc_codegen_utils::symbol_names_test;
68 use time_graph;
69 use mono_item::{MonoItem, MonoItemExt};
70 use type_::Type;
71 use type_of::LayoutLlvmExt;
72 use rustc::util::nodemap::FxHashMap;
73 use CrateInfo;
74 use rustc_data_structures::small_c_str::SmallCStr;
75 use rustc_data_structures::sync::Lrc;
76 use rustc_data_structures::indexed_vec::Idx;
77
78 use traits::BuilderMethods;
79
80 use std::any::Any;
81 use std::cmp;
82 use std::ffi::CString;
83 use std::ops::{Deref, DerefMut};
84 use std::sync::mpsc;
85 use std::time::{Instant, Duration};
86 use syntax_pos::Span;
87 use syntax_pos::symbol::InternedString;
88 use syntax::attr;
89 use rustc::hir::{self, CodegenFnAttrs};
90
91 use value::{Value, ValueTrait};
92
93 use mir::operand::OperandValue;
94
95 use rustc_codegen_utils::check_for_rustc_errors_attr;
96
97 pub struct StatRecorder<'a, 'll: 'a, 'tcx: 'll> {
98     cx: &'a CodegenCx<'ll, 'tcx>,
99     name: Option<String>,
100     istart: usize,
101 }
102
103 impl StatRecorder<'a, 'll, 'tcx> {
104     pub fn new(cx: &'a CodegenCx<'ll, 'tcx>, name: String) -> Self {
105         let istart = cx.stats.borrow().n_llvm_insns;
106         StatRecorder {
107             cx,
108             name: Some(name),
109             istart,
110         }
111     }
112 }
113
114 impl Drop for StatRecorder<'a, 'll, 'tcx> {
115     fn drop(&mut self) {
116         if self.cx.sess().codegen_stats() {
117             let mut stats = self.cx.stats.borrow_mut();
118             let iend = stats.n_llvm_insns;
119             stats.fn_stats.push((self.name.take().unwrap(), iend - self.istart));
120             stats.n_fns += 1;
121             // Reset LLVM insn count to avoid compound costs.
122             stats.n_llvm_insns = self.istart;
123         }
124     }
125 }
126
127 pub fn bin_op_to_icmp_predicate(op: hir::BinOpKind,
128                                 signed: bool)
129                                 -> llvm::IntPredicate {
130     match op {
131         hir::BinOpKind::Eq => llvm::IntEQ,
132         hir::BinOpKind::Ne => llvm::IntNE,
133         hir::BinOpKind::Lt => if signed { llvm::IntSLT } else { llvm::IntULT },
134         hir::BinOpKind::Le => if signed { llvm::IntSLE } else { llvm::IntULE },
135         hir::BinOpKind::Gt => if signed { llvm::IntSGT } else { llvm::IntUGT },
136         hir::BinOpKind::Ge => if signed { llvm::IntSGE } else { llvm::IntUGE },
137         op => {
138             bug!("comparison_op_to_icmp_predicate: expected comparison operator, \
139                   found {:?}",
140                  op)
141         }
142     }
143 }
144
145 pub fn bin_op_to_fcmp_predicate(op: hir::BinOpKind) -> llvm::RealPredicate {
146     match op {
147         hir::BinOpKind::Eq => llvm::RealOEQ,
148         hir::BinOpKind::Ne => llvm::RealUNE,
149         hir::BinOpKind::Lt => llvm::RealOLT,
150         hir::BinOpKind::Le => llvm::RealOLE,
151         hir::BinOpKind::Gt => llvm::RealOGT,
152         hir::BinOpKind::Ge => llvm::RealOGE,
153         op => {
154             bug!("comparison_op_to_fcmp_predicate: expected comparison operator, \
155                   found {:?}",
156                  op);
157         }
158     }
159 }
160
161 pub fn compare_simd_types(
162     bx: &Builder<'a, 'll, 'tcx>,
163     lhs: &'ll Value,
164     rhs: &'ll Value,
165     t: Ty<'tcx>,
166     ret_ty: &'ll Type,
167     op: hir::BinOpKind
168 ) -> &'ll Value {
169     let signed = match t.sty {
170         ty::Float(_) => {
171             let cmp = bin_op_to_fcmp_predicate(op);
172             return bx.sext(bx.fcmp(cmp, lhs, rhs), ret_ty);
173         },
174         ty::Uint(_) => false,
175         ty::Int(_) => true,
176         _ => bug!("compare_simd_types: invalid SIMD type"),
177     };
178
179     let cmp = bin_op_to_icmp_predicate(op, signed);
180     // LLVM outputs an `< size x i1 >`, so we need to perform a sign extension
181     // to get the correctly sized type. This will compile to a single instruction
182     // once the IR is converted to assembly if the SIMD instruction is supported
183     // by the target architecture.
184     bx.sext(bx.icmp(cmp, lhs, rhs), ret_ty)
185 }
186
187 /// Retrieve the information we are losing (making dynamic) in an unsizing
188 /// adjustment.
189 ///
190 /// The `old_info` argument is a bit funny. It is intended for use
191 /// in an upcast, where the new vtable for an object will be derived
192 /// from the old one.
193 pub fn unsized_info(
194     cx: &CodegenCx<'ll, 'tcx>,
195     source: Ty<'tcx>,
196     target: Ty<'tcx>,
197     old_info: Option<&'ll Value>,
198 ) -> &'ll Value {
199     let (source, target) = cx.tcx.struct_lockstep_tails(source, target);
200     match (&source.sty, &target.sty) {
201         (&ty::Array(_, len), &ty::Slice(_)) => {
202             C_usize(cx, len.unwrap_usize(cx.tcx))
203         }
204         (&ty::Dynamic(..), &ty::Dynamic(..)) => {
205             // For now, upcasts are limited to changes in marker
206             // traits, and hence never actually require an actual
207             // change to the vtable.
208             old_info.expect("unsized_info: missing old info for trait upcast")
209         }
210         (_, &ty::Dynamic(ref data, ..)) => {
211             let vtable_ptr = cx.layout_of(cx.tcx.mk_mut_ptr(target))
212                 .field(cx, abi::FAT_PTR_EXTRA);
213             consts::ptrcast(meth::get_vtable(cx, source, data.principal()),
214                             vtable_ptr.llvm_type(cx))
215         }
216         _ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
217                   source,
218                   target),
219     }
220 }
221
222 /// Coerce `src` to `dst_ty`. `src_ty` must be a thin pointer.
223 pub fn unsize_thin_ptr(
224     bx: &Builder<'a, 'll, 'tcx>,
225     src: &'ll Value,
226     src_ty: Ty<'tcx>,
227     dst_ty: Ty<'tcx>
228 ) -> (&'ll Value, &'ll Value) {
229     debug!("unsize_thin_ptr: {:?} => {:?}", src_ty, dst_ty);
230     match (&src_ty.sty, &dst_ty.sty) {
231         (&ty::Ref(_, a, _),
232          &ty::Ref(_, b, _)) |
233         (&ty::Ref(_, a, _),
234          &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) |
235         (&ty::RawPtr(ty::TypeAndMut { ty: a, .. }),
236          &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
237             assert!(bx.cx.type_is_sized(a));
238             let ptr_ty = bx.cx.layout_of(b).llvm_type(bx.cx).ptr_to();
239             (bx.pointercast(src, ptr_ty), unsized_info(bx.cx, a, b, None))
240         }
241         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
242             let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
243             assert!(bx.cx.type_is_sized(a));
244             let ptr_ty = bx.cx.layout_of(b).llvm_type(bx.cx).ptr_to();
245             (bx.pointercast(src, ptr_ty), unsized_info(bx.cx, a, b, None))
246         }
247         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
248             assert_eq!(def_a, def_b);
249
250             let src_layout = bx.cx.layout_of(src_ty);
251             let dst_layout = bx.cx.layout_of(dst_ty);
252             let mut result = None;
253             for i in 0..src_layout.fields.count() {
254                 let src_f = src_layout.field(bx.cx, i);
255                 assert_eq!(src_layout.fields.offset(i).bytes(), 0);
256                 assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
257                 if src_f.is_zst() {
258                     continue;
259                 }
260                 assert_eq!(src_layout.size, src_f.size);
261
262                 let dst_f = dst_layout.field(bx.cx, i);
263                 assert_ne!(src_f.ty, dst_f.ty);
264                 assert_eq!(result, None);
265                 result = Some(unsize_thin_ptr(bx, src, src_f.ty, dst_f.ty));
266             }
267             let (lldata, llextra) = result.unwrap();
268             // HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
269             (bx.bitcast(lldata, dst_layout.scalar_pair_element_llvm_type(bx.cx, 0, true)),
270              bx.bitcast(llextra, dst_layout.scalar_pair_element_llvm_type(bx.cx, 1, true)))
271         }
272         _ => bug!("unsize_thin_ptr: called on bad types"),
273     }
274 }
275
276 /// Coerce `src`, which is a reference to a value of type `src_ty`,
277 /// to a value of type `dst_ty` and store the result in `dst`
278 pub fn coerce_unsized_into(
279     bx: &Builder<'a, 'll, 'tcx>,
280     src: PlaceRef<'tcx, &'ll Value>,
281     dst: PlaceRef<'tcx, &'ll Value>
282 ) {
283     let src_ty = src.layout.ty;
284     let dst_ty = dst.layout.ty;
285     let coerce_ptr = || {
286         let (base, info) = match src.load(bx).val {
287             OperandValue::Pair(base, info) => {
288                 // fat-ptr to fat-ptr unsize preserves the vtable
289                 // i.e. &'a fmt::Debug+Send => &'a fmt::Debug
290                 // So we need to pointercast the base to ensure
291                 // the types match up.
292                 let thin_ptr = dst.layout.field(bx.cx, abi::FAT_PTR_ADDR);
293                 (bx.pointercast(base, thin_ptr.llvm_type(bx.cx)), info)
294             }
295             OperandValue::Immediate(base) => {
296                 unsize_thin_ptr(bx, base, src_ty, dst_ty)
297             }
298             OperandValue::Ref(..) => bug!()
299         };
300         OperandValue::Pair(base, info).store(bx, dst);
301     };
302     match (&src_ty.sty, &dst_ty.sty) {
303         (&ty::Ref(..), &ty::Ref(..)) |
304         (&ty::Ref(..), &ty::RawPtr(..)) |
305         (&ty::RawPtr(..), &ty::RawPtr(..)) => {
306             coerce_ptr()
307         }
308         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
309             coerce_ptr()
310         }
311
312         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
313             assert_eq!(def_a, def_b);
314
315             for i in 0..def_a.variants[VariantIdx::new(0)].fields.len() {
316                 let src_f = src.project_field(bx, i);
317                 let dst_f = dst.project_field(bx, i);
318
319                 if dst_f.layout.is_zst() {
320                     continue;
321                 }
322
323                 if src_f.layout.ty == dst_f.layout.ty {
324                     memcpy_ty(bx, dst_f.llval, dst_f.align, src_f.llval, src_f.align,
325                               src_f.layout, MemFlags::empty());
326                 } else {
327                     coerce_unsized_into(bx, src_f, dst_f);
328                 }
329             }
330         }
331         _ => bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}",
332                   src_ty,
333                   dst_ty),
334     }
335 }
336
337 pub fn cast_shift_expr_rhs(
338     cx: &Builder<'_, 'll, '_>, op: hir::BinOpKind, lhs: &'ll Value, rhs: &'ll Value
339 ) -> &'ll Value {
340     cast_shift_rhs(op, lhs, rhs, |a, b| cx.trunc(a, b), |a, b| cx.zext(a, b))
341 }
342
343 fn cast_shift_rhs<'ll, F, G>(op: hir::BinOpKind,
344                              lhs: &'ll Value,
345                              rhs: &'ll Value,
346                              trunc: F,
347                              zext: G)
348                              -> &'ll Value
349     where F: FnOnce(&'ll Value, &'ll Type) -> &'ll Value,
350           G: FnOnce(&'ll Value, &'ll Type) -> &'ll Value
351 {
352     // Shifts may have any size int on the rhs
353     if op.is_shift() {
354         let mut rhs_llty = val_ty(rhs);
355         let mut lhs_llty = val_ty(lhs);
356         if rhs_llty.kind() == TypeKind::Vector {
357             rhs_llty = rhs_llty.element_type()
358         }
359         if lhs_llty.kind() == TypeKind::Vector {
360             lhs_llty = lhs_llty.element_type()
361         }
362         let rhs_sz = rhs_llty.int_width();
363         let lhs_sz = lhs_llty.int_width();
364         if lhs_sz < rhs_sz {
365             trunc(rhs, lhs_llty)
366         } else if lhs_sz > rhs_sz {
367             // FIXME (#1877: If in the future shifting by negative
368             // values is no longer undefined then this is wrong.
369             zext(rhs, lhs_llty)
370         } else {
371             rhs
372         }
373     } else {
374         rhs
375     }
376 }
377
378 /// Returns whether this session's target will use SEH-based unwinding.
379 ///
380 /// This is only true for MSVC targets, and even then the 64-bit MSVC target
381 /// currently uses SEH-ish unwinding with DWARF info tables to the side (same as
382 /// 64-bit MinGW) instead of "full SEH".
383 pub fn wants_msvc_seh(sess: &Session) -> bool {
384     sess.target.target.options.is_like_msvc
385 }
386
387 pub fn call_assume(bx: &Builder<'_, 'll, '_>, val: &'ll Value) {
388     let assume_intrinsic = bx.cx.get_intrinsic("llvm.assume");
389     bx.call(assume_intrinsic, &[val], None);
390 }
391
392 pub fn from_immediate<'a, 'll: 'a, 'tcx: 'll,
393     Value : ?Sized,
394     Builder: BuilderMethods<'a, 'll, 'tcx, Value>>(
395     bx: &Builder,
396     val: &'ll Value
397 ) -> &'ll Value where Value : ValueTrait {
398     if val_ty(val) == Type::i1(bx.cx()) {
399         bx.zext(val, Type::i8(bx.cx()))
400     } else {
401         val
402     }
403 }
404
405 pub fn to_immediate(
406     bx: &Builder<'_, 'll, '_>,
407     val: &'ll Value,
408     layout: layout::TyLayout,
409 ) -> &'ll Value {
410     if let layout::Abi::Scalar(ref scalar) = layout.abi {
411         return to_immediate_scalar(bx, val, scalar);
412     }
413     val
414 }
415
416 pub fn to_immediate_scalar(
417     bx: &Builder<'_, 'll, '_>,
418     val: &'ll Value,
419     scalar: &layout::Scalar,
420 ) -> &'ll Value {
421     if scalar.is_bool() {
422         return bx.trunc(val, Type::i1(bx.cx));
423     }
424     val
425 }
426
427 pub fn call_memcpy<'a, 'll: 'a, 'tcx: 'll,
428     Value : ?Sized,
429     Builder: BuilderMethods<'a, 'll, 'tcx, Value>>(
430     bx: &Builder,
431     dst: &'ll Value,
432     dst_align: Align,
433     src: &'ll Value,
434     src_align: Align,
435     n_bytes: &'ll Value,
436     flags: MemFlags,
437 ) where Value : ValueTrait {
438     if flags.contains(MemFlags::NONTEMPORAL) {
439         // HACK(nox): This is inefficient but there is no nontemporal memcpy.
440         let val = bx.load(src, src_align);
441         let ptr = bx.pointercast(dst, val_ty(val).ptr_to());
442         bx.store_with_flags(val, ptr, dst_align, flags);
443         return;
444     }
445     let cx = bx.cx();
446     let src_ptr = bx.pointercast(src, Type::i8p(cx));
447     let dst_ptr = bx.pointercast(dst, Type::i8p(cx));
448     let size = bx.intcast(n_bytes, cx.isize_ty, false);
449     let volatile = flags.contains(MemFlags::VOLATILE);
450     bx.memcpy(dst_ptr, dst_align.abi(), src_ptr, src_align.abi(), size, volatile);
451 }
452
453 pub fn memcpy_ty<'a, 'll: 'a, 'tcx: 'll,
454     Value : ?Sized,
455     Builder: BuilderMethods<'a, 'll, 'tcx, Value>>(
456     bx: &Builder,
457     dst: &'ll Value,
458     dst_align: Align,
459     src: &'ll Value,
460     src_align: Align,
461     layout: TyLayout<'tcx>,
462     flags: MemFlags,
463 ) where Value : ValueTrait {
464     let size = layout.size.bytes();
465     if size == 0 {
466         return;
467     }
468
469     call_memcpy(bx, dst, dst_align, src, src_align, C_usize(bx.cx(), size), flags);
470 }
471
472 pub fn call_memset(
473     bx: &Builder<'_, 'll, '_>,
474     ptr: &'ll Value,
475     fill_byte: &'ll Value,
476     size: &'ll Value,
477     align: &'ll Value,
478     volatile: bool,
479 ) -> &'ll Value {
480     let ptr_width = &bx.cx.sess().target.target.target_pointer_width;
481     let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
482     let llintrinsicfn = bx.cx.get_intrinsic(&intrinsic_key);
483     let volatile = C_bool(bx.cx, volatile);
484     bx.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None)
485 }
486
487 pub fn codegen_instance<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, instance: Instance<'tcx>) {
488     let _s = if cx.sess().codegen_stats() {
489         let mut instance_name = String::new();
490         DefPathBasedNames::new(cx.tcx, true, true)
491             .push_def_path(instance.def_id(), &mut instance_name);
492         Some(StatRecorder::new(cx, instance_name))
493     } else {
494         None
495     };
496
497     // this is an info! to allow collecting monomorphization statistics
498     // and to allow finding the last function before LLVM aborts from
499     // release builds.
500     info!("codegen_instance({})", instance);
501
502     let sig = instance.fn_sig(cx.tcx);
503     let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
504
505     let lldecl = cx.instances.borrow().get(&instance).cloned().unwrap_or_else(||
506         bug!("Instance `{:?}` not already declared", instance));
507
508     cx.stats.borrow_mut().n_closures += 1;
509
510     let mir = cx.tcx.instance_mir(instance.def);
511     mir::codegen_mir(cx, lldecl, &mir, instance, sig);
512 }
513
514 pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
515     let sect = match attrs.link_section {
516         Some(name) => name,
517         None => return,
518     };
519     unsafe {
520         let buf = SmallCStr::new(&sect.as_str());
521         llvm::LLVMSetSection(llval, buf.as_ptr());
522     }
523 }
524
525 /// Create the `main` function which will initialize the rust runtime and call
526 /// users main function.
527 fn maybe_create_entry_wrapper(cx: &CodegenCx) {
528     let (main_def_id, span) = match *cx.sess().entry_fn.borrow() {
529         Some((id, span, _)) => {
530             (cx.tcx.hir.local_def_id(id), span)
531         }
532         None => return,
533     };
534
535     let instance = Instance::mono(cx.tcx, main_def_id);
536
537     if !cx.codegen_unit.contains_item(&MonoItem::Fn(instance)) {
538         // We want to create the wrapper in the same codegen unit as Rust's main
539         // function.
540         return;
541     }
542
543     let main_llfn = callee::get_fn(cx, instance);
544
545     let et = cx.sess().entry_fn.get().map(|e| e.2);
546     match et {
547         Some(EntryFnType::Main) => create_entry_fn(cx, span, main_llfn, main_def_id, true),
548         Some(EntryFnType::Start) => create_entry_fn(cx, span, main_llfn, main_def_id, false),
549         None => {}    // Do nothing.
550     }
551
552     fn create_entry_fn(
553         cx: &CodegenCx<'ll, '_>,
554         sp: Span,
555         rust_main: &'ll Value,
556         rust_main_def_id: DefId,
557         use_start_lang_item: bool,
558     ) {
559         let llfty =
560             Type::func::<Value>(&[Type::c_int(cx), Type::i8p(cx).ptr_to()], Type::c_int(cx));
561
562         let main_ret_ty = cx.tcx.fn_sig(rust_main_def_id).output();
563         // Given that `main()` has no arguments,
564         // then its return type cannot have
565         // late-bound regions, since late-bound
566         // regions must appear in the argument
567         // listing.
568         let main_ret_ty = cx.tcx.erase_regions(
569             &main_ret_ty.no_bound_vars().unwrap(),
570         );
571
572         if declare::get_defined_value(cx, "main").is_some() {
573             // FIXME: We should be smart and show a better diagnostic here.
574             cx.sess().struct_span_err(sp, "entry symbol `main` defined multiple times")
575                      .help("did you use #[no_mangle] on `fn main`? Use #[start] instead")
576                      .emit();
577             cx.sess().abort_if_errors();
578             bug!();
579         }
580         let llfn = declare::declare_cfn(cx, "main", llfty);
581
582         // `main` should respect same config for frame pointer elimination as rest of code
583         attributes::set_frame_pointer_elimination(cx, llfn);
584         attributes::apply_target_cpu_attr(cx, llfn);
585
586         let bx = Builder::new_block(cx, llfn, "top");
587
588         debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(&bx);
589
590         // Params from native main() used as args for rust start function
591         let param_argc = get_param(llfn, 0);
592         let param_argv = get_param(llfn, 1);
593         let arg_argc = bx.intcast(param_argc, cx.isize_ty, true);
594         let arg_argv = param_argv;
595
596         let (start_fn, args) = if use_start_lang_item {
597             let start_def_id = cx.tcx.require_lang_item(StartFnLangItem);
598             let start_fn = callee::resolve_and_get_fn(
599                 cx,
600                 start_def_id,
601                 cx.tcx.intern_substs(&[main_ret_ty.into()]),
602             );
603             (start_fn, vec![bx.pointercast(rust_main, Type::i8p(cx).ptr_to()),
604                             arg_argc, arg_argv])
605         } else {
606             debug!("using user-defined start fn");
607             (rust_main, vec![arg_argc, arg_argv])
608         };
609
610         let result = bx.call(start_fn, &args, None);
611         bx.ret(bx.intcast(result, Type::c_int(cx), true));
612     }
613 }
614
615 fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
616                             llvm_module: &ModuleLlvm)
617                             -> EncodedMetadata {
618     use std::io::Write;
619     use flate2::Compression;
620     use flate2::write::DeflateEncoder;
621
622     let (metadata_llcx, metadata_llmod) = (&*llvm_module.llcx, llvm_module.llmod());
623
624     #[derive(PartialEq, Eq, PartialOrd, Ord)]
625     enum MetadataKind {
626         None,
627         Uncompressed,
628         Compressed
629     }
630
631     let kind = tcx.sess.crate_types.borrow().iter().map(|ty| {
632         match *ty {
633             config::CrateType::Executable |
634             config::CrateType::Staticlib |
635             config::CrateType::Cdylib => MetadataKind::None,
636
637             config::CrateType::Rlib => MetadataKind::Uncompressed,
638
639             config::CrateType::Dylib |
640             config::CrateType::ProcMacro => MetadataKind::Compressed,
641         }
642     }).max().unwrap_or(MetadataKind::None);
643
644     if kind == MetadataKind::None {
645         return EncodedMetadata::new();
646     }
647
648     let metadata = tcx.encode_metadata();
649     if kind == MetadataKind::Uncompressed {
650         return metadata;
651     }
652
653     assert!(kind == MetadataKind::Compressed);
654     let mut compressed = tcx.metadata_encoding_version();
655     DeflateEncoder::new(&mut compressed, Compression::fast())
656         .write_all(&metadata.raw_data).unwrap();
657
658     let llmeta = C_bytes_in_context(metadata_llcx, &compressed);
659     let llconst = C_struct_in_context(metadata_llcx, &[llmeta], false);
660     let name = exported_symbols::metadata_symbol_name(tcx);
661     let buf = CString::new(name).unwrap();
662     let llglobal = unsafe {
663         llvm::LLVMAddGlobal(metadata_llmod, val_ty(llconst), buf.as_ptr())
664     };
665     unsafe {
666         llvm::LLVMSetInitializer(llglobal, llconst);
667         let section_name = metadata::metadata_section_name(&tcx.sess.target.target);
668         let name = SmallCStr::new(section_name);
669         llvm::LLVMSetSection(llglobal, name.as_ptr());
670
671         // Also generate a .section directive to force no
672         // flags, at least for ELF outputs, so that the
673         // metadata doesn't get loaded into memory.
674         let directive = format!(".section {}", section_name);
675         let directive = CString::new(directive).unwrap();
676         llvm::LLVMSetModuleInlineAsm(metadata_llmod, directive.as_ptr())
677     }
678     return metadata;
679 }
680
681 pub struct ValueIter<'ll> {
682     cur: Option<&'ll Value>,
683     step: unsafe extern "C" fn(&'ll Value) -> Option<&'ll Value>,
684 }
685
686 impl Iterator for ValueIter<'ll> {
687     type Item = &'ll Value;
688
689     fn next(&mut self) -> Option<&'ll Value> {
690         let old = self.cur;
691         if let Some(old) = old {
692             self.cur = unsafe { (self.step)(old) };
693         }
694         old
695     }
696 }
697
698 pub fn iter_globals(llmod: &'ll llvm::Module) -> ValueIter<'ll> {
699     unsafe {
700         ValueIter {
701             cur: llvm::LLVMGetFirstGlobal(llmod),
702             step: llvm::LLVMGetNextGlobal,
703         }
704     }
705 }
706
707 fn determine_cgu_reuse<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
708                                  cgu: &CodegenUnit<'tcx>)
709                                  -> CguReuse {
710     if !tcx.dep_graph.is_fully_enabled() {
711         return CguReuse::No
712     }
713
714     let work_product_id = &cgu.work_product_id();
715     if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
716         // We don't have anything cached for this CGU. This can happen
717         // if the CGU did not exist in the previous session.
718         return CguReuse::No
719     }
720
721     // Try to mark the CGU as green. If it we can do so, it means that nothing
722     // affecting the LLVM module has changed and we can re-use a cached version.
723     // If we compile with any kind of LTO, this means we can re-use the bitcode
724     // of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
725     // know that later). If we are not doing LTO, there is only one optimized
726     // version of each module, so we re-use that.
727     let dep_node = cgu.codegen_dep_node(tcx);
728     assert!(!tcx.dep_graph.dep_node_exists(&dep_node),
729         "CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
730         cgu.name());
731
732     if tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some() {
733         // We can re-use either the pre- or the post-thinlto state
734         if tcx.sess.lto() != Lto::No {
735             CguReuse::PreLto
736         } else {
737             CguReuse::PostLto
738         }
739     } else {
740         CguReuse::No
741     }
742 }
743
744 pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
745                                rx: mpsc::Receiver<Box<dyn Any + Send>>)
746                                -> OngoingCodegen
747 {
748     check_for_rustc_errors_attr(tcx);
749
750     let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
751
752     // Codegen the metadata.
753     tcx.sess.profiler(|p| p.start_activity(ProfileCategory::Codegen));
754
755     let metadata_cgu_name = cgu_name_builder.build_cgu_name(LOCAL_CRATE,
756                                                             &["crate"],
757                                                             Some("metadata")).as_str()
758                                                                              .to_string();
759     let metadata_llvm_module = ModuleLlvm::new(tcx.sess, &metadata_cgu_name);
760     let metadata = time(tcx.sess, "write metadata", || {
761         write_metadata(tcx, &metadata_llvm_module)
762     });
763     tcx.sess.profiler(|p| p.end_activity(ProfileCategory::Codegen));
764
765     let metadata_module = ModuleCodegen {
766         name: metadata_cgu_name,
767         module_llvm: metadata_llvm_module,
768         kind: ModuleKind::Metadata,
769     };
770
771     let time_graph = if tcx.sess.opts.debugging_opts.codegen_time_graph {
772         Some(time_graph::TimeGraph::new())
773     } else {
774         None
775     };
776
777     // Skip crate items and just output metadata in -Z no-codegen mode.
778     if tcx.sess.opts.debugging_opts.no_codegen ||
779        !tcx.sess.opts.output_types.should_codegen() {
780         let ongoing_codegen = write::start_async_codegen(
781             tcx,
782             time_graph,
783             metadata,
784             rx,
785             1);
786
787         ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
788         ongoing_codegen.codegen_finished(tcx);
789
790         assert_and_save_dep_graph(tcx);
791
792         ongoing_codegen.check_for_errors(tcx.sess);
793
794         return ongoing_codegen;
795     }
796
797     // Run the monomorphization collector and partition the collected items into
798     // codegen units.
799     let codegen_units = tcx.collect_and_partition_mono_items(LOCAL_CRATE).1;
800     let codegen_units = (*codegen_units).clone();
801
802     // Force all codegen_unit queries so they are already either red or green
803     // when compile_codegen_unit accesses them. We are not able to re-execute
804     // the codegen_unit query from just the DepNode, so an unknown color would
805     // lead to having to re-execute compile_codegen_unit, possibly
806     // unnecessarily.
807     if tcx.dep_graph.is_fully_enabled() {
808         for cgu in &codegen_units {
809             tcx.codegen_unit(cgu.name().clone());
810         }
811     }
812
813     let ongoing_codegen = write::start_async_codegen(
814         tcx,
815         time_graph.clone(),
816         metadata,
817         rx,
818         codegen_units.len());
819     let ongoing_codegen = AbortCodegenOnDrop(Some(ongoing_codegen));
820
821     // Codegen an allocator shim, if necessary.
822     //
823     // If the crate doesn't have an `allocator_kind` set then there's definitely
824     // no shim to generate. Otherwise we also check our dependency graph for all
825     // our output crate types. If anything there looks like its a `Dynamic`
826     // linkage, then it's already got an allocator shim and we'll be using that
827     // one instead. If nothing exists then it's our job to generate the
828     // allocator!
829     let any_dynamic_crate = tcx.sess.dependency_formats.borrow()
830         .iter()
831         .any(|(_, list)| {
832             use rustc::middle::dependency_format::Linkage;
833             list.iter().any(|&linkage| linkage == Linkage::Dynamic)
834         });
835     let allocator_module = if any_dynamic_crate {
836         None
837     } else if let Some(kind) = *tcx.sess.allocator_kind.get() {
838         let llmod_id = cgu_name_builder.build_cgu_name(LOCAL_CRATE,
839                                                        &["crate"],
840                                                        Some("allocator")).as_str()
841                                                                          .to_string();
842         let modules = ModuleLlvm::new(tcx.sess, &llmod_id);
843         time(tcx.sess, "write allocator module", || {
844             unsafe {
845                 allocator::codegen(tcx, &modules, kind)
846             }
847         });
848
849         Some(ModuleCodegen {
850             name: llmod_id,
851             module_llvm: modules,
852             kind: ModuleKind::Allocator,
853         })
854     } else {
855         None
856     };
857
858     if let Some(allocator_module) = allocator_module {
859         ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, allocator_module);
860     }
861
862     ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
863
864     // We sort the codegen units by size. This way we can schedule work for LLVM
865     // a bit more efficiently.
866     let codegen_units = {
867         let mut codegen_units = codegen_units;
868         codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
869         codegen_units
870     };
871
872     let mut total_codegen_time = Duration::new(0, 0);
873     let mut all_stats = Stats::default();
874
875     for cgu in codegen_units.into_iter() {
876         ongoing_codegen.wait_for_signal_to_codegen_item();
877         ongoing_codegen.check_for_errors(tcx.sess);
878
879         let cgu_reuse = determine_cgu_reuse(tcx, &cgu);
880         tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse);
881
882         match cgu_reuse {
883             CguReuse::No => {
884                 let _timing_guard = time_graph.as_ref().map(|time_graph| {
885                     time_graph.start(write::CODEGEN_WORKER_TIMELINE,
886                                      write::CODEGEN_WORK_PACKAGE_KIND,
887                                      &format!("codegen {}", cgu.name()))
888                 });
889                 let start_time = Instant::now();
890                 let stats = compile_codegen_unit(tcx, *cgu.name());
891                 all_stats.extend(stats);
892                 total_codegen_time += start_time.elapsed();
893                 false
894             }
895             CguReuse::PreLto => {
896                 write::submit_pre_lto_module_to_llvm(tcx, CachedModuleCodegen {
897                     name: cgu.name().to_string(),
898                     source: cgu.work_product(tcx),
899                 });
900                 true
901             }
902             CguReuse::PostLto => {
903                 write::submit_post_lto_module_to_llvm(tcx, CachedModuleCodegen {
904                     name: cgu.name().to_string(),
905                     source: cgu.work_product(tcx),
906                 });
907                 true
908             }
909         };
910     }
911
912     ongoing_codegen.codegen_finished(tcx);
913
914     // Since the main thread is sometimes blocked during codegen, we keep track
915     // -Ztime-passes output manually.
916     print_time_passes_entry(tcx.sess.time_passes(),
917                             "codegen to LLVM IR",
918                             total_codegen_time);
919
920     rustc_incremental::assert_module_sources::assert_module_sources(tcx);
921
922     symbol_names_test::report_symbol_names(tcx);
923
924     if tcx.sess.codegen_stats() {
925         println!("--- codegen stats ---");
926         println!("n_glues_created: {}", all_stats.n_glues_created);
927         println!("n_null_glues: {}", all_stats.n_null_glues);
928         println!("n_real_glues: {}", all_stats.n_real_glues);
929
930         println!("n_fns: {}", all_stats.n_fns);
931         println!("n_inlines: {}", all_stats.n_inlines);
932         println!("n_closures: {}", all_stats.n_closures);
933         println!("fn stats:");
934         all_stats.fn_stats.sort_by_key(|&(_, insns)| insns);
935         for &(ref name, insns) in all_stats.fn_stats.iter() {
936             println!("{} insns, {}", insns, *name);
937         }
938     }
939
940     if tcx.sess.count_llvm_insns() {
941         for (k, v) in all_stats.llvm_insns.iter() {
942             println!("{:7} {}", *v, *k);
943         }
944     }
945
946     ongoing_codegen.check_for_errors(tcx.sess);
947
948     assert_and_save_dep_graph(tcx);
949     ongoing_codegen.into_inner()
950 }
951
952 /// A curious wrapper structure whose only purpose is to call `codegen_aborted`
953 /// when it's dropped abnormally.
954 ///
955 /// In the process of working on rust-lang/rust#55238 a mysterious segfault was
956 /// stumbled upon. The segfault was never reproduced locally, but it was
957 /// suspected to be related to the fact that codegen worker threads were
958 /// sticking around by the time the main thread was exiting, causing issues.
959 ///
960 /// This structure is an attempt to fix that issue where the `codegen_aborted`
961 /// message will block until all workers have finished. This should ensure that
962 /// even if the main codegen thread panics we'll wait for pending work to
963 /// complete before returning from the main thread, hopefully avoiding
964 /// segfaults.
965 ///
966 /// If you see this comment in the code, then it means that this workaround
967 /// worked! We may yet one day track down the mysterious cause of that
968 /// segfault...
969 struct AbortCodegenOnDrop(Option<OngoingCodegen>);
970
971 impl AbortCodegenOnDrop {
972     fn into_inner(mut self) -> OngoingCodegen {
973         self.0.take().unwrap()
974     }
975 }
976
977 impl Deref for AbortCodegenOnDrop {
978     type Target = OngoingCodegen;
979
980     fn deref(&self) -> &OngoingCodegen {
981         self.0.as_ref().unwrap()
982     }
983 }
984
985 impl DerefMut for AbortCodegenOnDrop {
986     fn deref_mut(&mut self) -> &mut OngoingCodegen {
987         self.0.as_mut().unwrap()
988     }
989 }
990
991 impl Drop for AbortCodegenOnDrop {
992     fn drop(&mut self) {
993         if let Some(codegen) = self.0.take() {
994             codegen.codegen_aborted();
995         }
996     }
997 }
998
999 fn assert_and_save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
1000     time(tcx.sess,
1001          "assert dep graph",
1002          || rustc_incremental::assert_dep_graph(tcx));
1003
1004     time(tcx.sess,
1005          "serialize dep graph",
1006          || rustc_incremental::save_dep_graph(tcx));
1007 }
1008
1009 impl CrateInfo {
1010     pub fn new(tcx: TyCtxt) -> CrateInfo {
1011         let mut info = CrateInfo {
1012             panic_runtime: None,
1013             compiler_builtins: None,
1014             profiler_runtime: None,
1015             sanitizer_runtime: None,
1016             is_no_builtins: Default::default(),
1017             native_libraries: Default::default(),
1018             used_libraries: tcx.native_libraries(LOCAL_CRATE),
1019             link_args: tcx.link_args(LOCAL_CRATE),
1020             crate_name: Default::default(),
1021             used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic),
1022             used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),
1023             used_crate_source: Default::default(),
1024             wasm_imports: Default::default(),
1025             lang_item_to_crate: Default::default(),
1026             missing_lang_items: Default::default(),
1027         };
1028         let lang_items = tcx.lang_items();
1029
1030         let load_wasm_items = tcx.sess.crate_types.borrow()
1031             .iter()
1032             .any(|c| *c != config::CrateType::Rlib) &&
1033             tcx.sess.opts.target_triple.triple() == "wasm32-unknown-unknown";
1034
1035         if load_wasm_items {
1036             info.load_wasm_imports(tcx, LOCAL_CRATE);
1037         }
1038
1039         let crates = tcx.crates();
1040
1041         let n_crates = crates.len();
1042         info.native_libraries.reserve(n_crates);
1043         info.crate_name.reserve(n_crates);
1044         info.used_crate_source.reserve(n_crates);
1045         info.missing_lang_items.reserve(n_crates);
1046
1047         for &cnum in crates.iter() {
1048             info.native_libraries.insert(cnum, tcx.native_libraries(cnum));
1049             info.crate_name.insert(cnum, tcx.crate_name(cnum).to_string());
1050             info.used_crate_source.insert(cnum, tcx.used_crate_source(cnum));
1051             if tcx.is_panic_runtime(cnum) {
1052                 info.panic_runtime = Some(cnum);
1053             }
1054             if tcx.is_compiler_builtins(cnum) {
1055                 info.compiler_builtins = Some(cnum);
1056             }
1057             if tcx.is_profiler_runtime(cnum) {
1058                 info.profiler_runtime = Some(cnum);
1059             }
1060             if tcx.is_sanitizer_runtime(cnum) {
1061                 info.sanitizer_runtime = Some(cnum);
1062             }
1063             if tcx.is_no_builtins(cnum) {
1064                 info.is_no_builtins.insert(cnum);
1065             }
1066             if load_wasm_items {
1067                 info.load_wasm_imports(tcx, cnum);
1068             }
1069             let missing = tcx.missing_lang_items(cnum);
1070             for &item in missing.iter() {
1071                 if let Ok(id) = lang_items.require(item) {
1072                     info.lang_item_to_crate.insert(item, id.krate);
1073                 }
1074             }
1075
1076             // No need to look for lang items that are whitelisted and don't
1077             // actually need to exist.
1078             let missing = missing.iter()
1079                 .cloned()
1080                 .filter(|&l| !weak_lang_items::whitelisted(tcx, l))
1081                 .collect();
1082             info.missing_lang_items.insert(cnum, missing);
1083         }
1084
1085         return info
1086     }
1087
1088     fn load_wasm_imports(&mut self, tcx: TyCtxt, cnum: CrateNum) {
1089         self.wasm_imports.extend(tcx.wasm_import_module_map(cnum).iter().map(|(&id, module)| {
1090             let instance = Instance::mono(tcx, id);
1091             let import_name = tcx.symbol_name(instance);
1092
1093             (import_name.to_string(), module.clone())
1094         }));
1095     }
1096 }
1097
1098 fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1099                                   cgu_name: InternedString)
1100                                   -> Stats {
1101     let start_time = Instant::now();
1102
1103     let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
1104     let ((stats, module), _) = tcx.dep_graph.with_task(dep_node,
1105                                                        tcx,
1106                                                        cgu_name,
1107                                                        module_codegen);
1108     let time_to_codegen = start_time.elapsed();
1109
1110     // We assume that the cost to run LLVM on a CGU is proportional to
1111     // the time we needed for codegenning it.
1112     let cost = time_to_codegen.as_secs() * 1_000_000_000 +
1113                time_to_codegen.subsec_nanos() as u64;
1114
1115     write::submit_codegened_module_to_llvm(tcx,
1116                                            module,
1117                                            cost);
1118     return stats;
1119
1120     fn module_codegen<'a, 'tcx>(
1121         tcx: TyCtxt<'a, 'tcx, 'tcx>,
1122         cgu_name: InternedString)
1123         -> (Stats, ModuleCodegen)
1124     {
1125         let cgu = tcx.codegen_unit(cgu_name);
1126
1127         // Instantiate monomorphizations without filling out definitions yet...
1128         let llvm_module = ModuleLlvm::new(tcx.sess, &cgu_name.as_str());
1129         let stats = {
1130             let cx = CodegenCx::new(tcx, cgu, &llvm_module);
1131             let mono_items = cx.codegen_unit
1132                                .items_in_deterministic_order(cx.tcx);
1133             for &(mono_item, (linkage, visibility)) in &mono_items {
1134                 mono_item.predefine(&cx, linkage, visibility);
1135             }
1136
1137             // ... and now that we have everything pre-defined, fill out those definitions.
1138             for &(mono_item, _) in &mono_items {
1139                 mono_item.define(&cx);
1140             }
1141
1142             // If this codegen unit contains the main function, also create the
1143             // wrapper here
1144             maybe_create_entry_wrapper(&cx);
1145
1146             // Run replace-all-uses-with for statics that need it
1147             for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
1148                 unsafe {
1149                     let bitcast = llvm::LLVMConstPointerCast(new_g, val_ty(old_g));
1150                     llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
1151                     llvm::LLVMDeleteGlobal(old_g);
1152                 }
1153             }
1154
1155             // Create the llvm.used variable
1156             // This variable has type [N x i8*] and is stored in the llvm.metadata section
1157             if !cx.used_statics.borrow().is_empty() {
1158                 let name = const_cstr!("llvm.used");
1159                 let section = const_cstr!("llvm.metadata");
1160                 let array = C_array(Type::i8(&cx).ptr_to(), &*cx.used_statics.borrow());
1161
1162                 unsafe {
1163                     let g = llvm::LLVMAddGlobal(cx.llmod,
1164                                                 val_ty(array),
1165                                                 name.as_ptr());
1166                     llvm::LLVMSetInitializer(g, array);
1167                     llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
1168                     llvm::LLVMSetSection(g, section.as_ptr());
1169                 }
1170             }
1171
1172             // Finalize debuginfo
1173             if cx.sess().opts.debuginfo != DebugInfo::None {
1174                 debuginfo::finalize(&cx);
1175             }
1176
1177             cx.stats.into_inner()
1178         };
1179
1180         (stats, ModuleCodegen {
1181             name: cgu_name.to_string(),
1182             module_llvm: llvm_module,
1183             kind: ModuleKind::Regular,
1184         })
1185     }
1186 }
1187
1188 pub fn provide_both(providers: &mut Providers) {
1189     providers.dllimport_foreign_items = |tcx, krate| {
1190         let module_map = tcx.foreign_modules(krate);
1191         let module_map = module_map.iter()
1192             .map(|lib| (lib.def_id, lib))
1193             .collect::<FxHashMap<_, _>>();
1194
1195         let dllimports = tcx.native_libraries(krate)
1196             .iter()
1197             .filter(|lib| {
1198                 if lib.kind != cstore::NativeLibraryKind::NativeUnknown {
1199                     return false
1200                 }
1201                 let cfg = match lib.cfg {
1202                     Some(ref cfg) => cfg,
1203                     None => return true,
1204                 };
1205                 attr::cfg_matches(cfg, &tcx.sess.parse_sess, None)
1206             })
1207             .filter_map(|lib| lib.foreign_module)
1208             .map(|id| &module_map[&id])
1209             .flat_map(|module| module.foreign_items.iter().cloned())
1210             .collect();
1211         Lrc::new(dllimports)
1212     };
1213
1214     providers.is_dllimport_foreign_item = |tcx, def_id| {
1215         tcx.dllimport_foreign_items(def_id.krate).contains(&def_id)
1216     };
1217 }
1218
1219 pub fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
1220     match linkage {
1221         Linkage::External => llvm::Linkage::ExternalLinkage,
1222         Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
1223         Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
1224         Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
1225         Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
1226         Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
1227         Linkage::Appending => llvm::Linkage::AppendingLinkage,
1228         Linkage::Internal => llvm::Linkage::InternalLinkage,
1229         Linkage::Private => llvm::Linkage::PrivateLinkage,
1230         Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
1231         Linkage::Common => llvm::Linkage::CommonLinkage,
1232     }
1233 }
1234
1235 pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
1236     match linkage {
1237         Visibility::Default => llvm::Visibility::Default,
1238         Visibility::Hidden => llvm::Visibility::Hidden,
1239         Visibility::Protected => llvm::Visibility::Protected,
1240     }
1241 }
1242
1243 // FIXME(mw): Anything that is produced via DepGraph::with_task() must implement
1244 //            the HashStable trait. Normally DepGraph::with_task() calls are
1245 //            hidden behind queries, but CGU creation is a special case in two
1246 //            ways: (1) it's not a query and (2) CGU are output nodes, so their
1247 //            Fingerprints are not actually needed. It remains to be clarified
1248 //            how exactly this case will be handled in the red/green system but
1249 //            for now we content ourselves with providing a no-op HashStable
1250 //            implementation for CGUs.
1251 mod temp_stable_hash_impls {
1252     use rustc_data_structures::stable_hasher::{StableHasherResult, StableHasher,
1253                                                HashStable};
1254     use ModuleCodegen;
1255
1256     impl<HCX> HashStable<HCX> for ModuleCodegen {
1257         fn hash_stable<W: StableHasherResult>(&self,
1258                                               _: &mut HCX,
1259                                               _: &mut StableHasher<W>) {
1260             // do nothing
1261         }
1262     }
1263 }