[llvm-commits] [llvm] r131888 - /llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Chris Lattner sabre at nondot.org
Sun May 22 17:32:19 PDT 2011


Author: lattner
Date: Sun May 22 19:32:19 2011
New Revision: 131888

URL: http://llvm.org/viewvc/llvm-project?rev=131888&view=rev
Log:
rearrange two transforms, since one subsumes the other.  Make the shift-exactness
xform recurse.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=131888&r1=131887&r2=131888&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Sun May 22 19:32:19 2011
@@ -29,40 +29,47 @@
   // code.
   if (!V->hasOneUse()) return 0;
   
+  bool MadeChange = false;
+
+  // ((1 << A) >>u B) --> (1 << (A-B))
+  // Because V cannot be zero, we know that B is less than A.
+  Value *A = 0, *B = 0, *PowerOf2 = 0;
+  if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(PowerOf2), m_Value(A))),
+                      m_Value(B))) &&
+      // The "1" can be any value known to be a power of 2.
+      isPowerOfTwo(PowerOf2, IC.getTargetData())) {
+    A = IC.Builder->CreateSub(A, B, "tmp");
+    return IC.Builder->CreateShl(PowerOf2, A);
+  }
   
   // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
   // inexact.  Similarly for <<.
   if (BinaryOperator *I = dyn_cast<BinaryOperator>(V))
     if (I->isLogicalShift() &&
         isPowerOfTwo(I->getOperand(0), IC.getTargetData())) {
+      // We know that this is an exact/nuw shift and that the input is a
+      // non-zero context as well.
+      if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC)) {
+        I->setOperand(0, V2);
+        MadeChange = true;
+      }
+      
       if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
         I->setIsExact();
-        return I;
+        MadeChange = true;
       }
       
       if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
         I->setHasNoUnsignedWrap();
-        return I;
+        MadeChange = true;
       }
     }
-      
-  // ((1 << A) >>u B) --> (1 << (A-B))
-  // Because V cannot be zero, we know that B is less than A.
-  Value *A = 0, *B = 0, *PowerOf2 = 0;
-  if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(PowerOf2), m_Value(A))),
-                      m_Value(B))) &&
-      // The "1" can be any value known to be a power of 2.
-      isPowerOfTwo(PowerOf2, IC.getTargetData())) {
-    A = IC.Builder->CreateSub(A, B, "tmp");
-    return IC.Builder->CreateShl(PowerOf2, A);
-  }
-  
+
   // TODO: Lots more we could do here:
-  //    "1 >> X" could get an "isexact" bit.
   //    If V is a phi node, we can call this on each of its operands.
   //    "select cond, X, 0" can simplify to "X".
   
-  return 0;
+  return MadeChange ? V : 0;
 }
 
 





More information about the llvm-commits mailing list