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