1use std::borrow::{Borrow, Cow};
2use std::iter;
3use std::ops::Deref;
4
5use rustc_ast::expand::typetree::FncTree;
6pub(crate) mod autodiff;
7pub(crate) mod gpu_offload;
8
9use libc::{c_char, c_uint};
10use rustc_abi as abi;
11use rustc_abi::{Align, Size, WrappingRange};
12use rustc_codegen_ssa::MemFlags;
13use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind};
14use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
15use rustc_codegen_ssa::mir::place::PlaceRef;
16use rustc_codegen_ssa::traits::*;
17use rustc_data_structures::small_c_str::SmallCStr;
18use rustc_hir::def_id::DefId;
19use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
20use rustc_middle::ty::layout::{
21 FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
22 TyAndLayout,
23};
24use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
25use rustc_sanitizers::{cfi, kcfi};
26use rustc_session::config::OptLevel;
27use rustc_span::Span;
28use rustc_target::callconv::{FnAbi, PassMode};
29use rustc_target::spec::{Arch, HasTargetSpec, SanitizerSet, Target};
30use smallvec::SmallVec;
31use tracing::{debug, instrument};
32
33use crate::abi::FnAbiLlvmExt;
34use crate::attributes;
35use crate::common::Funclet;
36use crate::context::{CodegenCx, FullCx, GenericCx, SCx};
37use crate::llvm::{
38 self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, FromGeneric, GEPNoWrapFlags, Metadata, TRUE,
39 ToLlvmBool, Type, Value,
40};
41use crate::type_of::LayoutLlvmExt;
42
43#[must_use]
44pub(crate) struct GenericBuilder<'a, 'll, CX: Borrow<SCx<'ll>>> {
45 pub llbuilder: &'ll mut llvm::Builder<'ll>,
46 pub cx: &'a GenericCx<'ll, CX>,
47}
48
49pub(crate) type SBuilder<'a, 'll> = GenericBuilder<'a, 'll, SCx<'ll>>;
50pub(crate) type Builder<'a, 'll, 'tcx> = GenericBuilder<'a, 'll, FullCx<'ll, 'tcx>>;
51
52impl<'a, 'll, CX: Borrow<SCx<'ll>>> Drop for GenericBuilder<'a, 'll, CX> {
53 fn drop(&mut self) {
54 unsafe {
55 llvm::LLVMDisposeBuilder(&mut *(self.llbuilder as *mut _));
56 }
57 }
58}
59
60impl<'a, 'll> SBuilder<'a, 'll> {
61 pub(crate) fn call(
62 &mut self,
63 llty: &'ll Type,
64 llfn: &'ll Value,
65 args: &[&'ll Value],
66 funclet: Option<&Funclet<'ll>>,
67 ) -> &'ll Value {
68 debug!("call {:?} with args ({:?})", llfn, args);
69
70 let args = self.check_call("call", llty, llfn, args);
71 let funclet_bundle = funclet.map(|funclet| funclet.bundle());
72 let mut bundles: SmallVec<[_; 2]> = SmallVec::new();
73 if let Some(funclet_bundle) = funclet_bundle {
74 bundles.push(funclet_bundle);
75 }
76
77 let call = unsafe {
78 llvm::LLVMBuildCallWithOperandBundles(
79 self.llbuilder,
80 llty,
81 llfn,
82 args.as_ptr() as *const &llvm::Value,
83 args.len() as c_uint,
84 bundles.as_ptr(),
85 bundles.len() as c_uint,
86 c"".as_ptr(),
87 )
88 };
89 call
90 }
91}
92
93impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
94 fn with_cx(scx: &'a GenericCx<'ll, CX>) -> Self {
95 let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(scx.deref().borrow().llcx) };
97 GenericBuilder { llbuilder, cx: scx }
98 }
99
100 pub(crate) fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
101 unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
102 }
103
104 pub(crate) fn ret_void(&mut self) {
105 llvm::LLVMBuildRetVoid(self.llbuilder);
106 }
107
108 pub(crate) fn ret(&mut self, v: &'ll Value) {
109 unsafe {
110 llvm::LLVMBuildRet(self.llbuilder, v);
111 }
112 }
113
114 pub(crate) fn build(cx: &'a GenericCx<'ll, CX>, llbb: &'ll BasicBlock) -> Self {
115 let bx = Self::with_cx(cx);
116 unsafe {
117 llvm::LLVMPositionBuilderAtEnd(bx.llbuilder, llbb);
118 }
119 bx
120 }
121
122 pub(crate) fn direct_alloca(&mut self, ty: &'ll Type, align: Align, name: &str) -> &'ll Value {
127 let val = unsafe {
128 let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
129 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
130 llvm::LLVMBuildPointerCast(self.llbuilder, alloca, self.cx.type_ptr(), UNNAMED)
132 };
133 if name != "" {
134 let name = std::ffi::CString::new(name).unwrap();
135 llvm::set_value_name(val, &name.as_bytes());
136 }
137 val
138 }
139
140 pub(crate) fn inbounds_gep(
141 &mut self,
142 ty: &'ll Type,
143 ptr: &'ll Value,
144 indices: &[&'ll Value],
145 ) -> &'ll Value {
146 unsafe {
147 llvm::LLVMBuildGEPWithNoWrapFlags(
148 self.llbuilder,
149 ty,
150 ptr,
151 indices.as_ptr(),
152 indices.len() as c_uint,
153 UNNAMED,
154 GEPNoWrapFlags::InBounds,
155 )
156 }
157 }
158
159 pub(crate) fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
160 debug!("Store {:?} -> {:?}", val, ptr);
161 assert_eq!(self.cx.type_kind(self.cx.val_ty(ptr)), TypeKind::Pointer);
162 unsafe {
163 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
164 llvm::LLVMSetAlignment(store, align.bytes() as c_uint);
165 store
166 }
167 }
168
169 pub(crate) fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
170 unsafe {
171 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
172 llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
173 load
174 }
175 }
176
177 fn memset(&mut self, ptr: &'ll Value, fill_byte: &'ll Value, size: &'ll Value, align: Align) {
178 unsafe {
179 llvm::LLVMRustBuildMemSet(
180 self.llbuilder,
181 ptr,
182 align.bytes() as c_uint,
183 fill_byte,
184 size,
185 false,
186 );
187 }
188 }
189}
190
191pub(crate) const UNNAMED: *const c_char = c"".as_ptr();
195
196impl<'ll, CX: Borrow<SCx<'ll>>> BackendTypes for GenericBuilder<'_, 'll, CX> {
197 type Value = <GenericCx<'ll, CX> as BackendTypes>::Value;
198 type Metadata = <GenericCx<'ll, CX> as BackendTypes>::Metadata;
199 type Function = <GenericCx<'ll, CX> as BackendTypes>::Function;
200 type BasicBlock = <GenericCx<'ll, CX> as BackendTypes>::BasicBlock;
201 type Type = <GenericCx<'ll, CX> as BackendTypes>::Type;
202 type Funclet = <GenericCx<'ll, CX> as BackendTypes>::Funclet;
203
204 type DIScope = <GenericCx<'ll, CX> as BackendTypes>::DIScope;
205 type DILocation = <GenericCx<'ll, CX> as BackendTypes>::DILocation;
206 type DIVariable = <GenericCx<'ll, CX> as BackendTypes>::DIVariable;
207}
208
209impl abi::HasDataLayout for Builder<'_, '_, '_> {
210 fn data_layout(&self) -> &abi::TargetDataLayout {
211 self.cx.data_layout()
212 }
213}
214
215impl<'tcx> ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
216 #[inline]
217 fn tcx(&self) -> TyCtxt<'tcx> {
218 self.cx.tcx
219 }
220}
221
222impl<'tcx> ty::layout::HasTypingEnv<'tcx> for Builder<'_, '_, 'tcx> {
223 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
224 self.cx.typing_env()
225 }
226}
227
228impl HasTargetSpec for Builder<'_, '_, '_> {
229 #[inline]
230 fn target_spec(&self) -> &Target {
231 self.cx.target_spec()
232 }
233}
234
235impl<'tcx> LayoutOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
236 #[inline]
237 fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
238 self.cx.handle_layout_err(err, span, ty)
239 }
240}
241
242impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
243 #[inline]
244 fn handle_fn_abi_err(
245 &self,
246 err: FnAbiError<'tcx>,
247 span: Span,
248 fn_abi_request: FnAbiRequest<'tcx>,
249 ) -> ! {
250 self.cx.handle_fn_abi_err(err, span, fn_abi_request)
251 }
252}
253
254impl<'ll, 'tcx> Deref for Builder<'_, 'll, 'tcx> {
255 type Target = CodegenCx<'ll, 'tcx>;
256
257 #[inline]
258 fn deref(&self) -> &Self::Target {
259 self.cx
260 }
261}
262
263macro_rules! math_builder_methods {
264 ($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
265 $(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
266 unsafe {
267 llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED)
268 }
269 })+
270 }
271}
272
273macro_rules! set_math_builder_methods {
274 ($($name:ident($($arg:ident),*) => ($llvm_capi:ident, $llvm_set_math:ident)),+ $(,)?) => {
275 $(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
276 unsafe {
277 let instr = llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED);
278 llvm::$llvm_set_math(instr);
279 instr
280 }
281 })+
282 }
283}
284
285impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
286 type CodegenCx = CodegenCx<'ll, 'tcx>;
287
288 fn build(cx: &'a CodegenCx<'ll, 'tcx>, llbb: &'ll BasicBlock) -> Self {
289 let bx = Builder::with_cx(cx);
290 unsafe {
291 llvm::LLVMPositionBuilderAtEnd(bx.llbuilder, llbb);
292 }
293 bx
294 }
295
296 fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
297 self.cx
298 }
299
300 fn llbb(&self) -> &'ll BasicBlock {
301 unsafe { llvm::LLVMGetInsertBlock(self.llbuilder) }
302 }
303
304 fn set_span(&mut self, _span: Span) {}
305
306 fn append_block(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &str) -> &'ll BasicBlock {
307 unsafe {
308 let name = SmallCStr::new(name);
309 llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name.as_ptr())
310 }
311 }
312
313 fn append_sibling_block(&mut self, name: &str) -> &'ll BasicBlock {
314 Self::append_block(self.cx, self.llfn(), name)
315 }
316
317 fn switch_to_block(&mut self, llbb: Self::BasicBlock) {
318 *self = Self::build(self.cx, llbb)
319 }
320
321 fn ret_void(&mut self) {
322 llvm::LLVMBuildRetVoid(self.llbuilder);
323 }
324
325 fn ret(&mut self, v: &'ll Value) {
326 unsafe {
327 llvm::LLVMBuildRet(self.llbuilder, v);
328 }
329 }
330
331 fn br(&mut self, dest: &'ll BasicBlock) {
332 unsafe {
333 llvm::LLVMBuildBr(self.llbuilder, dest);
334 }
335 }
336
337 fn cond_br(
338 &mut self,
339 cond: &'ll Value,
340 then_llbb: &'ll BasicBlock,
341 else_llbb: &'ll BasicBlock,
342 ) {
343 unsafe {
344 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
345 }
346 }
347
348 fn switch(
349 &mut self,
350 v: &'ll Value,
351 else_llbb: &'ll BasicBlock,
352 cases: impl ExactSizeIterator<Item = (u128, &'ll BasicBlock)>,
353 ) {
354 let switch =
355 unsafe { llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, cases.len() as c_uint) };
356 for (on_val, dest) in cases {
357 let on_val = self.const_uint_big(self.val_ty(v), on_val);
358 unsafe { llvm::LLVMAddCase(switch, on_val, dest) }
359 }
360 }
361
362 fn switch_with_weights(
363 &mut self,
364 v: Self::Value,
365 else_llbb: Self::BasicBlock,
366 else_is_cold: bool,
367 cases: impl ExactSizeIterator<Item = (u128, Self::BasicBlock, bool)>,
368 ) {
369 if self.cx.sess().opts.optimize == rustc_session::config::OptLevel::No {
370 self.switch(v, else_llbb, cases.map(|(val, dest, _)| (val, dest)));
371 return;
372 }
373
374 let id = self.cx.create_metadata(b"branch_weights");
375
376 let cold_weight = llvm::LLVMValueAsMetadata(self.cx.const_u32(1));
381 let hot_weight = llvm::LLVMValueAsMetadata(self.cx.const_u32(2000));
382 let weight =
383 |is_cold: bool| -> &Metadata { if is_cold { cold_weight } else { hot_weight } };
384
385 let mut md: SmallVec<[&Metadata; 16]> = SmallVec::with_capacity(cases.len() + 2);
386 md.push(id);
387 md.push(weight(else_is_cold));
388
389 let switch =
390 unsafe { llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, cases.len() as c_uint) };
391 for (on_val, dest, is_cold) in cases {
392 let on_val = self.const_uint_big(self.val_ty(v), on_val);
393 unsafe { llvm::LLVMAddCase(switch, on_val, dest) }
394 md.push(weight(is_cold));
395 }
396
397 self.cx.set_metadata_node(switch, llvm::MD_prof, &md);
398 }
399
400 fn invoke(
401 &mut self,
402 llty: &'ll Type,
403 fn_attrs: Option<&CodegenFnAttrs>,
404 fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
405 llfn: &'ll Value,
406 args: &[&'ll Value],
407 then: &'ll BasicBlock,
408 catch: &'ll BasicBlock,
409 funclet: Option<&Funclet<'ll>>,
410 instance: Option<Instance<'tcx>>,
411 ) -> &'ll Value {
412 debug!("invoke {:?} with args ({:?})", llfn, args);
413
414 let args = self.check_call("invoke", llty, llfn, args);
415 let funclet_bundle = funclet.map(|funclet| funclet.bundle());
416 let mut bundles: SmallVec<[_; 2]> = SmallVec::new();
417 if let Some(funclet_bundle) = funclet_bundle {
418 bundles.push(funclet_bundle);
419 }
420
421 self.cfi_type_test(fn_attrs, fn_abi, instance, llfn);
423
424 let kcfi_bundle = self.kcfi_operand_bundle(fn_attrs, fn_abi, instance, llfn);
426 if let Some(kcfi_bundle) = kcfi_bundle.as_ref().map(|b| b.as_ref()) {
427 bundles.push(kcfi_bundle);
428 }
429
430 let invoke = unsafe {
431 llvm::LLVMBuildInvokeWithOperandBundles(
432 self.llbuilder,
433 llty,
434 llfn,
435 args.as_ptr(),
436 args.len() as c_uint,
437 then,
438 catch,
439 bundles.as_ptr(),
440 bundles.len() as c_uint,
441 UNNAMED,
442 )
443 };
444 if let Some(fn_abi) = fn_abi {
445 fn_abi.apply_attrs_callsite(self, invoke);
446 }
447 invoke
448 }
449
450 fn unreachable(&mut self) {
451 unsafe {
452 llvm::LLVMBuildUnreachable(self.llbuilder);
453 }
454 }
455
456 math_builder_methods! {
457 add(a, b) => LLVMBuildAdd,
458 fadd(a, b) => LLVMBuildFAdd,
459 sub(a, b) => LLVMBuildSub,
460 fsub(a, b) => LLVMBuildFSub,
461 mul(a, b) => LLVMBuildMul,
462 fmul(a, b) => LLVMBuildFMul,
463 udiv(a, b) => LLVMBuildUDiv,
464 exactudiv(a, b) => LLVMBuildExactUDiv,
465 sdiv(a, b) => LLVMBuildSDiv,
466 exactsdiv(a, b) => LLVMBuildExactSDiv,
467 fdiv(a, b) => LLVMBuildFDiv,
468 urem(a, b) => LLVMBuildURem,
469 srem(a, b) => LLVMBuildSRem,
470 frem(a, b) => LLVMBuildFRem,
471 shl(a, b) => LLVMBuildShl,
472 lshr(a, b) => LLVMBuildLShr,
473 ashr(a, b) => LLVMBuildAShr,
474 and(a, b) => LLVMBuildAnd,
475 or(a, b) => LLVMBuildOr,
476 xor(a, b) => LLVMBuildXor,
477 neg(x) => LLVMBuildNeg,
478 fneg(x) => LLVMBuildFNeg,
479 not(x) => LLVMBuildNot,
480 unchecked_sadd(x, y) => LLVMBuildNSWAdd,
481 unchecked_uadd(x, y) => LLVMBuildNUWAdd,
482 unchecked_ssub(x, y) => LLVMBuildNSWSub,
483 unchecked_usub(x, y) => LLVMBuildNUWSub,
484 unchecked_smul(x, y) => LLVMBuildNSWMul,
485 unchecked_umul(x, y) => LLVMBuildNUWMul,
486 }
487
488 fn unchecked_suadd(&mut self, a: &'ll Value, b: &'ll Value) -> &'ll Value {
489 unsafe {
490 let add = llvm::LLVMBuildAdd(self.llbuilder, a, b, UNNAMED);
491 if llvm::LLVMIsAInstruction(add).is_some() {
492 llvm::LLVMSetNUW(add, TRUE);
493 llvm::LLVMSetNSW(add, TRUE);
494 }
495 add
496 }
497 }
498 fn unchecked_susub(&mut self, a: &'ll Value, b: &'ll Value) -> &'ll Value {
499 unsafe {
500 let sub = llvm::LLVMBuildSub(self.llbuilder, a, b, UNNAMED);
501 if llvm::LLVMIsAInstruction(sub).is_some() {
502 llvm::LLVMSetNUW(sub, TRUE);
503 llvm::LLVMSetNSW(sub, TRUE);
504 }
505 sub
506 }
507 }
508 fn unchecked_sumul(&mut self, a: &'ll Value, b: &'ll Value) -> &'ll Value {
509 unsafe {
510 let mul = llvm::LLVMBuildMul(self.llbuilder, a, b, UNNAMED);
511 if llvm::LLVMIsAInstruction(mul).is_some() {
512 llvm::LLVMSetNUW(mul, TRUE);
513 llvm::LLVMSetNSW(mul, TRUE);
514 }
515 mul
516 }
517 }
518
519 fn or_disjoint(&mut self, a: &'ll Value, b: &'ll Value) -> &'ll Value {
520 unsafe {
521 let or = llvm::LLVMBuildOr(self.llbuilder, a, b, UNNAMED);
522
523 if llvm::LLVMIsAInstruction(or).is_some() {
527 llvm::LLVMSetIsDisjoint(or, TRUE);
528 }
529 or
530 }
531 }
532
533 set_math_builder_methods! {
534 fadd_fast(x, y) => (LLVMBuildFAdd, LLVMRustSetFastMath),
535 fsub_fast(x, y) => (LLVMBuildFSub, LLVMRustSetFastMath),
536 fmul_fast(x, y) => (LLVMBuildFMul, LLVMRustSetFastMath),
537 fdiv_fast(x, y) => (LLVMBuildFDiv, LLVMRustSetFastMath),
538 frem_fast(x, y) => (LLVMBuildFRem, LLVMRustSetFastMath),
539 fadd_algebraic(x, y) => (LLVMBuildFAdd, LLVMRustSetAlgebraicMath),
540 fsub_algebraic(x, y) => (LLVMBuildFSub, LLVMRustSetAlgebraicMath),
541 fmul_algebraic(x, y) => (LLVMBuildFMul, LLVMRustSetAlgebraicMath),
542 fdiv_algebraic(x, y) => (LLVMBuildFDiv, LLVMRustSetAlgebraicMath),
543 frem_algebraic(x, y) => (LLVMBuildFRem, LLVMRustSetAlgebraicMath),
544 }
545
546 fn checked_binop(
547 &mut self,
548 oop: OverflowOp,
549 ty: Ty<'tcx>,
550 lhs: Self::Value,
551 rhs: Self::Value,
552 ) -> (Self::Value, Self::Value) {
553 let (size, signed) = ty.int_size_and_signed(self.tcx);
554 let width = size.bits();
555
556 if !signed {
557 match oop {
558 OverflowOp::Sub => {
559 let sub = self.sub(lhs, rhs);
563 let cmp = self.icmp(IntPredicate::IntULT, lhs, rhs);
564 return (sub, cmp);
565 }
566 OverflowOp::Add => {
567 let add = self.add(lhs, rhs);
570 let cmp = self.icmp(IntPredicate::IntULT, add, lhs);
571 return (add, cmp);
572 }
573 OverflowOp::Mul => {}
574 }
575 }
576
577 let oop_str = match oop {
578 OverflowOp::Add => "add",
579 OverflowOp::Sub => "sub",
580 OverflowOp::Mul => "mul",
581 };
582
583 let name = format!("llvm.{}{oop_str}.with.overflow", if signed { 's' } else { 'u' });
584
585 let res = self.call_intrinsic(name, &[self.type_ix(width)], &[lhs, rhs]);
586 (self.extract_value(res, 0), self.extract_value(res, 1))
587 }
588
589 fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
590 if self.cx().val_ty(val) == self.cx().type_i1() {
591 self.zext(val, self.cx().type_i8())
592 } else {
593 val
594 }
595 }
596
597 fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
598 if scalar.is_bool() {
599 return self.unchecked_utrunc(val, self.cx().type_i1());
600 }
601 val
602 }
603
604 fn alloca(&mut self, size: Size, align: Align) -> &'ll Value {
605 let mut bx = Builder::with_cx(self.cx);
606 bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
607 let ty = self.cx().type_array(self.cx().type_i8(), size.bytes());
608 unsafe {
609 let alloca = llvm::LLVMBuildAlloca(bx.llbuilder, ty, UNNAMED);
610 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
611 llvm::LLVMBuildPointerCast(bx.llbuilder, alloca, self.cx().type_ptr(), UNNAMED)
613 }
614 }
615
616 fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
617 unsafe {
618 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
619 let align = align.min(self.cx().tcx.sess.target.max_reliable_alignment());
620 llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
621 load
622 }
623 }
624
625 fn volatile_load(&mut self, ty: &'ll Type, ptr: &'ll Value) -> &'ll Value {
626 unsafe {
627 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
628 llvm::LLVMSetVolatile(load, llvm::TRUE);
629 load
630 }
631 }
632
633 fn atomic_load(
634 &mut self,
635 ty: &'ll Type,
636 ptr: &'ll Value,
637 order: rustc_middle::ty::AtomicOrdering,
638 size: Size,
639 ) -> &'ll Value {
640 unsafe {
641 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
642 llvm::LLVMSetOrdering(load, AtomicOrdering::from_generic(order));
644 llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
646 load
647 }
648 }
649
650 #[instrument(level = "trace", skip(self))]
651 fn load_operand(&mut self, place: PlaceRef<'tcx, &'ll Value>) -> OperandRef<'tcx, &'ll Value> {
652 if place.layout.is_unsized() {
653 let tail = self.tcx.struct_tail_for_codegen(place.layout.ty, self.typing_env());
654 if matches!(tail.kind(), ty::Foreign(..)) {
655 panic!("unsized locals must not be `extern` types");
659 }
660 }
661 assert_eq!(place.val.llextra.is_some(), place.layout.is_unsized());
662
663 if place.layout.is_zst() {
664 return OperandRef::zero_sized(place.layout);
665 }
666
667 #[instrument(level = "trace", skip(bx))]
668 fn scalar_load_metadata<'a, 'll, 'tcx>(
669 bx: &mut Builder<'a, 'll, 'tcx>,
670 load: &'ll Value,
671 scalar: abi::Scalar,
672 layout: TyAndLayout<'tcx>,
673 offset: Size,
674 ) {
675 if bx.cx.sess().opts.optimize == OptLevel::No {
676 return;
678 }
679
680 if !scalar.is_uninit_valid() {
681 bx.noundef_metadata(load);
682 }
683
684 match scalar.primitive() {
685 abi::Primitive::Int(..) => {
686 if !scalar.is_always_valid(bx) {
687 bx.range_metadata(load, scalar.valid_range(bx));
688 }
689 }
690 abi::Primitive::Pointer(_) => {
691 if !scalar.valid_range(bx).contains(0) {
692 bx.nonnull_metadata(load);
693 }
694
695 if let Some(pointee) = layout.pointee_info_at(bx, offset)
696 && let Some(_) = pointee.safe
697 {
698 bx.align_metadata(load, pointee.align);
699 }
700 }
701 abi::Primitive::Float(_) => {}
702 }
703 }
704
705 let val = if let Some(_) = place.val.llextra {
706 OperandValue::Ref(place.val)
708 } else if place.layout.is_llvm_immediate() {
709 let mut const_llval = None;
710 let llty = place.layout.llvm_type(self);
711 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.val.llval) {
712 if llvm::LLVMIsGlobalConstant(global).is_true() {
713 if let Some(init) = llvm::LLVMGetInitializer(global) {
714 if self.val_ty(init) == llty {
715 const_llval = Some(init);
716 }
717 }
718 }
719 }
720
721 let llval = const_llval.unwrap_or_else(|| {
722 let load = self.load(llty, place.val.llval, place.val.align);
723 if let abi::BackendRepr::Scalar(scalar) = place.layout.backend_repr {
724 scalar_load_metadata(self, load, scalar, place.layout, Size::ZERO);
725 self.to_immediate_scalar(load, scalar)
726 } else {
727 load
728 }
729 });
730 OperandValue::Immediate(llval)
731 } else if let abi::BackendRepr::ScalarPair(a, b) = place.layout.backend_repr {
732 let b_offset = a.size(self).align_to(b.align(self).abi);
733
734 let mut load = |i, scalar: abi::Scalar, layout, align, offset| {
735 let llptr = if i == 0 {
736 place.val.llval
737 } else {
738 self.inbounds_ptradd(place.val.llval, self.const_usize(b_offset.bytes()))
739 };
740 let llty = place.layout.scalar_pair_element_llvm_type(self, i, false);
741 let load = self.load(llty, llptr, align);
742 scalar_load_metadata(self, load, scalar, layout, offset);
743 self.to_immediate_scalar(load, scalar)
744 };
745
746 OperandValue::Pair(
747 load(0, a, place.layout, place.val.align, Size::ZERO),
748 load(1, b, place.layout, place.val.align.restrict_for_offset(b_offset), b_offset),
749 )
750 } else {
751 OperandValue::Ref(place.val)
752 };
753
754 OperandRef { val, layout: place.layout, move_annotation: None }
755 }
756
757 fn write_operand_repeatedly(
758 &mut self,
759 cg_elem: OperandRef<'tcx, &'ll Value>,
760 count: u64,
761 dest: PlaceRef<'tcx, &'ll Value>,
762 ) {
763 if self.cx.sess().opts.optimize == OptLevel::No {
764 self.write_operand_repeatedly_unoptimized(cg_elem, count, dest);
772 } else {
773 self.write_operand_repeatedly_optimized(cg_elem, count, dest);
774 }
775 }
776
777 fn range_metadata(&mut self, load: &'ll Value, range: WrappingRange) {
778 if self.cx.sess().opts.optimize == OptLevel::No {
779 return;
781 }
782
783 let llty = self.cx.val_ty(load);
784 let md = [
785 llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.start)),
786 llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.end.wrapping_add(1))),
787 ];
788 self.set_metadata_node(load, llvm::MD_range, &md);
789 }
790
791 fn nonnull_metadata(&mut self, load: &'ll Value) {
792 self.set_metadata_node(load, llvm::MD_nonnull, &[]);
793 }
794
795 fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
796 self.store_with_flags(val, ptr, align, MemFlags::empty())
797 }
798
799 fn store_with_flags(
800 &mut self,
801 val: &'ll Value,
802 ptr: &'ll Value,
803 align: Align,
804 flags: MemFlags,
805 ) -> &'ll Value {
806 debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
807 assert_eq!(self.cx.type_kind(self.cx.val_ty(ptr)), TypeKind::Pointer);
808 unsafe {
809 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
810 let align = align.min(self.cx().tcx.sess.target.max_reliable_alignment());
811 let align =
812 if flags.contains(MemFlags::UNALIGNED) { 1 } else { align.bytes() as c_uint };
813 llvm::LLVMSetAlignment(store, align);
814 if flags.contains(MemFlags::VOLATILE) {
815 llvm::LLVMSetVolatile(store, llvm::TRUE);
816 }
817 if flags.contains(MemFlags::NONTEMPORAL) {
818 let use_nontemporal = matches!(
831 self.cx.tcx.sess.target.arch,
832 Arch::AArch64 | Arch::Arm | Arch::RiscV32 | Arch::RiscV64
833 );
834 if use_nontemporal {
835 let one = llvm::LLVMValueAsMetadata(self.cx.const_i32(1));
840 self.set_metadata_node(store, llvm::MD_nontemporal, &[one]);
841 }
842 }
843 store
844 }
845 }
846
847 fn atomic_store(
848 &mut self,
849 val: &'ll Value,
850 ptr: &'ll Value,
851 order: rustc_middle::ty::AtomicOrdering,
852 size: Size,
853 ) {
854 debug!("Store {:?} -> {:?}", val, ptr);
855 assert_eq!(self.cx.type_kind(self.cx.val_ty(ptr)), TypeKind::Pointer);
856 unsafe {
857 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
858 llvm::LLVMSetOrdering(store, AtomicOrdering::from_generic(order));
860 llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
862 }
863 }
864
865 fn gep(&mut self, ty: &'ll Type, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
866 unsafe {
867 llvm::LLVMBuildGEPWithNoWrapFlags(
868 self.llbuilder,
869 ty,
870 ptr,
871 indices.as_ptr(),
872 indices.len() as c_uint,
873 UNNAMED,
874 GEPNoWrapFlags::default(),
875 )
876 }
877 }
878
879 fn inbounds_gep(
880 &mut self,
881 ty: &'ll Type,
882 ptr: &'ll Value,
883 indices: &[&'ll Value],
884 ) -> &'ll Value {
885 unsafe {
886 llvm::LLVMBuildGEPWithNoWrapFlags(
887 self.llbuilder,
888 ty,
889 ptr,
890 indices.as_ptr(),
891 indices.len() as c_uint,
892 UNNAMED,
893 GEPNoWrapFlags::InBounds,
894 )
895 }
896 }
897
898 fn inbounds_nuw_gep(
899 &mut self,
900 ty: &'ll Type,
901 ptr: &'ll Value,
902 indices: &[&'ll Value],
903 ) -> &'ll Value {
904 unsafe {
905 llvm::LLVMBuildGEPWithNoWrapFlags(
906 self.llbuilder,
907 ty,
908 ptr,
909 indices.as_ptr(),
910 indices.len() as c_uint,
911 UNNAMED,
912 GEPNoWrapFlags::InBounds | GEPNoWrapFlags::NUW,
913 )
914 }
915 }
916
917 fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
919 unsafe { llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
920 }
921
922 fn unchecked_utrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
923 debug_assert_ne!(self.val_ty(val), dest_ty);
924
925 let trunc = self.trunc(val, dest_ty);
926 unsafe {
927 if llvm::LLVMIsAInstruction(trunc).is_some() {
928 llvm::LLVMSetNUW(trunc, TRUE);
929 }
930 }
931 trunc
932 }
933
934 fn unchecked_strunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
935 debug_assert_ne!(self.val_ty(val), dest_ty);
936
937 let trunc = self.trunc(val, dest_ty);
938 unsafe {
939 if llvm::LLVMIsAInstruction(trunc).is_some() {
940 llvm::LLVMSetNSW(trunc, TRUE);
941 }
942 }
943 trunc
944 }
945
946 fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
947 unsafe { llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED) }
948 }
949
950 fn fptoui_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
951 self.call_intrinsic("llvm.fptoui.sat", &[dest_ty, self.val_ty(val)], &[val])
952 }
953
954 fn fptosi_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
955 self.call_intrinsic("llvm.fptosi.sat", &[dest_ty, self.val_ty(val)], &[val])
956 }
957
958 fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
959 if self.sess().target.is_like_wasm {
974 let src_ty = self.cx.val_ty(val);
975 if self.cx.type_kind(src_ty) != TypeKind::Vector {
976 let float_width = self.cx.float_width(src_ty);
977 let int_width = self.cx.int_width(dest_ty);
978 if matches!((int_width, float_width), (32 | 64, 32 | 64)) {
979 return self.call_intrinsic(
980 "llvm.wasm.trunc.unsigned",
981 &[dest_ty, src_ty],
982 &[val],
983 );
984 }
985 }
986 }
987 unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) }
988 }
989
990 fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
991 if self.sess().target.is_like_wasm {
993 let src_ty = self.cx.val_ty(val);
994 if self.cx.type_kind(src_ty) != TypeKind::Vector {
995 let float_width = self.cx.float_width(src_ty);
996 let int_width = self.cx.int_width(dest_ty);
997 if matches!((int_width, float_width), (32 | 64, 32 | 64)) {
998 return self.call_intrinsic(
999 "llvm.wasm.trunc.signed",
1000 &[dest_ty, src_ty],
1001 &[val],
1002 );
1003 }
1004 }
1005 }
1006 unsafe { llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty, UNNAMED) }
1007 }
1008
1009 fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1010 unsafe { llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
1011 }
1012
1013 fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1014 unsafe { llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
1015 }
1016
1017 fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1018 unsafe { llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
1019 }
1020
1021 fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1022 unsafe { llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, UNNAMED) }
1023 }
1024
1025 fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1026 unsafe { llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, UNNAMED) }
1027 }
1028
1029 fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1030 unsafe { llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, UNNAMED) }
1031 }
1032
1033 fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1034 unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
1035 }
1036
1037 fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
1038 unsafe {
1039 llvm::LLVMBuildIntCast2(self.llbuilder, val, dest_ty, is_signed.to_llvm_bool(), UNNAMED)
1040 }
1041 }
1042
1043 fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1044 unsafe { llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED) }
1045 }
1046
1047 fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1049 let op = llvm::IntPredicate::from_generic(op);
1050 unsafe { llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
1051 }
1052
1053 fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1054 let op = llvm::RealPredicate::from_generic(op);
1055 unsafe { llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
1056 }
1057
1058 fn three_way_compare(
1059 &mut self,
1060 ty: Ty<'tcx>,
1061 lhs: Self::Value,
1062 rhs: Self::Value,
1063 ) -> Self::Value {
1064 let size = ty.primitive_size(self.tcx);
1065 let name = if ty.is_signed() { "llvm.scmp" } else { "llvm.ucmp" };
1066
1067 self.call_intrinsic(name, &[self.type_i8(), self.type_ix(size.bits())], &[lhs, rhs])
1068 }
1069
1070 fn memcpy(
1072 &mut self,
1073 dst: &'ll Value,
1074 dst_align: Align,
1075 src: &'ll Value,
1076 src_align: Align,
1077 size: &'ll Value,
1078 flags: MemFlags,
1079 tt: Option<FncTree>,
1080 ) {
1081 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
1082 let size = self.intcast(size, self.type_isize(), false);
1083 let is_volatile = flags.contains(MemFlags::VOLATILE);
1084 let memcpy = unsafe {
1085 llvm::LLVMRustBuildMemCpy(
1086 self.llbuilder,
1087 dst,
1088 dst_align.bytes() as c_uint,
1089 src,
1090 src_align.bytes() as c_uint,
1091 size,
1092 is_volatile,
1093 )
1094 };
1095
1096 if let Some(tt) = tt {
1102 crate::typetree::add_tt(self.cx().llmod, self.cx().llcx, memcpy, tt);
1103 }
1104 }
1105
1106 fn memmove(
1107 &mut self,
1108 dst: &'ll Value,
1109 dst_align: Align,
1110 src: &'ll Value,
1111 src_align: Align,
1112 size: &'ll Value,
1113 flags: MemFlags,
1114 ) {
1115 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memmove not supported");
1116 let size = self.intcast(size, self.type_isize(), false);
1117 let is_volatile = flags.contains(MemFlags::VOLATILE);
1118 unsafe {
1119 llvm::LLVMRustBuildMemMove(
1120 self.llbuilder,
1121 dst,
1122 dst_align.bytes() as c_uint,
1123 src,
1124 src_align.bytes() as c_uint,
1125 size,
1126 is_volatile,
1127 );
1128 }
1129 }
1130
1131 fn memset(
1132 &mut self,
1133 ptr: &'ll Value,
1134 fill_byte: &'ll Value,
1135 size: &'ll Value,
1136 align: Align,
1137 flags: MemFlags,
1138 ) {
1139 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memset not supported");
1140 let is_volatile = flags.contains(MemFlags::VOLATILE);
1141 unsafe {
1142 llvm::LLVMRustBuildMemSet(
1143 self.llbuilder,
1144 ptr,
1145 align.bytes() as c_uint,
1146 fill_byte,
1147 size,
1148 is_volatile,
1149 );
1150 }
1151 }
1152
1153 fn select(
1154 &mut self,
1155 cond: &'ll Value,
1156 then_val: &'ll Value,
1157 else_val: &'ll Value,
1158 ) -> &'ll Value {
1159 unsafe { llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, UNNAMED) }
1160 }
1161
1162 fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
1163 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
1164 }
1165
1166 fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
1167 unsafe { llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED) }
1168 }
1169
1170 fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
1171 unsafe {
1172 let elt_ty = self.cx.val_ty(elt);
1173 let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
1174 let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
1175 let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
1176 self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
1177 }
1178 }
1179
1180 fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
1181 assert_eq!(idx as c_uint as u64, idx);
1182 unsafe { llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED) }
1183 }
1184
1185 fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value, idx: u64) -> &'ll Value {
1186 assert_eq!(idx as c_uint as u64, idx);
1187 unsafe { llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint, UNNAMED) }
1188 }
1189
1190 fn set_personality_fn(&mut self, personality: &'ll Value) {
1191 unsafe {
1192 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
1193 }
1194 }
1195
1196 fn cleanup_landing_pad(&mut self, pers_fn: &'ll Value) -> (&'ll Value, &'ll Value) {
1197 let ty = self.type_struct(&[self.type_ptr(), self.type_i32()], false);
1198 let landing_pad = self.landing_pad(ty, pers_fn, 0);
1199 unsafe {
1200 llvm::LLVMSetCleanup(landing_pad, llvm::TRUE);
1201 }
1202 (self.extract_value(landing_pad, 0), self.extract_value(landing_pad, 1))
1203 }
1204
1205 fn filter_landing_pad(&mut self, pers_fn: &'ll Value) {
1206 let ty = self.type_struct(&[self.type_ptr(), self.type_i32()], false);
1207 let landing_pad = self.landing_pad(ty, pers_fn, 1);
1208 self.add_clause(landing_pad, self.const_array(self.type_ptr(), &[]));
1209 }
1210
1211 fn resume(&mut self, exn0: &'ll Value, exn1: &'ll Value) {
1212 let ty = self.type_struct(&[self.type_ptr(), self.type_i32()], false);
1213 let mut exn = self.const_poison(ty);
1214 exn = self.insert_value(exn, exn0, 0);
1215 exn = self.insert_value(exn, exn1, 1);
1216 unsafe {
1217 llvm::LLVMBuildResume(self.llbuilder, exn);
1218 }
1219 }
1220
1221 fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
1222 let ret = unsafe {
1223 llvm::LLVMBuildCleanupPad(
1224 self.llbuilder,
1225 parent,
1226 args.as_ptr(),
1227 args.len() as c_uint,
1228 c"cleanuppad".as_ptr(),
1229 )
1230 };
1231 Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
1232 }
1233
1234 fn cleanup_ret(&mut self, funclet: &Funclet<'ll>, unwind: Option<&'ll BasicBlock>) {
1235 unsafe {
1236 llvm::LLVMBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind)
1237 .expect("LLVM does not have support for cleanupret");
1238 }
1239 }
1240
1241 fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
1242 let ret = unsafe {
1243 llvm::LLVMBuildCatchPad(
1244 self.llbuilder,
1245 parent,
1246 args.as_ptr(),
1247 args.len() as c_uint,
1248 c"catchpad".as_ptr(),
1249 )
1250 };
1251 Funclet::new(ret.expect("LLVM does not have support for catchpad"))
1252 }
1253
1254 fn catch_switch(
1255 &mut self,
1256 parent: Option<&'ll Value>,
1257 unwind: Option<&'ll BasicBlock>,
1258 handlers: &[&'ll BasicBlock],
1259 ) -> &'ll Value {
1260 let ret = unsafe {
1261 llvm::LLVMBuildCatchSwitch(
1262 self.llbuilder,
1263 parent,
1264 unwind,
1265 handlers.len() as c_uint,
1266 c"catchswitch".as_ptr(),
1267 )
1268 };
1269 let ret = ret.expect("LLVM does not have support for catchswitch");
1270 for handler in handlers {
1271 unsafe {
1272 llvm::LLVMAddHandler(ret, handler);
1273 }
1274 }
1275 ret
1276 }
1277
1278 fn atomic_cmpxchg(
1280 &mut self,
1281 dst: &'ll Value,
1282 cmp: &'ll Value,
1283 src: &'ll Value,
1284 order: rustc_middle::ty::AtomicOrdering,
1285 failure_order: rustc_middle::ty::AtomicOrdering,
1286 weak: bool,
1287 ) -> (&'ll Value, &'ll Value) {
1288 unsafe {
1289 let value = llvm::LLVMBuildAtomicCmpXchg(
1290 self.llbuilder,
1291 dst,
1292 cmp,
1293 src,
1294 AtomicOrdering::from_generic(order),
1295 AtomicOrdering::from_generic(failure_order),
1296 llvm::FALSE, );
1298 llvm::LLVMSetWeak(value, weak.to_llvm_bool());
1299 let val = self.extract_value(value, 0);
1300 let success = self.extract_value(value, 1);
1301 (val, success)
1302 }
1303 }
1304
1305 fn atomic_rmw(
1306 &mut self,
1307 op: rustc_codegen_ssa::common::AtomicRmwBinOp,
1308 dst: &'ll Value,
1309 src: &'ll Value,
1310 order: rustc_middle::ty::AtomicOrdering,
1311 ret_ptr: bool,
1312 ) -> &'ll Value {
1313 let mut res = unsafe {
1317 llvm::LLVMBuildAtomicRMW(
1318 self.llbuilder,
1319 AtomicRmwBinOp::from_generic(op),
1320 dst,
1321 src,
1322 AtomicOrdering::from_generic(order),
1323 llvm::FALSE, )
1325 };
1326 if ret_ptr && self.val_ty(res) != self.type_ptr() {
1327 res = self.inttoptr(res, self.type_ptr());
1328 }
1329 res
1330 }
1331
1332 fn atomic_fence(
1333 &mut self,
1334 order: rustc_middle::ty::AtomicOrdering,
1335 scope: SynchronizationScope,
1336 ) {
1337 let single_threaded = match scope {
1338 SynchronizationScope::SingleThread => true,
1339 SynchronizationScope::CrossThread => false,
1340 };
1341 unsafe {
1342 llvm::LLVMBuildFence(
1343 self.llbuilder,
1344 AtomicOrdering::from_generic(order),
1345 single_threaded.to_llvm_bool(),
1346 UNNAMED,
1347 );
1348 }
1349 }
1350
1351 fn set_invariant_load(&mut self, load: &'ll Value) {
1352 self.set_metadata_node(load, llvm::MD_invariant_load, &[]);
1353 }
1354
1355 fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
1356 self.call_lifetime_intrinsic("llvm.lifetime.start", ptr, size);
1357 }
1358
1359 fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
1360 self.call_lifetime_intrinsic("llvm.lifetime.end", ptr, size);
1361 }
1362
1363 fn call(
1364 &mut self,
1365 llty: &'ll Type,
1366 fn_call_attrs: Option<&CodegenFnAttrs>,
1367 fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
1368 llfn: &'ll Value,
1369 args: &[&'ll Value],
1370 funclet: Option<&Funclet<'ll>>,
1371 instance: Option<Instance<'tcx>>,
1372 ) -> &'ll Value {
1373 debug!("call {:?} with args ({:?})", llfn, args);
1374
1375 let args = self.check_call("call", llty, llfn, args);
1376 let funclet_bundle = funclet.map(|funclet| funclet.bundle());
1377 let mut bundles: SmallVec<[_; 2]> = SmallVec::new();
1378 if let Some(funclet_bundle) = funclet_bundle {
1379 bundles.push(funclet_bundle);
1380 }
1381
1382 self.cfi_type_test(fn_call_attrs, fn_abi, instance, llfn);
1384
1385 let kcfi_bundle = self.kcfi_operand_bundle(fn_call_attrs, fn_abi, instance, llfn);
1387 if let Some(kcfi_bundle) = kcfi_bundle.as_ref().map(|b| b.as_ref()) {
1388 bundles.push(kcfi_bundle);
1389 }
1390
1391 let call = unsafe {
1392 llvm::LLVMBuildCallWithOperandBundles(
1393 self.llbuilder,
1394 llty,
1395 llfn,
1396 args.as_ptr() as *const &llvm::Value,
1397 args.len() as c_uint,
1398 bundles.as_ptr(),
1399 bundles.len() as c_uint,
1400 c"".as_ptr(),
1401 )
1402 };
1403
1404 if let Some(instance) = instance {
1405 let fn_defn_attrs = self.cx.tcx.codegen_fn_attrs(instance.def_id());
1407 if let Some(fn_call_attrs) = fn_call_attrs
1408 && !fn_call_attrs.target_features.is_empty()
1409 && let Some(inlining_rule) = attributes::inline_attr(&self.cx, self.cx.tcx, instance)
1413 && self.cx.tcx.is_target_feature_call_safe(
1414 &fn_call_attrs.target_features,
1415 &fn_defn_attrs.target_features,
1416 )
1417 {
1418 attributes::apply_to_callsite(
1419 call,
1420 llvm::AttributePlace::Function,
1421 &[inlining_rule],
1422 );
1423 }
1424 }
1425
1426 if let Some(fn_abi) = fn_abi {
1427 fn_abi.apply_attrs_callsite(self, call);
1428 }
1429 call
1430 }
1431
1432 fn tail_call(
1433 &mut self,
1434 llty: Self::Type,
1435 fn_attrs: Option<&CodegenFnAttrs>,
1436 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
1437 llfn: Self::Value,
1438 args: &[Self::Value],
1439 funclet: Option<&Self::Funclet>,
1440 instance: Option<Instance<'tcx>>,
1441 ) {
1442 let call = self.call(llty, fn_attrs, Some(fn_abi), llfn, args, funclet, instance);
1443 llvm::LLVMSetTailCallKind(call, llvm::TailCallKind::MustTail);
1444
1445 match &fn_abi.ret.mode {
1446 PassMode::Ignore | PassMode::Indirect { .. } => self.ret_void(),
1447 PassMode::Direct(_) | PassMode::Pair { .. } | PassMode::Cast { .. } => self.ret(call),
1448 }
1449 }
1450
1451 fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
1452 unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
1453 }
1454
1455 fn apply_attrs_to_cleanup_callsite(&mut self, llret: &'ll Value) {
1456 let cold_inline = llvm::AttributeKind::Cold.create_attr(self.llcx);
1458 attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_inline]);
1459 }
1460}
1461
1462impl<'ll> StaticBuilderMethods for Builder<'_, 'll, '_> {
1463 fn get_static(&mut self, def_id: DefId) -> &'ll Value {
1464 let global = self.cx().get_static(def_id);
1466 if self.cx().tcx.is_thread_local_static(def_id) {
1467 let pointer =
1468 self.call_intrinsic("llvm.threadlocal.address", &[self.val_ty(global)], &[global]);
1469 self.pointercast(pointer, self.type_ptr())
1471 } else {
1472 self.cx().const_pointercast(global, self.type_ptr())
1474 }
1475 }
1476}
1477
1478impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
1479 pub(crate) fn llfn(&self) -> &'ll Value {
1480 unsafe { llvm::LLVMGetBasicBlockParent(self.llbb()) }
1481 }
1482}
1483
1484impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
1485 fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
1486 unsafe {
1487 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
1488 }
1489 }
1490}
1491impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
1492 fn align_metadata(&mut self, load: &'ll Value, align: Align) {
1493 let md = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
1494 self.set_metadata_node(load, llvm::MD_align, &md);
1495 }
1496
1497 fn noundef_metadata(&mut self, load: &'ll Value) {
1498 self.set_metadata_node(load, llvm::MD_noundef, &[]);
1499 }
1500
1501 pub(crate) fn set_unpredictable(&mut self, inst: &'ll Value) {
1502 self.set_metadata_node(inst, llvm::MD_unpredictable, &[]);
1503 }
1504
1505 fn write_operand_repeatedly_optimized(
1506 &mut self,
1507 cg_elem: OperandRef<'tcx, &'ll Value>,
1508 count: u64,
1509 dest: PlaceRef<'tcx, &'ll Value>,
1510 ) {
1511 let zero = self.const_usize(0);
1512 let count = self.const_usize(count);
1513
1514 let header_bb = self.append_sibling_block("repeat_loop_header");
1515 let body_bb = self.append_sibling_block("repeat_loop_body");
1516 let next_bb = self.append_sibling_block("repeat_loop_next");
1517
1518 self.br(header_bb);
1519
1520 let mut header_bx = Self::build(self.cx, header_bb);
1521 let i = header_bx.phi(self.val_ty(zero), &[zero], &[self.llbb()]);
1522
1523 let keep_going = header_bx.icmp(IntPredicate::IntULT, i, count);
1524 header_bx.cond_br(keep_going, body_bb, next_bb);
1525
1526 let mut body_bx = Self::build(self.cx, body_bb);
1527 let dest_elem = dest.project_index(&mut body_bx, i);
1528 cg_elem.val.store(&mut body_bx, dest_elem);
1529
1530 let next = body_bx.unchecked_uadd(i, self.const_usize(1));
1531 body_bx.br(header_bb);
1532 header_bx.add_incoming_to_phi(i, next, body_bb);
1533
1534 *self = Self::build(self.cx, next_bb);
1535 }
1536
1537 fn write_operand_repeatedly_unoptimized(
1538 &mut self,
1539 cg_elem: OperandRef<'tcx, &'ll Value>,
1540 count: u64,
1541 dest: PlaceRef<'tcx, &'ll Value>,
1542 ) {
1543 let zero = self.const_usize(0);
1544 let count = self.const_usize(count);
1545 let start = dest.project_index(self, zero).val.llval;
1546 let end = dest.project_index(self, count).val.llval;
1547
1548 let header_bb = self.append_sibling_block("repeat_loop_header");
1549 let body_bb = self.append_sibling_block("repeat_loop_body");
1550 let next_bb = self.append_sibling_block("repeat_loop_next");
1551
1552 self.br(header_bb);
1553
1554 let mut header_bx = Self::build(self.cx, header_bb);
1555 let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
1556
1557 let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
1558 header_bx.cond_br(keep_going, body_bb, next_bb);
1559
1560 let mut body_bx = Self::build(self.cx, body_bb);
1561 let align = dest.val.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
1562 cg_elem
1563 .val
1564 .store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
1565
1566 let next = body_bx.inbounds_gep(
1567 self.backend_type(cg_elem.layout),
1568 current,
1569 &[self.const_usize(1)],
1570 );
1571 body_bx.br(header_bb);
1572 header_bx.add_incoming_to_phi(current, next, body_bb);
1573
1574 *self = Self::build(self.cx, next_bb);
1575 }
1576
1577 pub(crate) fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1578 self.call_intrinsic("llvm.minnum", &[self.val_ty(lhs)], &[lhs, rhs])
1579 }
1580
1581 pub(crate) fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
1582 self.call_intrinsic("llvm.maxnum", &[self.val_ty(lhs)], &[lhs, rhs])
1583 }
1584
1585 pub(crate) fn insert_element(
1586 &mut self,
1587 vec: &'ll Value,
1588 elt: &'ll Value,
1589 idx: &'ll Value,
1590 ) -> &'ll Value {
1591 unsafe { llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, UNNAMED) }
1592 }
1593
1594 pub(crate) fn shuffle_vector(
1595 &mut self,
1596 v1: &'ll Value,
1597 v2: &'ll Value,
1598 mask: &'ll Value,
1599 ) -> &'ll Value {
1600 unsafe { llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, UNNAMED) }
1601 }
1602
1603 pub(crate) fn vector_reduce_fadd(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1604 self.call_intrinsic("llvm.vector.reduce.fadd", &[self.val_ty(src)], &[acc, src])
1605 }
1606 pub(crate) fn vector_reduce_fmul(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1607 self.call_intrinsic("llvm.vector.reduce.fmul", &[self.val_ty(src)], &[acc, src])
1608 }
1609 pub(crate) fn vector_reduce_fadd_reassoc(
1610 &mut self,
1611 acc: &'ll Value,
1612 src: &'ll Value,
1613 ) -> &'ll Value {
1614 unsafe {
1615 let instr =
1616 self.call_intrinsic("llvm.vector.reduce.fadd", &[self.val_ty(src)], &[acc, src]);
1617 llvm::LLVMRustSetAllowReassoc(instr);
1618 instr
1619 }
1620 }
1621 pub(crate) fn vector_reduce_fmul_reassoc(
1622 &mut self,
1623 acc: &'ll Value,
1624 src: &'ll Value,
1625 ) -> &'ll Value {
1626 unsafe {
1627 let instr =
1628 self.call_intrinsic("llvm.vector.reduce.fmul", &[self.val_ty(src)], &[acc, src]);
1629 llvm::LLVMRustSetAllowReassoc(instr);
1630 instr
1631 }
1632 }
1633 pub(crate) fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
1634 self.call_intrinsic("llvm.vector.reduce.add", &[self.val_ty(src)], &[src])
1635 }
1636 pub(crate) fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
1637 self.call_intrinsic("llvm.vector.reduce.mul", &[self.val_ty(src)], &[src])
1638 }
1639 pub(crate) fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
1640 self.call_intrinsic("llvm.vector.reduce.and", &[self.val_ty(src)], &[src])
1641 }
1642 pub(crate) fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
1643 self.call_intrinsic("llvm.vector.reduce.or", &[self.val_ty(src)], &[src])
1644 }
1645 pub(crate) fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
1646 self.call_intrinsic("llvm.vector.reduce.xor", &[self.val_ty(src)], &[src])
1647 }
1648 pub(crate) fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
1649 self.call_intrinsic("llvm.vector.reduce.fmin", &[self.val_ty(src)], &[src])
1650 }
1651 pub(crate) fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
1652 self.call_intrinsic("llvm.vector.reduce.fmax", &[self.val_ty(src)], &[src])
1653 }
1654 pub(crate) fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1655 self.call_intrinsic(
1656 if is_signed { "llvm.vector.reduce.smin" } else { "llvm.vector.reduce.umin" },
1657 &[self.val_ty(src)],
1658 &[src],
1659 )
1660 }
1661 pub(crate) fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
1662 self.call_intrinsic(
1663 if is_signed { "llvm.vector.reduce.smax" } else { "llvm.vector.reduce.umax" },
1664 &[self.val_ty(src)],
1665 &[src],
1666 )
1667 }
1668}
1669impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
1670 pub(crate) fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
1671 unsafe {
1672 llvm::LLVMAddClause(landing_pad, clause);
1673 }
1674 }
1675
1676 pub(crate) fn catch_ret(
1677 &mut self,
1678 funclet: &Funclet<'ll>,
1679 unwind: &'ll BasicBlock,
1680 ) -> &'ll Value {
1681 let ret = unsafe { llvm::LLVMBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind) };
1682 ret.expect("LLVM does not have support for catchret")
1683 }
1684
1685 fn check_call<'b>(
1686 &mut self,
1687 typ: &str,
1688 fn_ty: &'ll Type,
1689 llfn: &'ll Value,
1690 args: &'b [&'ll Value],
1691 ) -> Cow<'b, [&'ll Value]> {
1692 assert!(
1693 self.cx.type_kind(fn_ty) == TypeKind::Function,
1694 "builder::{typ} not passed a function, but {fn_ty:?}"
1695 );
1696
1697 let param_tys = self.cx.func_params_types(fn_ty);
1698
1699 let all_args_match = iter::zip(¶m_tys, args.iter().map(|&v| self.cx.val_ty(v)))
1700 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1701
1702 if all_args_match {
1703 return Cow::Borrowed(args);
1704 }
1705
1706 let casted_args: Vec<_> = iter::zip(param_tys, args)
1707 .enumerate()
1708 .map(|(i, (expected_ty, &actual_val))| {
1709 let actual_ty = self.cx.val_ty(actual_val);
1710 if expected_ty != actual_ty {
1711 debug!(
1712 "type mismatch in function call of {:?}. \
1713 Expected {:?} for param {}, got {:?}; injecting bitcast",
1714 llfn, expected_ty, i, actual_ty
1715 );
1716 self.bitcast(actual_val, expected_ty)
1717 } else {
1718 actual_val
1719 }
1720 })
1721 .collect();
1722
1723 Cow::Owned(casted_args)
1724 }
1725
1726 pub(crate) fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
1727 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
1728 }
1729}
1730
1731impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
1732 pub(crate) fn call_intrinsic(
1733 &mut self,
1734 base_name: impl Into<Cow<'static, str>>,
1735 type_params: &[&'ll Type],
1736 args: &[&'ll Value],
1737 ) -> &'ll Value {
1738 let (ty, f) = self.cx.get_intrinsic(base_name.into(), type_params);
1739 self.call(ty, None, None, f, args, None, None)
1740 }
1741
1742 fn call_lifetime_intrinsic(&mut self, intrinsic: &'static str, ptr: &'ll Value, size: Size) {
1743 let size = size.bytes();
1744 if size == 0 {
1745 return;
1746 }
1747
1748 if !self.cx().sess().emit_lifetime_markers() {
1749 return;
1750 }
1751
1752 if crate::llvm_util::get_version() >= (22, 0, 0) {
1753 self.call_intrinsic(intrinsic, &[self.val_ty(ptr)], &[ptr]);
1754 } else {
1755 self.call_intrinsic(intrinsic, &[self.val_ty(ptr)], &[self.cx.const_u64(size), ptr]);
1756 }
1757 }
1758}
1759impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
1760 pub(crate) fn phi(
1761 &mut self,
1762 ty: &'ll Type,
1763 vals: &[&'ll Value],
1764 bbs: &[&'ll BasicBlock],
1765 ) -> &'ll Value {
1766 assert_eq!(vals.len(), bbs.len());
1767 let phi = unsafe { llvm::LLVMBuildPhi(self.llbuilder, ty, UNNAMED) };
1768 unsafe {
1769 llvm::LLVMAddIncoming(phi, vals.as_ptr(), bbs.as_ptr(), vals.len() as c_uint);
1770 phi
1771 }
1772 }
1773
1774 fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
1775 unsafe {
1776 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1777 }
1778 }
1779}
1780impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
1781 pub(crate) fn landing_pad(
1782 &mut self,
1783 ty: &'ll Type,
1784 pers_fn: &'ll Value,
1785 num_clauses: usize,
1786 ) -> &'ll Value {
1787 self.set_personality_fn(pers_fn);
1791 unsafe {
1792 llvm::LLVMBuildLandingPad(self.llbuilder, ty, None, num_clauses as c_uint, UNNAMED)
1793 }
1794 }
1795
1796 pub(crate) fn callbr(
1797 &mut self,
1798 llty: &'ll Type,
1799 fn_attrs: Option<&CodegenFnAttrs>,
1800 fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
1801 llfn: &'ll Value,
1802 args: &[&'ll Value],
1803 default_dest: &'ll BasicBlock,
1804 indirect_dest: &[&'ll BasicBlock],
1805 funclet: Option<&Funclet<'ll>>,
1806 instance: Option<Instance<'tcx>>,
1807 ) -> &'ll Value {
1808 debug!("invoke {:?} with args ({:?})", llfn, args);
1809
1810 let args = self.check_call("callbr", llty, llfn, args);
1811 let funclet_bundle = funclet.map(|funclet| funclet.bundle());
1812 let mut bundles: SmallVec<[_; 2]> = SmallVec::new();
1813 if let Some(funclet_bundle) = funclet_bundle {
1814 bundles.push(funclet_bundle);
1815 }
1816
1817 self.cfi_type_test(fn_attrs, fn_abi, instance, llfn);
1819
1820 let kcfi_bundle = self.kcfi_operand_bundle(fn_attrs, fn_abi, instance, llfn);
1822 if let Some(kcfi_bundle) = kcfi_bundle.as_ref().map(|b| b.as_ref()) {
1823 bundles.push(kcfi_bundle);
1824 }
1825
1826 let callbr = unsafe {
1827 llvm::LLVMBuildCallBr(
1828 self.llbuilder,
1829 llty,
1830 llfn,
1831 default_dest,
1832 indirect_dest.as_ptr(),
1833 indirect_dest.len() as c_uint,
1834 args.as_ptr(),
1835 args.len() as c_uint,
1836 bundles.as_ptr(),
1837 bundles.len() as c_uint,
1838 UNNAMED,
1839 )
1840 };
1841 if let Some(fn_abi) = fn_abi {
1842 fn_abi.apply_attrs_callsite(self, callbr);
1843 }
1844 callbr
1845 }
1846
1847 fn cfi_type_test(
1849 &mut self,
1850 fn_attrs: Option<&CodegenFnAttrs>,
1851 fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
1852 instance: Option<Instance<'tcx>>,
1853 llfn: &'ll Value,
1854 ) {
1855 let is_indirect_call = unsafe { llvm::LLVMRustIsNonGVFunctionPointerTy(llfn) };
1856 if self.tcx.sess.is_sanitizer_cfi_enabled()
1857 && let Some(fn_abi) = fn_abi
1858 && is_indirect_call
1859 {
1860 if let Some(fn_attrs) = fn_attrs
1861 && fn_attrs.sanitizers.disabled.contains(SanitizerSet::CFI)
1862 {
1863 return;
1864 }
1865
1866 let mut options = cfi::TypeIdOptions::empty();
1867 if self.tcx.sess.is_sanitizer_cfi_generalize_pointers_enabled() {
1868 options.insert(cfi::TypeIdOptions::GENERALIZE_POINTERS);
1869 }
1870 if self.tcx.sess.is_sanitizer_cfi_normalize_integers_enabled() {
1871 options.insert(cfi::TypeIdOptions::NORMALIZE_INTEGERS);
1872 }
1873
1874 let typeid = if let Some(instance) = instance {
1875 cfi::typeid_for_instance(self.tcx, instance, options)
1876 } else {
1877 cfi::typeid_for_fnabi(self.tcx, fn_abi, options)
1878 };
1879 let typeid_metadata = self.cx.create_metadata(typeid.as_bytes());
1880 let dbg_loc = self.get_dbg_loc();
1881
1882 let typeid = self.get_metadata_value(typeid_metadata);
1886 let cond = self.call_intrinsic("llvm.type.test", &[], &[llfn, typeid]);
1887 let bb_pass = self.append_sibling_block("type_test.pass");
1888 let bb_fail = self.append_sibling_block("type_test.fail");
1889 self.cond_br(cond, bb_pass, bb_fail);
1890
1891 self.switch_to_block(bb_fail);
1892 if let Some(dbg_loc) = dbg_loc {
1893 self.set_dbg_loc(dbg_loc);
1894 }
1895 self.abort();
1896 self.unreachable();
1897
1898 self.switch_to_block(bb_pass);
1899 if let Some(dbg_loc) = dbg_loc {
1900 self.set_dbg_loc(dbg_loc);
1901 }
1902 }
1903 }
1904
1905 fn kcfi_operand_bundle(
1907 &mut self,
1908 fn_attrs: Option<&CodegenFnAttrs>,
1909 fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
1910 instance: Option<Instance<'tcx>>,
1911 llfn: &'ll Value,
1912 ) -> Option<llvm::OperandBundleBox<'ll>> {
1913 let is_indirect_call = unsafe { llvm::LLVMRustIsNonGVFunctionPointerTy(llfn) };
1914 let kcfi_bundle = if self.tcx.sess.is_sanitizer_kcfi_enabled()
1915 && let Some(fn_abi) = fn_abi
1916 && is_indirect_call
1917 {
1918 if let Some(fn_attrs) = fn_attrs
1919 && fn_attrs.sanitizers.disabled.contains(SanitizerSet::KCFI)
1920 {
1921 return None;
1922 }
1923
1924 let mut options = kcfi::TypeIdOptions::empty();
1925 if self.tcx.sess.is_sanitizer_cfi_generalize_pointers_enabled() {
1926 options.insert(kcfi::TypeIdOptions::GENERALIZE_POINTERS);
1927 }
1928 if self.tcx.sess.is_sanitizer_cfi_normalize_integers_enabled() {
1929 options.insert(kcfi::TypeIdOptions::NORMALIZE_INTEGERS);
1930 }
1931
1932 let kcfi_typeid = if let Some(instance) = instance {
1933 kcfi::typeid_for_instance(self.tcx, instance, options)
1934 } else {
1935 kcfi::typeid_for_fnabi(self.tcx, fn_abi, options)
1936 };
1937
1938 Some(llvm::OperandBundleBox::new("kcfi", &[self.const_u32(kcfi_typeid)]))
1939 } else {
1940 None
1941 };
1942 kcfi_bundle
1943 }
1944
1945 #[instrument(level = "debug", skip(self))]
1947 pub(crate) fn instrprof_increment(
1948 &mut self,
1949 fn_name: &'ll Value,
1950 hash: &'ll Value,
1951 num_counters: &'ll Value,
1952 index: &'ll Value,
1953 ) {
1954 self.call_intrinsic("llvm.instrprof.increment", &[], &[fn_name, hash, num_counters, index]);
1955 }
1956}