4 #define compilingParser
5 #include <basic/Dynamic_Array.h>
6 #include <code_gen/code_gen.h>
7 #include <code_gen/spmd.h>
8 #include <omega/library_version.h>
10 #include <omega_calc/yylex.h>
11 #include <omega/hull.h>
12 #include <omega/calc_debug.h>
13 #include <basic/Exit.h>
14 #include <omega/closure.h>
15 #include <omega/reach.h>
16 #include <code_gen/mmap-codegen.h>
17 #include <code_gen/mmap-util.h>
20 #include <sys/resource.h>
22 #include <barvinok/bernstein.h>
29 #define CALC_VERSION_STRING "Omega Counting Calculator v1.2"
31 #define DEBUG_FILE_NAME "./oc.out"
34 Map
<Const_String
,Relation
*> relationMap
((Relation
*)0);
35 static int redundant_conj_level
;
37 #if defined BRAIN_DAMAGED_FREE
39 void *realloc
(void *p
, size_t s
);
42 #if !defined(OMIT_GETRUSAGE)
43 void start_clock
( void );
44 int clock_diff
( void );
45 bool anyTimingDone
= false
;
50 Argument_Tuple currentTuple
= Input_Tuple
;
51 char *currentVar
= NULL
;
53 Relation LexForward
(int n
);
56 reachable_information
*reachable_info
;
58 Relation
*build_relation
(tupleDescriptor
*tuple
, AST
* ast
)
60 Relation
* r
= new Relation
(tuple
->size
);
62 F_And
*f
= r
->add_and
();
64 for
(i
=1;i
<=tuple
->size
;i
++) {
65 tuple
->vars
[i
]->vid
= r
->set_var
(i
);
66 if
(!tuple
->vars
[i
]->anonymous
)
67 r
->name_set_var
(i
,tuple
->vars
[i
]->stripped_name
);
69 foreach
(e
,Exp
*,tuple
->eq_constraints
, install_eq
(f
,e
,0));
70 foreach
(e
,Exp
*,tuple
->geq_constraints
, install_geq
(f
,e
,0));
71 foreach
(c
,strideConstraint
*,tuple
->stride_constraints
, install_stride
(f
,c
));
72 if
(ast
) ast
->install
(f
);
78 Map
<Variable_Ref
*, GiNaC
::ex
> *variableMap
;
81 static void evalue_print_and_free
(Relation
*r
, evalue
*EP
)
86 const Variable_ID_Tuple
* globals
= r
->global_decls
();
87 const char **param_names
= new
const char *[globals
->size
()];
89 for
(int i
= 0; i
< globals
->size
(); ++i
)
90 param_names
[i
] = (*globals
)[i
+1]->char_name
();
91 print_evalue
(stdout
, EP
, param_names
);
93 delete
[] param_names
;
100 %token
<VAR_NAME
> VAR
101 %token
<INT_VALUE
> INT
102 %token
<STRING_VALUE
> STRING
103 %token OPEN_BRACE CLOSE_BRACE
109 %token OMEGA_DOMAIN RANGE
110 %token DIFFERENCE DIFFERENCE_TO_RELATION
111 %token GIST GIVEN HULL WITHIN MAXIMIZE MINIMIZE
112 %token AFFINE_HULL VENN CONVEX_COMBINATION POSITIVE_COMBINATION CONVEX_HULL CONIC_HULL LINEAR_HULL PAIRWISE_CHECK CONVEX_CHECK
113 %token MAXIMIZE_RANGE MINIMIZE_RANGE
114 %token MAXIMIZE_DOMAIN MINIMIZE_DOMAIN
117 %token COMPOSE JOIN INVERSE COMPLEMENT IN CARRIED_BY TIME TIMECLOSURE
118 %token UNION INTERSECTION
119 %token VERTICAL_BAR SUCH_THAT
120 %token SUBSET ITERATIONS SPMD CODEGEN DECOUPLED_FARKAS FARKAS
121 %token TCODEGEN TRANS_IS SET_MMAP UNROLL_IS PEEL_IS
122 %token MAKE_UPPER_BOUND MAKE_LOWER_BOUND
123 %token
<REL_OPERATOR
> REL_OP
124 %token RESTRICT_DOMAIN RESTRICT_RANGE
125 %token SUPERSETOF SUBSETOF SAMPLE SYM_SAMPLE
126 %token PROJECT_AWAY_SYMBOLS PROJECT_ON_SYMBOLS REACHABLE_FROM REACHABLE_OF
128 %token CARD USING BARVINOK PARKER RANKING COUNT_LEXSMALLER
133 %token PARSE_EXPRESSION PARSE_FORMULA PARSE_RELATION
135 %nonassoc ASSERT_UNSAT
136 %left UNION OMEGA_P1
'+' '-'
137 %nonassoc SUPERSETOF SUBSETOF
138 %left OMEGA_P2 RESTRICT_DOMAIN RESTRICT_RANGE
139 %left INTERSECTION OMEGA_P3
'*' '@'
144 %left COMPOSE JOIN CARRIED_BY
145 %right NOT APPROX OMEGA_DOMAIN RANGE HULL PROJECT_AWAY_SYMBOLS PROJECT_ON_SYMBOLS DIFFERENCE DIFFERENCE_TO_RELATION INVERSE COMPLEMENT FARKAS SAMPLE SYM_SAMPLE MAKE_UPPER_BOUND MAKE_LOWER_BOUND OMEGA_P7
150 %right CARD USING RANKING COUNT_LEXSMALLER
155 %type
<INT_VALUE
> effort
156 %type
<EXP
> exp simpleExp
157 %type
<EXP_LIST
> expList
158 %type
<VAR_LIST
> varList
159 %type
<ARGUMENT_TUPLE
> argumentList
160 %type
<ASTP
> formula optionalFormula
161 %type
<ASTCP
> constraintChain
162 %type
<TUPLE_DESCRIPTOR
> tupleDeclaration
163 %type
<DECLARATION_SITE
> varDecl varDeclOptBrackets
164 %type
<RELATION
> relation builtRelation context
165 %type
<RELATION
> reachable_of
166 %type
<REL_TUPLE_PAIR
> relPairList
167 %type
<REL_TUPLE_TRIPLE
> relTripList
168 %type
<RELATION_ARRAY_1D
> reachable
169 %type
<STM_INFO_TUPLE
> statementInfoList statementInfoResult
170 %type
<STM_INFO
> statementInfo
171 %type
<STM_INFO
> reads
173 %type
<READ
> partials
174 %type
<PREAD
> partial
175 %type
<MMAP
> partialwrites
176 %type
<PMMAP
> partialwrite
177 %type
<POLYFUNC
> polyfunc
178 %type
<POLYNOMIAL
> polynomial
179 %type
<INT_VALUE
> counting_method
189 Argument_Tuple ARGUMENT_TUPLE
;
190 AST_constraints
*ASTCP
;
191 Declaration_Site
* DECLARATION_SITE
;
193 tupleDescriptor
* TUPLE_DESCRIPTOR
;
194 RelTuplePair
* REL_TUPLE_PAIR
;
195 RelTupleTriple
* REL_TUPLE_TRIPLE
;
196 Dynamic_Array2
<Relation
> * RELATION_ARRAY_2D
;
197 Dynamic_Array1
<Relation
> * RELATION_ARRAY_1D
;
198 Tuple
<String
> *STRING_TUPLE
;
199 String
*STRING_VALUE
;
200 Tuple
<stm_info
> *STM_INFO_TUPLE
;
207 GiNaC
::ex
*POLYNOMIAL
;
218 inputSequence
: inputItem
219 | inputSequence
{ assert
( current_Declaration_Site
== globalDecls
);}
226 /* Kill all the local declarations -- ejr */
229 Declaration_Site
*ds1
, *ds2
;
230 for
(ds1
= current_Declaration_Site
; ds1
!= globalDecls
;) {
235 current_Declaration_Site
= globalDecls
;
236 yyerror("skipping to statement end");
238 | SYMBOLIC globVarList
';'
241 | VAR
{ currentVar
= $1; } IS_ASSIGNED relation
';'
244 $4->simplify
(min
(2,redundant_conj_level
),4);
245 Relation
*r
= relationMap
((Const_String
)$1);
247 relationMap
[(Const_String
)$1] = $4;
254 $1->simplify
(redundant_conj_level
,4);
255 $1->print_with_subs
(stdout
);
259 | TIME relation
';' {
261 #if defined(OMIT_GETRUSAGE)
262 printf
("'time' requires getrusage, but the omega calclator was compiled with OMIT_GETRUSAGE set!\n");
270 bool SKIP_FULL_CHECK
= getenv
("OC_TIMING_SKIP_FULL_CHECK");
271 ($2)->and_with_GEQ
();
273 for
(t
=1;t
<=100;t
++) {
277 int copyTime
= clock_diff
();
279 for
(t
=1;t
<=100;t
++) {
284 int simplifyTime
= clock_diff
() -copyTime
;
286 if
(!SKIP_FULL_CHECK
)
289 for
(t
=1;t
<=100;t
++) {
295 int excessiveTime
= clock_diff
() - copyTime
;
296 printf
("Times (in microseconds): \n");
297 printf
("%5d us to copy original set of constraints\n",copyTime
/100);
298 printf
("%5d us to do the default amount of simplification, obtaining: \n\t",
300 R.print_with_subs
(stdout
);
302 if
(!SKIP_FULL_CHECK
)
304 printf
("%5d us to do the maximum (i.e., excessive) amount of simplification, obtaining: \n\t",
306 R2.print_with_subs
(stdout
);
309 if
(!anyTimingDone
) {
318 printf
("WARNING: The Omega calculator was compiled with options that force\n");
319 printf
("it to perform additional consistency and error checks\n");
320 printf
("that may slow it down substantially\n");
323 printf
("NOTE: These times relect the time of the current _implementation_\n");
324 printf
("of our algorithms. Performance bugs do exist. If you intend to publish or \n");
325 printf
("report on the performance on the Omega test, we respectfully but strongly \n");
326 printf
("request that send your test cases to us to allow us to determine if the \n");
327 printf
("times are appropriate, and if the way you are using the Omega library to \n");
328 printf
("solve your problem is the most effective way.\n");
331 printf
("Also, please be aware that over the past two years, we have focused our \n");
332 printf
("efforts on the expressive power of the Omega library, sometimes at the\n");
333 printf
("expensive of raw speed. Our original implementation of the Omega test\n");
334 printf
("was substantially faster on the limited domain it handled.\n");
336 printf
(" Thanks, \n");
337 printf
(" the Omega Team \n");
339 anyTimingDone
= true
;
343 | TIMECLOSURE relation
';' {
345 #if defined(OMIT_GETRUSAGE)
346 printf
("'timeclosure' requires getrusage, but the omega calclator was compiled with OMIT_GETRUSAGE set!\n");
352 ($2)->and_with_GEQ
();
354 for
(t
=1;t
<=100;t
++) {
358 int copyTime
= clock_diff
();
360 for
(t
=1;t
<=100;t
++) {
365 int simplifyTime
= clock_diff
() -copyTime
;
368 for
(t
=1;t
<=100;t
++) {
371 Rclosed
= TransitiveClosure
(Rclosed
, 1,Relation
::Null
());
373 int closureTime
= clock_diff
() - copyTime
;
376 for
(t
=1;t
<=100;t
++) {
381 int excessiveTime
= clock_diff
() - copyTime
;
382 printf
("Times (in microseconds): \n");
383 printf
("%5d us to copy original set of constraints\n",copyTime
/100);
384 printf
("%5d us to do the default amount of simplification, obtaining: \n\t",
386 R.print_with_subs
(stdout
);
388 printf
("%5d us to do the maximum (i.e., excessive) amount of simplification, obtaining: \n\t",
390 R2.print_with_subs
(stdout
);
391 printf
("%5d us to do the transitive closure, obtaining: \n\t",
393 Rclosed.print_with_subs
(stdout
);
395 if
(!anyTimingDone
) {
404 printf
("WARNING: The Omega calculator was compiled with options that force\n");
405 printf
("it to perform additional consistency and error checks\n");
406 printf
("that may slow it down substantially\n");
409 printf
("NOTE: These times relect the time of the current _implementation_\n");
410 printf
("of our algorithms. Performance bugs do exist. If you intend to publish or \n");
411 printf
("report on the performance on the Omega test, we respectfully but strongly \n");
412 printf
("request that send your test cases to us to allow us to determine if the \n");
413 printf
("times are appropriate, and if the way you are using the Omega library to \n");
414 printf
("solve your problem is the most effective way.\n");
417 printf
("Also, please be aware that over the past two years, we have focused our \n");
418 printf
("efforts on the expressive power of the Omega library, sometimes at the\n");
419 printf
("expensive of raw speed. Our original implementation of the Omega test\n");
420 printf
("was substantially faster on the limited domain it handled.\n");
422 printf
(" Thanks, \n");
423 printf
(" the Omega Team \n");
425 anyTimingDone
= true
;
431 | relation SUBSET relation
';' {
433 int c
= Must_Be_Subset
(*$1, *$3);
434 printf
("\n%s\n", c ?
"True" : "False");
438 | CODEGEN effort relPairList context
';'
441 String s
= MMGenerateCode
($3->mappings
, $3->ispaces
,*$4,$2);
444 printf
("%s\n", (const char *) s
);
446 | TCODEGEN effort statementInfoResult context
';'
449 String s
= tcodegen
($2, *($3), *($4));
452 printf
("%s\n", (const char *) s
);
454 /* | TCODEGEN NOT effort statementInfoResult context';'
457 * String s = tcodegen($3, *($4), *($5), false);
460 * printf("%s\n", (const char *) s);
463 | SPMD blockAndProcsAndEffort relTripList
';'
465 Tuple
<Free_Var_Decl
*> lowerBounds
(0), upperBounds
(0), my_procs
(0);
466 Tuple
<spmd_stmt_info
*> names
(0);
469 int nr_statements
= $3->space.size
();
471 for
(int i
= 1; i
<= $3->space
[1].n_out
(); i
++)
473 lowerBounds.append
(new Free_Var_Decl
("lb" + itoS
(i
)));
474 upperBounds.append
(new Free_Var_Decl
("ub" + itoS
(i
)));
475 my_procs.append
(new Free_Var_Decl
("my_proc" + itoS
(i
)));
478 for
(int p
= 1; p
<= nr_statements
; p
++)
479 names.append
(new numbered_stmt_info
(p
-1, Identity
($3->time
[p
].n_out
()),
481 (char *)(const char *)("s"+itoS
(p
-1))));
483 String s
= SPMD_GenerateCode
("", $3->space
, $3->time
, $3->ispaces
,
485 lowerBounds
, upperBounds
, my_procs
,
489 printf
("%s\n", (const char *) s
);
493 Dynamic_Array1
<Relation
> &final
= *$1;
495 int i
,n_nodes
= reachable_info
->node_names.size
();
496 for
(i
= 1; i
<= n_nodes
; i
++) if
(final
[i
].is_upper_bound_satisfiable
()) {
498 fprintf
(stdout
,"Node %s: ",
499 (const char *) (reachable_info
->node_names
[i
]));
500 final
[i
].print_with_subs
(stdout
);
503 fprintf
(stdout
,"No nodes reachable.\n");
505 delete reachable_info
;
507 | CARD relation
';' {
508 evalue
*EP
= count_relation
(*$2, COUNT_RELATION_BARVINOK
);
509 evalue_print_and_free
($2, EP
);
512 | CARD relation USING counting_method
';' {
513 evalue
*EP
= count_relation
(*$2, $4);
514 evalue_print_and_free
($2, EP
);
517 | RANKING relation
';' {
518 evalue
*EP
= rank_relation
(*$2);
520 const Variable_ID_Tuple
* globals
= $2->global_decls
();
521 int nvar
= $2->n_set
();
522 int n
= nvar
+ globals
->size
();
523 const char **names
= new
const char *[n
];
525 for
(int i
= 0; i
< nvar
; ++i
)
526 names
[i
] = $2->set_var
(i
+1)->char_name
();
527 for
(int i
= 0; i
< globals
->size
(); ++i
)
528 names
[nvar
+i
] = (*globals
)[i
+1]->char_name
();
529 print_evalue
(stdout
, EP
, names
);
536 | COUNT_LEXSMALLER relation WITHIN relation
';' {
537 evalue
*EP
= count_lexsmaller
(*$2, *$4);
539 const Variable_ID_Tuple
* globals
= $4->global_decls
();
540 int nvar
= $4->n_set
();
541 int n
= nvar
+ globals
->size
();
542 const char **names
= new
const char *[n
];
544 for
(int i
= 0; i
< nvar
; ++i
)
545 names
[i
] = $4->set_var
(i
+1)->char_name
();
546 for
(int i
= 0; i
< globals
->size
(); ++i
)
547 names
[nvar
+i
] = (*globals
)[i
+1]->char_name
();
548 print_evalue
(stdout
, EP
, names
);
556 | VERTICES relation
';' {
562 relationDecl
= new Declaration_Site
();
563 variableMap
= new Map
<Variable_Ref
*, GiNaC
::ex
>(0);
566 maximize
($3, *variableMap
);
568 current_Declaration_Site
= globalDecls
;
572 | DUMP relation
';' {
577 relTripList: relTripList
',' relation
':' relation
':' relation
579 $1->space.append
(*$3);
580 $1->time.append
(*$5);
581 $1->ispaces.append
(*$7);
587 | relation
':' relation
':' relation
589 RelTupleTriple
*rtt
= new RelTupleTriple
;
590 rtt
->space.append
(*$1);
591 rtt
->time.append
(*$3);
592 rtt
->ispaces.append
(*$5);
600 blockAndProcsAndEffort
: { Block_Size
= 0; Num_Procs
= 0; overheadEffort
=0; }
601 | INT
{ Block_Size
= $1; Num_Procs
= 0; overheadEffort
=0;}
602 | INT INT
{ Block_Size
= $1; Num_Procs
= $2; overheadEffort
=0;}
603 | INT INT INT
{ Block_Size
= $1; Num_Procs
= $2; overheadEffort
=$3;}
607 BARVINOK
{ $$
= COUNT_RELATION_BARVINOK
; }
608 | PARKER
{ $$
= COUNT_RELATION_PARKER
; }
613 |
'-' INT
{ $$
= -$2; }
616 context
: { $$
= new Relation
();
617 *$$
= Relation
::Null
(); }
618 | GIVEN relation
{$$
= $2; }
621 relPairList: relPairList
',' relation
':' relation
623 $1->mappings.append
(*$3);
624 $1->mappings
[$1->mappings.size
()].compress
();
625 $1->ispaces.append
(*$5);
626 $1->ispaces
[$1->ispaces.size
()].compress
();
631 | relPairList
',' relation
633 $1->mappings.append
(Identity
($3->n_set
()));
634 $1->mappings
[$1->mappings.size
()].compress
();
635 $1->ispaces.append
(*$3);
636 $1->ispaces
[$1->ispaces.size
()].compress
();
640 | relation
':' relation
642 RelTuplePair
*rtp
= new RelTuplePair
;
643 rtp
->mappings.append
(*$1);
644 rtp
->mappings
[rtp
->mappings.size
()].compress
();
645 rtp
->ispaces.append
(*$3);
646 rtp
->ispaces
[rtp
->ispaces.size
()].compress
();
653 RelTuplePair
*rtp
= new RelTuplePair
;
654 rtp
->mappings.append
(Identity
($1->n_set
()));
655 rtp
->mappings
[rtp
->mappings.size
()].compress
();
656 rtp
->ispaces.append
(*$1);
657 rtp
->ispaces
[rtp
->ispaces.size
()].compress
();
663 statementInfoResult
: statementInfoList
665 /* | ASSERT_UNSAT statementInfoResult
667 * DoDebug2("Debug info requested in input", *($2));
670 | TRANS_IS relation statementInfoResult
671 { $$
= &Trans_IS
(*($3), *($2));
674 | SET_MMAP INT partialwrites statementInfoResult
675 { $$
= &Set_MMap
(*($4), $2, *($3));
678 | UNROLL_IS INT INT INT statementInfoResult
679 { $$
= &Unroll_One_IS
(*($5), $2, $3, $4);}
680 | PEEL_IS INT INT relation statementInfoResult
681 { $$
= &Peel_One_IS
(*($5), $2, $3, *($4));
684 | PEEL_IS INT INT relation
',' relation statementInfoResult
685 { $$
= &Peel_One_IS
(*($7), $2, $3, *($4), *($6));
691 statementInfoList
: statementInfo
{ $$
= new Tuple
<stm_info
>;
694 | statementInfoList
',' statementInfo
{ $$
= $1;
699 statementInfo
: '[' STRING
',' relation
',' partialwrites
',' reads
']'
701 $$
->stm
= *($2); delete
$2;
702 $$
->IS
= *($4); delete
$4;
703 $$
->map
= *($6); delete
$6;
705 |
'[' STRING
',' relation
',' partialwrites
']'
707 $$
->stm
= *($2); delete
$2;
708 $$
->IS
= *($4); delete
$4;
709 $$
->map
= *($6); delete
$6;
713 partialwrites
: partialwrites
',' partialwrite
715 $$
->partials.append
(*($3)); delete
$3;
717 | partialwrite
{ $$
= new MMap
;
718 $$
->partials.append
(*($1)); delete
$1;
722 partialwrite
: STRING
'[' relation
']' ',' relation
723 { $$
= new PartialMMap
;
724 $$
->mapping
= *($6); delete
$6;
725 $$
->bounds
= *($3); delete
$3;
726 $$
->var
= *($1); delete
$1;
728 | STRING
',' relation
{ $$
= new PartialMMap
;
729 $$
->mapping
= *($3); delete
$3;
730 $$
->bounds
= Relation
::True
(0);
731 $$
->var
= *($1); delete
$1;
735 reads
: reads
',' oneread
{ $$
= $1;
736 $$
->read.append
(*($3)); delete
$3;
738 | oneread
{ $$
= new stm_info
;
739 $$
->read.append
(*($1)); delete
$1;
743 oneread
: '[' partials
']' { $$
= $2; }
746 partials
: partials
',' partial
{ $$
= $1;
747 $$
->partials.append
(*($3)); delete
$3;
749 | partial
{ $$
= new Read
;
750 $$
->partials.append
(*($1)); delete
$1;
754 partial
: INT
',' relation
{ $$
= new PartialRead
;
756 $$
->dataFlow
= *($3); delete
$3;
760 globVarList: globVarList
',' globVar
764 globVar: VAR
'(' INT
')'
765 { globalDecls
->extend_both_tuples
($1, $3); free
($1); }
767 { globalDecls
->extend
($1); free
($1); }
770 polynomial
: INT
{ $$
= new GiNaC
::ex
($1); }
772 Variable_Ref
*v
= lookupScalar
($1);
775 if
((*variableMap
)(v
) == 0)
776 (*variableMap
)[v
] = GiNaC
::symbol
(std
::string(v
->name
));
777 $$
= new GiNaC
::ex
((*variableMap
)[v
]);
779 |
'(' polynomial
')' { $$
= $2; }
780 |
'-' polynomial %prec
'*' {
781 $$
= new GiNaC
::ex
(-*$2);
784 | polynomial
'+' polynomial
{
785 $$
= new GiNaC
::ex
(*$1 + *$3);
789 | polynomial
'-' polynomial
{
790 $$
= new GiNaC
::ex
(*$1 - *$3);
794 | polynomial
'/' polynomial
{
795 $$
= new GiNaC
::ex
(*$1 / *$3);
799 | polynomial
'*' polynomial
{
800 $$
= new GiNaC
::ex
(*$1 * *$3);
806 polyfunc
: OPEN_BRACE
807 tupleDeclaration GOES_TO polynomial optionalFormula CLOSE_BRACE
{
808 Relation
*r
= build_relation
($2, $5);
817 relation
: OPEN_BRACE
818 { relationDecl
= new Declaration_Site
(); }
822 if
(omega_calc_debug
) {
823 fprintf
(DebugFile
,"Built relation:\n");
824 $$
->prefix_print
(DebugFile
);
826 current_Declaration_Site
= globalDecls
;
831 if
(relationMap
(s
) == 0) {
832 fprintf
(stderr
,"Variable %s not declared\n",$1);
838 $$
= new Relation
(*relationMap
(s
));
840 |
'(' relation
')' {$$
= $2;}
841 | relation
'+' %prec OMEGA_P9
842 { $$
= new Relation
();
843 *$$
= TransitiveClosure
(*$1, 1,Relation
::Null
());
846 | relation
'*' %prec OMEGA_P9
847 { $$
= new Relation
();
848 int vars
= $1->n_inp
();
849 *$$
= Union
(Identity
(vars
),
850 TransitiveClosure
(*$1, 1,Relation
::Null
()));
853 | relation
'+' WITHIN relation %prec OMEGA_P9
854 {$$
= new Relation
();
855 *$$
= TransitiveClosure
(*$1, 1,*$4);
859 | MINIMIZE_RANGE relation %prec OMEGA_P8
863 r
= Join
(r
,LexForward
($2->n_out
()));
865 *$$
= Difference
(o
,r
);
868 | MAXIMIZE_RANGE relation %prec OMEGA_P8
872 r
= Join
(r
,Inverse
(LexForward
($2->n_out
())));
874 *$$
= Difference
(o
,r
);
877 | MINIMIZE_DOMAIN relation %prec OMEGA_P8
881 r
= Join
(LexForward
($2->n_inp
()),r
);
883 *$$
= Difference
(o
,r
);
886 | MAXIMIZE_DOMAIN relation %prec OMEGA_P8
890 r
= Join
(Inverse
(LexForward
($2->n_inp
())),r
);
892 *$$
= Difference
(o
,r
);
895 | MAXIMIZE relation %prec OMEGA_P8
900 *$$
= Cross_Product
(Relation
(*$2),c
);
902 assert
($$
->n_inp
() ==$$
->n_out
());
903 *$$
= Difference
(r
,Domain
(Intersection
(*$$
,LexForward
($$
->n_inp
()))));
905 | MINIMIZE relation %prec OMEGA_P8
910 *$$
= Cross_Product
(Relation
(*$2),c
);
912 assert
($$
->n_inp
() ==$$
->n_out
());
913 *$$
= Difference
(r
,Range
(Intersection
(*$$
,LexForward
($$
->n_inp
()))));
915 | FARKAS relation %prec OMEGA_P8
918 *$$
= Farkas
(*$2, Basic_Farkas
);
921 | DECOUPLED_FARKAS relation %prec OMEGA_P8
924 *$$
= Farkas
(*$2, Decoupled_Farkas
);
927 | relation
'@' %prec OMEGA_P9
928 { $$
= new Relation
();
929 *$$
= ConicClosure
(*$1);
932 | PROJECT_AWAY_SYMBOLS relation %prec OMEGA_P8
933 { $$
= new Relation
();
934 *$$
= Project_Sym
(*$2);
937 | PROJECT_ON_SYMBOLS relation %prec OMEGA_P8
938 { $$
= new Relation
();
939 *$$
= Project_On_Sym
(*$2);
942 | DIFFERENCE relation %prec OMEGA_P8
943 { $$
= new Relation
();
947 | DIFFERENCE_TO_RELATION relation %prec OMEGA_P8
948 { $$
= new Relation
();
949 *$$
= DeltasToRelation
(*$2,$2->n_set
(),$2->n_set
());
952 | OMEGA_DOMAIN relation %prec OMEGA_P8
953 { $$
= new Relation
();
957 | VENN relation %prec OMEGA_P8
958 { $$
= new Relation
();
959 *$$
= VennDiagramForm
(*$2,Relation
::True
(*$2));
962 | VENN relation GIVEN relation %prec OMEGA_P8
963 { $$
= new Relation
();
964 *$$
= VennDiagramForm
(*$2,*$4);
968 | CONVEX_HULL relation %prec OMEGA_P8
969 { $$
= new Relation
();
970 *$$
= ConvexHull
(*$2);
973 | POSITIVE_COMBINATION relation %prec OMEGA_P8
974 { $$
= new Relation
();
975 *$$
= Farkas
(*$2,Positive_Combination_Farkas
);
978 | CONVEX_COMBINATION relation %prec OMEGA_P8
979 { $$
= new Relation
();
980 *$$
= Farkas
(*$2,Convex_Combination_Farkas
);
983 | PAIRWISE_CHECK relation %prec OMEGA_P8
984 { $$
= new Relation
();
985 *$$
= CheckForConvexRepresentation
(CheckForConvexPairs
(*$2));
988 | CONVEX_CHECK relation %prec OMEGA_P8
989 { $$
= new Relation
();
990 *$$
= CheckForConvexRepresentation
(*$2);
993 | AFFINE_HULL relation %prec OMEGA_P8
994 { $$
= new Relation
();
995 *$$
= AffineHull
(*$2);
998 | CONIC_HULL relation %prec OMEGA_P8
999 { $$
= new Relation
();
1000 *$$
= ConicHull
(*$2);
1003 | LINEAR_HULL relation %prec OMEGA_P8
1004 { $$
= new Relation
();
1005 *$$
= LinearHull
(*$2);
1008 | HULL relation %prec OMEGA_P8
1009 { $$
= new Relation
();
1010 *$$
= Hull
(*$2,false
,1,Null_Relation
());
1013 | HULL relation GIVEN relation %prec OMEGA_P8
1014 { $$
= new Relation
();
1015 *$$
= Hull
(*$2,false
,1,*$4);
1019 | APPROX relation %prec OMEGA_P8
1020 { $$
= new Relation
();
1021 *$$
= Approximate
(*$2);
1024 | RANGE relation %prec OMEGA_P8
1025 { $$
= new Relation
();
1029 | INVERSE relation %prec OMEGA_P8
1030 { $$
= new Relation
();
1034 | COMPLEMENT relation %prec OMEGA_P8
1035 { $$
= new Relation
();
1036 *$$
= Complement
(*$2);
1039 | GIST relation GIVEN relation %prec OMEGA_P8
1040 { $$
= new Relation
();
1041 *$$
= Gist
(*$2,*$4,1);
1045 | relation
'(' relation
')'
1046 { $$
= new Relation
();
1047 *$$
= Composition
(*$1,*$3);
1051 | relation COMPOSE relation
1052 { $$
= new Relation
();
1053 *$$
= Composition
(*$1,*$3);
1057 | relation CARRIED_BY INT
1058 { $$
= new Relation
();
1059 *$$
= After
(*$1,$3,$3);
1061 (*$$
).prefix_print
(stdout
);
1063 | relation JOIN relation
1064 { $$
= new Relation
();
1065 *$$
= Composition
(*$3,*$1);
1069 | relation RESTRICT_RANGE relation
1070 { $$
= new Relation
();
1071 *$$
= Restrict_Range
(*$1,*$3);
1075 | relation RESTRICT_DOMAIN relation
1076 { $$
= new Relation
();
1077 *$$
= Restrict_Domain
(*$1,*$3);
1081 | relation INTERSECTION relation
1082 { $$
= new Relation
();
1083 *$$
= Intersection
(*$1,*$3);
1087 | relation
'-' relation %prec INTERSECTION
1088 { $$
= new Relation
();
1089 *$$
= Difference
(*$1,*$3);
1093 | relation UNION relation
1094 { $$
= new Relation
();
1095 *$$
= Union
(*$1,*$3);
1099 | relation
'*' relation
1100 { $$
= new Relation
();
1101 *$$
= Cross_Product
(*$1,*$3);
1105 | SUPERSETOF relation
1106 { $$
= new Relation
();
1107 *$$
= Union
(*$2, Relation
::Unknown
(*$2));
1111 { $$
= new Relation
();
1112 *$$
= Intersection
(*$2, Relation
::Unknown
(*$2));
1115 | MAKE_UPPER_BOUND relation %prec OMEGA_P8
1116 { $$
= new Relation
();
1117 *$$
= Upper_Bound
(*$2);
1120 | MAKE_LOWER_BOUND relation %prec OMEGA_P8
1121 { $$
= new Relation
();
1122 *$$
= Lower_Bound
(*$2);
1126 { $$
= new Relation
();
1127 *$$
= Sample_Solution
(*$2);
1130 | SYM_SAMPLE relation
1131 { $$
= new Relation
();
1132 *$$
= Symbolic_Solution
(*$2);
1135 | reachable_of
{ $$
= $1; }
1136 | ASSERT_UNSAT relation
1138 if
(($2)->is_satisfiable
())
1140 fprintf
(stderr
,"assert_unsatisfiable failed on ");
1141 ($2)->print_with_subs
(stderr
);
1150 tupleDeclaration GOES_TO
{currentTuple
= Output_Tuple
;}
1151 tupleDeclaration
{currentTuple
= Input_Tuple
;} optionalFormula
{
1152 Relation
* r
= new Relation
($1->size
,$4->size
);
1154 F_And
*f
= r
->add_and
();
1156 for
(i
=1;i
<=$1->size
;i
++) {
1157 $1->vars
[i
]->vid
= r
->input_var
(i
);
1158 if
(!$1->vars
[i
]->anonymous
)
1159 r
->name_input_var
(i
,$1->vars
[i
]->stripped_name
);
1161 for
(i
=1;i
<=$4->size
;i
++) {
1162 $4->vars
[i
]->vid
= r
->output_var
(i
);
1163 if
(!$4->vars
[i
]->anonymous
)
1164 r
->name_output_var
(i
,$4->vars
[i
]->stripped_name
);
1166 foreach
(e
,Exp
*,$1->eq_constraints
, install_eq
(f
,e
,0));
1167 foreach
(e
,Exp
*,$1->geq_constraints
, install_geq
(f
,e
,0));
1168 foreach
(c
,strideConstraint
*,$1->stride_constraints
, install_stride
(f
,c
));
1169 foreach
(e
,Exp
*,$4->eq_constraints
, install_eq
(f
,e
,0));
1170 foreach
(e
,Exp
*,$4->geq_constraints
, install_geq
(f
,e
,0));
1171 foreach
(c
,strideConstraint
*,$4->stride_constraints
, install_stride
(f
,c
));
1172 if
($6) $6->install
(f
);
1177 | tupleDeclaration optionalFormula
{
1178 $$
= build_relation
($1, $2);
1181 Relation
* r
= new Relation
(0,0);
1182 F_And
*f
= r
->add_and
();
1189 optionalFormula
: formula_sep formula
{ $$
= $2; }
1200 if
(currentTupleDescriptor
)
1201 delete currentTupleDescriptor
;
1202 currentTupleDescriptor
= new tupleDescriptor
;
1205 '[' optionalTupleVarList
']'
1206 {$$
= currentTupleDescriptor
; currentTupleDescriptor
= NULL
; }
1209 optionalTupleVarList
:
1211 | optionalTupleVarList
',' tupleVar
1215 tupleVar
: VAR %prec OMEGA_P10
1216 { Declaration_Site
*ds
= defined
($1);
1217 if
(!ds
) currentTupleDescriptor
->extend
($1,currentTuple
,tuplePos
);
1219 Variable_Ref
* v
= lookupScalar
($1);
1221 if
(ds
!= globalDecls
)
1222 currentTupleDescriptor
->extend
($1, new Exp
(v
));
1224 currentTupleDescriptor
->extend
(new Exp
(v
));
1230 {currentTupleDescriptor
->extend
(); tuplePos
++; }
1231 | exp %prec OMEGA_P1
1232 {currentTupleDescriptor
->extend
($1); tuplePos
++; }
1233 | exp
':' exp %prec OMEGA_P1
1234 {currentTupleDescriptor
->extend
($1,$3); tuplePos
++; }
1235 | exp
':' exp
':' INT %prec OMEGA_P1
1236 {currentTupleDescriptor
->extend
($1,$3,$5); tuplePos
++; }
1240 varList: varList
',' VAR
{$$
= $1; $$
->insert
($3); }
1241 | VAR
{ $$
= new VarList
;
1247 $$
= current_Declaration_Site
= new Declaration_Site
($1);
1248 foreach
(s
,char *, *$1, free
(s
));
1253 /* variable declaration with optional brackets */
1255 varDeclOptBrackets
: varDecl
{ $$
= $1; }
1256 |
'[' varDecl
']' { $$
= $2; }
1259 formula
: formula AND formula
{ $$
= new AST_And
($1,$3); }
1260 | formula OR formula
{ $$
= new AST_Or
($1,$3); }
1261 | constraintChain
{ $$
= $1; }
1262 |
'(' formula
')' { $$
= $2; }
1263 | NOT formula
{ $$
= new AST_Not
($2); }
1264 | start_exists varDeclOptBrackets exists_sep formula end_quant
1265 { $$
= new AST_exists
($2,$4); }
1266 | start_forall varDeclOptBrackets forall_sep formula end_quant
1267 { $$
= new AST_forall
($2,$4); }
1270 start_exists
: '(' EXISTS
1279 start_forall
: '(' FORALL
1290 expList
: exp
',' expList
1301 constraintChain
: expList REL_OP expList
1302 { $$
= new AST_constraints
($1,$2,$3); }
1303 | expList REL_OP constraintChain
1304 { $$
= new AST_constraints
($1,$2,$3); }
1309 { Variable_Ref
* v
= lookupScalar
($1);
1314 | VAR
'(' {argCount
= 1;} argumentList
')' %prec OMEGA_P9
1316 if
($4 == Input_Tuple
) v
= functionOfInput
[$1];
1317 else v
= functionOfOutput
[$1];
1319 fprintf
(stderr
,"Function %s(...) not declared\n",$1);
1326 |
'(' exp
')' { $$
= $2;}
1332 argumentList
',' VAR
{
1333 Variable_Ref
* v
= lookupScalar
($3);
1336 if
(v
->pos
!= argCount || v
->of
!= $1 || v
->of
!= Input_Tuple
&& v
->of
!= Output_Tuple
) {
1337 fprintf
(stderr
,"arguments to function must be prefix of input or output tuple\n");
1343 | VAR
{ Variable_Ref
* v
= lookupScalar
($1);
1346 if
(v
->pos
!= argCount || v
->of
!= Input_Tuple
&& v
->of
!= Output_Tuple
) {
1347 fprintf
(stderr
,"arguments to function must be prefix of input or output tuple\n");
1355 exp
: INT
{$$
= new Exp
($1);}
1356 | INT simpleExp %prec
'*' {$$
= multiply
($1,$2);}
1357 | simpleExp
{ $$
= $1; }
1358 |
'-' exp %prec
'*' { $$
= negate
($2);}
1359 | exp
'+' exp
{ $$
= add
($1,$3);}
1360 | exp
'-' exp
{ $$
= subtract
($1,$3);}
1361 | exp
'*' exp
{ $$
= multiply
($1,$3);}
1366 REACHABLE_FROM nodeNameList nodeSpecificationList
1368 Dynamic_Array1
<Relation
> *final
=
1369 Reachable_Nodes
(reachable_info
);
1375 REACHABLE_OF VAR IN nodeNameList nodeSpecificationList
1377 Dynamic_Array1
<Relation
> *final
=
1378 Reachable_Nodes
(reachable_info
);
1379 int index
= reachable_info
->node_names.index
(String
($2));
1380 assert
(index
!= 0 && "No such node");
1382 *$$
= (*final
)[index
];
1384 delete reachable_info
;
1390 nodeNameList: '(' realNodeNameList
')'
1391 { int sz
= reachable_info
->node_names.size
();
1392 reachable_info
->node_arity.reallocate
(sz
);
1393 reachable_info
->transitions.resize
(sz
+1,sz
+1);
1394 reachable_info
->start_nodes.resize
(sz
+1);
1398 realNodeNameList: realNodeNameList
',' VAR
1399 { reachable_info
->node_names.append
(String
($3));
1401 | VAR
{ reachable_info
= new reachable_information
;
1402 reachable_info
->node_names.append
(String
($1));
1407 nodeSpecificationList: OPEN_BRACE realNodeSpecificationList CLOSE_BRACE
1410 int n_nodes
= reachable_info
->node_names.size
();
1411 Tuple
<int> &arity
= reachable_info
->node_arity
;
1412 Dynamic_Array2
<Relation
> &transitions
= reachable_info
->transitions
;
1414 /* fixup unspecified transitions to be false */
1416 for
(i
= 1; i
<= n_nodes
; i
++) arity
[i
] = -1;
1417 for
(i
= 1; i
<= n_nodes
; i
++)
1418 for
(j
= 1; j
<= n_nodes
; j
++)
1419 if
(! transitions
[i
][j
].is_null
()) {
1420 int in_arity
= transitions
[i
][j
].n_inp
();
1421 int out_arity
= transitions
[i
][j
].n_out
();
1422 if
(arity
[i
] < 0) arity
[i
] = in_arity
;
1423 if
(arity
[j
] < 0) arity
[j
] = out_arity
;
1424 if
(in_arity
!= arity
[i
] || out_arity
!= arity
[j
]) {
1426 "Arity mismatch in node transition: %s -> %s",
1427 (const char *) reachable_info
->node_names
[i
],
1428 (const char *) reachable_info
->node_names
[j
]);
1433 for
(i
= 1; i
<= n_nodes
; i
++)
1434 if
(arity
[i
] < 0) arity
[i
] = 0;
1435 /* Fill in false relations */
1436 for
(i
= 1; i
<= n_nodes
; i
++)
1437 for
(j
= 1; j
<= n_nodes
; j
++)
1438 if
(transitions
[i
][j
].is_null
())
1439 transitions
[i
][j
] = Relation
::False
(arity
[i
],arity
[j
]);
1442 /* fixup unused start node positions */
1443 Dynamic_Array1
<Relation
> &nodes
= reachable_info
->start_nodes
;
1444 for
(i
= 1; i
<= n_nodes
; i
++)
1445 if
(nodes
[i
].is_null
())
1446 nodes
[i
] = Relation
::False
(arity
[i
]);
1448 if
(nodes
[i
].n_set
() != arity
[i
]){
1449 fprintf
(stderr
,"Arity mismatch in start node %s",
1450 (const char *) reachable_info
->node_names
[i
]);
1457 realNodeSpecificationList:
1458 realNodeSpecificationList
',' VAR
':' relation
1459 { int n_nodes
= reachable_info
->node_names.size
();
1460 int index
= reachable_info
->node_names.index
($3);
1461 assert
(index
!= 0 && index
<= n_nodes
);
1462 reachable_info
->start_nodes
[index
] = *$5;
1466 | realNodeSpecificationList
',' VAR GOES_TO VAR
':' relation
1467 { int n_nodes
= reachable_info
->node_names.size
();
1468 int from_index
= reachable_info
->node_names.index
($3);
1469 int to_index
= reachable_info
->node_names.index
($5);
1470 assert
(from_index
!= 0 && to_index
!= 0);
1471 assert
(from_index
<= n_nodes
&& to_index
<= n_nodes
);
1472 reachable_info
->transitions
[from_index
][to_index
] = *$7;
1477 | VAR GOES_TO VAR
':' relation
1478 { int n_nodes
= reachable_info
->node_names.size
();
1479 int from_index
= reachable_info
->node_names.index
($1);
1480 int to_index
= reachable_info
->node_names.index
($3);
1481 assert
(from_index
!= 0 && to_index
!= 0);
1482 assert
(from_index
<= n_nodes
&& to_index
<= n_nodes
);
1483 reachable_info
->transitions
[from_index
][to_index
] = *$5;
1489 { int n_nodes
= reachable_info
->node_names.size
();
1490 int index
= reachable_info
->node_names.index
($1);
1491 assert
(index
!= 0 && index
<= n_nodes
);
1492 reachable_info
->start_nodes
[index
] = *$3;
1500 #if !defined(OMIT_GETRUSAGE)
1501 #include <sys/types.h>
1502 #include <sys/time.h>
1503 #include <sys/resource.h>
1505 struct rusage start_time
;
1508 #if defined BRAIN_DAMAGED_FREE
1514 void *realloc
(void *p
, size_t s
)
1516 return realloc
((malloc_t
) p
, s
);
1520 #if ! defined(OMIT_GETRUSAGE)
1522 extern
"C" int getrusage
(int, struct rusage
*);
1525 void start_clock
( void )
1527 getrusage
(RUSAGE_SELF
, &start_time
);
1530 int clock_diff
( void )
1532 struct rusage current_time
;
1533 getrusage
(RUSAGE_SELF
, ¤t_time
);
1534 return
(current_time.ru_utime.tv_sec
-start_time.ru_utime.tv_sec
)*1000000 +
1535 (current_time.ru_utime.tv_usec
-start_time.ru_utime.tv_usec
);
1539 void printUsage
(FILE *outf
, char **argv
) {
1540 fprintf
(outf
, "usage: %s {-R} {-D[facility][level]...} infile\n -R means skip redundant conjunct elimination\n -D sets debugging level as follows:\n a = all debugging flags\n g = code generation\n l = calculator\n c = omega core\n p = presburger functions\n r = relational operators\n t = transitive closure\nAll debugging output goes to %s\n",argv
[0],DEBUG_FILE_NAME
);
1543 int omega_calc_debug
;
1546 int main
(int argc
, char **argv
){
1547 redundant_conj_level
= 2;
1548 current_Declaration_Site
= globalDecls
= new Global_Declaration_Site
();
1553 char * fileName
= 0;
1555 printf
("# %s\n", GIT_HEAD_ID
);
1556 printf
("# %s (based on %s, %s):\n",CALC_VERSION_STRING
, Omega_Library_Version
, Omega_Library_Date
);
1558 calc_all_debugging_off
();
1561 DebugFile
= fopen
("/dev/null","w");
1564 DebugFile
= fopen
(DEBUG_FILE_NAME
, "w");
1566 fprintf
(stderr
, "Can't open debug file %s\n", DEBUG_FILE_NAME
);
1569 setbuf
(DebugFile
,0);
1572 closure_presburger_debug
= 0;
1574 setOutputFile
(DebugFile
);
1577 for
(i
=1; i
<argc
; i
++) {
1578 if
(argv
[i
][0] == '-') {
1580 while
((c
=argv
[i
][j
++]) != 0) {
1583 if
(! process_calc_debugging_flags
(argv
[i
],j
)) {
1584 printUsage
(stderr
,argv
);
1590 fprintf
(stderr
,"Note: specifying number of GEQ's is no longer useful.\n");
1591 while
(argv
[i
][j
] != 0) j
++;
1596 fprintf
(stderr
,"Note: specifying number of EQ's is no longer useful.\n");
1597 while
(argv
[i
][j
] != 0) j
++;
1601 redundant_conj_level
= 1;
1603 // Other future options go here
1605 fprintf
(stderr
, "\nUnknown flag -%c\n", c
);
1606 printUsage
(stderr
,argv
);
1612 // Make sure this is a file name
1614 fprintf
(stderr
,"\nCan only handle a single input file\n");
1615 printUsage
(stderr
,argv
);
1619 yyin
= fopen
(fileName
, "r");
1621 fprintf
(stderr
, "\nCan't open input file %s\n",fileName
);
1622 printUsage
(stderr
,argv
);
1630 initializeScanBuffer
();
1631 currentTupleDescriptor
= NULL
;
1636 foreach_map
(cs
,Const_String
,r
,Relation
*,relationMap
,
1637 {delete r
; relationMap
[cs
]=0;});
1643 Relation LexForward
(int n
) {
1645 F_Or
*f
= r.add_or
();
1646 for
(int i
=1; i
<= n
; i
++) {
1647 F_And
*g
= f
->add_and
();
1648 for
(int j
=1;j
<i
;j
++) {
1649 EQ_Handle e
= g
->add_EQ
();
1650 e.update_coef
(r.input_var
(j
),-1);
1651 e.update_coef
(r.output_var
(j
),1);
1654 GEQ_Handle e
= g
->add_GEQ
();
1655 e.update_coef
(r.input_var
(i
),-1);
1656 e.update_coef
(r.output_var
(i
),1);