@@ -138,8 +138,18 @@ static int trans_count;
138138#define e1 (*ep1)
139139#define e2 (*ep2)
140140
141+ /*
142+ * expr_eliminate_eq() helper.
143+ *
144+ * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
145+ * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
146+ * against all other leaves. Two equal leaves are both replaced with either 'y'
147+ * or 'n' as appropriate for 'type', to be eliminated later.
148+ */
141149static void __expr_eliminate_eq (enum expr_type type , struct expr * * ep1 , struct expr * * ep2 )
142150{
151+ /* Recurse down to leaves */
152+
143153 if (e1 -> type == type ) {
144154 __expr_eliminate_eq (type , & e1 -> left .expr , & e2 );
145155 __expr_eliminate_eq (type , & e1 -> right .expr , & e2 );
@@ -150,12 +160,18 @@ static void __expr_eliminate_eq(enum expr_type type, struct expr **ep1, struct e
150160 __expr_eliminate_eq (type , & e1 , & e2 -> right .expr );
151161 return ;
152162 }
163+
164+ /* e1 and e2 are leaves. Compare them. */
165+
153166 if (e1 -> type == E_SYMBOL && e2 -> type == E_SYMBOL &&
154167 e1 -> left .sym == e2 -> left .sym &&
155168 (e1 -> left .sym == & symbol_yes || e1 -> left .sym == & symbol_no ))
156169 return ;
157170 if (!expr_eq (e1 , e2 ))
158171 return ;
172+
173+ /* e1 and e2 are equal leaves. Prepare them for elimination. */
174+
159175 trans_count ++ ;
160176 expr_free (e1 ); expr_free (e2 );
161177 switch (type ) {
@@ -172,6 +188,35 @@ static void __expr_eliminate_eq(enum expr_type type, struct expr **ep1, struct e
172188 }
173189}
174190
191+ /*
192+ * Rewrites the expressions 'ep1' and 'ep2' to remove operands common to both.
193+ * Example reductions:
194+ *
195+ * ep1: A && B -> ep1: y
196+ * ep2: A && B && C -> ep2: C
197+ *
198+ * ep1: A || B -> ep1: n
199+ * ep2: A || B || C -> ep2: C
200+ *
201+ * ep1: A && (B && FOO) -> ep1: FOO
202+ * ep2: (BAR && B) && A -> ep2: BAR
203+ *
204+ * ep1: A && (B || C) -> ep1: y
205+ * ep2: (C || B) && A -> ep2: y
206+ *
207+ * Comparisons are done between all operands at the same "level" of && or ||.
208+ * For example, in the expression 'e1 && (e2 || e3) && (e4 || e5)', the
209+ * following operands will be compared:
210+ *
211+ * - 'e1', 'e2 || e3', and 'e4 || e5', against each other
212+ * - e2 against e3
213+ * - e4 against e5
214+ *
215+ * Parentheses are irrelevant within a single level. 'e1 && (e2 && e3)' and
216+ * '(e1 && e2) && e3' are both a single level.
217+ *
218+ * See __expr_eliminate_eq() as well.
219+ */
175220void expr_eliminate_eq (struct expr * * ep1 , struct expr * * ep2 )
176221{
177222 if (!e1 || !e2 )
@@ -197,6 +242,12 @@ void expr_eliminate_eq(struct expr **ep1, struct expr **ep2)
197242#undef e1
198243#undef e2
199244
245+ /*
246+ * Returns true if 'e1' and 'e2' are equal, after minor simplification. Two
247+ * &&/|| expressions are considered equal if every operand in one expression
248+ * equals some operand in the other (operands do not need to appear in the same
249+ * order), recursively.
250+ */
200251static int expr_eq (struct expr * e1 , struct expr * e2 )
201252{
202253 int res , old_count ;
@@ -243,6 +294,17 @@ static int expr_eq(struct expr *e1, struct expr *e2)
243294 return 0 ;
244295}
245296
297+ /*
298+ * Recursively performs the following simplifications in-place (as well as the
299+ * corresponding simplifications with swapped operands):
300+ *
301+ * expr && n -> n
302+ * expr && y -> expr
303+ * expr || n -> expr
304+ * expr || y -> y
305+ *
306+ * Returns the optimized expression.
307+ */
246308static struct expr * expr_eliminate_yn (struct expr * e )
247309{
248310 struct expr * tmp ;
@@ -516,12 +578,21 @@ static struct expr *expr_join_and(struct expr *e1, struct expr *e2)
516578 return NULL ;
517579}
518580
581+ /*
582+ * expr_eliminate_dups() helper.
583+ *
584+ * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
585+ * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
586+ * against all other leaves to look for simplifications.
587+ */
519588static void expr_eliminate_dups1 (enum expr_type type , struct expr * * ep1 , struct expr * * ep2 )
520589{
521590#define e1 (*ep1)
522591#define e2 (*ep2)
523592 struct expr * tmp ;
524593
594+ /* Recurse down to leaves */
595+
525596 if (e1 -> type == type ) {
526597 expr_eliminate_dups1 (type , & e1 -> left .expr , & e2 );
527598 expr_eliminate_dups1 (type , & e1 -> right .expr , & e2 );
@@ -532,6 +603,9 @@ static void expr_eliminate_dups1(enum expr_type type, struct expr **ep1, struct
532603 expr_eliminate_dups1 (type , & e1 , & e2 -> right .expr );
533604 return ;
534605 }
606+
607+ /* e1 and e2 are leaves. Compare and process them. */
608+
535609 if (e1 == e2 )
536610 return ;
537611
@@ -568,6 +642,17 @@ static void expr_eliminate_dups1(enum expr_type type, struct expr **ep1, struct
568642#undef e2
569643}
570644
645+ /*
646+ * Rewrites 'e' in-place to remove ("join") duplicate and other redundant
647+ * operands.
648+ *
649+ * Example simplifications:
650+ *
651+ * A || B || A -> A || B
652+ * A && B && A=y -> A=y && B
653+ *
654+ * Returns the deduplicated expression.
655+ */
571656struct expr * expr_eliminate_dups (struct expr * e )
572657{
573658 int oldcount ;
@@ -584,13 +669,20 @@ struct expr *expr_eliminate_dups(struct expr *e)
584669 ;
585670 }
586671 if (!trans_count )
672+ /* No simplifications done in this pass. We're done */
587673 break ;
588674 e = expr_eliminate_yn (e );
589675 }
590676 trans_count = oldcount ;
591677 return e ;
592678}
593679
680+ /*
681+ * Performs various simplifications involving logical operators and
682+ * comparisons.
683+ *
684+ * Allocates and returns a new expression.
685+ */
594686struct expr * expr_transform (struct expr * e )
595687{
596688 struct expr * tmp ;
@@ -805,6 +897,20 @@ bool expr_depends_symbol(struct expr *dep, struct symbol *sym)
805897 return false;
806898}
807899
900+ /*
901+ * Inserts explicit comparisons of type 'type' to symbol 'sym' into the
902+ * expression 'e'.
903+ *
904+ * Examples transformations for type == E_UNEQUAL, sym == &symbol_no:
905+ *
906+ * A -> A!=n
907+ * !A -> A=n
908+ * A && B -> !(A=n || B=n)
909+ * A || B -> !(A=n && B=n)
910+ * A && (B || C) -> !(A=n || (B=n && C=n))
911+ *
912+ * Allocates and returns a new expression.
913+ */
808914struct expr * expr_trans_compare (struct expr * e , enum expr_type type , struct symbol * sym )
809915{
810916 struct expr * e1 , * e2 ;
0 commit comments