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