[PATCH] D156026: [InstCombine] Contracting x^2 + 2*x*y + y^2 to (x + y)^2 (integer)

Christoph Stiller via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 22 11:48:45 PDT 2023


rainerzufalldererste updated this revision to Diff 543213.
rainerzufalldererste retitled this revision from "[InstCombine] Contracting x^2 + 2*x*y + y^2 to (x + y)^2" to "[InstCombine] Contracting x^2 + 2*x*y + y^2 to (x + y)^2 (integer)".
rainerzufalldererste edited the summary of this revision.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156026/new/

https://reviews.llvm.org/D156026

Files:
  llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp


Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1615,6 +1615,18 @@
         I, Builder.CreateIntrinsic(Intrinsic::ctpop, {I.getType()},
                                    {Builder.CreateOr(A, B)}));
 
+  // (a * a) + ((a << 1) + b) * b -> (a + b)^2
+  // which is:
+  // (a * a) + (2 * a * b) + (b * b) -> (a + b)^2
+  if (match(RHS, m_Mul(m_Value(A), m_Deferred(A)))) {
+    if (match(LHS,
+              m_Mul(m_Add(m_Shl(m_Specific(A), m_SpecificInt(1)), m_Value(B)),
+                    m_Deferred(B)))) {
+      Value *AB = Builder.CreateAdd(A, B);
+      return BinaryOperator::CreateMul(AB, AB);
+    }
+  }
+
   if (Instruction *Res = foldBinOpOfDisplacedShifts(I))
     return Res;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156026.543213.patch
Type: text/x-patch
Size: 896 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230722/36208afe/attachment.bin>


More information about the llvm-commits mailing list