1 use crate::common::Funclet;
2 use crate::context::CodegenCx;
3 use crate::llvm::{self, BasicBlock, False};
4 use crate::llvm::{AtomicOrdering, AtomicRmwBinOp, SynchronizationScope};
6 use crate::type_::Type;
7 use crate::type_of::LayoutLlvmExt;
8 use crate::value::Value;
10 use libc::{c_char, c_uint};
11 use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, TypeKind};
12 use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
13 use rustc_codegen_ssa::mir::place::PlaceRef;
14 use rustc_codegen_ssa::traits::*;
15 use rustc_codegen_ssa::MemFlags;
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};
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 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 ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
73 fn tcx(&self) -> TyCtxt<'tcx> {
78 impl ty::layout::HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
79 fn param_env(&self) -> ty::ParamEnv<'tcx> {
84 impl HasTargetSpec for Builder<'_, '_, 'tcx> {
86 fn target_spec(&self) -> &Target {
87 &self.cx.target_spec()
91 impl abi::LayoutOf for Builder<'_, '_, 'tcx> {
93 type TyAndLayout = TyAndLayout<'tcx>;
95 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
100 impl Deref for Builder<'_, 'll, 'tcx> {
101 type Target = CodegenCx<'ll, 'tcx>;
104 fn deref(&self) -> &Self::Target {
109 impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
110 type CodegenCx = CodegenCx<'ll, 'tcx>;
113 macro_rules! builder_methods_for_value_instructions {
114 ($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
115 $(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
117 llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED)
123 impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
124 fn build(cx: &'a CodegenCx<'ll, 'tcx>, llbb: &'ll BasicBlock) -> Self {
125 let bx = Builder::with_cx(cx);
127 llvm::LLVMPositionBuilderAtEnd(bx.llbuilder, llbb);
132 fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
136 fn llbb(&self) -> &'ll BasicBlock {
137 unsafe { llvm::LLVMGetInsertBlock(self.llbuilder) }
140 fn set_span(&mut self, _span: Span) {}
142 fn append_block(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &str) -> &'ll BasicBlock {
144 let name = SmallCStr::new(name);
145 llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name.as_ptr())
149 fn append_sibling_block(&mut self, name: &str) -> &'ll BasicBlock {
150 Self::append_block(self.cx, self.llfn(), name)
153 fn build_sibling_block(&mut self, name: &str) -> Self {
154 let llbb = self.append_sibling_block(name);
155 Self::build(self.cx, llbb)
158 fn ret_void(&mut self) {
160 llvm::LLVMBuildRetVoid(self.llbuilder);
164 fn ret(&mut self, v: &'ll Value) {
166 llvm::LLVMBuildRet(self.llbuilder, v);
170 fn br(&mut self, dest: &'ll BasicBlock) {
172 llvm::LLVMBuildBr(self.llbuilder, dest);
179 then_llbb: &'ll BasicBlock,
180 else_llbb: &'ll BasicBlock,
183 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
190 else_llbb: &'ll BasicBlock,
191 cases: impl ExactSizeIterator<Item = (u128, &'ll BasicBlock)>,
194 unsafe { llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, cases.len() as c_uint) };
195 for (on_val, dest) in cases {
196 let on_val = self.const_uint_big(self.val_ty(v), on_val);
197 unsafe { llvm::LLVMAddCase(switch, on_val, dest) }
205 then: &'ll BasicBlock,
206 catch: &'ll BasicBlock,
207 funclet: Option<&Funclet<'ll>>,
209 debug!("invoke {:?} with args ({:?})", llfn, args);
211 let args = self.check_call("invoke", llfn, args);
212 let bundle = funclet.map(|funclet| funclet.bundle());
213 let bundle = bundle.as_ref().map(|b| &*b.raw);
216 llvm::LLVMRustBuildInvoke(
220 args.len() as c_uint,
229 fn unreachable(&mut self) {
231 llvm::LLVMBuildUnreachable(self.llbuilder);
235 builder_methods_for_value_instructions! {
236 add(a, b) => LLVMBuildAdd,
237 fadd(a, b) => LLVMBuildFAdd,
238 sub(a, b) => LLVMBuildSub,
239 fsub(a, b) => LLVMBuildFSub,
240 mul(a, b) => LLVMBuildMul,
241 fmul(a, b) => LLVMBuildFMul,
242 udiv(a, b) => LLVMBuildUDiv,
243 exactudiv(a, b) => LLVMBuildExactUDiv,
244 sdiv(a, b) => LLVMBuildSDiv,
245 exactsdiv(a, b) => LLVMBuildExactSDiv,
246 fdiv(a, b) => LLVMBuildFDiv,
247 urem(a, b) => LLVMBuildURem,
248 srem(a, b) => LLVMBuildSRem,
249 frem(a, b) => LLVMBuildFRem,
250 shl(a, b) => LLVMBuildShl,
251 lshr(a, b) => LLVMBuildLShr,
252 ashr(a, b) => LLVMBuildAShr,
253 and(a, b) => LLVMBuildAnd,
254 or(a, b) => LLVMBuildOr,
255 xor(a, b) => LLVMBuildXor,
256 neg(x) => LLVMBuildNeg,
257 fneg(x) => LLVMBuildFNeg,
258 not(x) => LLVMBuildNot,
259 unchecked_sadd(x, y) => LLVMBuildNSWAdd,
260 unchecked_uadd(x, y) => LLVMBuildNUWAdd,
261 unchecked_ssub(x, y) => LLVMBuildNSWSub,
262 unchecked_usub(x, y) => LLVMBuildNUWSub,
263 unchecked_smul(x, y) => LLVMBuildNSWMul,
264 unchecked_umul(x, y) => LLVMBuildNUWMul,
267 fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
269 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
270 llvm::LLVMRustSetFastMath(instr);
275 fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
277 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
278 llvm::LLVMRustSetFastMath(instr);
283 fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
285 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
286 llvm::LLVMRustSetFastMath(instr);
291 fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
293 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
294 llvm::LLVMRustSetFastMath(instr);
299 fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
301 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
302 llvm::LLVMRustSetFastMath(instr);
313 ) -> (Self::Value, Self::Value) {
314 use rustc_middle::ty::{Int, Uint};
315 use rustc_middle::ty::{IntTy::*, UintTy::*};
317 let new_kind = match ty.kind() {
318 Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.pointer_width)),
319 Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.pointer_width)),
320 t @ (Uint(_) | Int(_)) => t.clone(),
321 _ => panic!("tried to get overflow intrinsic for op applied to non-int type"),
324 let name = match oop {
325 OverflowOp::Add => match new_kind {
326 Int(I8) => "llvm.sadd.with.overflow.i8",
327 Int(I16) => "llvm.sadd.with.overflow.i16",
328 Int(I32) => "llvm.sadd.with.overflow.i32",
329 Int(I64) => "llvm.sadd.with.overflow.i64",
330 Int(I128) => "llvm.sadd.with.overflow.i128",
332 Uint(U8) => "llvm.uadd.with.overflow.i8",
333 Uint(U16) => "llvm.uadd.with.overflow.i16",
334 Uint(U32) => "llvm.uadd.with.overflow.i32",
335 Uint(U64) => "llvm.uadd.with.overflow.i64",
336 Uint(U128) => "llvm.uadd.with.overflow.i128",
340 OverflowOp::Sub => match new_kind {
341 Int(I8) => "llvm.ssub.with.overflow.i8",
342 Int(I16) => "llvm.ssub.with.overflow.i16",
343 Int(I32) => "llvm.ssub.with.overflow.i32",
344 Int(I64) => "llvm.ssub.with.overflow.i64",
345 Int(I128) => "llvm.ssub.with.overflow.i128",
347 Uint(U8) => "llvm.usub.with.overflow.i8",
348 Uint(U16) => "llvm.usub.with.overflow.i16",
349 Uint(U32) => "llvm.usub.with.overflow.i32",
350 Uint(U64) => "llvm.usub.with.overflow.i64",
351 Uint(U128) => "llvm.usub.with.overflow.i128",
355 OverflowOp::Mul => match new_kind {
356 Int(I8) => "llvm.smul.with.overflow.i8",
357 Int(I16) => "llvm.smul.with.overflow.i16",
358 Int(I32) => "llvm.smul.with.overflow.i32",
359 Int(I64) => "llvm.smul.with.overflow.i64",
360 Int(I128) => "llvm.smul.with.overflow.i128",
362 Uint(U8) => "llvm.umul.with.overflow.i8",
363 Uint(U16) => "llvm.umul.with.overflow.i16",
364 Uint(U32) => "llvm.umul.with.overflow.i32",
365 Uint(U64) => "llvm.umul.with.overflow.i64",
366 Uint(U128) => "llvm.umul.with.overflow.i128",
372 let intrinsic = self.get_intrinsic(&name);
373 let res = self.call(intrinsic, &[lhs, rhs], None);
374 (self.extract_value(res, 0), self.extract_value(res, 1))
377 fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
378 if self.cx().val_ty(val) == self.cx().type_i1() {
379 self.zext(val, self.cx().type_i8())
384 fn to_immediate_scalar(&mut self, val: Self::Value, scalar: &abi::Scalar) -> Self::Value {
385 if scalar.is_bool() {
386 return self.trunc(val, self.cx().type_i1());
391 fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
392 let mut bx = Builder::with_cx(self.cx);
393 bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
394 bx.dynamic_alloca(ty, align)
397 fn dynamic_alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
399 let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
400 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
405 fn array_alloca(&mut self, ty: &'ll Type, len: &'ll Value, align: Align) -> &'ll Value {
407 let alloca = llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED);
408 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
413 fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
415 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
416 llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
421 fn volatile_load(&mut self, ty: &'ll Type, ptr: &'ll Value) -> &'ll Value {
423 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
424 llvm::LLVMSetVolatile(load, llvm::True);
433 order: rustc_codegen_ssa::common::AtomicOrdering,
437 let load = llvm::LLVMRustBuildAtomicLoad(
442 AtomicOrdering::from_generic(order),
444 // LLVM requires the alignment of atomic loads to be at least the size of the type.
445 llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
450 fn load_operand(&mut self, place: PlaceRef<'tcx, &'ll Value>) -> OperandRef<'tcx, &'ll Value> {
451 debug!("PlaceRef::load: {:?}", place);
453 assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
455 if place.layout.is_zst() {
456 return OperandRef::new_zst(self, place.layout);
459 fn scalar_load_metadata<'a, 'll, 'tcx>(
460 bx: &mut Builder<'a, 'll, 'tcx>,
462 scalar: &abi::Scalar,
464 let vr = scalar.valid_range.clone();
467 let range = scalar.valid_range_exclusive(bx);
468 if range.start != range.end {
469 bx.range_metadata(load, range);
472 abi::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
473 bx.nonnull_metadata(load);
479 let val = if let Some(llextra) = place.llextra {
480 OperandValue::Ref(place.llval, Some(llextra), place.align)
481 } else if place.layout.is_llvm_immediate() {
482 let mut const_llval = None;
484 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
485 if llvm::LLVMIsGlobalConstant(global) == llvm::True {
486 const_llval = llvm::LLVMGetInitializer(global);
490 let llval = const_llval.unwrap_or_else(|| {
491 let load = self.load(place.layout.llvm_type(self), place.llval, place.align);
492 if let abi::Abi::Scalar(ref scalar) = place.layout.abi {
493 scalar_load_metadata(self, load, scalar);
497 OperandValue::Immediate(self.to_immediate(llval, place.layout))
498 } else if let abi::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
499 let b_offset = a.value.size(self).align_to(b.value.align(self).abi);
500 let pair_ty = place.layout.llvm_type(self);
502 let mut load = |i, scalar: &abi::Scalar, align| {
503 let llptr = self.struct_gep(pair_ty, place.llval, i as u64);
504 let llty = place.layout.scalar_pair_element_llvm_type(self, i, false);
505 let load = self.load(llty, llptr, align);
506 scalar_load_metadata(self, load, scalar);
507 self.to_immediate_scalar(load, scalar)
511 load(0, a, place.align),
512 load(1, b, place.align.restrict_for_offset(b_offset)),
515 OperandValue::Ref(place.llval, None, place.align)
518 OperandRef { val, layout: place.layout }
521 fn write_operand_repeatedly(
523 cg_elem: OperandRef<'tcx, &'ll Value>,
525 dest: PlaceRef<'tcx, &'ll Value>,
527 let zero = self.const_usize(0);
528 let count = self.const_usize(count);
529 let start = dest.project_index(&mut self, zero).llval;
530 let end = dest.project_index(&mut self, count).llval;
532 let mut header_bx = self.build_sibling_block("repeat_loop_header");
533 let mut body_bx = self.build_sibling_block("repeat_loop_body");
534 let next_bx = self.build_sibling_block("repeat_loop_next");
536 self.br(header_bx.llbb());
537 let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
539 let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
540 header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
542 let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
545 .store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
547 let next = body_bx.inbounds_gep(
548 self.backend_type(cg_elem.layout),
550 &[self.const_usize(1)],
552 body_bx.br(header_bx.llbb());
553 header_bx.add_incoming_to_phi(current, next, body_bx.llbb());
558 fn range_metadata(&mut self, load: &'ll Value, range: Range<u128>) {
559 if self.sess().target.arch == "amdgpu" {
560 // amdgpu/LLVM does something weird and thinks a i64 value is
561 // split into a v2i32, halving the bitwidth LLVM expects,
562 // tripping an assertion. So, for now, just disable this
568 let llty = self.cx.val_ty(load);
570 self.cx.const_uint_big(llty, range.start),
571 self.cx.const_uint_big(llty, range.end),
574 llvm::LLVMSetMetadata(
576 llvm::MD_range as c_uint,
577 llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
582 fn nonnull_metadata(&mut self, load: &'ll Value) {
584 llvm::LLVMSetMetadata(
586 llvm::MD_nonnull as c_uint,
587 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
592 fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
593 self.store_with_flags(val, ptr, align, MemFlags::empty())
603 debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
604 let ptr = self.check_store(val, ptr);
606 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
608 if flags.contains(MemFlags::UNALIGNED) { 1 } else { align.bytes() as c_uint };
609 llvm::LLVMSetAlignment(store, align);
610 if flags.contains(MemFlags::VOLATILE) {
611 llvm::LLVMSetVolatile(store, llvm::True);
613 if flags.contains(MemFlags::NONTEMPORAL) {
614 // According to LLVM [1] building a nontemporal store must
615 // *always* point to a metadata value of the integer 1.
617 // [1]: https://llvm.org/docs/LangRef.html#store-instruction
618 let one = self.cx.const_i32(1);
619 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
620 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
630 order: rustc_codegen_ssa::common::AtomicOrdering,
633 debug!("Store {:?} -> {:?}", val, ptr);
634 let ptr = self.check_store(val, ptr);
636 let store = llvm::LLVMRustBuildAtomicStore(
640 AtomicOrdering::from_generic(order),
642 // LLVM requires the alignment of atomic stores to be at least the size of the type.
643 llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
647 fn gep(&mut self, ty: &'ll Type, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
654 indices.len() as c_uint,
664 indices: &[&'ll Value],
667 llvm::LLVMBuildInBoundsGEP2(
672 indices.len() as c_uint,
678 fn struct_gep(&mut self, ty: &'ll Type, ptr: &'ll Value, idx: u64) -> &'ll Value {
679 assert_eq!(idx as c_uint as u64, idx);
680 unsafe { llvm::LLVMBuildStructGEP2(self.llbuilder, ty, ptr, idx as c_uint, UNNAMED) }
684 fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
685 unsafe { llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
688 fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
689 unsafe { llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED) }
692 fn fptoui_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
693 if llvm_util::get_version() >= (12, 0, 0) && !self.fptoint_sat_broken_in_llvm() {
694 let src_ty = self.cx.val_ty(val);
695 let float_width = self.cx.float_width(src_ty);
696 let int_width = self.cx.int_width(dest_ty);
697 let name = format!("llvm.fptoui.sat.i{}.f{}", int_width, float_width);
698 let intrinsic = self.get_intrinsic(&name);
699 return Some(self.call(intrinsic, &[val], None));
705 fn fptosi_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
706 if llvm_util::get_version() >= (12, 0, 0) && !self.fptoint_sat_broken_in_llvm() {
707 let src_ty = self.cx.val_ty(val);
708 let float_width = self.cx.float_width(src_ty);
709 let int_width = self.cx.int_width(dest_ty);
710 let name = format!("llvm.fptosi.sat.i{}.f{}", int_width, float_width);
711 let intrinsic = self.get_intrinsic(&name);
712 return Some(self.call(intrinsic, &[val], None));
718 fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
719 // On WebAssembly the `fptoui` and `fptosi` instructions currently have
720 // poor codegen. The reason for this is that the corresponding wasm
721 // instructions, `i32.trunc_f32_s` for example, will trap when the float
722 // is out-of-bounds, infinity, or nan. This means that LLVM
723 // automatically inserts control flow around `fptoui` and `fptosi`
724 // because the LLVM instruction `fptoui` is defined as producing a
725 // poison value, not having UB on out-of-bounds values.
727 // This method, however, is only used with non-saturating casts that
728 // have UB on out-of-bounds values. This means that it's ok if we use
729 // the raw wasm instruction since out-of-bounds values can do whatever
730 // we like. To ensure that LLVM picks the right instruction we choose
731 // the raw wasm intrinsic functions which avoid LLVM inserting all the
732 // other control flow automatically.
733 if self.sess().target.arch == "wasm32" {
734 let src_ty = self.cx.val_ty(val);
735 if self.cx.type_kind(src_ty) != TypeKind::Vector {
736 let float_width = self.cx.float_width(src_ty);
737 let int_width = self.cx.int_width(dest_ty);
738 let name = match (int_width, float_width) {
739 (32, 32) => Some("llvm.wasm.trunc.unsigned.i32.f32"),
740 (32, 64) => Some("llvm.wasm.trunc.unsigned.i32.f64"),
741 (64, 32) => Some("llvm.wasm.trunc.unsigned.i64.f32"),
742 (64, 64) => Some("llvm.wasm.trunc.unsigned.i64.f64"),
745 if let Some(name) = name {
746 let intrinsic = self.get_intrinsic(name);
747 return self.call(intrinsic, &[val], None);
751 unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) }
754 fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
755 // see `fptoui` above for why wasm is different here
756 if self.sess().target.arch == "wasm32" {
757 let src_ty = self.cx.val_ty(val);
758 if self.cx.type_kind(src_ty) != TypeKind::Vector {
759 let float_width = self.cx.float_width(src_ty);
760 let int_width = self.cx.int_width(dest_ty);
761 let name = match (int_width, float_width) {
762 (32, 32) => Some("llvm.wasm.trunc.signed.i32.f32"),
763 (32, 64) => Some("llvm.wasm.trunc.signed.i32.f64"),
764 (64, 32) => Some("llvm.wasm.trunc.signed.i64.f32"),
765 (64, 64) => Some("llvm.wasm.trunc.signed.i64.f64"),
768 if let Some(name) = name {
769 let intrinsic = self.get_intrinsic(name);
770 return self.call(intrinsic, &[val], None);
774 unsafe { llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty, UNNAMED) }
777 fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
778 unsafe { llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
781 fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
782 unsafe { llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
785 fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
786 unsafe { llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
789 fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
790 unsafe { llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, UNNAMED) }
793 fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
794 unsafe { llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, UNNAMED) }
797 fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
798 unsafe { llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, UNNAMED) }
801 fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
802 unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
805 fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
806 unsafe { llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed) }
809 fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
810 unsafe { llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED) }
814 fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
815 let op = llvm::IntPredicate::from_generic(op);
816 unsafe { llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
819 fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
820 unsafe { llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
823 /* Miscellaneous instructions */
833 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
834 let size = self.intcast(size, self.type_isize(), false);
835 let is_volatile = flags.contains(MemFlags::VOLATILE);
836 let dst = self.pointercast(dst, self.type_i8p());
837 let src = self.pointercast(src, self.type_i8p());
839 llvm::LLVMRustBuildMemCpy(
842 dst_align.bytes() as c_uint,
844 src_align.bytes() as c_uint,
860 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memmove not supported");
861 let size = self.intcast(size, self.type_isize(), false);
862 let is_volatile = flags.contains(MemFlags::VOLATILE);
863 let dst = self.pointercast(dst, self.type_i8p());
864 let src = self.pointercast(src, self.type_i8p());
866 llvm::LLVMRustBuildMemMove(
869 dst_align.bytes() as c_uint,
871 src_align.bytes() as c_uint,
881 fill_byte: &'ll Value,
886 let is_volatile = flags.contains(MemFlags::VOLATILE);
887 let ptr = self.pointercast(ptr, self.type_i8p());
889 llvm::LLVMRustBuildMemSet(
892 align.bytes() as c_uint,
903 then_val: &'ll Value,
904 else_val: &'ll Value,
906 unsafe { llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, UNNAMED) }
909 fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
910 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
913 fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
914 unsafe { llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED) }
917 fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
919 let elt_ty = self.cx.val_ty(elt);
920 let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
921 let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
922 let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
923 self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
927 fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
928 assert_eq!(idx as c_uint as u64, idx);
929 unsafe { llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED) }
932 fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value, idx: u64) -> &'ll Value {
933 assert_eq!(idx as c_uint as u64, idx);
934 unsafe { llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint, UNNAMED) }
943 // Use LLVMSetPersonalityFn to set the personality. It supports arbitrary Consts while,
944 // LLVMBuildLandingPad requires the argument to be a Function (as of LLVM 12). The
945 // personality lives on the parent function anyway.
946 self.set_personality_fn(pers_fn);
948 llvm::LLVMBuildLandingPad(self.llbuilder, ty, None, num_clauses as c_uint, UNNAMED)
952 fn set_cleanup(&mut self, landing_pad: &'ll Value) {
954 llvm::LLVMSetCleanup(landing_pad, llvm::True);
958 fn resume(&mut self, exn: &'ll Value) -> &'ll Value {
959 unsafe { llvm::LLVMBuildResume(self.llbuilder, exn) }
962 fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
963 let name = cstr!("cleanuppad");
965 llvm::LLVMRustBuildCleanupPad(
968 args.len() as c_uint,
973 Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
978 funclet: &Funclet<'ll>,
979 unwind: Option<&'ll BasicBlock>,
982 unsafe { llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind) };
983 ret.expect("LLVM does not have support for cleanupret")
986 fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
987 let name = cstr!("catchpad");
989 llvm::LLVMRustBuildCatchPad(
992 args.len() as c_uint,
997 Funclet::new(ret.expect("LLVM does not have support for catchpad"))
1002 parent: Option<&'ll Value>,
1003 unwind: Option<&'ll BasicBlock>,
1004 num_handlers: usize,
1006 let name = cstr!("catchswitch");
1008 llvm::LLVMRustBuildCatchSwitch(
1012 num_handlers as c_uint,
1016 ret.expect("LLVM does not have support for catchswitch")
1019 fn add_handler(&mut self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
1021 llvm::LLVMRustAddHandler(catch_switch, handler);
1025 fn set_personality_fn(&mut self, personality: &'ll Value) {
1027 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
1031 // Atomic Operations
1037 order: rustc_codegen_ssa::common::AtomicOrdering,
1038 failure_order: rustc_codegen_ssa::common::AtomicOrdering,
1041 let weak = if weak { llvm::True } else { llvm::False };
1043 llvm::LLVMRustBuildAtomicCmpXchg(
1048 AtomicOrdering::from_generic(order),
1049 AtomicOrdering::from_generic(failure_order),
1056 op: rustc_codegen_ssa::common::AtomicRmwBinOp,
1059 order: rustc_codegen_ssa::common::AtomicOrdering,
1062 llvm::LLVMBuildAtomicRMW(
1064 AtomicRmwBinOp::from_generic(op),
1067 AtomicOrdering::from_generic(order),
1075 order: rustc_codegen_ssa::common::AtomicOrdering,
1076 scope: rustc_codegen_ssa::common::SynchronizationScope,
1079 llvm::LLVMRustBuildAtomicFence(
1081 AtomicOrdering::from_generic(order),
1082 SynchronizationScope::from_generic(scope),
1087 fn set_invariant_load(&mut self, load: &'ll Value) {
1089 llvm::LLVMSetMetadata(
1091 llvm::MD_invariant_load as c_uint,
1092 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1097 fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
1098 self.call_lifetime_intrinsic("llvm.lifetime.start.p0i8", ptr, size);
1101 fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
1102 self.call_lifetime_intrinsic("llvm.lifetime.end.p0i8", ptr, size);
1105 fn instrprof_increment(
1107 fn_name: &'ll Value,
1109 num_counters: &'ll Value,
1113 "instrprof_increment() with args ({:?}, {:?}, {:?}, {:?})",
1114 fn_name, hash, num_counters, index
1117 let llfn = unsafe { llvm::LLVMRustGetInstrProfIncrementIntrinsic(self.cx().llmod) };
1118 let args = &[fn_name, hash, num_counters, index];
1119 let args = self.check_call("call", llfn, args);
1122 let _ = llvm::LLVMRustBuildCall(
1125 args.as_ptr() as *const &llvm::Value,
1126 args.len() as c_uint,
1135 args: &[&'ll Value],
1136 funclet: Option<&Funclet<'ll>>,
1138 debug!("call {:?} with args ({:?})", llfn, args);
1140 let args = self.check_call("call", llfn, args);
1141 let bundle = funclet.map(|funclet| funclet.bundle());
1142 let bundle = bundle.as_ref().map(|b| &*b.raw);
1145 llvm::LLVMRustBuildCall(
1148 args.as_ptr() as *const &llvm::Value,
1149 args.len() as c_uint,
1155 fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1156 unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
1159 fn do_not_inline(&mut self, llret: &'ll Value) {
1160 llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1164 impl StaticBuilderMethods for Builder<'a, 'll, 'tcx> {
1165 fn get_static(&mut self, def_id: DefId) -> &'ll Value {
1166 // Forward to the `get_static` method of `CodegenCx`
1167 self.cx().get_static(def_id)
1171 impl Builder<'a, 'll, 'tcx> {
1172 fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
1173 // Create a fresh builder from the crate context.
1174 let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(cx.llcx) };
1175 Builder { llbuilder, cx }
1178 pub fn llfn(&self) -> &'ll Value {
1179 unsafe { llvm::LLVMGetBasicBlockParent(self.llbb()) }
1182 fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
1184 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
1188 pub fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1189 unsafe { llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs) }
1192 pub fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1193 unsafe { llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs) }
1196 pub fn insert_element(
1202 unsafe { llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, UNNAMED) }
1205 pub fn shuffle_vector(
1211 unsafe { llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, UNNAMED) }
1214 pub fn vector_reduce_fadd(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1215 unsafe { llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src) }
1217 pub fn vector_reduce_fmul(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1218 unsafe { llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src) }
1220 pub fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1222 let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
1223 llvm::LLVMRustSetFastMath(instr);
1227 pub fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1229 let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
1230 llvm::LLVMRustSetFastMath(instr);
1234 pub fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
1235 unsafe { llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src) }
1237 pub fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
1238 unsafe { llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src) }
1240 pub fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
1241 unsafe { llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src) }
1243 pub fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
1244 unsafe { llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src) }
1246 pub fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
1247 unsafe { llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src) }
1249 pub fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
1251 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false)
1254 pub fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
1256 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false)
1259 pub fn vector_reduce_fmin_fast(&mut self, src: &'ll Value) -> &'ll Value {
1262 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true);
1263 llvm::LLVMRustSetFastMath(instr);
1267 pub fn vector_reduce_fmax_fast(&mut self, src: &'ll Value) -> &'ll Value {
1270 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true);
1271 llvm::LLVMRustSetFastMath(instr);
1275 pub fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1276 unsafe { llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed) }
1278 pub fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1279 unsafe { llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed) }
1282 pub fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
1284 llvm::LLVMAddClause(landing_pad, clause);
1288 pub fn catch_ret(&mut self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
1290 unsafe { llvm::LLVMRustBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind) };
1291 ret.expect("LLVM does not have support for catchret")
1294 fn check_store(&mut self, val: &'ll Value, ptr: &'ll Value) -> &'ll Value {
1295 let dest_ptr_ty = self.cx.val_ty(ptr);
1296 let stored_ty = self.cx.val_ty(val);
1297 let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
1299 assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
1301 if dest_ptr_ty == stored_ptr_ty {
1305 "type mismatch in store. \
1306 Expected {:?}, got {:?}; inserting bitcast",
1307 dest_ptr_ty, stored_ptr_ty
1309 self.bitcast(ptr, stored_ptr_ty)
1317 args: &'b [&'ll Value],
1318 ) -> Cow<'b, [&'ll Value]> {
1319 let mut fn_ty = self.cx.val_ty(llfn);
1320 // Strip off pointers
1321 while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
1322 fn_ty = self.cx.element_type(fn_ty);
1326 self.cx.type_kind(fn_ty) == TypeKind::Function,
1327 "builder::{} not passed a function, but {:?}",
1332 let param_tys = self.cx.func_params_types(fn_ty);
1334 let all_args_match = iter::zip(¶m_tys, args.iter().map(|&v| self.val_ty(v)))
1335 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1338 return Cow::Borrowed(args);
1341 let casted_args: Vec<_> = iter::zip(param_tys, args)
1343 .map(|(i, (expected_ty, &actual_val))| {
1344 let actual_ty = self.val_ty(actual_val);
1345 if expected_ty != actual_ty {
1347 "type mismatch in function call of {:?}. \
1348 Expected {:?} for param {}, got {:?}; injecting bitcast",
1349 llfn, expected_ty, i, actual_ty
1351 self.bitcast(actual_val, expected_ty)
1358 Cow::Owned(casted_args)
1361 pub fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
1362 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
1365 fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
1366 let size = size.bytes();
1371 if !self.cx().sess().emit_lifetime_markers() {
1375 let lifetime_intrinsic = self.cx.get_intrinsic(intrinsic);
1377 let ptr = self.pointercast(ptr, self.cx.type_i8p());
1378 self.call(lifetime_intrinsic, &[self.cx.const_u64(size), ptr], None);
1384 vals: &[&'ll Value],
1385 bbs: &[&'ll BasicBlock],
1387 assert_eq!(vals.len(), bbs.len());
1388 let phi = unsafe { llvm::LLVMBuildPhi(self.llbuilder, ty, UNNAMED) };
1390 llvm::LLVMAddIncoming(phi, vals.as_ptr(), bbs.as_ptr(), vals.len() as c_uint);
1395 fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
1397 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1401 fn fptoint_sat_broken_in_llvm(&self) -> bool {
1402 match self.tcx.sess.target.arch.as_str() {
1403 // FIXME - https://bugs.llvm.org/show_bug.cgi?id=50083
1404 "riscv64" => llvm_util::get_version() < (13, 0, 0),