[llvm] r327315 - [InstCombine] Replace calls to getNumUses with hasNUses or hasNUsesOrMore

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 12 11:46:06 PDT 2018


Author: ctopper
Date: Mon Mar 12 11:46:05 2018
New Revision: 327315

URL: http://llvm.org/viewvc/llvm-project?rev=327315&view=rev
Log:
[InstCombine] Replace calls to getNumUses with hasNUses or hasNUsesOrMore

getNumUses is a linear time operation. It traverses the user linked list to the end and counts as it goes. Since we are only interested in small constant counts, we should use hasNUses or hasNUsesMore more that terminate the traversal as soon as it can provide the answer.

There are still two other locations in InstCombine, but changing those would force a rebase of D44266 which if accepted would remove them.

Differential Revision: https://reviews.llvm.org/D44398

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=327315&r1=327314&r2=327315&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Mon Mar 12 11:46:05 2018
@@ -1192,7 +1192,7 @@ Instruction *InstCombiner::narrowMaskedB
     return nullptr;
 
   Value *X;
-  if (!match(Op1, m_ZExt(m_Value(X))) || Op1->getNumUses() > 2)
+  if (!match(Op1, m_ZExt(m_Value(X))) || Op1->hasNUsesOrMore(3))
     return nullptr;
 
   Type *Ty = And.getType();
@@ -2471,13 +2471,13 @@ Instruction *InstCombiner::visitXor(Bina
   // We're relying on the fact that we only do this transform when the shift has
   // exactly 2 uses and the add has exactly 1 use (otherwise, we might increase
   // instructions).
-  if (Op0->getNumUses() == 2)
+  if (Op0->hasNUses(2))
     std::swap(Op0, Op1);
 
   const APInt *ShAmt;
   Type *Ty = I.getType();
   if (match(Op1, m_AShr(m_Value(A), m_APInt(ShAmt))) &&
-      Op1->getNumUses() == 2 && *ShAmt == Ty->getScalarSizeInBits() - 1 &&
+      Op1->hasNUses(2) && *ShAmt == Ty->getScalarSizeInBits() - 1 &&
       match(Op0, m_OneUse(m_c_Add(m_Specific(A), m_Specific(Op1))))) {
     // B = ashr i32 A, 31 ; smear the sign bit
     // xor (add A, B), B  ; add -1 and flip bits if negative

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp?rev=327315&r1=327314&r2=327315&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp Mon Mar 12 11:46:05 2018
@@ -1333,7 +1333,7 @@ static Instruction *factorizeMinMaxTree(
   // the select.
   Value *MinMaxOp = nullptr;
   Value *ThirdOp = nullptr;
-  if (LHS->getNumUses() <= 2 && RHS->getNumUses() > 2) {
+  if (!LHS->hasNUsesOrMore(3) && RHS->hasNUsesOrMore(3)) {
     // If the LHS is only used in this chain and the RHS is used outside of it,
     // reuse the RHS min/max because that will eliminate the LHS.
     if (D == A || C == A) {
@@ -1347,7 +1347,7 @@ static Instruction *factorizeMinMaxTree(
       MinMaxOp = RHS;
       ThirdOp = A;
     }
-  } else if (RHS->getNumUses() <= 2) {
+  } else if (!RHS->hasNUsesOrMore(3)) {
     // Reuse the LHS. This will eliminate the RHS.
     if (D == A || D == B) {
       // min(min(a, b), min(c, a)) --> min(min(a, b), c)




More information about the llvm-commits mailing list