]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_llvm/src/builder.rs
New upstream version 1.58.1+dfsg1
[rustc.git] / compiler / rustc_codegen_llvm / src / builder.rs
CommitLineData
9fa01778
XL
1use crate::common::Funclet;
2use crate::context::CodegenCx;
dfeec247
XL
3use crate::llvm::{self, BasicBlock, False};
4use crate::llvm::{AtomicOrdering, AtomicRmwBinOp, SynchronizationScope};
cdc7bbd5 5use crate::llvm_util;
9fa01778
XL
6use crate::type_::Type;
7use crate::type_of::LayoutLlvmExt;
8use crate::value::Value;
6a06907d 9use cstr::cstr;
dfeec247 10use libc::{c_char, c_uint};
dfeec247
XL
11use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, TypeKind};
12use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
a1dfa0c6 13use rustc_codegen_ssa::mir::place::PlaceRef;
dfeec247
XL
14use rustc_codegen_ssa::traits::*;
15use rustc_codegen_ssa::MemFlags;
dfeec247
XL
16use rustc_data_structures::small_c_str::SmallCStr;
17use rustc_hir::def_id::DefId;
c295e0f8
XL
18use rustc_middle::ty::layout::{
19 FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
20};
ba9703b0 21use rustc_middle::ty::{self, Ty, TyCtxt};
cdc7bbd5 22use rustc_span::Span;
c295e0f8 23use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
48663c56 24use rustc_target::spec::{HasTargetSpec, Target};
1bb2cb6e 25use std::borrow::Cow;
dc9dc135 26use std::ffi::CStr;
cdc7bbd5 27use std::iter;
c295e0f8 28use std::ops::Deref;
85aaf69f 29use std::ptr;
3dfed10e 30use tracing::debug;
1a4d82fc 31
32a655c1
SL
32// All Builders must have an llfn associated with them
33#[must_use]
dc9dc135 34pub struct Builder<'a, 'll, 'tcx> {
b7449926
XL
35 pub llbuilder: &'ll mut llvm::Builder<'ll>,
36 pub cx: &'a CodegenCx<'ll, 'tcx>,
1a4d82fc
JJ
37}
38
b7449926 39impl Drop for Builder<'a, 'll, 'tcx> {
32a655c1
SL
40 fn drop(&mut self) {
41 unsafe {
b7449926 42 llvm::LLVMDisposeBuilder(&mut *(self.llbuilder as *mut _));
32a655c1
SL
43 }
44 }
45}
46
dc9dc135 47// FIXME(eddyb) use a checked constructor when they become `const fn`.
dfeec247 48const EMPTY_C_STR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };
dc9dc135
XL
49
50/// Empty string, to be used where LLVM expects an instruction name, indicating
51/// that the instruction is to be left unnamed (i.e. numbered, in textual IR).
52// FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer.
53const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
1a4d82fc 54
a1dfa0c6
XL
55impl BackendTypes for Builder<'_, 'll, 'tcx> {
56 type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
e74abb32 57 type Function = <CodegenCx<'ll, 'tcx> as BackendTypes>::Function;
a1dfa0c6
XL
58 type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
59 type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
60 type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;
61
62 type DIScope = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIScope;
29967ef6 63 type DILocation = <CodegenCx<'ll, 'tcx> as BackendTypes>::DILocation;
74b04a01 64 type DIVariable = <CodegenCx<'ll, 'tcx> as BackendTypes>::DIVariable;
a1dfa0c6
XL
65}
66
ba9703b0
XL
67impl abi::HasDataLayout for Builder<'_, '_, '_> {
68 fn data_layout(&self) -> &abi::TargetDataLayout {
a1dfa0c6 69 self.cx.data_layout()
83c7162d
XL
70 }
71}
72
a1dfa0c6 73impl ty::layout::HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
17df50a5 74 #[inline]
dc9dc135 75 fn tcx(&self) -> TyCtxt<'tcx> {
a1dfa0c6
XL
76 self.cx.tcx
77 }
78}
79
48663c56
XL
80impl ty::layout::HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
81 fn param_env(&self) -> ty::ParamEnv<'tcx> {
82 self.cx.param_env()
83 }
84}
85
86impl HasTargetSpec for Builder<'_, '_, 'tcx> {
17df50a5 87 #[inline]
48663c56 88 fn target_spec(&self) -> &Target {
c295e0f8 89 self.cx.target_spec()
48663c56
XL
90 }
91}
92
c295e0f8
XL
93impl LayoutOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
94 type LayoutOfResult = TyAndLayout<'tcx>;
a1dfa0c6 95
c295e0f8
XL
96 #[inline]
97 fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
98 self.cx.handle_layout_err(err, span, ty)
99 }
100}
101
102impl FnAbiOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
103 type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
104
105 #[inline]
106 fn handle_fn_abi_err(
107 &self,
108 err: FnAbiError<'tcx>,
109 span: Span,
110 fn_abi_request: FnAbiRequest<'tcx>,
111 ) -> ! {
112 self.cx.handle_fn_abi_err(err, span, fn_abi_request)
a1dfa0c6
XL
113 }
114}
115
116impl Deref for Builder<'_, 'll, 'tcx> {
117 type Target = CodegenCx<'ll, 'tcx>;
118
17df50a5 119 #[inline]
a1dfa0c6
XL
120 fn deref(&self) -> &Self::Target {
121 self.cx
122 }
123}
124
125impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
126 type CodegenCx = CodegenCx<'ll, 'tcx>;
127}
128
dc9dc135
XL
129macro_rules! builder_methods_for_value_instructions {
130 ($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
131 $(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
132 unsafe {
133 llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED)
134 }
135 })+
136 }
137}
138
a1dfa0c6 139impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
17df50a5
XL
140 fn build(cx: &'a CodegenCx<'ll, 'tcx>, llbb: &'ll BasicBlock) -> Self {
141 let bx = Builder::with_cx(cx);
142 unsafe {
143 llvm::LLVMPositionBuilderAtEnd(bx.llbuilder, llbb);
144 }
2c00a5a8 145 bx
32a655c1
SL
146 }
147
17df50a5
XL
148 fn cx(&self) -> &CodegenCx<'ll, 'tcx> {
149 self.cx
32a655c1
SL
150 }
151
a1dfa0c6 152 fn llbb(&self) -> &'ll BasicBlock {
dfeec247 153 unsafe { llvm::LLVMGetInsertBlock(self.llbuilder) }
32a655c1
SL
154 }
155
29967ef6
XL
156 fn set_span(&mut self, _span: Span) {}
157
17df50a5 158 fn append_block(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &str) -> &'ll BasicBlock {
1a4d82fc 159 unsafe {
17df50a5
XL
160 let name = SmallCStr::new(name);
161 llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, name.as_ptr())
1a4d82fc
JJ
162 }
163 }
164
17df50a5
XL
165 fn append_sibling_block(&mut self, name: &str) -> &'ll BasicBlock {
166 Self::append_block(self.cx, self.llfn(), name)
167 }
168
169 fn build_sibling_block(&mut self, name: &str) -> Self {
170 let llbb = self.append_sibling_block(name);
171 Self::build(self.cx, llbb)
172 }
173
a1dfa0c6 174 fn ret_void(&mut self) {
1a4d82fc
JJ
175 unsafe {
176 llvm::LLVMBuildRetVoid(self.llbuilder);
177 }
178 }
179
a1dfa0c6 180 fn ret(&mut self, v: &'ll Value) {
1a4d82fc
JJ
181 unsafe {
182 llvm::LLVMBuildRet(self.llbuilder, v);
183 }
184 }
185
a1dfa0c6 186 fn br(&mut self, dest: &'ll BasicBlock) {
1a4d82fc
JJ
187 unsafe {
188 llvm::LLVMBuildBr(self.llbuilder, dest);
189 }
190 }
191
a1dfa0c6
XL
192 fn cond_br(
193 &mut self,
b7449926
XL
194 cond: &'ll Value,
195 then_llbb: &'ll BasicBlock,
196 else_llbb: &'ll BasicBlock,
197 ) {
1a4d82fc
JJ
198 unsafe {
199 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
200 }
201 }
202
a1dfa0c6
XL
203 fn switch(
204 &mut self,
b7449926
XL
205 v: &'ll Value,
206 else_llbb: &'ll BasicBlock,
1b1a35ee 207 cases: impl ExactSizeIterator<Item = (u128, &'ll BasicBlock)>,
532ac7d7 208 ) {
dfeec247
XL
209 let switch =
210 unsafe { llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, cases.len() as c_uint) };
532ac7d7
XL
211 for (on_val, dest) in cases {
212 let on_val = self.const_uint_big(self.val_ty(v), on_val);
dfeec247 213 unsafe { llvm::LLVMAddCase(switch, on_val, dest) }
1a4d82fc
JJ
214 }
215 }
216
a1dfa0c6
XL
217 fn invoke(
218 &mut self,
94222f64 219 llty: &'ll Type,
a1dfa0c6
XL
220 llfn: &'ll Value,
221 args: &[&'ll Value],
222 then: &'ll BasicBlock,
223 catch: &'ll BasicBlock,
224 funclet: Option<&Funclet<'ll>>,
225 ) -> &'ll Value {
dfeec247 226 debug!("invoke {:?} with args ({:?})", llfn, args);
1a4d82fc 227
94222f64 228 let args = self.check_call("invoke", llty, llfn, args);
a1dfa0c6
XL
229 let bundle = funclet.map(|funclet| funclet.bundle());
230 let bundle = bundle.as_ref().map(|b| &*b.raw);
7453a54e 231
1a4d82fc 232 unsafe {
dfeec247
XL
233 llvm::LLVMRustBuildInvoke(
234 self.llbuilder,
94222f64 235 llty,
dfeec247
XL
236 llfn,
237 args.as_ptr(),
238 args.len() as c_uint,
239 then,
240 catch,
241 bundle,
242 UNNAMED,
243 )
1a4d82fc
JJ
244 }
245 }
246
a1dfa0c6 247 fn unreachable(&mut self) {
1a4d82fc
JJ
248 unsafe {
249 llvm::LLVMBuildUnreachable(self.llbuilder);
250 }
251 }
252
dc9dc135
XL
253 builder_methods_for_value_instructions! {
254 add(a, b) => LLVMBuildAdd,
255 fadd(a, b) => LLVMBuildFAdd,
256 sub(a, b) => LLVMBuildSub,
257 fsub(a, b) => LLVMBuildFSub,
258 mul(a, b) => LLVMBuildMul,
259 fmul(a, b) => LLVMBuildFMul,
260 udiv(a, b) => LLVMBuildUDiv,
261 exactudiv(a, b) => LLVMBuildExactUDiv,
262 sdiv(a, b) => LLVMBuildSDiv,
263 exactsdiv(a, b) => LLVMBuildExactSDiv,
264 fdiv(a, b) => LLVMBuildFDiv,
265 urem(a, b) => LLVMBuildURem,
266 srem(a, b) => LLVMBuildSRem,
267 frem(a, b) => LLVMBuildFRem,
268 shl(a, b) => LLVMBuildShl,
269 lshr(a, b) => LLVMBuildLShr,
270 ashr(a, b) => LLVMBuildAShr,
271 and(a, b) => LLVMBuildAnd,
272 or(a, b) => LLVMBuildOr,
273 xor(a, b) => LLVMBuildXor,
274 neg(x) => LLVMBuildNeg,
275 fneg(x) => LLVMBuildFNeg,
276 not(x) => LLVMBuildNot,
277 unchecked_sadd(x, y) => LLVMBuildNSWAdd,
278 unchecked_uadd(x, y) => LLVMBuildNUWAdd,
279 unchecked_ssub(x, y) => LLVMBuildNSWSub,
280 unchecked_usub(x, y) => LLVMBuildNUWSub,
281 unchecked_smul(x, y) => LLVMBuildNSWMul,
282 unchecked_umul(x, y) => LLVMBuildNUWMul,
1a4d82fc
JJ
283 }
284
a1dfa0c6 285 fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
54a0048b 286 unsafe {
dc9dc135 287 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
cdc7bbd5 288 llvm::LLVMRustSetFastMath(instr);
54a0048b
SL
289 instr
290 }
291 }
292
a1dfa0c6 293 fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
54a0048b 294 unsafe {
dc9dc135 295 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
cdc7bbd5 296 llvm::LLVMRustSetFastMath(instr);
54a0048b
SL
297 instr
298 }
299 }
300
a1dfa0c6 301 fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
54a0048b 302 unsafe {
dc9dc135 303 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
cdc7bbd5 304 llvm::LLVMRustSetFastMath(instr);
54a0048b
SL
305 instr
306 }
307 }
308
a1dfa0c6 309 fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
54a0048b 310 unsafe {
dc9dc135 311 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
cdc7bbd5 312 llvm::LLVMRustSetFastMath(instr);
54a0048b
SL
313 instr
314 }
315 }
316
a1dfa0c6 317 fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
54a0048b 318 unsafe {
dc9dc135 319 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
cdc7bbd5 320 llvm::LLVMRustSetFastMath(instr);
54a0048b
SL
321 instr
322 }
323 }
324
a1dfa0c6
XL
325 fn checked_binop(
326 &mut self,
327 oop: OverflowOp,
9fa01778 328 ty: Ty<'_>,
a1dfa0c6
XL
329 lhs: Self::Value,
330 rhs: Self::Value,
331 ) -> (Self::Value, Self::Value) {
ba9703b0 332 use rustc_middle::ty::{Int, Uint};
5869c6ff 333 use rustc_middle::ty::{IntTy::*, UintTy::*};
a1dfa0c6 334
1b1a35ee 335 let new_kind = match ty.kind() {
29967ef6
XL
336 Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.pointer_width)),
337 Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.pointer_width)),
1b1a35ee 338 t @ (Uint(_) | Int(_)) => t.clone(),
dfeec247 339 _ => panic!("tried to get overflow intrinsic for op applied to non-int type"),
a1dfa0c6
XL
340 };
341
342 let name = match oop {
e74abb32 343 OverflowOp::Add => match new_kind {
a1dfa0c6
XL
344 Int(I8) => "llvm.sadd.with.overflow.i8",
345 Int(I16) => "llvm.sadd.with.overflow.i16",
346 Int(I32) => "llvm.sadd.with.overflow.i32",
347 Int(I64) => "llvm.sadd.with.overflow.i64",
348 Int(I128) => "llvm.sadd.with.overflow.i128",
349
350 Uint(U8) => "llvm.uadd.with.overflow.i8",
351 Uint(U16) => "llvm.uadd.with.overflow.i16",
352 Uint(U32) => "llvm.uadd.with.overflow.i32",
353 Uint(U64) => "llvm.uadd.with.overflow.i64",
354 Uint(U128) => "llvm.uadd.with.overflow.i128",
355
356 _ => unreachable!(),
357 },
e74abb32 358 OverflowOp::Sub => match new_kind {
a1dfa0c6
XL
359 Int(I8) => "llvm.ssub.with.overflow.i8",
360 Int(I16) => "llvm.ssub.with.overflow.i16",
361 Int(I32) => "llvm.ssub.with.overflow.i32",
362 Int(I64) => "llvm.ssub.with.overflow.i64",
363 Int(I128) => "llvm.ssub.with.overflow.i128",
364
365 Uint(U8) => "llvm.usub.with.overflow.i8",
366 Uint(U16) => "llvm.usub.with.overflow.i16",
367 Uint(U32) => "llvm.usub.with.overflow.i32",
368 Uint(U64) => "llvm.usub.with.overflow.i64",
369 Uint(U128) => "llvm.usub.with.overflow.i128",
370
371 _ => unreachable!(),
372 },
e74abb32 373 OverflowOp::Mul => match new_kind {
a1dfa0c6
XL
374 Int(I8) => "llvm.smul.with.overflow.i8",
375 Int(I16) => "llvm.smul.with.overflow.i16",
376 Int(I32) => "llvm.smul.with.overflow.i32",
377 Int(I64) => "llvm.smul.with.overflow.i64",
378 Int(I128) => "llvm.smul.with.overflow.i128",
379
380 Uint(U8) => "llvm.umul.with.overflow.i8",
381 Uint(U16) => "llvm.umul.with.overflow.i16",
382 Uint(U32) => "llvm.umul.with.overflow.i32",
383 Uint(U64) => "llvm.umul.with.overflow.i64",
384 Uint(U128) => "llvm.umul.with.overflow.i128",
385
386 _ => unreachable!(),
387 },
388 };
389
94222f64 390 let res = self.call_intrinsic(name, &[lhs, rhs]);
dfeec247 391 (self.extract_value(res, 0), self.extract_value(res, 1))
a1dfa0c6
XL
392 }
393
1b1a35ee
XL
394 fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
395 if self.cx().val_ty(val) == self.cx().type_i1() {
396 self.zext(val, self.cx().type_i8())
397 } else {
398 val
399 }
400 }
c295e0f8 401 fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
1b1a35ee
XL
402 if scalar.is_bool() {
403 return self.trunc(val, self.cx().type_i1());
404 }
405 val
406 }
407
e1599b0c 408 fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
a1dfa0c6 409 let mut bx = Builder::with_cx(self.cx);
dfeec247 410 bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
e1599b0c 411 bx.dynamic_alloca(ty, align)
32a655c1
SL
412 }
413
e1599b0c 414 fn dynamic_alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
1a4d82fc 415 unsafe {
e1599b0c 416 let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
a1dfa0c6 417 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
cc61c64b 418 alloca
1a4d82fc
JJ
419 }
420 }
421
dfeec247 422 fn array_alloca(&mut self, ty: &'ll Type, len: &'ll Value, align: Align) -> &'ll Value {
1a4d82fc 423 unsafe {
e1599b0c 424 let alloca = llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED);
a1dfa0c6 425 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
b7449926 426 alloca
1a4d82fc
JJ
427 }
428 }
429
136023e0 430 fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
1a4d82fc 431 unsafe {
136023e0 432 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
a1dfa0c6 433 llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
32a655c1 434 load
1a4d82fc
JJ
435 }
436 }
437
136023e0 438 fn volatile_load(&mut self, ty: &'ll Type, ptr: &'ll Value) -> &'ll Value {
1a4d82fc 439 unsafe {
136023e0 440 let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
dc9dc135
XL
441 llvm::LLVMSetVolatile(load, llvm::True);
442 load
1a4d82fc
JJ
443 }
444 }
445
a1dfa0c6
XL
446 fn atomic_load(
447 &mut self,
136023e0 448 ty: &'ll Type,
a1dfa0c6
XL
449 ptr: &'ll Value,
450 order: rustc_codegen_ssa::common::AtomicOrdering,
451 size: Size,
452 ) -> &'ll Value {
1a4d82fc 453 unsafe {
a1dfa0c6
XL
454 let load = llvm::LLVMRustBuildAtomicLoad(
455 self.llbuilder,
136023e0 456 ty,
a1dfa0c6 457 ptr,
dc9dc135 458 UNNAMED,
a1dfa0c6
XL
459 AtomicOrdering::from_generic(order),
460 );
461 // LLVM requires the alignment of atomic loads to be at least the size of the type.
462 llvm::LLVMSetAlignment(load, size.bytes() as c_uint);
ff7c6d11 463 load
1a4d82fc
JJ
464 }
465 }
466
dfeec247 467 fn load_operand(&mut self, place: PlaceRef<'tcx, &'ll Value>) -> OperandRef<'tcx, &'ll Value> {
a1dfa0c6
XL
468 debug!("PlaceRef::load: {:?}", place);
469
470 assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
471
472 if place.layout.is_zst() {
532ac7d7 473 return OperandRef::new_zst(self, place.layout);
a1dfa0c6
XL
474 }
475
476 fn scalar_load_metadata<'a, 'll, 'tcx>(
477 bx: &mut Builder<'a, 'll, 'tcx>,
478 load: &'ll Value,
c295e0f8 479 scalar: abi::Scalar,
a1dfa0c6 480 ) {
a1dfa0c6 481 match scalar.value {
ba9703b0 482 abi::Int(..) => {
c295e0f8
XL
483 if !scalar.is_always_valid(bx) {
484 bx.range_metadata(load, scalar.valid_range);
a1dfa0c6
XL
485 }
486 }
c295e0f8 487 abi::Pointer if !scalar.valid_range.contains(0) => {
a1dfa0c6
XL
488 bx.nonnull_metadata(load);
489 }
490 _ => {}
491 }
492 }
493
494 let val = if let Some(llextra) = place.llextra {
495 OperandValue::Ref(place.llval, Some(llextra), place.align)
496 } else if place.layout.is_llvm_immediate() {
497 let mut const_llval = None;
498 unsafe {
499 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
500 if llvm::LLVMIsGlobalConstant(global) == llvm::True {
501 const_llval = llvm::LLVMGetInitializer(global);
502 }
503 }
504 }
505 let llval = const_llval.unwrap_or_else(|| {
136023e0 506 let load = self.load(place.layout.llvm_type(self), place.llval, place.align);
c295e0f8 507 if let abi::Abi::Scalar(scalar) = place.layout.abi {
a1dfa0c6
XL
508 scalar_load_metadata(self, load, scalar);
509 }
510 load
511 });
1b1a35ee 512 OperandValue::Immediate(self.to_immediate(llval, place.layout))
c295e0f8 513 } else if let abi::Abi::ScalarPair(a, b) = place.layout.abi {
a1dfa0c6 514 let b_offset = a.value.size(self).align_to(b.value.align(self).abi);
94222f64 515 let pair_ty = place.layout.llvm_type(self);
a1dfa0c6 516
c295e0f8 517 let mut load = |i, scalar: abi::Scalar, align| {
94222f64 518 let llptr = self.struct_gep(pair_ty, place.llval, i as u64);
136023e0
XL
519 let llty = place.layout.scalar_pair_element_llvm_type(self, i, false);
520 let load = self.load(llty, llptr, align);
a1dfa0c6 521 scalar_load_metadata(self, load, scalar);
1b1a35ee 522 self.to_immediate_scalar(load, scalar)
a1dfa0c6
XL
523 };
524
525 OperandValue::Pair(
526 load(0, a, place.align),
527 load(1, b, place.align.restrict_for_offset(b_offset)),
528 )
529 } else {
530 OperandValue::Ref(place.llval, None, place.align)
531 };
532
533 OperandRef { val, layout: place.layout }
534 }
535
532ac7d7
XL
536 fn write_operand_repeatedly(
537 mut self,
538 cg_elem: OperandRef<'tcx, &'ll Value>,
539 count: u64,
540 dest: PlaceRef<'tcx, &'ll Value>,
541 ) -> Self {
542 let zero = self.const_usize(0);
543 let count = self.const_usize(count);
544 let start = dest.project_index(&mut self, zero).llval;
545 let end = dest.project_index(&mut self, count).llval;
546
547 let mut header_bx = self.build_sibling_block("repeat_loop_header");
548 let mut body_bx = self.build_sibling_block("repeat_loop_body");
549 let next_bx = self.build_sibling_block("repeat_loop_next");
550
551 self.br(header_bx.llbb());
552 let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
553
554 let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
555 header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
556
557 let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
dfeec247
XL
558 cg_elem
559 .val
560 .store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
a1dfa0c6 561
94222f64
XL
562 let next = body_bx.inbounds_gep(
563 self.backend_type(cg_elem.layout),
564 current,
565 &[self.const_usize(1)],
566 );
532ac7d7
XL
567 body_bx.br(header_bx.llbb());
568 header_bx.add_incoming_to_phi(current, next, body_bx.llbb());
569
570 next_bx
571 }
1a4d82fc 572
c295e0f8 573 fn range_metadata(&mut self, load: &'ll Value, range: WrappingRange) {
29967ef6 574 if self.sess().target.arch == "amdgpu" {
94222f64 575 // amdgpu/LLVM does something weird and thinks an i64 value is
b7449926
XL
576 // split into a v2i32, halving the bitwidth LLVM expects,
577 // tripping an assertion. So, for now, just disable this
578 // optimization.
579 return;
580 }
581
1a4d82fc 582 unsafe {
a1dfa0c6 583 let llty = self.cx.val_ty(load);
ff7c6d11 584 let v = [
a1dfa0c6 585 self.cx.const_uint_big(llty, range.start),
c295e0f8 586 self.cx.const_uint_big(llty, range.end.wrapping_add(1)),
ff7c6d11 587 ];
1a4d82fc 588
dfeec247
XL
589 llvm::LLVMSetMetadata(
590 load,
591 llvm::MD_range as c_uint,
592 llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
593 );
1a4d82fc 594 }
1a4d82fc
JJ
595 }
596
a1dfa0c6 597 fn nonnull_metadata(&mut self, load: &'ll Value) {
85aaf69f 598 unsafe {
dfeec247
XL
599 llvm::LLVMSetMetadata(
600 load,
601 llvm::MD_nonnull as c_uint,
602 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
603 );
85aaf69f 604 }
85aaf69f
SL
605 }
606
3c0e092e
XL
607 fn type_metadata(&mut self, function: &'ll Value, typeid: String) {
608 let typeid_metadata = self.typeid_metadata(typeid);
609 let v = [self.const_usize(0), typeid_metadata];
610 unsafe {
611 llvm::LLVMGlobalSetMetadata(
612 function,
613 llvm::MD_type as c_uint,
614 llvm::LLVMValueAsMetadata(llvm::LLVMMDNodeInContext(
615 self.cx.llcx,
616 v.as_ptr(),
617 v.len() as c_uint,
618 )),
619 )
620 }
621 }
622
623 fn typeid_metadata(&mut self, typeid: String) -> Self::Value {
624 unsafe {
625 llvm::LLVMMDStringInContext(
626 self.cx.llcx,
627 typeid.as_ptr() as *const c_char,
628 typeid.as_bytes().len() as c_uint,
629 )
630 }
631 }
632
a1dfa0c6 633 fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
83c7162d
XL
634 self.store_with_flags(val, ptr, align, MemFlags::empty())
635 }
636
a1dfa0c6
XL
637 fn store_with_flags(
638 &mut self,
b7449926
XL
639 val: &'ll Value,
640 ptr: &'ll Value,
83c7162d
XL
641 align: Align,
642 flags: MemFlags,
b7449926
XL
643 ) -> &'ll Value {
644 debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
1bb2cb6e 645 let ptr = self.check_store(val, ptr);
1a4d82fc 646 unsafe {
32a655c1 647 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
dfeec247
XL
648 let align =
649 if flags.contains(MemFlags::UNALIGNED) { 1 } else { align.bytes() as c_uint };
8faf50e0 650 llvm::LLVMSetAlignment(store, align);
83c7162d
XL
651 if flags.contains(MemFlags::VOLATILE) {
652 llvm::LLVMSetVolatile(store, llvm::True);
653 }
654 if flags.contains(MemFlags::NONTEMPORAL) {
655 // According to LLVM [1] building a nontemporal store must
656 // *always* point to a metadata value of the integer 1.
657 //
136023e0 658 // [1]: https://llvm.org/docs/LangRef.html#store-instruction
a1dfa0c6 659 let one = self.cx.const_i32(1);
83c7162d
XL
660 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
661 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
662 }
32a655c1 663 store
1a4d82fc
JJ
664 }
665 }
666
dfeec247
XL
667 fn atomic_store(
668 &mut self,
669 val: &'ll Value,
670 ptr: &'ll Value,
671 order: rustc_codegen_ssa::common::AtomicOrdering,
672 size: Size,
673 ) {
b7449926 674 debug!("Store {:?} -> {:?}", val, ptr);
1bb2cb6e 675 let ptr = self.check_store(val, ptr);
1a4d82fc 676 unsafe {
a1dfa0c6
XL
677 let store = llvm::LLVMRustBuildAtomicStore(
678 self.llbuilder,
679 val,
680 ptr,
681 AtomicOrdering::from_generic(order),
682 );
683 // LLVM requires the alignment of atomic stores to be at least the size of the type.
684 llvm::LLVMSetAlignment(store, size.bytes() as c_uint);
ff7c6d11
XL
685 }
686 }
687
94222f64 688 fn gep(&mut self, ty: &'ll Type, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
1a4d82fc 689 unsafe {
94222f64 690 llvm::LLVMBuildGEP2(
dfeec247 691 self.llbuilder,
94222f64 692 ty,
dfeec247
XL
693 ptr,
694 indices.as_ptr(),
695 indices.len() as c_uint,
696 UNNAMED,
697 )
1a4d82fc
JJ
698 }
699 }
700
94222f64
XL
701 fn inbounds_gep(
702 &mut self,
703 ty: &'ll Type,
704 ptr: &'ll Value,
705 indices: &[&'ll Value],
706 ) -> &'ll Value {
1a4d82fc 707 unsafe {
94222f64 708 llvm::LLVMBuildInBoundsGEP2(
dfeec247 709 self.llbuilder,
94222f64 710 ty,
dfeec247
XL
711 ptr,
712 indices.as_ptr(),
713 indices.len() as c_uint,
714 UNNAMED,
715 )
1a4d82fc
JJ
716 }
717 }
718
94222f64 719 fn struct_gep(&mut self, ty: &'ll Type, ptr: &'ll Value, idx: u64) -> &'ll Value {
532ac7d7 720 assert_eq!(idx as c_uint as u64, idx);
94222f64 721 unsafe { llvm::LLVMBuildStructGEP2(self.llbuilder, ty, ptr, idx as c_uint, UNNAMED) }
532ac7d7
XL
722 }
723
1a4d82fc 724 /* Casts */
a1dfa0c6 725 fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 726 unsafe { llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
727 }
728
a1dfa0c6 729 fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 730 unsafe { llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
731 }
732
f035d41b 733 fn fptoui_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
3c0e092e 734 if !self.fptoint_sat_broken_in_llvm() {
f035d41b
XL
735 let src_ty = self.cx.val_ty(val);
736 let float_width = self.cx.float_width(src_ty);
737 let int_width = self.cx.int_width(dest_ty);
cdc7bbd5 738 let name = format!("llvm.fptoui.sat.i{}.f{}", int_width, float_width);
94222f64 739 return Some(self.call_intrinsic(&name, &[val]));
f035d41b 740 }
cdc7bbd5 741
f035d41b
XL
742 None
743 }
744
745 fn fptosi_sat(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> Option<&'ll Value> {
3c0e092e 746 if !self.fptoint_sat_broken_in_llvm() {
f035d41b
XL
747 let src_ty = self.cx.val_ty(val);
748 let float_width = self.cx.float_width(src_ty);
749 let int_width = self.cx.int_width(dest_ty);
cdc7bbd5 750 let name = format!("llvm.fptosi.sat.i{}.f{}", int_width, float_width);
94222f64 751 return Some(self.call_intrinsic(&name, &[val]));
f035d41b 752 }
f035d41b 753
cdc7bbd5 754 None
3dfed10e
XL
755 }
756
a1dfa0c6 757 fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
cdc7bbd5
XL
758 // On WebAssembly the `fptoui` and `fptosi` instructions currently have
759 // poor codegen. The reason for this is that the corresponding wasm
760 // instructions, `i32.trunc_f32_s` for example, will trap when the float
761 // is out-of-bounds, infinity, or nan. This means that LLVM
762 // automatically inserts control flow around `fptoui` and `fptosi`
763 // because the LLVM instruction `fptoui` is defined as producing a
764 // poison value, not having UB on out-of-bounds values.
3dfed10e 765 //
cdc7bbd5
XL
766 // This method, however, is only used with non-saturating casts that
767 // have UB on out-of-bounds values. This means that it's ok if we use
768 // the raw wasm instruction since out-of-bounds values can do whatever
769 // we like. To ensure that LLVM picks the right instruction we choose
770 // the raw wasm intrinsic functions which avoid LLVM inserting all the
771 // other control flow automatically.
3c0e092e 772 if self.sess().target.is_like_wasm {
3dfed10e
XL
773 let src_ty = self.cx.val_ty(val);
774 if self.cx.type_kind(src_ty) != TypeKind::Vector {
775 let float_width = self.cx.float_width(src_ty);
776 let int_width = self.cx.int_width(dest_ty);
777 let name = match (int_width, float_width) {
778 (32, 32) => Some("llvm.wasm.trunc.unsigned.i32.f32"),
779 (32, 64) => Some("llvm.wasm.trunc.unsigned.i32.f64"),
780 (64, 32) => Some("llvm.wasm.trunc.unsigned.i64.f32"),
781 (64, 64) => Some("llvm.wasm.trunc.unsigned.i64.f64"),
782 _ => None,
783 };
784 if let Some(name) = name {
94222f64 785 return self.call_intrinsic(name, &[val]);
3dfed10e
XL
786 }
787 }
788 }
dfeec247 789 unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
790 }
791
a1dfa0c6 792 fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
cdc7bbd5 793 // see `fptoui` above for why wasm is different here
3c0e092e 794 if self.sess().target.is_like_wasm {
3dfed10e
XL
795 let src_ty = self.cx.val_ty(val);
796 if self.cx.type_kind(src_ty) != TypeKind::Vector {
797 let float_width = self.cx.float_width(src_ty);
798 let int_width = self.cx.int_width(dest_ty);
799 let name = match (int_width, float_width) {
800 (32, 32) => Some("llvm.wasm.trunc.signed.i32.f32"),
801 (32, 64) => Some("llvm.wasm.trunc.signed.i32.f64"),
802 (64, 32) => Some("llvm.wasm.trunc.signed.i64.f32"),
803 (64, 64) => Some("llvm.wasm.trunc.signed.i64.f64"),
804 _ => None,
805 };
806 if let Some(name) = name {
94222f64 807 return self.call_intrinsic(name, &[val]);
3dfed10e
XL
808 }
809 }
810 }
dfeec247 811 unsafe { llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
812 }
813
a1dfa0c6 814 fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 815 unsafe { llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
816 }
817
a1dfa0c6 818 fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 819 unsafe { llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
820 }
821
a1dfa0c6 822 fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 823 unsafe { llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
824 }
825
a1dfa0c6 826 fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 827 unsafe { llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
828 }
829
a1dfa0c6 830 fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 831 unsafe { llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
832 }
833
a1dfa0c6 834 fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 835 unsafe { llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
836 }
837
a1dfa0c6 838 fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 839 unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
840 }
841
a1dfa0c6 842 fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
dfeec247 843 unsafe { llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed) }
1a4d82fc
JJ
844 }
845
a1dfa0c6 846 fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 847 unsafe { llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED) }
1a4d82fc
JJ
848 }
849
1a4d82fc 850 /* Comparisons */
a1dfa0c6 851 fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
a1dfa0c6 852 let op = llvm::IntPredicate::from_generic(op);
dfeec247 853 unsafe { llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
1a4d82fc
JJ
854 }
855
a1dfa0c6 856 fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
c295e0f8 857 let op = llvm::RealPredicate::from_generic(op);
dfeec247 858 unsafe { llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED) }
1a4d82fc
JJ
859 }
860
861 /* Miscellaneous instructions */
dfeec247
XL
862 fn memcpy(
863 &mut self,
864 dst: &'ll Value,
865 dst_align: Align,
866 src: &'ll Value,
867 src_align: Align,
868 size: &'ll Value,
869 flags: MemFlags,
870 ) {
136023e0 871 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
a1dfa0c6
XL
872 let size = self.intcast(size, self.type_isize(), false);
873 let is_volatile = flags.contains(MemFlags::VOLATILE);
874 let dst = self.pointercast(dst, self.type_i8p());
875 let src = self.pointercast(src, self.type_i8p());
876 unsafe {
dfeec247
XL
877 llvm::LLVMRustBuildMemCpy(
878 self.llbuilder,
879 dst,
880 dst_align.bytes() as c_uint,
881 src,
882 src_align.bytes() as c_uint,
883 size,
884 is_volatile,
885 );
a1dfa0c6
XL
886 }
887 }
7453a54e 888
dfeec247
XL
889 fn memmove(
890 &mut self,
891 dst: &'ll Value,
892 dst_align: Align,
893 src: &'ll Value,
894 src_align: Align,
895 size: &'ll Value,
896 flags: MemFlags,
897 ) {
136023e0 898 assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memmove not supported");
a1dfa0c6
XL
899 let size = self.intcast(size, self.type_isize(), false);
900 let is_volatile = flags.contains(MemFlags::VOLATILE);
901 let dst = self.pointercast(dst, self.type_i8p());
902 let src = self.pointercast(src, self.type_i8p());
1a4d82fc 903 unsafe {
dfeec247
XL
904 llvm::LLVMRustBuildMemMove(
905 self.llbuilder,
906 dst,
907 dst_align.bytes() as c_uint,
908 src,
909 src_align.bytes() as c_uint,
910 size,
911 is_volatile,
912 );
1a4d82fc
JJ
913 }
914 }
915
a1dfa0c6
XL
916 fn memset(
917 &mut self,
918 ptr: &'ll Value,
919 fill_byte: &'ll Value,
920 size: &'ll Value,
921 align: Align,
922 flags: MemFlags,
923 ) {
74b04a01 924 let is_volatile = flags.contains(MemFlags::VOLATILE);
a1dfa0c6 925 let ptr = self.pointercast(ptr, self.type_i8p());
74b04a01
XL
926 unsafe {
927 llvm::LLVMRustBuildMemSet(
928 self.llbuilder,
929 ptr,
930 align.bytes() as c_uint,
931 fill_byte,
932 size,
933 is_volatile,
934 );
935 }
a1dfa0c6
XL
936 }
937
a1dfa0c6 938 fn select(
dfeec247
XL
939 &mut self,
940 cond: &'ll Value,
b7449926
XL
941 then_val: &'ll Value,
942 else_val: &'ll Value,
943 ) -> &'ll Value {
dfeec247 944 unsafe { llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, UNNAMED) }
1a4d82fc
JJ
945 }
946
a1dfa0c6 947 fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
dfeec247 948 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
1a4d82fc
JJ
949 }
950
a1dfa0c6 951 fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
dfeec247 952 unsafe { llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED) }
1a4d82fc
JJ
953 }
954
a1dfa0c6 955 fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
1a4d82fc 956 unsafe {
a1dfa0c6
XL
957 let elt_ty = self.cx.val_ty(elt);
958 let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
959 let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
960 let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
961 self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
1a4d82fc
JJ
962 }
963 }
964
a1dfa0c6 965 fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
ff7c6d11 966 assert_eq!(idx as c_uint as u64, idx);
dfeec247 967 unsafe { llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED) }
1a4d82fc
JJ
968 }
969
dfeec247 970 fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value, idx: u64) -> &'ll Value {
ff7c6d11 971 assert_eq!(idx as c_uint as u64, idx);
dfeec247 972 unsafe { llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint, UNNAMED) }
1a4d82fc
JJ
973 }
974
dfeec247
XL
975 fn landing_pad(
976 &mut self,
977 ty: &'ll Type,
978 pers_fn: &'ll Value,
979 num_clauses: usize,
980 ) -> &'ll Value {
136023e0
XL
981 // Use LLVMSetPersonalityFn to set the personality. It supports arbitrary Consts while,
982 // LLVMBuildLandingPad requires the argument to be a Function (as of LLVM 12). The
983 // personality lives on the parent function anyway.
984 self.set_personality_fn(pers_fn);
1a4d82fc 985 unsafe {
136023e0 986 llvm::LLVMBuildLandingPad(self.llbuilder, ty, None, num_clauses as c_uint, UNNAMED)
c1a9b12d
SL
987 }
988 }
989
a1dfa0c6 990 fn set_cleanup(&mut self, landing_pad: &'ll Value) {
1a4d82fc
JJ
991 unsafe {
992 llvm::LLVMSetCleanup(landing_pad, llvm::True);
993 }
994 }
995
a1dfa0c6 996 fn resume(&mut self, exn: &'ll Value) -> &'ll Value {
dfeec247 997 unsafe { llvm::LLVMBuildResume(self.llbuilder, exn) }
1a4d82fc
JJ
998 }
999
dfeec247 1000 fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
6a06907d 1001 let name = cstr!("cleanuppad");
7453a54e 1002 let ret = unsafe {
dfeec247
XL
1003 llvm::LLVMRustBuildCleanupPad(
1004 self.llbuilder,
1005 parent,
1006 args.len() as c_uint,
1007 args.as_ptr(),
1008 name.as_ptr(),
1009 )
7453a54e 1010 };
a1dfa0c6 1011 Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
7453a54e
SL
1012 }
1013
a1dfa0c6 1014 fn cleanup_ret(
dfeec247
XL
1015 &mut self,
1016 funclet: &Funclet<'ll>,
b7449926
XL
1017 unwind: Option<&'ll BasicBlock>,
1018 ) -> &'ll Value {
dfeec247
XL
1019 let ret =
1020 unsafe { llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind) };
b7449926 1021 ret.expect("LLVM does not have support for cleanupret")
7453a54e
SL
1022 }
1023
dfeec247 1024 fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
6a06907d 1025 let name = cstr!("catchpad");
7453a54e 1026 let ret = unsafe {
dfeec247
XL
1027 llvm::LLVMRustBuildCatchPad(
1028 self.llbuilder,
1029 parent,
1030 args.len() as c_uint,
1031 args.as_ptr(),
1032 name.as_ptr(),
1033 )
7453a54e 1034 };
a1dfa0c6 1035 Funclet::new(ret.expect("LLVM does not have support for catchpad"))
7453a54e
SL
1036 }
1037
a1dfa0c6
XL
1038 fn catch_switch(
1039 &mut self,
b7449926
XL
1040 parent: Option<&'ll Value>,
1041 unwind: Option<&'ll BasicBlock>,
1042 num_handlers: usize,
1043 ) -> &'ll Value {
6a06907d 1044 let name = cstr!("catchswitch");
7453a54e 1045 let ret = unsafe {
dfeec247
XL
1046 llvm::LLVMRustBuildCatchSwitch(
1047 self.llbuilder,
1048 parent,
1049 unwind,
1050 num_handlers as c_uint,
1051 name.as_ptr(),
1052 )
7453a54e 1053 };
b7449926 1054 ret.expect("LLVM does not have support for catchswitch")
7453a54e
SL
1055 }
1056
a1dfa0c6 1057 fn add_handler(&mut self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
7453a54e
SL
1058 unsafe {
1059 llvm::LLVMRustAddHandler(catch_switch, handler);
1060 }
1061 }
1062
a1dfa0c6 1063 fn set_personality_fn(&mut self, personality: &'ll Value) {
7453a54e 1064 unsafe {
8bb4bdeb 1065 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
7453a54e
SL
1066 }
1067 }
1068
1a4d82fc 1069 // Atomic Operations
a1dfa0c6
XL
1070 fn atomic_cmpxchg(
1071 &mut self,
b7449926
XL
1072 dst: &'ll Value,
1073 cmp: &'ll Value,
1074 src: &'ll Value,
a1dfa0c6
XL
1075 order: rustc_codegen_ssa::common::AtomicOrdering,
1076 failure_order: rustc_codegen_ssa::common::AtomicOrdering,
1077 weak: bool,
b7449926 1078 ) -> &'ll Value {
a1dfa0c6
XL
1079 let weak = if weak { llvm::True } else { llvm::False };
1080 unsafe {
1081 llvm::LLVMRustBuildAtomicCmpXchg(
1082 self.llbuilder,
1083 dst,
1084 cmp,
1085 src,
1086 AtomicOrdering::from_generic(order),
1087 AtomicOrdering::from_generic(failure_order),
dfeec247 1088 weak,
a1dfa0c6 1089 )
1a4d82fc
JJ
1090 }
1091 }
a1dfa0c6
XL
1092 fn atomic_rmw(
1093 &mut self,
1094 op: rustc_codegen_ssa::common::AtomicRmwBinOp,
b7449926
XL
1095 dst: &'ll Value,
1096 src: &'ll Value,
a1dfa0c6 1097 order: rustc_codegen_ssa::common::AtomicOrdering,
b7449926 1098 ) -> &'ll Value {
1a4d82fc 1099 unsafe {
a1dfa0c6
XL
1100 llvm::LLVMBuildAtomicRMW(
1101 self.llbuilder,
1102 AtomicRmwBinOp::from_generic(op),
1103 dst,
1104 src,
1105 AtomicOrdering::from_generic(order),
dfeec247
XL
1106 False,
1107 )
1a4d82fc
JJ
1108 }
1109 }
1110
a1dfa0c6
XL
1111 fn atomic_fence(
1112 &mut self,
1113 order: rustc_codegen_ssa::common::AtomicOrdering,
dfeec247 1114 scope: rustc_codegen_ssa::common::SynchronizationScope,
a1dfa0c6 1115 ) {
1a4d82fc 1116 unsafe {
a1dfa0c6
XL
1117 llvm::LLVMRustBuildAtomicFence(
1118 self.llbuilder,
1119 AtomicOrdering::from_generic(order),
dfeec247 1120 SynchronizationScope::from_generic(scope),
a1dfa0c6 1121 );
1a4d82fc
JJ
1122 }
1123 }
a7813a04 1124
532ac7d7 1125 fn set_invariant_load(&mut self, load: &'ll Value) {
32a655c1 1126 unsafe {
dfeec247
XL
1127 llvm::LLVMSetMetadata(
1128 load,
1129 llvm::MD_invariant_load as c_uint,
1130 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1131 );
32a655c1
SL
1132 }
1133 }
1134
532ac7d7 1135 fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
74b04a01 1136 self.call_lifetime_intrinsic("llvm.lifetime.start.p0i8", ptr, size);
532ac7d7
XL
1137 }
1138
1139 fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
74b04a01 1140 self.call_lifetime_intrinsic("llvm.lifetime.end.p0i8", ptr, size);
532ac7d7
XL
1141 }
1142
f035d41b
XL
1143 fn instrprof_increment(
1144 &mut self,
1145 fn_name: &'ll Value,
1146 hash: &'ll Value,
1147 num_counters: &'ll Value,
1148 index: &'ll Value,
3dfed10e 1149 ) {
f035d41b
XL
1150 debug!(
1151 "instrprof_increment() with args ({:?}, {:?}, {:?}, {:?})",
1152 fn_name, hash, num_counters, index
1153 );
1154
3dfed10e 1155 let llfn = unsafe { llvm::LLVMRustGetInstrProfIncrementIntrinsic(self.cx().llmod) };
94222f64
XL
1156 let llty = self.cx.type_func(
1157 &[self.cx.type_i8p(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_i32()],
1158 self.cx.type_void(),
1159 );
f035d41b 1160 let args = &[fn_name, hash, num_counters, index];
94222f64 1161 let args = self.check_call("call", llty, llfn, args);
f035d41b
XL
1162
1163 unsafe {
3dfed10e 1164 let _ = llvm::LLVMRustBuildCall(
f035d41b 1165 self.llbuilder,
94222f64 1166 llty,
f035d41b
XL
1167 llfn,
1168 args.as_ptr() as *const &llvm::Value,
1169 args.len() as c_uint,
1170 None,
3dfed10e 1171 );
f035d41b
XL
1172 }
1173 }
1174
532ac7d7
XL
1175 fn call(
1176 &mut self,
94222f64 1177 llty: &'ll Type,
532ac7d7
XL
1178 llfn: &'ll Value,
1179 args: &[&'ll Value],
1180 funclet: Option<&Funclet<'ll>>,
1181 ) -> &'ll Value {
dfeec247 1182 debug!("call {:?} with args ({:?})", llfn, args);
532ac7d7 1183
94222f64 1184 let args = self.check_call("call", llty, llfn, args);
532ac7d7
XL
1185 let bundle = funclet.map(|funclet| funclet.bundle());
1186 let bundle = bundle.as_ref().map(|b| &*b.raw);
1187
32a655c1 1188 unsafe {
532ac7d7
XL
1189 llvm::LLVMRustBuildCall(
1190 self.llbuilder,
94222f64 1191 llty,
532ac7d7
XL
1192 llfn,
1193 args.as_ptr() as *const &llvm::Value,
1194 args.len() as c_uint,
dfeec247 1195 bundle,
532ac7d7 1196 )
32a655c1
SL
1197 }
1198 }
1199
532ac7d7 1200 fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
dfeec247 1201 unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
8bb4bdeb
XL
1202 }
1203
532ac7d7
XL
1204 fn do_not_inline(&mut self, llret: &'ll Value) {
1205 llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1206 }
1207}
1208
dc9dc135
XL
1209impl StaticBuilderMethods for Builder<'a, 'll, 'tcx> {
1210 fn get_static(&mut self, def_id: DefId) -> &'ll Value {
532ac7d7
XL
1211 // Forward to the `get_static` method of `CodegenCx`
1212 self.cx().get_static(def_id)
1213 }
532ac7d7
XL
1214}
1215
1216impl Builder<'a, 'll, 'tcx> {
17df50a5
XL
1217 fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
1218 // Create a fresh builder from the crate context.
1219 let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(cx.llcx) };
1220 Builder { llbuilder, cx }
1221 }
1222
532ac7d7 1223 pub fn llfn(&self) -> &'ll Value {
dfeec247 1224 unsafe { llvm::LLVMGetBasicBlockParent(self.llbb()) }
532ac7d7
XL
1225 }
1226
532ac7d7
XL
1227 fn position_at_start(&mut self, llbb: &'ll BasicBlock) {
1228 unsafe {
1229 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
1230 }
1231 }
1232
1233 pub fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
532ac7d7
XL
1234 unsafe { llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs) }
1235 }
1236
1237 pub fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
532ac7d7
XL
1238 unsafe { llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs) }
1239 }
1240
1241 pub fn insert_element(
dfeec247
XL
1242 &mut self,
1243 vec: &'ll Value,
532ac7d7
XL
1244 elt: &'ll Value,
1245 idx: &'ll Value,
1246 ) -> &'ll Value {
dfeec247 1247 unsafe { llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, UNNAMED) }
532ac7d7
XL
1248 }
1249
1250 pub fn shuffle_vector(
1251 &mut self,
1252 v1: &'ll Value,
1253 v2: &'ll Value,
1254 mask: &'ll Value,
1255 ) -> &'ll Value {
dfeec247 1256 unsafe { llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, UNNAMED) }
532ac7d7
XL
1257 }
1258
416331ca
XL
1259 pub fn vector_reduce_fadd(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1260 unsafe { llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src) }
1261 }
1262 pub fn vector_reduce_fmul(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
1263 unsafe { llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src) }
1264 }
532ac7d7 1265 pub fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
532ac7d7 1266 unsafe {
532ac7d7 1267 let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src);
cdc7bbd5 1268 llvm::LLVMRustSetFastMath(instr);
532ac7d7
XL
1269 instr
1270 }
1271 }
1272 pub fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
532ac7d7 1273 unsafe {
532ac7d7 1274 let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src);
cdc7bbd5 1275 llvm::LLVMRustSetFastMath(instr);
532ac7d7
XL
1276 instr
1277 }
1278 }
1279 pub fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
532ac7d7
XL
1280 unsafe { llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src) }
1281 }
1282 pub fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
532ac7d7
XL
1283 unsafe { llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src) }
1284 }
1285 pub fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
532ac7d7
XL
1286 unsafe { llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src) }
1287 }
1288 pub fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
532ac7d7
XL
1289 unsafe { llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src) }
1290 }
1291 pub fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
532ac7d7
XL
1292 unsafe { llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src) }
1293 }
1294 pub fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
dfeec247
XL
1295 unsafe {
1296 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false)
1297 }
532ac7d7
XL
1298 }
1299 pub fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
dfeec247
XL
1300 unsafe {
1301 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false)
1302 }
532ac7d7
XL
1303 }
1304 pub fn vector_reduce_fmin_fast(&mut self, src: &'ll Value) -> &'ll Value {
532ac7d7 1305 unsafe {
dfeec247
XL
1306 let instr =
1307 llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true);
cdc7bbd5 1308 llvm::LLVMRustSetFastMath(instr);
532ac7d7
XL
1309 instr
1310 }
1311 }
1312 pub fn vector_reduce_fmax_fast(&mut self, src: &'ll Value) -> &'ll Value {
532ac7d7 1313 unsafe {
dfeec247
XL
1314 let instr =
1315 llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true);
cdc7bbd5 1316 llvm::LLVMRustSetFastMath(instr);
532ac7d7
XL
1317 instr
1318 }
1319 }
1320 pub fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
532ac7d7
XL
1321 unsafe { llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed) }
1322 }
1323 pub fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
532ac7d7
XL
1324 unsafe { llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed) }
1325 }
1326
1327 pub fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
1328 unsafe {
1329 llvm::LLVMAddClause(landing_pad, clause);
1330 }
1331 }
1332
1333 pub fn catch_ret(&mut self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
dfeec247
XL
1334 let ret =
1335 unsafe { llvm::LLVMRustBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind) };
532ac7d7
XL
1336 ret.expect("LLVM does not have support for catchret")
1337 }
1338
dc9dc135 1339 fn check_store(&mut self, val: &'ll Value, ptr: &'ll Value) -> &'ll Value {
a1dfa0c6
XL
1340 let dest_ptr_ty = self.cx.val_ty(ptr);
1341 let stored_ty = self.cx.val_ty(val);
1342 let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
1bb2cb6e 1343
a1dfa0c6 1344 assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
1bb2cb6e
SL
1345
1346 if dest_ptr_ty == stored_ptr_ty {
1347 ptr
1348 } else {
dfeec247
XL
1349 debug!(
1350 "type mismatch in store. \
1bb2cb6e 1351 Expected {:?}, got {:?}; inserting bitcast",
dfeec247
XL
1352 dest_ptr_ty, stored_ptr_ty
1353 );
1bb2cb6e
SL
1354 self.bitcast(ptr, stored_ptr_ty)
1355 }
1356 }
1357
dfeec247
XL
1358 fn check_call<'b>(
1359 &mut self,
1360 typ: &str,
94222f64 1361 fn_ty: &'ll Type,
dfeec247
XL
1362 llfn: &'ll Value,
1363 args: &'b [&'ll Value],
1364 ) -> Cow<'b, [&'ll Value]> {
dfeec247
XL
1365 assert!(
1366 self.cx.type_kind(fn_ty) == TypeKind::Function,
1367 "builder::{} not passed a function, but {:?}",
1368 typ,
1369 fn_ty
1370 );
a7813a04 1371
a1dfa0c6 1372 let param_tys = self.cx.func_params_types(fn_ty);
a7813a04 1373
cdc7bbd5 1374 let all_args_match = iter::zip(&param_tys, args.iter().map(|&v| self.val_ty(v)))
1bb2cb6e
SL
1375 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1376
1377 if all_args_match {
1378 return Cow::Borrowed(args);
1379 }
1380
cdc7bbd5 1381 let casted_args: Vec<_> = iter::zip(param_tys, args)
1bb2cb6e
SL
1382 .enumerate()
1383 .map(|(i, (expected_ty, &actual_val))| {
a1dfa0c6 1384 let actual_ty = self.val_ty(actual_val);
1bb2cb6e 1385 if expected_ty != actual_ty {
dfeec247
XL
1386 debug!(
1387 "type mismatch in function call of {:?}. \
1bb2cb6e 1388 Expected {:?} for param {}, got {:?}; injecting bitcast",
dfeec247
XL
1389 llfn, expected_ty, i, actual_ty
1390 );
1bb2cb6e
SL
1391 self.bitcast(actual_val, expected_ty)
1392 } else {
1393 actual_val
1394 }
1395 })
1396 .collect();
a7813a04 1397
0bf4aa26 1398 Cow::Owned(casted_args)
a7813a04 1399 }
ff7c6d11 1400
532ac7d7 1401 pub fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
dfeec247 1402 unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
a1dfa0c6
XL
1403 }
1404
94222f64
XL
1405 crate fn call_intrinsic(&mut self, intrinsic: &str, args: &[&'ll Value]) -> &'ll Value {
1406 let (ty, f) = self.cx.get_intrinsic(intrinsic);
1407 self.call(ty, f, args, None)
1408 }
1409
a1dfa0c6 1410 fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
74b04a01
XL
1411 let size = size.bytes();
1412 if size == 0 {
ff7c6d11
XL
1413 return;
1414 }
1415
f9f354fc 1416 if !self.cx().sess().emit_lifetime_markers() {
ff7c6d11
XL
1417 return;
1418 }
1419
a1dfa0c6 1420 let ptr = self.pointercast(ptr, self.cx.type_i8p());
94222f64 1421 self.call_intrinsic(intrinsic, &[self.cx.const_u64(size), ptr]);
ff7c6d11 1422 }
532ac7d7 1423
3dfed10e
XL
1424 pub(crate) fn phi(
1425 &mut self,
1426 ty: &'ll Type,
1427 vals: &[&'ll Value],
1428 bbs: &[&'ll BasicBlock],
1429 ) -> &'ll Value {
532ac7d7 1430 assert_eq!(vals.len(), bbs.len());
dfeec247 1431 let phi = unsafe { llvm::LLVMBuildPhi(self.llbuilder, ty, UNNAMED) };
532ac7d7 1432 unsafe {
dfeec247 1433 llvm::LLVMAddIncoming(phi, vals.as_ptr(), bbs.as_ptr(), vals.len() as c_uint);
532ac7d7
XL
1434 phi
1435 }
1436 }
1437
1438 fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
532ac7d7
XL
1439 unsafe {
1440 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1441 }
1442 }
3dfed10e 1443
cdc7bbd5
XL
1444 fn fptoint_sat_broken_in_llvm(&self) -> bool {
1445 match self.tcx.sess.target.arch.as_str() {
1446 // FIXME - https://bugs.llvm.org/show_bug.cgi?id=50083
1447 "riscv64" => llvm_util::get_version() < (13, 0, 0),
1448 _ => false,
1449 }
3dfed10e 1450 }
a7813a04 1451}