|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use move_model::model::{FunId, FunctionEnv, QualifiedId}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + function_data_builder::FunctionDataBuilder, |
| 7 | + function_target::FunctionData, |
| 8 | + function_target_pipeline::{FunctionTargetProcessor, FunctionTargetsHolder}, |
| 9 | + stackless_bytecode::{AssignKind, Bytecode, Operation}, |
| 10 | +}; |
| 11 | + |
| 12 | +pub struct ReplacementAnalysisProcessor(); |
| 13 | + |
| 14 | +impl ReplacementAnalysisProcessor { |
| 15 | + pub fn new() -> Box<Self> { |
| 16 | + Box::new(Self()) |
| 17 | + } |
| 18 | + |
| 19 | + fn is_fn(code: &Bytecode, qid: QualifiedId<FunId>) -> Option<(&Vec<usize>, &Vec<usize>)> { |
| 20 | + match code { |
| 21 | + Bytecode::Call(_, dest, Operation::Function(mid, fid, _), srcs, _) => { |
| 22 | + if qid == mid.qualified(*fid) { |
| 23 | + return Some((dest, srcs)); |
| 24 | + } |
| 25 | + } |
| 26 | + _ => {} |
| 27 | + } |
| 28 | + |
| 29 | + None |
| 30 | + } |
| 31 | + |
| 32 | + pub fn find_ref_val_patterns( |
| 33 | + &self, |
| 34 | + func_env: &FunctionEnv, |
| 35 | + data: &FunctionData, |
| 36 | + ) -> BTreeMap<usize, (Vec<usize>, Vec<usize>)> { |
| 37 | + if data.code.len() < 2 { |
| 38 | + return BTreeMap::new(); |
| 39 | + } |
| 40 | + |
| 41 | + let mut matches = BTreeMap::new(); |
| 42 | + for i in 0..data.code.len() - 1 { |
| 43 | + if let Some((dest_val, srcs_val)) = |
| 44 | + Self::is_fn(&data.code[i], func_env.module_env.env.prover_val_qid()) |
| 45 | + { |
| 46 | + if let Some((dest_ref, srcs_ref)) = |
| 47 | + Self::is_fn(&data.code[i + 1], func_env.module_env.env.prover_ref_qid()) |
| 48 | + { |
| 49 | + if dest_val == srcs_ref { |
| 50 | + matches.insert(i, (dest_ref.clone(), srcs_val.clone())); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + matches |
| 57 | + } |
| 58 | + |
| 59 | + pub fn replace_patterns( |
| 60 | + &self, |
| 61 | + patterns: BTreeMap<usize, (Vec<usize>, Vec<usize>)>, |
| 62 | + func_env: &FunctionEnv, |
| 63 | + data: FunctionData, |
| 64 | + ) -> FunctionData { |
| 65 | + if patterns.is_empty() { |
| 66 | + return data; |
| 67 | + } |
| 68 | + |
| 69 | + let mut new_data = data.clone(); |
| 70 | + new_data.code = vec![]; // NOTE: for some reason it doesnt work properly without copy + erase |
| 71 | + |
| 72 | + let mut builder = FunctionDataBuilder::new(func_env, new_data); |
| 73 | + for (offset, bc) in data.code.into_iter().enumerate() { |
| 74 | + if patterns.contains_key(&offset) { |
| 75 | + continue; |
| 76 | + } else if offset > 0 && patterns.contains_key(&(offset - 1)) { |
| 77 | + // NOTE: we replace call only with an Assign because it automatically dereferences var |
| 78 | + let (dest, srcs) = patterns.get(&(offset - 1)).unwrap(); |
| 79 | + builder.emit(Bytecode::Assign( |
| 80 | + bc.get_attr_id(), |
| 81 | + dest[0], |
| 82 | + srcs[0], |
| 83 | + AssignKind::Copy, |
| 84 | + )); |
| 85 | + } else { |
| 86 | + builder.emit(bc); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + builder.data |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl FunctionTargetProcessor for ReplacementAnalysisProcessor { |
| 95 | + fn process( |
| 96 | + &self, |
| 97 | + targets: &mut FunctionTargetsHolder, |
| 98 | + func_env: &FunctionEnv, |
| 99 | + data: FunctionData, |
| 100 | + _scc_opt: Option<&[FunctionEnv]>, |
| 101 | + ) -> FunctionData { |
| 102 | + if func_env.is_native() { |
| 103 | + return data; |
| 104 | + } |
| 105 | + |
| 106 | + let patterns = self.find_ref_val_patterns(func_env, &data); |
| 107 | + self.replace_patterns(patterns, func_env, data) |
| 108 | + } |
| 109 | + |
| 110 | + fn name(&self) -> String { |
| 111 | + "replacement_analysis".to_string() |
| 112 | + } |
| 113 | +} |
0 commit comments