isl_pw_*_union_opt_cmp: avoid constructing a quadratic number of pieces
If the inputs have m and n pieces respectively, then the result computed
by the original code could have as many as 2 * (m + 1) * (n + 1) pieces,
while m + n pieces are sufficient.
This could result in an explosion of the number of pieces when
this function is applied several times in succession.
Moreover, some of the pieces would end up being defined over lower-dimensional
domains, resulting in a simplification of the function value expression,
making it more difficult to detect later on that these function value
expressions are actually equal to those of other pieces.
Create a result consisting of only m + n pieces by focusing
on the function value expressions. In particular, for each
function value expression in each of the inputs, there is
exactly one piece in the output. Note that the number of disjuncts
in the domains of the pieces may still increase more than linearly,
but the spurious simplification over parts of the domains no longer occurs.
For each pair of pieces, the corresponding domains A and B are updated
as
A <- (A \setminus C) \cup ((A \cap C) \setminus B)
B <- (B \cap C) \cup ((B \setminus C) \setminus A)
where "C" is the set where the function value expression of the B-piece
is better than that of the A-piece.
This computation could be further simplified for cases where
the intersection of A and B is either a subset of C or disjoint from C.
In the first case, B does not need to be updated (because the B-piece
is always better than the A-piece) and A is updated to A \setminus B.
In the second case, A does not need to be updated (because the
B-piece is never better than the A-piece) and B is updated to
B \setminus A.
It is not clear if this simplification would produce simpler
results in practice.
Also, if the A-piece and the B-piece have the same function value
expression, then in principle, their domains do not need to be updated
since the final result will contain a single piece with the union
of the domains as domain. Again, it is not clear if this
would produce simpler results in practice.
In fact, if the resulting domain is later used to subtract
elements from some other set, then the result of this subtraction
may even end up containing more disjuncts.
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>