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 syntax::symbol::LocalInternedString;
9 use rustc_codegen_ssa::common::{IntPredicate, TypeKind, RealPredicate};
10 use rustc_codegen_ssa::MemFlags;
11 use libc::{c_uint, c_char};
12 use rustc::ty::{self, Ty, TyCtxt};
13 use rustc::ty::layout::{self, Align, Size, TyLayout};
14 use rustc::hir::def_id::DefId;
15 use rustc::session::config;
16 use rustc_data_structures::small_c_str::SmallCStr;
17 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;
21 use rustc_target::spec::{HasTargetSpec, Target};
24 use std::ops::{Deref, Range};
26 use std::iter::TrustedLen;
28 // All Builders must have an llfn associated with them
30 pub struct Builder<'a, 'll: 'a, 'tcx: 'll> {
31 pub llbuilder: &'ll mut llvm::Builder<'ll>,
32 pub cx: &'a CodegenCx<'ll, 'tcx>,
35 impl Drop for Builder<'a, 'll, 'tcx> {
38 llvm::LLVMDisposeBuilder(&mut *(self.llbuilder as *mut _));
43 // FIXME(eddyb) use a checked constructor when they become `const fn`.
44 const EMPTY_C_STR: &CStr = unsafe {
45 CStr::from_bytes_with_nul_unchecked(b"\0")
48 /// Empty string, to be used where LLVM expects an instruction name, indicating
49 /// that the instruction is to be left unnamed (i.e. numbered, in textual IR).
50 // FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer.
51 const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
53 impl BackendTypes for Builder<'_, 'll, 'tcx> {
54 type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
55 type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
56 type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
57 type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;
59 type DIScope = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIScope;
62 impl ty::layout::HasDataLayout for Builder<'_, '_, '_> {
63 fn data_layout(&self) -> &ty::layout::TargetDataLayout {
68 impl ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
69 fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
74 impl ty::layout::HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
75 fn param_env(&self) -> ty::ParamEnv<'tcx> {
80 impl HasTargetSpec for Builder<'_, '_, 'tcx> {
81 fn target_spec(&self) -> &Target {
82 &self.cx.target_spec()
86 impl ty::layout::LayoutOf for Builder<'_, '_, 'tcx> {
88 type TyLayout = TyLayout<'tcx>;
90 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
95 impl Deref for Builder<'_, 'll, 'tcx> {
96 type Target = CodegenCx<'ll, 'tcx>;
98 fn deref(&self) -> &Self::Target {
103 impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
104 type CodegenCx = CodegenCx<'ll, 'tcx>;
107 macro_rules! builder_methods_for_value_instructions {
108 ($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
109 $(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
111 llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED)
117 impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
119 cx: &'a CodegenCx<'ll, 'tcx>,
123 let mut bx = Builder::with_cx(cx);
125 let name = SmallCStr::new(name);
126 llvm::LLVMAppendBasicBlockInContext(
132 bx.position_at_end(llbb);
136 fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
137 // Create a fresh builder from the crate context.
138 let llbuilder = unsafe {
139 llvm::LLVMCreateBuilderInContext(cx.llcx)
147 fn build_sibling_block<'b>(&self, name: &'b str) -> Self {
148 Builder::new_block(self.cx, self.llfn(), name)
151 fn llbb(&self) -> &'ll BasicBlock {
153 llvm::LLVMGetInsertBlock(self.llbuilder)
157 fn position_at_end(&mut self, llbb: &'ll BasicBlock) {
159 llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
163 fn ret_void(&mut self) {
165 llvm::LLVMBuildRetVoid(self.llbuilder);
169 fn ret(&mut self, v: &'ll Value) {
171 llvm::LLVMBuildRet(self.llbuilder, v);
175 fn br(&mut self, dest: &'ll BasicBlock) {
177 llvm::LLVMBuildBr(self.llbuilder, dest);
184 then_llbb: &'ll BasicBlock,
185 else_llbb: &'ll BasicBlock,
188 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
195 else_llbb: &'ll BasicBlock,
196 cases: impl ExactSizeIterator<Item = (u128, &'ll BasicBlock)> + TrustedLen,
198 let switch = unsafe {
199 llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, cases.len() as c_uint)
201 for (on_val, dest) in cases {
202 let on_val = self.const_uint_big(self.val_ty(v), on_val);
204 llvm::LLVMAddCase(switch, on_val, dest)
213 then: &'ll BasicBlock,
214 catch: &'ll BasicBlock,
215 funclet: Option<&Funclet<'ll>>,
218 debug!("Invoke {:?} with args ({:?})",
222 let args = self.check_call("invoke", llfn, args);
223 let bundle = funclet.map(|funclet| funclet.bundle());
224 let bundle = bundle.as_ref().map(|b| &*b.raw);
227 llvm::LLVMRustBuildInvoke(self.llbuilder,
230 args.len() as c_uint,
238 fn unreachable(&mut self) {
240 llvm::LLVMBuildUnreachable(self.llbuilder);
244 builder_methods_for_value_instructions! {
245 add(a, b) => LLVMBuildAdd,
246 fadd(a, b) => LLVMBuildFAdd,
247 sub(a, b) => LLVMBuildSub,
248 fsub(a, b) => LLVMBuildFSub,
249 mul(a, b) => LLVMBuildMul,
250 fmul(a, b) => LLVMBuildFMul,
251 udiv(a, b) => LLVMBuildUDiv,
252 exactudiv(a, b) => LLVMBuildExactUDiv,
253 sdiv(a, b) => LLVMBuildSDiv,
254 exactsdiv(a, b) => LLVMBuildExactSDiv,
255 fdiv(a, b) => LLVMBuildFDiv,
256 urem(a, b) => LLVMBuildURem,
257 srem(a, b) => LLVMBuildSRem,
258 frem(a, b) => LLVMBuildFRem,
259 shl(a, b) => LLVMBuildShl,
260 lshr(a, b) => LLVMBuildLShr,
261 ashr(a, b) => LLVMBuildAShr,
262 and(a, b) => LLVMBuildAnd,
263 or(a, b) => LLVMBuildOr,
264 xor(a, b) => LLVMBuildXor,
265 neg(x) => LLVMBuildNeg,
266 fneg(x) => LLVMBuildFNeg,
267 not(x) => LLVMBuildNot,
270 fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
272 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
273 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
278 fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
280 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
281 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
286 fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
288 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
289 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
294 fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
296 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
297 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
302 fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
304 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
305 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
316 ) -> (Self::Value, Self::Value) {
317 use syntax::ast::IntTy::*;
318 use syntax::ast::UintTy::*;
319 use rustc::ty::{Int, Uint};
321 let new_sty = match ty.sty {
322 Int(Isize) => Int(self.tcx.sess.target.isize_ty),
323 Uint(Usize) => Uint(self.tcx.sess.target.usize_ty),
324 ref t @ Uint(_) | ref t @ Int(_) => t.clone(),
325 _ => panic!("tried to get overflow intrinsic for op applied to non-int type")
328 let name = match oop {
329 OverflowOp::Add => match new_sty {
330 Int(I8) => "llvm.sadd.with.overflow.i8",
331 Int(I16) => "llvm.sadd.with.overflow.i16",
332 Int(I32) => "llvm.sadd.with.overflow.i32",
333 Int(I64) => "llvm.sadd.with.overflow.i64",
334 Int(I128) => "llvm.sadd.with.overflow.i128",
336 Uint(U8) => "llvm.uadd.with.overflow.i8",
337 Uint(U16) => "llvm.uadd.with.overflow.i16",
338 Uint(U32) => "llvm.uadd.with.overflow.i32",
339 Uint(U64) => "llvm.uadd.with.overflow.i64",
340 Uint(U128) => "llvm.uadd.with.overflow.i128",
344 OverflowOp::Sub => match new_sty {
345 Int(I8) => "llvm.ssub.with.overflow.i8",
346 Int(I16) => "llvm.ssub.with.overflow.i16",
347 Int(I32) => "llvm.ssub.with.overflow.i32",
348 Int(I64) => "llvm.ssub.with.overflow.i64",
349 Int(I128) => "llvm.ssub.with.overflow.i128",
351 Uint(U8) => "llvm.usub.with.overflow.i8",
352 Uint(U16) => "llvm.usub.with.overflow.i16",
353 Uint(U32) => "llvm.usub.with.overflow.i32",
354 Uint(U64) => "llvm.usub.with.overflow.i64",
355 Uint(U128) => "llvm.usub.with.overflow.i128",
359 OverflowOp::Mul => match new_sty {
360 Int(I8) => "llvm.smul.with.overflow.i8",
361 Int(I16) => "llvm.smul.with.overflow.i16",
362 Int(I32) => "llvm.smul.with.overflow.i32",
363 Int(I64) => "llvm.smul.with.overflow.i64",
364 Int(I128) => "llvm.smul.with.overflow.i128",
366 Uint(U8) => "llvm.umul.with.overflow.i8",
367 Uint(U16) => "llvm.umul.with.overflow.i16",
368 Uint(U32) => "llvm.umul.with.overflow.i32",
369 Uint(U64) => "llvm.umul.with.overflow.i64",
370 Uint(U128) => "llvm.umul.with.overflow.i128",
376 let intrinsic = self.get_intrinsic(&name);
377 let res = self.call(intrinsic, &[lhs, rhs], None);
379 self.extract_value(res, 0),
380 self.extract_value(res, 1),
384 fn alloca(&mut self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
385 let mut bx = Builder::with_cx(self.cx);
386 bx.position_at_start(unsafe {
387 llvm::LLVMGetFirstBasicBlock(self.llfn())
389 bx.dynamic_alloca(ty, name, align)
392 fn dynamic_alloca(&mut self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
394 let alloca = if name.is_empty() {
395 llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED)
397 let name = SmallCStr::new(name);
398 llvm::LLVMBuildAlloca(self.llbuilder, ty,
401 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
406 fn array_alloca(&mut self,
410 align: Align) -> &'ll Value {
412 let alloca = if name.is_empty() {
413 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED)
415 let name = SmallCStr::new(name);
416 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len,
419 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
424 fn load(&mut self, ptr: &'ll Value, align: Align) -> &'ll Value {
426 let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, UNNAMED);
427 llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
432 fn volatile_load(&mut self, ptr: &'ll Value) -> &'ll Value {
434 let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, UNNAMED);
435 llvm::LLVMSetVolatile(load, llvm::True);
443 order: rustc_codegen_ssa::common::AtomicOrdering,
447 let load = llvm::LLVMRustBuildAtomicLoad(
451 AtomicOrdering::from_generic(order),
453 // LLVM requires the alignment of atomic loads to be at least the size of the type.
454 llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
461 place: PlaceRef<'tcx, &'ll Value>
462 ) -> OperandRef<'tcx, &'ll Value> {
463 debug!("PlaceRef::load: {:?}", place);
465 assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
467 if place.layout.is_zst() {
468 return OperandRef::new_zst(self, place.layout);
471 fn scalar_load_metadata<'a, 'll, 'tcx>(
472 bx: &mut Builder<'a, 'll, 'tcx>,
474 scalar: &layout::Scalar
476 let vr = scalar.valid_range.clone();
479 let range = scalar.valid_range_exclusive(bx);
480 if range.start != range.end {
481 bx.range_metadata(load, range);
484 layout::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
485 bx.nonnull_metadata(load);
491 let val = if let Some(llextra) = place.llextra {
492 OperandValue::Ref(place.llval, Some(llextra), place.align)
493 } else if place.layout.is_llvm_immediate() {
494 let mut const_llval = None;
496 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
497 if llvm::LLVMIsGlobalConstant(global) == llvm::True {
498 const_llval = llvm::LLVMGetInitializer(global);
502 let llval = const_llval.unwrap_or_else(|| {
503 let load = self.load(place.llval, place.align);
504 if let layout::Abi::Scalar(ref scalar) = place.layout.abi {
505 scalar_load_metadata(self, load, scalar);
509 OperandValue::Immediate(to_immediate(self, llval, place.layout))
510 } else if let layout::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
511 let b_offset = a.value.size(self).align_to(b.value.align(self).abi);
513 let mut load = |i, scalar: &layout::Scalar, align| {
514 let llptr = self.struct_gep(place.llval, i as u64);
515 let load = self.load(llptr, align);
516 scalar_load_metadata(self, load, scalar);
517 if scalar.is_bool() {
518 self.trunc(load, self.type_i1())
525 load(0, a, place.align),
526 load(1, b, place.align.restrict_for_offset(b_offset)),
529 OperandValue::Ref(place.llval, None, place.align)
532 OperandRef { val, layout: place.layout }
535 fn write_operand_repeatedly(
537 cg_elem: OperandRef<'tcx, &'ll Value>,
539 dest: PlaceRef<'tcx, &'ll Value>,
541 let zero = self.const_usize(0);
542 let count = self.const_usize(count);
543 let start = dest.project_index(&mut self, zero).llval;
544 let end = dest.project_index(&mut self, count).llval;
546 let mut header_bx = self.build_sibling_block("repeat_loop_header");
547 let mut body_bx = self.build_sibling_block("repeat_loop_body");
548 let next_bx = self.build_sibling_block("repeat_loop_next");
550 self.br(header_bx.llbb());
551 let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
553 let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
554 header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
556 let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
557 cg_elem.val.store(&mut body_bx,
558 PlaceRef::new_sized(current, cg_elem.layout, align));
560 let next = body_bx.inbounds_gep(current, &[self.const_usize(1)]);
561 body_bx.br(header_bx.llbb());
562 header_bx.add_incoming_to_phi(current, next, body_bx.llbb());
567 fn range_metadata(&mut self, load: &'ll Value, range: Range<u128>) {
568 if self.sess().target.target.arch == "amdgpu" {
569 // amdgpu/LLVM does something weird and thinks a i64 value is
570 // split into a v2i32, halving the bitwidth LLVM expects,
571 // tripping an assertion. So, for now, just disable this
577 let llty = self.cx.val_ty(load);
579 self.cx.const_uint_big(llty, range.start),
580 self.cx.const_uint_big(llty, range.end)
583 llvm::LLVMSetMetadata(load, llvm::MD_range as c_uint,
584 llvm::LLVMMDNodeInContext(self.cx.llcx,
590 fn nonnull_metadata(&mut self, load: &'ll Value) {
592 llvm::LLVMSetMetadata(load, llvm::MD_nonnull as c_uint,
593 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
597 fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
598 self.store_with_flags(val, ptr, align, MemFlags::empty())
608 debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
609 let ptr = self.check_store(val, ptr);
611 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
612 let align = if flags.contains(MemFlags::UNALIGNED) {
615 align.bytes() as c_uint
617 llvm::LLVMSetAlignment(store, align);
618 if flags.contains(MemFlags::VOLATILE) {
619 llvm::LLVMSetVolatile(store, llvm::True);
621 if flags.contains(MemFlags::NONTEMPORAL) {
622 // According to LLVM [1] building a nontemporal store must
623 // *always* point to a metadata value of the integer 1.
625 // [1]: http://llvm.org/docs/LangRef.html#store-instruction
626 let one = self.cx.const_i32(1);
627 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
628 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
634 fn atomic_store(&mut self, val: &'ll Value, ptr: &'ll Value,
635 order: rustc_codegen_ssa::common::AtomicOrdering, size: Size) {
636 debug!("Store {:?} -> {:?}", val, ptr);
637 let ptr = self.check_store(val, ptr);
639 let store = llvm::LLVMRustBuildAtomicStore(
643 AtomicOrdering::from_generic(order),
645 // LLVM requires the alignment of atomic stores to be at least the size of the type.
646 llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
650 fn gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
652 llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
653 indices.len() as c_uint, UNNAMED)
657 fn inbounds_gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
659 llvm::LLVMBuildInBoundsGEP(
660 self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, UNNAMED)
664 fn struct_gep(&mut self, ptr: &'ll Value, idx: u64) -> &'ll Value {
665 assert_eq!(idx as c_uint as u64, idx);
667 llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, UNNAMED)
672 fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
674 llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED)
678 fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
680 llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED)
684 fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
686 llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED)
690 fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
692 llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty,UNNAMED)
696 fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
698 llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, UNNAMED)
702 fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
704 llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, UNNAMED)
708 fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
710 llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, UNNAMED)
714 fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
716 llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, UNNAMED)
720 fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
722 llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, UNNAMED)
726 fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
728 llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, UNNAMED)
732 fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
734 llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED)
739 fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
741 llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed)
745 fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
747 llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED)
752 fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
753 let op = llvm::IntPredicate::from_generic(op);
755 llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED)
759 fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
761 llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED)
765 /* Miscellaneous instructions */
766 fn memcpy(&mut self, dst: &'ll Value, dst_align: Align,
767 src: &'ll Value, src_align: Align,
768 size: &'ll Value, flags: MemFlags) {
769 if flags.contains(MemFlags::NONTEMPORAL) {
770 // HACK(nox): This is inefficient but there is no nontemporal memcpy.
771 let val = self.load(src, src_align);
772 let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
773 self.store_with_flags(val, ptr, dst_align, flags);
776 let size = self.intcast(size, self.type_isize(), false);
777 let is_volatile = flags.contains(MemFlags::VOLATILE);
778 let dst = self.pointercast(dst, self.type_i8p());
779 let src = self.pointercast(src, self.type_i8p());
781 llvm::LLVMRustBuildMemCpy(self.llbuilder, dst, dst_align.bytes() as c_uint,
782 src, src_align.bytes() as c_uint, size, is_volatile);
786 fn memmove(&mut self, dst: &'ll Value, dst_align: Align,
787 src: &'ll Value, src_align: Align,
788 size: &'ll Value, flags: MemFlags) {
789 if flags.contains(MemFlags::NONTEMPORAL) {
790 // HACK(nox): This is inefficient but there is no nontemporal memmove.
791 let val = self.load(src, src_align);
792 let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
793 self.store_with_flags(val, ptr, dst_align, flags);
796 let size = self.intcast(size, self.type_isize(), false);
797 let is_volatile = flags.contains(MemFlags::VOLATILE);
798 let dst = self.pointercast(dst, self.type_i8p());
799 let src = self.pointercast(src, self.type_i8p());
801 llvm::LLVMRustBuildMemMove(self.llbuilder, dst, dst_align.bytes() as c_uint,
802 src, src_align.bytes() as c_uint, size, is_volatile);
809 fill_byte: &'ll Value,
814 let ptr_width = &self.sess().target.target.target_pointer_width;
815 let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
816 let llintrinsicfn = self.get_intrinsic(&intrinsic_key);
817 let ptr = self.pointercast(ptr, self.type_i8p());
818 let align = self.const_u32(align.bytes() as u32);
819 let volatile = self.const_bool(flags.contains(MemFlags::VOLATILE));
820 self.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None);
824 &mut self, cond: &'ll Value,
825 then_val: &'ll Value,
826 else_val: &'ll Value,
829 llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, UNNAMED)
834 fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
836 llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED)
840 fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
842 llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED)
846 fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
848 let elt_ty = self.cx.val_ty(elt);
849 let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
850 let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
851 let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
852 self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
856 fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
857 assert_eq!(idx as c_uint as u64, idx);
859 llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED)
863 fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value,
864 idx: u64) -> &'ll Value {
865 assert_eq!(idx as c_uint as u64, idx);
867 llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
872 fn landing_pad(&mut self, ty: &'ll Type, pers_fn: &'ll Value,
873 num_clauses: usize) -> &'ll Value {
875 llvm::LLVMBuildLandingPad(self.llbuilder, ty, pers_fn,
876 num_clauses as c_uint, UNNAMED)
880 fn set_cleanup(&mut self, landing_pad: &'ll Value) {
882 llvm::LLVMSetCleanup(landing_pad, llvm::True);
886 fn resume(&mut self, exn: &'ll Value) -> &'ll Value {
888 llvm::LLVMBuildResume(self.llbuilder, exn)
892 fn cleanup_pad(&mut self,
893 parent: Option<&'ll Value>,
894 args: &[&'ll Value]) -> Funclet<'ll> {
895 let name = const_cstr!("cleanuppad");
897 llvm::LLVMRustBuildCleanupPad(self.llbuilder,
899 args.len() as c_uint,
903 Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
907 &mut self, funclet: &Funclet<'ll>,
908 unwind: Option<&'ll BasicBlock>,
911 llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind)
913 ret.expect("LLVM does not have support for cleanupret")
916 fn catch_pad(&mut self,
918 args: &[&'ll Value]) -> Funclet<'ll> {
919 let name = const_cstr!("catchpad");
921 llvm::LLVMRustBuildCatchPad(self.llbuilder, parent,
922 args.len() as c_uint, args.as_ptr(),
925 Funclet::new(ret.expect("LLVM does not have support for catchpad"))
930 parent: Option<&'ll Value>,
931 unwind: Option<&'ll BasicBlock>,
934 let name = const_cstr!("catchswitch");
936 llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind,
937 num_handlers as c_uint,
940 ret.expect("LLVM does not have support for catchswitch")
943 fn add_handler(&mut self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
945 llvm::LLVMRustAddHandler(catch_switch, handler);
949 fn set_personality_fn(&mut self, personality: &'ll Value) {
951 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
961 order: rustc_codegen_ssa::common::AtomicOrdering,
962 failure_order: rustc_codegen_ssa::common::AtomicOrdering,
965 let weak = if weak { llvm::True } else { llvm::False };
967 llvm::LLVMRustBuildAtomicCmpXchg(
972 AtomicOrdering::from_generic(order),
973 AtomicOrdering::from_generic(failure_order),
980 op: rustc_codegen_ssa::common::AtomicRmwBinOp,
983 order: rustc_codegen_ssa::common::AtomicOrdering,
986 llvm::LLVMBuildAtomicRMW(
988 AtomicRmwBinOp::from_generic(op),
991 AtomicOrdering::from_generic(order),
998 order: rustc_codegen_ssa::common::AtomicOrdering,
999 scope: rustc_codegen_ssa::common::SynchronizationScope
1002 llvm::LLVMRustBuildAtomicFence(
1004 AtomicOrdering::from_generic(order),
1005 SynchronizationScope::from_generic(scope)
1010 fn set_invariant_load(&mut self, load: &'ll Value) {
1012 llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint,
1013 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
1017 fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
1018 self.call_lifetime_intrinsic("llvm.lifetime.start", ptr, size);
1021 fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
1022 self.call_lifetime_intrinsic("llvm.lifetime.end", ptr, size);
1028 args: &[&'ll Value],
1029 funclet: Option<&Funclet<'ll>>,
1032 debug!("Call {:?} with args ({:?})",
1036 let args = self.check_call("call", llfn, args);
1037 let bundle = funclet.map(|funclet| funclet.bundle());
1038 let bundle = bundle.as_ref().map(|b| &*b.raw);
1041 llvm::LLVMRustBuildCall(
1044 args.as_ptr() as *const &llvm::Value,
1045 args.len() as c_uint,
1051 fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1053 llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED)
1058 fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
1062 unsafe fn delete_basic_block(&mut self, bb: &'ll BasicBlock) {
1063 llvm::LLVMDeleteBasicBlock(bb);
1066 fn do_not_inline(&mut self, llret: &'ll Value) {
1067 llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1071 impl StaticBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
1072 fn get_static(&mut self, def_id: DefId) -> &'ll Value {
1073 // Forward to the `get_static` method of `CodegenCx`
1074 self.cx().get_static(def_id)
1077 fn static_panic_msg(
1079 msg: Option<LocalInternedString>,
1080 filename: LocalInternedString,
1085 let align = self.tcx.data_layout.aggregate_align.abi
1086 .max(self.tcx.data_layout.i32_align.abi)
1087 .max(self.tcx.data_layout.pointer_align.abi);
1089 let filename = self.const_str_slice(filename);
1091 let with_msg_components;
1092 let without_msg_components;
1094 let components = if let Some(msg) = msg {
1095 let msg = self.const_str_slice(msg);
1096 with_msg_components = [msg, filename, line, col];
1097 &with_msg_components as &[_]
1099 without_msg_components = [filename, line, col];
1100 &without_msg_components as &[_]
1103 let struct_ = self.const_struct(&components, false);
1104 self.static_addr_of(struct_, align, Some(kind))
1108 impl Builder<'a, 'll, 'tcx> {
1109 pub fn llfn(&self) -> &'ll Value {
1111 llvm::LLVMGetBasicBlockParent(self.llbb())
1115 fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
1117 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
1121 pub fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1122 unsafe { llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs) }
1125 pub fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1126 unsafe { llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs) }
1129 pub fn insert_element(
1130 &mut self, vec: &'ll Value,
1135 llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, UNNAMED)
1139 pub fn shuffle_vector(
1146 llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, UNNAMED)
1150 pub fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1152 // FIXME: add a non-fast math version once
1153 // https://bugs.llvm.org/show_bug.cgi?id=36732
1155 let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
1156 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1160 pub fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1162 // FIXME: add a non-fast math version once
1163 // https://bugs.llvm.org/show_bug.cgi?id=36732
1165 let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
1166 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1170 pub fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
1171 unsafe { llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src) }
1173 pub fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
1174 unsafe { llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src) }
1176 pub fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
1177 unsafe { llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src) }
1179 pub fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
1180 unsafe { llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src) }
1182 pub fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
1183 unsafe { llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src) }
1185 pub fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
1186 unsafe { llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false) }
1188 pub fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
1189 unsafe { llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false) }
1191 pub fn vector_reduce_fmin_fast(&mut self, src: &'ll Value) -> &'ll Value {
1193 let instr = llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true);
1194 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1198 pub fn vector_reduce_fmax_fast(&mut self, src: &'ll Value) -> &'ll Value {
1200 let instr = llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true);
1201 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1205 pub fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1206 unsafe { llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed) }
1208 pub fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1209 unsafe { llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed) }
1212 pub fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
1214 llvm::LLVMAddClause(landing_pad, clause);
1218 pub fn catch_ret(&mut self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
1220 llvm::LLVMRustBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind)
1222 ret.expect("LLVM does not have support for catchret")
1225 fn check_store<'b>(&mut self,
1227 ptr: &'ll Value) -> &'ll Value {
1228 let dest_ptr_ty = self.cx.val_ty(ptr);
1229 let stored_ty = self.cx.val_ty(val);
1230 let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
1232 assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
1234 if dest_ptr_ty == stored_ptr_ty {
1237 debug!("Type mismatch in store. \
1238 Expected {:?}, got {:?}; inserting bitcast",
1239 dest_ptr_ty, stored_ptr_ty);
1240 self.bitcast(ptr, stored_ptr_ty)
1244 fn check_call<'b>(&mut self,
1247 args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
1248 let mut fn_ty = self.cx.val_ty(llfn);
1249 // Strip off pointers
1250 while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
1251 fn_ty = self.cx.element_type(fn_ty);
1254 assert!(self.cx.type_kind(fn_ty) == TypeKind::Function,
1255 "builder::{} not passed a function, but {:?}", typ, fn_ty);
1257 let param_tys = self.cx.func_params_types(fn_ty);
1259 let all_args_match = param_tys.iter()
1260 .zip(args.iter().map(|&v| self.val_ty(v)))
1261 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1264 return Cow::Borrowed(args);
1267 let casted_args: Vec<_> = param_tys.into_iter()
1270 .map(|(i, (expected_ty, &actual_val))| {
1271 let actual_ty = self.val_ty(actual_val);
1272 if expected_ty != actual_ty {
1273 debug!("Type mismatch in function call of {:?}. \
1274 Expected {:?} for param {}, got {:?}; injecting bitcast",
1275 llfn, expected_ty, i, actual_ty);
1276 self.bitcast(actual_val, expected_ty)
1283 Cow::Owned(casted_args)
1286 pub fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
1288 llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED)
1292 fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
1293 if self.cx.sess().opts.optimize == config::OptLevel::No {
1297 let size = size.bytes();
1302 let lifetime_intrinsic = self.cx.get_intrinsic(intrinsic);
1304 let ptr = self.pointercast(ptr, self.cx.type_i8p());
1305 self.call(lifetime_intrinsic, &[self.cx.const_u64(size), ptr], None);
1308 fn phi(&mut self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value {
1309 assert_eq!(vals.len(), bbs.len());
1311 llvm::LLVMBuildPhi(self.llbuilder, ty, UNNAMED)
1314 llvm::LLVMAddIncoming(phi, vals.as_ptr(),
1316 vals.len() as c_uint);
1321 fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
1323 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);