22
33#include < fstream>
44
5- #include " evaluator.hpp"
65#include " file.hpp"
76#include " optimizer.hpp"
87#include " program_util.hpp"
1211
1312bool Minimizer::minimize (Program& p, size_t num_terms) const {
1413 Log::get ().debug (" Minimizing program" );
15- Evaluator evaluator (settings);
1614
1715 // calculate target sequence
1816 Sequence target_sequence;
@@ -37,15 +35,7 @@ bool Minimizer::minimize(Program& p, size_t num_terms) const {
3735 continue ;
3836 } else if (op.type == Operation::Type::TRN) {
3937 p.ops [i].type = Operation::Type::SUB;
40- bool can_replace;
41- try {
42- auto res = evaluator.check (p, target_sequence);
43- can_replace = (res.first == status_t ::OK) &&
44- (res.second .total <= target_steps.total );
45- } catch (const std::exception&) {
46- can_replace = false ;
47- }
48- if (can_replace) {
38+ if (check (p, target_sequence, target_steps.total )) {
4939 local_change = true ;
5040 } else {
5141 // revert change
@@ -54,15 +44,7 @@ bool Minimizer::minimize(Program& p, size_t num_terms) const {
5444 } else if (op.type == Operation::Type::LPB) {
5545 if (op.source .type != Operand::Type::CONSTANT || op.source .value != 1 ) {
5646 p.ops [i].source = Operand (Operand::Type::CONSTANT, 1 );
57- bool can_reset;
58- try {
59- auto res = evaluator.check (p, target_sequence);
60- can_reset = (res.first == status_t ::OK) &&
61- (res.second .total <= target_steps.total );
62- } catch (const std::exception&) {
63- can_reset = false ;
64- }
65- if (can_reset) {
47+ if (check (p, target_sequence, target_steps.total )) {
6648 local_change = true ;
6749 } else {
6850 // revert change
@@ -73,15 +55,7 @@ bool Minimizer::minimize(Program& p, size_t num_terms) const {
7355 // try to remove the current operation (if there is at least one
7456 // operation, see A000004)
7557 p.ops .erase (p.ops .begin () + i, p.ops .begin () + i + 1 );
76- bool can_remove;
77- try {
78- auto res = evaluator.check (p, target_sequence);
79- can_remove = (res.first == status_t ::OK) &&
80- (res.second .total <= target_steps.total );
81- } catch (const std::exception&) {
82- can_remove = false ;
83- }
84- if (can_remove) {
58+ if (check (p, target_sequence, target_steps.total )) {
8559 local_change = true ;
8660 --i;
8761 } else {
@@ -120,16 +94,8 @@ bool Minimizer::minimize(Program& p, size_t num_terms) const {
12094 p.ops .insert (p.ops .begin () + i + 5 ,
12195 Operation (Operation::Type::MOV, op.target , tmp));
12296
123- bool can_rewrite;
124- try {
125- auto res = evaluator.check (p, target_sequence);
126- can_rewrite =
127- (res.first ==
128- status_t ::OK); // we don't check the number of steps here!
129- } catch (const std::exception&) {
130- can_rewrite = false ;
131- }
132- if (can_rewrite) {
97+ // we don't check the number of steps here!
98+ if (check (p, target_sequence, 0 )) {
13399 local_change = true ;
134100 } else {
135101 // revert change
@@ -145,6 +111,22 @@ bool Minimizer::minimize(Program& p, size_t num_terms) const {
145111 return global_change;
146112}
147113
114+ bool Minimizer::check (const Program& p, const Sequence& seq,
115+ size_t max_total) const {
116+ try {
117+ auto res = evaluator.check (p, seq);
118+ if (res.first != status_t ::OK) {
119+ return false ;
120+ }
121+ if (max_total > 0 && res.second .total > max_total) {
122+ return false ;
123+ }
124+ } catch (const std::exception&) {
125+ return false ;
126+ }
127+ return true ;
128+ }
129+
148130int64_t Minimizer::getPowerOf (const Number& v) {
149131 if (Number (9 ) < Semantics::getPowerOf (v, 2 )) {
150132 return 2 ;
0 commit comments