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
|
#include <algorithm>
#include "fpconfig.hh"
#include "fparser.hh"
#include "extrasrc/fptypes.hh"
#ifdef FP_SUPPORT_OPTIMIZER
#include "codetree.hh"
#include "optimize.hh"
#include "consts.hh"
#include <assert.h>
#include "rangeestimation.hh"
#include "constantfolding.hh"
#include "logic_boolgroups.hh"
/* ^For ConstantFolding_AndLogic()
* ConstantFolding_OrLogic()
* ConstantFolding_MulLogicItems()
* ConstantFolding_AddLogicItems()
*/
#include "logic_collections.hh"
/* ^For ConstantFolding_MulGrouping()
* ConstantFolding_AddGrouping()
*/
#include "logic_ifoperations.hh"
/* ^For ConstantFolding_IfOperations()
*/
#include "logic_powoperations.hh"
/* ^For ConstantFolding_PowOperations()
*/
#include "logic_comparisons.hh"
/* ^For ConstantFolding_Comparison()
*/
#define FP_MUL_COMBINE_EXPONENTS
namespace
{
using namespace FUNCTIONPARSERTYPES;
using namespace FPoptimizer_CodeTree;
/*************************************/
/* ADOPTING SAME-TYPE CHILDREN */
/*************************************/
template<typename Value_t>
static void AdoptChildrenWithSameOpcode(CodeTree<Value_t>& tree)
{
/* If the list contains another list of the same kind, assimilate it */
#ifdef DEBUG_SUBSTITUTIONS
bool assimilated = false;
#endif
for(size_t a=tree.GetParamCount(); a-- > 0; )
if(tree.GetParam(a).GetOpcode() == tree.GetOpcode())
{
#ifdef DEBUG_SUBSTITUTIONS
if(!assimilated)
{
std::cout << "Before assimilation: "; DumpTree(tree);
std::cout << "\n";
assimilated = true;
}
#endif
// Assimilate its children and remove it
tree.AddParamsMove(tree.GetParam(a).GetUniqueRef().GetParams(), a);
}
#ifdef DEBUG_SUBSTITUTIONS
if(assimilated)
{
std::cout << "After assimilation: "; DumpTree(tree);
std::cout << "\n";
}
#endif
}
}
namespace FPoptimizer_CodeTree
{
template<typename Value_t>
void ConstantFolding(CodeTree<Value_t>& tree)
{
tree.Sort(); // Otherwise "0 <= acos(x)" does not get properly optimized
#ifdef DEBUG_SUBSTITUTIONS
void* stackptr=0;
std::cout << "[" << (&stackptr) << "]Runs ConstantFolding for: ";
DumpTree(tree);
std::cout << "\n";
DumpHashes(tree); std::cout << std::flush;
#endif
if(false)
{
redo:;
tree.Sort();
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "[" << (&stackptr) << "]Re-runs ConstantFolding: ";
DumpTree(tree);
std::cout << "\n";
DumpHashes(tree);
#endif
}
// Insert here any hardcoded constant-folding optimizations
// that you want to be done whenever a new subtree is generated.
/* Not recursive. */
if(tree.GetOpcode() != cImmed)
{
range<Value_t> p = CalculateResultBoundaries(tree);
if(p.min.known && p.max.known && p.min.val == p.max.val)
{
// Replace us with this immed
tree.ReplaceWithImmed(p.min.val);
goto do_return;
}
}
if(false)
{
ReplaceTreeWithOne: tree.ReplaceWithImmed(Value_t(1)); goto do_return;
ReplaceTreeWithZero: tree.ReplaceWithImmed(Value_t(0)); goto do_return;
ReplaceTreeWithParam0:
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "Before replace: ";
std::cout << std::hex
<< '[' << tree.GetHash().hash1
<< ',' << tree.GetHash().hash2
<< ']' << std::dec;
DumpTree(tree);
std::cout << "\n";
#endif
tree.Become(tree.GetParam(0));
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "After replace: ";
std::cout << std::hex
<< '[' << tree.GetHash().hash1
<< ',' << tree.GetHash().hash2
<< ']' << std::dec;
DumpTree(tree);
std::cout << "\n";
#endif
goto redo;
}
/* Constant folding */
switch(tree.GetOpcode())
{
case cImmed:
break; // nothing to do
case VarBegin:
break; // nothing to do
case cAnd:
case cAbsAnd:
{
AdoptChildrenWithSameOpcode(tree);
bool has_nonlogical_values = false;
for(size_t a=tree.GetParamCount(); a-- > 0; )
{
if(!IsLogicalValue(tree.GetParam(a))) has_nonlogical_values = true;
switch(GetLogicalValue(tree.GetParam(a), tree.GetOpcode()==cAbsAnd))
{
case IsNever: goto ReplaceTreeWithZero;
case IsAlways: tree.DelParam(a); break; // x & y & 1 = x & y; x & 1 = !!x
case Unknown: default: ;
}
}
switch(tree.GetParamCount())
{
case 0: goto ReplaceTreeWithOne;
case 1: tree.SetOpcode(tree.GetOpcode()==cAnd ? cNotNot : cAbsNotNot); goto redo; // Replace self with the single operand
default:
if(tree.GetOpcode()==cAnd || !has_nonlogical_values)
if(ConstantFolding_AndLogic(tree)) goto redo;
}
break;
}
case cOr:
case cAbsOr:
{
AdoptChildrenWithSameOpcode(tree);
bool has_nonlogical_values = false;
for(size_t a=tree.GetParamCount(); a-- > 0; )
{
if(!IsLogicalValue(tree.GetParam(a))) has_nonlogical_values = true;
switch(GetLogicalValue(tree.GetParam(a), tree.GetOpcode()==cAbsOr))
{
case IsAlways: goto ReplaceTreeWithOne;
case IsNever: tree.DelParam(a); break;
case Unknown: default: ;
}
}
switch(tree.GetParamCount())
{
case 0: goto ReplaceTreeWithZero;
case 1: tree.SetOpcode(tree.GetOpcode()==cOr ? cNotNot : cAbsNotNot); goto redo; // Replace self with the single operand
default:
if(tree.GetOpcode()==cOr || !has_nonlogical_values)
if(ConstantFolding_OrLogic(tree)) goto redo;
}
break;
}
case cNot:
case cAbsNot:
{
unsigned opposite = 0;
switch(tree.GetParam(0).GetOpcode())
{
case cEqual: opposite = cNEqual; break;
case cNEqual: opposite = cEqual; break;
case cLess: opposite = cGreaterOrEq; break;
case cGreater: opposite = cLessOrEq; break;
case cLessOrEq: opposite = cGreater; break;
case cGreaterOrEq: opposite = cLess; break;
case cNotNot: opposite = cNot; break;
case cNot: opposite = cNotNot; break;
case cAbsNot: opposite = cAbsNotNot; break;
case cAbsNotNot: opposite = cAbsNot; break;
default: break;
}
if(opposite)
{
tree.SetOpcode(OPCODE(opposite));
tree.SetParamsMove(tree.GetParam(0).GetUniqueRef().GetParams());
goto redo;
}
// If the sub-expression evaluates to approx. zero, yield one.
// If the sub-expression evaluates to approx. nonzero, yield zero.
switch(GetLogicalValue(tree.GetParam(0), tree.GetOpcode()==cAbsNot))
{
case IsAlways: goto ReplaceTreeWithZero;
case IsNever: goto ReplaceTreeWithOne;
case Unknown: default: ;
}
if(tree.GetOpcode() == cNot && GetPositivityInfo(tree.GetParam(0)) == IsAlways)
tree.SetOpcode(cAbsNot);
if(tree.GetParam(0).GetOpcode() == cIf
|| tree.GetParam(0).GetOpcode() == cAbsIf)
{
CodeTree<Value_t> iftree = tree.GetParam(0);
const CodeTree<Value_t>& ifp1 = iftree.GetParam(1);
const CodeTree<Value_t>& ifp2 = iftree.GetParam(2);
if(ifp1.GetOpcode() == cNot
|| ifp1.GetOpcode() == cAbsNot)
{
// cNot [(cIf [x (cNot[y]) z])] -> cIf [x (cNotNot[y]) (cNot[z])]
tree.SetParam(0, iftree.GetParam(0)); // condition
CodeTree<Value_t> p1;
p1.SetOpcode(ifp1.GetOpcode()==cNot ? cNotNot : cAbsNotNot);
p1.AddParam(ifp1.GetParam(0));
p1.Rehash();
tree.AddParamMove(p1);
CodeTree<Value_t> p2;
p2.SetOpcode(tree.GetOpcode());
p2.AddParam(ifp2);
p2.Rehash();
tree.AddParamMove(p2);
tree.SetOpcode(iftree.GetOpcode());
goto redo;
}
if(ifp2.GetOpcode() == cNot
|| ifp2.GetOpcode() == cAbsNot)
{
// cNot [(cIf [x y (cNot[z])])] -> cIf [x (cNot[y]) (cNotNot[z])]
tree.SetParam(0, iftree.GetParam(0)); // condition
CodeTree<Value_t> p1;
p1.SetOpcode(tree.GetOpcode());
p1.AddParam(ifp1);
p1.Rehash();
tree.AddParamMove(p1);
CodeTree<Value_t> p2;
p2.SetOpcode(ifp2.GetOpcode()==cNot ? cNotNot : cAbsNotNot);
p2.AddParam(ifp2.GetParam(0));
p2.Rehash();
tree.AddParamMove(p2);
tree.SetOpcode(iftree.GetOpcode());
goto redo;
}
}
break;
}
case cNotNot:
case cAbsNotNot:
{
// The function of cNotNot is to protect a logical value from
// changing. If the parameter is already a logical value,
// then the cNotNot opcode is redundant.
if(IsLogicalValue(tree.GetParam(0)))
goto ReplaceTreeWithParam0;
// If the sub-expression evaluates to approx. zero, yield zero.
// If the sub-expression evaluates to approx. nonzero, yield one.
switch(GetLogicalValue(tree.GetParam(0), tree.GetOpcode()==cAbsNotNot))
{
case IsNever: goto ReplaceTreeWithZero;
case IsAlways: goto ReplaceTreeWithOne;
case Unknown: default: ;
}
if(tree.GetOpcode() == cNotNot && GetPositivityInfo(tree.GetParam(0)) == IsAlways)
tree.SetOpcode(cAbsNotNot);
if(tree.GetParam(0).GetOpcode() == cIf
|| tree.GetParam(0).GetOpcode() == cAbsIf)
{
CodeTree<Value_t> iftree = tree.GetParam(0);
const CodeTree<Value_t>& ifp1 = iftree.GetParam(1);
const CodeTree<Value_t>& ifp2 = iftree.GetParam(2);
if(ifp1.GetOpcode() == cNot
|| ifp1.GetOpcode() == cAbsNot)
{
// cNotNot [(cIf [x (cNot[y]) z])] -> cIf [x (cNot[y]) (cNotNot[z])]
tree.SetParam(0, iftree.GetParam(0)); // condition
tree.AddParam(ifp1);
CodeTree<Value_t> p2;
p2.SetOpcode(tree.GetOpcode());
p2.AddParam(ifp2);
p2.Rehash();
tree.AddParamMove(p2);
tree.SetOpcode(iftree.GetOpcode());
goto redo;
}
if(ifp2.GetOpcode() == cNot
|| ifp2.GetOpcode() == cAbsNot)
{
// cNotNot [(cIf [x y (cNot[z])])] -> cIf [x (cNotNot[y]) (cNot[z])]
tree.SetParam(0, iftree.GetParam(0)); // condition
CodeTree<Value_t> p1;
p1.SetOpcode(tree.GetOpcode());
p1.AddParam(ifp1);
p1.Rehash();
tree.AddParamMove(p1);
tree.AddParam(ifp2);
tree.SetOpcode(iftree.GetOpcode());
goto redo;
}
}
break;
}
case cIf:
case cAbsIf:
{
if(ConstantFolding_IfOperations(tree))
goto redo;
break;
}
case cMul:
{
NowWeAreMulGroup: ;
AdoptChildrenWithSameOpcode(tree);
// If one sub-expression evalutes to exact zero, yield zero.
Value_t immed_product = Value_t(1);
size_t n_immeds = 0; bool needs_resynth=false;
for(size_t a=0; a<tree.GetParamCount(); ++a)
{
if(!tree.GetParam(a).IsImmed()) continue;
// ^ Only check constant values
Value_t immed = tree.GetParam(a).GetImmed();
if(immed == Value_t(0) ) goto ReplaceTreeWithZero;
immed_product *= immed; ++n_immeds;
}
// Merge immeds.
if(n_immeds > 1 || (n_immeds == 1 && fp_equal(immed_product, Value_t(1))))
needs_resynth = true;
if(needs_resynth)
{
// delete immeds and add new ones
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "cMul: Will add new immed " << immed_product << "\n";
#endif
for(size_t a=tree.GetParamCount(); a-->0; )
if(tree.GetParam(a).IsImmed())
{
#ifdef DEBUG_SUBSTITUTIONS
std::cout << " - For that, deleting immed " << tree.GetParam(a).GetImmed();
std::cout << "\n";
#endif
tree.DelParam(a);
}
if(!fp_equal(immed_product, Value_t(1)))
tree.AddParam( CodeTreeImmed<Value_t> (immed_product) );
}
switch(tree.GetParamCount())
{
case 0: goto ReplaceTreeWithOne;
case 1: goto ReplaceTreeWithParam0; // Replace self with the single operand
default:
if(ConstantFolding_MulGrouping(tree)) goto redo;
if(ConstantFolding_MulLogicItems(tree)) goto redo;
}
break;
}
case cAdd:
{
AdoptChildrenWithSameOpcode(tree);
Value_t immed_sum = 0.0;
size_t n_immeds = 0; bool needs_resynth=false;
for(size_t a=0; a<tree.GetParamCount(); ++a)
{
if(!tree.GetParam(a).IsImmed()) continue;
// ^ Only check constant values
Value_t immed = tree.GetParam(a).GetImmed();
immed_sum += immed; ++n_immeds;
}
// Merge immeds.
if(n_immeds > 1 || (n_immeds == 1 && immed_sum == Value_t(0)))
needs_resynth = true;
if(needs_resynth)
{
// delete immeds and add new ones
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "cAdd: Will add new immed " << immed_sum << "\n";
std::cout << "In: "; DumpTree(tree);
std::cout << "\n";
#endif
for(size_t a=tree.GetParamCount(); a-->0; )
if(tree.GetParam(a).IsImmed())
{
#ifdef DEBUG_SUBSTITUTIONS
std::cout << " - For that, deleting immed " << tree.GetParam(a).GetImmed();
std::cout << "\n";
#endif
tree.DelParam(a);
}
if(!(immed_sum == Value_t(0.0)))
tree.AddParam( CodeTreeImmed<Value_t> (immed_sum) );
}
switch(tree.GetParamCount())
{
case 0: goto ReplaceTreeWithZero;
case 1: goto ReplaceTreeWithParam0; // Replace self with the single operand
default:
if(ConstantFolding_AddGrouping(tree)) goto redo;
if(ConstantFolding_AddLogicItems(tree)) goto redo;
}
break;
}
case cMin:
{
AdoptChildrenWithSameOpcode(tree);
/* Goal: If there is any pair of two operands, where
* their ranges form a disconnected set, i.e. as below:
* xxxxx
* yyyyyy
* Then remove the larger one.
*
* Algorithm: 1. figure out the smallest maximum of all operands.
* 2. eliminate all operands where their minimum is
* larger than the selected maximum.
*/
size_t preserve=0;
range<Value_t> smallest_maximum;
for(size_t a=0; a<tree.GetParamCount(); ++a)
{
while(a+1 < tree.GetParamCount() && tree.GetParam(a).IsIdenticalTo(tree.GetParam(a+1)))
tree.DelParam(a+1);
range<Value_t> p = CalculateResultBoundaries( tree.GetParam(a) );
if(p.max.known && (!smallest_maximum.max.known || (p.max.val) < smallest_maximum.max.val))
{
smallest_maximum.max.val = p.max.val;
smallest_maximum.max.known = true;
preserve=a;
} }
if(smallest_maximum.max.known)
for(size_t a=tree.GetParamCount(); a-- > 0; )
{
range<Value_t> p = CalculateResultBoundaries( tree.GetParam(a) );
if(p.min.known && a != preserve && p.min.val >= smallest_maximum.max.val)
tree.DelParam(a);
}
//fprintf(stderr, "Remains: %u\n", (unsigned)tree.GetParamCount());
if(tree.GetParamCount() == 1)
{
// Replace self with the single operand
goto ReplaceTreeWithParam0;
}
break;
}
case cMax:
{
AdoptChildrenWithSameOpcode(tree);
/* Goal: If there is any pair of two operands, where
* their ranges form a disconnected set, i.e. as below:
* xxxxx
* yyyyyy
* Then remove the smaller one.
*
* Algorithm: 1. figure out the biggest minimum of all operands.
* 2. eliminate all operands where their maximum is
* smaller than the selected minimum.
*/
size_t preserve=0;
range<Value_t> biggest_minimum;
for(size_t a=0; a<tree.GetParamCount(); ++a)
{
while(a+1 < tree.GetParamCount() && tree.GetParam(a).IsIdenticalTo(tree.GetParam(a+1)))
tree.DelParam(a+1);
range<Value_t> p = CalculateResultBoundaries( tree.GetParam(a) );
if(p.min.known && (!biggest_minimum.min.known || p.min.val > biggest_minimum.min.val))
{
biggest_minimum.min.val = p.min.val;
biggest_minimum.min.known = true;
preserve=a;
} }
if(biggest_minimum.min.known)
{
//fprintf(stderr, "Removing all where max < %g\n", biggest_minimum.min.val);
for(size_t a=tree.GetParamCount(); a-- > 0; )
{
range<Value_t> p = CalculateResultBoundaries( tree.GetParam(a) );
if(p.max.known && a != preserve && (p.max.val) < biggest_minimum.min.val)
{
//fprintf(stderr, "Removing %g\n", p.max.val);
tree.DelParam(a);
}
}
}
//fprintf(stderr, "Remains: %u\n", (unsigned)tree.GetParamCount());
if(tree.GetParamCount() == 1)
{
// Replace self with the single operand
goto ReplaceTreeWithParam0;
}
break;
}
case cEqual:
case cNEqual:
case cLess:
case cGreater:
case cLessOrEq:
case cGreaterOrEq:
if(ConstantFolding_Comparison(tree)) goto redo;
break;
case cAbs:
{
/* If we know the operand is always positive, cAbs is redundant.
* If we know the operand is always negative, use actual negation.
*/
range<Value_t> p0 = CalculateResultBoundaries( tree.GetParam(0) );
if(p0.min.known && p0.min.val >= Value_t(0.0))
goto ReplaceTreeWithParam0;
if(p0.max.known && p0.max.val <= fp_const_negativezero<Value_t>())
{
/* abs(negative) = negative*-1 */
tree.SetOpcode(cMul);
tree.AddParam( CodeTreeImmed(Value_t(1)) );
/* The caller of ConstantFolding() will do Sort() and Rehash() next.
* Thus, no need to do it here. */
/* We were changed into a cMul group. Do cMul folding. */
goto NowWeAreMulGroup;
}
/* If the operand is a cMul group, find elements
* that are always positive and always negative,
* and move them out, e.g. abs(p*n*x*y) = p*(-n)*abs(x*y)
*/
if(tree.GetParam(0).GetOpcode() == cMul)
{
const CodeTree<Value_t>& p = tree.GetParam(0);
std::vector<CodeTree<Value_t> > pos_set;
std::vector<CodeTree<Value_t> > neg_set;
for(size_t a=0; a<p.GetParamCount(); ++a)
{
p0 = CalculateResultBoundaries( p.GetParam(a) );
if(p0.min.known && p0.min.val >= Value_t(0.0))
{ pos_set.push_back(p.GetParam(a)); }
if(p0.max.known && p0.max.val <= fp_const_negativezero<Value_t>())
{ neg_set.push_back(p.GetParam(a)); }
}
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "Abs: mul group has " << pos_set.size()
<< " pos, " << neg_set.size() << "neg\n";
#endif
if(!pos_set.empty() || !neg_set.empty())
{
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "AbsReplace-Before: ";
DumpTree(tree);
std::cout << "\n" << std::flush;
DumpHashes(tree, std::cout);
#endif
CodeTree<Value_t> pclone;
pclone.SetOpcode(cMul);
for(size_t a=0; a<p.GetParamCount(); ++a)
{
p0 = CalculateResultBoundaries( p.GetParam(a) );
if((p0.min.known && p0.min.val >= Value_t(0.0))
|| (p0.max.known && p0.max.val <= fp_const_negativezero<Value_t>()))
{/*pclone.DelParam(a);*/}
else
pclone.AddParam( p.GetParam(a) );
/* Here, p*n*x*y -> x*y.
* p is saved in pos_set[]
* n is saved in neg_set[]
*/
}
pclone.Rehash();
CodeTree<Value_t> abs_mul;
abs_mul.SetOpcode(cAbs);
abs_mul.AddParamMove(pclone);
abs_mul.Rehash();
CodeTree<Value_t> mulgroup;
mulgroup.SetOpcode(cMul);
mulgroup.AddParamMove(abs_mul); // cAbs[whatever remains in p]
mulgroup.AddParamsMove(pos_set);
/* Now:
* mulgroup = p * Abs(x*y)
*/
if(!neg_set.empty())
{
if(neg_set.size() % 2)
mulgroup.AddParam( CodeTreeImmed(Value_t(-1)) );
mulgroup.AddParamsMove(neg_set);
/* Now:
* mulgroup = p * n * -1 * Abs(x*y)
*/
}
tree.Become(mulgroup);
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "AbsReplace-After: ";
DumpTree(tree, std::cout);
std::cout << "\n" << std::flush;
DumpHashes(tree, std::cout);
#endif
/* We were changed into a cMul group. Do cMul folding. */
goto NowWeAreMulGroup;
}
}
break;
}
#define HANDLE_UNARY_CONST_FUNC(funcname) \
if(tree.GetParam(0).IsImmed()) \
{ tree.ReplaceWithImmed( funcname(tree.GetParam(0).GetImmed()) ); \
goto do_return; }
case cLog:
HANDLE_UNARY_CONST_FUNC(fp_log);
if(tree.GetParam(0).GetOpcode() == cPow)
{
CodeTree<Value_t> pow = tree.GetParam(0);
if(GetPositivityInfo(pow.GetParam(0)) == IsAlways) // log(posi ^ y) = y*log(posi)
{
pow.CopyOnWrite();
pow.SetOpcode(cLog);
tree.SetOpcode(cMul);
tree.AddParamMove(pow.GetParam(1));
pow.DelParam(1);
pow.Rehash();
tree.SetParamMove(0, pow);
goto NowWeAreMulGroup;
}
if(GetEvennessInfo(pow.GetParam(1)) == IsAlways) // log(x ^ even) = even*log(abs(x))
{
pow.CopyOnWrite();
CodeTree<Value_t> abs;
abs.SetOpcode(cAbs);
abs.AddParamMove(pow.GetParam(0));
abs.Rehash();
pow.SetOpcode(cLog);
tree.SetOpcode(cMul);
pow.SetParamMove(0, abs);
tree.AddParamMove(pow.GetParam(1));
pow.DelParam(1);
pow.Rehash();
tree.SetParamMove(0, pow);
goto NowWeAreMulGroup;
}
}
else if(tree.GetParam(0).GetOpcode() == cAbs)
{
// log(abs(x^y)) = y*log(abs(x))
CodeTree<Value_t> pow = tree.GetParam(0).GetParam(0);
if(pow.GetOpcode() == cPow)
{
pow.CopyOnWrite();
CodeTree<Value_t> abs;
abs.SetOpcode(cAbs);
abs.AddParamMove(pow.GetParam(0));
abs.Rehash();
pow.SetOpcode(cLog);
tree.SetOpcode(cMul);
pow.SetParamMove(0, abs);
tree.AddParamMove(pow.GetParam(1));
pow.DelParam(1);
pow.Rehash();
tree.SetParamMove(0, pow);
goto NowWeAreMulGroup;
}
}
break;
case cAcosh: HANDLE_UNARY_CONST_FUNC(fp_acosh); break;
case cAsinh: HANDLE_UNARY_CONST_FUNC(fp_asinh); break;
case cAtanh: HANDLE_UNARY_CONST_FUNC(fp_atanh); break;
case cAcos: HANDLE_UNARY_CONST_FUNC(fp_acos); break;
case cAsin: HANDLE_UNARY_CONST_FUNC(fp_asin); break;
case cAtan: HANDLE_UNARY_CONST_FUNC(fp_atan); break;
case cCosh: HANDLE_UNARY_CONST_FUNC(fp_cosh); break;
case cSinh: HANDLE_UNARY_CONST_FUNC(fp_sinh); break;
case cTanh: HANDLE_UNARY_CONST_FUNC(fp_tanh); break;
case cSin: HANDLE_UNARY_CONST_FUNC(fp_sin); break;
case cCos: HANDLE_UNARY_CONST_FUNC(fp_cos); break;
case cTan: HANDLE_UNARY_CONST_FUNC(fp_tan); break;
case cCeil:
if(GetIntegerInfo(tree.GetParam(0)) == IsAlways) goto ReplaceTreeWithParam0;
HANDLE_UNARY_CONST_FUNC(fp_ceil); break;
case cTrunc:
if(GetIntegerInfo(tree.GetParam(0)) == IsAlways) goto ReplaceTreeWithParam0;
HANDLE_UNARY_CONST_FUNC(fp_trunc); break;
case cFloor:
if(GetIntegerInfo(tree.GetParam(0)) == IsAlways) goto ReplaceTreeWithParam0;
HANDLE_UNARY_CONST_FUNC(fp_floor); break;
case cInt:
if(GetIntegerInfo(tree.GetParam(0)) == IsAlways) goto ReplaceTreeWithParam0;
HANDLE_UNARY_CONST_FUNC(fp_int); break;
case cCbrt: HANDLE_UNARY_CONST_FUNC(fp_cbrt); break; // converted into cPow x 0.33333
case cSqrt: HANDLE_UNARY_CONST_FUNC(fp_sqrt); break; // converted into cPow x 0.5
case cExp: HANDLE_UNARY_CONST_FUNC(fp_exp); break; // convered into cPow CONSTANT_E x
case cLog2: HANDLE_UNARY_CONST_FUNC(fp_log2); break;
case cLog10: HANDLE_UNARY_CONST_FUNC(fp_log10); break;
case cLog2by:
if(tree.GetParam(0).IsImmed()
&& tree.GetParam(1).IsImmed())
{ tree.ReplaceWithImmed( fp_log2(tree.GetParam(0).GetImmed()) * tree.GetParam(1).GetImmed() );
goto do_return; }
break;
case cArg: HANDLE_UNARY_CONST_FUNC(fp_arg); break;
case cConj: HANDLE_UNARY_CONST_FUNC(fp_conj); break;
case cImag: HANDLE_UNARY_CONST_FUNC(fp_imag); break;
case cReal: HANDLE_UNARY_CONST_FUNC(fp_real); break;
case cPolar:
if(tree.GetParam(0).IsImmed()
&& tree.GetParam(1).IsImmed())
{ tree.ReplaceWithImmed( fp_polar(tree.GetParam(0).GetImmed(), tree.GetParam(1).GetImmed() ) );
goto do_return; }
break;
case cMod: /* Can more be done than this? */
if(tree.GetParam(0).IsImmed()
&& tree.GetParam(1).IsImmed())
{ tree.ReplaceWithImmed( fp_mod(tree.GetParam(0).GetImmed(), tree.GetParam(1).GetImmed()) );
goto do_return; }
break;
case cAtan2:
{
/* Range based optimizations for (y,x):
* If y is +0 and x <= -0, +pi is returned
* If y is -0 and x <= -0, -pi is returned (assumed never happening)
* If y is +0 and x >= +0, +0 is returned
* If y is -0 and x >= +0, -0 is returned (assumed never happening)
* If x is +-0 and y < 0, -pi/2 is returned
* If x is +-0 and y > 0, +pi/2 is returned
* Otherwise, perform constant folding when available
* If we know x <> 0, convert into atan(y / x)
* TODO: Figure out whether the above step is wise
* It allows e.g. atan2(6*x, 3*y) -> atan(2*x/y)
* when we know y != 0
*/
range<Value_t> p0 = CalculateResultBoundaries( tree.GetParam(0) );
range<Value_t> p1 = CalculateResultBoundaries( tree.GetParam(1) );
if(tree.GetParam(0).IsImmed()
&& fp_equal(tree.GetParam(0).GetImmed(), Value_t(0))) // y == 0
{
if(p1.max.known && (p1.max.val) < Value_t(0)) // y == 0 && x < 0
{ tree.ReplaceWithImmed( fp_const_pi<Value_t>() ); goto do_return; }
if(p1.min.known && p1.min.val >= Value_t(0.0)) // y == 0 && x >= 0.0
{ tree.ReplaceWithImmed( Value_t(0) ); goto do_return; }
}
if(tree.GetParam(1).IsImmed()
&& fp_equal(tree.GetParam(1).GetImmed(), Value_t(0))) // x == 0
{
if(p0.max.known && (p0.max.val) < Value_t(0)) // y < 0 && x == 0
{ tree.ReplaceWithImmed( -fp_const_pihalf<Value_t>() ); goto do_return; }
if(p0.min.known && p0.min.val > Value_t(0)) // y > 0 && x == 0
{ tree.ReplaceWithImmed( fp_const_pihalf<Value_t>() ); goto do_return; }
}
if(tree.GetParam(0).IsImmed()
&& tree.GetParam(1).IsImmed())
{ tree.ReplaceWithImmed( fp_atan2(tree.GetParam(0).GetImmed(),
tree.GetParam(1).GetImmed()) );
goto do_return; }
if((p1.min.known && p1.min.val > Value_t(0)) // p1 != 0.0
|| (p1.max.known && (p1.max.val) < fp_const_negativezero<Value_t>())) // become atan(p0 / p1)
{
CodeTree<Value_t> pow_tree;
pow_tree.SetOpcode(cPow);
pow_tree.AddParamMove(tree.GetParam(1));
pow_tree.AddParam(CodeTreeImmed(Value_t(-1)));
pow_tree.Rehash();
CodeTree<Value_t> div_tree;
div_tree.SetOpcode(cMul);
div_tree.AddParamMove(tree.GetParam(0));
div_tree.AddParamMove(pow_tree);
div_tree.Rehash();
tree.SetOpcode(cAtan);
tree.SetParamMove(0, div_tree);
tree.DelParam(1);
}
break;
}
case cPow:
{
if(ConstantFolding_PowOperations(tree)) goto redo;
break;
}
/* The following opcodes are processed by GenerateFrom()
* within fpoptimizer_bytecode_to_codetree.cc and thus
* they will never occur in the calling context for the
* most of the parsing context. They may however occur
* at the late phase, so we deal with them.
*/
case cDiv: // converted into cPow y -1
if(tree.GetParam(0).IsImmed()
&& tree.GetParam(1).IsImmed()
&& tree.GetParam(1).GetImmed() != Value_t(0.0))
{ tree.ReplaceWithImmed( tree.GetParam(0).GetImmed() / tree.GetParam(1).GetImmed() );
goto do_return; }
break;
case cInv: // converted into cPow y -1
if(tree.GetParam(0).IsImmed()
&& tree.GetParam(0).GetImmed() != Value_t(0.0))
{ tree.ReplaceWithImmed( Value_t(1) / tree.GetParam(0).GetImmed() );
goto do_return; }
// Note: Could use (mulgroup)^immed optimization from cPow
break;
case cSub: // converted into cMul y -1
if(tree.GetParam(0).IsImmed()
&& tree.GetParam(1).IsImmed())
{ tree.ReplaceWithImmed( tree.GetParam(0).GetImmed() - tree.GetParam(1).GetImmed() );
goto do_return; }
break;
case cNeg: // converted into cMul x -1
if(tree.GetParam(0).IsImmed())
{ tree.ReplaceWithImmed( -tree.GetParam(0).GetImmed() );
goto do_return; }
break;
case cRad: // converted into cMul x CONSTANT_RD
if(tree.GetParam(0).IsImmed())
{ tree.ReplaceWithImmed( RadiansToDegrees( tree.GetParam(0).GetImmed() ) );
goto do_return; }
break;
case cDeg: // converted into cMul x CONSTANT_DR
if(tree.GetParam(0).IsImmed())
{ tree.ReplaceWithImmed( DegreesToRadians( tree.GetParam(0).GetImmed() ) );
goto do_return; }
break;
case cSqr: // converted into cMul x x
if(tree.GetParam(0).IsImmed())
{ tree.ReplaceWithImmed( tree.GetParam(0).GetImmed() * tree.GetParam(0).GetImmed() );
goto do_return; }
break;
case cExp2: // converted into cPow 2.0 x
HANDLE_UNARY_CONST_FUNC(fp_exp2); break;
case cRSqrt: // converted into cPow x -0.5
if(tree.GetParam(0).IsImmed())
{ tree.ReplaceWithImmed( Value_t(1) / fp_sqrt(tree.GetParam(0).GetImmed()) );
goto do_return; }
break;
case cCot: // converted into cMul (cPow (cTan x) -1)
if(tree.GetParam(0).IsImmed())
{ Value_t tmp = fp_tan(tree.GetParam(0).GetImmed());
if(fp_nequal(tmp, Value_t(0)))
{ tree.ReplaceWithImmed( Value_t(1) / tmp );
goto do_return; } }
break;
case cSec: // converted into cMul (cPow (cCos x) -1)
if(tree.GetParam(0).IsImmed())
{ Value_t tmp = fp_cos(tree.GetParam(0).GetImmed());
if(fp_nequal(tmp, Value_t(0)))
{ tree.ReplaceWithImmed( Value_t(1) / tmp );
goto do_return; } }
break;
case cCsc: // converted into cMul (cPow (cSin x) -1)
if(tree.GetParam(0).IsImmed())
{ Value_t tmp = fp_sin(tree.GetParam(0).GetImmed());
if(fp_nequal(tmp, Value_t(0)))
{ tree.ReplaceWithImmed( Value_t(1) / tmp );
goto do_return; } }
break;
case cHypot: // converted into cSqrt(cAdd(cMul(x x), cMul(y y)))
if(tree.GetParam(0).IsImmed() && tree.GetParam(1).IsImmed())
{
tree.ReplaceWithImmed( fp_hypot(tree.GetParam(0).GetImmed(),
tree.GetParam(1).GetImmed()) );
goto do_return;
}
break;
/* Opcodes that do not occur in the tree for other reasons */
case cRDiv: // version of cDiv
case cRSub: // version of cSub
case cDup:
case cFetch:
case cPopNMov:
case cSinCos:
case cSinhCosh:
case cNop:
case cJump:
break; /* Should never occur */
/* Opcodes that we can't do anything about */
case cPCall:
case cFCall:
break;
}
do_return:;
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "[" << (&stackptr) << "]Done ConstantFolding, result: ";
DumpTree(tree);
std::cout << "\n";
DumpHashes(tree);
#endif
}
}
/* BEGIN_EXPLICIT_INSTANTATION */
#include "instantiate.hh"
namespace FPoptimizer_CodeTree
{
#define FP_INSTANTIATE(type) \
template void ConstantFolding(CodeTree<type>& );
FPOPTIMIZER_EXPLICITLY_INSTANTIATE(FP_INSTANTIATE)
#undef FP_INSTANTIATE
}
/* END_EXPLICIT_INSTANTATION */
#endif
|