@@ -216,5 +216,120 @@ pub(crate) mod optimize {
216216 } ;
217217 }
218218
219- pub ( crate ) use { replace, rewrite} ;
219+ macro_rules! define_local_source_resolver {
220+ (
221+ $name: ident,
222+ get = $get: ident,
223+ tee = $tee: ident,
224+ set = $set: ident,
225+ copy = $copy: ident,
226+ binop_local_local_tee = $lltee: ident,
227+ binop_local_local_set = $llset: ident,
228+ binop_local_const_tee = $lctee: ident,
229+ binop_local_const_set = $lcset: ident
230+ $( , load_local_tee = $loadtee: ident, load_local_set = $loadset: ident) ?
231+ ) => {
232+ fn $name( instrs: & mut [ Instruction ] , read: usize , instr: Instruction ) -> Option <( Instruction , u16 ) > {
233+ Some ( match instr {
234+ Instruction :: $get( local) => ( Instruction :: Nop , local) ,
235+ Instruction :: $tee( local) => {
236+ let replacement = if let Some ( [ ( prev_idx, Instruction :: $get( src) ) ] ) = previous_non_nop:: <1 >( instrs, read) {
237+ instrs[ prev_idx] = Instruction :: Nop ;
238+ if src == local { Instruction :: Nop } else { Instruction :: $copy( src, local) }
239+ } else {
240+ Instruction :: $set( local)
241+ } ;
242+ ( replacement, local)
243+ }
244+ Instruction :: $lltee( op, a, b, local) => ( Instruction :: $llset( op, a, b, local) , local) ,
245+ Instruction :: $lctee( op, src, c, local) => ( Instruction :: $lcset( op, src, c, local) , local) ,
246+ $( Instruction :: $loadtee( memarg, addr, local) => ( Instruction :: $loadset( memarg, addr, local) , local. into( ) ) , ) ?
247+ _ => return None ,
248+ } )
249+ }
250+ } ;
251+ }
252+
253+ macro_rules! fold_local_binop {
254+ (
255+ $instrs: ident, $read: expr, $dst: expr,
256+ source = $source: ident,
257+ op = $op: ident,
258+ const = $const: ident,
259+ local_local = $local_local: ident,
260+ local_const = $local_const: expr
261+ ) => { {
262+ if let Some ( [ ( lhs_idx, lhs_src) , ( rhs_idx, rhs_src) , ( op_idx, raw_op) ] ) =
263+ previous_non_nop:: <3 >( $instrs, $read)
264+ && let Some ( ( lhs_instr, lhs) ) = $source( $instrs, lhs_idx, lhs_src)
265+ && let Some ( op) = $op( raw_op)
266+ {
267+ if let Some ( ( rhs_instr, rhs) ) = $source( $instrs, rhs_idx, rhs_src) {
268+ $instrs[ lhs_idx] = lhs_instr;
269+ $instrs[ rhs_idx] = rhs_instr;
270+ $instrs[ op_idx] = Instruction :: Nop ;
271+ $instrs[ $read] = Instruction :: $local_local( op, lhs, rhs, $dst) ;
272+ } else if let Some ( imm) = $const( rhs_src, raw_op) {
273+ $instrs[ lhs_idx] = lhs_instr;
274+ $instrs[ rhs_idx] = Instruction :: Nop ;
275+ $instrs[ op_idx] = Instruction :: Nop ;
276+ $instrs[ $read] = $local_const( $dst, lhs, op, imm) ;
277+ }
278+ }
279+ } } ;
280+ }
281+
282+ macro_rules! rewrite_local_set_direct {
283+ (
284+ $instrs: ident, $read: ident, $dst: expr,
285+ get = $get: ident,
286+ copy = $copy: ident,
287+ binop_local_local = $ll: ident,
288+ binop_local_local_set = $llset: ident,
289+ binop_local_const = $lc: ident,
290+ binop_local_const_set = $lcset: expr
291+ $( , const_instr = $const_instr: ident, set_local_const = $set_local_const: ident) ?
292+ ) => { {
293+ rewrite!( $instrs, $read, [ $get( src) ] => if src == $dst { Instruction :: Nop } else { Instruction :: $copy( src, $dst) } ) ;
294+ $( rewrite!( $instrs, $read, [ $const_instr( c) ] => Instruction :: $set_local_const( $dst, c) ) ; ) ?
295+ rewrite!( $instrs, $read, [ $ll( op, a, b) ] => Instruction :: $llset( op, a, b, $dst) ) ;
296+ rewrite!( $instrs, $read, [ $lc( op, src, c) ] => { replace!( $instrs, $read, 1 => $lcset( $dst, src, op, c) ) ; } ) ;
297+ } } ;
298+ }
299+
300+ macro_rules! rewrite_local_tee_direct {
301+ (
302+ $instrs: ident, $read: ident, $dst: expr,
303+ get = $get: ident,
304+ binop_local_local = $ll: ident,
305+ binop_local_local_tee = $lltee: ident,
306+ binop_local_const = $lc: ident,
307+ binop_local_const_tee = $lctee: ident
308+ ) => { {
309+ rewrite!( $instrs, $read, [ $get( src) ] if ( src == $dst) => [ Instruction :: $get( src) , Instruction :: Nop ] ) ;
310+ rewrite!( $instrs, $read, [ $ll( op, a, b) ] => Instruction :: $lltee( op, a, b, $dst) ) ;
311+ rewrite!( $instrs, $read, [ $lc( op, src, c) ] => Instruction :: $lctee( op, src, c, $dst) ) ;
312+ } } ;
313+ }
314+
315+ macro_rules! rewrite_drop_tee_direct {
316+ (
317+ $instrs: ident, $read: ident,
318+ tee = $tee: ident,
319+ set = $set: ident,
320+ binop_local_local_tee = $lltee: ident,
321+ binop_local_local_set = $llset: ident,
322+ binop_local_const_tee = $lctee: ident,
323+ binop_local_const_set = $lcset: ident
324+ ) => { {
325+ rewrite!( $instrs, $read, [ $tee( local) ] => [ Instruction :: $set( local) , Instruction :: Nop ] ) ;
326+ rewrite!( $instrs, $read, [ $lltee( op, a, b, dst) ] => Instruction :: $llset( op, a, b, dst) ) ;
327+ rewrite!( $instrs, $read, [ $lctee( op, src, c, dst) ] => Instruction :: $lcset( op, src, c, dst) ) ;
328+ } } ;
329+ }
330+
331+ pub ( crate ) use {
332+ define_local_source_resolver, fold_local_binop, replace, rewrite, rewrite_drop_tee_direct,
333+ rewrite_local_set_direct, rewrite_local_tee_direct,
334+ } ;
220335}
0 commit comments