]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/context.rs
Auto merge of #34956 - nikomatsakis:incr-comp-o-files, r=mw
[rust.git] / src / librustc_trans / context.rs
1 // Copyright 2013 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 use llvm;
12 use llvm::{ContextRef, ModuleRef, ValueRef, BuilderRef};
13 use rustc::dep_graph::{DepNode, DepTrackingMap, DepTrackingMapConfig, WorkProduct};
14 use middle::cstore::LinkMeta;
15 use rustc::hir::def::ExportMap;
16 use rustc::hir::def_id::DefId;
17 use rustc::traits;
18 use rustc::mir::mir_map::MirMap;
19 use rustc::mir::repr as mir;
20 use adt;
21 use base;
22 use builder::Builder;
23 use common::BuilderRef_res;
24 use debuginfo;
25 use declare;
26 use glue::DropGlueKind;
27 use mir::CachedMir;
28 use monomorphize::Instance;
29
30 use partitioning::CodegenUnit;
31 use trans_item::TransItem;
32 use type_::{Type, TypeNames};
33 use rustc::ty::subst::{Substs, VecPerParamSpace};
34 use rustc::ty::{self, Ty, TyCtxt};
35 use session::config::NoDebugInfo;
36 use session::Session;
37 use symbol_map::SymbolMap;
38 use util::sha2::Sha256;
39 use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
40
41 use std::ffi::{CStr, CString};
42 use std::cell::{Cell, RefCell};
43 use std::marker::PhantomData;
44 use std::ptr;
45 use std::rc::Rc;
46 use std::str;
47 use syntax::ast;
48 use syntax::parse::token::InternedString;
49 use abi::FnType;
50
51 pub struct Stats {
52     pub n_glues_created: Cell<usize>,
53     pub n_null_glues: Cell<usize>,
54     pub n_real_glues: Cell<usize>,
55     pub n_fallback_instantiations: Cell<usize>,
56     pub n_fns: Cell<usize>,
57     pub n_monos: Cell<usize>,
58     pub n_inlines: Cell<usize>,
59     pub n_closures: Cell<usize>,
60     pub n_llvm_insns: Cell<usize>,
61     pub llvm_insns: RefCell<FnvHashMap<String, usize>>,
62     // (ident, llvm-instructions)
63     pub fn_stats: RefCell<Vec<(String, usize)> >,
64 }
65
66 /// The shared portion of a `CrateContext`.  There is one `SharedCrateContext`
67 /// per crate.  The data here is shared between all compilation units of the
68 /// crate, so it must not contain references to any LLVM data structures
69 /// (aside from metadata-related ones).
70 pub struct SharedCrateContext<'a, 'tcx: 'a> {
71     metadata_llmod: ModuleRef,
72     metadata_llcx: ContextRef,
73
74     export_map: ExportMap,
75     reachable: NodeSet,
76     link_meta: LinkMeta,
77     symbol_hasher: RefCell<Sha256>,
78     tcx: TyCtxt<'a, 'tcx, 'tcx>,
79     stats: Stats,
80     check_overflow: bool,
81     check_drop_flag_for_sanity: bool,
82     mir_map: &'a MirMap<'tcx>,
83     mir_cache: RefCell<DefIdMap<Rc<mir::Mir<'tcx>>>>,
84
85     use_dll_storage_attrs: bool,
86
87     translation_items: RefCell<FnvHashSet<TransItem<'tcx>>>,
88     trait_cache: RefCell<DepTrackingMap<TraitSelectionCache<'tcx>>>,
89 }
90
91 /// The local portion of a `CrateContext`.  There is one `LocalCrateContext`
92 /// per compilation unit.  Each one has its own LLVM `ContextRef` so that
93 /// several compilation units may be optimized in parallel.  All other LLVM
94 /// data structures in the `LocalCrateContext` are tied to that `ContextRef`.
95 pub struct LocalCrateContext<'tcx> {
96     llmod: ModuleRef,
97     llcx: ContextRef,
98     previous_work_product: Option<WorkProduct>,
99     tn: TypeNames, // FIXME: This seems to be largely unused.
100     codegen_unit: CodegenUnit<'tcx>,
101     needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
102     fn_pointer_shims: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
103     drop_glues: RefCell<FnvHashMap<DropGlueKind<'tcx>, (ValueRef, FnType)>>,
104     /// Track mapping of external ids to local items imported for inlining
105     external: RefCell<DefIdMap<Option<ast::NodeId>>>,
106     /// Backwards version of the `external` map (inlined items to where they
107     /// came from)
108     external_srcs: RefCell<NodeMap<DefId>>,
109     /// Cache instances of monomorphic and polymorphic items
110     instances: RefCell<FnvHashMap<Instance<'tcx>, ValueRef>>,
111     monomorphizing: RefCell<DefIdMap<usize>>,
112     /// Cache generated vtables
113     vtables: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>>,
114     /// Cache of constant strings,
115     const_cstr_cache: RefCell<FnvHashMap<InternedString, ValueRef>>,
116
117     /// Reverse-direction for const ptrs cast from globals.
118     /// Key is a ValueRef holding a *T,
119     /// Val is a ValueRef holding a *[T].
120     ///
121     /// Needed because LLVM loses pointer->pointee association
122     /// when we ptrcast, and we have to ptrcast during translation
123     /// of a [T] const because we form a slice, a (*T,usize) pair, not
124     /// a pointer to an LLVM array type. Similar for trait objects.
125     const_unsized: RefCell<FnvHashMap<ValueRef, ValueRef>>,
126
127     /// Cache of emitted const globals (value -> global)
128     const_globals: RefCell<FnvHashMap<ValueRef, ValueRef>>,
129
130     /// Cache of emitted const values
131     const_values: RefCell<FnvHashMap<(ast::NodeId, &'tcx Substs<'tcx>), ValueRef>>,
132
133     /// Cache of external const values
134     extern_const_values: RefCell<DefIdMap<ValueRef>>,
135
136     /// Mapping from static definitions to their DefId's.
137     statics: RefCell<FnvHashMap<ValueRef, DefId>>,
138
139     impl_method_cache: RefCell<FnvHashMap<(DefId, ast::Name), DefId>>,
140
141     /// Cache of closure wrappers for bare fn's.
142     closure_bare_wrapper_cache: RefCell<FnvHashMap<ValueRef, ValueRef>>,
143
144     /// List of globals for static variables which need to be passed to the
145     /// LLVM function ReplaceAllUsesWith (RAUW) when translation is complete.
146     /// (We have to make sure we don't invalidate any ValueRefs referring
147     /// to constants.)
148     statics_to_rauw: RefCell<Vec<(ValueRef, ValueRef)>>,
149
150     lltypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
151     llsizingtypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
152     adt_reprs: RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>>,
153     type_hashcodes: RefCell<FnvHashMap<Ty<'tcx>, String>>,
154     int_type: Type,
155     opaque_vec_type: Type,
156     builder: BuilderRef_res,
157
158     /// Holds the LLVM values for closure IDs.
159     closure_vals: RefCell<FnvHashMap<Instance<'tcx>, ValueRef>>,
160
161     dbg_cx: Option<debuginfo::CrateDebugContext<'tcx>>,
162
163     eh_personality: Cell<Option<ValueRef>>,
164     eh_unwind_resume: Cell<Option<ValueRef>>,
165     rust_try_fn: Cell<Option<ValueRef>>,
166
167     intrinsics: RefCell<FnvHashMap<&'static str, ValueRef>>,
168
169     /// Number of LLVM instructions translated into this `LocalCrateContext`.
170     /// This is used to perform some basic load-balancing to keep all LLVM
171     /// contexts around the same size.
172     n_llvm_insns: Cell<usize>,
173
174     /// Depth of the current type-of computation - used to bail out
175     type_of_depth: Cell<usize>,
176
177     symbol_map: Rc<SymbolMap<'tcx>>,
178 }
179
180 // Implement DepTrackingMapConfig for `trait_cache`
181 pub struct TraitSelectionCache<'tcx> {
182     data: PhantomData<&'tcx ()>
183 }
184
185 impl<'tcx> DepTrackingMapConfig for TraitSelectionCache<'tcx> {
186     type Key = ty::PolyTraitRef<'tcx>;
187     type Value = traits::Vtable<'tcx, ()>;
188     fn to_dep_node(key: &ty::PolyTraitRef<'tcx>) -> DepNode<DefId> {
189         key.to_poly_trait_predicate().dep_node()
190     }
191 }
192
193 /// This list owns a number of LocalCrateContexts and binds them to their common
194 /// SharedCrateContext. This type just exists as a convenience, something to
195 /// pass around all LocalCrateContexts with and get an iterator over them.
196 pub struct CrateContextList<'a, 'tcx: 'a> {
197     shared: &'a SharedCrateContext<'a, 'tcx>,
198     local_ccxs: Vec<LocalCrateContext<'tcx>>,
199 }
200
201 impl<'a, 'tcx: 'a> CrateContextList<'a, 'tcx> {
202     pub fn new(shared_ccx: &'a SharedCrateContext<'a, 'tcx>,
203                codegen_units: Vec<CodegenUnit<'tcx>>,
204                previous_work_products: Vec<Option<WorkProduct>>,
205                symbol_map: Rc<SymbolMap<'tcx>>)
206                -> CrateContextList<'a, 'tcx> {
207         CrateContextList {
208             shared: shared_ccx,
209             local_ccxs: codegen_units.into_iter().zip(previous_work_products).map(|(cgu, wp)| {
210                 LocalCrateContext::new(shared_ccx, cgu, wp, symbol_map.clone())
211             }).collect()
212         }
213     }
214
215     /// Iterate over all crate contexts, whether or not they need
216     /// translation.  That is, whether or not a `.o` file is available
217     /// for re-use from a previous incr. comp.).
218     pub fn iter_all<'b>(&'b self) -> CrateContextIterator<'b, 'tcx> {
219         CrateContextIterator {
220             shared: self.shared,
221             index: 0,
222             local_ccxs: &self.local_ccxs[..],
223             filter_to_previous_work_product_unavail: false,
224         }
225     }
226
227     /// Iterator over all CCX that need translation (cannot reuse results from
228     /// previous incr. comp.).
229     pub fn iter_need_trans<'b>(&'b self) -> CrateContextIterator<'b, 'tcx> {
230         CrateContextIterator {
231             shared: self.shared,
232             index: 0,
233             local_ccxs: &self.local_ccxs[..],
234             filter_to_previous_work_product_unavail: true,
235         }
236     }
237
238     pub fn shared(&self) -> &'a SharedCrateContext<'a, 'tcx> {
239         self.shared
240     }
241 }
242
243 /// A CrateContext value binds together one LocalCrateContext with the
244 /// SharedCrateContext. It exists as a convenience wrapper, so we don't have to
245 /// pass around (SharedCrateContext, LocalCrateContext) tuples all over trans.
246 pub struct CrateContext<'a, 'tcx: 'a> {
247     shared: &'a SharedCrateContext<'a, 'tcx>,
248     local_ccxs: &'a [LocalCrateContext<'tcx>],
249     /// The index of `local` in `local_ccxs`.  This is used in
250     /// `maybe_iter(true)` to identify the original `LocalCrateContext`.
251     index: usize,
252 }
253
254 pub struct CrateContextIterator<'a, 'tcx: 'a> {
255     shared: &'a SharedCrateContext<'a, 'tcx>,
256     local_ccxs: &'a [LocalCrateContext<'tcx>],
257     index: usize,
258
259     /// if true, only return results where `previous_work_product` is none
260     filter_to_previous_work_product_unavail: bool,
261 }
262
263 impl<'a, 'tcx> Iterator for CrateContextIterator<'a,'tcx> {
264     type Item = CrateContext<'a, 'tcx>;
265
266     fn next(&mut self) -> Option<CrateContext<'a, 'tcx>> {
267         loop {
268             if self.index >= self.local_ccxs.len() {
269                 return None;
270             }
271
272             let index = self.index;
273             self.index += 1;
274
275             let ccx = CrateContext {
276                 shared: self.shared,
277                 index: index,
278                 local_ccxs: self.local_ccxs,
279             };
280
281             if
282                 self.filter_to_previous_work_product_unavail &&
283                 ccx.previous_work_product().is_some()
284             {
285                 continue;
286             }
287
288             return Some(ccx);
289         }
290     }
291 }
292
293 /// The iterator produced by `CrateContext::maybe_iter`.
294 pub struct CrateContextMaybeIterator<'a, 'tcx: 'a> {
295     shared: &'a SharedCrateContext<'a, 'tcx>,
296     local_ccxs: &'a [LocalCrateContext<'tcx>],
297     index: usize,
298     single: bool,
299     origin: usize,
300 }
301
302 impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
303     type Item = (CrateContext<'a, 'tcx>, bool);
304
305     fn next(&mut self) -> Option<(CrateContext<'a, 'tcx>, bool)> {
306         if self.index >= self.local_ccxs.len() {
307             return None;
308         }
309
310         let index = self.index;
311         self.index += 1;
312         if self.single {
313             self.index = self.local_ccxs.len();
314         }
315
316         let ccx = CrateContext {
317             shared: self.shared,
318             index: index,
319             local_ccxs: self.local_ccxs
320         };
321         Some((ccx, index == self.origin))
322     }
323 }
324
325 unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
326     let llcx = llvm::LLVMContextCreate();
327     let mod_name = CString::new(mod_name).unwrap();
328     let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
329
330     // Ensure the data-layout values hardcoded remain the defaults.
331     if sess.target.target.options.is_builtin {
332         let tm = ::back::write::create_target_machine(sess);
333         llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
334         llvm::LLVMRustDisposeTargetMachine(tm);
335
336         let data_layout = llvm::LLVMGetDataLayout(llmod);
337         let data_layout = str::from_utf8(CStr::from_ptr(data_layout).to_bytes())
338             .ok().expect("got a non-UTF8 data-layout from LLVM");
339
340         if sess.target.target.data_layout != data_layout {
341             bug!("data-layout for builtin `{}` target, `{}`, \
342                   differs from LLVM default, `{}`",
343                  sess.target.target.llvm_target,
344                  sess.target.target.data_layout,
345                  data_layout);
346         }
347     }
348
349     let data_layout = CString::new(&sess.target.target.data_layout[..]).unwrap();
350     llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
351
352     let llvm_target = sess.target.target.llvm_target.as_bytes();
353     let llvm_target = CString::new(llvm_target).unwrap();
354     llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
355     (llcx, llmod)
356 }
357
358 impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
359     pub fn new(tcx: TyCtxt<'b, 'tcx, 'tcx>,
360                mir_map: &'b MirMap<'tcx>,
361                export_map: ExportMap,
362                symbol_hasher: Sha256,
363                link_meta: LinkMeta,
364                reachable: NodeSet,
365                check_overflow: bool,
366                check_drop_flag_for_sanity: bool)
367                -> SharedCrateContext<'b, 'tcx> {
368         let (metadata_llcx, metadata_llmod) = unsafe {
369             create_context_and_module(&tcx.sess, "metadata")
370         };
371
372         // An interesting part of Windows which MSVC forces our hand on (and
373         // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
374         // attributes in LLVM IR as well as native dependencies (in C these
375         // correspond to `__declspec(dllimport)`).
376         //
377         // Whenever a dynamic library is built by MSVC it must have its public
378         // interface specified by functions tagged with `dllexport` or otherwise
379         // they're not available to be linked against. This poses a few problems
380         // for the compiler, some of which are somewhat fundamental, but we use
381         // the `use_dll_storage_attrs` variable below to attach the `dllexport`
382         // attribute to all LLVM functions that are reachable (e.g. they're
383         // already tagged with external linkage). This is suboptimal for a few
384         // reasons:
385         //
386         // * If an object file will never be included in a dynamic library,
387         //   there's no need to attach the dllexport attribute. Most object
388         //   files in Rust are not destined to become part of a dll as binaries
389         //   are statically linked by default.
390         // * If the compiler is emitting both an rlib and a dylib, the same
391         //   source object file is currently used but with MSVC this may be less
392         //   feasible. The compiler may be able to get around this, but it may
393         //   involve some invasive changes to deal with this.
394         //
395         // The flipside of this situation is that whenever you link to a dll and
396         // you import a function from it, the import should be tagged with
397         // `dllimport`. At this time, however, the compiler does not emit
398         // `dllimport` for any declarations other than constants (where it is
399         // required), which is again suboptimal for even more reasons!
400         //
401         // * Calling a function imported from another dll without using
402         //   `dllimport` causes the linker/compiler to have extra overhead (one
403         //   `jmp` instruction on x86) when calling the function.
404         // * The same object file may be used in different circumstances, so a
405         //   function may be imported from a dll if the object is linked into a
406         //   dll, but it may be just linked against if linked into an rlib.
407         // * The compiler has no knowledge about whether native functions should
408         //   be tagged dllimport or not.
409         //
410         // For now the compiler takes the perf hit (I do not have any numbers to
411         // this effect) by marking very little as `dllimport` and praying the
412         // linker will take care of everything. Fixing this problem will likely
413         // require adding a few attributes to Rust itself (feature gated at the
414         // start) and then strongly recommending static linkage on MSVC!
415         let use_dll_storage_attrs = tcx.sess.target.target.options.is_like_msvc;
416
417         SharedCrateContext {
418             metadata_llmod: metadata_llmod,
419             metadata_llcx: metadata_llcx,
420             export_map: export_map,
421             reachable: reachable,
422             link_meta: link_meta,
423             symbol_hasher: RefCell::new(symbol_hasher),
424             tcx: tcx,
425             mir_map: mir_map,
426             mir_cache: RefCell::new(DefIdMap()),
427             stats: Stats {
428                 n_glues_created: Cell::new(0),
429                 n_null_glues: Cell::new(0),
430                 n_real_glues: Cell::new(0),
431                 n_fallback_instantiations: Cell::new(0),
432                 n_fns: Cell::new(0),
433                 n_monos: Cell::new(0),
434                 n_inlines: Cell::new(0),
435                 n_closures: Cell::new(0),
436                 n_llvm_insns: Cell::new(0),
437                 llvm_insns: RefCell::new(FnvHashMap()),
438                 fn_stats: RefCell::new(Vec::new()),
439             },
440             check_overflow: check_overflow,
441             check_drop_flag_for_sanity: check_drop_flag_for_sanity,
442             use_dll_storage_attrs: use_dll_storage_attrs,
443             translation_items: RefCell::new(FnvHashSet()),
444             trait_cache: RefCell::new(DepTrackingMap::new(tcx.dep_graph.clone())),
445         }
446     }
447
448     pub fn metadata_llmod(&self) -> ModuleRef {
449         self.metadata_llmod
450     }
451
452     pub fn metadata_llcx(&self) -> ContextRef {
453         self.metadata_llcx
454     }
455
456     pub fn export_map<'a>(&'a self) -> &'a ExportMap {
457         &self.export_map
458     }
459
460     pub fn reachable<'a>(&'a self) -> &'a NodeSet {
461         &self.reachable
462     }
463
464     pub fn trait_cache(&self) -> &RefCell<DepTrackingMap<TraitSelectionCache<'tcx>>> {
465         &self.trait_cache
466     }
467
468     pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
469         &self.link_meta
470     }
471
472     pub fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
473         self.tcx
474     }
475
476     pub fn sess<'a>(&'a self) -> &'a Session {
477         &self.tcx.sess
478     }
479
480     pub fn stats<'a>(&'a self) -> &'a Stats {
481         &self.stats
482     }
483
484     pub fn use_dll_storage_attrs(&self) -> bool {
485         self.use_dll_storage_attrs
486     }
487
488     pub fn get_mir(&self, def_id: DefId) -> Option<CachedMir<'b, 'tcx>> {
489         if def_id.is_local() {
490             let node_id = self.tcx.map.as_local_node_id(def_id).unwrap();
491             self.mir_map.map.get(&node_id).map(CachedMir::Ref)
492         } else {
493             if let Some(mir) = self.mir_cache.borrow().get(&def_id).cloned() {
494                 return Some(CachedMir::Owned(mir));
495             }
496
497             let mir = self.sess().cstore.maybe_get_item_mir(self.tcx, def_id);
498             let cached = mir.map(Rc::new);
499             if let Some(ref mir) = cached {
500                 self.mir_cache.borrow_mut().insert(def_id, mir.clone());
501             }
502             cached.map(CachedMir::Owned)
503         }
504     }
505
506     pub fn translation_items(&self) -> &RefCell<FnvHashSet<TransItem<'tcx>>> {
507         &self.translation_items
508     }
509
510     /// Given the def-id of some item that has no type parameters, make
511     /// a suitable "empty substs" for it.
512     pub fn empty_substs_for_def_id(&self, item_def_id: DefId) -> &'tcx Substs<'tcx> {
513         let scheme = self.tcx().lookup_item_type(item_def_id);
514         self.empty_substs_for_scheme(&scheme)
515     }
516
517     pub fn empty_substs_for_scheme(&self, scheme: &ty::TypeScheme<'tcx>)
518                                    -> &'tcx Substs<'tcx> {
519         assert!(scheme.generics.types.is_empty());
520         self.tcx().mk_substs(
521             Substs::new(VecPerParamSpace::empty(),
522                         scheme.generics.regions.map(|_| ty::ReErased)))
523     }
524
525     pub fn symbol_hasher(&self) -> &RefCell<Sha256> {
526         &self.symbol_hasher
527     }
528
529     pub fn mir_map(&self) -> &MirMap<'tcx> {
530         &self.mir_map
531     }
532
533     pub fn metadata_symbol_name(&self) -> String {
534         format!("rust_metadata_{}_{}",
535                 self.link_meta().crate_name,
536                 self.link_meta().crate_hash)
537     }
538 }
539
540 impl<'tcx> LocalCrateContext<'tcx> {
541     fn new<'a>(shared: &SharedCrateContext<'a, 'tcx>,
542                codegen_unit: CodegenUnit<'tcx>,
543                previous_work_product: Option<WorkProduct>,
544                symbol_map: Rc<SymbolMap<'tcx>>)
545            -> LocalCrateContext<'tcx> {
546         unsafe {
547             // Append ".rs" to LLVM module identifier.
548             //
549             // LLVM code generator emits a ".file filename" directive
550             // for ELF backends. Value of the "filename" is set as the
551             // LLVM module identifier.  Due to a LLVM MC bug[1], LLVM
552             // crashes if the module identifier is same as other symbols
553             // such as a function name in the module.
554             // 1. http://llvm.org/bugs/show_bug.cgi?id=11479
555             let llmod_id = format!("{}.rs", codegen_unit.name());
556
557             let (llcx, llmod) = create_context_and_module(&shared.tcx.sess,
558                                                           &llmod_id[..]);
559
560             let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo {
561                 Some(debuginfo::CrateDebugContext::new(llmod))
562             } else {
563                 None
564             };
565
566             let local_ccx = LocalCrateContext {
567                 llmod: llmod,
568                 llcx: llcx,
569                 previous_work_product: previous_work_product,
570                 codegen_unit: codegen_unit,
571                 tn: TypeNames::new(),
572                 needs_unwind_cleanup_cache: RefCell::new(FnvHashMap()),
573                 fn_pointer_shims: RefCell::new(FnvHashMap()),
574                 drop_glues: RefCell::new(FnvHashMap()),
575                 external: RefCell::new(DefIdMap()),
576                 external_srcs: RefCell::new(NodeMap()),
577                 instances: RefCell::new(FnvHashMap()),
578                 monomorphizing: RefCell::new(DefIdMap()),
579                 vtables: RefCell::new(FnvHashMap()),
580                 const_cstr_cache: RefCell::new(FnvHashMap()),
581                 const_unsized: RefCell::new(FnvHashMap()),
582                 const_globals: RefCell::new(FnvHashMap()),
583                 const_values: RefCell::new(FnvHashMap()),
584                 extern_const_values: RefCell::new(DefIdMap()),
585                 statics: RefCell::new(FnvHashMap()),
586                 impl_method_cache: RefCell::new(FnvHashMap()),
587                 closure_bare_wrapper_cache: RefCell::new(FnvHashMap()),
588                 statics_to_rauw: RefCell::new(Vec::new()),
589                 lltypes: RefCell::new(FnvHashMap()),
590                 llsizingtypes: RefCell::new(FnvHashMap()),
591                 adt_reprs: RefCell::new(FnvHashMap()),
592                 type_hashcodes: RefCell::new(FnvHashMap()),
593                 int_type: Type::from_ref(ptr::null_mut()),
594                 opaque_vec_type: Type::from_ref(ptr::null_mut()),
595                 builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)),
596                 closure_vals: RefCell::new(FnvHashMap()),
597                 dbg_cx: dbg_cx,
598                 eh_personality: Cell::new(None),
599                 eh_unwind_resume: Cell::new(None),
600                 rust_try_fn: Cell::new(None),
601                 intrinsics: RefCell::new(FnvHashMap()),
602                 n_llvm_insns: Cell::new(0),
603                 type_of_depth: Cell::new(0),
604                 symbol_map: symbol_map,
605             };
606
607             let (int_type, opaque_vec_type, str_slice_ty, mut local_ccx) = {
608                 // Do a little dance to create a dummy CrateContext, so we can
609                 // create some things in the LLVM module of this codegen unit
610                 let mut local_ccxs = vec![local_ccx];
611                 let (int_type, opaque_vec_type, str_slice_ty) = {
612                     let dummy_ccx = LocalCrateContext::dummy_ccx(shared,
613                                                                  local_ccxs.as_mut_slice());
614                     let mut str_slice_ty = Type::named_struct(&dummy_ccx, "str_slice");
615                     str_slice_ty.set_struct_body(&[Type::i8p(&dummy_ccx),
616                                                    Type::int(&dummy_ccx)],
617                                                  false);
618                     (Type::int(&dummy_ccx), Type::opaque_vec(&dummy_ccx), str_slice_ty)
619                 };
620                 (int_type, opaque_vec_type, str_slice_ty, local_ccxs.pop().unwrap())
621             };
622
623             local_ccx.int_type = int_type;
624             local_ccx.opaque_vec_type = opaque_vec_type;
625             local_ccx.tn.associate_type("str_slice", &str_slice_ty);
626
627             if shared.tcx.sess.count_llvm_insns() {
628                 base::init_insn_ctxt()
629             }
630
631             local_ccx
632         }
633     }
634
635     /// Create a dummy `CrateContext` from `self` and  the provided
636     /// `SharedCrateContext`.  This is somewhat dangerous because `self` may
637     /// not be fully initialized.
638     ///
639     /// This is used in the `LocalCrateContext` constructor to allow calling
640     /// functions that expect a complete `CrateContext`, even before the local
641     /// portion is fully initialized and attached to the `SharedCrateContext`.
642     fn dummy_ccx<'a>(shared: &'a SharedCrateContext<'a, 'tcx>,
643                      local_ccxs: &'a [LocalCrateContext<'tcx>])
644                      -> CrateContext<'a, 'tcx> {
645         assert!(local_ccxs.len() == 1);
646         CrateContext {
647             shared: shared,
648             index: 0,
649             local_ccxs: local_ccxs
650         }
651     }
652 }
653
654 impl<'b, 'tcx> CrateContext<'b, 'tcx> {
655     pub fn shared(&self) -> &'b SharedCrateContext<'b, 'tcx> {
656         self.shared
657     }
658
659     pub fn local(&self) -> &'b LocalCrateContext<'tcx> {
660         &self.local_ccxs[self.index]
661     }
662
663     /// Get a (possibly) different `CrateContext` from the same
664     /// `SharedCrateContext`.
665     pub fn rotate(&'b self) -> CrateContext<'b, 'tcx> {
666         let (_, index) =
667             self.local_ccxs
668                 .iter()
669                 .zip(0..self.local_ccxs.len())
670                 .min_by_key(|&(local_ccx, _idx)| local_ccx.n_llvm_insns.get())
671                 .unwrap();
672         CrateContext {
673             shared: self.shared,
674             index: index,
675             local_ccxs: &self.local_ccxs[..],
676         }
677     }
678
679     /// Either iterate over only `self`, or iterate over all `CrateContext`s in
680     /// the `SharedCrateContext`.  The iterator produces `(ccx, is_origin)`
681     /// pairs, where `is_origin` is `true` if `ccx` is `self` and `false`
682     /// otherwise.  This method is useful for avoiding code duplication in
683     /// cases where it may or may not be necessary to translate code into every
684     /// context.
685     pub fn maybe_iter(&self, iter_all: bool) -> CrateContextMaybeIterator<'b, 'tcx> {
686         CrateContextMaybeIterator {
687             shared: self.shared,
688             index: if iter_all { 0 } else { self.index },
689             single: !iter_all,
690             origin: self.index,
691             local_ccxs: self.local_ccxs,
692         }
693     }
694
695     pub fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
696         self.shared.tcx
697     }
698
699     pub fn sess<'a>(&'a self) -> &'a Session {
700         &self.shared.tcx.sess
701     }
702
703     pub fn builder<'a>(&'a self) -> Builder<'a, 'tcx> {
704         Builder::new(self)
705     }
706
707     pub fn raw_builder<'a>(&'a self) -> BuilderRef {
708         self.local().builder.b
709     }
710
711     pub fn get_intrinsic(&self, key: &str) -> ValueRef {
712         if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
713             return v;
714         }
715         match declare_intrinsic(self, key) {
716             Some(v) => return v,
717             None => bug!("unknown intrinsic '{}'", key)
718         }
719     }
720
721     pub fn llmod(&self) -> ModuleRef {
722         self.local().llmod
723     }
724
725     pub fn llcx(&self) -> ContextRef {
726         self.local().llcx
727     }
728
729     pub fn previous_work_product(&self) -> Option<&WorkProduct> {
730         self.local().previous_work_product.as_ref()
731     }
732
733     pub fn codegen_unit(&self) -> &CodegenUnit<'tcx> {
734         &self.local().codegen_unit
735     }
736
737     pub fn td(&self) -> llvm::TargetDataRef {
738         unsafe { llvm::LLVMRustGetModuleDataLayout(self.llmod()) }
739     }
740
741     pub fn tn<'a>(&'a self) -> &'a TypeNames {
742         &self.local().tn
743     }
744
745     pub fn export_map<'a>(&'a self) -> &'a ExportMap {
746         &self.shared.export_map
747     }
748
749     pub fn reachable<'a>(&'a self) -> &'a NodeSet {
750         &self.shared.reachable
751     }
752
753     pub fn link_meta<'a>(&'a self) -> &'a LinkMeta {
754         &self.shared.link_meta
755     }
756
757     pub fn needs_unwind_cleanup_cache(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, bool>> {
758         &self.local().needs_unwind_cleanup_cache
759     }
760
761     pub fn fn_pointer_shims(&self) -> &RefCell<FnvHashMap<Ty<'tcx>, ValueRef>> {
762         &self.local().fn_pointer_shims
763     }
764
765     pub fn drop_glues<'a>(&'a self)
766                           -> &'a RefCell<FnvHashMap<DropGlueKind<'tcx>, (ValueRef, FnType)>> {
767         &self.local().drop_glues
768     }
769
770     pub fn external<'a>(&'a self) -> &'a RefCell<DefIdMap<Option<ast::NodeId>>> {
771         &self.local().external
772     }
773
774     pub fn external_srcs<'a>(&'a self) -> &'a RefCell<NodeMap<DefId>> {
775         &self.local().external_srcs
776     }
777
778     pub fn instances<'a>(&'a self) -> &'a RefCell<FnvHashMap<Instance<'tcx>, ValueRef>> {
779         &self.local().instances
780     }
781
782     pub fn monomorphizing<'a>(&'a self) -> &'a RefCell<DefIdMap<usize>> {
783         &self.local().monomorphizing
784     }
785
786     pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>> {
787         &self.local().vtables
788     }
789
790     pub fn const_cstr_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<InternedString, ValueRef>> {
791         &self.local().const_cstr_cache
792     }
793
794     pub fn const_unsized<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
795         &self.local().const_unsized
796     }
797
798     pub fn const_globals<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
799         &self.local().const_globals
800     }
801
802     pub fn const_values<'a>(&'a self) -> &'a RefCell<FnvHashMap<(ast::NodeId, &'tcx Substs<'tcx>),
803                                                                 ValueRef>> {
804         &self.local().const_values
805     }
806
807     pub fn extern_const_values<'a>(&'a self) -> &'a RefCell<DefIdMap<ValueRef>> {
808         &self.local().extern_const_values
809     }
810
811     pub fn statics<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, DefId>> {
812         &self.local().statics
813     }
814
815     pub fn impl_method_cache<'a>(&'a self)
816             -> &'a RefCell<FnvHashMap<(DefId, ast::Name), DefId>> {
817         &self.local().impl_method_cache
818     }
819
820     pub fn closure_bare_wrapper_cache<'a>(&'a self) -> &'a RefCell<FnvHashMap<ValueRef, ValueRef>> {
821         &self.local().closure_bare_wrapper_cache
822     }
823
824     pub fn statics_to_rauw<'a>(&'a self) -> &'a RefCell<Vec<(ValueRef, ValueRef)>> {
825         &self.local().statics_to_rauw
826     }
827
828     pub fn lltypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
829         &self.local().lltypes
830     }
831
832     pub fn llsizingtypes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Type>> {
833         &self.local().llsizingtypes
834     }
835
836     pub fn adt_reprs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>> {
837         &self.local().adt_reprs
838     }
839
840     pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
841         &self.shared.symbol_hasher
842     }
843
844     pub fn type_hashcodes<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, String>> {
845         &self.local().type_hashcodes
846     }
847
848     pub fn stats<'a>(&'a self) -> &'a Stats {
849         &self.shared.stats
850     }
851
852     pub fn int_type(&self) -> Type {
853         self.local().int_type
854     }
855
856     pub fn opaque_vec_type(&self) -> Type {
857         self.local().opaque_vec_type
858     }
859
860     pub fn closure_vals<'a>(&'a self) -> &'a RefCell<FnvHashMap<Instance<'tcx>, ValueRef>> {
861         &self.local().closure_vals
862     }
863
864     pub fn dbg_cx<'a>(&'a self) -> &'a Option<debuginfo::CrateDebugContext<'tcx>> {
865         &self.local().dbg_cx
866     }
867
868     pub fn eh_personality<'a>(&'a self) -> &'a Cell<Option<ValueRef>> {
869         &self.local().eh_personality
870     }
871
872     pub fn eh_unwind_resume<'a>(&'a self) -> &'a Cell<Option<ValueRef>> {
873         &self.local().eh_unwind_resume
874     }
875
876     pub fn rust_try_fn<'a>(&'a self) -> &'a Cell<Option<ValueRef>> {
877         &self.local().rust_try_fn
878     }
879
880     fn intrinsics<'a>(&'a self) -> &'a RefCell<FnvHashMap<&'static str, ValueRef>> {
881         &self.local().intrinsics
882     }
883
884     pub fn count_llvm_insn(&self) {
885         self.local().n_llvm_insns.set(self.local().n_llvm_insns.get() + 1);
886     }
887
888     pub fn obj_size_bound(&self) -> u64 {
889         self.tcx().data_layout.obj_size_bound()
890     }
891
892     pub fn report_overbig_object(&self, obj: Ty<'tcx>) -> ! {
893         self.sess().fatal(
894             &format!("the type `{:?}` is too big for the current architecture",
895                     obj))
896     }
897
898     pub fn enter_type_of(&self, ty: Ty<'tcx>) -> TypeOfDepthLock<'b, 'tcx> {
899         let current_depth = self.local().type_of_depth.get();
900         debug!("enter_type_of({:?}) at depth {:?}", ty, current_depth);
901         if current_depth > self.sess().recursion_limit.get() {
902             self.sess().fatal(
903                 &format!("overflow representing the type `{}`", ty))
904         }
905         self.local().type_of_depth.set(current_depth + 1);
906         TypeOfDepthLock(self.local())
907     }
908
909     pub fn check_overflow(&self) -> bool {
910         self.shared.check_overflow
911     }
912
913     pub fn check_drop_flag_for_sanity(&self) -> bool {
914         // This controls whether we emit a conditional llvm.debugtrap
915         // guarded on whether the dropflag is one of its (two) valid
916         // values.
917         self.shared.check_drop_flag_for_sanity
918     }
919
920     pub fn use_dll_storage_attrs(&self) -> bool {
921         self.shared.use_dll_storage_attrs()
922     }
923
924     pub fn get_mir(&self, def_id: DefId) -> Option<CachedMir<'b, 'tcx>> {
925         self.shared.get_mir(def_id)
926     }
927
928     pub fn symbol_map(&self) -> &SymbolMap<'tcx> {
929         &*self.local().symbol_map
930     }
931
932     pub fn translation_items(&self) -> &RefCell<FnvHashSet<TransItem<'tcx>>> {
933         &self.shared.translation_items
934     }
935
936     /// Given the def-id of some item that has no type parameters, make
937     /// a suitable "empty substs" for it.
938     pub fn empty_substs_for_def_id(&self, item_def_id: DefId) -> &'tcx Substs<'tcx> {
939         self.shared().empty_substs_for_def_id(item_def_id)
940     }
941
942     pub fn empty_substs_for_scheme(&self, scheme: &ty::TypeScheme<'tcx>)
943                                    -> &'tcx Substs<'tcx> {
944         self.shared().empty_substs_for_scheme(scheme)
945     }
946 }
947
948 pub struct TypeOfDepthLock<'a, 'tcx: 'a>(&'a LocalCrateContext<'tcx>);
949
950 impl<'a, 'tcx> Drop for TypeOfDepthLock<'a, 'tcx> {
951     fn drop(&mut self) {
952         self.0.type_of_depth.set(self.0.type_of_depth.get() - 1);
953     }
954 }
955
956 /// Declare any llvm intrinsics that you might need
957 fn declare_intrinsic(ccx: &CrateContext, key: &str) -> Option<ValueRef> {
958     macro_rules! ifn {
959         ($name:expr, fn() -> $ret:expr) => (
960             if key == $name {
961                 let f = declare::declare_cfn(ccx, $name, Type::func(&[], &$ret));
962                 llvm::SetUnnamedAddr(f, false);
963                 ccx.intrinsics().borrow_mut().insert($name, f.clone());
964                 return Some(f);
965             }
966         );
967         ($name:expr, fn(...) -> $ret:expr) => (
968             if key == $name {
969                 let f = declare::declare_cfn(ccx, $name, Type::variadic_func(&[], &$ret));
970                 llvm::SetUnnamedAddr(f, false);
971                 ccx.intrinsics().borrow_mut().insert($name, f.clone());
972                 return Some(f);
973             }
974         );
975         ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
976             if key == $name {
977                 let f = declare::declare_cfn(ccx, $name, Type::func(&[$($arg),*], &$ret));
978                 llvm::SetUnnamedAddr(f, false);
979                 ccx.intrinsics().borrow_mut().insert($name, f.clone());
980                 return Some(f);
981             }
982         );
983     }
984     macro_rules! mk_struct {
985         ($($field_ty:expr),*) => (Type::struct_(ccx, &[$($field_ty),*], false))
986     }
987
988     let i8p = Type::i8p(ccx);
989     let void = Type::void(ccx);
990     let i1 = Type::i1(ccx);
991     let t_i8 = Type::i8(ccx);
992     let t_i16 = Type::i16(ccx);
993     let t_i32 = Type::i32(ccx);
994     let t_i64 = Type::i64(ccx);
995     let t_f32 = Type::f32(ccx);
996     let t_f64 = Type::f64(ccx);
997
998     ifn!("llvm.memcpy.p0i8.p0i8.i16", fn(i8p, i8p, t_i16, t_i32, i1) -> void);
999     ifn!("llvm.memcpy.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
1000     ifn!("llvm.memcpy.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
1001     ifn!("llvm.memmove.p0i8.p0i8.i16", fn(i8p, i8p, t_i16, t_i32, i1) -> void);
1002     ifn!("llvm.memmove.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
1003     ifn!("llvm.memmove.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
1004     ifn!("llvm.memset.p0i8.i16", fn(i8p, t_i8, t_i16, t_i32, i1) -> void);
1005     ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
1006     ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
1007
1008     ifn!("llvm.trap", fn() -> void);
1009     ifn!("llvm.debugtrap", fn() -> void);
1010     ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
1011
1012     ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
1013     ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
1014     ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
1015     ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
1016
1017     ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
1018     ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
1019     ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
1020     ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
1021     ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
1022     ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
1023     ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
1024     ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
1025     ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
1026     ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
1027     ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
1028     ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
1029     ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
1030     ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
1031     ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
1032     ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
1033
1034     ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
1035     ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
1036
1037     ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
1038     ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
1039
1040     ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
1041     ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
1042     ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
1043     ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
1044     ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
1045     ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
1046
1047     ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
1048     ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
1049     ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
1050     ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
1051
1052     ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
1053     ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
1054     ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
1055     ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
1056
1057     ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
1058     ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
1059     ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
1060     ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
1061
1062     ifn!("llvm.ctlz.i8", fn(t_i8 , i1) -> t_i8);
1063     ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
1064     ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
1065     ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
1066
1067     ifn!("llvm.cttz.i8", fn(t_i8 , i1) -> t_i8);
1068     ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
1069     ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
1070     ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
1071
1072     ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
1073     ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
1074     ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
1075
1076     ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
1077     ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
1078     ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
1079     ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
1080
1081     ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
1082     ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
1083     ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
1084     ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
1085
1086     ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
1087     ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
1088     ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
1089     ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
1090
1091     ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
1092     ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
1093     ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
1094     ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
1095
1096     ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
1097     ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
1098     ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
1099     ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
1100
1101     ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
1102     ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
1103     ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
1104     ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct!{t_i64, i1});
1105
1106     ifn!("llvm.lifetime.start", fn(t_i64,i8p) -> void);
1107     ifn!("llvm.lifetime.end", fn(t_i64, i8p) -> void);
1108
1109     ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
1110     ifn!("llvm.eh.typeid.for", fn(i8p) -> t_i32);
1111     ifn!("llvm.localescape", fn(...) -> void);
1112     ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
1113     ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
1114
1115     ifn!("llvm.assume", fn(i1) -> void);
1116
1117     if ccx.sess().opts.debuginfo != NoDebugInfo {
1118         ifn!("llvm.dbg.declare", fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
1119         ifn!("llvm.dbg.value", fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);
1120     }
1121     return None;
1122 }