]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mir/mod.rs
00b4bf96afa594b75e55115647bb84b9410c6179
[rust.git] / src / librustc_codegen_ssa / mir / mod.rs
1 use crate::base;
2 use crate::traits::*;
3 use rustc_errors::ErrorReported;
4 use rustc_middle::mir;
5 use rustc_middle::mir::interpret::ErrorHandled;
6 use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt, TyAndLayout};
7 use rustc_middle::ty::{self, Instance, Ty, TypeFoldable};
8 use rustc_target::abi::call::{FnAbi, PassMode};
9
10 use std::iter;
11
12 use rustc_index::bit_set::BitSet;
13 use rustc_index::vec::IndexVec;
14
15 use self::analyze::CleanupKind;
16 use self::debuginfo::{FunctionDebugContext, PerLocalVarDebugInfo};
17 use self::place::PlaceRef;
18 use rustc_middle::mir::traversal;
19
20 use self::operand::{OperandRef, OperandValue};
21
22 /// Master context for codegenning from MIR.
23 pub struct FunctionCx<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
24     instance: Instance<'tcx>,
25
26     mir: &'tcx mir::Body<'tcx>,
27
28     debug_context: Option<FunctionDebugContext<Bx::DIScope>>,
29
30     llfn: Bx::Function,
31
32     cx: &'a Bx::CodegenCx,
33
34     fn_abi: FnAbi<'tcx, Ty<'tcx>>,
35
36     /// When unwinding is initiated, we have to store this personality
37     /// value somewhere so that we can load it and re-use it in the
38     /// resume instruction. The personality is (afaik) some kind of
39     /// value used for C++ unwinding, which must filter by type: we
40     /// don't really care about it very much. Anyway, this value
41     /// contains an alloca into which the personality is stored and
42     /// then later loaded when generating the DIVERGE_BLOCK.
43     personality_slot: Option<PlaceRef<'tcx, Bx::Value>>,
44
45     /// A `Block` for each MIR `BasicBlock`
46     blocks: IndexVec<mir::BasicBlock, Bx::BasicBlock>,
47
48     /// The funclet status of each basic block
49     cleanup_kinds: IndexVec<mir::BasicBlock, analyze::CleanupKind>,
50
51     /// When targeting MSVC, this stores the cleanup info for each funclet
52     /// BB. This is initialized as we compute the funclets' head block in RPO.
53     funclets: IndexVec<mir::BasicBlock, Option<Bx::Funclet>>,
54
55     /// This stores the landing-pad block for a given BB, computed lazily on GNU
56     /// and eagerly on MSVC.
57     landing_pads: IndexVec<mir::BasicBlock, Option<Bx::BasicBlock>>,
58
59     /// Cached unreachable block
60     unreachable_block: Option<Bx::BasicBlock>,
61
62     /// The location where each MIR arg/var/tmp/ret is stored. This is
63     /// usually an `PlaceRef` representing an alloca, but not always:
64     /// sometimes we can skip the alloca and just store the value
65     /// directly using an `OperandRef`, which makes for tighter LLVM
66     /// IR. The conditions for using an `OperandRef` are as follows:
67     ///
68     /// - the type of the local must be judged "immediate" by `is_llvm_immediate`
69     /// - the operand must never be referenced indirectly
70     ///     - we should not take its address using the `&` operator
71     ///     - nor should it appear in a place path like `tmp.a`
72     /// - the operand must be defined by an rvalue that can generate immediate
73     ///   values
74     ///
75     /// Avoiding allocs can also be important for certain intrinsics,
76     /// notably `expect`.
77     locals: IndexVec<mir::Local, LocalRef<'tcx, Bx::Value>>,
78
79     /// All `VarDebugInfo` from the MIR body, partitioned by `Local`.
80     /// This is `None` if no var`#[non_exhaustive]`iable debuginfo/names are needed.
81     per_local_var_debug_info:
82         Option<IndexVec<mir::Local, Vec<PerLocalVarDebugInfo<'tcx, Bx::DIVariable>>>>,
83
84     /// Caller location propagated if this function has `#[track_caller]`.
85     caller_location: Option<OperandRef<'tcx, Bx::Value>>,
86 }
87
88 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
89     pub fn monomorphize<T>(&self, value: &T) -> T
90     where
91         T: Copy + TypeFoldable<'tcx>,
92     {
93         debug!("monomorphize: self.instance={:?}", self.instance);
94         if let Some(substs) = self.instance.substs_for_mir_body() {
95             self.cx.tcx().subst_and_normalize_erasing_regions(
96                 substs,
97                 ty::ParamEnv::reveal_all(),
98                 &value,
99             )
100         } else {
101             self.cx.tcx().normalize_erasing_regions(ty::ParamEnv::reveal_all(), *value)
102         }
103     }
104 }
105
106 enum LocalRef<'tcx, V> {
107     Place(PlaceRef<'tcx, V>),
108     /// `UnsizedPlace(p)`: `p` itself is a thin pointer (indirect place).
109     /// `*p` is the fat pointer that references the actual unsized place.
110     /// Every time it is initialized, we have to reallocate the place
111     /// and update the fat pointer. That's the reason why it is indirect.
112     UnsizedPlace(PlaceRef<'tcx, V>),
113     Operand(Option<OperandRef<'tcx, V>>),
114 }
115
116 impl<'a, 'tcx, V: CodegenObject> LocalRef<'tcx, V> {
117     fn new_operand<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
118         bx: &mut Bx,
119         layout: TyAndLayout<'tcx>,
120     ) -> LocalRef<'tcx, V> {
121         if layout.is_zst() {
122             // Zero-size temporaries aren't always initialized, which
123             // doesn't matter because they don't contain data, but
124             // we need something in the operand.
125             LocalRef::Operand(Some(OperandRef::new_zst(bx, layout)))
126         } else {
127             LocalRef::Operand(None)
128         }
129     }
130 }
131
132 ///////////////////////////////////////////////////////////////////////////
133
134 pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
135     cx: &'a Bx::CodegenCx,
136     instance: Instance<'tcx>,
137 ) {
138     assert!(!instance.substs.needs_infer());
139
140     let llfn = cx.get_fn(instance);
141
142     let mir = cx.tcx().instance_mir(instance.def);
143
144     let fn_abi = FnAbi::of_instance(cx, instance, &[]);
145     debug!("fn_abi: {:?}", fn_abi);
146
147     let debug_context = cx.create_function_debug_context(instance, &fn_abi, llfn, &mir);
148
149     let mut bx = Bx::new_block(cx, llfn, "start");
150
151     if mir.basic_blocks().iter().any(|bb| bb.is_cleanup) {
152         bx.set_personality_fn(cx.eh_personality());
153     }
154
155     bx.sideeffect();
156
157     let cleanup_kinds = analyze::cleanup_kinds(&mir);
158     // Allocate a `Block` for every basic block, except
159     // the start block, if nothing loops back to it.
160     let reentrant_start_block = !mir.predecessors()[mir::START_BLOCK].is_empty();
161     let block_bxs: IndexVec<mir::BasicBlock, Bx::BasicBlock> = mir
162         .basic_blocks()
163         .indices()
164         .map(|bb| {
165             if bb == mir::START_BLOCK && !reentrant_start_block {
166                 bx.llbb()
167             } else {
168                 bx.build_sibling_block(&format!("{:?}", bb)).llbb()
169             }
170         })
171         .collect();
172
173     let (landing_pads, funclets) = create_funclets(&mir, &mut bx, &cleanup_kinds, &block_bxs);
174     let mut fx = FunctionCx {
175         instance,
176         mir,
177         llfn,
178         fn_abi,
179         cx,
180         personality_slot: None,
181         blocks: block_bxs,
182         unreachable_block: None,
183         cleanup_kinds,
184         landing_pads,
185         funclets,
186         locals: IndexVec::new(),
187         debug_context,
188         per_local_var_debug_info: None,
189         caller_location: None,
190     };
191
192     fx.per_local_var_debug_info = fx.compute_per_local_var_debug_info();
193
194     for const_ in &mir.required_consts {
195         if let Err(err) = fx.eval_mir_constant(const_) {
196             match err {
197                 // errored or at least linted
198                 ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => {}
199                 ErrorHandled::TooGeneric => {
200                     span_bug!(const_.span, "codgen encountered polymorphic constant: {:?}", err)
201                 }
202             }
203         }
204     }
205
206     let memory_locals = analyze::non_ssa_locals(&fx);
207
208     // Allocate variable and temp allocas
209     fx.locals = {
210         let args = arg_local_refs(&mut bx, &mut fx, &memory_locals);
211
212         let mut allocate_local = |local| {
213             let decl = &mir.local_decls[local];
214             let layout = bx.layout_of(fx.monomorphize(&decl.ty));
215             assert!(!layout.ty.has_erasable_regions());
216
217             if local == mir::RETURN_PLACE && fx.fn_abi.ret.is_indirect() {
218                 debug!("alloc: {:?} (return place) -> place", local);
219                 let llretptr = bx.get_param(0);
220                 return LocalRef::Place(PlaceRef::new_sized(llretptr, layout));
221             }
222
223             if memory_locals.contains(local) {
224                 debug!("alloc: {:?} -> place", local);
225                 if layout.is_unsized() {
226                     LocalRef::UnsizedPlace(PlaceRef::alloca_unsized_indirect(&mut bx, layout))
227                 } else {
228                     LocalRef::Place(PlaceRef::alloca(&mut bx, layout))
229                 }
230             } else {
231                 debug!("alloc: {:?} -> operand", local);
232                 LocalRef::new_operand(&mut bx, layout)
233             }
234         };
235
236         let retptr = allocate_local(mir::RETURN_PLACE);
237         iter::once(retptr)
238             .chain(args.into_iter())
239             .chain(mir.vars_and_temps_iter().map(allocate_local))
240             .collect()
241     };
242
243     // Apply debuginfo to the newly allocated locals.
244     fx.debug_introduce_locals(&mut bx);
245
246     // Branch to the START block, if it's not the entry block.
247     if reentrant_start_block {
248         bx.br(fx.blocks[mir::START_BLOCK]);
249     }
250
251     let rpo = traversal::reverse_postorder(&mir);
252     let mut visited = BitSet::new_empty(mir.basic_blocks().len());
253
254     // Codegen the body of each block using reverse postorder
255     for (bb, _) in rpo {
256         visited.insert(bb.index());
257         fx.codegen_block(bb);
258     }
259
260     // Remove blocks that haven't been visited, or have no
261     // predecessors.
262     for bb in mir.basic_blocks().indices() {
263         // Unreachable block
264         if !visited.contains(bb.index()) {
265             debug!("codegen_mir: block {:?} was not visited", bb);
266             unsafe {
267                 bx.delete_basic_block(fx.blocks[bb]);
268             }
269         }
270     }
271 }
272
273 fn create_funclets<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
274     mir: &'tcx mir::Body<'tcx>,
275     bx: &mut Bx,
276     cleanup_kinds: &IndexVec<mir::BasicBlock, CleanupKind>,
277     block_bxs: &IndexVec<mir::BasicBlock, Bx::BasicBlock>,
278 ) -> (
279     IndexVec<mir::BasicBlock, Option<Bx::BasicBlock>>,
280     IndexVec<mir::BasicBlock, Option<Bx::Funclet>>,
281 ) {
282     block_bxs
283         .iter_enumerated()
284         .zip(cleanup_kinds)
285         .map(|((bb, &llbb), cleanup_kind)| {
286             match *cleanup_kind {
287                 CleanupKind::Funclet if base::wants_msvc_seh(bx.sess()) => {}
288                 _ => return (None, None),
289             }
290
291             let funclet;
292             let ret_llbb;
293             match mir[bb].terminator.as_ref().map(|t| &t.kind) {
294                 // This is a basic block that we're aborting the program for,
295                 // notably in an `extern` function. These basic blocks are inserted
296                 // so that we assert that `extern` functions do indeed not panic,
297                 // and if they do we abort the process.
298                 //
299                 // On MSVC these are tricky though (where we're doing funclets). If
300                 // we were to do a cleanuppad (like below) the normal functions like
301                 // `longjmp` would trigger the abort logic, terminating the
302                 // program. Instead we insert the equivalent of `catch(...)` for C++
303                 // which magically doesn't trigger when `longjmp` files over this
304                 // frame.
305                 //
306                 // Lots more discussion can be found on #48251 but this codegen is
307                 // modeled after clang's for:
308                 //
309                 //      try {
310                 //          foo();
311                 //      } catch (...) {
312                 //          bar();
313                 //      }
314                 Some(&mir::TerminatorKind::Abort) => {
315                     let mut cs_bx = bx.build_sibling_block(&format!("cs_funclet{:?}", bb));
316                     let mut cp_bx = bx.build_sibling_block(&format!("cp_funclet{:?}", bb));
317                     ret_llbb = cs_bx.llbb();
318
319                     let cs = cs_bx.catch_switch(None, None, 1);
320                     cs_bx.add_handler(cs, cp_bx.llbb());
321
322                     // The "null" here is actually a RTTI type descriptor for the
323                     // C++ personality function, but `catch (...)` has no type so
324                     // it's null. The 64 here is actually a bitfield which
325                     // represents that this is a catch-all block.
326                     let null = bx.const_null(bx.type_i8p());
327                     let sixty_four = bx.const_i32(64);
328                     funclet = cp_bx.catch_pad(cs, &[null, sixty_four, null]);
329                     cp_bx.br(llbb);
330                 }
331                 _ => {
332                     let mut cleanup_bx = bx.build_sibling_block(&format!("funclet_{:?}", bb));
333                     ret_llbb = cleanup_bx.llbb();
334                     funclet = cleanup_bx.cleanup_pad(None, &[]);
335                     cleanup_bx.br(llbb);
336                 }
337             };
338
339             (Some(ret_llbb), Some(funclet))
340         })
341         .unzip()
342 }
343
344 /// Produces, for each argument, a `Value` pointing at the
345 /// argument's value. As arguments are places, these are always
346 /// indirect.
347 fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
348     bx: &mut Bx,
349     fx: &mut FunctionCx<'a, 'tcx, Bx>,
350     memory_locals: &BitSet<mir::Local>,
351 ) -> Vec<LocalRef<'tcx, Bx::Value>> {
352     let mir = fx.mir;
353     let mut idx = 0;
354     let mut llarg_idx = fx.fn_abi.ret.is_indirect() as usize;
355
356     let args = mir
357         .args_iter()
358         .enumerate()
359         .map(|(arg_index, local)| {
360             let arg_decl = &mir.local_decls[local];
361
362             if Some(local) == mir.spread_arg {
363                 // This argument (e.g., the last argument in the "rust-call" ABI)
364                 // is a tuple that was spread at the ABI level and now we have
365                 // to reconstruct it into a tuple local variable, from multiple
366                 // individual LLVM function arguments.
367
368                 let arg_ty = fx.monomorphize(&arg_decl.ty);
369                 let tupled_arg_tys = match arg_ty.kind {
370                     ty::Tuple(ref tys) => tys,
371                     _ => bug!("spread argument isn't a tuple?!"),
372                 };
373
374                 let place = PlaceRef::alloca(bx, bx.layout_of(arg_ty));
375                 for i in 0..tupled_arg_tys.len() {
376                     let arg = &fx.fn_abi.args[idx];
377                     idx += 1;
378                     if arg.pad.is_some() {
379                         llarg_idx += 1;
380                     }
381                     let pr_field = place.project_field(bx, i);
382                     bx.store_fn_arg(arg, &mut llarg_idx, pr_field);
383                 }
384
385                 return LocalRef::Place(place);
386             }
387
388             if fx.fn_abi.c_variadic && arg_index == fx.fn_abi.args.len() {
389                 let arg_ty = fx.monomorphize(&arg_decl.ty);
390
391                 let va_list = PlaceRef::alloca(bx, bx.layout_of(arg_ty));
392                 bx.va_start(va_list.llval);
393
394                 return LocalRef::Place(va_list);
395             }
396
397             let arg = &fx.fn_abi.args[idx];
398             idx += 1;
399             if arg.pad.is_some() {
400                 llarg_idx += 1;
401             }
402
403             if !memory_locals.contains(local) {
404                 // We don't have to cast or keep the argument in the alloca.
405                 // FIXME(eddyb): We should figure out how to use llvm.dbg.value instead
406                 // of putting everything in allocas just so we can use llvm.dbg.declare.
407                 let local = |op| LocalRef::Operand(Some(op));
408                 match arg.mode {
409                     PassMode::Ignore => {
410                         return local(OperandRef::new_zst(bx, arg.layout));
411                     }
412                     PassMode::Direct(_) => {
413                         let llarg = bx.get_param(llarg_idx);
414                         llarg_idx += 1;
415                         return local(OperandRef::from_immediate_or_packed_pair(
416                             bx, llarg, arg.layout,
417                         ));
418                     }
419                     PassMode::Pair(..) => {
420                         let (a, b) = (bx.get_param(llarg_idx), bx.get_param(llarg_idx + 1));
421                         llarg_idx += 2;
422
423                         return local(OperandRef {
424                             val: OperandValue::Pair(a, b),
425                             layout: arg.layout,
426                         });
427                     }
428                     _ => {}
429                 }
430             }
431
432             if arg.is_sized_indirect() {
433                 // Don't copy an indirect argument to an alloca, the caller
434                 // already put it in a temporary alloca and gave it up.
435                 // FIXME: lifetimes
436                 let llarg = bx.get_param(llarg_idx);
437                 llarg_idx += 1;
438                 LocalRef::Place(PlaceRef::new_sized(llarg, arg.layout))
439             } else if arg.is_unsized_indirect() {
440                 // As the storage for the indirect argument lives during
441                 // the whole function call, we just copy the fat pointer.
442                 let llarg = bx.get_param(llarg_idx);
443                 llarg_idx += 1;
444                 let llextra = bx.get_param(llarg_idx);
445                 llarg_idx += 1;
446                 let indirect_operand = OperandValue::Pair(llarg, llextra);
447
448                 let tmp = PlaceRef::alloca_unsized_indirect(bx, arg.layout);
449                 indirect_operand.store(bx, tmp);
450                 LocalRef::UnsizedPlace(tmp)
451             } else {
452                 let tmp = PlaceRef::alloca(bx, arg.layout);
453                 bx.store_fn_arg(arg, &mut llarg_idx, tmp);
454                 LocalRef::Place(tmp)
455             }
456         })
457         .collect::<Vec<_>>();
458
459     if fx.instance.def.requires_caller_location(bx.tcx()) {
460         assert_eq!(
461             fx.fn_abi.args.len(),
462             args.len() + 1,
463             "#[track_caller] fn's must have 1 more argument in their ABI than in their MIR",
464         );
465
466         let arg = fx.fn_abi.args.last().unwrap();
467         match arg.mode {
468             PassMode::Direct(_) => (),
469             _ => bug!("caller location must be PassMode::Direct, found {:?}", arg.mode),
470         }
471
472         fx.caller_location = Some(OperandRef {
473             val: OperandValue::Immediate(bx.get_param(llarg_idx)),
474             layout: arg.layout,
475         });
476     }
477
478     args
479 }
480
481 mod analyze;
482 mod block;
483 pub mod constant;
484 pub mod debuginfo;
485 pub mod operand;
486 pub mod place;
487 mod rvalue;
488 mod statement;