1 /* Source code for an implementation of the Omega test, an integer
2 programming algorithm for dependence analysis, by William Pugh,
3 appeared in Supercomputing '91 and CACM Aug 92.
5 This code has no license restrictions, and is considered public
8 Changes copyright (C) 2005, 2006, 2007, 2008, 2009,
9 2010 Free Software Foundation, Inc.
10 Contributed by Sebastian Pop <sebastian.pop@inria.fr>
12 This file is part of GCC.
14 GCC is free software; you can redistribute it and/or modify it under
15 the terms of the GNU General Public License as published by the Free
16 Software Foundation; either version 3, or (at your option) any later
19 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
20 WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 You should have received a copy of the GNU General Public License
25 along with GCC; see the file COPYING3. If not see
26 <http://www.gnu.org/licenses/>. */
28 /* For a detailed description, see "Constraint-Based Array Dependence
29 Analysis" William Pugh, David Wonnacott, TOPLAS'98 and David
31 ftp://ftp.cs.umd.edu/pub/omega/davewThesis/davewThesis.ps.gz
36 #include "coretypes.h"
38 #include "diagnostic-core.h"
39 #include "tree-pass.h"
42 /* When set to true, keep substitution variables. When set to false,
43 resurrect substitution variables (convert substitutions back to EQs). */
44 static bool omega_reduce_with_subs
= true;
46 /* When set to true, omega_simplify_problem checks for problem with no
47 solutions, calling verify_omega_pb. */
48 static bool omega_verify_simplification
= false;
50 /* When set to true, only produce a single simplified result. */
51 static bool omega_single_result
= false;
53 /* Set return_single_result to 1 when omega_single_result is true. */
54 static int return_single_result
= 0;
56 /* Hash table for equations generated by the solver. */
57 #define HASH_TABLE_SIZE PARAM_VALUE (PARAM_OMEGA_HASH_TABLE_SIZE)
58 #define MAX_KEYS PARAM_VALUE (PARAM_OMEGA_MAX_KEYS)
59 static eqn hash_master
;
61 static int hash_version
= 0;
63 /* Set to true for making the solver enter in approximation mode. */
64 static bool in_approximate_mode
= false;
66 /* When set to zero, the solver is allowed to add new equalities to
67 the problem to be solved. */
68 static int conservative
= 0;
70 /* Set to omega_true when the problem was successfully reduced, set to
71 omega_unknown when the solver is unable to determine an answer. */
72 static enum omega_result omega_found_reduction
;
74 /* Set to true when the solver is allowed to add omega_red equations. */
75 static bool create_color
= false;
77 /* Set to nonzero when the problem to be solved can be reduced. */
78 static int may_be_red
= 0;
80 /* When false, there should be no substitution equations in the
81 simplified problem. */
82 static int please_no_equalities_in_simplified_problems
= 0;
84 /* Variables names for pretty printing. */
85 static char wild_name
[200][40];
87 /* Pointer to the void problem. */
88 static omega_pb no_problem
= (omega_pb
) 0;
90 /* Pointer to the problem to be solved. */
91 static omega_pb original_problem
= (omega_pb
) 0;
94 /* Return the integer A divided by B. */
97 int_div (int a
, int b
)
102 return -((-a
+ b
- 1)/b
);
105 /* Return the integer A modulo B. */
108 int_mod (int a
, int b
)
110 return a
- b
* int_div (a
, b
);
113 /* For X and Y positive integers, return X multiplied by Y and check
114 that the result does not overflow. */
117 check_pos_mul (int x
, int y
)
120 gcc_assert ((INT_MAX
) / x
> y
);
125 /* Return X multiplied by Y and check that the result does not
129 check_mul (int x
, int y
)
134 return check_pos_mul (x
, y
);
136 return -check_pos_mul (x
, -y
);
139 return -check_pos_mul (-x
, y
);
141 return check_pos_mul (-x
, -y
);
144 /* Test whether equation E is red. */
147 omega_eqn_is_red (eqn e
, int desired_res
)
149 return (desired_res
== omega_simplify
&& e
->color
== omega_red
);
152 /* Return a string for VARIABLE. */
155 omega_var_to_str (int variable
)
157 if (0 <= variable
&& variable
<= 20)
158 return wild_name
[variable
];
160 if (-20 < variable
&& variable
< 0)
161 return wild_name
[40 + variable
];
163 /* Collapse all the entries that would have overflowed. */
164 return wild_name
[21];
167 /* Return a string for variable I in problem PB. */
170 omega_variable_to_str (omega_pb pb
, int i
)
172 return omega_var_to_str (pb
->var
[i
]);
175 /* Do nothing function: used for default initializations. */
178 omega_no_procedure (omega_pb pb ATTRIBUTE_UNUSED
)
182 void (*omega_when_reduced
) (omega_pb
) = omega_no_procedure
;
184 /* Print to FILE from PB equation E with all its coefficients
188 omega_print_term (FILE *file
, omega_pb pb
, eqn e
, int c
)
192 int n
= pb
->num_vars
;
195 for (i
= 1; i
<= n
; i
++)
196 if (c
* e
->coef
[i
] > 0)
201 if (c
* e
->coef
[i
] == 1)
202 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
204 fprintf (file
, "%d * %s", c
* e
->coef
[i
],
205 omega_variable_to_str (pb
, i
));
209 for (i
= 1; i
<= n
; i
++)
210 if (i
!= went_first
&& c
* e
->coef
[i
] != 0)
212 if (!first
&& c
* e
->coef
[i
] > 0)
213 fprintf (file
, " + ");
217 if (c
* e
->coef
[i
] == 1)
218 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
219 else if (c
* e
->coef
[i
] == -1)
220 fprintf (file
, " - %s", omega_variable_to_str (pb
, i
));
222 fprintf (file
, "%d * %s", c
* e
->coef
[i
],
223 omega_variable_to_str (pb
, i
));
226 if (!first
&& c
* e
->coef
[0] > 0)
227 fprintf (file
, " + ");
229 if (first
|| c
* e
->coef
[0] != 0)
230 fprintf (file
, "%d", c
* e
->coef
[0]);
233 /* Print to FILE the equation E of problem PB. */
236 omega_print_eqn (FILE *file
, omega_pb pb
, eqn e
, bool test
, int extra
)
239 int n
= pb
->num_vars
+ extra
;
240 bool is_lt
= test
&& e
->coef
[0] == -1;
248 else if (e
->key
!= 0)
249 fprintf (file
, "%d: ", e
->key
);
252 if (e
->color
== omega_red
)
257 for (i
= is_lt
? 1 : 0; i
<= n
; i
++)
261 fprintf (file
, " + ");
266 fprintf (file
, "%d", -e
->coef
[i
]);
267 else if (e
->coef
[i
] == -1)
268 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
270 fprintf (file
, "%d * %s", -e
->coef
[i
],
271 omega_variable_to_str (pb
, i
));
286 fprintf (file
, " = ");
288 fprintf (file
, " < ");
290 fprintf (file
, " <= ");
294 for (i
= 0; i
<= n
; i
++)
298 fprintf (file
, " + ");
303 fprintf (file
, "%d", e
->coef
[i
]);
304 else if (e
->coef
[i
] == 1)
305 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
307 fprintf (file
, "%d * %s", e
->coef
[i
],
308 omega_variable_to_str (pb
, i
));
314 if (e
->color
== omega_red
)
318 /* Print to FILE all the variables of problem PB. */
321 omega_print_vars (FILE *file
, omega_pb pb
)
325 fprintf (file
, "variables = ");
327 if (pb
->safe_vars
> 0)
328 fprintf (file
, "protected (");
330 for (i
= 1; i
<= pb
->num_vars
; i
++)
332 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
334 if (i
== pb
->safe_vars
)
337 if (i
< pb
->num_vars
)
338 fprintf (file
, ", ");
341 fprintf (file
, "\n");
344 /* Debug problem PB. */
347 debug_omega_problem (omega_pb pb
)
349 omega_print_problem (stderr
, pb
);
352 /* Print to FILE problem PB. */
355 omega_print_problem (FILE *file
, omega_pb pb
)
359 if (!pb
->variables_initialized
)
360 omega_initialize_variables (pb
);
362 omega_print_vars (file
, pb
);
364 for (e
= 0; e
< pb
->num_eqs
; e
++)
366 omega_print_eq (file
, pb
, &pb
->eqs
[e
]);
367 fprintf (file
, "\n");
370 fprintf (file
, "Done with EQ\n");
372 for (e
= 0; e
< pb
->num_geqs
; e
++)
374 omega_print_geq (file
, pb
, &pb
->geqs
[e
]);
375 fprintf (file
, "\n");
378 fprintf (file
, "Done with GEQ\n");
380 for (e
= 0; e
< pb
->num_subs
; e
++)
382 eqn eq
= &pb
->subs
[e
];
384 if (eq
->color
== omega_red
)
388 fprintf (file
, "%s := ", omega_var_to_str (eq
->key
));
390 fprintf (file
, "#%d := ", eq
->key
);
392 omega_print_term (file
, pb
, eq
, 1);
394 if (eq
->color
== omega_red
)
397 fprintf (file
, "\n");
401 /* Return the number of equations in PB tagged omega_red. */
404 omega_count_red_equations (omega_pb pb
)
409 for (e
= 0; e
< pb
->num_eqs
; e
++)
410 if (pb
->eqs
[e
].color
== omega_red
)
412 for (i
= pb
->num_vars
; i
> 0; i
--)
413 if (pb
->geqs
[e
].coef
[i
])
416 if (i
== 0 && pb
->geqs
[e
].coef
[0] == 1)
422 for (e
= 0; e
< pb
->num_geqs
; e
++)
423 if (pb
->geqs
[e
].color
== omega_red
)
426 for (e
= 0; e
< pb
->num_subs
; e
++)
427 if (pb
->subs
[e
].color
== omega_red
)
433 /* Print to FILE all the equations in PB that are tagged omega_red. */
436 omega_print_red_equations (FILE *file
, omega_pb pb
)
440 if (!pb
->variables_initialized
)
441 omega_initialize_variables (pb
);
443 omega_print_vars (file
, pb
);
445 for (e
= 0; e
< pb
->num_eqs
; e
++)
446 if (pb
->eqs
[e
].color
== omega_red
)
448 omega_print_eq (file
, pb
, &pb
->eqs
[e
]);
449 fprintf (file
, "\n");
452 for (e
= 0; e
< pb
->num_geqs
; e
++)
453 if (pb
->geqs
[e
].color
== omega_red
)
455 omega_print_geq (file
, pb
, &pb
->geqs
[e
]);
456 fprintf (file
, "\n");
459 for (e
= 0; e
< pb
->num_subs
; e
++)
460 if (pb
->subs
[e
].color
== omega_red
)
462 eqn eq
= &pb
->subs
[e
];
466 fprintf (file
, "%s := ", omega_var_to_str (eq
->key
));
468 fprintf (file
, "#%d := ", eq
->key
);
470 omega_print_term (file
, pb
, eq
, 1);
471 fprintf (file
, "]\n");
475 /* Pretty print PB to FILE. */
478 omega_pretty_print_problem (FILE *file
, omega_pb pb
)
480 int e
, v
, v1
, v2
, v3
, t
;
481 bool *live
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
482 int stuffPrinted
= 0;
487 } partial_order_type
;
489 partial_order_type
**po
= XNEWVEC (partial_order_type
*,
490 OMEGA_MAX_VARS
* OMEGA_MAX_VARS
);
491 int **po_eq
= XNEWVEC (int *, OMEGA_MAX_VARS
* OMEGA_MAX_VARS
);
492 int *last_links
= XNEWVEC (int, OMEGA_MAX_VARS
);
493 int *first_links
= XNEWVEC (int, OMEGA_MAX_VARS
);
494 int *chain_length
= XNEWVEC (int, OMEGA_MAX_VARS
);
495 int *chain
= XNEWVEC (int, OMEGA_MAX_VARS
);
499 if (!pb
->variables_initialized
)
500 omega_initialize_variables (pb
);
502 if (pb
->num_vars
> 0)
504 omega_eliminate_redundant (pb
, false);
506 for (e
= 0; e
< pb
->num_eqs
; e
++)
509 fprintf (file
, "; ");
512 omega_print_eq (file
, pb
, &pb
->eqs
[e
]);
515 for (e
= 0; e
< pb
->num_geqs
; e
++)
520 for (v
= 1; v
<= pb
->num_vars
; v
++)
522 last_links
[v
] = first_links
[v
] = 0;
525 for (v2
= 1; v2
<= pb
->num_vars
; v2
++)
529 for (e
= 0; e
< pb
->num_geqs
; e
++)
532 for (v
= 1; v
<= pb
->num_vars
; v
++)
533 if (pb
->geqs
[e
].coef
[v
] == 1)
535 else if (pb
->geqs
[e
].coef
[v
] == -1)
540 while (v1
> 0 && pb
->geqs
[e
].coef
[v1
] == 0)
545 while (v2
> 0 && pb
->geqs
[e
].coef
[v2
] == 0)
550 while (v3
> 0 && pb
->geqs
[e
].coef
[v3
] == 0)
553 if (pb
->geqs
[e
].coef
[0] > 0 || pb
->geqs
[e
].coef
[0] < -1
555 || pb
->geqs
[e
].coef
[v1
] * pb
->geqs
[e
].coef
[v2
] != -1)
557 /* Not a partial order relation. */
561 if (pb
->geqs
[e
].coef
[v1
] == 1)
568 /* Relation is v1 <= v2 or v1 < v2. */
569 po
[v1
][v2
] = ((pb
->geqs
[e
].coef
[0] == 0) ? le
: lt
);
573 for (v
= 1; v
<= pb
->num_vars
; v
++)
574 chain_length
[v
] = last_links
[v
];
576 /* Just in case pb->num_vars <= 0. */
578 for (t
= 0; t
< pb
->num_vars
; t
++)
582 for (v1
= 1; v1
<= pb
->num_vars
; v1
++)
583 for (v2
= 1; v2
<= pb
->num_vars
; v2
++)
584 if (po
[v1
][v2
] != none
&&
585 chain_length
[v1
] <= chain_length
[v2
])
587 chain_length
[v1
] = chain_length
[v2
] + 1;
592 /* Caught in cycle. */
593 gcc_assert (!change
);
595 for (v1
= 1; v1
<= pb
->num_vars
; v1
++)
596 if (chain_length
[v1
] == 0)
601 for (v1
= 2; v1
<= pb
->num_vars
; v1
++)
602 if (chain_length
[v1
] + first_links
[v1
] >
603 chain_length
[v
] + first_links
[v
])
606 if (chain_length
[v
] + first_links
[v
] == 0)
610 fprintf (file
, "; ");
614 /* Chain starts at v. */
619 for (e
= 0; e
< pb
->num_geqs
; e
++)
620 if (live
[e
] && pb
->geqs
[e
].coef
[v
] == 1)
623 fprintf (file
, ", ");
625 tmp
= pb
->geqs
[e
].coef
[v
];
626 pb
->geqs
[e
].coef
[v
] = 0;
627 omega_print_term (file
, pb
, &pb
->geqs
[e
], -1);
628 pb
->geqs
[e
].coef
[v
] = tmp
;
634 fprintf (file
, " <= ");
643 for (v2
= 1; v2
<= pb
->num_vars
; v2
++)
644 if (po
[v
][v2
] && chain_length
[v
] == 1 + chain_length
[v2
])
647 if (v2
> pb
->num_vars
)
654 fprintf (file
, "%s", omega_variable_to_str (pb
, chain
[0]));
656 for (multiprint
= false, i
= 1; i
< m
; i
++)
662 fprintf (file
, " <= ");
664 fprintf (file
, " < ");
666 fprintf (file
, "%s", omega_variable_to_str (pb
, v2
));
667 live
[po_eq
[v
][v2
]] = false;
669 if (!multiprint
&& i
< m
- 1)
670 for (v3
= 1; v3
<= pb
->num_vars
; v3
++)
672 if (v
== v3
|| v2
== v3
673 || po
[v
][v2
] != po
[v
][v3
]
674 || po
[v2
][chain
[i
+ 1]] != po
[v3
][chain
[i
+ 1]])
677 fprintf (file
, ",%s", omega_variable_to_str (pb
, v3
));
678 live
[po_eq
[v
][v3
]] = false;
679 live
[po_eq
[v3
][chain
[i
+ 1]]] = false;
687 /* Print last_links. */
692 for (e
= 0; e
< pb
->num_geqs
; e
++)
693 if (live
[e
] && pb
->geqs
[e
].coef
[v
] == -1)
696 fprintf (file
, ", ");
698 fprintf (file
, " <= ");
700 tmp
= pb
->geqs
[e
].coef
[v
];
701 pb
->geqs
[e
].coef
[v
] = 0;
702 omega_print_term (file
, pb
, &pb
->geqs
[e
], 1);
703 pb
->geqs
[e
].coef
[v
] = tmp
;
710 for (e
= 0; e
< pb
->num_geqs
; e
++)
714 fprintf (file
, "; ");
716 omega_print_geq (file
, pb
, &pb
->geqs
[e
]);
719 for (e
= 0; e
< pb
->num_subs
; e
++)
721 eqn eq
= &pb
->subs
[e
];
724 fprintf (file
, "; ");
729 fprintf (file
, "%s := ", omega_var_to_str (eq
->key
));
731 fprintf (file
, "#%d := ", eq
->key
);
733 omega_print_term (file
, pb
, eq
, 1);
746 /* Assign to variable I in PB the next wildcard name. The name of a
747 wildcard is a negative number. */
748 static int next_wild_card
= 0;
751 omega_name_wild_card (omega_pb pb
, int i
)
754 if (next_wild_card
< -PARAM_VALUE (PARAM_OMEGA_MAX_WILD_CARDS
))
756 pb
->var
[i
] = next_wild_card
;
759 /* Return the index of the last protected (or safe) variable in PB,
760 after having added a new wildcard variable. */
763 omega_add_new_wild_card (omega_pb pb
)
766 int i
= ++pb
->safe_vars
;
769 /* Make a free place in the protected (safe) variables, by moving
770 the non protected variable pointed by "I" at the end, ie. at
771 offset pb->num_vars. */
772 if (pb
->num_vars
!= i
)
774 /* Move "I" for all the inequalities. */
775 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
777 if (pb
->geqs
[e
].coef
[i
])
778 pb
->geqs
[e
].touched
= 1;
780 pb
->geqs
[e
].coef
[pb
->num_vars
] = pb
->geqs
[e
].coef
[i
];
783 /* Move "I" for all the equalities. */
784 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
785 pb
->eqs
[e
].coef
[pb
->num_vars
] = pb
->eqs
[e
].coef
[i
];
787 /* Move "I" for all the substitutions. */
788 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
789 pb
->subs
[e
].coef
[pb
->num_vars
] = pb
->subs
[e
].coef
[i
];
791 /* Move the identifier. */
792 pb
->var
[pb
->num_vars
] = pb
->var
[i
];
795 /* Initialize at zero all the coefficients */
796 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
797 pb
->geqs
[e
].coef
[i
] = 0;
799 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
800 pb
->eqs
[e
].coef
[i
] = 0;
802 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
803 pb
->subs
[e
].coef
[i
] = 0;
805 /* And give it a name. */
806 omega_name_wild_card (pb
, i
);
810 /* Delete inequality E from problem PB that has N_VARS variables. */
813 omega_delete_geq (omega_pb pb
, int e
, int n_vars
)
815 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
817 fprintf (dump_file
, "Deleting %d (last:%d): ", e
, pb
->num_geqs
- 1);
818 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
819 fprintf (dump_file
, "\n");
822 if (e
< pb
->num_geqs
- 1)
823 omega_copy_eqn (&pb
->geqs
[e
], &pb
->geqs
[pb
->num_geqs
- 1], n_vars
);
828 /* Delete extra inequality E from problem PB that has N_VARS
832 omega_delete_geq_extra (omega_pb pb
, int e
, int n_vars
)
834 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
836 fprintf (dump_file
, "Deleting %d: ",e
);
837 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[e
]);
838 fprintf (dump_file
, "\n");
841 if (e
< pb
->num_geqs
- 1)
842 omega_copy_eqn (&pb
->geqs
[e
], &pb
->geqs
[pb
->num_geqs
- 1], n_vars
);
848 /* Remove variable I from problem PB. */
851 omega_delete_variable (omega_pb pb
, int i
)
853 int n_vars
= pb
->num_vars
;
856 if (omega_safe_var_p (pb
, i
))
858 int j
= pb
->safe_vars
;
860 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
862 pb
->geqs
[e
].touched
= 1;
863 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[j
];
864 pb
->geqs
[e
].coef
[j
] = pb
->geqs
[e
].coef
[n_vars
];
867 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
869 pb
->eqs
[e
].coef
[i
] = pb
->eqs
[e
].coef
[j
];
870 pb
->eqs
[e
].coef
[j
] = pb
->eqs
[e
].coef
[n_vars
];
873 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
875 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[j
];
876 pb
->subs
[e
].coef
[j
] = pb
->subs
[e
].coef
[n_vars
];
879 pb
->var
[i
] = pb
->var
[j
];
880 pb
->var
[j
] = pb
->var
[n_vars
];
884 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
885 if (pb
->geqs
[e
].coef
[n_vars
])
887 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[n_vars
];
888 pb
->geqs
[e
].touched
= 1;
891 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
892 pb
->eqs
[e
].coef
[i
] = pb
->eqs
[e
].coef
[n_vars
];
894 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
895 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[n_vars
];
897 pb
->var
[i
] = pb
->var
[n_vars
];
900 if (omega_safe_var_p (pb
, i
))
906 /* Because the coefficients of an equation are sparse, PACKING records
907 indices for non null coefficients. */
910 /* Set up the coefficients of PACKING, following the coefficients of
911 equation EQN that has NUM_VARS variables. */
914 setup_packing (eqn eqn
, int num_vars
)
919 for (k
= num_vars
; k
>= 0; k
--)
926 /* Computes a linear combination of EQ and SUB at VAR with coefficient
927 C, such that EQ->coef[VAR] is set to 0. TOP_VAR is the number of
928 non null indices of SUB stored in PACKING. */
931 omega_substitute_red_1 (eqn eq
, eqn sub
, int var
, int c
, bool *found_black
,
934 if (eq
->coef
[var
] != 0)
936 if (eq
->color
== omega_black
)
940 int j
, k
= eq
->coef
[var
];
944 for (j
= top_var
; j
>= 0; j
--)
945 eq
->coef
[packing
[j
]] -= sub
->coef
[packing
[j
]] * k
* c
;
950 /* Substitute in PB variable VAR with "C * SUB". */
953 omega_substitute_red (omega_pb pb
, eqn sub
, int var
, int c
, bool *found_black
)
955 int e
, top_var
= setup_packing (sub
, pb
->num_vars
);
957 *found_black
= false;
959 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
961 if (sub
->color
== omega_red
)
962 fprintf (dump_file
, "[");
964 fprintf (dump_file
, "substituting using %s := ",
965 omega_variable_to_str (pb
, var
));
966 omega_print_term (dump_file
, pb
, sub
, -c
);
968 if (sub
->color
== omega_red
)
969 fprintf (dump_file
, "]");
971 fprintf (dump_file
, "\n");
972 omega_print_vars (dump_file
, pb
);
975 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
977 eqn eqn
= &(pb
->eqs
[e
]);
979 omega_substitute_red_1 (eqn
, sub
, var
, c
, found_black
, top_var
);
981 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
983 omega_print_eq (dump_file
, pb
, eqn
);
984 fprintf (dump_file
, "\n");
988 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
990 eqn eqn
= &(pb
->geqs
[e
]);
992 omega_substitute_red_1 (eqn
, sub
, var
, c
, found_black
, top_var
);
994 if (eqn
->coef
[var
] && eqn
->color
== omega_red
)
997 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
999 omega_print_geq (dump_file
, pb
, eqn
);
1000 fprintf (dump_file
, "\n");
1004 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1006 eqn eqn
= &(pb
->subs
[e
]);
1008 omega_substitute_red_1 (eqn
, sub
, var
, c
, found_black
, top_var
);
1010 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1012 fprintf (dump_file
, "%s := ", omega_var_to_str (eqn
->key
));
1013 omega_print_term (dump_file
, pb
, eqn
, 1);
1014 fprintf (dump_file
, "\n");
1018 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1019 fprintf (dump_file
, "---\n\n");
1021 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1022 *found_black
= true;
1025 /* Substitute in PB variable VAR with "C * SUB". */
1028 omega_substitute (omega_pb pb
, eqn sub
, int var
, int c
)
1031 int top_var
= setup_packing (sub
, pb
->num_vars
);
1033 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1035 fprintf (dump_file
, "substituting using %s := ",
1036 omega_variable_to_str (pb
, var
));
1037 omega_print_term (dump_file
, pb
, sub
, -c
);
1038 fprintf (dump_file
, "\n");
1039 omega_print_vars (dump_file
, pb
);
1044 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1045 pb
->eqs
[e
].coef
[var
] = 0;
1047 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1048 if (pb
->geqs
[e
].coef
[var
] != 0)
1050 pb
->geqs
[e
].touched
= 1;
1051 pb
->geqs
[e
].coef
[var
] = 0;
1054 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1055 pb
->subs
[e
].coef
[var
] = 0;
1057 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1060 eqn eqn
= &(pb
->subs
[pb
->num_subs
++]);
1062 for (k
= pb
->num_vars
; k
>= 0; k
--)
1065 eqn
->key
= pb
->var
[var
];
1066 eqn
->color
= omega_black
;
1069 else if (top_var
== 0 && packing
[0] == 0)
1071 c
= -sub
->coef
[0] * c
;
1073 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1075 pb
->eqs
[e
].coef
[0] += pb
->eqs
[e
].coef
[var
] * c
;
1076 pb
->eqs
[e
].coef
[var
] = 0;
1079 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1080 if (pb
->geqs
[e
].coef
[var
] != 0)
1082 pb
->geqs
[e
].coef
[0] += pb
->geqs
[e
].coef
[var
] * c
;
1083 pb
->geqs
[e
].coef
[var
] = 0;
1084 pb
->geqs
[e
].touched
= 1;
1087 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1089 pb
->subs
[e
].coef
[0] += pb
->subs
[e
].coef
[var
] * c
;
1090 pb
->subs
[e
].coef
[var
] = 0;
1093 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1096 eqn eqn
= &(pb
->subs
[pb
->num_subs
++]);
1098 for (k
= pb
->num_vars
; k
>= 1; k
--)
1102 eqn
->key
= pb
->var
[var
];
1103 eqn
->color
= omega_black
;
1106 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1108 fprintf (dump_file
, "---\n\n");
1109 omega_print_problem (dump_file
, pb
);
1110 fprintf (dump_file
, "===\n\n");
1115 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1117 eqn eqn
= &(pb
->eqs
[e
]);
1118 int k
= eqn
->coef
[var
];
1125 for (j
= top_var
; j
>= 0; j
--)
1128 eqn
->coef
[j0
] -= sub
->coef
[j0
] * k
;
1132 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1134 omega_print_eq (dump_file
, pb
, eqn
);
1135 fprintf (dump_file
, "\n");
1139 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1141 eqn eqn
= &(pb
->geqs
[e
]);
1142 int k
= eqn
->coef
[var
];
1150 for (j
= top_var
; j
>= 0; j
--)
1153 eqn
->coef
[j0
] -= sub
->coef
[j0
] * k
;
1157 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1159 omega_print_geq (dump_file
, pb
, eqn
);
1160 fprintf (dump_file
, "\n");
1164 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1166 eqn eqn
= &(pb
->subs
[e
]);
1167 int k
= eqn
->coef
[var
];
1174 for (j
= top_var
; j
>= 0; j
--)
1177 eqn
->coef
[j0
] -= sub
->coef
[j0
] * k
;
1181 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1183 fprintf (dump_file
, "%s := ", omega_var_to_str (eqn
->key
));
1184 omega_print_term (dump_file
, pb
, eqn
, 1);
1185 fprintf (dump_file
, "\n");
1189 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1191 fprintf (dump_file
, "---\n\n");
1192 omega_print_problem (dump_file
, pb
);
1193 fprintf (dump_file
, "===\n\n");
1196 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1199 eqn eqn
= &(pb
->subs
[pb
->num_subs
++]);
1202 for (k
= pb
->num_vars
; k
>= 0; k
--)
1203 eqn
->coef
[k
] = c
* (sub
->coef
[k
]);
1205 eqn
->key
= pb
->var
[var
];
1206 eqn
->color
= sub
->color
;
1211 /* Solve e = factor alpha for x_j and substitute. */
1214 omega_do_mod (omega_pb pb
, int factor
, int e
, int j
)
1217 eqn eq
= omega_alloc_eqns (0, 1);
1219 bool kill_j
= false;
1221 omega_copy_eqn (eq
, &pb
->eqs
[e
], pb
->num_vars
);
1223 for (k
= pb
->num_vars
; k
>= 0; k
--)
1225 eq
->coef
[k
] = int_mod (eq
->coef
[k
], factor
);
1227 if (2 * eq
->coef
[k
] >= factor
)
1228 eq
->coef
[k
] -= factor
;
1231 nfactor
= eq
->coef
[j
];
1233 if (omega_safe_var_p (pb
, j
) && !omega_wildcard_p (pb
, j
))
1235 i
= omega_add_new_wild_card (pb
);
1236 eq
->coef
[pb
->num_vars
] = eq
->coef
[i
];
1238 eq
->coef
[i
] = -factor
;
1243 eq
->coef
[j
] = -factor
;
1244 if (!omega_wildcard_p (pb
, j
))
1245 omega_name_wild_card (pb
, j
);
1248 omega_substitute (pb
, eq
, j
, nfactor
);
1250 for (k
= pb
->num_vars
; k
>= 0; k
--)
1251 pb
->eqs
[e
].coef
[k
] = pb
->eqs
[e
].coef
[k
] / factor
;
1254 omega_delete_variable (pb
, j
);
1256 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1258 fprintf (dump_file
, "Mod-ing and normalizing produces:\n");
1259 omega_print_problem (dump_file
, pb
);
1262 omega_free_eqns (eq
, 1);
1265 /* Multiplies by -1 inequality E. */
1268 omega_negate_geq (omega_pb pb
, int e
)
1272 for (i
= pb
->num_vars
; i
>= 0; i
--)
1273 pb
->geqs
[e
].coef
[i
] *= -1;
1275 pb
->geqs
[e
].coef
[0]--;
1276 pb
->geqs
[e
].touched
= 1;
1279 /* Returns OMEGA_TRUE when problem PB has a solution. */
1281 static enum omega_result
1282 verify_omega_pb (omega_pb pb
)
1284 enum omega_result result
;
1286 bool any_color
= false;
1287 omega_pb tmp_problem
= XNEW (struct omega_pb_d
);
1289 omega_copy_problem (tmp_problem
, pb
);
1290 tmp_problem
->safe_vars
= 0;
1291 tmp_problem
->num_subs
= 0;
1293 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1294 if (pb
->geqs
[e
].color
== omega_red
)
1300 if (please_no_equalities_in_simplified_problems
)
1304 original_problem
= no_problem
;
1306 original_problem
= pb
;
1308 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1310 fprintf (dump_file
, "verifying problem");
1313 fprintf (dump_file
, " (color mode)");
1315 fprintf (dump_file
, " :\n");
1316 omega_print_problem (dump_file
, pb
);
1319 result
= omega_solve_problem (tmp_problem
, omega_unknown
);
1320 original_problem
= no_problem
;
1323 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1325 if (result
!= omega_false
)
1326 fprintf (dump_file
, "verified problem\n");
1328 fprintf (dump_file
, "disproved problem\n");
1329 omega_print_problem (dump_file
, pb
);
1335 /* Add a new equality to problem PB at last position E. */
1338 adding_equality_constraint (omega_pb pb
, int e
)
1340 if (original_problem
!= no_problem
1341 && original_problem
!= pb
1345 int e2
= original_problem
->num_eqs
++;
1347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1349 "adding equality constraint %d to outer problem\n", e2
);
1350 omega_init_eqn_zero (&original_problem
->eqs
[e2
],
1351 original_problem
->num_vars
);
1353 for (i
= pb
->num_vars
; i
>= 1; i
--)
1355 for (j
= original_problem
->num_vars
; j
>= 1; j
--)
1356 if (original_problem
->var
[j
] == pb
->var
[i
])
1361 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1362 fprintf (dump_file
, "retracting\n");
1363 original_problem
->num_eqs
--;
1366 original_problem
->eqs
[e2
].coef
[j
] = pb
->eqs
[e
].coef
[i
];
1369 original_problem
->eqs
[e2
].coef
[0] = pb
->eqs
[e
].coef
[0];
1371 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1372 omega_print_problem (dump_file
, original_problem
);
1376 static int *fast_lookup
;
1377 static int *fast_lookup_red
;
1381 normalize_uncoupled
,
1383 } normalize_return_type
;
1385 /* Normalizes PB by removing redundant constraints. Returns
1386 normalize_false when the constraints system has no solution,
1387 otherwise returns normalize_coupled or normalize_uncoupled. */
1389 static normalize_return_type
1390 normalize_omega_problem (omega_pb pb
)
1392 int e
, i
, j
, k
, n_vars
;
1393 int coupled_subscripts
= 0;
1395 n_vars
= pb
->num_vars
;
1397 for (e
= 0; e
< pb
->num_geqs
; e
++)
1399 if (!pb
->geqs
[e
].touched
)
1401 if (!single_var_geq (&pb
->geqs
[e
], n_vars
))
1402 coupled_subscripts
= 1;
1406 int g
, top_var
, i0
, hashCode
;
1407 int *p
= &packing
[0];
1409 for (k
= 1; k
<= n_vars
; k
++)
1410 if (pb
->geqs
[e
].coef
[k
])
1413 top_var
= (p
- &packing
[0]) - 1;
1417 if (pb
->geqs
[e
].coef
[0] < 0)
1419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1421 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1422 fprintf (dump_file
, "\nequations have no solution \n");
1424 return normalize_false
;
1427 omega_delete_geq (pb
, e
, n_vars
);
1431 else if (top_var
== 0)
1433 int singlevar
= packing
[0];
1435 g
= pb
->geqs
[e
].coef
[singlevar
];
1439 pb
->geqs
[e
].coef
[singlevar
] = 1;
1440 pb
->geqs
[e
].key
= singlevar
;
1445 pb
->geqs
[e
].coef
[singlevar
] = -1;
1446 pb
->geqs
[e
].key
= -singlevar
;
1450 pb
->geqs
[e
].coef
[0] = int_div (pb
->geqs
[e
].coef
[0], g
);
1455 int hash_key_multiplier
= 31;
1457 coupled_subscripts
= 1;
1460 g
= pb
->geqs
[e
].coef
[i
];
1461 hashCode
= g
* (i
+ 3);
1466 for (; i0
>= 0; i0
--)
1471 x
= pb
->geqs
[e
].coef
[i
];
1472 hashCode
= hashCode
* hash_key_multiplier
* (i
+ 3) + x
;
1487 for (; i0
>= 0; i0
--)
1491 x
= pb
->geqs
[e
].coef
[i
];
1492 hashCode
= hashCode
* hash_key_multiplier
* (i
+ 3) + x
;
1497 pb
->geqs
[e
].coef
[0] = int_div (pb
->geqs
[e
].coef
[0], g
);
1500 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[i
] / g
;
1501 hashCode
= pb
->geqs
[e
].coef
[i
] * (i
+ 3);
1503 for (; i0
>= 0; i0
--)
1506 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[i
] / g
;
1507 hashCode
= hashCode
* hash_key_multiplier
* (i
+ 3)
1508 + pb
->geqs
[e
].coef
[i
];
1512 g2
= abs (hashCode
);
1514 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1516 fprintf (dump_file
, "Hash code = %d, eqn = ", hashCode
);
1517 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1518 fprintf (dump_file
, "\n");
1521 j
= g2
% HASH_TABLE_SIZE
;
1524 eqn proto
= &(hash_master
[j
]);
1526 if (proto
->touched
== g2
)
1528 if (proto
->coef
[0] == top_var
)
1531 for (i0
= top_var
; i0
>= 0; i0
--)
1535 if (pb
->geqs
[e
].coef
[i
] != proto
->coef
[i
])
1539 for (i0
= top_var
; i0
>= 0; i0
--)
1543 if (pb
->geqs
[e
].coef
[i
] != -proto
->coef
[i
])
1550 pb
->geqs
[e
].key
= proto
->key
;
1552 pb
->geqs
[e
].key
= -proto
->key
;
1557 else if (proto
->touched
< 0)
1559 omega_init_eqn_zero (proto
, pb
->num_vars
);
1561 for (i0
= top_var
; i0
>= 0; i0
--)
1564 proto
->coef
[i
] = pb
->geqs
[e
].coef
[i
];
1567 for (i0
= top_var
; i0
>= 0; i0
--)
1570 proto
->coef
[i
] = -pb
->geqs
[e
].coef
[i
];
1573 proto
->coef
[0] = top_var
;
1574 proto
->touched
= g2
;
1576 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1577 fprintf (dump_file
, " constraint key = %d\n",
1580 proto
->key
= next_key
++;
1582 /* Too many hash keys generated. */
1583 gcc_assert (proto
->key
<= MAX_KEYS
);
1586 pb
->geqs
[e
].key
= proto
->key
;
1588 pb
->geqs
[e
].key
= -proto
->key
;
1593 j
= (j
+ 1) % HASH_TABLE_SIZE
;
1597 pb
->geqs
[e
].touched
= 0;
1601 int eKey
= pb
->geqs
[e
].key
;
1605 int cTerm
= pb
->geqs
[e
].coef
[0];
1606 e2
= fast_lookup
[MAX_KEYS
- eKey
];
1608 if (e2
< e
&& pb
->geqs
[e2
].key
== -eKey
1609 && pb
->geqs
[e2
].color
== omega_black
)
1611 if (pb
->geqs
[e2
].coef
[0] < -cTerm
)
1613 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1615 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1616 fprintf (dump_file
, "\n");
1617 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e2
]);
1619 "\nequations have no solution \n");
1621 return normalize_false
;
1624 if (pb
->geqs
[e2
].coef
[0] == -cTerm
1626 || pb
->geqs
[e
].color
== omega_black
))
1628 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
], &pb
->geqs
[e
],
1630 if (pb
->geqs
[e
].color
== omega_black
)
1631 adding_equality_constraint (pb
, pb
->num_eqs
);
1633 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
1637 e2
= fast_lookup_red
[MAX_KEYS
- eKey
];
1639 if (e2
< e
&& pb
->geqs
[e2
].key
== -eKey
1640 && pb
->geqs
[e2
].color
== omega_red
)
1642 if (pb
->geqs
[e2
].coef
[0] < -cTerm
)
1644 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1646 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1647 fprintf (dump_file
, "\n");
1648 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e2
]);
1650 "\nequations have no solution \n");
1652 return normalize_false
;
1655 if (pb
->geqs
[e2
].coef
[0] == -cTerm
&& create_color
)
1657 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
], &pb
->geqs
[e
],
1659 pb
->eqs
[pb
->num_eqs
].color
= omega_red
;
1661 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
1665 e2
= fast_lookup
[MAX_KEYS
+ eKey
];
1667 if (e2
< e
&& pb
->geqs
[e2
].key
== eKey
1668 && pb
->geqs
[e2
].color
== omega_black
)
1670 if (pb
->geqs
[e2
].coef
[0] > cTerm
)
1672 if (pb
->geqs
[e
].color
== omega_black
)
1674 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1677 "Removing Redundant Equation: ");
1678 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1679 fprintf (dump_file
, "\n");
1681 "[a] Made Redundant by: ");
1682 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1683 fprintf (dump_file
, "\n");
1685 pb
->geqs
[e2
].coef
[0] = cTerm
;
1686 omega_delete_geq (pb
, e
, n_vars
);
1693 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1695 fprintf (dump_file
, "Removing Redundant Equation: ");
1696 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1697 fprintf (dump_file
, "\n");
1698 fprintf (dump_file
, "[b] Made Redundant by: ");
1699 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1700 fprintf (dump_file
, "\n");
1702 omega_delete_geq (pb
, e
, n_vars
);
1708 e2
= fast_lookup_red
[MAX_KEYS
+ eKey
];
1710 if (e2
< e
&& pb
->geqs
[e2
].key
== eKey
1711 && pb
->geqs
[e2
].color
== omega_red
)
1713 if (pb
->geqs
[e2
].coef
[0] >= cTerm
)
1715 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1717 fprintf (dump_file
, "Removing Redundant Equation: ");
1718 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1719 fprintf (dump_file
, "\n");
1720 fprintf (dump_file
, "[c] Made Redundant by: ");
1721 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1722 fprintf (dump_file
, "\n");
1724 pb
->geqs
[e2
].coef
[0] = cTerm
;
1725 pb
->geqs
[e2
].color
= pb
->geqs
[e
].color
;
1727 else if (pb
->geqs
[e
].color
== omega_red
)
1729 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1731 fprintf (dump_file
, "Removing Redundant Equation: ");
1732 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1733 fprintf (dump_file
, "\n");
1734 fprintf (dump_file
, "[d] Made Redundant by: ");
1735 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1736 fprintf (dump_file
, "\n");
1739 omega_delete_geq (pb
, e
, n_vars
);
1746 if (pb
->geqs
[e
].color
== omega_red
)
1747 fast_lookup_red
[MAX_KEYS
+ eKey
] = e
;
1749 fast_lookup
[MAX_KEYS
+ eKey
] = e
;
1753 create_color
= false;
1754 return coupled_subscripts
? normalize_coupled
: normalize_uncoupled
;
1757 /* Divide the coefficients of EQN by their gcd. N_VARS is the number
1758 of variables in EQN. */
1761 divide_eqn_by_gcd (eqn eqn
, int n_vars
)
1765 for (var
= n_vars
; var
>= 0; var
--)
1766 g
= gcd (abs (eqn
->coef
[var
]), g
);
1769 for (var
= n_vars
; var
>= 0; var
--)
1770 eqn
->coef
[var
] = eqn
->coef
[var
] / g
;
1773 /* Rewrite some non-safe variables in function of protected
1774 wildcard variables. */
1777 cleanout_wildcards (omega_pb pb
)
1780 int n_vars
= pb
->num_vars
;
1781 bool renormalize
= false;
1783 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1784 for (i
= n_vars
; !omega_safe_var_p (pb
, i
); i
--)
1785 if (pb
->eqs
[e
].coef
[i
] != 0)
1787 /* i is the last nonzero non-safe variable. */
1789 for (j
= i
- 1; !omega_safe_var_p (pb
, j
); j
--)
1790 if (pb
->eqs
[e
].coef
[j
] != 0)
1793 /* j is the next nonzero non-safe variable, or points
1794 to a safe variable: it is then a wildcard variable. */
1797 if (omega_safe_var_p (pb
, j
))
1799 eqn sub
= &(pb
->eqs
[e
]);
1800 int c
= pb
->eqs
[e
].coef
[i
];
1804 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1807 "Found a single wild card equality: ");
1808 omega_print_eq (dump_file
, pb
, &pb
->eqs
[e
]);
1809 fprintf (dump_file
, "\n");
1810 omega_print_problem (dump_file
, pb
);
1813 for (e2
= pb
->num_eqs
- 1; e2
>= 0; e2
--)
1814 if (e
!= e2
&& pb
->eqs
[e2
].coef
[i
]
1815 && (pb
->eqs
[e2
].color
== omega_red
1816 || (pb
->eqs
[e2
].color
== omega_black
1817 && pb
->eqs
[e
].color
== omega_black
)))
1819 eqn eqn
= &(pb
->eqs
[e2
]);
1822 for (var
= n_vars
; var
>= 0; var
--)
1823 eqn
->coef
[var
] *= a
;
1827 for (var
= n_vars
; var
>= 0; var
--)
1828 eqn
->coef
[var
] -= sub
->coef
[var
] * k
/ c
;
1831 divide_eqn_by_gcd (eqn
, n_vars
);
1834 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
1835 if (pb
->geqs
[e2
].coef
[i
]
1836 && (pb
->geqs
[e2
].color
== omega_red
1837 || (pb
->eqs
[e
].color
== omega_black
1838 && pb
->geqs
[e2
].color
== omega_black
)))
1840 eqn eqn
= &(pb
->geqs
[e2
]);
1843 for (var
= n_vars
; var
>= 0; var
--)
1844 eqn
->coef
[var
] *= a
;
1848 for (var
= n_vars
; var
>= 0; var
--)
1849 eqn
->coef
[var
] -= sub
->coef
[var
] * k
/ c
;
1856 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
1857 if (pb
->subs
[e2
].coef
[i
]
1858 && (pb
->subs
[e2
].color
== omega_red
1859 || (pb
->subs
[e2
].color
== omega_black
1860 && pb
->eqs
[e
].color
== omega_black
)))
1862 eqn eqn
= &(pb
->subs
[e2
]);
1865 for (var
= n_vars
; var
>= 0; var
--)
1866 eqn
->coef
[var
] *= a
;
1870 for (var
= n_vars
; var
>= 0; var
--)
1871 eqn
->coef
[var
] -= sub
->coef
[var
] * k
/ c
;
1874 divide_eqn_by_gcd (eqn
, n_vars
);
1877 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1879 fprintf (dump_file
, "cleaned-out wildcard: ");
1880 omega_print_problem (dump_file
, pb
);
1887 normalize_omega_problem (pb
);
1890 /* Swap values contained in I and J. */
1893 swap (int *i
, int *j
)
1901 /* Swap values contained in I and J. */
1904 bswap (bool *i
, bool *j
)
1912 /* Make variable IDX unprotected in PB, by swapping its index at the
1913 PB->safe_vars rank. */
1916 omega_unprotect_1 (omega_pb pb
, int *idx
, bool *unprotect
)
1918 /* If IDX is protected... */
1919 if (*idx
< pb
->safe_vars
)
1921 /* ... swap its index with the last non protected index. */
1922 int j
= pb
->safe_vars
;
1925 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1927 pb
->geqs
[e
].touched
= 1;
1928 swap (&pb
->geqs
[e
].coef
[*idx
], &pb
->geqs
[e
].coef
[j
]);
1931 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1932 swap (&pb
->eqs
[e
].coef
[*idx
], &pb
->eqs
[e
].coef
[j
]);
1934 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1935 swap (&pb
->subs
[e
].coef
[*idx
], &pb
->subs
[e
].coef
[j
]);
1938 bswap (&unprotect
[*idx
], &unprotect
[j
]);
1940 swap (&pb
->var
[*idx
], &pb
->var
[j
]);
1941 pb
->forwarding_address
[pb
->var
[*idx
]] = *idx
;
1942 pb
->forwarding_address
[pb
->var
[j
]] = j
;
1946 /* The variable at pb->safe_vars is also unprotected now. */
1950 /* During the Fourier-Motzkin elimination some variables are
1951 substituted with other variables. This function resurrects the
1952 substituted variables in PB. */
1955 resurrect_subs (omega_pb pb
)
1957 if (pb
->num_subs
> 0
1958 && please_no_equalities_in_simplified_problems
== 0)
1962 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1965 "problem reduced, bringing variables back to life\n");
1966 omega_print_problem (dump_file
, pb
);
1969 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
1970 if (omega_wildcard_p (pb
, i
))
1971 omega_unprotect_1 (pb
, &i
, NULL
);
1975 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1976 if (single_var_geq (&pb
->geqs
[e
], pb
->num_vars
))
1978 if (!omega_safe_var_p (pb
, abs (pb
->geqs
[e
].key
)))
1979 pb
->geqs
[e
].key
+= (pb
->geqs
[e
].key
> 0 ? m
: -m
);
1983 pb
->geqs
[e
].touched
= 1;
1984 pb
->geqs
[e
].key
= 0;
1987 for (i
= pb
->num_vars
; !omega_safe_var_p (pb
, i
); i
--)
1989 pb
->var
[i
+ m
] = pb
->var
[i
];
1991 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1992 pb
->geqs
[e
].coef
[i
+ m
] = pb
->geqs
[e
].coef
[i
];
1994 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1995 pb
->eqs
[e
].coef
[i
+ m
] = pb
->eqs
[e
].coef
[i
];
1997 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1998 pb
->subs
[e
].coef
[i
+ m
] = pb
->subs
[e
].coef
[i
];
2001 for (i
= pb
->safe_vars
+ m
; !omega_safe_var_p (pb
, i
); i
--)
2003 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2004 pb
->geqs
[e
].coef
[i
] = 0;
2006 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
2007 pb
->eqs
[e
].coef
[i
] = 0;
2009 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2010 pb
->subs
[e
].coef
[i
] = 0;
2015 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2017 pb
->var
[pb
->safe_vars
+ 1 + e
] = pb
->subs
[e
].key
;
2018 omega_copy_eqn (&(pb
->eqs
[pb
->num_eqs
]), &(pb
->subs
[e
]),
2020 pb
->eqs
[pb
->num_eqs
].coef
[pb
->safe_vars
+ 1 + e
] = -1;
2021 pb
->eqs
[pb
->num_eqs
].color
= omega_black
;
2023 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2025 fprintf (dump_file
, "brought back: ");
2026 omega_print_eq (dump_file
, pb
, &pb
->eqs
[pb
->num_eqs
]);
2027 fprintf (dump_file
, "\n");
2031 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2037 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2039 fprintf (dump_file
, "variables brought back to life\n");
2040 omega_print_problem (dump_file
, pb
);
2043 cleanout_wildcards (pb
);
2048 implies (unsigned int a
, unsigned int b
)
2050 return (a
== (a
& b
));
2053 /* Eliminate redundant equations in PB. When EXPENSIVE is true, an
2054 extra step is performed. Returns omega_false when there exist no
2055 solution, omega_true otherwise. */
2058 omega_eliminate_redundant (omega_pb pb
, bool expensive
)
2060 int c
, e
, e1
, e2
, e3
, p
, q
, i
, k
, alpha
, alpha1
, alpha2
, alpha3
;
2061 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2062 omega_pb tmp_problem
;
2064 /* {P,Z,N}EQS = {Positive,Zero,Negative} Equations. */
2065 unsigned int *peqs
= XNEWVEC (unsigned int, OMEGA_MAX_GEQS
);
2066 unsigned int *zeqs
= XNEWVEC (unsigned int, OMEGA_MAX_GEQS
);
2067 unsigned int *neqs
= XNEWVEC (unsigned int, OMEGA_MAX_GEQS
);
2069 /* PP = Possible Positives, PZ = Possible Zeros, PN = Possible Negatives */
2070 unsigned int pp
, pz
, pn
;
2072 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2074 fprintf (dump_file
, "in eliminate Redundant:\n");
2075 omega_print_problem (dump_file
, pb
);
2078 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2083 peqs
[e
] = zeqs
[e
] = neqs
[e
] = 0;
2085 for (i
= pb
->num_vars
; i
>= 1; i
--)
2087 if (pb
->geqs
[e
].coef
[i
] > 0)
2089 else if (pb
->geqs
[e
].coef
[i
] < 0)
2099 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
2101 for (e2
= e1
- 1; e2
>= 0; e2
--)
2104 for (p
= pb
->num_vars
; p
> 1; p
--)
2105 for (q
= p
- 1; q
> 0; q
--)
2106 if ((alpha
= pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e2
].coef
[q
]
2107 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e1
].coef
[q
]) != 0)
2113 pz
= ((zeqs
[e1
] & zeqs
[e2
]) | (peqs
[e1
] & neqs
[e2
])
2114 | (neqs
[e1
] & peqs
[e2
]));
2115 pp
= peqs
[e1
] | peqs
[e2
];
2116 pn
= neqs
[e1
] | neqs
[e2
];
2118 for (e3
= pb
->num_geqs
- 1; e3
>= 0; e3
--)
2119 if (e3
!= e1
&& e3
!= e2
)
2121 if (!implies (zeqs
[e3
], pz
))
2124 alpha1
= (pb
->geqs
[e2
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2125 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2126 alpha2
= -(pb
->geqs
[e1
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2127 - pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2130 if (alpha1
* alpha2
<= 0)
2142 /* Trying to prove e3 is redundant. */
2143 if (!implies (peqs
[e3
], pp
)
2144 || !implies (neqs
[e3
], pn
))
2147 if (pb
->geqs
[e3
].color
== omega_black
2148 && (pb
->geqs
[e1
].color
== omega_red
2149 || pb
->geqs
[e2
].color
== omega_red
))
2152 for (k
= pb
->num_vars
; k
>= 1; k
--)
2153 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2154 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2155 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2158 c
= (alpha1
* pb
->geqs
[e1
].coef
[0]
2159 + alpha2
* pb
->geqs
[e2
].coef
[0]);
2161 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] + 1))
2163 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2166 "found redundant inequality\n");
2168 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2169 alpha1
, alpha2
, alpha3
);
2171 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2172 fprintf (dump_file
, "\n");
2173 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2174 fprintf (dump_file
, "\n=> ");
2175 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2176 fprintf (dump_file
, "\n\n");
2184 /* Trying to prove e3 <= 0 and therefore e3 = 0,
2185 or trying to prove e3 < 0, and therefore the
2186 problem has no solutions. */
2187 if (!implies (peqs
[e3
], pn
)
2188 || !implies (neqs
[e3
], pp
))
2191 if (pb
->geqs
[e1
].color
== omega_red
2192 || pb
->geqs
[e2
].color
== omega_red
2193 || pb
->geqs
[e3
].color
== omega_red
)
2196 /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */
2197 for (k
= pb
->num_vars
; k
>= 1; k
--)
2198 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2199 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2200 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2203 c
= (alpha1
* pb
->geqs
[e1
].coef
[0]
2204 + alpha2
* pb
->geqs
[e2
].coef
[0]);
2206 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0]))
2208 /* We just proved e3 < 0, so no solutions exist. */
2209 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2212 "found implied over tight inequality\n");
2214 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2215 alpha1
, alpha2
, -alpha3
);
2216 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2217 fprintf (dump_file
, "\n");
2218 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2219 fprintf (dump_file
, "\n=> not ");
2220 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2221 fprintf (dump_file
, "\n\n");
2229 else if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] - 1))
2231 /* We just proved that e3 <=0, so e3 = 0. */
2232 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2235 "found implied tight inequality\n");
2237 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2238 alpha1
, alpha2
, -alpha3
);
2239 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2240 fprintf (dump_file
, "\n");
2241 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2242 fprintf (dump_file
, "\n=> inverse ");
2243 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2244 fprintf (dump_file
, "\n\n");
2247 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
++],
2248 &pb
->geqs
[e3
], pb
->num_vars
);
2249 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2250 adding_equality_constraint (pb
, pb
->num_eqs
- 1);
2258 /* Delete the inequalities that were marked as dead. */
2259 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2261 omega_delete_geq (pb
, e
, pb
->num_vars
);
2264 goto eliminate_redundant_done
;
2266 tmp_problem
= XNEW (struct omega_pb_d
);
2269 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2271 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2274 "checking equation %d to see if it is redundant: ", e
);
2275 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2276 fprintf (dump_file
, "\n");
2279 omega_copy_problem (tmp_problem
, pb
);
2280 omega_negate_geq (tmp_problem
, e
);
2281 tmp_problem
->safe_vars
= 0;
2282 tmp_problem
->variables_freed
= false;
2284 if (omega_solve_problem (tmp_problem
, omega_false
) == omega_false
)
2285 omega_delete_geq (pb
, e
, pb
->num_vars
);
2291 if (!omega_reduce_with_subs
)
2293 resurrect_subs (pb
);
2294 gcc_assert (please_no_equalities_in_simplified_problems
2295 || pb
->num_subs
== 0);
2298 eliminate_redundant_done
:
2306 /* For each inequality that has coefficients bigger than 20, try to
2307 create a new constraint that cannot be derived from the original
2308 constraint and that has smaller coefficients. Add the new
2309 constraint at the end of geqs. Return the number of inequalities
2310 that have been added to PB. */
2313 smooth_weird_equations (omega_pb pb
)
2315 int e1
, e2
, e3
, p
, q
, k
, alpha
, alpha1
, alpha2
, alpha3
;
2320 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
2321 if (pb
->geqs
[e1
].color
== omega_black
)
2325 for (v
= pb
->num_vars
; v
>= 1; v
--)
2326 if (pb
->geqs
[e1
].coef
[v
] != 0 && abs (pb
->geqs
[e1
].coef
[v
]) < g
)
2327 g
= abs (pb
->geqs
[e1
].coef
[v
]);
2334 for (v
= pb
->num_vars
; v
>= 1; v
--)
2335 pb
->geqs
[e3
].coef
[v
] = int_div (6 * pb
->geqs
[e1
].coef
[v
] + g
/ 2,
2338 pb
->geqs
[e3
].color
= omega_black
;
2339 pb
->geqs
[e3
].touched
= 1;
2341 pb
->geqs
[e3
].coef
[0] = 9997;
2343 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2345 fprintf (dump_file
, "Checking to see if we can derive: ");
2346 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e3
]);
2347 fprintf (dump_file
, "\n from: ");
2348 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e1
]);
2349 fprintf (dump_file
, "\n");
2352 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
2353 if (e1
!= e2
&& pb
->geqs
[e2
].color
== omega_black
)
2355 for (p
= pb
->num_vars
; p
> 1; p
--)
2357 for (q
= p
- 1; q
> 0; q
--)
2360 (pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e2
].coef
[q
] -
2361 pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e1
].coef
[q
]);
2370 alpha1
= (pb
->geqs
[e2
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2371 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2372 alpha2
= -(pb
->geqs
[e1
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2373 - pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2376 if (alpha1
* alpha2
<= 0)
2388 /* Try to prove e3 is redundant: verify
2389 alpha1*v1 + alpha2*v2 = alpha3*v3. */
2390 for (k
= pb
->num_vars
; k
>= 1; k
--)
2391 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2392 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2393 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2396 c
= alpha1
* pb
->geqs
[e1
].coef
[0]
2397 + alpha2
* pb
->geqs
[e2
].coef
[0];
2399 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] + 1))
2400 pb
->geqs
[e3
].coef
[0] = int_div (c
, alpha3
);
2405 if (pb
->geqs
[e3
].coef
[0] < 9997)
2410 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2413 "Smoothing weird equations; adding:\n");
2414 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e3
]);
2415 fprintf (dump_file
, "\nto:\n");
2416 omega_print_problem (dump_file
, pb
);
2417 fprintf (dump_file
, "\n\n");
2425 /* Replace tuples of inequalities, that define upper and lower half
2426 spaces, with an equation. */
2429 coalesce (omega_pb pb
)
2434 int found_something
= 0;
2436 for (e
= 0; e
< pb
->num_geqs
; e
++)
2437 if (pb
->geqs
[e
].color
== omega_red
)
2443 is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2445 for (e
= 0; e
< pb
->num_geqs
; e
++)
2448 for (e
= 0; e
< pb
->num_geqs
; e
++)
2449 if (pb
->geqs
[e
].color
== omega_red
2450 && !pb
->geqs
[e
].touched
)
2451 for (e2
= e
+ 1; e2
< pb
->num_geqs
; e2
++)
2452 if (!pb
->geqs
[e2
].touched
2453 && pb
->geqs
[e
].key
== -pb
->geqs
[e2
].key
2454 && pb
->geqs
[e
].coef
[0] == -pb
->geqs
[e2
].coef
[0]
2455 && pb
->geqs
[e2
].color
== omega_red
)
2457 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
++], &pb
->geqs
[e
],
2459 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2465 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2467 omega_delete_geq (pb
, e
, pb
->num_vars
);
2469 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && found_something
)
2471 fprintf (dump_file
, "Coalesced pb->geqs into %d EQ's:\n",
2473 omega_print_problem (dump_file
, pb
);
2479 /* Eliminate red inequalities from PB. When ELIMINATE_ALL is
2480 true, continue to eliminate all the red inequalities. */
2483 omega_eliminate_red (omega_pb pb
, bool eliminate_all
)
2485 int e
, e2
, e3
, i
, j
, k
, a
, alpha1
, alpha2
;
2487 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2490 omega_pb tmp_problem
;
2492 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2494 fprintf (dump_file
, "in eliminate RED:\n");
2495 omega_print_problem (dump_file
, pb
);
2498 if (pb
->num_eqs
> 0)
2499 omega_simplify_problem (pb
);
2501 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2504 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2505 if (pb
->geqs
[e
].color
== omega_black
&& !is_dead
[e
])
2506 for (e2
= e
- 1; e2
>= 0; e2
--)
2507 if (pb
->geqs
[e2
].color
== omega_black
2512 for (i
= pb
->num_vars
; i
> 1; i
--)
2513 for (j
= i
- 1; j
> 0; j
--)
2514 if ((a
= (pb
->geqs
[e
].coef
[i
] * pb
->geqs
[e2
].coef
[j
]
2515 - pb
->geqs
[e2
].coef
[i
] * pb
->geqs
[e
].coef
[j
])) != 0)
2521 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2524 "found two equations to combine, i = %s, ",
2525 omega_variable_to_str (pb
, i
));
2526 fprintf (dump_file
, "j = %s, alpha = %d\n",
2527 omega_variable_to_str (pb
, j
), a
);
2528 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2529 fprintf (dump_file
, "\n");
2530 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2531 fprintf (dump_file
, "\n");
2534 for (e3
= pb
->num_geqs
- 1; e3
>= 0; e3
--)
2535 if (pb
->geqs
[e3
].color
== omega_red
)
2537 alpha1
= (pb
->geqs
[e2
].coef
[j
] * pb
->geqs
[e3
].coef
[i
]
2538 - pb
->geqs
[e2
].coef
[i
] * pb
->geqs
[e3
].coef
[j
]);
2539 alpha2
= -(pb
->geqs
[e
].coef
[j
] * pb
->geqs
[e3
].coef
[i
]
2540 - pb
->geqs
[e
].coef
[i
] * pb
->geqs
[e3
].coef
[j
]);
2542 if ((a
> 0 && alpha1
> 0 && alpha2
> 0)
2543 || (a
< 0 && alpha1
< 0 && alpha2
< 0))
2545 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2548 "alpha1 = %d, alpha2 = %d;"
2549 "comparing against: ",
2551 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2552 fprintf (dump_file
, "\n");
2555 for (k
= pb
->num_vars
; k
>= 0; k
--)
2557 c
= (alpha1
* pb
->geqs
[e
].coef
[k
]
2558 + alpha2
* pb
->geqs
[e2
].coef
[k
]);
2560 if (c
!= a
* pb
->geqs
[e3
].coef
[k
])
2563 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && k
> 0)
2564 fprintf (dump_file
, " %s: %d, %d\n",
2565 omega_variable_to_str (pb
, k
), c
,
2566 a
* pb
->geqs
[e3
].coef
[k
]);
2571 ((a
> 0 && c
< a
* pb
->geqs
[e3
].coef
[k
])
2572 || (a
< 0 && c
> a
* pb
->geqs
[e3
].coef
[k
]))))
2574 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2578 "red equation#%d is dead "
2579 "(%d dead so far, %d remain)\n",
2581 pb
->num_geqs
- dead_count
);
2582 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2583 fprintf (dump_file
, "\n");
2584 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2585 fprintf (dump_file
, "\n");
2586 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2587 fprintf (dump_file
, "\n");
2595 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2597 omega_delete_geq (pb
, e
, pb
->num_vars
);
2601 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2603 fprintf (dump_file
, "in eliminate RED, easy tests done:\n");
2604 omega_print_problem (dump_file
, pb
);
2607 for (red_found
= 0, e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2608 if (pb
->geqs
[e
].color
== omega_red
)
2613 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2614 fprintf (dump_file
, "fast checks worked\n");
2616 if (!omega_reduce_with_subs
)
2617 gcc_assert (please_no_equalities_in_simplified_problems
2618 || pb
->num_subs
== 0);
2623 if (!omega_verify_simplification
2624 && verify_omega_pb (pb
) == omega_false
)
2628 tmp_problem
= XNEW (struct omega_pb_d
);
2630 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2631 if (pb
->geqs
[e
].color
== omega_red
)
2633 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2636 "checking equation %d to see if it is redundant: ", e
);
2637 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2638 fprintf (dump_file
, "\n");
2641 omega_copy_problem (tmp_problem
, pb
);
2642 omega_negate_geq (tmp_problem
, e
);
2643 tmp_problem
->safe_vars
= 0;
2644 tmp_problem
->variables_freed
= false;
2645 tmp_problem
->num_subs
= 0;
2647 if (omega_solve_problem (tmp_problem
, omega_false
) == omega_false
)
2649 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2650 fprintf (dump_file
, "it is redundant\n");
2651 omega_delete_geq (pb
, e
, pb
->num_vars
);
2655 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2656 fprintf (dump_file
, "it is not redundant\n");
2660 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2661 fprintf (dump_file
, "no need to check other red equations\n");
2669 /* omega_simplify_problem (pb); */
2671 if (!omega_reduce_with_subs
)
2672 gcc_assert (please_no_equalities_in_simplified_problems
2673 || pb
->num_subs
== 0);
2676 /* Transform some wildcard variables to non-safe variables. */
2679 chain_unprotect (omega_pb pb
)
2682 bool *unprotect
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2684 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2686 unprotect
[i
] = omega_wildcard_p (pb
, i
);
2688 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2689 if (pb
->subs
[e
].coef
[i
])
2690 unprotect
[i
] = false;
2693 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2695 fprintf (dump_file
, "Doing chain reaction unprotection\n");
2696 omega_print_problem (dump_file
, pb
);
2698 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2700 fprintf (dump_file
, "unprotecting %s\n",
2701 omega_variable_to_str (pb
, i
));
2704 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2706 omega_unprotect_1 (pb
, &i
, unprotect
);
2708 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2710 fprintf (dump_file
, "After chain reactions\n");
2711 omega_print_problem (dump_file
, pb
);
2717 /* Reduce problem PB. */
2720 omega_problem_reduced (omega_pb pb
)
2722 if (omega_verify_simplification
2723 && !in_approximate_mode
2724 && verify_omega_pb (pb
) == omega_false
)
2727 if (PARAM_VALUE (PARAM_OMEGA_ELIMINATE_REDUNDANT_CONSTRAINTS
)
2728 && !omega_eliminate_redundant (pb
, true))
2731 omega_found_reduction
= omega_true
;
2733 if (!please_no_equalities_in_simplified_problems
)
2736 if (omega_reduce_with_subs
2737 || please_no_equalities_in_simplified_problems
)
2738 chain_unprotect (pb
);
2740 resurrect_subs (pb
);
2742 if (!return_single_result
)
2746 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2747 pb
->forwarding_address
[pb
->var
[i
]] = i
;
2749 for (i
= 0; i
< pb
->num_subs
; i
++)
2750 pb
->forwarding_address
[pb
->subs
[i
].key
] = -i
- 1;
2752 (*omega_when_reduced
) (pb
);
2755 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2757 fprintf (dump_file
, "-------------------------------------------\n");
2758 fprintf (dump_file
, "problem reduced:\n");
2759 omega_print_problem (dump_file
, pb
);
2760 fprintf (dump_file
, "-------------------------------------------\n");
2764 /* Eliminates all the free variables for problem PB, that is all the
2765 variables from FV to PB->NUM_VARS. */
2768 omega_free_eliminations (omega_pb pb
, int fv
)
2770 bool try_again
= true;
2772 int n_vars
= pb
->num_vars
;
2778 for (i
= n_vars
; i
> fv
; i
--)
2780 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2781 if (pb
->geqs
[e
].coef
[i
])
2786 else if (pb
->geqs
[e
].coef
[i
] > 0)
2788 for (e2
= e
- 1; e2
>= 0; e2
--)
2789 if (pb
->geqs
[e2
].coef
[i
] < 0)
2794 for (e2
= e
- 1; e2
>= 0; e2
--)
2795 if (pb
->geqs
[e2
].coef
[i
] > 0)
2802 for (e3
= pb
->num_subs
- 1; e3
>= 0; e3
--)
2803 if (pb
->subs
[e3
].coef
[i
])
2809 for (e3
= pb
->num_eqs
- 1; e3
>= 0; e3
--)
2810 if (pb
->eqs
[e3
].coef
[i
])
2816 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2817 fprintf (dump_file
, "a free elimination of %s\n",
2818 omega_variable_to_str (pb
, i
));
2822 omega_delete_geq (pb
, e
, n_vars
);
2824 for (e
--; e
>= 0; e
--)
2825 if (pb
->geqs
[e
].coef
[i
])
2826 omega_delete_geq (pb
, e
, n_vars
);
2828 try_again
= (i
< n_vars
);
2831 omega_delete_variable (pb
, i
);
2832 n_vars
= pb
->num_vars
;
2837 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2839 fprintf (dump_file
, "\nafter free eliminations:\n");
2840 omega_print_problem (dump_file
, pb
);
2841 fprintf (dump_file
, "\n");
2845 /* Do free red eliminations. */
2848 free_red_eliminations (omega_pb pb
)
2850 bool try_again
= true;
2852 int n_vars
= pb
->num_vars
;
2853 bool *is_red_var
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2854 bool *is_dead_var
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2855 bool *is_dead_geq
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2857 for (i
= n_vars
; i
> 0; i
--)
2859 is_red_var
[i
] = false;
2860 is_dead_var
[i
] = false;
2863 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2865 is_dead_geq
[e
] = false;
2867 if (pb
->geqs
[e
].color
== omega_red
)
2868 for (i
= n_vars
; i
> 0; i
--)
2869 if (pb
->geqs
[e
].coef
[i
] != 0)
2870 is_red_var
[i
] = true;
2876 for (i
= n_vars
; i
> 0; i
--)
2877 if (!is_red_var
[i
] && !is_dead_var
[i
])
2879 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2880 if (!is_dead_geq
[e
] && pb
->geqs
[e
].coef
[i
])
2885 else if (pb
->geqs
[e
].coef
[i
] > 0)
2887 for (e2
= e
- 1; e2
>= 0; e2
--)
2888 if (!is_dead_geq
[e2
] && pb
->geqs
[e2
].coef
[i
] < 0)
2893 for (e2
= e
- 1; e2
>= 0; e2
--)
2894 if (!is_dead_geq
[e2
] && pb
->geqs
[e2
].coef
[i
] > 0)
2901 for (e3
= pb
->num_subs
- 1; e3
>= 0; e3
--)
2902 if (pb
->subs
[e3
].coef
[i
])
2908 for (e3
= pb
->num_eqs
- 1; e3
>= 0; e3
--)
2909 if (pb
->eqs
[e3
].coef
[i
])
2915 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2916 fprintf (dump_file
, "a free red elimination of %s\n",
2917 omega_variable_to_str (pb
, i
));
2920 if (pb
->geqs
[e
].coef
[i
])
2921 is_dead_geq
[e
] = true;
2924 is_dead_var
[i
] = true;
2929 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2931 omega_delete_geq (pb
, e
, n_vars
);
2933 for (i
= n_vars
; i
> 0; i
--)
2935 omega_delete_variable (pb
, i
);
2937 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2939 fprintf (dump_file
, "\nafter free red eliminations:\n");
2940 omega_print_problem (dump_file
, pb
);
2941 fprintf (dump_file
, "\n");
2949 /* For equation EQ of the form "0 = EQN", insert in PB two
2950 inequalities "0 <= EQN" and "0 <= -EQN". */
2953 omega_convert_eq_to_geqs (omega_pb pb
, int eq
)
2957 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2958 fprintf (dump_file
, "Converting Eq to Geqs\n");
2960 /* Insert "0 <= EQN". */
2961 omega_copy_eqn (&pb
->geqs
[pb
->num_geqs
], &pb
->eqs
[eq
], pb
->num_vars
);
2962 pb
->geqs
[pb
->num_geqs
].touched
= 1;
2965 /* Insert "0 <= -EQN". */
2966 omega_copy_eqn (&pb
->geqs
[pb
->num_geqs
], &pb
->eqs
[eq
], pb
->num_vars
);
2967 pb
->geqs
[pb
->num_geqs
].touched
= 1;
2969 for (i
= 0; i
<= pb
->num_vars
; i
++)
2970 pb
->geqs
[pb
->num_geqs
].coef
[i
] *= -1;
2974 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2975 omega_print_problem (dump_file
, pb
);
2978 /* Eliminates variable I from PB. */
2981 omega_do_elimination (omega_pb pb
, int e
, int i
)
2983 eqn sub
= omega_alloc_eqns (0, 1);
2985 int n_vars
= pb
->num_vars
;
2987 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2988 fprintf (dump_file
, "eliminating variable %s\n",
2989 omega_variable_to_str (pb
, i
));
2991 omega_copy_eqn (sub
, &pb
->eqs
[e
], pb
->num_vars
);
2994 if (c
== 1 || c
== -1)
2996 if (pb
->eqs
[e
].color
== omega_red
)
2999 omega_substitute_red (pb
, sub
, i
, c
, &fB
);
3001 omega_convert_eq_to_geqs (pb
, e
);
3003 omega_delete_variable (pb
, i
);
3007 omega_substitute (pb
, sub
, i
, c
);
3008 omega_delete_variable (pb
, i
);
3016 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3017 fprintf (dump_file
, "performing non-exact elimination, c = %d\n", c
);
3019 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
3020 if (pb
->eqs
[e
].coef
[i
])
3022 eqn eqn
= &(pb
->eqs
[e
]);
3024 for (j
= n_vars
; j
>= 0; j
--)
3028 if (sub
->color
== omega_red
)
3029 eqn
->color
= omega_red
;
3030 for (j
= n_vars
; j
>= 0; j
--)
3031 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3034 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3035 if (pb
->geqs
[e
].coef
[i
])
3037 eqn eqn
= &(pb
->geqs
[e
]);
3040 if (sub
->color
== omega_red
)
3041 eqn
->color
= omega_red
;
3043 for (j
= n_vars
; j
>= 0; j
--)
3050 for (j
= n_vars
; j
>= 0; j
--)
3051 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3055 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
3056 if (pb
->subs
[e
].coef
[i
])
3058 eqn eqn
= &(pb
->subs
[e
]);
3061 gcc_assert (sub
->color
== omega_black
);
3062 for (j
= n_vars
; j
>= 0; j
--)
3066 for (j
= n_vars
; j
>= 0; j
--)
3067 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3070 if (in_approximate_mode
)
3071 omega_delete_variable (pb
, i
);
3073 omega_convert_eq_to_geqs (pb
, e2
);
3076 omega_free_eqns (sub
, 1);
3079 /* Helper function for printing "sorry, no solution". */
3081 static inline enum omega_result
3082 omega_problem_has_no_solution (void)
3084 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3085 fprintf (dump_file
, "\nequations have no solution \n");
3090 /* Helper function: solve equations in PB one at a time, following the
3091 DESIRED_RES result. */
3093 static enum omega_result
3094 omega_solve_eq (omega_pb pb
, enum omega_result desired_res
)
3101 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && pb
->num_eqs
> 0)
3103 fprintf (dump_file
, "\nomega_solve_eq (%d, %d)\n",
3104 desired_res
, may_be_red
);
3105 omega_print_problem (dump_file
, pb
);
3106 fprintf (dump_file
, "\n");
3112 j
= pb
->num_eqs
- 1;
3118 while (i
<= j
&& pb
->eqs
[i
].color
== omega_red
)
3121 while (i
<= j
&& pb
->eqs
[j
].color
== omega_black
)
3127 eq
= omega_alloc_eqns (0, 1);
3128 omega_copy_eqn (eq
, &pb
->eqs
[i
], pb
->num_vars
);
3129 omega_copy_eqn (&pb
->eqs
[i
], &pb
->eqs
[j
], pb
->num_vars
);
3130 omega_copy_eqn (&pb
->eqs
[j
], eq
, pb
->num_vars
);
3131 omega_free_eqns (eq
, 1);
3137 /* Eliminate all EQ equations */
3138 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
3140 eqn eqn
= &(pb
->eqs
[e
]);
3143 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3144 fprintf (dump_file
, "----\n");
3146 for (i
= pb
->num_vars
; i
> 0; i
--)
3152 for (j
= i
- 1; j
> 0; j
--)
3156 /* i is the position of last nonzero coefficient,
3157 g is the coefficient of i,
3158 j is the position of next nonzero coefficient. */
3162 if (eqn
->coef
[0] % g
!= 0)
3163 return omega_problem_has_no_solution ();
3165 eqn
->coef
[0] = eqn
->coef
[0] / g
;
3168 omega_do_elimination (pb
, e
, i
);
3174 if (eqn
->coef
[0] != 0)
3175 return omega_problem_has_no_solution ();
3187 omega_do_elimination (pb
, e
, i
);
3193 bool promotion_possible
=
3194 (omega_safe_var_p (pb
, j
)
3195 && pb
->safe_vars
+ 1 == i
3196 && !omega_eqn_is_red (eqn
, desired_res
)
3197 && !in_approximate_mode
);
3199 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && promotion_possible
)
3200 fprintf (dump_file
, " Promotion possible\n");
3203 if (!omega_safe_var_p (pb
, j
))
3205 for (; g
!= 1 && !omega_safe_var_p (pb
, j
); j
--)
3206 g
= gcd (abs (eqn
->coef
[j
]), g
);
3209 else if (!omega_safe_var_p (pb
, i
))
3214 for (; g
!= 1 && j
> 0; j
--)
3215 g
= gcd (abs (eqn
->coef
[j
]), g
);
3219 if (eqn
->coef
[0] % g
!= 0)
3220 return omega_problem_has_no_solution ();
3222 for (j
= 0; j
<= pb
->num_vars
; j
++)
3232 for (e2
= e
- 1; e2
>= 0; e2
--)
3233 if (pb
->eqs
[e2
].coef
[i
])
3237 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
3238 if (pb
->geqs
[e2
].coef
[i
])
3242 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
3243 if (pb
->subs
[e2
].coef
[i
])
3248 bool change
= false;
3250 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3252 fprintf (dump_file
, "Ha! We own it! \n");
3253 omega_print_eq (dump_file
, pb
, eqn
);
3254 fprintf (dump_file
, " \n");
3260 for (j
= i
- 1; j
>= 0; j
--)
3262 int t
= int_mod (eqn
->coef
[j
], g
);
3267 if (t
!= eqn
->coef
[j
])
3276 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3277 fprintf (dump_file
, "So what?\n");
3282 omega_name_wild_card (pb
, i
);
3284 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3286 omega_print_eq (dump_file
, pb
, eqn
);
3287 fprintf (dump_file
, " \n");
3296 if (promotion_possible
)
3298 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3300 fprintf (dump_file
, "promoting %s to safety\n",
3301 omega_variable_to_str (pb
, i
));
3302 omega_print_vars (dump_file
, pb
);
3307 if (!omega_wildcard_p (pb
, i
))
3308 omega_name_wild_card (pb
, i
);
3310 promotion_possible
= false;
3315 if (g2
> 1 && !in_approximate_mode
)
3317 if (pb
->eqs
[e
].color
== omega_red
)
3319 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3320 fprintf (dump_file
, "handling red equality\n");
3323 omega_do_elimination (pb
, e
, i
);
3327 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3330 "adding equation to handle safe variable \n");
3331 omega_print_eq (dump_file
, pb
, eqn
);
3332 fprintf (dump_file
, "\n----\n");
3333 omega_print_problem (dump_file
, pb
);
3334 fprintf (dump_file
, "\n----\n");
3335 fprintf (dump_file
, "\n----\n");
3338 i
= omega_add_new_wild_card (pb
);
3340 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
3341 omega_init_eqn_zero (&pb
->eqs
[e
+ 1], pb
->num_vars
);
3342 omega_copy_eqn (&pb
->eqs
[e
+ 1], eqn
, pb
->safe_vars
);
3344 for (j
= pb
->num_vars
; j
>= 0; j
--)
3346 pb
->eqs
[e
+ 1].coef
[j
] = int_mod (pb
->eqs
[e
+ 1].coef
[j
], g2
);
3348 if (2 * pb
->eqs
[e
+ 1].coef
[j
] >= g2
)
3349 pb
->eqs
[e
+ 1].coef
[j
] -= g2
;
3352 pb
->eqs
[e
+ 1].coef
[i
] = g2
;
3355 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3356 omega_print_problem (dump_file
, pb
);
3365 /* Find variable to eliminate. */
3368 gcc_assert (in_approximate_mode
);
3370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3372 fprintf (dump_file
, "non-exact elimination: ");
3373 omega_print_eq (dump_file
, pb
, eqn
);
3374 fprintf (dump_file
, "\n");
3375 omega_print_problem (dump_file
, pb
);
3378 for (i
= pb
->num_vars
; i
> sv
; i
--)
3379 if (pb
->eqs
[e
].coef
[i
] != 0)
3383 for (i
= pb
->num_vars
; i
> sv
; i
--)
3384 if (pb
->eqs
[e
].coef
[i
] == 1 || pb
->eqs
[e
].coef
[i
] == -1)
3390 omega_do_elimination (pb
, e
, i
);
3392 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && g2
> 1)
3394 fprintf (dump_file
, "result of non-exact elimination:\n");
3395 omega_print_problem (dump_file
, pb
);
3400 int factor
= (INT_MAX
);
3403 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3404 fprintf (dump_file
, "doing moding\n");
3406 for (i
= pb
->num_vars
; i
!= sv
; i
--)
3407 if ((pb
->eqs
[e
].coef
[i
] & 1) != 0)
3412 for (; i
!= sv
; i
--)
3413 if ((pb
->eqs
[e
].coef
[i
] & 1) != 0)
3419 if (j
!= 0 && i
== sv
)
3421 omega_do_mod (pb
, 2, e
, j
);
3427 for (i
= pb
->num_vars
; i
!= sv
; i
--)
3428 if (pb
->eqs
[e
].coef
[i
] != 0
3429 && factor
> abs (pb
->eqs
[e
].coef
[i
]) + 1)
3431 factor
= abs (pb
->eqs
[e
].coef
[i
]) + 1;
3437 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3438 fprintf (dump_file
, "should not have happened\n");
3442 omega_do_mod (pb
, factor
, e
, j
);
3443 /* Go back and try this equation again. */
3450 return omega_unknown
;
3453 /* Transform an inequation E to an equality, then solve DIFF problems
3454 based on PB, and only differing by the constant part that is
3455 diminished by one, trying to figure out which of the constants
3458 static enum omega_result
3459 parallel_splinter (omega_pb pb
, int e
, int diff
,
3460 enum omega_result desired_res
)
3462 omega_pb tmp_problem
;
3465 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3467 fprintf (dump_file
, "Using parallel splintering\n");
3468 omega_print_problem (dump_file
, pb
);
3471 tmp_problem
= XNEW (struct omega_pb_d
);
3472 omega_copy_eqn (&pb
->eqs
[0], &pb
->geqs
[e
], pb
->num_vars
);
3475 for (i
= 0; i
<= diff
; i
++)
3477 omega_copy_problem (tmp_problem
, pb
);
3479 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3481 fprintf (dump_file
, "Splinter # %i\n", i
);
3482 omega_print_problem (dump_file
, pb
);
3485 if (omega_solve_problem (tmp_problem
, desired_res
) == omega_true
)
3491 pb
->eqs
[0].coef
[0]--;
3498 /* Helper function: solve equations one at a time. */
3500 static enum omega_result
3501 omega_solve_geq (omega_pb pb
, enum omega_result desired_res
)
3505 enum omega_result result
;
3506 bool coupled_subscripts
= false;
3507 bool smoothed
= false;
3508 bool eliminate_again
;
3509 bool tried_eliminating_redundant
= false;
3511 if (desired_res
!= omega_simplify
)
3519 gcc_assert (desired_res
== omega_simplify
|| pb
->num_subs
== 0);
3521 /* Verify that there are not too many inequalities. */
3522 gcc_assert (pb
->num_geqs
<= OMEGA_MAX_GEQS
);
3524 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3526 fprintf (dump_file
, "\nomega_solve_geq (%d,%d):\n",
3527 desired_res
, please_no_equalities_in_simplified_problems
);
3528 omega_print_problem (dump_file
, pb
);
3529 fprintf (dump_file
, "\n");
3532 n_vars
= pb
->num_vars
;
3536 enum omega_eqn_color u_color
= omega_black
;
3537 enum omega_eqn_color l_color
= omega_black
;
3538 int upper_bound
= pos_infinity
;
3539 int lower_bound
= neg_infinity
;
3541 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3543 int a
= pb
->geqs
[e
].coef
[1];
3544 int c
= pb
->geqs
[e
].coef
[0];
3546 /* Our equation is ax + c >= 0, or ax >= -c, or c >= -ax. */
3550 return omega_problem_has_no_solution ();
3557 if (lower_bound
< -c
3558 || (lower_bound
== -c
3559 && !omega_eqn_is_red (&pb
->geqs
[e
], desired_res
)))
3562 l_color
= pb
->geqs
[e
].color
;
3568 c
= int_div (c
, -a
);
3571 || (upper_bound
== c
3572 && !omega_eqn_is_red (&pb
->geqs
[e
], desired_res
)))
3575 u_color
= pb
->geqs
[e
].color
;
3580 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3582 fprintf (dump_file
, "upper bound = %d\n", upper_bound
);
3583 fprintf (dump_file
, "lower bound = %d\n", lower_bound
);
3586 if (lower_bound
> upper_bound
)
3587 return omega_problem_has_no_solution ();
3589 if (desired_res
== omega_simplify
)
3592 if (pb
->safe_vars
== 1)
3595 if (lower_bound
== upper_bound
3596 && u_color
== omega_black
3597 && l_color
== omega_black
)
3599 pb
->eqs
[0].coef
[0] = -lower_bound
;
3600 pb
->eqs
[0].coef
[1] = 1;
3601 pb
->eqs
[0].color
= omega_black
;
3603 return omega_solve_problem (pb
, desired_res
);
3607 if (lower_bound
> neg_infinity
)
3609 pb
->geqs
[0].coef
[0] = -lower_bound
;
3610 pb
->geqs
[0].coef
[1] = 1;
3611 pb
->geqs
[0].key
= 1;
3612 pb
->geqs
[0].color
= l_color
;
3613 pb
->geqs
[0].touched
= 0;
3617 if (upper_bound
< pos_infinity
)
3619 pb
->geqs
[pb
->num_geqs
].coef
[0] = upper_bound
;
3620 pb
->geqs
[pb
->num_geqs
].coef
[1] = -1;
3621 pb
->geqs
[pb
->num_geqs
].key
= -1;
3622 pb
->geqs
[pb
->num_geqs
].color
= u_color
;
3623 pb
->geqs
[pb
->num_geqs
].touched
= 0;
3631 omega_problem_reduced (pb
);
3635 if (original_problem
!= no_problem
3636 && l_color
== omega_black
3637 && u_color
== omega_black
3639 && lower_bound
== upper_bound
)
3641 pb
->eqs
[0].coef
[0] = -lower_bound
;
3642 pb
->eqs
[0].coef
[1] = 1;
3644 adding_equality_constraint (pb
, 0);
3650 if (!pb
->variables_freed
)
3652 pb
->variables_freed
= true;
3654 if (desired_res
!= omega_simplify
)
3655 omega_free_eliminations (pb
, 0);
3657 omega_free_eliminations (pb
, pb
->safe_vars
);
3659 n_vars
= pb
->num_vars
;
3665 switch (normalize_omega_problem (pb
))
3667 case normalize_false
:
3671 case normalize_coupled
:
3672 coupled_subscripts
= true;
3675 case normalize_uncoupled
:
3676 coupled_subscripts
= false;
3683 n_vars
= pb
->num_vars
;
3685 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3687 fprintf (dump_file
, "\nafter normalization:\n");
3688 omega_print_problem (dump_file
, pb
);
3689 fprintf (dump_file
, "\n");
3690 fprintf (dump_file
, "eliminating variable using Fourier-Motzkin.\n");
3694 int parallel_difference
= INT_MAX
;
3695 int best_parallel_eqn
= -1;
3696 int minC
, maxC
, minCj
= 0;
3697 int lower_bound_count
= 0;
3699 bool possible_easy_int_solution
;
3700 int max_splinters
= 1;
3702 bool lucky_exact
= false;
3703 int best
= (INT_MAX
);
3704 int j
= 0, jLe
= 0, jLowerBoundCount
= 0;
3707 eliminate_again
= false;
3709 if (pb
->num_eqs
> 0)
3710 return omega_solve_problem (pb
, desired_res
);
3712 if (!coupled_subscripts
)
3714 if (pb
->safe_vars
== 0)
3717 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3718 if (!omega_safe_var_p (pb
, abs (pb
->geqs
[e
].key
)))
3719 omega_delete_geq (pb
, e
, n_vars
);
3721 pb
->num_vars
= pb
->safe_vars
;
3723 if (desired_res
== omega_simplify
)
3725 omega_problem_reduced (pb
);
3732 if (desired_res
!= omega_simplify
)
3737 if (pb
->num_geqs
== 0)
3739 if (desired_res
== omega_simplify
)
3741 pb
->num_vars
= pb
->safe_vars
;
3742 omega_problem_reduced (pb
);
3748 if (desired_res
== omega_simplify
&& n_vars
== pb
->safe_vars
)
3750 omega_problem_reduced (pb
);
3754 if (pb
->num_geqs
> OMEGA_MAX_GEQS
- 30
3755 || pb
->num_geqs
> 2 * n_vars
* n_vars
+ 4 * n_vars
+ 10)
3757 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3759 "TOO MANY EQUATIONS; "
3760 "%d equations, %d variables, "
3761 "ELIMINATING REDUNDANT ONES\n",
3762 pb
->num_geqs
, n_vars
);
3764 if (!omega_eliminate_redundant (pb
, false))
3767 n_vars
= pb
->num_vars
;
3769 if (pb
->num_eqs
> 0)
3770 return omega_solve_problem (pb
, desired_res
);
3772 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3773 fprintf (dump_file
, "END ELIMINATION OF REDUNDANT EQUATIONS\n");
3776 if (desired_res
!= omega_simplify
)
3781 for (i
= n_vars
; i
!= fv
; i
--)
3787 int upper_bound_count
= 0;
3789 lower_bound_count
= 0;
3792 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3793 if (pb
->geqs
[e
].coef
[i
] < 0)
3795 minC
= MIN (minC
, pb
->geqs
[e
].coef
[i
]);
3796 upper_bound_count
++;
3797 if (pb
->geqs
[e
].coef
[i
] < -1)
3805 else if (pb
->geqs
[e
].coef
[i
] > 0)
3807 maxC
= MAX (maxC
, pb
->geqs
[e
].coef
[i
]);
3808 lower_bound_count
++;
3810 if (pb
->geqs
[e
].coef
[i
] > 1)
3819 if (lower_bound_count
== 0
3820 || upper_bound_count
== 0)
3822 lower_bound_count
= 0;
3826 if (ub
>= 0 && lb
>= 0
3827 && pb
->geqs
[lb
].key
== -pb
->geqs
[ub
].key
)
3829 int Lc
= pb
->geqs
[lb
].coef
[i
];
3830 int Uc
= -pb
->geqs
[ub
].coef
[i
];
3832 Lc
* pb
->geqs
[ub
].coef
[0] + Uc
* pb
->geqs
[lb
].coef
[0];
3833 lucky
= (diff
>= (Uc
- 1) * (Lc
- 1));
3839 || in_approximate_mode
)
3841 score
= upper_bound_count
* lower_bound_count
;
3843 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3845 "For %s, exact, score = %d*%d, range = %d ... %d,"
3846 "\nlucky = %d, in_approximate_mode=%d \n",
3847 omega_variable_to_str (pb
, i
),
3849 lower_bound_count
, minC
, maxC
, lucky
,
3850 in_approximate_mode
);
3860 jLowerBoundCount
= lower_bound_count
;
3862 lucky_exact
= lucky
;
3869 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3871 "For %s, non-exact, score = %d*%d,"
3872 "range = %d ... %d \n",
3873 omega_variable_to_str (pb
, i
),
3875 lower_bound_count
, minC
, maxC
);
3877 score
= maxC
- minC
;
3885 jLowerBoundCount
= lower_bound_count
;
3890 if (lower_bound_count
== 0)
3892 omega_free_eliminations (pb
, pb
->safe_vars
);
3893 n_vars
= pb
->num_vars
;
3894 eliminate_again
= true;
3901 lower_bound_count
= jLowerBoundCount
;
3903 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3904 if (pb
->geqs
[e
].coef
[i
] > 0)
3906 if (pb
->geqs
[e
].coef
[i
] == -minC
)
3907 max_splinters
+= -minC
- 1;
3910 check_pos_mul ((pb
->geqs
[e
].coef
[i
] - 1),
3911 (-minC
- 1)) / (-minC
) + 1;
3915 /* Trying to produce exact elimination by finding redundant
3917 if (!exact
&& !tried_eliminating_redundant
)
3919 omega_eliminate_redundant (pb
, false);
3920 tried_eliminating_redundant
= true;
3921 eliminate_again
= true;
3924 tried_eliminating_redundant
= false;
3927 if (return_single_result
&& desired_res
== omega_simplify
&& !exact
)
3929 omega_problem_reduced (pb
);
3933 /* #ifndef Omega3 */
3934 /* Trying to produce exact elimination by finding redundant
3936 if (!exact
&& !tried_eliminating_redundant
)
3938 omega_eliminate_redundant (pb
, false);
3939 tried_eliminating_redundant
= true;
3942 tried_eliminating_redundant
= false;
3949 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
3950 if (pb
->geqs
[e1
].color
== omega_black
)
3951 for (e2
= e1
- 1; e2
>= 0; e2
--)
3952 if (pb
->geqs
[e2
].color
== omega_black
3953 && pb
->geqs
[e1
].key
== -pb
->geqs
[e2
].key
3954 && ((pb
->geqs
[e1
].coef
[0] + pb
->geqs
[e2
].coef
[0])
3955 * (3 - single_var_geq (&pb
->geqs
[e1
], pb
->num_vars
))
3956 / 2 < parallel_difference
))
3958 parallel_difference
=
3959 (pb
->geqs
[e1
].coef
[0] + pb
->geqs
[e2
].coef
[0])
3960 * (3 - single_var_geq (&pb
->geqs
[e1
], pb
->num_vars
))
3962 best_parallel_eqn
= e1
;
3965 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
3966 && best_parallel_eqn
>= 0)
3969 "Possible parallel projection, diff = %d, in ",
3970 parallel_difference
);
3971 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[best_parallel_eqn
]));
3972 fprintf (dump_file
, "\n");
3973 omega_print_problem (dump_file
, pb
);
3977 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3979 fprintf (dump_file
, "going to eliminate %s, (%d,%d,%d)\n",
3980 omega_variable_to_str (pb
, i
), i
, minC
,
3982 omega_print_problem (dump_file
, pb
);
3985 fprintf (dump_file
, "(a lucky exact elimination)\n");
3988 fprintf (dump_file
, "(an exact elimination)\n");
3990 fprintf (dump_file
, "Max # of splinters = %d\n", max_splinters
);
3993 gcc_assert (max_splinters
>= 1);
3995 if (!exact
&& desired_res
== omega_simplify
&& best_parallel_eqn
>= 0
3996 && parallel_difference
<= max_splinters
)
3997 return parallel_splinter (pb
, best_parallel_eqn
, parallel_difference
,
4005 int j
= pb
->num_vars
;
4007 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4009 fprintf (dump_file
, "Swapping %d and %d\n", i
, j
);
4010 omega_print_problem (dump_file
, pb
);
4013 swap (&pb
->var
[i
], &pb
->var
[j
]);
4015 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4016 if (pb
->geqs
[e
].coef
[i
] != pb
->geqs
[e
].coef
[j
])
4018 pb
->geqs
[e
].touched
= 1;
4019 t
= pb
->geqs
[e
].coef
[i
];
4020 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[j
];
4021 pb
->geqs
[e
].coef
[j
] = t
;
4024 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
4025 if (pb
->subs
[e
].coef
[i
] != pb
->subs
[e
].coef
[j
])
4027 t
= pb
->subs
[e
].coef
[i
];
4028 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[j
];
4029 pb
->subs
[e
].coef
[j
] = t
;
4032 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4034 fprintf (dump_file
, "Swapping complete \n");
4035 omega_print_problem (dump_file
, pb
);
4036 fprintf (dump_file
, "\n");
4042 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4044 fprintf (dump_file
, "No swap needed\n");
4045 omega_print_problem (dump_file
, pb
);
4049 n_vars
= pb
->num_vars
;
4055 int upper_bound
= pos_infinity
;
4056 int lower_bound
= neg_infinity
;
4057 enum omega_eqn_color ub_color
= omega_black
;
4058 enum omega_eqn_color lb_color
= omega_black
;
4059 int topeqn
= pb
->num_geqs
- 1;
4060 int Lc
= pb
->geqs
[Le
].coef
[i
];
4062 for (Le
= topeqn
; Le
>= 0; Le
--)
4063 if ((Lc
= pb
->geqs
[Le
].coef
[i
]) == 0)
4065 if (pb
->geqs
[Le
].coef
[1] == 1)
4067 int constantTerm
= -pb
->geqs
[Le
].coef
[0];
4069 if (constantTerm
> lower_bound
||
4070 (constantTerm
== lower_bound
&&
4071 !omega_eqn_is_red (&pb
->geqs
[Le
], desired_res
)))
4073 lower_bound
= constantTerm
;
4074 lb_color
= pb
->geqs
[Le
].color
;
4077 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4079 if (pb
->geqs
[Le
].color
== omega_black
)
4080 fprintf (dump_file
, " :::=> %s >= %d\n",
4081 omega_variable_to_str (pb
, 1),
4085 " :::=> [%s >= %d]\n",
4086 omega_variable_to_str (pb
, 1),
4092 int constantTerm
= pb
->geqs
[Le
].coef
[0];
4093 if (constantTerm
< upper_bound
||
4094 (constantTerm
== upper_bound
4095 && !omega_eqn_is_red (&pb
->geqs
[Le
],
4098 upper_bound
= constantTerm
;
4099 ub_color
= pb
->geqs
[Le
].color
;
4102 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4104 if (pb
->geqs
[Le
].color
== omega_black
)
4105 fprintf (dump_file
, " :::=> %s <= %d\n",
4106 omega_variable_to_str (pb
, 1),
4110 " :::=> [%s <= %d]\n",
4111 omega_variable_to_str (pb
, 1),
4117 for (Ue
= topeqn
; Ue
>= 0; Ue
--)
4118 if (pb
->geqs
[Ue
].coef
[i
] < 0
4119 && pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4121 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4122 int coefficient
= pb
->geqs
[Ue
].coef
[1] * Lc
4123 + pb
->geqs
[Le
].coef
[1] * Uc
;
4124 int constantTerm
= pb
->geqs
[Ue
].coef
[0] * Lc
4125 + pb
->geqs
[Le
].coef
[0] * Uc
;
4127 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4129 omega_print_geq_extra (dump_file
, pb
,
4131 fprintf (dump_file
, "\n");
4132 omega_print_geq_extra (dump_file
, pb
,
4134 fprintf (dump_file
, "\n");
4137 if (coefficient
> 0)
4139 constantTerm
= -int_div (constantTerm
, coefficient
);
4141 if (constantTerm
> lower_bound
4142 || (constantTerm
== lower_bound
4143 && (desired_res
!= omega_simplify
4144 || (pb
->geqs
[Ue
].color
== omega_black
4145 && pb
->geqs
[Le
].color
== omega_black
))))
4147 lower_bound
= constantTerm
;
4148 lb_color
= (pb
->geqs
[Ue
].color
== omega_red
4149 || pb
->geqs
[Le
].color
== omega_red
)
4150 ? omega_red
: omega_black
;
4153 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4155 if (pb
->geqs
[Ue
].color
== omega_red
4156 || pb
->geqs
[Le
].color
== omega_red
)
4158 " ::=> [%s >= %d]\n",
4159 omega_variable_to_str (pb
, 1),
4164 omega_variable_to_str (pb
, 1),
4170 constantTerm
= int_div (constantTerm
, -coefficient
);
4171 if (constantTerm
< upper_bound
4172 || (constantTerm
== upper_bound
4173 && pb
->geqs
[Ue
].color
== omega_black
4174 && pb
->geqs
[Le
].color
== omega_black
))
4176 upper_bound
= constantTerm
;
4177 ub_color
= (pb
->geqs
[Ue
].color
== omega_red
4178 || pb
->geqs
[Le
].color
== omega_red
)
4179 ? omega_red
: omega_black
;
4183 && (dump_flags
& TDF_DETAILS
))
4185 if (pb
->geqs
[Ue
].color
== omega_red
4186 || pb
->geqs
[Le
].color
== omega_red
)
4188 " ::=> [%s <= %d]\n",
4189 omega_variable_to_str (pb
, 1),
4194 omega_variable_to_str (pb
, 1),
4202 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4204 " therefore, %c%d <= %c%s%c <= %d%c\n",
4205 lb_color
== omega_red
? '[' : ' ', lower_bound
,
4206 (lb_color
== omega_red
&& ub_color
== omega_black
)
4208 omega_variable_to_str (pb
, 1),
4209 (lb_color
== omega_black
&& ub_color
== omega_red
)
4211 upper_bound
, ub_color
== omega_red
? ']' : ' ');
4213 if (lower_bound
> upper_bound
)
4216 if (pb
->safe_vars
== 1)
4218 if (upper_bound
== lower_bound
4219 && !(ub_color
== omega_red
|| lb_color
== omega_red
)
4220 && !please_no_equalities_in_simplified_problems
)
4223 pb
->eqs
[0].coef
[1] = -1;
4224 pb
->eqs
[0].coef
[0] = upper_bound
;
4226 if (ub_color
== omega_red
4227 || lb_color
== omega_red
)
4228 pb
->eqs
[0].color
= omega_red
;
4230 if (desired_res
== omega_simplify
4231 && pb
->eqs
[0].color
== omega_black
)
4232 return omega_solve_problem (pb
, desired_res
);
4235 if (upper_bound
!= pos_infinity
)
4237 pb
->geqs
[0].coef
[1] = -1;
4238 pb
->geqs
[0].coef
[0] = upper_bound
;
4239 pb
->geqs
[0].color
= ub_color
;
4240 pb
->geqs
[0].key
= -1;
4241 pb
->geqs
[0].touched
= 0;
4245 if (lower_bound
!= neg_infinity
)
4247 pb
->geqs
[pb
->num_geqs
].coef
[1] = 1;
4248 pb
->geqs
[pb
->num_geqs
].coef
[0] = -lower_bound
;
4249 pb
->geqs
[pb
->num_geqs
].color
= lb_color
;
4250 pb
->geqs
[pb
->num_geqs
].key
= 1;
4251 pb
->geqs
[pb
->num_geqs
].touched
= 0;
4256 if (desired_res
== omega_simplify
)
4258 omega_problem_reduced (pb
);
4264 && (desired_res
!= omega_simplify
4265 || (lb_color
== omega_black
4266 && ub_color
== omega_black
))
4267 && original_problem
!= no_problem
4268 && lower_bound
== upper_bound
)
4270 for (i
= original_problem
->num_vars
; i
>= 0; i
--)
4271 if (original_problem
->var
[i
] == pb
->var
[1])
4277 e
= original_problem
->num_eqs
++;
4278 omega_init_eqn_zero (&original_problem
->eqs
[e
],
4279 original_problem
->num_vars
);
4280 original_problem
->eqs
[e
].coef
[i
] = -1;
4281 original_problem
->eqs
[e
].coef
[0] = upper_bound
;
4283 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4286 "adding equality %d to outer problem\n", e
);
4287 omega_print_problem (dump_file
, original_problem
);
4294 eliminate_again
= true;
4296 if (lower_bound_count
== 1)
4298 eqn lbeqn
= omega_alloc_eqns (0, 1);
4299 int Lc
= pb
->geqs
[Le
].coef
[i
];
4301 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4302 fprintf (dump_file
, "an inplace elimination\n");
4304 omega_copy_eqn (lbeqn
, &pb
->geqs
[Le
], (n_vars
+ 1));
4305 omega_delete_geq_extra (pb
, Le
, n_vars
+ 1);
4307 for (Ue
= pb
->num_geqs
- 1; Ue
>= 0; Ue
--)
4308 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4310 if (lbeqn
->key
== -pb
->geqs
[Ue
].key
)
4311 omega_delete_geq_extra (pb
, Ue
, n_vars
+ 1);
4315 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4316 pb
->geqs
[Ue
].touched
= 1;
4317 eliminate_again
= false;
4319 if (lbeqn
->color
== omega_red
)
4320 pb
->geqs
[Ue
].color
= omega_red
;
4322 for (k
= 0; k
<= n_vars
; k
++)
4323 pb
->geqs
[Ue
].coef
[k
] =
4324 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4325 check_mul (lbeqn
->coef
[k
], Uc
);
4327 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4329 omega_print_geq (dump_file
, pb
,
4331 fprintf (dump_file
, "\n");
4336 omega_free_eqns (lbeqn
, 1);
4341 int *dead_eqns
= XNEWVEC (int, OMEGA_MAX_GEQS
);
4342 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
4344 int top_eqn
= pb
->num_geqs
- 1;
4345 lower_bound_count
--;
4347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4348 fprintf (dump_file
, "lower bound count = %d\n",
4351 for (Le
= top_eqn
; Le
>= 0; Le
--)
4352 if (pb
->geqs
[Le
].coef
[i
] > 0)
4354 int Lc
= pb
->geqs
[Le
].coef
[i
];
4355 for (Ue
= top_eqn
; Ue
>= 0; Ue
--)
4356 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4358 if (pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4361 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4364 e2
= pb
->num_geqs
++;
4366 e2
= dead_eqns
[--num_dead
];
4368 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4373 "Le = %d, Ue = %d, gen = %d\n",
4375 omega_print_geq_extra (dump_file
, pb
,
4377 fprintf (dump_file
, "\n");
4378 omega_print_geq_extra (dump_file
, pb
,
4380 fprintf (dump_file
, "\n");
4383 eliminate_again
= false;
4385 for (k
= n_vars
; k
>= 0; k
--)
4386 pb
->geqs
[e2
].coef
[k
] =
4387 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4388 check_mul (pb
->geqs
[Le
].coef
[k
], Uc
);
4390 pb
->geqs
[e2
].coef
[n_vars
+ 1] = 0;
4391 pb
->geqs
[e2
].touched
= 1;
4393 if (pb
->geqs
[Ue
].color
== omega_red
4394 || pb
->geqs
[Le
].color
== omega_red
)
4395 pb
->geqs
[e2
].color
= omega_red
;
4397 pb
->geqs
[e2
].color
= omega_black
;
4399 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4401 omega_print_geq (dump_file
, pb
,
4403 fprintf (dump_file
, "\n");
4407 if (lower_bound_count
== 0)
4409 dead_eqns
[num_dead
++] = Ue
;
4411 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4412 fprintf (dump_file
, "Killed %d\n", Ue
);
4416 lower_bound_count
--;
4417 dead_eqns
[num_dead
++] = Le
;
4419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4420 fprintf (dump_file
, "Killed %d\n", Le
);
4423 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4426 while (num_dead
> 0)
4427 is_dead
[dead_eqns
[--num_dead
]] = true;
4429 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4431 omega_delete_geq_extra (pb
, e
, n_vars
+ 1);
4442 rS
= omega_alloc_problem (0, 0);
4443 iS
= omega_alloc_problem (0, 0);
4445 possible_easy_int_solution
= true;
4447 for (e
= 0; e
< pb
->num_geqs
; e
++)
4448 if (pb
->geqs
[e
].coef
[i
] == 0)
4450 omega_copy_eqn (&(rS
->geqs
[e2
]), &pb
->geqs
[e
],
4452 omega_copy_eqn (&(iS
->geqs
[e2
]), &pb
->geqs
[e
],
4455 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4458 fprintf (dump_file
, "Copying (%d, %d): ", i
,
4459 pb
->geqs
[e
].coef
[i
]);
4460 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[e
]);
4461 fprintf (dump_file
, "\n");
4462 for (t
= 0; t
<= n_vars
+ 1; t
++)
4463 fprintf (dump_file
, "%d ", pb
->geqs
[e
].coef
[t
]);
4464 fprintf (dump_file
, "\n");
4468 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4471 for (Le
= pb
->num_geqs
- 1; Le
>= 0; Le
--)
4472 if (pb
->geqs
[Le
].coef
[i
] > 0)
4473 for (Ue
= pb
->num_geqs
- 1; Ue
>= 0; Ue
--)
4474 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4477 int Lc
= pb
->geqs
[Le
].coef
[i
];
4478 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4480 if (pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4483 rS
->geqs
[e2
].touched
= iS
->geqs
[e2
].touched
= 1;
4485 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4487 fprintf (dump_file
, "---\n");
4489 "Le(Lc) = %d(%d_, Ue(Uc) = %d(%d), gen = %d\n",
4490 Le
, Lc
, Ue
, Uc
, e2
);
4491 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[Le
]);
4492 fprintf (dump_file
, "\n");
4493 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[Ue
]);
4494 fprintf (dump_file
, "\n");
4499 for (k
= n_vars
; k
>= 0; k
--)
4500 iS
->geqs
[e2
].coef
[k
] = rS
->geqs
[e2
].coef
[k
] =
4501 pb
->geqs
[Ue
].coef
[k
] + pb
->geqs
[Le
].coef
[k
];
4503 iS
->geqs
[e2
].coef
[0] -= (Uc
- 1);
4507 for (k
= n_vars
; k
>= 0; k
--)
4508 iS
->geqs
[e2
].coef
[k
] = rS
->geqs
[e2
].coef
[k
] =
4509 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4510 check_mul (pb
->geqs
[Le
].coef
[k
], Uc
);
4512 iS
->geqs
[e2
].coef
[0] -= (Uc
- 1) * (Lc
- 1);
4515 if (pb
->geqs
[Ue
].color
== omega_red
4516 || pb
->geqs
[Le
].color
== omega_red
)
4517 iS
->geqs
[e2
].color
= rS
->geqs
[e2
].color
= omega_red
;
4519 iS
->geqs
[e2
].color
= rS
->geqs
[e2
].color
= omega_black
;
4521 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4523 omega_print_geq (dump_file
, pb
, &(rS
->geqs
[e2
]));
4524 fprintf (dump_file
, "\n");
4528 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4530 else if (pb
->geqs
[Ue
].coef
[0] * Lc
+
4531 pb
->geqs
[Le
].coef
[0] * Uc
-
4532 (Uc
- 1) * (Lc
- 1) < 0)
4533 possible_easy_int_solution
= false;
4536 iS
->variables_initialized
= rS
->variables_initialized
= true;
4537 iS
->num_vars
= rS
->num_vars
= pb
->num_vars
;
4538 iS
->num_geqs
= rS
->num_geqs
= e2
;
4539 iS
->num_eqs
= rS
->num_eqs
= 0;
4540 iS
->num_subs
= rS
->num_subs
= pb
->num_subs
;
4541 iS
->safe_vars
= rS
->safe_vars
= pb
->safe_vars
;
4543 for (e
= n_vars
; e
>= 0; e
--)
4544 rS
->var
[e
] = pb
->var
[e
];
4546 for (e
= n_vars
; e
>= 0; e
--)
4547 iS
->var
[e
] = pb
->var
[e
];
4549 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
4551 omega_copy_eqn (&(rS
->subs
[e
]), &(pb
->subs
[e
]), pb
->num_vars
);
4552 omega_copy_eqn (&(iS
->subs
[e
]), &(pb
->subs
[e
]), pb
->num_vars
);
4556 n_vars
= pb
->num_vars
;
4558 if (desired_res
!= omega_true
)
4560 if (original_problem
== no_problem
)
4562 original_problem
= pb
;
4563 result
= omega_solve_geq (rS
, omega_false
);
4564 original_problem
= no_problem
;
4567 result
= omega_solve_geq (rS
, omega_false
);
4569 if (result
== omega_false
)
4576 if (pb
->num_eqs
> 0)
4578 /* An equality constraint must have been found */
4581 return omega_solve_problem (pb
, desired_res
);
4585 if (desired_res
!= omega_false
)
4588 int lower_bounds
= 0;
4589 int *lower_bound
= XNEWVEC (int, OMEGA_MAX_GEQS
);
4591 if (possible_easy_int_solution
)
4594 result
= omega_solve_geq (iS
, desired_res
);
4597 if (result
!= omega_false
)
4606 if (!exact
&& best_parallel_eqn
>= 0
4607 && parallel_difference
<= max_splinters
)
4612 return parallel_splinter (pb
, best_parallel_eqn
,
4613 parallel_difference
,
4617 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4618 fprintf (dump_file
, "have to do exact analysis\n");
4622 for (e
= 0; e
< pb
->num_geqs
; e
++)
4623 if (pb
->geqs
[e
].coef
[i
] > 1)
4624 lower_bound
[lower_bounds
++] = e
;
4626 /* Sort array LOWER_BOUND. */
4627 for (j
= 0; j
< lower_bounds
; j
++)
4629 int k
, smallest
= j
;
4631 for (k
= j
+ 1; k
< lower_bounds
; k
++)
4632 if (pb
->geqs
[lower_bound
[smallest
]].coef
[i
] >
4633 pb
->geqs
[lower_bound
[k
]].coef
[i
])
4636 k
= lower_bound
[smallest
];
4637 lower_bound
[smallest
] = lower_bound
[j
];
4641 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4643 fprintf (dump_file
, "lower bound coefficients = ");
4645 for (j
= 0; j
< lower_bounds
; j
++)
4646 fprintf (dump_file
, " %d",
4647 pb
->geqs
[lower_bound
[j
]].coef
[i
]);
4649 fprintf (dump_file
, "\n");
4652 for (j
= 0; j
< lower_bounds
; j
++)
4656 int worst_lower_bound_constant
= -minC
;
4659 max_incr
= (((pb
->geqs
[e
].coef
[i
] - 1) *
4660 (worst_lower_bound_constant
- 1) - 1)
4661 / worst_lower_bound_constant
);
4662 /* max_incr += 2; */
4664 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4666 fprintf (dump_file
, "for equation ");
4667 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
4669 "\ntry decrements from 0 to %d\n",
4671 omega_print_problem (dump_file
, pb
);
4674 if (max_incr
> 50 && !smoothed
4675 && smooth_weird_equations (pb
))
4681 goto solve_geq_start
;
4684 omega_copy_eqn (&pb
->eqs
[0], &pb
->geqs
[e
],
4686 pb
->eqs
[0].color
= omega_black
;
4687 omega_init_eqn_zero (&pb
->geqs
[e
], pb
->num_vars
);
4688 pb
->geqs
[e
].touched
= 1;
4691 for (c
= max_incr
; c
>= 0; c
--)
4693 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4696 "trying next decrement of %d\n",
4698 omega_print_problem (dump_file
, pb
);
4701 omega_copy_problem (rS
, pb
);
4703 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4704 omega_print_problem (dump_file
, rS
);
4706 result
= omega_solve_problem (rS
, desired_res
);
4708 if (result
== omega_true
)
4717 pb
->eqs
[0].coef
[0]--;
4720 if (j
+ 1 < lower_bounds
)
4723 omega_copy_eqn (&pb
->geqs
[e
], &pb
->eqs
[0],
4725 pb
->geqs
[e
].touched
= 1;
4726 pb
->geqs
[e
].color
= omega_black
;
4727 omega_copy_problem (rS
, pb
);
4729 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4731 "exhausted lower bound, "
4732 "checking if still feasible ");
4734 result
= omega_solve_problem (rS
, omega_false
);
4736 if (result
== omega_false
)
4741 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4742 fprintf (dump_file
, "fall-off the end\n");
4754 return omega_unknown
;
4755 } while (eliminate_again
);
4759 /* Because the omega solver is recursive, this counter limits the
4761 static int omega_solve_depth
= 0;
4763 /* Return omega_true when the problem PB has a solution following the
4767 omega_solve_problem (omega_pb pb
, enum omega_result desired_res
)
4769 enum omega_result result
;
4771 gcc_assert (pb
->num_vars
>= pb
->safe_vars
);
4772 omega_solve_depth
++;
4774 if (desired_res
!= omega_simplify
)
4777 if (omega_solve_depth
> 50)
4779 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4782 "Solve depth = %d, in_approximate_mode = %d, aborting\n",
4783 omega_solve_depth
, in_approximate_mode
);
4784 omega_print_problem (dump_file
, pb
);
4789 if (omega_solve_eq (pb
, desired_res
) == omega_false
)
4791 omega_solve_depth
--;
4795 if (in_approximate_mode
&& !pb
->num_geqs
)
4797 result
= omega_true
;
4798 pb
->num_vars
= pb
->safe_vars
;
4799 omega_problem_reduced (pb
);
4802 result
= omega_solve_geq (pb
, desired_res
);
4804 omega_solve_depth
--;
4806 if (!omega_reduce_with_subs
)
4808 resurrect_subs (pb
);
4809 gcc_assert (please_no_equalities_in_simplified_problems
4810 || !result
|| pb
->num_subs
== 0);
4816 /* Return true if red equations constrain the set of possible solutions.
4817 We assume that there are solutions to the black equations by
4818 themselves, so if there is no solution to the combined problem, we
4822 omega_problem_has_red_equations (omega_pb pb
)
4828 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4830 fprintf (dump_file
, "Checking for red equations:\n");
4831 omega_print_problem (dump_file
, pb
);
4834 please_no_equalities_in_simplified_problems
++;
4837 if (omega_single_result
)
4838 return_single_result
++;
4840 create_color
= true;
4841 result
= (omega_simplify_problem (pb
) == omega_false
);
4843 if (omega_single_result
)
4844 return_single_result
--;
4847 please_no_equalities_in_simplified_problems
--;
4851 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4852 fprintf (dump_file
, "Gist is FALSE\n");
4857 pb
->eqs
[0].color
= omega_red
;
4859 for (i
= pb
->num_vars
; i
> 0; i
--)
4860 pb
->eqs
[0].coef
[i
] = 0;
4862 pb
->eqs
[0].coef
[0] = 1;
4866 free_red_eliminations (pb
);
4867 gcc_assert (pb
->num_eqs
== 0);
4869 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4870 if (pb
->geqs
[e
].color
== omega_red
)
4876 for (i
= pb
->safe_vars
; i
>= 1; i
--)
4881 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4883 if (pb
->geqs
[e
].coef
[i
])
4885 if (pb
->geqs
[e
].coef
[i
] > 0)
4886 lb
|= (1 + (pb
->geqs
[e
].color
== omega_red
? 1 : 0));
4889 ub
|= (1 + (pb
->geqs
[e
].color
== omega_red
? 1 : 0));
4893 if (ub
== 2 || lb
== 2)
4896 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4897 fprintf (dump_file
, "checks for upper/lower bounds worked!\n");
4899 if (!omega_reduce_with_subs
)
4901 resurrect_subs (pb
);
4902 gcc_assert (pb
->num_subs
== 0);
4910 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4912 "*** Doing potentially expensive elimination tests "
4913 "for red equations\n");
4915 please_no_equalities_in_simplified_problems
++;
4916 omega_eliminate_red (pb
, true);
4917 please_no_equalities_in_simplified_problems
--;
4920 gcc_assert (pb
->num_eqs
== 0);
4922 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4923 if (pb
->geqs
[e
].color
== omega_red
)
4926 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4930 "******************** Redundant Red Equations eliminated!!\n");
4933 "******************** Red Equations remain\n");
4935 omega_print_problem (dump_file
, pb
);
4938 if (!omega_reduce_with_subs
)
4940 normalize_return_type r
;
4942 resurrect_subs (pb
);
4943 r
= normalize_omega_problem (pb
);
4944 gcc_assert (r
!= normalize_false
);
4947 cleanout_wildcards (pb
);
4948 gcc_assert (pb
->num_subs
== 0);
4954 /* Calls omega_simplify_problem in approximate mode. */
4957 omega_simplify_approximate (omega_pb pb
)
4959 enum omega_result result
;
4961 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4962 fprintf (dump_file
, "(Entering approximate mode\n");
4964 in_approximate_mode
= true;
4965 result
= omega_simplify_problem (pb
);
4966 in_approximate_mode
= false;
4968 gcc_assert (pb
->num_vars
== pb
->safe_vars
);
4969 if (!omega_reduce_with_subs
)
4970 gcc_assert (pb
->num_subs
== 0);
4972 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4973 fprintf (dump_file
, "Leaving approximate mode)\n");
4979 /* Simplifies problem PB by eliminating redundant constraints and
4980 reducing the constraints system to a minimal form. Returns
4981 omega_true when the problem was successfully reduced, omega_unknown
4982 when the solver is unable to determine an answer. */
4985 omega_simplify_problem (omega_pb pb
)
4989 omega_found_reduction
= omega_false
;
4991 if (!pb
->variables_initialized
)
4992 omega_initialize_variables (pb
);
4994 if (next_key
* 3 > MAX_KEYS
)
4999 next_key
= OMEGA_MAX_VARS
+ 1;
5001 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5002 pb
->geqs
[e
].touched
= 1;
5004 for (i
= 0; i
< HASH_TABLE_SIZE
; i
++)
5005 hash_master
[i
].touched
= -1;
5007 pb
->hash_version
= hash_version
;
5010 else if (pb
->hash_version
!= hash_version
)
5014 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5015 pb
->geqs
[e
].touched
= 1;
5017 pb
->hash_version
= hash_version
;
5020 if (pb
->num_vars
> pb
->num_eqs
+ 3 * pb
->safe_vars
)
5021 omega_free_eliminations (pb
, pb
->safe_vars
);
5023 if (!may_be_red
&& pb
->num_subs
== 0 && pb
->safe_vars
== 0)
5025 omega_found_reduction
= omega_solve_problem (pb
, omega_unknown
);
5027 if (omega_found_reduction
!= omega_false
5028 && !return_single_result
)
5032 (*omega_when_reduced
) (pb
);
5035 return omega_found_reduction
;
5038 omega_solve_problem (pb
, omega_simplify
);
5040 if (omega_found_reduction
!= omega_false
)
5042 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
5043 pb
->forwarding_address
[pb
->var
[i
]] = i
;
5045 for (i
= 0; i
< pb
->num_subs
; i
++)
5046 pb
->forwarding_address
[pb
->subs
[i
].key
] = -i
- 1;
5049 if (!omega_reduce_with_subs
)
5050 gcc_assert (please_no_equalities_in_simplified_problems
5051 || omega_found_reduction
== omega_false
5052 || pb
->num_subs
== 0);
5054 return omega_found_reduction
;
5057 /* Make variable VAR unprotected: it then can be eliminated. */
5060 omega_unprotect_variable (omega_pb pb
, int var
)
5063 idx
= pb
->forwarding_address
[var
];
5070 if (idx
< pb
->num_subs
)
5072 omega_copy_eqn (&pb
->subs
[idx
], &pb
->subs
[pb
->num_subs
],
5074 pb
->forwarding_address
[pb
->subs
[idx
].key
] = -idx
- 1;
5079 int *bring_to_life
= XNEWVEC (int, OMEGA_MAX_VARS
);
5082 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5083 bring_to_life
[e
] = (pb
->subs
[e
].coef
[idx
] != 0);
5085 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
5086 if (bring_to_life
[e2
])
5091 if (pb
->safe_vars
< pb
->num_vars
)
5093 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5095 pb
->geqs
[e
].coef
[pb
->num_vars
] =
5096 pb
->geqs
[e
].coef
[pb
->safe_vars
];
5098 pb
->geqs
[e
].coef
[pb
->safe_vars
] = 0;
5101 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5103 pb
->eqs
[e
].coef
[pb
->num_vars
] =
5104 pb
->eqs
[e
].coef
[pb
->safe_vars
];
5106 pb
->eqs
[e
].coef
[pb
->safe_vars
] = 0;
5109 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5111 pb
->subs
[e
].coef
[pb
->num_vars
] =
5112 pb
->subs
[e
].coef
[pb
->safe_vars
];
5114 pb
->subs
[e
].coef
[pb
->safe_vars
] = 0;
5117 pb
->var
[pb
->num_vars
] = pb
->var
[pb
->safe_vars
];
5118 pb
->forwarding_address
[pb
->var
[pb
->num_vars
]] =
5123 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5124 pb
->geqs
[e
].coef
[pb
->safe_vars
] = 0;
5126 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5127 pb
->eqs
[e
].coef
[pb
->safe_vars
] = 0;
5129 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5130 pb
->subs
[e
].coef
[pb
->safe_vars
] = 0;
5133 pb
->var
[pb
->safe_vars
] = pb
->subs
[e2
].key
;
5134 pb
->forwarding_address
[pb
->subs
[e2
].key
] = pb
->safe_vars
;
5136 omega_copy_eqn (&(pb
->eqs
[pb
->num_eqs
]), &(pb
->subs
[e2
]),
5138 pb
->eqs
[pb
->num_eqs
++].coef
[pb
->safe_vars
] = -1;
5139 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5141 if (e2
< pb
->num_subs
- 1)
5142 omega_copy_eqn (&(pb
->subs
[e2
]), &(pb
->subs
[pb
->num_subs
- 1]),
5148 omega_unprotect_1 (pb
, &idx
, NULL
);
5149 free (bring_to_life
);
5152 chain_unprotect (pb
);
5155 /* Unprotects VAR and simplifies PB. */
5158 omega_constrain_variable_sign (omega_pb pb
, enum omega_eqn_color color
,
5161 int n_vars
= pb
->num_vars
;
5163 int k
= pb
->forwarding_address
[var
];
5172 omega_copy_eqn (&pb
->geqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5174 for (j
= 0; j
<= n_vars
; j
++)
5175 pb
->geqs
[e
].coef
[j
] *= sign
;
5177 pb
->geqs
[e
].coef
[0]--;
5178 pb
->geqs
[e
].touched
= 1;
5179 pb
->geqs
[e
].color
= color
;
5184 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5185 omega_copy_eqn (&pb
->eqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5186 pb
->eqs
[e
].color
= color
;
5192 omega_init_eqn_zero (&pb
->geqs
[e
], pb
->num_vars
);
5193 pb
->geqs
[e
].coef
[k
] = sign
;
5194 pb
->geqs
[e
].coef
[0] = -1;
5195 pb
->geqs
[e
].touched
= 1;
5196 pb
->geqs
[e
].color
= color
;
5201 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5202 omega_init_eqn_zero (&pb
->eqs
[e
], pb
->num_vars
);
5203 pb
->eqs
[e
].coef
[k
] = 1;
5204 pb
->eqs
[e
].color
= color
;
5207 omega_unprotect_variable (pb
, var
);
5208 return omega_simplify_problem (pb
);
5211 /* Add an equation "VAR = VALUE" with COLOR to PB. */
5214 omega_constrain_variable_value (omega_pb pb
, enum omega_eqn_color color
,
5218 int k
= pb
->forwarding_address
[var
];
5224 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5225 omega_copy_eqn (&pb
->eqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5226 pb
->eqs
[e
].coef
[0] -= value
;
5231 omega_init_eqn_zero (&pb
->eqs
[e
], pb
->num_vars
);
5232 pb
->eqs
[e
].coef
[k
] = 1;
5233 pb
->eqs
[e
].coef
[0] = -value
;
5236 pb
->eqs
[e
].color
= color
;
5239 /* Return false when the upper and lower bounds are not coupled.
5240 Initialize the bounds LOWER_BOUND and UPPER_BOUND for the values of
5244 omega_query_variable (omega_pb pb
, int i
, int *lower_bound
, int *upper_bound
)
5246 int n_vars
= pb
->num_vars
;
5249 bool coupled
= false;
5251 *lower_bound
= neg_infinity
;
5252 *upper_bound
= pos_infinity
;
5253 i
= pb
->forwarding_address
[i
];
5259 for (j
= 1; j
<= n_vars
; j
++)
5260 if (pb
->subs
[i
].coef
[j
] != 0)
5263 *upper_bound
= *lower_bound
= pb
->subs
[i
].coef
[0];
5267 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5268 if (pb
->subs
[e
].coef
[i
] != 0)
5271 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5272 if (pb
->eqs
[e
].coef
[i
] != 0)
5276 for (j
= 1; j
<= n_vars
; j
++)
5277 if (i
!= j
&& pb
->eqs
[e
].coef
[j
] != 0)
5288 *lower_bound
= *upper_bound
=
5289 -pb
->eqs
[e
].coef
[i
] * pb
->eqs
[e
].coef
[0];
5294 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5295 if (pb
->geqs
[e
].coef
[i
] != 0)
5297 if (pb
->geqs
[e
].key
== i
)
5298 *lower_bound
= MAX (*lower_bound
, -pb
->geqs
[e
].coef
[0]);
5300 else if (pb
->geqs
[e
].key
== -i
)
5301 *upper_bound
= MIN (*upper_bound
, pb
->geqs
[e
].coef
[0]);
5310 /* Sets the lower bound L and upper bound U for the values of variable
5311 I, and sets COULD_BE_ZERO to true if variable I might take value
5312 zero. LOWER_BOUND and UPPER_BOUND are bounds on the values of
5316 query_coupled_variable (omega_pb pb
, int i
, int *l
, int *u
,
5317 bool *could_be_zero
, int lower_bound
, int upper_bound
)
5324 /* Preconditions. */
5325 gcc_assert (abs (pb
->forwarding_address
[i
]) == 1
5326 && pb
->num_vars
+ pb
->num_subs
== 2
5327 && pb
->num_eqs
+ pb
->num_subs
== 1);
5329 /* Define variable I in terms of variable V. */
5330 if (pb
->forwarding_address
[i
] == -1)
5339 sign
= -eqn
->coef
[1];
5343 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5344 if (pb
->geqs
[e
].coef
[v
] != 0)
5346 if (pb
->geqs
[e
].coef
[v
] == 1)
5347 lower_bound
= MAX (lower_bound
, -pb
->geqs
[e
].coef
[0]);
5350 upper_bound
= MIN (upper_bound
, pb
->geqs
[e
].coef
[0]);
5353 if (lower_bound
> upper_bound
)
5361 if (lower_bound
== neg_infinity
)
5363 if (eqn
->coef
[v
] > 0)
5364 b1
= sign
* neg_infinity
;
5367 b1
= -sign
* neg_infinity
;
5370 b1
= sign
* (eqn
->coef
[0] + eqn
->coef
[v
] * lower_bound
);
5372 if (upper_bound
== pos_infinity
)
5374 if (eqn
->coef
[v
] > 0)
5375 b2
= sign
* pos_infinity
;
5378 b2
= -sign
* pos_infinity
;
5381 b2
= sign
* (eqn
->coef
[0] + eqn
->coef
[v
] * upper_bound
);
5383 *l
= MAX (*l
, b1
<= b2
? b1
: b2
);
5384 *u
= MIN (*u
, b1
<= b2
? b2
: b1
);
5386 *could_be_zero
= (*l
<= 0 && 0 <= *u
5387 && int_mod (eqn
->coef
[0], abs (eqn
->coef
[v
])) == 0);
5390 /* Return false when a lower bound L and an upper bound U for variable
5391 I in problem PB have been initialized. */
5394 omega_query_variable_bounds (omega_pb pb
, int i
, int *l
, int *u
)
5399 if (!omega_query_variable (pb
, i
, l
, u
)
5400 || (pb
->num_vars
== 1 && pb
->forwarding_address
[i
] == 1))
5403 if (abs (pb
->forwarding_address
[i
]) == 1
5404 && pb
->num_vars
+ pb
->num_subs
== 2
5405 && pb
->num_eqs
+ pb
->num_subs
== 1)
5408 query_coupled_variable (pb
, i
, l
, u
, &could_be_zero
, neg_infinity
,
5416 /* For problem PB, return an integer that represents the classic data
5417 dependence direction in function of the DD_LT, DD_EQ and DD_GT bit
5418 masks that are added to the result. When DIST_KNOWN is true, DIST
5419 is set to the classic data dependence distance. LOWER_BOUND and
5420 UPPER_BOUND are bounds on the value of variable I, for example, it
5421 is possible to narrow the iteration domain with safe approximations
5422 of loop counts, and thus discard some data dependences that cannot
5426 omega_query_variable_signs (omega_pb pb
, int i
, int dd_lt
,
5427 int dd_eq
, int dd_gt
, int lower_bound
,
5428 int upper_bound
, bool *dist_known
, int *dist
)
5437 omega_query_variable (pb
, i
, &l
, &u
);
5438 query_coupled_variable (pb
, i
, &l
, &u
, &could_be_zero
, lower_bound
,
5457 *dist_known
= false;
5462 /* Initialize PB as an Omega problem with NVARS variables and NPROT
5463 safe variables. Safe variables are not eliminated during the
5464 Fourier-Motzkin elimination. Safe variables are all those
5465 variables that are placed at the beginning of the array of
5466 variables: P->var[0, ..., NPROT - 1]. */
5469 omega_alloc_problem (int nvars
, int nprot
)
5473 gcc_assert (nvars
<= OMEGA_MAX_VARS
);
5474 omega_initialize ();
5476 /* Allocate and initialize PB. */
5477 pb
= XCNEW (struct omega_pb_d
);
5478 pb
->var
= XCNEWVEC (int, OMEGA_MAX_VARS
+ 2);
5479 pb
->forwarding_address
= XCNEWVEC (int, OMEGA_MAX_VARS
+ 2);
5480 pb
->geqs
= omega_alloc_eqns (0, OMEGA_MAX_GEQS
);
5481 pb
->eqs
= omega_alloc_eqns (0, OMEGA_MAX_EQS
);
5482 pb
->subs
= omega_alloc_eqns (0, OMEGA_MAX_VARS
+ 1);
5484 pb
->hash_version
= hash_version
;
5485 pb
->num_vars
= nvars
;
5486 pb
->safe_vars
= nprot
;
5487 pb
->variables_initialized
= false;
5488 pb
->variables_freed
= false;
5495 /* Keeps the state of the initialization. */
5496 static bool omega_initialized
= false;
5498 /* Initialization of the Omega solver. */
5501 omega_initialize (void)
5505 if (omega_initialized
)
5509 next_key
= OMEGA_MAX_VARS
+ 1;
5510 packing
= XCNEWVEC (int, OMEGA_MAX_VARS
);
5511 fast_lookup
= XCNEWVEC (int, MAX_KEYS
* 2);
5512 fast_lookup_red
= XCNEWVEC (int, MAX_KEYS
* 2);
5513 hash_master
= omega_alloc_eqns (0, HASH_TABLE_SIZE
);
5515 for (i
= 0; i
< HASH_TABLE_SIZE
; i
++)
5516 hash_master
[i
].touched
= -1;
5518 sprintf (wild_name
[0], "1");
5519 sprintf (wild_name
[1], "a");
5520 sprintf (wild_name
[2], "b");
5521 sprintf (wild_name
[3], "c");
5522 sprintf (wild_name
[4], "d");
5523 sprintf (wild_name
[5], "e");
5524 sprintf (wild_name
[6], "f");
5525 sprintf (wild_name
[7], "g");
5526 sprintf (wild_name
[8], "h");
5527 sprintf (wild_name
[9], "i");
5528 sprintf (wild_name
[10], "j");
5529 sprintf (wild_name
[11], "k");
5530 sprintf (wild_name
[12], "l");
5531 sprintf (wild_name
[13], "m");
5532 sprintf (wild_name
[14], "n");
5533 sprintf (wild_name
[15], "o");
5534 sprintf (wild_name
[16], "p");
5535 sprintf (wild_name
[17], "q");
5536 sprintf (wild_name
[18], "r");
5537 sprintf (wild_name
[19], "s");
5538 sprintf (wild_name
[20], "t");
5539 sprintf (wild_name
[40 - 1], "alpha");
5540 sprintf (wild_name
[40 - 2], "beta");
5541 sprintf (wild_name
[40 - 3], "gamma");
5542 sprintf (wild_name
[40 - 4], "delta");
5543 sprintf (wild_name
[40 - 5], "tau");
5544 sprintf (wild_name
[40 - 6], "sigma");
5545 sprintf (wild_name
[40 - 7], "chi");
5546 sprintf (wild_name
[40 - 8], "omega");
5547 sprintf (wild_name
[40 - 9], "pi");
5548 sprintf (wild_name
[40 - 10], "ni");
5549 sprintf (wild_name
[40 - 11], "Alpha");
5550 sprintf (wild_name
[40 - 12], "Beta");
5551 sprintf (wild_name
[40 - 13], "Gamma");
5552 sprintf (wild_name
[40 - 14], "Delta");
5553 sprintf (wild_name
[40 - 15], "Tau");
5554 sprintf (wild_name
[40 - 16], "Sigma");
5555 sprintf (wild_name
[40 - 17], "Chi");
5556 sprintf (wild_name
[40 - 18], "Omega");
5557 sprintf (wild_name
[40 - 19], "xxx");
5559 omega_initialized
= true;