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