2 use crate::common::Funclet;
3 use crate::context::CodegenCx;
4 use crate::llvm::{self, BasicBlock, False};
5 use crate::llvm::{AtomicOrdering, AtomicRmwBinOp, SynchronizationScope};
7 use crate::type_::Type;
8 use crate::type_of::LayoutLlvmExt;
9 use crate::value::Value;
11 use libc::{c_char, c_uint};
12 use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, TypeKind};
13 use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
14 use rustc_codegen_ssa::mir::place::PlaceRef;
15 use rustc_codegen_ssa::traits::*;
16 use rustc_codegen_ssa::MemFlags;
17 use rustc_data_structures::small_c_str::SmallCStr;
18 use rustc_hir::def_id::DefId;
19 use rustc_middle::ty::layout::{
20 FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
22 use rustc_middle::ty::{self, Ty, TyCtxt};
24 use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
25 use rustc_target::spec::{HasTargetSpec, Target};
31 use tracing::{debug, instrument};
33 // All Builders must have an llfn associated with them
35 pub struct Builder<'a, 'll, 'tcx> {
36 pub llbuilder: &'ll mut llvm::Builder<'ll>,
37 pub cx: &'a CodegenCx<'ll, 'tcx>,
40 impl Drop for Builder<'_, '_, '_> {
43 llvm::LLVMDisposeBuilder(&mut *(self.llbuilder as *mut _));
48 // FIXME(eddyb) use a checked constructor when they become `const fn`.
49 const EMPTY_C_STR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };
51 /// Empty string, to be used where LLVM expects an instruction name, indicating
52 /// that the instruction is to be left unnamed (i.e. numbered, in textual IR).
53 // FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer.
54 const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
56 impl<'ll, 'tcx> BackendTypes for Builder<'_, 'll, 'tcx> {
57 type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
58 type Function = <CodegenCx<'ll, 'tcx> as BackendTypes>::Function;
59 type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
60 type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
61 type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;
63 type DIScope = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIScope;
64 type DILocation = <CodegenCx<'ll, 'tcx> as BackendTypes>::DILocation;
65 type DIVariable = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIVariable;
68 impl abi::HasDataLayout for Builder<'_, '_, '_> {
69 fn data_layout(&self) -> &abi::TargetDataLayout {
74 impl<'tcx> ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
76 fn tcx(&self) -> TyCtxt<'tcx> {
81 impl<'tcx> ty::layout::HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
82 fn param_env(&self) -> ty::ParamEnv<'tcx> {
87 impl HasTargetSpec for Builder<'_, '_, '_> {
89 fn target_spec(&self) -> &Target {
94 impl<'tcx> LayoutOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
95 type LayoutOfResult = TyAndLayout<'tcx>;
98 fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
99 self.cx.handle_layout_err(err, span, ty)
103 impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
104 type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
107 fn handle_fn_abi_err(
109 err: FnAbiError<'tcx>,
111 fn_abi_request: FnAbiRequest<'tcx>,
113 self.cx.handle_fn_abi_err(err, span, fn_abi_request)
117 impl<'ll, 'tcx> Deref for Builder<'_, 'll, 'tcx> {
118 type Target = CodegenCx<'ll, 'tcx>;
121 fn deref(&self) -> &Self::Target {
126 impl<'ll, 'tcx> HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
127 type CodegenCx = CodegenCx<'ll, 'tcx>;
130 macro_rules! builder_methods_for_value_instructions {
131 ($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
132 $(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
134 llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED)
140 impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
141 fn build(cx: &'a CodegenCx<'ll, 'tcx>, llbb: &'ll BasicBlock) -> Self {
142 let bx = Builder::with_cx(cx);
144 llvm::LLVMPositionBuilderAtEnd(bx.llbuilder, llbb);
149 fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
153 fn llbb(&self) -> &'ll BasicBlock {
154 unsafe { llvm::LLVMGetInsertBlock(self.llbuilder) }
157 fn set_span(&mut self, _span: Span) {}
159 fn append_block(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &str) -> &'ll BasicBlock {
161 let name = SmallCStr::new(name);
162 llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name.as_ptr())
166 fn append_sibling_block(&mut self, name: &str) -> &'ll BasicBlock {
167 Self::append_block(self.cx, self.llfn(), name)
170 fn switch_to_block(&mut self, llbb: Self::BasicBlock) {
171 *self = Self::build(self.cx, llbb)
174 fn ret_void(&mut self) {
176 llvm::LLVMBuildRetVoid(self.llbuilder);
180 fn ret(&mut self, v: &'ll Value) {
182 llvm::LLVMBuildRet(self.llbuilder, v);
186 fn br(&mut self, dest: &'ll BasicBlock) {
188 llvm::LLVMBuildBr(self.llbuilder, dest);
195 then_llbb: &'ll BasicBlock,
196 else_llbb: &'ll BasicBlock,
199 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
206 else_llbb: &'ll BasicBlock,
207 cases: impl ExactSizeIterator<Item = (u128, &'ll BasicBlock)>,
210 unsafe { llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, cases.len() as c_uint) };
211 for (on_val, dest) in cases {
212 let on_val = self.const_uint_big(self.val_ty(v), on_val);
213 unsafe { llvm::LLVMAddCase(switch, on_val, dest) }
222 then: &'ll BasicBlock,
223 catch: &'ll BasicBlock,
224 funclet: Option<&Funclet<'ll>>,
226 debug!("invoke {:?} with args ({:?})", llfn, args);
228 let args = self.check_call("invoke", llty, llfn, args);
229 let bundle = funclet.map(|funclet| funclet.bundle());
230 let bundle = bundle.as_ref().map(|b| &*b.raw);
233 llvm::LLVMRustBuildInvoke(
238 args.len() as c_uint,
247 fn unreachable(&mut self) {
249 llvm::LLVMBuildUnreachable(self.llbuilder);
253 builder_methods_for_value_instructions! {
254 add(a, b) => LLVMBuildAdd,
255 fadd(a, b) => LLVMBuildFAdd,
256 sub(a, b) => LLVMBuildSub,
257 fsub(a, b) => LLVMBuildFSub,
258 mul(a, b) => LLVMBuildMul,
259 fmul(a, b) => LLVMBuildFMul,
260 udiv(a, b) => LLVMBuildUDiv,
261 exactudiv(a, b) => LLVMBuildExactUDiv,
262 sdiv(a, b) => LLVMBuildSDiv,
263 exactsdiv(a, b) => LLVMBuildExactSDiv,
264 fdiv(a, b) => LLVMBuildFDiv,
265 urem(a, b) => LLVMBuildURem,
266 srem(a, b) => LLVMBuildSRem,
267 frem(a, b) => LLVMBuildFRem,
268 shl(a, b) => LLVMBuildShl,
269 lshr(a, b) => LLVMBuildLShr,
270 ashr(a, b) => LLVMBuildAShr,
271 and(a, b) => LLVMBuildAnd,
272 or(a, b) => LLVMBuildOr,
273 xor(a, b) => LLVMBuildXor,
274 neg(x) => LLVMBuildNeg,
275 fneg(x) => LLVMBuildFNeg,
276 not(x) => LLVMBuildNot,
277 unchecked_sadd(x, y) => LLVMBuildNSWAdd,
278 unchecked_uadd(x, y) => LLVMBuildNUWAdd,
279 unchecked_ssub(x, y) => LLVMBuildNSWSub,
280 unchecked_usub(x, y) => LLVMBuildNUWSub,
281 unchecked_smul(x, y) => LLVMBuildNSWMul,
282 unchecked_umul(x, y) => LLVMBuildNUWMul,
285 fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
287 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
288 llvm::LLVMRustSetFastMath(instr);
293 fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
295 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
296 llvm::LLVMRustSetFastMath(instr);
301 fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
303 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
304 llvm::LLVMRustSetFastMath(instr);
309 fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
311 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
312 llvm::LLVMRustSetFastMath(instr);
317 fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
319 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
320 llvm::LLVMRustSetFastMath(instr);
331 ) -> (Self::Value, Self::Value) {
332 use rustc_middle::ty::{Int, Uint};
333 use rustc_middle::ty::{IntTy::*, UintTy::*};
335 let new_kind = match ty.kind() {
336 Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.pointer_width)),
337 Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.pointer_width)),
338 t @ (Uint(_) | Int(_)) => t.clone(),
339 _ => panic!("tried to get overflow intrinsic for op applied to non-int type"),
342 let name = match oop {
343 OverflowOp::Add => match new_kind {
344 Int(I8) => "llvm.sadd.with.overflow.i8",
345 Int(I16) => "llvm.sadd.with.overflow.i16",
346 Int(I32) => "llvm.sadd.with.overflow.i32",
347 Int(I64) => "llvm.sadd.with.overflow.i64",
348 Int(I128) => "llvm.sadd.with.overflow.i128",
350 Uint(U8) => "llvm.uadd.with.overflow.i8",
351 Uint(U16) => "llvm.uadd.with.overflow.i16",
352 Uint(U32) => "llvm.uadd.with.overflow.i32",
353 Uint(U64) => "llvm.uadd.with.overflow.i64",
354 Uint(U128) => "llvm.uadd.with.overflow.i128",
358 OverflowOp::Sub => match new_kind {
359 Int(I8) => "llvm.ssub.with.overflow.i8",
360 Int(I16) => "llvm.ssub.with.overflow.i16",
361 Int(I32) => "llvm.ssub.with.overflow.i32",
362 Int(I64) => "llvm.ssub.with.overflow.i64",
363 Int(I128) => "llvm.ssub.with.overflow.i128",
365 Uint(U8) => "llvm.usub.with.overflow.i8",
366 Uint(U16) => "llvm.usub.with.overflow.i16",
367 Uint(U32) => "llvm.usub.with.overflow.i32",
368 Uint(U64) => "llvm.usub.with.overflow.i64",
369 Uint(U128) => "llvm.usub.with.overflow.i128",
373 OverflowOp::Mul => match new_kind {
374 Int(I8) => "llvm.smul.with.overflow.i8",
375 Int(I16) => "llvm.smul.with.overflow.i16",
376 Int(I32) => "llvm.smul.with.overflow.i32",
377 Int(I64) => "llvm.smul.with.overflow.i64",
378 Int(I128) => "llvm.smul.with.overflow.i128",
380 Uint(U8) => "llvm.umul.with.overflow.i8",
381 Uint(U16) => "llvm.umul.with.overflow.i16",
382 Uint(U32) => "llvm.umul.with.overflow.i32",
383 Uint(U64) => "llvm.umul.with.overflow.i64",
384 Uint(U128) => "llvm.umul.with.overflow.i128",
390 let res = self.call_intrinsic(name, &[lhs, rhs]);
391 (self.extract_value(res, 0), self.extract_value(res, 1))
394 fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
395 if self.cx().val_ty(val) == self.cx().type_i1() {
396 self.zext(val, self.cx().type_i8())
401 fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
402 if scalar.is_bool() {
403 return self.trunc(val, self.cx().type_i1());
408 fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
409 let mut bx = Builder::with_cx(self.cx);
410 bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
411 bx.dynamic_alloca(ty, align)
414 fn dynamic_alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
416 let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
417 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
422 fn array_alloca(&mut self, ty: &'ll Type, len: &'ll Value, align: Align) -> &'ll Value {
424 let alloca = llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED);
425 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
430 fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
432 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
433 llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
438 fn volatile_load(&mut self, ty: &'ll Type, ptr: &'ll Value) -> &'ll Value {
440 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
441 llvm::LLVMSetVolatile(load, llvm::True);
450 order: rustc_codegen_ssa::common::AtomicOrdering,
454 let load = llvm::LLVMRustBuildAtomicLoad(
459 AtomicOrdering::from_generic(order),
461 // LLVM requires the alignment of atomic loads to be at least the size of the type.
462 llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
467 #[instrument(level = "trace", skip(self))]
468 fn load_operand(&mut self, place: PlaceRef<'tcx, &'ll Value>) -> OperandRef<'tcx, &'ll Value> {
469 assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
471 if place.layout.is_zst() {
472 return OperandRef::new_zst(self, place.layout);
475 #[instrument(level = "trace", skip(bx))]
476 fn scalar_load_metadata<'a, 'll, 'tcx>(
477 bx: &mut Builder<'a, 'll, 'tcx>,
480 layout: TyAndLayout<'tcx>,
483 if !scalar.is_always_valid(bx) {
484 bx.noundef_metadata(load);
487 match scalar.primitive() {
489 if !scalar.is_always_valid(bx) {
490 bx.range_metadata(load, scalar.valid_range(bx));
494 if !scalar.valid_range(bx).contains(0) {
495 bx.nonnull_metadata(load);
498 if let Some(pointee) = layout.pointee_info_at(bx, offset) {
499 if let Some(_) = pointee.safe {
500 bx.align_metadata(load, pointee.align);
504 abi::F32 | abi::F64 => {}
508 let val = if let Some(llextra) = place.llextra {
509 OperandValue::Ref(place.llval, Some(llextra), place.align)
510 } else if place.layout.is_llvm_immediate() {
511 let mut const_llval = None;
512 let llty = place.layout.llvm_type(self);
514 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
515 if llvm::LLVMIsGlobalConstant(global) == llvm::True {
516 if let Some(init) = llvm::LLVMGetInitializer(global) {
517 if self.val_ty(init) == llty {
518 const_llval = Some(init);
524 let llval = const_llval.unwrap_or_else(|| {
525 let load = self.load(llty, place.llval, place.align);
526 if let abi::Abi::Scalar(scalar) = place.layout.abi {
527 scalar_load_metadata(self, load, scalar, place.layout, Size::ZERO);
531 OperandValue::Immediate(self.to_immediate(llval, place.layout))
532 } else if let abi::Abi::ScalarPair(a, b) = place.layout.abi {
533 let b_offset = a.size(self).align_to(b.align(self).abi);
534 let pair_ty = place.layout.llvm_type(self);
536 let mut load = |i, scalar: abi::Scalar, layout, align, offset| {
537 let llptr = self.struct_gep(pair_ty, place.llval, i as u64);
538 let llty = place.layout.scalar_pair_element_llvm_type(self, i, false);
539 let load = self.load(llty, llptr, align);
540 scalar_load_metadata(self, load, scalar, layout, offset);
541 self.to_immediate_scalar(load, scalar)
545 load(0, a, place.layout, place.align, Size::ZERO),
546 load(1, b, place.layout, place.align.restrict_for_offset(b_offset), b_offset),
549 OperandValue::Ref(place.llval, None, place.align)
552 OperandRef { val, layout: place.layout }
555 fn write_operand_repeatedly(
557 cg_elem: OperandRef<'tcx, &'ll Value>,
559 dest: PlaceRef<'tcx, &'ll Value>,
561 let zero = self.const_usize(0);
562 let count = self.const_usize(count);
563 let start = dest.project_index(&mut self, zero).llval;
564 let end = dest.project_index(&mut self, count).llval;
566 let header_bb = self.append_sibling_block("repeat_loop_header");
567 let body_bb = self.append_sibling_block("repeat_loop_body");
568 let next_bb = self.append_sibling_block("repeat_loop_next");
572 let mut header_bx = Self::build(self.cx, header_bb);
573 let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
575 let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
576 header_bx.cond_br(keep_going, body_bb, next_bb);
578 let mut body_bx = Self::build(self.cx, body_bb);
579 let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
582 .store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
584 let next = body_bx.inbounds_gep(
585 self.backend_type(cg_elem.layout),
587 &[self.const_usize(1)],
589 body_bx.br(header_bb);
590 header_bx.add_incoming_to_phi(current, next, body_bb);
592 Self::build(self.cx, next_bb)
595 fn range_metadata(&mut self, load: &'ll Value, range: WrappingRange) {
596 if self.sess().target.arch == "amdgpu" {
597 // amdgpu/LLVM does something weird and thinks an i64 value is
598 // split into a v2i32, halving the bitwidth LLVM expects,
599 // tripping an assertion. So, for now, just disable this
605 let llty = self.cx.val_ty(load);
607 self.cx.const_uint_big(llty, range.start),
608 self.cx.const_uint_big(llty, range.end.wrapping_add(1)),
611 llvm::LLVMSetMetadata(
613 llvm::MD_range as c_uint,
614 llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
619 fn nonnull_metadata(&mut self, load: &'ll Value) {
621 llvm::LLVMSetMetadata(
623 llvm::MD_nonnull as c_uint,
624 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
629 fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
630 self.store_with_flags(val, ptr, align, MemFlags::empty())
640 debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
641 let ptr = self.check_store(val, ptr);
643 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
645 if flags.contains(MemFlags::UNALIGNED) { 1 } else { align.bytes() as c_uint };
646 llvm::LLVMSetAlignment(store, align);
647 if flags.contains(MemFlags::VOLATILE) {
648 llvm::LLVMSetVolatile(store, llvm::True);
650 if flags.contains(MemFlags::NONTEMPORAL) {
651 // According to LLVM [1] building a nontemporal store must
652 // *always* point to a metadata value of the integer 1.
654 // [1]: https://llvm.org/docs/LangRef.html#store-instruction
655 let one = self.cx.const_i32(1);
656 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
657 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
667 order: rustc_codegen_ssa::common::AtomicOrdering,
670 debug!("Store {:?} -> {:?}", val, ptr);
671 let ptr = self.check_store(val, ptr);
673 let store = llvm::LLVMRustBuildAtomicStore(
677 AtomicOrdering::from_generic(order),
679 // LLVM requires the alignment of atomic stores to be at least the size of the type.
680 llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
684 fn gep(&mut self, ty: &'ll Type, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
691 indices.len() as c_uint,
701 indices: &[&'ll Value],
704 llvm::LLVMBuildInBoundsGEP2(
709 indices.len() as c_uint,
715 fn struct_gep(&mut self, ty: &'ll Type, ptr: &'ll Value, idx: u64) -> &'ll Value {
716 assert_eq!(idx as c_uint as u64, idx);
717 unsafe { llvm::LLVMBuildStructGEP2(self.llbuilder, ty, ptr, idx as c_uint, UNNAMED) }
721 fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
722 unsafe { llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
725 fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
726 unsafe { llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED) }
729 fn fptoui_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
730 self.fptoint_sat(false, val, dest_ty)
733 fn fptosi_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
734 self.fptoint_sat(true, val, dest_ty)
737 fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
738 // On WebAssembly the `fptoui` and `fptosi` instructions currently have
739 // poor codegen. The reason for this is that the corresponding wasm
740 // instructions, `i32.trunc_f32_s` for example, will trap when the float
741 // is out-of-bounds, infinity, or nan. This means that LLVM
742 // automatically inserts control flow around `fptoui` and `fptosi`
743 // because the LLVM instruction `fptoui` is defined as producing a
744 // poison value, not having UB on out-of-bounds values.
746 // This method, however, is only used with non-saturating casts that
747 // have UB on out-of-bounds values. This means that it's ok if we use
748 // the raw wasm instruction since out-of-bounds values can do whatever
749 // we like. To ensure that LLVM picks the right instruction we choose
750 // the raw wasm intrinsic functions which avoid LLVM inserting all the
751 // other control flow automatically.
752 if self.sess().target.is_like_wasm {
753 let src_ty = self.cx.val_ty(val);
754 if self.cx.type_kind(src_ty) != TypeKind::Vector {
755 let float_width = self.cx.float_width(src_ty);
756 let int_width = self.cx.int_width(dest_ty);
757 let name = match (int_width, float_width) {
758 (32, 32) => Some("llvm.wasm.trunc.unsigned.i32.f32"),
759 (32, 64) => Some("llvm.wasm.trunc.unsigned.i32.f64"),
760 (64, 32) => Some("llvm.wasm.trunc.unsigned.i64.f32"),
761 (64, 64) => Some("llvm.wasm.trunc.unsigned.i64.f64"),
764 if let Some(name) = name {
765 return self.call_intrinsic(name, &[val]);
769 unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) }
772 fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
773 // see `fptoui` above for why wasm is different here
774 if self.sess().target.is_like_wasm {
775 let src_ty = self.cx.val_ty(val);
776 if self.cx.type_kind(src_ty) != TypeKind::Vector {
777 let float_width = self.cx.float_width(src_ty);
778 let int_width = self.cx.int_width(dest_ty);
779 let name = match (int_width, float_width) {
780 (32, 32) => Some("llvm.wasm.trunc.signed.i32.f32"),
781 (32, 64) => Some("llvm.wasm.trunc.signed.i32.f64"),
782 (64, 32) => Some("llvm.wasm.trunc.signed.i64.f32"),
783 (64, 64) => Some("llvm.wasm.trunc.signed.i64.f64"),
786 if let Some(name) = name {
787 return self.call_intrinsic(name, &[val]);
791 unsafe { llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty, UNNAMED) }
794 fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
795 unsafe { llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
798 fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
799 unsafe { llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
802 fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
803 unsafe { llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
806 fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
807 unsafe { llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, UNNAMED) }
810 fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
811 unsafe { llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, UNNAMED) }
814 fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
815 unsafe { llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, UNNAMED) }
818 fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
819 unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
822 fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
823 unsafe { llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed) }
826 fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
827 unsafe { llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED) }
831 fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
832 let op = llvm::IntPredicate::from_generic(op);
833 unsafe { llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
836 fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
837 let op = llvm::RealPredicate::from_generic(op);
838 unsafe { llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
841 /* Miscellaneous instructions */
851 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
852 let size = self.intcast(size, self.type_isize(), false);
853 let is_volatile = flags.contains(MemFlags::VOLATILE);
854 let dst = self.pointercast(dst, self.type_i8p());
855 let src = self.pointercast(src, self.type_i8p());
857 llvm::LLVMRustBuildMemCpy(
860 dst_align.bytes() as c_uint,
862 src_align.bytes() as c_uint,
878 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memmove not supported");
879 let size = self.intcast(size, self.type_isize(), false);
880 let is_volatile = flags.contains(MemFlags::VOLATILE);
881 let dst = self.pointercast(dst, self.type_i8p());
882 let src = self.pointercast(src, self.type_i8p());
884 llvm::LLVMRustBuildMemMove(
887 dst_align.bytes() as c_uint,
889 src_align.bytes() as c_uint,
899 fill_byte: &'ll Value,
904 let is_volatile = flags.contains(MemFlags::VOLATILE);
905 let ptr = self.pointercast(ptr, self.type_i8p());
907 llvm::LLVMRustBuildMemSet(
910 align.bytes() as c_uint,
921 then_val: &'ll Value,
922 else_val: &'ll Value,
924 unsafe { llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, UNNAMED) }
927 fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
928 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
931 fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
932 unsafe { llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED) }
935 fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
937 let elt_ty = self.cx.val_ty(elt);
938 let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
939 let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
940 let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
941 self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
945 fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
946 assert_eq!(idx as c_uint as u64, idx);
947 unsafe { llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED) }
950 fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value, idx: u64) -> &'ll Value {
951 assert_eq!(idx as c_uint as u64, idx);
952 unsafe { llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint, UNNAMED) }
955 fn set_personality_fn(&mut self, personality: &'ll Value) {
957 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
961 fn cleanup_landing_pad(&mut self, ty: &'ll Type, pers_fn: &'ll Value) -> &'ll Value {
962 let landing_pad = self.landing_pad(ty, pers_fn, 1 /* FIXME should this be 0? */);
964 llvm::LLVMSetCleanup(landing_pad, llvm::True);
969 fn resume(&mut self, exn: &'ll Value) {
971 llvm::LLVMBuildResume(self.llbuilder, exn);
975 fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
976 let name = cstr!("cleanuppad");
978 llvm::LLVMRustBuildCleanupPad(
981 args.len() as c_uint,
986 Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
989 fn cleanup_ret(&mut self, funclet: &Funclet<'ll>, unwind: Option<&'ll BasicBlock>) {
991 llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind)
992 .expect("LLVM does not have support for cleanupret");
996 fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
997 let name = cstr!("catchpad");
999 llvm::LLVMRustBuildCatchPad(
1002 args.len() as c_uint,
1007 Funclet::new(ret.expect("LLVM does not have support for catchpad"))
1012 parent: Option<&'ll Value>,
1013 unwind: Option<&'ll BasicBlock>,
1014 handlers: &[&'ll BasicBlock],
1016 let name = cstr!("catchswitch");
1018 llvm::LLVMRustBuildCatchSwitch(
1022 handlers.len() as c_uint,
1026 let ret = ret.expect("LLVM does not have support for catchswitch");
1027 for handler in handlers {
1029 llvm::LLVMRustAddHandler(ret, handler);
1035 // Atomic Operations
1041 mut order: rustc_codegen_ssa::common::AtomicOrdering,
1042 failure_order: rustc_codegen_ssa::common::AtomicOrdering,
1045 let weak = if weak { llvm::True } else { llvm::False };
1046 if llvm_util::get_version() < (13, 0, 0) {
1047 use rustc_codegen_ssa::common::AtomicOrdering::*;
1048 // Older llvm has the pre-C++17 restriction on
1049 // success and failure memory ordering,
1050 // requiring the former to be at least as strong as the latter.
1051 // So, for llvm 12, we upgrade the success ordering to a stronger
1052 // one if necessary.
1053 match (order, failure_order) {
1054 (Relaxed, Acquire) => order = Acquire,
1055 (Release, Acquire) => order = AcquireRelease,
1056 (_, SequentiallyConsistent) => order = SequentiallyConsistent,
1061 llvm::LLVMRustBuildAtomicCmpXchg(
1066 AtomicOrdering::from_generic(order),
1067 AtomicOrdering::from_generic(failure_order),
1074 op: rustc_codegen_ssa::common::AtomicRmwBinOp,
1077 order: rustc_codegen_ssa::common::AtomicOrdering,
1080 llvm::LLVMBuildAtomicRMW(
1082 AtomicRmwBinOp::from_generic(op),
1085 AtomicOrdering::from_generic(order),
1093 order: rustc_codegen_ssa::common::AtomicOrdering,
1094 scope: rustc_codegen_ssa::common::SynchronizationScope,
1097 llvm::LLVMRustBuildAtomicFence(
1099 AtomicOrdering::from_generic(order),
1100 SynchronizationScope::from_generic(scope),
1105 fn set_invariant_load(&mut self, load: &'ll Value) {
1107 llvm::LLVMSetMetadata(
1109 llvm::MD_invariant_load as c_uint,
1110 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1115 fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
1116 self.call_lifetime_intrinsic("llvm.lifetime.start.p0i8", ptr, size);
1119 fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
1120 self.call_lifetime_intrinsic("llvm.lifetime.end.p0i8", ptr, size);
1123 fn instrprof_increment(
1125 fn_name: &'ll Value,
1127 num_counters: &'ll Value,
1131 "instrprof_increment() with args ({:?}, {:?}, {:?}, {:?})",
1132 fn_name, hash, num_counters, index
1135 let llfn = unsafe { llvm::LLVMRustGetInstrProfIncrementIntrinsic(self.cx().llmod) };
1136 let llty = self.cx.type_func(
1137 &[self.cx.type_i8p(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_i32()],
1138 self.cx.type_void(),
1140 let args = &[fn_name, hash, num_counters, index];
1141 let args = self.check_call("call", llty, llfn, args);
1144 let _ = llvm::LLVMRustBuildCall(
1148 args.as_ptr() as *const &llvm::Value,
1149 args.len() as c_uint,
1159 args: &[&'ll Value],
1160 funclet: Option<&Funclet<'ll>>,
1162 debug!("call {:?} with args ({:?})", llfn, args);
1164 let args = self.check_call("call", llty, llfn, args);
1165 let bundle = funclet.map(|funclet| funclet.bundle());
1166 let bundle = bundle.as_ref().map(|b| &*b.raw);
1169 llvm::LLVMRustBuildCall(
1173 args.as_ptr() as *const &llvm::Value,
1174 args.len() as c_uint,
1180 fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1181 unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
1184 fn do_not_inline(&mut self, llret: &'ll Value) {
1185 let noinline = llvm::AttributeKind::NoInline.create_attr(self.llcx);
1186 attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[noinline]);
1190 impl<'ll> StaticBuilderMethods for Builder<'_, 'll, '_> {
1191 fn get_static(&mut self, def_id: DefId) -> &'ll Value {
1192 // Forward to the `get_static` method of `CodegenCx`
1193 self.cx().get_static(def_id)
1197 impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
1198 fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
1199 // Create a fresh builder from the crate context.
1200 let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(cx.llcx) };
1201 Builder { llbuilder, cx }
1204 pub fn llfn(&self) -> &'ll Value {
1205 unsafe { llvm::LLVMGetBasicBlockParent(self.llbb()) }
1208 fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
1210 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
1214 fn align_metadata(&mut self, load: &'ll Value, align: Align) {
1216 let v = [self.cx.const_u64(align.bytes())];
1218 llvm::LLVMSetMetadata(
1220 llvm::MD_align as c_uint,
1221 llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
1226 fn noundef_metadata(&mut self, load: &'ll Value) {
1228 llvm::LLVMSetMetadata(
1230 llvm::MD_noundef as c_uint,
1231 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1236 pub fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1237 unsafe { llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs) }
1240 pub fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1241 unsafe { llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs) }
1244 pub fn insert_element(
1250 unsafe { llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, UNNAMED) }
1253 pub fn shuffle_vector(
1259 unsafe { llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, UNNAMED) }
1262 pub fn vector_reduce_fadd(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1263 unsafe { llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src) }
1265 pub fn vector_reduce_fmul(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1266 unsafe { llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src) }
1268 pub fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1270 let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
1271 llvm::LLVMRustSetFastMath(instr);
1275 pub fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1277 let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
1278 llvm::LLVMRustSetFastMath(instr);
1282 pub fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
1283 unsafe { llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src) }
1285 pub fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
1286 unsafe { llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src) }
1288 pub fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
1289 unsafe { llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src) }
1291 pub fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
1292 unsafe { llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src) }
1294 pub fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
1295 unsafe { llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src) }
1297 pub fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
1299 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false)
1302 pub fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
1304 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false)
1307 pub fn vector_reduce_fmin_fast(&mut self, src: &'ll Value) -> &'ll Value {
1310 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true);
1311 llvm::LLVMRustSetFastMath(instr);
1315 pub fn vector_reduce_fmax_fast(&mut self, src: &'ll Value) -> &'ll Value {
1318 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true);
1319 llvm::LLVMRustSetFastMath(instr);
1323 pub fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1324 unsafe { llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed) }
1326 pub fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1327 unsafe { llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed) }
1330 pub fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
1332 llvm::LLVMAddClause(landing_pad, clause);
1336 pub fn catch_ret(&mut self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
1338 unsafe { llvm::LLVMRustBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind) };
1339 ret.expect("LLVM does not have support for catchret")
1342 fn check_store(&mut self, val: &'ll Value, ptr: &'ll Value) -> &'ll Value {
1343 let dest_ptr_ty = self.cx.val_ty(ptr);
1344 let stored_ty = self.cx.val_ty(val);
1345 let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
1347 assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
1349 if dest_ptr_ty == stored_ptr_ty {
1353 "type mismatch in store. \
1354 Expected {:?}, got {:?}; inserting bitcast",
1355 dest_ptr_ty, stored_ptr_ty
1357 self.bitcast(ptr, stored_ptr_ty)
1366 args: &'b [&'ll Value],
1367 ) -> Cow<'b, [&'ll Value]> {
1369 self.cx.type_kind(fn_ty) == TypeKind::Function,
1370 "builder::{} not passed a function, but {:?}",
1375 let param_tys = self.cx.func_params_types(fn_ty);
1377 let all_args_match = iter::zip(¶m_tys, args.iter().map(|&v| self.val_ty(v)))
1378 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1381 return Cow::Borrowed(args);
1384 let casted_args: Vec<_> = iter::zip(param_tys, args)
1386 .map(|(i, (expected_ty, &actual_val))| {
1387 let actual_ty = self.val_ty(actual_val);
1388 if expected_ty != actual_ty {
1390 "type mismatch in function call of {:?}. \
1391 Expected {:?} for param {}, got {:?}; injecting bitcast",
1392 llfn, expected_ty, i, actual_ty
1394 self.bitcast(actual_val, expected_ty)
1401 Cow::Owned(casted_args)
1404 pub fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
1405 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
1408 pub(crate) fn call_intrinsic(&mut self, intrinsic: &str, args: &[&'ll Value]) -> &'ll Value {
1409 let (ty, f) = self.cx.get_intrinsic(intrinsic);
1410 self.call(ty, f, args, None)
1413 fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
1414 let size = size.bytes();
1419 if !self.cx().sess().emit_lifetime_markers() {
1423 let ptr = self.pointercast(ptr, self.cx.type_i8p());
1424 self.call_intrinsic(intrinsic, &[self.cx.const_u64(size), ptr]);
1430 vals: &[&'ll Value],
1431 bbs: &[&'ll BasicBlock],
1433 assert_eq!(vals.len(), bbs.len());
1434 let phi = unsafe { llvm::LLVMBuildPhi(self.llbuilder, ty, UNNAMED) };
1436 llvm::LLVMAddIncoming(phi, vals.as_ptr(), bbs.as_ptr(), vals.len() as c_uint);
1441 fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
1443 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1447 fn fptoint_sat_broken_in_llvm(&self) -> bool {
1448 match self.tcx.sess.target.arch.as_ref() {
1449 // FIXME - https://bugs.llvm.org/show_bug.cgi?id=50083
1450 "riscv64" => llvm_util::get_version() < (13, 0, 0),
1460 ) -> Option<&'ll Value> {
1461 if !self.fptoint_sat_broken_in_llvm() {
1462 let src_ty = self.cx.val_ty(val);
1463 let (float_ty, int_ty, vector_length) = if self.cx.type_kind(src_ty) == TypeKind::Vector
1465 assert_eq!(self.cx.vector_length(src_ty), self.cx.vector_length(dest_ty));
1467 self.cx.element_type(src_ty),
1468 self.cx.element_type(dest_ty),
1469 Some(self.cx.vector_length(src_ty)),
1472 (src_ty, dest_ty, None)
1474 let float_width = self.cx.float_width(float_ty);
1475 let int_width = self.cx.int_width(int_ty);
1477 let instr = if signed { "fptosi" } else { "fptoui" };
1478 let name = if let Some(vector_length) = vector_length {
1480 "llvm.{}.sat.v{}i{}.v{}f{}",
1481 instr, vector_length, int_width, vector_length, float_width
1484 format!("llvm.{}.sat.i{}.f{}", instr, int_width, float_width)
1487 self.declare_cfn(&name, llvm::UnnamedAddr::No, self.type_func(&[src_ty], dest_ty));
1488 Some(self.call(self.type_func(&[src_ty], dest_ty), f, &[val], None))
1494 pub(crate) fn landing_pad(
1497 pers_fn: &'ll Value,
1500 // Use LLVMSetPersonalityFn to set the personality. It supports arbitrary Consts while,
1501 // LLVMBuildLandingPad requires the argument to be a Function (as of LLVM 12). The
1502 // personality lives on the parent function anyway.
1503 self.set_personality_fn(pers_fn);
1505 llvm::LLVMBuildLandingPad(self.llbuilder, ty, None, num_clauses as c_uint, UNNAMED)