stage3/constant_folding.cc
changeset 965 c9eeb67ba939
parent 964 5f4dfe6670da
child 967 544ff4dff04f
equal deleted inserted replaced
964:5f4dfe6670da 965:c9eeb67ba939
   184 
   184 
   185 
   185 
   186 
   186 
   187 
   187 
   188 
   188 
   189 #define SET_CVALUE(dtype, symbol, new_value) {((symbol)->const_value._##dtype.value) = new_value; ((symbol)->const_value._##dtype.status) = symbol_c::cs_const_value;}
   189 #define SET_CVALUE(dtype, symbol, new_value)  ((symbol)->const_value._##dtype.set(new_value))
   190 #define GET_CVALUE(dtype, symbol)             ((symbol)->const_value._##dtype.value)
   190 #define GET_CVALUE(dtype, symbol)             ((symbol)->const_value._##dtype.get())
   191 #define SET_OVFLOW(dtype, symbol)             ((symbol)->const_value._##dtype.status) = symbol_c::cs_overflow
   191 #define SET_OVFLOW(dtype, symbol)             ((symbol)->const_value._##dtype.set_overflow())
   192 #define SET_NONCONST(dtype, symbol)           ((symbol)->const_value._##dtype.status) = symbol_c::cs_non_const
   192 #define SET_NONCONST(dtype, symbol)           ((symbol)->const_value._##dtype.set_nonconst())
   193 
   193 
   194 #define VALID_CVALUE(dtype, symbol)           (symbol_c::cs_const_value == (symbol)->const_value._##dtype.status)
   194 #define VALID_CVALUE(dtype, symbol)           ((symbol)->const_value._##dtype.is_valid())
   195 #define IS_OVFLOW(dtype, symbol)              (symbol_c::cs_overflow    == (symbol)->const_value._##dtype.status)
   195 #define IS_OVFLOW(dtype, symbol)              ((symbol)->const_value._##dtype.is_overflow())
   196 #define IS_NONCONST(dtype, symbol)            (symbol_c::cs_non_const   == (symbol)->const_value._##dtype.status)
   196 #define IS_NONCONST(dtype, symbol)            ((symbol)->const_value._##dtype.is_nonconst())
   197 #define ISZERO_CVALUE(dtype, symbol)          ((VALID_CVALUE(dtype, symbol)) && (GET_CVALUE(dtype, symbol) == 0))
   197 #define ISZERO_CVALUE(dtype, symbol)          ((symbol)->const_value._##dtype.is_zero())
       
   198 
   198 
   199 
   199 #define ISEQUAL_CVALUE(dtype, symbol1, symbol2) \
   200 #define ISEQUAL_CVALUE(dtype, symbol1, symbol2) \
   200 	(VALID_CVALUE(dtype, symbol1) && VALID_CVALUE(dtype, symbol2) && (GET_CVALUE(dtype, symbol1) == GET_CVALUE(dtype, symbol2))) 
   201 	(VALID_CVALUE(dtype, symbol1) && VALID_CVALUE(dtype, symbol2) && (GET_CVALUE(dtype, symbol1) == GET_CVALUE(dtype, symbol2))) 
   201 
   202 
   202 #define DO_BINARY_OPER(oper_type, operation, res_type, operand1, operand2) {                                              \
   203 #define DO_BINARY_OPER(oper_type, operation, res_type, operand1, operand2) {                                              \
   223  * - any * non_const = non_const
   224  * - any * non_const = non_const
   224  * - constant * constant = constant  (if equal)
   225  * - constant * constant = constant  (if equal)
   225  * - constant * constant = non_const (if not equal)
   226  * - constant * constant = non_const (if not equal)
   226  */
   227  */
   227 #define COMPUTE_MEET_SEMILATTICE(dtype, c1, c2, resValue) {\
   228 #define COMPUTE_MEET_SEMILATTICE(dtype, c1, c2, resValue) {\
   228 		if (( c1._##dtype.value  != c2._##dtype.value && c2._##dtype.status == symbol_c::cs_const_value &&\
   229 		if (( c1._##dtype.get()  != c2._##dtype.get() && c2._##dtype.is_valid() && c1._##dtype.is_valid()) ||\
   229               c1._##dtype.status == symbol_c::cs_const_value) ||\
   230 		    ( c1._##dtype.is_nonconst() && c2._##dtype.is_valid() ) ||\
   230 		    ( c1._##dtype.status == symbol_c::cs_non_const && c2._##dtype.status == symbol_c::cs_const_value ) ||\
   231 		    ( c2._##dtype.is_nonconst() && c1._##dtype.is_valid() )) {\
   231 		    ( c2._##dtype.status == symbol_c::cs_non_const && c1._##dtype.status == symbol_c::cs_const_value  )) {\
   232 			resValue._##dtype.set_nonconst();\
   232 			resValue._##dtype.status = symbol_c::cs_non_const;\
       
   233 		} else {\
   233 		} else {\
   234 			resValue._##dtype.status = symbol_c::cs_const_value;\
   234 			resValue._##dtype.set(c1._##dtype.get());\
   235 			resValue._##dtype.value  = c1._##dtype.value;\
       
   236 		}\
   235 		}\
   237 }
   236 }
   238 
   237 
   239 typedef std::map <std::string, symbol_c::const_value_t> map_values_t;
   238 typedef std::map <std::string, const_value_c> map_values_t;
   240 
   239 
   241 static map_values_t values;
   240 static map_values_t values;
   242 
   241 
   243 
   242 
   244 
   243 
   732 	map_values_t ret;
   731 	map_values_t ret;
   733 
   732 
   734 	itr = m1.begin();
   733 	itr = m1.begin();
   735 	for ( ; itr != m1.end(); ++itr) {
   734 	for ( ; itr != m1.end(); ++itr) {
   736 		std::string name = itr->first;
   735 		std::string name = itr->first;
   737 		symbol_c::const_value_t value;
   736 		const_value_c value;
   738 
   737 
   739 		if (m2.count(name) > 0) {
   738 		if (m2.count(name) > 0) {
   740 			symbol_c::const_value_t c1 = itr->second;
   739 			const_value_c c1 = itr->second;
   741 			symbol_c::const_value_t c2 = m2[name];
   740 			const_value_c c2 = m2[name];
   742 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
   741 			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
   743 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
   742 			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
   744 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
   743 			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
   745 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
   744 			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
   746 		} else
   745 		} else
  1579 
  1578 
  1580 	values_incoming = values; /* save incoming status */
  1579 	values_incoming = values; /* save incoming status */
  1581 	symbol->beg_expression->accept(*this);
  1580 	symbol->beg_expression->accept(*this);
  1582 	symbol->end_expression->accept(*this);
  1581 	symbol->end_expression->accept(*this);
  1583 	varName =  get_var_name_c::get_name(symbol->control_variable)->value;
  1582 	varName =  get_var_name_c::get_name(symbol->control_variable)->value;
  1584 	values[varName]._int64.status = symbol_c::cs_non_const;
  1583 	values[varName]._int64.status = const_value_c::cs_non_const;
  1585 
  1584 
  1586 	/* Optimize dead code */
  1585 	/* Optimize dead code */
  1587 	if (NULL != symbol->by_expression) {
  1586 	if (NULL != symbol->by_expression) {
  1588 		symbol->by_expression->accept(*this);
  1587 		symbol->by_expression->accept(*this);
  1589 		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) > 0 &&
  1588 		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) > 0 &&