3 use std::convert::TryFrom;
4 use std::ops::{Deref, Range};
6 use gccjit::FunctionType;
18 use rustc_codegen_ssa::MemFlags;
19 use rustc_codegen_ssa::common::{AtomicOrdering, AtomicRmwBinOp, IntPredicate, RealPredicate, SynchronizationScope};
20 use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
21 use rustc_codegen_ssa::mir::place::PlaceRef;
22 use rustc_codegen_ssa::traits::{
33 use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
34 use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, TyAndLayout};
36 use rustc_span::def_id::DefId;
37 use rustc_target::abi::{
45 use rustc_target::spec::{HasTargetSpec, Target};
47 use crate::common::{SignType, TypeReflection, type_is_pointer};
48 use crate::context::CodegenCx;
49 use crate::type_of::LayoutGccExt;
54 // TODO: remove this variable.
55 static mut RETURN_VALUE_COUNT: usize = 0;
57 enum ExtremumOperation {
63 fn clone(&self) -> Self;
66 impl EnumClone for AtomicOrdering {
67 fn clone(&self) -> Self {
69 AtomicOrdering::NotAtomic => AtomicOrdering::NotAtomic,
70 AtomicOrdering::Unordered => AtomicOrdering::Unordered,
71 AtomicOrdering::Monotonic => AtomicOrdering::Monotonic,
72 AtomicOrdering::Acquire => AtomicOrdering::Acquire,
73 AtomicOrdering::Release => AtomicOrdering::Release,
74 AtomicOrdering::AcquireRelease => AtomicOrdering::AcquireRelease,
75 AtomicOrdering::SequentiallyConsistent => AtomicOrdering::SequentiallyConsistent,
80 pub struct Builder<'a: 'gcc, 'gcc, 'tcx> {
81 pub cx: &'a CodegenCx<'gcc, 'tcx>,
82 pub block: Option<Block<'gcc>>,
83 stack_var_count: Cell<usize>,
86 impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
87 fn with_cx(cx: &'a CodegenCx<'gcc, 'tcx>) -> Self {
91 stack_var_count: Cell::new(0),
95 fn atomic_extremum(&mut self, operation: ExtremumOperation, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
96 let size = self.cx.int_width(src.get_type()) / 8;
98 let func = self.current_func();
102 // TODO: does this make sense?
103 AtomicOrdering::AcquireRelease | AtomicOrdering::Release => AtomicOrdering::Acquire,
106 let previous_value = self.atomic_load(dst.get_type(), dst, load_ordering.clone(), Size::from_bytes(size));
107 let previous_var = func.new_local(None, previous_value.get_type(), "previous_value");
108 let return_value = func.new_local(None, previous_value.get_type(), "return_value");
109 self.llbb().add_assignment(None, previous_var, previous_value);
110 self.llbb().add_assignment(None, return_value, previous_var.to_rvalue());
112 let while_block = func.new_block("while");
113 let after_block = func.new_block("after_while");
114 self.llbb().end_with_jump(None, while_block);
116 // NOTE: since jumps were added and compare_exchange doesn't expect this, the current blocks in the
117 // state need to be updated.
118 self.block = Some(while_block);
119 *self.cx.current_block.borrow_mut() = Some(while_block);
121 let comparison_operator =
123 ExtremumOperation::Max => ComparisonOp::LessThan,
124 ExtremumOperation::Min => ComparisonOp::GreaterThan,
127 let cond1 = self.context.new_comparison(None, comparison_operator, previous_var.to_rvalue(), self.context.new_cast(None, src, previous_value.get_type()));
128 let compare_exchange = self.compare_exchange(dst, previous_var, src, order, load_ordering, false);
129 let cond2 = self.cx.context.new_unary_op(None, UnaryOp::LogicalNegate, compare_exchange.get_type(), compare_exchange);
130 let cond = self.cx.context.new_binary_op(None, BinaryOp::LogicalAnd, self.cx.bool_type, cond1, cond2);
132 while_block.end_with_conditional(None, cond, while_block, after_block);
134 // NOTE: since jumps were added in a place rustc does not expect, the current blocks in the
135 // state need to be updated.
136 self.block = Some(after_block);
137 *self.cx.current_block.borrow_mut() = Some(after_block);
139 return_value.to_rvalue()
142 fn compare_exchange(&self, dst: RValue<'gcc>, cmp: LValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
143 let size = self.cx.int_width(src.get_type());
144 let compare_exchange = self.context.get_builtin_function(&format!("__atomic_compare_exchange_{}", size / 8));
145 let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
146 let failure_order = self.context.new_rvalue_from_int(self.i32_type, failure_order.to_gcc());
147 let weak = self.context.new_rvalue_from_int(self.bool_type, weak as i32);
149 let void_ptr_type = self.context.new_type::<*mut ()>();
150 let volatile_void_ptr_type = void_ptr_type.make_volatile();
151 let dst = self.context.new_cast(None, dst, volatile_void_ptr_type);
152 let expected = self.context.new_cast(None, cmp.get_address(None), void_ptr_type);
154 // NOTE: not sure why, but we have the wrong type here.
155 let int_type = compare_exchange.get_param(2).to_rvalue().get_type();
156 let src = self.context.new_cast(None, src, int_type);
157 self.context.new_call(None, compare_exchange, &[dst, expected, src, weak, order, failure_order])
160 pub fn assign(&self, lvalue: LValue<'gcc>, value: RValue<'gcc>) {
161 self.llbb().add_assignment(None, lvalue, value);
164 fn check_call<'b>(&mut self, _typ: &str, func: Function<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
165 //let mut fn_ty = self.cx.val_ty(func);
166 // Strip off pointers
167 /*while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
168 fn_ty = self.cx.element_type(fn_ty);
172 self.cx.type_kind(fn_ty) == TypeKind::Function,
173 "builder::{} not passed a function, but {:?}",
178 let param_tys = self.cx.func_params_types(fn_ty);
180 let all_args_match = param_tys
182 .zip(args.iter().map(|&v| self.val_ty(v)))
183 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);*/
185 let mut all_args_match = true;
186 let mut param_types = vec![];
187 let param_count = func.get_param_count();
188 for (index, arg) in args.iter().enumerate().take(param_count) {
189 let param = func.get_param(index as i32);
190 let param = param.to_rvalue().get_type();
191 if param != arg.get_type() {
192 all_args_match = false;
194 param_types.push(param);
198 return Cow::Borrowed(args);
201 let casted_args: Vec<_> = param_types
205 .map(|(_i, (expected_ty, &actual_val))| {
206 let actual_ty = actual_val.get_type();
207 if expected_ty != actual_ty {
209 "type mismatch in function call of {:?}. \
210 Expected {:?} for param {}, got {:?}; injecting bitcast",
211 func, expected_ty, i, actual_ty
214 "type mismatch in function call of {:?}. \
215 Expected {:?} for param {}, got {:?}; injecting bitcast",
216 func, expected_ty, i, actual_ty
218 self.bitcast(actual_val, expected_ty)
226 Cow::Owned(casted_args)
229 fn check_ptr_call<'b>(&mut self, _typ: &str, func_ptr: RValue<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
230 //let mut fn_ty = self.cx.val_ty(func);
231 // Strip off pointers
232 /*while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
233 fn_ty = self.cx.element_type(fn_ty);
237 self.cx.type_kind(fn_ty) == TypeKind::Function,
238 "builder::{} not passed a function, but {:?}",
243 let param_tys = self.cx.func_params_types(fn_ty);
245 let all_args_match = param_tys
247 .zip(args.iter().map(|&v| self.val_ty(v)))
248 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);*/
250 let mut all_args_match = true;
251 let mut param_types = vec![];
252 let gcc_func = func_ptr.get_type().is_function_ptr_type().expect("function ptr");
253 for (index, arg) in args.iter().enumerate().take(gcc_func.get_param_count()) {
254 let param = gcc_func.get_param_type(index);
255 if param != arg.get_type() {
256 all_args_match = false;
258 param_types.push(param);
262 return Cow::Borrowed(args);
265 let casted_args: Vec<_> = param_types
269 .map(|(_i, (expected_ty, &actual_val))| {
270 let actual_ty = actual_val.get_type();
271 if expected_ty != actual_ty {
273 "type mismatch in function call of {:?}. \
274 Expected {:?} for param {}, got {:?}; injecting bitcast",
275 func, expected_ty, i, actual_ty
278 "type mismatch in function call of {:?}. \
279 Expected {:?} for param {}, got {:?}; injecting bitcast",
280 func, expected_ty, i, actual_ty
282 self.bitcast(actual_val, expected_ty)
290 Cow::Owned(casted_args)
293 fn check_store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
294 let dest_ptr_ty = self.cx.val_ty(ptr).make_pointer(); // TODO: make sure make_pointer() is okay here.
295 let stored_ty = self.cx.val_ty(val);
296 let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
298 //assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
300 if dest_ptr_ty == stored_ptr_ty {
305 "type mismatch in store. \
306 Expected {:?}, got {:?}; inserting bitcast",
307 dest_ptr_ty, stored_ptr_ty
310 "type mismatch in store. \
311 Expected {:?}, got {:?}; inserting bitcast",
312 dest_ptr_ty, stored_ptr_ty
315 self.bitcast(ptr, stored_ptr_ty)
319 pub fn current_func(&self) -> Function<'gcc> {
320 self.block.expect("block").get_function()
323 fn function_call(&mut self, func: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
324 //debug!("call {:?} with args ({:?})", func, args);
326 // TODO: remove when the API supports a different type for functions.
327 let func: Function<'gcc> = self.cx.rvalue_as_function(func);
328 let args = self.check_call("call", func, args);
329 //let bundle = funclet.map(|funclet| funclet.bundle());
330 //let bundle = bundle.as_ref().map(|b| &*b.raw);
332 // gccjit requires to use the result of functions, even when it's not used.
333 // That's why we assign the result to a local or call add_eval().
334 let return_type = func.get_return_type();
335 let current_block = self.current_block.borrow().expect("block");
336 let void_type = self.context.new_type::<()>();
337 let current_func = current_block.get_function();
338 if return_type != void_type {
339 unsafe { RETURN_VALUE_COUNT += 1 };
340 let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
341 current_block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
345 current_block.add_eval(None, self.cx.context.new_call(None, func, &args));
346 // Return dummy value when not having return value.
347 self.context.new_rvalue_from_long(self.isize_type, 0)
351 fn function_ptr_call(&mut self, func_ptr: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
352 //debug!("func ptr call {:?} with args ({:?})", func, args);
354 let args = self.check_ptr_call("call", func_ptr, args);
355 //let bundle = funclet.map(|funclet| funclet.bundle());
356 //let bundle = bundle.as_ref().map(|b| &*b.raw);
358 // gccjit requires to use the result of functions, even when it's not used.
359 // That's why we assign the result to a local or call add_eval().
360 let gcc_func = func_ptr.get_type().is_function_ptr_type().expect("function ptr");
361 let mut return_type = gcc_func.get_return_type();
362 let current_block = self.current_block.borrow().expect("block");
363 let void_type = self.context.new_type::<()>();
364 let current_func = current_block.get_function();
366 // FIXME: As a temporary workaround for unsupported LLVM intrinsics.
367 if gcc_func.get_param_count() == 0 && format!("{:?}", func_ptr) == "__builtin_ia32_pmovmskb128" {
368 return_type = self.int_type;
371 if return_type != void_type {
372 unsafe { RETURN_VALUE_COUNT += 1 };
373 let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
374 current_block.add_assignment(None, result, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
378 if gcc_func.get_param_count() == 0 {
379 // FIXME: As a temporary workaround for unsupported LLVM intrinsics.
380 current_block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &[]));
383 current_block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
385 // Return dummy value when not having return value.
386 let result = current_func.new_local(None, self.isize_type, "dummyValueThatShouldNeverBeUsed");
387 current_block.add_assignment(None, result, self.context.new_rvalue_from_long(self.isize_type, 0));
392 pub fn overflow_call(&mut self, func: Function<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
393 //debug!("overflow_call {:?} with args ({:?})", func, args);
395 //let bundle = funclet.map(|funclet| funclet.bundle());
396 //let bundle = bundle.as_ref().map(|b| &*b.raw);
398 // gccjit requires to use the result of functions, even when it's not used.
399 // That's why we assign the result to a local.
400 let return_type = self.context.new_type::<bool>();
401 let current_block = self.current_block.borrow().expect("block");
402 let current_func = current_block.get_function();
403 // TODO: return the new_call() directly? Since the overflow function has no side-effects.
404 unsafe { RETURN_VALUE_COUNT += 1 };
405 let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
406 current_block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
411 impl<'gcc, 'tcx> HasCodegen<'tcx> for Builder<'_, 'gcc, 'tcx> {
412 type CodegenCx = CodegenCx<'gcc, 'tcx>;
415 impl<'tcx> HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
416 fn tcx(&self) -> TyCtxt<'tcx> {
421 impl HasDataLayout for Builder<'_, '_, '_> {
422 fn data_layout(&self) -> &TargetDataLayout {
423 self.cx.data_layout()
427 impl<'tcx> LayoutOf for Builder<'_, '_, 'tcx> {
429 type TyAndLayout = TyAndLayout<'tcx>;
431 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
432 self.cx.layout_of(ty)
436 impl<'gcc, 'tcx> Deref for Builder<'_, 'gcc, 'tcx> {
437 type Target = CodegenCx<'gcc, 'tcx>;
439 fn deref(&self) -> &Self::Target {
444 impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
445 type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
446 type Function = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Function;
447 type BasicBlock = <CodegenCx<'gcc, 'tcx> as BackendTypes>::BasicBlock;
448 type Type = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Type;
449 type Funclet = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Funclet;
451 type DIScope = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIScope;
452 type DILocation = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DILocation;
453 type DIVariable = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIVariable;
456 impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
457 fn build(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
458 let mut bx = Builder::with_cx(cx);
459 *cx.current_block.borrow_mut() = Some(block);
460 bx.block = Some(block);
464 fn build_sibling_block(&mut self, name: &str) -> Self {
465 let block = self.append_sibling_block(name);
466 Self::build(self.cx, block)
469 fn llbb(&self) -> Block<'gcc> {
470 self.block.expect("block")
473 fn append_block(cx: &'a CodegenCx<'gcc, 'tcx>, func: RValue<'gcc>, name: &str) -> Block<'gcc> {
474 let func = cx.rvalue_as_function(func);
478 fn append_sibling_block(&mut self, name: &str) -> Block<'gcc> {
479 let func = self.current_func();
483 fn ret_void(&mut self) {
484 self.llbb().end_with_void_return(None)
487 fn ret(&mut self, value: RValue<'gcc>) {
489 if self.structs_as_pointer.borrow().contains(&value) {
490 // NOTE: hack to workaround a limitation of the rustc API: see comment on
491 // CodegenCx.structs_as_pointer
492 value.dereference(None).to_rvalue()
497 self.llbb().end_with_return(None, value);
500 fn br(&mut self, dest: Block<'gcc>) {
501 self.llbb().end_with_jump(None, dest)
504 fn cond_br(&mut self, cond: RValue<'gcc>, then_block: Block<'gcc>, else_block: Block<'gcc>) {
505 self.llbb().end_with_conditional(None, cond, then_block, else_block)
508 fn switch(&mut self, value: RValue<'gcc>, default_block: Block<'gcc>, cases: impl ExactSizeIterator<Item = (u128, Block<'gcc>)>) {
509 let mut gcc_cases = vec![];
510 let typ = self.val_ty(value);
511 for (on_val, dest) in cases {
512 let on_val = self.const_uint_big(typ, on_val);
513 gcc_cases.push(self.context.new_case(on_val, on_val, dest));
515 self.block.expect("block").end_with_switch(None, value, default_block, &gcc_cases);
518 fn invoke(&mut self, _typ: Type<'gcc>, _func: RValue<'gcc>, _args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
519 let condition = self.context.new_rvalue_from_int(self.bool_type, 0);
520 self.llbb().end_with_conditional(None, condition, then, catch);
521 self.context.new_rvalue_from_int(self.int_type, 0)
524 /*debug!("invoke {:?} with args ({:?})", func, args);
526 let args = self.check_call("invoke", func, args);
527 let bundle = funclet.map(|funclet| funclet.bundle());
528 let bundle = bundle.as_ref().map(|b| &*b.raw);
531 llvm::LLVMRustBuildInvoke(
535 args.len() as c_uint,
544 fn unreachable(&mut self) {
545 let func = self.context.get_builtin_function("__builtin_unreachable");
546 let block = self.block.expect("block");
547 block.add_eval(None, self.context.new_call(None, func, &[]));
548 let return_type = block.get_function().get_return_type();
549 let void_type = self.context.new_type::<()>();
550 if return_type == void_type {
551 block.end_with_void_return(None)
554 let return_value = self.current_func()
555 .new_local(None, return_type, "unreachableReturn");
556 block.end_with_return(None, return_value)
560 fn add(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
561 // FIXME: this should not be required.
562 if format!("{:?}", a.get_type()) != format!("{:?}", b.get_type()) {
563 b = self.context.new_cast(None, b, a.get_type());
568 fn fadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
572 fn sub(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
573 if a.get_type() != b.get_type() {
574 b = self.context.new_cast(None, b, a.get_type());
579 fn fsub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
583 fn mul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
587 fn fmul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
591 fn udiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
592 // TODO: convert the arguments to unsigned?
596 fn exactudiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
597 // TODO: convert the arguments to unsigned?
598 // TODO: poison if not exact.
602 fn sdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
603 // TODO: convert the arguments to signed?
607 fn exactsdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
608 // TODO: posion if not exact.
609 // FIXME: rustc_codegen_ssa::mir::intrinsic uses different types for a and b but they
610 // should be the same.
611 let typ = a.get_type().to_signed(self);
612 let a = self.context.new_cast(None, a, typ);
613 let b = self.context.new_cast(None, b, typ);
617 fn fdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
621 fn urem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
625 fn srem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
629 fn frem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
630 if a.get_type() == self.cx.float_type {
631 let fmodf = self.context.get_builtin_function("fmodf");
632 // FIXME: this seems to produce the wrong result.
633 return self.context.new_call(None, fmodf, &[a, b]);
635 assert_eq!(a.get_type(), self.cx.double_type);
637 let fmod = self.context.get_builtin_function("fmod");
638 return self.context.new_call(None, fmod, &[a, b]);
641 fn shl(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
642 // FIXME: remove the casts when libgccjit can shift an unsigned number by an unsigned number.
643 let a_type = a.get_type();
644 let b_type = b.get_type();
645 if a_type.is_unsigned(self) && b_type.is_signed(self) {
646 //println!("shl: {:?} -> {:?}", a, b_type);
647 let a = self.context.new_cast(None, a, b_type);
649 //println!("shl: {:?} -> {:?}", result, a_type);
650 self.context.new_cast(None, result, a_type)
652 else if a_type.is_signed(self) && b_type.is_unsigned(self) {
653 //println!("shl: {:?} -> {:?}", b, a_type);
654 let b = self.context.new_cast(None, b, a_type);
662 fn lshr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
663 // FIXME: remove the casts when libgccjit can shift an unsigned number by an unsigned number.
664 // TODO: cast to unsigned to do a logical shift if that does not work.
665 let a_type = a.get_type();
666 let b_type = b.get_type();
667 if a_type.is_unsigned(self) && b_type.is_signed(self) {
668 //println!("lshl: {:?} -> {:?}", a, b_type);
669 let a = self.context.new_cast(None, a, b_type);
671 //println!("lshl: {:?} -> {:?}", result, a_type);
672 self.context.new_cast(None, result, a_type)
674 else if a_type.is_signed(self) && b_type.is_unsigned(self) {
675 //println!("lshl: {:?} -> {:?}", b, a_type);
676 let b = self.context.new_cast(None, b, a_type);
684 fn ashr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
685 // TODO: check whether behavior is an arithmetic shift for >> .
686 // FIXME: remove the casts when libgccjit can shift an unsigned number by an unsigned number.
687 let a_type = a.get_type();
688 let b_type = b.get_type();
689 if a_type.is_unsigned(self) && b_type.is_signed(self) {
690 //println!("ashl: {:?} -> {:?}", a, b_type);
691 let a = self.context.new_cast(None, a, b_type);
693 //println!("ashl: {:?} -> {:?}", result, a_type);
694 self.context.new_cast(None, result, a_type)
696 else if a_type.is_signed(self) && b_type.is_unsigned(self) {
697 //println!("ashl: {:?} -> {:?}", b, a_type);
698 let b = self.context.new_cast(None, b, a_type);
706 fn and(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
707 // FIXME: hack by putting the result in a variable to workaround this bug:
708 // https://gcc.gnu.org/bugzilla//show_bug.cgi?id=95498
709 if a.get_type() != b.get_type() {
710 b = self.context.new_cast(None, b, a.get_type());
712 let res = self.current_func().new_local(None, b.get_type(), "andResult");
713 self.llbb().add_assignment(None, res, a & b);
717 fn or(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
718 // FIXME: hack by putting the result in a variable to workaround this bug:
719 // https://gcc.gnu.org/bugzilla//show_bug.cgi?id=95498
720 let res = self.current_func().new_local(None, b.get_type(), "orResult");
721 self.llbb().add_assignment(None, res, a | b);
725 fn xor(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
729 fn neg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
730 // TODO: use new_unary_op()?
731 self.cx.context.new_rvalue_from_long(a.get_type(), 0) - a
734 fn fneg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
735 self.cx.context.new_unary_op(None, UnaryOp::Minus, a.get_type(), a)
738 fn not(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
740 if a.get_type().is_bool() {
741 UnaryOp::LogicalNegate
744 UnaryOp::BitwiseNegate
746 self.cx.context.new_unary_op(None, operation, a.get_type(), a)
749 fn unchecked_sadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
753 fn unchecked_uadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
757 fn unchecked_ssub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
761 fn unchecked_usub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
762 // TODO: should generate poison value?
766 fn unchecked_smul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
770 fn unchecked_umul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
774 fn fadd_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
777 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
778 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
783 fn fsub_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
786 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
787 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
792 fn fmul_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
795 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
796 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
801 fn fdiv_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
804 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
805 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
810 fn frem_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
813 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
814 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
819 fn checked_binop(&mut self, oop: OverflowOp, typ: Ty<'_>, lhs: Self::Value, rhs: Self::Value) -> (Self::Value, Self::Value) {
820 use rustc_middle::ty::{Int, IntTy::*, Uint, UintTy::*};
824 Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.pointer_width)),
825 Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.pointer_width)),
826 t @ (Uint(_) | Int(_)) => t.clone(),
827 _ => panic!("tried to get overflow intrinsic for op applied to non-int type"),
830 // TODO: remove duplication with intrinsic?
835 Int(I8) => "__builtin_add_overflow",
836 Int(I16) => "__builtin_add_overflow",
837 Int(I32) => "__builtin_sadd_overflow",
838 Int(I64) => "__builtin_saddll_overflow",
839 Int(I128) => "__builtin_add_overflow",
841 Uint(U8) => "__builtin_add_overflow",
842 Uint(U16) => "__builtin_add_overflow",
843 Uint(U32) => "__builtin_uadd_overflow",
844 Uint(U64) => "__builtin_uaddll_overflow",
845 Uint(U128) => "__builtin_add_overflow",
851 Int(I8) => "__builtin_sub_overflow",
852 Int(I16) => "__builtin_sub_overflow",
853 Int(I32) => "__builtin_ssub_overflow",
854 Int(I64) => "__builtin_ssubll_overflow",
855 Int(I128) => "__builtin_sub_overflow",
857 Uint(U8) => "__builtin_sub_overflow",
858 Uint(U16) => "__builtin_sub_overflow",
859 Uint(U32) => "__builtin_usub_overflow",
860 Uint(U64) => "__builtin_usubll_overflow",
861 Uint(U128) => "__builtin_sub_overflow",
867 Int(I8) => "__builtin_mul_overflow",
868 Int(I16) => "__builtin_mul_overflow",
869 Int(I32) => "__builtin_smul_overflow",
870 Int(I64) => "__builtin_smulll_overflow",
871 Int(I128) => "__builtin_mul_overflow",
873 Uint(U8) => "__builtin_mul_overflow",
874 Uint(U16) => "__builtin_mul_overflow",
875 Uint(U32) => "__builtin_umul_overflow",
876 Uint(U64) => "__builtin_umulll_overflow",
877 Uint(U128) => "__builtin_mul_overflow",
883 let intrinsic = self.context.get_builtin_function(&name);
884 let res = self.current_func()
885 // TODO: is it correct to use rhs type instead of the parameter typ?
886 .new_local(None, rhs.get_type(), "binopResult")
888 let overflow = self.overflow_call(intrinsic, &[lhs, rhs, res], None);
889 (res.dereference(None).to_rvalue(), overflow)
892 fn alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
893 // FIXME: this check that we don't call get_aligned() a second time on a time.
894 // Ideally, we shouldn't need to do this check.
896 if ty == self.cx.u128_type || ty == self.cx.i128_type {
900 ty.get_aligned(align.bytes())
902 // TODO: It might be better to return a LValue, but fixing the rustc API is non-trivial.
903 self.stack_var_count.set(self.stack_var_count.get() + 1);
904 self.current_func().new_local(None, aligned_type, &format!("stack_var_{}", self.stack_var_count.get())).get_address(None)
907 fn dynamic_alloca(&mut self, _ty: Type<'gcc>, _align: Align) -> RValue<'gcc> {
910 let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
911 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
916 fn array_alloca(&mut self, _ty: Type<'gcc>, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
919 let alloca = llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED);
920 llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
925 fn load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
927 let block = self.llbb();
928 let function = block.get_function();
929 // NOTE: instead of returning the dereference here, we have to assign it to a variable in
930 // the current basic block. Otherwise, it could be used in another basic block, causing a
931 // dereference after a drop, for instance.
932 // TODO: handle align.
933 let deref = ptr.dereference(None).to_rvalue();
934 let value_type = deref.get_type();
935 unsafe { RETURN_VALUE_COUNT += 1 };
936 let loaded_value = function.new_local(None, value_type, &format!("loadedValue{}", unsafe { RETURN_VALUE_COUNT }));
937 block.add_assignment(None, loaded_value, deref);
938 loaded_value.to_rvalue()
941 fn volatile_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
943 //println!("5: volatile load: {:?} to {:?}", ptr, ptr.get_type().make_volatile());
944 let ptr = self.context.new_cast(None, ptr, ptr.get_type().make_volatile());
946 ptr.dereference(None).to_rvalue()
949 fn atomic_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) -> RValue<'gcc> {
951 // TODO: handle alignment.
952 let atomic_load = self.context.get_builtin_function(&format!("__atomic_load_{}", size.bytes()));
953 let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
955 let volatile_const_void_ptr_type = self.context.new_type::<*mut ()>().make_const().make_volatile();
956 let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
957 self.context.new_call(None, atomic_load, &[ptr, ordering])
960 fn load_operand(&mut self, place: PlaceRef<'tcx, RValue<'gcc>>) -> OperandRef<'tcx, RValue<'gcc>> {
961 //debug!("PlaceRef::load: {:?}", place);
963 assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
965 if place.layout.is_zst() {
966 return OperandRef::new_zst(self, place.layout);
969 fn scalar_load_metadata<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, load: RValue<'gcc>, scalar: &abi::Scalar) {
970 let vr = scalar.valid_range.clone();
973 let range = scalar.valid_range_exclusive(bx);
974 if range.start != range.end {
975 bx.range_metadata(load, range);
978 abi::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
979 bx.nonnull_metadata(load);
986 if let Some(llextra) = place.llextra {
987 OperandValue::Ref(place.llval, Some(llextra), place.align)
989 else if place.layout.is_gcc_immediate() {
990 let const_llval = None;
992 if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
993 if llvm::LLVMIsGlobalConstant(global) == llvm::True {
994 const_llval = llvm::LLVMGetInitializer(global);
998 let llval = const_llval.unwrap_or_else(|| {
999 let load = self.load(place.llval.get_type(), place.llval, place.align);
1000 if let abi::Abi::Scalar(ref scalar) = place.layout.abi {
1001 scalar_load_metadata(self, load, scalar);
1005 OperandValue::Immediate(self.to_immediate(llval, place.layout))
1007 else if let abi::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
1008 let b_offset = a.value.size(self).align_to(b.value.align(self).abi);
1009 let pair_type = place.layout.gcc_type(self, false);
1011 let mut load = |i, scalar: &abi::Scalar, align| {
1012 let llptr = self.struct_gep(pair_type, place.llval, i as u64);
1013 let load = self.load(llptr.get_type(), llptr, align);
1014 scalar_load_metadata(self, load, scalar);
1015 if scalar.is_bool() { self.trunc(load, self.type_i1()) } else { load }
1019 load(0, a, place.align),
1020 load(1, b, place.align.restrict_for_offset(b_offset)),
1024 OperandValue::Ref(place.llval, None, place.align)
1027 OperandRef { val, layout: place.layout }
1030 fn write_operand_repeatedly(mut self, cg_elem: OperandRef<'tcx, RValue<'gcc>>, count: u64, dest: PlaceRef<'tcx, RValue<'gcc>>) -> Self {
1031 let zero = self.const_usize(0);
1032 let count = self.const_usize(count);
1033 let start = dest.project_index(&mut self, zero).llval;
1034 let end = dest.project_index(&mut self, count).llval;
1036 let mut header_bx = self.build_sibling_block("repeat_loop_header");
1037 let mut body_bx = self.build_sibling_block("repeat_loop_body");
1038 let next_bx = self.build_sibling_block("repeat_loop_next");
1040 let ptr_type = start.get_type();
1041 let current = self.llbb().get_function().new_local(None, ptr_type, "loop_var");
1042 let current_val = current.to_rvalue();
1043 self.assign(current, start);
1045 self.br(header_bx.llbb());
1047 let keep_going = header_bx.icmp(IntPredicate::IntNE, current_val, end);
1048 header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
1050 let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
1051 cg_elem.val.store(&mut body_bx, PlaceRef::new_sized_aligned(current_val, cg_elem.layout, align));
1053 let next = body_bx.inbounds_gep(self.backend_type(cg_elem.layout), current.to_rvalue(), &[self.const_usize(1)]);
1054 body_bx.llbb().add_assignment(None, current, next);
1055 body_bx.br(header_bx.llbb());
1060 fn range_metadata(&mut self, _load: RValue<'gcc>, _range: Range<u128>) {
1062 /*if self.sess().target.target.arch == "amdgpu" {
1063 // amdgpu/LLVM does something weird and thinks a i64 value is
1064 // split into a v2i32, halving the bitwidth LLVM expects,
1065 // tripping an assertion. So, for now, just disable this
1071 let llty = self.cx.val_ty(load);
1073 self.cx.const_uint_big(llty, range.start),
1074 self.cx.const_uint_big(llty, range.end),
1077 llvm::LLVMSetMetadata(
1079 llvm::MD_range as c_uint,
1080 llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
1085 fn nonnull_metadata(&mut self, _load: RValue<'gcc>) {
1088 llvm::LLVMSetMetadata(
1090 llvm::MD_nonnull as c_uint,
1091 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1096 fn store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
1097 self.store_with_flags(val, ptr, align, MemFlags::empty())
1100 fn store_with_flags(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, _align: Align, _flags: MemFlags) -> RValue<'gcc> {
1101 //debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
1102 let ptr = self.check_store(val, ptr);
1103 self.llbb().add_assignment(None, ptr.dereference(None), val);
1105 if flags.contains(MemFlags::UNALIGNED) { 1 } else { align.bytes() as c_uint };
1106 llvm::LLVMSetAlignment(store, align);
1107 if flags.contains(MemFlags::VOLATILE) {
1108 llvm::LLVMSetVolatile(store, llvm::True);
1110 if flags.contains(MemFlags::NONTEMPORAL) {
1111 // According to LLVM [1] building a nontemporal store must
1112 // *always* point to a metadata value of the integer 1.
1114 // [1]: http://llvm.org/docs/LangRef.html#store-instruction
1115 let one = self.cx.const_i32(1);
1116 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
1117 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
1119 // NOTE: dummy value here since it's never used. FIXME: API should not return a value here?
1120 self.cx.context.new_rvalue_zero(self.type_i32())
1123 fn atomic_store(&mut self, value: RValue<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) {
1124 // TODO: handle alignment.
1125 let atomic_store = self.context.get_builtin_function(&format!("__atomic_store_{}", size.bytes()));
1126 let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1127 let volatile_const_void_ptr_type = self.context.new_type::<*mut ()>().make_const().make_volatile();
1128 let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
1130 // FIXME: fix libgccjit to allow comparing an integer type with an aligned integer type because
1131 // the following cast is required to avoid this error:
1132 // gcc_jit_context_new_call: mismatching types for argument 2 of function "__atomic_store_4": assignment to param arg1 (type: int) from loadedValue3577 (type: unsigned int __attribute__((aligned(4))))
1133 let int_type = atomic_store.get_param(1).to_rvalue().get_type();
1134 let value = self.context.new_cast(None, value, int_type);
1136 .add_eval(None, self.context.new_call(None, atomic_store, &[ptr, value, ordering]));
1139 fn gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
1140 let mut result = ptr;
1141 for index in indices {
1142 result = self.context.new_array_access(None, result, *index).get_address(None).to_rvalue();
1147 fn inbounds_gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
1148 // FIXME: would be safer if doing the same thing (loop) as gep.
1149 // TODO: specify inbounds somehow.
1150 match indices.len() {
1152 self.context.new_array_access(None, ptr, indices[0]).get_address(None)
1155 let array = ptr.dereference(None); // TODO: assert that first index is 0?
1156 self.context.new_array_access(None, array, indices[1]).get_address(None)
1158 _ => unimplemented!(),
1162 fn struct_gep(&mut self, value_type: Type<'gcc>, ptr: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1163 // FIXME: it would be better if the API only called this on struct, not on arrays.
1164 assert_eq!(idx as usize as u64, idx);
1165 let value = ptr.dereference(None).to_rvalue();
1167 if value_type.is_array().is_some() {
1168 let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1169 let element = self.context.new_array_access(None, value, index);
1170 element.get_address(None)
1172 else if let Some(vector_type) = value_type.is_vector() {
1173 let array_type = vector_type.get_element_type().make_pointer();
1174 let array = self.bitcast(ptr, array_type);
1175 let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1176 let element = self.context.new_array_access(None, array, index);
1177 element.get_address(None)
1179 else if let Some(struct_type) = value_type.is_struct() {
1180 ptr.dereference_field(None, struct_type.get_field(idx as i32)).get_address(None)
1183 panic!("Unexpected type {:?}", value_type);
1188 fn trunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1189 // TODO: check that it indeed truncate the value.
1190 //println!("trunc: {:?} -> {:?}", value, dest_ty);
1191 self.context.new_cast(None, value, dest_ty)
1194 fn sext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1195 // TODO: check that it indeed sign extend the value.
1196 //println!("Sext {:?} to {:?}", value, dest_ty);
1197 //if let Some(vector_type) = value.get_type().is_vector() {
1198 if dest_ty.is_vector().is_some() {
1199 // TODO: nothing to do as it is only for LLVM?
1201 /*let dest_type = self.context.new_vector_type(dest_ty, vector_type.get_num_units() as u64);
1202 println!("Casting {:?} to {:?}", value, dest_type);
1203 return self.context.new_cast(None, value, dest_type);*/
1205 self.context.new_cast(None, value, dest_ty)
1208 fn fptoui(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1209 //println!("7: fptoui: {:?} to {:?}", value, dest_ty);
1210 let ret = self.context.new_cast(None, value, dest_ty);
1213 //unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) }
1216 fn fptosi(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1217 self.context.new_cast(None, value, dest_ty)
1220 fn uitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1221 //println!("1: uitofp: {:?} -> {:?}", value, dest_ty);
1222 let ret = self.context.new_cast(None, value, dest_ty);
1227 fn sitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1228 //println!("3: sitofp: {:?} -> {:?}", value, dest_ty);
1229 let ret = self.context.new_cast(None, value, dest_ty);
1234 fn fptrunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1235 // TODO: make sure it trancates.
1236 self.context.new_cast(None, value, dest_ty)
1239 fn fpext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1240 self.context.new_cast(None, value, dest_ty)
1243 fn ptrtoint(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1244 self.cx.ptrtoint(self.block.expect("block"), value, dest_ty)
1247 fn inttoptr(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1248 self.cx.inttoptr(self.block.expect("block"), value, dest_ty)
1251 fn bitcast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1252 self.cx.const_bitcast(value, dest_ty)
1255 fn intcast(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>, _is_signed: bool) -> RValue<'gcc> {
1256 // NOTE: is_signed is for value, not dest_typ.
1257 //println!("intcast: {:?} ({:?}) -> {:?}", value, value.get_type(), dest_typ);
1258 self.cx.context.new_cast(None, value, dest_typ)
1261 fn pointercast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1262 //println!("pointercast: {:?} ({:?}) -> {:?}", value, value.get_type(), dest_ty);
1263 let val_type = value.get_type();
1264 match (type_is_pointer(val_type), type_is_pointer(dest_ty)) {
1266 // NOTE: Projecting a field of a pointer type will attemp a cast from a signed char to
1267 // a pointer, which is not supported by gccjit.
1268 return self.cx.context.new_cast(None, self.inttoptr(value, val_type.make_pointer()), dest_ty);
1271 // When they are not pointers, we want a transmute (or reinterpret_cast).
1272 //self.cx.context.new_cast(None, value, dest_ty)
1273 self.bitcast(value, dest_ty)
1275 (true, true) => self.cx.context.new_cast(None, value, dest_ty),
1276 (true, false) => unimplemented!(),
1281 fn icmp(&mut self, op: IntPredicate, lhs: RValue<'gcc>, mut rhs: RValue<'gcc>) -> RValue<'gcc> {
1282 if lhs.get_type() != rhs.get_type() {
1283 // NOTE: hack because we try to cast a vector type to the same vector type.
1284 if format!("{:?}", lhs.get_type()) != format!("{:?}", rhs.get_type()) {
1285 rhs = self.context.new_cast(None, rhs, lhs.get_type());
1288 self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
1291 fn fcmp(&mut self, op: RealPredicate, lhs: RValue<'gcc>, rhs: RValue<'gcc>) -> RValue<'gcc> {
1292 self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
1295 /* Miscellaneous instructions */
1296 fn memcpy(&mut self, dst: RValue<'gcc>, dst_align: Align, src: RValue<'gcc>, src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
1297 if flags.contains(MemFlags::NONTEMPORAL) {
1298 // HACK(nox): This is inefficient but there is no nontemporal memcpy.
1299 let val = self.load(src.get_type(), src, src_align);
1300 let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
1301 self.store_with_flags(val, ptr, dst_align, flags);
1304 let size = self.intcast(size, self.type_size_t(), false);
1305 let _is_volatile = flags.contains(MemFlags::VOLATILE);
1306 let dst = self.pointercast(dst, self.type_i8p());
1307 let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
1308 let memcpy = self.context.get_builtin_function("memcpy");
1309 let block = self.block.expect("block");
1310 // TODO: handle aligns and is_volatile.
1311 block.add_eval(None, self.context.new_call(None, memcpy, &[dst, src, size]));
1314 fn memmove(&mut self, dst: RValue<'gcc>, dst_align: Align, src: RValue<'gcc>, src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
1315 if flags.contains(MemFlags::NONTEMPORAL) {
1316 // HACK(nox): This is inefficient but there is no nontemporal memmove.
1317 let val = self.load(src.get_type(), src, src_align);
1318 let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
1319 self.store_with_flags(val, ptr, dst_align, flags);
1322 let size = self.intcast(size, self.type_size_t(), false);
1323 let _is_volatile = flags.contains(MemFlags::VOLATILE);
1324 let dst = self.pointercast(dst, self.type_i8p());
1325 let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
1327 let memmove = self.context.get_builtin_function("memmove");
1328 let block = self.block.expect("block");
1329 // TODO: handle is_volatile.
1330 block.add_eval(None, self.context.new_call(None, memmove, &[dst, src, size]));
1333 fn memset(&mut self, ptr: RValue<'gcc>, fill_byte: RValue<'gcc>, size: RValue<'gcc>, _align: Align, flags: MemFlags) {
1334 let _is_volatile = flags.contains(MemFlags::VOLATILE);
1335 let ptr = self.pointercast(ptr, self.type_i8p());
1336 let memset = self.context.get_builtin_function("memset");
1337 let block = self.block.expect("block");
1338 // TODO: handle aligns and is_volatile.
1339 //println!("memset: {:?} -> {:?}", fill_byte, self.i32_type);
1340 let fill_byte = self.context.new_cast(None, fill_byte, self.i32_type);
1341 let size = self.intcast(size, self.type_size_t(), false);
1342 block.add_eval(None, self.context.new_call(None, memset, &[ptr, fill_byte, size]));
1345 fn select(&mut self, cond: RValue<'gcc>, then_val: RValue<'gcc>, mut else_val: RValue<'gcc>) -> RValue<'gcc> {
1346 let func = self.current_func();
1347 let variable = func.new_local(None, then_val.get_type(), "selectVar");
1348 let then_block = func.new_block("then");
1349 let else_block = func.new_block("else");
1350 let after_block = func.new_block("after");
1351 self.llbb().end_with_conditional(None, cond, then_block, else_block);
1353 then_block.add_assignment(None, variable, then_val);
1354 then_block.end_with_jump(None, after_block);
1356 if then_val.get_type() != else_val.get_type() {
1357 else_val = self.context.new_cast(None, else_val, then_val.get_type());
1359 else_block.add_assignment(None, variable, else_val);
1360 else_block.end_with_jump(None, after_block);
1362 // NOTE: since jumps were added in a place rustc does not expect, the current blocks in the
1363 // state need to be updated.
1364 self.block = Some(after_block);
1365 *self.cx.current_block.borrow_mut() = Some(after_block);
1367 variable.to_rvalue()
1371 fn va_arg(&mut self, _list: RValue<'gcc>, _ty: Type<'gcc>) -> RValue<'gcc> {
1373 //unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
1376 fn extract_element(&mut self, _vec: RValue<'gcc>, _idx: RValue<'gcc>) -> RValue<'gcc> {
1378 //unsafe { llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED) }
1381 fn vector_splat(&mut self, _num_elts: usize, _elt: RValue<'gcc>) -> RValue<'gcc> {
1384 let elt_ty = self.cx.val_ty(elt);
1385 let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
1386 let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
1387 let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
1388 self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
1392 fn extract_value(&mut self, aggregate_value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1393 // FIXME: it would be better if the API only called this on struct, not on arrays.
1394 assert_eq!(idx as usize as u64, idx);
1395 let value_type = aggregate_value.get_type();
1397 if value_type.is_array().is_some() {
1398 let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1399 let element = self.context.new_array_access(None, aggregate_value, index);
1400 element.get_address(None)
1402 else if value_type.is_vector().is_some() {
1405 else if let Some(pointer_type) = value_type.get_pointee() {
1406 if let Some(struct_type) = pointer_type.is_struct() {
1407 // NOTE: hack to workaround a limitation of the rustc API: see comment on
1408 // CodegenCx.structs_as_pointer
1409 aggregate_value.dereference_field(None, struct_type.get_field(idx as i32)).to_rvalue()
1412 panic!("Unexpected type {:?}", value_type);
1415 else if let Some(struct_type) = value_type.is_struct() {
1416 aggregate_value.access_field(None, struct_type.get_field(idx as i32)).to_rvalue()
1419 panic!("Unexpected type {:?}", value_type);
1421 /*assert_eq!(idx as c_uint as u64, idx);
1422 unsafe { llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED) }*/
1425 fn insert_value(&mut self, aggregate_value: RValue<'gcc>, value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1426 // FIXME: it would be better if the API only called this on struct, not on arrays.
1427 assert_eq!(idx as usize as u64, idx);
1428 let value_type = aggregate_value.get_type();
1431 if value_type.is_array().is_some() {
1432 let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1433 self.context.new_array_access(None, aggregate_value, index)
1435 else if value_type.is_vector().is_some() {
1438 else if let Some(pointer_type) = value_type.get_pointee() {
1439 if let Some(struct_type) = pointer_type.is_struct() {
1440 // NOTE: hack to workaround a limitation of the rustc API: see comment on
1441 // CodegenCx.structs_as_pointer
1442 aggregate_value.dereference_field(None, struct_type.get_field(idx as i32))
1445 panic!("Unexpected type {:?}", value_type);
1449 panic!("Unexpected type {:?}", value_type);
1451 self.llbb().add_assignment(None, lvalue, value);
1456 fn landing_pad(&mut self, _ty: Type<'gcc>, _pers_fn: RValue<'gcc>, _num_clauses: usize) -> RValue<'gcc> {
1457 let field1 = self.context.new_field(None, self.u8_type, "landing_pad_field_1");
1458 let field2 = self.context.new_field(None, self.i32_type, "landing_pad_field_1");
1459 let struct_type = self.context.new_struct_type(None, "landing_pad", &[field1, field2]);
1460 self.current_func().new_local(None, struct_type.as_type(), "landing_pad")
1464 llvm::LLVMBuildLandingPad(self.llbuilder, ty, pers_fn, num_clauses as c_uint, UNNAMED)
1468 fn set_cleanup(&mut self, _landing_pad: RValue<'gcc>) {
1471 llvm::LLVMSetCleanup(landing_pad, llvm::True);
1475 fn resume(&mut self, _exn: RValue<'gcc>) -> RValue<'gcc> {
1477 //unsafe { llvm::LLVMBuildResume(self.llbuilder, exn) }
1480 fn cleanup_pad(&mut self, _parent: Option<RValue<'gcc>>, _args: &[RValue<'gcc>]) -> Funclet {
1482 /*let name = const_cstr!("cleanuppad");
1484 llvm::LLVMRustBuildCleanupPad(
1487 args.len() as c_uint,
1492 Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))*/
1495 fn cleanup_ret(&mut self, _funclet: &Funclet, _unwind: Option<Block<'gcc>>) -> RValue<'gcc> {
1498 unsafe { llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind) };
1499 ret.expect("LLVM does not have support for cleanupret")*/
1502 fn catch_pad(&mut self, _parent: RValue<'gcc>, _args: &[RValue<'gcc>]) -> Funclet {
1504 /*let name = const_cstr!("catchpad");
1506 llvm::LLVMRustBuildCatchPad(
1509 args.len() as c_uint,
1514 Funclet::new(ret.expect("LLVM does not have support for catchpad"))*/
1517 fn catch_switch(&mut self, _parent: Option<RValue<'gcc>>, _unwind: Option<Block<'gcc>>, _num_handlers: usize) -> RValue<'gcc> {
1519 /*let name = const_cstr!("catchswitch");
1521 llvm::LLVMRustBuildCatchSwitch(
1525 num_handlers as c_uint,
1529 ret.expect("LLVM does not have support for catchswitch")*/
1532 fn add_handler(&mut self, _catch_switch: RValue<'gcc>, _handler: Block<'gcc>) {
1535 llvm::LLVMRustAddHandler(catch_switch, handler);
1539 fn set_personality_fn(&mut self, _personality: RValue<'gcc>) {
1542 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
1546 // Atomic Operations
1547 fn atomic_cmpxchg(&mut self, dst: RValue<'gcc>, cmp: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
1548 let expected = self.current_func().new_local(None, cmp.get_type(), "expected");
1549 self.llbb().add_assignment(None, expected, cmp);
1550 let success = self.compare_exchange(dst, expected, src, order, failure_order, weak);
1552 let pair_type = self.cx.type_struct(&[src.get_type(), self.bool_type], false);
1553 let result = self.current_func().new_local(None, pair_type, "atomic_cmpxchg_result");
1554 let align = Align::from_bits(64).expect("align"); // TODO: use good align.
1556 let value_type = result.to_rvalue().get_type();
1557 if let Some(struct_type) = value_type.is_struct() {
1558 self.store(success, result.access_field(None, struct_type.get_field(1)).get_address(None), align);
1559 // NOTE: since success contains the call to the intrinsic, it must be stored before
1560 // expected so that we store expected after the call.
1561 self.store(expected.to_rvalue(), result.access_field(None, struct_type.get_field(0)).get_address(None), align);
1563 // TODO: handle when value is not a struct.
1568 fn atomic_rmw(&mut self, op: AtomicRmwBinOp, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
1569 let size = self.cx.int_width(src.get_type()) / 8;
1572 AtomicRmwBinOp::AtomicXchg => format!("__atomic_exchange_{}", size),
1573 AtomicRmwBinOp::AtomicAdd => format!("__atomic_fetch_add_{}", size),
1574 AtomicRmwBinOp::AtomicSub => format!("__atomic_fetch_sub_{}", size),
1575 AtomicRmwBinOp::AtomicAnd => format!("__atomic_fetch_and_{}", size),
1576 AtomicRmwBinOp::AtomicNand => format!("__atomic_fetch_nand_{}", size),
1577 AtomicRmwBinOp::AtomicOr => format!("__atomic_fetch_or_{}", size),
1578 AtomicRmwBinOp::AtomicXor => format!("__atomic_fetch_xor_{}", size),
1579 AtomicRmwBinOp::AtomicMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
1580 AtomicRmwBinOp::AtomicMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
1581 AtomicRmwBinOp::AtomicUMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
1582 AtomicRmwBinOp::AtomicUMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
1586 let atomic_function = self.context.get_builtin_function(name);
1587 let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1589 let void_ptr_type = self.context.new_type::<*mut ()>();
1590 let volatile_void_ptr_type = void_ptr_type.make_volatile();
1591 let dst = self.context.new_cast(None, dst, volatile_void_ptr_type);
1592 // NOTE: not sure why, but we have the wrong type here.
1593 let new_src_type = atomic_function.get_param(1).to_rvalue().get_type();
1594 let src = self.context.new_cast(None, src, new_src_type);
1595 let res = self.context.new_call(None, atomic_function, &[dst, src, order]);
1596 self.context.new_cast(None, res, src.get_type())
1599 fn atomic_fence(&mut self, order: AtomicOrdering, scope: SynchronizationScope) {
1602 SynchronizationScope::SingleThread => "__atomic_signal_fence",
1603 SynchronizationScope::CrossThread => "__atomic_thread_fence",
1605 let thread_fence = self.context.get_builtin_function(name);
1606 let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1607 self.llbb().add_eval(None, self.context.new_call(None, thread_fence, &[order]));
1610 fn set_invariant_load(&mut self, load: RValue<'gcc>) {
1611 // NOTE: Hack to consider vtable function pointer as non-global-variable function pointer.
1612 self.normal_function_addresses.borrow_mut().insert(load);
1615 llvm::LLVMSetMetadata(
1617 llvm::MD_invariant_load as c_uint,
1618 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1623 fn lifetime_start(&mut self, _ptr: RValue<'gcc>, _size: Size) {
1625 //self.call_lifetime_intrinsic("llvm.lifetime.start.p0i8", ptr, size);
1628 fn lifetime_end(&mut self, _ptr: RValue<'gcc>, _size: Size) {
1630 //self.call_lifetime_intrinsic("llvm.lifetime.end.p0i8", ptr, size);
1633 fn call(&mut self, _typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], funclet: Option<&Funclet>) -> RValue<'gcc> {
1634 // FIXME: remove when having a proper API.
1635 let gcc_func = unsafe { std::mem::transmute(func) };
1636 if self.functions.borrow().values().find(|value| **value == gcc_func).is_some() {
1637 self.function_call(func, args, funclet)
1640 // If it's a not function that was defined, it's a function pointer.
1641 self.function_ptr_call(func, args, funclet)
1645 fn zext(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
1646 // FIXME: this does not zero-extend.
1647 if value.get_type().is_bool() && dest_typ.is_i8(&self.cx) {
1648 // FIXME: hack because base::from_immediate converts i1 to i8.
1649 // Fix the code in codegen_ssa::base::from_immediate.
1652 //println!("zext: {:?} -> {:?}", value, dest_typ);
1653 self.context.new_cast(None, value, dest_typ)
1656 fn cx(&self) -> &CodegenCx<'gcc, 'tcx> {
1660 fn do_not_inline(&mut self, _llret: RValue<'gcc>) {
1662 //llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1665 fn set_span(&mut self, _span: Span) {}
1667 fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
1668 if self.cx().val_ty(val) == self.cx().type_i1() {
1669 self.zext(val, self.cx().type_i8())
1676 fn to_immediate_scalar(&mut self, val: Self::Value, scalar: &abi::Scalar) -> Self::Value {
1677 if scalar.is_bool() {
1678 return self.trunc(val, self.cx().type_i1());
1683 fn fptoui_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
1687 fn fptosi_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
1691 fn instrprof_increment(&mut self, _fn_name: RValue<'gcc>, _hash: RValue<'gcc>, _num_counters: RValue<'gcc>, _index: RValue<'gcc>) {
1694 "instrprof_increment() with args ({:?}, {:?}, {:?}, {:?})",
1695 fn_name, hash, num_counters, index
1698 let llfn = unsafe { llvm::LLVMRustGetInstrProfIncrementIntrinsic(self.cx().llmod) };
1699 let args = &[fn_name, hash, num_counters, index];
1700 let args = self.check_call("call", llfn, args);
1703 let _ = llvm::LLVMRustBuildCall(
1706 args.as_ptr() as *const &llvm::Value,
1707 args.len() as c_uint,
1714 impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
1715 pub fn shuffle_vector(&mut self, v1: RValue<'gcc>, v2: RValue<'gcc>, mask: RValue<'gcc>) -> RValue<'gcc> {
1716 let return_type = v1.get_type();
1718 self.context.new_parameter(None, return_type, "v1"),
1719 self.context.new_parameter(None, return_type, "v2"),
1720 self.context.new_parameter(None, mask.get_type(), "mask"),
1722 let shuffle = self.context.new_function(None, FunctionType::Extern, return_type, ¶ms, "_mm_shuffle_epi8", false);
1723 self.context.new_call(None, shuffle, &[v1, v2, mask])
1727 impl<'a, 'gcc, 'tcx> StaticBuilderMethods for Builder<'a, 'gcc, 'tcx> {
1728 fn get_static(&mut self, def_id: DefId) -> RValue<'gcc> {
1729 // Forward to the `get_static` method of `CodegenCx`
1730 self.cx().get_static(def_id)
1734 impl<'tcx> HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
1735 fn param_env(&self) -> ParamEnv<'tcx> {
1740 impl<'tcx> HasTargetSpec for Builder<'_, '_, 'tcx> {
1741 fn target_spec(&self) -> &Target {
1742 &self.cx.target_spec()
1747 fn to_gcc_comparison(&self) -> ComparisonOp;
1750 impl ToGccComp for IntPredicate {
1751 fn to_gcc_comparison(&self) -> ComparisonOp {
1753 IntPredicate::IntEQ => ComparisonOp::Equals,
1754 IntPredicate::IntNE => ComparisonOp::NotEquals,
1755 IntPredicate::IntUGT => ComparisonOp::GreaterThan,
1756 IntPredicate::IntUGE => ComparisonOp::GreaterThanEquals,
1757 IntPredicate::IntULT => ComparisonOp::LessThan,
1758 IntPredicate::IntULE => ComparisonOp::LessThanEquals,
1759 IntPredicate::IntSGT => ComparisonOp::GreaterThan,
1760 IntPredicate::IntSGE => ComparisonOp::GreaterThanEquals,
1761 IntPredicate::IntSLT => ComparisonOp::LessThan,
1762 IntPredicate::IntSLE => ComparisonOp::LessThanEquals,
1767 impl ToGccComp for RealPredicate {
1768 fn to_gcc_comparison(&self) -> ComparisonOp {
1769 // TODO: check that ordered vs non-ordered is respected.
1771 RealPredicate::RealPredicateFalse => unreachable!(),
1772 RealPredicate::RealOEQ => ComparisonOp::Equals,
1773 RealPredicate::RealOGT => ComparisonOp::GreaterThan,
1774 RealPredicate::RealOGE => ComparisonOp::GreaterThanEquals,
1775 RealPredicate::RealOLT => ComparisonOp::LessThan,
1776 RealPredicate::RealOLE => ComparisonOp::LessThanEquals,
1777 RealPredicate::RealONE => ComparisonOp::NotEquals,
1778 RealPredicate::RealORD => unreachable!(),
1779 RealPredicate::RealUNO => unreachable!(),
1780 RealPredicate::RealUEQ => ComparisonOp::Equals,
1781 RealPredicate::RealUGT => ComparisonOp::GreaterThan,
1782 RealPredicate::RealUGE => ComparisonOp::GreaterThan,
1783 RealPredicate::RealULT => ComparisonOp::LessThan,
1784 RealPredicate::RealULE => ComparisonOp::LessThan,
1785 RealPredicate::RealUNE => ComparisonOp::NotEquals,
1786 RealPredicate::RealPredicateTrue => unreachable!(),
1792 #[allow(non_camel_case_types)]
1802 trait ToGccOrdering {
1803 fn to_gcc(self) -> i32;
1806 impl ToGccOrdering for AtomicOrdering {
1807 fn to_gcc(self) -> i32 {
1812 AtomicOrdering::NotAtomic => __ATOMIC_RELAXED, // TODO: check if that's the same.
1813 AtomicOrdering::Unordered => __ATOMIC_RELAXED,
1814 AtomicOrdering::Monotonic => __ATOMIC_RELAXED, // TODO: check if that's the same.
1815 AtomicOrdering::Acquire => __ATOMIC_ACQUIRE,
1816 AtomicOrdering::Release => __ATOMIC_RELEASE,
1817 AtomicOrdering::AcquireRelease => __ATOMIC_ACQ_REL,
1818 AtomicOrdering::SequentiallyConsistent => __ATOMIC_SEQ_CST,