3 #define compilingParser
4 #include <basic/Dynamic_Array.h>
5 #include <code_gen/code_gen.h>
6 #include <code_gen/spmd.h>
7 #include <omega/library_version.h>
9 #include <omega_calc/yylex.h>
10 #include <omega/hull.h>
11 #include <omega/calc_debug.h>
12 #include <basic/Exit.h>
13 #include <omega/closure.h>
14 #include <omega/reach.h>
15 #include <code_gen/mmap-codegen.h>
16 #include <code_gen/mmap-util.h>
19 #include <sys/resource.h>
21 #include <barvinok/bernstein.h>
26 #define CALC_VERSION_STRING "Omega Calculator v1.2"
28 #define DEBUG_FILE_NAME "./oc.out"
31 Map
<Const_String
,Relation
*> relationMap
((Relation
*)0);
32 static int redundant_conj_level
;
34 #if defined BRAIN_DAMAGED_FREE
36 void *realloc
(void *p
, size_t s
);
39 #if !defined(OMIT_GETRUSAGE)
40 void start_clock
( void );
41 int clock_diff
( void );
42 bool anyTimingDone
= false
;
47 Argument_Tuple currentTuple
= Input_Tuple
;
48 char *currentVar
= NULL
;
50 Relation LexForward
(int n
);
53 reachable_information
*reachable_info
;
55 Relation
*build_relation
(tupleDescriptor
*tuple
, AST
* ast
)
57 Relation
* r
= new Relation
(tuple
->size
);
59 F_And
*f
= r
->add_and
();
61 for
(i
=1;i
<=tuple
->size
;i
++) {
62 tuple
->vars
[i
]->vid
= r
->set_var
(i
);
63 if
(!tuple
->vars
[i
]->anonymous
)
64 r
->name_set_var
(i
,tuple
->vars
[i
]->stripped_name
);
66 foreach
(e
,Exp
*,tuple
->eq_constraints
, install_eq
(f
,e
,0));
67 foreach
(e
,Exp
*,tuple
->geq_constraints
, install_geq
(f
,e
,0));
68 foreach
(c
,strideConstraint
*,tuple
->stride_constraints
, install_stride
(f
,c
));
69 if
(ast
) ast
->install
(f
);
75 Map
<Variable_Ref
*, GiNaC
::ex
> variableMap
(0);
81 %token
<INT_VALUE
> INT
82 %token
<STRING_VALUE
> STRING
83 %token OPEN_BRACE CLOSE_BRACE
89 %token OMEGA_DOMAIN RANGE
90 %token DIFFERENCE DIFFERENCE_TO_RELATION
91 %token GIST GIVEN HULL WITHIN MAXIMIZE MINIMIZE
92 %token AFFINE_HULL VENN CONVEX_COMBINATION POSITIVE_COMBINATION CONVEX_HULL CONIC_HULL LINEAR_HULL PAIRWISE_CHECK CONVEX_CHECK
93 %token MAXIMIZE_RANGE MINIMIZE_RANGE
94 %token MAXIMIZE_DOMAIN MINIMIZE_DOMAIN
97 %token COMPOSE JOIN INVERSE COMPLEMENT IN CARRIED_BY TIME TIMECLOSURE
98 %token UNION INTERSECTION
99 %token VERTICAL_BAR SUCH_THAT
100 %token SUBSET ITERATIONS SPMD CODEGEN DECOUPLED_FARKAS FARKAS
101 %token TCODEGEN TRANS_IS SET_MMAP UNROLL_IS PEEL_IS
102 %token MAKE_UPPER_BOUND MAKE_LOWER_BOUND
103 %token
<REL_OPERATOR
> REL_OP
104 %token RESTRICT_DOMAIN RESTRICT_RANGE
105 %token SUPERSETOF SUBSETOF SAMPLE SYM_SAMPLE
106 %token PROJECT_AWAY_SYMBOLS PROJECT_ON_SYMBOLS REACHABLE_FROM REACHABLE_OF
108 %token CARD RANKING COUNT_LEXSMALLER
112 %token PARSE_EXPRESSION PARSE_FORMULA PARSE_RELATION
114 %nonassoc ASSERT_UNSAT
115 %left UNION OMEGA_P1
'+' '-'
116 %nonassoc SUPERSETOF SUBSETOF
117 %left OMEGA_P2 RESTRICT_DOMAIN RESTRICT_RANGE
118 %left INTERSECTION OMEGA_P3
'*' '@'
123 %left COMPOSE JOIN CARRIED_BY
124 %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
129 %right CARD RANKING COUNT_LEXSMALLER
133 %type
<INT_VALUE
> effort
134 %type
<EXP
> exp simpleExp
135 %type
<EXP_LIST
> expList
136 %type
<VAR_LIST
> varList
137 %type
<ARGUMENT_TUPLE
> argumentList
138 %type
<ASTP
> formula optionalFormula
139 %type
<ASTCP
> constraintChain
140 %type
<TUPLE_DESCRIPTOR
> tupleDeclaration
141 %type
<DECLARATION_SITE
> varDecl varDeclOptBrackets
142 %type
<RELATION
> relation builtRelation context
143 %type
<RELATION
> reachable_of
144 %type
<REL_TUPLE_PAIR
> relPairList
145 %type
<REL_TUPLE_TRIPLE
> relTripList
146 %type
<RELATION_ARRAY_1D
> reachable
147 %type
<STM_INFO_TUPLE
> statementInfoList statementInfoResult
148 %type
<STM_INFO
> statementInfo
149 %type
<STM_INFO
> reads
151 %type
<READ
> partials
152 %type
<PREAD
> partial
153 %type
<MMAP
> partialwrites
154 %type
<PMMAP
> partialwrite
155 %type
<POLYFUNC
> polyfunc
156 %type
<POLYNOMIAL
> polynomial
166 Argument_Tuple ARGUMENT_TUPLE
;
167 AST_constraints
*ASTCP
;
168 Declaration_Site
* DECLARATION_SITE
;
170 tupleDescriptor
* TUPLE_DESCRIPTOR
;
171 RelTuplePair
* REL_TUPLE_PAIR
;
172 RelTupleTriple
* REL_TUPLE_TRIPLE
;
173 Dynamic_Array2
<Relation
> * RELATION_ARRAY_2D
;
174 Dynamic_Array1
<Relation
> * RELATION_ARRAY_1D
;
175 Tuple
<String
> *STRING_TUPLE
;
176 String
*STRING_VALUE
;
177 Tuple
<stm_info
> *STM_INFO_TUPLE
;
184 GiNaC
::ex
*POLYNOMIAL
;
195 inputSequence
: inputItem
196 | inputSequence
{ assert
( current_Declaration_Site
== globalDecls
);}
203 /* Kill all the local declarations -- ejr */
206 Declaration_Site
*ds1
, *ds2
;
207 for
(ds1
= current_Declaration_Site
; ds1
!= globalDecls
;) {
212 current_Declaration_Site
= globalDecls
;
213 yyerror("skipping to statement end");
215 | SYMBOLIC globVarList
';'
218 | VAR
{ currentVar
= $1; } IS_ASSIGNED relation
';'
221 $4->simplify
(min
(2,redundant_conj_level
),4);
222 Relation
*r
= relationMap
((Const_String
)$1);
224 relationMap
[(Const_String
)$1] = $4;
231 $1->simplify
(redundant_conj_level
,4);
232 $1->print_with_subs
(stdout
);
236 | TIME relation
';' {
238 #if defined(OMIT_GETRUSAGE)
239 printf
("'time' requires getrusage, but the omega calclator was compiled with OMIT_GETRUSAGE set!\n");
247 bool SKIP_FULL_CHECK
= getenv
("OC_TIMING_SKIP_FULL_CHECK");
248 ($2)->and_with_GEQ
();
250 for
(t
=1;t
<=100;t
++) {
254 int copyTime
= clock_diff
();
256 for
(t
=1;t
<=100;t
++) {
261 int simplifyTime
= clock_diff
() -copyTime
;
263 if
(!SKIP_FULL_CHECK
)
266 for
(t
=1;t
<=100;t
++) {
272 int excessiveTime
= clock_diff
() - copyTime
;
273 printf
("Times (in microseconds): \n");
274 printf
("%5d us to copy original set of constraints\n",copyTime
/100);
275 printf
("%5d us to do the default amount of simplification, obtaining: \n\t",
277 R.print_with_subs
(stdout
);
279 if
(!SKIP_FULL_CHECK
)
281 printf
("%5d us to do the maximum (i.e., excessive) amount of simplification, obtaining: \n\t",
283 R2.print_with_subs
(stdout
);
286 if
(!anyTimingDone
) {
295 printf
("WARNING: The Omega calculator was compiled with options that force\n");
296 printf
("it to perform additional consistency and error checks\n");
297 printf
("that may slow it down substantially\n");
300 printf
("NOTE: These times relect the time of the current _implementation_\n");
301 printf
("of our algorithms. Performance bugs do exist. If you intend to publish or \n");
302 printf
("report on the performance on the Omega test, we respectfully but strongly \n");
303 printf
("request that send your test cases to us to allow us to determine if the \n");
304 printf
("times are appropriate, and if the way you are using the Omega library to \n");
305 printf
("solve your problem is the most effective way.\n");
308 printf
("Also, please be aware that over the past two years, we have focused our \n");
309 printf
("efforts on the expressive power of the Omega library, sometimes at the\n");
310 printf
("expensive of raw speed. Our original implementation of the Omega test\n");
311 printf
("was substantially faster on the limited domain it handled.\n");
313 printf
(" Thanks, \n");
314 printf
(" the Omega Team \n");
316 anyTimingDone
= true
;
320 | TIMECLOSURE relation
';' {
322 #if defined(OMIT_GETRUSAGE)
323 printf
("'timeclosure' requires getrusage, but the omega calclator was compiled with OMIT_GETRUSAGE set!\n");
329 ($2)->and_with_GEQ
();
331 for
(t
=1;t
<=100;t
++) {
335 int copyTime
= clock_diff
();
337 for
(t
=1;t
<=100;t
++) {
342 int simplifyTime
= clock_diff
() -copyTime
;
345 for
(t
=1;t
<=100;t
++) {
348 Rclosed
= TransitiveClosure
(Rclosed
, 1,Relation
::Null
());
350 int closureTime
= clock_diff
() - copyTime
;
353 for
(t
=1;t
<=100;t
++) {
358 int excessiveTime
= clock_diff
() - copyTime
;
359 printf
("Times (in microseconds): \n");
360 printf
("%5d us to copy original set of constraints\n",copyTime
/100);
361 printf
("%5d us to do the default amount of simplification, obtaining: \n\t",
363 R.print_with_subs
(stdout
);
365 printf
("%5d us to do the maximum (i.e., excessive) amount of simplification, obtaining: \n\t",
367 R2.print_with_subs
(stdout
);
368 printf
("%5d us to do the transitive closure, obtaining: \n\t",
370 Rclosed.print_with_subs
(stdout
);
372 if
(!anyTimingDone
) {
381 printf
("WARNING: The Omega calculator was compiled with options that force\n");
382 printf
("it to perform additional consistency and error checks\n");
383 printf
("that may slow it down substantially\n");
386 printf
("NOTE: These times relect the time of the current _implementation_\n");
387 printf
("of our algorithms. Performance bugs do exist. If you intend to publish or \n");
388 printf
("report on the performance on the Omega test, we respectfully but strongly \n");
389 printf
("request that send your test cases to us to allow us to determine if the \n");
390 printf
("times are appropriate, and if the way you are using the Omega library to \n");
391 printf
("solve your problem is the most effective way.\n");
394 printf
("Also, please be aware that over the past two years, we have focused our \n");
395 printf
("efforts on the expressive power of the Omega library, sometimes at the\n");
396 printf
("expensive of raw speed. Our original implementation of the Omega test\n");
397 printf
("was substantially faster on the limited domain it handled.\n");
399 printf
(" Thanks, \n");
400 printf
(" the Omega Team \n");
402 anyTimingDone
= true
;
408 | relation SUBSET relation
';' {
410 int c
= Must_Be_Subset
(*$1, *$3);
411 printf
("\n%s\n", c ?
"True" : "False");
415 | CODEGEN effort relPairList context
';'
418 String s
= MMGenerateCode
($3->mappings
, $3->ispaces
,*$4,$2);
421 printf
("%s\n", (const char *) s
);
423 | TCODEGEN effort statementInfoResult context
';'
426 String s
= tcodegen
($2, *($3), *($4));
429 printf
("%s\n", (const char *) s
);
431 /* | TCODEGEN NOT effort statementInfoResult context';'
434 * String s = tcodegen($3, *($4), *($5), false);
437 * printf("%s\n", (const char *) s);
440 | SPMD blockAndProcsAndEffort relTripList
';'
442 Tuple
<Free_Var_Decl
*> lowerBounds
(0), upperBounds
(0), my_procs
(0);
443 Tuple
<spmd_stmt_info
*> names
(0);
446 int nr_statements
= $3->space.size
();
448 for
(int i
= 1; i
<= $3->space
[1].n_out
(); i
++)
450 lowerBounds.append
(new Free_Var_Decl
("lb" + itoS
(i
)));
451 upperBounds.append
(new Free_Var_Decl
("ub" + itoS
(i
)));
452 my_procs.append
(new Free_Var_Decl
("my_proc" + itoS
(i
)));
455 for
(int p
= 1; p
<= nr_statements
; p
++)
456 names.append
(new numbered_stmt_info
(p
-1, Identity
($3->time
[p
].n_out
()),
458 (char *)(const char *)("s"+itoS
(p
-1))));
460 String s
= SPMD_GenerateCode
("", $3->space
, $3->time
, $3->ispaces
,
462 lowerBounds
, upperBounds
, my_procs
,
466 printf
("%s\n", (const char *) s
);
470 Dynamic_Array1
<Relation
> &final
= *$1;
472 int i
,n_nodes
= reachable_info
->node_names.size
();
473 for
(i
= 1; i
<= n_nodes
; i
++) if
(final
[i
].is_upper_bound_satisfiable
()) {
475 fprintf
(stdout
,"Node %s: ",
476 (const char *) (reachable_info
->node_names
[i
]));
477 final
[i
].print_with_subs
(stdout
);
480 fprintf
(stdout
,"No nodes reachable.\n");
482 delete reachable_info
;
484 | CARD relation
';' {
485 evalue
*EP
= count_relation
(*$2);
487 const Variable_ID_Tuple
* globals
= $2->global_decls
();
488 const char **param_names
= new
(const char *)[globals
->size
()];
490 for
(int i
= 0; i
< globals
->size
(); ++i
)
491 param_names
[i
] = (*globals
)[i
+1]->char_name
();
492 print_evalue
(stdout
, EP
, (char**)param_names
);
494 delete
[] param_names
;
495 free_evalue_refs
(EP
);
500 | RANKING relation
';' {
501 evalue
*EP
= rank_relation
(*$2);
503 const Variable_ID_Tuple
* globals
= $2->global_decls
();
504 int nvar
= $2->n_set
();
505 int n
= nvar
+ globals
->size
();
506 const char **names
= new
(const char *)[n
];
508 for
(int i
= 0; i
< nvar
; ++i
)
509 names
[i
] = $2->set_var
(i
+1)->char_name
();
510 for
(int i
= 0; i
< globals
->size
(); ++i
)
511 names
[nvar
+i
] = (*globals
)[i
+1]->char_name
();
512 print_evalue
(stdout
, EP
, (char**)names
);
515 free_evalue_refs
(EP
);
520 | COUNT_LEXSMALLER relation WITHIN relation
';' {
521 evalue
*EP
= count_lexsmaller
(*$2, *$4);
523 const Variable_ID_Tuple
* globals
= $4->global_decls
();
524 int nvar
= $4->n_set
();
525 int n
= nvar
+ globals
->size
();
526 const char **names
= new
(const char *)[n
];
528 for
(int i
= 0; i
< nvar
; ++i
)
529 names
[i
] = $4->set_var
(i
+1)->char_name
();
530 for
(int i
= 0; i
< globals
->size
(); ++i
)
531 names
[nvar
+i
] = (*globals
)[i
+1]->char_name
();
532 print_evalue
(stdout
, EP
, (char**)names
);
535 free_evalue_refs
(EP
);
541 | VERTICES relation
';' {
546 { relationDecl
= new Declaration_Site
(); }
548 maximize
($3, variableMap
);
550 current_Declaration_Site
= globalDecls
;
555 relTripList: relTripList
',' relation
':' relation
':' relation
557 $1->space.append
(*$3);
558 $1->time.append
(*$5);
559 $1->ispaces.append
(*$7);
565 | relation
':' relation
':' relation
567 RelTupleTriple
*rtt
= new RelTupleTriple
;
568 rtt
->space.append
(*$1);
569 rtt
->time.append
(*$3);
570 rtt
->ispaces.append
(*$5);
578 blockAndProcsAndEffort
: { Block_Size
= 0; Num_Procs
= 0; overheadEffort
=0; }
579 | INT
{ Block_Size
= $1; Num_Procs
= 0; overheadEffort
=0;}
580 | INT INT
{ Block_Size
= $1; Num_Procs
= $2; overheadEffort
=0;}
581 | INT INT INT
{ Block_Size
= $1; Num_Procs
= $2; overheadEffort
=$3;}
586 |
'-' INT
{ $$
= -$2; }
589 context
: { $$
= new Relation
();
590 *$$
= Relation
::Null
(); }
591 | GIVEN relation
{$$
= $2; }
594 relPairList: relPairList
',' relation
':' relation
596 $1->mappings.append
(*$3);
597 $1->mappings
[$1->mappings.size
()].compress
();
598 $1->ispaces.append
(*$5);
599 $1->ispaces
[$1->ispaces.size
()].compress
();
604 | relPairList
',' relation
606 $1->mappings.append
(Identity
($3->n_set
()));
607 $1->mappings
[$1->mappings.size
()].compress
();
608 $1->ispaces.append
(*$3);
609 $1->ispaces
[$1->ispaces.size
()].compress
();
613 | relation
':' relation
615 RelTuplePair
*rtp
= new RelTuplePair
;
616 rtp
->mappings.append
(*$1);
617 rtp
->mappings
[rtp
->mappings.size
()].compress
();
618 rtp
->ispaces.append
(*$3);
619 rtp
->ispaces
[rtp
->ispaces.size
()].compress
();
626 RelTuplePair
*rtp
= new RelTuplePair
;
627 rtp
->mappings.append
(Identity
($1->n_set
()));
628 rtp
->mappings
[rtp
->mappings.size
()].compress
();
629 rtp
->ispaces.append
(*$1);
630 rtp
->ispaces
[rtp
->ispaces.size
()].compress
();
636 statementInfoResult
: statementInfoList
638 /* | ASSERT_UNSAT statementInfoResult
640 * DoDebug2("Debug info requested in input", *($2));
643 | TRANS_IS relation statementInfoResult
644 { $$
= &Trans_IS
(*($3), *($2));
647 | SET_MMAP INT partialwrites statementInfoResult
648 { $$
= &Set_MMap
(*($4), $2, *($3));
651 | UNROLL_IS INT INT INT statementInfoResult
652 { $$
= &Unroll_One_IS
(*($5), $2, $3, $4);}
653 | PEEL_IS INT INT relation statementInfoResult
654 { $$
= &Peel_One_IS
(*($5), $2, $3, *($4));
657 | PEEL_IS INT INT relation
',' relation statementInfoResult
658 { $$
= &Peel_One_IS
(*($7), $2, $3, *($4), *($6));
664 statementInfoList
: statementInfo
{ $$
= new Tuple
<stm_info
>;
667 | statementInfoList
',' statementInfo
{ $$
= $1;
672 statementInfo
: '[' STRING
',' relation
',' partialwrites
',' reads
']'
674 $$
->stm
= *($2); delete
$2;
675 $$
->IS
= *($4); delete
$4;
676 $$
->map
= *($6); delete
$6;
678 |
'[' STRING
',' relation
',' partialwrites
']'
680 $$
->stm
= *($2); delete
$2;
681 $$
->IS
= *($4); delete
$4;
682 $$
->map
= *($6); delete
$6;
686 partialwrites
: partialwrites
',' partialwrite
688 $$
->partials.append
(*($3)); delete
$3;
690 | partialwrite
{ $$
= new MMap
;
691 $$
->partials.append
(*($1)); delete
$1;
695 partialwrite
: STRING
'[' relation
']' ',' relation
696 { $$
= new PartialMMap
;
697 $$
->mapping
= *($6); delete
$6;
698 $$
->bounds
= *($3); delete
$3;
699 $$
->var
= *($1); delete
$1;
701 | STRING
',' relation
{ $$
= new PartialMMap
;
702 $$
->mapping
= *($3); delete
$3;
703 $$
->bounds
= Relation
::True
(0);
704 $$
->var
= *($1); delete
$1;
708 reads
: reads
',' oneread
{ $$
= $1;
709 $$
->read.append
(*($3)); delete
$3;
711 | oneread
{ $$
= new stm_info
;
712 $$
->read.append
(*($1)); delete
$1;
716 oneread
: '[' partials
']' { $$
= $2; }
719 partials
: partials
',' partial
{ $$
= $1;
720 $$
->partials.append
(*($3)); delete
$3;
722 | partial
{ $$
= new Read
;
723 $$
->partials.append
(*($1)); delete
$1;
727 partial
: INT
',' relation
{ $$
= new PartialRead
;
729 $$
->dataFlow
= *($3); delete
$3;
733 globVarList: globVarList
',' globVar
737 globVar: VAR
'(' INT
')'
738 { globalDecls
->extend_both_tuples
($1, $3); free
($1); }
740 { globalDecls
->extend
($1); free
($1); }
743 polynomial
: INT
{ $$
= new GiNaC
::ex
($1); }
745 Variable_Ref
*v
= lookupScalar
($1);
748 if
(variableMap
(v
) == 0)
749 variableMap
[v
] = GiNaC
::symbol
(std
::string(v
->name
));
750 $$
= new GiNaC
::ex
(variableMap
[v
]);
752 |
'(' polynomial
')' { $$
= $2; }
753 |
'-' polynomial %prec
'*' {
754 $$
= new GiNaC
::ex
(-*$2);
757 | polynomial
'+' polynomial
{
758 $$
= new GiNaC
::ex
(*$1 + *$3);
762 | polynomial
'-' polynomial
{
763 $$
= new GiNaC
::ex
(*$1 - *$3);
767 | polynomial
'/' polynomial
{
768 $$
= new GiNaC
::ex
(*$1 / *$3);
772 | polynomial
'*' polynomial
{
773 $$
= new GiNaC
::ex
(*$1 * *$3);
779 polyfunc
: OPEN_BRACE
780 tupleDeclaration GOES_TO polynomial optionalFormula CLOSE_BRACE
{
781 Relation
*r
= build_relation
($2, $5);
790 relation
: OPEN_BRACE
791 { relationDecl
= new Declaration_Site
(); }
795 if
(omega_calc_debug
) {
796 fprintf
(DebugFile
,"Built relation:\n");
797 $$
->prefix_print
(DebugFile
);
799 current_Declaration_Site
= globalDecls
;
804 if
(relationMap
(s
) == 0) {
805 fprintf
(stderr
,"Variable %s not declared\n",$1);
811 $$
= new Relation
(*relationMap
(s
));
813 |
'(' relation
')' {$$
= $2;}
814 | relation
'+' %prec OMEGA_P9
815 { $$
= new Relation
();
816 *$$
= TransitiveClosure
(*$1, 1,Relation
::Null
());
819 | relation
'*' %prec OMEGA_P9
820 { $$
= new Relation
();
821 int vars
= $1->n_inp
();
822 *$$
= Union
(Identity
(vars
),
823 TransitiveClosure
(*$1, 1,Relation
::Null
()));
826 | relation
'+' WITHIN relation %prec OMEGA_P9
827 {$$
= new Relation
();
828 *$$
= TransitiveClosure
(*$1, 1,*$4);
832 | MINIMIZE_RANGE relation %prec OMEGA_P8
836 r
= Join
(r
,LexForward
($2->n_out
()));
838 *$$
= Difference
(o
,r
);
841 | MAXIMIZE_RANGE relation %prec OMEGA_P8
845 r
= Join
(r
,Inverse
(LexForward
($2->n_out
())));
847 *$$
= Difference
(o
,r
);
850 | MINIMIZE_DOMAIN relation %prec OMEGA_P8
854 r
= Join
(LexForward
($2->n_inp
()),r
);
856 *$$
= Difference
(o
,r
);
859 | MAXIMIZE_DOMAIN relation %prec OMEGA_P8
863 r
= Join
(Inverse
(LexForward
($2->n_inp
())),r
);
865 *$$
= Difference
(o
,r
);
868 | MAXIMIZE relation %prec OMEGA_P8
873 *$$
= Cross_Product
(Relation
(*$2),c
);
875 assert
($$
->n_inp
() ==$$
->n_out
());
876 *$$
= Difference
(r
,Domain
(Intersection
(*$$
,LexForward
($$
->n_inp
()))));
878 | MINIMIZE relation %prec OMEGA_P8
883 *$$
= Cross_Product
(Relation
(*$2),c
);
885 assert
($$
->n_inp
() ==$$
->n_out
());
886 *$$
= Difference
(r
,Range
(Intersection
(*$$
,LexForward
($$
->n_inp
()))));
888 | FARKAS relation %prec OMEGA_P8
891 *$$
= Farkas
(*$2, Basic_Farkas
);
894 | DECOUPLED_FARKAS relation %prec OMEGA_P8
897 *$$
= Farkas
(*$2, Decoupled_Farkas
);
900 | relation
'@' %prec OMEGA_P9
901 { $$
= new Relation
();
902 *$$
= ConicClosure
(*$1);
905 | PROJECT_AWAY_SYMBOLS relation %prec OMEGA_P8
906 { $$
= new Relation
();
907 *$$
= Project_Sym
(*$2);
910 | PROJECT_ON_SYMBOLS relation %prec OMEGA_P8
911 { $$
= new Relation
();
912 *$$
= Project_On_Sym
(*$2);
915 | DIFFERENCE relation %prec OMEGA_P8
916 { $$
= new Relation
();
920 | DIFFERENCE_TO_RELATION relation %prec OMEGA_P8
921 { $$
= new Relation
();
922 *$$
= DeltasToRelation
(*$2,$2->n_set
(),$2->n_set
());
925 | OMEGA_DOMAIN relation %prec OMEGA_P8
926 { $$
= new Relation
();
930 | VENN relation %prec OMEGA_P8
931 { $$
= new Relation
();
932 *$$
= VennDiagramForm
(*$2,Relation
::True
(*$2));
935 | VENN relation GIVEN relation %prec OMEGA_P8
936 { $$
= new Relation
();
937 *$$
= VennDiagramForm
(*$2,*$4);
941 | CONVEX_HULL relation %prec OMEGA_P8
942 { $$
= new Relation
();
943 *$$
= ConvexHull
(*$2);
946 | POSITIVE_COMBINATION relation %prec OMEGA_P8
947 { $$
= new Relation
();
948 *$$
= Farkas
(*$2,Positive_Combination_Farkas
);
951 | CONVEX_COMBINATION relation %prec OMEGA_P8
952 { $$
= new Relation
();
953 *$$
= Farkas
(*$2,Convex_Combination_Farkas
);
956 | PAIRWISE_CHECK relation %prec OMEGA_P8
957 { $$
= new Relation
();
958 *$$
= CheckForConvexRepresentation
(CheckForConvexPairs
(*$2));
961 | CONVEX_CHECK relation %prec OMEGA_P8
962 { $$
= new Relation
();
963 *$$
= CheckForConvexRepresentation
(*$2);
966 | AFFINE_HULL relation %prec OMEGA_P8
967 { $$
= new Relation
();
968 *$$
= AffineHull
(*$2);
971 | CONIC_HULL relation %prec OMEGA_P8
972 { $$
= new Relation
();
973 *$$
= ConicHull
(*$2);
976 | LINEAR_HULL relation %prec OMEGA_P8
977 { $$
= new Relation
();
978 *$$
= LinearHull
(*$2);
981 | HULL relation %prec OMEGA_P8
982 { $$
= new Relation
();
983 *$$
= Hull
(*$2,false
,1,Null_Relation
());
986 | HULL relation GIVEN relation %prec OMEGA_P8
987 { $$
= new Relation
();
988 *$$
= Hull
(*$2,false
,1,*$4);
992 | APPROX relation %prec OMEGA_P8
993 { $$
= new Relation
();
994 *$$
= Approximate
(*$2);
997 | RANGE relation %prec OMEGA_P8
998 { $$
= new Relation
();
1002 | INVERSE relation %prec OMEGA_P8
1003 { $$
= new Relation
();
1007 | COMPLEMENT relation %prec OMEGA_P8
1008 { $$
= new Relation
();
1009 *$$
= Complement
(*$2);
1012 | GIST relation GIVEN relation %prec OMEGA_P8
1013 { $$
= new Relation
();
1014 *$$
= Gist
(*$2,*$4,1);
1018 | relation
'(' relation
')'
1019 { $$
= new Relation
();
1020 *$$
= Composition
(*$1,*$3);
1024 | relation COMPOSE relation
1025 { $$
= new Relation
();
1026 *$$
= Composition
(*$1,*$3);
1030 | relation CARRIED_BY INT
1031 { $$
= new Relation
();
1032 *$$
= After
(*$1,$3,$3);
1034 (*$$
).prefix_print
(stdout
);
1036 | relation JOIN relation
1037 { $$
= new Relation
();
1038 *$$
= Composition
(*$3,*$1);
1042 | relation RESTRICT_RANGE relation
1043 { $$
= new Relation
();
1044 *$$
= Restrict_Range
(*$1,*$3);
1048 | relation RESTRICT_DOMAIN relation
1049 { $$
= new Relation
();
1050 *$$
= Restrict_Domain
(*$1,*$3);
1054 | relation INTERSECTION relation
1055 { $$
= new Relation
();
1056 *$$
= Intersection
(*$1,*$3);
1060 | relation
'-' relation %prec INTERSECTION
1061 { $$
= new Relation
();
1062 *$$
= Difference
(*$1,*$3);
1066 | relation UNION relation
1067 { $$
= new Relation
();
1068 *$$
= Union
(*$1,*$3);
1072 | relation
'*' relation
1073 { $$
= new Relation
();
1074 *$$
= Cross_Product
(*$1,*$3);
1078 | SUPERSETOF relation
1079 { $$
= new Relation
();
1080 *$$
= Union
(*$2, Relation
::Unknown
(*$2));
1084 { $$
= new Relation
();
1085 *$$
= Intersection
(*$2, Relation
::Unknown
(*$2));
1088 | MAKE_UPPER_BOUND relation %prec OMEGA_P8
1089 { $$
= new Relation
();
1090 *$$
= Upper_Bound
(*$2);
1093 | MAKE_LOWER_BOUND relation %prec OMEGA_P8
1094 { $$
= new Relation
();
1095 *$$
= Lower_Bound
(*$2);
1099 { $$
= new Relation
();
1100 *$$
= Sample_Solution
(*$2);
1103 | SYM_SAMPLE relation
1104 { $$
= new Relation
();
1105 *$$
= Symbolic_Solution
(*$2);
1108 | reachable_of
{ $$
= $1; }
1109 | ASSERT_UNSAT relation
1111 if
(($2)->is_satisfiable
())
1113 fprintf
(stderr
,"assert_unsatisfiable failed on ");
1114 ($2)->print_with_subs
(stderr
);
1123 tupleDeclaration GOES_TO
{currentTuple
= Output_Tuple
;}
1124 tupleDeclaration
{currentTuple
= Input_Tuple
;} optionalFormula
{
1125 Relation
* r
= new Relation
($1->size
,$4->size
);
1127 F_And
*f
= r
->add_and
();
1129 for
(i
=1;i
<=$1->size
;i
++) {
1130 $1->vars
[i
]->vid
= r
->input_var
(i
);
1131 if
(!$1->vars
[i
]->anonymous
)
1132 r
->name_input_var
(i
,$1->vars
[i
]->stripped_name
);
1134 for
(i
=1;i
<=$4->size
;i
++) {
1135 $4->vars
[i
]->vid
= r
->output_var
(i
);
1136 if
(!$4->vars
[i
]->anonymous
)
1137 r
->name_output_var
(i
,$4->vars
[i
]->stripped_name
);
1139 foreach
(e
,Exp
*,$1->eq_constraints
, install_eq
(f
,e
,0));
1140 foreach
(e
,Exp
*,$1->geq_constraints
, install_geq
(f
,e
,0));
1141 foreach
(c
,strideConstraint
*,$1->stride_constraints
, install_stride
(f
,c
));
1142 foreach
(e
,Exp
*,$4->eq_constraints
, install_eq
(f
,e
,0));
1143 foreach
(e
,Exp
*,$4->geq_constraints
, install_geq
(f
,e
,0));
1144 foreach
(c
,strideConstraint
*,$4->stride_constraints
, install_stride
(f
,c
));
1145 if
($6) $6->install
(f
);
1150 | tupleDeclaration optionalFormula
{
1151 $$
= build_relation
($1, $2);
1154 Relation
* r
= new Relation
(0,0);
1155 F_And
*f
= r
->add_and
();
1162 optionalFormula
: formula_sep formula
{ $$
= $2; }
1173 if
(currentTupleDescriptor
)
1174 delete currentTupleDescriptor
;
1175 currentTupleDescriptor
= new tupleDescriptor
;
1178 '[' optionalTupleVarList
']'
1179 {$$
= currentTupleDescriptor
; currentTupleDescriptor
= NULL
; }
1182 optionalTupleVarList
:
1184 | optionalTupleVarList
',' tupleVar
1188 tupleVar
: VAR %prec OMEGA_P10
1189 { Declaration_Site
*ds
= defined
($1);
1190 if
(!ds
) currentTupleDescriptor
->extend
($1,currentTuple
,tuplePos
);
1192 Variable_Ref
* v
= lookupScalar
($1);
1194 if
(ds
!= globalDecls
)
1195 currentTupleDescriptor
->extend
($1, new Exp
(v
));
1197 currentTupleDescriptor
->extend
(new Exp
(v
));
1203 {currentTupleDescriptor
->extend
(); tuplePos
++; }
1204 | exp %prec OMEGA_P1
1205 {currentTupleDescriptor
->extend
($1); tuplePos
++; }
1206 | exp
':' exp %prec OMEGA_P1
1207 {currentTupleDescriptor
->extend
($1,$3); tuplePos
++; }
1208 | exp
':' exp
':' INT %prec OMEGA_P1
1209 {currentTupleDescriptor
->extend
($1,$3,$5); tuplePos
++; }
1213 varList: varList
',' VAR
{$$
= $1; $$
->insert
($3); }
1214 | VAR
{ $$
= new VarList
;
1220 $$
= current_Declaration_Site
= new Declaration_Site
($1);
1221 foreach
(s
,char *, *$1, delete s
);
1226 /* variable declaration with optional brackets */
1228 varDeclOptBrackets
: varDecl
{ $$
= $1; }
1229 |
'[' varDecl
']' { $$
= $2; }
1232 formula
: formula AND formula
{ $$
= new AST_And
($1,$3); }
1233 | formula OR formula
{ $$
= new AST_Or
($1,$3); }
1234 | constraintChain
{ $$
= $1; }
1235 |
'(' formula
')' { $$
= $2; }
1236 | NOT formula
{ $$
= new AST_Not
($2); }
1237 | start_exists varDeclOptBrackets exists_sep formula end_quant
1238 { $$
= new AST_exists
($2,$4); }
1239 | start_forall varDeclOptBrackets forall_sep formula end_quant
1240 { $$
= new AST_forall
($2,$4); }
1243 start_exists
: '(' EXISTS
1252 start_forall
: '(' FORALL
1263 expList
: exp
',' expList
1274 constraintChain
: expList REL_OP expList
1275 { $$
= new AST_constraints
($1,$2,$3); }
1276 | expList REL_OP constraintChain
1277 { $$
= new AST_constraints
($1,$2,$3); }
1282 { Variable_Ref
* v
= lookupScalar
($1);
1287 | VAR
'(' {argCount
= 1;} argumentList
')' %prec OMEGA_P9
1289 if
($4 == Input_Tuple
) v
= functionOfInput
[$1];
1290 else v
= functionOfOutput
[$1];
1292 fprintf
(stderr
,"Function %s(...) not declared\n",$1);
1299 |
'(' exp
')' { $$
= $2;}
1305 argumentList
',' VAR
{
1306 Variable_Ref
* v
= lookupScalar
($3);
1309 if
(v
->pos
!= argCount || v
->of
!= $1 || v
->of
!= Input_Tuple
&& v
->of
!= Output_Tuple
) {
1310 fprintf
(stderr
,"arguments to function must be prefix of input or output tuple\n");
1316 | VAR
{ Variable_Ref
* v
= lookupScalar
($1);
1319 if
(v
->pos
!= argCount || v
->of
!= Input_Tuple
&& v
->of
!= Output_Tuple
) {
1320 fprintf
(stderr
,"arguments to function must be prefix of input or output tuple\n");
1328 exp
: INT
{$$
= new Exp
($1);}
1329 | INT simpleExp %prec
'*' {$$
= multiply
($1,$2);}
1330 | simpleExp
{ $$
= $1; }
1331 |
'-' exp %prec
'*' { $$
= negate
($2);}
1332 | exp
'+' exp
{ $$
= add
($1,$3);}
1333 | exp
'-' exp
{ $$
= subtract
($1,$3);}
1334 | exp
'*' exp
{ $$
= multiply
($1,$3);}
1339 REACHABLE_FROM nodeNameList nodeSpecificationList
1341 Dynamic_Array1
<Relation
> *final
=
1342 Reachable_Nodes
(reachable_info
);
1348 REACHABLE_OF VAR IN nodeNameList nodeSpecificationList
1350 Dynamic_Array1
<Relation
> *final
=
1351 Reachable_Nodes
(reachable_info
);
1352 int index
= reachable_info
->node_names.index
(String
($2));
1353 assert
(index
!= 0 && "No such node");
1355 *$$
= (*final
)[index
];
1357 delete reachable_info
;
1363 nodeNameList: '(' realNodeNameList
')'
1364 { int sz
= reachable_info
->node_names.size
();
1365 reachable_info
->node_arity.reallocate
(sz
);
1366 reachable_info
->transitions.resize
(sz
+1,sz
+1);
1367 reachable_info
->start_nodes.resize
(sz
+1);
1371 realNodeNameList: realNodeNameList
',' VAR
1372 { reachable_info
->node_names.append
(String
($3));
1374 | VAR
{ reachable_info
= new reachable_information
;
1375 reachable_info
->node_names.append
(String
($1));
1380 nodeSpecificationList: OPEN_BRACE realNodeSpecificationList CLOSE_BRACE
1383 int n_nodes
= reachable_info
->node_names.size
();
1384 Tuple
<int> &arity
= reachable_info
->node_arity
;
1385 Dynamic_Array2
<Relation
> &transitions
= reachable_info
->transitions
;
1387 /* fixup unspecified transitions to be false */
1389 for
(i
= 1; i
<= n_nodes
; i
++) arity
[i
] = -1;
1390 for
(i
= 1; i
<= n_nodes
; i
++)
1391 for
(j
= 1; j
<= n_nodes
; j
++)
1392 if
(! transitions
[i
][j
].is_null
()) {
1393 int in_arity
= transitions
[i
][j
].n_inp
();
1394 int out_arity
= transitions
[i
][j
].n_out
();
1395 if
(arity
[i
] < 0) arity
[i
] = in_arity
;
1396 if
(arity
[j
] < 0) arity
[j
] = out_arity
;
1397 if
(in_arity
!= arity
[i
] || out_arity
!= arity
[j
]) {
1399 "Arity mismatch in node transition: %s -> %s",
1400 (const char *) reachable_info
->node_names
[i
],
1401 (const char *) reachable_info
->node_names
[j
]);
1406 for
(i
= 1; i
<= n_nodes
; i
++)
1407 if
(arity
[i
] < 0) arity
[i
] = 0;
1408 /* Fill in false relations */
1409 for
(i
= 1; i
<= n_nodes
; i
++)
1410 for
(j
= 1; j
<= n_nodes
; j
++)
1411 if
(transitions
[i
][j
].is_null
())
1412 transitions
[i
][j
] = Relation
::False
(arity
[i
],arity
[j
]);
1415 /* fixup unused start node positions */
1416 Dynamic_Array1
<Relation
> &nodes
= reachable_info
->start_nodes
;
1417 for
(i
= 1; i
<= n_nodes
; i
++)
1418 if
(nodes
[i
].is_null
())
1419 nodes
[i
] = Relation
::False
(arity
[i
]);
1421 if
(nodes
[i
].n_set
() != arity
[i
]){
1422 fprintf
(stderr
,"Arity mismatch in start node %s",
1423 (const char *) reachable_info
->node_names
[i
]);
1430 realNodeSpecificationList:
1431 realNodeSpecificationList
',' VAR
':' relation
1432 { int n_nodes
= reachable_info
->node_names.size
();
1433 int index
= reachable_info
->node_names.index
($3);
1434 assert
(index
!= 0 && index
<= n_nodes
);
1435 reachable_info
->start_nodes
[index
] = *$5;
1439 | realNodeSpecificationList
',' VAR GOES_TO VAR
':' relation
1440 { int n_nodes
= reachable_info
->node_names.size
();
1441 int from_index
= reachable_info
->node_names.index
($3);
1442 int to_index
= reachable_info
->node_names.index
($5);
1443 assert
(from_index
!= 0 && to_index
!= 0);
1444 assert
(from_index
<= n_nodes
&& to_index
<= n_nodes
);
1445 reachable_info
->transitions
[from_index
][to_index
] = *$7;
1450 | VAR GOES_TO VAR
':' relation
1451 { int n_nodes
= reachable_info
->node_names.size
();
1452 int from_index
= reachable_info
->node_names.index
($1);
1453 int to_index
= reachable_info
->node_names.index
($3);
1454 assert
(from_index
!= 0 && to_index
!= 0);
1455 assert
(from_index
<= n_nodes
&& to_index
<= n_nodes
);
1456 reachable_info
->transitions
[from_index
][to_index
] = *$5;
1462 { int n_nodes
= reachable_info
->node_names.size
();
1463 int index
= reachable_info
->node_names.index
($1);
1464 assert
(index
!= 0 && index
<= n_nodes
);
1465 reachable_info
->start_nodes
[index
] = *$3;
1473 #if !defined(OMIT_GETRUSAGE)
1474 #include <sys/types.h>
1475 #include <sys/time.h>
1476 #include <sys/resource.h>
1478 struct rusage start_time
;
1481 #if defined BRAIN_DAMAGED_FREE
1487 void *realloc
(void *p
, size_t s
)
1489 return realloc
((malloc_t
) p
, s
);
1493 #if ! defined(OMIT_GETRUSAGE)
1495 extern
"C" int getrusage
(int, struct rusage
*);
1498 void start_clock
( void )
1500 getrusage
(RUSAGE_SELF
, &start_time
);
1503 int clock_diff
( void )
1505 struct rusage current_time
;
1506 getrusage
(RUSAGE_SELF
, ¤t_time
);
1507 return
(current_time.ru_utime.tv_sec
-start_time.ru_utime.tv_sec
)*1000000 +
1508 (current_time.ru_utime.tv_usec
-start_time.ru_utime.tv_usec
);
1512 void printUsage
(FILE *outf
, char **argv
) {
1513 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
);
1516 int omega_calc_debug
;
1519 int main
(int argc
, char **argv
){
1520 redundant_conj_level
= 2;
1521 current_Declaration_Site
= globalDecls
= new Global_Declaration_Site
();
1526 char * fileName
= 0;
1528 printf
("# %s (based on %s, %s):\n",CALC_VERSION_STRING
, Omega_Library_Version
, Omega_Library_Date
);
1530 calc_all_debugging_off
();
1533 DebugFile
= fopen
("/dev/null","w");
1536 DebugFile
= fopen
(DEBUG_FILE_NAME
, "w");
1538 fprintf
(stderr
, "Can't open debug file %s\n", DEBUG_FILE_NAME
);
1541 setbuf
(DebugFile
,0);
1544 closure_presburger_debug
= 0;
1546 setOutputFile
(DebugFile
);
1549 for
(i
=1; i
<argc
; i
++) {
1550 if
(argv
[i
][0] == '-') {
1552 while
((c
=argv
[i
][j
++]) != 0) {
1555 if
(! process_calc_debugging_flags
(argv
[i
],j
)) {
1556 printUsage
(stderr
,argv
);
1562 fprintf
(stderr
,"Note: specifying number of GEQ's is no longer useful.\n");
1563 while
(argv
[i
][j
] != 0) j
++;
1568 fprintf
(stderr
,"Note: specifying number of EQ's is no longer useful.\n");
1569 while
(argv
[i
][j
] != 0) j
++;
1573 redundant_conj_level
= 1;
1575 // Other future options go here
1577 fprintf
(stderr
, "\nUnknown flag -%c\n", c
);
1578 printUsage
(stderr
,argv
);
1584 // Make sure this is a file name
1586 fprintf
(stderr
,"\nCan only handle a single input file\n");
1587 printUsage
(stderr
,argv
);
1591 yyin
= fopen
(fileName
, "r");
1593 fprintf
(stderr
, "\nCan't open input file %s\n",fileName
);
1594 printUsage
(stderr
,argv
);
1602 initializeScanBuffer
();
1603 currentTupleDescriptor
= NULL
;
1608 foreach_map
(cs
,Const_String
,r
,Relation
*,relationMap
,
1609 {delete r
; relationMap
[cs
]=0;});
1615 Relation LexForward
(int n
) {
1617 F_Or
*f
= r.add_or
();
1618 for
(int i
=1; i
<= n
; i
++) {
1619 F_And
*g
= f
->add_and
();
1620 for
(int j
=1;j
<i
;j
++) {
1621 EQ_Handle e
= g
->add_EQ
();
1622 e.update_coef
(r.input_var
(j
),-1);
1623 e.update_coef
(r.output_var
(j
),1);
1626 GEQ_Handle e
= g
->add_GEQ
();
1627 e.update_coef
(r.input_var
(i
),-1);
1628 e.update_coef
(r.output_var
(i
),1);