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
)
2217 /* verify alpha1*v1+alpha2*v2 = alpha3*v3 */
2218 for (k
= pb
->num_vars
; k
>= 1; k
--)
2219 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2220 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2221 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2224 c
= (alpha1
* pb
->geqs
[e1
].coef
[0]
2225 + alpha2
* pb
->geqs
[e2
].coef
[0]);
2227 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0]))
2229 /* We just proved e3 < 0, so no solutions exist. */
2230 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2233 "found implied over tight inequality\n");
2235 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2236 alpha1
, alpha2
, -alpha3
);
2237 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2238 fprintf (dump_file
, "\n");
2239 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2240 fprintf (dump_file
, "\n=> not ");
2241 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2242 fprintf (dump_file
, "\n\n");
2250 else if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] - 1))
2252 /* We just proved that e3 <=0, so e3 = 0. */
2253 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2256 "found implied tight inequality\n");
2258 "alpha1, alpha2, alpha3 = %d,%d,%d\n",
2259 alpha1
, alpha2
, -alpha3
);
2260 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e1
]));
2261 fprintf (dump_file
, "\n");
2262 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2263 fprintf (dump_file
, "\n=> inverse ");
2264 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2265 fprintf (dump_file
, "\n\n");
2268 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
++],
2269 &pb
->geqs
[e3
], pb
->num_vars
);
2270 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2271 adding_equality_constraint (pb
, pb
->num_eqs
- 1);
2279 /* Delete the inequalities that were marked as dead. */
2280 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2282 omega_delete_geq (pb
, e
, pb
->num_vars
);
2285 goto eliminate_redundant_done
;
2287 tmp_problem
= XNEW (struct omega_pb_d
);
2290 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2292 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2295 "checking equation %d to see if it is redundant: ", e
);
2296 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2297 fprintf (dump_file
, "\n");
2300 omega_copy_problem (tmp_problem
, pb
);
2301 omega_negate_geq (tmp_problem
, e
);
2302 tmp_problem
->safe_vars
= 0;
2303 tmp_problem
->variables_freed
= false;
2305 if (omega_solve_problem (tmp_problem
, omega_false
) == omega_false
)
2306 omega_delete_geq (pb
, e
, pb
->num_vars
);
2312 if (!omega_reduce_with_subs
)
2314 resurrect_subs (pb
);
2315 gcc_assert (please_no_equalities_in_simplified_problems
2316 || pb
->num_subs
== 0);
2319 eliminate_redundant_done
:
2327 /* For each inequality that has coefficients bigger than 20, try to
2328 create a new constraint that cannot be derived from the original
2329 constraint and that has smaller coefficients. Add the new
2330 constraint at the end of geqs. Return the number of inequalities
2331 that have been added to PB. */
2334 smooth_weird_equations (omega_pb pb
)
2336 int e1
, e2
, e3
, p
, q
, k
, alpha
, alpha1
, alpha2
, alpha3
;
2341 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
2342 if (pb
->geqs
[e1
].color
== omega_black
)
2346 for (v
= pb
->num_vars
; v
>= 1; v
--)
2347 if (pb
->geqs
[e1
].coef
[v
] != 0 && abs (pb
->geqs
[e1
].coef
[v
]) < g
)
2348 g
= abs (pb
->geqs
[e1
].coef
[v
]);
2355 for (v
= pb
->num_vars
; v
>= 1; v
--)
2356 pb
->geqs
[e3
].coef
[v
] = int_div (6 * pb
->geqs
[e1
].coef
[v
] + g
/ 2,
2359 pb
->geqs
[e3
].color
= omega_black
;
2360 pb
->geqs
[e3
].touched
= 1;
2362 pb
->geqs
[e3
].coef
[0] = 9997;
2364 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2366 fprintf (dump_file
, "Checking to see if we can derive: ");
2367 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e3
]);
2368 fprintf (dump_file
, "\n from: ");
2369 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e1
]);
2370 fprintf (dump_file
, "\n");
2373 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
2374 if (e1
!= e2
&& pb
->geqs
[e2
].color
== omega_black
)
2376 for (p
= pb
->num_vars
; p
> 1; p
--)
2378 for (q
= p
- 1; q
> 0; q
--)
2381 (pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e2
].coef
[q
] -
2382 pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e1
].coef
[q
]);
2391 alpha1
= (pb
->geqs
[e2
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2392 - pb
->geqs
[e2
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2393 alpha2
= -(pb
->geqs
[e1
].coef
[q
] * pb
->geqs
[e3
].coef
[p
]
2394 - pb
->geqs
[e1
].coef
[p
] * pb
->geqs
[e3
].coef
[q
]);
2397 if (alpha1
* alpha2
<= 0)
2409 /* Try to prove e3 is redundant: verify
2410 alpha1*v1 + alpha2*v2 = alpha3*v3. */
2411 for (k
= pb
->num_vars
; k
>= 1; k
--)
2412 if (alpha3
* pb
->geqs
[e3
].coef
[k
]
2413 != (alpha1
* pb
->geqs
[e1
].coef
[k
]
2414 + alpha2
* pb
->geqs
[e2
].coef
[k
]))
2417 c
= alpha1
* pb
->geqs
[e1
].coef
[0]
2418 + alpha2
* pb
->geqs
[e2
].coef
[0];
2420 if (c
< alpha3
* (pb
->geqs
[e3
].coef
[0] + 1))
2421 pb
->geqs
[e3
].coef
[0] = int_div (c
, alpha3
);
2426 if (pb
->geqs
[e3
].coef
[0] < 9997)
2431 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2434 "Smoothing weird equations; adding:\n");
2435 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e3
]);
2436 fprintf (dump_file
, "\nto:\n");
2437 omega_print_problem (dump_file
, pb
);
2438 fprintf (dump_file
, "\n\n");
2446 /* Replace tuples of inequalities, that define upper and lower half
2447 spaces, with an equation. */
2450 coalesce (omega_pb pb
)
2455 int found_something
= 0;
2457 for (e
= 0; e
< pb
->num_geqs
; e
++)
2458 if (pb
->geqs
[e
].color
== omega_red
)
2464 is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2466 for (e
= 0; e
< pb
->num_geqs
; e
++)
2469 for (e
= 0; e
< pb
->num_geqs
; e
++)
2470 if (pb
->geqs
[e
].color
== omega_red
2471 && !pb
->geqs
[e
].touched
)
2472 for (e2
= e
+ 1; e2
< pb
->num_geqs
; e2
++)
2473 if (!pb
->geqs
[e2
].touched
2474 && pb
->geqs
[e
].key
== -pb
->geqs
[e2
].key
2475 && pb
->geqs
[e
].coef
[0] == -pb
->geqs
[e2
].coef
[0]
2476 && pb
->geqs
[e2
].color
== omega_red
)
2478 omega_copy_eqn (&pb
->eqs
[pb
->num_eqs
++], &pb
->geqs
[e
],
2480 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
2486 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2488 omega_delete_geq (pb
, e
, pb
->num_vars
);
2490 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && found_something
)
2492 fprintf (dump_file
, "Coalesced pb->geqs into %d EQ's:\n",
2494 omega_print_problem (dump_file
, pb
);
2500 /* Eliminate red inequalities from PB. When ELIMINATE_ALL is
2501 true, continue to eliminate all the red inequalities. */
2504 omega_eliminate_red (omega_pb pb
, bool eliminate_all
)
2506 int e
, e2
, e3
, i
, j
, k
, a
, alpha1
, alpha2
;
2508 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2511 omega_pb tmp_problem
;
2513 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2515 fprintf (dump_file
, "in eliminate RED:\n");
2516 omega_print_problem (dump_file
, pb
);
2519 if (pb
->num_eqs
> 0)
2520 omega_simplify_problem (pb
);
2522 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2525 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2526 if (pb
->geqs
[e
].color
== omega_black
&& !is_dead
[e
])
2527 for (e2
= e
- 1; e2
>= 0; e2
--)
2528 if (pb
->geqs
[e2
].color
== omega_black
2533 for (i
= pb
->num_vars
; i
> 1; i
--)
2534 for (j
= i
- 1; j
> 0; j
--)
2535 if ((a
= (pb
->geqs
[e
].coef
[i
] * pb
->geqs
[e2
].coef
[j
]
2536 - pb
->geqs
[e2
].coef
[i
] * pb
->geqs
[e
].coef
[j
])) != 0)
2542 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2545 "found two equations to combine, i = %s, ",
2546 omega_variable_to_str (pb
, i
));
2547 fprintf (dump_file
, "j = %s, alpha = %d\n",
2548 omega_variable_to_str (pb
, j
), a
);
2549 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2550 fprintf (dump_file
, "\n");
2551 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2552 fprintf (dump_file
, "\n");
2555 for (e3
= pb
->num_geqs
- 1; e3
>= 0; e3
--)
2556 if (pb
->geqs
[e3
].color
== omega_red
)
2558 alpha1
= (pb
->geqs
[e2
].coef
[j
] * pb
->geqs
[e3
].coef
[i
]
2559 - pb
->geqs
[e2
].coef
[i
] * pb
->geqs
[e3
].coef
[j
]);
2560 alpha2
= -(pb
->geqs
[e
].coef
[j
] * pb
->geqs
[e3
].coef
[i
]
2561 - pb
->geqs
[e
].coef
[i
] * pb
->geqs
[e3
].coef
[j
]);
2563 if ((a
> 0 && alpha1
> 0 && alpha2
> 0)
2564 || (a
< 0 && alpha1
< 0 && alpha2
< 0))
2566 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2569 "alpha1 = %d, alpha2 = %d;"
2570 "comparing against: ",
2572 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2573 fprintf (dump_file
, "\n");
2576 for (k
= pb
->num_vars
; k
>= 0; k
--)
2578 c
= (alpha1
* pb
->geqs
[e
].coef
[k
]
2579 + alpha2
* pb
->geqs
[e2
].coef
[k
]);
2581 if (c
!= a
* pb
->geqs
[e3
].coef
[k
])
2584 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && k
> 0)
2585 fprintf (dump_file
, " %s: %d, %d\n",
2586 omega_variable_to_str (pb
, k
), c
,
2587 a
* pb
->geqs
[e3
].coef
[k
]);
2592 ((a
> 0 && c
< a
* pb
->geqs
[e3
].coef
[k
])
2593 || (a
< 0 && c
> a
* pb
->geqs
[e3
].coef
[k
]))))
2595 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2599 "red equation#%d is dead "
2600 "(%d dead so far, %d remain)\n",
2602 pb
->num_geqs
- dead_count
);
2603 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2604 fprintf (dump_file
, "\n");
2605 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e2
]));
2606 fprintf (dump_file
, "\n");
2607 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e3
]));
2608 fprintf (dump_file
, "\n");
2616 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2618 omega_delete_geq (pb
, e
, pb
->num_vars
);
2622 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2624 fprintf (dump_file
, "in eliminate RED, easy tests done:\n");
2625 omega_print_problem (dump_file
, pb
);
2628 for (red_found
= 0, e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2629 if (pb
->geqs
[e
].color
== omega_red
)
2634 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2635 fprintf (dump_file
, "fast checks worked\n");
2637 if (!omega_reduce_with_subs
)
2638 gcc_assert (please_no_equalities_in_simplified_problems
2639 || pb
->num_subs
== 0);
2644 if (!omega_verify_simplification
2645 && verify_omega_pb (pb
) == omega_false
)
2649 tmp_problem
= XNEW (struct omega_pb_d
);
2651 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2652 if (pb
->geqs
[e
].color
== omega_red
)
2654 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2657 "checking equation %d to see if it is redundant: ", e
);
2658 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[e
]));
2659 fprintf (dump_file
, "\n");
2662 omega_copy_problem (tmp_problem
, pb
);
2663 omega_negate_geq (tmp_problem
, e
);
2664 tmp_problem
->safe_vars
= 0;
2665 tmp_problem
->variables_freed
= false;
2666 tmp_problem
->num_subs
= 0;
2668 if (omega_solve_problem (tmp_problem
, omega_false
) == omega_false
)
2670 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2671 fprintf (dump_file
, "it is redundant\n");
2672 omega_delete_geq (pb
, e
, pb
->num_vars
);
2676 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2677 fprintf (dump_file
, "it is not redundant\n");
2681 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2682 fprintf (dump_file
, "no need to check other red equations\n");
2690 /* omega_simplify_problem (pb); */
2692 if (!omega_reduce_with_subs
)
2693 gcc_assert (please_no_equalities_in_simplified_problems
2694 || pb
->num_subs
== 0);
2697 /* Transform some wildcard variables to non-safe variables. */
2700 chain_unprotect (omega_pb pb
)
2703 bool *unprotect
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2705 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2707 unprotect
[i
] = omega_wildcard_p (pb
, i
);
2709 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
2710 if (pb
->subs
[e
].coef
[i
])
2711 unprotect
[i
] = false;
2714 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2716 fprintf (dump_file
, "Doing chain reaction unprotection\n");
2717 omega_print_problem (dump_file
, pb
);
2719 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2721 fprintf (dump_file
, "unprotecting %s\n",
2722 omega_variable_to_str (pb
, i
));
2725 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2727 omega_unprotect_1 (pb
, &i
, unprotect
);
2729 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2731 fprintf (dump_file
, "After chain reactions\n");
2732 omega_print_problem (dump_file
, pb
);
2738 /* Reduce problem PB. */
2741 omega_problem_reduced (omega_pb pb
)
2743 if (omega_verify_simplification
2744 && !in_approximate_mode
2745 && verify_omega_pb (pb
) == omega_false
)
2748 if (PARAM_VALUE (PARAM_OMEGA_ELIMINATE_REDUNDANT_CONSTRAINTS
)
2749 && !omega_eliminate_redundant (pb
, true))
2752 omega_found_reduction
= omega_true
;
2754 if (!please_no_equalities_in_simplified_problems
)
2757 if (omega_reduce_with_subs
2758 || please_no_equalities_in_simplified_problems
)
2759 chain_unprotect (pb
);
2761 resurrect_subs (pb
);
2763 if (!return_single_result
)
2767 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
2768 pb
->forwarding_address
[pb
->var
[i
]] = i
;
2770 for (i
= 0; i
< pb
->num_subs
; i
++)
2771 pb
->forwarding_address
[pb
->subs
[i
].key
] = -i
- 1;
2773 (*omega_when_reduced
) (pb
);
2776 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2778 fprintf (dump_file
, "-------------------------------------------\n");
2779 fprintf (dump_file
, "problem reduced:\n");
2780 omega_print_problem (dump_file
, pb
);
2781 fprintf (dump_file
, "-------------------------------------------\n");
2785 /* Eliminates all the free variables for problem PB, that is all the
2786 variables from FV to PB->NUM_VARS. */
2789 omega_free_eliminations (omega_pb pb
, int fv
)
2791 bool try_again
= true;
2793 int n_vars
= pb
->num_vars
;
2799 for (i
= n_vars
; i
> fv
; i
--)
2801 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2802 if (pb
->geqs
[e
].coef
[i
])
2807 else if (pb
->geqs
[e
].coef
[i
] > 0)
2809 for (e2
= e
- 1; e2
>= 0; e2
--)
2810 if (pb
->geqs
[e2
].coef
[i
] < 0)
2815 for (e2
= e
- 1; e2
>= 0; e2
--)
2816 if (pb
->geqs
[e2
].coef
[i
] > 0)
2823 for (e3
= pb
->num_subs
- 1; e3
>= 0; e3
--)
2824 if (pb
->subs
[e3
].coef
[i
])
2830 for (e3
= pb
->num_eqs
- 1; e3
>= 0; e3
--)
2831 if (pb
->eqs
[e3
].coef
[i
])
2837 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2838 fprintf (dump_file
, "a free elimination of %s\n",
2839 omega_variable_to_str (pb
, i
));
2843 omega_delete_geq (pb
, e
, n_vars
);
2845 for (e
--; e
>= 0; e
--)
2846 if (pb
->geqs
[e
].coef
[i
])
2847 omega_delete_geq (pb
, e
, n_vars
);
2849 try_again
= (i
< n_vars
);
2852 omega_delete_variable (pb
, i
);
2853 n_vars
= pb
->num_vars
;
2858 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2860 fprintf (dump_file
, "\nafter free eliminations:\n");
2861 omega_print_problem (dump_file
, pb
);
2862 fprintf (dump_file
, "\n");
2866 /* Do free red eliminations. */
2869 free_red_eliminations (omega_pb pb
)
2871 bool try_again
= true;
2873 int n_vars
= pb
->num_vars
;
2874 bool *is_red_var
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2875 bool *is_dead_var
= XNEWVEC (bool, OMEGA_MAX_VARS
);
2876 bool *is_dead_geq
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
2878 for (i
= n_vars
; i
> 0; i
--)
2880 is_red_var
[i
] = false;
2881 is_dead_var
[i
] = false;
2884 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2886 is_dead_geq
[e
] = false;
2888 if (pb
->geqs
[e
].color
== omega_red
)
2889 for (i
= n_vars
; i
> 0; i
--)
2890 if (pb
->geqs
[e
].coef
[i
] != 0)
2891 is_red_var
[i
] = true;
2897 for (i
= n_vars
; i
> 0; i
--)
2898 if (!is_red_var
[i
] && !is_dead_var
[i
])
2900 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2901 if (!is_dead_geq
[e
] && pb
->geqs
[e
].coef
[i
])
2906 else if (pb
->geqs
[e
].coef
[i
] > 0)
2908 for (e2
= e
- 1; e2
>= 0; e2
--)
2909 if (!is_dead_geq
[e2
] && pb
->geqs
[e2
].coef
[i
] < 0)
2914 for (e2
= e
- 1; e2
>= 0; e2
--)
2915 if (!is_dead_geq
[e2
] && pb
->geqs
[e2
].coef
[i
] > 0)
2922 for (e3
= pb
->num_subs
- 1; e3
>= 0; e3
--)
2923 if (pb
->subs
[e3
].coef
[i
])
2929 for (e3
= pb
->num_eqs
- 1; e3
>= 0; e3
--)
2930 if (pb
->eqs
[e3
].coef
[i
])
2936 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2937 fprintf (dump_file
, "a free red elimination of %s\n",
2938 omega_variable_to_str (pb
, i
));
2941 if (pb
->geqs
[e
].coef
[i
])
2942 is_dead_geq
[e
] = true;
2945 is_dead_var
[i
] = true;
2950 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
2952 omega_delete_geq (pb
, e
, n_vars
);
2954 for (i
= n_vars
; i
> 0; i
--)
2956 omega_delete_variable (pb
, i
);
2958 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2960 fprintf (dump_file
, "\nafter free red eliminations:\n");
2961 omega_print_problem (dump_file
, pb
);
2962 fprintf (dump_file
, "\n");
2970 /* For equation EQ of the form "0 = EQN", insert in PB two
2971 inequalities "0 <= EQN" and "0 <= -EQN". */
2974 omega_convert_eq_to_geqs (omega_pb pb
, int eq
)
2978 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2979 fprintf (dump_file
, "Converting Eq to Geqs\n");
2981 /* Insert "0 <= EQN". */
2982 omega_copy_eqn (&pb
->geqs
[pb
->num_geqs
], &pb
->eqs
[eq
], pb
->num_vars
);
2983 pb
->geqs
[pb
->num_geqs
].touched
= 1;
2986 /* Insert "0 <= -EQN". */
2987 omega_copy_eqn (&pb
->geqs
[pb
->num_geqs
], &pb
->eqs
[eq
], pb
->num_vars
);
2988 pb
->geqs
[pb
->num_geqs
].touched
= 1;
2990 for (i
= 0; i
<= pb
->num_vars
; i
++)
2991 pb
->geqs
[pb
->num_geqs
].coef
[i
] *= -1;
2995 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2996 omega_print_problem (dump_file
, pb
);
2999 /* Eliminates variable I from PB. */
3002 omega_do_elimination (omega_pb pb
, int e
, int i
)
3004 eqn sub
= omega_alloc_eqns (0, 1);
3006 int n_vars
= pb
->num_vars
;
3008 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3009 fprintf (dump_file
, "eliminating variable %s\n",
3010 omega_variable_to_str (pb
, i
));
3012 omega_copy_eqn (sub
, &pb
->eqs
[e
], pb
->num_vars
);
3015 if (c
== 1 || c
== -1)
3017 if (pb
->eqs
[e
].color
== omega_red
)
3020 omega_substitute_red (pb
, sub
, i
, c
, &fB
);
3022 omega_convert_eq_to_geqs (pb
, e
);
3024 omega_delete_variable (pb
, i
);
3028 omega_substitute (pb
, sub
, i
, c
);
3029 omega_delete_variable (pb
, i
);
3037 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3038 fprintf (dump_file
, "performing non-exact elimination, c = %d\n", c
);
3040 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
3041 if (pb
->eqs
[e
].coef
[i
])
3043 eqn eqn
= &(pb
->eqs
[e
]);
3045 for (j
= n_vars
; j
>= 0; j
--)
3049 if (sub
->color
== omega_red
)
3050 eqn
->color
= omega_red
;
3051 for (j
= n_vars
; j
>= 0; j
--)
3052 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3055 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3056 if (pb
->geqs
[e
].coef
[i
])
3058 eqn eqn
= &(pb
->geqs
[e
]);
3061 if (sub
->color
== omega_red
)
3062 eqn
->color
= omega_red
;
3064 for (j
= n_vars
; j
>= 0; j
--)
3071 for (j
= n_vars
; j
>= 0; j
--)
3072 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3076 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
3077 if (pb
->subs
[e
].coef
[i
])
3079 eqn eqn
= &(pb
->subs
[e
]);
3082 gcc_assert (sub
->color
== omega_black
);
3083 for (j
= n_vars
; j
>= 0; j
--)
3087 for (j
= n_vars
; j
>= 0; j
--)
3088 eqn
->coef
[j
] -= sub
->coef
[j
] * k
/ c
;
3091 if (in_approximate_mode
)
3092 omega_delete_variable (pb
, i
);
3094 omega_convert_eq_to_geqs (pb
, e2
);
3097 omega_free_eqns (sub
, 1);
3100 /* Helper function for printing "sorry, no solution". */
3102 static inline enum omega_result
3103 omega_problem_has_no_solution (void)
3105 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3106 fprintf (dump_file
, "\nequations have no solution \n");
3111 /* Helper function: solve equations in PB one at a time, following the
3112 DESIRED_RES result. */
3114 static enum omega_result
3115 omega_solve_eq (omega_pb pb
, enum omega_result desired_res
)
3122 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && pb
->num_eqs
> 0)
3124 fprintf (dump_file
, "\nomega_solve_eq (%d, %d)\n",
3125 desired_res
, may_be_red
);
3126 omega_print_problem (dump_file
, pb
);
3127 fprintf (dump_file
, "\n");
3133 j
= pb
->num_eqs
- 1;
3139 while (i
<= j
&& pb
->eqs
[i
].color
== omega_red
)
3142 while (i
<= j
&& pb
->eqs
[j
].color
== omega_black
)
3148 eq
= omega_alloc_eqns (0, 1);
3149 omega_copy_eqn (eq
, &pb
->eqs
[i
], pb
->num_vars
);
3150 omega_copy_eqn (&pb
->eqs
[i
], &pb
->eqs
[j
], pb
->num_vars
);
3151 omega_copy_eqn (&pb
->eqs
[j
], eq
, pb
->num_vars
);
3152 omega_free_eqns (eq
, 1);
3158 /* Eliminate all EQ equations */
3159 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
3161 eqn eqn
= &(pb
->eqs
[e
]);
3164 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3165 fprintf (dump_file
, "----\n");
3167 for (i
= pb
->num_vars
; i
> 0; i
--)
3173 for (j
= i
- 1; j
> 0; j
--)
3177 /* i is the position of last nonzero coefficient,
3178 g is the coefficient of i,
3179 j is the position of next nonzero coefficient. */
3183 if (eqn
->coef
[0] % g
!= 0)
3184 return omega_problem_has_no_solution ();
3186 eqn
->coef
[0] = eqn
->coef
[0] / g
;
3189 omega_do_elimination (pb
, e
, i
);
3195 if (eqn
->coef
[0] != 0)
3196 return omega_problem_has_no_solution ();
3208 omega_do_elimination (pb
, e
, i
);
3214 bool promotion_possible
=
3215 (omega_safe_var_p (pb
, j
)
3216 && pb
->safe_vars
+ 1 == i
3217 && !omega_eqn_is_red (eqn
, desired_res
)
3218 && !in_approximate_mode
);
3220 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && promotion_possible
)
3221 fprintf (dump_file
, " Promotion possible\n");
3224 if (!omega_safe_var_p (pb
, j
))
3226 for (; g
!= 1 && !omega_safe_var_p (pb
, j
); j
--)
3227 g
= gcd (abs (eqn
->coef
[j
]), g
);
3230 else if (!omega_safe_var_p (pb
, i
))
3235 for (; g
!= 1 && j
> 0; j
--)
3236 g
= gcd (abs (eqn
->coef
[j
]), g
);
3240 if (eqn
->coef
[0] % g
!= 0)
3241 return omega_problem_has_no_solution ();
3243 for (j
= 0; j
<= pb
->num_vars
; j
++)
3253 for (e2
= e
- 1; e2
>= 0; e2
--)
3254 if (pb
->eqs
[e2
].coef
[i
])
3258 for (e2
= pb
->num_geqs
- 1; e2
>= 0; e2
--)
3259 if (pb
->geqs
[e2
].coef
[i
])
3263 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
3264 if (pb
->subs
[e2
].coef
[i
])
3269 bool change
= false;
3271 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3273 fprintf (dump_file
, "Ha! We own it! \n");
3274 omega_print_eq (dump_file
, pb
, eqn
);
3275 fprintf (dump_file
, " \n");
3281 for (j
= i
- 1; j
>= 0; j
--)
3283 int t
= int_mod (eqn
->coef
[j
], g
);
3288 if (t
!= eqn
->coef
[j
])
3297 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3298 fprintf (dump_file
, "So what?\n");
3303 omega_name_wild_card (pb
, i
);
3305 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3307 omega_print_eq (dump_file
, pb
, eqn
);
3308 fprintf (dump_file
, " \n");
3317 if (promotion_possible
)
3319 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3321 fprintf (dump_file
, "promoting %s to safety\n",
3322 omega_variable_to_str (pb
, i
));
3323 omega_print_vars (dump_file
, pb
);
3328 if (!omega_wildcard_p (pb
, i
))
3329 omega_name_wild_card (pb
, i
);
3331 promotion_possible
= false;
3336 if (g2
> 1 && !in_approximate_mode
)
3338 if (pb
->eqs
[e
].color
== omega_red
)
3340 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3341 fprintf (dump_file
, "handling red equality\n");
3344 omega_do_elimination (pb
, e
, i
);
3348 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3351 "adding equation to handle safe variable \n");
3352 omega_print_eq (dump_file
, pb
, eqn
);
3353 fprintf (dump_file
, "\n----\n");
3354 omega_print_problem (dump_file
, pb
);
3355 fprintf (dump_file
, "\n----\n");
3356 fprintf (dump_file
, "\n----\n");
3359 i
= omega_add_new_wild_card (pb
);
3361 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
3362 omega_init_eqn_zero (&pb
->eqs
[e
+ 1], pb
->num_vars
);
3363 omega_copy_eqn (&pb
->eqs
[e
+ 1], eqn
, pb
->safe_vars
);
3365 for (j
= pb
->num_vars
; j
>= 0; j
--)
3367 pb
->eqs
[e
+ 1].coef
[j
] = int_mod (pb
->eqs
[e
+ 1].coef
[j
], g2
);
3369 if (2 * pb
->eqs
[e
+ 1].coef
[j
] >= g2
)
3370 pb
->eqs
[e
+ 1].coef
[j
] -= g2
;
3373 pb
->eqs
[e
+ 1].coef
[i
] = g2
;
3376 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3377 omega_print_problem (dump_file
, pb
);
3386 /* Find variable to eliminate. */
3389 gcc_assert (in_approximate_mode
);
3391 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3393 fprintf (dump_file
, "non-exact elimination: ");
3394 omega_print_eq (dump_file
, pb
, eqn
);
3395 fprintf (dump_file
, "\n");
3396 omega_print_problem (dump_file
, pb
);
3399 for (i
= pb
->num_vars
; i
> sv
; i
--)
3400 if (pb
->eqs
[e
].coef
[i
] != 0)
3404 for (i
= pb
->num_vars
; i
> sv
; i
--)
3405 if (pb
->eqs
[e
].coef
[i
] == 1 || pb
->eqs
[e
].coef
[i
] == -1)
3411 omega_do_elimination (pb
, e
, i
);
3413 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && g2
> 1)
3415 fprintf (dump_file
, "result of non-exact elimination:\n");
3416 omega_print_problem (dump_file
, pb
);
3421 int factor
= (INT_MAX
);
3424 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3425 fprintf (dump_file
, "doing moding\n");
3427 for (i
= pb
->num_vars
; i
!= sv
; i
--)
3428 if ((pb
->eqs
[e
].coef
[i
] & 1) != 0)
3433 for (; i
!= sv
; i
--)
3434 if ((pb
->eqs
[e
].coef
[i
] & 1) != 0)
3440 if (j
!= 0 && i
== sv
)
3442 omega_do_mod (pb
, 2, e
, j
);
3448 for (i
= pb
->num_vars
; i
!= sv
; i
--)
3449 if (pb
->eqs
[e
].coef
[i
] != 0
3450 && factor
> abs (pb
->eqs
[e
].coef
[i
]) + 1)
3452 factor
= abs (pb
->eqs
[e
].coef
[i
]) + 1;
3458 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3459 fprintf (dump_file
, "should not have happened\n");
3463 omega_do_mod (pb
, factor
, e
, j
);
3464 /* Go back and try this equation again. */
3471 return omega_unknown
;
3474 /* Transform an inequation E to an equality, then solve DIFF problems
3475 based on PB, and only differing by the constant part that is
3476 diminished by one, trying to figure out which of the constants
3479 static enum omega_result
3480 parallel_splinter (omega_pb pb
, int e
, int diff
,
3481 enum omega_result desired_res
)
3483 omega_pb tmp_problem
;
3486 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3488 fprintf (dump_file
, "Using parallel splintering\n");
3489 omega_print_problem (dump_file
, pb
);
3492 tmp_problem
= XNEW (struct omega_pb_d
);
3493 omega_copy_eqn (&pb
->eqs
[0], &pb
->geqs
[e
], pb
->num_vars
);
3496 for (i
= 0; i
<= diff
; i
++)
3498 omega_copy_problem (tmp_problem
, pb
);
3500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3502 fprintf (dump_file
, "Splinter # %i\n", i
);
3503 omega_print_problem (dump_file
, pb
);
3506 if (omega_solve_problem (tmp_problem
, desired_res
) == omega_true
)
3512 pb
->eqs
[0].coef
[0]--;
3519 /* Helper function: solve equations one at a time. */
3521 static enum omega_result
3522 omega_solve_geq (omega_pb pb
, enum omega_result desired_res
)
3526 enum omega_result result
;
3527 bool coupled_subscripts
= false;
3528 bool smoothed
= false;
3529 bool eliminate_again
;
3530 bool tried_eliminating_redundant
= false;
3532 if (desired_res
!= omega_simplify
)
3540 gcc_assert (desired_res
== omega_simplify
|| pb
->num_subs
== 0);
3542 /* Verify that there are not too many inequalities. */
3543 gcc_assert (pb
->num_geqs
<= OMEGA_MAX_GEQS
);
3545 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3547 fprintf (dump_file
, "\nomega_solve_geq (%d,%d):\n",
3548 desired_res
, please_no_equalities_in_simplified_problems
);
3549 omega_print_problem (dump_file
, pb
);
3550 fprintf (dump_file
, "\n");
3553 n_vars
= pb
->num_vars
;
3557 enum omega_eqn_color u_color
= omega_black
;
3558 enum omega_eqn_color l_color
= omega_black
;
3559 int upper_bound
= pos_infinity
;
3560 int lower_bound
= neg_infinity
;
3562 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3564 int a
= pb
->geqs
[e
].coef
[1];
3565 int c
= pb
->geqs
[e
].coef
[0];
3567 /* Our equation is ax + c >= 0, or ax >= -c, or c >= -ax. */
3571 return omega_problem_has_no_solution ();
3578 if (lower_bound
< -c
3579 || (lower_bound
== -c
3580 && !omega_eqn_is_red (&pb
->geqs
[e
], desired_res
)))
3583 l_color
= pb
->geqs
[e
].color
;
3589 c
= int_div (c
, -a
);
3592 || (upper_bound
== c
3593 && !omega_eqn_is_red (&pb
->geqs
[e
], desired_res
)))
3596 u_color
= pb
->geqs
[e
].color
;
3601 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3603 fprintf (dump_file
, "upper bound = %d\n", upper_bound
);
3604 fprintf (dump_file
, "lower bound = %d\n", lower_bound
);
3607 if (lower_bound
> upper_bound
)
3608 return omega_problem_has_no_solution ();
3610 if (desired_res
== omega_simplify
)
3613 if (pb
->safe_vars
== 1)
3616 if (lower_bound
== upper_bound
3617 && u_color
== omega_black
3618 && l_color
== omega_black
)
3620 pb
->eqs
[0].coef
[0] = -lower_bound
;
3621 pb
->eqs
[0].coef
[1] = 1;
3622 pb
->eqs
[0].color
= omega_black
;
3624 return omega_solve_problem (pb
, desired_res
);
3628 if (lower_bound
> neg_infinity
)
3630 pb
->geqs
[0].coef
[0] = -lower_bound
;
3631 pb
->geqs
[0].coef
[1] = 1;
3632 pb
->geqs
[0].key
= 1;
3633 pb
->geqs
[0].color
= l_color
;
3634 pb
->geqs
[0].touched
= 0;
3638 if (upper_bound
< pos_infinity
)
3640 pb
->geqs
[pb
->num_geqs
].coef
[0] = upper_bound
;
3641 pb
->geqs
[pb
->num_geqs
].coef
[1] = -1;
3642 pb
->geqs
[pb
->num_geqs
].key
= -1;
3643 pb
->geqs
[pb
->num_geqs
].color
= u_color
;
3644 pb
->geqs
[pb
->num_geqs
].touched
= 0;
3652 omega_problem_reduced (pb
);
3656 if (original_problem
!= no_problem
3657 && l_color
== omega_black
3658 && u_color
== omega_black
3660 && lower_bound
== upper_bound
)
3662 pb
->eqs
[0].coef
[0] = -lower_bound
;
3663 pb
->eqs
[0].coef
[1] = 1;
3665 adding_equality_constraint (pb
, 0);
3671 if (!pb
->variables_freed
)
3673 pb
->variables_freed
= true;
3675 if (desired_res
!= omega_simplify
)
3676 omega_free_eliminations (pb
, 0);
3678 omega_free_eliminations (pb
, pb
->safe_vars
);
3680 n_vars
= pb
->num_vars
;
3686 switch (normalize_omega_problem (pb
))
3688 case normalize_false
:
3692 case normalize_coupled
:
3693 coupled_subscripts
= true;
3696 case normalize_uncoupled
:
3697 coupled_subscripts
= false;
3704 n_vars
= pb
->num_vars
;
3706 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3708 fprintf (dump_file
, "\nafter normalization:\n");
3709 omega_print_problem (dump_file
, pb
);
3710 fprintf (dump_file
, "\n");
3711 fprintf (dump_file
, "eliminating variable using Fourier-Motzkin.\n");
3715 int parallel_difference
= INT_MAX
;
3716 int best_parallel_eqn
= -1;
3717 int minC
, maxC
, minCj
= 0;
3718 int lower_bound_count
= 0;
3720 bool possible_easy_int_solution
;
3721 int max_splinters
= 1;
3723 bool lucky_exact
= false;
3724 int best
= (INT_MAX
);
3725 int j
= 0, jLe
= 0, jLowerBoundCount
= 0;
3728 eliminate_again
= false;
3730 if (pb
->num_eqs
> 0)
3731 return omega_solve_problem (pb
, desired_res
);
3733 if (!coupled_subscripts
)
3735 if (pb
->safe_vars
== 0)
3738 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3739 if (!omega_safe_var_p (pb
, abs (pb
->geqs
[e
].key
)))
3740 omega_delete_geq (pb
, e
, n_vars
);
3742 pb
->num_vars
= pb
->safe_vars
;
3744 if (desired_res
== omega_simplify
)
3746 omega_problem_reduced (pb
);
3753 if (desired_res
!= omega_simplify
)
3758 if (pb
->num_geqs
== 0)
3760 if (desired_res
== omega_simplify
)
3762 pb
->num_vars
= pb
->safe_vars
;
3763 omega_problem_reduced (pb
);
3769 if (desired_res
== omega_simplify
&& n_vars
== pb
->safe_vars
)
3771 omega_problem_reduced (pb
);
3775 if (pb
->num_geqs
> OMEGA_MAX_GEQS
- 30
3776 || pb
->num_geqs
> 2 * n_vars
* n_vars
+ 4 * n_vars
+ 10)
3778 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3780 "TOO MANY EQUATIONS; "
3781 "%d equations, %d variables, "
3782 "ELIMINATING REDUNDANT ONES\n",
3783 pb
->num_geqs
, n_vars
);
3785 if (!omega_eliminate_redundant (pb
, false))
3788 n_vars
= pb
->num_vars
;
3790 if (pb
->num_eqs
> 0)
3791 return omega_solve_problem (pb
, desired_res
);
3793 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3794 fprintf (dump_file
, "END ELIMINATION OF REDUNDANT EQUATIONS\n");
3797 if (desired_res
!= omega_simplify
)
3802 for (i
= n_vars
; i
!= fv
; i
--)
3808 int upper_bound_count
= 0;
3810 lower_bound_count
= 0;
3813 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3814 if (pb
->geqs
[e
].coef
[i
] < 0)
3816 minC
= MIN (minC
, pb
->geqs
[e
].coef
[i
]);
3817 upper_bound_count
++;
3818 if (pb
->geqs
[e
].coef
[i
] < -1)
3826 else if (pb
->geqs
[e
].coef
[i
] > 0)
3828 maxC
= MAX (maxC
, pb
->geqs
[e
].coef
[i
]);
3829 lower_bound_count
++;
3831 if (pb
->geqs
[e
].coef
[i
] > 1)
3840 if (lower_bound_count
== 0
3841 || upper_bound_count
== 0)
3843 lower_bound_count
= 0;
3847 if (ub
>= 0 && lb
>= 0
3848 && pb
->geqs
[lb
].key
== -pb
->geqs
[ub
].key
)
3850 int Lc
= pb
->geqs
[lb
].coef
[i
];
3851 int Uc
= -pb
->geqs
[ub
].coef
[i
];
3853 Lc
* pb
->geqs
[ub
].coef
[0] + Uc
* pb
->geqs
[lb
].coef
[0];
3854 lucky
= (diff
>= (Uc
- 1) * (Lc
- 1));
3860 || in_approximate_mode
)
3862 score
= upper_bound_count
* lower_bound_count
;
3864 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3866 "For %s, exact, score = %d*%d, range = %d ... %d,"
3867 "\nlucky = %d, in_approximate_mode=%d \n",
3868 omega_variable_to_str (pb
, i
),
3870 lower_bound_count
, minC
, maxC
, lucky
,
3871 in_approximate_mode
);
3881 jLowerBoundCount
= lower_bound_count
;
3883 lucky_exact
= lucky
;
3890 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3892 "For %s, non-exact, score = %d*%d,"
3893 "range = %d ... %d \n",
3894 omega_variable_to_str (pb
, i
),
3896 lower_bound_count
, minC
, maxC
);
3898 score
= maxC
- minC
;
3906 jLowerBoundCount
= lower_bound_count
;
3911 if (lower_bound_count
== 0)
3913 omega_free_eliminations (pb
, pb
->safe_vars
);
3914 n_vars
= pb
->num_vars
;
3915 eliminate_again
= true;
3922 lower_bound_count
= jLowerBoundCount
;
3924 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
3925 if (pb
->geqs
[e
].coef
[i
] > 0)
3927 if (pb
->geqs
[e
].coef
[i
] == -minC
)
3928 max_splinters
+= -minC
- 1;
3931 check_pos_mul ((pb
->geqs
[e
].coef
[i
] - 1),
3932 (-minC
- 1)) / (-minC
) + 1;
3936 /* Trying to produce exact elimination by finding redundant
3938 if (!exact
&& !tried_eliminating_redundant
)
3940 omega_eliminate_redundant (pb
, false);
3941 tried_eliminating_redundant
= true;
3942 eliminate_again
= true;
3945 tried_eliminating_redundant
= false;
3948 if (return_single_result
&& desired_res
== omega_simplify
&& !exact
)
3950 omega_problem_reduced (pb
);
3954 /* #ifndef Omega3 */
3955 /* Trying to produce exact elimination by finding redundant
3957 if (!exact
&& !tried_eliminating_redundant
)
3959 omega_eliminate_redundant (pb
, false);
3960 tried_eliminating_redundant
= true;
3963 tried_eliminating_redundant
= false;
3970 for (e1
= pb
->num_geqs
- 1; e1
>= 0; e1
--)
3971 if (pb
->geqs
[e1
].color
== omega_black
)
3972 for (e2
= e1
- 1; e2
>= 0; e2
--)
3973 if (pb
->geqs
[e2
].color
== omega_black
3974 && pb
->geqs
[e1
].key
== -pb
->geqs
[e2
].key
3975 && ((pb
->geqs
[e1
].coef
[0] + pb
->geqs
[e2
].coef
[0])
3976 * (3 - single_var_geq (&pb
->geqs
[e1
], pb
->num_vars
))
3977 / 2 < parallel_difference
))
3979 parallel_difference
=
3980 (pb
->geqs
[e1
].coef
[0] + pb
->geqs
[e2
].coef
[0])
3981 * (3 - single_var_geq (&pb
->geqs
[e1
], pb
->num_vars
))
3983 best_parallel_eqn
= e1
;
3986 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
3987 && best_parallel_eqn
>= 0)
3990 "Possible parallel projection, diff = %d, in ",
3991 parallel_difference
);
3992 omega_print_geq (dump_file
, pb
, &(pb
->geqs
[best_parallel_eqn
]));
3993 fprintf (dump_file
, "\n");
3994 omega_print_problem (dump_file
, pb
);
3998 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4000 fprintf (dump_file
, "going to eliminate %s, (%d,%d,%d)\n",
4001 omega_variable_to_str (pb
, i
), i
, minC
,
4003 omega_print_problem (dump_file
, pb
);
4006 fprintf (dump_file
, "(a lucky exact elimination)\n");
4009 fprintf (dump_file
, "(an exact elimination)\n");
4011 fprintf (dump_file
, "Max # of splinters = %d\n", max_splinters
);
4014 gcc_assert (max_splinters
>= 1);
4016 if (!exact
&& desired_res
== omega_simplify
&& best_parallel_eqn
>= 0
4017 && parallel_difference
<= max_splinters
)
4018 return parallel_splinter (pb
, best_parallel_eqn
, parallel_difference
,
4026 int j
= pb
->num_vars
;
4028 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4030 fprintf (dump_file
, "Swapping %d and %d\n", i
, j
);
4031 omega_print_problem (dump_file
, pb
);
4034 swap (&pb
->var
[i
], &pb
->var
[j
]);
4036 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4037 if (pb
->geqs
[e
].coef
[i
] != pb
->geqs
[e
].coef
[j
])
4039 pb
->geqs
[e
].touched
= 1;
4040 t
= pb
->geqs
[e
].coef
[i
];
4041 pb
->geqs
[e
].coef
[i
] = pb
->geqs
[e
].coef
[j
];
4042 pb
->geqs
[e
].coef
[j
] = t
;
4045 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
4046 if (pb
->subs
[e
].coef
[i
] != pb
->subs
[e
].coef
[j
])
4048 t
= pb
->subs
[e
].coef
[i
];
4049 pb
->subs
[e
].coef
[i
] = pb
->subs
[e
].coef
[j
];
4050 pb
->subs
[e
].coef
[j
] = t
;
4053 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4055 fprintf (dump_file
, "Swapping complete \n");
4056 omega_print_problem (dump_file
, pb
);
4057 fprintf (dump_file
, "\n");
4063 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4065 fprintf (dump_file
, "No swap needed\n");
4066 omega_print_problem (dump_file
, pb
);
4070 n_vars
= pb
->num_vars
;
4076 int upper_bound
= pos_infinity
;
4077 int lower_bound
= neg_infinity
;
4078 enum omega_eqn_color ub_color
= omega_black
;
4079 enum omega_eqn_color lb_color
= omega_black
;
4080 int topeqn
= pb
->num_geqs
- 1;
4081 int Lc
= pb
->geqs
[Le
].coef
[i
];
4083 for (Le
= topeqn
; Le
>= 0; Le
--)
4084 if ((Lc
= pb
->geqs
[Le
].coef
[i
]) == 0)
4086 if (pb
->geqs
[Le
].coef
[1] == 1)
4088 int constantTerm
= -pb
->geqs
[Le
].coef
[0];
4090 if (constantTerm
> lower_bound
||
4091 (constantTerm
== lower_bound
&&
4092 !omega_eqn_is_red (&pb
->geqs
[Le
], desired_res
)))
4094 lower_bound
= constantTerm
;
4095 lb_color
= pb
->geqs
[Le
].color
;
4098 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4100 if (pb
->geqs
[Le
].color
== omega_black
)
4101 fprintf (dump_file
, " :::=> %s >= %d\n",
4102 omega_variable_to_str (pb
, 1),
4106 " :::=> [%s >= %d]\n",
4107 omega_variable_to_str (pb
, 1),
4113 int constantTerm
= pb
->geqs
[Le
].coef
[0];
4114 if (constantTerm
< upper_bound
||
4115 (constantTerm
== upper_bound
4116 && !omega_eqn_is_red (&pb
->geqs
[Le
],
4119 upper_bound
= constantTerm
;
4120 ub_color
= pb
->geqs
[Le
].color
;
4123 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4125 if (pb
->geqs
[Le
].color
== omega_black
)
4126 fprintf (dump_file
, " :::=> %s <= %d\n",
4127 omega_variable_to_str (pb
, 1),
4131 " :::=> [%s <= %d]\n",
4132 omega_variable_to_str (pb
, 1),
4138 for (Ue
= topeqn
; Ue
>= 0; Ue
--)
4139 if (pb
->geqs
[Ue
].coef
[i
] < 0
4140 && pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4142 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4143 int coefficient
= pb
->geqs
[Ue
].coef
[1] * Lc
4144 + pb
->geqs
[Le
].coef
[1] * Uc
;
4145 int constantTerm
= pb
->geqs
[Ue
].coef
[0] * Lc
4146 + pb
->geqs
[Le
].coef
[0] * Uc
;
4148 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4150 omega_print_geq_extra (dump_file
, pb
,
4152 fprintf (dump_file
, "\n");
4153 omega_print_geq_extra (dump_file
, pb
,
4155 fprintf (dump_file
, "\n");
4158 if (coefficient
> 0)
4160 constantTerm
= -int_div (constantTerm
, coefficient
);
4162 if (constantTerm
> lower_bound
4163 || (constantTerm
== lower_bound
4164 && (desired_res
!= omega_simplify
4165 || (pb
->geqs
[Ue
].color
== omega_black
4166 && pb
->geqs
[Le
].color
== omega_black
))))
4168 lower_bound
= constantTerm
;
4169 lb_color
= (pb
->geqs
[Ue
].color
== omega_red
4170 || pb
->geqs
[Le
].color
== omega_red
)
4171 ? omega_red
: omega_black
;
4174 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4176 if (pb
->geqs
[Ue
].color
== omega_red
4177 || pb
->geqs
[Le
].color
== omega_red
)
4179 " ::=> [%s >= %d]\n",
4180 omega_variable_to_str (pb
, 1),
4185 omega_variable_to_str (pb
, 1),
4191 constantTerm
= int_div (constantTerm
, -coefficient
);
4192 if (constantTerm
< upper_bound
4193 || (constantTerm
== upper_bound
4194 && pb
->geqs
[Ue
].color
== omega_black
4195 && pb
->geqs
[Le
].color
== omega_black
))
4197 upper_bound
= constantTerm
;
4198 ub_color
= (pb
->geqs
[Ue
].color
== omega_red
4199 || pb
->geqs
[Le
].color
== omega_red
)
4200 ? omega_red
: omega_black
;
4204 && (dump_flags
& TDF_DETAILS
))
4206 if (pb
->geqs
[Ue
].color
== omega_red
4207 || pb
->geqs
[Le
].color
== omega_red
)
4209 " ::=> [%s <= %d]\n",
4210 omega_variable_to_str (pb
, 1),
4215 omega_variable_to_str (pb
, 1),
4223 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4225 " therefore, %c%d <= %c%s%c <= %d%c\n",
4226 lb_color
== omega_red
? '[' : ' ', lower_bound
,
4227 (lb_color
== omega_red
&& ub_color
== omega_black
)
4229 omega_variable_to_str (pb
, 1),
4230 (lb_color
== omega_black
&& ub_color
== omega_red
)
4232 upper_bound
, ub_color
== omega_red
? ']' : ' ');
4234 if (lower_bound
> upper_bound
)
4237 if (pb
->safe_vars
== 1)
4239 if (upper_bound
== lower_bound
4240 && !(ub_color
== omega_red
|| lb_color
== omega_red
)
4241 && !please_no_equalities_in_simplified_problems
)
4244 pb
->eqs
[0].coef
[1] = -1;
4245 pb
->eqs
[0].coef
[0] = upper_bound
;
4247 if (ub_color
== omega_red
4248 || lb_color
== omega_red
)
4249 pb
->eqs
[0].color
= omega_red
;
4251 if (desired_res
== omega_simplify
4252 && pb
->eqs
[0].color
== omega_black
)
4253 return omega_solve_problem (pb
, desired_res
);
4256 if (upper_bound
!= pos_infinity
)
4258 pb
->geqs
[0].coef
[1] = -1;
4259 pb
->geqs
[0].coef
[0] = upper_bound
;
4260 pb
->geqs
[0].color
= ub_color
;
4261 pb
->geqs
[0].key
= -1;
4262 pb
->geqs
[0].touched
= 0;
4266 if (lower_bound
!= neg_infinity
)
4268 pb
->geqs
[pb
->num_geqs
].coef
[1] = 1;
4269 pb
->geqs
[pb
->num_geqs
].coef
[0] = -lower_bound
;
4270 pb
->geqs
[pb
->num_geqs
].color
= lb_color
;
4271 pb
->geqs
[pb
->num_geqs
].key
= 1;
4272 pb
->geqs
[pb
->num_geqs
].touched
= 0;
4277 if (desired_res
== omega_simplify
)
4279 omega_problem_reduced (pb
);
4285 && (desired_res
!= omega_simplify
4286 || (lb_color
== omega_black
4287 && ub_color
== omega_black
))
4288 && original_problem
!= no_problem
4289 && lower_bound
== upper_bound
)
4291 for (i
= original_problem
->num_vars
; i
>= 0; i
--)
4292 if (original_problem
->var
[i
] == pb
->var
[1])
4298 e
= original_problem
->num_eqs
++;
4299 omega_init_eqn_zero (&original_problem
->eqs
[e
],
4300 original_problem
->num_vars
);
4301 original_problem
->eqs
[e
].coef
[i
] = -1;
4302 original_problem
->eqs
[e
].coef
[0] = upper_bound
;
4304 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4307 "adding equality %d to outer problem\n", e
);
4308 omega_print_problem (dump_file
, original_problem
);
4315 eliminate_again
= true;
4317 if (lower_bound_count
== 1)
4319 eqn lbeqn
= omega_alloc_eqns (0, 1);
4320 int Lc
= pb
->geqs
[Le
].coef
[i
];
4322 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4323 fprintf (dump_file
, "an inplace elimination\n");
4325 omega_copy_eqn (lbeqn
, &pb
->geqs
[Le
], (n_vars
+ 1));
4326 omega_delete_geq_extra (pb
, Le
, n_vars
+ 1);
4328 for (Ue
= pb
->num_geqs
- 1; Ue
>= 0; Ue
--)
4329 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4331 if (lbeqn
->key
== -pb
->geqs
[Ue
].key
)
4332 omega_delete_geq_extra (pb
, Ue
, n_vars
+ 1);
4336 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4337 pb
->geqs
[Ue
].touched
= 1;
4338 eliminate_again
= false;
4340 if (lbeqn
->color
== omega_red
)
4341 pb
->geqs
[Ue
].color
= omega_red
;
4343 for (k
= 0; k
<= n_vars
; k
++)
4344 pb
->geqs
[Ue
].coef
[k
] =
4345 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4346 check_mul (lbeqn
->coef
[k
], Uc
);
4348 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4350 omega_print_geq (dump_file
, pb
,
4352 fprintf (dump_file
, "\n");
4357 omega_free_eqns (lbeqn
, 1);
4362 int *dead_eqns
= XNEWVEC (int, OMEGA_MAX_GEQS
);
4363 bool *is_dead
= XNEWVEC (bool, OMEGA_MAX_GEQS
);
4365 int top_eqn
= pb
->num_geqs
- 1;
4366 lower_bound_count
--;
4368 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4369 fprintf (dump_file
, "lower bound count = %d\n",
4372 for (Le
= top_eqn
; Le
>= 0; Le
--)
4373 if (pb
->geqs
[Le
].coef
[i
] > 0)
4375 int Lc
= pb
->geqs
[Le
].coef
[i
];
4376 for (Ue
= top_eqn
; Ue
>= 0; Ue
--)
4377 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4379 if (pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4382 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4385 e2
= pb
->num_geqs
++;
4387 e2
= dead_eqns
[--num_dead
];
4389 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4391 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4394 "Le = %d, Ue = %d, gen = %d\n",
4396 omega_print_geq_extra (dump_file
, pb
,
4398 fprintf (dump_file
, "\n");
4399 omega_print_geq_extra (dump_file
, pb
,
4401 fprintf (dump_file
, "\n");
4404 eliminate_again
= false;
4406 for (k
= n_vars
; k
>= 0; k
--)
4407 pb
->geqs
[e2
].coef
[k
] =
4408 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4409 check_mul (pb
->geqs
[Le
].coef
[k
], Uc
);
4411 pb
->geqs
[e2
].coef
[n_vars
+ 1] = 0;
4412 pb
->geqs
[e2
].touched
= 1;
4414 if (pb
->geqs
[Ue
].color
== omega_red
4415 || pb
->geqs
[Le
].color
== omega_red
)
4416 pb
->geqs
[e2
].color
= omega_red
;
4418 pb
->geqs
[e2
].color
= omega_black
;
4420 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4422 omega_print_geq (dump_file
, pb
,
4424 fprintf (dump_file
, "\n");
4428 if (lower_bound_count
== 0)
4430 dead_eqns
[num_dead
++] = Ue
;
4432 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4433 fprintf (dump_file
, "Killed %d\n", Ue
);
4437 lower_bound_count
--;
4438 dead_eqns
[num_dead
++] = Le
;
4440 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4441 fprintf (dump_file
, "Killed %d\n", Le
);
4444 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4447 while (num_dead
> 0)
4448 is_dead
[dead_eqns
[--num_dead
]] = true;
4450 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4452 omega_delete_geq_extra (pb
, e
, n_vars
+ 1);
4463 rS
= omega_alloc_problem (0, 0);
4464 iS
= omega_alloc_problem (0, 0);
4466 possible_easy_int_solution
= true;
4468 for (e
= 0; e
< pb
->num_geqs
; e
++)
4469 if (pb
->geqs
[e
].coef
[i
] == 0)
4471 omega_copy_eqn (&(rS
->geqs
[e2
]), &pb
->geqs
[e
],
4473 omega_copy_eqn (&(iS
->geqs
[e2
]), &pb
->geqs
[e
],
4476 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4479 fprintf (dump_file
, "Copying (%d, %d): ", i
,
4480 pb
->geqs
[e
].coef
[i
]);
4481 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[e
]);
4482 fprintf (dump_file
, "\n");
4483 for (t
= 0; t
<= n_vars
+ 1; t
++)
4484 fprintf (dump_file
, "%d ", pb
->geqs
[e
].coef
[t
]);
4485 fprintf (dump_file
, "\n");
4489 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4492 for (Le
= pb
->num_geqs
- 1; Le
>= 0; Le
--)
4493 if (pb
->geqs
[Le
].coef
[i
] > 0)
4494 for (Ue
= pb
->num_geqs
- 1; Ue
>= 0; Ue
--)
4495 if (pb
->geqs
[Ue
].coef
[i
] < 0)
4498 int Lc
= pb
->geqs
[Le
].coef
[i
];
4499 int Uc
= -pb
->geqs
[Ue
].coef
[i
];
4501 if (pb
->geqs
[Le
].key
!= -pb
->geqs
[Ue
].key
)
4504 rS
->geqs
[e2
].touched
= iS
->geqs
[e2
].touched
= 1;
4506 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4508 fprintf (dump_file
, "---\n");
4510 "Le(Lc) = %d(%d_, Ue(Uc) = %d(%d), gen = %d\n",
4511 Le
, Lc
, Ue
, Uc
, e2
);
4512 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[Le
]);
4513 fprintf (dump_file
, "\n");
4514 omega_print_geq_extra (dump_file
, pb
, &pb
->geqs
[Ue
]);
4515 fprintf (dump_file
, "\n");
4520 for (k
= n_vars
; k
>= 0; k
--)
4521 iS
->geqs
[e2
].coef
[k
] = rS
->geqs
[e2
].coef
[k
] =
4522 pb
->geqs
[Ue
].coef
[k
] + pb
->geqs
[Le
].coef
[k
];
4524 iS
->geqs
[e2
].coef
[0] -= (Uc
- 1);
4528 for (k
= n_vars
; k
>= 0; k
--)
4529 iS
->geqs
[e2
].coef
[k
] = rS
->geqs
[e2
].coef
[k
] =
4530 check_mul (pb
->geqs
[Ue
].coef
[k
], Lc
) +
4531 check_mul (pb
->geqs
[Le
].coef
[k
], Uc
);
4533 iS
->geqs
[e2
].coef
[0] -= (Uc
- 1) * (Lc
- 1);
4536 if (pb
->geqs
[Ue
].color
== omega_red
4537 || pb
->geqs
[Le
].color
== omega_red
)
4538 iS
->geqs
[e2
].color
= rS
->geqs
[e2
].color
= omega_red
;
4540 iS
->geqs
[e2
].color
= rS
->geqs
[e2
].color
= omega_black
;
4542 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4544 omega_print_geq (dump_file
, pb
, &(rS
->geqs
[e2
]));
4545 fprintf (dump_file
, "\n");
4549 gcc_assert (e2
< OMEGA_MAX_GEQS
);
4551 else if (pb
->geqs
[Ue
].coef
[0] * Lc
+
4552 pb
->geqs
[Le
].coef
[0] * Uc
-
4553 (Uc
- 1) * (Lc
- 1) < 0)
4554 possible_easy_int_solution
= false;
4557 iS
->variables_initialized
= rS
->variables_initialized
= true;
4558 iS
->num_vars
= rS
->num_vars
= pb
->num_vars
;
4559 iS
->num_geqs
= rS
->num_geqs
= e2
;
4560 iS
->num_eqs
= rS
->num_eqs
= 0;
4561 iS
->num_subs
= rS
->num_subs
= pb
->num_subs
;
4562 iS
->safe_vars
= rS
->safe_vars
= pb
->safe_vars
;
4564 for (e
= n_vars
; e
>= 0; e
--)
4565 rS
->var
[e
] = pb
->var
[e
];
4567 for (e
= n_vars
; e
>= 0; e
--)
4568 iS
->var
[e
] = pb
->var
[e
];
4570 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
4572 omega_copy_eqn (&(rS
->subs
[e
]), &(pb
->subs
[e
]), pb
->num_vars
);
4573 omega_copy_eqn (&(iS
->subs
[e
]), &(pb
->subs
[e
]), pb
->num_vars
);
4577 n_vars
= pb
->num_vars
;
4579 if (desired_res
!= omega_true
)
4581 if (original_problem
== no_problem
)
4583 original_problem
= pb
;
4584 result
= omega_solve_geq (rS
, omega_false
);
4585 original_problem
= no_problem
;
4588 result
= omega_solve_geq (rS
, omega_false
);
4590 if (result
== omega_false
)
4597 if (pb
->num_eqs
> 0)
4599 /* An equality constraint must have been found */
4602 return omega_solve_problem (pb
, desired_res
);
4606 if (desired_res
!= omega_false
)
4609 int lower_bounds
= 0;
4610 int *lower_bound
= XNEWVEC (int, OMEGA_MAX_GEQS
);
4612 if (possible_easy_int_solution
)
4615 result
= omega_solve_geq (iS
, desired_res
);
4618 if (result
!= omega_false
)
4627 if (!exact
&& best_parallel_eqn
>= 0
4628 && parallel_difference
<= max_splinters
)
4633 return parallel_splinter (pb
, best_parallel_eqn
,
4634 parallel_difference
,
4638 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4639 fprintf (dump_file
, "have to do exact analysis\n");
4643 for (e
= 0; e
< pb
->num_geqs
; e
++)
4644 if (pb
->geqs
[e
].coef
[i
] > 1)
4645 lower_bound
[lower_bounds
++] = e
;
4647 /* Sort array LOWER_BOUND. */
4648 for (j
= 0; j
< lower_bounds
; j
++)
4650 int k
, smallest
= j
;
4652 for (k
= j
+ 1; k
< lower_bounds
; k
++)
4653 if (pb
->geqs
[lower_bound
[smallest
]].coef
[i
] >
4654 pb
->geqs
[lower_bound
[k
]].coef
[i
])
4657 k
= lower_bound
[smallest
];
4658 lower_bound
[smallest
] = lower_bound
[j
];
4662 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4664 fprintf (dump_file
, "lower bound coefficients = ");
4666 for (j
= 0; j
< lower_bounds
; j
++)
4667 fprintf (dump_file
, " %d",
4668 pb
->geqs
[lower_bound
[j
]].coef
[i
]);
4670 fprintf (dump_file
, "\n");
4673 for (j
= 0; j
< lower_bounds
; j
++)
4677 int worst_lower_bound_constant
= -minC
;
4680 max_incr
= (((pb
->geqs
[e
].coef
[i
] - 1) *
4681 (worst_lower_bound_constant
- 1) - 1)
4682 / worst_lower_bound_constant
);
4683 /* max_incr += 2; */
4685 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4687 fprintf (dump_file
, "for equation ");
4688 omega_print_geq (dump_file
, pb
, &pb
->geqs
[e
]);
4690 "\ntry decrements from 0 to %d\n",
4692 omega_print_problem (dump_file
, pb
);
4695 if (max_incr
> 50 && !smoothed
4696 && smooth_weird_equations (pb
))
4702 goto solve_geq_start
;
4705 omega_copy_eqn (&pb
->eqs
[0], &pb
->geqs
[e
],
4707 pb
->eqs
[0].color
= omega_black
;
4708 omega_init_eqn_zero (&pb
->geqs
[e
], pb
->num_vars
);
4709 pb
->geqs
[e
].touched
= 1;
4712 for (c
= max_incr
; c
>= 0; c
--)
4714 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4717 "trying next decrement of %d\n",
4719 omega_print_problem (dump_file
, pb
);
4722 omega_copy_problem (rS
, pb
);
4724 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4725 omega_print_problem (dump_file
, rS
);
4727 result
= omega_solve_problem (rS
, desired_res
);
4729 if (result
== omega_true
)
4738 pb
->eqs
[0].coef
[0]--;
4741 if (j
+ 1 < lower_bounds
)
4744 omega_copy_eqn (&pb
->geqs
[e
], &pb
->eqs
[0],
4746 pb
->geqs
[e
].touched
= 1;
4747 pb
->geqs
[e
].color
= omega_black
;
4748 omega_copy_problem (rS
, pb
);
4750 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4752 "exhausted lower bound, "
4753 "checking if still feasible ");
4755 result
= omega_solve_problem (rS
, omega_false
);
4757 if (result
== omega_false
)
4762 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4763 fprintf (dump_file
, "fall-off the end\n");
4775 return omega_unknown
;
4776 } while (eliminate_again
);
4780 /* Because the omega solver is recursive, this counter limits the
4782 static int omega_solve_depth
= 0;
4784 /* Return omega_true when the problem PB has a solution following the
4788 omega_solve_problem (omega_pb pb
, enum omega_result desired_res
)
4790 enum omega_result result
;
4792 gcc_assert (pb
->num_vars
>= pb
->safe_vars
);
4793 omega_solve_depth
++;
4795 if (desired_res
!= omega_simplify
)
4798 if (omega_solve_depth
> 50)
4800 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4803 "Solve depth = %d, in_approximate_mode = %d, aborting\n",
4804 omega_solve_depth
, in_approximate_mode
);
4805 omega_print_problem (dump_file
, pb
);
4810 if (omega_solve_eq (pb
, desired_res
) == omega_false
)
4812 omega_solve_depth
--;
4816 if (in_approximate_mode
&& !pb
->num_geqs
)
4818 result
= omega_true
;
4819 pb
->num_vars
= pb
->safe_vars
;
4820 omega_problem_reduced (pb
);
4823 result
= omega_solve_geq (pb
, desired_res
);
4825 omega_solve_depth
--;
4827 if (!omega_reduce_with_subs
)
4829 resurrect_subs (pb
);
4830 gcc_assert (please_no_equalities_in_simplified_problems
4831 || !result
|| pb
->num_subs
== 0);
4837 /* Return true if red equations constrain the set of possible solutions.
4838 We assume that there are solutions to the black equations by
4839 themselves, so if there is no solution to the combined problem, we
4843 omega_problem_has_red_equations (omega_pb pb
)
4849 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4851 fprintf (dump_file
, "Checking for red equations:\n");
4852 omega_print_problem (dump_file
, pb
);
4855 please_no_equalities_in_simplified_problems
++;
4858 if (omega_single_result
)
4859 return_single_result
++;
4861 create_color
= true;
4862 result
= (omega_simplify_problem (pb
) == omega_false
);
4864 if (omega_single_result
)
4865 return_single_result
--;
4868 please_no_equalities_in_simplified_problems
--;
4872 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4873 fprintf (dump_file
, "Gist is FALSE\n");
4878 pb
->eqs
[0].color
= omega_red
;
4880 for (i
= pb
->num_vars
; i
> 0; i
--)
4881 pb
->eqs
[0].coef
[i
] = 0;
4883 pb
->eqs
[0].coef
[0] = 1;
4887 free_red_eliminations (pb
);
4888 gcc_assert (pb
->num_eqs
== 0);
4890 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4891 if (pb
->geqs
[e
].color
== omega_red
)
4897 for (i
= pb
->safe_vars
; i
>= 1; i
--)
4902 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4904 if (pb
->geqs
[e
].coef
[i
])
4906 if (pb
->geqs
[e
].coef
[i
] > 0)
4907 lb
|= (1 + (pb
->geqs
[e
].color
== omega_red
? 1 : 0));
4910 ub
|= (1 + (pb
->geqs
[e
].color
== omega_red
? 1 : 0));
4914 if (ub
== 2 || lb
== 2)
4917 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4918 fprintf (dump_file
, "checks for upper/lower bounds worked!\n");
4920 if (!omega_reduce_with_subs
)
4922 resurrect_subs (pb
);
4923 gcc_assert (pb
->num_subs
== 0);
4931 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4933 "*** Doing potentially expensive elimination tests "
4934 "for red equations\n");
4936 please_no_equalities_in_simplified_problems
++;
4937 omega_eliminate_red (pb
, true);
4938 please_no_equalities_in_simplified_problems
--;
4941 gcc_assert (pb
->num_eqs
== 0);
4943 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
4944 if (pb
->geqs
[e
].color
== omega_red
)
4947 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4951 "******************** Redundant Red Equations eliminated!!\n");
4954 "******************** Red Equations remain\n");
4956 omega_print_problem (dump_file
, pb
);
4959 if (!omega_reduce_with_subs
)
4961 normalize_return_type r
;
4963 resurrect_subs (pb
);
4964 r
= normalize_omega_problem (pb
);
4965 gcc_assert (r
!= normalize_false
);
4968 cleanout_wildcards (pb
);
4969 gcc_assert (pb
->num_subs
== 0);
4975 /* Calls omega_simplify_problem in approximate mode. */
4978 omega_simplify_approximate (omega_pb pb
)
4980 enum omega_result result
;
4982 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4983 fprintf (dump_file
, "(Entering approximate mode\n");
4985 in_approximate_mode
= true;
4986 result
= omega_simplify_problem (pb
);
4987 in_approximate_mode
= false;
4989 gcc_assert (pb
->num_vars
== pb
->safe_vars
);
4990 if (!omega_reduce_with_subs
)
4991 gcc_assert (pb
->num_subs
== 0);
4993 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4994 fprintf (dump_file
, "Leaving approximate mode)\n");
5000 /* Simplifies problem PB by eliminating redundant constraints and
5001 reducing the constraints system to a minimal form. Returns
5002 omega_true when the problem was successfully reduced, omega_unknown
5003 when the solver is unable to determine an answer. */
5006 omega_simplify_problem (omega_pb pb
)
5010 omega_found_reduction
= omega_false
;
5012 if (!pb
->variables_initialized
)
5013 omega_initialize_variables (pb
);
5015 if (next_key
* 3 > MAX_KEYS
)
5020 next_key
= OMEGA_MAX_VARS
+ 1;
5022 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5023 pb
->geqs
[e
].touched
= 1;
5025 for (i
= 0; i
< HASH_TABLE_SIZE
; i
++)
5026 hash_master
[i
].touched
= -1;
5028 pb
->hash_version
= hash_version
;
5031 else if (pb
->hash_version
!= hash_version
)
5035 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5036 pb
->geqs
[e
].touched
= 1;
5038 pb
->hash_version
= hash_version
;
5041 if (pb
->num_vars
> pb
->num_eqs
+ 3 * pb
->safe_vars
)
5042 omega_free_eliminations (pb
, pb
->safe_vars
);
5044 if (!may_be_red
&& pb
->num_subs
== 0 && pb
->safe_vars
== 0)
5046 omega_found_reduction
= omega_solve_problem (pb
, omega_unknown
);
5048 if (omega_found_reduction
!= omega_false
5049 && !return_single_result
)
5053 (*omega_when_reduced
) (pb
);
5056 return omega_found_reduction
;
5059 omega_solve_problem (pb
, omega_simplify
);
5061 if (omega_found_reduction
!= omega_false
)
5063 for (i
= 1; omega_safe_var_p (pb
, i
); i
++)
5064 pb
->forwarding_address
[pb
->var
[i
]] = i
;
5066 for (i
= 0; i
< pb
->num_subs
; i
++)
5067 pb
->forwarding_address
[pb
->subs
[i
].key
] = -i
- 1;
5070 if (!omega_reduce_with_subs
)
5071 gcc_assert (please_no_equalities_in_simplified_problems
5072 || omega_found_reduction
== omega_false
5073 || pb
->num_subs
== 0);
5075 return omega_found_reduction
;
5078 /* Make variable VAR unprotected: it then can be eliminated. */
5081 omega_unprotect_variable (omega_pb pb
, int var
)
5084 idx
= pb
->forwarding_address
[var
];
5091 if (idx
< pb
->num_subs
)
5093 omega_copy_eqn (&pb
->subs
[idx
], &pb
->subs
[pb
->num_subs
],
5095 pb
->forwarding_address
[pb
->subs
[idx
].key
] = -idx
- 1;
5100 int *bring_to_life
= XNEWVEC (int, OMEGA_MAX_VARS
);
5103 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5104 bring_to_life
[e
] = (pb
->subs
[e
].coef
[idx
] != 0);
5106 for (e2
= pb
->num_subs
- 1; e2
>= 0; e2
--)
5107 if (bring_to_life
[e2
])
5112 if (pb
->safe_vars
< pb
->num_vars
)
5114 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5116 pb
->geqs
[e
].coef
[pb
->num_vars
] =
5117 pb
->geqs
[e
].coef
[pb
->safe_vars
];
5119 pb
->geqs
[e
].coef
[pb
->safe_vars
] = 0;
5122 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5124 pb
->eqs
[e
].coef
[pb
->num_vars
] =
5125 pb
->eqs
[e
].coef
[pb
->safe_vars
];
5127 pb
->eqs
[e
].coef
[pb
->safe_vars
] = 0;
5130 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5132 pb
->subs
[e
].coef
[pb
->num_vars
] =
5133 pb
->subs
[e
].coef
[pb
->safe_vars
];
5135 pb
->subs
[e
].coef
[pb
->safe_vars
] = 0;
5138 pb
->var
[pb
->num_vars
] = pb
->var
[pb
->safe_vars
];
5139 pb
->forwarding_address
[pb
->var
[pb
->num_vars
]] =
5144 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5145 pb
->geqs
[e
].coef
[pb
->safe_vars
] = 0;
5147 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5148 pb
->eqs
[e
].coef
[pb
->safe_vars
] = 0;
5150 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5151 pb
->subs
[e
].coef
[pb
->safe_vars
] = 0;
5154 pb
->var
[pb
->safe_vars
] = pb
->subs
[e2
].key
;
5155 pb
->forwarding_address
[pb
->subs
[e2
].key
] = pb
->safe_vars
;
5157 omega_copy_eqn (&(pb
->eqs
[pb
->num_eqs
]), &(pb
->subs
[e2
]),
5159 pb
->eqs
[pb
->num_eqs
++].coef
[pb
->safe_vars
] = -1;
5160 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5162 if (e2
< pb
->num_subs
- 1)
5163 omega_copy_eqn (&(pb
->subs
[e2
]), &(pb
->subs
[pb
->num_subs
- 1]),
5169 omega_unprotect_1 (pb
, &idx
, NULL
);
5170 free (bring_to_life
);
5173 chain_unprotect (pb
);
5176 /* Unprotects VAR and simplifies PB. */
5179 omega_constrain_variable_sign (omega_pb pb
, enum omega_eqn_color color
,
5182 int n_vars
= pb
->num_vars
;
5184 int k
= pb
->forwarding_address
[var
];
5193 omega_copy_eqn (&pb
->geqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5195 for (j
= 0; j
<= n_vars
; j
++)
5196 pb
->geqs
[e
].coef
[j
] *= sign
;
5198 pb
->geqs
[e
].coef
[0]--;
5199 pb
->geqs
[e
].touched
= 1;
5200 pb
->geqs
[e
].color
= color
;
5205 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5206 omega_copy_eqn (&pb
->eqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5207 pb
->eqs
[e
].color
= color
;
5213 omega_init_eqn_zero (&pb
->geqs
[e
], pb
->num_vars
);
5214 pb
->geqs
[e
].coef
[k
] = sign
;
5215 pb
->geqs
[e
].coef
[0] = -1;
5216 pb
->geqs
[e
].touched
= 1;
5217 pb
->geqs
[e
].color
= color
;
5222 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5223 omega_init_eqn_zero (&pb
->eqs
[e
], pb
->num_vars
);
5224 pb
->eqs
[e
].coef
[k
] = 1;
5225 pb
->eqs
[e
].color
= color
;
5228 omega_unprotect_variable (pb
, var
);
5229 return omega_simplify_problem (pb
);
5232 /* Add an equation "VAR = VALUE" with COLOR to PB. */
5235 omega_constrain_variable_value (omega_pb pb
, enum omega_eqn_color color
,
5239 int k
= pb
->forwarding_address
[var
];
5245 gcc_assert (pb
->num_eqs
<= OMEGA_MAX_EQS
);
5246 omega_copy_eqn (&pb
->eqs
[e
], &pb
->subs
[k
], pb
->num_vars
);
5247 pb
->eqs
[e
].coef
[0] -= value
;
5252 omega_init_eqn_zero (&pb
->eqs
[e
], pb
->num_vars
);
5253 pb
->eqs
[e
].coef
[k
] = 1;
5254 pb
->eqs
[e
].coef
[0] = -value
;
5257 pb
->eqs
[e
].color
= color
;
5260 /* Return false when the upper and lower bounds are not coupled.
5261 Initialize the bounds LOWER_BOUND and UPPER_BOUND for the values of
5265 omega_query_variable (omega_pb pb
, int i
, int *lower_bound
, int *upper_bound
)
5267 int n_vars
= pb
->num_vars
;
5270 bool coupled
= false;
5272 *lower_bound
= neg_infinity
;
5273 *upper_bound
= pos_infinity
;
5274 i
= pb
->forwarding_address
[i
];
5280 for (j
= 1; j
<= n_vars
; j
++)
5281 if (pb
->subs
[i
].coef
[j
] != 0)
5284 *upper_bound
= *lower_bound
= pb
->subs
[i
].coef
[0];
5288 for (e
= pb
->num_subs
- 1; e
>= 0; e
--)
5289 if (pb
->subs
[e
].coef
[i
] != 0)
5292 for (e
= pb
->num_eqs
- 1; e
>= 0; e
--)
5293 if (pb
->eqs
[e
].coef
[i
] != 0)
5297 for (j
= 1; j
<= n_vars
; j
++)
5298 if (i
!= j
&& pb
->eqs
[e
].coef
[j
] != 0)
5309 *lower_bound
= *upper_bound
=
5310 -pb
->eqs
[e
].coef
[i
] * pb
->eqs
[e
].coef
[0];
5315 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5316 if (pb
->geqs
[e
].coef
[i
] != 0)
5318 if (pb
->geqs
[e
].key
== i
)
5319 *lower_bound
= MAX (*lower_bound
, -pb
->geqs
[e
].coef
[0]);
5321 else if (pb
->geqs
[e
].key
== -i
)
5322 *upper_bound
= MIN (*upper_bound
, pb
->geqs
[e
].coef
[0]);
5331 /* Sets the lower bound L and upper bound U for the values of variable
5332 I, and sets COULD_BE_ZERO to true if variable I might take value
5333 zero. LOWER_BOUND and UPPER_BOUND are bounds on the values of
5337 query_coupled_variable (omega_pb pb
, int i
, int *l
, int *u
,
5338 bool *could_be_zero
, int lower_bound
, int upper_bound
)
5345 /* Preconditions. */
5346 gcc_assert (abs (pb
->forwarding_address
[i
]) == 1
5347 && pb
->num_vars
+ pb
->num_subs
== 2
5348 && pb
->num_eqs
+ pb
->num_subs
== 1);
5350 /* Define variable I in terms of variable V. */
5351 if (pb
->forwarding_address
[i
] == -1)
5360 sign
= -eqn
->coef
[1];
5364 for (e
= pb
->num_geqs
- 1; e
>= 0; e
--)
5365 if (pb
->geqs
[e
].coef
[v
] != 0)
5367 if (pb
->geqs
[e
].coef
[v
] == 1)
5368 lower_bound
= MAX (lower_bound
, -pb
->geqs
[e
].coef
[0]);
5371 upper_bound
= MIN (upper_bound
, pb
->geqs
[e
].coef
[0]);
5374 if (lower_bound
> upper_bound
)
5382 if (lower_bound
== neg_infinity
)
5384 if (eqn
->coef
[v
] > 0)
5385 b1
= sign
* neg_infinity
;
5388 b1
= -sign
* neg_infinity
;
5391 b1
= sign
* (eqn
->coef
[0] + eqn
->coef
[v
] * lower_bound
);
5393 if (upper_bound
== pos_infinity
)
5395 if (eqn
->coef
[v
] > 0)
5396 b2
= sign
* pos_infinity
;
5399 b2
= -sign
* pos_infinity
;
5402 b2
= sign
* (eqn
->coef
[0] + eqn
->coef
[v
] * upper_bound
);
5404 *l
= MAX (*l
, b1
<= b2
? b1
: b2
);
5405 *u
= MIN (*u
, b1
<= b2
? b2
: b1
);
5407 *could_be_zero
= (*l
<= 0 && 0 <= *u
5408 && int_mod (eqn
->coef
[0], abs (eqn
->coef
[v
])) == 0);
5411 /* Return false when a lower bound L and an upper bound U for variable
5412 I in problem PB have been initialized. */
5415 omega_query_variable_bounds (omega_pb pb
, int i
, int *l
, int *u
)
5420 if (!omega_query_variable (pb
, i
, l
, u
)
5421 || (pb
->num_vars
== 1 && pb
->forwarding_address
[i
] == 1))
5424 if (abs (pb
->forwarding_address
[i
]) == 1
5425 && pb
->num_vars
+ pb
->num_subs
== 2
5426 && pb
->num_eqs
+ pb
->num_subs
== 1)
5429 query_coupled_variable (pb
, i
, l
, u
, &could_be_zero
, neg_infinity
,
5437 /* For problem PB, return an integer that represents the classic data
5438 dependence direction in function of the DD_LT, DD_EQ and DD_GT bit
5439 masks that are added to the result. When DIST_KNOWN is true, DIST
5440 is set to the classic data dependence distance. LOWER_BOUND and
5441 UPPER_BOUND are bounds on the value of variable I, for example, it
5442 is possible to narrow the iteration domain with safe approximations
5443 of loop counts, and thus discard some data dependences that cannot
5447 omega_query_variable_signs (omega_pb pb
, int i
, int dd_lt
,
5448 int dd_eq
, int dd_gt
, int lower_bound
,
5449 int upper_bound
, bool *dist_known
, int *dist
)
5458 omega_query_variable (pb
, i
, &l
, &u
);
5459 query_coupled_variable (pb
, i
, &l
, &u
, &could_be_zero
, lower_bound
,
5478 *dist_known
= false;
5483 /* Initialize PB as an Omega problem with NVARS variables and NPROT
5484 safe variables. Safe variables are not eliminated during the
5485 Fourier-Motzkin elimination. Safe variables are all those
5486 variables that are placed at the beginning of the array of
5487 variables: P->var[0, ..., NPROT - 1]. */
5490 omega_alloc_problem (int nvars
, int nprot
)
5494 gcc_assert (nvars
<= OMEGA_MAX_VARS
);
5495 omega_initialize ();
5497 /* Allocate and initialize PB. */
5498 pb
= XCNEW (struct omega_pb_d
);
5499 pb
->var
= XCNEWVEC (int, OMEGA_MAX_VARS
+ 2);
5500 pb
->forwarding_address
= XCNEWVEC (int, OMEGA_MAX_VARS
+ 2);
5501 pb
->geqs
= omega_alloc_eqns (0, OMEGA_MAX_GEQS
);
5502 pb
->eqs
= omega_alloc_eqns (0, OMEGA_MAX_EQS
);
5503 pb
->subs
= omega_alloc_eqns (0, OMEGA_MAX_VARS
+ 1);
5505 pb
->hash_version
= hash_version
;
5506 pb
->num_vars
= nvars
;
5507 pb
->safe_vars
= nprot
;
5508 pb
->variables_initialized
= false;
5509 pb
->variables_freed
= false;
5516 /* Keeps the state of the initialization. */
5517 static bool omega_initialized
= false;
5519 /* Initialization of the Omega solver. */
5522 omega_initialize (void)
5526 if (omega_initialized
)
5530 next_key
= OMEGA_MAX_VARS
+ 1;
5531 packing
= XCNEWVEC (int, OMEGA_MAX_VARS
);
5532 fast_lookup
= XCNEWVEC (int, MAX_KEYS
* 2);
5533 fast_lookup_red
= XCNEWVEC (int, MAX_KEYS
* 2);
5534 hash_master
= omega_alloc_eqns (0, HASH_TABLE_SIZE
);
5536 for (i
= 0; i
< HASH_TABLE_SIZE
; i
++)
5537 hash_master
[i
].touched
= -1;
5539 sprintf (wild_name
[0], "1");
5540 sprintf (wild_name
[1], "a");
5541 sprintf (wild_name
[2], "b");
5542 sprintf (wild_name
[3], "c");
5543 sprintf (wild_name
[4], "d");
5544 sprintf (wild_name
[5], "e");
5545 sprintf (wild_name
[6], "f");
5546 sprintf (wild_name
[7], "g");
5547 sprintf (wild_name
[8], "h");
5548 sprintf (wild_name
[9], "i");
5549 sprintf (wild_name
[10], "j");
5550 sprintf (wild_name
[11], "k");
5551 sprintf (wild_name
[12], "l");
5552 sprintf (wild_name
[13], "m");
5553 sprintf (wild_name
[14], "n");
5554 sprintf (wild_name
[15], "o");
5555 sprintf (wild_name
[16], "p");
5556 sprintf (wild_name
[17], "q");
5557 sprintf (wild_name
[18], "r");
5558 sprintf (wild_name
[19], "s");
5559 sprintf (wild_name
[20], "t");
5560 sprintf (wild_name
[40 - 1], "alpha");
5561 sprintf (wild_name
[40 - 2], "beta");
5562 sprintf (wild_name
[40 - 3], "gamma");
5563 sprintf (wild_name
[40 - 4], "delta");
5564 sprintf (wild_name
[40 - 5], "tau");
5565 sprintf (wild_name
[40 - 6], "sigma");
5566 sprintf (wild_name
[40 - 7], "chi");
5567 sprintf (wild_name
[40 - 8], "omega");
5568 sprintf (wild_name
[40 - 9], "pi");
5569 sprintf (wild_name
[40 - 10], "ni");
5570 sprintf (wild_name
[40 - 11], "Alpha");
5571 sprintf (wild_name
[40 - 12], "Beta");
5572 sprintf (wild_name
[40 - 13], "Gamma");
5573 sprintf (wild_name
[40 - 14], "Delta");
5574 sprintf (wild_name
[40 - 15], "Tau");
5575 sprintf (wild_name
[40 - 16], "Sigma");
5576 sprintf (wild_name
[40 - 17], "Chi");
5577 sprintf (wild_name
[40 - 18], "Omega");
5578 sprintf (wild_name
[40 - 19], "xxx");
5580 omega_initialized
= true;