@@ -15,8 +15,9 @@ void bpf_ir_optimize_code_compaction(struct bpf_ir_env *env,
1515 {
1616 struct ir_basic_block * bb = * pos ;
1717 list_for_each_entry (cur_insn , & bb -> ir_insn_head , list_ptr ) {
18- if (! prev_insn ) {
18+ if (prev_insn == NULL ) {
1919 prev_insn = cur_insn ;
20+ continue ;
2021 }
2122 if (prev_insn -> op == IR_INSN_LSH &&
2223 cur_insn -> op == IR_INSN_RSH ) {
@@ -26,35 +27,47 @@ void bpf_ir_optimize_code_compaction(struct bpf_ir_env *env,
2627 IR_VALUE_CONSTANT ) {
2728 continue ;
2829 }
30+ if (prev_insn -> values [1 ].data .constant_d !=
31+ 32 ||
32+ cur_insn -> values [1 ].data .constant_d != 32 ) {
33+ continue ;
34+ }
2935 if (prev_insn -> values [0 ].type ==
3036 IR_VALUE_INSN &&
3137 cur_insn -> values [0 ].type == IR_VALUE_INSN &&
32- prev_insn -> values [ 0 ]. data . insn_d ==
38+ prev_insn ==
3339 cur_insn -> values [0 ].data .insn_d ) {
3440 // Same!
3541 bpf_ir_array_push (env , & opt_insns ,
3642 & cur_insn );
37- bpf_ir_array_push (env , & opt_insns ,
38- & prev_insn );
3943 }
4044 }
4145 prev_insn = cur_insn ;
4246 }
4347 }
48+ PRINT_LOG_DEBUG (env , "Found %d compaction opt\n" , opt_insns .num_elem );
4449 if (opt_insns .num_elem == 0 ) {
4550 return ;
4651 }
4752 struct ir_insn * * pos2 ;
4853 array_for (pos2 , opt_insns )
4954 {
5055 struct ir_insn * insn = * pos2 ;
51- if (insn -> op == IR_INSN_RSH ) {
52- // Insert optimized code
53- struct ir_insn * ni = bpf_ir_create_assign_insn (
54- env , insn , insn -> values [0 ], INSERT_BACK );
55- ni -> vr_type = IR_VR_TYPE_32 ;
56- }
57- bpf_ir_erase_insn (env , insn );
56+ struct ir_insn * lsh_insn = insn -> values [0 ].data .insn_d ;
57+ DBGASSERT (insn -> op == IR_INSN_RSH );
58+ DBGASSERT (lsh_insn -> op == IR_INSN_LSH );
59+ // Insert optimized code
60+ bpf_ir_change_value (env , insn , & insn -> values [0 ],
61+ lsh_insn -> values [0 ]);
62+ insn -> op = IR_INSN_ASSIGN ;
63+ insn -> vr_type = IR_VR_TYPE_32 ;
64+ insn -> alu_op = IR_ALU_32 ;
65+ insn -> value_num = 1 ;
66+ insn -> raw_pos .valid = false;
67+ bpf_ir_erase_insn (env , lsh_insn );
5868 }
5969 bpf_ir_array_free (& opt_insns );
60- }
70+ }
71+
72+ const struct builtin_pass_cfg bpf_ir_kern_compaction_pass =
73+ DEF_BUILTIN_PASS_ENABLE_CFG ("optimize_compaction" , NULL , NULL );
0 commit comments