summaryrefslogtreecommitdiff
path: root/isl_tab_pip.c
blob: f284d25f669165b2c1ec120e3a4800f9e34b7786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
#include "isl_map_private.h"
#include "isl_seq.h"
#include "isl_tab.h"

/*
 * The implementation of parametric integer linear programming in this file
 * was inspired by the paper "Parametric Integer Programming" and the
 * report "Solving systems of affine (in)equalities" by Paul Feautrier
 * (and others).
 *
 * The strategy used for obtaining a feasible solution is different
 * from the one used in isl_tab.c.  In particular, in isl_tab.c,
 * upon finding a constraint that is not yet satisfied, we pivot
 * in a row that increases the constant term of row holding the
 * constraint, making sure the sample solution remains feasible
 * for all the constraints it already satisfied.
 * Here, we always pivot in the row holding the constraint,
 * choosing a column that induces the lexicographically smallest
 * increment to the sample solution.
 *
 * By starting out from a sample value that is lexicographically
 * smaller than any integer point in the problem space, the first
 * feasible integer sample point we find will also be the lexicographically
 * smallest.  If all variables can be assumed to be non-negative,
 * then the initial sample value may be chosen equal to zero.
 * However, we will not make this assumption.  Instead, we apply
 * the "big parameter" trick.  Any variable x is then not directly
 * used in the tableau, but instead it its represented by another
 * variable x' = M + x, where M is an arbitrarily large (positive)
 * value.  x' is therefore always non-negative, whatever the value of x.
 * Taking as initial smaple value x' = 0 corresponds to x = -M,
 * which is always smaller than any possible value of x.
 *
 * We use the big parameter trick both in the main tableau and
 * the context tableau, each of course having its own big parameter.
 * Before doing any real work, we check if all the parameters
 * happen to be non-negative.  If so, we drop the column corresponding
 * to M from the initial context tableau.
 */

/* isl_sol is an interface for constructing a solution to
 * a parametric integer linear programming problem.
 * Every time the algorithm reaches a state where a solution
 * can be read off from the tableau (including cases where the tableau
 * is empty), the function "add" is called on the isl_sol passed
 * to find_solutions_main.
 *
 * The context tableau is owned by isl_sol and is updated incrementally.
 *
 * There is currently only one implementation of this interface,
 * isl_sol_map, which simply collects the solutions in an isl_map
 * and (optionally) the parts of the context where there is no solution
 * in an isl_set.
 */
struct isl_sol {
	struct isl_tab *context_tab;
	struct isl_sol *(*add)(struct isl_sol *sol, struct isl_tab *tab);
	void (*free)(struct isl_sol *sol);
};

static void sol_free(struct isl_sol *sol)
{
	if (!sol)
		return;
	sol->free(sol);
}

struct isl_sol_map {
	struct isl_sol	sol;
	struct isl_map	*map;
	struct isl_set	*empty;
	int		max;
};

static void sol_map_free(struct isl_sol_map *sol_map)
{
	isl_tab_free(sol_map->sol.context_tab);
	isl_map_free(sol_map->map);
	isl_set_free(sol_map->empty);
	free(sol_map);
}

static void sol_map_free_wrap(struct isl_sol *sol)
{
	sol_map_free((struct isl_sol_map *)sol);
}

static struct isl_sol_map *add_empty(struct isl_sol_map *sol)
{
	struct isl_basic_set *bset;

	if (!sol->empty)
		return sol;
	sol->empty = isl_set_grow(sol->empty, 1);
	bset = isl_basic_set_copy(sol->sol.context_tab->bset);
	bset = isl_basic_set_simplify(bset);
	bset = isl_basic_set_finalize(bset);
	sol->empty = isl_set_add(sol->empty, bset);
	if (!sol->empty)
		goto error;
	return sol;
error:
	sol_map_free(sol);
	return NULL;
}

/* Add the solution identified by the tableau and the context tableau.
 *
 * The layout of the variables is as follows.
 *	tab->n_var is equal to the total number of variables in the input
 *			map (including divs that were copied from the context)
 *			+ the number of extra divs constructed
 *      Of these, the first tab->n_param and the last tab->n_div variables
 *	correspond to the variables in the context, i.e.,
		tab->n_param + tab->n_div = context_tab->n_var
 *	tab->n_param is equal to the number of parameters and input
 *			dimensions in the input map
 *	tab->n_div is equal to the number of divs in the context
 *
 * If there is no solution, then the basic set corresponding to the
 * context tableau is added to the set "empty".
 *
 * Otherwise, a basic map is constructed with the same parameters
 * and divs as the context, the dimensions of the context as input
 * dimensions and a number of output dimensions that is equal to
 * the number of output dimensions in the input map.
 * The divs in the input map (if any) that do not correspond to any
 * div in the context do not appear in the solution.
 * The algorithm will make sure that they have an integer value,
 * but these values themselves are of no interest.
 *
 * The constraints and divs of the context are simply copied
 * fron context_tab->bset.
 * To extract the value of the output variables, it should be noted
 * that we always use a big parameter M and so the variable stored
 * in the tableau is not an output variable x itself, but
 *	x' = M + x (in case of minimization)
 * or
 *	x' = M - x (in case of maximization)
 * If x' appears in a column, then its optimal value is zero,
 * which means that the optimal value of x is an unbounded number
 * (-M for minimization and M for maximization).
 * We currently assume that the output dimensions in the original map
 * are bounded, so this cannot occur.
 * Similarly, when x' appears in a row, then the coefficient of M in that
 * row is necessarily 1.
 * If the row represents
 *	d x' = c + d M + e(y)
 * then, in case of minimization, an equality
 *	c + e(y) - d x' = 0
 * is added, and in case of maximization,
 *	c + e(y) + d x' = 0
 */
static struct isl_sol_map *sol_map_add(struct isl_sol_map *sol,
	struct isl_tab *tab)
{
	int i;
	struct isl_basic_map *bmap = NULL;
	struct isl_tab *context_tab;
	unsigned n_eq;
	unsigned n_ineq;
	unsigned nparam;
	unsigned total;
	unsigned n_div;
	unsigned n_out;
	unsigned off;

	if (!sol || !tab)
		goto error;

	if (tab->empty)
		return add_empty(sol);

	context_tab = sol->sol.context_tab;
	off = 2 + tab->M;
	n_out = isl_map_dim(sol->map, isl_dim_out);
	n_eq = context_tab->bset->n_eq + n_out;
	n_ineq = context_tab->bset->n_ineq;
	nparam = tab->n_param;
	total = isl_map_dim(sol->map, isl_dim_all);
	bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
				    tab->n_div, n_eq, 2 * tab->n_div + n_ineq);
	if (!bmap)
		goto error;
	n_div = tab->n_div;
	if (tab->rational)
		ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
	for (i = 0; i < context_tab->bset->n_div; ++i) {
		int k = isl_basic_map_alloc_div(bmap);
		if (k < 0)
			goto error;
		isl_seq_cpy(bmap->div[k],
			    context_tab->bset->div[i], 1 + 1 + nparam);
		isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
		isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
			    context_tab->bset->div[i] + 1 + 1 + nparam, i);
	}
	for (i = 0; i < context_tab->bset->n_eq; ++i) {
		int k = isl_basic_map_alloc_equality(bmap);
		if (k < 0)
			goto error;
		isl_seq_cpy(bmap->eq[k], context_tab->bset->eq[i], 1 + nparam);
		isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
		isl_seq_cpy(bmap->eq[k] + 1 + total,
			    context_tab->bset->eq[i] + 1 + nparam, n_div);
	}
	for (i = 0; i < context_tab->bset->n_ineq; ++i) {
		int k = isl_basic_map_alloc_inequality(bmap);
		if (k < 0)
			goto error;
		isl_seq_cpy(bmap->ineq[k],
			context_tab->bset->ineq[i], 1 + nparam);
		isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
		isl_seq_cpy(bmap->ineq[k] + 1 + total,
			context_tab->bset->ineq[i] + 1 + nparam, n_div);
	}
	for (i = tab->n_param; i < total; ++i) {
		int k = isl_basic_map_alloc_equality(bmap);
		if (k < 0)
			goto error;
		isl_seq_clr(bmap->eq[k] + 1, isl_basic_map_total_dim(bmap));
		if (!tab->var[i].is_row) {
			/* no unbounded */
			isl_assert(bmap->ctx, !tab->M, goto error);
			isl_int_set_si(bmap->eq[k][0], 0);
			if (sol->max)
				isl_int_set_si(bmap->eq[k][1 + i], 1);
			else
				isl_int_set_si(bmap->eq[k][1 + i], -1);
		} else {
			int row, j;
			row = tab->var[i].index;
			/* no unbounded */
			if (tab->M)
				isl_assert(bmap->ctx,
					isl_int_eq(tab->mat->row[row][2],
						   tab->mat->row[row][0]),
					goto error);
			isl_int_set(bmap->eq[k][0], tab->mat->row[row][1]);
			for (j = 0; j < tab->n_param; ++j) {
				int col;
				if (tab->var[j].is_row)
					continue;
				col = tab->var[j].index;
				isl_int_set(bmap->eq[k][1 + j],
					    tab->mat->row[row][off + col]);
			}
			for (j = 0; j < tab->n_div; ++j) {
				int col;
				if (tab->var[tab->n_var - tab->n_div+j].is_row)
					continue;
				col = tab->var[tab->n_var - tab->n_div+j].index;
				isl_int_set(bmap->eq[k][1 + total + j],
					    tab->mat->row[row][off + col]);
			}
			if (sol->max)
				isl_int_set(bmap->eq[k][1 + i],
					    tab->mat->row[row][0]);
			else
				isl_int_neg(bmap->eq[k][1 + i],
					    tab->mat->row[row][0]);
		}
	}
	bmap = isl_basic_map_gauss(bmap, NULL);
	bmap = isl_basic_map_normalize_constraints(bmap);
	bmap = isl_basic_map_finalize(bmap);
	sol->map = isl_map_grow(sol->map, 1);
	sol->map = isl_map_add(sol->map, bmap);
	if (!sol->map)
		goto error;
	return sol;
error:
	isl_basic_map_free(bmap);
	sol_free(&sol->sol);
	return NULL;
}

static struct isl_sol *sol_map_add_wrap(struct isl_sol *sol,
	struct isl_tab *tab)
{
	return (struct isl_sol *)sol_map_add((struct isl_sol_map *)sol, tab);
}


static struct isl_basic_set *isl_basic_set_add_ineq(struct isl_basic_set *bset,
	isl_int *ineq)
{
	int k;

	bset = isl_basic_set_extend_constraints(bset, 0, 1);
	if (!bset)
		return NULL;
	k = isl_basic_set_alloc_inequality(bset);
	if (k < 0)
		goto error;
	isl_seq_cpy(bset->ineq[k], ineq, 1 + isl_basic_set_total_dim(bset));
	return bset;
error:
	isl_basic_set_free(bset);
	return NULL;
}

static struct isl_basic_set *isl_basic_set_add_eq(struct isl_basic_set *bset,
	isl_int *eq)
{
	int k;

	bset = isl_basic_set_extend_constraints(bset, 1, 0);
	if (!bset)
		return NULL;
	k = isl_basic_set_alloc_equality(bset);
	if (k < 0)
		goto error;
	isl_seq_cpy(bset->eq[k], eq, 1 + isl_basic_set_total_dim(bset));
	return bset;
error:
	isl_basic_set_free(bset);
	return NULL;
}


/* Store the "parametric constant" of row "row" of tableau "tab" in "line",
 * i.e., the constant term and the coefficients of all variables that
 * appear in the context tableau.
 * Note that the coefficient of the big parameter M is NOT copied.
 * The context tableau may not have a big parameter and even when it
 * does, it is a different big parameter.
 */
static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
{
	int i;
	unsigned off = 2 + tab->M;

	isl_int_set(line[0], tab->mat->row[row][1]);
	for (i = 0; i < tab->n_param; ++i) {
		if (tab->var[i].is_row)
			isl_int_set_si(line[1 + i], 0);
		else {
			int col = tab->var[i].index;
			isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
		}
	}
	for (i = 0; i < tab->n_div; ++i) {
		if (tab->var[tab->n_var - tab->n_div + i].is_row)
			isl_int_set_si(line[1 + tab->n_param + i], 0);
		else {
			int col = tab->var[tab->n_var - tab->n_div + i].index;
			isl_int_set(line[1 + tab->n_param + i],
				    tab->mat->row[row][off + col]);
		}
	}
}

/* Check if rows "row1" and "row2" have identical "parametric constants",
 * as explained above.
 * In this case, we also insist that the coefficients of the big parameter
 * be the same as the values of the constants will only be the same
 * if these coefficients are also the same.
 */
static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
{
	int i;
	unsigned off = 2 + tab->M;

	if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
		return 0;

	if (tab->M && isl_int_ne(tab->mat->row[row1][2],
				 tab->mat->row[row2][2]))
		return 0;

	for (i = 0; i < tab->n_param + tab->n_div; ++i) {
		int pos = i < tab->n_param ? i :
			tab->n_var - tab->n_div + i - tab->n_param;
		int col;

		if (tab->var[pos].is_row)
			continue;
		col = tab->var[pos].index;
		if (isl_int_ne(tab->mat->row[row1][off + col],
			       tab->mat->row[row2][off + col]))
			return 0;
	}
	return 1;
}

/* Return an inequality that expresses that the "parametric constant"
 * should be non-negative.
 * This function is only called when the coefficient of the big parameter
 * is equal to zero.
 */
static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
{
	struct isl_vec *ineq;

	ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
	if (!ineq)
		return NULL;

	get_row_parameter_line(tab, row, ineq->el);
	if (ineq)
		ineq = isl_vec_normalize(ineq);

	return ineq;
}

/* Return a integer division for use in a parametric cut based on the given row.
 * In particular, let the parametric constant of the row be
 *
 *		\sum_i a_i y_i
 *
 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
 * The div returned is equal to
 *
 *		floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
 */
static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
{
	struct isl_vec *div;

	div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
	if (!div)
		return NULL;

	isl_int_set(div->el[0], tab->mat->row[row][0]);
	get_row_parameter_line(tab, row, div->el + 1);
	div = isl_vec_normalize(div);
	isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
	isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);

	return div;
}

/* Return a integer division for use in transferring an integrality constraint
 * to the context.
 * In particular, let the parametric constant of the row be
 *
 *		\sum_i a_i y_i
 *
 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
 * The the returned div is equal to
 *
 *		floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
 */
static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
{
	struct isl_vec *div;

	div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
	if (!div)
		return NULL;

	isl_int_set(div->el[0], tab->mat->row[row][0]);
	get_row_parameter_line(tab, row, div->el + 1);
	div = isl_vec_normalize(div);
	isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);

	return div;
}

/* Construct and return an inequality that expresses an upper bound
 * on the given div.
 * In particular, if the div is given by
 *
 *	d = floor(e/m)
 *
 * then the inequality expresses
 *
 *	m d <= e
 */
static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
{
	unsigned total;
	unsigned div_pos;
	struct isl_vec *ineq;

	total = isl_basic_set_total_dim(bset);
	div_pos = 1 + total - bset->n_div + div;

	ineq = isl_vec_alloc(bset->ctx, 1 + total);
	if (!ineq)
		return NULL;

	isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
	isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
	return ineq;
}

/* Given a row in the tableau and a div that was created
 * using get_row_split_div and that been constrained to equality, i.e.,
 *
 *		d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
 *
 * replace the expression "\sum_i {a_i} y_i" in the row by d,
 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
 * The coefficients of the non-parameters in the tableau have been
 * verified to be integral.  We can therefore simply replace coefficient b
 * by floor(b).  For the coefficients of the parameters we have
 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
 * floor(b) = b.
 */
static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
{
	int i;
	int col;
	unsigned off = 2 + tab->M;

	isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
			tab->mat->row[row][0], 1 + tab->M + tab->n_col);

	isl_int_set_si(tab->mat->row[row][0], 1);

	isl_assert(tab->mat->ctx,
		!tab->var[tab->n_var - tab->n_div + div].is_row, goto error);

	col = tab->var[tab->n_var - tab->n_div + div].index;
	isl_int_set_si(tab->mat->row[row][off + col], 1);

	return tab;
error:
	isl_tab_free(tab);
	return NULL;
}

/* Check if the (parametric) constant of the given row is obviously
 * negative, meaning that we don't need to consult the context tableau.
 * If there is a big parameter and its coefficient is non-zero,
 * then this coefficient determines the outcome.
 * Otherwise, we check whether the constant is negative and
 * all non-zero coefficients of parameters are negative and
 * belong to non-negative parameters.
 */
static int is_obviously_neg(struct isl_tab *tab, int row)
{
	int i;
	int col;
	unsigned off = 2 + tab->M;

	if (tab->M) {
		if (isl_int_is_pos(tab->mat->row[row][2]))
			return 0;
		if (isl_int_is_neg(tab->mat->row[row][2]))
			return 1;
	}

	if (isl_int_is_nonneg(tab->mat->row[row][1]))
		return 0;
	for (i = 0; i < tab->n_param; ++i) {
		/* Eliminated parameter */
		if (tab->var[i].is_row)
			continue;
		col = tab->var[i].index;
		if (isl_int_is_zero(tab->mat->row[row][off + col]))
			continue;
		if (!tab->var[i].is_nonneg)
			return 0;
		if (isl_int_is_pos(tab->mat->row[row][off + col]))
			return 0;
	}
	for (i = 0; i < tab->n_div; ++i) {
		if (tab->var[tab->n_var - tab->n_div + i].is_row)
			continue;
		col = tab->var[tab->n_var - tab->n_div + i].index;
		if (isl_int_is_zero(tab->mat->row[row][off + col]))
			continue;
		if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
			return 0;
		if (isl_int_is_pos(tab->mat->row[row][off + col]))
			return 0;
	}
	return 1;
}

/* Check if the (parametric) constant of the given row is obviously
 * non-negative, meaning that we don't need to consult the context tableau.
 * If there is a big parameter and its coefficient is non-zero,
 * then this coefficient determines the outcome.
 * Otherwise, we check whether the constant is non-negative and
 * all non-zero coefficients of parameters are positive and
 * belong to non-negative parameters.
 */
static int is_obviously_nonneg(struct isl_tab *tab, int row)
{
	int i;
	int col;
	unsigned off = 2 + tab->M;

	if (tab->M) {
		if (isl_int_is_pos(tab->mat->row[row][2]))
			return 1;
		if (isl_int_is_neg(tab->mat->row[row][2]))
			return 0;
	}

	if (isl_int_is_neg(tab->mat->row[row][1]))
		return 0;
	for (i = 0; i < tab->n_param; ++i) {
		/* Eliminated parameter */
		if (tab->var[i].is_row)
			continue;
		col = tab->var[i].index;
		if (isl_int_is_zero(tab->mat->row[row][off + col]))
			continue;
		if (!tab->var[i].is_nonneg)
			return 0;
		if (isl_int_is_neg(tab->mat->row[row][off + col]))
			return 0;
	}
	for (i = 0; i < tab->n_div; ++i) {
		if (tab->var[tab->n_var - tab->n_div + i].is_row)
			continue;
		col = tab->var[tab->n_var - tab->n_div + i].index;
		if (isl_int_is_zero(tab->mat->row[row][off + col]))
			continue;
		if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
			return 0;
		if (isl_int_is_neg(tab->mat->row[row][off + col]))
			return 0;
	}
	return 1;
}

/* Given a row r and two columns, return the column that would
 * lead to the lexicographically smallest increment in the sample
 * solution when leaving the basis in favor of the row.
 * Pivoting with column c will increment the sample value by a non-negative
 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
 * corresponding to the non-parametric variables.
 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
 * with all other entries in this virtual row equal to zero.
 * If variable v appears in a row, then a_{v,c} is the element in column c
 * of that row.
 *
 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
 * increment.  Otherwise, it's c2.
 */
static int lexmin_col_pair(struct isl_tab *tab,
	int row, int col1, int col2, isl_int tmp)
{
	int i;
	isl_int *tr;

	tr = tab->mat->row[row] + 2 + tab->M;

	for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
		int s1, s2;
		isl_int *r;

		if (!tab->var[i].is_row) {
			if (tab->var[i].index == col1)
				return col2;
			if (tab->var[i].index == col2)
				return col1;
			continue;
		}

		if (tab->var[i].index == row)
			continue;

		r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
		s1 = isl_int_sgn(r[col1]);
		s2 = isl_int_sgn(r[col2]);
		if (s1 == 0 && s2 == 0)
			continue;
		if (s1 < s2)
			return col1;
		if (s2 < s1)
			return col2;

		isl_int_mul(tmp, r[col2], tr[col1]);
		isl_int_submul(tmp, r[col1], tr[col2]);
		if (isl_int_is_pos(tmp))
			return col1;
		if (isl_int_is_neg(tmp))
			return col2;
	}
	return -1;
}

/* Given a row in the tableau, find and return the column that would
 * result in the lexicographically smallest, but positive, increment
 * in the sample point.
 * If there is no such column, then return tab->n_col.
 * If anything goes wrong, return -1.
 */
static int lexmin_pivot_col(struct isl_tab *tab, int row)
{
	int j;
	int col = tab->n_col;
	isl_int *tr;
	isl_int tmp;

	tr = tab->mat->row[row] + 2 + tab->M;

	isl_int_init(tmp);

	for (j = tab->n_dead; j < tab->n_col; ++j) {
		if (tab->col_var[j] >= 0 &&
		    (tab->col_var[j] < tab->n_param  ||
		    tab->col_var[j] >= tab->n_var - tab->n_div))
			continue;

		if (!isl_int_is_pos(tr[j]))
			continue;

		if (col == tab->n_col)
			col = j;
		else
			col = lexmin_col_pair(tab, row, col, j, tmp);
		isl_assert(tab->mat->ctx, col >= 0, goto error);
	}

	isl_int_clear(tmp);
	return col;
error:
	isl_int_clear(tmp);
	return -1;
}

/* Return the first known violated constraint, i.e., a non-negative
 * contraint that currently has an either obviously negative value
 * or a previously determined to be negative value.
 *
 * If any constraint has a negative coefficient for the big parameter,
 * if any, then we return one of these first.
 */
static int first_neg(struct isl_tab *tab)
{
	int row;

	if (tab->M)
		for (row = tab->n_redundant; row < tab->n_row; ++row) {
			if (!isl_tab_var_from_row(tab, row)->is_nonneg)
				continue;
			if (isl_int_is_neg(tab->mat->row[row][2]))
				return row;
		}
	for (row = tab->n_redundant; row < tab->n_row; ++row) {
		if (!isl_tab_var_from_row(tab, row)->is_nonneg)
			continue;
		if (tab->row_sign) {
			if (tab->row_sign[row] == 0 &&
			    is_obviously_neg(tab, row))
				tab->row_sign[row] = isl_tab_row_neg;
			if (tab->row_sign[row] != isl_tab_row_neg)
				continue;
		} else if (!is_obviously_neg(tab, row))
			continue;
		return row;
	}
	return -1;
}

/* Resolve all known or obviously violated constraints through pivoting.
 * In particular, as long as we can find any violated constraint, we
 * look for a pivoting column that would result in the lexicographicallly
 * smallest increment in the sample point.  If there is no such column
 * then the tableau is infeasible.
 */
static struct isl_tab *restore_lexmin(struct isl_tab *tab)
{
	int row, col;

	if (!tab)
		return NULL;
	if (tab->empty)
		return tab;
	while ((row = first_neg(tab)) != -1) {
		col = lexmin_pivot_col(tab, row);
		if (col >= tab->n_col)
			return isl_tab_mark_empty(tab);
		if (col < 0)
			goto error;
		isl_tab_pivot(tab, row, col);
	}
	return tab;
error:
	isl_tab_free(tab);
	return NULL;
}

/* Given a row that represents an equality, look for an appropriate
 * pivoting column.
 * In particular, if there are any non-zero coefficients among
 * the non-parameter variables, then we take the last of these
 * variables.  Eliminating this variable in terms of the other
 * variables and/or parameters does not influence the property
 * that all column in the initial tableau are lexicographically
 * positive.  The row corresponding to the eliminated variable
 * will only have non-zero entries below the diagonal of the
 * initial tableau.  That is, we transform
 *
 *		I				I
 *		  1		into		a
 *		    I				  I
 *
 * If there is no such non-parameter variable, then we are dealing with
 * pure parameter equality and we pick any parameter with coefficient 1 or -1
 * for elimination.  This will ensure that the eliminated parameter
 * always has an integer value whenever all the other parameters are integral.
 * If there is no such parameter then we return -1.
 */
static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
{
	unsigned off = 2 + tab->M;
	int i;

	for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
		int col;
		if (tab->var[i].is_row)
			continue;
		col = tab->var[i].index;
		if (col <= tab->n_dead)
			continue;
		if (!isl_int_is_zero(tab->mat->row[row][off + col]))
			return col;
	}
	for (i = tab->n_dead; i < tab->n_col; ++i) {
		if (isl_int_is_one(tab->mat->row[row][off + i]))
			return i;
		if (isl_int_is_negone(tab->mat->row[row][off + i]))
			return i;
	}
	return -1;
}

/* Add an equality that is known to be valid to the tableau.
 * We first check if we can eliminate a variable or a parameter.
 * If not, we add the equality as two inequalities.
 * In this case, the equality was a pure parameter equality and there
 * is no need to resolve any constraint violations.
 */
static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
{
	int i;
	int r;

	if (!tab)
		return NULL;
	r = isl_tab_add_row(tab, eq);
	if (r < 0)
		goto error;

	r = tab->con[r].index;
	i = last_var_col_or_int_par_col(tab, r);
	if (i < 0) {
		tab->con[r].is_nonneg = 1;
		isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
		isl_seq_neg(eq, eq, 1 + tab->n_var);
		r = isl_tab_add_row(tab, eq);
		if (r < 0)
			goto error;
		tab->con[r].is_nonneg = 1;
		isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
	} else {
		isl_tab_pivot(tab, r, i);
		isl_tab_kill_col(tab, i);
		tab->n_eq++;

		tab = restore_lexmin(tab);
	}

	return tab;
error:
	isl_tab_free(tab);
	return NULL;
}

/* Check if the given row is a pure constant.
 */
static int is_constant(struct isl_tab *tab, int row)
{
	unsigned off = 2 + tab->M;

	return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
					tab->n_col - tab->n_dead) == -1;
}

/* Add an equality that may or may not be valid to the tableau.
 * If the resulting row is a pure constant, then it must be zero.
 * Otherwise, the resulting tableau is empty.
 *
 * If the row is not a pure constant, then we add two inequalities,
 * each time checking that they can be satisfied.
 * In the end we try to use one of the two constraints to eliminate
 * a column.
 */
static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
{
	int r1, r2;
	int sgn;
	int row;

	if (!tab)
		return NULL;
	if (tab->bset) {
		tab->bset = isl_basic_set_add_eq(tab->bset, eq);
		isl_tab_push(tab, isl_tab_undo_bset_eq);
		if (!tab->bset)
			goto error;
	}
	r1 = isl_tab_add_row(tab, eq);
	if (r1 < 0)
		goto error;
	tab->con[r1].is_nonneg = 1;
	isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]);

	row = tab->con[r1].index;
	if (is_constant(tab, row)) {
		if (!isl_int_is_zero(tab->mat->row[row][1]) ||
		    (tab->M && !isl_int_is_zero(tab->mat->row[row][2])))
			return isl_tab_mark_empty(tab);
		return tab;
	}

	tab = restore_lexmin(tab);
	if (!tab || tab->empty)
		return tab;

	isl_seq_neg(eq, eq, 1 + tab->n_var);

	r2 = isl_tab_add_row(tab, eq);
	if (r2 < 0)
		goto error;
	tab->con[r2].is_nonneg = 1;
	isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]);

	tab = restore_lexmin(tab);
	if (!tab || tab->empty)
		return tab;

	if (!tab->con[r1].is_row)
		isl_tab_kill_col(tab, tab->con[r1].index);
	else if (!tab->con[r2].is_row)
		isl_tab_kill_col(tab, tab->con[r2].index);
	else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
		unsigned off = 2 + tab->M;
		int i;
		int row = tab->con[r1].index;
		i = isl_seq_first_non_zero(tab->mat->row[row]+off+tab->n_dead,
						tab->n_col - tab->n_dead);
		if (i != -1) {
			isl_tab_pivot(tab, row, tab->n_dead + i);
			isl_tab_kill_col(tab, tab->n_dead + i);
		}
	}

	return tab;
error:
	isl_tab_free(tab);
	return NULL;
}

/* Add an inequality to the tableau, resolving violations using
 * restore_lexmin.
 */
static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
{
	int r;
	int sgn;

	if (!tab)
		return NULL;
	if (tab->bset) {
		tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
		isl_tab_push(tab, isl_tab_undo_bset_ineq);
		if (!tab->bset)
			goto error;
	}
	r = isl_tab_add_row(tab, ineq);
	if (r < 0)
		goto error;
	tab->con[r].is_nonneg = 1;
	isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
	if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
		isl_tab_mark_redundant(tab, tab->con[r].index);
		return tab;
	}

	tab = restore_lexmin(tab);
	if (tab && !tab->empty && tab->con[r].is_row &&
		 isl_tab_row_is_redundant(tab, tab->con[r].index))
		isl_tab_mark_redundant(tab, tab->con[r].index);
	return tab;
error:
	isl_tab_free(tab);
	return NULL;
}

/* Check if the coefficients of the parameters are all integral.
 */
static int integer_parameter(struct isl_tab *tab, int row)
{
	int i;
	int col;
	unsigned off = 2 + tab->M;

	for (i = 0; i < tab->n_param; ++i) {
		/* Eliminated parameter */
		if (tab->var[i].is_row)
			continue;
		col = tab->var[i].index;
		if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
						tab->mat->row[row][0]))
			return 0;
	}
	for (i = 0; i < tab->n_div; ++i) {
		if (tab->var[tab->n_var - tab->n_div + i].is_row)
			continue;
		col = tab->var[tab->n_var - tab->n_div + i].index;
		if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
						tab->mat->row[row][0]))
			return 0;
	}
	return 1;
}

/* Check if the coefficients of the non-parameter variables are all integral.
 */
static int integer_variable(struct isl_tab *tab, int row)
{
	int i;
	unsigned off = 2 + tab->M;

	for (i = 0; i < tab->n_col; ++i) {
		if (tab->col_var[i] >= 0 &&
		    (tab->col_var[i] < tab->n_param ||
		     tab->col_var[i] >= tab->n_var - tab->n_div))
			continue;
		if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
						tab->mat->row[row][0]))
			return 0;
	}
	return 1;
}

/* Check if the constant term is integral.
 */
static int integer_constant(struct isl_tab *tab, int row)
{
	return isl_int_is_divisible_by(tab->mat->row[row][1],
					tab->mat->row[row][0]);
}

#define I_CST	1 << 0
#define I_PAR	1 << 1
#define I_VAR	1 << 2

/* Check for first (non-parameter) variable that is non-integer and
 * therefore requires a cut.
 * For parametric tableaus, there are three parts in a row,
 * the constant, the coefficients of the parameters and the rest.
 * For each part, we check whether the coefficients in that part
 * are all integral and if so, set the corresponding flag in *f.
 * If the constant and the parameter part are integral, then the
 * current sample value is integral and no cut is required
 * (irrespective of whether the variable part is integral).
 */
static int first_non_integer(struct isl_tab *tab, int *f)
{
	int i;

	for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
		int flags = 0;
		int row;
		if (!tab->var[i].is_row)
			continue;
		row = tab->var[i].index;
		if (integer_constant(tab, row))
			ISL_FL_SET(flags, I_CST);
		if (integer_parameter(tab, row))
			ISL_FL_SET(flags, I_PAR);
		if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
			continue;
		if (integer_variable(tab, row))
			ISL_FL_SET(flags, I_VAR);
		*f = flags;
		return row;
	}
	return -1;
}

/* Add a (non-parametric) cut to cut away the non-integral sample
 * value of the given row.
 *
 * If the row is given by
 *
 *	m r = f + \sum_i a_i y_i
 *
 * then the cut is
 *
 *	c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
 *
 * The big parameter, if any, is ignored, since it is assumed to be big
 * enough to be divisible by any integer.
 * If the tableau is actually a parametric tableau, then this function
 * is only called when all coefficients of the parameters are integral.
 * The cut therefore has zero coefficients for the parameters.
 *
 * The current value is known to be negative, so row_sign, if it
 * exists, is set accordingly.
 *
 * Return the row of the cut or -1.
 */
static int add_cut(struct isl_tab *tab, int row)
{
	int i;
	int r;
	isl_int *r_row;
	unsigned off = 2 + tab->M;

	if (isl_tab_extend_cons(tab, 1) < 0)
		return -1;
	r = isl_tab_allocate_con(tab);
	if (r < 0)
		return -1;

	r_row = tab->mat->row[tab->con[r].index];
	isl_int_set(r_row[0], tab->mat->row[row][0]);
	isl_int_neg(r_row[1], tab->mat->row[row][1]);
	isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
	isl_int_neg(r_row[1], r_row[1]);
	if (tab->M)
		isl_int_set_si(r_row[2], 0);
	for (i = 0; i < tab->n_col; ++i)
		isl_int_fdiv_r(r_row[off + i],
			tab->mat->row[row][off + i], tab->mat->row[row][0]);

	tab->con[r].is_nonneg = 1;
	isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
	if (tab->row_sign)
		tab->row_sign[tab->con[r].index] = isl_tab_row_neg;

	return tab->con[r].index;
}

/* Given a non-parametric tableau, add cuts until an integer
 * sample point is obtained or until the tableau is determined
 * to be integer infeasible.
 * As long as there is any non-integer value in the sample point,
 * we add an appropriate cut, if possible and resolve the violated
 * cut constraint using restore_lexmin.
 * If one of the corresponding rows is equal to an integral
 * combination of variables/constraints plus a non-integral constant,
 * then there is no way to obtain an integer point an we return
 * a tableau that is marked empty.
 */
static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
{
	int row;
	int flags;

	if (!tab)
		return NULL;
	if (tab->empty)
		return tab;

	while ((row = first_non_integer(tab, &flags)) != -1) {
		if (ISL_FL_ISSET(flags, I_VAR))
			return isl_tab_mark_empty(tab);
		row = add_cut(tab, row);
		if (row < 0)
			goto error;
		tab = restore_lexmin(tab);
		if (!tab || tab->empty)
			break;
	}
	return tab;
error:
	isl_tab_free(tab);
	return NULL;
}

static struct isl_tab *drop_sample(struct isl_tab *tab, int s)
{
	if (s != tab->n_outside)
		isl_mat_swap_rows(tab->samples, tab->n_outside, s);
	tab->n_outside++;
	isl_tab_push(tab, isl_tab_undo_drop_sample);

	return tab;
}

/* Check whether all the currently active samples also satisfy the inequality
 * "ineq" (treated as an equality if eq is set).
 * Remove those samples that do not.
 */
static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
{
	int i;
	isl_int v;

	if (!tab)
		return NULL;

	isl_assert(tab->mat->ctx, tab->bset, goto error);
	isl_assert(tab->mat->ctx, tab->samples, goto error);
	isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);

	isl_int_init(v);
	for (i = tab->n_outside; i < tab->n_sample; ++i) {
		int sgn;
		isl_seq_inner_product(ineq, tab->samples->row[i],
					1 + tab->n_var, &v);
		sgn = isl_int_sgn(v);
		if (eq ? (sgn == 0) : (sgn >= 0))
			continue;
		tab = drop_sample(tab, i);
		if (!tab)
			break;
	}
	isl_int_clear(v);

	return tab;
}

/* Check whether the sample value of the tableau is finite,
 * i.e., either the tableau does not use a big parameter, or
 * all values of the variables are equal to the big parameter plus
 * some constant.  This constant is the actual sample value.
 */
int sample_is_finite(struct isl_tab *tab)
{
	int i;

	if (!tab->M)
		return 1;

	for (i = 0; i < tab->n_var; ++i) {
		int row;
		if (!tab->var[i].is_row)
			return 0;
		row = tab->var[i].index;
		if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
			return 0;
	}
	return 1;
}

/* Check if the context tableau of sol has any integer points.
 * Returns -1 if an error occurred.
 * If an integer point can be found and if moreover it is finite,
 * then it is added to the list of sample values.
 *
 * This function is only called when none of the currently active sample
 * values satisfies the most recently added constraint.
 */
static int context_is_feasible(struct isl_sol *sol)
{
	struct isl_tab_undo *snap;
	struct isl_tab *tab;
	int feasible;

	if (!sol || !sol->context_tab)
		return -1;

	snap = isl_tab_snap(sol->context_tab);
	isl_tab_push_basis(sol->context_tab);

	sol->context_tab = cut_to_integer_lexmin(sol->context_tab);
	if (!sol->context_tab)
		goto error;

	tab = sol->context_tab;
	if (!tab->empty && sample_is_finite(tab)) {
		struct isl_vec *sample;

		tab->samples = isl_mat_extend(tab->samples,
					tab->n_sample + 1, tab->samples->n_col);
		if (!tab->samples)
			goto error;

		sample = isl_tab_get_sample_value(tab);
		if (!sample)
			goto error;
		isl_seq_cpy(tab->samples->row[tab->n_sample],
				sample->el, sample->size);
		isl_vec_free(sample);
		tab->n_sample++;
	}

	feasible = !sol->context_tab->empty;
	if (isl_tab_rollback(sol->context_tab, snap) < 0)
		goto error;

	return feasible;
error:
	isl_tab_free(sol->context_tab);
	sol->context_tab = NULL;
	return -1;
}

/* First check if any of the currently active sample values satisfies
 * the inequality "ineq" (an equality if eq is set).
 * If not, continue with check_integer_feasible.
 */
static int context_valid_sample_or_feasible(struct isl_sol *sol,
	isl_int *ineq, int eq)
{
	int i;
	isl_int v;
	struct isl_tab *tab;

	if (!sol || !sol->context_tab)
		return -1;

	tab = sol->context_tab;
	isl_assert(tab->mat->ctx, tab->bset, goto error);
	isl_assert(tab->mat->ctx, tab->samples, goto error);
	isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);

	isl_int_init(v);
	for (i = tab->n_outside; i < tab->n_sample; ++i) {
		int sgn;
		isl_seq_inner_product(ineq, tab->samples->row[i],
					1 + tab->n_var, &v);
		sgn = isl_int_sgn(v);
		if (eq ? (sgn == 0) : (sgn >= 0))
			break;
	}
	isl_int_clear(v);

	if (i < tab->n_sample)
		return 1;

	return context_is_feasible(sol);
}

/* For a div d = floor(f/m), add the constraints
 *
 *		f - m d >= 0
 *		-(f-(m-1)) + m d >= 0
 *
 * Note that the second constraint is the negation of
 *
 *		f - m d >= m
 */
static struct isl_tab *add_div_constraints(struct isl_tab *tab, unsigned div)
{
	int i, j;
	unsigned total;
	unsigned div_pos;
	struct isl_vec *ineq;

	if (!tab)
		return NULL;

	total = isl_basic_set_total_dim(tab->bset);
	div_pos = 1 + total - tab->bset->n_div + div;

	ineq = ineq_for_div(tab->bset, div);
	if (!ineq)
		goto error;

	tab = add_lexmin_ineq(tab, ineq->el);

	isl_seq_neg(ineq->el, tab->bset->div[div] + 1, 1 + total);
	isl_int_set(ineq->el[div_pos], tab->bset->div[div][0]);
	isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
	isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
	tab = add_lexmin_ineq(tab, ineq->el);

	isl_vec_free(ineq);

	return tab;
error:
	isl_tab_free(tab);
	return NULL;
}

/* Add a div specified by "div" to both the main tableau and
 * the context tableau.  In case of the main tableau, we only
 * need to add an extra div.  In the context tableau, we also
 * need to express the meaning of the div.
 * Return the index of the div or -1 if anything went wrong.
 */
static int add_div(struct isl_tab *tab, struct isl_tab **context_tab,
	struct isl_vec *div)
{
	int i;
	int r;
	int k;
	struct isl_mat *samples;

	if (isl_tab_extend_vars(*context_tab, 1) < 0)
		goto error;
	r = isl_tab_allocate_var(*context_tab);
	if (r < 0)
		goto error;
	(*context_tab)->var[r].is_nonneg = 1;
	(*context_tab)->var[r].frozen = 1;

	samples = isl_mat_extend((*context_tab)->samples,
			(*context_tab)->n_sample, 1 + (*context_tab)->n_var);
	(*context_tab)->samples = samples;
	if (!samples)
		goto error;
	for (i = (*context_tab)->n_outside; i < samples->n_row; ++i) {
		isl_seq_inner_product(div->el + 1, samples->row[i],
			div->size - 1, &samples->row[i][samples->n_col - 1]);
		isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
			       samples->row[i][samples->n_col - 1], div->el[0]);
	}

	(*context_tab)->bset = isl_basic_set_extend_dim((*context_tab)->bset,
		isl_basic_set_get_dim((*context_tab)->bset), 1, 0, 2);
	k = isl_basic_set_alloc_div((*context_tab)->bset);
	if (k < 0)
		goto error;
	isl_seq_cpy((*context_tab)->bset->div[k], div->el, div->size);
	isl_tab_push((*context_tab), isl_tab_undo_bset_div);
	*context_tab = add_div_constraints(*context_tab, k);
	if (!*context_tab)
		goto error;

	if (isl_tab_extend_vars(tab, 1) < 0)
		goto error;
	r = isl_tab_allocate_var(tab);
	if (r < 0)
		goto error;
	if (!(*context_tab)->M)
		tab->var[r].is_nonneg = 1;
	tab->var[r].frozen = 1;
	tab->n_div++;

	return tab->n_div - 1;
error:
	isl_tab_free(*context_tab);
	*context_tab = NULL;
	return -1;
}

static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
{
	int i;
	unsigned total = isl_basic_set_total_dim(tab->bset);

	for (i = 0; i < tab->bset->n_div; ++i) {
		if (isl_int_ne(tab->bset->div[i][0], denom))
			continue;
		if (!isl_seq_eq(tab->bset->div[i] + 1, div, total))
			continue;
		return i;
	}
	return -1;
}

/* Return the index of a div that corresponds to "div".
 * We first check if we already have such a div and if not, we create one.
 */
static int get_div(struct isl_tab *tab, struct isl_tab **context_tab,
	struct isl_vec *div)
{
	int d;

	d = find_div(*context_tab, div->el + 1, div->el[0]);
	if (d != -1)
		return d;

	return add_div(tab, context_tab, div);
}

/* Add a parametric cut to cut away the non-integral sample value
 * of the give row.
 * Let a_i be the coefficients of the constant term and the parameters
 * and let b_i be the coefficients of the variables or constraints
 * in basis of the tableau.
 * Let q be the div q = floor(\sum_i {-a_i} y_i).
 *
 * The cut is expressed as
 *
 *	c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
 *
 * If q did not already exist in the context tableau, then it is added first.
 * If q is in a column of the main tableau then the "+ q" can be accomplished
 * by setting the corresponding entry to the denominator of the constraint.
 * If q happens to be in a row of the main tableau, then the corresponding
 * row needs to be added instead (taking care of the denominators).
 * Note that this is very unlikely, but perhaps not entirely impossible.
 *
 * The current value of the cut is known to be negative (or at least
 * non-positive), so row_sign is set accordingly.
 *
 * Return the row of the cut or -1.
 */
static int add_parametric_cut(struct isl_tab *tab, int row,
	struct isl_tab **context_tab)
{
	struct isl_vec *div;
	int d;
	int i;
	int r;
	isl_int *r_row;
	int col;
	unsigned off = 2 + tab->M;

	if (!*context_tab)
		goto error;

	if (isl_tab_extend_cons(*context_tab, 3) < 0)
		goto error;

	div = get_row_parameter_div(tab, row);
	if (!div)
		return -1;

	d = get_div(tab, context_tab, div);
	if (d < 0)
		goto error;

	if (isl_tab_extend_cons(tab, 1) < 0)
		return -1;
	r = isl_tab_allocate_con(tab);
	if (r < 0)
		return -1;

	r_row = tab->mat->row[tab->con[r].index];
	isl_int_set(r_row[0], tab->mat->row[row][0]);
	isl_int_neg(r_row[1], tab->mat->row[row][1]);
	isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
	isl_int_neg(r_row[1], r_row[1]);
	if (tab->M)
		isl_int_set_si(r_row[2], 0);
	for (i = 0; i < tab->n_param; ++i) {
		if (tab->var[i].is_row)
			continue;
		col = tab->var[i].index;
		isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
		isl_int_fdiv_r(r_row[off + col], r_row[off + col],
				tab->mat->row[row][0]);
		isl_int_neg(r_row[off + col], r_row[off + col]);
	}
	for (i = 0; i < tab->n_div; ++i) {
		if (tab->var[tab->n_var - tab->n_div + i].is_row)
			continue;
		col = tab->var[tab->n_var - tab->n_div + i].index;
		isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
		isl_int_fdiv_r(r_row[off + col], r_row[off + col],
				tab->mat->row[row][0]);
		isl_int_neg(r_row[off + col], r_row[off + col]);
	}
	for (i = 0; i < tab->n_col; ++i) {
		if (tab->col_var[i] >= 0 &&
		    (tab->col_var[i] < tab->n_param ||
		     tab->col_var[i] >= tab->n_var - tab->n_div))
			continue;
		isl_int_fdiv_r(r_row[off + i],
			tab->mat->row[row][off + i], tab->mat->row[row][0]);
	}
	if (tab->var[tab->n_var - tab->n_div + d].is_row) {
		isl_int gcd;
		int d_row = tab->var[tab->n_var - tab->n_div + d].index;
		isl_int_init(gcd);
		isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
		isl_int_divexact(r_row[0], r_row[0], gcd);
		isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
		isl_seq_combine(r_row + 1, gcd, r_row + 1,
				r_row[0], tab->mat->row[d_row] + 1,
				off - 1 + tab->n_col);
		isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
		isl_int_clear(gcd);
	} else {
		col = tab->var[tab->n_var - tab->n_div + d].index;
		isl_int_set(r_row[off + col], tab->mat->row[row][0]);
	}

	tab->con[r].is_nonneg = 1;
	isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
	if (tab->row_sign)
		tab->row_sign[tab->con[r].index] = isl_tab_row_neg;

	isl_vec_free(div);

	return tab->con[r].index;
error:
	isl_tab_free(*context_tab);
	*context_tab = NULL;
	return -1;
}

/* Construct a tableau for bmap that can be used for computing
 * the lexicographic minimum (or maximum) of bmap.
 * If not NULL, then dom is the domain where the minimum
 * should be computed.  In this case, we set up a parametric
 * tableau with row signs (initialized to "unknown").
 * If M is set, then the tableau will use a big parameter.
 * If max is set, then a maximum should be computed instead of a minimum.
 * This means that for each variable x, the tableau will contain the variable
 * x' = M - x, rather than x' = M + x.  This in turn means that the coefficient
 * of the variables in all constraints are negated prior to adding them
 * to the tableau.
 */
static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
	struct isl_basic_set *dom, unsigned M, int max)
{
	int i;
	struct isl_tab *tab;

	tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
			    isl_basic_map_total_dim(bmap), M);
	if (!tab)
		return NULL;

	tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
	if (dom) {
		tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
		tab->n_div = dom->n_div;
		tab->row_sign = isl_calloc_array(bmap->ctx,
					enum isl_tab_row_sign, tab->mat->n_row);
		if (!tab->row_sign)
			goto error;
	}
	if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
		return isl_tab_mark_empty(tab);

	for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
		tab->var[i].is_nonneg = 1;
		tab->var[i].frozen = 1;
	}
	for (i = 0; i < bmap->n_eq; ++i) {
		if (max)
			isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
				    bmap->eq[i] + 1 + tab->n_param,
				    tab->n_var - tab->n_param - tab->n_div);
		tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
		if (max)
			isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
				    bmap->eq[i] + 1 + tab->n_param,
				    tab->n_var - tab->n_param - tab->n_div);
		if (!tab || tab->empty)
			return tab;
	}
	for (i = 0; i < bmap->n_ineq; ++i) {
		if (max)
			isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
				    bmap->ineq[i] + 1 + tab->n_param,
				    tab->n_var - tab->n_param - tab->n_div);
		tab = add_lexmin_ineq(tab, bmap->ineq[i]);
		if (max)
			isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
				    bmap->ineq[i] + 1 + tab->n_param,
				    tab->n_var - tab->n_param - tab->n_div);
		if (!tab || tab->empty)
			return tab;
	}
	return tab;
error:
	isl_tab_free(tab);
	return NULL;
}

static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
{
	struct isl_tab *tab;

	bset = isl_basic_set_cow(bset);
	if (!bset)
		return NULL;
	tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
	if (!tab)
		goto error;
	tab->bset = bset;
	tab->n_sample = 0;
	tab->n_outside = 0;
	tab->samples = isl_mat_alloc(bset->ctx, 1, 1 + tab->n_var);
	if (!tab->samples)
		goto error;
	return tab;
error:
	isl_basic_set_free(bset);
	return NULL;
}

/* Construct an isl_sol_map structure for accumulating the solution.
 * If track_empty is set, then we also keep track of the parts
 * of the context where there is no solution.
 * If max is set, then we are solving a maximization, rather than
 * a minimization problem, which means that the variables in the
 * tableau have value "M - x" rather than "M + x".
 */
static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
	struct isl_basic_set *dom, int track_empty, int max)
{
	struct isl_sol_map *sol_map;
	struct isl_tab *context_tab;
	int f;

	sol_map = isl_calloc_type(bset->ctx, struct isl_sol_map);
	if (!sol_map)
		goto error;

	sol_map->max = max;
	sol_map->sol.add = &sol_map_add_wrap;
	sol_map->sol.free = &sol_map_free_wrap;
	sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
					    ISL_MAP_DISJOINT);
	if (!sol_map->map)
		goto error;

	context_tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
	context_tab = restore_lexmin(context_tab);
	sol_map->sol.context_tab = context_tab;
	f = context_is_feasible(&sol_map->sol);
	if (f < 0)
		goto error;

	if (track_empty) {
		sol_map->empty = isl_set_alloc_dim(isl_basic_set_get_dim(dom),
							1, ISL_SET_DISJOINT);
		if (!sol_map->empty)
			goto error;
	}

	isl_basic_set_free(dom);
	return sol_map;
error:
	isl_basic_set_free(dom);
	sol_map_free(sol_map);
	return NULL;
}

/* For each variable in the context tableau, check if the variable can
 * only attain non-negative values.  If so, mark the parameter as non-negative
 * in the main tableau.  This allows for a more direct identification of some
 * cases of violated constraints.
 */
static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
	struct isl_tab *context_tab)
{
	int i;
	struct isl_tab_undo *snap, *snap2;
	struct isl_vec *ineq = NULL;
	struct isl_tab_var *var;
	int n;

	if (context_tab->n_var == 0)
		return tab;

	ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
	if (!ineq)
		goto error;

	if (isl_tab_extend_cons(context_tab, 1) < 0)
		goto error;

	snap = isl_tab_snap(context_tab);
	isl_tab_push_basis(context_tab);

	snap2 = isl_tab_snap(context_tab);

	n = 0;
	isl_seq_clr(ineq->el, ineq->size);
	for (i = 0; i < context_tab->n_var; ++i) {
		isl_int_set_si(ineq->el[1 + i], 1);
		context_tab = isl_tab_add_ineq(context_tab, ineq->el);
		var = &context_tab->con[context_tab->n_con - 1];
		if (!context_tab->empty &&
		    !isl_tab_min_at_most_neg_one(context_tab, var)) {
			int j = i;
			if (i >= tab->n_param)
				j = i - tab->n_param + tab->n_var - tab->n_div;
			tab->var[j].is_nonneg = 1;
			n++;
		}
		isl_int_set_si(ineq->el[1 + i], 0);
		if (isl_tab_rollback(context_tab, snap2) < 0)
			goto error;
	}

	if (isl_tab_rollback(context_tab, snap) < 0)
		goto error;

	if (n == context_tab->n_var) {
		context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
		context_tab->M = 0;
	}

	isl_vec_free(ineq);
	return tab;
error:
	isl_vec_free(ineq);
	isl_tab_free(tab);
	return NULL;
}

/* Check whether all coefficients of (non-parameter) variables
 * are non-positive, meaning that no pivots can be performed on the row.
 */
static int is_critical(struct isl_tab *tab, int row)
{
	int j;
	unsigned off = 2 + tab->M;

	for (j = tab->n_dead; j < tab->n_col; ++j) {
		if (tab->col_var[j] >= 0 &&
		    (tab->col_var[j] < tab->n_param  ||
		    tab->col_var[j] >= tab->n_var - tab->n_div))
			continue;

		if (isl_int_is_pos(tab->mat->row[row][off + j]))
			return 0;
	}

	return 1;
}

/* Check whether the inequality represented by vec is strict over the integers,
 * i.e., there are no integer values satisfying the constraint with
 * equality.  This happens if the gcd of the coefficients is not a divisor
 * of the constant term.  If so, scale the constraint down by the gcd
 * of the coefficients.
 */
static int is_strict(struct isl_vec *vec)
{
	isl_int gcd;
	int strict = 0;

	isl_int_init(gcd);
	isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
	if (!isl_int_is_one(gcd)) {
		strict = !isl_int_is_divisible_by(vec->el[0], gcd);
		isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
		isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
	}
	isl_int_clear(gcd);

	return strict;
}

/* Determine the sign of the given row of the main tableau.
 * The result is one of
 *	isl_tab_row_pos: always non-negative; no pivot needed
 *	isl_tab_row_neg: always non-positive; pivot
 *	isl_tab_row_any: can be both positive and negative; split
 *
 * We first handle some simple cases
 *	- the row sign may be known already
 *	- the row may be obviously non-negative
 *	- the parametric constant may be equal to that of another row
 *	  for which we know the sign.  This sign will be either "pos" or
 *	  "any".  If it had been "neg" then we would have pivoted before.
 *
 * If none of these cases hold, we check the value of the row for each
 * of the currently active samples.  Based on the signs of these values
 * we make an initial determination of the sign of the row.
 *
 *	all zero			->	unk(nown)
 *	all non-negative		->	pos
 *	all non-positive		->	neg
 *	both negative and positive	->	all
 *
 * If we end up with "all", we are done.
 * Otherwise, we perform a check for positive and/or negative
 * values as follows.
 *
 *	samples	       neg	       unk	       pos
 *	<0 ?			    Y        N	    Y        N
 *					    pos    any      pos
 *	>0 ?	     Y      N	 Y     N
 *		    any    neg  any   neg
 *
 * There is no special sign for "zero", because we can usually treat zero
 * as either non-negative or non-positive, whatever works out best.
 * However, if the row is "critical", meaning that pivoting is impossible
 * then we don't want to limp zero with the non-positive case, because
 * then we we would lose the solution for those values of the parameters
 * where the value of the row is zero.  Instead, we treat 0 as non-negative
 * ensuring a split if the row can attain both zero and negative values.
 * The same happens when the original constraint was one that could not
 * be satisfied with equality by any integer values of the parameters.
 * In this case, we normalize the constraint, but then a value of zero
 * for the normalized constraint is actually a positive value for the
 * original constraint, so again we need to treat zero as non-negative.
 * In both these cases, we have the following decision tree instead:
 *
 *	all non-negative		->	pos
 *	all negative			->	neg
 *	both negative and non-negative	->	all
 *
 *	samples	       neg	          	       pos
 *	<0 ?			             	    Y        N
 *					           any      pos
 *	>=0 ?	     Y      N
 *		    any    neg
 */
static int row_sign(struct isl_tab *tab, struct isl_sol *sol, int row)
{
	int i;
	struct isl_tab_undo *snap = NULL;
	struct isl_vec *ineq = NULL;
	int res = isl_tab_row_unknown;
	int r;
	int context_row;
	int critical;
	int strict;
	int sgn;
	int row2;
	isl_int tmp;
	struct isl_tab *context_tab = sol->context_tab;

	if (tab->row_sign[row] != isl_tab_row_unknown)
		return tab->row_sign[row];
	if (is_obviously_nonneg(tab, row))
		return isl_tab_row_pos;
	for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
		if (tab->row_sign[row2] == isl_tab_row_unknown)
			continue;
		if (identical_parameter_line(tab, row, row2))
			return tab->row_sign[row2];
	}

	critical = is_critical(tab, row);

	isl_assert(tab->mat->ctx, context_tab->samples, goto error);
	isl_assert(tab->mat->ctx, context_tab->samples->n_col == 1 + context_tab->n_var, goto error);

	ineq = get_row_parameter_ineq(tab, row);
	if (!ineq)
		goto error;

	strict = is_strict(ineq);

	isl_int_init(tmp);
	for (i = context_tab->n_outside; i < context_tab->n_sample; ++i) {
		isl_seq_inner_product(context_tab->samples->row[i], ineq->el,
					ineq->size, &tmp);
		sgn = isl_int_sgn(tmp);
		if (sgn > 0 || (sgn == 0 && (critical || strict))) {
			if (res == isl_tab_row_unknown)
				res = isl_tab_row_pos;
			if (res == isl_tab_row_neg)
				res = isl_tab_row_any;
		}
		if (sgn < 0) {
			if (res == isl_tab_row_unknown)
				res = isl_tab_row_neg;
			if (res == isl_tab_row_pos)
				res = isl_tab_row_any;
		}
		if (res == isl_tab_row_any)
			break;
	}
	isl_int_clear(tmp);

	if (res != isl_tab_row_any) {
		if (isl_tab_extend_cons(context_tab, 1) < 0)
			goto error;

		snap = isl_tab_snap(context_tab);
		isl_tab_push_basis(context_tab);
	}

	if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
		/* test for negative values */
		int feasible;
		isl_seq_neg(ineq->el, ineq->el, ineq->size);
		isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);

		isl_tab_push_basis(context_tab);
		sol->context_tab = add_lexmin_ineq(sol->context_tab, ineq->el);
		feasible = context_is_feasible(sol);
		if (feasible < 0)
			goto error;
		context_tab = sol->context_tab;
		if (!feasible)
			res = isl_tab_row_pos;
		else
			res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
							   : isl_tab_row_any;
		if (isl_tab_rollback(context_tab, snap) < 0)
			goto error;

		if (res == isl_tab_row_neg) {
			isl_seq_neg(ineq->el, ineq->el, ineq->size);
			isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
		}
	}

	if (res == isl_tab_row_neg) {
		/* test for positive values */
		int feasible;
		if (!critical && !strict)
			isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);

		isl_tab_push_basis(context_tab);
		sol->context_tab = add_lexmin_ineq(sol->context_tab, ineq->el);
		feasible = context_is_feasible(sol);
		if (feasible < 0)
			goto error;
		context_tab = sol->context_tab;
		if (feasible)
			res = isl_tab_row_any;
		if (isl_tab_rollback(context_tab, snap) < 0)
			goto error;
	}

	isl_vec_free(ineq);
	return res;
error:
	isl_vec_free(ineq);
	return 0;
}

static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab);

/* Find solutions for values of the parameters that satisfy the given
 * inequality.
 *
 * We currently take a snapshot of the context tableau that is reset
 * when we return from this function, while we make a copy of the main
 * tableau, leaving the original main tableau untouched.
 * These are fairly arbitrary choices.  Making a copy also of the context
 * tableau would obviate the need to undo any changes made to it later,
 * while taking a snapshot of the main tableau could reduce memory usage.
 * If we were to switch to taking a snapshot of the main tableau,
 * we would have to keep in mind that we need to save the row signs
 * and that we need to do this before saving the current basis
 * such that the basis has been restore before we restore the row signs.
 */
static struct isl_sol *find_in_pos(struct isl_sol *sol,
	struct isl_tab *tab, isl_int *ineq)
{
	struct isl_tab_undo *snap;

	snap = isl_tab_snap(sol->context_tab);
	isl_tab_push_basis(sol->context_tab);
	if (isl_tab_extend_cons(sol->context_tab, 1) < 0)
		goto error;

	tab = isl_tab_dup(tab);
	if (!tab)
		goto error;

	sol->context_tab = add_lexmin_ineq(sol->context_tab, ineq);
	sol->context_tab = check_samples(sol->context_tab, ineq, 0);

	sol = find_solutions(sol, tab);

	isl_tab_rollback(sol->context_tab, snap);
	return sol;
error:
	isl_tab_rollback(sol->context_tab, snap);
	sol_free(sol);
	return NULL;
}

/* Record the absence of solutions for those values of the parameters
 * that do not satisfy the given inequality with equality.
 */
static struct isl_sol *no_sol_in_strict(struct isl_sol *sol,
	struct isl_tab *tab, struct isl_vec *ineq)
{
	int empty;
	int f;
	struct isl_tab_undo *snap;
	snap = isl_tab_snap(sol->context_tab);
	isl_tab_push_basis(sol->context_tab);
	if (isl_tab_extend_cons(sol->context_tab, 1) < 0)
		goto error;

	isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);

	sol->context_tab = add_lexmin_ineq(sol->context_tab, ineq->el);
	f = context_valid_sample_or_feasible(sol, ineq->el, 0);
	if (f < 0)
		goto error;

	empty = tab->empty;
	tab->empty = 1;
	sol = sol->add(sol, tab);
	tab->empty = empty;

	isl_int_add_ui(ineq->el[0], ineq->el[0], 1);

	if (isl_tab_rollback(sol->context_tab, snap) < 0)
		goto error;
	return sol;
error:
	sol_free(sol);
	return NULL;
}

/* Given a main tableau where more than one row requires a split,
 * determine and return the "best" row to split on.
 *
 * Given two rows in the main tableau, if the inequality corresponding
 * to the first row is redundant with respect to that of the second row
 * in the current tableau, then it is better to split on the second row,
 * since in the positive part, both row will be positive.
 * (In the negative part a pivot will have to be performed and just about
 * anything can happen to the sign of the other row.)
 *
 * As a simple heuristic, we therefore select the row that makes the most
 * of the other rows redundant.
 *
 * Perhaps it would also be useful to look at the number of constraints
 * that conflict with any given constraint.
 */
static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
{
	struct isl_tab_undo *snap, *snap2;
	int split;
	int row;
	int best = -1;
	int best_r;

	if (isl_tab_extend_cons(context_tab, 2) < 0)
		return -1;

	snap = isl_tab_snap(context_tab);
	isl_tab_push_basis(context_tab);
	snap2 = isl_tab_snap(context_tab);

	for (split = tab->n_redundant; split < tab->n_row; ++split) {
		struct isl_tab_undo *snap3;
		struct isl_vec *ineq = NULL;
		int r = 0;

		if (!isl_tab_var_from_row(tab, split)->is_nonneg)
			continue;
		if (tab->row_sign[split] != isl_tab_row_any)
			continue;

		ineq = get_row_parameter_ineq(tab, split);
		if (!ineq)
			return -1;
		context_tab = isl_tab_add_ineq(context_tab, ineq->el);
		isl_vec_free(ineq);

		snap3 = isl_tab_snap(context_tab);

		for (row = tab->n_redundant; row < tab->n_row; ++row) {
			struct isl_tab_var *var;

			if (row == split)
				continue;
			if (!isl_tab_var_from_row(tab, row)->is_nonneg)
				continue;
			if (tab->row_sign[row] != isl_tab_row_any)
				continue;

			ineq = get_row_parameter_ineq(tab, row);
			if (!ineq)
				return -1;
			context_tab = isl_tab_add_ineq(context_tab, ineq->el);
			isl_vec_free(ineq);
			var = &context_tab->con[context_tab->n_con - 1];
			if (!context_tab->empty &&
			    !isl_tab_min_at_most_neg_one(context_tab, var))
				r++;
			if (isl_tab_rollback(context_tab, snap3) < 0)
				return -1;
		}
		if (best == -1 || r > best_r) {
			best = split;
			best_r = r;
		}
		if (isl_tab_rollback(context_tab, snap2) < 0)
			return -1;
	}

	if (isl_tab_rollback(context_tab, snap) < 0)
		return -1;

	return best;
}

/* Compute the lexicographic minimum of the set represented by the main
 * tableau "tab" within the context "sol->context_tab".
 * On entry the sample value of the main tableau is lexicographically
 * less than or equal to this lexicographic minimum.
 * Pivots are performed until a feasible point is found, which is then
 * necessarily equal to the minimum, or until the tableau is found to
 * be infeasible.  Some pivots may need to be performed for only some
 * feasible values of the context tableau.  If so, the context tableau
 * is split into a part where the pivot is needed and a part where it is not.
 *
 * Whenever we enter the main loop, the main tableau is such that no
 * "obvious" pivots need to be performed on it, where "obvious" means
 * that the given row can be seen to be negative without looking at
 * the context tableau.  In particular, for non-parametric problems,
 * no pivots need to be performed on the main tableau.
 * The caller of find_solutions is responsible for making this property
 * hold prior to the first iteration of the loop, while restore_lexmin
 * is called before every other iteration.
 *
 * Inside the main loop, we first examine the signs of the rows of
 * the main tableau within the context of the context tableau.
 * If we find a row that is always non-positive for all values of
 * the parameters satisfying the context tableau and negative for at
 * least one value of the parameters, we perform the appropriate pivot
 * and start over.  An exception is the case where no pivot can be
 * performed on the row.  In this case, we require that the sign of
 * the row is negative for all values of the parameters (rather than just
 * non-positive).  This special case is handled inside row_sign, which
 * will say that the row can have any sign if it determines that it can
 * attain both negative and zero values.
 *
 * If we can't find a row that always requires a pivot, but we can find
 * one or more rows that require a pivot for some values of the parameters
 * (i.e., the row can attain both positive and negative signs), then we split
 * the context tableau into two parts, one where we force the sign to be
 * non-negative and one where we force is to be negative.
 * The non-negative part is handled by a recursive call (through find_in_pos).
 * Upon returning from this call, we continue with the negative part and
 * perform the required pivot.
 *
 * If no such rows can be found, all rows are non-negative and we have
 * found a (rational) feasible point.  If we only wanted a rational point
 * then we are done.
 * Otherwise, we check if all values of the sample point of the tableau
 * are integral for the variables.  If so, we have found the minimal
 * integral point and we are done.
 * If the sample point is not integral, then we need to make a distinction
 * based on whether the constant term is non-integral or the coefficients
 * of the parameters.  Furthermore, in order to decide how to handle
 * the non-integrality, we also need to know whether the coefficients
 * of the other columns in the tableau are integral.  This leads
 * to the following table.  The first two rows do not correspond
 * to a non-integral sample point and are only mentioned for completeness.
 *
 *	constant	parameters	other
 *
 *	int		int		int	|
 *	int		int		rat	| -> no problem
 *
 *	rat		int		int	  -> fail
 *
 *	rat		int		rat	  -> cut
 *
 *	int		rat		rat	|
 *	rat		rat		rat	| -> parametric cut
 *
 *	int		rat		int	|
 *	rat		rat		int	| -> split context
 *
 * If the parametric constant is completely integral, then there is nothing
 * to be done.  If the constant term is non-integral, but all the other
 * coefficient are integral, then there is nothing that can be done
 * and the tableau has no integral solution.
 * If, on the other hand, one or more of the other columns have rational
 * coeffcients, but the parameter coefficients are all integral, then
 * we can perform a regular (non-parametric) cut.
 * Finally, if there is any parameter coefficient that is non-integral,
 * then we need to involve the context tableau.  There are two cases here.
 * If at least one other column has a rational coefficient, then we
 * can perform a parametric cut in the main tableau by adding a new
 * integer division in the context tableau.
 * If all other columns have integral coefficients, then we need to
 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
 * is always integral.  We do this by introducing an integer division
 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
 * Since q is expressed in the tableau as
 *	c + \sum a_i y_i - m q >= 0
 *	-c - \sum a_i y_i + m q + m - 1 >= 0
 * it is sufficient to add the inequality
 *	-c - \sum a_i y_i + m q >= 0
 * In the part of the context where this inequality does not hold, the
 * main tableau is marked as being empty.
 */
static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab)
{
	struct isl_tab **context_tab;

	if (!tab || !sol)
		goto error;

	context_tab = &sol->context_tab;

	if (tab->empty)
		goto done;
	if ((*context_tab)->empty)
		goto done;

	for (; tab && !tab->empty; tab = restore_lexmin(tab)) {
		int flags;
		int row;
		int sgn;
		int split = -1;
		int n_split = 0;

		for (row = tab->n_redundant; row < tab->n_row; ++row) {
			if (!isl_tab_var_from_row(tab, row)->is_nonneg)
				continue;
			sgn = row_sign(tab, sol, row);
			if (!sgn)
				goto error;
			tab->row_sign[row] = sgn;
			if (sgn == isl_tab_row_any)
				n_split++;
			if (sgn == isl_tab_row_any && split == -1)
				split = row;
			if (sgn == isl_tab_row_neg)
				break;
		}
		if (row < tab->n_row)
			continue;
		if (split != -1) {
			struct isl_vec *ineq;
			if (n_split != 1)
				split = best_split(tab, *context_tab);
			if (split < 0)
				goto error;
			ineq = get_row_parameter_ineq(tab, split);
			if (!ineq)
				goto error;
			is_strict(ineq);
			for (row = tab->n_redundant; row < tab->n_row; ++row) {
				if (!isl_tab_var_from_row(tab, row)->is_nonneg)
					continue;
				if (tab->row_sign[row] == isl_tab_row_any)
					tab->row_sign[row] = isl_tab_row_unknown;
			}
			tab->row_sign[split] = isl_tab_row_pos;
			sol = find_in_pos(sol, tab, ineq->el);
			tab->row_sign[split] = isl_tab_row_neg;
			row = split;
			isl_seq_neg(ineq->el, ineq->el, ineq->size);
			isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
			*context_tab = add_lexmin_ineq(*context_tab, ineq->el);
			*context_tab = check_samples(*context_tab, ineq->el, 0);
			isl_vec_free(ineq);
			if (!sol)
				goto error;
			continue;
		}
		if (tab->rational)
			break;
		row = first_non_integer(tab, &flags);
		if (row < 0)
			break;
		if (ISL_FL_ISSET(flags, I_PAR)) {
			if (ISL_FL_ISSET(flags, I_VAR)) {
				tab = isl_tab_mark_empty(tab);
				break;
			}
			row = add_cut(tab, row);
		} else if (ISL_FL_ISSET(flags, I_VAR)) {
			struct isl_vec *div;
			struct isl_vec *ineq;
			int d;
			if (isl_tab_extend_cons(*context_tab, 3) < 0)
				goto error;
			div = get_row_split_div(tab, row);
			if (!div)
				goto error;
			d = get_div(tab, context_tab, div);
			isl_vec_free(div);
			if (d < 0)
				goto error;
			ineq = ineq_for_div((*context_tab)->bset, d);
			sol = no_sol_in_strict(sol, tab, ineq);
			isl_seq_neg(ineq->el, ineq->el, ineq->size);
			*context_tab = add_lexmin_ineq(*context_tab, ineq->el);
			*context_tab = check_samples(*context_tab, ineq->el, 0);
			isl_vec_free(ineq);
			if (!sol)
				goto error;
			tab = set_row_cst_to_div(tab, row, d);
		} else
			row = add_parametric_cut(tab, row, context_tab);
		if (row < 0)
			goto error;
	}
done:
	sol = sol->add(sol, tab);
	isl_tab_free(tab);
	return sol;
error:
	isl_tab_free(tab);
	sol_free(sol);
	return NULL;
}

/* Compute the lexicographic minimum of the set represented by the main
 * tableau "tab" within the context "sol->context_tab".
 *
 * As a preprocessing step, we first transfer all the purely parametric
 * equalities from the main tableau to the context tableau, i.e.,
 * parameters that have been pivoted to a row.
 * These equalities are ignored by the main algorithm, because the
 * corresponding rows may not be marked as being non-negative.
 * In parts of the context where the added equality does not hold,
 * the main tableau is marked as being empty.
 */
static struct isl_sol *find_solutions_main(struct isl_sol *sol,
	struct isl_tab *tab)
{
	int row;

	for (row = tab->n_redundant; row < tab->n_row; ++row) {
		int p;
		struct isl_vec *eq;

		if (tab->row_var[row] < 0)
			continue;
		if (tab->row_var[row] >= tab->n_param &&
		    tab->row_var[row] < tab->n_var - tab->n_div)
			continue;
		if (tab->row_var[row] < tab->n_param)
			p = tab->row_var[row];
		else
			p = tab->row_var[row]
				+ tab->n_param - (tab->n_var - tab->n_div);

		if (isl_tab_extend_cons(sol->context_tab, 2) < 0)
			goto error;

		eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
		get_row_parameter_line(tab, row, eq->el);
		isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
		eq = isl_vec_normalize(eq);

		sol = no_sol_in_strict(sol, tab, eq);

		isl_seq_neg(eq->el, eq->el, eq->size);
		sol = no_sol_in_strict(sol, tab, eq);
		isl_seq_neg(eq->el, eq->el, eq->size);

		sol->context_tab = add_lexmin_eq(sol->context_tab, eq->el);
		context_valid_sample_or_feasible(sol, eq->el, 1);
		sol->context_tab = check_samples(sol->context_tab, eq->el, 1);

		isl_vec_free(eq);

		isl_tab_mark_redundant(tab, row);

		if (!sol->context_tab)
			goto error;
		if (sol->context_tab->empty)
			break;

		row = tab->n_redundant - 1;
	}

	return find_solutions(sol, tab);
error:
	isl_tab_free(tab);
	sol_free(sol);
	return NULL;
}

static struct isl_sol_map *sol_map_find_solutions(struct isl_sol_map *sol_map,
	struct isl_tab *tab)
{
	return (struct isl_sol_map *)find_solutions_main(&sol_map->sol, tab);
}

/* Check if integer division "div" of "dom" also occurs in "bmap".
 * If so, return its position within the divs.
 * If not, return -1.
 */
static int find_context_div(struct isl_basic_map *bmap,
	struct isl_basic_set *dom, unsigned div)
{
	int i;
	unsigned b_dim = isl_dim_total(bmap->dim);
	unsigned d_dim = isl_dim_total(dom->dim);

	if (isl_int_is_zero(dom->div[div][0]))
		return -1;
	if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
		return -1;

	for (i = 0; i < bmap->n_div; ++i) {
		if (isl_int_is_zero(bmap->div[i][0]))
			continue;
		if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
					   (b_dim - d_dim) + bmap->n_div) != -1)
			continue;
		if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
			return i;
	}
	return -1;
}

/* The correspondence between the variables in the main tableau,
 * the context tableau, and the input map and domain is as follows.
 * The first n_param and the last n_div variables of the main tableau
 * form the variables of the context tableau.
 * In the basic map, these n_param variables correspond to the
 * parameters and the input dimensions.  In the domain, they correspond
 * to the parameters and the set dimensions.
 * The n_div variables correspond to the integer divisions in the domain.
 * To ensure that everything lines up, we may need to copy some of the
 * integer divisions of the domain to the map.  These have to be placed
 * in the same order as those in the context and they have to be placed
 * after any other integer divisions that the map may have.
 * This function performs the required reordering.
 */
static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
	struct isl_basic_set *dom)
{
	int i;
	int common = 0;
	int other;

	for (i = 0; i < dom->n_div; ++i)
		if (find_context_div(bmap, dom, i) != -1)
			common++;
	other = bmap->n_div - common;
	if (dom->n_div - common > 0) {
		bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
				dom->n_div - common, 0, 0);
		if (!bmap)
			return NULL;
	}
	for (i = 0; i < dom->n_div; ++i) {
		int pos = find_context_div(bmap, dom, i);
		if (pos < 0) {
			pos = isl_basic_map_alloc_div(bmap);
			if (pos < 0)
				goto error;
			isl_int_set_si(bmap->div[pos][0], 0);
		}
		if (pos != other + i)
			isl_basic_map_swap_div(bmap, pos, other + i);
	}
	return bmap;
error:
	isl_basic_map_free(bmap);
	return NULL;
}

/* Compute the lexicographic minimum (or maximum if "max" is set)
 * of "bmap" over the domain "dom" and return the result as a map.
 * If "empty" is not NULL, then *empty is assigned a set that
 * contains those parts of the domain where there is no solution.
 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
 * then we compute the rational optimum.  Otherwise, we compute
 * the integral optimum.
 *
 * We perform some preprocessing.  As the PILP solver does not
 * handle implicit equalities very well, we first make sure all
 * the equalities are explicitly available.
 * We also make sure the divs in the domain are properly order,
 * because they will be added one by one in the given order
 * during the construction of the solution map.
 */
struct isl_map *isl_tab_basic_map_partial_lexopt(
		struct isl_basic_map *bmap, struct isl_basic_set *dom,
		struct isl_set **empty, int max)
{
	struct isl_tab *tab;
	struct isl_map *result = NULL;
	struct isl_sol_map *sol_map = NULL;

	if (empty)
		*empty = NULL;
	if (!bmap || !dom)
		goto error;

	isl_assert(bmap->ctx,
	    isl_basic_map_compatible_domain(bmap, dom), goto error);

	bmap = isl_basic_map_detect_equalities(bmap);

	if (dom->n_div) {
		dom = isl_basic_set_order_divs(dom);
		bmap = align_context_divs(bmap, dom);
	}
	sol_map = sol_map_init(bmap, dom, !!empty, max);
	if (!sol_map)
		goto error;

	if (isl_basic_set_fast_is_empty(sol_map->sol.context_tab->bset))
		/* nothing */;
	else if (isl_basic_map_fast_is_empty(bmap))
		sol_map = add_empty(sol_map);
	else {
		tab = tab_for_lexmin(bmap,
					sol_map->sol.context_tab->bset, 1, max);
		tab = tab_detect_nonnegative_parameters(tab,
						sol_map->sol.context_tab);
		sol_map = sol_map_find_solutions(sol_map, tab);
		if (!sol_map)
			goto error;
	}

	result = isl_map_copy(sol_map->map);
	if (empty)
		*empty = isl_set_copy(sol_map->empty);
	sol_map_free(sol_map);
	isl_basic_map_free(bmap);
	return result;
error:
	sol_map_free(sol_map);
	isl_basic_map_free(bmap);
	return NULL;
}