]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/builder.rs
6994eeb00c3e2e6de92639e6039d5e00e8d0c63a
[rust.git] / compiler / rustc_codegen_gcc / src / builder.rs
1 use std::borrow::Cow;
2 use std::cell::Cell;
3 use std::convert::TryFrom;
4 use std::ops::Deref;
5
6 use gccjit::{
7     BinaryOp,
8     Block,
9     ComparisonOp,
10     Context,
11     Function,
12     LValue,
13     RValue,
14     ToRValue,
15     Type,
16     UnaryOp,
17 };
18 use rustc_apfloat::{ieee, Float, Round, Status};
19 use rustc_codegen_ssa::MemFlags;
20 use rustc_codegen_ssa::common::{
21     AtomicOrdering, AtomicRmwBinOp, IntPredicate, RealPredicate, SynchronizationScope, TypeKind,
22 };
23 use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
24 use rustc_codegen_ssa::mir::place::PlaceRef;
25 use rustc_codegen_ssa::traits::{
26     BackendTypes,
27     BaseTypeMethods,
28     BuilderMethods,
29     ConstMethods,
30     DerivedTypeMethods,
31     LayoutTypeMethods,
32     HasCodegen,
33     OverflowOp,
34     StaticBuilderMethods,
35 };
36 use rustc_data_structures::fx::FxHashSet;
37 use rustc_middle::bug;
38 use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
39 use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers, TyAndLayout};
40 use rustc_span::Span;
41 use rustc_span::def_id::DefId;
42 use rustc_target::abi::{
43     self,
44     call::FnAbi,
45     Align,
46     HasDataLayout,
47     Size,
48     TargetDataLayout,
49     WrappingRange,
50 };
51 use rustc_target::spec::{HasTargetSpec, Target};
52
53 use crate::common::{SignType, TypeReflection, type_is_pointer};
54 use crate::context::CodegenCx;
55 use crate::intrinsic::llvm;
56 use crate::type_of::LayoutGccExt;
57
58 // TODO(antoyo)
59 type Funclet = ();
60
61 // TODO(antoyo): remove this variable.
62 static mut RETURN_VALUE_COUNT: usize = 0;
63
64 enum ExtremumOperation {
65     Max,
66     Min,
67 }
68
69 pub struct Builder<'a: 'gcc, 'gcc, 'tcx> {
70     pub cx: &'a CodegenCx<'gcc, 'tcx>,
71     pub block: Block<'gcc>,
72     stack_var_count: Cell<usize>,
73 }
74
75 impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
76     fn with_cx(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
77         Builder {
78             cx,
79             block,
80             stack_var_count: Cell::new(0),
81         }
82     }
83
84     fn atomic_extremum(&mut self, operation: ExtremumOperation, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
85         let size = src.get_type().get_size();
86
87         let func = self.current_func();
88
89         let load_ordering =
90             match order {
91                 // TODO(antoyo): does this make sense?
92                 AtomicOrdering::AcquireRelease | AtomicOrdering::Release => AtomicOrdering::Acquire,
93                 _ => order,
94             };
95         let previous_value = self.atomic_load(dst.get_type(), dst, load_ordering, Size::from_bytes(size));
96         let previous_var = func.new_local(None, previous_value.get_type(), "previous_value");
97         let return_value = func.new_local(None, previous_value.get_type(), "return_value");
98         self.llbb().add_assignment(None, previous_var, previous_value);
99         self.llbb().add_assignment(None, return_value, previous_var.to_rvalue());
100
101         let while_block = func.new_block("while");
102         let after_block = func.new_block("after_while");
103         self.llbb().end_with_jump(None, while_block);
104
105         // NOTE: since jumps were added and compare_exchange doesn't expect this, the current block in the
106         // state need to be updated.
107         self.switch_to_block(while_block);
108
109         let comparison_operator =
110             match operation {
111                 ExtremumOperation::Max => ComparisonOp::LessThan,
112                 ExtremumOperation::Min => ComparisonOp::GreaterThan,
113             };
114
115         let cond1 = self.context.new_comparison(None, comparison_operator, previous_var.to_rvalue(), self.context.new_cast(None, src, previous_value.get_type()));
116         let compare_exchange = self.compare_exchange(dst, previous_var, src, order, load_ordering, false);
117         let cond2 = self.cx.context.new_unary_op(None, UnaryOp::LogicalNegate, compare_exchange.get_type(), compare_exchange);
118         let cond = self.cx.context.new_binary_op(None, BinaryOp::LogicalAnd, self.cx.bool_type, cond1, cond2);
119
120         while_block.end_with_conditional(None, cond, while_block, after_block);
121
122         // NOTE: since jumps were added in a place rustc does not expect, the current block in the
123         // state need to be updated.
124         self.switch_to_block(after_block);
125
126         return_value.to_rvalue()
127     }
128
129     fn compare_exchange(&self, dst: RValue<'gcc>, cmp: LValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
130         let size = src.get_type().get_size();
131         let compare_exchange = self.context.get_builtin_function(&format!("__atomic_compare_exchange_{}", size));
132         let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
133         let failure_order = self.context.new_rvalue_from_int(self.i32_type, failure_order.to_gcc());
134         let weak = self.context.new_rvalue_from_int(self.bool_type, weak as i32);
135
136         let void_ptr_type = self.context.new_type::<*mut ()>();
137         let volatile_void_ptr_type = void_ptr_type.make_volatile();
138         let dst = self.context.new_cast(None, dst, volatile_void_ptr_type);
139         let expected = self.context.new_cast(None, cmp.get_address(None), void_ptr_type);
140
141         // NOTE: not sure why, but we have the wrong type here.
142         let int_type = compare_exchange.get_param(2).to_rvalue().get_type();
143         let src = self.context.new_cast(None, src, int_type);
144         self.context.new_call(None, compare_exchange, &[dst, expected, src, weak, order, failure_order])
145     }
146
147     pub fn assign(&self, lvalue: LValue<'gcc>, value: RValue<'gcc>) {
148         self.llbb().add_assignment(None, lvalue, value);
149     }
150
151     fn check_call<'b>(&mut self, _typ: &str, func: Function<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
152         let mut all_args_match = true;
153         let mut param_types = vec![];
154         let param_count = func.get_param_count();
155         for (index, arg) in args.iter().enumerate().take(param_count) {
156             let param = func.get_param(index as i32);
157             let param = param.to_rvalue().get_type();
158             if param != arg.get_type() {
159                 all_args_match = false;
160             }
161             param_types.push(param);
162         }
163
164         if all_args_match {
165             return Cow::Borrowed(args);
166         }
167
168         let casted_args: Vec<_> = param_types
169             .into_iter()
170             .zip(args.iter())
171             .enumerate()
172             .map(|(_i, (expected_ty, &actual_val))| {
173                 let actual_ty = actual_val.get_type();
174                 if expected_ty != actual_ty {
175                     self.bitcast(actual_val, expected_ty)
176                 }
177                 else {
178                     actual_val
179                 }
180             })
181             .collect();
182
183         Cow::Owned(casted_args)
184     }
185
186     fn check_ptr_call<'b>(&mut self, _typ: &str, func_ptr: RValue<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
187         let mut all_args_match = true;
188         let mut param_types = vec![];
189         let gcc_func = func_ptr.get_type().dyncast_function_ptr_type().expect("function ptr");
190         for (index, arg) in args.iter().enumerate().take(gcc_func.get_param_count()) {
191             let param = gcc_func.get_param_type(index);
192             if param != arg.get_type() {
193                 all_args_match = false;
194             }
195             param_types.push(param);
196         }
197
198         let mut on_stack_param_indices = FxHashSet::default();
199         if let Some(indices) = self.on_stack_params.borrow().get(&gcc_func) {
200             on_stack_param_indices = indices.clone();
201         }
202
203         if all_args_match {
204             return Cow::Borrowed(args);
205         }
206
207         let func_name = format!("{:?}", func_ptr);
208
209         let casted_args: Vec<_> = param_types
210             .into_iter()
211             .zip(args.iter())
212             .enumerate()
213             .map(|(index, (expected_ty, &actual_val))| {
214                 if llvm::ignore_arg_cast(&func_name, index, args.len()) {
215                     return actual_val;
216                 }
217
218                 let actual_ty = actual_val.get_type();
219                 if expected_ty != actual_ty {
220                     if !actual_ty.is_vector() && !expected_ty.is_vector() && actual_ty.is_integral() && expected_ty.is_integral() && actual_ty.get_size() != expected_ty.get_size() {
221                         self.context.new_cast(None, actual_val, expected_ty)
222                     }
223                     else if on_stack_param_indices.contains(&index) {
224                         actual_val.dereference(None).to_rvalue()
225                     }
226                     else {
227                         assert!(!((actual_ty.is_vector() && !expected_ty.is_vector()) || (!actual_ty.is_vector() && expected_ty.is_vector())), "{:?} ({}) -> {:?} ({}), index: {:?}[{}]", actual_ty, actual_ty.is_vector(), expected_ty, expected_ty.is_vector(), func_ptr, index);
228                         // TODO(antoyo): perhaps use __builtin_convertvector for vector casting.
229                         self.bitcast(actual_val, expected_ty)
230                     }
231                 }
232                 else {
233                     actual_val
234                 }
235             })
236             .collect();
237
238         Cow::Owned(casted_args)
239     }
240
241     fn check_store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
242         let dest_ptr_ty = self.cx.val_ty(ptr).make_pointer(); // TODO(antoyo): make sure make_pointer() is okay here.
243         let stored_ty = self.cx.val_ty(val);
244         let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
245
246         if dest_ptr_ty == stored_ptr_ty {
247             ptr
248         }
249         else {
250             self.bitcast(ptr, stored_ptr_ty)
251         }
252     }
253
254     pub fn current_func(&self) -> Function<'gcc> {
255         self.block.get_function()
256     }
257
258     fn function_call(&mut self, func: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
259         // TODO(antoyo): remove when the API supports a different type for functions.
260         let func: Function<'gcc> = self.cx.rvalue_as_function(func);
261         let args = self.check_call("call", func, args);
262
263         // gccjit requires to use the result of functions, even when it's not used.
264         // That's why we assign the result to a local or call add_eval().
265         let return_type = func.get_return_type();
266         let void_type = self.context.new_type::<()>();
267         let current_func = self.block.get_function();
268         if return_type != void_type {
269             unsafe { RETURN_VALUE_COUNT += 1 };
270             let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
271             self.block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
272             result.to_rvalue()
273         }
274         else {
275             self.block.add_eval(None, self.cx.context.new_call(None, func, &args));
276             // Return dummy value when not having return value.
277             self.context.new_rvalue_from_long(self.isize_type, 0)
278         }
279     }
280
281     fn function_ptr_call(&mut self, func_ptr: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
282         let args = self.check_ptr_call("call", func_ptr, args);
283
284         // gccjit requires to use the result of functions, even when it's not used.
285         // That's why we assign the result to a local or call add_eval().
286         let gcc_func = func_ptr.get_type().dyncast_function_ptr_type().expect("function ptr");
287         let return_type = gcc_func.get_return_type();
288         let void_type = self.context.new_type::<()>();
289         let current_func = self.block.get_function();
290
291         if return_type != void_type {
292             unsafe { RETURN_VALUE_COUNT += 1 };
293             let result = current_func.new_local(None, return_type, &format!("ptrReturnValue{}", unsafe { RETURN_VALUE_COUNT }));
294             let func_name = format!("{:?}", func_ptr);
295             let args = llvm::adjust_intrinsic_arguments(&self, gcc_func, args, &func_name);
296             self.block.add_assignment(None, result, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
297             result.to_rvalue()
298         }
299         else {
300             #[cfg(not(feature="master"))]
301             if gcc_func.get_param_count() == 0 {
302                 // FIXME(antoyo): As a temporary workaround for unsupported LLVM intrinsics.
303                 self.block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &[]));
304             }
305             else {
306                 self.block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
307             }
308             #[cfg(feature="master")]
309             self.block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
310             // Return dummy value when not having return value.
311             let result = current_func.new_local(None, self.isize_type, "dummyValueThatShouldNeverBeUsed");
312             self.block.add_assignment(None, result, self.context.new_rvalue_from_long(self.isize_type, 0));
313             result.to_rvalue()
314         }
315     }
316
317     pub fn overflow_call(&self, func: Function<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
318         // gccjit requires to use the result of functions, even when it's not used.
319         // That's why we assign the result to a local.
320         let return_type = self.context.new_type::<bool>();
321         let current_func = self.block.get_function();
322         // TODO(antoyo): return the new_call() directly? Since the overflow function has no side-effects.
323         unsafe { RETURN_VALUE_COUNT += 1 };
324         let result = current_func.new_local(None, return_type, &format!("overflowReturnValue{}", unsafe { RETURN_VALUE_COUNT }));
325         self.block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
326         result.to_rvalue()
327     }
328 }
329
330 impl<'gcc, 'tcx> HasCodegen<'tcx> for Builder<'_, 'gcc, 'tcx> {
331     type CodegenCx = CodegenCx<'gcc, 'tcx>;
332 }
333
334 impl<'tcx> HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
335     fn tcx(&self) -> TyCtxt<'tcx> {
336         self.cx.tcx()
337     }
338 }
339
340 impl HasDataLayout for Builder<'_, '_, '_> {
341     fn data_layout(&self) -> &TargetDataLayout {
342         self.cx.data_layout()
343     }
344 }
345
346 impl<'tcx> LayoutOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
347     type LayoutOfResult = TyAndLayout<'tcx>;
348
349     #[inline]
350     fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
351         self.cx.handle_layout_err(err, span, ty)
352     }
353 }
354
355 impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
356     type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
357
358     #[inline]
359     fn handle_fn_abi_err(
360         &self,
361         err: FnAbiError<'tcx>,
362         span: Span,
363         fn_abi_request: FnAbiRequest<'tcx>,
364     ) -> ! {
365         self.cx.handle_fn_abi_err(err, span, fn_abi_request)
366     }
367 }
368
369 impl<'gcc, 'tcx> Deref for Builder<'_, 'gcc, 'tcx> {
370     type Target = CodegenCx<'gcc, 'tcx>;
371
372     fn deref(&self) -> &Self::Target {
373         self.cx
374     }
375 }
376
377 impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
378     type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
379     type Function = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Function;
380     type BasicBlock = <CodegenCx<'gcc, 'tcx> as BackendTypes>::BasicBlock;
381     type Type = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Type;
382     type Funclet = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Funclet;
383
384     type DIScope = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIScope;
385     type DILocation = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DILocation;
386     type DIVariable = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIVariable;
387 }
388
389 impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
390     fn build(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
391         Builder::with_cx(cx, block)
392     }
393
394     fn llbb(&self) -> Block<'gcc> {
395         self.block
396     }
397
398     fn append_block(cx: &'a CodegenCx<'gcc, 'tcx>, func: RValue<'gcc>, name: &str) -> Block<'gcc> {
399         let func = cx.rvalue_as_function(func);
400         func.new_block(name)
401     }
402
403     fn append_sibling_block(&mut self, name: &str) -> Block<'gcc> {
404         let func = self.current_func();
405         func.new_block(name)
406     }
407
408     fn switch_to_block(&mut self, block: Self::BasicBlock) {
409         self.block = block;
410     }
411
412     fn ret_void(&mut self) {
413         self.llbb().end_with_void_return(None)
414     }
415
416     fn ret(&mut self, value: RValue<'gcc>) {
417         let value =
418             if self.structs_as_pointer.borrow().contains(&value) {
419                 // NOTE: hack to workaround a limitation of the rustc API: see comment on
420                 // CodegenCx.structs_as_pointer
421                 value.dereference(None).to_rvalue()
422             }
423             else {
424                 value
425             };
426         self.llbb().end_with_return(None, value);
427     }
428
429     fn br(&mut self, dest: Block<'gcc>) {
430         self.llbb().end_with_jump(None, dest)
431     }
432
433     fn cond_br(&mut self, cond: RValue<'gcc>, then_block: Block<'gcc>, else_block: Block<'gcc>) {
434         self.llbb().end_with_conditional(None, cond, then_block, else_block)
435     }
436
437     fn switch(&mut self, value: RValue<'gcc>, default_block: Block<'gcc>, cases: impl ExactSizeIterator<Item = (u128, Block<'gcc>)>) {
438         let mut gcc_cases = vec![];
439         let typ = self.val_ty(value);
440         for (on_val, dest) in cases {
441             let on_val = self.const_uint_big(typ, on_val);
442             gcc_cases.push(self.context.new_case(on_val, on_val, dest));
443         }
444         self.block.end_with_switch(None, value, default_block, &gcc_cases);
445     }
446
447     fn invoke(&mut self, typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
448         // TODO(bjorn3): Properly implement unwinding.
449         let call_site = self.call(typ, func, args, None);
450         let condition = self.context.new_rvalue_from_int(self.bool_type, 1);
451         self.llbb().end_with_conditional(None, condition, then, catch);
452         call_site
453     }
454
455     fn unreachable(&mut self) {
456         let func = self.context.get_builtin_function("__builtin_unreachable");
457         self.block.add_eval(None, self.context.new_call(None, func, &[]));
458         let return_type = self.block.get_function().get_return_type();
459         let void_type = self.context.new_type::<()>();
460         if return_type == void_type {
461             self.block.end_with_void_return(None)
462         }
463         else {
464             let return_value = self.current_func()
465                 .new_local(None, return_type, "unreachableReturn");
466             self.block.end_with_return(None, return_value)
467         }
468     }
469
470     fn add(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
471         self.gcc_add(a, b)
472     }
473
474     fn fadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
475         a + b
476     }
477
478     fn sub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
479         self.gcc_sub(a, b)
480     }
481
482     fn fsub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
483         a - b
484     }
485
486     fn mul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
487         self.gcc_mul(a, b)
488     }
489
490     fn fmul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
491         a * b
492     }
493
494     fn udiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
495         self.gcc_udiv(a, b)
496     }
497
498     fn exactudiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
499         // TODO(antoyo): poison if not exact.
500         let a_type = a.get_type().to_unsigned(self);
501         let a = self.gcc_int_cast(a, a_type);
502         let b_type = b.get_type().to_unsigned(self);
503         let b = self.gcc_int_cast(b, b_type);
504         a / b
505     }
506
507     fn sdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
508         self.gcc_sdiv(a, b)
509     }
510
511     fn exactsdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
512         // TODO(antoyo): poison if not exact.
513         // FIXME(antoyo): rustc_codegen_ssa::mir::intrinsic uses different types for a and b but they
514         // should be the same.
515         let typ = a.get_type().to_signed(self);
516         let b = self.context.new_cast(None, b, typ);
517         a / b
518     }
519
520     fn fdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
521         a / b
522     }
523
524     fn urem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
525         self.gcc_urem(a, b)
526     }
527
528     fn srem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
529         self.gcc_srem(a, b)
530     }
531
532     fn frem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
533         if a.get_type().is_compatible_with(self.cx.float_type) {
534             let fmodf = self.context.get_builtin_function("fmodf");
535             // FIXME(antoyo): this seems to produce the wrong result.
536             return self.context.new_call(None, fmodf, &[a, b]);
537         }
538         assert_eq!(a.get_type().unqualified(), self.cx.double_type);
539
540         let fmod = self.context.get_builtin_function("fmod");
541         return self.context.new_call(None, fmod, &[a, b]);
542     }
543
544     fn shl(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
545         self.gcc_shl(a, b)
546     }
547
548     fn lshr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
549         self.gcc_lshr(a, b)
550     }
551
552     fn ashr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
553         // TODO(antoyo): check whether behavior is an arithmetic shift for >> .
554         // It seems to be if the value is signed.
555         self.gcc_lshr(a, b)
556     }
557
558     fn and(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
559         self.gcc_and(a, b)
560     }
561
562     fn or(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
563         self.cx.gcc_or(a, b)
564     }
565
566     fn xor(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
567         self.gcc_xor(a, b)
568     }
569
570     fn neg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
571         self.gcc_neg(a)
572     }
573
574     fn fneg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
575         self.cx.context.new_unary_op(None, UnaryOp::Minus, a.get_type(), a)
576     }
577
578     fn not(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
579         self.gcc_not(a)
580     }
581
582     fn unchecked_sadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
583         a + b
584     }
585
586     fn unchecked_uadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
587         self.gcc_add(a, b)
588     }
589
590     fn unchecked_ssub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
591         a - b
592     }
593
594     fn unchecked_usub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
595         // TODO(antoyo): should generate poison value?
596         self.gcc_sub(a, b)
597     }
598
599     fn unchecked_smul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
600         a * b
601     }
602
603     fn unchecked_umul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
604         a * b
605     }
606
607     fn fadd_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
608         unimplemented!();
609     }
610
611     fn fsub_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
612         unimplemented!();
613     }
614
615     fn fmul_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
616         unimplemented!();
617     }
618
619     fn fdiv_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
620         unimplemented!();
621     }
622
623     fn frem_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
624         unimplemented!();
625     }
626
627     fn checked_binop(&mut self, oop: OverflowOp, typ: Ty<'_>, lhs: Self::Value, rhs: Self::Value) -> (Self::Value, Self::Value) {
628         self.gcc_checked_binop(oop, typ, lhs, rhs)
629     }
630
631     fn alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
632         // FIXME(antoyo): this check that we don't call get_aligned() a second time on a type.
633         // Ideally, we shouldn't need to do this check.
634         let aligned_type =
635             if ty == self.cx.u128_type || ty == self.cx.i128_type {
636                 ty
637             }
638             else {
639                 ty.get_aligned(align.bytes())
640             };
641         // TODO(antoyo): It might be better to return a LValue, but fixing the rustc API is non-trivial.
642         self.stack_var_count.set(self.stack_var_count.get() + 1);
643         self.current_func().new_local(None, aligned_type, &format!("stack_var_{}", self.stack_var_count.get())).get_address(None)
644     }
645
646     fn dynamic_alloca(&mut self, _ty: Type<'gcc>, _align: Align) -> RValue<'gcc> {
647         unimplemented!();
648     }
649
650     fn array_alloca(&mut self, _ty: Type<'gcc>, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
651         unimplemented!();
652     }
653
654     fn load(&mut self, pointee_ty: Type<'gcc>, ptr: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
655         let block = self.llbb();
656         let function = block.get_function();
657         // NOTE: instead of returning the dereference here, we have to assign it to a variable in
658         // the current basic block. Otherwise, it could be used in another basic block, causing a
659         // dereference after a drop, for instance.
660         // TODO(antoyo): handle align of the load instruction.
661         let ptr = self.context.new_cast(None, ptr, pointee_ty.make_pointer());
662         let deref = ptr.dereference(None).to_rvalue();
663         unsafe { RETURN_VALUE_COUNT += 1 };
664         let loaded_value = function.new_local(None, pointee_ty, &format!("loadedValue{}", unsafe { RETURN_VALUE_COUNT }));
665         block.add_assignment(None, loaded_value, deref);
666         loaded_value.to_rvalue()
667     }
668
669     fn volatile_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
670         // TODO(antoyo): use ty.
671         let ptr = self.context.new_cast(None, ptr, ptr.get_type().make_volatile());
672         ptr.dereference(None).to_rvalue()
673     }
674
675     fn atomic_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) -> RValue<'gcc> {
676         // TODO(antoyo): use ty.
677         // TODO(antoyo): handle alignment.
678         let atomic_load = self.context.get_builtin_function(&format!("__atomic_load_{}", size.bytes()));
679         let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
680
681         let volatile_const_void_ptr_type = self.context.new_type::<()>()
682             .make_const()
683             .make_volatile()
684             .make_pointer();
685         let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
686         self.context.new_call(None, atomic_load, &[ptr, ordering])
687     }
688
689     fn load_operand(&mut self, place: PlaceRef<'tcx, RValue<'gcc>>) -> OperandRef<'tcx, RValue<'gcc>> {
690         assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
691
692         if place.layout.is_zst() {
693             return OperandRef::new_zst(self, place.layout);
694         }
695
696         fn scalar_load_metadata<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, load: RValue<'gcc>, scalar: &abi::Scalar) {
697             let vr = scalar.valid_range(bx);
698             match scalar.primitive() {
699                 abi::Int(..) => {
700                     if !scalar.is_always_valid(bx) {
701                         bx.range_metadata(load, vr);
702                     }
703                 }
704                 abi::Pointer if vr.start < vr.end && !vr.contains(0) => {
705                     bx.nonnull_metadata(load);
706                 }
707                 _ => {}
708             }
709         }
710
711         let val =
712             if let Some(llextra) = place.llextra {
713                 OperandValue::Ref(place.llval, Some(llextra), place.align)
714             }
715             else if place.layout.is_gcc_immediate() {
716                 let load = self.load(
717                     place.layout.gcc_type(self, false),
718                     place.llval,
719                     place.align,
720                 );
721                 if let abi::Abi::Scalar(ref scalar) = place.layout.abi {
722                     scalar_load_metadata(self, load, scalar);
723                 }
724                 OperandValue::Immediate(self.to_immediate(load, place.layout))
725             }
726             else if let abi::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
727                 let b_offset = a.size(self).align_to(b.align(self).abi);
728                 let pair_type = place.layout.gcc_type(self, false);
729
730                 let mut load = |i, scalar: &abi::Scalar, align| {
731                     let llptr = self.struct_gep(pair_type, place.llval, i as u64);
732                     let llty = place.layout.scalar_pair_element_gcc_type(self, i, false);
733                     let load = self.load(llty, llptr, align);
734                     scalar_load_metadata(self, load, scalar);
735                     if scalar.is_bool() { self.trunc(load, self.type_i1()) } else { load }
736                 };
737
738                 OperandValue::Pair(
739                     load(0, a, place.align),
740                     load(1, b, place.align.restrict_for_offset(b_offset)),
741                 )
742             }
743             else {
744                 OperandValue::Ref(place.llval, None, place.align)
745             };
746
747         OperandRef { val, layout: place.layout }
748     }
749
750     fn write_operand_repeatedly(mut self, cg_elem: OperandRef<'tcx, RValue<'gcc>>, count: u64, dest: PlaceRef<'tcx, RValue<'gcc>>) -> Self {
751         let zero = self.const_usize(0);
752         let count = self.const_usize(count);
753         let start = dest.project_index(&mut self, zero).llval;
754         let end = dest.project_index(&mut self, count).llval;
755
756         let header_bb = self.append_sibling_block("repeat_loop_header");
757         let body_bb = self.append_sibling_block("repeat_loop_body");
758         let next_bb = self.append_sibling_block("repeat_loop_next");
759
760         let ptr_type = start.get_type();
761         let current = self.llbb().get_function().new_local(None, ptr_type, "loop_var");
762         let current_val = current.to_rvalue();
763         self.assign(current, start);
764
765         self.br(header_bb);
766
767         self.switch_to_block(header_bb);
768         let keep_going = self.icmp(IntPredicate::IntNE, current_val, end);
769         self.cond_br(keep_going, body_bb, next_bb);
770
771         self.switch_to_block(body_bb);
772         let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
773         cg_elem.val.store(&mut self, PlaceRef::new_sized_aligned(current_val, cg_elem.layout, align));
774
775         let next = self.inbounds_gep(self.backend_type(cg_elem.layout), current.to_rvalue(), &[self.const_usize(1)]);
776         self.llbb().add_assignment(None, current, next);
777         self.br(header_bb);
778
779         self.switch_to_block(next_bb);
780         self
781     }
782
783     fn range_metadata(&mut self, _load: RValue<'gcc>, _range: WrappingRange) {
784         // TODO(antoyo)
785     }
786
787     fn nonnull_metadata(&mut self, _load: RValue<'gcc>) {
788         // TODO(antoyo)
789     }
790
791     fn store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
792         self.store_with_flags(val, ptr, align, MemFlags::empty())
793     }
794
795     fn store_with_flags(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align, _flags: MemFlags) -> RValue<'gcc> {
796         let ptr = self.check_store(val, ptr);
797         let destination = ptr.dereference(None);
798         // NOTE: libgccjit does not support specifying the alignment on the assignment, so we cast
799         // to type so it gets the proper alignment.
800         let destination_type = destination.to_rvalue().get_type().unqualified();
801         let aligned_type = destination_type.get_aligned(align.bytes()).make_pointer();
802         let aligned_destination = self.cx.context.new_bitcast(None, ptr, aligned_type);
803         let aligned_destination = aligned_destination.dereference(None);
804         self.llbb().add_assignment(None, aligned_destination, val);
805         // TODO(antoyo): handle align and flags.
806         // NOTE: dummy value here since it's never used. FIXME(antoyo): API should not return a value here?
807         self.cx.context.new_rvalue_zero(self.type_i32())
808     }
809
810     fn atomic_store(&mut self, value: RValue<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) {
811         // TODO(antoyo): handle alignment.
812         let atomic_store = self.context.get_builtin_function(&format!("__atomic_store_{}", size.bytes()));
813         let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
814         let volatile_const_void_ptr_type = self.context.new_type::<()>()
815             .make_volatile()
816             .make_pointer();
817         let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
818
819         // FIXME(antoyo): fix libgccjit to allow comparing an integer type with an aligned integer type because
820         // the following cast is required to avoid this error:
821         // 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))))
822         let int_type = atomic_store.get_param(1).to_rvalue().get_type();
823         let value = self.context.new_cast(None, value, int_type);
824         self.llbb()
825             .add_eval(None, self.context.new_call(None, atomic_store, &[ptr, value, ordering]));
826     }
827
828     fn gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
829         let mut result = ptr;
830         for index in indices {
831             result = self.context.new_array_access(None, result, *index).get_address(None).to_rvalue();
832         }
833         result
834     }
835
836     fn inbounds_gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
837         // FIXME(antoyo): would be safer if doing the same thing (loop) as gep.
838         // TODO(antoyo): specify inbounds somehow.
839         match indices.len() {
840             1 => {
841                 self.context.new_array_access(None, ptr, indices[0]).get_address(None)
842             },
843             2 => {
844                 let array = ptr.dereference(None); // TODO(antoyo): assert that first index is 0?
845                 self.context.new_array_access(None, array, indices[1]).get_address(None)
846             },
847             _ => unimplemented!(),
848         }
849     }
850
851     fn struct_gep(&mut self, value_type: Type<'gcc>, ptr: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
852         // FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
853         assert_eq!(idx as usize as u64, idx);
854         let value = ptr.dereference(None).to_rvalue();
855
856         if value_type.dyncast_array().is_some() {
857             let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
858             let element = self.context.new_array_access(None, value, index);
859             element.get_address(None)
860         }
861         else if let Some(vector_type) = value_type.dyncast_vector() {
862             let array_type = vector_type.get_element_type().make_pointer();
863             let array = self.bitcast(ptr, array_type);
864             let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
865             let element = self.context.new_array_access(None, array, index);
866             element.get_address(None)
867         }
868         else if let Some(struct_type) = value_type.is_struct() {
869             ptr.dereference_field(None, struct_type.get_field(idx as i32)).get_address(None)
870         }
871         else {
872             panic!("Unexpected type {:?}", value_type);
873         }
874     }
875
876     /* Casts */
877     fn trunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
878         // TODO(antoyo): check that it indeed truncate the value.
879         self.gcc_int_cast(value, dest_ty)
880     }
881
882     fn sext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
883         // TODO(antoyo): check that it indeed sign extend the value.
884         if dest_ty.dyncast_vector().is_some() {
885             // TODO(antoyo): nothing to do as it is only for LLVM?
886             return value;
887         }
888         self.context.new_cast(None, value, dest_ty)
889     }
890
891     fn fptoui(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
892         self.gcc_float_to_uint_cast(value, dest_ty)
893     }
894
895     fn fptosi(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
896         self.gcc_float_to_int_cast(value, dest_ty)
897     }
898
899     fn uitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
900         self.gcc_uint_to_float_cast(value, dest_ty)
901     }
902
903     fn sitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
904         self.gcc_int_to_float_cast(value, dest_ty)
905     }
906
907     fn fptrunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
908         // TODO(antoyo): make sure it truncates.
909         self.context.new_cast(None, value, dest_ty)
910     }
911
912     fn fpext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
913         self.context.new_cast(None, value, dest_ty)
914     }
915
916     fn ptrtoint(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
917         let usize_value = self.cx.const_bitcast(value, self.cx.type_isize());
918         self.intcast(usize_value, dest_ty, false)
919     }
920
921     fn inttoptr(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
922         let usize_value = self.intcast(value, self.cx.type_isize(), false);
923         self.cx.const_bitcast(usize_value, dest_ty)
924     }
925
926     fn bitcast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
927         self.cx.const_bitcast(value, dest_ty)
928     }
929
930     fn intcast(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>, _is_signed: bool) -> RValue<'gcc> {
931         // NOTE: is_signed is for value, not dest_typ.
932         self.gcc_int_cast(value, dest_typ)
933     }
934
935     fn pointercast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
936         let val_type = value.get_type();
937         match (type_is_pointer(val_type), type_is_pointer(dest_ty)) {
938             (false, true) => {
939                 // NOTE: Projecting a field of a pointer type will attempt a cast from a signed char to
940                 // a pointer, which is not supported by gccjit.
941                 return self.cx.context.new_cast(None, self.inttoptr(value, val_type.make_pointer()), dest_ty);
942             },
943             (false, false) => {
944                 // When they are not pointers, we want a transmute (or reinterpret_cast).
945                 self.bitcast(value, dest_ty)
946             },
947             (true, true) => self.cx.context.new_cast(None, value, dest_ty),
948             (true, false) => unimplemented!(),
949         }
950     }
951
952     /* Comparisons */
953     fn icmp(&mut self, op: IntPredicate, lhs: RValue<'gcc>, rhs: RValue<'gcc>) -> RValue<'gcc> {
954         self.gcc_icmp(op, lhs, rhs)
955     }
956
957     fn fcmp(&mut self, op: RealPredicate, lhs: RValue<'gcc>, rhs: RValue<'gcc>) -> RValue<'gcc> {
958         self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
959     }
960
961     /* Miscellaneous instructions */
962     fn memcpy(&mut self, dst: RValue<'gcc>, _dst_align: Align, src: RValue<'gcc>, _src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
963         assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
964         let size = self.intcast(size, self.type_size_t(), false);
965         let _is_volatile = flags.contains(MemFlags::VOLATILE);
966         let dst = self.pointercast(dst, self.type_i8p());
967         let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
968         let memcpy = self.context.get_builtin_function("memcpy");
969         // TODO(antoyo): handle aligns and is_volatile.
970         self.block.add_eval(None, self.context.new_call(None, memcpy, &[dst, src, size]));
971     }
972
973     fn memmove(&mut self, dst: RValue<'gcc>, dst_align: Align, src: RValue<'gcc>, src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
974         if flags.contains(MemFlags::NONTEMPORAL) {
975             // HACK(nox): This is inefficient but there is no nontemporal memmove.
976             let val = self.load(src.get_type().get_pointee().expect("get_pointee"), src, src_align);
977             let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
978             self.store_with_flags(val, ptr, dst_align, flags);
979             return;
980         }
981         let size = self.intcast(size, self.type_size_t(), false);
982         let _is_volatile = flags.contains(MemFlags::VOLATILE);
983         let dst = self.pointercast(dst, self.type_i8p());
984         let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
985
986         let memmove = self.context.get_builtin_function("memmove");
987         // TODO(antoyo): handle is_volatile.
988         self.block.add_eval(None, self.context.new_call(None, memmove, &[dst, src, size]));
989     }
990
991     fn memset(&mut self, ptr: RValue<'gcc>, fill_byte: RValue<'gcc>, size: RValue<'gcc>, _align: Align, flags: MemFlags) {
992         let _is_volatile = flags.contains(MemFlags::VOLATILE);
993         let ptr = self.pointercast(ptr, self.type_i8p());
994         let memset = self.context.get_builtin_function("memset");
995         // TODO(antoyo): handle align and is_volatile.
996         let fill_byte = self.context.new_cast(None, fill_byte, self.i32_type);
997         let size = self.intcast(size, self.type_size_t(), false);
998         self.block.add_eval(None, self.context.new_call(None, memset, &[ptr, fill_byte, size]));
999     }
1000
1001     fn select(&mut self, cond: RValue<'gcc>, then_val: RValue<'gcc>, mut else_val: RValue<'gcc>) -> RValue<'gcc> {
1002         let func = self.current_func();
1003         let variable = func.new_local(None, then_val.get_type(), "selectVar");
1004         let then_block = func.new_block("then");
1005         let else_block = func.new_block("else");
1006         let after_block = func.new_block("after");
1007         self.llbb().end_with_conditional(None, cond, then_block, else_block);
1008
1009         then_block.add_assignment(None, variable, then_val);
1010         then_block.end_with_jump(None, after_block);
1011
1012         if !then_val.get_type().is_compatible_with(else_val.get_type()) {
1013             else_val = self.context.new_cast(None, else_val, then_val.get_type());
1014         }
1015         else_block.add_assignment(None, variable, else_val);
1016         else_block.end_with_jump(None, after_block);
1017
1018         // NOTE: since jumps were added in a place rustc does not expect, the current block in the
1019         // state need to be updated.
1020         self.switch_to_block(after_block);
1021
1022         variable.to_rvalue()
1023     }
1024
1025     #[allow(dead_code)]
1026     fn va_arg(&mut self, _list: RValue<'gcc>, _ty: Type<'gcc>) -> RValue<'gcc> {
1027         unimplemented!();
1028     }
1029
1030     fn extract_element(&mut self, _vec: RValue<'gcc>, _idx: RValue<'gcc>) -> RValue<'gcc> {
1031         unimplemented!();
1032     }
1033
1034     fn vector_splat(&mut self, _num_elts: usize, _elt: RValue<'gcc>) -> RValue<'gcc> {
1035         unimplemented!();
1036     }
1037
1038     fn extract_value(&mut self, aggregate_value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1039         // FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
1040         assert_eq!(idx as usize as u64, idx);
1041         let value_type = aggregate_value.get_type();
1042
1043         if value_type.dyncast_array().is_some() {
1044             let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1045             let element = self.context.new_array_access(None, aggregate_value, index);
1046             element.get_address(None)
1047         }
1048         else if value_type.dyncast_vector().is_some() {
1049             panic!();
1050         }
1051         else if let Some(pointer_type) = value_type.get_pointee() {
1052             if let Some(struct_type) = pointer_type.is_struct() {
1053                 // NOTE: hack to workaround a limitation of the rustc API: see comment on
1054                 // CodegenCx.structs_as_pointer
1055                 aggregate_value.dereference_field(None, struct_type.get_field(idx as i32)).to_rvalue()
1056             }
1057             else {
1058                 panic!("Unexpected type {:?}", value_type);
1059             }
1060         }
1061         else if let Some(struct_type) = value_type.is_struct() {
1062             aggregate_value.access_field(None, struct_type.get_field(idx as i32)).to_rvalue()
1063         }
1064         else {
1065             panic!("Unexpected type {:?}", value_type);
1066         }
1067     }
1068
1069     fn insert_value(&mut self, aggregate_value: RValue<'gcc>, value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1070         // FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
1071         assert_eq!(idx as usize as u64, idx);
1072         let value_type = aggregate_value.get_type();
1073
1074         let lvalue =
1075             if value_type.dyncast_array().is_some() {
1076                 let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1077                 self.context.new_array_access(None, aggregate_value, index)
1078             }
1079             else if value_type.dyncast_vector().is_some() {
1080                 panic!();
1081             }
1082             else if let Some(pointer_type) = value_type.get_pointee() {
1083                 if let Some(struct_type) = pointer_type.is_struct() {
1084                     // NOTE: hack to workaround a limitation of the rustc API: see comment on
1085                     // CodegenCx.structs_as_pointer
1086                     aggregate_value.dereference_field(None, struct_type.get_field(idx as i32))
1087                 }
1088                 else {
1089                     panic!("Unexpected type {:?}", value_type);
1090                 }
1091             }
1092             else {
1093                 panic!("Unexpected type {:?}", value_type);
1094             };
1095
1096         let lvalue_type = lvalue.to_rvalue().get_type();
1097         let value =
1098             // NOTE: sometimes, rustc will create a value with the wrong type.
1099             if lvalue_type != value.get_type() {
1100                 self.context.new_cast(None, value, lvalue_type)
1101             }
1102             else {
1103                 value
1104             };
1105
1106         self.llbb().add_assignment(None, lvalue, value);
1107
1108         aggregate_value
1109     }
1110
1111     fn set_personality_fn(&mut self, _personality: RValue<'gcc>) {
1112         // TODO(antoyo)
1113     }
1114
1115     fn cleanup_landing_pad(&mut self, _ty: Type<'gcc>, _pers_fn: RValue<'gcc>) -> RValue<'gcc> {
1116         let field1 = self.context.new_field(None, self.u8_type.make_pointer(), "landing_pad_field_1");
1117         let field2 = self.context.new_field(None, self.i32_type, "landing_pad_field_1");
1118         let struct_type = self.context.new_struct_type(None, "landing_pad", &[field1, field2]);
1119         self.current_func().new_local(None, struct_type.as_type(), "landing_pad")
1120             .to_rvalue()
1121         // TODO(antoyo): Properly implement unwinding.
1122         // the above is just to make the compilation work as it seems
1123         // rustc_codegen_ssa now calls the unwinding builder methods even on panic=abort.
1124     }
1125
1126     fn resume(&mut self, _exn: RValue<'gcc>) {
1127         // TODO(bjorn3): Properly implement unwinding.
1128         self.unreachable();
1129     }
1130
1131     fn cleanup_pad(&mut self, _parent: Option<RValue<'gcc>>, _args: &[RValue<'gcc>]) -> Funclet {
1132         unimplemented!();
1133     }
1134
1135     fn cleanup_ret(&mut self, _funclet: &Funclet, _unwind: Option<Block<'gcc>>) {
1136         unimplemented!();
1137     }
1138
1139     fn catch_pad(&mut self, _parent: RValue<'gcc>, _args: &[RValue<'gcc>]) -> Funclet {
1140         unimplemented!();
1141     }
1142
1143     fn catch_switch(
1144         &mut self,
1145         _parent: Option<RValue<'gcc>>,
1146         _unwind: Option<Block<'gcc>>,
1147         _handlers: &[Block<'gcc>],
1148     ) -> RValue<'gcc> {
1149         unimplemented!();
1150     }
1151
1152     // Atomic Operations
1153     fn atomic_cmpxchg(&mut self, dst: RValue<'gcc>, cmp: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
1154         let expected = self.current_func().new_local(None, cmp.get_type(), "expected");
1155         self.llbb().add_assignment(None, expected, cmp);
1156         let success = self.compare_exchange(dst, expected, src, order, failure_order, weak);
1157
1158         let pair_type = self.cx.type_struct(&[src.get_type(), self.bool_type], false);
1159         let result = self.current_func().new_local(None, pair_type, "atomic_cmpxchg_result");
1160         let align = Align::from_bits(64).expect("align"); // TODO(antoyo): use good align.
1161
1162         let value_type = result.to_rvalue().get_type();
1163         if let Some(struct_type) = value_type.is_struct() {
1164             self.store(success, result.access_field(None, struct_type.get_field(1)).get_address(None), align);
1165             // NOTE: since success contains the call to the intrinsic, it must be stored before
1166             // expected so that we store expected after the call.
1167             self.store(expected.to_rvalue(), result.access_field(None, struct_type.get_field(0)).get_address(None), align);
1168         }
1169         // TODO(antoyo): handle when value is not a struct.
1170
1171         result.to_rvalue()
1172     }
1173
1174     fn atomic_rmw(&mut self, op: AtomicRmwBinOp, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
1175         let size = src.get_type().get_size();
1176         let name =
1177             match op {
1178                 AtomicRmwBinOp::AtomicXchg => format!("__atomic_exchange_{}", size),
1179                 AtomicRmwBinOp::AtomicAdd => format!("__atomic_fetch_add_{}", size),
1180                 AtomicRmwBinOp::AtomicSub => format!("__atomic_fetch_sub_{}", size),
1181                 AtomicRmwBinOp::AtomicAnd => format!("__atomic_fetch_and_{}", size),
1182                 AtomicRmwBinOp::AtomicNand => format!("__atomic_fetch_nand_{}", size),
1183                 AtomicRmwBinOp::AtomicOr => format!("__atomic_fetch_or_{}", size),
1184                 AtomicRmwBinOp::AtomicXor => format!("__atomic_fetch_xor_{}", size),
1185                 AtomicRmwBinOp::AtomicMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
1186                 AtomicRmwBinOp::AtomicMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
1187                 AtomicRmwBinOp::AtomicUMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
1188                 AtomicRmwBinOp::AtomicUMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
1189             };
1190
1191
1192         let atomic_function = self.context.get_builtin_function(name);
1193         let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1194
1195         let void_ptr_type = self.context.new_type::<*mut ()>();
1196         let volatile_void_ptr_type = void_ptr_type.make_volatile();
1197         let dst = self.context.new_cast(None, dst, volatile_void_ptr_type);
1198         // FIXME(antoyo): not sure why, but we have the wrong type here.
1199         let new_src_type = atomic_function.get_param(1).to_rvalue().get_type();
1200         let src = self.context.new_cast(None, src, new_src_type);
1201         let res = self.context.new_call(None, atomic_function, &[dst, src, order]);
1202         self.context.new_cast(None, res, src.get_type())
1203     }
1204
1205     fn atomic_fence(&mut self, order: AtomicOrdering, scope: SynchronizationScope) {
1206         let name =
1207             match scope {
1208                 SynchronizationScope::SingleThread => "__atomic_signal_fence",
1209                 SynchronizationScope::CrossThread => "__atomic_thread_fence",
1210             };
1211         let thread_fence = self.context.get_builtin_function(name);
1212         let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1213         self.llbb().add_eval(None, self.context.new_call(None, thread_fence, &[order]));
1214     }
1215
1216     fn set_invariant_load(&mut self, load: RValue<'gcc>) {
1217         // NOTE: Hack to consider vtable function pointer as non-global-variable function pointer.
1218         self.normal_function_addresses.borrow_mut().insert(load);
1219         // TODO(antoyo)
1220     }
1221
1222     fn lifetime_start(&mut self, _ptr: RValue<'gcc>, _size: Size) {
1223         // TODO(antoyo)
1224     }
1225
1226     fn lifetime_end(&mut self, _ptr: RValue<'gcc>, _size: Size) {
1227         // TODO(antoyo)
1228     }
1229
1230     fn call(&mut self, _typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], funclet: Option<&Funclet>) -> RValue<'gcc> {
1231         // FIXME(antoyo): remove when having a proper API.
1232         let gcc_func = unsafe { std::mem::transmute(func) };
1233         if self.functions.borrow().values().find(|value| **value == gcc_func).is_some() {
1234             self.function_call(func, args, funclet)
1235         }
1236         else {
1237             // If it's a not function that was defined, it's a function pointer.
1238             self.function_ptr_call(func, args, funclet)
1239         }
1240     }
1241
1242     fn zext(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
1243         // FIXME(antoyo): this does not zero-extend.
1244         if value.get_type().is_bool() && dest_typ.is_i8(&self.cx) {
1245             // FIXME(antoyo): hack because base::from_immediate converts i1 to i8.
1246             // Fix the code in codegen_ssa::base::from_immediate.
1247             return value;
1248         }
1249         self.gcc_int_cast(value, dest_typ)
1250     }
1251
1252     fn cx(&self) -> &CodegenCx<'gcc, 'tcx> {
1253         self.cx
1254     }
1255
1256     fn do_not_inline(&mut self, _llret: RValue<'gcc>) {
1257         // FIXME(bjorn3): implement
1258     }
1259
1260     fn set_span(&mut self, _span: Span) {}
1261
1262     fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
1263         if self.cx().val_ty(val) == self.cx().type_i1() {
1264             self.zext(val, self.cx().type_i8())
1265         }
1266         else {
1267             val
1268         }
1269     }
1270
1271     fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
1272         if scalar.is_bool() {
1273             return self.trunc(val, self.cx().type_i1());
1274         }
1275         val
1276     }
1277
1278     fn fptoui_sat(&mut self, val: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1279         self.fptoint_sat(false, val, dest_ty)
1280     }
1281
1282     fn fptosi_sat(&mut self, val: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1283         self.fptoint_sat(true, val, dest_ty)
1284     }
1285
1286     fn instrprof_increment(&mut self, _fn_name: RValue<'gcc>, _hash: RValue<'gcc>, _num_counters: RValue<'gcc>, _index: RValue<'gcc>) {
1287         unimplemented!();
1288     }
1289 }
1290
1291 impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
1292     fn fptoint_sat(&mut self, signed: bool, val: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1293         let src_ty = self.cx.val_ty(val);
1294         let (float_ty, int_ty) = if self.cx.type_kind(src_ty) == TypeKind::Vector {
1295             assert_eq!(self.cx.vector_length(src_ty), self.cx.vector_length(dest_ty));
1296             (self.cx.element_type(src_ty), self.cx.element_type(dest_ty))
1297         } else {
1298             (src_ty, dest_ty)
1299         };
1300
1301         // FIXME(jistone): the following was originally the fallback SSA implementation, before LLVM 13
1302         // added native `fptosi.sat` and `fptoui.sat` conversions, but it was used by GCC as well.
1303         // Now that LLVM always relies on its own, the code has been moved to GCC, but the comments are
1304         // still LLVM-specific. This should be updated, and use better GCC specifics if possible.
1305
1306         let int_width = self.cx.int_width(int_ty);
1307         let float_width = self.cx.float_width(float_ty);
1308         // LLVM's fpto[su]i returns undef when the input val is infinite, NaN, or does not fit into the
1309         // destination integer type after rounding towards zero. This `undef` value can cause UB in
1310         // safe code (see issue #10184), so we implement a saturating conversion on top of it:
1311         // Semantically, the mathematical value of the input is rounded towards zero to the next
1312         // mathematical integer, and then the result is clamped into the range of the destination
1313         // integer type. Positive and negative infinity are mapped to the maximum and minimum value of
1314         // the destination integer type. NaN is mapped to 0.
1315         //
1316         // Define f_min and f_max as the largest and smallest (finite) floats that are exactly equal to
1317         // a value representable in int_ty.
1318         // They are exactly equal to int_ty::{MIN,MAX} if float_ty has enough significand bits.
1319         // Otherwise, int_ty::MAX must be rounded towards zero, as it is one less than a power of two.
1320         // int_ty::MIN, however, is either zero or a negative power of two and is thus exactly
1321         // representable. Note that this only works if float_ty's exponent range is sufficiently large.
1322         // f16 or 256 bit integers would break this property. Right now the smallest float type is f32
1323         // with exponents ranging up to 127, which is barely enough for i128::MIN = -2^127.
1324         // On the other hand, f_max works even if int_ty::MAX is greater than float_ty::MAX. Because
1325         // we're rounding towards zero, we just get float_ty::MAX (which is always an integer).
1326         // This already happens today with u128::MAX = 2^128 - 1 > f32::MAX.
1327         let int_max = |signed: bool, int_width: u64| -> u128 {
1328             let shift_amount = 128 - int_width;
1329             if signed { i128::MAX as u128 >> shift_amount } else { u128::MAX >> shift_amount }
1330         };
1331         let int_min = |signed: bool, int_width: u64| -> i128 {
1332             if signed { i128::MIN >> (128 - int_width) } else { 0 }
1333         };
1334
1335         let compute_clamp_bounds_single = |signed: bool, int_width: u64| -> (u128, u128) {
1336             let rounded_min =
1337                 ieee::Single::from_i128_r(int_min(signed, int_width), Round::TowardZero);
1338             assert_eq!(rounded_min.status, Status::OK);
1339             let rounded_max =
1340                 ieee::Single::from_u128_r(int_max(signed, int_width), Round::TowardZero);
1341             assert!(rounded_max.value.is_finite());
1342             (rounded_min.value.to_bits(), rounded_max.value.to_bits())
1343         };
1344         let compute_clamp_bounds_double = |signed: bool, int_width: u64| -> (u128, u128) {
1345             let rounded_min =
1346                 ieee::Double::from_i128_r(int_min(signed, int_width), Round::TowardZero);
1347             assert_eq!(rounded_min.status, Status::OK);
1348             let rounded_max =
1349                 ieee::Double::from_u128_r(int_max(signed, int_width), Round::TowardZero);
1350             assert!(rounded_max.value.is_finite());
1351             (rounded_min.value.to_bits(), rounded_max.value.to_bits())
1352         };
1353         // To implement saturation, we perform the following steps:
1354         //
1355         // 1. Cast val to an integer with fpto[su]i. This may result in undef.
1356         // 2. Compare val to f_min and f_max, and use the comparison results to select:
1357         //  a) int_ty::MIN if val < f_min or val is NaN
1358         //  b) int_ty::MAX if val > f_max
1359         //  c) the result of fpto[su]i otherwise
1360         // 3. If val is NaN, return 0.0, otherwise return the result of step 2.
1361         //
1362         // This avoids resulting undef because values in range [f_min, f_max] by definition fit into the
1363         // destination type. It creates an undef temporary, but *producing* undef is not UB. Our use of
1364         // undef does not introduce any non-determinism either.
1365         // More importantly, the above procedure correctly implements saturating conversion.
1366         // Proof (sketch):
1367         // If val is NaN, 0 is returned by definition.
1368         // Otherwise, val is finite or infinite and thus can be compared with f_min and f_max.
1369         // This yields three cases to consider:
1370         // (1) if val in [f_min, f_max], the result of fpto[su]i is returned, which agrees with
1371         //     saturating conversion for inputs in that range.
1372         // (2) if val > f_max, then val is larger than int_ty::MAX. This holds even if f_max is rounded
1373         //     (i.e., if f_max < int_ty::MAX) because in those cases, nextUp(f_max) is already larger
1374         //     than int_ty::MAX. Because val is larger than int_ty::MAX, the return value of int_ty::MAX
1375         //     is correct.
1376         // (3) if val < f_min, then val is smaller than int_ty::MIN. As shown earlier, f_min exactly equals
1377         //     int_ty::MIN and therefore the return value of int_ty::MIN is correct.
1378         // QED.
1379
1380         let float_bits_to_llval = |bx: &mut Self, bits| {
1381             let bits_llval = match float_width {
1382                 32 => bx.cx().const_u32(bits as u32),
1383                 64 => bx.cx().const_u64(bits as u64),
1384                 n => bug!("unsupported float width {}", n),
1385             };
1386             bx.bitcast(bits_llval, float_ty)
1387         };
1388         let (f_min, f_max) = match float_width {
1389             32 => compute_clamp_bounds_single(signed, int_width),
1390             64 => compute_clamp_bounds_double(signed, int_width),
1391             n => bug!("unsupported float width {}", n),
1392         };
1393         let f_min = float_bits_to_llval(self, f_min);
1394         let f_max = float_bits_to_llval(self, f_max);
1395         let int_max = self.cx.const_uint_big(int_ty, int_max(signed, int_width));
1396         let int_min = self.cx.const_uint_big(int_ty, int_min(signed, int_width) as u128);
1397         let zero = self.cx.const_uint(int_ty, 0);
1398
1399         // If we're working with vectors, constants must be "splatted": the constant is duplicated
1400         // into each lane of the vector.  The algorithm stays the same, we are just using the
1401         // same constant across all lanes.
1402         let maybe_splat = |bx: &mut Self, val| {
1403             if bx.cx().type_kind(dest_ty) == TypeKind::Vector {
1404                 bx.vector_splat(bx.vector_length(dest_ty), val)
1405             } else {
1406                 val
1407             }
1408         };
1409         let f_min = maybe_splat(self, f_min);
1410         let f_max = maybe_splat(self, f_max);
1411         let int_max = maybe_splat(self, int_max);
1412         let int_min = maybe_splat(self, int_min);
1413         let zero = maybe_splat(self, zero);
1414
1415         // Step 1 ...
1416         let fptosui_result = if signed { self.fptosi(val, dest_ty) } else { self.fptoui(val, dest_ty) };
1417         let less_or_nan = self.fcmp(RealPredicate::RealULT, val, f_min);
1418         let greater = self.fcmp(RealPredicate::RealOGT, val, f_max);
1419
1420         // Step 2: We use two comparisons and two selects, with %s1 being the
1421         // result:
1422         //     %less_or_nan = fcmp ult %val, %f_min
1423         //     %greater = fcmp olt %val, %f_max
1424         //     %s0 = select %less_or_nan, int_ty::MIN, %fptosi_result
1425         //     %s1 = select %greater, int_ty::MAX, %s0
1426         // Note that %less_or_nan uses an *unordered* comparison. This
1427         // comparison is true if the operands are not comparable (i.e., if val is
1428         // NaN). The unordered comparison ensures that s1 becomes int_ty::MIN if
1429         // val is NaN.
1430         //
1431         // Performance note: Unordered comparison can be lowered to a "flipped"
1432         // comparison and a negation, and the negation can be merged into the
1433         // select. Therefore, it not necessarily any more expensive than an
1434         // ordered ("normal") comparison. Whether these optimizations will be
1435         // performed is ultimately up to the backend, but at least x86 does
1436         // perform them.
1437         let s0 = self.select(less_or_nan, int_min, fptosui_result);
1438         let s1 = self.select(greater, int_max, s0);
1439
1440         // Step 3: NaN replacement.
1441         // For unsigned types, the above step already yielded int_ty::MIN == 0 if val is NaN.
1442         // Therefore we only need to execute this step for signed integer types.
1443         if signed {
1444             // LLVM has no isNaN predicate, so we use (val == val) instead
1445             let cmp = self.fcmp(RealPredicate::RealOEQ, val, val);
1446             self.select(cmp, s1, zero)
1447         } else {
1448             s1
1449         }
1450     }
1451
1452     #[cfg(feature="master")]
1453     pub fn shuffle_vector(&mut self, v1: RValue<'gcc>, v2: RValue<'gcc>, mask: RValue<'gcc>) -> RValue<'gcc> {
1454         let struct_type = mask.get_type().is_struct().expect("mask of struct type");
1455
1456         // TODO(antoyo): use a recursive unqualified() here.
1457         let vector_type = v1.get_type().unqualified().dyncast_vector().expect("vector type");
1458         let element_type = vector_type.get_element_type();
1459         let vec_num_units = vector_type.get_num_units();
1460
1461         let mask_num_units = struct_type.get_field_count();
1462         let mut vector_elements = vec![];
1463         let mask_element_type =
1464             if element_type.is_integral() {
1465                 element_type
1466             }
1467             else {
1468                 #[cfg(feature="master")]
1469                 {
1470                     self.cx.type_ix(element_type.get_size() as u64 * 8)
1471                 }
1472                 #[cfg(not(feature="master"))]
1473                 self.int_type
1474             };
1475         for i in 0..mask_num_units {
1476             let field = struct_type.get_field(i as i32);
1477             vector_elements.push(self.context.new_cast(None, mask.access_field(None, field).to_rvalue(), mask_element_type));
1478         }
1479
1480         // NOTE: the mask needs to be the same length as the input vectors, so add the missing
1481         // elements in the mask if needed.
1482         for _ in mask_num_units..vec_num_units {
1483             vector_elements.push(self.context.new_rvalue_zero(mask_element_type));
1484         }
1485
1486         let array_type = self.context.new_array_type(None, element_type, vec_num_units as i32);
1487         let result_type = self.context.new_vector_type(element_type, mask_num_units as u64);
1488         let (v1, v2) =
1489             if vec_num_units < mask_num_units {
1490                 // NOTE: the mask needs to be the same length as the input vectors, so join the 2
1491                 // vectors and create a dummy second vector.
1492                 // TODO(antoyo): switch to using new_vector_access.
1493                 let array = self.context.new_bitcast(None, v1, array_type);
1494                 let mut elements = vec![];
1495                 for i in 0..vec_num_units {
1496                     elements.push(self.context.new_array_access(None, array, self.context.new_rvalue_from_int(self.int_type, i as i32)).to_rvalue());
1497                 }
1498                 // TODO(antoyo): switch to using new_vector_access.
1499                 let array = self.context.new_bitcast(None, v2, array_type);
1500                 for i in 0..(mask_num_units - vec_num_units) {
1501                     elements.push(self.context.new_array_access(None, array, self.context.new_rvalue_from_int(self.int_type, i as i32)).to_rvalue());
1502                 }
1503                 let v1 = self.context.new_rvalue_from_vector(None, result_type, &elements);
1504                 let zero = self.context.new_rvalue_zero(element_type);
1505                 let v2 = self.context.new_rvalue_from_vector(None, result_type, &vec![zero; mask_num_units]);
1506                 (v1, v2)
1507             }
1508             else {
1509                 (v1, v2)
1510             };
1511
1512         let new_mask_num_units = std::cmp::max(mask_num_units, vec_num_units);
1513         let mask_type = self.context.new_vector_type(mask_element_type, new_mask_num_units as u64);
1514         let mask = self.context.new_rvalue_from_vector(None, mask_type, &vector_elements);
1515         let result = self.context.new_rvalue_vector_perm(None, v1, v2, mask);
1516
1517         if vec_num_units != mask_num_units {
1518             // NOTE: if padding was added, only select the number of elements of the masks to
1519             // remove that padding in the result.
1520             let mut elements = vec![];
1521             // TODO(antoyo): switch to using new_vector_access.
1522             let array = self.context.new_bitcast(None, result, array_type);
1523             for i in 0..mask_num_units {
1524                 elements.push(self.context.new_array_access(None, array, self.context.new_rvalue_from_int(self.int_type, i as i32)).to_rvalue());
1525             }
1526             self.context.new_rvalue_from_vector(None, result_type, &elements)
1527         }
1528         else {
1529             result
1530         }
1531     }
1532
1533     #[cfg(not(feature="master"))]
1534     pub fn shuffle_vector(&mut self, _v1: RValue<'gcc>, _v2: RValue<'gcc>, _mask: RValue<'gcc>) -> RValue<'gcc> {
1535         unimplemented!();
1536     }
1537
1538     #[cfg(feature="master")]
1539     pub fn vector_reduce<F>(&mut self, src: RValue<'gcc>, op: F) -> RValue<'gcc>
1540     where F: Fn(RValue<'gcc>, RValue<'gcc>, &'gcc Context<'gcc>) -> RValue<'gcc>
1541     {
1542         let vector_type = src.get_type().unqualified().dyncast_vector().expect("vector type");
1543         let element_count = vector_type.get_num_units();
1544         let mut vector_elements = vec![];
1545         for i in 0..element_count {
1546             vector_elements.push(i);
1547         }
1548         let mask_type = self.context.new_vector_type(self.int_type, element_count as u64);
1549         let mut shift = 1;
1550         let mut res = src;
1551         while shift < element_count {
1552             let vector_elements: Vec<_> =
1553                 vector_elements.iter()
1554                     .map(|i| self.context.new_rvalue_from_int(self.int_type, ((i + shift) % element_count) as i32))
1555                     .collect();
1556             let mask = self.context.new_rvalue_from_vector(None, mask_type, &vector_elements);
1557             let shifted = self.context.new_rvalue_vector_perm(None, res, res, mask);
1558             shift *= 2;
1559             res = op(res, shifted, &self.context);
1560         }
1561         self.context.new_vector_access(None, res, self.context.new_rvalue_zero(self.int_type))
1562             .to_rvalue()
1563     }
1564
1565     #[cfg(not(feature="master"))]
1566     pub fn vector_reduce<F>(&mut self, src: RValue<'gcc>, op: F) -> RValue<'gcc>
1567     where F: Fn(RValue<'gcc>, RValue<'gcc>, &'gcc Context<'gcc>) -> RValue<'gcc>
1568     {
1569         unimplemented!();
1570     }
1571
1572     pub fn vector_reduce_op(&mut self, src: RValue<'gcc>, op: BinaryOp) -> RValue<'gcc> {
1573         self.vector_reduce(src, |a, b, context| context.new_binary_op(None, op, a.get_type(), a, b))
1574     }
1575
1576     pub fn vector_reduce_fadd_fast(&mut self, _acc: RValue<'gcc>, _src: RValue<'gcc>) -> RValue<'gcc> {
1577         unimplemented!();
1578     }
1579
1580     pub fn vector_reduce_fmul_fast(&mut self, _acc: RValue<'gcc>, _src: RValue<'gcc>) -> RValue<'gcc> {
1581         unimplemented!();
1582     }
1583
1584     // Inspired by Hacker's Delight min implementation.
1585     pub fn vector_reduce_min(&mut self, src: RValue<'gcc>) -> RValue<'gcc> {
1586         self.vector_reduce(src, |a, b, context| {
1587             let differences_or_zeros = difference_or_zero(a, b, context);
1588             context.new_binary_op(None, BinaryOp::Minus, a.get_type(), a, differences_or_zeros)
1589         })
1590     }
1591
1592     // Inspired by Hacker's Delight max implementation.
1593     pub fn vector_reduce_max(&mut self, src: RValue<'gcc>) -> RValue<'gcc> {
1594         self.vector_reduce(src, |a, b, context| {
1595             let differences_or_zeros = difference_or_zero(a, b, context);
1596             context.new_binary_op(None, BinaryOp::Plus, b.get_type(), b, differences_or_zeros)
1597         })
1598     }
1599
1600     pub fn vector_select(&mut self, cond: RValue<'gcc>, then_val: RValue<'gcc>, else_val: RValue<'gcc>) -> RValue<'gcc> {
1601         // cond is a vector of integers, not of bools.
1602         let cond_type = cond.get_type();
1603         let vector_type = cond_type.unqualified().dyncast_vector().expect("vector type");
1604         let num_units = vector_type.get_num_units();
1605         let element_type = vector_type.get_element_type();
1606         let zeros = vec![self.context.new_rvalue_zero(element_type); num_units];
1607         let zeros = self.context.new_rvalue_from_vector(None, cond_type, &zeros);
1608
1609         let masks = self.context.new_comparison(None, ComparisonOp::NotEquals, cond, zeros);
1610         let then_vals = masks & then_val;
1611
1612         let ones = vec![self.context.new_rvalue_one(element_type); num_units];
1613         let ones = self.context.new_rvalue_from_vector(None, cond_type, &ones);
1614         let inverted_masks = masks + ones;
1615         // NOTE: sometimes, the type of else_val can be different than the type of then_val in
1616         // libgccjit (vector of int vs vector of int32_t), but they should be the same for the AND
1617         // operation to work.
1618         let else_val = self.context.new_bitcast(None, else_val, then_val.get_type());
1619         let else_vals = inverted_masks & else_val;
1620
1621         then_vals | else_vals
1622     }
1623 }
1624
1625 fn difference_or_zero<'gcc>(a: RValue<'gcc>, b: RValue<'gcc>, context: &'gcc Context<'gcc>) -> RValue<'gcc> {
1626     let difference = a - b;
1627     let masks = context.new_comparison(None, ComparisonOp::GreaterThanEquals, b, a);
1628     difference & masks
1629 }
1630
1631 impl<'a, 'gcc, 'tcx> StaticBuilderMethods for Builder<'a, 'gcc, 'tcx> {
1632     fn get_static(&mut self, def_id: DefId) -> RValue<'gcc> {
1633         // Forward to the `get_static` method of `CodegenCx`
1634         self.cx().get_static(def_id).get_address(None)
1635     }
1636 }
1637
1638 impl<'tcx> HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
1639     fn param_env(&self) -> ParamEnv<'tcx> {
1640         self.cx.param_env()
1641     }
1642 }
1643
1644 impl<'tcx> HasTargetSpec for Builder<'_, '_, 'tcx> {
1645     fn target_spec(&self) -> &Target {
1646         &self.cx.target_spec()
1647     }
1648 }
1649
1650 pub trait ToGccComp {
1651     fn to_gcc_comparison(&self) -> ComparisonOp;
1652 }
1653
1654 impl ToGccComp for IntPredicate {
1655     fn to_gcc_comparison(&self) -> ComparisonOp {
1656         match *self {
1657             IntPredicate::IntEQ => ComparisonOp::Equals,
1658             IntPredicate::IntNE => ComparisonOp::NotEquals,
1659             IntPredicate::IntUGT => ComparisonOp::GreaterThan,
1660             IntPredicate::IntUGE => ComparisonOp::GreaterThanEquals,
1661             IntPredicate::IntULT => ComparisonOp::LessThan,
1662             IntPredicate::IntULE => ComparisonOp::LessThanEquals,
1663             IntPredicate::IntSGT => ComparisonOp::GreaterThan,
1664             IntPredicate::IntSGE => ComparisonOp::GreaterThanEquals,
1665             IntPredicate::IntSLT => ComparisonOp::LessThan,
1666             IntPredicate::IntSLE => ComparisonOp::LessThanEquals,
1667         }
1668     }
1669 }
1670
1671 impl ToGccComp for RealPredicate {
1672     fn to_gcc_comparison(&self) -> ComparisonOp {
1673         // TODO(antoyo): check that ordered vs non-ordered is respected.
1674         match *self {
1675             RealPredicate::RealPredicateFalse => unreachable!(),
1676             RealPredicate::RealOEQ => ComparisonOp::Equals,
1677             RealPredicate::RealOGT => ComparisonOp::GreaterThan,
1678             RealPredicate::RealOGE => ComparisonOp::GreaterThanEquals,
1679             RealPredicate::RealOLT => ComparisonOp::LessThan,
1680             RealPredicate::RealOLE => ComparisonOp::LessThanEquals,
1681             RealPredicate::RealONE => ComparisonOp::NotEquals,
1682             RealPredicate::RealORD => unreachable!(),
1683             RealPredicate::RealUNO => unreachable!(),
1684             RealPredicate::RealUEQ => ComparisonOp::Equals,
1685             RealPredicate::RealUGT => ComparisonOp::GreaterThan,
1686             RealPredicate::RealUGE => ComparisonOp::GreaterThan,
1687             RealPredicate::RealULT => ComparisonOp::LessThan,
1688             RealPredicate::RealULE => ComparisonOp::LessThan,
1689             RealPredicate::RealUNE => ComparisonOp::NotEquals,
1690             RealPredicate::RealPredicateTrue => unreachable!(),
1691         }
1692     }
1693 }
1694
1695 #[repr(C)]
1696 #[allow(non_camel_case_types)]
1697 enum MemOrdering {
1698     __ATOMIC_RELAXED,
1699     __ATOMIC_CONSUME,
1700     __ATOMIC_ACQUIRE,
1701     __ATOMIC_RELEASE,
1702     __ATOMIC_ACQ_REL,
1703     __ATOMIC_SEQ_CST,
1704 }
1705
1706 trait ToGccOrdering {
1707     fn to_gcc(self) -> i32;
1708 }
1709
1710 impl ToGccOrdering for AtomicOrdering {
1711     fn to_gcc(self) -> i32 {
1712         use MemOrdering::*;
1713
1714         let ordering =
1715             match self {
1716                 AtomicOrdering::Unordered => __ATOMIC_RELAXED,
1717                 AtomicOrdering::Relaxed => __ATOMIC_RELAXED, // TODO(antoyo): check if that's the same.
1718                 AtomicOrdering::Acquire => __ATOMIC_ACQUIRE,
1719                 AtomicOrdering::Release => __ATOMIC_RELEASE,
1720                 AtomicOrdering::AcquireRelease => __ATOMIC_ACQ_REL,
1721                 AtomicOrdering::SequentiallyConsistent => __ATOMIC_SEQ_CST,
1722             };
1723         ordering as i32
1724     }
1725 }