1 use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
2 use llvm::{self, False, BasicBlock};
3 use rustc_codegen_ssa::common::{IntPredicate, TypeKind, RealPredicate};
4 use rustc_codegen_ssa::{self, MemFlags};
6 use context::CodegenCx;
8 use type_of::LayoutLlvmExt;
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::*;
18 use rustc_codegen_ssa::base::to_immediate;
19 use rustc_codegen_ssa::mir::operand::{OperandValue, OperandRef};
20 use rustc_codegen_ssa::mir::place::PlaceRef;
23 use std::ops::{Deref, Range};
26 // All Builders must have an llfn associated with them
28 pub struct Builder<'a, 'll: 'a, 'tcx: 'll> {
29 pub llbuilder: &'ll mut llvm::Builder<'ll>,
30 pub cx: &'a CodegenCx<'ll, 'tcx>,
33 impl Drop for Builder<'a, 'll, 'tcx> {
36 llvm::LLVMDisposeBuilder(&mut *(self.llbuilder as *mut _));
41 // This is a really awful way to get a zero-length c-string, but better (and a
42 // lot more efficient) than doing str::as_c_str("", ...) every time.
43 fn noname() -> *const c_char {
44 static CNULL: c_char = 0;
48 impl BackendTypes for Builder<'_, 'll, 'tcx> {
49 type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
50 type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
51 type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
52 type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;
54 type DIScope = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIScope;
57 impl ty::layout::HasDataLayout for Builder<'_, '_, '_> {
58 fn data_layout(&self) -> &ty::layout::TargetDataLayout {
63 impl ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
64 fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
69 impl ty::layout::LayoutOf for Builder<'_, '_, 'tcx> {
71 type TyLayout = TyLayout<'tcx>;
73 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
78 impl Deref for Builder<'_, 'll, 'tcx> {
79 type Target = CodegenCx<'ll, 'tcx>;
81 fn deref(&self) -> &Self::Target {
86 impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
87 type CodegenCx = CodegenCx<'ll, 'tcx>;
90 impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
92 cx: &'a CodegenCx<'ll, 'tcx>,
96 let mut bx = Builder::with_cx(cx);
98 let name = SmallCStr::new(name);
99 llvm::LLVMAppendBasicBlockInContext(
105 bx.position_at_end(llbb);
109 fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
110 // Create a fresh builder from the crate context.
111 let llbuilder = unsafe {
112 llvm::LLVMCreateBuilderInContext(cx.llcx)
120 fn build_sibling_block<'b>(&self, name: &'b str) -> Self {
121 Builder::new_block(self.cx, self.llfn(), name)
124 fn llfn(&self) -> &'ll Value {
126 llvm::LLVMGetBasicBlockParent(self.llbb())
130 fn llbb(&self) -> &'ll BasicBlock {
132 llvm::LLVMGetInsertBlock(self.llbuilder)
136 fn count_insn(&self, category: &str) {
137 if self.sess().codegen_stats() {
138 self.stats.borrow_mut().n_llvm_insns += 1;
140 if self.sess().count_llvm_insns() {
144 .entry(category.to_string())
149 fn set_value_name(&mut self, value: &'ll Value, name: &str) {
150 let cname = SmallCStr::new(name);
152 llvm::LLVMSetValueName(value, cname.as_ptr());
156 fn position_at_end(&mut self, llbb: &'ll BasicBlock) {
158 llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
162 fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
164 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
168 fn ret_void(&mut self) {
169 self.count_insn("retvoid");
171 llvm::LLVMBuildRetVoid(self.llbuilder);
175 fn ret(&mut self, v: &'ll Value) {
176 self.count_insn("ret");
178 llvm::LLVMBuildRet(self.llbuilder, v);
182 fn br(&mut self, dest: &'ll BasicBlock) {
183 self.count_insn("br");
185 llvm::LLVMBuildBr(self.llbuilder, dest);
192 then_llbb: &'ll BasicBlock,
193 else_llbb: &'ll BasicBlock,
195 self.count_insn("condbr");
197 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
204 else_llbb: &'ll BasicBlock,
208 llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
216 then: &'ll BasicBlock,
217 catch: &'ll BasicBlock,
218 funclet: Option<&Funclet<'ll>>,
220 self.count_insn("invoke");
222 debug!("Invoke {:?} with args ({:?})",
226 let args = self.check_call("invoke", llfn, args);
227 let bundle = funclet.map(|funclet| funclet.bundle());
228 let bundle = bundle.as_ref().map(|b| &*b.raw);
231 llvm::LLVMRustBuildInvoke(self.llbuilder,
234 args.len() as c_uint,
242 fn unreachable(&mut self) {
243 self.count_insn("unreachable");
245 llvm::LLVMBuildUnreachable(self.llbuilder);
250 fn add(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
251 self.count_insn("add");
253 llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
257 fn fadd(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
258 self.count_insn("fadd");
260 llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
264 fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
265 self.count_insn("fadd");
267 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname());
268 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
273 fn sub(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
274 self.count_insn("sub");
276 llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
280 fn fsub(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
281 self.count_insn("fsub");
283 llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
287 fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
288 self.count_insn("fsub");
290 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname());
291 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
296 fn mul(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
297 self.count_insn("mul");
299 llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
303 fn fmul(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
304 self.count_insn("fmul");
306 llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
310 fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
311 self.count_insn("fmul");
313 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname());
314 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
320 fn udiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
321 self.count_insn("udiv");
323 llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
327 fn exactudiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
328 self.count_insn("exactudiv");
330 llvm::LLVMBuildExactUDiv(self.llbuilder, lhs, rhs, noname())
334 fn sdiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
335 self.count_insn("sdiv");
337 llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
341 fn exactsdiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
342 self.count_insn("exactsdiv");
344 llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
348 fn fdiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
349 self.count_insn("fdiv");
351 llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
355 fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
356 self.count_insn("fdiv");
358 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname());
359 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
364 fn urem(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
365 self.count_insn("urem");
367 llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
371 fn srem(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
372 self.count_insn("srem");
374 llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
378 fn frem(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
379 self.count_insn("frem");
381 llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
385 fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
386 self.count_insn("frem");
388 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname());
389 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
394 fn shl(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
395 self.count_insn("shl");
397 llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
401 fn lshr(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
402 self.count_insn("lshr");
404 llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
408 fn ashr(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
409 self.count_insn("ashr");
411 llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
415 fn and(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
416 self.count_insn("and");
418 llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
422 fn or(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
423 self.count_insn("or");
425 llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
429 fn xor(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
430 self.count_insn("xor");
432 llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
436 fn neg(&mut self, v: &'ll Value) -> &'ll Value {
437 self.count_insn("neg");
439 llvm::LLVMBuildNeg(self.llbuilder, v, noname())
443 fn fneg(&mut self, v: &'ll Value) -> &'ll Value {
444 self.count_insn("fneg");
446 llvm::LLVMBuildFNeg(self.llbuilder, v, noname())
450 fn not(&mut self, v: &'ll Value) -> &'ll Value {
451 self.count_insn("not");
453 llvm::LLVMBuildNot(self.llbuilder, v, noname())
463 ) -> (Self::Value, Self::Value) {
464 use syntax::ast::IntTy::*;
465 use syntax::ast::UintTy::*;
466 use rustc::ty::{Int, Uint};
468 let new_sty = match ty.sty {
469 Int(Isize) => Int(self.tcx.sess.target.isize_ty),
470 Uint(Usize) => Uint(self.tcx.sess.target.usize_ty),
471 ref t @ Uint(_) | ref t @ Int(_) => t.clone(),
472 _ => panic!("tried to get overflow intrinsic for op applied to non-int type")
475 let name = match oop {
476 OverflowOp::Add => match new_sty {
477 Int(I8) => "llvm.sadd.with.overflow.i8",
478 Int(I16) => "llvm.sadd.with.overflow.i16",
479 Int(I32) => "llvm.sadd.with.overflow.i32",
480 Int(I64) => "llvm.sadd.with.overflow.i64",
481 Int(I128) => "llvm.sadd.with.overflow.i128",
483 Uint(U8) => "llvm.uadd.with.overflow.i8",
484 Uint(U16) => "llvm.uadd.with.overflow.i16",
485 Uint(U32) => "llvm.uadd.with.overflow.i32",
486 Uint(U64) => "llvm.uadd.with.overflow.i64",
487 Uint(U128) => "llvm.uadd.with.overflow.i128",
491 OverflowOp::Sub => match new_sty {
492 Int(I8) => "llvm.ssub.with.overflow.i8",
493 Int(I16) => "llvm.ssub.with.overflow.i16",
494 Int(I32) => "llvm.ssub.with.overflow.i32",
495 Int(I64) => "llvm.ssub.with.overflow.i64",
496 Int(I128) => "llvm.ssub.with.overflow.i128",
498 Uint(U8) => "llvm.usub.with.overflow.i8",
499 Uint(U16) => "llvm.usub.with.overflow.i16",
500 Uint(U32) => "llvm.usub.with.overflow.i32",
501 Uint(U64) => "llvm.usub.with.overflow.i64",
502 Uint(U128) => "llvm.usub.with.overflow.i128",
506 OverflowOp::Mul => match new_sty {
507 Int(I8) => "llvm.smul.with.overflow.i8",
508 Int(I16) => "llvm.smul.with.overflow.i16",
509 Int(I32) => "llvm.smul.with.overflow.i32",
510 Int(I64) => "llvm.smul.with.overflow.i64",
511 Int(I128) => "llvm.smul.with.overflow.i128",
513 Uint(U8) => "llvm.umul.with.overflow.i8",
514 Uint(U16) => "llvm.umul.with.overflow.i16",
515 Uint(U32) => "llvm.umul.with.overflow.i32",
516 Uint(U64) => "llvm.umul.with.overflow.i64",
517 Uint(U128) => "llvm.umul.with.overflow.i128",
523 let intrinsic = self.get_intrinsic(&name);
524 let res = self.call(intrinsic, &[lhs, rhs], None);
526 self.extract_value(res, 0),
527 self.extract_value(res, 1),
531 fn alloca(&mut self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
532 let mut bx = Builder::with_cx(self.cx);
533 bx.position_at_start(unsafe {
534 llvm::LLVMGetFirstBasicBlock(self.llfn())
536 bx.dynamic_alloca(ty, name, align)
539 fn dynamic_alloca(&mut self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
540 self.count_insn("alloca");
542 let alloca = if name.is_empty() {
543 llvm::LLVMBuildAlloca(self.llbuilder, ty, noname())
545 let name = SmallCStr::new(name);
546 llvm::LLVMBuildAlloca(self.llbuilder, ty,
549 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
554 fn array_alloca(&mut self,
558 align: Align) -> &'ll Value {
559 self.count_insn("alloca");
561 let alloca = if name.is_empty() {
562 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, noname())
564 let name = SmallCStr::new(name);
565 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len,
568 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
573 fn load(&mut self, ptr: &'ll Value, align: Align) -> &'ll Value {
574 self.count_insn("load");
576 let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
577 llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
582 fn volatile_load(&mut self, ptr: &'ll Value) -> &'ll Value {
583 self.count_insn("load.volatile");
585 let insn = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
586 llvm::LLVMSetVolatile(insn, llvm::True);
594 order: rustc_codegen_ssa::common::AtomicOrdering,
597 self.count_insn("load.atomic");
599 let load = llvm::LLVMRustBuildAtomicLoad(
603 AtomicOrdering::from_generic(order),
605 // LLVM requires the alignment of atomic loads to be at least the size of the type.
606 llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
613 place: PlaceRef<'tcx, &'ll Value>
614 ) -> OperandRef<'tcx, &'ll Value> {
615 debug!("PlaceRef::load: {:?}", place);
617 assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
619 if place.layout.is_zst() {
620 return OperandRef::new_zst(self.cx(), place.layout);
623 fn scalar_load_metadata<'a, 'll, 'tcx>(
624 bx: &mut Builder<'a, 'll, 'tcx>,
626 scalar: &layout::Scalar
628 let vr = scalar.valid_range.clone();
631 let range = scalar.valid_range_exclusive(bx);
632 if range.start != range.end {
633 bx.range_metadata(load, range);
636 layout::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
637 bx.nonnull_metadata(load);
643 let val = if let Some(llextra) = place.llextra {
644 OperandValue::Ref(place.llval, Some(llextra), place.align)
645 } else if place.layout.is_llvm_immediate() {
646 let mut const_llval = None;
648 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
649 if llvm::LLVMIsGlobalConstant(global) == llvm::True {
650 const_llval = llvm::LLVMGetInitializer(global);
654 let llval = const_llval.unwrap_or_else(|| {
655 let load = self.load(place.llval, place.align);
656 if let layout::Abi::Scalar(ref scalar) = place.layout.abi {
657 scalar_load_metadata(self, load, scalar);
661 OperandValue::Immediate(to_immediate(self, llval, place.layout))
662 } else if let layout::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
663 let b_offset = a.value.size(self).align_to(b.value.align(self).abi);
665 let mut load = |i, scalar: &layout::Scalar, align| {
666 let llptr = self.struct_gep(place.llval, i as u64);
667 let load = self.load(llptr, align);
668 scalar_load_metadata(self, load, scalar);
669 if scalar.is_bool() {
670 self.trunc(load, self.type_i1())
677 load(0, a, place.align),
678 load(1, b, place.align.restrict_for_offset(b_offset)),
681 OperandValue::Ref(place.llval, None, place.align)
684 OperandRef { val, layout: place.layout }
689 fn range_metadata(&mut self, load: &'ll Value, range: Range<u128>) {
690 if self.sess().target.target.arch == "amdgpu" {
691 // amdgpu/LLVM does something weird and thinks a i64 value is
692 // split into a v2i32, halving the bitwidth LLVM expects,
693 // tripping an assertion. So, for now, just disable this
699 let llty = self.cx.val_ty(load);
701 self.cx.const_uint_big(llty, range.start),
702 self.cx.const_uint_big(llty, range.end)
705 llvm::LLVMSetMetadata(load, llvm::MD_range as c_uint,
706 llvm::LLVMMDNodeInContext(self.cx.llcx,
712 fn nonnull_metadata(&mut self, load: &'ll Value) {
714 llvm::LLVMSetMetadata(load, llvm::MD_nonnull as c_uint,
715 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
719 fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
720 self.store_with_flags(val, ptr, align, MemFlags::empty())
730 debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
731 self.count_insn("store");
732 let ptr = self.check_store(val, ptr);
734 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
735 let align = if flags.contains(MemFlags::UNALIGNED) {
738 align.bytes() as c_uint
740 llvm::LLVMSetAlignment(store, align);
741 if flags.contains(MemFlags::VOLATILE) {
742 llvm::LLVMSetVolatile(store, llvm::True);
744 if flags.contains(MemFlags::NONTEMPORAL) {
745 // According to LLVM [1] building a nontemporal store must
746 // *always* point to a metadata value of the integer 1.
748 // [1]: http://llvm.org/docs/LangRef.html#store-instruction
749 let one = self.cx.const_i32(1);
750 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
751 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
757 fn atomic_store(&mut self, val: &'ll Value, ptr: &'ll Value,
758 order: rustc_codegen_ssa::common::AtomicOrdering, size: Size) {
759 debug!("Store {:?} -> {:?}", val, ptr);
760 self.count_insn("store.atomic");
761 let ptr = self.check_store(val, ptr);
763 let store = llvm::LLVMRustBuildAtomicStore(
767 AtomicOrdering::from_generic(order),
769 // LLVM requires the alignment of atomic stores to be at least the size of the type.
770 llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
774 fn gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
775 self.count_insn("gep");
777 llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
778 indices.len() as c_uint, noname())
782 fn inbounds_gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
783 self.count_insn("inboundsgep");
785 llvm::LLVMBuildInBoundsGEP(
786 self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname())
791 fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
792 self.count_insn("trunc");
794 llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, noname())
798 fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
799 self.count_insn("sext");
801 llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, noname())
805 fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
806 self.count_insn("fptoui");
808 llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, noname())
812 fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
813 self.count_insn("fptosi");
815 llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty,noname())
819 fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
820 self.count_insn("uitofp");
822 llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, noname())
826 fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
827 self.count_insn("sitofp");
829 llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, noname())
833 fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
834 self.count_insn("fptrunc");
836 llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, noname())
840 fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
841 self.count_insn("fpext");
843 llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, noname())
847 fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
848 self.count_insn("ptrtoint");
850 llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, noname())
854 fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
855 self.count_insn("inttoptr");
857 llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, noname())
861 fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
862 self.count_insn("bitcast");
864 llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, noname())
869 fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
870 self.count_insn("intcast");
872 llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed)
876 fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
877 self.count_insn("pointercast");
879 llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, noname())
884 fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
885 self.count_insn("icmp");
886 let op = llvm::IntPredicate::from_generic(op);
888 llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
892 fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
893 self.count_insn("fcmp");
895 llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
899 /* Miscellaneous instructions */
900 fn empty_phi(&mut self, ty: &'ll Type) -> &'ll Value {
901 self.count_insn("emptyphi");
903 llvm::LLVMBuildPhi(self.llbuilder, ty, noname())
907 fn phi(&mut self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value {
908 assert_eq!(vals.len(), bbs.len());
909 let phi = self.empty_phi(ty);
910 self.count_insn("addincoming");
912 llvm::LLVMAddIncoming(phi, vals.as_ptr(),
914 vals.len() as c_uint);
919 fn inline_asm_call(&mut self, asm: &CStr, cons: &CStr,
920 inputs: &[&'ll Value], output: &'ll Type,
921 volatile: bool, alignstack: bool,
922 dia: syntax::ast::AsmDialect) -> Option<&'ll Value> {
923 self.count_insn("inlineasm");
925 let volatile = if volatile { llvm::True }
926 else { llvm::False };
927 let alignstack = if alignstack { llvm::True }
928 else { llvm::False };
930 let argtys = inputs.iter().map(|v| {
931 debug!("Asm Input Type: {:?}", *v);
933 }).collect::<Vec<_>>();
935 debug!("Asm Output Type: {:?}", output);
936 let fty = self.type_func(&argtys[..], output);
938 // Ask LLVM to verify that the constraints are well-formed.
939 let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons.as_ptr());
940 debug!("Constraint verification result: {:?}", constraints_ok);
942 let v = llvm::LLVMRustInlineAsm(
948 AsmDialect::from_generic(dia),
950 Some(self.call(v, inputs, None))
952 // LLVM has detected an issue with our constraints, bail out
958 fn memcpy(&mut self, dst: &'ll Value, dst_align: Align,
959 src: &'ll Value, src_align: Align,
960 size: &'ll Value, flags: MemFlags) {
961 if flags.contains(MemFlags::NONTEMPORAL) {
962 // HACK(nox): This is inefficient but there is no nontemporal memcpy.
963 let val = self.load(src, src_align);
964 let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
965 self.store_with_flags(val, ptr, dst_align, flags);
968 let size = self.intcast(size, self.type_isize(), false);
969 let is_volatile = flags.contains(MemFlags::VOLATILE);
970 let dst = self.pointercast(dst, self.type_i8p());
971 let src = self.pointercast(src, self.type_i8p());
973 llvm::LLVMRustBuildMemCpy(self.llbuilder, dst, dst_align.bytes() as c_uint,
974 src, src_align.bytes() as c_uint, size, is_volatile);
978 fn memmove(&mut self, dst: &'ll Value, dst_align: Align,
979 src: &'ll Value, src_align: Align,
980 size: &'ll Value, flags: MemFlags) {
981 if flags.contains(MemFlags::NONTEMPORAL) {
982 // HACK(nox): This is inefficient but there is no nontemporal memmove.
983 let val = self.load(src, src_align);
984 let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
985 self.store_with_flags(val, ptr, dst_align, flags);
988 let size = self.intcast(size, self.type_isize(), false);
989 let is_volatile = flags.contains(MemFlags::VOLATILE);
990 let dst = self.pointercast(dst, self.type_i8p());
991 let src = self.pointercast(src, self.type_i8p());
993 llvm::LLVMRustBuildMemMove(self.llbuilder, dst, dst_align.bytes() as c_uint,
994 src, src_align.bytes() as c_uint, size, is_volatile);
1001 fill_byte: &'ll Value,
1006 let ptr_width = &self.sess().target.target.target_pointer_width;
1007 let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
1008 let llintrinsicfn = self.get_intrinsic(&intrinsic_key);
1009 let ptr = self.pointercast(ptr, self.type_i8p());
1010 let align = self.const_u32(align.bytes() as u32);
1011 let volatile = self.const_bool(flags.contains(MemFlags::VOLATILE));
1012 self.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None);
1015 fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1016 self.count_insn("minnum");
1017 unsafe { llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs) }
1019 fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1020 self.count_insn("maxnum");
1021 unsafe { llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs) }
1025 &mut self, cond: &'ll Value,
1026 then_val: &'ll Value,
1027 else_val: &'ll Value,
1029 self.count_insn("select");
1031 llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
1036 fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
1037 self.count_insn("vaarg");
1039 llvm::LLVMBuildVAArg(self.llbuilder, list, ty, noname())
1043 fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
1044 self.count_insn("extractelement");
1046 llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
1051 &mut self, vec: &'ll Value,
1055 self.count_insn("insertelement");
1057 llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
1061 fn shuffle_vector(&mut self, v1: &'ll Value, v2: &'ll Value, mask: &'ll Value) -> &'ll Value {
1062 self.count_insn("shufflevector");
1064 llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
1068 fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
1070 let elt_ty = self.cx.val_ty(elt);
1071 let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
1072 let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
1073 let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
1074 self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
1078 fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1079 self.count_insn("vector.reduce.fadd_fast");
1081 // FIXME: add a non-fast math version once
1082 // https://bugs.llvm.org/show_bug.cgi?id=36732
1084 let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
1085 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1089 fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1090 self.count_insn("vector.reduce.fmul_fast");
1092 // FIXME: add a non-fast math version once
1093 // https://bugs.llvm.org/show_bug.cgi?id=36732
1095 let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
1096 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1100 fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
1101 self.count_insn("vector.reduce.add");
1102 unsafe { llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src) }
1104 fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
1105 self.count_insn("vector.reduce.mul");
1106 unsafe { llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src) }
1108 fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
1109 self.count_insn("vector.reduce.and");
1110 unsafe { llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src) }
1112 fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
1113 self.count_insn("vector.reduce.or");
1114 unsafe { llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src) }
1116 fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
1117 self.count_insn("vector.reduce.xor");
1118 unsafe { llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src) }
1120 fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
1121 self.count_insn("vector.reduce.fmin");
1122 unsafe { llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false) }
1124 fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
1125 self.count_insn("vector.reduce.fmax");
1126 unsafe { llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false) }
1128 fn vector_reduce_fmin_fast(&mut self, src: &'ll Value) -> &'ll Value {
1129 self.count_insn("vector.reduce.fmin_fast");
1131 let instr = llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true);
1132 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1136 fn vector_reduce_fmax_fast(&mut self, src: &'ll Value) -> &'ll Value {
1137 self.count_insn("vector.reduce.fmax_fast");
1139 let instr = llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true);
1140 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1144 fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1145 self.count_insn("vector.reduce.min");
1146 unsafe { llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed) }
1148 fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1149 self.count_insn("vector.reduce.max");
1150 unsafe { llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed) }
1153 fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
1154 self.count_insn("extractvalue");
1155 assert_eq!(idx as c_uint as u64, idx);
1157 llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
1161 fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value,
1162 idx: u64) -> &'ll Value {
1163 self.count_insn("insertvalue");
1164 assert_eq!(idx as c_uint as u64, idx);
1166 llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
1171 fn landing_pad(&mut self, ty: &'ll Type, pers_fn: &'ll Value,
1172 num_clauses: usize) -> &'ll Value {
1173 self.count_insn("landingpad");
1175 llvm::LLVMBuildLandingPad(self.llbuilder, ty, pers_fn,
1176 num_clauses as c_uint, noname())
1180 fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
1182 llvm::LLVMAddClause(landing_pad, clause);
1186 fn set_cleanup(&mut self, landing_pad: &'ll Value) {
1187 self.count_insn("setcleanup");
1189 llvm::LLVMSetCleanup(landing_pad, llvm::True);
1193 fn resume(&mut self, exn: &'ll Value) -> &'ll Value {
1194 self.count_insn("resume");
1196 llvm::LLVMBuildResume(self.llbuilder, exn)
1200 fn cleanup_pad(&mut self,
1201 parent: Option<&'ll Value>,
1202 args: &[&'ll Value]) -> Funclet<'ll> {
1203 self.count_insn("cleanuppad");
1204 let name = const_cstr!("cleanuppad");
1206 llvm::LLVMRustBuildCleanupPad(self.llbuilder,
1208 args.len() as c_uint,
1212 Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
1216 &mut self, funclet: &Funclet<'ll>,
1217 unwind: Option<&'ll BasicBlock>,
1219 self.count_insn("cleanupret");
1221 llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind)
1223 ret.expect("LLVM does not have support for cleanupret")
1226 fn catch_pad(&mut self,
1228 args: &[&'ll Value]) -> Funclet<'ll> {
1229 self.count_insn("catchpad");
1230 let name = const_cstr!("catchpad");
1232 llvm::LLVMRustBuildCatchPad(self.llbuilder, parent,
1233 args.len() as c_uint, args.as_ptr(),
1236 Funclet::new(ret.expect("LLVM does not have support for catchpad"))
1239 fn catch_ret(&mut self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
1240 self.count_insn("catchret");
1242 llvm::LLVMRustBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind)
1244 ret.expect("LLVM does not have support for catchret")
1249 parent: Option<&'ll Value>,
1250 unwind: Option<&'ll BasicBlock>,
1251 num_handlers: usize,
1253 self.count_insn("catchswitch");
1254 let name = const_cstr!("catchswitch");
1256 llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind,
1257 num_handlers as c_uint,
1260 ret.expect("LLVM does not have support for catchswitch")
1263 fn add_handler(&mut self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
1265 llvm::LLVMRustAddHandler(catch_switch, handler);
1269 fn set_personality_fn(&mut self, personality: &'ll Value) {
1271 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
1275 // Atomic Operations
1281 order: rustc_codegen_ssa::common::AtomicOrdering,
1282 failure_order: rustc_codegen_ssa::common::AtomicOrdering,
1285 let weak = if weak { llvm::True } else { llvm::False };
1287 llvm::LLVMRustBuildAtomicCmpXchg(
1292 AtomicOrdering::from_generic(order),
1293 AtomicOrdering::from_generic(failure_order),
1300 op: rustc_codegen_ssa::common::AtomicRmwBinOp,
1303 order: rustc_codegen_ssa::common::AtomicOrdering,
1306 llvm::LLVMBuildAtomicRMW(
1308 AtomicRmwBinOp::from_generic(op),
1311 AtomicOrdering::from_generic(order),
1318 order: rustc_codegen_ssa::common::AtomicOrdering,
1319 scope: rustc_codegen_ssa::common::SynchronizationScope
1322 llvm::LLVMRustBuildAtomicFence(
1324 AtomicOrdering::from_generic(order),
1325 SynchronizationScope::from_generic(scope)
1330 fn add_case(&mut self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
1332 llvm::LLVMAddCase(s, on_val, dest)
1336 fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
1337 self.count_insn("addincoming");
1339 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1343 fn set_invariant_load(&mut self, load: &'ll Value) {
1345 llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint,
1346 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
1350 fn check_store<'b>(&mut self,
1352 ptr: &'ll Value) -> &'ll Value {
1353 let dest_ptr_ty = self.cx.val_ty(ptr);
1354 let stored_ty = self.cx.val_ty(val);
1355 let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
1357 assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
1359 if dest_ptr_ty == stored_ptr_ty {
1362 debug!("Type mismatch in store. \
1363 Expected {:?}, got {:?}; inserting bitcast",
1364 dest_ptr_ty, stored_ptr_ty);
1365 self.bitcast(ptr, stored_ptr_ty)
1369 fn check_call<'b>(&mut self,
1372 args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
1373 let mut fn_ty = self.cx.val_ty(llfn);
1374 // Strip off pointers
1375 while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
1376 fn_ty = self.cx.element_type(fn_ty);
1379 assert!(self.cx.type_kind(fn_ty) == TypeKind::Function,
1380 "builder::{} not passed a function, but {:?}", typ, fn_ty);
1382 let param_tys = self.cx.func_params_types(fn_ty);
1384 let all_args_match = param_tys.iter()
1385 .zip(args.iter().map(|&v| self.val_ty(v)))
1386 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1389 return Cow::Borrowed(args);
1392 let casted_args: Vec<_> = param_tys.into_iter()
1395 .map(|(i, (expected_ty, &actual_val))| {
1396 let actual_ty = self.val_ty(actual_val);
1397 if expected_ty != actual_ty {
1398 debug!("Type mismatch in function call of {:?}. \
1399 Expected {:?} for param {}, got {:?}; injecting bitcast",
1400 llfn, expected_ty, i, actual_ty);
1401 self.bitcast(actual_val, expected_ty)
1408 Cow::Owned(casted_args)
1411 fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
1412 self.call_lifetime_intrinsic("llvm.lifetime.start", ptr, size);
1415 fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
1416 self.call_lifetime_intrinsic("llvm.lifetime.end", ptr, size);
1422 args: &[&'ll Value],
1423 funclet: Option<&Funclet<'ll>>,
1425 self.count_insn("call");
1427 debug!("Call {:?} with args ({:?})",
1431 let args = self.check_call("call", llfn, args);
1432 let bundle = funclet.map(|funclet| funclet.bundle());
1433 let bundle = bundle.as_ref().map(|b| &*b.raw);
1436 llvm::LLVMRustBuildCall(
1439 args.as_ptr() as *const &llvm::Value,
1440 args.len() as c_uint,
1446 fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1447 self.count_insn("zext");
1449 llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, noname())
1453 fn struct_gep(&mut self, ptr: &'ll Value, idx: u64) -> &'ll Value {
1454 self.count_insn("structgep");
1455 assert_eq!(idx as c_uint as u64, idx);
1457 llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
1461 fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
1465 unsafe fn delete_basic_block(&mut self, bb: &'ll BasicBlock) {
1466 llvm::LLVMDeleteBasicBlock(bb);
1469 fn do_not_inline(&mut self, llret: &'ll Value) {
1470 llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1474 impl StaticBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
1475 fn get_static(&self, def_id: DefId) -> &'ll Value {
1476 self.cx().get_static(def_id)
1480 impl Builder<'a, 'll, 'tcx> {
1481 fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
1482 if self.cx.sess().opts.optimize == config::OptLevel::No {
1486 let size = size.bytes();
1491 let lifetime_intrinsic = self.cx.get_intrinsic(intrinsic);
1493 let ptr = self.pointercast(ptr, self.cx.type_i8p());
1494 self.call(lifetime_intrinsic, &[self.cx.const_u64(size), ptr], None);