]>
Commit | Line | Data |
---|---|---|
8bb4bdeb XL |
1 | //! Inlining pass for MIR functions |
2 | ||
6a06907d | 3 | use rustc_attr::InlineAttr; |
29967ef6 | 4 | use rustc_hir as hir; |
e74abb32 | 5 | use rustc_index::bit_set::BitSet; |
29967ef6 | 6 | use rustc_index::vec::Idx; |
1b1a35ee | 7 | use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; |
ba9703b0 XL |
8 | use rustc_middle::mir::visit::*; |
9 | use rustc_middle::mir::*; | |
fc512014 | 10 | use rustc_middle::ty::subst::Subst; |
f9f354fc | 11 | use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt}; |
29967ef6 | 12 | use rustc_span::{hygiene::ExpnKind, ExpnData, Span}; |
ba9703b0 | 13 | use rustc_target::spec::abi::Abi; |
8bb4bdeb | 14 | |
dfeec247 | 15 | use super::simplify::{remove_dead_blocks, CfgSimplifier}; |
29967ef6 | 16 | use crate::transform::MirPass; |
ff7c6d11 | 17 | use std::iter; |
29967ef6 | 18 | use std::ops::{Range, RangeFrom}; |
8bb4bdeb | 19 | |
5869c6ff XL |
20 | crate mod cycle; |
21 | ||
8bb4bdeb XL |
22 | const INSTR_COST: usize = 5; |
23 | const CALL_PENALTY: usize = 25; | |
ba9703b0 XL |
24 | const LANDINGPAD_PENALTY: usize = 50; |
25 | const RESUME_PENALTY: usize = 45; | |
8bb4bdeb XL |
26 | |
27 | const UNKNOWN_SIZE_COST: usize = 10; | |
28 | ||
29 | pub struct Inline; | |
30 | ||
abe05a73 | 31 | #[derive(Copy, Clone, Debug)] |
8bb4bdeb | 32 | struct CallSite<'tcx> { |
29967ef6 | 33 | callee: Instance<'tcx>, |
fc512014 | 34 | fn_sig: ty::PolyFnSig<'tcx>, |
29967ef6 XL |
35 | block: BasicBlock, |
36 | target: Option<BasicBlock>, | |
37 | source_info: SourceInfo, | |
8bb4bdeb XL |
38 | } |
39 | ||
6a06907d XL |
40 | /// Returns true if MIR inlining is enabled in the current compilation session. |
41 | crate fn is_enabled(tcx: TyCtxt<'_>) -> bool { | |
42 | if let Some(enabled) = tcx.sess.opts.debugging_opts.inline_mir { | |
43 | return enabled; | |
44 | } | |
45 | ||
46 | tcx.sess.mir_opt_level() >= 3 | |
47 | } | |
48 | ||
e1599b0c | 49 | impl<'tcx> MirPass<'tcx> for Inline { |
29967ef6 | 50 | fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
6a06907d | 51 | if !is_enabled(tcx) { |
5869c6ff XL |
52 | return; |
53 | } | |
54 | ||
55 | let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id())); | |
56 | let _guard = span.enter(); | |
29967ef6 XL |
57 | if inline(tcx, body) { |
58 | debug!("running simplify cfg on {:?}", body.source); | |
59 | CfgSimplifier::new(body).simplify(); | |
17df50a5 | 60 | remove_dead_blocks(tcx, body); |
7cac9316 XL |
61 | } |
62 | } | |
63 | } | |
8bb4bdeb | 64 | |
29967ef6 XL |
65 | fn inline(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool { |
66 | let def_id = body.source.def_id(); | |
67 | let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); | |
68 | ||
69 | // Only do inlining into fn bodies. | |
70 | if !tcx.hir().body_owner_kind(hir_id).is_fn_or_closure() { | |
71 | return false; | |
72 | } | |
73 | if body.source.promoted.is_some() { | |
74 | return false; | |
75 | } | |
76 | ||
77 | let mut this = Inliner { | |
78 | tcx, | |
79 | param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()), | |
80 | codegen_fn_attrs: tcx.codegen_fn_attrs(body.source.def_id()), | |
81 | hir_id, | |
82 | history: Vec::new(), | |
83 | changed: false, | |
84 | }; | |
85 | let blocks = BasicBlock::new(0)..body.basic_blocks().next_index(); | |
86 | this.process_blocks(body, blocks); | |
87 | this.changed | |
88 | } | |
89 | ||
dc9dc135 XL |
90 | struct Inliner<'tcx> { |
91 | tcx: TyCtxt<'tcx>, | |
29967ef6 XL |
92 | param_env: ParamEnv<'tcx>, |
93 | /// Caller codegen attributes. | |
1b1a35ee | 94 | codegen_fn_attrs: &'tcx CodegenFnAttrs, |
29967ef6 XL |
95 | /// Caller HirID. |
96 | hir_id: hir::HirId, | |
5869c6ff XL |
97 | /// Stack of inlined Instances. |
98 | history: Vec<ty::Instance<'tcx>>, | |
29967ef6 XL |
99 | /// Indicates that the caller body has been modified. |
100 | changed: bool, | |
7cac9316 | 101 | } |
8bb4bdeb | 102 | |
dc9dc135 | 103 | impl Inliner<'tcx> { |
29967ef6 XL |
104 | fn process_blocks(&mut self, caller_body: &mut Body<'tcx>, blocks: Range<BasicBlock>) { |
105 | for bb in blocks { | |
6a06907d XL |
106 | let bb_data = &caller_body[bb]; |
107 | if bb_data.is_cleanup { | |
108 | continue; | |
109 | } | |
110 | ||
111 | let callsite = match self.resolve_callsite(caller_body, bb, bb_data) { | |
29967ef6 XL |
112 | None => continue, |
113 | Some(it) => it, | |
114 | }; | |
6a06907d | 115 | |
5869c6ff XL |
116 | let span = trace_span!("process_blocks", %callsite.callee, ?bb); |
117 | let _guard = span.enter(); | |
118 | ||
6a06907d XL |
119 | match self.try_inlining(caller_body, &callsite) { |
120 | Err(reason) => { | |
121 | debug!("not-inlined {} [{}]", callsite.callee, reason); | |
122 | continue; | |
123 | } | |
124 | Ok(new_blocks) => { | |
125 | debug!("inlined {}", callsite.callee); | |
126 | self.changed = true; | |
127 | self.history.push(callsite.callee); | |
128 | self.process_blocks(caller_body, new_blocks); | |
129 | self.history.pop(); | |
130 | } | |
8bb4bdeb | 131 | } |
6a06907d XL |
132 | } |
133 | } | |
8bb4bdeb | 134 | |
6a06907d XL |
135 | /// Attempts to inline a callsite into the caller body. When successful returns basic blocks |
136 | /// containing the inlined body. Otherwise returns an error describing why inlining didn't take | |
137 | /// place. | |
138 | fn try_inlining( | |
139 | &self, | |
140 | caller_body: &mut Body<'tcx>, | |
141 | callsite: &CallSite<'tcx>, | |
142 | ) -> Result<std::ops::Range<BasicBlock>, &'static str> { | |
143 | let callee_attrs = self.tcx.codegen_fn_attrs(callsite.callee.def_id()); | |
144 | self.check_codegen_attributes(callsite, callee_attrs)?; | |
145 | self.check_mir_is_available(caller_body, &callsite.callee)?; | |
146 | let callee_body = self.tcx.instance_mir(callsite.callee.def); | |
147 | self.check_mir_body(callsite, callee_body, callee_attrs)?; | |
148 | ||
149 | if !self.tcx.consider_optimizing(|| { | |
150 | format!("Inline {:?} into {}", callee_body.span, callsite.callee) | |
151 | }) { | |
152 | return Err("optimization fuel exhausted"); | |
153 | } | |
8bb4bdeb | 154 | |
6a06907d XL |
155 | let callee_body = callsite.callee.subst_mir_and_normalize_erasing_regions( |
156 | self.tcx, | |
157 | self.param_env, | |
158 | callee_body.clone(), | |
159 | ); | |
f9f354fc | 160 | |
6a06907d XL |
161 | let old_blocks = caller_body.basic_blocks().next_index(); |
162 | self.inline_call(caller_body, &callsite, callee_body); | |
163 | let new_blocks = old_blocks..caller_body.basic_blocks().next_index(); | |
8bb4bdeb | 164 | |
6a06907d XL |
165 | Ok(new_blocks) |
166 | } | |
8bb4bdeb | 167 | |
6a06907d XL |
168 | fn check_mir_is_available( |
169 | &self, | |
170 | caller_body: &Body<'tcx>, | |
171 | callee: &Instance<'tcx>, | |
172 | ) -> Result<(), &'static str> { | |
173 | if callee.def_id() == caller_body.source.def_id() { | |
174 | return Err("self-recursion"); | |
29967ef6 | 175 | } |
8bb4bdeb | 176 | |
5869c6ff XL |
177 | match callee.def { |
178 | InstanceDef::Item(_) => { | |
179 | // If there is no MIR available (either because it was not in metadata or | |
180 | // because it has no MIR because it's an extern function), then the inliner | |
181 | // won't cause cycles on this. | |
182 | if !self.tcx.is_mir_available(callee.def_id()) { | |
6a06907d | 183 | return Err("item MIR unavailable"); |
5869c6ff | 184 | } |
8bb4bdeb | 185 | } |
5869c6ff | 186 | // These have no own callable MIR. |
6a06907d XL |
187 | InstanceDef::Intrinsic(_) | InstanceDef::Virtual(..) => { |
188 | return Err("instance without MIR (intrinsic / virtual)"); | |
189 | } | |
5869c6ff XL |
190 | // This cannot result in an immediate cycle since the callee MIR is a shim, which does |
191 | // not get any optimizations run on it. Any subsequent inlining may cause cycles, but we | |
192 | // do not need to catch this here, we can wait until the inliner decides to continue | |
193 | // inlining a second time. | |
194 | InstanceDef::VtableShim(_) | |
195 | | InstanceDef::ReifyShim(_) | |
196 | | InstanceDef::FnPtrShim(..) | |
197 | | InstanceDef::ClosureOnceShim { .. } | |
198 | | InstanceDef::DropGlue(..) | |
6a06907d | 199 | | InstanceDef::CloneShim(..) => return Ok(()), |
5869c6ff XL |
200 | } |
201 | ||
202 | if self.tcx.is_constructor(callee.def_id()) { | |
203 | trace!("constructors always have MIR"); | |
204 | // Constructor functions cannot cause a query cycle. | |
6a06907d | 205 | return Ok(()); |
8bb4bdeb XL |
206 | } |
207 | ||
29967ef6 XL |
208 | if let Some(callee_def_id) = callee.def_id().as_local() { |
209 | let callee_hir_id = self.tcx.hir().local_def_id_to_hir_id(callee_def_id); | |
5869c6ff | 210 | // Avoid inlining into generators, |
29967ef6 XL |
211 | // since their `optimized_mir` is used for layout computation, which can |
212 | // create a cycle, even when no attempt is made to inline the function | |
213 | // in the other direction. | |
6a06907d XL |
214 | if caller_body.generator.is_some() { |
215 | return Err("local generator (query cycle avoidance)"); | |
216 | } | |
217 | ||
218 | // Avoid a cycle here by only using `instance_mir` only if we have | |
219 | // a lower `HirId` than the callee. This ensures that the callee will | |
220 | // not inline us. This trick only works without incremental compilation. | |
221 | // So don't do it if that is enabled. | |
222 | if !self.tcx.dep_graph.is_fully_enabled() && self.hir_id < callee_hir_id { | |
223 | return Ok(()); | |
224 | } | |
225 | ||
226 | // If we know for sure that the function we're calling will itself try to | |
227 | // call us, then we avoid inlining that function. | |
228 | if self | |
229 | .tcx | |
230 | .mir_callgraph_reachable((*callee, caller_body.source.def_id().expect_local())) | |
231 | { | |
232 | return Err("caller might be reachable from callee (query cycle avoidance)"); | |
233 | } | |
234 | ||
235 | Ok(()) | |
29967ef6 | 236 | } else { |
5869c6ff XL |
237 | // This cannot result in an immediate cycle since the callee MIR is from another crate |
238 | // and is already optimized. Any subsequent inlining may cause cycles, but we do | |
239 | // not need to catch this here, we can wait until the inliner decides to continue | |
240 | // inlining a second time. | |
241 | trace!("functions from other crates always have MIR"); | |
6a06907d | 242 | Ok(()) |
8bb4bdeb | 243 | } |
8bb4bdeb XL |
244 | } |
245 | ||
6a06907d | 246 | fn resolve_callsite( |
dfeec247 | 247 | &self, |
6a06907d | 248 | caller_body: &Body<'tcx>, |
dfeec247 XL |
249 | bb: BasicBlock, |
250 | bb_data: &BasicBlockData<'tcx>, | |
a1dfa0c6 | 251 | ) -> Option<CallSite<'tcx>> { |
a1dfa0c6 XL |
252 | // Only consider direct calls to functions |
253 | let terminator = bb_data.terminator(); | |
fc512014 XL |
254 | if let TerminatorKind::Call { ref func, ref destination, .. } = terminator.kind { |
255 | let func_ty = func.ty(caller_body, self.tcx); | |
256 | if let ty::FnDef(def_id, substs) = *func_ty.kind() { | |
257 | // To resolve an instance its substs have to be fully normalized. | |
258 | let substs = self.tcx.normalize_erasing_regions(self.param_env, substs); | |
29967ef6 | 259 | let callee = |
fc512014 | 260 | Instance::resolve(self.tcx, self.param_env, def_id, substs).ok().flatten()?; |
29967ef6 XL |
261 | |
262 | if let InstanceDef::Virtual(..) | InstanceDef::Intrinsic(_) = callee.def { | |
a1dfa0c6 XL |
263 | return None; |
264 | } | |
265 | ||
fc512014 XL |
266 | let fn_sig = self.tcx.fn_sig(def_id).subst(self.tcx, substs); |
267 | ||
a1dfa0c6 | 268 | return Some(CallSite { |
29967ef6 | 269 | callee, |
fc512014 | 270 | fn_sig, |
29967ef6 XL |
271 | block: bb, |
272 | target: destination.map(|(_, target)| target), | |
273 | source_info: terminator.source_info, | |
a1dfa0c6 XL |
274 | }); |
275 | } | |
276 | } | |
277 | ||
278 | None | |
279 | } | |
280 | ||
6a06907d XL |
281 | /// Returns an error if inlining is not possible based on codegen attributes alone. A success |
282 | /// indicates that inlining decision should be based on other criteria. | |
283 | fn check_codegen_attributes( | |
284 | &self, | |
285 | callsite: &CallSite<'tcx>, | |
286 | callee_attrs: &CodegenFnAttrs, | |
136023e0 | 287 | ) -> Result<(), &'static str> { |
6a06907d XL |
288 | if let InlineAttr::Never = callee_attrs.inline { |
289 | return Err("never inline hint"); | |
290 | } | |
8bb4bdeb | 291 | |
6a06907d XL |
292 | // Only inline local functions if they would be eligible for cross-crate |
293 | // inlining. This is to ensure that the final crate doesn't have MIR that | |
294 | // reference unexported symbols | |
295 | if callsite.callee.def_id().is_local() { | |
296 | let is_generic = callsite.callee.substs.non_erasable_generics().next().is_some(); | |
297 | if !is_generic && !callee_attrs.requests_inline() { | |
298 | return Err("not exported"); | |
299 | } | |
ea8adc8c | 300 | } |
8bb4bdeb | 301 | |
6a06907d XL |
302 | if callsite.fn_sig.c_variadic() { |
303 | return Err("C variadic"); | |
304 | } | |
60c5eb7d | 305 | |
6a06907d XL |
306 | if callee_attrs.flags.contains(CodegenFnAttrFlags::NAKED) { |
307 | return Err("naked"); | |
1b1a35ee XL |
308 | } |
309 | ||
6a06907d XL |
310 | if callee_attrs.flags.contains(CodegenFnAttrFlags::COLD) { |
311 | return Err("cold"); | |
74b04a01 XL |
312 | } |
313 | ||
6a06907d XL |
314 | if callee_attrs.no_sanitize != self.codegen_fn_attrs.no_sanitize { |
315 | return Err("incompatible sanitizer set"); | |
5869c6ff XL |
316 | } |
317 | ||
6a06907d XL |
318 | if callee_attrs.instruction_set != self.codegen_fn_attrs.instruction_set { |
319 | return Err("incompatible instruction set"); | |
320 | } | |
8bb4bdeb | 321 | |
6a06907d XL |
322 | for feature in &callee_attrs.target_features { |
323 | if !self.codegen_fn_attrs.target_features.contains(feature) { | |
324 | return Err("incompatible target feature"); | |
8bb4bdeb XL |
325 | } |
326 | } | |
327 | ||
6a06907d XL |
328 | Ok(()) |
329 | } | |
8bb4bdeb | 330 | |
6a06907d XL |
331 | /// Returns inlining decision that is based on the examination of callee MIR body. |
332 | /// Assumes that codegen attributes have been checked for compatibility already. | |
333 | #[instrument(level = "debug", skip(self, callee_body))] | |
334 | fn check_mir_body( | |
335 | &self, | |
336 | callsite: &CallSite<'tcx>, | |
337 | callee_body: &Body<'tcx>, | |
338 | callee_attrs: &CodegenFnAttrs, | |
339 | ) -> Result<(), &'static str> { | |
340 | let tcx = self.tcx; | |
fc512014 | 341 | |
6a06907d XL |
342 | let mut threshold = if callee_attrs.requests_inline() { |
343 | self.tcx.sess.opts.debugging_opts.inline_mir_hint_threshold.unwrap_or(100) | |
344 | } else { | |
345 | self.tcx.sess.opts.debugging_opts.inline_mir_threshold.unwrap_or(50) | |
346 | }; | |
8bb4bdeb XL |
347 | |
348 | // Give a bonus functions with a small number of blocks, | |
349 | // We normally have two or three blocks for even | |
350 | // very small functions. | |
dc9dc135 | 351 | if callee_body.basic_blocks().len() <= 3 { |
8bb4bdeb XL |
352 | threshold += threshold / 4; |
353 | } | |
abe05a73 | 354 | debug!(" final inline threshold = {}", threshold); |
8bb4bdeb XL |
355 | |
356 | // FIXME: Give a bonus to functions with only a single caller | |
8bb4bdeb XL |
357 | let mut first_block = true; |
358 | let mut cost = 0; | |
359 | ||
360 | // Traverse the MIR manually so we can account for the effects of | |
361 | // inlining on the CFG. | |
362 | let mut work_list = vec![START_BLOCK]; | |
dc9dc135 | 363 | let mut visited = BitSet::new_empty(callee_body.basic_blocks().len()); |
8bb4bdeb | 364 | while let Some(bb) = work_list.pop() { |
dfeec247 XL |
365 | if !visited.insert(bb.index()) { |
366 | continue; | |
367 | } | |
dc9dc135 | 368 | let blk = &callee_body.basic_blocks()[bb]; |
8bb4bdeb XL |
369 | |
370 | for stmt in &blk.statements { | |
371 | // Don't count StorageLive/StorageDead in the inlining cost. | |
372 | match stmt.kind { | |
dfeec247 XL |
373 | StatementKind::StorageLive(_) |
374 | | StatementKind::StorageDead(_) | |
375 | | StatementKind::Nop => {} | |
376 | _ => cost += INSTR_COST, | |
8bb4bdeb XL |
377 | } |
378 | } | |
379 | let term = blk.terminator(); | |
380 | let mut is_drop = false; | |
381 | match term.kind { | |
f035d41b XL |
382 | TerminatorKind::Drop { ref place, target, unwind } |
383 | | TerminatorKind::DropAndReplace { ref place, target, unwind, .. } => { | |
8bb4bdeb XL |
384 | is_drop = true; |
385 | work_list.push(target); | |
f035d41b | 386 | // If the place doesn't actually need dropping, treat it like |
8bb4bdeb | 387 | // a regular goto. |
29967ef6 XL |
388 | let ty = callsite.callee.subst_mir(self.tcx, &place.ty(callee_body, tcx).ty); |
389 | if ty.needs_drop(tcx, self.param_env) { | |
8bb4bdeb XL |
390 | cost += CALL_PENALTY; |
391 | if let Some(unwind) = unwind { | |
ba9703b0 | 392 | cost += LANDINGPAD_PENALTY; |
8bb4bdeb XL |
393 | work_list.push(unwind); |
394 | } | |
395 | } else { | |
396 | cost += INSTR_COST; | |
397 | } | |
398 | } | |
399 | ||
dfeec247 XL |
400 | TerminatorKind::Unreachable | TerminatorKind::Call { destination: None, .. } |
401 | if first_block => | |
402 | { | |
8bb4bdeb XL |
403 | // If the function always diverges, don't inline |
404 | // unless the cost is zero | |
405 | threshold = 0; | |
406 | } | |
407 | ||
ba9703b0 | 408 | TerminatorKind::Call { func: Operand::Constant(ref f), cleanup, .. } => { |
29967ef6 | 409 | if let ty::FnDef(def_id, substs) = |
6a06907d | 410 | *callsite.callee.subst_mir(self.tcx, &f.literal.ty()).kind() |
29967ef6 XL |
411 | { |
412 | let substs = self.tcx.normalize_erasing_regions(self.param_env, substs); | |
413 | if let Ok(Some(instance)) = | |
414 | Instance::resolve(self.tcx, self.param_env, def_id, substs) | |
415 | { | |
6a06907d XL |
416 | if callsite.callee.def_id() == instance.def_id() { |
417 | return Err("self-recursion"); | |
418 | } else if self.history.contains(&instance) { | |
419 | return Err("already inlined"); | |
29967ef6 XL |
420 | } |
421 | } | |
8bb4bdeb | 422 | // Don't give intrinsics the extra penalty for calls |
041b39d2 | 423 | let f = tcx.fn_sig(def_id); |
8bb4bdeb XL |
424 | if f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic { |
425 | cost += INSTR_COST; | |
426 | } else { | |
427 | cost += CALL_PENALTY; | |
428 | } | |
ba9703b0 XL |
429 | } else { |
430 | cost += CALL_PENALTY; | |
431 | } | |
432 | if cleanup.is_some() { | |
433 | cost += LANDINGPAD_PENALTY; | |
434 | } | |
435 | } | |
436 | TerminatorKind::Assert { cleanup, .. } => { | |
437 | cost += CALL_PENALTY; | |
438 | ||
439 | if cleanup.is_some() { | |
440 | cost += LANDINGPAD_PENALTY; | |
8bb4bdeb XL |
441 | } |
442 | } | |
ba9703b0 | 443 | TerminatorKind::Resume => cost += RESUME_PENALTY, |
dfeec247 | 444 | _ => cost += INSTR_COST, |
8bb4bdeb XL |
445 | } |
446 | ||
447 | if !is_drop { | |
83c7162d | 448 | for &succ in term.successors() { |
8bb4bdeb XL |
449 | work_list.push(succ); |
450 | } | |
451 | } | |
452 | ||
453 | first_block = false; | |
454 | } | |
455 | ||
456 | // Count up the cost of local variables and temps, if we know the size | |
457 | // use that, otherwise we use a moderately-large dummy cost. | |
458 | ||
459 | let ptr_size = tcx.data_layout.pointer_size.bytes(); | |
460 | ||
dc9dc135 | 461 | for v in callee_body.vars_and_temps_iter() { |
29967ef6 | 462 | let ty = callsite.callee.subst_mir(self.tcx, &callee_body.local_decls[v].ty); |
8bb4bdeb XL |
463 | // Cost of the var is the size in machine-words, if we know |
464 | // it. | |
29967ef6 | 465 | if let Some(size) = type_size_of(tcx, self.param_env, ty) { |
5869c6ff | 466 | cost += ((size + ptr_size - 1) / ptr_size) as usize; |
8bb4bdeb XL |
467 | } else { |
468 | cost += UNKNOWN_SIZE_COST; | |
469 | } | |
470 | } | |
471 | ||
6a06907d | 472 | if let InlineAttr::Always = callee_attrs.inline { |
abe05a73 | 473 | debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost); |
6a06907d | 474 | Ok(()) |
8bb4bdeb | 475 | } else { |
abe05a73 XL |
476 | if cost <= threshold { |
477 | debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold); | |
6a06907d | 478 | Ok(()) |
abe05a73 XL |
479 | } else { |
480 | debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold); | |
6a06907d | 481 | Err("cost above threshold") |
abe05a73 | 482 | } |
8bb4bdeb XL |
483 | } |
484 | } | |
485 | ||
dfeec247 XL |
486 | fn inline_call( |
487 | &self, | |
f9f354fc | 488 | caller_body: &mut Body<'tcx>, |
6a06907d | 489 | callsite: &CallSite<'tcx>, |
f9f354fc | 490 | mut callee_body: Body<'tcx>, |
29967ef6 XL |
491 | ) { |
492 | let terminator = caller_body[callsite.block].terminator.take().unwrap(); | |
8bb4bdeb | 493 | match terminator.kind { |
29967ef6 | 494 | TerminatorKind::Call { args, destination, cleanup, .. } => { |
8bb4bdeb XL |
495 | // If the call is something like `a[*i] = f(i)`, where |
496 | // `i : &mut usize`, then just duplicating the `a[*i]` | |
ff7c6d11 | 497 | // Place could result in two different locations if `f` |
8bb4bdeb | 498 | // writes to `i`. To prevent this we need to create a temporary |
ff7c6d11 | 499 | // borrow of the place and pass the destination as `*temp` instead. |
ba9703b0 | 500 | fn dest_needs_borrow(place: Place<'_>) -> bool { |
e1599b0c XL |
501 | for elem in place.projection.iter() { |
502 | match elem { | |
dfeec247 | 503 | ProjectionElem::Deref | ProjectionElem::Index(_) => return true, |
e1599b0c | 504 | _ => {} |
8bb4bdeb | 505 | } |
e1599b0c | 506 | } |
dc9dc135 | 507 | |
dfeec247 | 508 | false |
8bb4bdeb XL |
509 | } |
510 | ||
29967ef6 XL |
511 | let dest = if let Some((destination_place, _)) = destination { |
512 | if dest_needs_borrow(destination_place) { | |
513 | trace!("creating temp for return destination"); | |
514 | let dest = Rvalue::Ref( | |
515 | self.tcx.lifetimes.re_erased, | |
516 | BorrowKind::Mut { allow_two_phase_borrow: false }, | |
517 | destination_place, | |
518 | ); | |
519 | let dest_ty = dest.ty(caller_body, self.tcx); | |
520 | let temp = Place::from(self.new_call_temp(caller_body, &callsite, dest_ty)); | |
521 | caller_body[callsite.block].statements.push(Statement { | |
522 | source_info: callsite.source_info, | |
94222f64 | 523 | kind: StatementKind::Assign(Box::new((temp, dest))), |
29967ef6 XL |
524 | }); |
525 | self.tcx.mk_place_deref(temp) | |
526 | } else { | |
527 | destination_place | |
528 | } | |
8bb4bdeb | 529 | } else { |
29967ef6 XL |
530 | trace!("creating temp for return place"); |
531 | Place::from(self.new_call_temp(caller_body, &callsite, callee_body.return_ty())) | |
8bb4bdeb XL |
532 | }; |
533 | ||
83c7162d | 534 | // Copy the arguments if needed. |
fc512014 | 535 | let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, &callee_body); |
8bb4bdeb | 536 | |
8bb4bdeb | 537 | let mut integrator = Integrator { |
8bb4bdeb | 538 | args: &args, |
29967ef6 XL |
539 | new_locals: Local::new(caller_body.local_decls.len()).., |
540 | new_scopes: SourceScope::new(caller_body.source_scopes.len()).., | |
541 | new_blocks: BasicBlock::new(caller_body.basic_blocks().len()).., | |
8bb4bdeb | 542 | destination: dest, |
29967ef6 | 543 | return_block: callsite.target, |
8bb4bdeb | 544 | cleanup_block: cleanup, |
e1599b0c | 545 | in_cleanup_block: false, |
e74abb32 | 546 | tcx: self.tcx, |
29967ef6 XL |
547 | callsite_span: callsite.source_info.span, |
548 | body_span: callee_body.span, | |
fc512014 | 549 | always_live_locals: BitSet::new_filled(callee_body.local_decls.len()), |
8bb4bdeb XL |
550 | }; |
551 | ||
29967ef6 XL |
552 | // Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones |
553 | // (or existing ones, in a few special cases) in the caller. | |
554 | integrator.visit_body(&mut callee_body); | |
8bb4bdeb | 555 | |
29967ef6 XL |
556 | for scope in &mut callee_body.source_scopes { |
557 | // FIXME(eddyb) move this into a `fn visit_scope_data` in `Integrator`. | |
558 | if scope.parent_scope.is_none() { | |
559 | let callsite_scope = &caller_body.source_scopes[callsite.source_info.scope]; | |
560 | ||
561 | // Attach the outermost callee scope as a child of the callsite | |
562 | // scope, via the `parent_scope` and `inlined_parent_scope` chains. | |
563 | scope.parent_scope = Some(callsite.source_info.scope); | |
564 | assert_eq!(scope.inlined_parent_scope, None); | |
565 | scope.inlined_parent_scope = if callsite_scope.inlined.is_some() { | |
566 | Some(callsite.source_info.scope) | |
567 | } else { | |
568 | callsite_scope.inlined_parent_scope | |
569 | }; | |
570 | ||
571 | // Mark the outermost callee scope as an inlined one. | |
572 | assert_eq!(scope.inlined, None); | |
573 | scope.inlined = Some((callsite.callee, callsite.source_info.span)); | |
574 | } else if scope.inlined_parent_scope.is_none() { | |
575 | // Make it easy to find the scope with `inlined` set above. | |
576 | scope.inlined_parent_scope = | |
577 | Some(integrator.map_scope(OUTERMOST_SOURCE_SCOPE)); | |
578 | } | |
8bb4bdeb XL |
579 | } |
580 | ||
fc512014 XL |
581 | // If there are any locals without storage markers, give them storage only for the |
582 | // duration of the call. | |
583 | for local in callee_body.vars_and_temps_iter() { | |
584 | if integrator.always_live_locals.contains(local) { | |
585 | let new_local = integrator.map_local(local); | |
586 | caller_body[callsite.block].statements.push(Statement { | |
587 | source_info: callsite.source_info, | |
588 | kind: StatementKind::StorageLive(new_local), | |
589 | }); | |
590 | } | |
591 | } | |
592 | if let Some(block) = callsite.target { | |
593 | // To avoid repeated O(n) insert, push any new statements to the end and rotate | |
594 | // the slice once. | |
595 | let mut n = 0; | |
596 | for local in callee_body.vars_and_temps_iter().rev() { | |
597 | if integrator.always_live_locals.contains(local) { | |
598 | let new_local = integrator.map_local(local); | |
599 | caller_body[block].statements.push(Statement { | |
600 | source_info: callsite.source_info, | |
601 | kind: StatementKind::StorageDead(new_local), | |
602 | }); | |
603 | n += 1; | |
604 | } | |
605 | } | |
606 | caller_body[block].statements.rotate_right(n); | |
607 | } | |
608 | ||
29967ef6 | 609 | // Insert all of the (mapped) parts of the callee body into the caller. |
94222f64 XL |
610 | caller_body.local_decls.extend(callee_body.drain_vars_and_temps()); |
611 | caller_body.source_scopes.extend(&mut callee_body.source_scopes.drain(..)); | |
612 | caller_body.var_debug_info.append(&mut callee_body.var_debug_info); | |
29967ef6 | 613 | caller_body.basic_blocks_mut().extend(callee_body.basic_blocks_mut().drain(..)); |
8bb4bdeb | 614 | |
29967ef6 XL |
615 | caller_body[callsite.block].terminator = Some(Terminator { |
616 | source_info: callsite.source_info, | |
617 | kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) }, | |
618 | }); | |
8bb4bdeb | 619 | |
29967ef6 XL |
620 | // Copy only unevaluated constants from the callee_body into the caller_body. |
621 | // Although we are only pushing `ConstKind::Unevaluated` consts to | |
622 | // `required_consts`, here we may not only have `ConstKind::Unevaluated` | |
623 | // because we are calling `subst_and_normalize_erasing_regions`. | |
624 | caller_body.required_consts.extend( | |
6a06907d XL |
625 | callee_body.required_consts.iter().copied().filter(|&ct| { |
626 | match ct.literal.const_for_ty() { | |
cdc7bbd5 | 627 | Some(ct) => matches!(ct.val, ConstKind::Unevaluated(_)), |
6a06907d XL |
628 | None => true, |
629 | } | |
29967ef6 XL |
630 | }), |
631 | ); | |
8bb4bdeb | 632 | } |
29967ef6 | 633 | kind => bug!("unexpected terminator kind {:?}", kind), |
8bb4bdeb XL |
634 | } |
635 | } | |
636 | ||
abe05a73 XL |
637 | fn make_call_args( |
638 | &self, | |
639 | args: Vec<Operand<'tcx>>, | |
640 | callsite: &CallSite<'tcx>, | |
f9f354fc | 641 | caller_body: &mut Body<'tcx>, |
fc512014 | 642 | callee_body: &Body<'tcx>, |
ff7c6d11 | 643 | ) -> Vec<Local> { |
8bb4bdeb | 644 | let tcx = self.tcx; |
abe05a73 | 645 | |
ff7c6d11 XL |
646 | // There is a bit of a mismatch between the *caller* of a closure and the *callee*. |
647 | // The caller provides the arguments wrapped up in a tuple: | |
648 | // | |
649 | // tuple_tmp = (a, b, c) | |
650 | // Fn::call(closure_ref, tuple_tmp) | |
651 | // | |
652 | // meanwhile the closure body expects the arguments (here, `a`, `b`, and `c`) | |
94b46f34 XL |
653 | // as distinct arguments. (This is the "rust-call" ABI hack.) Normally, codegen has |
654 | // the job of unpacking this tuple. But here, we are codegen. =) So we want to create | |
ff7c6d11 XL |
655 | // a vector like |
656 | // | |
657 | // [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2] | |
658 | // | |
659 | // Except for one tiny wrinkle: we don't actually want `tuple_tmp.0`. It's more convenient | |
660 | // if we "spill" that into *another* temporary, so that we can map the argument | |
661 | // variable in the callee MIR directly to an argument variable on our side. | |
662 | // So we introduce temporaries like: | |
663 | // | |
664 | // tmp0 = tuple_tmp.0 | |
665 | // tmp1 = tuple_tmp.1 | |
666 | // tmp2 = tuple_tmp.2 | |
667 | // | |
668 | // and the vector is `[closure_ref, tmp0, tmp1, tmp2]`. | |
fc512014 | 669 | if callsite.fn_sig.abi() == Abi::RustCall && callee_body.spread_arg.is_none() { |
abe05a73 | 670 | let mut args = args.into_iter(); |
29967ef6 XL |
671 | let self_ = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body); |
672 | let tuple = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body); | |
abe05a73 XL |
673 | assert!(args.next().is_none()); |
674 | ||
dc9dc135 | 675 | let tuple = Place::from(tuple); |
1b1a35ee | 676 | let tuple_tys = if let ty::Tuple(s) = tuple.ty(caller_body, tcx).ty.kind() { |
abe05a73 XL |
677 | s |
678 | } else { | |
679 | bug!("Closure arguments are not passed as a tuple"); | |
680 | }; | |
681 | ||
ff7c6d11 XL |
682 | // The `closure_ref` in our example above. |
683 | let closure_ref_arg = iter::once(self_); | |
684 | ||
685 | // The `tmp0`, `tmp1`, and `tmp2` in our example abonve. | |
dfeec247 XL |
686 | let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| { |
687 | // This is e.g., `tuple_tmp.0` in our example above. | |
688 | let tuple_field = | |
ba9703b0 | 689 | Operand::Move(tcx.mk_place_field(tuple, Field::new(i), ty.expect_ty())); |
dfeec247 XL |
690 | |
691 | // Spill to a local to make e.g., `tmp0`. | |
29967ef6 | 692 | self.create_temp_if_necessary(tuple_field, callsite, caller_body) |
dfeec247 | 693 | }); |
ff7c6d11 XL |
694 | |
695 | closure_ref_arg.chain(tuple_tmp_args).collect() | |
abe05a73 XL |
696 | } else { |
697 | args.into_iter() | |
29967ef6 | 698 | .map(|a| self.create_temp_if_necessary(a, callsite, caller_body)) |
abe05a73 XL |
699 | .collect() |
700 | } | |
701 | } | |
702 | ||
703 | /// If `arg` is already a temporary, returns it. Otherwise, introduces a fresh | |
704 | /// temporary `T` and an instruction `T = arg`, and returns `T`. | |
705 | fn create_temp_if_necessary( | |
706 | &self, | |
707 | arg: Operand<'tcx>, | |
708 | callsite: &CallSite<'tcx>, | |
f9f354fc | 709 | caller_body: &mut Body<'tcx>, |
ff7c6d11 | 710 | ) -> Local { |
29967ef6 | 711 | // Reuse the operand if it is a moved temporary. |
e74abb32 XL |
712 | if let Operand::Move(place) = &arg { |
713 | if let Some(local) = place.as_local() { | |
714 | if caller_body.local_kind(local) == LocalKind::Temp { | |
e74abb32 XL |
715 | return local; |
716 | } | |
8bb4bdeb | 717 | } |
abe05a73 | 718 | } |
8bb4bdeb | 719 | |
29967ef6 XL |
720 | // Otherwise, create a temporary for the argument. |
721 | trace!("creating temp for argument {:?}", arg); | |
722 | let arg_ty = arg.ty(caller_body, self.tcx); | |
723 | let local = self.new_call_temp(caller_body, callsite, arg_ty); | |
724 | caller_body[callsite.block].statements.push(Statement { | |
725 | source_info: callsite.source_info, | |
94222f64 | 726 | kind: StatementKind::Assign(Box::new((Place::from(local), Rvalue::Use(arg)))), |
29967ef6 XL |
727 | }); |
728 | local | |
729 | } | |
8bb4bdeb | 730 | |
29967ef6 XL |
731 | /// Introduces a new temporary into the caller body that is live for the duration of the call. |
732 | fn new_call_temp( | |
733 | &self, | |
734 | caller_body: &mut Body<'tcx>, | |
735 | callsite: &CallSite<'tcx>, | |
736 | ty: Ty<'tcx>, | |
737 | ) -> Local { | |
738 | let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span)); | |
8bb4bdeb | 739 | |
29967ef6 XL |
740 | caller_body[callsite.block].statements.push(Statement { |
741 | source_info: callsite.source_info, | |
742 | kind: StatementKind::StorageLive(local), | |
1b1a35ee | 743 | }); |
1b1a35ee | 744 | |
29967ef6 XL |
745 | if let Some(block) = callsite.target { |
746 | caller_body[block].statements.insert( | |
747 | 0, | |
748 | Statement { | |
749 | source_info: callsite.source_info, | |
750 | kind: StatementKind::StorageDead(local), | |
751 | }, | |
752 | ); | |
753 | } | |
754 | ||
755 | local | |
8bb4bdeb XL |
756 | } |
757 | } | |
758 | ||
dc9dc135 XL |
759 | fn type_size_of<'tcx>( |
760 | tcx: TyCtxt<'tcx>, | |
761 | param_env: ty::ParamEnv<'tcx>, | |
762 | ty: Ty<'tcx>, | |
763 | ) -> Option<u64> { | |
2c00a5a8 | 764 | tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes()) |
8bb4bdeb XL |
765 | } |
766 | ||
767 | /** | |
768 | * Integrator. | |
769 | * | |
770 | * Integrates blocks from the callee function into the calling function. | |
771 | * Updates block indices, references to locals and other control flow | |
772 | * stuff. | |
dc9dc135 XL |
773 | */ |
774 | struct Integrator<'a, 'tcx> { | |
ff7c6d11 | 775 | args: &'a [Local], |
29967ef6 XL |
776 | new_locals: RangeFrom<Local>, |
777 | new_scopes: RangeFrom<SourceScope>, | |
778 | new_blocks: RangeFrom<BasicBlock>, | |
ff7c6d11 | 779 | destination: Place<'tcx>, |
29967ef6 | 780 | return_block: Option<BasicBlock>, |
8bb4bdeb XL |
781 | cleanup_block: Option<BasicBlock>, |
782 | in_cleanup_block: bool, | |
e74abb32 | 783 | tcx: TyCtxt<'tcx>, |
29967ef6 XL |
784 | callsite_span: Span, |
785 | body_span: Span, | |
fc512014 | 786 | always_live_locals: BitSet<Local>, |
8bb4bdeb XL |
787 | } |
788 | ||
789 | impl<'a, 'tcx> Integrator<'a, 'tcx> { | |
29967ef6 XL |
790 | fn map_local(&self, local: Local) -> Local { |
791 | let new = if local == RETURN_PLACE { | |
792 | self.destination.local | |
793 | } else { | |
794 | let idx = local.index() - 1; | |
795 | if idx < self.args.len() { | |
796 | self.args[idx] | |
797 | } else { | |
798 | Local::new(self.new_locals.start.index() + (idx - self.args.len())) | |
799 | } | |
800 | }; | |
801 | trace!("mapping local `{:?}` to `{:?}`", local, new); | |
8bb4bdeb XL |
802 | new |
803 | } | |
8bb4bdeb | 804 | |
29967ef6 XL |
805 | fn map_scope(&self, scope: SourceScope) -> SourceScope { |
806 | let new = SourceScope::new(self.new_scopes.start.index() + scope.index()); | |
807 | trace!("mapping scope `{:?}` to `{:?}`", scope, new); | |
808 | new | |
809 | } | |
e74abb32 | 810 | |
29967ef6 XL |
811 | fn map_block(&self, block: BasicBlock) -> BasicBlock { |
812 | let new = BasicBlock::new(self.new_blocks.start.index() + block.index()); | |
813 | trace!("mapping block `{:?}` to `{:?}`", block, new); | |
814 | new | |
e74abb32 XL |
815 | } |
816 | } | |
817 | ||
818 | impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> { | |
819 | fn tcx(&self) -> TyCtxt<'tcx> { | |
820 | self.tcx | |
ea8adc8c XL |
821 | } |
822 | ||
dfeec247 | 823 | fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) { |
29967ef6 XL |
824 | *local = self.map_local(*local); |
825 | } | |
826 | ||
827 | fn visit_source_scope(&mut self, scope: &mut SourceScope) { | |
828 | *scope = self.map_scope(*scope); | |
829 | } | |
830 | ||
831 | fn visit_span(&mut self, span: &mut Span) { | |
5869c6ff | 832 | let mut expn_data = |
136023e0 | 833 | ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None, None); |
5869c6ff | 834 | expn_data.def_site = self.body_span; |
29967ef6 | 835 | // Make sure that all spans track the fact that they were inlined. |
136023e0 XL |
836 | *span = |
837 | self.callsite_span.fresh_expansion(expn_data, self.tcx.create_stable_hashing_context()); | |
e74abb32 XL |
838 | } |
839 | ||
dfeec247 | 840 | fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) { |
29967ef6 XL |
841 | for elem in place.projection { |
842 | // FIXME: Make sure that return place is not used in an indexing projection, since it | |
843 | // won't be rebased as it is supposed to be. | |
844 | assert_ne!(ProjectionElem::Index(RETURN_PLACE), elem); | |
845 | } | |
846 | ||
dfeec247 XL |
847 | // If this is the `RETURN_PLACE`, we need to rebase any projections onto it. |
848 | let dest_proj_len = self.destination.projection.len(); | |
849 | if place.local == RETURN_PLACE && dest_proj_len > 0 { | |
850 | let mut projs = Vec::with_capacity(dest_proj_len + place.projection.len()); | |
851 | projs.extend(self.destination.projection); | |
852 | projs.extend(place.projection); | |
853 | ||
854 | place.projection = self.tcx.intern_place_elems(&*projs); | |
8bb4bdeb | 855 | } |
dfeec247 XL |
856 | // Handles integrating any locals that occur in the base |
857 | // or projections | |
858 | self.super_place(place, context, location) | |
8bb4bdeb XL |
859 | } |
860 | ||
861 | fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) { | |
862 | self.in_cleanup_block = data.is_cleanup; | |
863 | self.super_basic_block_data(block, data); | |
864 | self.in_cleanup_block = false; | |
865 | } | |
866 | ||
dfeec247 | 867 | fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) { |
0731742a | 868 | self.super_retag(kind, place, loc); |
a1dfa0c6 XL |
869 | |
870 | // We have to patch all inlined retags to be aware that they are no longer | |
871 | // happening on function entry. | |
0731742a XL |
872 | if *kind == RetagKind::FnEntry { |
873 | *kind = RetagKind::Default; | |
874 | } | |
a1dfa0c6 XL |
875 | } |
876 | ||
fc512014 XL |
877 | fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) { |
878 | if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) = | |
879 | statement.kind | |
880 | { | |
881 | self.always_live_locals.remove(local); | |
882 | } | |
883 | self.super_statement(statement, location); | |
884 | } | |
885 | ||
f035d41b | 886 | fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) { |
f9f354fc XL |
887 | // Don't try to modify the implicit `_0` access on return (`return` terminators are |
888 | // replaced down below anyways). | |
f035d41b XL |
889 | if !matches!(terminator.kind, TerminatorKind::Return) { |
890 | self.super_terminator(terminator, loc); | |
f9f354fc | 891 | } |
8bb4bdeb | 892 | |
f035d41b | 893 | match terminator.kind { |
dfeec247 XL |
894 | TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => bug!(), |
895 | TerminatorKind::Goto { ref mut target } => { | |
29967ef6 | 896 | *target = self.map_block(*target); |
8bb4bdeb XL |
897 | } |
898 | TerminatorKind::SwitchInt { ref mut targets, .. } => { | |
29967ef6 XL |
899 | for tgt in targets.all_targets_mut() { |
900 | *tgt = self.map_block(*tgt); | |
8bb4bdeb XL |
901 | } |
902 | } | |
dfeec247 XL |
903 | TerminatorKind::Drop { ref mut target, ref mut unwind, .. } |
904 | | TerminatorKind::DropAndReplace { ref mut target, ref mut unwind, .. } => { | |
29967ef6 | 905 | *target = self.map_block(*target); |
8bb4bdeb | 906 | if let Some(tgt) = *unwind { |
29967ef6 | 907 | *unwind = Some(self.map_block(tgt)); |
8bb4bdeb XL |
908 | } else if !self.in_cleanup_block { |
909 | // Unless this drop is in a cleanup block, add an unwind edge to | |
b7449926 | 910 | // the original call's cleanup block |
8bb4bdeb XL |
911 | *unwind = self.cleanup_block; |
912 | } | |
913 | } | |
914 | TerminatorKind::Call { ref mut destination, ref mut cleanup, .. } => { | |
915 | if let Some((_, ref mut tgt)) = *destination { | |
29967ef6 | 916 | *tgt = self.map_block(*tgt); |
8bb4bdeb XL |
917 | } |
918 | if let Some(tgt) = *cleanup { | |
29967ef6 | 919 | *cleanup = Some(self.map_block(tgt)); |
8bb4bdeb XL |
920 | } else if !self.in_cleanup_block { |
921 | // Unless this call is in a cleanup block, add an unwind edge to | |
b7449926 | 922 | // the original call's cleanup block |
8bb4bdeb XL |
923 | *cleanup = self.cleanup_block; |
924 | } | |
925 | } | |
926 | TerminatorKind::Assert { ref mut target, ref mut cleanup, .. } => { | |
29967ef6 | 927 | *target = self.map_block(*target); |
8bb4bdeb | 928 | if let Some(tgt) = *cleanup { |
29967ef6 | 929 | *cleanup = Some(self.map_block(tgt)); |
8bb4bdeb XL |
930 | } else if !self.in_cleanup_block { |
931 | // Unless this assert is in a cleanup block, add an unwind edge to | |
b7449926 | 932 | // the original call's cleanup block |
8bb4bdeb XL |
933 | *cleanup = self.cleanup_block; |
934 | } | |
935 | } | |
936 | TerminatorKind::Return => { | |
29967ef6 XL |
937 | terminator.kind = if let Some(tgt) = self.return_block { |
938 | TerminatorKind::Goto { target: tgt } | |
939 | } else { | |
940 | TerminatorKind::Unreachable | |
941 | } | |
8bb4bdeb XL |
942 | } |
943 | TerminatorKind::Resume => { | |
944 | if let Some(tgt) = self.cleanup_block { | |
f035d41b | 945 | terminator.kind = TerminatorKind::Goto { target: tgt } |
8bb4bdeb XL |
946 | } |
947 | } | |
dfeec247 XL |
948 | TerminatorKind::Abort => {} |
949 | TerminatorKind::Unreachable => {} | |
f035d41b | 950 | TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => { |
29967ef6 XL |
951 | *real_target = self.map_block(*real_target); |
952 | *imaginary_target = self.map_block(*imaginary_target); | |
abe05a73 | 953 | } |
dfeec247 XL |
954 | TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => |
955 | // see the ordering of passes in the optimized_mir query. | |
956 | { | |
2c00a5a8 | 957 | bug!("False unwinds should have been removed before inlining") |
dfeec247 | 958 | } |
f9f354fc XL |
959 | TerminatorKind::InlineAsm { ref mut destination, .. } => { |
960 | if let Some(ref mut tgt) = *destination { | |
29967ef6 | 961 | *tgt = self.map_block(*tgt); |
f9f354fc XL |
962 | } |
963 | } | |
8bb4bdeb XL |
964 | } |
965 | } | |
8bb4bdeb | 966 | } |