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 Free Software Foundation, Inc.
9 Contributed by Sebastian Pop <sebastian.pop@inria.fr>
11 This file is part of GCC.
13 GCC is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free
15 Software Foundation; either version 2, or (at your option) any later
18 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING. If not, write to the Free
25 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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"
41 #include "diagnostic.h"
43 #include "tree-pass.h"
46 /* When set to true, keep substitution variables. When set to false,
47 resurrect substitution variables (convert substitutions back to EQs). */
48 static bool omega_reduce_with_subs
= true;
50 /* When set to true, omega_simplify_problem checks for problem with no
51 solutions, calling verify_omega_pb. */
52 static bool omega_verify_simplification
= false;
54 /* When set to true, only produce a single simplified result. */
55 static bool omega_single_result
= false;
57 /* Set return_single_result to 1 when omega_single_result is true. */
58 static int return_single_result
= 0;
60 /* Hash table for equations generated by the solver. */
61 #define HASH_TABLE_SIZE PARAM_VALUE (PARAM_OMEGA_HASH_TABLE_SIZE)
62 #define MAX_KEYS PARAM_VALUE (PARAM_OMEGA_MAX_KEYS)
63 static eqn hash_master
;
65 static int hash_version
= 0;
67 /* Set to true for making the solver enter in approximation mode. */
68 static bool in_approximate_mode
= false;
70 /* When set to zero, the solver is allowed to add new equalities to
71 the problem to be solved. */
72 static int conservative
= 0;
74 /* Set to omega_true when the problem was successfully reduced, set to
75 omega_unknown when the solver is unable to determine an answer. */
76 static enum omega_result omega_found_reduction
;
78 /* Set to true when the solver is allowed to add omega_red equations. */
79 static bool create_color
= false;
81 /* Set to nonzero when the problem to be solved can be reduced. */
82 static int may_be_red
= 0;
84 /* When false, there should be no substitution equations in the
85 simplified problem. */
86 static int please_no_equalities_in_simplified_problems
= 0;
88 /* Variables names for pretty printing. */
89 static char wild_name
[200][40];
91 /* Pointer to the void problem. */
92 static omega_pb no_problem
= (omega_pb
) 0;
94 /* Pointer to the problem to be solved. */
95 static omega_pb original_problem
= (omega_pb
) 0;
98 /* Return the integer A divided by B. */
101 int_div (int a
, int b
)
106 return -((-a
+ b
- 1)/b
);
109 /* Return the integer A modulo B. */
112 int_mod (int a
, int b
)
114 return a
- b
* int_div (a
, b
);
117 /* For X and Y positive integers, return X multiplied by Y and check
118 that the result does not overflow. */
121 check_pos_mul (int x
, int y
)
124 gcc_assert ((INT_MAX
) / x
> y
);
129 /* Return X multiplied by Y and check that the result does not
133 check_mul (int x
, int y
)
138 return check_pos_mul (x
, y
);
140 return -check_pos_mul (x
, -y
);
143 return -check_pos_mul (-x
, y
);
145 return check_pos_mul (-x
, -y
);
148 /* Test whether equation E is red. */
151 omega_eqn_is_red (eqn e
, int desired_res
)
153 return (desired_res
== omega_simplify
&& e
->color
== omega_red
);
156 /* Return a string for VARIABLE. */
159 omega_var_to_str (int variable
)
161 if (0 <= variable
&& variable
<= 20)
162 return wild_name
[variable
];
164 if (-20 < variable
&& variable
< 0)
165 return wild_name
[40 + variable
];
167 /* Collapse all the entries that would have overflowed. */
168 return wild_name
[21];
171 /* Return a string for variable I in problem PB. */
174 omega_variable_to_str (omega_pb pb
, int i
)
176 return omega_var_to_str (pb
->var
[i
]);
179 /* Do nothing function: used for default initializations. */
182 omega_no_procedure (omega_pb pb ATTRIBUTE_UNUSED
)
186 void (*omega_when_reduced
) (omega_pb
) = omega_no_procedure
;
188 /* Compute the greatest common divisor of A and B. */
206 /* Print to FILE from PB equation E with all its coefficients
210 omega_print_term (FILE *file
, omega_pb pb
, eqn e
, int c
)
214 int n
= pb
->num_vars
;
217 for (i
= 1; i
<= n
; i
++)
218 if (c
* e
->coef
[i
] > 0)
223 if (c
* e
->coef
[i
] == 1)
224 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
226 fprintf (file
, "%d * %s", c
* e
->coef
[i
],
227 omega_variable_to_str (pb
, i
));
231 for (i
= 1; i
<= n
; i
++)
232 if (i
!= went_first
&& c
* e
->coef
[i
] != 0)
234 if (!first
&& c
* e
->coef
[i
] > 0)
235 fprintf (file
, " + ");
239 if (c
* e
->coef
[i
] == 1)
240 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
241 else if (c
* e
->coef
[i
] == -1)
242 fprintf (file
, " - %s", omega_variable_to_str (pb
, i
));
244 fprintf (file
, "%d * %s", c
* e
->coef
[i
],
245 omega_variable_to_str (pb
, i
));
248 if (!first
&& c
* e
->coef
[0] > 0)
249 fprintf (file
, " + ");
251 if (first
|| c
* e
->coef
[0] != 0)
252 fprintf (file
, "%d", c
* e
->coef
[0]);
255 /* Print to FILE the equation E of problem PB. */
258 omega_print_eqn (FILE *file
, omega_pb pb
, eqn e
, bool test
, int extra
)
261 int n
= pb
->num_vars
+ extra
;
262 bool is_lt
= test
&& e
->coef
[0] == -1;
270 else if (e
->key
!= 0)
271 fprintf (file
, "%d: ", e
->key
);
274 if (e
->color
== omega_red
)
279 for (i
= is_lt
? 1 : 0; i
<= n
; i
++)
283 fprintf (file
, " + ");
288 fprintf (file
, "%d", -e
->coef
[i
]);
289 else if (e
->coef
[i
] == -1)
290 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
292 fprintf (file
, "%d * %s", -e
->coef
[i
],
293 omega_variable_to_str (pb
, i
));
308 fprintf (file
, " = ");
310 fprintf (file
, " < ");
312 fprintf (file
, " <= ");
316 for (i
= 0; i
<= n
; i
++)
320 fprintf (file
, " + ");
325 fprintf (file
, "%d", e
->coef
[i
]);
326 else if (e
->coef
[i
] == 1)
327 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
329 fprintf (file
, "%d * %s", e
->coef
[i
],
330 omega_variable_to_str (pb
, i
));
336 if (e
->color
== omega_red
)
340 /* Print to FILE all the variables of problem PB. */
343 omega_print_vars (FILE *file
, omega_pb pb
)
347 fprintf (file
, "variables = ");
349 if (pb
->safe_vars
> 0)
350 fprintf (file
, "protected (");
352 for (i
= 1; i
<= pb
->num_vars
; i
++)
354 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
356 if (i
== pb
->safe_vars
)
359 if (i
< pb
->num_vars
)
360 fprintf (file
, ", ");
363 fprintf (file
, "\n");
366 /* Debug problem PB. */
369 debug_omega_problem (omega_pb pb
)
371 omega_print_problem (stderr
, pb
);
374 /* Print to FILE problem PB. */
377 omega_print_problem (FILE *file
, omega_pb pb
)
381 if (!pb
->variables_initialized
)
382 omega_initialize_variables (pb
);
384 omega_print_vars (file
, pb
);
386 for (e
= 0; e
< pb
->num_eqs
; e
++)
388 omega_print_eq (file
, pb
, &pb
->eqs
[e
]);
389 fprintf (file
, "\n");
392 fprintf (file
, "Done with EQ\n");
394 for (e
= 0; e
< pb
->num_geqs
; e
++)
396 omega_print_geq (file
, pb
, &pb
->geqs
[e
]);
397 fprintf (file
, "\n");
400 fprintf (file
, "Done with GEQ\n");
402 for (e
= 0; e
< pb
->num_subs
; e
++)
404 eqn eq
= &pb
->subs
[e
];
406 if (eq
->color
== omega_red
)
410 fprintf (file
, "%s := ", omega_var_to_str (eq
->key
));
412 fprintf (file
, "#%d := ", eq
->key
);
414 omega_print_term (file
, pb
, eq
, 1);
416 if (eq
->color
== omega_red
)
419 fprintf (file
, "\n");
423 /* Return the number of equations in PB tagged omega_red. */
426 omega_count_red_equations (omega_pb pb
)
431 for (e
= 0; e
< pb
->num_eqs
; e
++)
432 if (pb
->eqs
[e
].color
== omega_red
)
434 for (i
= pb
->num_vars
; i
> 0; i
--)
435 if (pb
->geqs
[e
].coef
[i
])
438 if (i
== 0 && pb
->geqs
[e
].coef
[0] == 1)
444 for (e
= 0; e
< pb
->num_geqs
; e
++)
445 if (pb
->geqs
[e
].color
== omega_red
)
448 for (e
= 0; e
< pb
->num_subs
; e
++)
449 if (pb
->subs
[e
].color
== omega_red
)
455 /* Print to FILE all the equations in PB that are tagged omega_red. */
458 omega_print_red_equations (FILE *file
, omega_pb pb
)
462 if (!pb
->variables_initialized
)
463 omega_initialize_variables (pb
);
465 omega_print_vars (file
, pb
);
467 for (e
= 0; e
< pb
->num_eqs
; e
++)
468 if (pb
->eqs
[e
].color
== omega_red
)
470 omega_print_eq (file
, pb
, &pb
->eqs
[e
]);
471 fprintf (file
, "\n");
474 for (e
= 0; e
< pb
->num_geqs
; e
++)
475 if (pb
->geqs
[e
].color
== omega_red
)
477 omega_print_geq (file
, pb
, &pb
->geqs
[e
]);
478 fprintf (file
, "\n");
481 for (e
= 0; e
< pb
->num_subs
; e
++)
482 if (pb
->subs
[e
].color
== omega_red
)
484 eqn eq
= &pb
->subs
[e
];
488 fprintf (file
, "%s := ", omega_var_to_str (eq
->key
));
490 fprintf (file
, "#%d := ", eq
->key
);
492 omega_print_term (file
, pb
, eq
, 1);
493 fprintf (file
, "]\n");
497 /* Pretty print PB to FILE. */
500 omega_pretty_print_problem (FILE *file
, omega_pb pb
)
502 int e
, v
, v1
, v2
, v3
, t
;
503 bool *live
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
504 int stuffPrinted
= 0;
509 } partial_order_type
;
511 partial_order_type
**po
= XNEWVEC (partial_order_type
*,
512 OMEGA_MAX_VARS
* OMEGA_MAX_VARS
);
513 int **po_eq
= XNEWVEC (int *, OMEGA_MAX_VARS
* OMEGA_MAX_VARS
);
514 int *last_links
= XNEWVEC (int, OMEGA_MAX_VARS
);
515 int *first_links
= XNEWVEC (int, OMEGA_MAX_VARS
);
516 int *chain_length
= XNEWVEC (int, OMEGA_MAX_VARS
);
517 int *chain
= XNEWVEC (int, OMEGA_MAX_VARS
);
521 if (!pb
->variables_initialized
)
522 omega_initialize_variables (pb
);
524 if (pb
->num_vars
> 0)
526 omega_eliminate_redundant (pb
, false);
528 for (e
= 0; e
< pb
->num_eqs
; e
++)
531 fprintf (file
, "; ");
534 omega_print_eq (file
, pb
, &pb
->eqs
[e
]);
537 for (e
= 0; e
< pb
->num_geqs
; e
++)
542 for (v
= 1; v
<= pb
->num_vars
; v
++)
544 last_links
[v
] = first_links
[v
] = 0;
547 for (v2
= 1; v2
<= pb
->num_vars
; v2
++)
551 for (e
= 0; e
< pb
->num_geqs
; e
++)
554 for (v
= 1; v
<= pb
->num_vars
; v
++)
555 if (pb
->geqs
[e
].coef
[v
] == 1)
557 else if (pb
->geqs
[e
].coef
[v
] == -1)
562 while (v1
> 0 && pb
->geqs
[e
].coef
[v1
] == 0)
567 while (v2
> 0 && pb
->geqs
[e
].coef
[v2
] == 0)
572 while (v3
> 0 && pb
->geqs
[e
].coef
[v3
] == 0)
575 if (pb
->geqs
[e
].coef
[0] > 0 || pb
->geqs
[e
].coef
[0] < -1
577 || pb
->geqs
[e
].coef
[v1
] * pb
->geqs
[e
].coef
[v2
] != -1)
579 /* Not a partial order relation. */
583 if (pb
->geqs
[e
].coef
[v1
] == 1)
590 /* Relation is v1 <= v2 or v1 < v2. */
591 po
[v1
][v2
] = ((pb
->geqs
[e
].coef
[0] == 0) ? le
: lt
);
595 for (v
= 1; v
<= pb
->num_vars
; v
++)
596 chain_length
[v
] = last_links
[v
];
598 /* Just in case pb->num_vars <= 0. */
600 for (t
= 0; t
< pb
->num_vars
; t
++)
604 for (v1
= 1; v1
<= pb
->num_vars
; v1
++)
605 for (v2
= 1; v2
<= pb
->num_vars
; v2
++)
606 if (po
[v1
][v2
] != none
&&
607 chain_length
[v1
] <= chain_length
[v2
])
609 chain_length
[v1
] = chain_length
[v2
] + 1;
614 /* Caught in cycle. */
615 gcc_assert (!change
);
617 for (v1
= 1; v1
<= pb
->num_vars
; v1
++)
618 if (chain_length
[v1
] == 0)
623 for (v1
= 2; v1
<= pb
->num_vars
; v1
++)
624 if (chain_length
[v1
] + first_links
[v1
] >
625 chain_length
[v
] + first_links
[v
])
628 if (chain_length
[v
] + first_links
[v
] == 0)
632 fprintf (file
, "; ");
636 /* Chain starts at v. */
641 for (e
= 0; e
< pb
->num_geqs
; e
++)
642 if (live
[e
] && pb
->geqs
[e
].coef
[v
] == 1)
645 fprintf (file
, ", ");
647 tmp
= pb
->geqs
[e
].coef
[v
];
648 pb
->geqs
[e
].coef
[v
] = 0;
649 omega_print_term (file
, pb
, &pb
->geqs
[e
], -1);
650 pb
->geqs
[e
].coef
[v
] = tmp
;
656 fprintf (file
, " <= ");
665 for (v2
= 1; v2
<= pb
->num_vars
; v2
++)
666 if (po
[v
][v2
] && chain_length
[v
] == 1 + chain_length
[v2
])
669 if (v2
> pb
->num_vars
)
676 fprintf (file
, "%s", omega_variable_to_str (pb
, chain
[0]));
678 for (multiprint
= false, i
= 1; i
< m
; i
++)
684 fprintf (file
, " <= ");
686 fprintf (file
, " < ");
688 fprintf (file
, "%s", omega_variable_to_str (pb
, v2
));
689 live
[po_eq
[v
][v2
]] = false;
691 if (!multiprint
&& i
< m
- 1)
692 for (v3
= 1; v3
<= pb
->num_vars
; v3
++)
694 if (v
== v3
|| v2
== v3
695 || po
[v
][v2
] != po
[v
][v3
]
696 || po
[v2
][chain
[i
+ 1]] != po
[v3
][chain
[i
+ 1]])
699 fprintf (file
, ",%s", omega_variable_to_str (pb
, v3
));
700 live
[po_eq
[v
][v3
]] = false;
701 live
[po_eq
[v3
][chain
[i
+ 1]]] = false;
709 /* Print last_links. */
714 for (e
= 0; e
< pb
->num_geqs
; e
++)
715 if (live
[e
] && pb
->geqs
[e
].coef
[v
] == -1)
718 fprintf (file
, ", ");
720 fprintf (file
, " <= ");
722 tmp
= pb
->geqs
[e
].coef
[v
];
723 pb
->geqs
[e
].coef
[v
] = 0;
724 omega_print_term (file
, pb
, &pb
->geqs
[e
], 1);
725 pb
->geqs
[e
].coef
[v
] = tmp
;
732 for (e
= 0; e
< pb
->num_geqs
; e
++)
736 fprintf (file
, "; ");
738 omega_print_geq (file
, pb
, &pb
->geqs
[e
]);
741 for (e
= 0; e
< pb
->num_subs
; e
++)
743 eqn eq
= &pb
->subs
[e
];
746 fprintf (file
, "; ");
751 fprintf (file
, "%s := ", omega_var_to_str (eq
->key
));
753 fprintf (file
, "#%d := ", eq
->key
);
755 omega_print_term (file
, pb
, eq
, 1);
768 /* Assign to variable I in PB the next wildcard name. The name of a
769 wildcard is a negative number. */
770 static int next_wild_card
= 0;
773 omega_name_wild_card (omega_pb pb
, int i
)
776 if (next_wild_card
< -PARAM_VALUE (PARAM_OMEGA_MAX_WILD_CARDS
))
778 pb
->var
[i
] = next_wild_card
;
781 /* Return the index of the last protected (or safe) variable in PB,
782 after having added a new wildcard variable. */
785 omega_add_new_wild_card (omega_pb pb
)
788 int i
= ++pb
->safe_vars
;
791 /* Make a free place in the protected (safe) variables, by moving
792 the non protected variable pointed by "I" at the end, ie. at
793 offset pb->num_vars. */
794 if (pb
->num_vars
!= i
)
796 /* Move "I" for all the inequalities. */
797 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
799 if (pb
->geqs
[e
].coef
[i
])
800 pb
->geqs
[e
].touched
= 1;
802 pb
->geqs
[e
].coef
[pb
->num_vars
] = pb
->geqs
[e
].coef
[i
];
805 /* Move "I" for all the equalities. */
806 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
807 pb
->eqs
[e
].coef
[pb
->num_vars
] = pb
->eqs
[e
].coef
[i
];
809 /* Move "I" for all the substitutions. */
810 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
811 pb
->subs
[e
].coef
[pb
->num_vars
] = pb
->subs
[e
].coef
[i
];
813 /* Move the identifier. */
814 pb
->var
[pb
->num_vars
] = pb
->var
[i
];
817 /* Initialize at zero all the coefficients */
818 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
819 pb
->geqs
[e
].coef
[i
] = 0;
821 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
822 pb
->eqs
[e
].coef
[i
] = 0;
824 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
825 pb
->subs
[e
].coef
[i
] = 0;
827 /* And give it a name. */
828 omega_name_wild_card (pb
, i
);
832 /* Delete inequality E from problem PB that has N_VARS variables. */
835 omega_delete_geq (omega_pb pb
, int e
, int n_vars
)
837 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
839 fprintf (dump_file
, "Deleting %d (last:%d): ", e
, pb
->num_geqs
- 1);
840 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
841 fprintf (dump_file
, "\n");
844 if (e
< pb
->num_geqs
- 1)
845 omega_copy_eqn (&pb
->geqs
[e
], &pb
->geqs
[pb
->num_geqs
- 1], n_vars
);
850 /* Delete extra inequality E from problem PB that has N_VARS
854 omega_delete_geq_extra (omega_pb pb
, int e
, int n_vars
)
856 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
858 fprintf (dump_file
, "Deleting %d: ",e
);
859 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[e
]);
860 fprintf (dump_file
, "\n");
863 if (e
< pb
->num_geqs
- 1)
864 omega_copy_eqn (&pb
->geqs
[e
], &pb
->geqs
[pb
->num_geqs
- 1], n_vars
);
870 /* Remove variable I from problem PB. */
873 omega_delete_variable (omega_pb pb
, int i
)
875 int n_vars
= pb
->num_vars
;
878 if (omega_safe_var_p (pb
, i
))
880 int j
= pb
->safe_vars
;
882 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
884 pb
->geqs
[e
].touched
= 1;
885 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[j
];
886 pb
->geqs
[e
].coef
[j
] = pb
->geqs
[e
].coef
[n_vars
];
889 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
891 pb
->eqs
[e
].coef
[i
] = pb
->eqs
[e
].coef
[j
];
892 pb
->eqs
[e
].coef
[j
] = pb
->eqs
[e
].coef
[n_vars
];
895 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
897 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[j
];
898 pb
->subs
[e
].coef
[j
] = pb
->subs
[e
].coef
[n_vars
];
901 pb
->var
[i
] = pb
->var
[j
];
902 pb
->var
[j
] = pb
->var
[n_vars
];
906 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
907 if (pb
->geqs
[e
].coef
[n_vars
])
909 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[n_vars
];
910 pb
->geqs
[e
].touched
= 1;
913 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
914 pb
->eqs
[e
].coef
[i
] = pb
->eqs
[e
].coef
[n_vars
];
916 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
917 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[n_vars
];
919 pb
->var
[i
] = pb
->var
[n_vars
];
922 if (omega_safe_var_p (pb
, i
))
928 /* Because the coefficients of an equation are sparse, PACKING records
929 indices for non null coefficients. */
932 /* Set up the coefficients of PACKING, following the coefficients of
933 equation EQN that has NUM_VARS variables. */
936 setup_packing (eqn eqn
, int num_vars
)
941 for (k
= num_vars
; k
>= 0; k
--)
948 /* Computes a linear combination of EQ and SUB at VAR with coefficient
949 C, such that EQ->coef[VAR] is set to 0. TOP_VAR is the number of
950 non null indices of SUB stored in PACKING. */
953 omega_substitute_red_1 (eqn eq
, eqn sub
, int var
, int c
, bool *found_black
,
956 if (eq
->coef
[var
] != 0)
958 if (eq
->color
== omega_black
)
962 int j
, k
= eq
->coef
[var
];
966 for (j
= top_var
; j
>= 0; j
--)
967 eq
->coef
[packing
[j
]] -= sub
->coef
[packing
[j
]] * k
* c
;
972 /* Substitute in PB variable VAR with "C * SUB". */
975 omega_substitute_red (omega_pb pb
, eqn sub
, int var
, int c
, bool *found_black
)
977 int e
, top_var
= setup_packing (sub
, pb
->num_vars
);
979 *found_black
= false;
981 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
983 if (sub
->color
== omega_red
)
984 fprintf (dump_file
, "[");
986 fprintf (dump_file
, "substituting using %s := ",
987 omega_variable_to_str (pb
, var
));
988 omega_print_term (dump_file
, pb
, sub
, -c
);
990 if (sub
->color
== omega_red
)
991 fprintf (dump_file
, "]");
993 fprintf (dump_file
, "\n");
994 omega_print_vars (dump_file
, pb
);
997 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
999 eqn eqn
= &(pb
->eqs
[e
]);
1001 omega_substitute_red_1 (eqn
, sub
, var
, c
, found_black
, top_var
);
1003 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1005 omega_print_eq (dump_file
, pb
, eqn
);
1006 fprintf (dump_file
, "\n");
1010 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1012 eqn eqn
= &(pb
->geqs
[e
]);
1014 omega_substitute_red_1 (eqn
, sub
, var
, c
, found_black
, top_var
);
1016 if (eqn
->coef
[var
] && eqn
->color
== omega_red
)
1019 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1021 omega_print_geq (dump_file
, pb
, eqn
);
1022 fprintf (dump_file
, "\n");
1026 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1028 eqn eqn
= &(pb
->subs
[e
]);
1030 omega_substitute_red_1 (eqn
, sub
, var
, c
, found_black
, top_var
);
1032 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1034 fprintf (dump_file
, "%s := ", omega_var_to_str (eqn
->key
));
1035 omega_print_term (dump_file
, pb
, eqn
, 1);
1036 fprintf (dump_file
, "\n");
1040 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1041 fprintf (dump_file
, "---\n\n");
1043 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1044 *found_black
= true;
1047 /* Substitute in PB variable VAR with "C * SUB". */
1050 omega_substitute (omega_pb pb
, eqn sub
, int var
, int c
)
1053 int top_var
= setup_packing (sub
, pb
->num_vars
);
1055 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1057 fprintf (dump_file
, "substituting using %s := ",
1058 omega_variable_to_str (pb
, var
));
1059 omega_print_term (dump_file
, pb
, sub
, -c
);
1060 fprintf (dump_file
, "\n");
1061 omega_print_vars (dump_file
, pb
);
1066 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1067 pb
->eqs
[e
].coef
[var
] = 0;
1069 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1070 if (pb
->geqs
[e
].coef
[var
] != 0)
1072 pb
->geqs
[e
].touched
= 1;
1073 pb
->geqs
[e
].coef
[var
] = 0;
1076 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1077 pb
->subs
[e
].coef
[var
] = 0;
1079 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1082 eqn eqn
= &(pb
->subs
[pb
->num_subs
++]);
1084 for (k
= pb
->num_vars
; k
>= 0; k
--)
1087 eqn
->key
= pb
->var
[var
];
1088 eqn
->color
= omega_black
;
1091 else if (top_var
== 0 && packing
[0] == 0)
1093 c
= -sub
->coef
[0] * c
;
1095 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1097 pb
->eqs
[e
].coef
[0] += pb
->eqs
[e
].coef
[var
] * c
;
1098 pb
->eqs
[e
].coef
[var
] = 0;
1101 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1102 if (pb
->geqs
[e
].coef
[var
] != 0)
1104 pb
->geqs
[e
].coef
[0] += pb
->geqs
[e
].coef
[var
] * c
;
1105 pb
->geqs
[e
].coef
[var
] = 0;
1106 pb
->geqs
[e
].touched
= 1;
1109 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1111 pb
->subs
[e
].coef
[0] += pb
->subs
[e
].coef
[var
] * c
;
1112 pb
->subs
[e
].coef
[var
] = 0;
1115 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1118 eqn eqn
= &(pb
->subs
[pb
->num_subs
++]);
1120 for (k
= pb
->num_vars
; k
>= 1; k
--)
1124 eqn
->key
= pb
->var
[var
];
1125 eqn
->color
= omega_black
;
1128 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1130 fprintf (dump_file
, "---\n\n");
1131 omega_print_problem (dump_file
, pb
);
1132 fprintf (dump_file
, "===\n\n");
1137 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1139 eqn eqn
= &(pb
->eqs
[e
]);
1140 int k
= eqn
->coef
[var
];
1147 for (j
= top_var
; j
>= 0; j
--)
1150 eqn
->coef
[j0
] -= sub
->coef
[j0
] * k
;
1154 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1156 omega_print_eq (dump_file
, pb
, eqn
);
1157 fprintf (dump_file
, "\n");
1161 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1163 eqn eqn
= &(pb
->geqs
[e
]);
1164 int k
= eqn
->coef
[var
];
1172 for (j
= top_var
; j
>= 0; j
--)
1175 eqn
->coef
[j0
] -= sub
->coef
[j0
] * k
;
1179 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1181 omega_print_geq (dump_file
, pb
, eqn
);
1182 fprintf (dump_file
, "\n");
1186 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1188 eqn eqn
= &(pb
->subs
[e
]);
1189 int k
= eqn
->coef
[var
];
1196 for (j
= top_var
; j
>= 0; j
--)
1199 eqn
->coef
[j0
] -= sub
->coef
[j0
] * k
;
1203 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1205 fprintf (dump_file
, "%s := ", omega_var_to_str (eqn
->key
));
1206 omega_print_term (dump_file
, pb
, eqn
, 1);
1207 fprintf (dump_file
, "\n");
1211 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1213 fprintf (dump_file
, "---\n\n");
1214 omega_print_problem (dump_file
, pb
);
1215 fprintf (dump_file
, "===\n\n");
1218 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1221 eqn eqn
= &(pb
->subs
[pb
->num_subs
++]);
1224 for (k
= pb
->num_vars
; k
>= 0; k
--)
1225 eqn
->coef
[k
] = c
* (sub
->coef
[k
]);
1227 eqn
->key
= pb
->var
[var
];
1228 eqn
->color
= sub
->color
;
1233 /* Solve e = factor alpha for x_j and substitute. */
1236 omega_do_mod (omega_pb pb
, int factor
, int e
, int j
)
1239 eqn eq
= omega_alloc_eqns (0, 1);
1241 bool kill_j
= false;
1243 omega_copy_eqn (eq
, &pb
->eqs
[e
], pb
->num_vars
);
1245 for (k
= pb
->num_vars
; k
>= 0; k
--)
1247 eq
->coef
[k
] = int_mod (eq
->coef
[k
], factor
);
1249 if (2 * eq
->coef
[k
] >= factor
)
1250 eq
->coef
[k
] -= factor
;
1253 nfactor
= eq
->coef
[j
];
1255 if (omega_safe_var_p (pb
, j
) && !omega_wildcard_p (pb
, j
))
1257 i
= omega_add_new_wild_card (pb
);
1258 eq
->coef
[pb
->num_vars
] = eq
->coef
[i
];
1260 eq
->coef
[i
] = -factor
;
1265 eq
->coef
[j
] = -factor
;
1266 if (!omega_wildcard_p (pb
, j
))
1267 omega_name_wild_card (pb
, j
);
1270 omega_substitute (pb
, eq
, j
, nfactor
);
1272 for (k
= pb
->num_vars
; k
>= 0; k
--)
1273 pb
->eqs
[e
].coef
[k
] = pb
->eqs
[e
].coef
[k
] / factor
;
1276 omega_delete_variable (pb
, j
);
1278 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1280 fprintf (dump_file
, "Mod-ing and normalizing produces:\n");
1281 omega_print_problem (dump_file
, pb
);
1284 omega_free_eqns (eq
, 1);
1287 /* Multiplies by -1 inequality E. */
1290 omega_negate_geq (omega_pb pb
, int e
)
1294 for (i
= pb
->num_vars
; i
>= 0; i
--)
1295 pb
->geqs
[e
].coef
[i
] *= -1;
1297 pb
->geqs
[e
].coef
[0]--;
1298 pb
->geqs
[e
].touched
= 1;
1301 /* Returns OMEGA_TRUE when problem PB has a solution. */
1303 static enum omega_result
1304 verify_omega_pb (omega_pb pb
)
1306 enum omega_result result
;
1308 bool any_color
= false;
1309 omega_pb tmp_problem
= XNEW (struct omega_pb
);
1311 omega_copy_problem (tmp_problem
, pb
);
1312 tmp_problem
->safe_vars
= 0;
1313 tmp_problem
->num_subs
= 0;
1315 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1316 if (pb
->geqs
[e
].color
== omega_red
)
1322 if (please_no_equalities_in_simplified_problems
)
1326 original_problem
= no_problem
;
1328 original_problem
= pb
;
1330 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1332 fprintf (dump_file
, "verifying problem");
1335 fprintf (dump_file
, " (color mode)");
1337 fprintf (dump_file
, " :\n");
1338 omega_print_problem (dump_file
, pb
);
1341 result
= omega_solve_problem (tmp_problem
, omega_unknown
);
1342 original_problem
= no_problem
;
1345 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1347 if (result
!= omega_false
)
1348 fprintf (dump_file
, "verified problem\n");
1350 fprintf (dump_file
, "disproved problem\n");
1351 omega_print_problem (dump_file
, pb
);
1357 /* Add a new equality to problem PB at last position E. */
1360 adding_equality_constraint (omega_pb pb
, int e
)
1362 if (original_problem
!= no_problem
1363 && original_problem
!= pb
1367 int e2
= original_problem
->num_eqs
++;
1369 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1371 "adding equality constraint %d to outer problem\n", e2
);
1372 omega_init_eqn_zero (&original_problem
->eqs
[e2
],
1373 original_problem
->num_vars
);
1375 for (i
= pb
->num_vars
; i
>= 1; i
--)
1377 for (j
= original_problem
->num_vars
; j
>= 1; j
--)
1378 if (original_problem
->var
[j
] == pb
->var
[i
])
1383 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1384 fprintf (dump_file
, "retracting\n");
1385 original_problem
->num_eqs
--;
1388 original_problem
->eqs
[e2
].coef
[j
] = pb
->eqs
[e
].coef
[i
];
1391 original_problem
->eqs
[e2
].coef
[0] = pb
->eqs
[e
].coef
[0];
1393 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1394 omega_print_problem (dump_file
, original_problem
);
1398 static int *fast_lookup
;
1399 static int *fast_lookup_red
;
1403 normalize_uncoupled
,
1405 } normalize_return_type
;
1407 /* Normalizes PB by removing redundant constraints. Returns
1408 normalize_false when the constraints system has no solution,
1409 otherwise returns normalize_coupled or normalize_uncoupled. */
1411 static normalize_return_type
1412 normalize_omega_problem (omega_pb pb
)
1414 int e
, i
, j
, k
, n_vars
;
1415 int coupled_subscripts
= 0;
1417 n_vars
= pb
->num_vars
;
1419 for (e
= 0; e
< pb
->num_geqs
; e
++)
1421 if (!pb
->geqs
[e
].touched
)
1423 if (!single_var_geq (&pb
->geqs
[e
], n_vars
))
1424 coupled_subscripts
= 1;
1428 int g
, top_var
, i0
, hashCode
;
1429 int *p
= &packing
[0];
1431 for (k
= 1; k
<= n_vars
; k
++)
1432 if (pb
->geqs
[e
].coef
[k
])
1435 top_var
= (p
- &packing
[0]) - 1;
1439 if (pb
->geqs
[e
].coef
[0] < 0)
1441 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1443 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1444 fprintf (dump_file
, "\nequations have no solution \n");
1446 return normalize_false
;
1449 omega_delete_geq (pb
, e
, n_vars
);
1453 else if (top_var
== 0)
1455 int singlevar
= packing
[0];
1457 g
= pb
->geqs
[e
].coef
[singlevar
];
1461 pb
->geqs
[e
].coef
[singlevar
] = 1;
1462 pb
->geqs
[e
].key
= singlevar
;
1467 pb
->geqs
[e
].coef
[singlevar
] = -1;
1468 pb
->geqs
[e
].key
= -singlevar
;
1472 pb
->geqs
[e
].coef
[0] = int_div (pb
->geqs
[e
].coef
[0], g
);
1477 int hash_key_multiplier
= 31;
1479 coupled_subscripts
= 1;
1482 g
= pb
->geqs
[e
].coef
[i
];
1483 hashCode
= g
* (i
+ 3);
1488 for (; i0
>= 0; i0
--)
1493 x
= pb
->geqs
[e
].coef
[i
];
1494 hashCode
= hashCode
* hash_key_multiplier
* (i
+ 3) + x
;
1509 for (; i0
>= 0; i0
--)
1513 x
= pb
->geqs
[e
].coef
[i
];
1514 hashCode
= hashCode
* hash_key_multiplier
* (i
+ 3) + x
;
1519 pb
->geqs
[e
].coef
[0] = int_div (pb
->geqs
[e
].coef
[0], g
);
1522 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[i
] / g
;
1523 hashCode
= pb
->geqs
[e
].coef
[i
] * (i
+ 3);
1525 for (; i0
>= 0; i0
--)
1528 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[i
] / g
;
1529 hashCode
= hashCode
* hash_key_multiplier
* (i
+ 3)
1530 + pb
->geqs
[e
].coef
[i
];
1534 g2
= abs (hashCode
);
1536 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1538 fprintf (dump_file
, "Hash code = %d, eqn = ", hashCode
);
1539 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1540 fprintf (dump_file
, "\n");
1543 j
= g2
% HASH_TABLE_SIZE
;
1546 eqn proto
= &(hash_master
[j
]);
1548 if (proto
->touched
== g2
)
1550 if (proto
->coef
[0] == top_var
)
1553 for (i0
= top_var
; i0
>= 0; i0
--)
1557 if (pb
->geqs
[e
].coef
[i
] != proto
->coef
[i
])
1561 for (i0
= top_var
; i0
>= 0; i0
--)
1565 if (pb
->geqs
[e
].coef
[i
] != -proto
->coef
[i
])
1572 pb
->geqs
[e
].key
= proto
->key
;
1574 pb
->geqs
[e
].key
= -proto
->key
;
1579 else if (proto
->touched
< 0)
1581 omega_init_eqn_zero (proto
, pb
->num_vars
);
1583 for (i0
= top_var
; i0
>= 0; i0
--)
1586 proto
->coef
[i
] = pb
->geqs
[e
].coef
[i
];
1589 for (i0
= top_var
; i0
>= 0; i0
--)
1592 proto
->coef
[i
] = -pb
->geqs
[e
].coef
[i
];
1595 proto
->coef
[0] = top_var
;
1596 proto
->touched
= g2
;
1598 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1599 fprintf (dump_file
, " constraint key = %d\n",
1602 proto
->key
= next_key
++;
1604 /* Too many hash keys generated. */
1605 gcc_assert (proto
->key
<= MAX_KEYS
);
1608 pb
->geqs
[e
].key
= proto
->key
;
1610 pb
->geqs
[e
].key
= -proto
->key
;
1615 j
= (j
+ 1) % HASH_TABLE_SIZE
;
1619 pb
->geqs
[e
].touched
= 0;
1623 int eKey
= pb
->geqs
[e
].key
;
1627 int cTerm
= pb
->geqs
[e
].coef
[0];
1628 e2
= fast_lookup
[MAX_KEYS
- eKey
];
1630 if (e2
< e
&& pb
->geqs
[e2
].key
== -eKey
1631 && pb
->geqs
[e2
].color
== omega_black
)
1633 if (pb
->geqs
[e2
].coef
[0] < -cTerm
)
1635 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1637 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1638 fprintf (dump_file
, "\n");
1639 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e2
]);
1641 "\nequations have no solution \n");
1643 return normalize_false
;
1646 if (pb
->geqs
[e2
].coef
[0] == -cTerm
1648 || pb
->geqs
[e
].color
== omega_black
))
1650 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
], &pb
->geqs
[e
],
1652 if (pb
->geqs
[e
].color
== omega_black
)
1653 adding_equality_constraint (pb
, pb
->num_eqs
);
1655 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
1659 e2
= fast_lookup_red
[MAX_KEYS
- eKey
];
1661 if (e2
< e
&& pb
->geqs
[e2
].key
== -eKey
1662 && pb
->geqs
[e2
].color
== omega_red
)
1664 if (pb
->geqs
[e2
].coef
[0] < -cTerm
)
1666 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1668 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1669 fprintf (dump_file
, "\n");
1670 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e2
]);
1672 "\nequations have no solution \n");
1674 return normalize_false
;
1677 if (pb
->geqs
[e2
].coef
[0] == -cTerm
&& create_color
)
1679 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
], &pb
->geqs
[e
],
1681 pb
->eqs
[pb
->num_eqs
].color
= omega_red
;
1683 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
1687 e2
= fast_lookup
[MAX_KEYS
+ eKey
];
1689 if (e2
< e
&& pb
->geqs
[e2
].key
== eKey
1690 && pb
->geqs
[e2
].color
== omega_black
)
1692 if (pb
->geqs
[e2
].coef
[0] > cTerm
)
1694 if (pb
->geqs
[e
].color
== omega_black
)
1696 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1699 "Removing Redudant Equation: ");
1700 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1701 fprintf (dump_file
, "\n");
1703 "[a] Made Redundant by: ");
1704 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1705 fprintf (dump_file
, "\n");
1707 pb
->geqs
[e2
].coef
[0] = cTerm
;
1708 omega_delete_geq (pb
, e
, n_vars
);
1715 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1717 fprintf (dump_file
, "Removing Redudant Equation: ");
1718 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1719 fprintf (dump_file
, "\n");
1720 fprintf (dump_file
, "[b] Made Redundant by: ");
1721 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1722 fprintf (dump_file
, "\n");
1724 omega_delete_geq (pb
, e
, n_vars
);
1730 e2
= fast_lookup_red
[MAX_KEYS
+ eKey
];
1732 if (e2
< e
&& pb
->geqs
[e2
].key
== eKey
1733 && pb
->geqs
[e2
].color
== omega_red
)
1735 if (pb
->geqs
[e2
].coef
[0] >= cTerm
)
1737 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1739 fprintf (dump_file
, "Removing Redudant Equation: ");
1740 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1741 fprintf (dump_file
, "\n");
1742 fprintf (dump_file
, "[c] Made Redundant by: ");
1743 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1744 fprintf (dump_file
, "\n");
1746 pb
->geqs
[e2
].coef
[0] = cTerm
;
1747 pb
->geqs
[e2
].color
= pb
->geqs
[e
].color
;
1749 else if (pb
->geqs
[e
].color
== omega_red
)
1751 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1753 fprintf (dump_file
, "Removing Redudant Equation: ");
1754 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1755 fprintf (dump_file
, "\n");
1756 fprintf (dump_file
, "[d] Made Redundant by: ");
1757 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1758 fprintf (dump_file
, "\n");
1761 omega_delete_geq (pb
, e
, n_vars
);
1768 if (pb
->geqs
[e
].color
== omega_red
)
1769 fast_lookup_red
[MAX_KEYS
+ eKey
] = e
;
1771 fast_lookup
[MAX_KEYS
+ eKey
] = e
;
1775 create_color
= false;
1776 return coupled_subscripts
? normalize_coupled
: normalize_uncoupled
;
1779 /* Divide the coefficients of EQN by their gcd. N_VARS is the number
1780 of variables in EQN. */
1783 divide_eqn_by_gcd (eqn eqn
, int n_vars
)
1787 for (var
= n_vars
; var
>= 0; var
--)
1788 g
= gcd (abs (eqn
->coef
[var
]), g
);
1791 for (var
= n_vars
; var
>= 0; var
--)
1792 eqn
->coef
[var
] = eqn
->coef
[var
] / g
;
1795 /* Rewrite some non-safe variables in function of protected
1796 wildcard variables. */
1799 cleanout_wildcards (omega_pb pb
)
1802 int n_vars
= pb
->num_vars
;
1803 bool renormalize
= false;
1805 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1806 for (i
= n_vars
; !omega_safe_var_p (pb
, i
); i
--)
1807 if (pb
->eqs
[e
].coef
[i
] != 0)
1809 /* i is the last non-zero non-safe variable. */
1811 for (j
= i
- 1; !omega_safe_var_p (pb
, j
); j
--)
1812 if (pb
->eqs
[e
].coef
[j
] != 0)
1815 /* j is the next non-zero non-safe variable, or points
1816 to a safe variable: it is then a wildcard variable. */
1819 if (omega_safe_var_p (pb
, j
))
1821 eqn sub
= &(pb
->eqs
[e
]);
1822 int c
= pb
->eqs
[e
].coef
[i
];
1826 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1829 "Found a single wild card equality: ");
1830 omega_print_eq (dump_file
, pb
, &pb
->eqs
[e
]);
1831 fprintf (dump_file
, "\n");
1832 omega_print_problem (dump_file
, pb
);
1835 for (e2
= pb
->num_eqs
- 1; e2
>= 0; e2
--)
1836 if (e
!= e2
&& pb
->eqs
[e2
].coef
[i
]
1837 && (pb
->eqs
[e2
].color
== omega_red
1838 || (pb
->eqs
[e2
].color
== omega_black
1839 && pb
->eqs
[e
].color
== omega_black
)))
1841 eqn eqn
= &(pb
->eqs
[e2
]);
1844 for (var
= n_vars
; var
>= 0; var
--)
1845 eqn
->coef
[var
] *= a
;
1849 for (var
= n_vars
; var
>= 0; var
--)
1850 eqn
->coef
[var
] -= sub
->coef
[var
] * k
/ c
;
1853 divide_eqn_by_gcd (eqn
, n_vars
);
1856 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
1857 if (pb
->geqs
[e2
].coef
[i
]
1858 && (pb
->geqs
[e2
].color
== omega_red
1859 || (pb
->eqs
[e
].color
== omega_black
1860 && pb
->geqs
[e2
].color
== omega_black
)))
1862 eqn eqn
= &(pb
->geqs
[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
;
1878 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
1879 if (pb
->subs
[e2
].coef
[i
]
1880 && (pb
->subs
[e2
].color
== omega_red
1881 || (pb
->subs
[e2
].color
== omega_black
1882 && pb
->eqs
[e
].color
== omega_black
)))
1884 eqn eqn
= &(pb
->subs
[e2
]);
1887 for (var
= n_vars
; var
>= 0; var
--)
1888 eqn
->coef
[var
] *= a
;
1892 for (var
= n_vars
; var
>= 0; var
--)
1893 eqn
->coef
[var
] -= sub
->coef
[var
] * k
/ c
;
1896 divide_eqn_by_gcd (eqn
, n_vars
);
1899 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1901 fprintf (dump_file
, "cleaned-out wildcard: ");
1902 omega_print_problem (dump_file
, pb
);
1909 normalize_omega_problem (pb
);
1912 /* Swap values contained in I and J. */
1915 swap (int *i
, int *j
)
1923 /* Swap values contained in I and J. */
1926 bswap (bool *i
, bool *j
)
1934 /* Make variable IDX unprotected in PB, by swapping its index at the
1935 PB->safe_vars rank. */
1938 omega_unprotect_1 (omega_pb pb
, int *idx
, bool *unprotect
)
1940 /* If IDX is protected... */
1941 if (*idx
< pb
->safe_vars
)
1943 /* ... swap its index with the last non protected index. */
1944 int j
= pb
->safe_vars
;
1947 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1949 pb
->geqs
[e
].touched
= 1;
1950 swap (&pb
->geqs
[e
].coef
[*idx
], &pb
->geqs
[e
].coef
[j
]);
1953 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1954 swap (&pb
->eqs
[e
].coef
[*idx
], &pb
->eqs
[e
].coef
[j
]);
1956 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1957 swap (&pb
->subs
[e
].coef
[*idx
], &pb
->subs
[e
].coef
[j
]);
1960 bswap (&unprotect
[*idx
], &unprotect
[j
]);
1962 swap (&pb
->var
[*idx
], &pb
->var
[j
]);
1963 pb
->forwarding_address
[pb
->var
[*idx
]] = *idx
;
1964 pb
->forwarding_address
[pb
->var
[j
]] = j
;
1968 /* The variable at pb->safe_vars is also unprotected now. */
1972 /* During the Fourier-Motzkin elimination some variables are
1973 substituted with other variables. This function resurrects the
1974 substituted variables in PB. */
1977 resurrect_subs (omega_pb pb
)
1979 if (pb
->num_subs
> 0
1980 && please_no_equalities_in_simplified_problems
== 0)
1984 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1987 "problem reduced, bringing variables back to life\n");
1988 omega_print_problem (dump_file
, pb
);
1991 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
1992 if (omega_wildcard_p (pb
, i
))
1993 omega_unprotect_1 (pb
, &i
, NULL
);
1996 n
= MAX (pb
->num_vars
, pb
->safe_vars
+ m
);
1998 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1999 if (single_var_geq (&pb
->geqs
[e
], pb
->num_vars
))
2001 if (!omega_safe_var_p (pb
, abs (pb
->geqs
[e
].key
)))
2002 pb
->geqs
[e
].key
+= (pb
->geqs
[e
].key
> 0 ? m
: -m
);
2006 pb
->geqs
[e
].touched
= 1;
2007 pb
->geqs
[e
].key
= 0;
2010 for (i
= pb
->num_vars
; !omega_safe_var_p (pb
, i
); i
--)
2012 pb
->var
[i
+ m
] = pb
->var
[i
];
2014 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2015 pb
->geqs
[e
].coef
[i
+ m
] = pb
->geqs
[e
].coef
[i
];
2017 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
2018 pb
->eqs
[e
].coef
[i
+ m
] = pb
->eqs
[e
].coef
[i
];
2020 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2021 pb
->subs
[e
].coef
[i
+ m
] = pb
->subs
[e
].coef
[i
];
2024 for (i
= pb
->safe_vars
+ m
; !omega_safe_var_p (pb
, i
); i
--)
2026 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2027 pb
->geqs
[e
].coef
[i
] = 0;
2029 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
2030 pb
->eqs
[e
].coef
[i
] = 0;
2032 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2033 pb
->subs
[e
].coef
[i
] = 0;
2038 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2040 pb
->var
[pb
->safe_vars
+ 1 + e
] = pb
->subs
[e
].key
;
2041 omega_copy_eqn (&(pb
->eqs
[pb
->num_eqs
]), &(pb
->subs
[e
]),
2043 pb
->eqs
[pb
->num_eqs
].coef
[pb
->safe_vars
+ 1 + e
] = -1;
2044 pb
->eqs
[pb
->num_eqs
].color
= omega_black
;
2046 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2048 fprintf (dump_file
, "brought back: ");
2049 omega_print_eq (dump_file
, pb
, &pb
->eqs
[pb
->num_eqs
]);
2050 fprintf (dump_file
, "\n");
2054 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2060 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2062 fprintf (dump_file
, "variables brought back to life\n");
2063 omega_print_problem (dump_file
, pb
);
2066 cleanout_wildcards (pb
);
2071 implies (unsigned int a
, unsigned int b
)
2073 return (a
== (a
& b
));
2076 /* Eliminate redundant equations in PB. When EXPENSIVE is true, an
2077 extra step is performed. Returns omega_false when there exist no
2078 solution, omega_true otherwise. */
2081 omega_eliminate_redundant (omega_pb pb
, bool expensive
)
2083 int c
, e
, e1
, e2
, e3
, p
, q
, i
, k
, alpha
, alpha1
, alpha2
, alpha3
;
2084 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2085 omega_pb tmp_problem
;
2087 /* {P,Z,N}EQS = {Positive,Zero,Negative} Equations. */
2088 unsigned int *peqs
= XNEWVEC (unsigned int, OMEGA_MAX_GEQS
);
2089 unsigned int *zeqs
= XNEWVEC (unsigned int, OMEGA_MAX_GEQS
);
2090 unsigned int *neqs
= XNEWVEC (unsigned int, OMEGA_MAX_GEQS
);
2092 /* PP = Possible Positives, PZ = Possible Zeros, PN = Possible Negatives */
2093 unsigned int pp
, pz
, pn
;
2095 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2097 fprintf (dump_file
, "in eliminate Redudant:\n");
2098 omega_print_problem (dump_file
, pb
);
2101 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2106 peqs
[e
] = zeqs
[e
] = neqs
[e
] = 0;
2108 for (i
= pb
->num_vars
; i
>= 1; i
--)
2110 if (pb
->geqs
[e
].coef
[i
] > 0)
2112 else if (pb
->geqs
[e
].coef
[i
] < 0)
2122 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
2124 for (e2
= e1
- 1; e2
>= 0; e2
--)
2127 for (p
= pb
->num_vars
; p
> 1; p
--)
2128 for (q
= p
- 1; q
> 0; q
--)
2129 if ((alpha
= pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e2
].coef
[q
]
2130 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e1
].coef
[q
]) != 0)
2136 pz
= ((zeqs
[e1
] & zeqs
[e2
]) | (peqs
[e1
] & neqs
[e2
])
2137 | (neqs
[e1
] & peqs
[e2
]));
2138 pp
= peqs
[e1
] | peqs
[e2
];
2139 pn
= neqs
[e1
] | neqs
[e2
];
2141 for (e3
= pb
->num_geqs
- 1; e3
>= 0; e3
--)
2142 if (e3
!= e1
&& e3
!= e2
)
2144 if (!implies (zeqs
[e3
], pz
))
2147 alpha1
= (pb
->geqs
[e2
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2148 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2149 alpha2
= -(pb
->geqs
[e1
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2150 - pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2153 if (alpha1
* alpha2
<= 0)
2165 /* Trying to prove e3 is redundant. */
2166 if (!implies (peqs
[e3
], pp
)
2167 || !implies (neqs
[e3
], pn
))
2170 if (pb
->geqs
[e3
].color
== omega_black
2171 && (pb
->geqs
[e1
].color
== omega_red
2172 || pb
->geqs
[e2
].color
== omega_red
))
2175 for (k
= pb
->num_vars
; k
>= 1; k
--)
2176 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2177 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2178 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2181 c
= (alpha1
* pb
->geqs
[e1
].coef
[0]
2182 + alpha2
* pb
->geqs
[e2
].coef
[0]);
2184 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] + 1))
2186 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2189 "found redundant inequality\n");
2191 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2192 alpha1
, alpha2
, alpha3
);
2194 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2195 fprintf (dump_file
, "\n");
2196 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2197 fprintf (dump_file
, "\n=> ");
2198 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2199 fprintf (dump_file
, "\n\n");
2207 /* Trying to prove e3 <= 0 and therefore e3 = 0,
2208 or trying to prove e3 < 0, and therefore the
2209 problem has no solutions. */
2210 if (!implies (peqs
[e3
], pn
)
2211 || !implies (neqs
[e3
], pp
))
2214 if (pb
->geqs
[e1
].color
== omega_red
2215 || pb
->geqs
[e2
].color
== omega_red
2216 || pb
->geqs
[e3
].color
== omega_red
)
2220 /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */
2221 for (k
= pb
->num_vars
; k
>= 1; k
--)
2222 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2223 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2224 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2227 c
= (alpha1
* pb
->geqs
[e1
].coef
[0]
2228 + alpha2
* pb
->geqs
[e2
].coef
[0]);
2230 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0]))
2232 /* We just proved e3 < 0, so no solutions exist. */
2233 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2236 "found implied over tight inequality\n");
2238 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2239 alpha1
, alpha2
, -alpha3
);
2240 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2241 fprintf (dump_file
, "\n");
2242 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2243 fprintf (dump_file
, "\n=> not ");
2244 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2245 fprintf (dump_file
, "\n\n");
2253 else if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] - 1))
2255 /* We just proved that e3 <=0, so e3 = 0. */
2256 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2259 "found implied tight inequality\n");
2261 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2262 alpha1
, alpha2
, -alpha3
);
2263 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2264 fprintf (dump_file
, "\n");
2265 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2266 fprintf (dump_file
, "\n=> inverse ");
2267 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2268 fprintf (dump_file
, "\n\n");
2271 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
++],
2272 &pb
->geqs
[e3
], pb
->num_vars
);
2273 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2274 adding_equality_constraint (pb
, pb
->num_eqs
- 1);
2282 /* Delete the inequalities that were marked as dead. */
2283 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2285 omega_delete_geq (pb
, e
, pb
->num_vars
);
2288 goto eliminate_redundant_done
;
2290 tmp_problem
= XNEW (struct omega_pb
);
2293 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2295 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2298 "checking equation %d to see if it is redundant: ", e
);
2299 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2300 fprintf (dump_file
, "\n");
2303 omega_copy_problem (tmp_problem
, pb
);
2304 omega_negate_geq (tmp_problem
, e
);
2305 tmp_problem
->safe_vars
= 0;
2306 tmp_problem
->variables_freed
= false;
2308 if (omega_solve_problem (tmp_problem
, omega_false
) == omega_false
)
2309 omega_delete_geq (pb
, e
, pb
->num_vars
);
2315 if (!omega_reduce_with_subs
)
2317 resurrect_subs (pb
);
2318 gcc_assert (please_no_equalities_in_simplified_problems
2319 || pb
->num_subs
== 0);
2322 eliminate_redundant_done
:
2330 /* For each inequality that has coefficients bigger than 20, try to
2331 create a new constraint that cannot be derived from the original
2332 constraint and that has smaller coefficients. Add the new
2333 constraint at the end of geqs. Return the number of inequalities
2334 that have been added to PB. */
2337 smooth_weird_equations (omega_pb pb
)
2339 int e1
, e2
, e3
, p
, q
, k
, alpha
, alpha1
, alpha2
, alpha3
;
2344 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
2345 if (pb
->geqs
[e1
].color
== omega_black
)
2349 for (v
= pb
->num_vars
; v
>= 1; v
--)
2350 if (pb
->geqs
[e1
].coef
[v
] != 0 && abs (pb
->geqs
[e1
].coef
[v
]) < g
)
2351 g
= abs (pb
->geqs
[e1
].coef
[v
]);
2358 for (v
= pb
->num_vars
; v
>= 1; v
--)
2359 pb
->geqs
[e3
].coef
[v
] = int_div (6 * pb
->geqs
[e1
].coef
[v
] + g
/ 2,
2362 pb
->geqs
[e3
].color
= omega_black
;
2363 pb
->geqs
[e3
].touched
= 1;
2365 pb
->geqs
[e3
].coef
[0] = 9997;
2367 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2369 fprintf (dump_file
, "Checking to see if we can derive: ");
2370 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e3
]);
2371 fprintf (dump_file
, "\n from: ");
2372 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e1
]);
2373 fprintf (dump_file
, "\n");
2376 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
2377 if (e1
!= e2
&& pb
->geqs
[e2
].color
== omega_black
)
2379 for (p
= pb
->num_vars
; p
> 1; p
--)
2381 for (q
= p
- 1; q
> 0; q
--)
2384 (pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e2
].coef
[q
] -
2385 pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e1
].coef
[q
]);
2394 alpha1
= (pb
->geqs
[e2
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2395 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2396 alpha2
= -(pb
->geqs
[e1
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2397 - pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2400 if (alpha1
* alpha2
<= 0)
2412 /* Try to prove e3 is redundant: verify
2413 alpha1*v1 + alpha2*v2 = alpha3*v3. */
2414 for (k
= pb
->num_vars
; k
>= 1; k
--)
2415 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2416 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2417 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2420 c
= alpha1
* pb
->geqs
[e1
].coef
[0]
2421 + alpha2
* pb
->geqs
[e2
].coef
[0];
2423 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] + 1))
2424 pb
->geqs
[e3
].coef
[0] = int_div (c
, alpha3
);
2429 if (pb
->geqs
[e3
].coef
[0] < 9997)
2434 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2437 "Smoothing wierd equations; adding:\n");
2438 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e3
]);
2439 fprintf (dump_file
, "\nto:\n");
2440 omega_print_problem (dump_file
, pb
);
2441 fprintf (dump_file
, "\n\n");
2449 /* Replace tuples of inequalities, that define upper and lower half
2450 spaces, with an equation. */
2453 coalesce (omega_pb pb
)
2457 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2458 int found_something
= 0;
2460 for (e
= 0; e
< pb
->num_geqs
; e
++)
2461 if (pb
->geqs
[e
].color
== omega_red
)
2467 for (e
= 0; e
< pb
->num_geqs
; e
++)
2470 for (e
= 0; e
< pb
->num_geqs
; e
++)
2471 if (pb
->geqs
[e
].color
== omega_red
2472 && !pb
->geqs
[e
].touched
)
2473 for (e2
= e
+ 1; e2
< pb
->num_geqs
; e2
++)
2474 if (!pb
->geqs
[e2
].touched
2475 && pb
->geqs
[e
].key
== -pb
->geqs
[e2
].key
2476 && pb
->geqs
[e
].coef
[0] == -pb
->geqs
[e2
].coef
[0]
2477 && pb
->geqs
[e2
].color
== omega_red
)
2479 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
++], &pb
->geqs
[e
],
2481 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2487 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2489 omega_delete_geq (pb
, e
, pb
->num_vars
);
2491 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && found_something
)
2493 fprintf (dump_file
, "Coalesced pb->geqs into %d EQ's:\n",
2495 omega_print_problem (dump_file
, pb
);
2501 /* Eliminate red inequalities from PB. When ELIMINATE_ALL is
2502 true, continue to eliminate all the red inequalities. */
2505 omega_eliminate_red (omega_pb pb
, bool eliminate_all
)
2507 int e
, e2
, e3
, i
, j
, k
, a
, alpha1
, alpha2
;
2509 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2512 omega_pb tmp_problem
;
2514 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2516 fprintf (dump_file
, "in eliminate RED:\n");
2517 omega_print_problem (dump_file
, pb
);
2520 if (pb
->num_eqs
> 0)
2521 omega_simplify_problem (pb
);
2523 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2526 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2527 if (pb
->geqs
[e
].color
== omega_black
&& !is_dead
[e
])
2528 for (e2
= e
- 1; e2
>= 0; e2
--)
2529 if (pb
->geqs
[e2
].color
== omega_black
2534 for (i
= pb
->num_vars
; i
> 1; i
--)
2535 for (j
= i
- 1; j
> 0; j
--)
2536 if ((a
= (pb
->geqs
[e
].coef
[i
] * pb
->geqs
[e2
].coef
[j
]
2537 - pb
->geqs
[e2
].coef
[i
] * pb
->geqs
[e
].coef
[j
])) != 0)
2543 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2546 "found two equations to combine, i = %s, ",
2547 omega_variable_to_str (pb
, i
));
2548 fprintf (dump_file
, "j = %s, alpha = %d\n",
2549 omega_variable_to_str (pb
, j
), a
);
2550 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2551 fprintf (dump_file
, "\n");
2552 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2553 fprintf (dump_file
, "\n");
2556 for (e3
= pb
->num_geqs
- 1; e3
>= 0; e3
--)
2557 if (pb
->geqs
[e3
].color
== omega_red
)
2559 alpha1
= (pb
->geqs
[e2
].coef
[j
] * pb
->geqs
[e3
].coef
[i
]
2560 - pb
->geqs
[e2
].coef
[i
] * pb
->geqs
[e3
].coef
[j
]);
2561 alpha2
= -(pb
->geqs
[e
].coef
[j
] * pb
->geqs
[e3
].coef
[i
]
2562 - pb
->geqs
[e
].coef
[i
] * pb
->geqs
[e3
].coef
[j
]);
2564 if ((a
> 0 && alpha1
> 0 && alpha2
> 0)
2565 || (a
< 0 && alpha1
< 0 && alpha2
< 0))
2567 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2570 "alpha1 = %d, alpha2 = %d;"
2571 "comparing against: ",
2573 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2574 fprintf (dump_file
, "\n");
2577 for (k
= pb
->num_vars
; k
>= 0; k
--)
2579 c
= (alpha1
* pb
->geqs
[e
].coef
[k
]
2580 + alpha2
* pb
->geqs
[e2
].coef
[k
]);
2582 if (c
!= a
* pb
->geqs
[e3
].coef
[k
])
2585 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && k
> 0)
2586 fprintf (dump_file
, " %s: %d, %d\n",
2587 omega_variable_to_str (pb
, k
), c
,
2588 a
* pb
->geqs
[e3
].coef
[k
]);
2593 ((a
> 0 && c
< a
* pb
->geqs
[e3
].coef
[k
])
2594 || (a
< 0 && c
> a
* pb
->geqs
[e3
].coef
[k
]))))
2596 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2600 "red equation#%d is dead "
2601 "(%d dead so far, %d remain)\n",
2603 pb
->num_geqs
- dead_count
);
2604 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2605 fprintf (dump_file
, "\n");
2606 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2607 fprintf (dump_file
, "\n");
2608 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2609 fprintf (dump_file
, "\n");
2617 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2619 omega_delete_geq (pb
, e
, pb
->num_vars
);
2623 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2625 fprintf (dump_file
, "in eliminate RED, easy tests done:\n");
2626 omega_print_problem (dump_file
, pb
);
2629 for (red_found
= 0, e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2630 if (pb
->geqs
[e
].color
== omega_red
)
2635 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2636 fprintf (dump_file
, "fast checks worked\n");
2638 if (!omega_reduce_with_subs
)
2639 gcc_assert (please_no_equalities_in_simplified_problems
2640 || pb
->num_subs
== 0);
2645 if (!omega_verify_simplification
2646 && verify_omega_pb (pb
) == omega_false
)
2650 tmp_problem
= XNEW (struct omega_pb
);
2652 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2653 if (pb
->geqs
[e
].color
== omega_red
)
2655 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2658 "checking equation %d to see if it is redundant: ", e
);
2659 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2660 fprintf (dump_file
, "\n");
2663 omega_copy_problem (tmp_problem
, pb
);
2664 omega_negate_geq (tmp_problem
, e
);
2665 tmp_problem
->safe_vars
= 0;
2666 tmp_problem
->variables_freed
= false;
2667 tmp_problem
->num_subs
= 0;
2669 if (omega_solve_problem (tmp_problem
, omega_false
) == omega_false
)
2671 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2672 fprintf (dump_file
, "it is redundant\n");
2673 omega_delete_geq (pb
, e
, pb
->num_vars
);
2677 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2678 fprintf (dump_file
, "it is not redundant\n");
2682 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2683 fprintf (dump_file
, "no need to check other red equations\n");
2691 /* omega_simplify_problem (pb); */
2693 if (!omega_reduce_with_subs
)
2694 gcc_assert (please_no_equalities_in_simplified_problems
2695 || pb
->num_subs
== 0);
2698 /* Transform some wildcard variables to non-safe variables. */
2701 chain_unprotect (omega_pb pb
)
2704 bool *unprotect
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2706 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2708 unprotect
[i
] = omega_wildcard_p (pb
, i
);
2710 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2711 if (pb
->subs
[e
].coef
[i
])
2712 unprotect
[i
] = false;
2715 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2717 fprintf (dump_file
, "Doing chain reaction unprotection\n");
2718 omega_print_problem (dump_file
, pb
);
2720 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2722 fprintf (dump_file
, "unprotecting %s\n",
2723 omega_variable_to_str (pb
, i
));
2726 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2728 omega_unprotect_1 (pb
, &i
, unprotect
);
2730 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2732 fprintf (dump_file
, "After chain reactions\n");
2733 omega_print_problem (dump_file
, pb
);
2739 /* Reduce problem PB. */
2742 omega_problem_reduced (omega_pb pb
)
2744 if (omega_verify_simplification
2745 && !in_approximate_mode
2746 && verify_omega_pb (pb
) == omega_false
)
2749 if (PARAM_VALUE (PARAM_OMEGA_ELIMINATE_REDUNDANT_CONSTRAINTS
)
2750 && !omega_eliminate_redundant (pb
, true))
2753 omega_found_reduction
= omega_true
;
2755 if (!please_no_equalities_in_simplified_problems
)
2758 if (omega_reduce_with_subs
2759 || please_no_equalities_in_simplified_problems
)
2760 chain_unprotect (pb
);
2762 resurrect_subs (pb
);
2764 if (!return_single_result
)
2768 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2769 pb
->forwarding_address
[pb
->var
[i
]] = i
;
2771 for (i
= 0; i
< pb
->num_subs
; i
++)
2772 pb
->forwarding_address
[pb
->subs
[i
].key
] = -i
- 1;
2774 (*omega_when_reduced
) (pb
);
2777 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2779 fprintf (dump_file
, "-------------------------------------------\n");
2780 fprintf (dump_file
, "problem reduced:\n");
2781 omega_print_problem (dump_file
, pb
);
2782 fprintf (dump_file
, "-------------------------------------------\n");
2786 /* Eliminates all the free variables for problem PB, that is all the
2787 variables from FV to PB->NUM_VARS. */
2790 omega_free_eliminations (omega_pb pb
, int fv
)
2792 bool try_again
= true;
2794 int n_vars
= pb
->num_vars
;
2800 for (i
= n_vars
; i
> fv
; i
--)
2802 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2803 if (pb
->geqs
[e
].coef
[i
])
2808 else if (pb
->geqs
[e
].coef
[i
] > 0)
2810 for (e2
= e
- 1; e2
>= 0; e2
--)
2811 if (pb
->geqs
[e2
].coef
[i
] < 0)
2816 for (e2
= e
- 1; e2
>= 0; e2
--)
2817 if (pb
->geqs
[e2
].coef
[i
] > 0)
2824 for (e3
= pb
->num_subs
- 1; e3
>= 0; e3
--)
2825 if (pb
->subs
[e3
].coef
[i
])
2831 for (e3
= pb
->num_eqs
- 1; e3
>= 0; e3
--)
2832 if (pb
->eqs
[e3
].coef
[i
])
2838 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2839 fprintf (dump_file
, "a free elimination of %s\n",
2840 omega_variable_to_str (pb
, i
));
2844 omega_delete_geq (pb
, e
, n_vars
);
2846 for (e
--; e
>= 0; e
--)
2847 if (pb
->geqs
[e
].coef
[i
])
2848 omega_delete_geq (pb
, e
, n_vars
);
2850 try_again
= (i
< n_vars
);
2853 omega_delete_variable (pb
, i
);
2854 n_vars
= pb
->num_vars
;
2859 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2861 fprintf (dump_file
, "\nafter free eliminations:\n");
2862 omega_print_problem (dump_file
, pb
);
2863 fprintf (dump_file
, "\n");
2867 /* Do free red eliminations. */
2870 free_red_eliminations (omega_pb pb
)
2872 bool try_again
= true;
2874 int n_vars
= pb
->num_vars
;
2875 bool *is_red_var
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2876 bool *is_dead_var
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2877 bool *is_dead_geq
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2879 for (i
= n_vars
; i
> 0; i
--)
2881 is_red_var
[i
] = false;
2882 is_dead_var
[i
] = false;
2885 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2887 is_dead_geq
[e
] = false;
2889 if (pb
->geqs
[e
].color
== omega_red
)
2890 for (i
= n_vars
; i
> 0; i
--)
2891 if (pb
->geqs
[e
].coef
[i
] != 0)
2892 is_red_var
[i
] = true;
2898 for (i
= n_vars
; i
> 0; i
--)
2899 if (!is_red_var
[i
] && !is_dead_var
[i
])
2901 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2902 if (!is_dead_geq
[e
] && pb
->geqs
[e
].coef
[i
])
2907 else if (pb
->geqs
[e
].coef
[i
] > 0)
2909 for (e2
= e
- 1; e2
>= 0; e2
--)
2910 if (!is_dead_geq
[e2
] && pb
->geqs
[e2
].coef
[i
] < 0)
2915 for (e2
= e
- 1; e2
>= 0; e2
--)
2916 if (!is_dead_geq
[e2
] && pb
->geqs
[e2
].coef
[i
] > 0)
2923 for (e3
= pb
->num_subs
- 1; e3
>= 0; e3
--)
2924 if (pb
->subs
[e3
].coef
[i
])
2930 for (e3
= pb
->num_eqs
- 1; e3
>= 0; e3
--)
2931 if (pb
->eqs
[e3
].coef
[i
])
2937 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2938 fprintf (dump_file
, "a free red elimination of %s\n",
2939 omega_variable_to_str (pb
, i
));
2942 if (pb
->geqs
[e
].coef
[i
])
2943 is_dead_geq
[e
] = true;
2946 is_dead_var
[i
] = true;
2951 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2953 omega_delete_geq (pb
, e
, n_vars
);
2955 for (i
= n_vars
; i
> 0; i
--)
2957 omega_delete_variable (pb
, i
);
2959 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2961 fprintf (dump_file
, "\nafter free red eliminations:\n");
2962 omega_print_problem (dump_file
, pb
);
2963 fprintf (dump_file
, "\n");
2971 /* For equation EQ of the form "0 = EQN", insert in PB two
2972 inequalities "0 <= EQN" and "0 <= -EQN". */
2975 omega_convert_eq_to_geqs (omega_pb pb
, int eq
)
2979 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2980 fprintf (dump_file
, "Converting Eq to Geqs\n");
2982 /* Insert "0 <= EQN". */
2983 omega_copy_eqn (&pb
->geqs
[pb
->num_geqs
], &pb
->eqs
[eq
], pb
->num_vars
);
2984 pb
->geqs
[pb
->num_geqs
].touched
= 1;
2987 /* Insert "0 <= -EQN". */
2988 omega_copy_eqn (&pb
->geqs
[pb
->num_geqs
], &pb
->eqs
[eq
], pb
->num_vars
);
2989 pb
->geqs
[pb
->num_geqs
].touched
= 1;
2991 for (i
= 0; i
<= pb
->num_vars
; i
++)
2992 pb
->geqs
[pb
->num_geqs
].coef
[i
] *= -1;
2996 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2997 omega_print_problem (dump_file
, pb
);
3000 /* Eliminates variable I from PB. */
3003 omega_do_elimination (omega_pb pb
, int e
, int i
)
3005 eqn sub
= omega_alloc_eqns (0, 1);
3007 int n_vars
= pb
->num_vars
;
3009 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3010 fprintf (dump_file
, "eliminating variable %s\n",
3011 omega_variable_to_str (pb
, i
));
3013 omega_copy_eqn (sub
, &pb
->eqs
[e
], pb
->num_vars
);
3016 if (c
== 1 || c
== -1)
3018 if (pb
->eqs
[e
].color
== omega_red
)
3021 omega_substitute_red (pb
, sub
, i
, c
, &fB
);
3023 omega_convert_eq_to_geqs (pb
, e
);
3025 omega_delete_variable (pb
, i
);
3029 omega_substitute (pb
, sub
, i
, c
);
3030 omega_delete_variable (pb
, i
);
3038 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3039 fprintf (dump_file
, "performing non-exact elimination, c = %d\n", c
);
3041 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
3042 if (pb
->eqs
[e
].coef
[i
])
3044 eqn eqn
= &(pb
->eqs
[e
]);
3046 for (j
= n_vars
; j
>= 0; j
--)
3050 eqn
->color
|= sub
->color
;
3051 for (j
= n_vars
; j
>= 0; j
--)
3052 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3055 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3056 if (pb
->geqs
[e
].coef
[i
])
3058 eqn eqn
= &(pb
->geqs
[e
]);
3061 if (sub
->color
== omega_red
)
3062 eqn
->color
= omega_red
;
3064 for (j
= n_vars
; j
>= 0; j
--)
3071 for (j
= n_vars
; j
>= 0; j
--)
3072 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3076 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
3077 if (pb
->subs
[e
].coef
[i
])
3079 eqn eqn
= &(pb
->subs
[e
]);
3082 gcc_assert (sub
->color
== omega_black
);
3083 for (j
= n_vars
; j
>= 0; j
--)
3087 for (j
= n_vars
; j
>= 0; j
--)
3088 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3091 if (in_approximate_mode
)
3092 omega_delete_variable (pb
, i
);
3094 omega_convert_eq_to_geqs (pb
, e2
);
3097 omega_free_eqns (sub
, 1);
3100 /* Helper function for printing "sorry, no solution". */
3102 static inline enum omega_result
3103 omega_problem_has_no_solution (void)
3105 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3106 fprintf (dump_file
, "\nequations have no solution \n");
3111 /* Helper function: solve equations in PB one at a time, following the
3112 DESIRED_RES result. */
3114 static enum omega_result
3115 omega_solve_eq (omega_pb pb
, enum omega_result desired_res
)
3122 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && pb
->num_eqs
> 0)
3124 fprintf (dump_file
, "\nomega_solve_eq (%d, %d)\n",
3125 desired_res
, may_be_red
);
3126 omega_print_problem (dump_file
, pb
);
3127 fprintf (dump_file
, "\n");
3133 j
= pb
->num_eqs
- 1;
3139 while (i
<= j
&& pb
->eqs
[i
].color
== omega_red
)
3142 while (i
<= j
&& pb
->eqs
[j
].color
== omega_black
)
3148 eq
= omega_alloc_eqns (0, 1);
3149 omega_copy_eqn (eq
, &pb
->eqs
[i
], pb
->num_vars
);
3150 omega_copy_eqn (&pb
->eqs
[i
], &pb
->eqs
[j
], pb
->num_vars
);
3151 omega_copy_eqn (&pb
->eqs
[j
], eq
, pb
->num_vars
);
3152 omega_free_eqns (eq
, 1);
3158 /* Eliminate all EQ equations */
3159 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
3161 eqn eqn
= &(pb
->eqs
[e
]);
3164 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3165 fprintf (dump_file
, "----\n");
3167 for (i
= pb
->num_vars
; i
> 0; i
--)
3173 for (j
= i
- 1; j
> 0; j
--)
3177 /* i is the position of last non-zero coefficient,
3178 g is the coefficient of i,
3179 j is the position of next non-zero coefficient. */
3183 if (eqn
->coef
[0] % g
!= 0)
3184 return omega_problem_has_no_solution ();
3186 eqn
->coef
[0] = eqn
->coef
[0] / g
;
3189 omega_do_elimination (pb
, e
, i
);
3195 if (eqn
->coef
[0] != 0)
3196 return omega_problem_has_no_solution ();
3208 omega_do_elimination (pb
, e
, i
);
3214 bool promotion_possible
=
3215 (omega_safe_var_p (pb
, j
)
3216 && pb
->safe_vars
+ 1 == i
3217 && !omega_eqn_is_red (eqn
, desired_res
)
3218 && !in_approximate_mode
);
3220 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && promotion_possible
)
3221 fprintf (dump_file
, " Promotion possible\n");
3224 if (!omega_safe_var_p (pb
, j
))
3226 for (; g
!= 1 && !omega_safe_var_p (pb
, j
); j
--)
3227 g
= gcd (abs (eqn
->coef
[j
]), g
);
3230 else if (!omega_safe_var_p (pb
, i
))
3235 for (; g
!= 1 && j
> 0; j
--)
3236 g
= gcd (abs (eqn
->coef
[j
]), g
);
3240 if (eqn
->coef
[0] % g
!= 0)
3241 return omega_problem_has_no_solution ();
3243 for (j
= 0; j
<= pb
->num_vars
; j
++)
3253 for (e2
= e
- 1; e2
>= 0; e2
--)
3254 if (pb
->eqs
[e2
].coef
[i
])
3258 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
3259 if (pb
->geqs
[e2
].coef
[i
])
3263 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
3264 if (pb
->subs
[e2
].coef
[i
])
3269 bool change
= false;
3271 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3273 fprintf (dump_file
, "Ha! We own it! \n");
3274 omega_print_eq (dump_file
, pb
, eqn
);
3275 fprintf (dump_file
, " \n");
3281 for (j
= i
- 1; j
>= 0; j
--)
3283 int t
= int_mod (eqn
->coef
[j
], g
);
3288 if (t
!= eqn
->coef
[j
])
3297 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3298 fprintf (dump_file
, "So what?\n");
3303 omega_name_wild_card (pb
, i
);
3305 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3307 omega_print_eq (dump_file
, pb
, eqn
);
3308 fprintf (dump_file
, " \n");
3317 if (promotion_possible
)
3319 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3321 fprintf (dump_file
, "promoting %s to safety\n",
3322 omega_variable_to_str (pb
, i
));
3323 omega_print_vars (dump_file
, pb
);
3328 if (!omega_wildcard_p (pb
, i
))
3329 omega_name_wild_card (pb
, i
);
3331 promotion_possible
= false;
3336 if (g2
> 1 && !in_approximate_mode
)
3338 if (pb
->eqs
[e
].color
== omega_red
)
3340 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3341 fprintf (dump_file
, "handling red equality\n");
3344 omega_do_elimination (pb
, e
, i
);
3348 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3351 "adding equation to handle safe variable \n");
3352 omega_print_eq (dump_file
, pb
, eqn
);
3353 fprintf (dump_file
, "\n----\n");
3354 omega_print_problem (dump_file
, pb
);
3355 fprintf (dump_file
, "\n----\n");
3356 fprintf (dump_file
, "\n----\n");
3359 i
= omega_add_new_wild_card (pb
);
3361 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
3362 omega_init_eqn_zero (&pb
->eqs
[e
+ 1], pb
->num_vars
);
3363 omega_copy_eqn (&pb
->eqs
[e
+ 1], eqn
, pb
->safe_vars
);
3365 for (j
= pb
->num_vars
; j
>= 0; j
--)
3367 pb
->eqs
[e
+ 1].coef
[j
] = int_mod (pb
->eqs
[e
+ 1].coef
[j
], g2
);
3369 if (2 * pb
->eqs
[e
+ 1].coef
[j
] >= g2
)
3370 pb
->eqs
[e
+ 1].coef
[j
] -= g2
;
3373 pb
->eqs
[e
+ 1].coef
[i
] = g2
;
3376 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3377 omega_print_problem (dump_file
, pb
);
3386 /* Find variable to eliminate. */
3389 gcc_assert (in_approximate_mode
);
3391 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3393 fprintf (dump_file
, "non-exact elimination: ");
3394 omega_print_eq (dump_file
, pb
, eqn
);
3395 fprintf (dump_file
, "\n");
3396 omega_print_problem (dump_file
, pb
);
3399 for (i
= pb
->num_vars
; i
> sv
; i
--)
3400 if (pb
->eqs
[e
].coef
[i
] != 0)
3404 for (i
= pb
->num_vars
; i
> sv
; i
--)
3405 if (pb
->eqs
[e
].coef
[i
] == 1 || pb
->eqs
[e
].coef
[i
] == -1)
3411 omega_do_elimination (pb
, e
, i
);
3413 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && g2
> 1)
3415 fprintf (dump_file
, "result of non-exact elimination:\n");
3416 omega_print_problem (dump_file
, pb
);
3421 int factor
= (INT_MAX
);
3424 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3425 fprintf (dump_file
, "doing moding\n");
3427 for (i
= pb
->num_vars
; i
!= sv
; i
--)
3428 if ((pb
->eqs
[e
].coef
[i
] & 1) != 0)
3433 for (; i
!= sv
; i
--)
3434 if ((pb
->eqs
[e
].coef
[i
] & 1) != 0)
3440 if (j
!= 0 && i
== sv
)
3442 omega_do_mod (pb
, 2, e
, j
);
3448 for (i
= pb
->num_vars
; i
!= sv
; i
--)
3449 if (pb
->eqs
[e
].coef
[i
] != 0
3450 && factor
> abs (pb
->eqs
[e
].coef
[i
]) + 1)
3452 factor
= abs (pb
->eqs
[e
].coef
[i
]) + 1;
3458 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3459 fprintf (dump_file
, "should not have happened\n");
3463 omega_do_mod (pb
, factor
, e
, j
);
3464 /* Go back and try this equation again. */
3471 return omega_unknown
;
3474 /* Transform an inequation E to an equality, then solve DIFF problems
3475 based on PB, and only differing by the constant part that is
3476 diminished by one, trying to figure out which of the constants
3479 static enum omega_result
3480 parallel_splinter (omega_pb pb
, int e
, int diff
,
3481 enum omega_result desired_res
)
3483 omega_pb tmp_problem
;
3486 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3488 fprintf (dump_file
, "Using parallel splintering\n");
3489 omega_print_problem (dump_file
, pb
);
3492 tmp_problem
= XNEW (struct omega_pb
);
3493 omega_copy_eqn (&pb
->eqs
[0], &pb
->geqs
[e
], pb
->num_vars
);
3496 for (i
= 0; i
<= diff
; i
++)
3498 omega_copy_problem (tmp_problem
, pb
);
3500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3502 fprintf (dump_file
, "Splinter # %i\n", i
);
3503 omega_print_problem (dump_file
, pb
);
3506 if (omega_solve_problem (tmp_problem
, desired_res
) == omega_true
)
3512 pb
->eqs
[0].coef
[0]--;
3519 /* Helper function: solve equations one at a time. */
3521 static enum omega_result
3522 omega_solve_geq (omega_pb pb
, enum omega_result desired_res
)
3526 enum omega_result result
;
3527 bool coupled_subscripts
= false;
3528 bool smoothed
= false;
3529 bool eliminate_again
;
3530 bool tried_eliminating_redundant
= false;
3532 if (desired_res
!= omega_simplify
)
3540 gcc_assert (desired_res
== omega_simplify
|| pb
->num_subs
== 0);
3542 /* Verify that there are not too many inequalities. */
3543 gcc_assert (pb
->num_geqs
<= OMEGA_MAX_GEQS
);
3545 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3547 fprintf (dump_file
, "\nomega_solve_geq (%d,%d):\n",
3548 desired_res
, please_no_equalities_in_simplified_problems
);
3549 omega_print_problem (dump_file
, pb
);
3550 fprintf (dump_file
, "\n");
3553 n_vars
= pb
->num_vars
;
3557 enum omega_eqn_color u_color
= omega_black
;
3558 enum omega_eqn_color l_color
= omega_black
;
3559 int upper_bound
= pos_infinity
;
3560 int lower_bound
= neg_infinity
;
3562 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3564 int a
= pb
->geqs
[e
].coef
[1];
3565 int c
= pb
->geqs
[e
].coef
[0];
3567 /* Our equation is ax + c >= 0, or ax >= -c, or c >= -ax. */
3571 return omega_problem_has_no_solution ();
3578 if (lower_bound
< -c
3579 || (lower_bound
== -c
3580 && !omega_eqn_is_red (&pb
->geqs
[e
], desired_res
)))
3583 l_color
= pb
->geqs
[e
].color
;
3589 c
= int_div (c
, -a
);
3592 || (upper_bound
== c
3593 && !omega_eqn_is_red (&pb
->geqs
[e
], desired_res
)))
3596 u_color
= pb
->geqs
[e
].color
;
3601 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3603 fprintf (dump_file
, "upper bound = %d\n", upper_bound
);
3604 fprintf (dump_file
, "lower bound = %d\n", lower_bound
);
3607 if (lower_bound
> upper_bound
)
3608 return omega_problem_has_no_solution ();
3610 if (desired_res
== omega_simplify
)
3613 if (pb
->safe_vars
== 1)
3616 if (lower_bound
== upper_bound
3617 && u_color
== omega_black
3618 && l_color
== omega_black
)
3620 pb
->eqs
[0].coef
[0] = -lower_bound
;
3621 pb
->eqs
[0].coef
[1] = 1;
3622 pb
->eqs
[0].color
= omega_black
;
3624 return omega_solve_problem (pb
, desired_res
);
3628 if (lower_bound
> neg_infinity
)
3630 pb
->geqs
[0].coef
[0] = -lower_bound
;
3631 pb
->geqs
[0].coef
[1] = 1;
3632 pb
->geqs
[0].key
= 1;
3633 pb
->geqs
[0].color
= l_color
;
3634 pb
->geqs
[0].touched
= 0;
3638 if (upper_bound
< pos_infinity
)
3640 pb
->geqs
[pb
->num_geqs
].coef
[0] = upper_bound
;
3641 pb
->geqs
[pb
->num_geqs
].coef
[1] = -1;
3642 pb
->geqs
[pb
->num_geqs
].key
= -1;
3643 pb
->geqs
[pb
->num_geqs
].color
= u_color
;
3644 pb
->geqs
[pb
->num_geqs
].touched
= 0;
3652 omega_problem_reduced (pb
);
3656 if (original_problem
!= no_problem
3657 && l_color
== omega_black
3658 && u_color
== omega_black
3660 && lower_bound
== upper_bound
)
3662 pb
->eqs
[0].coef
[0] = -lower_bound
;
3663 pb
->eqs
[0].coef
[1] = 1;
3665 adding_equality_constraint (pb
, 0);
3671 if (!pb
->variables_freed
)
3673 pb
->variables_freed
= true;
3675 if (desired_res
!= omega_simplify
)
3676 omega_free_eliminations (pb
, 0);
3678 omega_free_eliminations (pb
, pb
->safe_vars
);
3680 n_vars
= pb
->num_vars
;
3686 switch (normalize_omega_problem (pb
))
3688 case normalize_false
:
3692 case normalize_coupled
:
3693 coupled_subscripts
= true;
3696 case normalize_uncoupled
:
3697 coupled_subscripts
= false;
3704 n_vars
= pb
->num_vars
;
3706 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3708 fprintf (dump_file
, "\nafter normalization:\n");
3709 omega_print_problem (dump_file
, pb
);
3710 fprintf (dump_file
, "\n");
3711 fprintf (dump_file
, "eliminating variable using Fourier-Motzkin.\n");
3715 int parallel_difference
= INT_MAX
;
3716 int best_parallel_eqn
= -1;
3717 int minC
, maxC
, minCj
= 0;
3718 int lower_bound_count
= 0;
3720 bool possible_easy_int_solution
;
3721 int max_splinters
= 1;
3723 bool lucky_exact
= false;
3725 int best
= (INT_MAX
);
3726 int j
= 0, jLe
= 0, jLowerBoundCount
= 0;
3729 eliminate_again
= false;
3731 if (pb
->num_eqs
> 0)
3732 return omega_solve_problem (pb
, desired_res
);
3734 if (!coupled_subscripts
)
3736 if (pb
->safe_vars
== 0)
3739 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3740 if (!omega_safe_var_p (pb
, abs (pb
->geqs
[e
].key
)))
3741 omega_delete_geq (pb
, e
, n_vars
);
3743 pb
->num_vars
= pb
->safe_vars
;
3745 if (desired_res
== omega_simplify
)
3747 omega_problem_reduced (pb
);
3754 if (desired_res
!= omega_simplify
)
3759 if (pb
->num_geqs
== 0)
3761 if (desired_res
== omega_simplify
)
3763 pb
->num_vars
= pb
->safe_vars
;
3764 omega_problem_reduced (pb
);
3770 if (desired_res
== omega_simplify
&& n_vars
== pb
->safe_vars
)
3772 omega_problem_reduced (pb
);
3776 if (pb
->num_geqs
> OMEGA_MAX_GEQS
- 30
3777 || pb
->num_geqs
> 2 * n_vars
* n_vars
+ 4 * n_vars
+ 10)
3779 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3781 "TOO MANY EQUATIONS; "
3782 "%d equations, %d variables, "
3783 "ELIMINATING REDUNDANT ONES\n",
3784 pb
->num_geqs
, n_vars
);
3786 if (!omega_eliminate_redundant (pb
, false))
3789 n_vars
= pb
->num_vars
;
3791 if (pb
->num_eqs
> 0)
3792 return omega_solve_problem (pb
, desired_res
);
3794 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3795 fprintf (dump_file
, "END ELIMINATION OF REDUNDANT EQUATIONS\n");
3798 if (desired_res
!= omega_simplify
)
3803 for (i
= n_vars
; i
!= fv
; i
--)
3809 int upper_bound_count
= 0;
3811 lower_bound_count
= 0;
3814 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3815 if (pb
->geqs
[e
].coef
[i
] < 0)
3817 minC
= MIN (minC
, pb
->geqs
[e
].coef
[i
]);
3818 upper_bound_count
++;
3819 if (pb
->geqs
[e
].coef
[i
] < -1)
3827 else if (pb
->geqs
[e
].coef
[i
] > 0)
3829 maxC
= MAX (maxC
, pb
->geqs
[e
].coef
[i
]);
3830 lower_bound_count
++;
3832 if (pb
->geqs
[e
].coef
[i
] > 1)
3841 if (lower_bound_count
== 0
3842 || upper_bound_count
== 0)
3844 lower_bound_count
= 0;
3848 if (ub
>= 0 && lb
>= 0
3849 && pb
->geqs
[lb
].key
== -pb
->geqs
[ub
].key
)
3851 int Lc
= pb
->geqs
[lb
].coef
[i
];
3852 int Uc
= -pb
->geqs
[ub
].coef
[i
];
3854 Lc
* pb
->geqs
[ub
].coef
[0] + Uc
* pb
->geqs
[lb
].coef
[0];
3855 lucky
= (diff
>= (Uc
- 1) * (Lc
- 1));
3861 || in_approximate_mode
)
3863 neweqns
= score
= upper_bound_count
* lower_bound_count
;
3865 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3867 "For %s, exact, score = %d*%d, range = %d ... %d,"
3868 "\nlucky = %d, in_approximate_mode=%d \n",
3869 omega_variable_to_str (pb
, i
),
3871 lower_bound_count
, minC
, maxC
, lucky
,
3872 in_approximate_mode
);
3882 jLowerBoundCount
= lower_bound_count
;
3884 lucky_exact
= lucky
;
3891 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3893 "For %s, non-exact, score = %d*%d,"
3894 "range = %d ... %d \n",
3895 omega_variable_to_str (pb
, i
),
3897 lower_bound_count
, minC
, maxC
);
3899 neweqns
= upper_bound_count
* lower_bound_count
;
3900 score
= maxC
- minC
;
3908 jLowerBoundCount
= lower_bound_count
;
3913 if (lower_bound_count
== 0)
3915 omega_free_eliminations (pb
, pb
->safe_vars
);
3916 n_vars
= pb
->num_vars
;
3917 eliminate_again
= true;
3924 lower_bound_count
= jLowerBoundCount
;
3926 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3927 if (pb
->geqs
[e
].coef
[i
] > 0)
3929 if (pb
->geqs
[e
].coef
[i
] == -minC
)
3930 max_splinters
+= -minC
- 1;
3933 check_pos_mul ((pb
->geqs
[e
].coef
[i
] - 1),
3934 (-minC
- 1)) / (-minC
) + 1;
3938 /* Trying to produce exact elimination by finding redundant
3940 if (!exact
&& !tried_eliminating_redundant
)
3942 omega_eliminate_redundant (pb
, false);
3943 tried_eliminating_redundant
= true;
3944 eliminate_again
= true;
3947 tried_eliminating_redundant
= false;
3950 if (return_single_result
&& desired_res
== omega_simplify
&& !exact
)
3952 omega_problem_reduced (pb
);
3956 /* #ifndef Omega3 */
3957 /* Trying to produce exact elimination by finding redundant
3959 if (!exact
&& !tried_eliminating_redundant
)
3961 omega_eliminate_redundant (pb
, false);
3962 tried_eliminating_redundant
= true;
3965 tried_eliminating_redundant
= false;
3972 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
3973 if (pb
->geqs
[e1
].color
== omega_black
)
3974 for (e2
= e1
- 1; e2
>= 0; e2
--)
3975 if (pb
->geqs
[e2
].color
== omega_black
3976 && pb
->geqs
[e1
].key
== -pb
->geqs
[e2
].key
3977 && ((pb
->geqs
[e1
].coef
[0] + pb
->geqs
[e2
].coef
[0])
3978 * (3 - single_var_geq (&pb
->geqs
[e1
], pb
->num_vars
))
3979 / 2 < parallel_difference
))
3981 parallel_difference
=
3982 (pb
->geqs
[e1
].coef
[0] + pb
->geqs
[e2
].coef
[0])
3983 * (3 - single_var_geq (&pb
->geqs
[e1
], pb
->num_vars
))
3985 best_parallel_eqn
= e1
;
3988 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
3989 && best_parallel_eqn
>= 0)
3992 "Possible parallel projection, diff = %d, in ",
3993 parallel_difference
);
3994 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[best_parallel_eqn
]));
3995 fprintf (dump_file
, "\n");
3996 omega_print_problem (dump_file
, pb
);
4000 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4002 fprintf (dump_file
, "going to eliminate %s, (%d,%d,%d)\n",
4003 omega_variable_to_str (pb
, i
), i
, minC
,
4005 omega_print_problem (dump_file
, pb
);
4008 fprintf (dump_file
, "(a lucky exact elimination)\n");
4011 fprintf (dump_file
, "(an exact elimination)\n");
4013 fprintf (dump_file
, "Max # of splinters = %d\n", max_splinters
);
4016 gcc_assert (max_splinters
>= 1);
4018 if (!exact
&& desired_res
== omega_simplify
&& best_parallel_eqn
>= 0
4019 && parallel_difference
<= max_splinters
)
4020 return parallel_splinter (pb
, best_parallel_eqn
, parallel_difference
,
4028 int j
= pb
->num_vars
;
4030 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4032 fprintf (dump_file
, "Swapping %d and %d\n", i
, j
);
4033 omega_print_problem (dump_file
, pb
);
4036 swap (&pb
->var
[i
], &pb
->var
[j
]);
4038 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4039 if (pb
->geqs
[e
].coef
[i
] != pb
->geqs
[e
].coef
[j
])
4041 pb
->geqs
[e
].touched
= 1;
4042 t
= pb
->geqs
[e
].coef
[i
];
4043 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[j
];
4044 pb
->geqs
[e
].coef
[j
] = t
;
4047 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
4048 if (pb
->subs
[e
].coef
[i
] != pb
->subs
[e
].coef
[j
])
4050 t
= pb
->subs
[e
].coef
[i
];
4051 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[j
];
4052 pb
->subs
[e
].coef
[j
] = t
;
4055 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4057 fprintf (dump_file
, "Swapping complete \n");
4058 omega_print_problem (dump_file
, pb
);
4059 fprintf (dump_file
, "\n");
4065 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4067 fprintf (dump_file
, "No swap needed\n");
4068 omega_print_problem (dump_file
, pb
);
4072 n_vars
= pb
->num_vars
;
4078 int upper_bound
= pos_infinity
;
4079 int lower_bound
= neg_infinity
;
4080 enum omega_eqn_color ub_color
= omega_black
;
4081 enum omega_eqn_color lb_color
= omega_black
;
4082 int topeqn
= pb
->num_geqs
- 1;
4083 int Lc
= pb
->geqs
[Le
].coef
[i
];
4085 for (Le
= topeqn
; Le
>= 0; Le
--)
4086 if ((Lc
= pb
->geqs
[Le
].coef
[i
]) == 0)
4088 if (pb
->geqs
[Le
].coef
[1] == 1)
4090 int constantTerm
= -pb
->geqs
[Le
].coef
[0];
4092 if (constantTerm
> lower_bound
||
4093 (constantTerm
== lower_bound
&&
4094 !omega_eqn_is_red (&pb
->geqs
[Le
], desired_res
)))
4096 lower_bound
= constantTerm
;
4097 lb_color
= pb
->geqs
[Le
].color
;
4100 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4102 if (pb
->geqs
[Le
].color
== omega_black
)
4103 fprintf (dump_file
, " :::=> %s >= %d\n",
4104 omega_variable_to_str (pb
, 1),
4108 " :::=> [%s >= %d]\n",
4109 omega_variable_to_str (pb
, 1),
4115 int constantTerm
= pb
->geqs
[Le
].coef
[0];
4116 if (constantTerm
< upper_bound
||
4117 (constantTerm
== upper_bound
4118 && !omega_eqn_is_red (&pb
->geqs
[Le
],
4121 upper_bound
= constantTerm
;
4122 ub_color
= pb
->geqs
[Le
].color
;
4125 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4127 if (pb
->geqs
[Le
].color
== omega_black
)
4128 fprintf (dump_file
, " :::=> %s <= %d\n",
4129 omega_variable_to_str (pb
, 1),
4133 " :::=> [%s <= %d]\n",
4134 omega_variable_to_str (pb
, 1),
4140 for (Ue
= topeqn
; Ue
>= 0; Ue
--)
4141 if (pb
->geqs
[Ue
].coef
[i
] < 0
4142 && pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4144 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4145 int coefficient
= pb
->geqs
[Ue
].coef
[1] * Lc
4146 + pb
->geqs
[Le
].coef
[1] * Uc
;
4147 int constantTerm
= pb
->geqs
[Ue
].coef
[0] * Lc
4148 + pb
->geqs
[Le
].coef
[0] * Uc
;
4150 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4152 omega_print_geq_extra (dump_file
, pb
,
4154 fprintf (dump_file
, "\n");
4155 omega_print_geq_extra (dump_file
, pb
,
4157 fprintf (dump_file
, "\n");
4160 if (coefficient
> 0)
4162 constantTerm
= -int_div (constantTerm
, coefficient
);
4164 if (constantTerm
> lower_bound
4165 || (constantTerm
== lower_bound
4166 && (desired_res
!= omega_simplify
4167 || (pb
->geqs
[Ue
].color
== omega_black
4168 && pb
->geqs
[Le
].color
== omega_black
))))
4170 lower_bound
= constantTerm
;
4171 lb_color
= (pb
->geqs
[Ue
].color
== omega_red
4172 || pb
->geqs
[Le
].color
== omega_red
)
4173 ? omega_red
: omega_black
;
4176 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4178 if (pb
->geqs
[Ue
].color
== omega_red
4179 || pb
->geqs
[Le
].color
== omega_red
)
4181 " ::=> [%s >= %d]\n",
4182 omega_variable_to_str (pb
, 1),
4187 omega_variable_to_str (pb
, 1),
4193 constantTerm
= int_div (constantTerm
, -coefficient
);
4194 if (constantTerm
< upper_bound
4195 || (constantTerm
== upper_bound
4196 && pb
->geqs
[Ue
].color
== omega_black
4197 && pb
->geqs
[Le
].color
== omega_black
))
4199 upper_bound
= constantTerm
;
4200 ub_color
= (pb
->geqs
[Ue
].color
== omega_red
4201 || pb
->geqs
[Le
].color
== omega_red
)
4202 ? omega_red
: omega_black
;
4206 && (dump_flags
& TDF_DETAILS
))
4208 if (pb
->geqs
[Ue
].color
== omega_red
4209 || pb
->geqs
[Le
].color
== omega_red
)
4211 " ::=> [%s <= %d]\n",
4212 omega_variable_to_str (pb
, 1),
4217 omega_variable_to_str (pb
, 1),
4225 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4227 " therefore, %c%d <= %c%s%c <= %d%c\n",
4228 lb_color
== omega_red
? '[' : ' ', lower_bound
,
4229 (lb_color
== omega_red
&& ub_color
== omega_black
)
4231 omega_variable_to_str (pb
, 1),
4232 (lb_color
== omega_black
&& ub_color
== omega_red
)
4234 upper_bound
, ub_color
== omega_red
? ']' : ' ');
4236 if (lower_bound
> upper_bound
)
4239 if (pb
->safe_vars
== 1)
4241 if (upper_bound
== lower_bound
4242 && !(ub_color
== omega_red
|| lb_color
== omega_red
)
4243 && !please_no_equalities_in_simplified_problems
)
4246 pb
->eqs
[0].coef
[1] = -1;
4247 pb
->eqs
[0].coef
[0] = upper_bound
;
4249 if (ub_color
== omega_red
4250 || lb_color
== omega_red
)
4251 pb
->eqs
[0].color
= omega_red
;
4253 if (desired_res
== omega_simplify
4254 && pb
->eqs
[0].color
== omega_black
)
4255 return omega_solve_problem (pb
, desired_res
);
4258 if (upper_bound
!= pos_infinity
)
4260 pb
->geqs
[0].coef
[1] = -1;
4261 pb
->geqs
[0].coef
[0] = upper_bound
;
4262 pb
->geqs
[0].color
= ub_color
;
4263 pb
->geqs
[0].key
= -1;
4264 pb
->geqs
[0].touched
= 0;
4268 if (lower_bound
!= neg_infinity
)
4270 pb
->geqs
[pb
->num_geqs
].coef
[1] = 1;
4271 pb
->geqs
[pb
->num_geqs
].coef
[0] = -lower_bound
;
4272 pb
->geqs
[pb
->num_geqs
].color
= lb_color
;
4273 pb
->geqs
[pb
->num_geqs
].key
= 1;
4274 pb
->geqs
[pb
->num_geqs
].touched
= 0;
4279 if (desired_res
== omega_simplify
)
4281 omega_problem_reduced (pb
);
4287 && (desired_res
!= omega_simplify
4288 || (lb_color
== omega_black
4289 && ub_color
== omega_black
))
4290 && original_problem
!= no_problem
4291 && lower_bound
== upper_bound
)
4293 for (i
= original_problem
->num_vars
; i
>= 0; i
--)
4294 if (original_problem
->var
[i
] == pb
->var
[1])
4300 e
= original_problem
->num_eqs
++;
4301 omega_init_eqn_zero (&original_problem
->eqs
[e
],
4302 original_problem
->num_vars
);
4303 original_problem
->eqs
[e
].coef
[i
] = -1;
4304 original_problem
->eqs
[e
].coef
[0] = upper_bound
;
4306 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4309 "adding equality %d to outer problem\n", e
);
4310 omega_print_problem (dump_file
, original_problem
);
4317 eliminate_again
= true;
4319 if (lower_bound_count
== 1)
4321 eqn lbeqn
= omega_alloc_eqns (0, 1);
4322 int Lc
= pb
->geqs
[Le
].coef
[i
];
4324 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4325 fprintf (dump_file
, "an inplace elimination\n");
4327 omega_copy_eqn (lbeqn
, &pb
->geqs
[Le
], (n_vars
+ 1));
4328 omega_delete_geq_extra (pb
, Le
, n_vars
+ 1);
4330 for (Ue
= pb
->num_geqs
- 1; Ue
>= 0; Ue
--)
4331 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4333 if (lbeqn
->key
== -pb
->geqs
[Ue
].key
)
4334 omega_delete_geq_extra (pb
, Ue
, n_vars
+ 1);
4338 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4339 pb
->geqs
[Ue
].touched
= 1;
4340 eliminate_again
= false;
4342 if (lbeqn
->color
== omega_red
)
4343 pb
->geqs
[Ue
].color
= omega_red
;
4345 for (k
= 0; k
<= n_vars
; k
++)
4346 pb
->geqs
[Ue
].coef
[k
] =
4347 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4348 check_mul (lbeqn
->coef
[k
], Uc
);
4350 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4352 omega_print_geq (dump_file
, pb
,
4354 fprintf (dump_file
, "\n");
4359 omega_free_eqns (lbeqn
, 1);
4364 int *dead_eqns
= XNEWVEC (int, OMEGA_MAX_GEQS
);
4365 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
4367 int top_eqn
= pb
->num_geqs
- 1;
4368 lower_bound_count
--;
4370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4371 fprintf (dump_file
, "lower bound count = %d\n",
4374 for (Le
= top_eqn
; Le
>= 0; Le
--)
4375 if (pb
->geqs
[Le
].coef
[i
] > 0)
4377 int Lc
= pb
->geqs
[Le
].coef
[i
];
4378 for (Ue
= top_eqn
; Ue
>= 0; Ue
--)
4379 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4381 if (pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4384 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4387 e2
= pb
->num_geqs
++;
4389 e2
= dead_eqns
[--num_dead
];
4391 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4393 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4396 "Le = %d, Ue = %d, gen = %d\n",
4398 omega_print_geq_extra (dump_file
, pb
,
4400 fprintf (dump_file
, "\n");
4401 omega_print_geq_extra (dump_file
, pb
,
4403 fprintf (dump_file
, "\n");
4406 eliminate_again
= false;
4408 for (k
= n_vars
; k
>= 0; k
--)
4409 pb
->geqs
[e2
].coef
[k
] =
4410 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4411 check_mul (pb
->geqs
[Le
].coef
[k
], Uc
);
4413 pb
->geqs
[e2
].coef
[n_vars
+ 1] = 0;
4414 pb
->geqs
[e2
].touched
= 1;
4416 if (pb
->geqs
[Ue
].color
== omega_red
4417 || pb
->geqs
[Le
].color
== omega_red
)
4418 pb
->geqs
[e2
].color
= omega_red
;
4420 pb
->geqs
[e2
].color
= omega_black
;
4422 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4424 omega_print_geq (dump_file
, pb
,
4426 fprintf (dump_file
, "\n");
4430 if (lower_bound_count
== 0)
4432 dead_eqns
[num_dead
++] = Ue
;
4434 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4435 fprintf (dump_file
, "Killed %d\n", Ue
);
4439 lower_bound_count
--;
4440 dead_eqns
[num_dead
++] = Le
;
4442 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4443 fprintf (dump_file
, "Killed %d\n", Le
);
4446 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4449 while (num_dead
> 0)
4450 is_dead
[dead_eqns
[--num_dead
]] = true;
4452 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4454 omega_delete_geq_extra (pb
, e
, n_vars
+ 1);
4465 rS
= omega_alloc_problem (0, 0);
4466 iS
= omega_alloc_problem (0, 0);
4468 possible_easy_int_solution
= true;
4470 for (e
= 0; e
< pb
->num_geqs
; e
++)
4471 if (pb
->geqs
[e
].coef
[i
] == 0)
4473 omega_copy_eqn (&(rS
->geqs
[e2
]), &pb
->geqs
[e
],
4475 omega_copy_eqn (&(iS
->geqs
[e2
]), &pb
->geqs
[e
],
4478 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4481 fprintf (dump_file
, "Copying (%d, %d): ", i
,
4482 pb
->geqs
[e
].coef
[i
]);
4483 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[e
]);
4484 fprintf (dump_file
, "\n");
4485 for (t
= 0; t
<= n_vars
+ 1; t
++)
4486 fprintf (dump_file
, "%d ", pb
->geqs
[e
].coef
[t
]);
4487 fprintf (dump_file
, "\n");
4491 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4494 for (Le
= pb
->num_geqs
- 1; Le
>= 0; Le
--)
4495 if (pb
->geqs
[Le
].coef
[i
] > 0)
4496 for (Ue
= pb
->num_geqs
- 1; Ue
>= 0; Ue
--)
4497 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4500 int Lc
= pb
->geqs
[Le
].coef
[i
];
4501 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4503 if (pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4506 rS
->geqs
[e2
].touched
= iS
->geqs
[e2
].touched
= 1;
4508 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4510 fprintf (dump_file
, "---\n");
4512 "Le(Lc) = %d(%d_, Ue(Uc) = %d(%d), gen = %d\n",
4513 Le
, Lc
, Ue
, Uc
, e2
);
4514 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[Le
]);
4515 fprintf (dump_file
, "\n");
4516 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[Ue
]);
4517 fprintf (dump_file
, "\n");
4522 for (k
= n_vars
; k
>= 0; k
--)
4523 iS
->geqs
[e2
].coef
[k
] = rS
->geqs
[e2
].coef
[k
] =
4524 pb
->geqs
[Ue
].coef
[k
] + pb
->geqs
[Le
].coef
[k
];
4526 iS
->geqs
[e2
].coef
[0] -= (Uc
- 1);
4530 for (k
= n_vars
; k
>= 0; k
--)
4531 iS
->geqs
[e2
].coef
[k
] = rS
->geqs
[e2
].coef
[k
] =
4532 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4533 check_mul (pb
->geqs
[Le
].coef
[k
], Uc
);
4535 iS
->geqs
[e2
].coef
[0] -= (Uc
- 1) * (Lc
- 1);
4538 if (pb
->geqs
[Ue
].color
== omega_red
4539 || pb
->geqs
[Le
].color
== omega_red
)
4540 iS
->geqs
[e2
].color
= rS
->geqs
[e2
].color
= omega_red
;
4542 iS
->geqs
[e2
].color
= rS
->geqs
[e2
].color
= omega_black
;
4544 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4546 omega_print_geq (dump_file
, pb
, &(rS
->geqs
[e2
]));
4547 fprintf (dump_file
, "\n");
4551 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4553 else if (pb
->geqs
[Ue
].coef
[0] * Lc
+
4554 pb
->geqs
[Le
].coef
[0] * Uc
-
4555 (Uc
- 1) * (Lc
- 1) < 0)
4556 possible_easy_int_solution
= false;
4559 iS
->variables_initialized
= rS
->variables_initialized
= true;
4560 iS
->num_vars
= rS
->num_vars
= pb
->num_vars
;
4561 iS
->num_geqs
= rS
->num_geqs
= e2
;
4562 iS
->num_eqs
= rS
->num_eqs
= 0;
4563 iS
->num_subs
= rS
->num_subs
= pb
->num_subs
;
4564 iS
->safe_vars
= rS
->safe_vars
= pb
->safe_vars
;
4566 for (e
= n_vars
; e
>= 0; e
--)
4567 rS
->var
[e
] = pb
->var
[e
];
4569 for (e
= n_vars
; e
>= 0; e
--)
4570 iS
->var
[e
] = pb
->var
[e
];
4572 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
4574 omega_copy_eqn (&(rS
->subs
[e
]), &(pb
->subs
[e
]), pb
->num_vars
);
4575 omega_copy_eqn (&(iS
->subs
[e
]), &(pb
->subs
[e
]), pb
->num_vars
);
4579 n_vars
= pb
->num_vars
;
4581 if (desired_res
!= omega_true
)
4583 if (original_problem
== no_problem
)
4585 original_problem
= pb
;
4586 result
= omega_solve_geq (rS
, omega_false
);
4587 original_problem
= no_problem
;
4590 result
= omega_solve_geq (rS
, omega_false
);
4592 if (result
== omega_false
)
4599 if (pb
->num_eqs
> 0)
4601 /* An equality constraint must have been found */
4604 return omega_solve_problem (pb
, desired_res
);
4608 if (desired_res
!= omega_false
)
4611 int lower_bounds
= 0;
4612 int *lower_bound
= XNEWVEC (int, OMEGA_MAX_GEQS
);
4614 if (possible_easy_int_solution
)
4617 result
= omega_solve_geq (iS
, desired_res
);
4620 if (result
!= omega_false
)
4629 if (!exact
&& best_parallel_eqn
>= 0
4630 && parallel_difference
<= max_splinters
)
4635 return parallel_splinter (pb
, best_parallel_eqn
,
4636 parallel_difference
,
4640 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4641 fprintf (dump_file
, "have to do exact analysis\n");
4645 for (e
= 0; e
< pb
->num_geqs
; e
++)
4646 if (pb
->geqs
[e
].coef
[i
] > 1)
4647 lower_bound
[lower_bounds
++] = e
;
4649 /* Sort array LOWER_BOUND. */
4650 for (j
= 0; j
< lower_bounds
; j
++)
4652 int k
, smallest
= j
;
4654 for (k
= j
+ 1; k
< lower_bounds
; k
++)
4655 if (pb
->geqs
[lower_bound
[smallest
]].coef
[i
] >
4656 pb
->geqs
[lower_bound
[k
]].coef
[i
])
4659 k
= lower_bound
[smallest
];
4660 lower_bound
[smallest
] = lower_bound
[j
];
4664 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4666 fprintf (dump_file
, "lower bound coeeficients = ");
4668 for (j
= 0; j
< lower_bounds
; j
++)
4669 fprintf (dump_file
, " %d",
4670 pb
->geqs
[lower_bound
[j
]].coef
[i
]);
4672 fprintf (dump_file
, "\n");
4675 for (j
= 0; j
< lower_bounds
; j
++)
4679 int worst_lower_bound_constant
= -minC
;
4682 max_incr
= (((pb
->geqs
[e
].coef
[i
] - 1) *
4683 (worst_lower_bound_constant
- 1) - 1)
4684 / worst_lower_bound_constant
);
4685 /* max_incr += 2; */
4687 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4689 fprintf (dump_file
, "for equation ");
4690 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
4692 "\ntry decrements from 0 to %d\n",
4694 omega_print_problem (dump_file
, pb
);
4697 if (max_incr
> 50 && !smoothed
4698 && smooth_weird_equations (pb
))
4704 goto solve_geq_start
;
4707 omega_copy_eqn (&pb
->eqs
[0], &pb
->geqs
[e
],
4709 pb
->eqs
[0].color
= omega_black
;
4710 omega_init_eqn_zero (&pb
->geqs
[e
], pb
->num_vars
);
4711 pb
->geqs
[e
].touched
= 1;
4714 for (c
= max_incr
; c
>= 0; c
--)
4716 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4719 "trying next decrement of %d\n",
4721 omega_print_problem (dump_file
, pb
);
4724 omega_copy_problem (rS
, pb
);
4726 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4727 omega_print_problem (dump_file
, rS
);
4729 result
= omega_solve_problem (rS
, desired_res
);
4731 if (result
== omega_true
)
4740 pb
->eqs
[0].coef
[0]--;
4743 if (j
+ 1 < lower_bounds
)
4746 omega_copy_eqn (&pb
->geqs
[e
], &pb
->eqs
[0],
4748 pb
->geqs
[e
].touched
= 1;
4749 pb
->geqs
[e
].color
= omega_black
;
4750 omega_copy_problem (rS
, pb
);
4752 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4754 "exhausted lower bound, "
4755 "checking if still feasible ");
4757 result
= omega_solve_problem (rS
, omega_false
);
4759 if (result
== omega_false
)
4764 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4765 fprintf (dump_file
, "fall-off the end\n");
4777 return omega_unknown
;
4778 } while (eliminate_again
);
4782 /* Because the omega solver is recursive, this counter limits the
4784 static int omega_solve_depth
= 0;
4786 /* Return omega_true when the problem PB has a solution following the
4790 omega_solve_problem (omega_pb pb
, enum omega_result desired_res
)
4792 enum omega_result result
;
4794 gcc_assert (pb
->num_vars
>= pb
->safe_vars
);
4795 omega_solve_depth
++;
4797 if (desired_res
!= omega_simplify
)
4800 if (omega_solve_depth
> 50)
4802 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4805 "Solve depth = %d, in_approximate_mode = %d, aborting\n",
4806 omega_solve_depth
, in_approximate_mode
);
4807 omega_print_problem (dump_file
, pb
);
4812 if (omega_solve_eq (pb
, desired_res
) == omega_false
)
4814 omega_solve_depth
--;
4818 if (in_approximate_mode
&& !pb
->num_geqs
)
4820 result
= omega_true
;
4821 pb
->num_vars
= pb
->safe_vars
;
4822 omega_problem_reduced (pb
);
4825 result
= omega_solve_geq (pb
, desired_res
);
4827 omega_solve_depth
--;
4829 if (!omega_reduce_with_subs
)
4831 resurrect_subs (pb
);
4832 gcc_assert (please_no_equalities_in_simplified_problems
4833 || !result
|| pb
->num_subs
== 0);
4839 /* Return true if red equations constrain the set of possible solutions.
4840 We assume that there are solutions to the black equations by
4841 themselves, so if there is no solution to the combined problem, we
4845 omega_problem_has_red_equations (omega_pb pb
)
4851 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4853 fprintf (dump_file
, "Checking for red equations:\n");
4854 omega_print_problem (dump_file
, pb
);
4857 please_no_equalities_in_simplified_problems
++;
4860 if (omega_single_result
)
4861 return_single_result
++;
4863 create_color
= true;
4864 result
= (omega_simplify_problem (pb
) == omega_false
);
4866 if (omega_single_result
)
4867 return_single_result
--;
4870 please_no_equalities_in_simplified_problems
--;
4874 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4875 fprintf (dump_file
, "Gist is FALSE\n");
4880 pb
->eqs
[0].color
= omega_red
;
4882 for (i
= pb
->num_vars
; i
> 0; i
--)
4883 pb
->eqs
[0].coef
[i
] = 0;
4885 pb
->eqs
[0].coef
[0] = 1;
4889 free_red_eliminations (pb
);
4890 gcc_assert (pb
->num_eqs
== 0);
4892 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4893 if (pb
->geqs
[e
].color
== omega_red
)
4899 for (i
= pb
->safe_vars
; i
>= 1; i
--)
4904 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4906 if (pb
->geqs
[e
].coef
[i
])
4908 if (pb
->geqs
[e
].coef
[i
] > 0)
4909 lb
|= (1 + (pb
->geqs
[e
].color
== omega_red
? 1 : 0));
4912 ub
|= (1 + (pb
->geqs
[e
].color
== omega_red
? 1 : 0));
4916 if (ub
== 2 || lb
== 2)
4919 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4920 fprintf (dump_file
, "checks for upper/lower bounds worked!\n");
4922 if (!omega_reduce_with_subs
)
4924 resurrect_subs (pb
);
4925 gcc_assert (pb
->num_subs
== 0);
4933 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4935 "*** Doing potentially expensive elimination tests "
4936 "for red equations\n");
4938 please_no_equalities_in_simplified_problems
++;
4939 omega_eliminate_red (pb
, true);
4940 please_no_equalities_in_simplified_problems
--;
4943 gcc_assert (pb
->num_eqs
== 0);
4945 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4946 if (pb
->geqs
[e
].color
== omega_red
)
4949 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4953 "******************** Redudant Red Equations eliminated!!\n");
4956 "******************** Red Equations remain\n");
4958 omega_print_problem (dump_file
, pb
);
4961 if (!omega_reduce_with_subs
)
4963 normalize_return_type r
;
4965 resurrect_subs (pb
);
4966 r
= normalize_omega_problem (pb
);
4967 gcc_assert (r
!= normalize_false
);
4970 cleanout_wildcards (pb
);
4971 gcc_assert (pb
->num_subs
== 0);
4977 /* Calls omega_simplify_problem in approximate mode. */
4980 omega_simplify_approximate (omega_pb pb
)
4982 enum omega_result result
;
4984 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4985 fprintf (dump_file
, "(Entering approximate mode\n");
4987 in_approximate_mode
= true;
4988 result
= omega_simplify_problem (pb
);
4989 in_approximate_mode
= false;
4991 gcc_assert (pb
->num_vars
== pb
->safe_vars
);
4992 if (!omega_reduce_with_subs
)
4993 gcc_assert (pb
->num_subs
== 0);
4995 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4996 fprintf (dump_file
, "Leaving approximate mode)\n");
5002 /* Simplifies problem PB by eliminating redundant constraints and
5003 reducing the constraints system to a minimal form. Returns
5004 omega_true when the problem was successfully reduced, omega_unknown
5005 when the solver is unable to determine an answer. */
5008 omega_simplify_problem (omega_pb pb
)
5012 omega_found_reduction
= omega_false
;
5014 if (!pb
->variables_initialized
)
5015 omega_initialize_variables (pb
);
5017 if (next_key
* 3 > MAX_KEYS
)
5022 next_key
= OMEGA_MAX_VARS
+ 1;
5024 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5025 pb
->geqs
[e
].touched
= 1;
5027 for (i
= 0; i
< HASH_TABLE_SIZE
; i
++)
5028 hash_master
[i
].touched
= -1;
5030 pb
->hash_version
= hash_version
;
5033 else if (pb
->hash_version
!= hash_version
)
5037 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5038 pb
->geqs
[e
].touched
= 1;
5040 pb
->hash_version
= hash_version
;
5043 if (pb
->num_vars
> pb
->num_eqs
+ 3 * pb
->safe_vars
)
5044 omega_free_eliminations (pb
, pb
->safe_vars
);
5046 if (!may_be_red
&& pb
->num_subs
== 0 && pb
->safe_vars
== 0)
5048 omega_found_reduction
= omega_solve_problem (pb
, omega_unknown
);
5050 if (omega_found_reduction
!= omega_false
5051 && !return_single_result
)
5055 (*omega_when_reduced
) (pb
);
5058 return omega_found_reduction
;
5061 omega_solve_problem (pb
, omega_simplify
);
5063 if (omega_found_reduction
!= omega_false
)
5065 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
5066 pb
->forwarding_address
[pb
->var
[i
]] = i
;
5068 for (i
= 0; i
< pb
->num_subs
; i
++)
5069 pb
->forwarding_address
[pb
->subs
[i
].key
] = -i
- 1;
5072 if (!omega_reduce_with_subs
)
5073 gcc_assert (please_no_equalities_in_simplified_problems
5074 || omega_found_reduction
== omega_false
5075 || pb
->num_subs
== 0);
5077 return omega_found_reduction
;
5080 /* Make variable VAR unprotected: it then can be eliminated. */
5083 omega_unprotect_variable (omega_pb pb
, int var
)
5086 idx
= pb
->forwarding_address
[var
];
5093 if (idx
< pb
->num_subs
)
5095 omega_copy_eqn (&pb
->subs
[idx
], &pb
->subs
[pb
->num_subs
],
5097 pb
->forwarding_address
[pb
->subs
[idx
].key
] = -idx
- 1;
5102 int *bring_to_life
= XNEWVEC (int, OMEGA_MAX_VARS
);
5105 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5106 bring_to_life
[e
] = (pb
->subs
[e
].coef
[idx
] != 0);
5108 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
5109 if (bring_to_life
[e2
])
5114 if (pb
->safe_vars
< pb
->num_vars
)
5116 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5118 pb
->geqs
[e
].coef
[pb
->num_vars
] =
5119 pb
->geqs
[e
].coef
[pb
->safe_vars
];
5121 pb
->geqs
[e
].coef
[pb
->safe_vars
] = 0;
5124 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5126 pb
->eqs
[e
].coef
[pb
->num_vars
] =
5127 pb
->eqs
[e
].coef
[pb
->safe_vars
];
5129 pb
->eqs
[e
].coef
[pb
->safe_vars
] = 0;
5132 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5134 pb
->subs
[e
].coef
[pb
->num_vars
] =
5135 pb
->subs
[e
].coef
[pb
->safe_vars
];
5137 pb
->subs
[e
].coef
[pb
->safe_vars
] = 0;
5140 pb
->var
[pb
->num_vars
] = pb
->var
[pb
->safe_vars
];
5141 pb
->forwarding_address
[pb
->var
[pb
->num_vars
]] =
5146 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5147 pb
->geqs
[e
].coef
[pb
->safe_vars
] = 0;
5149 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5150 pb
->eqs
[e
].coef
[pb
->safe_vars
] = 0;
5152 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5153 pb
->subs
[e
].coef
[pb
->safe_vars
] = 0;
5156 pb
->var
[pb
->safe_vars
] = pb
->subs
[e2
].key
;
5157 pb
->forwarding_address
[pb
->subs
[e2
].key
] = pb
->safe_vars
;
5159 omega_copy_eqn (&(pb
->eqs
[pb
->num_eqs
]), &(pb
->subs
[e2
]),
5161 pb
->eqs
[pb
->num_eqs
++].coef
[pb
->safe_vars
] = -1;
5162 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5164 if (e2
< pb
->num_subs
- 1)
5165 omega_copy_eqn (&(pb
->subs
[e2
]), &(pb
->subs
[pb
->num_subs
- 1]),
5171 omega_unprotect_1 (pb
, &idx
, NULL
);
5172 free (bring_to_life
);
5175 chain_unprotect (pb
);
5178 /* Unprotects VAR and simplifies PB. */
5181 omega_constrain_variable_sign (omega_pb pb
, enum omega_eqn_color color
,
5184 int n_vars
= pb
->num_vars
;
5186 int k
= pb
->forwarding_address
[var
];
5195 omega_copy_eqn (&pb
->geqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5197 for (j
= 0; j
<= n_vars
; j
++)
5198 pb
->geqs
[e
].coef
[j
] *= sign
;
5200 pb
->geqs
[e
].coef
[0]--;
5201 pb
->geqs
[e
].touched
= 1;
5202 pb
->geqs
[e
].color
= color
;
5207 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5208 omega_copy_eqn (&pb
->eqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5209 pb
->eqs
[e
].color
= color
;
5215 omega_init_eqn_zero (&pb
->geqs
[e
], pb
->num_vars
);
5216 pb
->geqs
[e
].coef
[k
] = sign
;
5217 pb
->geqs
[e
].coef
[0] = -1;
5218 pb
->geqs
[e
].touched
= 1;
5219 pb
->geqs
[e
].color
= color
;
5224 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5225 omega_init_eqn_zero (&pb
->eqs
[e
], pb
->num_vars
);
5226 pb
->eqs
[e
].coef
[k
] = 1;
5227 pb
->eqs
[e
].color
= color
;
5230 omega_unprotect_variable (pb
, var
);
5231 return omega_simplify_problem (pb
);
5234 /* Add an equation "VAR = VALUE" with COLOR to PB. */
5237 omega_constrain_variable_value (omega_pb pb
, enum omega_eqn_color color
,
5241 int k
= pb
->forwarding_address
[var
];
5247 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5248 omega_copy_eqn (&pb
->eqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5249 pb
->eqs
[e
].coef
[0] -= value
;
5254 omega_init_eqn_zero (&pb
->eqs
[e
], pb
->num_vars
);
5255 pb
->eqs
[e
].coef
[k
] = 1;
5256 pb
->eqs
[e
].coef
[0] = -value
;
5259 pb
->eqs
[e
].color
= color
;
5262 /* Return false when the upper and lower bounds are not coupled.
5263 Initialize the bounds LOWER_BOUND and UPPER_BOUND for the values of
5267 omega_query_variable (omega_pb pb
, int i
, int *lower_bound
, int *upper_bound
)
5269 int n_vars
= pb
->num_vars
;
5272 bool coupled
= false;
5274 *lower_bound
= neg_infinity
;
5275 *upper_bound
= pos_infinity
;
5276 i
= pb
->forwarding_address
[i
];
5282 for (j
= 1; j
<= n_vars
; j
++)
5283 if (pb
->subs
[i
].coef
[j
] != 0)
5286 *upper_bound
= *lower_bound
= pb
->subs
[i
].coef
[0];
5290 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5291 if (pb
->subs
[e
].coef
[i
] != 0)
5294 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5295 if (pb
->eqs
[e
].coef
[i
] != 0)
5299 for (j
= 1; j
<= n_vars
; j
++)
5300 if (i
!= j
&& pb
->eqs
[e
].coef
[j
] != 0)
5311 *lower_bound
= *upper_bound
=
5312 -pb
->eqs
[e
].coef
[i
] * pb
->eqs
[e
].coef
[0];
5317 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5318 if (pb
->geqs
[e
].coef
[i
] != 0)
5320 if (pb
->geqs
[e
].key
== i
)
5321 *lower_bound
= MAX (*lower_bound
, -pb
->geqs
[e
].coef
[0]);
5323 else if (pb
->geqs
[e
].key
== -i
)
5324 *upper_bound
= MIN (*upper_bound
, pb
->geqs
[e
].coef
[0]);
5333 /* Sets the lower bound L and upper bound U for the values of variable
5334 I, and sets COULD_BE_ZERO to true if variable I might take value
5335 zero. LOWER_BOUND and UPPER_BOUND are bounds on the values of
5339 query_coupled_variable (omega_pb pb
, int i
, int *l
, int *u
,
5340 bool *could_be_zero
, int lower_bound
, int upper_bound
)
5347 /* Preconditions. */
5348 gcc_assert (abs (pb
->forwarding_address
[i
]) == 1
5349 && pb
->num_vars
+ pb
->num_subs
== 2
5350 && pb
->num_eqs
+ pb
->num_subs
== 1);
5352 /* Define variable I in terms of variable V. */
5353 if (pb
->forwarding_address
[i
] == -1)
5362 sign
= -eqn
->coef
[1];
5366 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5367 if (pb
->geqs
[e
].coef
[v
] != 0)
5369 if (pb
->geqs
[e
].coef
[v
] == 1)
5370 lower_bound
= MAX (lower_bound
, -pb
->geqs
[e
].coef
[0]);
5373 upper_bound
= MIN (upper_bound
, pb
->geqs
[e
].coef
[0]);
5376 if (lower_bound
> upper_bound
)
5384 if (lower_bound
== neg_infinity
)
5386 if (eqn
->coef
[v
] > 0)
5387 b1
= sign
* neg_infinity
;
5390 b1
= -sign
* neg_infinity
;
5393 b1
= sign
* (eqn
->coef
[0] + eqn
->coef
[v
] * lower_bound
);
5395 if (upper_bound
== pos_infinity
)
5397 if (eqn
->coef
[v
] > 0)
5398 b2
= sign
* pos_infinity
;
5401 b2
= -sign
* pos_infinity
;
5404 b2
= sign
* (eqn
->coef
[0] + eqn
->coef
[v
] * upper_bound
);
5406 *l
= MAX (*l
, b1
<= b2
? b1
: b2
);
5407 *u
= MIN (*u
, b1
<= b2
? b2
: b1
);
5409 *could_be_zero
= (*l
<= 0 && 0 <= *u
5410 && int_mod (eqn
->coef
[0], abs (eqn
->coef
[v
])) == 0);
5413 /* Return false when a lower bound L and an upper bound U for variable
5414 I in problem PB have been initialized. */
5417 omega_query_variable_bounds (omega_pb pb
, int i
, int *l
, int *u
)
5422 if (!omega_query_variable (pb
, i
, l
, u
)
5423 || (pb
->num_vars
== 1 && pb
->forwarding_address
[i
] == 1))
5426 if (abs (pb
->forwarding_address
[i
]) == 1
5427 && pb
->num_vars
+ pb
->num_subs
== 2
5428 && pb
->num_eqs
+ pb
->num_subs
== 1)
5431 query_coupled_variable (pb
, i
, l
, u
, &could_be_zero
, neg_infinity
,
5439 /* For problem PB, return an integer that represents the classic data
5440 dependence direction in function of the DD_LT, DD_EQ and DD_GT bit
5441 masks that are added to the result. When DIST_KNOWN is true, DIST
5442 is set to the classic data dependence distance. LOWER_BOUND and
5443 UPPER_BOUND are bounds on the value of variable I, for example, it
5444 is possible to narrow the iteration domain with safe approximations
5445 of loop counts, and thus discard some data dependences that cannot
5449 omega_query_variable_signs (omega_pb pb
, int i
, int dd_lt
,
5450 int dd_eq
, int dd_gt
, int lower_bound
,
5451 int upper_bound
, bool *dist_known
, int *dist
)
5460 omega_query_variable (pb
, i
, &l
, &u
);
5461 query_coupled_variable (pb
, i
, &l
, &u
, &could_be_zero
, lower_bound
,
5480 *dist_known
= false;
5485 /* Initialize PB as an Omega problem with NVARS variables and NPROT
5486 safe variables. Safe variables are not eliminated during the
5487 Fourier-Motzkin elimination. Safe variables are all those
5488 variables that are placed at the beginning of the array of
5489 variables: P->var[0, ..., NPROT - 1]. */
5492 omega_alloc_problem (int nvars
, int nprot
)
5496 gcc_assert (nvars
<= OMEGA_MAX_VARS
);
5497 omega_initialize ();
5499 /* Allocate and initialize PB. */
5500 pb
= XCNEW (struct omega_pb
);
5501 pb
->var
= XCNEWVEC (int, OMEGA_MAX_VARS
+ 2);
5502 pb
->forwarding_address
= XCNEWVEC (int, OMEGA_MAX_VARS
+ 2);
5503 pb
->geqs
= omega_alloc_eqns (0, OMEGA_MAX_GEQS
);
5504 pb
->eqs
= omega_alloc_eqns (0, OMEGA_MAX_EQS
);
5505 pb
->subs
= omega_alloc_eqns (0, OMEGA_MAX_VARS
+ 1);
5507 pb
->hash_version
= hash_version
;
5508 pb
->num_vars
= nvars
;
5509 pb
->safe_vars
= nprot
;
5510 pb
->variables_initialized
= false;
5511 pb
->variables_freed
= false;
5518 /* Keeps the state of the initialization. */
5519 static bool omega_initialized
= false;
5521 /* Initialization of the Omega solver. */
5524 omega_initialize (void)
5528 if (omega_initialized
)
5532 next_key
= OMEGA_MAX_VARS
+ 1;
5533 packing
= XCNEWVEC (int, OMEGA_MAX_VARS
);
5534 fast_lookup
= XCNEWVEC (int, MAX_KEYS
* 2);
5535 fast_lookup_red
= XCNEWVEC (int, MAX_KEYS
* 2);
5536 hash_master
= omega_alloc_eqns (0, HASH_TABLE_SIZE
);
5538 for (i
= 0; i
< HASH_TABLE_SIZE
; i
++)
5539 hash_master
[i
].touched
= -1;
5541 sprintf (wild_name
[0], "1");
5542 sprintf (wild_name
[1], "a");
5543 sprintf (wild_name
[2], "b");
5544 sprintf (wild_name
[3], "c");
5545 sprintf (wild_name
[4], "d");
5546 sprintf (wild_name
[5], "e");
5547 sprintf (wild_name
[6], "f");
5548 sprintf (wild_name
[7], "g");
5549 sprintf (wild_name
[8], "h");
5550 sprintf (wild_name
[9], "i");
5551 sprintf (wild_name
[10], "j");
5552 sprintf (wild_name
[11], "k");
5553 sprintf (wild_name
[12], "l");
5554 sprintf (wild_name
[13], "m");
5555 sprintf (wild_name
[14], "n");
5556 sprintf (wild_name
[15], "o");
5557 sprintf (wild_name
[16], "p");
5558 sprintf (wild_name
[17], "q");
5559 sprintf (wild_name
[18], "r");
5560 sprintf (wild_name
[19], "s");
5561 sprintf (wild_name
[20], "t");
5562 sprintf (wild_name
[40 - 1], "alpha");
5563 sprintf (wild_name
[40 - 2], "beta");
5564 sprintf (wild_name
[40 - 3], "gamma");
5565 sprintf (wild_name
[40 - 4], "delta");
5566 sprintf (wild_name
[40 - 5], "tau");
5567 sprintf (wild_name
[40 - 6], "sigma");
5568 sprintf (wild_name
[40 - 7], "chi");
5569 sprintf (wild_name
[40 - 8], "omega");
5570 sprintf (wild_name
[40 - 9], "pi");
5571 sprintf (wild_name
[40 - 10], "ni");
5572 sprintf (wild_name
[40 - 11], "Alpha");
5573 sprintf (wild_name
[40 - 12], "Beta");
5574 sprintf (wild_name
[40 - 13], "Gamma");
5575 sprintf (wild_name
[40 - 14], "Delta");
5576 sprintf (wild_name
[40 - 15], "Tau");
5577 sprintf (wild_name
[40 - 16], "Sigma");
5578 sprintf (wild_name
[40 - 17], "Chi");
5579 sprintf (wild_name
[40 - 18], "Omega");
5580 sprintf (wild_name
[40 - 19], "xxx");
5582 omega_initialized
= true;