]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_cranelift/src/optimize/stack2reg.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / compiler / rustc_codegen_cranelift / src / optimize / stack2reg.rs
1 //! This optimization replaces stack accesses with SSA variables and removes dead stores when possible.
2 //!
3 //! # Undefined behaviour
4 //!
5 //! This optimization is based on the assumption that stack slots which don't have their address
6 //! leaked through `stack_addr` are only accessed using `stack_load` and `stack_store` in the
7 //! function which has the stack slots. This optimization also assumes that stack slot accesses
8 //! are never out of bounds. If these assumptions are not correct, then this optimization may remove
9 //! `stack_store` instruction incorrectly, or incorrectly use a previously stored value as the value
10 //! being loaded by a `stack_load`.
11
12 use std::collections::BTreeMap;
13 use std::fmt;
14 use std::ops::Not;
15
16 use rustc_data_structures::fx::FxHashSet;
17
18 use cranelift_codegen::cursor::{Cursor, FuncCursor};
19 use cranelift_codegen::ir::immediates::Offset32;
20 use cranelift_codegen::ir::{InstructionData, Opcode, ValueDef};
21
22 use crate::prelude::*;
23
24 /// Workaround for `StackSlot` not implementing `Ord`.
25 #[derive(Copy, Clone, PartialEq, Eq)]
26 struct OrdStackSlot(StackSlot);
27
28 impl fmt::Debug for OrdStackSlot {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 write!(f, "{:?}", self.0)
31 }
32 }
33
34 impl PartialOrd for OrdStackSlot {
35 fn partial_cmp(&self, rhs: &Self) -> Option<std::cmp::Ordering> {
36 self.0.as_u32().partial_cmp(&rhs.0.as_u32())
37 }
38 }
39
40 impl Ord for OrdStackSlot {
41 fn cmp(&self, rhs: &Self) -> std::cmp::Ordering {
42 self.0.as_u32().cmp(&rhs.0.as_u32())
43 }
44 }
45
46 #[derive(Debug, Default)]
47 struct StackSlotUsage {
48 stack_addr: FxHashSet<Inst>,
49 stack_load: FxHashSet<Inst>,
50 stack_store: FxHashSet<Inst>,
51 }
52
53 impl StackSlotUsage {
54 fn potential_stores_for_load(&self, ctx: &Context, load: Inst) -> Vec<Inst> {
55 self.stack_store
56 .iter()
57 .cloned()
58 .filter(|&store| {
59 match spatial_overlap(&ctx.func, store, load) {
60 SpatialOverlap::No => false, // Can never be the source of the loaded value.
61 SpatialOverlap::Partial | SpatialOverlap::Full => true,
62 }
63 })
64 .filter(|&store| {
65 match temporal_order(ctx, store, load) {
66 TemporalOrder::NeverBefore => false, // Can never be the source of the loaded value.
67 TemporalOrder::MaybeBefore | TemporalOrder::DefinitivelyBefore => true,
68 }
69 })
70 .collect::<Vec<Inst>>()
71 }
72
73 fn potential_loads_of_store(&self, ctx: &Context, store: Inst) -> Vec<Inst> {
74 self.stack_load
75 .iter()
76 .cloned()
77 .filter(|&load| {
78 match spatial_overlap(&ctx.func, store, load) {
79 SpatialOverlap::No => false, // Can never be the source of the loaded value.
80 SpatialOverlap::Partial | SpatialOverlap::Full => true,
81 }
82 })
83 .filter(|&load| {
84 match temporal_order(ctx, store, load) {
85 TemporalOrder::NeverBefore => false, // Can never be the source of the loaded value.
86 TemporalOrder::MaybeBefore | TemporalOrder::DefinitivelyBefore => true,
87 }
88 })
89 .collect::<Vec<Inst>>()
90 }
91
92 fn remove_unused_stack_addr(func: &mut Function, inst: Inst) {
93 func.dfg.detach_results(inst);
94 func.dfg.replace(inst).nop();
95 }
96
97 fn remove_unused_load(func: &mut Function, load: Inst) {
98 func.dfg.detach_results(load);
99 func.dfg.replace(load).nop();
100 }
101
102 fn remove_dead_store(&mut self, func: &mut Function, store: Inst) {
103 func.dfg.replace(store).nop();
104 self.stack_store.remove(&store);
105 }
106
107 fn change_load_to_alias(&mut self, func: &mut Function, load: Inst, value: Value) {
108 let loaded_value = func.dfg.inst_results(load)[0];
109 let loaded_type = func.dfg.value_type(loaded_value);
110
111 if func.dfg.value_type(value) == loaded_type {
112 func.dfg.detach_results(load);
113 func.dfg.replace(load).nop();
114 func.dfg.change_to_alias(loaded_value, value);
115 } else {
116 func.dfg.replace(load).bitcast(loaded_type, value);
117 }
118
119 self.stack_load.remove(&load);
120 }
121 }
122
123 struct OptimizeContext<'a> {
124 ctx: &'a mut Context,
125 stack_slot_usage_map: BTreeMap<OrdStackSlot, StackSlotUsage>,
126 }
127
128 impl<'a> OptimizeContext<'a> {
129 fn for_context(ctx: &'a mut Context) -> Self {
130 ctx.flowgraph(); // Compute cfg and domtree.
131
132 // Record all stack_addr, stack_load and stack_store instructions.
133 let mut stack_slot_usage_map = BTreeMap::<OrdStackSlot, StackSlotUsage>::new();
134
135 let mut cursor = FuncCursor::new(&mut ctx.func);
136 while let Some(_block) = cursor.next_block() {
137 while let Some(inst) = cursor.next_inst() {
138 match cursor.func.dfg[inst] {
139 InstructionData::StackLoad {
140 opcode: Opcode::StackAddr,
141 stack_slot,
142 offset: _,
143 } => {
144 stack_slot_usage_map
145 .entry(OrdStackSlot(stack_slot))
146 .or_insert_with(StackSlotUsage::default)
147 .stack_addr
148 .insert(inst);
149 }
150 InstructionData::StackLoad {
151 opcode: Opcode::StackLoad,
152 stack_slot,
153 offset: _,
154 } => {
155 stack_slot_usage_map
156 .entry(OrdStackSlot(stack_slot))
157 .or_insert_with(StackSlotUsage::default)
158 .stack_load
159 .insert(inst);
160 }
161 InstructionData::StackStore {
162 opcode: Opcode::StackStore,
163 arg: _,
164 stack_slot,
165 offset: _,
166 } => {
167 stack_slot_usage_map
168 .entry(OrdStackSlot(stack_slot))
169 .or_insert_with(StackSlotUsage::default)
170 .stack_store
171 .insert(inst);
172 }
173 _ => {}
174 }
175 }
176 }
177
178 OptimizeContext { ctx, stack_slot_usage_map }
179 }
180 }
181
182 pub(super) fn optimize_function(
183 ctx: &mut Context,
184 #[cfg_attr(not(debug_assertions), allow(unused_variables))]
185 clif_comments: &mut crate::pretty_clif::CommentWriter,
186 ) {
187 combine_stack_addr_with_load_store(&mut ctx.func);
188
189 let mut opt_ctx = OptimizeContext::for_context(ctx);
190
191 // FIXME Repeat following instructions until fixpoint.
192
193 remove_unused_stack_addr_and_stack_load(&mut opt_ctx);
194
195 #[cfg(debug_assertions)]
196 {
197 for (&OrdStackSlot(stack_slot), usage) in &opt_ctx.stack_slot_usage_map {
198 clif_comments.add_comment(stack_slot, format!("used by: {:?}", usage));
199 }
200 }
201
202 for (stack_slot, users) in opt_ctx.stack_slot_usage_map.iter_mut() {
203 if users.stack_addr.is_empty().not() {
204 // Stack addr leaked; there may be unknown loads and stores.
205 // FIXME use stacked borrows to optimize
206 continue;
207 }
208
209 for load in users.stack_load.clone().into_iter() {
210 let potential_stores = users.potential_stores_for_load(&opt_ctx.ctx, load);
211
212 #[cfg(debug_assertions)]
213 for &store in &potential_stores {
214 clif_comments.add_comment(
215 load,
216 format!(
217 "Potential store -> load forwarding {} -> {} ({:?}, {:?})",
218 opt_ctx.ctx.func.dfg.display_inst(store, None),
219 opt_ctx.ctx.func.dfg.display_inst(load, None),
220 spatial_overlap(&opt_ctx.ctx.func, store, load),
221 temporal_order(&opt_ctx.ctx, store, load),
222 ),
223 );
224 }
225
226 match *potential_stores {
227 [] => {
228 #[cfg(debug_assertions)]
229 clif_comments
230 .add_comment(load, "[BUG?] Reading uninitialized memory".to_string());
231 }
232 [store]
233 if spatial_overlap(&opt_ctx.ctx.func, store, load) == SpatialOverlap::Full
234 && temporal_order(&opt_ctx.ctx, store, load)
235 == TemporalOrder::DefinitivelyBefore =>
236 {
237 // Only one store could have been the origin of the value.
238 let stored_value = opt_ctx.ctx.func.dfg.inst_args(store)[0];
239
240 #[cfg(debug_assertions)]
241 clif_comments
242 .add_comment(load, format!("Store to load forward {} -> {}", store, load));
243
244 users.change_load_to_alias(&mut opt_ctx.ctx.func, load, stored_value);
245 }
246 _ => {} // FIXME implement this
247 }
248 }
249
250 for store in users.stack_store.clone().into_iter() {
251 let potential_loads = users.potential_loads_of_store(&opt_ctx.ctx, store);
252
253 #[cfg(debug_assertions)]
254 for &load in &potential_loads {
255 clif_comments.add_comment(
256 store,
257 format!(
258 "Potential load from store {} <- {} ({:?}, {:?})",
259 opt_ctx.ctx.func.dfg.display_inst(load, None),
260 opt_ctx.ctx.func.dfg.display_inst(store, None),
261 spatial_overlap(&opt_ctx.ctx.func, store, load),
262 temporal_order(&opt_ctx.ctx, store, load),
263 ),
264 );
265 }
266
267 if potential_loads.is_empty() {
268 // Never loaded; can safely remove all stores and the stack slot.
269 // FIXME also remove stores when there is always a next store before a load.
270
271 #[cfg(debug_assertions)]
272 clif_comments.add_comment(
273 store,
274 format!(
275 "Remove dead stack store {} of {}",
276 opt_ctx.ctx.func.dfg.display_inst(store, None),
277 stack_slot.0
278 ),
279 );
280
281 users.remove_dead_store(&mut opt_ctx.ctx.func, store);
282 }
283 }
284
285 if users.stack_store.is_empty() && users.stack_load.is_empty() {
286 opt_ctx.ctx.func.stack_slots[stack_slot.0].size = 0;
287 }
288 }
289 }
290
291 fn combine_stack_addr_with_load_store(func: &mut Function) {
292 // Turn load and store into stack_load and stack_store when possible.
293 let mut cursor = FuncCursor::new(func);
294 while let Some(_block) = cursor.next_block() {
295 while let Some(inst) = cursor.next_inst() {
296 match cursor.func.dfg[inst] {
297 InstructionData::Load { opcode: Opcode::Load, arg: addr, flags: _, offset } => {
298 if cursor.func.dfg.ctrl_typevar(inst) == types::I128
299 || cursor.func.dfg.ctrl_typevar(inst).is_vector()
300 {
301 continue; // WORKAROUD: stack_load.i128 not yet implemented
302 }
303 if let Some((stack_slot, stack_addr_offset)) =
304 try_get_stack_slot_and_offset_for_addr(cursor.func, addr)
305 {
306 if let Some(combined_offset) = offset.try_add_i64(stack_addr_offset.into())
307 {
308 let ty = cursor.func.dfg.ctrl_typevar(inst);
309 cursor.func.dfg.replace(inst).stack_load(
310 ty,
311 stack_slot,
312 combined_offset,
313 );
314 }
315 }
316 }
317 InstructionData::Store {
318 opcode: Opcode::Store,
319 args: [value, addr],
320 flags: _,
321 offset,
322 } => {
323 if cursor.func.dfg.ctrl_typevar(inst) == types::I128
324 || cursor.func.dfg.ctrl_typevar(inst).is_vector()
325 {
326 continue; // WORKAROUND: stack_store.i128 not yet implemented
327 }
328 if let Some((stack_slot, stack_addr_offset)) =
329 try_get_stack_slot_and_offset_for_addr(cursor.func, addr)
330 {
331 if let Some(combined_offset) = offset.try_add_i64(stack_addr_offset.into())
332 {
333 cursor.func.dfg.replace(inst).stack_store(
334 value,
335 stack_slot,
336 combined_offset,
337 );
338 }
339 }
340 }
341 _ => {}
342 }
343 }
344 }
345 }
346
347 fn remove_unused_stack_addr_and_stack_load(opt_ctx: &mut OptimizeContext<'_>) {
348 // FIXME incrementally rebuild on each call?
349 let mut stack_addr_load_insts_users = FxHashMap::<Inst, FxHashSet<Inst>>::default();
350
351 let mut cursor = FuncCursor::new(&mut opt_ctx.ctx.func);
352 while let Some(_block) = cursor.next_block() {
353 while let Some(inst) = cursor.next_inst() {
354 for &arg in cursor.func.dfg.inst_args(inst) {
355 if let ValueDef::Result(arg_origin, 0) = cursor.func.dfg.value_def(arg) {
356 match cursor.func.dfg[arg_origin].opcode() {
357 Opcode::StackAddr | Opcode::StackLoad => {
358 stack_addr_load_insts_users
359 .entry(arg_origin)
360 .or_insert_with(FxHashSet::default)
361 .insert(inst);
362 }
363 _ => {}
364 }
365 }
366 }
367 }
368 }
369
370 #[cfg(debug_assertions)]
371 for inst in stack_addr_load_insts_users.keys() {
372 let mut is_recorded_stack_addr_or_stack_load = false;
373 for stack_slot_users in opt_ctx.stack_slot_usage_map.values() {
374 is_recorded_stack_addr_or_stack_load |= stack_slot_users.stack_addr.contains(inst)
375 || stack_slot_users.stack_load.contains(inst);
376 }
377 assert!(is_recorded_stack_addr_or_stack_load);
378 }
379
380 // Replace all unused stack_addr and stack_load instructions with nop.
381 let mut func = &mut opt_ctx.ctx.func;
382
383 for stack_slot_users in opt_ctx.stack_slot_usage_map.values_mut() {
384 stack_slot_users
385 .stack_addr
386 .drain_filter(|inst| {
387 stack_addr_load_insts_users.get(inst).map(|users| users.is_empty()).unwrap_or(true)
388 })
389 .for_each(|inst| StackSlotUsage::remove_unused_stack_addr(&mut func, inst));
390
391 stack_slot_users
392 .stack_load
393 .drain_filter(|inst| {
394 stack_addr_load_insts_users.get(inst).map(|users| users.is_empty()).unwrap_or(true)
395 })
396 .for_each(|inst| StackSlotUsage::remove_unused_load(&mut func, inst));
397 }
398 }
399
400 fn try_get_stack_slot_and_offset_for_addr(
401 func: &Function,
402 addr: Value,
403 ) -> Option<(StackSlot, Offset32)> {
404 if let ValueDef::Result(addr_inst, 0) = func.dfg.value_def(addr) {
405 if let InstructionData::StackLoad { opcode: Opcode::StackAddr, stack_slot, offset } =
406 func.dfg[addr_inst]
407 {
408 return Some((stack_slot, offset));
409 }
410 }
411 None
412 }
413
414 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
415 enum SpatialOverlap {
416 No,
417 Partial,
418 Full,
419 }
420
421 fn spatial_overlap(func: &Function, src: Inst, dest: Inst) -> SpatialOverlap {
422 fn inst_info(func: &Function, inst: Inst) -> (StackSlot, Offset32, u32) {
423 match func.dfg[inst] {
424 InstructionData::StackLoad { opcode: Opcode::StackAddr, stack_slot, offset }
425 | InstructionData::StackLoad { opcode: Opcode::StackLoad, stack_slot, offset }
426 | InstructionData::StackStore {
427 opcode: Opcode::StackStore,
428 stack_slot,
429 offset,
430 arg: _,
431 } => (stack_slot, offset, func.dfg.ctrl_typevar(inst).bytes()),
432 _ => unreachable!("{:?}", func.dfg[inst]),
433 }
434 }
435
436 debug_assert_ne!(src, dest);
437
438 let (src_ss, src_offset, src_size) = inst_info(func, src);
439 let (dest_ss, dest_offset, dest_size) = inst_info(func, dest);
440
441 if src_ss != dest_ss {
442 return SpatialOverlap::No;
443 }
444
445 if src_offset == dest_offset && src_size == dest_size {
446 return SpatialOverlap::Full;
447 }
448
449 let src_end: i64 = src_offset.try_add_i64(i64::from(src_size)).unwrap().into();
450 let dest_end: i64 = dest_offset.try_add_i64(i64::from(dest_size)).unwrap().into();
451 if src_end <= dest_offset.into() || dest_end <= src_offset.into() {
452 return SpatialOverlap::No;
453 }
454
455 SpatialOverlap::Partial
456 }
457
458 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
459 enum TemporalOrder {
460 /// `src` will never be executed before `dest`.
461 NeverBefore,
462
463 /// `src` may be executed before `dest`.
464 MaybeBefore,
465
466 /// `src` will always be executed before `dest`.
467 /// There may still be other instructions in between.
468 DefinitivelyBefore,
469 }
470
471 fn temporal_order(ctx: &Context, src: Inst, dest: Inst) -> TemporalOrder {
472 debug_assert_ne!(src, dest);
473
474 if ctx.domtree.dominates(src, dest, &ctx.func.layout) {
475 TemporalOrder::DefinitivelyBefore
476 } else if ctx.domtree.dominates(src, dest, &ctx.func.layout) {
477 TemporalOrder::NeverBefore
478 } else {
479 TemporalOrder::MaybeBefore
480 }
481 }