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