llvm.org GIT mirror
When factoring multiply expressions across adds, factor both positive and negative forms of constants together. This allows us to compile: int foo(int x, int y) { return (x-y) + (x-y) + (x-y); } into: _foo: ## @foo subl %esi, %edi leal (%rdi,%rdi,2), %eax ret instead of (where the 3 and -3 were not factored): _foo: imull \$-3, 8(%esp), %ecx imull \$3, 4(%esp), %eax addl %ecx, %eax ret this started out as: movl 12(%ebp), %ecx imull \$3, 8(%ebp), %eax subl %ecx, %eax subl %ecx, %eax subl %ecx, %eax ret This comes from PR5359. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92381 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
2 changed file(s) with 64 addition(s) and 23 deletion(s).
 509 509 } 510 510 511 511 // Scan backwards and forwards among values with the same rank as element i to 512 // see if X exists. If X does not exist, return i.⏎ 512 // see if X exists. If X does not exist, return i. This is useful when⏎ 513 // scanning for 'x' when we see '-x' because they both get the same rank. 513 514 static unsigned FindInOperandList(SmallVectorImpl &Ops, unsigned i, 514 515 Value *X) { 515 516 unsigned XRank = Ops[i].Rank; 517 518 for (unsigned j = i+1; j != e && Ops[j].Rank == XRank; ++j) 518 519 if (Ops[j].Op == X) 519 520 return j; 520 // Scan backwards⏎ 521 // Scan backwards.⏎ 521 522 for (unsigned j = i-1; j != ~0U && Ops[j].Rank == XRank; --j) 522 523 if (Ops[j].Op == X) 523 524 return j; 546 547 LinearizeExprTree(BO, Factors); 547 548 548 549 bool FoundFactor = false; 549 for (unsigned i = 0, e = Factors.size(); i != e; ++i)⏎ 550 bool NeedsNegate = false;⏎ 551 for (unsigned i = 0, e = Factors.size(); i != e; ++i) { 550 552 if (Factors[i].Op == Factor) { 551 553 FoundFactor = true; 552 554 Factors.erase(Factors.begin()+i); 553 555 break; 554 556 } 557 558 // If this is a negative version of this factor, remove it. 559 if (ConstantInt *FC1 = dyn_cast(Factor)) 560 if (ConstantInt *FC2 = dyn_cast(Factors[i].Op)) 561 if (FC1->getValue() == -FC2->getValue()) { 562 FoundFactor = NeedsNegate = true; 563 Factors.erase(Factors.begin()+i); 564 break; 565 } 566 } 567 555 568 if (!FoundFactor) { 556 569 // Make sure to restore the operands to the expression tree. 557 570 RewriteExprTree(BO, Factors); 558 571 return 0; 559 572 } 573 574 BasicBlock::iterator InsertPt = BO; ++InsertPt; 560 575 561 576 // If this was just a single multiply, remove the multiply and return the only 562 577 // remaining operand. 563 578 if (Factors.size() == 1) { 564 579 ValueRankMap.erase(BO); 565 580 BO->eraseFromParent(); 566 return Factors[0].Op; 567 } 568 569 RewriteExprTree(BO, Factors); 570 return BO;⏎ 581 V = Factors[0].Op;⏎ 582 } else { 583 RewriteExprTree(BO, Factors); 584 V = BO; 585 } 586 587 if (NeedsNegate) 588 V = BinaryOperator::CreateNeg(V, "neg", InsertPt); 589 590 return V; 571 591 } 572 592 573 593 /// FindSingleUseMultiplyFactors - If V is a single-use multiply, recursively 644 664 // Scan the operand lists looking for X and -X pairs. If we find any, we 645 665 // can simplify the expression. X+-X == 0. While we're at it, scan for any 646 666 // duplicates. We want to canonicalize Y+Y+Y+Z -> 3*Y+Z. 667 // 668 // TODO: We could handle "X + ~X" -> "-1" if we wanted, since "-X = ~X+1". 669 // 647 670 for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 648 671 Value *TheOp = Ops[i].Op; 649 672 // Check to see if we've seen this operand before. If so, we factor all 729 752 assert(Factors.size() > 1 && "Bad linearize!"); 730 753 731 754 // Add one to FactorOccurrences for each unique factor in this op. 732 if (Factors.size() == 2) { 733 unsigned Occ = ++FactorOccurrences[Factors[0]]; 734 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factors[0]; } 735 if (Factors[0] != Factors[1]) { // Don't double count A*A. 736 Occ = ++FactorOccurrences[Factors[1]]; 737 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factors[1]; } 738 } 739 } else { 740 SmallPtrSet Duplicates; 741 for (unsigned i = 0, e = Factors.size(); i != e; ++i) { 742 if (!Duplicates.insert(Factors[i])) continue; 743 744 unsigned Occ = ++FactorOccurrences[Factors[i]]; 745 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factors[i]; } 746 }⏎ 755 SmallPtrSet Duplicates;⏎ 756 for (unsigned i = 0, e = Factors.size(); i != e; ++i) { 757 Value *Factor = Factors[i]; 758 if (!Duplicates.insert(Factor)) continue; 759 760 unsigned Occ = ++FactorOccurrences[Factor]; 761 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; } 762 763 // If Factor is a negative constant, add the negated value as a factor 764 // because we can percolate the negate out. Watch for minint, which 765 // cannot be positivified. 766 if (ConstantInt *CI = dyn_cast(Factor)) 767 if (CI->getValue().isNegative() && !CI->getValue().isMinSignedValue()) { 768 Factor = ConstantInt::get(CI->getContext(), -CI->getValue()); 769 assert(!Duplicates.count(Factor) && 770 "Shouldn't have two constant factors, missed a canonicalize"); 771 772 unsigned Occ = ++FactorOccurrences[Factor]; 773 if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factor; } 774 } 747 775 } 748 776 } 749 777
 190 190 ; CHECK-NEXT: ret i32 191 191 } 192 192 193 ; PR5359 194 define i32 @test14(i32 %X1, i32 %X2) { 195 %B = mul i32 %X1, 47 ; X1*47 196 %C = mul i32 %X2, -47 ; X2*-47 197 %D = add i32 %B, %C ; X1*47 + X2*-47 -> 47*(X1-X2) 198 ret i32 %D 199 ; CHECK: @test14 200 ; CHECK-NEXT: sub i32 %X1, %X2 201 ; CHECK-NEXT: mul i32 {{.*}}, 47 202 ; CHECK-NEXT: ret i32 203 } 204 205