]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/builder.rs
Rollup merge of #67363 - alexcrichton:wasm-import-modules, r=eddyb
[rust.git] / src / librustc_codegen_llvm / builder.rs
1 use crate::llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope};
2 use crate::llvm::{self, False, BasicBlock};
3 use crate::common::Funclet;
4 use crate::context::CodegenCx;
5 use crate::type_::Type;
6 use crate::type_of::LayoutLlvmExt;
7 use crate::value::Value;
8 use rustc_codegen_ssa::common::{IntPredicate, TypeKind, RealPredicate};
9 use rustc_codegen_ssa::MemFlags;
10 use libc::{c_uint, c_char};
11 use rustc::ty::{self, Ty, TyCtxt};
12 use rustc::ty::layout::{self, Align, Size, TyLayout};
13 use rustc::hir::def_id::DefId;
14 use rustc::session::config;
15 use rustc_data_structures::small_c_str::SmallCStr;
16 use rustc_codegen_ssa::traits::*;
17 use rustc_codegen_ssa::base::to_immediate;
18 use rustc_codegen_ssa::mir::operand::{OperandValue, OperandRef};
19 use rustc_codegen_ssa::mir::place::PlaceRef;
20 use rustc_target::spec::{HasTargetSpec, Target};
21 use std::borrow::Cow;
22 use std::ffi::CStr;
23 use std::ops::{Deref, Range};
24 use std::ptr;
25 use std::iter::TrustedLen;
26 use rustc_data_structures::const_cstr;
27 use log::debug;
28
29 // All Builders must have an llfn associated with them
30 #[must_use]
31 pub struct Builder<'a, 'll, 'tcx> {
32     pub llbuilder: &'ll mut llvm::Builder<'ll>,
33     pub cx: &'a CodegenCx<'ll, 'tcx>,
34 }
35
36 impl Drop for Builder<'a, 'll, 'tcx> {
37     fn drop(&mut self) {
38         unsafe {
39             llvm::LLVMDisposeBuilder(&mut *(self.llbuilder as *mut _));
40         }
41     }
42 }
43
44 // FIXME(eddyb) use a checked constructor when they become `const fn`.
45 const EMPTY_C_STR: &CStr = unsafe {
46     CStr::from_bytes_with_nul_unchecked(b"\0")
47 };
48
49 /// Empty string, to be used where LLVM expects an instruction name, indicating
50 /// that the instruction is to be left unnamed (i.e. numbered, in textual IR).
51 // FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer.
52 const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
53
54 impl BackendTypes for Builder<'_, 'll, 'tcx> {
55     type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
56     type Function = <CodegenCx<'ll, 'tcx> as BackendTypes>::Function;
57     type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
58     type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
59     type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;
60
61     type DIScope = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIScope;
62 }
63
64 impl ty::layout::HasDataLayout for Builder<'_, '_, '_> {
65     fn data_layout(&self) -> &ty::layout::TargetDataLayout {
66         self.cx.data_layout()
67     }
68 }
69
70 impl ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
71     fn tcx(&self) -> TyCtxt<'tcx> {
72         self.cx.tcx
73     }
74 }
75
76 impl ty::layout::HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
77     fn param_env(&self) -> ty::ParamEnv<'tcx> {
78         self.cx.param_env()
79     }
80 }
81
82 impl HasTargetSpec for Builder<'_, '_, 'tcx> {
83     fn target_spec(&self) -> &Target {
84         &self.cx.target_spec()
85     }
86 }
87
88 impl ty::layout::LayoutOf for Builder<'_, '_, 'tcx> {
89     type Ty = Ty<'tcx>;
90     type TyLayout = TyLayout<'tcx>;
91
92     fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
93         self.cx.layout_of(ty)
94     }
95 }
96
97 impl Deref for Builder<'_, 'll, 'tcx> {
98     type Target = CodegenCx<'ll, 'tcx>;
99
100     fn deref(&self) -> &Self::Target {
101         self.cx
102     }
103 }
104
105 impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
106     type CodegenCx = CodegenCx<'ll, 'tcx>;
107 }
108
109 macro_rules! builder_methods_for_value_instructions {
110     ($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
111         $(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
112             unsafe {
113                 llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED)
114             }
115         })+
116     }
117 }
118
119 impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
120     fn new_block<'b>(
121         cx: &'a CodegenCx<'ll, 'tcx>,
122         llfn: &'ll Value,
123         name: &'b str
124     ) -> Self {
125         let mut bx = Builder::with_cx(cx);
126         let llbb = unsafe {
127             let name = SmallCStr::new(name);
128             llvm::LLVMAppendBasicBlockInContext(
129                 cx.llcx,
130                 llfn,
131                 name.as_ptr()
132             )
133         };
134         bx.position_at_end(llbb);
135         bx
136     }
137
138     fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
139         // Create a fresh builder from the crate context.
140         let llbuilder = unsafe {
141             llvm::LLVMCreateBuilderInContext(cx.llcx)
142         };
143         Builder {
144             llbuilder,
145             cx,
146         }
147     }
148
149     fn build_sibling_block(&self, name: &str) -> Self {
150         Builder::new_block(self.cx, self.llfn(), name)
151     }
152
153     fn llbb(&self) -> &'ll BasicBlock {
154         unsafe {
155             llvm::LLVMGetInsertBlock(self.llbuilder)
156         }
157     }
158
159     fn position_at_end(&mut self, llbb: &'ll BasicBlock) {
160         unsafe {
161             llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
162         }
163     }
164
165     fn ret_void(&mut self) {
166         unsafe {
167             llvm::LLVMBuildRetVoid(self.llbuilder);
168         }
169     }
170
171     fn ret(&mut self, v: &'ll Value) {
172         unsafe {
173             llvm::LLVMBuildRet(self.llbuilder, v);
174         }
175     }
176
177     fn br(&mut self, dest: &'ll BasicBlock) {
178         unsafe {
179             llvm::LLVMBuildBr(self.llbuilder, dest);
180         }
181     }
182
183     fn cond_br(
184         &mut self,
185         cond: &'ll Value,
186         then_llbb: &'ll BasicBlock,
187         else_llbb: &'ll BasicBlock,
188     ) {
189         unsafe {
190             llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
191         }
192     }
193
194     fn switch(
195         &mut self,
196         v: &'ll Value,
197         else_llbb: &'ll BasicBlock,
198         cases: impl ExactSizeIterator<Item = (u128, &'ll BasicBlock)> + TrustedLen,
199     ) {
200         let switch = unsafe {
201             llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, cases.len() as c_uint)
202         };
203         for (on_val, dest) in cases {
204             let on_val = self.const_uint_big(self.val_ty(v), on_val);
205             unsafe {
206                 llvm::LLVMAddCase(switch, on_val, dest)
207             }
208         }
209     }
210
211     fn invoke(
212         &mut self,
213         llfn: &'ll Value,
214         args: &[&'ll Value],
215         then: &'ll BasicBlock,
216         catch: &'ll BasicBlock,
217         funclet: Option<&Funclet<'ll>>,
218     ) -> &'ll Value {
219
220         debug!("invoke {:?} with args ({:?})",
221                llfn,
222                args);
223
224         let args = self.check_call("invoke", llfn, args);
225         let bundle = funclet.map(|funclet| funclet.bundle());
226         let bundle = bundle.as_ref().map(|b| &*b.raw);
227
228         unsafe {
229             llvm::LLVMRustBuildInvoke(self.llbuilder,
230                                       llfn,
231                                       args.as_ptr(),
232                                       args.len() as c_uint,
233                                       then,
234                                       catch,
235                                       bundle,
236                                       UNNAMED)
237         }
238     }
239
240     fn unreachable(&mut self) {
241         unsafe {
242             llvm::LLVMBuildUnreachable(self.llbuilder);
243         }
244     }
245
246     builder_methods_for_value_instructions! {
247         add(a, b) => LLVMBuildAdd,
248         fadd(a, b) => LLVMBuildFAdd,
249         sub(a, b) => LLVMBuildSub,
250         fsub(a, b) => LLVMBuildFSub,
251         mul(a, b) => LLVMBuildMul,
252         fmul(a, b) => LLVMBuildFMul,
253         udiv(a, b) => LLVMBuildUDiv,
254         exactudiv(a, b) => LLVMBuildExactUDiv,
255         sdiv(a, b) => LLVMBuildSDiv,
256         exactsdiv(a, b) => LLVMBuildExactSDiv,
257         fdiv(a, b) => LLVMBuildFDiv,
258         urem(a, b) => LLVMBuildURem,
259         srem(a, b) => LLVMBuildSRem,
260         frem(a, b) => LLVMBuildFRem,
261         shl(a, b) => LLVMBuildShl,
262         lshr(a, b) => LLVMBuildLShr,
263         ashr(a, b) => LLVMBuildAShr,
264         and(a, b) => LLVMBuildAnd,
265         or(a, b) => LLVMBuildOr,
266         xor(a, b) => LLVMBuildXor,
267         neg(x) => LLVMBuildNeg,
268         fneg(x) => LLVMBuildFNeg,
269         not(x) => LLVMBuildNot,
270         unchecked_sadd(x, y) => LLVMBuildNSWAdd,
271         unchecked_uadd(x, y) => LLVMBuildNUWAdd,
272         unchecked_ssub(x, y) => LLVMBuildNSWSub,
273         unchecked_usub(x, y) => LLVMBuildNUWSub,
274         unchecked_smul(x, y) => LLVMBuildNSWMul,
275         unchecked_umul(x, y) => LLVMBuildNUWMul,
276     }
277
278     fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
279         unsafe {
280             let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
281             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
282             instr
283         }
284     }
285
286     fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
287         unsafe {
288             let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
289             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
290             instr
291         }
292     }
293
294     fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
295         unsafe {
296             let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
297             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
298             instr
299         }
300     }
301
302     fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
303         unsafe {
304             let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
305             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
306             instr
307         }
308     }
309
310     fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
311         unsafe {
312             let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
313             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
314             instr
315         }
316     }
317
318     fn checked_binop(
319         &mut self,
320         oop: OverflowOp,
321         ty: Ty<'_>,
322         lhs: Self::Value,
323         rhs: Self::Value,
324     ) -> (Self::Value, Self::Value) {
325         use syntax::ast::IntTy::*;
326         use syntax::ast::UintTy::*;
327         use rustc::ty::{Int, Uint};
328
329         let new_kind = match ty.kind {
330             Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.ptr_width)),
331             Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.ptr_width)),
332             ref t @ Uint(_) | ref t @ Int(_) => t.clone(),
333             _ => panic!("tried to get overflow intrinsic for op applied to non-int type")
334         };
335
336         let name = match oop {
337             OverflowOp::Add => match new_kind {
338                 Int(I8) => "llvm.sadd.with.overflow.i8",
339                 Int(I16) => "llvm.sadd.with.overflow.i16",
340                 Int(I32) => "llvm.sadd.with.overflow.i32",
341                 Int(I64) => "llvm.sadd.with.overflow.i64",
342                 Int(I128) => "llvm.sadd.with.overflow.i128",
343
344                 Uint(U8) => "llvm.uadd.with.overflow.i8",
345                 Uint(U16) => "llvm.uadd.with.overflow.i16",
346                 Uint(U32) => "llvm.uadd.with.overflow.i32",
347                 Uint(U64) => "llvm.uadd.with.overflow.i64",
348                 Uint(U128) => "llvm.uadd.with.overflow.i128",
349
350                 _ => unreachable!(),
351             },
352             OverflowOp::Sub => match new_kind {
353                 Int(I8) => "llvm.ssub.with.overflow.i8",
354                 Int(I16) => "llvm.ssub.with.overflow.i16",
355                 Int(I32) => "llvm.ssub.with.overflow.i32",
356                 Int(I64) => "llvm.ssub.with.overflow.i64",
357                 Int(I128) => "llvm.ssub.with.overflow.i128",
358
359                 Uint(U8) => "llvm.usub.with.overflow.i8",
360                 Uint(U16) => "llvm.usub.with.overflow.i16",
361                 Uint(U32) => "llvm.usub.with.overflow.i32",
362                 Uint(U64) => "llvm.usub.with.overflow.i64",
363                 Uint(U128) => "llvm.usub.with.overflow.i128",
364
365                 _ => unreachable!(),
366             },
367             OverflowOp::Mul => match new_kind {
368                 Int(I8) => "llvm.smul.with.overflow.i8",
369                 Int(I16) => "llvm.smul.with.overflow.i16",
370                 Int(I32) => "llvm.smul.with.overflow.i32",
371                 Int(I64) => "llvm.smul.with.overflow.i64",
372                 Int(I128) => "llvm.smul.with.overflow.i128",
373
374                 Uint(U8) => "llvm.umul.with.overflow.i8",
375                 Uint(U16) => "llvm.umul.with.overflow.i16",
376                 Uint(U32) => "llvm.umul.with.overflow.i32",
377                 Uint(U64) => "llvm.umul.with.overflow.i64",
378                 Uint(U128) => "llvm.umul.with.overflow.i128",
379
380                 _ => unreachable!(),
381             },
382         };
383
384         let intrinsic = self.get_intrinsic(&name);
385         let res = self.call(intrinsic, &[lhs, rhs], None);
386         (
387             self.extract_value(res, 0),
388             self.extract_value(res, 1),
389         )
390     }
391
392     fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
393         let mut bx = Builder::with_cx(self.cx);
394         bx.position_at_start(unsafe {
395             llvm::LLVMGetFirstBasicBlock(self.llfn())
396         });
397         bx.dynamic_alloca(ty, align)
398     }
399
400     fn dynamic_alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
401         unsafe {
402             let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
403             llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
404             alloca
405         }
406     }
407
408     fn array_alloca(&mut self,
409                         ty: &'ll Type,
410                         len: &'ll Value,
411                         align: Align) -> &'ll Value {
412         unsafe {
413             let alloca = llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED);
414             llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
415             alloca
416         }
417     }
418
419     fn load(&mut self, ptr: &'ll Value, align: Align) -> &'ll Value {
420         unsafe {
421             let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, UNNAMED);
422             llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
423             load
424         }
425     }
426
427     fn volatile_load(&mut self, ptr: &'ll Value) -> &'ll Value {
428         unsafe {
429             let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, UNNAMED);
430             llvm::LLVMSetVolatile(load, llvm::True);
431             load
432         }
433     }
434
435     fn atomic_load(
436         &mut self,
437         ptr: &'ll Value,
438         order: rustc_codegen_ssa::common::AtomicOrdering,
439         size: Size,
440     ) -> &'ll Value {
441         unsafe {
442             let load = llvm::LLVMRustBuildAtomicLoad(
443                 self.llbuilder,
444                 ptr,
445                 UNNAMED,
446                 AtomicOrdering::from_generic(order),
447             );
448             // LLVM requires the alignment of atomic loads to be at least the size of the type.
449             llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
450             load
451         }
452     }
453
454     fn load_operand(
455         &mut self,
456         place: PlaceRef<'tcx, &'ll Value>
457     ) -> OperandRef<'tcx, &'ll Value> {
458         debug!("PlaceRef::load: {:?}", place);
459
460         assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
461
462         if place.layout.is_zst() {
463             return OperandRef::new_zst(self, place.layout);
464         }
465
466         fn scalar_load_metadata<'a, 'll, 'tcx>(
467             bx: &mut Builder<'a, 'll, 'tcx>,
468             load: &'ll Value,
469             scalar: &layout::Scalar
470         ) {
471             let vr = scalar.valid_range.clone();
472             match scalar.value {
473                 layout::Int(..) => {
474                     let range = scalar.valid_range_exclusive(bx);
475                     if range.start != range.end {
476                         bx.range_metadata(load, range);
477                     }
478                 }
479                 layout::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
480                     bx.nonnull_metadata(load);
481                 }
482                 _ => {}
483             }
484         }
485
486         let val = if let Some(llextra) = place.llextra {
487             OperandValue::Ref(place.llval, Some(llextra), place.align)
488         } else if place.layout.is_llvm_immediate() {
489             let mut const_llval = None;
490             unsafe {
491                 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
492                     if llvm::LLVMIsGlobalConstant(global) == llvm::True {
493                         const_llval = llvm::LLVMGetInitializer(global);
494                     }
495                 }
496             }
497             let llval = const_llval.unwrap_or_else(|| {
498                 let load = self.load(place.llval, place.align);
499                 if let layout::Abi::Scalar(ref scalar) = place.layout.abi {
500                     scalar_load_metadata(self, load, scalar);
501                 }
502                 load
503             });
504             OperandValue::Immediate(to_immediate(self, llval, place.layout))
505         } else if let layout::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
506             let b_offset = a.value.size(self).align_to(b.value.align(self).abi);
507
508             let mut load = |i, scalar: &layout::Scalar, align| {
509                 let llptr = self.struct_gep(place.llval, i as u64);
510                 let load = self.load(llptr, align);
511                 scalar_load_metadata(self, load, scalar);
512                 if scalar.is_bool() {
513                     self.trunc(load, self.type_i1())
514                 } else {
515                     load
516                 }
517             };
518
519             OperandValue::Pair(
520                 load(0, a, place.align),
521                 load(1, b, place.align.restrict_for_offset(b_offset)),
522             )
523         } else {
524             OperandValue::Ref(place.llval, None, place.align)
525         };
526
527         OperandRef { val, layout: place.layout }
528     }
529
530     fn write_operand_repeatedly(
531         mut self,
532         cg_elem: OperandRef<'tcx, &'ll Value>,
533         count: u64,
534         dest: PlaceRef<'tcx, &'ll Value>,
535     ) -> Self {
536         let zero = self.const_usize(0);
537         let count = self.const_usize(count);
538         let start = dest.project_index(&mut self, zero).llval;
539         let end = dest.project_index(&mut self, count).llval;
540
541         let mut header_bx = self.build_sibling_block("repeat_loop_header");
542         let mut body_bx = self.build_sibling_block("repeat_loop_body");
543         let next_bx = self.build_sibling_block("repeat_loop_next");
544
545         self.br(header_bx.llbb());
546         let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
547
548         let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
549         header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
550
551         let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
552         cg_elem.val.store(&mut body_bx,
553             PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
554
555         let next = body_bx.inbounds_gep(current, &[self.const_usize(1)]);
556         body_bx.br(header_bx.llbb());
557         header_bx.add_incoming_to_phi(current, next, body_bx.llbb());
558
559         next_bx
560     }
561
562     fn range_metadata(&mut self, load: &'ll Value, range: Range<u128>) {
563         if self.sess().target.target.arch == "amdgpu" {
564             // amdgpu/LLVM does something weird and thinks a i64 value is
565             // split into a v2i32, halving the bitwidth LLVM expects,
566             // tripping an assertion. So, for now, just disable this
567             // optimization.
568             return;
569         }
570
571         unsafe {
572             let llty = self.cx.val_ty(load);
573             let v = [
574                 self.cx.const_uint_big(llty, range.start),
575                 self.cx.const_uint_big(llty, range.end)
576             ];
577
578             llvm::LLVMSetMetadata(load, llvm::MD_range as c_uint,
579                                   llvm::LLVMMDNodeInContext(self.cx.llcx,
580                                                             v.as_ptr(),
581                                                             v.len() as c_uint));
582         }
583     }
584
585     fn nonnull_metadata(&mut self, load: &'ll Value) {
586         unsafe {
587             llvm::LLVMSetMetadata(load, llvm::MD_nonnull as c_uint,
588                                   llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
589         }
590     }
591
592     fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
593         self.store_with_flags(val, ptr, align, MemFlags::empty())
594     }
595
596     fn store_with_flags(
597         &mut self,
598         val: &'ll Value,
599         ptr: &'ll Value,
600         align: Align,
601         flags: MemFlags,
602     ) -> &'ll Value {
603         debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
604         let ptr = self.check_store(val, ptr);
605         unsafe {
606             let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
607             let align = if flags.contains(MemFlags::UNALIGNED) {
608                 1
609             } else {
610                 align.bytes() as c_uint
611             };
612             llvm::LLVMSetAlignment(store, align);
613             if flags.contains(MemFlags::VOLATILE) {
614                 llvm::LLVMSetVolatile(store, llvm::True);
615             }
616             if flags.contains(MemFlags::NONTEMPORAL) {
617                 // According to LLVM [1] building a nontemporal store must
618                 // *always* point to a metadata value of the integer 1.
619                 //
620                 // [1]: http://llvm.org/docs/LangRef.html#store-instruction
621                 let one = self.cx.const_i32(1);
622                 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
623                 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
624             }
625             store
626         }
627     }
628
629    fn atomic_store(&mut self, val: &'ll Value, ptr: &'ll Value,
630                    order: rustc_codegen_ssa::common::AtomicOrdering, size: Size) {
631         debug!("Store {:?} -> {:?}", val, ptr);
632         let ptr = self.check_store(val, ptr);
633         unsafe {
634             let store = llvm::LLVMRustBuildAtomicStore(
635                 self.llbuilder,
636                 val,
637                 ptr,
638                 AtomicOrdering::from_generic(order),
639             );
640             // LLVM requires the alignment of atomic stores to be at least the size of the type.
641             llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
642         }
643     }
644
645     fn gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
646         unsafe {
647             llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
648                                indices.len() as c_uint, UNNAMED)
649         }
650     }
651
652     fn inbounds_gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
653         unsafe {
654             llvm::LLVMBuildInBoundsGEP(
655                 self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, UNNAMED)
656         }
657     }
658
659     fn struct_gep(&mut self, ptr: &'ll Value, idx: u64) -> &'ll Value {
660         assert_eq!(idx as c_uint as u64, idx);
661         unsafe {
662             llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, UNNAMED)
663         }
664     }
665
666     /* Casts */
667     fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
668         unsafe {
669             llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED)
670         }
671     }
672
673     fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
674         unsafe {
675             llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED)
676         }
677     }
678
679     fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
680         unsafe {
681             llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED)
682         }
683     }
684
685     fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
686         unsafe {
687             llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty,UNNAMED)
688         }
689     }
690
691     fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
692         unsafe {
693             llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, UNNAMED)
694         }
695     }
696
697     fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
698         unsafe {
699             llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, UNNAMED)
700         }
701     }
702
703     fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
704         unsafe {
705             llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, UNNAMED)
706         }
707     }
708
709     fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
710         unsafe {
711             llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, UNNAMED)
712         }
713     }
714
715     fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
716         unsafe {
717             llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, UNNAMED)
718         }
719     }
720
721     fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
722         unsafe {
723             llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, UNNAMED)
724         }
725     }
726
727     fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
728         unsafe {
729             llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED)
730         }
731     }
732
733
734     fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
735         unsafe {
736             llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed)
737         }
738     }
739
740     fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
741         unsafe {
742             llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED)
743         }
744     }
745
746     /* Comparisons */
747     fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
748         let op = llvm::IntPredicate::from_generic(op);
749         unsafe {
750             llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED)
751         }
752     }
753
754     fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
755         unsafe {
756             llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED)
757         }
758     }
759
760     /* Miscellaneous instructions */
761     fn memcpy(&mut self, dst: &'ll Value, dst_align: Align,
762                   src: &'ll Value, src_align: Align,
763                   size: &'ll Value, flags: MemFlags) {
764         if flags.contains(MemFlags::NONTEMPORAL) {
765             // HACK(nox): This is inefficient but there is no nontemporal memcpy.
766             let val = self.load(src, src_align);
767             let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
768             self.store_with_flags(val, ptr, dst_align, flags);
769             return;
770         }
771         let size = self.intcast(size, self.type_isize(), false);
772         let is_volatile = flags.contains(MemFlags::VOLATILE);
773         let dst = self.pointercast(dst, self.type_i8p());
774         let src = self.pointercast(src, self.type_i8p());
775         unsafe {
776             llvm::LLVMRustBuildMemCpy(self.llbuilder, dst, dst_align.bytes() as c_uint,
777                                       src, src_align.bytes() as c_uint, size, is_volatile);
778         }
779     }
780
781     fn memmove(&mut self, dst: &'ll Value, dst_align: Align,
782                   src: &'ll Value, src_align: Align,
783                   size: &'ll Value, flags: MemFlags) {
784         if flags.contains(MemFlags::NONTEMPORAL) {
785             // HACK(nox): This is inefficient but there is no nontemporal memmove.
786             let val = self.load(src, src_align);
787             let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
788             self.store_with_flags(val, ptr, dst_align, flags);
789             return;
790         }
791         let size = self.intcast(size, self.type_isize(), false);
792         let is_volatile = flags.contains(MemFlags::VOLATILE);
793         let dst = self.pointercast(dst, self.type_i8p());
794         let src = self.pointercast(src, self.type_i8p());
795         unsafe {
796             llvm::LLVMRustBuildMemMove(self.llbuilder, dst, dst_align.bytes() as c_uint,
797                                       src, src_align.bytes() as c_uint, size, is_volatile);
798         }
799     }
800
801     fn memset(
802         &mut self,
803         ptr: &'ll Value,
804         fill_byte: &'ll Value,
805         size: &'ll Value,
806         align: Align,
807         flags: MemFlags,
808     ) {
809         let ptr_width = &self.sess().target.target.target_pointer_width;
810         let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
811         let llintrinsicfn = self.get_intrinsic(&intrinsic_key);
812         let ptr = self.pointercast(ptr, self.type_i8p());
813         let align = self.const_u32(align.bytes() as u32);
814         let volatile = self.const_bool(flags.contains(MemFlags::VOLATILE));
815         self.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None);
816     }
817
818     fn select(
819         &mut self, cond: &'ll Value,
820         then_val: &'ll Value,
821         else_val: &'ll Value,
822     ) -> &'ll Value {
823         unsafe {
824             llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, UNNAMED)
825         }
826     }
827
828     #[allow(dead_code)]
829     fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
830         unsafe {
831             llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED)
832         }
833     }
834
835     fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
836         unsafe {
837             llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED)
838         }
839     }
840
841     fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
842         unsafe {
843             let elt_ty = self.cx.val_ty(elt);
844             let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
845             let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
846             let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
847             self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
848         }
849     }
850
851     fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
852         assert_eq!(idx as c_uint as u64, idx);
853         unsafe {
854             llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED)
855         }
856     }
857
858     fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value,
859                        idx: u64) -> &'ll Value {
860         assert_eq!(idx as c_uint as u64, idx);
861         unsafe {
862             llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
863                                        UNNAMED)
864         }
865     }
866
867     fn landing_pad(&mut self, ty: &'ll Type, pers_fn: &'ll Value,
868                        num_clauses: usize) -> &'ll Value {
869         unsafe {
870             llvm::LLVMBuildLandingPad(self.llbuilder, ty, pers_fn,
871                                       num_clauses as c_uint, UNNAMED)
872         }
873     }
874
875     fn set_cleanup(&mut self, landing_pad: &'ll Value) {
876         unsafe {
877             llvm::LLVMSetCleanup(landing_pad, llvm::True);
878         }
879     }
880
881     fn resume(&mut self, exn: &'ll Value) -> &'ll Value {
882         unsafe {
883             llvm::LLVMBuildResume(self.llbuilder, exn)
884         }
885     }
886
887     fn cleanup_pad(&mut self,
888                        parent: Option<&'ll Value>,
889                        args: &[&'ll Value]) -> Funclet<'ll> {
890         let name = const_cstr!("cleanuppad");
891         let ret = unsafe {
892             llvm::LLVMRustBuildCleanupPad(self.llbuilder,
893                                           parent,
894                                           args.len() as c_uint,
895                                           args.as_ptr(),
896                                           name.as_ptr())
897         };
898         Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
899     }
900
901     fn cleanup_ret(
902         &mut self, funclet: &Funclet<'ll>,
903         unwind: Option<&'ll BasicBlock>,
904     ) -> &'ll Value {
905         let ret = unsafe {
906             llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind)
907         };
908         ret.expect("LLVM does not have support for cleanupret")
909     }
910
911     fn catch_pad(&mut self,
912                      parent: &'ll Value,
913                      args: &[&'ll Value]) -> Funclet<'ll> {
914         let name = const_cstr!("catchpad");
915         let ret = unsafe {
916             llvm::LLVMRustBuildCatchPad(self.llbuilder, parent,
917                                         args.len() as c_uint, args.as_ptr(),
918                                         name.as_ptr())
919         };
920         Funclet::new(ret.expect("LLVM does not have support for catchpad"))
921     }
922
923     fn catch_switch(
924         &mut self,
925         parent: Option<&'ll Value>,
926         unwind: Option<&'ll BasicBlock>,
927         num_handlers: usize,
928     ) -> &'ll Value {
929         let name = const_cstr!("catchswitch");
930         let ret = unsafe {
931             llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind,
932                                            num_handlers as c_uint,
933                                            name.as_ptr())
934         };
935         ret.expect("LLVM does not have support for catchswitch")
936     }
937
938     fn add_handler(&mut self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
939         unsafe {
940             llvm::LLVMRustAddHandler(catch_switch, handler);
941         }
942     }
943
944     fn set_personality_fn(&mut self, personality: &'ll Value) {
945         unsafe {
946             llvm::LLVMSetPersonalityFn(self.llfn(), personality);
947         }
948     }
949
950     // Atomic Operations
951     fn atomic_cmpxchg(
952         &mut self,
953         dst: &'ll Value,
954         cmp: &'ll Value,
955         src: &'ll Value,
956         order: rustc_codegen_ssa::common::AtomicOrdering,
957         failure_order: rustc_codegen_ssa::common::AtomicOrdering,
958         weak: bool,
959     ) -> &'ll Value {
960         let weak = if weak { llvm::True } else { llvm::False };
961         unsafe {
962             llvm::LLVMRustBuildAtomicCmpXchg(
963                 self.llbuilder,
964                 dst,
965                 cmp,
966                 src,
967                 AtomicOrdering::from_generic(order),
968                 AtomicOrdering::from_generic(failure_order),
969                 weak
970             )
971         }
972     }
973     fn atomic_rmw(
974         &mut self,
975         op: rustc_codegen_ssa::common::AtomicRmwBinOp,
976         dst: &'ll Value,
977         src: &'ll Value,
978         order: rustc_codegen_ssa::common::AtomicOrdering,
979     ) -> &'ll Value {
980         unsafe {
981             llvm::LLVMBuildAtomicRMW(
982                 self.llbuilder,
983                 AtomicRmwBinOp::from_generic(op),
984                 dst,
985                 src,
986                 AtomicOrdering::from_generic(order),
987                 False)
988         }
989     }
990
991     fn atomic_fence(
992         &mut self,
993         order: rustc_codegen_ssa::common::AtomicOrdering,
994         scope: rustc_codegen_ssa::common::SynchronizationScope
995     ) {
996         unsafe {
997             llvm::LLVMRustBuildAtomicFence(
998                 self.llbuilder,
999                 AtomicOrdering::from_generic(order),
1000                 SynchronizationScope::from_generic(scope)
1001             );
1002         }
1003     }
1004
1005     fn set_invariant_load(&mut self, load: &'ll Value) {
1006         unsafe {
1007             llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint,
1008                                   llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
1009         }
1010     }
1011
1012     fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
1013         self.call_lifetime_intrinsic("llvm.lifetime.start", ptr, size);
1014     }
1015
1016     fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
1017         self.call_lifetime_intrinsic("llvm.lifetime.end", ptr, size);
1018     }
1019
1020     fn call(
1021         &mut self,
1022         llfn: &'ll Value,
1023         args: &[&'ll Value],
1024         funclet: Option<&Funclet<'ll>>,
1025     ) -> &'ll Value {
1026
1027         debug!("call {:?} with args ({:?})",
1028                llfn,
1029                args);
1030
1031         let args = self.check_call("call", llfn, args);
1032         let bundle = funclet.map(|funclet| funclet.bundle());
1033         let bundle = bundle.as_ref().map(|b| &*b.raw);
1034
1035         unsafe {
1036             llvm::LLVMRustBuildCall(
1037                 self.llbuilder,
1038                 llfn,
1039                 args.as_ptr() as *const &llvm::Value,
1040                 args.len() as c_uint,
1041                 bundle, UNNAMED
1042             )
1043         }
1044     }
1045
1046     fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1047         unsafe {
1048             llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED)
1049         }
1050     }
1051
1052
1053     fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
1054         self.cx
1055     }
1056
1057     unsafe fn delete_basic_block(&mut self, bb: &'ll BasicBlock) {
1058         llvm::LLVMDeleteBasicBlock(bb);
1059     }
1060
1061     fn do_not_inline(&mut self, llret: &'ll Value) {
1062         llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1063     }
1064 }
1065
1066 impl StaticBuilderMethods for Builder<'a, 'll, 'tcx> {
1067     fn get_static(&mut self, def_id: DefId) -> &'ll Value {
1068         // Forward to the `get_static` method of `CodegenCx`
1069         self.cx().get_static(def_id)
1070     }
1071 }
1072
1073 impl Builder<'a, 'll, 'tcx> {
1074     pub fn llfn(&self) -> &'ll Value {
1075         unsafe {
1076             llvm::LLVMGetBasicBlockParent(self.llbb())
1077         }
1078     }
1079
1080     fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
1081         unsafe {
1082             llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
1083         }
1084     }
1085
1086     pub fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1087         unsafe { llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs) }
1088     }
1089
1090     pub fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1091         unsafe { llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs) }
1092     }
1093
1094     pub fn insert_element(
1095         &mut self, vec: &'ll Value,
1096         elt: &'ll Value,
1097         idx: &'ll Value,
1098     ) -> &'ll Value {
1099         unsafe {
1100             llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, UNNAMED)
1101         }
1102     }
1103
1104     pub fn shuffle_vector(
1105         &mut self,
1106         v1: &'ll Value,
1107         v2: &'ll Value,
1108         mask: &'ll Value,
1109     ) -> &'ll Value {
1110         unsafe {
1111             llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, UNNAMED)
1112         }
1113     }
1114
1115     pub fn vector_reduce_fadd(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1116         unsafe { llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src) }
1117     }
1118     pub fn vector_reduce_fmul(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1119         unsafe { llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src) }
1120     }
1121     pub fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1122         unsafe {
1123             let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
1124             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1125             instr
1126         }
1127     }
1128     pub fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1129         unsafe {
1130             let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
1131             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1132             instr
1133         }
1134     }
1135     pub fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
1136         unsafe { llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src) }
1137     }
1138     pub fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
1139         unsafe { llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src) }
1140     }
1141     pub fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
1142         unsafe { llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src) }
1143     }
1144     pub fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
1145         unsafe { llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src) }
1146     }
1147     pub fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
1148         unsafe { llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src) }
1149     }
1150     pub fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
1151         unsafe { llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false) }
1152     }
1153     pub fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
1154         unsafe { llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false) }
1155     }
1156     pub fn vector_reduce_fmin_fast(&mut self, src: &'ll Value) -> &'ll Value {
1157         unsafe {
1158             let instr = llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true);
1159             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1160             instr
1161         }
1162     }
1163     pub fn vector_reduce_fmax_fast(&mut self, src: &'ll Value) -> &'ll Value {
1164         unsafe {
1165             let instr = llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true);
1166             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1167             instr
1168         }
1169     }
1170     pub fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1171         unsafe { llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed) }
1172     }
1173     pub fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1174         unsafe { llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed) }
1175     }
1176
1177     pub fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
1178         unsafe {
1179             llvm::LLVMAddClause(landing_pad, clause);
1180         }
1181     }
1182
1183     pub fn catch_ret(&mut self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
1184         let ret = unsafe {
1185             llvm::LLVMRustBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind)
1186         };
1187         ret.expect("LLVM does not have support for catchret")
1188     }
1189
1190     fn check_store(&mut self, val: &'ll Value, ptr: &'ll Value) -> &'ll Value {
1191         let dest_ptr_ty = self.cx.val_ty(ptr);
1192         let stored_ty = self.cx.val_ty(val);
1193         let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
1194
1195         assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
1196
1197         if dest_ptr_ty == stored_ptr_ty {
1198             ptr
1199         } else {
1200             debug!("type mismatch in store. \
1201                     Expected {:?}, got {:?}; inserting bitcast",
1202                    dest_ptr_ty, stored_ptr_ty);
1203             self.bitcast(ptr, stored_ptr_ty)
1204         }
1205     }
1206
1207     fn check_call<'b>(&mut self,
1208                       typ: &str,
1209                       llfn: &'ll Value,
1210                       args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
1211         let mut fn_ty = self.cx.val_ty(llfn);
1212         // Strip off pointers
1213         while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
1214             fn_ty = self.cx.element_type(fn_ty);
1215         }
1216
1217         assert!(self.cx.type_kind(fn_ty) == TypeKind::Function,
1218                 "builder::{} not passed a function, but {:?}", typ, fn_ty);
1219
1220         let param_tys = self.cx.func_params_types(fn_ty);
1221
1222         let all_args_match = param_tys.iter()
1223             .zip(args.iter().map(|&v| self.val_ty(v)))
1224             .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1225
1226         if all_args_match {
1227             return Cow::Borrowed(args);
1228         }
1229
1230         let casted_args: Vec<_> = param_tys.into_iter()
1231             .zip(args.iter())
1232             .enumerate()
1233             .map(|(i, (expected_ty, &actual_val))| {
1234                 let actual_ty = self.val_ty(actual_val);
1235                 if expected_ty != actual_ty {
1236                     debug!("type mismatch in function call of {:?}. \
1237                             Expected {:?} for param {}, got {:?}; injecting bitcast",
1238                            llfn, expected_ty, i, actual_ty);
1239                     self.bitcast(actual_val, expected_ty)
1240                 } else {
1241                     actual_val
1242                 }
1243             })
1244             .collect();
1245
1246         Cow::Owned(casted_args)
1247     }
1248
1249     pub fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
1250         unsafe {
1251             llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED)
1252         }
1253     }
1254
1255     fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
1256         if self.cx.sess().opts.optimize == config::OptLevel::No {
1257             return;
1258         }
1259
1260         let size = size.bytes();
1261         if size == 0 {
1262             return;
1263         }
1264
1265         let lifetime_intrinsic = self.cx.get_intrinsic(intrinsic);
1266
1267         let ptr = self.pointercast(ptr, self.cx.type_i8p());
1268         self.call(lifetime_intrinsic, &[self.cx.const_u64(size), ptr], None);
1269     }
1270
1271     fn phi(&mut self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value {
1272         assert_eq!(vals.len(), bbs.len());
1273         let phi = unsafe {
1274             llvm::LLVMBuildPhi(self.llbuilder, ty, UNNAMED)
1275         };
1276         unsafe {
1277             llvm::LLVMAddIncoming(phi, vals.as_ptr(),
1278                                   bbs.as_ptr(),
1279                                   vals.len() as c_uint);
1280             phi
1281         }
1282     }
1283
1284     fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
1285         unsafe {
1286             llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1287         }
1288     }
1289 }