1 use crate::common::Funclet;
2 use crate::context::CodegenCx;
3 use crate::llvm::{self, BasicBlock, False};
4 use crate::llvm::{AtomicOrdering, AtomicRmwBinOp, SynchronizationScope};
5 use crate::type_::Type;
6 use crate::type_of::LayoutLlvmExt;
7 use crate::value::Value;
8 use libc::{c_char, c_uint};
9 use rustc_codegen_ssa::base::to_immediate;
10 use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, TypeKind};
11 use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
12 use rustc_codegen_ssa::mir::place::PlaceRef;
13 use rustc_codegen_ssa::traits::*;
14 use rustc_codegen_ssa::MemFlags;
15 use rustc_data_structures::const_cstr;
16 use rustc_data_structures::small_c_str::SmallCStr;
17 use rustc_hir::def_id::DefId;
18 use rustc_middle::ty::layout::TyAndLayout;
19 use rustc_middle::ty::{self, Ty, TyCtxt};
21 use rustc_target::abi::{self, Align, Size};
22 use rustc_target::spec::{HasTargetSpec, Target};
25 use std::iter::TrustedLen;
26 use std::ops::{Deref, Range};
30 // All Builders must have an llfn associated with them
32 pub struct Builder<'a, 'll, 'tcx> {
33 pub llbuilder: &'ll mut llvm::Builder<'ll>,
34 pub cx: &'a CodegenCx<'ll, 'tcx>,
37 impl Drop for Builder<'a, 'll, 'tcx> {
40 llvm::LLVMDisposeBuilder(&mut *(self.llbuilder as *mut _));
45 // FIXME(eddyb) use a checked constructor when they become `const fn`.
46 const EMPTY_C_STR: &CStr = unsafe { 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 Function = <CodegenCx<'ll, 'tcx> as BackendTypes>::Function;
56 type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
57 type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
58 type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;
60 type DIScope = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIScope;
61 type DIVariable = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIVariable;
64 impl abi::HasDataLayout for Builder<'_, '_, '_> {
65 fn data_layout(&self) -> &abi::TargetDataLayout {
70 impl ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
71 fn tcx(&self) -> TyCtxt<'tcx> {
76 impl ty::layout::HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
77 fn param_env(&self) -> ty::ParamEnv<'tcx> {
82 impl HasTargetSpec for Builder<'_, '_, 'tcx> {
83 fn target_spec(&self) -> &Target {
84 &self.cx.target_spec()
88 impl abi::LayoutOf for Builder<'_, '_, 'tcx> {
90 type TyAndLayout = TyAndLayout<'tcx>;
92 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
97 impl Deref for Builder<'_, 'll, 'tcx> {
98 type Target = CodegenCx<'ll, 'tcx>;
100 fn deref(&self) -> &Self::Target {
105 impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
106 type CodegenCx = CodegenCx<'ll, 'tcx>;
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 {
113 llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED)
119 impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
120 fn new_block<'b>(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &'b str) -> Self {
121 let mut bx = Builder::with_cx(cx);
123 let name = SmallCStr::new(name);
124 llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name.as_ptr())
126 bx.position_at_end(llbb);
130 fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
131 // Create a fresh builder from the crate context.
132 let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(cx.llcx) };
133 Builder { llbuilder, cx }
136 fn build_sibling_block(&self, name: &str) -> Self {
137 Builder::new_block(self.cx, self.llfn(), name)
140 fn llbb(&self) -> &'ll BasicBlock {
141 unsafe { llvm::LLVMGetInsertBlock(self.llbuilder) }
144 fn position_at_end(&mut self, llbb: &'ll BasicBlock) {
146 llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
150 fn ret_void(&mut self) {
152 llvm::LLVMBuildRetVoid(self.llbuilder);
156 fn ret(&mut self, v: &'ll Value) {
158 llvm::LLVMBuildRet(self.llbuilder, v);
162 fn br(&mut self, dest: &'ll BasicBlock) {
164 llvm::LLVMBuildBr(self.llbuilder, dest);
171 then_llbb: &'ll BasicBlock,
172 else_llbb: &'ll BasicBlock,
175 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
182 else_llbb: &'ll BasicBlock,
183 cases: impl ExactSizeIterator<Item = (u128, &'ll BasicBlock)> + TrustedLen,
186 unsafe { llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, cases.len() as c_uint) };
187 for (on_val, dest) in cases {
188 let on_val = self.const_uint_big(self.val_ty(v), on_val);
189 unsafe { llvm::LLVMAddCase(switch, on_val, dest) }
197 then: &'ll BasicBlock,
198 catch: &'ll BasicBlock,
199 funclet: Option<&Funclet<'ll>>,
201 debug!("invoke {:?} with args ({:?})", llfn, args);
203 let args = self.check_call("invoke", llfn, args);
204 let bundle = funclet.map(|funclet| funclet.bundle());
205 let bundle = bundle.as_ref().map(|b| &*b.raw);
208 llvm::LLVMRustBuildInvoke(
212 args.len() as c_uint,
221 fn unreachable(&mut self) {
223 llvm::LLVMBuildUnreachable(self.llbuilder);
227 builder_methods_for_value_instructions! {
228 add(a, b) => LLVMBuildAdd,
229 fadd(a, b) => LLVMBuildFAdd,
230 sub(a, b) => LLVMBuildSub,
231 fsub(a, b) => LLVMBuildFSub,
232 mul(a, b) => LLVMBuildMul,
233 fmul(a, b) => LLVMBuildFMul,
234 udiv(a, b) => LLVMBuildUDiv,
235 exactudiv(a, b) => LLVMBuildExactUDiv,
236 sdiv(a, b) => LLVMBuildSDiv,
237 exactsdiv(a, b) => LLVMBuildExactSDiv,
238 fdiv(a, b) => LLVMBuildFDiv,
239 urem(a, b) => LLVMBuildURem,
240 srem(a, b) => LLVMBuildSRem,
241 frem(a, b) => LLVMBuildFRem,
242 shl(a, b) => LLVMBuildShl,
243 lshr(a, b) => LLVMBuildLShr,
244 ashr(a, b) => LLVMBuildAShr,
245 and(a, b) => LLVMBuildAnd,
246 or(a, b) => LLVMBuildOr,
247 xor(a, b) => LLVMBuildXor,
248 neg(x) => LLVMBuildNeg,
249 fneg(x) => LLVMBuildFNeg,
250 not(x) => LLVMBuildNot,
251 unchecked_sadd(x, y) => LLVMBuildNSWAdd,
252 unchecked_uadd(x, y) => LLVMBuildNUWAdd,
253 unchecked_ssub(x, y) => LLVMBuildNSWSub,
254 unchecked_usub(x, y) => LLVMBuildNUWSub,
255 unchecked_smul(x, y) => LLVMBuildNSWMul,
256 unchecked_umul(x, y) => LLVMBuildNUWMul,
259 fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
261 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
262 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
267 fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
269 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
270 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
275 fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
277 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
278 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
283 fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
285 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
286 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
291 fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
293 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
294 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
305 ) -> (Self::Value, Self::Value) {
306 use rustc_ast::ast::IntTy::*;
307 use rustc_ast::ast::UintTy::*;
308 use rustc_middle::ty::{Int, Uint};
310 let new_kind = match ty.kind {
311 Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.ptr_width)),
312 Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.ptr_width)),
313 ref t @ (Uint(_) | Int(_)) => t.clone(),
314 _ => panic!("tried to get overflow intrinsic for op applied to non-int type"),
317 let name = match oop {
318 OverflowOp::Add => match new_kind {
319 Int(I8) => "llvm.sadd.with.overflow.i8",
320 Int(I16) => "llvm.sadd.with.overflow.i16",
321 Int(I32) => "llvm.sadd.with.overflow.i32",
322 Int(I64) => "llvm.sadd.with.overflow.i64",
323 Int(I128) => "llvm.sadd.with.overflow.i128",
325 Uint(U8) => "llvm.uadd.with.overflow.i8",
326 Uint(U16) => "llvm.uadd.with.overflow.i16",
327 Uint(U32) => "llvm.uadd.with.overflow.i32",
328 Uint(U64) => "llvm.uadd.with.overflow.i64",
329 Uint(U128) => "llvm.uadd.with.overflow.i128",
333 OverflowOp::Sub => match new_kind {
334 Int(I8) => "llvm.ssub.with.overflow.i8",
335 Int(I16) => "llvm.ssub.with.overflow.i16",
336 Int(I32) => "llvm.ssub.with.overflow.i32",
337 Int(I64) => "llvm.ssub.with.overflow.i64",
338 Int(I128) => "llvm.ssub.with.overflow.i128",
340 Uint(U8) => "llvm.usub.with.overflow.i8",
341 Uint(U16) => "llvm.usub.with.overflow.i16",
342 Uint(U32) => "llvm.usub.with.overflow.i32",
343 Uint(U64) => "llvm.usub.with.overflow.i64",
344 Uint(U128) => "llvm.usub.with.overflow.i128",
348 OverflowOp::Mul => match new_kind {
349 Int(I8) => "llvm.smul.with.overflow.i8",
350 Int(I16) => "llvm.smul.with.overflow.i16",
351 Int(I32) => "llvm.smul.with.overflow.i32",
352 Int(I64) => "llvm.smul.with.overflow.i64",
353 Int(I128) => "llvm.smul.with.overflow.i128",
355 Uint(U8) => "llvm.umul.with.overflow.i8",
356 Uint(U16) => "llvm.umul.with.overflow.i16",
357 Uint(U32) => "llvm.umul.with.overflow.i32",
358 Uint(U64) => "llvm.umul.with.overflow.i64",
359 Uint(U128) => "llvm.umul.with.overflow.i128",
365 let intrinsic = self.get_intrinsic(&name);
366 let res = self.call(intrinsic, &[lhs, rhs], None);
367 (self.extract_value(res, 0), self.extract_value(res, 1))
370 fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
371 let mut bx = Builder::with_cx(self.cx);
372 bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
373 bx.dynamic_alloca(ty, align)
376 fn dynamic_alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
378 let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
379 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
384 fn array_alloca(&mut self, ty: &'ll Type, len: &'ll Value, align: Align) -> &'ll Value {
386 let alloca = llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED);
387 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
392 fn load(&mut self, ptr: &'ll Value, align: Align) -> &'ll Value {
394 let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, UNNAMED);
395 llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
400 fn volatile_load(&mut self, ptr: &'ll Value) -> &'ll Value {
402 let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, UNNAMED);
403 llvm::LLVMSetVolatile(load, llvm::True);
411 order: rustc_codegen_ssa::common::AtomicOrdering,
415 let load = llvm::LLVMRustBuildAtomicLoad(
419 AtomicOrdering::from_generic(order),
421 // LLVM requires the alignment of atomic loads to be at least the size of the type.
422 llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
427 fn load_operand(&mut self, place: PlaceRef<'tcx, &'ll Value>) -> OperandRef<'tcx, &'ll Value> {
428 debug!("PlaceRef::load: {:?}", place);
430 assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
432 if place.layout.is_zst() {
433 return OperandRef::new_zst(self, place.layout);
436 fn scalar_load_metadata<'a, 'll, 'tcx>(
437 bx: &mut Builder<'a, 'll, 'tcx>,
439 scalar: &abi::Scalar,
441 let vr = scalar.valid_range.clone();
444 let range = scalar.valid_range_exclusive(bx);
445 if range.start != range.end {
446 bx.range_metadata(load, range);
449 abi::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
450 bx.nonnull_metadata(load);
456 let val = if let Some(llextra) = place.llextra {
457 OperandValue::Ref(place.llval, Some(llextra), place.align)
458 } else if place.layout.is_llvm_immediate() {
459 let mut const_llval = None;
461 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
462 if llvm::LLVMIsGlobalConstant(global) == llvm::True {
463 const_llval = llvm::LLVMGetInitializer(global);
467 let llval = const_llval.unwrap_or_else(|| {
468 let load = self.load(place.llval, place.align);
469 if let abi::Abi::Scalar(ref scalar) = place.layout.abi {
470 scalar_load_metadata(self, load, scalar);
474 OperandValue::Immediate(to_immediate(self, llval, place.layout))
475 } else if let abi::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
476 let b_offset = a.value.size(self).align_to(b.value.align(self).abi);
478 let mut load = |i, scalar: &abi::Scalar, align| {
479 let llptr = self.struct_gep(place.llval, i as u64);
480 let load = self.load(llptr, align);
481 scalar_load_metadata(self, load, scalar);
482 if scalar.is_bool() { self.trunc(load, self.type_i1()) } else { load }
486 load(0, a, place.align),
487 load(1, b, place.align.restrict_for_offset(b_offset)),
490 OperandValue::Ref(place.llval, None, place.align)
493 OperandRef { val, layout: place.layout }
496 fn write_operand_repeatedly(
498 cg_elem: OperandRef<'tcx, &'ll Value>,
500 dest: PlaceRef<'tcx, &'ll Value>,
502 let zero = self.const_usize(0);
503 let count = self.const_usize(count);
504 let start = dest.project_index(&mut self, zero).llval;
505 let end = dest.project_index(&mut self, count).llval;
507 let mut header_bx = self.build_sibling_block("repeat_loop_header");
508 let mut body_bx = self.build_sibling_block("repeat_loop_body");
509 let next_bx = self.build_sibling_block("repeat_loop_next");
511 self.br(header_bx.llbb());
512 let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
514 let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
515 header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
517 let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
520 .store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
522 let next = body_bx.inbounds_gep(current, &[self.const_usize(1)]);
523 body_bx.br(header_bx.llbb());
524 header_bx.add_incoming_to_phi(current, next, body_bx.llbb());
529 fn range_metadata(&mut self, load: &'ll Value, range: Range<u128>) {
530 if self.sess().target.target.arch == "amdgpu" {
531 // amdgpu/LLVM does something weird and thinks a i64 value is
532 // split into a v2i32, halving the bitwidth LLVM expects,
533 // tripping an assertion. So, for now, just disable this
539 let llty = self.cx.val_ty(load);
541 self.cx.const_uint_big(llty, range.start),
542 self.cx.const_uint_big(llty, range.end),
545 llvm::LLVMSetMetadata(
547 llvm::MD_range as c_uint,
548 llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
553 fn nonnull_metadata(&mut self, load: &'ll Value) {
555 llvm::LLVMSetMetadata(
557 llvm::MD_nonnull as c_uint,
558 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
563 fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
564 self.store_with_flags(val, ptr, align, MemFlags::empty())
574 debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
575 let ptr = self.check_store(val, ptr);
577 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
579 if flags.contains(MemFlags::UNALIGNED) { 1 } else { align.bytes() as c_uint };
580 llvm::LLVMSetAlignment(store, align);
581 if flags.contains(MemFlags::VOLATILE) {
582 llvm::LLVMSetVolatile(store, llvm::True);
584 if flags.contains(MemFlags::NONTEMPORAL) {
585 // According to LLVM [1] building a nontemporal store must
586 // *always* point to a metadata value of the integer 1.
588 // [1]: http://llvm.org/docs/LangRef.html#store-instruction
589 let one = self.cx.const_i32(1);
590 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
591 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
601 order: rustc_codegen_ssa::common::AtomicOrdering,
604 debug!("Store {:?} -> {:?}", val, ptr);
605 let ptr = self.check_store(val, ptr);
607 let store = llvm::LLVMRustBuildAtomicStore(
611 AtomicOrdering::from_generic(order),
613 // LLVM requires the alignment of atomic stores to be at least the size of the type.
614 llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
618 fn gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
624 indices.len() as c_uint,
630 fn inbounds_gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
632 llvm::LLVMBuildInBoundsGEP(
636 indices.len() as c_uint,
642 fn struct_gep(&mut self, ptr: &'ll Value, idx: u64) -> &'ll Value {
643 assert_eq!(idx as c_uint as u64, idx);
644 unsafe { llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, UNNAMED) }
648 fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
649 unsafe { llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
652 fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
653 unsafe { llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED) }
656 fn fptoui_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
657 // WebAssembly has saturating floating point to integer casts if the
658 // `nontrapping-fptoint` target feature is activated. We'll use those if
659 // they are available.
660 if self.sess().target.target.arch == "wasm32"
661 && self.sess().target_features.contains(&sym::nontrapping_dash_fptoint)
663 let src_ty = self.cx.val_ty(val);
664 let float_width = self.cx.float_width(src_ty);
665 let int_width = self.cx.int_width(dest_ty);
666 let name = match (int_width, float_width) {
667 (32, 32) => Some("llvm.wasm.trunc.saturate.unsigned.i32.f32"),
668 (32, 64) => Some("llvm.wasm.trunc.saturate.unsigned.i32.f64"),
669 (64, 32) => Some("llvm.wasm.trunc.saturate.unsigned.i64.f32"),
670 (64, 64) => Some("llvm.wasm.trunc.saturate.unsigned.i64.f64"),
673 if let Some(name) = name {
674 let intrinsic = self.get_intrinsic(name);
675 return Some(self.call(intrinsic, &[val], None));
681 fn fptosi_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
682 // WebAssembly has saturating floating point to integer casts if the
683 // `nontrapping-fptoint` target feature is activated. We'll use those if
684 // they are available.
685 if self.sess().target.target.arch == "wasm32"
686 && self.sess().target_features.contains(&sym::nontrapping_dash_fptoint)
688 let src_ty = self.cx.val_ty(val);
689 let float_width = self.cx.float_width(src_ty);
690 let int_width = self.cx.int_width(dest_ty);
691 let name = match (int_width, float_width) {
692 (32, 32) => Some("llvm.wasm.trunc.saturate.signed.i32.f32"),
693 (32, 64) => Some("llvm.wasm.trunc.saturate.signed.i32.f64"),
694 (64, 32) => Some("llvm.wasm.trunc.saturate.signed.i64.f32"),
695 (64, 64) => Some("llvm.wasm.trunc.saturate.signed.i64.f64"),
698 if let Some(name) = name {
699 let intrinsic = self.get_intrinsic(name);
700 return Some(self.call(intrinsic, &[val], None));
706 fn fptosui_may_trap(&self, val: &'ll Value, dest_ty: &'ll Type) -> bool {
707 // Most of the time we'll be generating the `fptosi` or `fptoui`
708 // instruction for floating-point-to-integer conversions. These
709 // instructions by definition in LLVM do not trap. For the WebAssembly
710 // target, however, we'll lower in some cases to intrinsic calls instead
711 // which may trap. If we detect that this is a situation where we'll be
712 // using the intrinsics then we report that the call map trap, which
713 // callers might need to handle.
714 if !self.wasm_and_missing_nontrapping_fptoint() {
717 let src_ty = self.cx.val_ty(val);
718 let float_width = self.cx.float_width(src_ty);
719 let int_width = self.cx.int_width(dest_ty);
720 match (int_width, float_width) {
721 (32, 32) | (32, 64) | (64, 32) | (64, 64) => true,
726 fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
727 // When we can, use the native wasm intrinsics which have tighter
728 // codegen. Note that this has a semantic difference in that the
729 // intrinsic can trap whereas `fptoui` never traps. That difference,
730 // however, is handled by `fptosui_may_trap` above.
731 if self.wasm_and_missing_nontrapping_fptoint() {
732 let src_ty = self.cx.val_ty(val);
733 let float_width = self.cx.float_width(src_ty);
734 let int_width = self.cx.int_width(dest_ty);
735 let name = match (int_width, float_width) {
736 (32, 32) => Some("llvm.wasm.trunc.unsigned.i32.f32"),
737 (32, 64) => Some("llvm.wasm.trunc.unsigned.i32.f64"),
738 (64, 32) => Some("llvm.wasm.trunc.unsigned.i64.f32"),
739 (64, 64) => Some("llvm.wasm.trunc.unsigned.i64.f64"),
742 if let Some(name) = name {
743 let intrinsic = self.get_intrinsic(name);
744 return self.call(intrinsic, &[val], None);
747 unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) }
750 fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
751 if self.wasm_and_missing_nontrapping_fptoint() {
752 let src_ty = self.cx.val_ty(val);
753 let float_width = self.cx.float_width(src_ty);
754 let int_width = self.cx.int_width(dest_ty);
755 let name = match (int_width, float_width) {
756 (32, 32) => Some("llvm.wasm.trunc.signed.i32.f32"),
757 (32, 64) => Some("llvm.wasm.trunc.signed.i32.f64"),
758 (64, 32) => Some("llvm.wasm.trunc.signed.i64.f32"),
759 (64, 64) => Some("llvm.wasm.trunc.signed.i64.f64"),
762 if let Some(name) = name {
763 let intrinsic = self.get_intrinsic(name);
764 return self.call(intrinsic, &[val], None);
767 unsafe { llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty, UNNAMED) }
770 fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
771 unsafe { llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
774 fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
775 unsafe { llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
778 fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
779 unsafe { llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
782 fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
783 unsafe { llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, UNNAMED) }
786 fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
787 unsafe { llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, UNNAMED) }
790 fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
791 unsafe { llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, UNNAMED) }
794 fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
795 unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
798 fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
799 unsafe { llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed) }
802 fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
803 unsafe { llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED) }
807 fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
808 let op = llvm::IntPredicate::from_generic(op);
809 unsafe { llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
812 fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
813 unsafe { llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
816 /* Miscellaneous instructions */
826 if flags.contains(MemFlags::NONTEMPORAL) {
827 // HACK(nox): This is inefficient but there is no nontemporal memcpy.
828 let val = self.load(src, src_align);
829 let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
830 self.store_with_flags(val, ptr, dst_align, flags);
833 let size = self.intcast(size, self.type_isize(), false);
834 let is_volatile = flags.contains(MemFlags::VOLATILE);
835 let dst = self.pointercast(dst, self.type_i8p());
836 let src = self.pointercast(src, self.type_i8p());
838 llvm::LLVMRustBuildMemCpy(
841 dst_align.bytes() as c_uint,
843 src_align.bytes() as c_uint,
859 if flags.contains(MemFlags::NONTEMPORAL) {
860 // HACK(nox): This is inefficient but there is no nontemporal memmove.
861 let val = self.load(src, src_align);
862 let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
863 self.store_with_flags(val, ptr, dst_align, flags);
866 let size = self.intcast(size, self.type_isize(), false);
867 let is_volatile = flags.contains(MemFlags::VOLATILE);
868 let dst = self.pointercast(dst, self.type_i8p());
869 let src = self.pointercast(src, self.type_i8p());
871 llvm::LLVMRustBuildMemMove(
874 dst_align.bytes() as c_uint,
876 src_align.bytes() as c_uint,
886 fill_byte: &'ll Value,
891 let is_volatile = flags.contains(MemFlags::VOLATILE);
892 let ptr = self.pointercast(ptr, self.type_i8p());
894 llvm::LLVMRustBuildMemSet(
897 align.bytes() as c_uint,
908 then_val: &'ll Value,
909 else_val: &'ll Value,
911 unsafe { llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, UNNAMED) }
915 fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
916 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
919 fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
920 unsafe { llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED) }
923 fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
925 let elt_ty = self.cx.val_ty(elt);
926 let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
927 let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
928 let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
929 self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
933 fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
934 assert_eq!(idx as c_uint as u64, idx);
935 unsafe { llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED) }
938 fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value, idx: u64) -> &'ll Value {
939 assert_eq!(idx as c_uint as u64, idx);
940 unsafe { llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint, UNNAMED) }
950 llvm::LLVMBuildLandingPad(self.llbuilder, ty, pers_fn, num_clauses as c_uint, UNNAMED)
954 fn set_cleanup(&mut self, landing_pad: &'ll Value) {
956 llvm::LLVMSetCleanup(landing_pad, llvm::True);
960 fn resume(&mut self, exn: &'ll Value) -> &'ll Value {
961 unsafe { llvm::LLVMBuildResume(self.llbuilder, exn) }
964 fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
965 let name = const_cstr!("cleanuppad");
967 llvm::LLVMRustBuildCleanupPad(
970 args.len() as c_uint,
975 Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
980 funclet: &Funclet<'ll>,
981 unwind: Option<&'ll BasicBlock>,
984 unsafe { llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind) };
985 ret.expect("LLVM does not have support for cleanupret")
988 fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
989 let name = const_cstr!("catchpad");
991 llvm::LLVMRustBuildCatchPad(
994 args.len() as c_uint,
999 Funclet::new(ret.expect("LLVM does not have support for catchpad"))
1004 parent: Option<&'ll Value>,
1005 unwind: Option<&'ll BasicBlock>,
1006 num_handlers: usize,
1008 let name = const_cstr!("catchswitch");
1010 llvm::LLVMRustBuildCatchSwitch(
1014 num_handlers as c_uint,
1018 ret.expect("LLVM does not have support for catchswitch")
1021 fn add_handler(&mut self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
1023 llvm::LLVMRustAddHandler(catch_switch, handler);
1027 fn set_personality_fn(&mut self, personality: &'ll Value) {
1029 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
1033 // Atomic Operations
1039 order: rustc_codegen_ssa::common::AtomicOrdering,
1040 failure_order: rustc_codegen_ssa::common::AtomicOrdering,
1043 let weak = if weak { llvm::True } else { llvm::False };
1045 llvm::LLVMRustBuildAtomicCmpXchg(
1050 AtomicOrdering::from_generic(order),
1051 AtomicOrdering::from_generic(failure_order),
1058 op: rustc_codegen_ssa::common::AtomicRmwBinOp,
1061 order: rustc_codegen_ssa::common::AtomicOrdering,
1064 llvm::LLVMBuildAtomicRMW(
1066 AtomicRmwBinOp::from_generic(op),
1069 AtomicOrdering::from_generic(order),
1077 order: rustc_codegen_ssa::common::AtomicOrdering,
1078 scope: rustc_codegen_ssa::common::SynchronizationScope,
1081 llvm::LLVMRustBuildAtomicFence(
1083 AtomicOrdering::from_generic(order),
1084 SynchronizationScope::from_generic(scope),
1089 fn set_invariant_load(&mut self, load: &'ll Value) {
1091 llvm::LLVMSetMetadata(
1093 llvm::MD_invariant_load as c_uint,
1094 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1099 fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
1100 self.call_lifetime_intrinsic("llvm.lifetime.start.p0i8", ptr, size);
1103 fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
1104 self.call_lifetime_intrinsic("llvm.lifetime.end.p0i8", ptr, size);
1107 fn instrprof_increment(
1109 fn_name: &'ll Value,
1111 num_counters: &'ll Value,
1115 "instrprof_increment() with args ({:?}, {:?}, {:?}, {:?})",
1116 fn_name, hash, num_counters, index
1119 let llfn = unsafe { llvm::LLVMRustGetInstrProfIncrementIntrinsic(self.cx().llmod) };
1120 let args = &[fn_name, hash, num_counters, index];
1121 let args = self.check_call("call", llfn, args);
1124 llvm::LLVMRustBuildCall(
1127 args.as_ptr() as *const &llvm::Value,
1128 args.len() as c_uint,
1137 args: &[&'ll Value],
1138 funclet: Option<&Funclet<'ll>>,
1140 debug!("call {:?} with args ({:?})", llfn, args);
1142 let args = self.check_call("call", llfn, args);
1143 let bundle = funclet.map(|funclet| funclet.bundle());
1144 let bundle = bundle.as_ref().map(|b| &*b.raw);
1147 llvm::LLVMRustBuildCall(
1150 args.as_ptr() as *const &llvm::Value,
1151 args.len() as c_uint,
1157 fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1158 unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
1161 fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
1165 unsafe fn delete_basic_block(&mut self, bb: &'ll BasicBlock) {
1166 llvm::LLVMDeleteBasicBlock(bb);
1169 fn do_not_inline(&mut self, llret: &'ll Value) {
1170 llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1174 impl StaticBuilderMethods for Builder<'a, 'll, 'tcx> {
1175 fn get_static(&mut self, def_id: DefId) -> &'ll Value {
1176 // Forward to the `get_static` method of `CodegenCx`
1177 self.cx().get_static(def_id)
1181 impl Builder<'a, 'll, 'tcx> {
1182 pub fn llfn(&self) -> &'ll Value {
1183 unsafe { llvm::LLVMGetBasicBlockParent(self.llbb()) }
1186 fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
1188 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
1192 pub fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1193 unsafe { llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs) }
1196 pub fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1197 unsafe { llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs) }
1200 pub fn insert_element(
1206 unsafe { llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, UNNAMED) }
1209 pub fn shuffle_vector(
1215 unsafe { llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, UNNAMED) }
1218 pub fn vector_reduce_fadd(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1219 unsafe { llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src) }
1221 pub fn vector_reduce_fmul(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1222 unsafe { llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src) }
1224 pub fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1226 let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
1227 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1231 pub fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1233 let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
1234 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1238 pub fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
1239 unsafe { llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src) }
1241 pub fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
1242 unsafe { llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src) }
1244 pub fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
1245 unsafe { llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src) }
1247 pub fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
1248 unsafe { llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src) }
1250 pub fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
1251 unsafe { llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src) }
1253 pub fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
1255 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false)
1258 pub fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
1260 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false)
1263 pub fn vector_reduce_fmin_fast(&mut self, src: &'ll Value) -> &'ll Value {
1266 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true);
1267 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1271 pub fn vector_reduce_fmax_fast(&mut self, src: &'ll Value) -> &'ll Value {
1274 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true);
1275 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
1279 pub fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1280 unsafe { llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed) }
1282 pub fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1283 unsafe { llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed) }
1286 pub fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
1288 llvm::LLVMAddClause(landing_pad, clause);
1292 pub fn catch_ret(&mut self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
1294 unsafe { llvm::LLVMRustBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind) };
1295 ret.expect("LLVM does not have support for catchret")
1298 fn check_store(&mut self, val: &'ll Value, ptr: &'ll Value) -> &'ll Value {
1299 let dest_ptr_ty = self.cx.val_ty(ptr);
1300 let stored_ty = self.cx.val_ty(val);
1301 let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
1303 assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
1305 if dest_ptr_ty == stored_ptr_ty {
1309 "type mismatch in store. \
1310 Expected {:?}, got {:?}; inserting bitcast",
1311 dest_ptr_ty, stored_ptr_ty
1313 self.bitcast(ptr, stored_ptr_ty)
1321 args: &'b [&'ll Value],
1322 ) -> Cow<'b, [&'ll Value]> {
1323 let mut fn_ty = self.cx.val_ty(llfn);
1324 // Strip off pointers
1325 while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
1326 fn_ty = self.cx.element_type(fn_ty);
1330 self.cx.type_kind(fn_ty) == TypeKind::Function,
1331 "builder::{} not passed a function, but {:?}",
1336 let param_tys = self.cx.func_params_types(fn_ty);
1338 let all_args_match = param_tys
1340 .zip(args.iter().map(|&v| self.val_ty(v)))
1341 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1344 return Cow::Borrowed(args);
1347 let casted_args: Vec<_> = param_tys
1351 .map(|(i, (expected_ty, &actual_val))| {
1352 let actual_ty = self.val_ty(actual_val);
1353 if expected_ty != actual_ty {
1355 "type mismatch in function call of {:?}. \
1356 Expected {:?} for param {}, got {:?}; injecting bitcast",
1357 llfn, expected_ty, i, actual_ty
1359 self.bitcast(actual_val, expected_ty)
1366 Cow::Owned(casted_args)
1369 pub fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
1370 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
1373 fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
1374 let size = size.bytes();
1379 if !self.cx().sess().emit_lifetime_markers() {
1383 let lifetime_intrinsic = self.cx.get_intrinsic(intrinsic);
1385 let ptr = self.pointercast(ptr, self.cx.type_i8p());
1386 self.call(lifetime_intrinsic, &[self.cx.const_u64(size), ptr], None);
1392 vals: &[&'ll Value],
1393 bbs: &[&'ll BasicBlock],
1395 assert_eq!(vals.len(), bbs.len());
1396 let phi = unsafe { llvm::LLVMBuildPhi(self.llbuilder, ty, UNNAMED) };
1398 llvm::LLVMAddIncoming(phi, vals.as_ptr(), bbs.as_ptr(), vals.len() as c_uint);
1403 fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
1405 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1409 fn wasm_and_missing_nontrapping_fptoint(&self) -> bool {
1410 self.sess().target.target.arch == "wasm32"
1411 && !self.sess().target_features.contains(&sym::nontrapping_dash_fptoint)