2 use crate::common::Funclet;
3 use crate::context::CodegenCx;
4 use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock};
5 use crate::type_::Type;
6 use crate::type_of::LayoutLlvmExt;
7 use crate::value::Value;
9 use libc::{c_char, c_uint};
10 use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, 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::small_c_str::SmallCStr;
16 use rustc_hir::def_id::DefId;
17 use rustc_middle::ty::layout::{
18 FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
20 use rustc_middle::ty::{self, Ty, TyCtxt};
22 use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
23 use rustc_target::spec::{HasTargetSpec, Target};
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<'_, '_, '_> {
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<'ll, 'tcx> 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 DILocation = <CodegenCx<'ll, 'tcx> as BackendTypes>::DILocation;
62 type DIVariable = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIVariable;
65 impl abi::HasDataLayout for Builder<'_, '_, '_> {
66 fn data_layout(&self) -> &abi::TargetDataLayout {
71 impl<'tcx> ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
73 fn tcx(&self) -> TyCtxt<'tcx> {
78 impl<'tcx> ty::layout::HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
79 fn param_env(&self) -> ty::ParamEnv<'tcx> {
84 impl HasTargetSpec for Builder<'_, '_, '_> {
86 fn target_spec(&self) -> &Target {
91 impl<'tcx> LayoutOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
92 type LayoutOfResult = TyAndLayout<'tcx>;
95 fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
96 self.cx.handle_layout_err(err, span, ty)
100 impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
101 type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
104 fn handle_fn_abi_err(
106 err: FnAbiError<'tcx>,
108 fn_abi_request: FnAbiRequest<'tcx>,
110 self.cx.handle_fn_abi_err(err, span, fn_abi_request)
114 impl<'ll, 'tcx> Deref for Builder<'_, 'll, 'tcx> {
115 type Target = CodegenCx<'ll, 'tcx>;
118 fn deref(&self) -> &Self::Target {
123 impl<'ll, 'tcx> HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
124 type CodegenCx = CodegenCx<'ll, 'tcx>;
127 macro_rules! builder_methods_for_value_instructions {
128 ($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
129 $(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
131 llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED)
137 impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
138 fn build(cx: &'a CodegenCx<'ll, 'tcx>, llbb: &'ll BasicBlock) -> Self {
139 let bx = Builder::with_cx(cx);
141 llvm::LLVMPositionBuilderAtEnd(bx.llbuilder, llbb);
146 fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
150 fn llbb(&self) -> &'ll BasicBlock {
151 unsafe { llvm::LLVMGetInsertBlock(self.llbuilder) }
154 fn set_span(&mut self, _span: Span) {}
156 fn append_block(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &str) -> &'ll BasicBlock {
158 let name = SmallCStr::new(name);
159 llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name.as_ptr())
163 fn append_sibling_block(&mut self, name: &str) -> &'ll BasicBlock {
164 Self::append_block(self.cx, self.llfn(), name)
167 fn switch_to_block(&mut self, llbb: Self::BasicBlock) {
168 *self = Self::build(self.cx, llbb)
171 fn ret_void(&mut self) {
173 llvm::LLVMBuildRetVoid(self.llbuilder);
177 fn ret(&mut self, v: &'ll Value) {
179 llvm::LLVMBuildRet(self.llbuilder, v);
183 fn br(&mut self, dest: &'ll BasicBlock) {
185 llvm::LLVMBuildBr(self.llbuilder, dest);
192 then_llbb: &'ll BasicBlock,
193 else_llbb: &'ll BasicBlock,
196 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
203 else_llbb: &'ll BasicBlock,
204 cases: impl ExactSizeIterator<Item = (u128, &'ll BasicBlock)>,
207 unsafe { llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, cases.len() as c_uint) };
208 for (on_val, dest) in cases {
209 let on_val = self.const_uint_big(self.val_ty(v), on_val);
210 unsafe { llvm::LLVMAddCase(switch, on_val, dest) }
219 then: &'ll BasicBlock,
220 catch: &'ll BasicBlock,
221 funclet: Option<&Funclet<'ll>>,
223 debug!("invoke {:?} with args ({:?})", llfn, args);
225 let args = self.check_call("invoke", llty, llfn, args);
226 let bundle = funclet.map(|funclet| funclet.bundle());
227 let bundle = bundle.as_ref().map(|b| &*b.raw);
230 llvm::LLVMRustBuildInvoke(
235 args.len() as c_uint,
244 fn unreachable(&mut self) {
246 llvm::LLVMBuildUnreachable(self.llbuilder);
250 builder_methods_for_value_instructions! {
251 add(a, b) => LLVMBuildAdd,
252 fadd(a, b) => LLVMBuildFAdd,
253 sub(a, b) => LLVMBuildSub,
254 fsub(a, b) => LLVMBuildFSub,
255 mul(a, b) => LLVMBuildMul,
256 fmul(a, b) => LLVMBuildFMul,
257 udiv(a, b) => LLVMBuildUDiv,
258 exactudiv(a, b) => LLVMBuildExactUDiv,
259 sdiv(a, b) => LLVMBuildSDiv,
260 exactsdiv(a, b) => LLVMBuildExactSDiv,
261 fdiv(a, b) => LLVMBuildFDiv,
262 urem(a, b) => LLVMBuildURem,
263 srem(a, b) => LLVMBuildSRem,
264 frem(a, b) => LLVMBuildFRem,
265 shl(a, b) => LLVMBuildShl,
266 lshr(a, b) => LLVMBuildLShr,
267 ashr(a, b) => LLVMBuildAShr,
268 and(a, b) => LLVMBuildAnd,
269 or(a, b) => LLVMBuildOr,
270 xor(a, b) => LLVMBuildXor,
271 neg(x) => LLVMBuildNeg,
272 fneg(x) => LLVMBuildFNeg,
273 not(x) => LLVMBuildNot,
274 unchecked_sadd(x, y) => LLVMBuildNSWAdd,
275 unchecked_uadd(x, y) => LLVMBuildNUWAdd,
276 unchecked_ssub(x, y) => LLVMBuildNSWSub,
277 unchecked_usub(x, y) => LLVMBuildNUWSub,
278 unchecked_smul(x, y) => LLVMBuildNSWMul,
279 unchecked_umul(x, y) => LLVMBuildNUWMul,
282 fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
284 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
285 llvm::LLVMRustSetFastMath(instr);
290 fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
292 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
293 llvm::LLVMRustSetFastMath(instr);
298 fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
300 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
301 llvm::LLVMRustSetFastMath(instr);
306 fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
308 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
309 llvm::LLVMRustSetFastMath(instr);
314 fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
316 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
317 llvm::LLVMRustSetFastMath(instr);
328 ) -> (Self::Value, Self::Value) {
329 use rustc_middle::ty::{Int, Uint};
330 use rustc_middle::ty::{IntTy::*, UintTy::*};
332 let new_kind = match ty.kind() {
333 Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.pointer_width)),
334 Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.pointer_width)),
335 t @ (Uint(_) | Int(_)) => t.clone(),
336 _ => panic!("tried to get overflow intrinsic for op applied to non-int type"),
339 let name = match oop {
340 OverflowOp::Add => match new_kind {
341 Int(I8) => "llvm.sadd.with.overflow.i8",
342 Int(I16) => "llvm.sadd.with.overflow.i16",
343 Int(I32) => "llvm.sadd.with.overflow.i32",
344 Int(I64) => "llvm.sadd.with.overflow.i64",
345 Int(I128) => "llvm.sadd.with.overflow.i128",
347 Uint(U8) => "llvm.uadd.with.overflow.i8",
348 Uint(U16) => "llvm.uadd.with.overflow.i16",
349 Uint(U32) => "llvm.uadd.with.overflow.i32",
350 Uint(U64) => "llvm.uadd.with.overflow.i64",
351 Uint(U128) => "llvm.uadd.with.overflow.i128",
355 OverflowOp::Sub => match new_kind {
356 Int(I8) => "llvm.ssub.with.overflow.i8",
357 Int(I16) => "llvm.ssub.with.overflow.i16",
358 Int(I32) => "llvm.ssub.with.overflow.i32",
359 Int(I64) => "llvm.ssub.with.overflow.i64",
360 Int(I128) => "llvm.ssub.with.overflow.i128",
362 Uint(U8) => "llvm.usub.with.overflow.i8",
363 Uint(U16) => "llvm.usub.with.overflow.i16",
364 Uint(U32) => "llvm.usub.with.overflow.i32",
365 Uint(U64) => "llvm.usub.with.overflow.i64",
366 Uint(U128) => "llvm.usub.with.overflow.i128",
370 OverflowOp::Mul => match new_kind {
371 Int(I8) => "llvm.smul.with.overflow.i8",
372 Int(I16) => "llvm.smul.with.overflow.i16",
373 Int(I32) => "llvm.smul.with.overflow.i32",
374 Int(I64) => "llvm.smul.with.overflow.i64",
375 Int(I128) => "llvm.smul.with.overflow.i128",
377 Uint(U8) => "llvm.umul.with.overflow.i8",
378 Uint(U16) => "llvm.umul.with.overflow.i16",
379 Uint(U32) => "llvm.umul.with.overflow.i32",
380 Uint(U64) => "llvm.umul.with.overflow.i64",
381 Uint(U128) => "llvm.umul.with.overflow.i128",
387 let res = self.call_intrinsic(name, &[lhs, rhs]);
388 (self.extract_value(res, 0), self.extract_value(res, 1))
391 fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
392 if self.cx().val_ty(val) == self.cx().type_i1() {
393 self.zext(val, self.cx().type_i8())
398 fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
399 if scalar.is_bool() {
400 return self.trunc(val, self.cx().type_i1());
405 fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
406 let mut bx = Builder::with_cx(self.cx);
407 bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
408 bx.dynamic_alloca(ty, align)
411 fn dynamic_alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
413 let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
414 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
419 fn array_alloca(&mut self, ty: &'ll Type, len: &'ll Value, align: Align) -> &'ll Value {
421 let alloca = llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED);
422 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
427 fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
429 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
430 llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
435 fn volatile_load(&mut self, ty: &'ll Type, ptr: &'ll Value) -> &'ll Value {
437 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
438 llvm::LLVMSetVolatile(load, llvm::True);
447 order: rustc_codegen_ssa::common::AtomicOrdering,
451 let load = llvm::LLVMRustBuildAtomicLoad(
456 AtomicOrdering::from_generic(order),
458 // LLVM requires the alignment of atomic loads to be at least the size of the type.
459 llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
464 #[instrument(level = "trace", skip(self))]
465 fn load_operand(&mut self, place: PlaceRef<'tcx, &'ll Value>) -> OperandRef<'tcx, &'ll Value> {
466 assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
468 if place.layout.is_zst() {
469 return OperandRef::new_zst(self, place.layout);
472 #[instrument(level = "trace", skip(bx))]
473 fn scalar_load_metadata<'a, 'll, 'tcx>(
474 bx: &mut Builder<'a, 'll, 'tcx>,
477 layout: TyAndLayout<'tcx>,
480 if !scalar.is_always_valid(bx) {
481 bx.noundef_metadata(load);
484 match scalar.primitive() {
486 if !scalar.is_always_valid(bx) {
487 bx.range_metadata(load, scalar.valid_range(bx));
491 if !scalar.valid_range(bx).contains(0) {
492 bx.nonnull_metadata(load);
495 if let Some(pointee) = layout.pointee_info_at(bx, offset) {
496 if let Some(_) = pointee.safe {
497 bx.align_metadata(load, pointee.align);
501 abi::F32 | abi::F64 => {}
505 let val = if let Some(llextra) = place.llextra {
506 OperandValue::Ref(place.llval, Some(llextra), place.align)
507 } else if place.layout.is_llvm_immediate() {
508 let mut const_llval = None;
509 let llty = place.layout.llvm_type(self);
511 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
512 if llvm::LLVMIsGlobalConstant(global) == llvm::True {
513 if let Some(init) = llvm::LLVMGetInitializer(global) {
514 if self.val_ty(init) == llty {
515 const_llval = Some(init);
521 let llval = const_llval.unwrap_or_else(|| {
522 let load = self.load(llty, place.llval, place.align);
523 if let abi::Abi::Scalar(scalar) = place.layout.abi {
524 scalar_load_metadata(self, load, scalar, place.layout, Size::ZERO);
528 OperandValue::Immediate(self.to_immediate(llval, place.layout))
529 } else if let abi::Abi::ScalarPair(a, b) = place.layout.abi {
530 let b_offset = a.size(self).align_to(b.align(self).abi);
531 let pair_ty = place.layout.llvm_type(self);
533 let mut load = |i, scalar: abi::Scalar, layout, align, offset| {
534 let llptr = self.struct_gep(pair_ty, place.llval, i as u64);
535 let llty = place.layout.scalar_pair_element_llvm_type(self, i, false);
536 let load = self.load(llty, llptr, align);
537 scalar_load_metadata(self, load, scalar, layout, offset);
538 self.to_immediate_scalar(load, scalar)
542 load(0, a, place.layout, place.align, Size::ZERO),
543 load(1, b, place.layout, place.align.restrict_for_offset(b_offset), b_offset),
546 OperandValue::Ref(place.llval, None, place.align)
549 OperandRef { val, layout: place.layout }
552 fn write_operand_repeatedly(
554 cg_elem: OperandRef<'tcx, &'ll Value>,
556 dest: PlaceRef<'tcx, &'ll Value>,
558 let zero = self.const_usize(0);
559 let count = self.const_usize(count);
560 let start = dest.project_index(&mut self, zero).llval;
561 let end = dest.project_index(&mut self, count).llval;
563 let header_bb = self.append_sibling_block("repeat_loop_header");
564 let body_bb = self.append_sibling_block("repeat_loop_body");
565 let next_bb = self.append_sibling_block("repeat_loop_next");
569 let mut header_bx = Self::build(self.cx, header_bb);
570 let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
572 let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
573 header_bx.cond_br(keep_going, body_bb, next_bb);
575 let mut body_bx = Self::build(self.cx, body_bb);
576 let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
579 .store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
581 let next = body_bx.inbounds_gep(
582 self.backend_type(cg_elem.layout),
584 &[self.const_usize(1)],
586 body_bx.br(header_bb);
587 header_bx.add_incoming_to_phi(current, next, body_bb);
589 Self::build(self.cx, next_bb)
592 fn range_metadata(&mut self, load: &'ll Value, range: WrappingRange) {
593 if self.sess().target.arch == "amdgpu" {
594 // amdgpu/LLVM does something weird and thinks an i64 value is
595 // split into a v2i32, halving the bitwidth LLVM expects,
596 // tripping an assertion. So, for now, just disable this
602 let llty = self.cx.val_ty(load);
604 self.cx.const_uint_big(llty, range.start),
605 self.cx.const_uint_big(llty, range.end.wrapping_add(1)),
608 llvm::LLVMSetMetadata(
610 llvm::MD_range as c_uint,
611 llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
616 fn nonnull_metadata(&mut self, load: &'ll Value) {
618 llvm::LLVMSetMetadata(
620 llvm::MD_nonnull as c_uint,
621 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
626 fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
627 self.store_with_flags(val, ptr, align, MemFlags::empty())
637 debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
638 let ptr = self.check_store(val, ptr);
640 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
642 if flags.contains(MemFlags::UNALIGNED) { 1 } else { align.bytes() as c_uint };
643 llvm::LLVMSetAlignment(store, align);
644 if flags.contains(MemFlags::VOLATILE) {
645 llvm::LLVMSetVolatile(store, llvm::True);
647 if flags.contains(MemFlags::NONTEMPORAL) {
648 // According to LLVM [1] building a nontemporal store must
649 // *always* point to a metadata value of the integer 1.
651 // [1]: https://llvm.org/docs/LangRef.html#store-instruction
652 let one = self.cx.const_i32(1);
653 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
654 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
664 order: rustc_codegen_ssa::common::AtomicOrdering,
667 debug!("Store {:?} -> {:?}", val, ptr);
668 let ptr = self.check_store(val, ptr);
670 let store = llvm::LLVMRustBuildAtomicStore(
674 AtomicOrdering::from_generic(order),
676 // LLVM requires the alignment of atomic stores to be at least the size of the type.
677 llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
681 fn gep(&mut self, ty: &'ll Type, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
688 indices.len() as c_uint,
698 indices: &[&'ll Value],
701 llvm::LLVMBuildInBoundsGEP2(
706 indices.len() as c_uint,
712 fn struct_gep(&mut self, ty: &'ll Type, ptr: &'ll Value, idx: u64) -> &'ll Value {
713 assert_eq!(idx as c_uint as u64, idx);
714 unsafe { llvm::LLVMBuildStructGEP2(self.llbuilder, ty, ptr, idx as c_uint, UNNAMED) }
718 fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
719 unsafe { llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
722 fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
723 unsafe { llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED) }
726 fn fptoui_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
727 self.fptoint_sat(false, val, dest_ty)
730 fn fptosi_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
731 self.fptoint_sat(true, val, dest_ty)
734 fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
735 // On WebAssembly the `fptoui` and `fptosi` instructions currently have
736 // poor codegen. The reason for this is that the corresponding wasm
737 // instructions, `i32.trunc_f32_s` for example, will trap when the float
738 // is out-of-bounds, infinity, or nan. This means that LLVM
739 // automatically inserts control flow around `fptoui` and `fptosi`
740 // because the LLVM instruction `fptoui` is defined as producing a
741 // poison value, not having UB on out-of-bounds values.
743 // This method, however, is only used with non-saturating casts that
744 // have UB on out-of-bounds values. This means that it's ok if we use
745 // the raw wasm instruction since out-of-bounds values can do whatever
746 // we like. To ensure that LLVM picks the right instruction we choose
747 // the raw wasm intrinsic functions which avoid LLVM inserting all the
748 // other control flow automatically.
749 if self.sess().target.is_like_wasm {
750 let src_ty = self.cx.val_ty(val);
751 if self.cx.type_kind(src_ty) != TypeKind::Vector {
752 let float_width = self.cx.float_width(src_ty);
753 let int_width = self.cx.int_width(dest_ty);
754 let name = match (int_width, float_width) {
755 (32, 32) => Some("llvm.wasm.trunc.unsigned.i32.f32"),
756 (32, 64) => Some("llvm.wasm.trunc.unsigned.i32.f64"),
757 (64, 32) => Some("llvm.wasm.trunc.unsigned.i64.f32"),
758 (64, 64) => Some("llvm.wasm.trunc.unsigned.i64.f64"),
761 if let Some(name) = name {
762 return self.call_intrinsic(name, &[val]);
766 unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) }
769 fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
770 // see `fptoui` above for why wasm is different here
771 if self.sess().target.is_like_wasm {
772 let src_ty = self.cx.val_ty(val);
773 if self.cx.type_kind(src_ty) != TypeKind::Vector {
774 let float_width = self.cx.float_width(src_ty);
775 let int_width = self.cx.int_width(dest_ty);
776 let name = match (int_width, float_width) {
777 (32, 32) => Some("llvm.wasm.trunc.signed.i32.f32"),
778 (32, 64) => Some("llvm.wasm.trunc.signed.i32.f64"),
779 (64, 32) => Some("llvm.wasm.trunc.signed.i64.f32"),
780 (64, 64) => Some("llvm.wasm.trunc.signed.i64.f64"),
783 if let Some(name) = name {
784 return self.call_intrinsic(name, &[val]);
788 unsafe { llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty, UNNAMED) }
791 fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
792 unsafe { llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
795 fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
796 unsafe { llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
799 fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
800 unsafe { llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
803 fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
804 unsafe { llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, UNNAMED) }
807 fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
808 unsafe { llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, UNNAMED) }
811 fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
812 unsafe { llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, UNNAMED) }
815 fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
816 unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
819 fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
820 unsafe { llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed) }
823 fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
824 unsafe { llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED) }
828 fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
829 let op = llvm::IntPredicate::from_generic(op);
830 unsafe { llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
833 fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
834 let op = llvm::RealPredicate::from_generic(op);
835 unsafe { llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
838 /* Miscellaneous instructions */
848 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
849 let size = self.intcast(size, self.type_isize(), false);
850 let is_volatile = flags.contains(MemFlags::VOLATILE);
851 let dst = self.pointercast(dst, self.type_i8p());
852 let src = self.pointercast(src, self.type_i8p());
854 llvm::LLVMRustBuildMemCpy(
857 dst_align.bytes() as c_uint,
859 src_align.bytes() as c_uint,
875 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memmove not supported");
876 let size = self.intcast(size, self.type_isize(), false);
877 let is_volatile = flags.contains(MemFlags::VOLATILE);
878 let dst = self.pointercast(dst, self.type_i8p());
879 let src = self.pointercast(src, self.type_i8p());
881 llvm::LLVMRustBuildMemMove(
884 dst_align.bytes() as c_uint,
886 src_align.bytes() as c_uint,
896 fill_byte: &'ll Value,
901 let is_volatile = flags.contains(MemFlags::VOLATILE);
902 let ptr = self.pointercast(ptr, self.type_i8p());
904 llvm::LLVMRustBuildMemSet(
907 align.bytes() as c_uint,
918 then_val: &'ll Value,
919 else_val: &'ll Value,
921 unsafe { llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, UNNAMED) }
924 fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
925 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
928 fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
929 unsafe { llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED) }
932 fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
934 let elt_ty = self.cx.val_ty(elt);
935 let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
936 let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
937 let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
938 self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
942 fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
943 assert_eq!(idx as c_uint as u64, idx);
944 unsafe { llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED) }
947 fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value, idx: u64) -> &'ll Value {
948 assert_eq!(idx as c_uint as u64, idx);
949 unsafe { llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint, UNNAMED) }
952 fn set_personality_fn(&mut self, personality: &'ll Value) {
954 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
958 fn cleanup_landing_pad(&mut self, ty: &'ll Type, pers_fn: &'ll Value) -> &'ll Value {
959 let landing_pad = self.landing_pad(ty, pers_fn, 1 /* FIXME should this be 0? */);
961 llvm::LLVMSetCleanup(landing_pad, llvm::True);
966 fn resume(&mut self, exn: &'ll Value) {
968 llvm::LLVMBuildResume(self.llbuilder, exn);
972 fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
973 let name = cstr!("cleanuppad");
975 llvm::LLVMRustBuildCleanupPad(
978 args.len() as c_uint,
983 Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
986 fn cleanup_ret(&mut self, funclet: &Funclet<'ll>, unwind: Option<&'ll BasicBlock>) {
988 llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind)
989 .expect("LLVM does not have support for cleanupret");
993 fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
994 let name = cstr!("catchpad");
996 llvm::LLVMRustBuildCatchPad(
999 args.len() as c_uint,
1004 Funclet::new(ret.expect("LLVM does not have support for catchpad"))
1009 parent: Option<&'ll Value>,
1010 unwind: Option<&'ll BasicBlock>,
1011 handlers: &[&'ll BasicBlock],
1013 let name = cstr!("catchswitch");
1015 llvm::LLVMRustBuildCatchSwitch(
1019 handlers.len() as c_uint,
1023 let ret = ret.expect("LLVM does not have support for catchswitch");
1024 for handler in handlers {
1026 llvm::LLVMRustAddHandler(ret, handler);
1032 // Atomic Operations
1038 order: rustc_codegen_ssa::common::AtomicOrdering,
1039 failure_order: rustc_codegen_ssa::common::AtomicOrdering,
1042 let weak = if weak { llvm::True } else { llvm::False };
1044 let value = llvm::LLVMBuildAtomicCmpXchg(
1049 AtomicOrdering::from_generic(order),
1050 AtomicOrdering::from_generic(failure_order),
1051 llvm::False, // SingleThreaded
1053 llvm::LLVMSetWeak(value, weak);
1059 op: rustc_codegen_ssa::common::AtomicRmwBinOp,
1062 order: rustc_codegen_ssa::common::AtomicOrdering,
1065 llvm::LLVMBuildAtomicRMW(
1067 AtomicRmwBinOp::from_generic(op),
1070 AtomicOrdering::from_generic(order),
1071 llvm::False, // SingleThreaded
1078 order: rustc_codegen_ssa::common::AtomicOrdering,
1079 scope: SynchronizationScope,
1081 let single_threaded = match scope {
1082 SynchronizationScope::SingleThread => llvm::True,
1083 SynchronizationScope::CrossThread => llvm::False,
1086 llvm::LLVMBuildFence(
1088 AtomicOrdering::from_generic(order),
1095 fn set_invariant_load(&mut self, load: &'ll Value) {
1097 llvm::LLVMSetMetadata(
1099 llvm::MD_invariant_load as c_uint,
1100 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1105 fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
1106 self.call_lifetime_intrinsic("llvm.lifetime.start.p0i8", ptr, size);
1109 fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
1110 self.call_lifetime_intrinsic("llvm.lifetime.end.p0i8", ptr, size);
1113 fn instrprof_increment(
1115 fn_name: &'ll Value,
1117 num_counters: &'ll Value,
1121 "instrprof_increment() with args ({:?}, {:?}, {:?}, {:?})",
1122 fn_name, hash, num_counters, index
1125 let llfn = unsafe { llvm::LLVMRustGetInstrProfIncrementIntrinsic(self.cx().llmod) };
1126 let llty = self.cx.type_func(
1127 &[self.cx.type_i8p(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_i32()],
1128 self.cx.type_void(),
1130 let args = &[fn_name, hash, num_counters, index];
1131 let args = self.check_call("call", llty, llfn, args);
1134 let _ = llvm::LLVMRustBuildCall(
1138 args.as_ptr() as *const &llvm::Value,
1139 args.len() as c_uint,
1149 args: &[&'ll Value],
1150 funclet: Option<&Funclet<'ll>>,
1152 debug!("call {:?} with args ({:?})", llfn, args);
1154 let args = self.check_call("call", llty, llfn, args);
1155 let bundle = funclet.map(|funclet| funclet.bundle());
1156 let bundle = bundle.as_ref().map(|b| &*b.raw);
1159 llvm::LLVMRustBuildCall(
1163 args.as_ptr() as *const &llvm::Value,
1164 args.len() as c_uint,
1170 fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1171 unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
1174 fn do_not_inline(&mut self, llret: &'ll Value) {
1175 let noinline = llvm::AttributeKind::NoInline.create_attr(self.llcx);
1176 attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[noinline]);
1180 impl<'ll> StaticBuilderMethods for Builder<'_, 'll, '_> {
1181 fn get_static(&mut self, def_id: DefId) -> &'ll Value {
1182 // Forward to the `get_static` method of `CodegenCx`
1183 self.cx().get_static(def_id)
1187 impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
1188 fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
1189 // Create a fresh builder from the crate context.
1190 let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(cx.llcx) };
1191 Builder { llbuilder, cx }
1194 pub fn llfn(&self) -> &'ll Value {
1195 unsafe { llvm::LLVMGetBasicBlockParent(self.llbb()) }
1198 fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
1200 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
1204 fn align_metadata(&mut self, load: &'ll Value, align: Align) {
1206 let v = [self.cx.const_u64(align.bytes())];
1208 llvm::LLVMSetMetadata(
1210 llvm::MD_align as c_uint,
1211 llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
1216 fn noundef_metadata(&mut self, load: &'ll Value) {
1218 llvm::LLVMSetMetadata(
1220 llvm::MD_noundef as c_uint,
1221 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1226 pub fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1227 unsafe { llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs) }
1230 pub fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1231 unsafe { llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs) }
1234 pub fn insert_element(
1240 unsafe { llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, UNNAMED) }
1243 pub fn shuffle_vector(
1249 unsafe { llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, UNNAMED) }
1252 pub fn vector_reduce_fadd(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1253 unsafe { llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src) }
1255 pub fn vector_reduce_fmul(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1256 unsafe { llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src) }
1258 pub fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1260 let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
1261 llvm::LLVMRustSetFastMath(instr);
1265 pub fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1267 let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
1268 llvm::LLVMRustSetFastMath(instr);
1272 pub fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
1273 unsafe { llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src) }
1275 pub fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
1276 unsafe { llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src) }
1278 pub fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
1279 unsafe { llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src) }
1281 pub fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
1282 unsafe { llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src) }
1284 pub fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
1285 unsafe { llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src) }
1287 pub fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
1289 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false)
1292 pub fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
1294 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false)
1297 pub fn vector_reduce_fmin_fast(&mut self, src: &'ll Value) -> &'ll Value {
1300 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true);
1301 llvm::LLVMRustSetFastMath(instr);
1305 pub fn vector_reduce_fmax_fast(&mut self, src: &'ll Value) -> &'ll Value {
1308 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true);
1309 llvm::LLVMRustSetFastMath(instr);
1313 pub fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1314 unsafe { llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed) }
1316 pub fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1317 unsafe { llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed) }
1320 pub fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
1322 llvm::LLVMAddClause(landing_pad, clause);
1326 pub fn catch_ret(&mut self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
1328 unsafe { llvm::LLVMRustBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind) };
1329 ret.expect("LLVM does not have support for catchret")
1332 fn check_store(&mut self, val: &'ll Value, ptr: &'ll Value) -> &'ll Value {
1333 let dest_ptr_ty = self.cx.val_ty(ptr);
1334 let stored_ty = self.cx.val_ty(val);
1335 let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
1337 assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
1339 if dest_ptr_ty == stored_ptr_ty {
1343 "type mismatch in store. \
1344 Expected {:?}, got {:?}; inserting bitcast",
1345 dest_ptr_ty, stored_ptr_ty
1347 self.bitcast(ptr, stored_ptr_ty)
1356 args: &'b [&'ll Value],
1357 ) -> Cow<'b, [&'ll Value]> {
1359 self.cx.type_kind(fn_ty) == TypeKind::Function,
1360 "builder::{} not passed a function, but {:?}",
1365 let param_tys = self.cx.func_params_types(fn_ty);
1367 let all_args_match = iter::zip(¶m_tys, args.iter().map(|&v| self.val_ty(v)))
1368 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1371 return Cow::Borrowed(args);
1374 let casted_args: Vec<_> = iter::zip(param_tys, args)
1376 .map(|(i, (expected_ty, &actual_val))| {
1377 let actual_ty = self.val_ty(actual_val);
1378 if expected_ty != actual_ty {
1380 "type mismatch in function call of {:?}. \
1381 Expected {:?} for param {}, got {:?}; injecting bitcast",
1382 llfn, expected_ty, i, actual_ty
1384 self.bitcast(actual_val, expected_ty)
1391 Cow::Owned(casted_args)
1394 pub fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
1395 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
1398 pub(crate) fn call_intrinsic(&mut self, intrinsic: &str, args: &[&'ll Value]) -> &'ll Value {
1399 let (ty, f) = self.cx.get_intrinsic(intrinsic);
1400 self.call(ty, f, args, None)
1403 fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
1404 let size = size.bytes();
1409 if !self.cx().sess().emit_lifetime_markers() {
1413 let ptr = self.pointercast(ptr, self.cx.type_i8p());
1414 self.call_intrinsic(intrinsic, &[self.cx.const_u64(size), ptr]);
1420 vals: &[&'ll Value],
1421 bbs: &[&'ll BasicBlock],
1423 assert_eq!(vals.len(), bbs.len());
1424 let phi = unsafe { llvm::LLVMBuildPhi(self.llbuilder, ty, UNNAMED) };
1426 llvm::LLVMAddIncoming(phi, vals.as_ptr(), bbs.as_ptr(), vals.len() as c_uint);
1431 fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
1433 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1437 fn fptoint_sat(&mut self, signed: bool, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1438 let src_ty = self.cx.val_ty(val);
1439 let (float_ty, int_ty, vector_length) = if self.cx.type_kind(src_ty) == TypeKind::Vector {
1440 assert_eq!(self.cx.vector_length(src_ty), self.cx.vector_length(dest_ty));
1442 self.cx.element_type(src_ty),
1443 self.cx.element_type(dest_ty),
1444 Some(self.cx.vector_length(src_ty)),
1447 (src_ty, dest_ty, None)
1449 let float_width = self.cx.float_width(float_ty);
1450 let int_width = self.cx.int_width(int_ty);
1452 let instr = if signed { "fptosi" } else { "fptoui" };
1453 let name = if let Some(vector_length) = vector_length {
1455 "llvm.{}.sat.v{}i{}.v{}f{}",
1456 instr, vector_length, int_width, vector_length, float_width
1459 format!("llvm.{}.sat.i{}.f{}", instr, int_width, float_width)
1461 let f = self.declare_cfn(&name, llvm::UnnamedAddr::No, self.type_func(&[src_ty], dest_ty));
1462 self.call(self.type_func(&[src_ty], dest_ty), f, &[val], None)
1465 pub(crate) fn landing_pad(
1468 pers_fn: &'ll Value,
1471 // Use LLVMSetPersonalityFn to set the personality. It supports arbitrary Consts while,
1472 // LLVMBuildLandingPad requires the argument to be a Function (as of LLVM 12). The
1473 // personality lives on the parent function anyway.
1474 self.set_personality_fn(pers_fn);
1476 llvm::LLVMBuildLandingPad(self.llbuilder, ty, None, num_clauses as c_uint, UNNAMED)