]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/mir/block.rs
Auto merge of #83776 - jyn514:update-stdarch-docs, r=Amanieu
[rust.git] / compiler / rustc_codegen_ssa / src / mir / block.rs
1 use super::operand::OperandRef;
2 use super::operand::OperandValue::{Immediate, Pair, Ref};
3 use super::place::PlaceRef;
4 use super::{FunctionCx, LocalRef};
5
6 use crate::base;
7 use crate::common::{self, IntPredicate};
8 use crate::meth;
9 use crate::traits::*;
10 use crate::MemFlags;
11
12 use rustc_ast as ast;
13 use rustc_hir::lang_items::LangItem;
14 use rustc_index::vec::Idx;
15 use rustc_middle::mir::interpret::ConstValue;
16 use rustc_middle::mir::AssertKind;
17 use rustc_middle::mir::{self, SwitchTargets};
18 use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt};
19 use rustc_middle::ty::print::with_no_trimmed_paths;
20 use rustc_middle::ty::{self, Instance, Ty, TypeFoldable};
21 use rustc_span::source_map::Span;
22 use rustc_span::{sym, Symbol};
23 use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
24 use rustc_target::abi::{self, LayoutOf};
25 use rustc_target::spec::abi::Abi;
26
27 /// Used by `FunctionCx::codegen_terminator` for emitting common patterns
28 /// e.g., creating a basic block, calling a function, etc.
29 struct TerminatorCodegenHelper<'tcx> {
30     bb: mir::BasicBlock,
31     terminator: &'tcx mir::Terminator<'tcx>,
32     funclet_bb: Option<mir::BasicBlock>,
33 }
34
35 impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
36     /// Returns the associated funclet from `FunctionCx::funclets` for the
37     /// `funclet_bb` member if it is not `None`.
38     fn funclet<'b, Bx: BuilderMethods<'a, 'tcx>>(
39         &self,
40         fx: &'b FunctionCx<'a, 'tcx, Bx>,
41     ) -> Option<&'b Bx::Funclet> {
42         self.funclet_bb.and_then(|funcl| fx.funclets[funcl].as_ref())
43     }
44
45     fn lltarget<Bx: BuilderMethods<'a, 'tcx>>(
46         &self,
47         fx: &mut FunctionCx<'a, 'tcx, Bx>,
48         target: mir::BasicBlock,
49     ) -> (Bx::BasicBlock, bool) {
50         let span = self.terminator.source_info.span;
51         let lltarget = fx.blocks[target];
52         let target_funclet = fx.cleanup_kinds[target].funclet_bb(target);
53         match (self.funclet_bb, target_funclet) {
54             (None, None) => (lltarget, false),
55             (Some(f), Some(t_f)) if f == t_f || !base::wants_msvc_seh(fx.cx.tcx().sess) => {
56                 (lltarget, false)
57             }
58             // jump *into* cleanup - need a landing pad if GNU
59             (None, Some(_)) => (fx.landing_pad_to(target), false),
60             (Some(_), None) => span_bug!(span, "{:?} - jump out of cleanup?", self.terminator),
61             (Some(_), Some(_)) => (fx.landing_pad_to(target), true),
62         }
63     }
64
65     /// Create a basic block.
66     fn llblock<Bx: BuilderMethods<'a, 'tcx>>(
67         &self,
68         fx: &mut FunctionCx<'a, 'tcx, Bx>,
69         target: mir::BasicBlock,
70     ) -> Bx::BasicBlock {
71         let (lltarget, is_cleanupret) = self.lltarget(fx, target);
72         if is_cleanupret {
73             // MSVC cross-funclet jump - need a trampoline
74
75             debug!("llblock: creating cleanup trampoline for {:?}", target);
76             let name = &format!("{:?}_cleanup_trampoline_{:?}", self.bb, target);
77             let mut trampoline = fx.new_block(name);
78             trampoline.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget));
79             trampoline.llbb()
80         } else {
81             lltarget
82         }
83     }
84
85     fn funclet_br<Bx: BuilderMethods<'a, 'tcx>>(
86         &self,
87         fx: &mut FunctionCx<'a, 'tcx, Bx>,
88         bx: &mut Bx,
89         target: mir::BasicBlock,
90     ) {
91         let (lltarget, is_cleanupret) = self.lltarget(fx, target);
92         if is_cleanupret {
93             // micro-optimization: generate a `ret` rather than a jump
94             // to a trampoline.
95             bx.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget));
96         } else {
97             bx.br(lltarget);
98         }
99     }
100
101     /// Call `fn_ptr` of `fn_abi` with the arguments `llargs`, the optional
102     /// return destination `destination` and the cleanup function `cleanup`.
103     fn do_call<Bx: BuilderMethods<'a, 'tcx>>(
104         &self,
105         fx: &mut FunctionCx<'a, 'tcx, Bx>,
106         bx: &mut Bx,
107         fn_abi: FnAbi<'tcx, Ty<'tcx>>,
108         fn_ptr: Bx::Value,
109         llargs: &[Bx::Value],
110         destination: Option<(ReturnDest<'tcx, Bx::Value>, mir::BasicBlock)>,
111         cleanup: Option<mir::BasicBlock>,
112     ) {
113         // If there is a cleanup block and the function we're calling can unwind, then
114         // do an invoke, otherwise do a call.
115         if let Some(cleanup) = cleanup.filter(|_| fn_abi.can_unwind) {
116             let ret_bx = if let Some((_, target)) = destination {
117                 fx.blocks[target]
118             } else {
119                 fx.unreachable_block()
120             };
121             let invokeret =
122                 bx.invoke(fn_ptr, &llargs, ret_bx, self.llblock(fx, cleanup), self.funclet(fx));
123             bx.apply_attrs_callsite(&fn_abi, invokeret);
124
125             if let Some((ret_dest, target)) = destination {
126                 let mut ret_bx = fx.build_block(target);
127                 fx.set_debug_loc(&mut ret_bx, self.terminator.source_info);
128                 fx.store_return(&mut ret_bx, ret_dest, &fn_abi.ret, invokeret);
129             }
130         } else {
131             let llret = bx.call(fn_ptr, &llargs, self.funclet(fx));
132             bx.apply_attrs_callsite(&fn_abi, llret);
133             if fx.mir[self.bb].is_cleanup {
134                 // Cleanup is always the cold path. Don't inline
135                 // drop glue. Also, when there is a deeply-nested
136                 // struct, there are "symmetry" issues that cause
137                 // exponential inlining - see issue #41696.
138                 bx.do_not_inline(llret);
139             }
140
141             if let Some((ret_dest, target)) = destination {
142                 fx.store_return(bx, ret_dest, &fn_abi.ret, llret);
143                 self.funclet_br(fx, bx, target);
144             } else {
145                 bx.unreachable();
146             }
147         }
148     }
149 }
150
151 /// Codegen implementations for some terminator variants.
152 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
153     /// Generates code for a `Resume` terminator.
154     fn codegen_resume_terminator(&mut self, helper: TerminatorCodegenHelper<'tcx>, mut bx: Bx) {
155         if let Some(funclet) = helper.funclet(self) {
156             bx.cleanup_ret(funclet, None);
157         } else {
158             let slot = self.get_personality_slot(&mut bx);
159             let lp0 = slot.project_field(&mut bx, 0);
160             let lp0 = bx.load_operand(lp0).immediate();
161             let lp1 = slot.project_field(&mut bx, 1);
162             let lp1 = bx.load_operand(lp1).immediate();
163             slot.storage_dead(&mut bx);
164
165             let mut lp = bx.const_undef(self.landing_pad_type());
166             lp = bx.insert_value(lp, lp0, 0);
167             lp = bx.insert_value(lp, lp1, 1);
168             bx.resume(lp);
169         }
170     }
171
172     fn codegen_switchint_terminator(
173         &mut self,
174         helper: TerminatorCodegenHelper<'tcx>,
175         mut bx: Bx,
176         discr: &mir::Operand<'tcx>,
177         switch_ty: Ty<'tcx>,
178         targets: &SwitchTargets,
179     ) {
180         let discr = self.codegen_operand(&mut bx, &discr);
181         // `switch_ty` is redundant, sanity-check that.
182         assert_eq!(discr.layout.ty, switch_ty);
183         let mut target_iter = targets.iter();
184         if target_iter.len() == 1 {
185             // If there are two targets (one conditional, one fallback), emit br instead of switch
186             let (test_value, target) = target_iter.next().unwrap();
187             let lltrue = helper.llblock(self, target);
188             let llfalse = helper.llblock(self, targets.otherwise());
189             if switch_ty == bx.tcx().types.bool {
190                 // Don't generate trivial icmps when switching on bool
191                 match test_value {
192                     0 => bx.cond_br(discr.immediate(), llfalse, lltrue),
193                     1 => bx.cond_br(discr.immediate(), lltrue, llfalse),
194                     _ => bug!(),
195                 }
196             } else {
197                 let switch_llty = bx.immediate_backend_type(bx.layout_of(switch_ty));
198                 let llval = bx.const_uint_big(switch_llty, test_value);
199                 let cmp = bx.icmp(IntPredicate::IntEQ, discr.immediate(), llval);
200                 bx.cond_br(cmp, lltrue, llfalse);
201             }
202         } else {
203             bx.switch(
204                 discr.immediate(),
205                 helper.llblock(self, targets.otherwise()),
206                 target_iter.map(|(value, target)| (value, helper.llblock(self, target))),
207             );
208         }
209     }
210
211     fn codegen_return_terminator(&mut self, mut bx: Bx) {
212         // Call `va_end` if this is the definition of a C-variadic function.
213         if self.fn_abi.c_variadic {
214             // The `VaList` "spoofed" argument is just after all the real arguments.
215             let va_list_arg_idx = self.fn_abi.args.len();
216             match self.locals[mir::Local::new(1 + va_list_arg_idx)] {
217                 LocalRef::Place(va_list) => {
218                     bx.va_end(va_list.llval);
219                 }
220                 _ => bug!("C-variadic function must have a `VaList` place"),
221             }
222         }
223         if self.fn_abi.ret.layout.abi.is_uninhabited() {
224             // Functions with uninhabited return values are marked `noreturn`,
225             // so we should make sure that we never actually do.
226             // We play it safe by using a well-defined `abort`, but we could go for immediate UB
227             // if that turns out to be helpful.
228             bx.abort();
229             // `abort` does not terminate the block, so we still need to generate
230             // an `unreachable` terminator after it.
231             bx.unreachable();
232             return;
233         }
234         let llval = match self.fn_abi.ret.mode {
235             PassMode::Ignore | PassMode::Indirect { .. } => {
236                 bx.ret_void();
237                 return;
238             }
239
240             PassMode::Direct(_) | PassMode::Pair(..) => {
241                 let op = self.codegen_consume(&mut bx, mir::Place::return_place().as_ref());
242                 if let Ref(llval, _, align) = op.val {
243                     bx.load(llval, align)
244                 } else {
245                     op.immediate_or_packed_pair(&mut bx)
246                 }
247             }
248
249             PassMode::Cast(cast_ty) => {
250                 let op = match self.locals[mir::RETURN_PLACE] {
251                     LocalRef::Operand(Some(op)) => op,
252                     LocalRef::Operand(None) => bug!("use of return before def"),
253                     LocalRef::Place(cg_place) => OperandRef {
254                         val: Ref(cg_place.llval, None, cg_place.align),
255                         layout: cg_place.layout,
256                     },
257                     LocalRef::UnsizedPlace(_) => bug!("return type must be sized"),
258                 };
259                 let llslot = match op.val {
260                     Immediate(_) | Pair(..) => {
261                         let scratch = PlaceRef::alloca(&mut bx, self.fn_abi.ret.layout);
262                         op.val.store(&mut bx, scratch);
263                         scratch.llval
264                     }
265                     Ref(llval, _, align) => {
266                         assert_eq!(align, op.layout.align.abi, "return place is unaligned!");
267                         llval
268                     }
269                 };
270                 let addr = bx.pointercast(llslot, bx.type_ptr_to(bx.cast_backend_type(&cast_ty)));
271                 bx.load(addr, self.fn_abi.ret.layout.align.abi)
272             }
273         };
274         bx.ret(llval);
275     }
276
277     fn codegen_drop_terminator(
278         &mut self,
279         helper: TerminatorCodegenHelper<'tcx>,
280         mut bx: Bx,
281         location: mir::Place<'tcx>,
282         target: mir::BasicBlock,
283         unwind: Option<mir::BasicBlock>,
284     ) {
285         let ty = location.ty(self.mir, bx.tcx()).ty;
286         let ty = self.monomorphize(ty);
287         let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty);
288
289         if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
290             // we don't actually need to drop anything.
291             helper.funclet_br(self, &mut bx, target);
292             return;
293         }
294
295         let place = self.codegen_place(&mut bx, location.as_ref());
296         let (args1, args2);
297         let mut args = if let Some(llextra) = place.llextra {
298             args2 = [place.llval, llextra];
299             &args2[..]
300         } else {
301             args1 = [place.llval];
302             &args1[..]
303         };
304         let (drop_fn, fn_abi) = match ty.kind() {
305             // FIXME(eddyb) perhaps move some of this logic into
306             // `Instance::resolve_drop_in_place`?
307             ty::Dynamic(..) => {
308                 let virtual_drop = Instance {
309                     def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0),
310                     substs: drop_fn.substs,
311                 };
312                 let fn_abi = FnAbi::of_instance(&bx, virtual_drop, &[]);
313                 let vtable = args[1];
314                 args = &args[..1];
315                 (meth::DESTRUCTOR.get_fn(&mut bx, vtable, &fn_abi), fn_abi)
316             }
317             _ => (bx.get_fn_addr(drop_fn), FnAbi::of_instance(&bx, drop_fn, &[])),
318         };
319         helper.do_call(
320             self,
321             &mut bx,
322             fn_abi,
323             drop_fn,
324             args,
325             Some((ReturnDest::Nothing, target)),
326             unwind,
327         );
328     }
329
330     fn codegen_assert_terminator(
331         &mut self,
332         helper: TerminatorCodegenHelper<'tcx>,
333         mut bx: Bx,
334         terminator: &mir::Terminator<'tcx>,
335         cond: &mir::Operand<'tcx>,
336         expected: bool,
337         msg: &mir::AssertMessage<'tcx>,
338         target: mir::BasicBlock,
339         cleanup: Option<mir::BasicBlock>,
340     ) {
341         let span = terminator.source_info.span;
342         let cond = self.codegen_operand(&mut bx, cond).immediate();
343         let mut const_cond = bx.const_to_opt_u128(cond, false).map(|c| c == 1);
344
345         // This case can currently arise only from functions marked
346         // with #[rustc_inherit_overflow_checks] and inlined from
347         // another crate (mostly core::num generic/#[inline] fns),
348         // while the current crate doesn't use overflow checks.
349         // NOTE: Unlike binops, negation doesn't have its own
350         // checked operation, just a comparison with the minimum
351         // value, so we have to check for the assert message.
352         if !bx.check_overflow() {
353             if let AssertKind::OverflowNeg(_) = *msg {
354                 const_cond = Some(expected);
355             }
356         }
357
358         // Don't codegen the panic block if success if known.
359         if const_cond == Some(expected) {
360             helper.funclet_br(self, &mut bx, target);
361             return;
362         }
363
364         // Pass the condition through llvm.expect for branch hinting.
365         let cond = bx.expect(cond, expected);
366
367         // Create the failure block and the conditional branch to it.
368         let lltarget = helper.llblock(self, target);
369         let panic_block = self.new_block("panic");
370         if expected {
371             bx.cond_br(cond, lltarget, panic_block.llbb());
372         } else {
373             bx.cond_br(cond, panic_block.llbb(), lltarget);
374         }
375
376         // After this point, bx is the block for the call to panic.
377         bx = panic_block;
378         self.set_debug_loc(&mut bx, terminator.source_info);
379
380         // Get the location information.
381         let location = self.get_caller_location(&mut bx, terminator.source_info).immediate();
382
383         // Put together the arguments to the panic entry point.
384         let (lang_item, args) = match msg {
385             AssertKind::BoundsCheck { ref len, ref index } => {
386                 let len = self.codegen_operand(&mut bx, len).immediate();
387                 let index = self.codegen_operand(&mut bx, index).immediate();
388                 // It's `fn panic_bounds_check(index: usize, len: usize)`,
389                 // and `#[track_caller]` adds an implicit third argument.
390                 (LangItem::PanicBoundsCheck, vec![index, len, location])
391             }
392             _ => {
393                 let msg_str = Symbol::intern(msg.description());
394                 let msg = bx.const_str(msg_str);
395                 // It's `pub fn panic(expr: &str)`, with the wide reference being passed
396                 // as two arguments, and `#[track_caller]` adds an implicit third argument.
397                 (LangItem::Panic, vec![msg.0, msg.1, location])
398             }
399         };
400
401         // Obtain the panic entry point.
402         let def_id = common::langcall(bx.tcx(), Some(span), "", lang_item);
403         let instance = ty::Instance::mono(bx.tcx(), def_id);
404         let fn_abi = FnAbi::of_instance(&bx, instance, &[]);
405         let llfn = bx.get_fn_addr(instance);
406
407         // Codegen the actual panic invoke/call.
408         helper.do_call(self, &mut bx, fn_abi, llfn, &args, None, cleanup);
409     }
410
411     /// Returns `true` if this is indeed a panic intrinsic and codegen is done.
412     fn codegen_panic_intrinsic(
413         &mut self,
414         helper: &TerminatorCodegenHelper<'tcx>,
415         bx: &mut Bx,
416         intrinsic: Option<Symbol>,
417         instance: Option<Instance<'tcx>>,
418         source_info: mir::SourceInfo,
419         destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>,
420         cleanup: Option<mir::BasicBlock>,
421     ) -> bool {
422         // Emit a panic or a no-op for `assert_*` intrinsics.
423         // These are intrinsics that compile to panics so that we can get a message
424         // which mentions the offending type, even from a const context.
425         #[derive(Debug, PartialEq)]
426         enum AssertIntrinsic {
427             Inhabited,
428             ZeroValid,
429             UninitValid,
430         }
431         let panic_intrinsic = intrinsic.and_then(|i| match i {
432             sym::assert_inhabited => Some(AssertIntrinsic::Inhabited),
433             sym::assert_zero_valid => Some(AssertIntrinsic::ZeroValid),
434             sym::assert_uninit_valid => Some(AssertIntrinsic::UninitValid),
435             _ => None,
436         });
437         if let Some(intrinsic) = panic_intrinsic {
438             use AssertIntrinsic::*;
439             let ty = instance.unwrap().substs.type_at(0);
440             let layout = bx.layout_of(ty);
441             let do_panic = match intrinsic {
442                 Inhabited => layout.abi.is_uninhabited(),
443                 // We unwrap as the error type is `!`.
444                 ZeroValid => !layout.might_permit_raw_init(bx, /*zero:*/ true).unwrap(),
445                 // We unwrap as the error type is `!`.
446                 UninitValid => !layout.might_permit_raw_init(bx, /*zero:*/ false).unwrap(),
447             };
448             if do_panic {
449                 let msg_str = with_no_trimmed_paths(|| {
450                     if layout.abi.is_uninhabited() {
451                         // Use this error even for the other intrinsics as it is more precise.
452                         format!("attempted to instantiate uninhabited type `{}`", ty)
453                     } else if intrinsic == ZeroValid {
454                         format!("attempted to zero-initialize type `{}`, which is invalid", ty)
455                     } else {
456                         format!("attempted to leave type `{}` uninitialized, which is invalid", ty)
457                     }
458                 });
459                 let msg = bx.const_str(Symbol::intern(&msg_str));
460                 let location = self.get_caller_location(bx, source_info).immediate();
461
462                 // Obtain the panic entry point.
463                 // FIXME: dedup this with `codegen_assert_terminator` above.
464                 let def_id =
465                     common::langcall(bx.tcx(), Some(source_info.span), "", LangItem::Panic);
466                 let instance = ty::Instance::mono(bx.tcx(), def_id);
467                 let fn_abi = FnAbi::of_instance(bx, instance, &[]);
468                 let llfn = bx.get_fn_addr(instance);
469
470                 // Codegen the actual panic invoke/call.
471                 helper.do_call(
472                     self,
473                     bx,
474                     fn_abi,
475                     llfn,
476                     &[msg.0, msg.1, location],
477                     destination.as_ref().map(|(_, bb)| (ReturnDest::Nothing, *bb)),
478                     cleanup,
479                 );
480             } else {
481                 // a NOP
482                 let target = destination.as_ref().unwrap().1;
483                 helper.funclet_br(self, bx, target)
484             }
485             true
486         } else {
487             false
488         }
489     }
490
491     fn codegen_call_terminator(
492         &mut self,
493         helper: TerminatorCodegenHelper<'tcx>,
494         mut bx: Bx,
495         terminator: &mir::Terminator<'tcx>,
496         func: &mir::Operand<'tcx>,
497         args: &[mir::Operand<'tcx>],
498         destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>,
499         cleanup: Option<mir::BasicBlock>,
500         fn_span: Span,
501     ) {
502         let source_info = terminator.source_info;
503         let span = source_info.span;
504
505         // Create the callee. This is a fn ptr or zero-sized and hence a kind of scalar.
506         let callee = self.codegen_operand(&mut bx, func);
507
508         let (instance, mut llfn) = match *callee.layout.ty.kind() {
509             ty::FnDef(def_id, substs) => (
510                 Some(
511                     ty::Instance::resolve(bx.tcx(), ty::ParamEnv::reveal_all(), def_id, substs)
512                         .unwrap()
513                         .unwrap()
514                         .polymorphize(bx.tcx()),
515                 ),
516                 None,
517             ),
518             ty::FnPtr(_) => (None, Some(callee.immediate())),
519             _ => bug!("{} is not callable", callee.layout.ty),
520         };
521         let def = instance.map(|i| i.def);
522
523         if let Some(ty::InstanceDef::DropGlue(_, None)) = def {
524             // Empty drop glue; a no-op.
525             let &(_, target) = destination.as_ref().unwrap();
526             helper.funclet_br(self, &mut bx, target);
527             return;
528         }
529
530         // FIXME(eddyb) avoid computing this if possible, when `instance` is
531         // available - right now `sig` is only needed for getting the `abi`
532         // and figuring out how many extra args were passed to a C-variadic `fn`.
533         let sig = callee.layout.ty.fn_sig(bx.tcx());
534         let abi = sig.abi();
535
536         // Handle intrinsics old codegen wants Expr's for, ourselves.
537         let intrinsic = match def {
538             Some(ty::InstanceDef::Intrinsic(def_id)) => Some(bx.tcx().item_name(def_id)),
539             _ => None,
540         };
541
542         let extra_args = &args[sig.inputs().skip_binder().len()..];
543         let extra_args = extra_args
544             .iter()
545             .map(|op_arg| {
546                 let op_ty = op_arg.ty(self.mir, bx.tcx());
547                 self.monomorphize(op_ty)
548             })
549             .collect::<Vec<_>>();
550
551         let fn_abi = match instance {
552             Some(instance) => FnAbi::of_instance(&bx, instance, &extra_args),
553             None => FnAbi::of_fn_ptr(&bx, sig, &extra_args),
554         };
555
556         if intrinsic == Some(sym::transmute) {
557             if let Some(destination_ref) = destination.as_ref() {
558                 let &(dest, target) = destination_ref;
559                 self.codegen_transmute(&mut bx, &args[0], dest);
560                 helper.funclet_br(self, &mut bx, target);
561             } else {
562                 // If we are trying to transmute to an uninhabited type,
563                 // it is likely there is no allotted destination. In fact,
564                 // transmuting to an uninhabited type is UB, which means
565                 // we can do what we like. Here, we declare that transmuting
566                 // into an uninhabited type is impossible, so anything following
567                 // it must be unreachable.
568                 assert_eq!(fn_abi.ret.layout.abi, abi::Abi::Uninhabited);
569                 bx.unreachable();
570             }
571             return;
572         }
573
574         if self.codegen_panic_intrinsic(
575             &helper,
576             &mut bx,
577             intrinsic,
578             instance,
579             source_info,
580             destination,
581             cleanup,
582         ) {
583             return;
584         }
585
586         // The arguments we'll be passing. Plus one to account for outptr, if used.
587         let arg_count = fn_abi.args.len() + fn_abi.ret.is_indirect() as usize;
588         let mut llargs = Vec::with_capacity(arg_count);
589
590         // Prepare the return value destination
591         let ret_dest = if let Some((dest, _)) = *destination {
592             let is_intrinsic = intrinsic.is_some();
593             self.make_return_dest(&mut bx, dest, &fn_abi.ret, &mut llargs, is_intrinsic)
594         } else {
595             ReturnDest::Nothing
596         };
597
598         if intrinsic == Some(sym::caller_location) {
599             if let Some((_, target)) = destination.as_ref() {
600                 let location = self
601                     .get_caller_location(&mut bx, mir::SourceInfo { span: fn_span, ..source_info });
602
603                 if let ReturnDest::IndirectOperand(tmp, _) = ret_dest {
604                     location.val.store(&mut bx, tmp);
605                 }
606                 self.store_return(&mut bx, ret_dest, &fn_abi.ret, location.immediate());
607                 helper.funclet_br(self, &mut bx, *target);
608             }
609             return;
610         }
611
612         match intrinsic {
613             None | Some(sym::drop_in_place) => {}
614             Some(sym::copy_nonoverlapping) => unreachable!(),
615             Some(intrinsic) => {
616                 let dest = match ret_dest {
617                     _ if fn_abi.ret.is_indirect() => llargs[0],
618                     ReturnDest::Nothing => {
619                         bx.const_undef(bx.type_ptr_to(bx.arg_memory_ty(&fn_abi.ret)))
620                     }
621                     ReturnDest::IndirectOperand(dst, _) | ReturnDest::Store(dst) => dst.llval,
622                     ReturnDest::DirectOperand(_) => {
623                         bug!("Cannot use direct operand with an intrinsic call")
624                     }
625                 };
626
627                 let args: Vec<_> = args
628                     .iter()
629                     .enumerate()
630                     .map(|(i, arg)| {
631                         // The indices passed to simd_shuffle* in the
632                         // third argument must be constant. This is
633                         // checked by const-qualification, which also
634                         // promotes any complex rvalues to constants.
635                         if i == 2 && intrinsic.as_str().starts_with("simd_shuffle") {
636                             if let mir::Operand::Constant(constant) = arg {
637                                 let c = self.eval_mir_constant(constant);
638                                 let (llval, ty) =
639                                     self.simd_shuffle_indices(&bx, constant.span, constant.ty(), c);
640                                 return OperandRef {
641                                     val: Immediate(llval),
642                                     layout: bx.layout_of(ty),
643                                 };
644                             } else {
645                                 span_bug!(span, "shuffle indices must be constant");
646                             }
647                         }
648
649                         self.codegen_operand(&mut bx, arg)
650                     })
651                     .collect();
652
653                 Self::codegen_intrinsic_call(
654                     &mut bx,
655                     *instance.as_ref().unwrap(),
656                     &fn_abi,
657                     &args,
658                     dest,
659                     span,
660                 );
661
662                 if let ReturnDest::IndirectOperand(dst, _) = ret_dest {
663                     self.store_return(&mut bx, ret_dest, &fn_abi.ret, dst.llval);
664                 }
665
666                 if let Some((_, target)) = *destination {
667                     helper.funclet_br(self, &mut bx, target);
668                 } else {
669                     bx.unreachable();
670                 }
671
672                 return;
673             }
674         }
675
676         // Split the rust-call tupled arguments off.
677         let (first_args, untuple) = if abi == Abi::RustCall && !args.is_empty() {
678             let (tup, args) = args.split_last().unwrap();
679             (args, Some(tup))
680         } else {
681             (args, None)
682         };
683
684         'make_args: for (i, arg) in first_args.iter().enumerate() {
685             let mut op = self.codegen_operand(&mut bx, arg);
686
687             if let (0, Some(ty::InstanceDef::Virtual(_, idx))) = (i, def) {
688                 if let Pair(..) = op.val {
689                     // In the case of Rc<Self>, we need to explicitly pass a
690                     // *mut RcBox<Self> with a Scalar (not ScalarPair) ABI. This is a hack
691                     // that is understood elsewhere in the compiler as a method on
692                     // `dyn Trait`.
693                     // To get a `*mut RcBox<Self>`, we just keep unwrapping newtypes until
694                     // we get a value of a built-in pointer type
695                     'descend_newtypes: while !op.layout.ty.is_unsafe_ptr()
696                         && !op.layout.ty.is_region_ptr()
697                     {
698                         for i in 0..op.layout.fields.count() {
699                             let field = op.extract_field(&mut bx, i);
700                             if !field.layout.is_zst() {
701                                 // we found the one non-zero-sized field that is allowed
702                                 // now find *its* non-zero-sized field, or stop if it's a
703                                 // pointer
704                                 op = field;
705                                 continue 'descend_newtypes;
706                             }
707                         }
708
709                         span_bug!(span, "receiver has no non-zero-sized fields {:?}", op);
710                     }
711
712                     // now that we have `*dyn Trait` or `&dyn Trait`, split it up into its
713                     // data pointer and vtable. Look up the method in the vtable, and pass
714                     // the data pointer as the first argument
715                     match op.val {
716                         Pair(data_ptr, meta) => {
717                             llfn = Some(
718                                 meth::VirtualIndex::from_index(idx).get_fn(&mut bx, meta, &fn_abi),
719                             );
720                             llargs.push(data_ptr);
721                             continue 'make_args;
722                         }
723                         other => bug!("expected a Pair, got {:?}", other),
724                     }
725                 } else if let Ref(data_ptr, Some(meta), _) = op.val {
726                     // by-value dynamic dispatch
727                     llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(&mut bx, meta, &fn_abi));
728                     llargs.push(data_ptr);
729                     continue;
730                 } else {
731                     span_bug!(span, "can't codegen a virtual call on {:?}", op);
732                 }
733             }
734
735             // The callee needs to own the argument memory if we pass it
736             // by-ref, so make a local copy of non-immediate constants.
737             match (arg, op.val) {
738                 (&mir::Operand::Copy(_), Ref(_, None, _))
739                 | (&mir::Operand::Constant(_), Ref(_, None, _)) => {
740                     let tmp = PlaceRef::alloca(&mut bx, op.layout);
741                     op.val.store(&mut bx, tmp);
742                     op.val = Ref(tmp.llval, None, tmp.align);
743                 }
744                 _ => {}
745             }
746
747             self.codegen_argument(&mut bx, op, &mut llargs, &fn_abi.args[i]);
748         }
749         if let Some(tup) = untuple {
750             self.codegen_arguments_untupled(
751                 &mut bx,
752                 tup,
753                 &mut llargs,
754                 &fn_abi.args[first_args.len()..],
755             )
756         }
757
758         let needs_location =
759             instance.map_or(false, |i| i.def.requires_caller_location(self.cx.tcx()));
760         if needs_location {
761             assert_eq!(
762                 fn_abi.args.len(),
763                 args.len() + 1,
764                 "#[track_caller] fn's must have 1 more argument in their ABI than in their MIR",
765             );
766             let location =
767                 self.get_caller_location(&mut bx, mir::SourceInfo { span: fn_span, ..source_info });
768             debug!(
769                 "codegen_call_terminator({:?}): location={:?} (fn_span {:?})",
770                 terminator, location, fn_span
771             );
772
773             let last_arg = fn_abi.args.last().unwrap();
774             self.codegen_argument(&mut bx, location, &mut llargs, last_arg);
775         }
776
777         let fn_ptr = match (llfn, instance) {
778             (Some(llfn), _) => llfn,
779             (None, Some(instance)) => bx.get_fn_addr(instance),
780             _ => span_bug!(span, "no llfn for call"),
781         };
782
783         helper.do_call(
784             self,
785             &mut bx,
786             fn_abi,
787             fn_ptr,
788             &llargs,
789             destination.as_ref().map(|&(_, target)| (ret_dest, target)),
790             cleanup,
791         );
792     }
793
794     fn codegen_asm_terminator(
795         &mut self,
796         helper: TerminatorCodegenHelper<'tcx>,
797         mut bx: Bx,
798         terminator: &mir::Terminator<'tcx>,
799         template: &[ast::InlineAsmTemplatePiece],
800         operands: &[mir::InlineAsmOperand<'tcx>],
801         options: ast::InlineAsmOptions,
802         line_spans: &[Span],
803         destination: Option<mir::BasicBlock>,
804     ) {
805         let span = terminator.source_info.span;
806
807         let operands: Vec<_> = operands
808             .iter()
809             .map(|op| match *op {
810                 mir::InlineAsmOperand::In { reg, ref value } => {
811                     let value = self.codegen_operand(&mut bx, value);
812                     InlineAsmOperandRef::In { reg, value }
813                 }
814                 mir::InlineAsmOperand::Out { reg, late, ref place } => {
815                     let place = place.map(|place| self.codegen_place(&mut bx, place.as_ref()));
816                     InlineAsmOperandRef::Out { reg, late, place }
817                 }
818                 mir::InlineAsmOperand::InOut { reg, late, ref in_value, ref out_place } => {
819                     let in_value = self.codegen_operand(&mut bx, in_value);
820                     let out_place =
821                         out_place.map(|out_place| self.codegen_place(&mut bx, out_place.as_ref()));
822                     InlineAsmOperandRef::InOut { reg, late, in_value, out_place }
823                 }
824                 mir::InlineAsmOperand::Const { ref value } => {
825                     let const_value = self
826                         .eval_mir_constant(value)
827                         .unwrap_or_else(|_| span_bug!(span, "asm const cannot be resolved"));
828                     let ty = value.ty();
829                     let size = bx.layout_of(ty).size;
830                     let scalar = match const_value {
831                         ConstValue::Scalar(s) => s,
832                         _ => span_bug!(
833                             span,
834                             "expected Scalar for promoted asm const, but got {:#?}",
835                             const_value
836                         ),
837                     };
838                     let value = scalar.assert_bits(size);
839                     let string = match ty.kind() {
840                         ty::Uint(_) => value.to_string(),
841                         ty::Int(int_ty) => {
842                             match int_ty.normalize(bx.tcx().sess.target.pointer_width) {
843                                 ty::IntTy::I8 => (value as i8).to_string(),
844                                 ty::IntTy::I16 => (value as i16).to_string(),
845                                 ty::IntTy::I32 => (value as i32).to_string(),
846                                 ty::IntTy::I64 => (value as i64).to_string(),
847                                 ty::IntTy::I128 => (value as i128).to_string(),
848                                 ty::IntTy::Isize => unreachable!(),
849                             }
850                         }
851                         ty::Float(ty::FloatTy::F32) => f32::from_bits(value as u32).to_string(),
852                         ty::Float(ty::FloatTy::F64) => f64::from_bits(value as u64).to_string(),
853                         _ => span_bug!(span, "asm const has bad type {}", ty),
854                     };
855                     InlineAsmOperandRef::Const { string }
856                 }
857                 mir::InlineAsmOperand::SymFn { ref value } => {
858                     let literal = self.monomorphize(value.literal);
859                     if let ty::FnDef(def_id, substs) = *literal.ty().kind() {
860                         let instance = ty::Instance::resolve_for_fn_ptr(
861                             bx.tcx(),
862                             ty::ParamEnv::reveal_all(),
863                             def_id,
864                             substs,
865                         )
866                         .unwrap();
867                         InlineAsmOperandRef::SymFn { instance }
868                     } else {
869                         span_bug!(span, "invalid type for asm sym (fn)");
870                     }
871                 }
872                 mir::InlineAsmOperand::SymStatic { def_id } => {
873                     InlineAsmOperandRef::SymStatic { def_id }
874                 }
875             })
876             .collect();
877
878         bx.codegen_inline_asm(template, &operands, options, line_spans);
879
880         if let Some(target) = destination {
881             helper.funclet_br(self, &mut bx, target);
882         } else {
883             bx.unreachable();
884         }
885     }
886 }
887
888 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
889     pub fn codegen_block(&mut self, bb: mir::BasicBlock) {
890         let mut bx = self.build_block(bb);
891         let mir = self.mir;
892         let data = &mir[bb];
893
894         debug!("codegen_block({:?}={:?})", bb, data);
895
896         for statement in &data.statements {
897             bx = self.codegen_statement(bx, statement);
898         }
899
900         self.codegen_terminator(bx, bb, data.terminator());
901     }
902
903     fn codegen_terminator(
904         &mut self,
905         mut bx: Bx,
906         bb: mir::BasicBlock,
907         terminator: &'tcx mir::Terminator<'tcx>,
908     ) {
909         debug!("codegen_terminator: {:?}", terminator);
910
911         // Create the cleanup bundle, if needed.
912         let funclet_bb = self.cleanup_kinds[bb].funclet_bb(bb);
913         let helper = TerminatorCodegenHelper { bb, terminator, funclet_bb };
914
915         self.set_debug_loc(&mut bx, terminator.source_info);
916         match terminator.kind {
917             mir::TerminatorKind::Resume => self.codegen_resume_terminator(helper, bx),
918
919             mir::TerminatorKind::Abort => {
920                 bx.abort();
921                 // `abort` does not terminate the block, so we still need to generate
922                 // an `unreachable` terminator after it.
923                 bx.unreachable();
924             }
925
926             mir::TerminatorKind::Goto { target } => {
927                 if bb == target {
928                     // This is an unconditional branch back to this same basic block. That means we
929                     // have something like a `loop {}` statement. LLVM versions before 12.0
930                     // miscompile this because they assume forward progress. For older versions
931                     // try to handle just this specific case which comes up commonly in practice
932                     // (e.g., in embedded code).
933                     //
934                     // NB: the `sideeffect` currently checks for the LLVM version used internally.
935                     bx.sideeffect();
936                 }
937
938                 helper.funclet_br(self, &mut bx, target);
939             }
940
941             mir::TerminatorKind::SwitchInt { ref discr, switch_ty, ref targets } => {
942                 self.codegen_switchint_terminator(helper, bx, discr, switch_ty, targets);
943             }
944
945             mir::TerminatorKind::Return => {
946                 self.codegen_return_terminator(bx);
947             }
948
949             mir::TerminatorKind::Unreachable => {
950                 bx.unreachable();
951             }
952
953             mir::TerminatorKind::Drop { place, target, unwind } => {
954                 self.codegen_drop_terminator(helper, bx, place, target, unwind);
955             }
956
957             mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, cleanup } => {
958                 self.codegen_assert_terminator(
959                     helper, bx, terminator, cond, expected, msg, target, cleanup,
960                 );
961             }
962
963             mir::TerminatorKind::DropAndReplace { .. } => {
964                 bug!("undesugared DropAndReplace in codegen: {:?}", terminator);
965             }
966
967             mir::TerminatorKind::Call {
968                 ref func,
969                 ref args,
970                 ref destination,
971                 cleanup,
972                 from_hir_call: _,
973                 fn_span,
974             } => {
975                 self.codegen_call_terminator(
976                     helper,
977                     bx,
978                     terminator,
979                     func,
980                     args,
981                     destination,
982                     cleanup,
983                     fn_span,
984                 );
985             }
986             mir::TerminatorKind::GeneratorDrop | mir::TerminatorKind::Yield { .. } => {
987                 bug!("generator ops in codegen")
988             }
989             mir::TerminatorKind::FalseEdge { .. } | mir::TerminatorKind::FalseUnwind { .. } => {
990                 bug!("borrowck false edges in codegen")
991             }
992
993             mir::TerminatorKind::InlineAsm {
994                 template,
995                 ref operands,
996                 options,
997                 line_spans,
998                 destination,
999             } => {
1000                 self.codegen_asm_terminator(
1001                     helper,
1002                     bx,
1003                     terminator,
1004                     template,
1005                     operands,
1006                     options,
1007                     line_spans,
1008                     destination,
1009                 );
1010             }
1011         }
1012     }
1013
1014     fn codegen_argument(
1015         &mut self,
1016         bx: &mut Bx,
1017         op: OperandRef<'tcx, Bx::Value>,
1018         llargs: &mut Vec<Bx::Value>,
1019         arg: &ArgAbi<'tcx, Ty<'tcx>>,
1020     ) {
1021         // Fill padding with undef value, where applicable.
1022         if let Some(ty) = arg.pad {
1023             llargs.push(bx.const_undef(bx.reg_backend_type(&ty)))
1024         }
1025
1026         if arg.is_ignore() {
1027             return;
1028         }
1029
1030         if let PassMode::Pair(..) = arg.mode {
1031             match op.val {
1032                 Pair(a, b) => {
1033                     llargs.push(a);
1034                     llargs.push(b);
1035                     return;
1036                 }
1037                 _ => bug!("codegen_argument: {:?} invalid for pair argument", op),
1038             }
1039         } else if arg.is_unsized_indirect() {
1040             match op.val {
1041                 Ref(a, Some(b), _) => {
1042                     llargs.push(a);
1043                     llargs.push(b);
1044                     return;
1045                 }
1046                 _ => bug!("codegen_argument: {:?} invalid for unsized indirect argument", op),
1047             }
1048         }
1049
1050         // Force by-ref if we have to load through a cast pointer.
1051         let (mut llval, align, by_ref) = match op.val {
1052             Immediate(_) | Pair(..) => match arg.mode {
1053                 PassMode::Indirect { .. } | PassMode::Cast(_) => {
1054                     let scratch = PlaceRef::alloca(bx, arg.layout);
1055                     op.val.store(bx, scratch);
1056                     (scratch.llval, scratch.align, true)
1057                 }
1058                 _ => (op.immediate_or_packed_pair(bx), arg.layout.align.abi, false),
1059             },
1060             Ref(llval, _, align) => {
1061                 if arg.is_indirect() && align < arg.layout.align.abi {
1062                     // `foo(packed.large_field)`. We can't pass the (unaligned) field directly. I
1063                     // think that ATM (Rust 1.16) we only pass temporaries, but we shouldn't
1064                     // have scary latent bugs around.
1065
1066                     let scratch = PlaceRef::alloca(bx, arg.layout);
1067                     base::memcpy_ty(
1068                         bx,
1069                         scratch.llval,
1070                         scratch.align,
1071                         llval,
1072                         align,
1073                         op.layout,
1074                         MemFlags::empty(),
1075                     );
1076                     (scratch.llval, scratch.align, true)
1077                 } else {
1078                     (llval, align, true)
1079                 }
1080             }
1081         };
1082
1083         if by_ref && !arg.is_indirect() {
1084             // Have to load the argument, maybe while casting it.
1085             if let PassMode::Cast(ty) = arg.mode {
1086                 let addr = bx.pointercast(llval, bx.type_ptr_to(bx.cast_backend_type(&ty)));
1087                 llval = bx.load(addr, align.min(arg.layout.align.abi));
1088             } else {
1089                 // We can't use `PlaceRef::load` here because the argument
1090                 // may have a type we don't treat as immediate, but the ABI
1091                 // used for this call is passing it by-value. In that case,
1092                 // the load would just produce `OperandValue::Ref` instead
1093                 // of the `OperandValue::Immediate` we need for the call.
1094                 llval = bx.load(llval, align);
1095                 if let abi::Abi::Scalar(ref scalar) = arg.layout.abi {
1096                     if scalar.is_bool() {
1097                         bx.range_metadata(llval, 0..2);
1098                     }
1099                 }
1100                 // We store bools as `i8` so we need to truncate to `i1`.
1101                 llval = bx.to_immediate(llval, arg.layout);
1102             }
1103         }
1104
1105         llargs.push(llval);
1106     }
1107
1108     fn codegen_arguments_untupled(
1109         &mut self,
1110         bx: &mut Bx,
1111         operand: &mir::Operand<'tcx>,
1112         llargs: &mut Vec<Bx::Value>,
1113         args: &[ArgAbi<'tcx, Ty<'tcx>>],
1114     ) {
1115         let tuple = self.codegen_operand(bx, operand);
1116
1117         // Handle both by-ref and immediate tuples.
1118         if let Ref(llval, None, align) = tuple.val {
1119             let tuple_ptr = PlaceRef::new_sized_aligned(llval, tuple.layout, align);
1120             for i in 0..tuple.layout.fields.count() {
1121                 let field_ptr = tuple_ptr.project_field(bx, i);
1122                 let field = bx.load_operand(field_ptr);
1123                 self.codegen_argument(bx, field, llargs, &args[i]);
1124             }
1125         } else if let Ref(_, Some(_), _) = tuple.val {
1126             bug!("closure arguments must be sized")
1127         } else {
1128             // If the tuple is immediate, the elements are as well.
1129             for i in 0..tuple.layout.fields.count() {
1130                 let op = tuple.extract_field(bx, i);
1131                 self.codegen_argument(bx, op, llargs, &args[i]);
1132             }
1133         }
1134     }
1135
1136     fn get_caller_location(
1137         &mut self,
1138         bx: &mut Bx,
1139         mut source_info: mir::SourceInfo,
1140     ) -> OperandRef<'tcx, Bx::Value> {
1141         let tcx = bx.tcx();
1142
1143         let mut span_to_caller_location = |span: Span| {
1144             let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
1145             let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
1146             let const_loc = tcx.const_caller_location((
1147                 Symbol::intern(&caller.file.name.to_string()),
1148                 caller.line as u32,
1149                 caller.col_display as u32 + 1,
1150             ));
1151             OperandRef::from_const(bx, const_loc, bx.tcx().caller_location_ty())
1152         };
1153
1154         // Walk up the `SourceScope`s, in case some of them are from MIR inlining.
1155         // If so, the starting `source_info.span` is in the innermost inlined
1156         // function, and will be replaced with outer callsite spans as long
1157         // as the inlined functions were `#[track_caller]`.
1158         loop {
1159             let scope_data = &self.mir.source_scopes[source_info.scope];
1160
1161             if let Some((callee, callsite_span)) = scope_data.inlined {
1162                 // Stop inside the most nested non-`#[track_caller]` function,
1163                 // before ever reaching its caller (which is irrelevant).
1164                 if !callee.def.requires_caller_location(tcx) {
1165                     return span_to_caller_location(source_info.span);
1166                 }
1167                 source_info.span = callsite_span;
1168             }
1169
1170             // Skip past all of the parents with `inlined: None`.
1171             match scope_data.inlined_parent_scope {
1172                 Some(parent) => source_info.scope = parent,
1173                 None => break,
1174             }
1175         }
1176
1177         // No inlined `SourceScope`s, or all of them were `#[track_caller]`.
1178         self.caller_location.unwrap_or_else(|| span_to_caller_location(source_info.span))
1179     }
1180
1181     fn get_personality_slot(&mut self, bx: &mut Bx) -> PlaceRef<'tcx, Bx::Value> {
1182         let cx = bx.cx();
1183         if let Some(slot) = self.personality_slot {
1184             slot
1185         } else {
1186             let layout = cx.layout_of(
1187                 cx.tcx().intern_tup(&[cx.tcx().mk_mut_ptr(cx.tcx().types.u8), cx.tcx().types.i32]),
1188             );
1189             let slot = PlaceRef::alloca(bx, layout);
1190             self.personality_slot = Some(slot);
1191             slot
1192         }
1193     }
1194
1195     /// Returns the landing-pad wrapper around the given basic block.
1196     ///
1197     /// No-op in MSVC SEH scheme.
1198     fn landing_pad_to(&mut self, target_bb: mir::BasicBlock) -> Bx::BasicBlock {
1199         if let Some(block) = self.landing_pads[target_bb] {
1200             return block;
1201         }
1202
1203         let block = self.blocks[target_bb];
1204         let landing_pad = self.landing_pad_uncached(block);
1205         self.landing_pads[target_bb] = Some(landing_pad);
1206         landing_pad
1207     }
1208
1209     fn landing_pad_uncached(&mut self, target_bb: Bx::BasicBlock) -> Bx::BasicBlock {
1210         if base::wants_msvc_seh(self.cx.sess()) {
1211             span_bug!(self.mir.span, "landing pad was not inserted?")
1212         }
1213
1214         let mut bx = self.new_block("cleanup");
1215
1216         let llpersonality = self.cx.eh_personality();
1217         let llretty = self.landing_pad_type();
1218         let lp = bx.landing_pad(llretty, llpersonality, 1);
1219         bx.set_cleanup(lp);
1220
1221         let slot = self.get_personality_slot(&mut bx);
1222         slot.storage_live(&mut bx);
1223         Pair(bx.extract_value(lp, 0), bx.extract_value(lp, 1)).store(&mut bx, slot);
1224
1225         bx.br(target_bb);
1226         bx.llbb()
1227     }
1228
1229     fn landing_pad_type(&self) -> Bx::Type {
1230         let cx = self.cx;
1231         cx.type_struct(&[cx.type_i8p(), cx.type_i32()], false)
1232     }
1233
1234     fn unreachable_block(&mut self) -> Bx::BasicBlock {
1235         self.unreachable_block.unwrap_or_else(|| {
1236             let mut bx = self.new_block("unreachable");
1237             bx.unreachable();
1238             self.unreachable_block = Some(bx.llbb());
1239             bx.llbb()
1240         })
1241     }
1242
1243     pub fn new_block(&self, name: &str) -> Bx {
1244         Bx::new_block(self.cx, self.llfn, name)
1245     }
1246
1247     pub fn build_block(&self, bb: mir::BasicBlock) -> Bx {
1248         let mut bx = Bx::with_cx(self.cx);
1249         bx.position_at_end(self.blocks[bb]);
1250         bx
1251     }
1252
1253     fn make_return_dest(
1254         &mut self,
1255         bx: &mut Bx,
1256         dest: mir::Place<'tcx>,
1257         fn_ret: &ArgAbi<'tcx, Ty<'tcx>>,
1258         llargs: &mut Vec<Bx::Value>,
1259         is_intrinsic: bool,
1260     ) -> ReturnDest<'tcx, Bx::Value> {
1261         // If the return is ignored, we can just return a do-nothing `ReturnDest`.
1262         if fn_ret.is_ignore() {
1263             return ReturnDest::Nothing;
1264         }
1265         let dest = if let Some(index) = dest.as_local() {
1266             match self.locals[index] {
1267                 LocalRef::Place(dest) => dest,
1268                 LocalRef::UnsizedPlace(_) => bug!("return type must be sized"),
1269                 LocalRef::Operand(None) => {
1270                     // Handle temporary places, specifically `Operand` ones, as
1271                     // they don't have `alloca`s.
1272                     return if fn_ret.is_indirect() {
1273                         // Odd, but possible, case, we have an operand temporary,
1274                         // but the calling convention has an indirect return.
1275                         let tmp = PlaceRef::alloca(bx, fn_ret.layout);
1276                         tmp.storage_live(bx);
1277                         llargs.push(tmp.llval);
1278                         ReturnDest::IndirectOperand(tmp, index)
1279                     } else if is_intrinsic {
1280                         // Currently, intrinsics always need a location to store
1281                         // the result, so we create a temporary `alloca` for the
1282                         // result.
1283                         let tmp = PlaceRef::alloca(bx, fn_ret.layout);
1284                         tmp.storage_live(bx);
1285                         ReturnDest::IndirectOperand(tmp, index)
1286                     } else {
1287                         ReturnDest::DirectOperand(index)
1288                     };
1289                 }
1290                 LocalRef::Operand(Some(_)) => {
1291                     bug!("place local already assigned to");
1292                 }
1293             }
1294         } else {
1295             self.codegen_place(
1296                 bx,
1297                 mir::PlaceRef { local: dest.local, projection: &dest.projection },
1298             )
1299         };
1300         if fn_ret.is_indirect() {
1301             if dest.align < dest.layout.align.abi {
1302                 // Currently, MIR code generation does not create calls
1303                 // that store directly to fields of packed structs (in
1304                 // fact, the calls it creates write only to temps).
1305                 //
1306                 // If someone changes that, please update this code path
1307                 // to create a temporary.
1308                 span_bug!(self.mir.span, "can't directly store to unaligned value");
1309             }
1310             llargs.push(dest.llval);
1311             ReturnDest::Nothing
1312         } else {
1313             ReturnDest::Store(dest)
1314         }
1315     }
1316
1317     fn codegen_transmute(&mut self, bx: &mut Bx, src: &mir::Operand<'tcx>, dst: mir::Place<'tcx>) {
1318         if let Some(index) = dst.as_local() {
1319             match self.locals[index] {
1320                 LocalRef::Place(place) => self.codegen_transmute_into(bx, src, place),
1321                 LocalRef::UnsizedPlace(_) => bug!("transmute must not involve unsized locals"),
1322                 LocalRef::Operand(None) => {
1323                     let dst_layout = bx.layout_of(self.monomorphized_place_ty(dst.as_ref()));
1324                     assert!(!dst_layout.ty.has_erasable_regions());
1325                     let place = PlaceRef::alloca(bx, dst_layout);
1326                     place.storage_live(bx);
1327                     self.codegen_transmute_into(bx, src, place);
1328                     let op = bx.load_operand(place);
1329                     place.storage_dead(bx);
1330                     self.locals[index] = LocalRef::Operand(Some(op));
1331                     self.debug_introduce_local(bx, index);
1332                 }
1333                 LocalRef::Operand(Some(op)) => {
1334                     assert!(op.layout.is_zst(), "assigning to initialized SSAtemp");
1335                 }
1336             }
1337         } else {
1338             let dst = self.codegen_place(bx, dst.as_ref());
1339             self.codegen_transmute_into(bx, src, dst);
1340         }
1341     }
1342
1343     fn codegen_transmute_into(
1344         &mut self,
1345         bx: &mut Bx,
1346         src: &mir::Operand<'tcx>,
1347         dst: PlaceRef<'tcx, Bx::Value>,
1348     ) {
1349         let src = self.codegen_operand(bx, src);
1350
1351         // Special-case transmutes between scalars as simple bitcasts.
1352         match (&src.layout.abi, &dst.layout.abi) {
1353             (abi::Abi::Scalar(src_scalar), abi::Abi::Scalar(dst_scalar)) => {
1354                 // HACK(eddyb) LLVM doesn't like `bitcast`s between pointers and non-pointers.
1355                 if (src_scalar.value == abi::Pointer) == (dst_scalar.value == abi::Pointer) {
1356                     assert_eq!(src.layout.size, dst.layout.size);
1357
1358                     // NOTE(eddyb) the `from_immediate` and `to_immediate_scalar`
1359                     // conversions allow handling `bool`s the same as `u8`s.
1360                     let src = bx.from_immediate(src.immediate());
1361                     let src_as_dst = bx.bitcast(src, bx.backend_type(dst.layout));
1362                     Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst);
1363                     return;
1364                 }
1365             }
1366             _ => {}
1367         }
1368
1369         let llty = bx.backend_type(src.layout);
1370         let cast_ptr = bx.pointercast(dst.llval, bx.type_ptr_to(llty));
1371         let align = src.layout.align.abi.min(dst.align);
1372         src.val.store(bx, PlaceRef::new_sized_aligned(cast_ptr, src.layout, align));
1373     }
1374
1375     // Stores the return value of a function call into it's final location.
1376     fn store_return(
1377         &mut self,
1378         bx: &mut Bx,
1379         dest: ReturnDest<'tcx, Bx::Value>,
1380         ret_abi: &ArgAbi<'tcx, Ty<'tcx>>,
1381         llval: Bx::Value,
1382     ) {
1383         use self::ReturnDest::*;
1384
1385         match dest {
1386             Nothing => (),
1387             Store(dst) => bx.store_arg(&ret_abi, llval, dst),
1388             IndirectOperand(tmp, index) => {
1389                 let op = bx.load_operand(tmp);
1390                 tmp.storage_dead(bx);
1391                 self.locals[index] = LocalRef::Operand(Some(op));
1392                 self.debug_introduce_local(bx, index);
1393             }
1394             DirectOperand(index) => {
1395                 // If there is a cast, we have to store and reload.
1396                 let op = if let PassMode::Cast(_) = ret_abi.mode {
1397                     let tmp = PlaceRef::alloca(bx, ret_abi.layout);
1398                     tmp.storage_live(bx);
1399                     bx.store_arg(&ret_abi, llval, tmp);
1400                     let op = bx.load_operand(tmp);
1401                     tmp.storage_dead(bx);
1402                     op
1403                 } else {
1404                     OperandRef::from_immediate_or_packed_pair(bx, llval, ret_abi.layout)
1405                 };
1406                 self.locals[index] = LocalRef::Operand(Some(op));
1407                 self.debug_introduce_local(bx, index);
1408             }
1409         }
1410     }
1411 }
1412
1413 enum ReturnDest<'tcx, V> {
1414     // Do nothing; the return value is indirect or ignored.
1415     Nothing,
1416     // Store the return value to the pointer.
1417     Store(PlaceRef<'tcx, V>),
1418     // Store an indirect return value to an operand local place.
1419     IndirectOperand(PlaceRef<'tcx, V>, mir::Local),
1420     // Store a direct return value to an operand local place.
1421     DirectOperand(mir::Local),
1422 }