1 /* Source code for an implementation of the Omega test, an integer
2 programming algorithm for dependence analysis, by William Pugh,
3 appeared in Supercomputing '91 and CACM Aug 92.
5 This code has no license restrictions, and is considered public
8 Changes copyright (C) 2005, 2006, 2007, 2008, 2009,
9 2010 Free Software Foundation, Inc.
10 Contributed by Sebastian Pop <sebastian.pop@inria.fr>
12 This file is part of GCC.
14 GCC is free software; you can redistribute it and/or modify it under
15 the terms of the GNU General Public License as published by the Free
16 Software Foundation; either version 3, or (at your option) any later
19 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
20 WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 You should have received a copy of the GNU General Public License
25 along with GCC; see the file COPYING3. If not see
26 <http://www.gnu.org/licenses/>. */
28 /* For a detailed description, see "Constraint-Based Array Dependence
29 Analysis" William Pugh, David Wonnacott, TOPLAS'98 and David
31 ftp://ftp.cs.umd.edu/pub/omega/davewThesis/davewThesis.ps.gz
36 #include "coretypes.h"
40 #include "diagnostic-core.h"
41 #include "tree-pass.h"
44 /* When set to true, keep substitution variables. When set to false,
45 resurrect substitution variables (convert substitutions back to EQs). */
46 static bool omega_reduce_with_subs
= true;
48 /* When set to true, omega_simplify_problem checks for problem with no
49 solutions, calling verify_omega_pb. */
50 static bool omega_verify_simplification
= false;
52 /* When set to true, only produce a single simplified result. */
53 static bool omega_single_result
= false;
55 /* Set return_single_result to 1 when omega_single_result is true. */
56 static int return_single_result
= 0;
58 /* Hash table for equations generated by the solver. */
59 #define HASH_TABLE_SIZE PARAM_VALUE (PARAM_OMEGA_HASH_TABLE_SIZE)
60 #define MAX_KEYS PARAM_VALUE (PARAM_OMEGA_MAX_KEYS)
61 static eqn hash_master
;
63 static int hash_version
= 0;
65 /* Set to true for making the solver enter in approximation mode. */
66 static bool in_approximate_mode
= false;
68 /* When set to zero, the solver is allowed to add new equalities to
69 the problem to be solved. */
70 static int conservative
= 0;
72 /* Set to omega_true when the problem was successfully reduced, set to
73 omega_unknown when the solver is unable to determine an answer. */
74 static enum omega_result omega_found_reduction
;
76 /* Set to true when the solver is allowed to add omega_red equations. */
77 static bool create_color
= false;
79 /* Set to nonzero when the problem to be solved can be reduced. */
80 static int may_be_red
= 0;
82 /* When false, there should be no substitution equations in the
83 simplified problem. */
84 static int please_no_equalities_in_simplified_problems
= 0;
86 /* Variables names for pretty printing. */
87 static char wild_name
[200][40];
89 /* Pointer to the void problem. */
90 static omega_pb no_problem
= (omega_pb
) 0;
92 /* Pointer to the problem to be solved. */
93 static omega_pb original_problem
= (omega_pb
) 0;
96 /* Return the integer A divided by B. */
99 int_div (int a
, int b
)
104 return -((-a
+ b
- 1)/b
);
107 /* Return the integer A modulo B. */
110 int_mod (int a
, int b
)
112 return a
- b
* int_div (a
, b
);
115 /* For X and Y positive integers, return X multiplied by Y and check
116 that the result does not overflow. */
119 check_pos_mul (int x
, int y
)
122 gcc_assert ((INT_MAX
) / x
> y
);
127 /* Return X multiplied by Y and check that the result does not
131 check_mul (int x
, int y
)
136 return check_pos_mul (x
, y
);
138 return -check_pos_mul (x
, -y
);
141 return -check_pos_mul (-x
, y
);
143 return check_pos_mul (-x
, -y
);
146 /* Test whether equation E is red. */
149 omega_eqn_is_red (eqn e
, int desired_res
)
151 return (desired_res
== omega_simplify
&& e
->color
== omega_red
);
154 /* Return a string for VARIABLE. */
157 omega_var_to_str (int variable
)
159 if (0 <= variable
&& variable
<= 20)
160 return wild_name
[variable
];
162 if (-20 < variable
&& variable
< 0)
163 return wild_name
[40 + variable
];
165 /* Collapse all the entries that would have overflowed. */
166 return wild_name
[21];
169 /* Return a string for variable I in problem PB. */
172 omega_variable_to_str (omega_pb pb
, int i
)
174 return omega_var_to_str (pb
->var
[i
]);
177 /* Do nothing function: used for default initializations. */
180 omega_no_procedure (omega_pb pb ATTRIBUTE_UNUSED
)
184 void (*omega_when_reduced
) (omega_pb
) = omega_no_procedure
;
186 /* Compute the greatest common divisor of A and B. */
204 /* Print to FILE from PB equation E with all its coefficients
208 omega_print_term (FILE *file
, omega_pb pb
, eqn e
, int c
)
212 int n
= pb
->num_vars
;
215 for (i
= 1; i
<= n
; i
++)
216 if (c
* e
->coef
[i
] > 0)
221 if (c
* e
->coef
[i
] == 1)
222 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
224 fprintf (file
, "%d * %s", c
* e
->coef
[i
],
225 omega_variable_to_str (pb
, i
));
229 for (i
= 1; i
<= n
; i
++)
230 if (i
!= went_first
&& c
* e
->coef
[i
] != 0)
232 if (!first
&& c
* e
->coef
[i
] > 0)
233 fprintf (file
, " + ");
237 if (c
* e
->coef
[i
] == 1)
238 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
239 else if (c
* e
->coef
[i
] == -1)
240 fprintf (file
, " - %s", omega_variable_to_str (pb
, i
));
242 fprintf (file
, "%d * %s", c
* e
->coef
[i
],
243 omega_variable_to_str (pb
, i
));
246 if (!first
&& c
* e
->coef
[0] > 0)
247 fprintf (file
, " + ");
249 if (first
|| c
* e
->coef
[0] != 0)
250 fprintf (file
, "%d", c
* e
->coef
[0]);
253 /* Print to FILE the equation E of problem PB. */
256 omega_print_eqn (FILE *file
, omega_pb pb
, eqn e
, bool test
, int extra
)
259 int n
= pb
->num_vars
+ extra
;
260 bool is_lt
= test
&& e
->coef
[0] == -1;
268 else if (e
->key
!= 0)
269 fprintf (file
, "%d: ", e
->key
);
272 if (e
->color
== omega_red
)
277 for (i
= is_lt
? 1 : 0; i
<= n
; i
++)
281 fprintf (file
, " + ");
286 fprintf (file
, "%d", -e
->coef
[i
]);
287 else if (e
->coef
[i
] == -1)
288 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
290 fprintf (file
, "%d * %s", -e
->coef
[i
],
291 omega_variable_to_str (pb
, i
));
306 fprintf (file
, " = ");
308 fprintf (file
, " < ");
310 fprintf (file
, " <= ");
314 for (i
= 0; i
<= n
; i
++)
318 fprintf (file
, " + ");
323 fprintf (file
, "%d", e
->coef
[i
]);
324 else if (e
->coef
[i
] == 1)
325 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
327 fprintf (file
, "%d * %s", e
->coef
[i
],
328 omega_variable_to_str (pb
, i
));
334 if (e
->color
== omega_red
)
338 /* Print to FILE all the variables of problem PB. */
341 omega_print_vars (FILE *file
, omega_pb pb
)
345 fprintf (file
, "variables = ");
347 if (pb
->safe_vars
> 0)
348 fprintf (file
, "protected (");
350 for (i
= 1; i
<= pb
->num_vars
; i
++)
352 fprintf (file
, "%s", omega_variable_to_str (pb
, i
));
354 if (i
== pb
->safe_vars
)
357 if (i
< pb
->num_vars
)
358 fprintf (file
, ", ");
361 fprintf (file
, "\n");
364 /* Debug problem PB. */
367 debug_omega_problem (omega_pb pb
)
369 omega_print_problem (stderr
, pb
);
372 /* Print to FILE problem PB. */
375 omega_print_problem (FILE *file
, omega_pb pb
)
379 if (!pb
->variables_initialized
)
380 omega_initialize_variables (pb
);
382 omega_print_vars (file
, pb
);
384 for (e
= 0; e
< pb
->num_eqs
; e
++)
386 omega_print_eq (file
, pb
, &pb
->eqs
[e
]);
387 fprintf (file
, "\n");
390 fprintf (file
, "Done with EQ\n");
392 for (e
= 0; e
< pb
->num_geqs
; e
++)
394 omega_print_geq (file
, pb
, &pb
->geqs
[e
]);
395 fprintf (file
, "\n");
398 fprintf (file
, "Done with GEQ\n");
400 for (e
= 0; e
< pb
->num_subs
; e
++)
402 eqn eq
= &pb
->subs
[e
];
404 if (eq
->color
== omega_red
)
408 fprintf (file
, "%s := ", omega_var_to_str (eq
->key
));
410 fprintf (file
, "#%d := ", eq
->key
);
412 omega_print_term (file
, pb
, eq
, 1);
414 if (eq
->color
== omega_red
)
417 fprintf (file
, "\n");
421 /* Return the number of equations in PB tagged omega_red. */
424 omega_count_red_equations (omega_pb pb
)
429 for (e
= 0; e
< pb
->num_eqs
; e
++)
430 if (pb
->eqs
[e
].color
== omega_red
)
432 for (i
= pb
->num_vars
; i
> 0; i
--)
433 if (pb
->geqs
[e
].coef
[i
])
436 if (i
== 0 && pb
->geqs
[e
].coef
[0] == 1)
442 for (e
= 0; e
< pb
->num_geqs
; e
++)
443 if (pb
->geqs
[e
].color
== omega_red
)
446 for (e
= 0; e
< pb
->num_subs
; e
++)
447 if (pb
->subs
[e
].color
== omega_red
)
453 /* Print to FILE all the equations in PB that are tagged omega_red. */
456 omega_print_red_equations (FILE *file
, omega_pb pb
)
460 if (!pb
->variables_initialized
)
461 omega_initialize_variables (pb
);
463 omega_print_vars (file
, pb
);
465 for (e
= 0; e
< pb
->num_eqs
; e
++)
466 if (pb
->eqs
[e
].color
== omega_red
)
468 omega_print_eq (file
, pb
, &pb
->eqs
[e
]);
469 fprintf (file
, "\n");
472 for (e
= 0; e
< pb
->num_geqs
; e
++)
473 if (pb
->geqs
[e
].color
== omega_red
)
475 omega_print_geq (file
, pb
, &pb
->geqs
[e
]);
476 fprintf (file
, "\n");
479 for (e
= 0; e
< pb
->num_subs
; e
++)
480 if (pb
->subs
[e
].color
== omega_red
)
482 eqn eq
= &pb
->subs
[e
];
486 fprintf (file
, "%s := ", omega_var_to_str (eq
->key
));
488 fprintf (file
, "#%d := ", eq
->key
);
490 omega_print_term (file
, pb
, eq
, 1);
491 fprintf (file
, "]\n");
495 /* Pretty print PB to FILE. */
498 omega_pretty_print_problem (FILE *file
, omega_pb pb
)
500 int e
, v
, v1
, v2
, v3
, t
;
501 bool *live
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
502 int stuffPrinted
= 0;
507 } partial_order_type
;
509 partial_order_type
**po
= XNEWVEC (partial_order_type
*,
510 OMEGA_MAX_VARS
* OMEGA_MAX_VARS
);
511 int **po_eq
= XNEWVEC (int *, OMEGA_MAX_VARS
* OMEGA_MAX_VARS
);
512 int *last_links
= XNEWVEC (int, OMEGA_MAX_VARS
);
513 int *first_links
= XNEWVEC (int, OMEGA_MAX_VARS
);
514 int *chain_length
= XNEWVEC (int, OMEGA_MAX_VARS
);
515 int *chain
= XNEWVEC (int, OMEGA_MAX_VARS
);
519 if (!pb
->variables_initialized
)
520 omega_initialize_variables (pb
);
522 if (pb
->num_vars
> 0)
524 omega_eliminate_redundant (pb
, false);
526 for (e
= 0; e
< pb
->num_eqs
; e
++)
529 fprintf (file
, "; ");
532 omega_print_eq (file
, pb
, &pb
->eqs
[e
]);
535 for (e
= 0; e
< pb
->num_geqs
; e
++)
540 for (v
= 1; v
<= pb
->num_vars
; v
++)
542 last_links
[v
] = first_links
[v
] = 0;
545 for (v2
= 1; v2
<= pb
->num_vars
; v2
++)
549 for (e
= 0; e
< pb
->num_geqs
; e
++)
552 for (v
= 1; v
<= pb
->num_vars
; v
++)
553 if (pb
->geqs
[e
].coef
[v
] == 1)
555 else if (pb
->geqs
[e
].coef
[v
] == -1)
560 while (v1
> 0 && pb
->geqs
[e
].coef
[v1
] == 0)
565 while (v2
> 0 && pb
->geqs
[e
].coef
[v2
] == 0)
570 while (v3
> 0 && pb
->geqs
[e
].coef
[v3
] == 0)
573 if (pb
->geqs
[e
].coef
[0] > 0 || pb
->geqs
[e
].coef
[0] < -1
575 || pb
->geqs
[e
].coef
[v1
] * pb
->geqs
[e
].coef
[v2
] != -1)
577 /* Not a partial order relation. */
581 if (pb
->geqs
[e
].coef
[v1
] == 1)
588 /* Relation is v1 <= v2 or v1 < v2. */
589 po
[v1
][v2
] = ((pb
->geqs
[e
].coef
[0] == 0) ? le
: lt
);
593 for (v
= 1; v
<= pb
->num_vars
; v
++)
594 chain_length
[v
] = last_links
[v
];
596 /* Just in case pb->num_vars <= 0. */
598 for (t
= 0; t
< pb
->num_vars
; t
++)
602 for (v1
= 1; v1
<= pb
->num_vars
; v1
++)
603 for (v2
= 1; v2
<= pb
->num_vars
; v2
++)
604 if (po
[v1
][v2
] != none
&&
605 chain_length
[v1
] <= chain_length
[v2
])
607 chain_length
[v1
] = chain_length
[v2
] + 1;
612 /* Caught in cycle. */
613 gcc_assert (!change
);
615 for (v1
= 1; v1
<= pb
->num_vars
; v1
++)
616 if (chain_length
[v1
] == 0)
621 for (v1
= 2; v1
<= pb
->num_vars
; v1
++)
622 if (chain_length
[v1
] + first_links
[v1
] >
623 chain_length
[v
] + first_links
[v
])
626 if (chain_length
[v
] + first_links
[v
] == 0)
630 fprintf (file
, "; ");
634 /* Chain starts at v. */
639 for (e
= 0; e
< pb
->num_geqs
; e
++)
640 if (live
[e
] && pb
->geqs
[e
].coef
[v
] == 1)
643 fprintf (file
, ", ");
645 tmp
= pb
->geqs
[e
].coef
[v
];
646 pb
->geqs
[e
].coef
[v
] = 0;
647 omega_print_term (file
, pb
, &pb
->geqs
[e
], -1);
648 pb
->geqs
[e
].coef
[v
] = tmp
;
654 fprintf (file
, " <= ");
663 for (v2
= 1; v2
<= pb
->num_vars
; v2
++)
664 if (po
[v
][v2
] && chain_length
[v
] == 1 + chain_length
[v2
])
667 if (v2
> pb
->num_vars
)
674 fprintf (file
, "%s", omega_variable_to_str (pb
, chain
[0]));
676 for (multiprint
= false, i
= 1; i
< m
; i
++)
682 fprintf (file
, " <= ");
684 fprintf (file
, " < ");
686 fprintf (file
, "%s", omega_variable_to_str (pb
, v2
));
687 live
[po_eq
[v
][v2
]] = false;
689 if (!multiprint
&& i
< m
- 1)
690 for (v3
= 1; v3
<= pb
->num_vars
; v3
++)
692 if (v
== v3
|| v2
== v3
693 || po
[v
][v2
] != po
[v
][v3
]
694 || po
[v2
][chain
[i
+ 1]] != po
[v3
][chain
[i
+ 1]])
697 fprintf (file
, ",%s", omega_variable_to_str (pb
, v3
));
698 live
[po_eq
[v
][v3
]] = false;
699 live
[po_eq
[v3
][chain
[i
+ 1]]] = false;
707 /* Print last_links. */
712 for (e
= 0; e
< pb
->num_geqs
; e
++)
713 if (live
[e
] && pb
->geqs
[e
].coef
[v
] == -1)
716 fprintf (file
, ", ");
718 fprintf (file
, " <= ");
720 tmp
= pb
->geqs
[e
].coef
[v
];
721 pb
->geqs
[e
].coef
[v
] = 0;
722 omega_print_term (file
, pb
, &pb
->geqs
[e
], 1);
723 pb
->geqs
[e
].coef
[v
] = tmp
;
730 for (e
= 0; e
< pb
->num_geqs
; e
++)
734 fprintf (file
, "; ");
736 omega_print_geq (file
, pb
, &pb
->geqs
[e
]);
739 for (e
= 0; e
< pb
->num_subs
; e
++)
741 eqn eq
= &pb
->subs
[e
];
744 fprintf (file
, "; ");
749 fprintf (file
, "%s := ", omega_var_to_str (eq
->key
));
751 fprintf (file
, "#%d := ", eq
->key
);
753 omega_print_term (file
, pb
, eq
, 1);
766 /* Assign to variable I in PB the next wildcard name. The name of a
767 wildcard is a negative number. */
768 static int next_wild_card
= 0;
771 omega_name_wild_card (omega_pb pb
, int i
)
774 if (next_wild_card
< -PARAM_VALUE (PARAM_OMEGA_MAX_WILD_CARDS
))
776 pb
->var
[i
] = next_wild_card
;
779 /* Return the index of the last protected (or safe) variable in PB,
780 after having added a new wildcard variable. */
783 omega_add_new_wild_card (omega_pb pb
)
786 int i
= ++pb
->safe_vars
;
789 /* Make a free place in the protected (safe) variables, by moving
790 the non protected variable pointed by "I" at the end, ie. at
791 offset pb->num_vars. */
792 if (pb
->num_vars
!= i
)
794 /* Move "I" for all the inequalities. */
795 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
797 if (pb
->geqs
[e
].coef
[i
])
798 pb
->geqs
[e
].touched
= 1;
800 pb
->geqs
[e
].coef
[pb
->num_vars
] = pb
->geqs
[e
].coef
[i
];
803 /* Move "I" for all the equalities. */
804 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
805 pb
->eqs
[e
].coef
[pb
->num_vars
] = pb
->eqs
[e
].coef
[i
];
807 /* Move "I" for all the substitutions. */
808 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
809 pb
->subs
[e
].coef
[pb
->num_vars
] = pb
->subs
[e
].coef
[i
];
811 /* Move the identifier. */
812 pb
->var
[pb
->num_vars
] = pb
->var
[i
];
815 /* Initialize at zero all the coefficients */
816 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
817 pb
->geqs
[e
].coef
[i
] = 0;
819 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
820 pb
->eqs
[e
].coef
[i
] = 0;
822 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
823 pb
->subs
[e
].coef
[i
] = 0;
825 /* And give it a name. */
826 omega_name_wild_card (pb
, i
);
830 /* Delete inequality E from problem PB that has N_VARS variables. */
833 omega_delete_geq (omega_pb pb
, int e
, int n_vars
)
835 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
837 fprintf (dump_file
, "Deleting %d (last:%d): ", e
, pb
->num_geqs
- 1);
838 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
839 fprintf (dump_file
, "\n");
842 if (e
< pb
->num_geqs
- 1)
843 omega_copy_eqn (&pb
->geqs
[e
], &pb
->geqs
[pb
->num_geqs
- 1], n_vars
);
848 /* Delete extra inequality E from problem PB that has N_VARS
852 omega_delete_geq_extra (omega_pb pb
, int e
, int n_vars
)
854 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
856 fprintf (dump_file
, "Deleting %d: ",e
);
857 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[e
]);
858 fprintf (dump_file
, "\n");
861 if (e
< pb
->num_geqs
- 1)
862 omega_copy_eqn (&pb
->geqs
[e
], &pb
->geqs
[pb
->num_geqs
- 1], n_vars
);
868 /* Remove variable I from problem PB. */
871 omega_delete_variable (omega_pb pb
, int i
)
873 int n_vars
= pb
->num_vars
;
876 if (omega_safe_var_p (pb
, i
))
878 int j
= pb
->safe_vars
;
880 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
882 pb
->geqs
[e
].touched
= 1;
883 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[j
];
884 pb
->geqs
[e
].coef
[j
] = pb
->geqs
[e
].coef
[n_vars
];
887 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
889 pb
->eqs
[e
].coef
[i
] = pb
->eqs
[e
].coef
[j
];
890 pb
->eqs
[e
].coef
[j
] = pb
->eqs
[e
].coef
[n_vars
];
893 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
895 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[j
];
896 pb
->subs
[e
].coef
[j
] = pb
->subs
[e
].coef
[n_vars
];
899 pb
->var
[i
] = pb
->var
[j
];
900 pb
->var
[j
] = pb
->var
[n_vars
];
904 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
905 if (pb
->geqs
[e
].coef
[n_vars
])
907 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[n_vars
];
908 pb
->geqs
[e
].touched
= 1;
911 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
912 pb
->eqs
[e
].coef
[i
] = pb
->eqs
[e
].coef
[n_vars
];
914 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
915 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[n_vars
];
917 pb
->var
[i
] = pb
->var
[n_vars
];
920 if (omega_safe_var_p (pb
, i
))
926 /* Because the coefficients of an equation are sparse, PACKING records
927 indices for non null coefficients. */
930 /* Set up the coefficients of PACKING, following the coefficients of
931 equation EQN that has NUM_VARS variables. */
934 setup_packing (eqn eqn
, int num_vars
)
939 for (k
= num_vars
; k
>= 0; k
--)
946 /* Computes a linear combination of EQ and SUB at VAR with coefficient
947 C, such that EQ->coef[VAR] is set to 0. TOP_VAR is the number of
948 non null indices of SUB stored in PACKING. */
951 omega_substitute_red_1 (eqn eq
, eqn sub
, int var
, int c
, bool *found_black
,
954 if (eq
->coef
[var
] != 0)
956 if (eq
->color
== omega_black
)
960 int j
, k
= eq
->coef
[var
];
964 for (j
= top_var
; j
>= 0; j
--)
965 eq
->coef
[packing
[j
]] -= sub
->coef
[packing
[j
]] * k
* c
;
970 /* Substitute in PB variable VAR with "C * SUB". */
973 omega_substitute_red (omega_pb pb
, eqn sub
, int var
, int c
, bool *found_black
)
975 int e
, top_var
= setup_packing (sub
, pb
->num_vars
);
977 *found_black
= false;
979 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
981 if (sub
->color
== omega_red
)
982 fprintf (dump_file
, "[");
984 fprintf (dump_file
, "substituting using %s := ",
985 omega_variable_to_str (pb
, var
));
986 omega_print_term (dump_file
, pb
, sub
, -c
);
988 if (sub
->color
== omega_red
)
989 fprintf (dump_file
, "]");
991 fprintf (dump_file
, "\n");
992 omega_print_vars (dump_file
, pb
);
995 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
997 eqn eqn
= &(pb
->eqs
[e
]);
999 omega_substitute_red_1 (eqn
, sub
, var
, c
, found_black
, top_var
);
1001 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1003 omega_print_eq (dump_file
, pb
, eqn
);
1004 fprintf (dump_file
, "\n");
1008 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1010 eqn eqn
= &(pb
->geqs
[e
]);
1012 omega_substitute_red_1 (eqn
, sub
, var
, c
, found_black
, top_var
);
1014 if (eqn
->coef
[var
] && eqn
->color
== omega_red
)
1017 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1019 omega_print_geq (dump_file
, pb
, eqn
);
1020 fprintf (dump_file
, "\n");
1024 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1026 eqn eqn
= &(pb
->subs
[e
]);
1028 omega_substitute_red_1 (eqn
, sub
, var
, c
, found_black
, top_var
);
1030 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1032 fprintf (dump_file
, "%s := ", omega_var_to_str (eqn
->key
));
1033 omega_print_term (dump_file
, pb
, eqn
, 1);
1034 fprintf (dump_file
, "\n");
1038 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1039 fprintf (dump_file
, "---\n\n");
1041 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1042 *found_black
= true;
1045 /* Substitute in PB variable VAR with "C * SUB". */
1048 omega_substitute (omega_pb pb
, eqn sub
, int var
, int c
)
1051 int top_var
= setup_packing (sub
, pb
->num_vars
);
1053 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1055 fprintf (dump_file
, "substituting using %s := ",
1056 omega_variable_to_str (pb
, var
));
1057 omega_print_term (dump_file
, pb
, sub
, -c
);
1058 fprintf (dump_file
, "\n");
1059 omega_print_vars (dump_file
, pb
);
1064 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1065 pb
->eqs
[e
].coef
[var
] = 0;
1067 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1068 if (pb
->geqs
[e
].coef
[var
] != 0)
1070 pb
->geqs
[e
].touched
= 1;
1071 pb
->geqs
[e
].coef
[var
] = 0;
1074 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1075 pb
->subs
[e
].coef
[var
] = 0;
1077 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1080 eqn eqn
= &(pb
->subs
[pb
->num_subs
++]);
1082 for (k
= pb
->num_vars
; k
>= 0; k
--)
1085 eqn
->key
= pb
->var
[var
];
1086 eqn
->color
= omega_black
;
1089 else if (top_var
== 0 && packing
[0] == 0)
1091 c
= -sub
->coef
[0] * c
;
1093 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1095 pb
->eqs
[e
].coef
[0] += pb
->eqs
[e
].coef
[var
] * c
;
1096 pb
->eqs
[e
].coef
[var
] = 0;
1099 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1100 if (pb
->geqs
[e
].coef
[var
] != 0)
1102 pb
->geqs
[e
].coef
[0] += pb
->geqs
[e
].coef
[var
] * c
;
1103 pb
->geqs
[e
].coef
[var
] = 0;
1104 pb
->geqs
[e
].touched
= 1;
1107 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1109 pb
->subs
[e
].coef
[0] += pb
->subs
[e
].coef
[var
] * c
;
1110 pb
->subs
[e
].coef
[var
] = 0;
1113 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1116 eqn eqn
= &(pb
->subs
[pb
->num_subs
++]);
1118 for (k
= pb
->num_vars
; k
>= 1; k
--)
1122 eqn
->key
= pb
->var
[var
];
1123 eqn
->color
= omega_black
;
1126 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1128 fprintf (dump_file
, "---\n\n");
1129 omega_print_problem (dump_file
, pb
);
1130 fprintf (dump_file
, "===\n\n");
1135 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1137 eqn eqn
= &(pb
->eqs
[e
]);
1138 int k
= eqn
->coef
[var
];
1145 for (j
= top_var
; j
>= 0; j
--)
1148 eqn
->coef
[j0
] -= sub
->coef
[j0
] * k
;
1152 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1154 omega_print_eq (dump_file
, pb
, eqn
);
1155 fprintf (dump_file
, "\n");
1159 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1161 eqn eqn
= &(pb
->geqs
[e
]);
1162 int k
= eqn
->coef
[var
];
1170 for (j
= top_var
; j
>= 0; j
--)
1173 eqn
->coef
[j0
] -= sub
->coef
[j0
] * k
;
1177 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1179 omega_print_geq (dump_file
, pb
, eqn
);
1180 fprintf (dump_file
, "\n");
1184 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1186 eqn eqn
= &(pb
->subs
[e
]);
1187 int k
= eqn
->coef
[var
];
1194 for (j
= top_var
; j
>= 0; j
--)
1197 eqn
->coef
[j0
] -= sub
->coef
[j0
] * k
;
1201 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1203 fprintf (dump_file
, "%s := ", omega_var_to_str (eqn
->key
));
1204 omega_print_term (dump_file
, pb
, eqn
, 1);
1205 fprintf (dump_file
, "\n");
1209 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1211 fprintf (dump_file
, "---\n\n");
1212 omega_print_problem (dump_file
, pb
);
1213 fprintf (dump_file
, "===\n\n");
1216 if (omega_safe_var_p (pb
, var
) && !omega_wildcard_p (pb
, var
))
1219 eqn eqn
= &(pb
->subs
[pb
->num_subs
++]);
1222 for (k
= pb
->num_vars
; k
>= 0; k
--)
1223 eqn
->coef
[k
] = c
* (sub
->coef
[k
]);
1225 eqn
->key
= pb
->var
[var
];
1226 eqn
->color
= sub
->color
;
1231 /* Solve e = factor alpha for x_j and substitute. */
1234 omega_do_mod (omega_pb pb
, int factor
, int e
, int j
)
1237 eqn eq
= omega_alloc_eqns (0, 1);
1239 bool kill_j
= false;
1241 omega_copy_eqn (eq
, &pb
->eqs
[e
], pb
->num_vars
);
1243 for (k
= pb
->num_vars
; k
>= 0; k
--)
1245 eq
->coef
[k
] = int_mod (eq
->coef
[k
], factor
);
1247 if (2 * eq
->coef
[k
] >= factor
)
1248 eq
->coef
[k
] -= factor
;
1251 nfactor
= eq
->coef
[j
];
1253 if (omega_safe_var_p (pb
, j
) && !omega_wildcard_p (pb
, j
))
1255 i
= omega_add_new_wild_card (pb
);
1256 eq
->coef
[pb
->num_vars
] = eq
->coef
[i
];
1258 eq
->coef
[i
] = -factor
;
1263 eq
->coef
[j
] = -factor
;
1264 if (!omega_wildcard_p (pb
, j
))
1265 omega_name_wild_card (pb
, j
);
1268 omega_substitute (pb
, eq
, j
, nfactor
);
1270 for (k
= pb
->num_vars
; k
>= 0; k
--)
1271 pb
->eqs
[e
].coef
[k
] = pb
->eqs
[e
].coef
[k
] / factor
;
1274 omega_delete_variable (pb
, j
);
1276 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1278 fprintf (dump_file
, "Mod-ing and normalizing produces:\n");
1279 omega_print_problem (dump_file
, pb
);
1282 omega_free_eqns (eq
, 1);
1285 /* Multiplies by -1 inequality E. */
1288 omega_negate_geq (omega_pb pb
, int e
)
1292 for (i
= pb
->num_vars
; i
>= 0; i
--)
1293 pb
->geqs
[e
].coef
[i
] *= -1;
1295 pb
->geqs
[e
].coef
[0]--;
1296 pb
->geqs
[e
].touched
= 1;
1299 /* Returns OMEGA_TRUE when problem PB has a solution. */
1301 static enum omega_result
1302 verify_omega_pb (omega_pb pb
)
1304 enum omega_result result
;
1306 bool any_color
= false;
1307 omega_pb tmp_problem
= XNEW (struct omega_pb_d
);
1309 omega_copy_problem (tmp_problem
, pb
);
1310 tmp_problem
->safe_vars
= 0;
1311 tmp_problem
->num_subs
= 0;
1313 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1314 if (pb
->geqs
[e
].color
== omega_red
)
1320 if (please_no_equalities_in_simplified_problems
)
1324 original_problem
= no_problem
;
1326 original_problem
= pb
;
1328 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1330 fprintf (dump_file
, "verifying problem");
1333 fprintf (dump_file
, " (color mode)");
1335 fprintf (dump_file
, " :\n");
1336 omega_print_problem (dump_file
, pb
);
1339 result
= omega_solve_problem (tmp_problem
, omega_unknown
);
1340 original_problem
= no_problem
;
1343 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1345 if (result
!= omega_false
)
1346 fprintf (dump_file
, "verified problem\n");
1348 fprintf (dump_file
, "disproved problem\n");
1349 omega_print_problem (dump_file
, pb
);
1355 /* Add a new equality to problem PB at last position E. */
1358 adding_equality_constraint (omega_pb pb
, int e
)
1360 if (original_problem
!= no_problem
1361 && original_problem
!= pb
1365 int e2
= original_problem
->num_eqs
++;
1367 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1369 "adding equality constraint %d to outer problem\n", e2
);
1370 omega_init_eqn_zero (&original_problem
->eqs
[e2
],
1371 original_problem
->num_vars
);
1373 for (i
= pb
->num_vars
; i
>= 1; i
--)
1375 for (j
= original_problem
->num_vars
; j
>= 1; j
--)
1376 if (original_problem
->var
[j
] == pb
->var
[i
])
1381 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1382 fprintf (dump_file
, "retracting\n");
1383 original_problem
->num_eqs
--;
1386 original_problem
->eqs
[e2
].coef
[j
] = pb
->eqs
[e
].coef
[i
];
1389 original_problem
->eqs
[e2
].coef
[0] = pb
->eqs
[e
].coef
[0];
1391 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1392 omega_print_problem (dump_file
, original_problem
);
1396 static int *fast_lookup
;
1397 static int *fast_lookup_red
;
1401 normalize_uncoupled
,
1403 } normalize_return_type
;
1405 /* Normalizes PB by removing redundant constraints. Returns
1406 normalize_false when the constraints system has no solution,
1407 otherwise returns normalize_coupled or normalize_uncoupled. */
1409 static normalize_return_type
1410 normalize_omega_problem (omega_pb pb
)
1412 int e
, i
, j
, k
, n_vars
;
1413 int coupled_subscripts
= 0;
1415 n_vars
= pb
->num_vars
;
1417 for (e
= 0; e
< pb
->num_geqs
; e
++)
1419 if (!pb
->geqs
[e
].touched
)
1421 if (!single_var_geq (&pb
->geqs
[e
], n_vars
))
1422 coupled_subscripts
= 1;
1426 int g
, top_var
, i0
, hashCode
;
1427 int *p
= &packing
[0];
1429 for (k
= 1; k
<= n_vars
; k
++)
1430 if (pb
->geqs
[e
].coef
[k
])
1433 top_var
= (p
- &packing
[0]) - 1;
1437 if (pb
->geqs
[e
].coef
[0] < 0)
1439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1441 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1442 fprintf (dump_file
, "\nequations have no solution \n");
1444 return normalize_false
;
1447 omega_delete_geq (pb
, e
, n_vars
);
1451 else if (top_var
== 0)
1453 int singlevar
= packing
[0];
1455 g
= pb
->geqs
[e
].coef
[singlevar
];
1459 pb
->geqs
[e
].coef
[singlevar
] = 1;
1460 pb
->geqs
[e
].key
= singlevar
;
1465 pb
->geqs
[e
].coef
[singlevar
] = -1;
1466 pb
->geqs
[e
].key
= -singlevar
;
1470 pb
->geqs
[e
].coef
[0] = int_div (pb
->geqs
[e
].coef
[0], g
);
1475 int hash_key_multiplier
= 31;
1477 coupled_subscripts
= 1;
1480 g
= pb
->geqs
[e
].coef
[i
];
1481 hashCode
= g
* (i
+ 3);
1486 for (; i0
>= 0; i0
--)
1491 x
= pb
->geqs
[e
].coef
[i
];
1492 hashCode
= hashCode
* hash_key_multiplier
* (i
+ 3) + x
;
1507 for (; i0
>= 0; i0
--)
1511 x
= pb
->geqs
[e
].coef
[i
];
1512 hashCode
= hashCode
* hash_key_multiplier
* (i
+ 3) + x
;
1517 pb
->geqs
[e
].coef
[0] = int_div (pb
->geqs
[e
].coef
[0], g
);
1520 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[i
] / g
;
1521 hashCode
= pb
->geqs
[e
].coef
[i
] * (i
+ 3);
1523 for (; i0
>= 0; i0
--)
1526 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[i
] / g
;
1527 hashCode
= hashCode
* hash_key_multiplier
* (i
+ 3)
1528 + pb
->geqs
[e
].coef
[i
];
1532 g2
= abs (hashCode
);
1534 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1536 fprintf (dump_file
, "Hash code = %d, eqn = ", hashCode
);
1537 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1538 fprintf (dump_file
, "\n");
1541 j
= g2
% HASH_TABLE_SIZE
;
1544 eqn proto
= &(hash_master
[j
]);
1546 if (proto
->touched
== g2
)
1548 if (proto
->coef
[0] == top_var
)
1551 for (i0
= top_var
; i0
>= 0; i0
--)
1555 if (pb
->geqs
[e
].coef
[i
] != proto
->coef
[i
])
1559 for (i0
= top_var
; i0
>= 0; i0
--)
1563 if (pb
->geqs
[e
].coef
[i
] != -proto
->coef
[i
])
1570 pb
->geqs
[e
].key
= proto
->key
;
1572 pb
->geqs
[e
].key
= -proto
->key
;
1577 else if (proto
->touched
< 0)
1579 omega_init_eqn_zero (proto
, pb
->num_vars
);
1581 for (i0
= top_var
; i0
>= 0; i0
--)
1584 proto
->coef
[i
] = pb
->geqs
[e
].coef
[i
];
1587 for (i0
= top_var
; i0
>= 0; i0
--)
1590 proto
->coef
[i
] = -pb
->geqs
[e
].coef
[i
];
1593 proto
->coef
[0] = top_var
;
1594 proto
->touched
= g2
;
1596 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1597 fprintf (dump_file
, " constraint key = %d\n",
1600 proto
->key
= next_key
++;
1602 /* Too many hash keys generated. */
1603 gcc_assert (proto
->key
<= MAX_KEYS
);
1606 pb
->geqs
[e
].key
= proto
->key
;
1608 pb
->geqs
[e
].key
= -proto
->key
;
1613 j
= (j
+ 1) % HASH_TABLE_SIZE
;
1617 pb
->geqs
[e
].touched
= 0;
1621 int eKey
= pb
->geqs
[e
].key
;
1625 int cTerm
= pb
->geqs
[e
].coef
[0];
1626 e2
= fast_lookup
[MAX_KEYS
- eKey
];
1628 if (e2
< e
&& pb
->geqs
[e2
].key
== -eKey
1629 && pb
->geqs
[e2
].color
== omega_black
)
1631 if (pb
->geqs
[e2
].coef
[0] < -cTerm
)
1633 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1635 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1636 fprintf (dump_file
, "\n");
1637 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e2
]);
1639 "\nequations have no solution \n");
1641 return normalize_false
;
1644 if (pb
->geqs
[e2
].coef
[0] == -cTerm
1646 || pb
->geqs
[e
].color
== omega_black
))
1648 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
], &pb
->geqs
[e
],
1650 if (pb
->geqs
[e
].color
== omega_black
)
1651 adding_equality_constraint (pb
, pb
->num_eqs
);
1653 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
1657 e2
= fast_lookup_red
[MAX_KEYS
- eKey
];
1659 if (e2
< e
&& pb
->geqs
[e2
].key
== -eKey
1660 && pb
->geqs
[e2
].color
== omega_red
)
1662 if (pb
->geqs
[e2
].coef
[0] < -cTerm
)
1664 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1666 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
1667 fprintf (dump_file
, "\n");
1668 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e2
]);
1670 "\nequations have no solution \n");
1672 return normalize_false
;
1675 if (pb
->geqs
[e2
].coef
[0] == -cTerm
&& create_color
)
1677 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
], &pb
->geqs
[e
],
1679 pb
->eqs
[pb
->num_eqs
].color
= omega_red
;
1681 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
1685 e2
= fast_lookup
[MAX_KEYS
+ eKey
];
1687 if (e2
< e
&& pb
->geqs
[e2
].key
== eKey
1688 && pb
->geqs
[e2
].color
== omega_black
)
1690 if (pb
->geqs
[e2
].coef
[0] > cTerm
)
1692 if (pb
->geqs
[e
].color
== omega_black
)
1694 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1697 "Removing Redundant Equation: ");
1698 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1699 fprintf (dump_file
, "\n");
1701 "[a] Made Redundant by: ");
1702 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1703 fprintf (dump_file
, "\n");
1705 pb
->geqs
[e2
].coef
[0] = cTerm
;
1706 omega_delete_geq (pb
, e
, n_vars
);
1713 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1715 fprintf (dump_file
, "Removing Redundant Equation: ");
1716 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1717 fprintf (dump_file
, "\n");
1718 fprintf (dump_file
, "[b] Made Redundant by: ");
1719 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1720 fprintf (dump_file
, "\n");
1722 omega_delete_geq (pb
, e
, n_vars
);
1728 e2
= fast_lookup_red
[MAX_KEYS
+ eKey
];
1730 if (e2
< e
&& pb
->geqs
[e2
].key
== eKey
1731 && pb
->geqs
[e2
].color
== omega_red
)
1733 if (pb
->geqs
[e2
].coef
[0] >= cTerm
)
1735 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1737 fprintf (dump_file
, "Removing Redundant Equation: ");
1738 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1739 fprintf (dump_file
, "\n");
1740 fprintf (dump_file
, "[c] Made Redundant by: ");
1741 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1742 fprintf (dump_file
, "\n");
1744 pb
->geqs
[e2
].coef
[0] = cTerm
;
1745 pb
->geqs
[e2
].color
= pb
->geqs
[e
].color
;
1747 else if (pb
->geqs
[e
].color
== omega_red
)
1749 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1751 fprintf (dump_file
, "Removing Redundant Equation: ");
1752 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
1753 fprintf (dump_file
, "\n");
1754 fprintf (dump_file
, "[d] Made Redundant by: ");
1755 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
1756 fprintf (dump_file
, "\n");
1759 omega_delete_geq (pb
, e
, n_vars
);
1766 if (pb
->geqs
[e
].color
== omega_red
)
1767 fast_lookup_red
[MAX_KEYS
+ eKey
] = e
;
1769 fast_lookup
[MAX_KEYS
+ eKey
] = e
;
1773 create_color
= false;
1774 return coupled_subscripts
? normalize_coupled
: normalize_uncoupled
;
1777 /* Divide the coefficients of EQN by their gcd. N_VARS is the number
1778 of variables in EQN. */
1781 divide_eqn_by_gcd (eqn eqn
, int n_vars
)
1785 for (var
= n_vars
; var
>= 0; var
--)
1786 g
= gcd (abs (eqn
->coef
[var
]), g
);
1789 for (var
= n_vars
; var
>= 0; var
--)
1790 eqn
->coef
[var
] = eqn
->coef
[var
] / g
;
1793 /* Rewrite some non-safe variables in function of protected
1794 wildcard variables. */
1797 cleanout_wildcards (omega_pb pb
)
1800 int n_vars
= pb
->num_vars
;
1801 bool renormalize
= false;
1803 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1804 for (i
= n_vars
; !omega_safe_var_p (pb
, i
); i
--)
1805 if (pb
->eqs
[e
].coef
[i
] != 0)
1807 /* i is the last nonzero non-safe variable. */
1809 for (j
= i
- 1; !omega_safe_var_p (pb
, j
); j
--)
1810 if (pb
->eqs
[e
].coef
[j
] != 0)
1813 /* j is the next nonzero non-safe variable, or points
1814 to a safe variable: it is then a wildcard variable. */
1817 if (omega_safe_var_p (pb
, j
))
1819 eqn sub
= &(pb
->eqs
[e
]);
1820 int c
= pb
->eqs
[e
].coef
[i
];
1824 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1827 "Found a single wild card equality: ");
1828 omega_print_eq (dump_file
, pb
, &pb
->eqs
[e
]);
1829 fprintf (dump_file
, "\n");
1830 omega_print_problem (dump_file
, pb
);
1833 for (e2
= pb
->num_eqs
- 1; e2
>= 0; e2
--)
1834 if (e
!= e2
&& pb
->eqs
[e2
].coef
[i
]
1835 && (pb
->eqs
[e2
].color
== omega_red
1836 || (pb
->eqs
[e2
].color
== omega_black
1837 && pb
->eqs
[e
].color
== omega_black
)))
1839 eqn eqn
= &(pb
->eqs
[e2
]);
1842 for (var
= n_vars
; var
>= 0; var
--)
1843 eqn
->coef
[var
] *= a
;
1847 for (var
= n_vars
; var
>= 0; var
--)
1848 eqn
->coef
[var
] -= sub
->coef
[var
] * k
/ c
;
1851 divide_eqn_by_gcd (eqn
, n_vars
);
1854 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
1855 if (pb
->geqs
[e2
].coef
[i
]
1856 && (pb
->geqs
[e2
].color
== omega_red
1857 || (pb
->eqs
[e
].color
== omega_black
1858 && pb
->geqs
[e2
].color
== omega_black
)))
1860 eqn eqn
= &(pb
->geqs
[e2
]);
1863 for (var
= n_vars
; var
>= 0; var
--)
1864 eqn
->coef
[var
] *= a
;
1868 for (var
= n_vars
; var
>= 0; var
--)
1869 eqn
->coef
[var
] -= sub
->coef
[var
] * k
/ c
;
1876 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
1877 if (pb
->subs
[e2
].coef
[i
]
1878 && (pb
->subs
[e2
].color
== omega_red
1879 || (pb
->subs
[e2
].color
== omega_black
1880 && pb
->eqs
[e
].color
== omega_black
)))
1882 eqn eqn
= &(pb
->subs
[e2
]);
1885 for (var
= n_vars
; var
>= 0; var
--)
1886 eqn
->coef
[var
] *= a
;
1890 for (var
= n_vars
; var
>= 0; var
--)
1891 eqn
->coef
[var
] -= sub
->coef
[var
] * k
/ c
;
1894 divide_eqn_by_gcd (eqn
, n_vars
);
1897 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1899 fprintf (dump_file
, "cleaned-out wildcard: ");
1900 omega_print_problem (dump_file
, pb
);
1907 normalize_omega_problem (pb
);
1910 /* Swap values contained in I and J. */
1913 swap (int *i
, int *j
)
1921 /* Swap values contained in I and J. */
1924 bswap (bool *i
, bool *j
)
1932 /* Make variable IDX unprotected in PB, by swapping its index at the
1933 PB->safe_vars rank. */
1936 omega_unprotect_1 (omega_pb pb
, int *idx
, bool *unprotect
)
1938 /* If IDX is protected... */
1939 if (*idx
< pb
->safe_vars
)
1941 /* ... swap its index with the last non protected index. */
1942 int j
= pb
->safe_vars
;
1945 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1947 pb
->geqs
[e
].touched
= 1;
1948 swap (&pb
->geqs
[e
].coef
[*idx
], &pb
->geqs
[e
].coef
[j
]);
1951 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
1952 swap (&pb
->eqs
[e
].coef
[*idx
], &pb
->eqs
[e
].coef
[j
]);
1954 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
1955 swap (&pb
->subs
[e
].coef
[*idx
], &pb
->subs
[e
].coef
[j
]);
1958 bswap (&unprotect
[*idx
], &unprotect
[j
]);
1960 swap (&pb
->var
[*idx
], &pb
->var
[j
]);
1961 pb
->forwarding_address
[pb
->var
[*idx
]] = *idx
;
1962 pb
->forwarding_address
[pb
->var
[j
]] = j
;
1966 /* The variable at pb->safe_vars is also unprotected now. */
1970 /* During the Fourier-Motzkin elimination some variables are
1971 substituted with other variables. This function resurrects the
1972 substituted variables in PB. */
1975 resurrect_subs (omega_pb pb
)
1977 if (pb
->num_subs
> 0
1978 && please_no_equalities_in_simplified_problems
== 0)
1982 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1985 "problem reduced, bringing variables back to life\n");
1986 omega_print_problem (dump_file
, pb
);
1989 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
1990 if (omega_wildcard_p (pb
, i
))
1991 omega_unprotect_1 (pb
, &i
, NULL
);
1995 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
1996 if (single_var_geq (&pb
->geqs
[e
], pb
->num_vars
))
1998 if (!omega_safe_var_p (pb
, abs (pb
->geqs
[e
].key
)))
1999 pb
->geqs
[e
].key
+= (pb
->geqs
[e
].key
> 0 ? m
: -m
);
2003 pb
->geqs
[e
].touched
= 1;
2004 pb
->geqs
[e
].key
= 0;
2007 for (i
= pb
->num_vars
; !omega_safe_var_p (pb
, i
); i
--)
2009 pb
->var
[i
+ m
] = pb
->var
[i
];
2011 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2012 pb
->geqs
[e
].coef
[i
+ m
] = pb
->geqs
[e
].coef
[i
];
2014 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
2015 pb
->eqs
[e
].coef
[i
+ m
] = pb
->eqs
[e
].coef
[i
];
2017 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2018 pb
->subs
[e
].coef
[i
+ m
] = pb
->subs
[e
].coef
[i
];
2021 for (i
= pb
->safe_vars
+ m
; !omega_safe_var_p (pb
, i
); i
--)
2023 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2024 pb
->geqs
[e
].coef
[i
] = 0;
2026 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
2027 pb
->eqs
[e
].coef
[i
] = 0;
2029 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2030 pb
->subs
[e
].coef
[i
] = 0;
2035 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2037 pb
->var
[pb
->safe_vars
+ 1 + e
] = pb
->subs
[e
].key
;
2038 omega_copy_eqn (&(pb
->eqs
[pb
->num_eqs
]), &(pb
->subs
[e
]),
2040 pb
->eqs
[pb
->num_eqs
].coef
[pb
->safe_vars
+ 1 + e
] = -1;
2041 pb
->eqs
[pb
->num_eqs
].color
= omega_black
;
2043 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2045 fprintf (dump_file
, "brought back: ");
2046 omega_print_eq (dump_file
, pb
, &pb
->eqs
[pb
->num_eqs
]);
2047 fprintf (dump_file
, "\n");
2051 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2057 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2059 fprintf (dump_file
, "variables brought back to life\n");
2060 omega_print_problem (dump_file
, pb
);
2063 cleanout_wildcards (pb
);
2068 implies (unsigned int a
, unsigned int b
)
2070 return (a
== (a
& b
));
2073 /* Eliminate redundant equations in PB. When EXPENSIVE is true, an
2074 extra step is performed. Returns omega_false when there exist no
2075 solution, omega_true otherwise. */
2078 omega_eliminate_redundant (omega_pb pb
, bool expensive
)
2080 int c
, e
, e1
, e2
, e3
, p
, q
, i
, k
, alpha
, alpha1
, alpha2
, alpha3
;
2081 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2082 omega_pb tmp_problem
;
2084 /* {P,Z,N}EQS = {Positive,Zero,Negative} Equations. */
2085 unsigned int *peqs
= XNEWVEC (unsigned int, OMEGA_MAX_GEQS
);
2086 unsigned int *zeqs
= XNEWVEC (unsigned int, OMEGA_MAX_GEQS
);
2087 unsigned int *neqs
= XNEWVEC (unsigned int, OMEGA_MAX_GEQS
);
2089 /* PP = Possible Positives, PZ = Possible Zeros, PN = Possible Negatives */
2090 unsigned int pp
, pz
, pn
;
2092 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2094 fprintf (dump_file
, "in eliminate Redundant:\n");
2095 omega_print_problem (dump_file
, pb
);
2098 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2103 peqs
[e
] = zeqs
[e
] = neqs
[e
] = 0;
2105 for (i
= pb
->num_vars
; i
>= 1; i
--)
2107 if (pb
->geqs
[e
].coef
[i
] > 0)
2109 else if (pb
->geqs
[e
].coef
[i
] < 0)
2119 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
2121 for (e2
= e1
- 1; e2
>= 0; e2
--)
2124 for (p
= pb
->num_vars
; p
> 1; p
--)
2125 for (q
= p
- 1; q
> 0; q
--)
2126 if ((alpha
= pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e2
].coef
[q
]
2127 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e1
].coef
[q
]) != 0)
2133 pz
= ((zeqs
[e1
] & zeqs
[e2
]) | (peqs
[e1
] & neqs
[e2
])
2134 | (neqs
[e1
] & peqs
[e2
]));
2135 pp
= peqs
[e1
] | peqs
[e2
];
2136 pn
= neqs
[e1
] | neqs
[e2
];
2138 for (e3
= pb
->num_geqs
- 1; e3
>= 0; e3
--)
2139 if (e3
!= e1
&& e3
!= e2
)
2141 if (!implies (zeqs
[e3
], pz
))
2144 alpha1
= (pb
->geqs
[e2
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2145 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2146 alpha2
= -(pb
->geqs
[e1
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2147 - pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2150 if (alpha1
* alpha2
<= 0)
2162 /* Trying to prove e3 is redundant. */
2163 if (!implies (peqs
[e3
], pp
)
2164 || !implies (neqs
[e3
], pn
))
2167 if (pb
->geqs
[e3
].color
== omega_black
2168 && (pb
->geqs
[e1
].color
== omega_red
2169 || pb
->geqs
[e2
].color
== omega_red
))
2172 for (k
= pb
->num_vars
; k
>= 1; k
--)
2173 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2174 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2175 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2178 c
= (alpha1
* pb
->geqs
[e1
].coef
[0]
2179 + alpha2
* pb
->geqs
[e2
].coef
[0]);
2181 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] + 1))
2183 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2186 "found redundant inequality\n");
2188 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2189 alpha1
, alpha2
, alpha3
);
2191 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2192 fprintf (dump_file
, "\n");
2193 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2194 fprintf (dump_file
, "\n=> ");
2195 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2196 fprintf (dump_file
, "\n\n");
2204 /* Trying to prove e3 <= 0 and therefore e3 = 0,
2205 or trying to prove e3 < 0, and therefore the
2206 problem has no solutions. */
2207 if (!implies (peqs
[e3
], pn
)
2208 || !implies (neqs
[e3
], pp
))
2211 if (pb
->geqs
[e1
].color
== omega_red
2212 || pb
->geqs
[e2
].color
== omega_red
2213 || pb
->geqs
[e3
].color
== omega_red
)
2216 /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */
2217 for (k
= pb
->num_vars
; k
>= 1; k
--)
2218 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2219 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2220 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2223 c
= (alpha1
* pb
->geqs
[e1
].coef
[0]
2224 + alpha2
* pb
->geqs
[e2
].coef
[0]);
2226 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0]))
2228 /* We just proved e3 < 0, so no solutions exist. */
2229 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2232 "found implied over tight inequality\n");
2234 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2235 alpha1
, alpha2
, -alpha3
);
2236 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2237 fprintf (dump_file
, "\n");
2238 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2239 fprintf (dump_file
, "\n=> not ");
2240 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2241 fprintf (dump_file
, "\n\n");
2249 else if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] - 1))
2251 /* We just proved that e3 <=0, so e3 = 0. */
2252 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2255 "found implied tight inequality\n");
2257 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2258 alpha1
, alpha2
, -alpha3
);
2259 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2260 fprintf (dump_file
, "\n");
2261 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2262 fprintf (dump_file
, "\n=> inverse ");
2263 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2264 fprintf (dump_file
, "\n\n");
2267 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
++],
2268 &pb
->geqs
[e3
], pb
->num_vars
);
2269 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2270 adding_equality_constraint (pb
, pb
->num_eqs
- 1);
2278 /* Delete the inequalities that were marked as dead. */
2279 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2281 omega_delete_geq (pb
, e
, pb
->num_vars
);
2284 goto eliminate_redundant_done
;
2286 tmp_problem
= XNEW (struct omega_pb_d
);
2289 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2291 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2294 "checking equation %d to see if it is redundant: ", e
);
2295 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2296 fprintf (dump_file
, "\n");
2299 omega_copy_problem (tmp_problem
, pb
);
2300 omega_negate_geq (tmp_problem
, e
);
2301 tmp_problem
->safe_vars
= 0;
2302 tmp_problem
->variables_freed
= false;
2304 if (omega_solve_problem (tmp_problem
, omega_false
) == omega_false
)
2305 omega_delete_geq (pb
, e
, pb
->num_vars
);
2311 if (!omega_reduce_with_subs
)
2313 resurrect_subs (pb
);
2314 gcc_assert (please_no_equalities_in_simplified_problems
2315 || pb
->num_subs
== 0);
2318 eliminate_redundant_done
:
2326 /* For each inequality that has coefficients bigger than 20, try to
2327 create a new constraint that cannot be derived from the original
2328 constraint and that has smaller coefficients. Add the new
2329 constraint at the end of geqs. Return the number of inequalities
2330 that have been added to PB. */
2333 smooth_weird_equations (omega_pb pb
)
2335 int e1
, e2
, e3
, p
, q
, k
, alpha
, alpha1
, alpha2
, alpha3
;
2340 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
2341 if (pb
->geqs
[e1
].color
== omega_black
)
2345 for (v
= pb
->num_vars
; v
>= 1; v
--)
2346 if (pb
->geqs
[e1
].coef
[v
] != 0 && abs (pb
->geqs
[e1
].coef
[v
]) < g
)
2347 g
= abs (pb
->geqs
[e1
].coef
[v
]);
2354 for (v
= pb
->num_vars
; v
>= 1; v
--)
2355 pb
->geqs
[e3
].coef
[v
] = int_div (6 * pb
->geqs
[e1
].coef
[v
] + g
/ 2,
2358 pb
->geqs
[e3
].color
= omega_black
;
2359 pb
->geqs
[e3
].touched
= 1;
2361 pb
->geqs
[e3
].coef
[0] = 9997;
2363 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2365 fprintf (dump_file
, "Checking to see if we can derive: ");
2366 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e3
]);
2367 fprintf (dump_file
, "\n from: ");
2368 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e1
]);
2369 fprintf (dump_file
, "\n");
2372 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
2373 if (e1
!= e2
&& pb
->geqs
[e2
].color
== omega_black
)
2375 for (p
= pb
->num_vars
; p
> 1; p
--)
2377 for (q
= p
- 1; q
> 0; q
--)
2380 (pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e2
].coef
[q
] -
2381 pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e1
].coef
[q
]);
2390 alpha1
= (pb
->geqs
[e2
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2391 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2392 alpha2
= -(pb
->geqs
[e1
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2393 - pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2396 if (alpha1
* alpha2
<= 0)
2408 /* Try to prove e3 is redundant: verify
2409 alpha1*v1 + alpha2*v2 = alpha3*v3. */
2410 for (k
= pb
->num_vars
; k
>= 1; k
--)
2411 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2412 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2413 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2416 c
= alpha1
* pb
->geqs
[e1
].coef
[0]
2417 + alpha2
* pb
->geqs
[e2
].coef
[0];
2419 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] + 1))
2420 pb
->geqs
[e3
].coef
[0] = int_div (c
, alpha3
);
2425 if (pb
->geqs
[e3
].coef
[0] < 9997)
2430 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2433 "Smoothing weird equations; adding:\n");
2434 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e3
]);
2435 fprintf (dump_file
, "\nto:\n");
2436 omega_print_problem (dump_file
, pb
);
2437 fprintf (dump_file
, "\n\n");
2445 /* Replace tuples of inequalities, that define upper and lower half
2446 spaces, with an equation. */
2449 coalesce (omega_pb pb
)
2454 int found_something
= 0;
2456 for (e
= 0; e
< pb
->num_geqs
; e
++)
2457 if (pb
->geqs
[e
].color
== omega_red
)
2463 is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2465 for (e
= 0; e
< pb
->num_geqs
; e
++)
2468 for (e
= 0; e
< pb
->num_geqs
; e
++)
2469 if (pb
->geqs
[e
].color
== omega_red
2470 && !pb
->geqs
[e
].touched
)
2471 for (e2
= e
+ 1; e2
< pb
->num_geqs
; e2
++)
2472 if (!pb
->geqs
[e2
].touched
2473 && pb
->geqs
[e
].key
== -pb
->geqs
[e2
].key
2474 && pb
->geqs
[e
].coef
[0] == -pb
->geqs
[e2
].coef
[0]
2475 && pb
->geqs
[e2
].color
== omega_red
)
2477 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
++], &pb
->geqs
[e
],
2479 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2485 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2487 omega_delete_geq (pb
, e
, pb
->num_vars
);
2489 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && found_something
)
2491 fprintf (dump_file
, "Coalesced pb->geqs into %d EQ's:\n",
2493 omega_print_problem (dump_file
, pb
);
2499 /* Eliminate red inequalities from PB. When ELIMINATE_ALL is
2500 true, continue to eliminate all the red inequalities. */
2503 omega_eliminate_red (omega_pb pb
, bool eliminate_all
)
2505 int e
, e2
, e3
, i
, j
, k
, a
, alpha1
, alpha2
;
2507 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2510 omega_pb tmp_problem
;
2512 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2514 fprintf (dump_file
, "in eliminate RED:\n");
2515 omega_print_problem (dump_file
, pb
);
2518 if (pb
->num_eqs
> 0)
2519 omega_simplify_problem (pb
);
2521 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2524 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2525 if (pb
->geqs
[e
].color
== omega_black
&& !is_dead
[e
])
2526 for (e2
= e
- 1; e2
>= 0; e2
--)
2527 if (pb
->geqs
[e2
].color
== omega_black
2532 for (i
= pb
->num_vars
; i
> 1; i
--)
2533 for (j
= i
- 1; j
> 0; j
--)
2534 if ((a
= (pb
->geqs
[e
].coef
[i
] * pb
->geqs
[e2
].coef
[j
]
2535 - pb
->geqs
[e2
].coef
[i
] * pb
->geqs
[e
].coef
[j
])) != 0)
2541 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2544 "found two equations to combine, i = %s, ",
2545 omega_variable_to_str (pb
, i
));
2546 fprintf (dump_file
, "j = %s, alpha = %d\n",
2547 omega_variable_to_str (pb
, j
), a
);
2548 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2549 fprintf (dump_file
, "\n");
2550 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2551 fprintf (dump_file
, "\n");
2554 for (e3
= pb
->num_geqs
- 1; e3
>= 0; e3
--)
2555 if (pb
->geqs
[e3
].color
== omega_red
)
2557 alpha1
= (pb
->geqs
[e2
].coef
[j
] * pb
->geqs
[e3
].coef
[i
]
2558 - pb
->geqs
[e2
].coef
[i
] * pb
->geqs
[e3
].coef
[j
]);
2559 alpha2
= -(pb
->geqs
[e
].coef
[j
] * pb
->geqs
[e3
].coef
[i
]
2560 - pb
->geqs
[e
].coef
[i
] * pb
->geqs
[e3
].coef
[j
]);
2562 if ((a
> 0 && alpha1
> 0 && alpha2
> 0)
2563 || (a
< 0 && alpha1
< 0 && alpha2
< 0))
2565 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2568 "alpha1 = %d, alpha2 = %d;"
2569 "comparing against: ",
2571 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2572 fprintf (dump_file
, "\n");
2575 for (k
= pb
->num_vars
; k
>= 0; k
--)
2577 c
= (alpha1
* pb
->geqs
[e
].coef
[k
]
2578 + alpha2
* pb
->geqs
[e2
].coef
[k
]);
2580 if (c
!= a
* pb
->geqs
[e3
].coef
[k
])
2583 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && k
> 0)
2584 fprintf (dump_file
, " %s: %d, %d\n",
2585 omega_variable_to_str (pb
, k
), c
,
2586 a
* pb
->geqs
[e3
].coef
[k
]);
2591 ((a
> 0 && c
< a
* pb
->geqs
[e3
].coef
[k
])
2592 || (a
< 0 && c
> a
* pb
->geqs
[e3
].coef
[k
]))))
2594 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2598 "red equation#%d is dead "
2599 "(%d dead so far, %d remain)\n",
2601 pb
->num_geqs
- dead_count
);
2602 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2603 fprintf (dump_file
, "\n");
2604 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2605 fprintf (dump_file
, "\n");
2606 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2607 fprintf (dump_file
, "\n");
2615 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2617 omega_delete_geq (pb
, e
, pb
->num_vars
);
2621 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2623 fprintf (dump_file
, "in eliminate RED, easy tests done:\n");
2624 omega_print_problem (dump_file
, pb
);
2627 for (red_found
= 0, e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2628 if (pb
->geqs
[e
].color
== omega_red
)
2633 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2634 fprintf (dump_file
, "fast checks worked\n");
2636 if (!omega_reduce_with_subs
)
2637 gcc_assert (please_no_equalities_in_simplified_problems
2638 || pb
->num_subs
== 0);
2643 if (!omega_verify_simplification
2644 && verify_omega_pb (pb
) == omega_false
)
2648 tmp_problem
= XNEW (struct omega_pb_d
);
2650 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2651 if (pb
->geqs
[e
].color
== omega_red
)
2653 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2656 "checking equation %d to see if it is redundant: ", e
);
2657 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2658 fprintf (dump_file
, "\n");
2661 omega_copy_problem (tmp_problem
, pb
);
2662 omega_negate_geq (tmp_problem
, e
);
2663 tmp_problem
->safe_vars
= 0;
2664 tmp_problem
->variables_freed
= false;
2665 tmp_problem
->num_subs
= 0;
2667 if (omega_solve_problem (tmp_problem
, omega_false
) == omega_false
)
2669 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2670 fprintf (dump_file
, "it is redundant\n");
2671 omega_delete_geq (pb
, e
, pb
->num_vars
);
2675 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2676 fprintf (dump_file
, "it is not redundant\n");
2680 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2681 fprintf (dump_file
, "no need to check other red equations\n");
2689 /* omega_simplify_problem (pb); */
2691 if (!omega_reduce_with_subs
)
2692 gcc_assert (please_no_equalities_in_simplified_problems
2693 || pb
->num_subs
== 0);
2696 /* Transform some wildcard variables to non-safe variables. */
2699 chain_unprotect (omega_pb pb
)
2702 bool *unprotect
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2704 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2706 unprotect
[i
] = omega_wildcard_p (pb
, i
);
2708 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2709 if (pb
->subs
[e
].coef
[i
])
2710 unprotect
[i
] = false;
2713 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2715 fprintf (dump_file
, "Doing chain reaction unprotection\n");
2716 omega_print_problem (dump_file
, pb
);
2718 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2720 fprintf (dump_file
, "unprotecting %s\n",
2721 omega_variable_to_str (pb
, i
));
2724 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2726 omega_unprotect_1 (pb
, &i
, unprotect
);
2728 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2730 fprintf (dump_file
, "After chain reactions\n");
2731 omega_print_problem (dump_file
, pb
);
2737 /* Reduce problem PB. */
2740 omega_problem_reduced (omega_pb pb
)
2742 if (omega_verify_simplification
2743 && !in_approximate_mode
2744 && verify_omega_pb (pb
) == omega_false
)
2747 if (PARAM_VALUE (PARAM_OMEGA_ELIMINATE_REDUNDANT_CONSTRAINTS
)
2748 && !omega_eliminate_redundant (pb
, true))
2751 omega_found_reduction
= omega_true
;
2753 if (!please_no_equalities_in_simplified_problems
)
2756 if (omega_reduce_with_subs
2757 || please_no_equalities_in_simplified_problems
)
2758 chain_unprotect (pb
);
2760 resurrect_subs (pb
);
2762 if (!return_single_result
)
2766 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2767 pb
->forwarding_address
[pb
->var
[i
]] = i
;
2769 for (i
= 0; i
< pb
->num_subs
; i
++)
2770 pb
->forwarding_address
[pb
->subs
[i
].key
] = -i
- 1;
2772 (*omega_when_reduced
) (pb
);
2775 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2777 fprintf (dump_file
, "-------------------------------------------\n");
2778 fprintf (dump_file
, "problem reduced:\n");
2779 omega_print_problem (dump_file
, pb
);
2780 fprintf (dump_file
, "-------------------------------------------\n");
2784 /* Eliminates all the free variables for problem PB, that is all the
2785 variables from FV to PB->NUM_VARS. */
2788 omega_free_eliminations (omega_pb pb
, int fv
)
2790 bool try_again
= true;
2792 int n_vars
= pb
->num_vars
;
2798 for (i
= n_vars
; i
> fv
; i
--)
2800 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2801 if (pb
->geqs
[e
].coef
[i
])
2806 else if (pb
->geqs
[e
].coef
[i
] > 0)
2808 for (e2
= e
- 1; e2
>= 0; e2
--)
2809 if (pb
->geqs
[e2
].coef
[i
] < 0)
2814 for (e2
= e
- 1; e2
>= 0; e2
--)
2815 if (pb
->geqs
[e2
].coef
[i
] > 0)
2822 for (e3
= pb
->num_subs
- 1; e3
>= 0; e3
--)
2823 if (pb
->subs
[e3
].coef
[i
])
2829 for (e3
= pb
->num_eqs
- 1; e3
>= 0; e3
--)
2830 if (pb
->eqs
[e3
].coef
[i
])
2836 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2837 fprintf (dump_file
, "a free elimination of %s\n",
2838 omega_variable_to_str (pb
, i
));
2842 omega_delete_geq (pb
, e
, n_vars
);
2844 for (e
--; e
>= 0; e
--)
2845 if (pb
->geqs
[e
].coef
[i
])
2846 omega_delete_geq (pb
, e
, n_vars
);
2848 try_again
= (i
< n_vars
);
2851 omega_delete_variable (pb
, i
);
2852 n_vars
= pb
->num_vars
;
2857 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2859 fprintf (dump_file
, "\nafter free eliminations:\n");
2860 omega_print_problem (dump_file
, pb
);
2861 fprintf (dump_file
, "\n");
2865 /* Do free red eliminations. */
2868 free_red_eliminations (omega_pb pb
)
2870 bool try_again
= true;
2872 int n_vars
= pb
->num_vars
;
2873 bool *is_red_var
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2874 bool *is_dead_var
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2875 bool *is_dead_geq
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2877 for (i
= n_vars
; i
> 0; i
--)
2879 is_red_var
[i
] = false;
2880 is_dead_var
[i
] = false;
2883 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2885 is_dead_geq
[e
] = false;
2887 if (pb
->geqs
[e
].color
== omega_red
)
2888 for (i
= n_vars
; i
> 0; i
--)
2889 if (pb
->geqs
[e
].coef
[i
] != 0)
2890 is_red_var
[i
] = true;
2896 for (i
= n_vars
; i
> 0; i
--)
2897 if (!is_red_var
[i
] && !is_dead_var
[i
])
2899 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2900 if (!is_dead_geq
[e
] && pb
->geqs
[e
].coef
[i
])
2905 else if (pb
->geqs
[e
].coef
[i
] > 0)
2907 for (e2
= e
- 1; e2
>= 0; e2
--)
2908 if (!is_dead_geq
[e2
] && pb
->geqs
[e2
].coef
[i
] < 0)
2913 for (e2
= e
- 1; e2
>= 0; e2
--)
2914 if (!is_dead_geq
[e2
] && pb
->geqs
[e2
].coef
[i
] > 0)
2921 for (e3
= pb
->num_subs
- 1; e3
>= 0; e3
--)
2922 if (pb
->subs
[e3
].coef
[i
])
2928 for (e3
= pb
->num_eqs
- 1; e3
>= 0; e3
--)
2929 if (pb
->eqs
[e3
].coef
[i
])
2935 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2936 fprintf (dump_file
, "a free red elimination of %s\n",
2937 omega_variable_to_str (pb
, i
));
2940 if (pb
->geqs
[e
].coef
[i
])
2941 is_dead_geq
[e
] = true;
2944 is_dead_var
[i
] = true;
2949 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2951 omega_delete_geq (pb
, e
, n_vars
);
2953 for (i
= n_vars
; i
> 0; i
--)
2955 omega_delete_variable (pb
, i
);
2957 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2959 fprintf (dump_file
, "\nafter free red eliminations:\n");
2960 omega_print_problem (dump_file
, pb
);
2961 fprintf (dump_file
, "\n");
2969 /* For equation EQ of the form "0 = EQN", insert in PB two
2970 inequalities "0 <= EQN" and "0 <= -EQN". */
2973 omega_convert_eq_to_geqs (omega_pb pb
, int eq
)
2977 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2978 fprintf (dump_file
, "Converting Eq to Geqs\n");
2980 /* Insert "0 <= EQN". */
2981 omega_copy_eqn (&pb
->geqs
[pb
->num_geqs
], &pb
->eqs
[eq
], pb
->num_vars
);
2982 pb
->geqs
[pb
->num_geqs
].touched
= 1;
2985 /* Insert "0 <= -EQN". */
2986 omega_copy_eqn (&pb
->geqs
[pb
->num_geqs
], &pb
->eqs
[eq
], pb
->num_vars
);
2987 pb
->geqs
[pb
->num_geqs
].touched
= 1;
2989 for (i
= 0; i
<= pb
->num_vars
; i
++)
2990 pb
->geqs
[pb
->num_geqs
].coef
[i
] *= -1;
2994 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2995 omega_print_problem (dump_file
, pb
);
2998 /* Eliminates variable I from PB. */
3001 omega_do_elimination (omega_pb pb
, int e
, int i
)
3003 eqn sub
= omega_alloc_eqns (0, 1);
3005 int n_vars
= pb
->num_vars
;
3007 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3008 fprintf (dump_file
, "eliminating variable %s\n",
3009 omega_variable_to_str (pb
, i
));
3011 omega_copy_eqn (sub
, &pb
->eqs
[e
], pb
->num_vars
);
3014 if (c
== 1 || c
== -1)
3016 if (pb
->eqs
[e
].color
== omega_red
)
3019 omega_substitute_red (pb
, sub
, i
, c
, &fB
);
3021 omega_convert_eq_to_geqs (pb
, e
);
3023 omega_delete_variable (pb
, i
);
3027 omega_substitute (pb
, sub
, i
, c
);
3028 omega_delete_variable (pb
, i
);
3036 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3037 fprintf (dump_file
, "performing non-exact elimination, c = %d\n", c
);
3039 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
3040 if (pb
->eqs
[e
].coef
[i
])
3042 eqn eqn
= &(pb
->eqs
[e
]);
3044 for (j
= n_vars
; j
>= 0; j
--)
3048 if (sub
->color
== omega_red
)
3049 eqn
->color
= omega_red
;
3050 for (j
= n_vars
; j
>= 0; j
--)
3051 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3054 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3055 if (pb
->geqs
[e
].coef
[i
])
3057 eqn eqn
= &(pb
->geqs
[e
]);
3060 if (sub
->color
== omega_red
)
3061 eqn
->color
= omega_red
;
3063 for (j
= n_vars
; j
>= 0; j
--)
3070 for (j
= n_vars
; j
>= 0; j
--)
3071 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3075 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
3076 if (pb
->subs
[e
].coef
[i
])
3078 eqn eqn
= &(pb
->subs
[e
]);
3081 gcc_assert (sub
->color
== omega_black
);
3082 for (j
= n_vars
; j
>= 0; j
--)
3086 for (j
= n_vars
; j
>= 0; j
--)
3087 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3090 if (in_approximate_mode
)
3091 omega_delete_variable (pb
, i
);
3093 omega_convert_eq_to_geqs (pb
, e2
);
3096 omega_free_eqns (sub
, 1);
3099 /* Helper function for printing "sorry, no solution". */
3101 static inline enum omega_result
3102 omega_problem_has_no_solution (void)
3104 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3105 fprintf (dump_file
, "\nequations have no solution \n");
3110 /* Helper function: solve equations in PB one at a time, following the
3111 DESIRED_RES result. */
3113 static enum omega_result
3114 omega_solve_eq (omega_pb pb
, enum omega_result desired_res
)
3121 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && pb
->num_eqs
> 0)
3123 fprintf (dump_file
, "\nomega_solve_eq (%d, %d)\n",
3124 desired_res
, may_be_red
);
3125 omega_print_problem (dump_file
, pb
);
3126 fprintf (dump_file
, "\n");
3132 j
= pb
->num_eqs
- 1;
3138 while (i
<= j
&& pb
->eqs
[i
].color
== omega_red
)
3141 while (i
<= j
&& pb
->eqs
[j
].color
== omega_black
)
3147 eq
= omega_alloc_eqns (0, 1);
3148 omega_copy_eqn (eq
, &pb
->eqs
[i
], pb
->num_vars
);
3149 omega_copy_eqn (&pb
->eqs
[i
], &pb
->eqs
[j
], pb
->num_vars
);
3150 omega_copy_eqn (&pb
->eqs
[j
], eq
, pb
->num_vars
);
3151 omega_free_eqns (eq
, 1);
3157 /* Eliminate all EQ equations */
3158 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
3160 eqn eqn
= &(pb
->eqs
[e
]);
3163 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3164 fprintf (dump_file
, "----\n");
3166 for (i
= pb
->num_vars
; i
> 0; i
--)
3172 for (j
= i
- 1; j
> 0; j
--)
3176 /* i is the position of last nonzero coefficient,
3177 g is the coefficient of i,
3178 j is the position of next nonzero coefficient. */
3182 if (eqn
->coef
[0] % g
!= 0)
3183 return omega_problem_has_no_solution ();
3185 eqn
->coef
[0] = eqn
->coef
[0] / g
;
3188 omega_do_elimination (pb
, e
, i
);
3194 if (eqn
->coef
[0] != 0)
3195 return omega_problem_has_no_solution ();
3207 omega_do_elimination (pb
, e
, i
);
3213 bool promotion_possible
=
3214 (omega_safe_var_p (pb
, j
)
3215 && pb
->safe_vars
+ 1 == i
3216 && !omega_eqn_is_red (eqn
, desired_res
)
3217 && !in_approximate_mode
);
3219 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && promotion_possible
)
3220 fprintf (dump_file
, " Promotion possible\n");
3223 if (!omega_safe_var_p (pb
, j
))
3225 for (; g
!= 1 && !omega_safe_var_p (pb
, j
); j
--)
3226 g
= gcd (abs (eqn
->coef
[j
]), g
);
3229 else if (!omega_safe_var_p (pb
, i
))
3234 for (; g
!= 1 && j
> 0; j
--)
3235 g
= gcd (abs (eqn
->coef
[j
]), g
);
3239 if (eqn
->coef
[0] % g
!= 0)
3240 return omega_problem_has_no_solution ();
3242 for (j
= 0; j
<= pb
->num_vars
; j
++)
3252 for (e2
= e
- 1; e2
>= 0; e2
--)
3253 if (pb
->eqs
[e2
].coef
[i
])
3257 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
3258 if (pb
->geqs
[e2
].coef
[i
])
3262 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
3263 if (pb
->subs
[e2
].coef
[i
])
3268 bool change
= false;
3270 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3272 fprintf (dump_file
, "Ha! We own it! \n");
3273 omega_print_eq (dump_file
, pb
, eqn
);
3274 fprintf (dump_file
, " \n");
3280 for (j
= i
- 1; j
>= 0; j
--)
3282 int t
= int_mod (eqn
->coef
[j
], g
);
3287 if (t
!= eqn
->coef
[j
])
3296 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3297 fprintf (dump_file
, "So what?\n");
3302 omega_name_wild_card (pb
, i
);
3304 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3306 omega_print_eq (dump_file
, pb
, eqn
);
3307 fprintf (dump_file
, " \n");
3316 if (promotion_possible
)
3318 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3320 fprintf (dump_file
, "promoting %s to safety\n",
3321 omega_variable_to_str (pb
, i
));
3322 omega_print_vars (dump_file
, pb
);
3327 if (!omega_wildcard_p (pb
, i
))
3328 omega_name_wild_card (pb
, i
);
3330 promotion_possible
= false;
3335 if (g2
> 1 && !in_approximate_mode
)
3337 if (pb
->eqs
[e
].color
== omega_red
)
3339 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3340 fprintf (dump_file
, "handling red equality\n");
3343 omega_do_elimination (pb
, e
, i
);
3347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3350 "adding equation to handle safe variable \n");
3351 omega_print_eq (dump_file
, pb
, eqn
);
3352 fprintf (dump_file
, "\n----\n");
3353 omega_print_problem (dump_file
, pb
);
3354 fprintf (dump_file
, "\n----\n");
3355 fprintf (dump_file
, "\n----\n");
3358 i
= omega_add_new_wild_card (pb
);
3360 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
3361 omega_init_eqn_zero (&pb
->eqs
[e
+ 1], pb
->num_vars
);
3362 omega_copy_eqn (&pb
->eqs
[e
+ 1], eqn
, pb
->safe_vars
);
3364 for (j
= pb
->num_vars
; j
>= 0; j
--)
3366 pb
->eqs
[e
+ 1].coef
[j
] = int_mod (pb
->eqs
[e
+ 1].coef
[j
], g2
);
3368 if (2 * pb
->eqs
[e
+ 1].coef
[j
] >= g2
)
3369 pb
->eqs
[e
+ 1].coef
[j
] -= g2
;
3372 pb
->eqs
[e
+ 1].coef
[i
] = g2
;
3375 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3376 omega_print_problem (dump_file
, pb
);
3385 /* Find variable to eliminate. */
3388 gcc_assert (in_approximate_mode
);
3390 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3392 fprintf (dump_file
, "non-exact elimination: ");
3393 omega_print_eq (dump_file
, pb
, eqn
);
3394 fprintf (dump_file
, "\n");
3395 omega_print_problem (dump_file
, pb
);
3398 for (i
= pb
->num_vars
; i
> sv
; i
--)
3399 if (pb
->eqs
[e
].coef
[i
] != 0)
3403 for (i
= pb
->num_vars
; i
> sv
; i
--)
3404 if (pb
->eqs
[e
].coef
[i
] == 1 || pb
->eqs
[e
].coef
[i
] == -1)
3410 omega_do_elimination (pb
, e
, i
);
3412 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && g2
> 1)
3414 fprintf (dump_file
, "result of non-exact elimination:\n");
3415 omega_print_problem (dump_file
, pb
);
3420 int factor
= (INT_MAX
);
3423 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3424 fprintf (dump_file
, "doing moding\n");
3426 for (i
= pb
->num_vars
; i
!= sv
; i
--)
3427 if ((pb
->eqs
[e
].coef
[i
] & 1) != 0)
3432 for (; i
!= sv
; i
--)
3433 if ((pb
->eqs
[e
].coef
[i
] & 1) != 0)
3439 if (j
!= 0 && i
== sv
)
3441 omega_do_mod (pb
, 2, e
, j
);
3447 for (i
= pb
->num_vars
; i
!= sv
; i
--)
3448 if (pb
->eqs
[e
].coef
[i
] != 0
3449 && factor
> abs (pb
->eqs
[e
].coef
[i
]) + 1)
3451 factor
= abs (pb
->eqs
[e
].coef
[i
]) + 1;
3457 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3458 fprintf (dump_file
, "should not have happened\n");
3462 omega_do_mod (pb
, factor
, e
, j
);
3463 /* Go back and try this equation again. */
3470 return omega_unknown
;
3473 /* Transform an inequation E to an equality, then solve DIFF problems
3474 based on PB, and only differing by the constant part that is
3475 diminished by one, trying to figure out which of the constants
3478 static enum omega_result
3479 parallel_splinter (omega_pb pb
, int e
, int diff
,
3480 enum omega_result desired_res
)
3482 omega_pb tmp_problem
;
3485 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3487 fprintf (dump_file
, "Using parallel splintering\n");
3488 omega_print_problem (dump_file
, pb
);
3491 tmp_problem
= XNEW (struct omega_pb_d
);
3492 omega_copy_eqn (&pb
->eqs
[0], &pb
->geqs
[e
], pb
->num_vars
);
3495 for (i
= 0; i
<= diff
; i
++)
3497 omega_copy_problem (tmp_problem
, pb
);
3499 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3501 fprintf (dump_file
, "Splinter # %i\n", i
);
3502 omega_print_problem (dump_file
, pb
);
3505 if (omega_solve_problem (tmp_problem
, desired_res
) == omega_true
)
3511 pb
->eqs
[0].coef
[0]--;
3518 /* Helper function: solve equations one at a time. */
3520 static enum omega_result
3521 omega_solve_geq (omega_pb pb
, enum omega_result desired_res
)
3525 enum omega_result result
;
3526 bool coupled_subscripts
= false;
3527 bool smoothed
= false;
3528 bool eliminate_again
;
3529 bool tried_eliminating_redundant
= false;
3531 if (desired_res
!= omega_simplify
)
3539 gcc_assert (desired_res
== omega_simplify
|| pb
->num_subs
== 0);
3541 /* Verify that there are not too many inequalities. */
3542 gcc_assert (pb
->num_geqs
<= OMEGA_MAX_GEQS
);
3544 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3546 fprintf (dump_file
, "\nomega_solve_geq (%d,%d):\n",
3547 desired_res
, please_no_equalities_in_simplified_problems
);
3548 omega_print_problem (dump_file
, pb
);
3549 fprintf (dump_file
, "\n");
3552 n_vars
= pb
->num_vars
;
3556 enum omega_eqn_color u_color
= omega_black
;
3557 enum omega_eqn_color l_color
= omega_black
;
3558 int upper_bound
= pos_infinity
;
3559 int lower_bound
= neg_infinity
;
3561 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3563 int a
= pb
->geqs
[e
].coef
[1];
3564 int c
= pb
->geqs
[e
].coef
[0];
3566 /* Our equation is ax + c >= 0, or ax >= -c, or c >= -ax. */
3570 return omega_problem_has_no_solution ();
3577 if (lower_bound
< -c
3578 || (lower_bound
== -c
3579 && !omega_eqn_is_red (&pb
->geqs
[e
], desired_res
)))
3582 l_color
= pb
->geqs
[e
].color
;
3588 c
= int_div (c
, -a
);
3591 || (upper_bound
== c
3592 && !omega_eqn_is_red (&pb
->geqs
[e
], desired_res
)))
3595 u_color
= pb
->geqs
[e
].color
;
3600 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3602 fprintf (dump_file
, "upper bound = %d\n", upper_bound
);
3603 fprintf (dump_file
, "lower bound = %d\n", lower_bound
);
3606 if (lower_bound
> upper_bound
)
3607 return omega_problem_has_no_solution ();
3609 if (desired_res
== omega_simplify
)
3612 if (pb
->safe_vars
== 1)
3615 if (lower_bound
== upper_bound
3616 && u_color
== omega_black
3617 && l_color
== omega_black
)
3619 pb
->eqs
[0].coef
[0] = -lower_bound
;
3620 pb
->eqs
[0].coef
[1] = 1;
3621 pb
->eqs
[0].color
= omega_black
;
3623 return omega_solve_problem (pb
, desired_res
);
3627 if (lower_bound
> neg_infinity
)
3629 pb
->geqs
[0].coef
[0] = -lower_bound
;
3630 pb
->geqs
[0].coef
[1] = 1;
3631 pb
->geqs
[0].key
= 1;
3632 pb
->geqs
[0].color
= l_color
;
3633 pb
->geqs
[0].touched
= 0;
3637 if (upper_bound
< pos_infinity
)
3639 pb
->geqs
[pb
->num_geqs
].coef
[0] = upper_bound
;
3640 pb
->geqs
[pb
->num_geqs
].coef
[1] = -1;
3641 pb
->geqs
[pb
->num_geqs
].key
= -1;
3642 pb
->geqs
[pb
->num_geqs
].color
= u_color
;
3643 pb
->geqs
[pb
->num_geqs
].touched
= 0;
3651 omega_problem_reduced (pb
);
3655 if (original_problem
!= no_problem
3656 && l_color
== omega_black
3657 && u_color
== omega_black
3659 && lower_bound
== upper_bound
)
3661 pb
->eqs
[0].coef
[0] = -lower_bound
;
3662 pb
->eqs
[0].coef
[1] = 1;
3664 adding_equality_constraint (pb
, 0);
3670 if (!pb
->variables_freed
)
3672 pb
->variables_freed
= true;
3674 if (desired_res
!= omega_simplify
)
3675 omega_free_eliminations (pb
, 0);
3677 omega_free_eliminations (pb
, pb
->safe_vars
);
3679 n_vars
= pb
->num_vars
;
3685 switch (normalize_omega_problem (pb
))
3687 case normalize_false
:
3691 case normalize_coupled
:
3692 coupled_subscripts
= true;
3695 case normalize_uncoupled
:
3696 coupled_subscripts
= false;
3703 n_vars
= pb
->num_vars
;
3705 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3707 fprintf (dump_file
, "\nafter normalization:\n");
3708 omega_print_problem (dump_file
, pb
);
3709 fprintf (dump_file
, "\n");
3710 fprintf (dump_file
, "eliminating variable using Fourier-Motzkin.\n");
3714 int parallel_difference
= INT_MAX
;
3715 int best_parallel_eqn
= -1;
3716 int minC
, maxC
, minCj
= 0;
3717 int lower_bound_count
= 0;
3719 bool possible_easy_int_solution
;
3720 int max_splinters
= 1;
3722 bool lucky_exact
= false;
3723 int best
= (INT_MAX
);
3724 int j
= 0, jLe
= 0, jLowerBoundCount
= 0;
3727 eliminate_again
= false;
3729 if (pb
->num_eqs
> 0)
3730 return omega_solve_problem (pb
, desired_res
);
3732 if (!coupled_subscripts
)
3734 if (pb
->safe_vars
== 0)
3737 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3738 if (!omega_safe_var_p (pb
, abs (pb
->geqs
[e
].key
)))
3739 omega_delete_geq (pb
, e
, n_vars
);
3741 pb
->num_vars
= pb
->safe_vars
;
3743 if (desired_res
== omega_simplify
)
3745 omega_problem_reduced (pb
);
3752 if (desired_res
!= omega_simplify
)
3757 if (pb
->num_geqs
== 0)
3759 if (desired_res
== omega_simplify
)
3761 pb
->num_vars
= pb
->safe_vars
;
3762 omega_problem_reduced (pb
);
3768 if (desired_res
== omega_simplify
&& n_vars
== pb
->safe_vars
)
3770 omega_problem_reduced (pb
);
3774 if (pb
->num_geqs
> OMEGA_MAX_GEQS
- 30
3775 || pb
->num_geqs
> 2 * n_vars
* n_vars
+ 4 * n_vars
+ 10)
3777 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3779 "TOO MANY EQUATIONS; "
3780 "%d equations, %d variables, "
3781 "ELIMINATING REDUNDANT ONES\n",
3782 pb
->num_geqs
, n_vars
);
3784 if (!omega_eliminate_redundant (pb
, false))
3787 n_vars
= pb
->num_vars
;
3789 if (pb
->num_eqs
> 0)
3790 return omega_solve_problem (pb
, desired_res
);
3792 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3793 fprintf (dump_file
, "END ELIMINATION OF REDUNDANT EQUATIONS\n");
3796 if (desired_res
!= omega_simplify
)
3801 for (i
= n_vars
; i
!= fv
; i
--)
3807 int upper_bound_count
= 0;
3809 lower_bound_count
= 0;
3812 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3813 if (pb
->geqs
[e
].coef
[i
] < 0)
3815 minC
= MIN (minC
, pb
->geqs
[e
].coef
[i
]);
3816 upper_bound_count
++;
3817 if (pb
->geqs
[e
].coef
[i
] < -1)
3825 else if (pb
->geqs
[e
].coef
[i
] > 0)
3827 maxC
= MAX (maxC
, pb
->geqs
[e
].coef
[i
]);
3828 lower_bound_count
++;
3830 if (pb
->geqs
[e
].coef
[i
] > 1)
3839 if (lower_bound_count
== 0
3840 || upper_bound_count
== 0)
3842 lower_bound_count
= 0;
3846 if (ub
>= 0 && lb
>= 0
3847 && pb
->geqs
[lb
].key
== -pb
->geqs
[ub
].key
)
3849 int Lc
= pb
->geqs
[lb
].coef
[i
];
3850 int Uc
= -pb
->geqs
[ub
].coef
[i
];
3852 Lc
* pb
->geqs
[ub
].coef
[0] + Uc
* pb
->geqs
[lb
].coef
[0];
3853 lucky
= (diff
>= (Uc
- 1) * (Lc
- 1));
3859 || in_approximate_mode
)
3861 score
= upper_bound_count
* lower_bound_count
;
3863 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3865 "For %s, exact, score = %d*%d, range = %d ... %d,"
3866 "\nlucky = %d, in_approximate_mode=%d \n",
3867 omega_variable_to_str (pb
, i
),
3869 lower_bound_count
, minC
, maxC
, lucky
,
3870 in_approximate_mode
);
3880 jLowerBoundCount
= lower_bound_count
;
3882 lucky_exact
= lucky
;
3889 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3891 "For %s, non-exact, score = %d*%d,"
3892 "range = %d ... %d \n",
3893 omega_variable_to_str (pb
, i
),
3895 lower_bound_count
, minC
, maxC
);
3897 score
= maxC
- minC
;
3905 jLowerBoundCount
= lower_bound_count
;
3910 if (lower_bound_count
== 0)
3912 omega_free_eliminations (pb
, pb
->safe_vars
);
3913 n_vars
= pb
->num_vars
;
3914 eliminate_again
= true;
3921 lower_bound_count
= jLowerBoundCount
;
3923 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3924 if (pb
->geqs
[e
].coef
[i
] > 0)
3926 if (pb
->geqs
[e
].coef
[i
] == -minC
)
3927 max_splinters
+= -minC
- 1;
3930 check_pos_mul ((pb
->geqs
[e
].coef
[i
] - 1),
3931 (-minC
- 1)) / (-minC
) + 1;
3935 /* Trying to produce exact elimination by finding redundant
3937 if (!exact
&& !tried_eliminating_redundant
)
3939 omega_eliminate_redundant (pb
, false);
3940 tried_eliminating_redundant
= true;
3941 eliminate_again
= true;
3944 tried_eliminating_redundant
= false;
3947 if (return_single_result
&& desired_res
== omega_simplify
&& !exact
)
3949 omega_problem_reduced (pb
);
3953 /* #ifndef Omega3 */
3954 /* Trying to produce exact elimination by finding redundant
3956 if (!exact
&& !tried_eliminating_redundant
)
3958 omega_eliminate_redundant (pb
, false);
3959 tried_eliminating_redundant
= true;
3962 tried_eliminating_redundant
= false;
3969 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
3970 if (pb
->geqs
[e1
].color
== omega_black
)
3971 for (e2
= e1
- 1; e2
>= 0; e2
--)
3972 if (pb
->geqs
[e2
].color
== omega_black
3973 && pb
->geqs
[e1
].key
== -pb
->geqs
[e2
].key
3974 && ((pb
->geqs
[e1
].coef
[0] + pb
->geqs
[e2
].coef
[0])
3975 * (3 - single_var_geq (&pb
->geqs
[e1
], pb
->num_vars
))
3976 / 2 < parallel_difference
))
3978 parallel_difference
=
3979 (pb
->geqs
[e1
].coef
[0] + pb
->geqs
[e2
].coef
[0])
3980 * (3 - single_var_geq (&pb
->geqs
[e1
], pb
->num_vars
))
3982 best_parallel_eqn
= e1
;
3985 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
3986 && best_parallel_eqn
>= 0)
3989 "Possible parallel projection, diff = %d, in ",
3990 parallel_difference
);
3991 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[best_parallel_eqn
]));
3992 fprintf (dump_file
, "\n");
3993 omega_print_problem (dump_file
, pb
);
3997 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3999 fprintf (dump_file
, "going to eliminate %s, (%d,%d,%d)\n",
4000 omega_variable_to_str (pb
, i
), i
, minC
,
4002 omega_print_problem (dump_file
, pb
);
4005 fprintf (dump_file
, "(a lucky exact elimination)\n");
4008 fprintf (dump_file
, "(an exact elimination)\n");
4010 fprintf (dump_file
, "Max # of splinters = %d\n", max_splinters
);
4013 gcc_assert (max_splinters
>= 1);
4015 if (!exact
&& desired_res
== omega_simplify
&& best_parallel_eqn
>= 0
4016 && parallel_difference
<= max_splinters
)
4017 return parallel_splinter (pb
, best_parallel_eqn
, parallel_difference
,
4025 int j
= pb
->num_vars
;
4027 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4029 fprintf (dump_file
, "Swapping %d and %d\n", i
, j
);
4030 omega_print_problem (dump_file
, pb
);
4033 swap (&pb
->var
[i
], &pb
->var
[j
]);
4035 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4036 if (pb
->geqs
[e
].coef
[i
] != pb
->geqs
[e
].coef
[j
])
4038 pb
->geqs
[e
].touched
= 1;
4039 t
= pb
->geqs
[e
].coef
[i
];
4040 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[j
];
4041 pb
->geqs
[e
].coef
[j
] = t
;
4044 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
4045 if (pb
->subs
[e
].coef
[i
] != pb
->subs
[e
].coef
[j
])
4047 t
= pb
->subs
[e
].coef
[i
];
4048 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[j
];
4049 pb
->subs
[e
].coef
[j
] = t
;
4052 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4054 fprintf (dump_file
, "Swapping complete \n");
4055 omega_print_problem (dump_file
, pb
);
4056 fprintf (dump_file
, "\n");
4062 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4064 fprintf (dump_file
, "No swap needed\n");
4065 omega_print_problem (dump_file
, pb
);
4069 n_vars
= pb
->num_vars
;
4075 int upper_bound
= pos_infinity
;
4076 int lower_bound
= neg_infinity
;
4077 enum omega_eqn_color ub_color
= omega_black
;
4078 enum omega_eqn_color lb_color
= omega_black
;
4079 int topeqn
= pb
->num_geqs
- 1;
4080 int Lc
= pb
->geqs
[Le
].coef
[i
];
4082 for (Le
= topeqn
; Le
>= 0; Le
--)
4083 if ((Lc
= pb
->geqs
[Le
].coef
[i
]) == 0)
4085 if (pb
->geqs
[Le
].coef
[1] == 1)
4087 int constantTerm
= -pb
->geqs
[Le
].coef
[0];
4089 if (constantTerm
> lower_bound
||
4090 (constantTerm
== lower_bound
&&
4091 !omega_eqn_is_red (&pb
->geqs
[Le
], desired_res
)))
4093 lower_bound
= constantTerm
;
4094 lb_color
= pb
->geqs
[Le
].color
;
4097 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4099 if (pb
->geqs
[Le
].color
== omega_black
)
4100 fprintf (dump_file
, " :::=> %s >= %d\n",
4101 omega_variable_to_str (pb
, 1),
4105 " :::=> [%s >= %d]\n",
4106 omega_variable_to_str (pb
, 1),
4112 int constantTerm
= pb
->geqs
[Le
].coef
[0];
4113 if (constantTerm
< upper_bound
||
4114 (constantTerm
== upper_bound
4115 && !omega_eqn_is_red (&pb
->geqs
[Le
],
4118 upper_bound
= constantTerm
;
4119 ub_color
= pb
->geqs
[Le
].color
;
4122 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4124 if (pb
->geqs
[Le
].color
== omega_black
)
4125 fprintf (dump_file
, " :::=> %s <= %d\n",
4126 omega_variable_to_str (pb
, 1),
4130 " :::=> [%s <= %d]\n",
4131 omega_variable_to_str (pb
, 1),
4137 for (Ue
= topeqn
; Ue
>= 0; Ue
--)
4138 if (pb
->geqs
[Ue
].coef
[i
] < 0
4139 && pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4141 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4142 int coefficient
= pb
->geqs
[Ue
].coef
[1] * Lc
4143 + pb
->geqs
[Le
].coef
[1] * Uc
;
4144 int constantTerm
= pb
->geqs
[Ue
].coef
[0] * Lc
4145 + pb
->geqs
[Le
].coef
[0] * Uc
;
4147 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4149 omega_print_geq_extra (dump_file
, pb
,
4151 fprintf (dump_file
, "\n");
4152 omega_print_geq_extra (dump_file
, pb
,
4154 fprintf (dump_file
, "\n");
4157 if (coefficient
> 0)
4159 constantTerm
= -int_div (constantTerm
, coefficient
);
4161 if (constantTerm
> lower_bound
4162 || (constantTerm
== lower_bound
4163 && (desired_res
!= omega_simplify
4164 || (pb
->geqs
[Ue
].color
== omega_black
4165 && pb
->geqs
[Le
].color
== omega_black
))))
4167 lower_bound
= constantTerm
;
4168 lb_color
= (pb
->geqs
[Ue
].color
== omega_red
4169 || pb
->geqs
[Le
].color
== omega_red
)
4170 ? omega_red
: omega_black
;
4173 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4175 if (pb
->geqs
[Ue
].color
== omega_red
4176 || pb
->geqs
[Le
].color
== omega_red
)
4178 " ::=> [%s >= %d]\n",
4179 omega_variable_to_str (pb
, 1),
4184 omega_variable_to_str (pb
, 1),
4190 constantTerm
= int_div (constantTerm
, -coefficient
);
4191 if (constantTerm
< upper_bound
4192 || (constantTerm
== upper_bound
4193 && pb
->geqs
[Ue
].color
== omega_black
4194 && pb
->geqs
[Le
].color
== omega_black
))
4196 upper_bound
= constantTerm
;
4197 ub_color
= (pb
->geqs
[Ue
].color
== omega_red
4198 || pb
->geqs
[Le
].color
== omega_red
)
4199 ? omega_red
: omega_black
;
4203 && (dump_flags
& TDF_DETAILS
))
4205 if (pb
->geqs
[Ue
].color
== omega_red
4206 || pb
->geqs
[Le
].color
== omega_red
)
4208 " ::=> [%s <= %d]\n",
4209 omega_variable_to_str (pb
, 1),
4214 omega_variable_to_str (pb
, 1),
4222 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4224 " therefore, %c%d <= %c%s%c <= %d%c\n",
4225 lb_color
== omega_red
? '[' : ' ', lower_bound
,
4226 (lb_color
== omega_red
&& ub_color
== omega_black
)
4228 omega_variable_to_str (pb
, 1),
4229 (lb_color
== omega_black
&& ub_color
== omega_red
)
4231 upper_bound
, ub_color
== omega_red
? ']' : ' ');
4233 if (lower_bound
> upper_bound
)
4236 if (pb
->safe_vars
== 1)
4238 if (upper_bound
== lower_bound
4239 && !(ub_color
== omega_red
|| lb_color
== omega_red
)
4240 && !please_no_equalities_in_simplified_problems
)
4243 pb
->eqs
[0].coef
[1] = -1;
4244 pb
->eqs
[0].coef
[0] = upper_bound
;
4246 if (ub_color
== omega_red
4247 || lb_color
== omega_red
)
4248 pb
->eqs
[0].color
= omega_red
;
4250 if (desired_res
== omega_simplify
4251 && pb
->eqs
[0].color
== omega_black
)
4252 return omega_solve_problem (pb
, desired_res
);
4255 if (upper_bound
!= pos_infinity
)
4257 pb
->geqs
[0].coef
[1] = -1;
4258 pb
->geqs
[0].coef
[0] = upper_bound
;
4259 pb
->geqs
[0].color
= ub_color
;
4260 pb
->geqs
[0].key
= -1;
4261 pb
->geqs
[0].touched
= 0;
4265 if (lower_bound
!= neg_infinity
)
4267 pb
->geqs
[pb
->num_geqs
].coef
[1] = 1;
4268 pb
->geqs
[pb
->num_geqs
].coef
[0] = -lower_bound
;
4269 pb
->geqs
[pb
->num_geqs
].color
= lb_color
;
4270 pb
->geqs
[pb
->num_geqs
].key
= 1;
4271 pb
->geqs
[pb
->num_geqs
].touched
= 0;
4276 if (desired_res
== omega_simplify
)
4278 omega_problem_reduced (pb
);
4284 && (desired_res
!= omega_simplify
4285 || (lb_color
== omega_black
4286 && ub_color
== omega_black
))
4287 && original_problem
!= no_problem
4288 && lower_bound
== upper_bound
)
4290 for (i
= original_problem
->num_vars
; i
>= 0; i
--)
4291 if (original_problem
->var
[i
] == pb
->var
[1])
4297 e
= original_problem
->num_eqs
++;
4298 omega_init_eqn_zero (&original_problem
->eqs
[e
],
4299 original_problem
->num_vars
);
4300 original_problem
->eqs
[e
].coef
[i
] = -1;
4301 original_problem
->eqs
[e
].coef
[0] = upper_bound
;
4303 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4306 "adding equality %d to outer problem\n", e
);
4307 omega_print_problem (dump_file
, original_problem
);
4314 eliminate_again
= true;
4316 if (lower_bound_count
== 1)
4318 eqn lbeqn
= omega_alloc_eqns (0, 1);
4319 int Lc
= pb
->geqs
[Le
].coef
[i
];
4321 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4322 fprintf (dump_file
, "an inplace elimination\n");
4324 omega_copy_eqn (lbeqn
, &pb
->geqs
[Le
], (n_vars
+ 1));
4325 omega_delete_geq_extra (pb
, Le
, n_vars
+ 1);
4327 for (Ue
= pb
->num_geqs
- 1; Ue
>= 0; Ue
--)
4328 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4330 if (lbeqn
->key
== -pb
->geqs
[Ue
].key
)
4331 omega_delete_geq_extra (pb
, Ue
, n_vars
+ 1);
4335 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4336 pb
->geqs
[Ue
].touched
= 1;
4337 eliminate_again
= false;
4339 if (lbeqn
->color
== omega_red
)
4340 pb
->geqs
[Ue
].color
= omega_red
;
4342 for (k
= 0; k
<= n_vars
; k
++)
4343 pb
->geqs
[Ue
].coef
[k
] =
4344 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4345 check_mul (lbeqn
->coef
[k
], Uc
);
4347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4349 omega_print_geq (dump_file
, pb
,
4351 fprintf (dump_file
, "\n");
4356 omega_free_eqns (lbeqn
, 1);
4361 int *dead_eqns
= XNEWVEC (int, OMEGA_MAX_GEQS
);
4362 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
4364 int top_eqn
= pb
->num_geqs
- 1;
4365 lower_bound_count
--;
4367 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4368 fprintf (dump_file
, "lower bound count = %d\n",
4371 for (Le
= top_eqn
; Le
>= 0; Le
--)
4372 if (pb
->geqs
[Le
].coef
[i
] > 0)
4374 int Lc
= pb
->geqs
[Le
].coef
[i
];
4375 for (Ue
= top_eqn
; Ue
>= 0; Ue
--)
4376 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4378 if (pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4381 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4384 e2
= pb
->num_geqs
++;
4386 e2
= dead_eqns
[--num_dead
];
4388 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4390 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4393 "Le = %d, Ue = %d, gen = %d\n",
4395 omega_print_geq_extra (dump_file
, pb
,
4397 fprintf (dump_file
, "\n");
4398 omega_print_geq_extra (dump_file
, pb
,
4400 fprintf (dump_file
, "\n");
4403 eliminate_again
= false;
4405 for (k
= n_vars
; k
>= 0; k
--)
4406 pb
->geqs
[e2
].coef
[k
] =
4407 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4408 check_mul (pb
->geqs
[Le
].coef
[k
], Uc
);
4410 pb
->geqs
[e2
].coef
[n_vars
+ 1] = 0;
4411 pb
->geqs
[e2
].touched
= 1;
4413 if (pb
->geqs
[Ue
].color
== omega_red
4414 || pb
->geqs
[Le
].color
== omega_red
)
4415 pb
->geqs
[e2
].color
= omega_red
;
4417 pb
->geqs
[e2
].color
= omega_black
;
4419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4421 omega_print_geq (dump_file
, pb
,
4423 fprintf (dump_file
, "\n");
4427 if (lower_bound_count
== 0)
4429 dead_eqns
[num_dead
++] = Ue
;
4431 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4432 fprintf (dump_file
, "Killed %d\n", Ue
);
4436 lower_bound_count
--;
4437 dead_eqns
[num_dead
++] = Le
;
4439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4440 fprintf (dump_file
, "Killed %d\n", Le
);
4443 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4446 while (num_dead
> 0)
4447 is_dead
[dead_eqns
[--num_dead
]] = true;
4449 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4451 omega_delete_geq_extra (pb
, e
, n_vars
+ 1);
4462 rS
= omega_alloc_problem (0, 0);
4463 iS
= omega_alloc_problem (0, 0);
4465 possible_easy_int_solution
= true;
4467 for (e
= 0; e
< pb
->num_geqs
; e
++)
4468 if (pb
->geqs
[e
].coef
[i
] == 0)
4470 omega_copy_eqn (&(rS
->geqs
[e2
]), &pb
->geqs
[e
],
4472 omega_copy_eqn (&(iS
->geqs
[e2
]), &pb
->geqs
[e
],
4475 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4478 fprintf (dump_file
, "Copying (%d, %d): ", i
,
4479 pb
->geqs
[e
].coef
[i
]);
4480 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[e
]);
4481 fprintf (dump_file
, "\n");
4482 for (t
= 0; t
<= n_vars
+ 1; t
++)
4483 fprintf (dump_file
, "%d ", pb
->geqs
[e
].coef
[t
]);
4484 fprintf (dump_file
, "\n");
4488 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4491 for (Le
= pb
->num_geqs
- 1; Le
>= 0; Le
--)
4492 if (pb
->geqs
[Le
].coef
[i
] > 0)
4493 for (Ue
= pb
->num_geqs
- 1; Ue
>= 0; Ue
--)
4494 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4497 int Lc
= pb
->geqs
[Le
].coef
[i
];
4498 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4500 if (pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4503 rS
->geqs
[e2
].touched
= iS
->geqs
[e2
].touched
= 1;
4505 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4507 fprintf (dump_file
, "---\n");
4509 "Le(Lc) = %d(%d_, Ue(Uc) = %d(%d), gen = %d\n",
4510 Le
, Lc
, Ue
, Uc
, e2
);
4511 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[Le
]);
4512 fprintf (dump_file
, "\n");
4513 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[Ue
]);
4514 fprintf (dump_file
, "\n");
4519 for (k
= n_vars
; k
>= 0; k
--)
4520 iS
->geqs
[e2
].coef
[k
] = rS
->geqs
[e2
].coef
[k
] =
4521 pb
->geqs
[Ue
].coef
[k
] + pb
->geqs
[Le
].coef
[k
];
4523 iS
->geqs
[e2
].coef
[0] -= (Uc
- 1);
4527 for (k
= n_vars
; k
>= 0; k
--)
4528 iS
->geqs
[e2
].coef
[k
] = rS
->geqs
[e2
].coef
[k
] =
4529 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4530 check_mul (pb
->geqs
[Le
].coef
[k
], Uc
);
4532 iS
->geqs
[e2
].coef
[0] -= (Uc
- 1) * (Lc
- 1);
4535 if (pb
->geqs
[Ue
].color
== omega_red
4536 || pb
->geqs
[Le
].color
== omega_red
)
4537 iS
->geqs
[e2
].color
= rS
->geqs
[e2
].color
= omega_red
;
4539 iS
->geqs
[e2
].color
= rS
->geqs
[e2
].color
= omega_black
;
4541 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4543 omega_print_geq (dump_file
, pb
, &(rS
->geqs
[e2
]));
4544 fprintf (dump_file
, "\n");
4548 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4550 else if (pb
->geqs
[Ue
].coef
[0] * Lc
+
4551 pb
->geqs
[Le
].coef
[0] * Uc
-
4552 (Uc
- 1) * (Lc
- 1) < 0)
4553 possible_easy_int_solution
= false;
4556 iS
->variables_initialized
= rS
->variables_initialized
= true;
4557 iS
->num_vars
= rS
->num_vars
= pb
->num_vars
;
4558 iS
->num_geqs
= rS
->num_geqs
= e2
;
4559 iS
->num_eqs
= rS
->num_eqs
= 0;
4560 iS
->num_subs
= rS
->num_subs
= pb
->num_subs
;
4561 iS
->safe_vars
= rS
->safe_vars
= pb
->safe_vars
;
4563 for (e
= n_vars
; e
>= 0; e
--)
4564 rS
->var
[e
] = pb
->var
[e
];
4566 for (e
= n_vars
; e
>= 0; e
--)
4567 iS
->var
[e
] = pb
->var
[e
];
4569 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
4571 omega_copy_eqn (&(rS
->subs
[e
]), &(pb
->subs
[e
]), pb
->num_vars
);
4572 omega_copy_eqn (&(iS
->subs
[e
]), &(pb
->subs
[e
]), pb
->num_vars
);
4576 n_vars
= pb
->num_vars
;
4578 if (desired_res
!= omega_true
)
4580 if (original_problem
== no_problem
)
4582 original_problem
= pb
;
4583 result
= omega_solve_geq (rS
, omega_false
);
4584 original_problem
= no_problem
;
4587 result
= omega_solve_geq (rS
, omega_false
);
4589 if (result
== omega_false
)
4596 if (pb
->num_eqs
> 0)
4598 /* An equality constraint must have been found */
4601 return omega_solve_problem (pb
, desired_res
);
4605 if (desired_res
!= omega_false
)
4608 int lower_bounds
= 0;
4609 int *lower_bound
= XNEWVEC (int, OMEGA_MAX_GEQS
);
4611 if (possible_easy_int_solution
)
4614 result
= omega_solve_geq (iS
, desired_res
);
4617 if (result
!= omega_false
)
4626 if (!exact
&& best_parallel_eqn
>= 0
4627 && parallel_difference
<= max_splinters
)
4632 return parallel_splinter (pb
, best_parallel_eqn
,
4633 parallel_difference
,
4637 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4638 fprintf (dump_file
, "have to do exact analysis\n");
4642 for (e
= 0; e
< pb
->num_geqs
; e
++)
4643 if (pb
->geqs
[e
].coef
[i
] > 1)
4644 lower_bound
[lower_bounds
++] = e
;
4646 /* Sort array LOWER_BOUND. */
4647 for (j
= 0; j
< lower_bounds
; j
++)
4649 int k
, smallest
= j
;
4651 for (k
= j
+ 1; k
< lower_bounds
; k
++)
4652 if (pb
->geqs
[lower_bound
[smallest
]].coef
[i
] >
4653 pb
->geqs
[lower_bound
[k
]].coef
[i
])
4656 k
= lower_bound
[smallest
];
4657 lower_bound
[smallest
] = lower_bound
[j
];
4661 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4663 fprintf (dump_file
, "lower bound coefficients = ");
4665 for (j
= 0; j
< lower_bounds
; j
++)
4666 fprintf (dump_file
, " %d",
4667 pb
->geqs
[lower_bound
[j
]].coef
[i
]);
4669 fprintf (dump_file
, "\n");
4672 for (j
= 0; j
< lower_bounds
; j
++)
4676 int worst_lower_bound_constant
= -minC
;
4679 max_incr
= (((pb
->geqs
[e
].coef
[i
] - 1) *
4680 (worst_lower_bound_constant
- 1) - 1)
4681 / worst_lower_bound_constant
);
4682 /* max_incr += 2; */
4684 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4686 fprintf (dump_file
, "for equation ");
4687 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
4689 "\ntry decrements from 0 to %d\n",
4691 omega_print_problem (dump_file
, pb
);
4694 if (max_incr
> 50 && !smoothed
4695 && smooth_weird_equations (pb
))
4701 goto solve_geq_start
;
4704 omega_copy_eqn (&pb
->eqs
[0], &pb
->geqs
[e
],
4706 pb
->eqs
[0].color
= omega_black
;
4707 omega_init_eqn_zero (&pb
->geqs
[e
], pb
->num_vars
);
4708 pb
->geqs
[e
].touched
= 1;
4711 for (c
= max_incr
; c
>= 0; c
--)
4713 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4716 "trying next decrement of %d\n",
4718 omega_print_problem (dump_file
, pb
);
4721 omega_copy_problem (rS
, pb
);
4723 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4724 omega_print_problem (dump_file
, rS
);
4726 result
= omega_solve_problem (rS
, desired_res
);
4728 if (result
== omega_true
)
4737 pb
->eqs
[0].coef
[0]--;
4740 if (j
+ 1 < lower_bounds
)
4743 omega_copy_eqn (&pb
->geqs
[e
], &pb
->eqs
[0],
4745 pb
->geqs
[e
].touched
= 1;
4746 pb
->geqs
[e
].color
= omega_black
;
4747 omega_copy_problem (rS
, pb
);
4749 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4751 "exhausted lower bound, "
4752 "checking if still feasible ");
4754 result
= omega_solve_problem (rS
, omega_false
);
4756 if (result
== omega_false
)
4761 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4762 fprintf (dump_file
, "fall-off the end\n");
4774 return omega_unknown
;
4775 } while (eliminate_again
);
4779 /* Because the omega solver is recursive, this counter limits the
4781 static int omega_solve_depth
= 0;
4783 /* Return omega_true when the problem PB has a solution following the
4787 omega_solve_problem (omega_pb pb
, enum omega_result desired_res
)
4789 enum omega_result result
;
4791 gcc_assert (pb
->num_vars
>= pb
->safe_vars
);
4792 omega_solve_depth
++;
4794 if (desired_res
!= omega_simplify
)
4797 if (omega_solve_depth
> 50)
4799 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4802 "Solve depth = %d, in_approximate_mode = %d, aborting\n",
4803 omega_solve_depth
, in_approximate_mode
);
4804 omega_print_problem (dump_file
, pb
);
4809 if (omega_solve_eq (pb
, desired_res
) == omega_false
)
4811 omega_solve_depth
--;
4815 if (in_approximate_mode
&& !pb
->num_geqs
)
4817 result
= omega_true
;
4818 pb
->num_vars
= pb
->safe_vars
;
4819 omega_problem_reduced (pb
);
4822 result
= omega_solve_geq (pb
, desired_res
);
4824 omega_solve_depth
--;
4826 if (!omega_reduce_with_subs
)
4828 resurrect_subs (pb
);
4829 gcc_assert (please_no_equalities_in_simplified_problems
4830 || !result
|| pb
->num_subs
== 0);
4836 /* Return true if red equations constrain the set of possible solutions.
4837 We assume that there are solutions to the black equations by
4838 themselves, so if there is no solution to the combined problem, we
4842 omega_problem_has_red_equations (omega_pb pb
)
4848 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4850 fprintf (dump_file
, "Checking for red equations:\n");
4851 omega_print_problem (dump_file
, pb
);
4854 please_no_equalities_in_simplified_problems
++;
4857 if (omega_single_result
)
4858 return_single_result
++;
4860 create_color
= true;
4861 result
= (omega_simplify_problem (pb
) == omega_false
);
4863 if (omega_single_result
)
4864 return_single_result
--;
4867 please_no_equalities_in_simplified_problems
--;
4871 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4872 fprintf (dump_file
, "Gist is FALSE\n");
4877 pb
->eqs
[0].color
= omega_red
;
4879 for (i
= pb
->num_vars
; i
> 0; i
--)
4880 pb
->eqs
[0].coef
[i
] = 0;
4882 pb
->eqs
[0].coef
[0] = 1;
4886 free_red_eliminations (pb
);
4887 gcc_assert (pb
->num_eqs
== 0);
4889 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4890 if (pb
->geqs
[e
].color
== omega_red
)
4896 for (i
= pb
->safe_vars
; i
>= 1; i
--)
4901 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4903 if (pb
->geqs
[e
].coef
[i
])
4905 if (pb
->geqs
[e
].coef
[i
] > 0)
4906 lb
|= (1 + (pb
->geqs
[e
].color
== omega_red
? 1 : 0));
4909 ub
|= (1 + (pb
->geqs
[e
].color
== omega_red
? 1 : 0));
4913 if (ub
== 2 || lb
== 2)
4916 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4917 fprintf (dump_file
, "checks for upper/lower bounds worked!\n");
4919 if (!omega_reduce_with_subs
)
4921 resurrect_subs (pb
);
4922 gcc_assert (pb
->num_subs
== 0);
4930 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4932 "*** Doing potentially expensive elimination tests "
4933 "for red equations\n");
4935 please_no_equalities_in_simplified_problems
++;
4936 omega_eliminate_red (pb
, true);
4937 please_no_equalities_in_simplified_problems
--;
4940 gcc_assert (pb
->num_eqs
== 0);
4942 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4943 if (pb
->geqs
[e
].color
== omega_red
)
4946 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4950 "******************** Redundant Red Equations eliminated!!\n");
4953 "******************** Red Equations remain\n");
4955 omega_print_problem (dump_file
, pb
);
4958 if (!omega_reduce_with_subs
)
4960 normalize_return_type r
;
4962 resurrect_subs (pb
);
4963 r
= normalize_omega_problem (pb
);
4964 gcc_assert (r
!= normalize_false
);
4967 cleanout_wildcards (pb
);
4968 gcc_assert (pb
->num_subs
== 0);
4974 /* Calls omega_simplify_problem in approximate mode. */
4977 omega_simplify_approximate (omega_pb pb
)
4979 enum omega_result result
;
4981 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4982 fprintf (dump_file
, "(Entering approximate mode\n");
4984 in_approximate_mode
= true;
4985 result
= omega_simplify_problem (pb
);
4986 in_approximate_mode
= false;
4988 gcc_assert (pb
->num_vars
== pb
->safe_vars
);
4989 if (!omega_reduce_with_subs
)
4990 gcc_assert (pb
->num_subs
== 0);
4992 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4993 fprintf (dump_file
, "Leaving approximate mode)\n");
4999 /* Simplifies problem PB by eliminating redundant constraints and
5000 reducing the constraints system to a minimal form. Returns
5001 omega_true when the problem was successfully reduced, omega_unknown
5002 when the solver is unable to determine an answer. */
5005 omega_simplify_problem (omega_pb pb
)
5009 omega_found_reduction
= omega_false
;
5011 if (!pb
->variables_initialized
)
5012 omega_initialize_variables (pb
);
5014 if (next_key
* 3 > MAX_KEYS
)
5019 next_key
= OMEGA_MAX_VARS
+ 1;
5021 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5022 pb
->geqs
[e
].touched
= 1;
5024 for (i
= 0; i
< HASH_TABLE_SIZE
; i
++)
5025 hash_master
[i
].touched
= -1;
5027 pb
->hash_version
= hash_version
;
5030 else if (pb
->hash_version
!= hash_version
)
5034 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5035 pb
->geqs
[e
].touched
= 1;
5037 pb
->hash_version
= hash_version
;
5040 if (pb
->num_vars
> pb
->num_eqs
+ 3 * pb
->safe_vars
)
5041 omega_free_eliminations (pb
, pb
->safe_vars
);
5043 if (!may_be_red
&& pb
->num_subs
== 0 && pb
->safe_vars
== 0)
5045 omega_found_reduction
= omega_solve_problem (pb
, omega_unknown
);
5047 if (omega_found_reduction
!= omega_false
5048 && !return_single_result
)
5052 (*omega_when_reduced
) (pb
);
5055 return omega_found_reduction
;
5058 omega_solve_problem (pb
, omega_simplify
);
5060 if (omega_found_reduction
!= omega_false
)
5062 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
5063 pb
->forwarding_address
[pb
->var
[i
]] = i
;
5065 for (i
= 0; i
< pb
->num_subs
; i
++)
5066 pb
->forwarding_address
[pb
->subs
[i
].key
] = -i
- 1;
5069 if (!omega_reduce_with_subs
)
5070 gcc_assert (please_no_equalities_in_simplified_problems
5071 || omega_found_reduction
== omega_false
5072 || pb
->num_subs
== 0);
5074 return omega_found_reduction
;
5077 /* Make variable VAR unprotected: it then can be eliminated. */
5080 omega_unprotect_variable (omega_pb pb
, int var
)
5083 idx
= pb
->forwarding_address
[var
];
5090 if (idx
< pb
->num_subs
)
5092 omega_copy_eqn (&pb
->subs
[idx
], &pb
->subs
[pb
->num_subs
],
5094 pb
->forwarding_address
[pb
->subs
[idx
].key
] = -idx
- 1;
5099 int *bring_to_life
= XNEWVEC (int, OMEGA_MAX_VARS
);
5102 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5103 bring_to_life
[e
] = (pb
->subs
[e
].coef
[idx
] != 0);
5105 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
5106 if (bring_to_life
[e2
])
5111 if (pb
->safe_vars
< pb
->num_vars
)
5113 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5115 pb
->geqs
[e
].coef
[pb
->num_vars
] =
5116 pb
->geqs
[e
].coef
[pb
->safe_vars
];
5118 pb
->geqs
[e
].coef
[pb
->safe_vars
] = 0;
5121 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5123 pb
->eqs
[e
].coef
[pb
->num_vars
] =
5124 pb
->eqs
[e
].coef
[pb
->safe_vars
];
5126 pb
->eqs
[e
].coef
[pb
->safe_vars
] = 0;
5129 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5131 pb
->subs
[e
].coef
[pb
->num_vars
] =
5132 pb
->subs
[e
].coef
[pb
->safe_vars
];
5134 pb
->subs
[e
].coef
[pb
->safe_vars
] = 0;
5137 pb
->var
[pb
->num_vars
] = pb
->var
[pb
->safe_vars
];
5138 pb
->forwarding_address
[pb
->var
[pb
->num_vars
]] =
5143 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5144 pb
->geqs
[e
].coef
[pb
->safe_vars
] = 0;
5146 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5147 pb
->eqs
[e
].coef
[pb
->safe_vars
] = 0;
5149 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5150 pb
->subs
[e
].coef
[pb
->safe_vars
] = 0;
5153 pb
->var
[pb
->safe_vars
] = pb
->subs
[e2
].key
;
5154 pb
->forwarding_address
[pb
->subs
[e2
].key
] = pb
->safe_vars
;
5156 omega_copy_eqn (&(pb
->eqs
[pb
->num_eqs
]), &(pb
->subs
[e2
]),
5158 pb
->eqs
[pb
->num_eqs
++].coef
[pb
->safe_vars
] = -1;
5159 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5161 if (e2
< pb
->num_subs
- 1)
5162 omega_copy_eqn (&(pb
->subs
[e2
]), &(pb
->subs
[pb
->num_subs
- 1]),
5168 omega_unprotect_1 (pb
, &idx
, NULL
);
5169 free (bring_to_life
);
5172 chain_unprotect (pb
);
5175 /* Unprotects VAR and simplifies PB. */
5178 omega_constrain_variable_sign (omega_pb pb
, enum omega_eqn_color color
,
5181 int n_vars
= pb
->num_vars
;
5183 int k
= pb
->forwarding_address
[var
];
5192 omega_copy_eqn (&pb
->geqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5194 for (j
= 0; j
<= n_vars
; j
++)
5195 pb
->geqs
[e
].coef
[j
] *= sign
;
5197 pb
->geqs
[e
].coef
[0]--;
5198 pb
->geqs
[e
].touched
= 1;
5199 pb
->geqs
[e
].color
= color
;
5204 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5205 omega_copy_eqn (&pb
->eqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5206 pb
->eqs
[e
].color
= color
;
5212 omega_init_eqn_zero (&pb
->geqs
[e
], pb
->num_vars
);
5213 pb
->geqs
[e
].coef
[k
] = sign
;
5214 pb
->geqs
[e
].coef
[0] = -1;
5215 pb
->geqs
[e
].touched
= 1;
5216 pb
->geqs
[e
].color
= color
;
5221 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5222 omega_init_eqn_zero (&pb
->eqs
[e
], pb
->num_vars
);
5223 pb
->eqs
[e
].coef
[k
] = 1;
5224 pb
->eqs
[e
].color
= color
;
5227 omega_unprotect_variable (pb
, var
);
5228 return omega_simplify_problem (pb
);
5231 /* Add an equation "VAR = VALUE" with COLOR to PB. */
5234 omega_constrain_variable_value (omega_pb pb
, enum omega_eqn_color color
,
5238 int k
= pb
->forwarding_address
[var
];
5244 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5245 omega_copy_eqn (&pb
->eqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5246 pb
->eqs
[e
].coef
[0] -= value
;
5251 omega_init_eqn_zero (&pb
->eqs
[e
], pb
->num_vars
);
5252 pb
->eqs
[e
].coef
[k
] = 1;
5253 pb
->eqs
[e
].coef
[0] = -value
;
5256 pb
->eqs
[e
].color
= color
;
5259 /* Return false when the upper and lower bounds are not coupled.
5260 Initialize the bounds LOWER_BOUND and UPPER_BOUND for the values of
5264 omega_query_variable (omega_pb pb
, int i
, int *lower_bound
, int *upper_bound
)
5266 int n_vars
= pb
->num_vars
;
5269 bool coupled
= false;
5271 *lower_bound
= neg_infinity
;
5272 *upper_bound
= pos_infinity
;
5273 i
= pb
->forwarding_address
[i
];
5279 for (j
= 1; j
<= n_vars
; j
++)
5280 if (pb
->subs
[i
].coef
[j
] != 0)
5283 *upper_bound
= *lower_bound
= pb
->subs
[i
].coef
[0];
5287 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5288 if (pb
->subs
[e
].coef
[i
] != 0)
5291 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5292 if (pb
->eqs
[e
].coef
[i
] != 0)
5296 for (j
= 1; j
<= n_vars
; j
++)
5297 if (i
!= j
&& pb
->eqs
[e
].coef
[j
] != 0)
5308 *lower_bound
= *upper_bound
=
5309 -pb
->eqs
[e
].coef
[i
] * pb
->eqs
[e
].coef
[0];
5314 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5315 if (pb
->geqs
[e
].coef
[i
] != 0)
5317 if (pb
->geqs
[e
].key
== i
)
5318 *lower_bound
= MAX (*lower_bound
, -pb
->geqs
[e
].coef
[0]);
5320 else if (pb
->geqs
[e
].key
== -i
)
5321 *upper_bound
= MIN (*upper_bound
, pb
->geqs
[e
].coef
[0]);
5330 /* Sets the lower bound L and upper bound U for the values of variable
5331 I, and sets COULD_BE_ZERO to true if variable I might take value
5332 zero. LOWER_BOUND and UPPER_BOUND are bounds on the values of
5336 query_coupled_variable (omega_pb pb
, int i
, int *l
, int *u
,
5337 bool *could_be_zero
, int lower_bound
, int upper_bound
)
5344 /* Preconditions. */
5345 gcc_assert (abs (pb
->forwarding_address
[i
]) == 1
5346 && pb
->num_vars
+ pb
->num_subs
== 2
5347 && pb
->num_eqs
+ pb
->num_subs
== 1);
5349 /* Define variable I in terms of variable V. */
5350 if (pb
->forwarding_address
[i
] == -1)
5359 sign
= -eqn
->coef
[1];
5363 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5364 if (pb
->geqs
[e
].coef
[v
] != 0)
5366 if (pb
->geqs
[e
].coef
[v
] == 1)
5367 lower_bound
= MAX (lower_bound
, -pb
->geqs
[e
].coef
[0]);
5370 upper_bound
= MIN (upper_bound
, pb
->geqs
[e
].coef
[0]);
5373 if (lower_bound
> upper_bound
)
5381 if (lower_bound
== neg_infinity
)
5383 if (eqn
->coef
[v
] > 0)
5384 b1
= sign
* neg_infinity
;
5387 b1
= -sign
* neg_infinity
;
5390 b1
= sign
* (eqn
->coef
[0] + eqn
->coef
[v
] * lower_bound
);
5392 if (upper_bound
== pos_infinity
)
5394 if (eqn
->coef
[v
] > 0)
5395 b2
= sign
* pos_infinity
;
5398 b2
= -sign
* pos_infinity
;
5401 b2
= sign
* (eqn
->coef
[0] + eqn
->coef
[v
] * upper_bound
);
5403 *l
= MAX (*l
, b1
<= b2
? b1
: b2
);
5404 *u
= MIN (*u
, b1
<= b2
? b2
: b1
);
5406 *could_be_zero
= (*l
<= 0 && 0 <= *u
5407 && int_mod (eqn
->coef
[0], abs (eqn
->coef
[v
])) == 0);
5410 /* Return false when a lower bound L and an upper bound U for variable
5411 I in problem PB have been initialized. */
5414 omega_query_variable_bounds (omega_pb pb
, int i
, int *l
, int *u
)
5419 if (!omega_query_variable (pb
, i
, l
, u
)
5420 || (pb
->num_vars
== 1 && pb
->forwarding_address
[i
] == 1))
5423 if (abs (pb
->forwarding_address
[i
]) == 1
5424 && pb
->num_vars
+ pb
->num_subs
== 2
5425 && pb
->num_eqs
+ pb
->num_subs
== 1)
5428 query_coupled_variable (pb
, i
, l
, u
, &could_be_zero
, neg_infinity
,
5436 /* For problem PB, return an integer that represents the classic data
5437 dependence direction in function of the DD_LT, DD_EQ and DD_GT bit
5438 masks that are added to the result. When DIST_KNOWN is true, DIST
5439 is set to the classic data dependence distance. LOWER_BOUND and
5440 UPPER_BOUND are bounds on the value of variable I, for example, it
5441 is possible to narrow the iteration domain with safe approximations
5442 of loop counts, and thus discard some data dependences that cannot
5446 omega_query_variable_signs (omega_pb pb
, int i
, int dd_lt
,
5447 int dd_eq
, int dd_gt
, int lower_bound
,
5448 int upper_bound
, bool *dist_known
, int *dist
)
5457 omega_query_variable (pb
, i
, &l
, &u
);
5458 query_coupled_variable (pb
, i
, &l
, &u
, &could_be_zero
, lower_bound
,
5477 *dist_known
= false;
5482 /* Initialize PB as an Omega problem with NVARS variables and NPROT
5483 safe variables. Safe variables are not eliminated during the
5484 Fourier-Motzkin elimination. Safe variables are all those
5485 variables that are placed at the beginning of the array of
5486 variables: P->var[0, ..., NPROT - 1]. */
5489 omega_alloc_problem (int nvars
, int nprot
)
5493 gcc_assert (nvars
<= OMEGA_MAX_VARS
);
5494 omega_initialize ();
5496 /* Allocate and initialize PB. */
5497 pb
= XCNEW (struct omega_pb_d
);
5498 pb
->var
= XCNEWVEC (int, OMEGA_MAX_VARS
+ 2);
5499 pb
->forwarding_address
= XCNEWVEC (int, OMEGA_MAX_VARS
+ 2);
5500 pb
->geqs
= omega_alloc_eqns (0, OMEGA_MAX_GEQS
);
5501 pb
->eqs
= omega_alloc_eqns (0, OMEGA_MAX_EQS
);
5502 pb
->subs
= omega_alloc_eqns (0, OMEGA_MAX_VARS
+ 1);
5504 pb
->hash_version
= hash_version
;
5505 pb
->num_vars
= nvars
;
5506 pb
->safe_vars
= nprot
;
5507 pb
->variables_initialized
= false;
5508 pb
->variables_freed
= false;
5515 /* Keeps the state of the initialization. */
5516 static bool omega_initialized
= false;
5518 /* Initialization of the Omega solver. */
5521 omega_initialize (void)
5525 if (omega_initialized
)
5529 next_key
= OMEGA_MAX_VARS
+ 1;
5530 packing
= XCNEWVEC (int, OMEGA_MAX_VARS
);
5531 fast_lookup
= XCNEWVEC (int, MAX_KEYS
* 2);
5532 fast_lookup_red
= XCNEWVEC (int, MAX_KEYS
* 2);
5533 hash_master
= omega_alloc_eqns (0, HASH_TABLE_SIZE
);
5535 for (i
= 0; i
< HASH_TABLE_SIZE
; i
++)
5536 hash_master
[i
].touched
= -1;
5538 sprintf (wild_name
[0], "1");
5539 sprintf (wild_name
[1], "a");
5540 sprintf (wild_name
[2], "b");
5541 sprintf (wild_name
[3], "c");
5542 sprintf (wild_name
[4], "d");
5543 sprintf (wild_name
[5], "e");
5544 sprintf (wild_name
[6], "f");
5545 sprintf (wild_name
[7], "g");
5546 sprintf (wild_name
[8], "h");
5547 sprintf (wild_name
[9], "i");
5548 sprintf (wild_name
[10], "j");
5549 sprintf (wild_name
[11], "k");
5550 sprintf (wild_name
[12], "l");
5551 sprintf (wild_name
[13], "m");
5552 sprintf (wild_name
[14], "n");
5553 sprintf (wild_name
[15], "o");
5554 sprintf (wild_name
[16], "p");
5555 sprintf (wild_name
[17], "q");
5556 sprintf (wild_name
[18], "r");
5557 sprintf (wild_name
[19], "s");
5558 sprintf (wild_name
[20], "t");
5559 sprintf (wild_name
[40 - 1], "alpha");
5560 sprintf (wild_name
[40 - 2], "beta");
5561 sprintf (wild_name
[40 - 3], "gamma");
5562 sprintf (wild_name
[40 - 4], "delta");
5563 sprintf (wild_name
[40 - 5], "tau");
5564 sprintf (wild_name
[40 - 6], "sigma");
5565 sprintf (wild_name
[40 - 7], "chi");
5566 sprintf (wild_name
[40 - 8], "omega");
5567 sprintf (wild_name
[40 - 9], "pi");
5568 sprintf (wild_name
[40 - 10], "ni");
5569 sprintf (wild_name
[40 - 11], "Alpha");
5570 sprintf (wild_name
[40 - 12], "Beta");
5571 sprintf (wild_name
[40 - 13], "Gamma");
5572 sprintf (wild_name
[40 - 14], "Delta");
5573 sprintf (wild_name
[40 - 15], "Tau");
5574 sprintf (wild_name
[40 - 16], "Sigma");
5575 sprintf (wild_name
[40 - 17], "Chi");
5576 sprintf (wild_name
[40 - 18], "Omega");
5577 sprintf (wild_name
[40 - 19], "xxx");
5579 omega_initialized
= true;