[llvm-commits] [llvm] r138340 - in /llvm/trunk: include/llvm/Constant.h lib/Analysis/ConstantFolding.cpp lib/VMCore/Constants.cpp test/Transforms/InstCombine/bitcast.ll

Nadav Rotem nadav.rotem at intel.com
Tue Aug 23 10:48:43 PDT 2011


Author: nadav
Date: Tue Aug 23 12:48:43 2011
New Revision: 138340

URL: http://llvm.org/viewvc/llvm-project?rev=138340&view=rev
Log:
Address Duncan's CR request:
1. Cleanup the tests in ConstantFolding.cpp
2. Implement isAllOnes for Constant, ConstantFP, ConstantVector




Modified:
    llvm/trunk/include/llvm/Constant.h
    llvm/trunk/lib/Analysis/ConstantFolding.cpp
    llvm/trunk/lib/VMCore/Constants.cpp
    llvm/trunk/test/Transforms/InstCombine/bitcast.ll

Modified: llvm/trunk/include/llvm/Constant.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Constant.h?rev=138340&r1=138339&r2=138340&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Constant.h (original)
+++ llvm/trunk/include/llvm/Constant.h Tue Aug 23 12:48:43 2011
@@ -51,6 +51,9 @@
   /// isNullValue - Return true if this is the value that would be returned by
   /// getNullValue.
   bool isNullValue() const;
+  /// isAllOnesValue - Return true if this is the value that would be returned by
+  /// getAllOnesValue.
+  bool isAllOnesValue() const;
 
   /// isNegativeZeroValue - Return true if the value is what would be returned 
   /// by getZeroValueForNegation.

Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=138340&r1=138339&r2=138340&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Tue Aug 23 12:48:43 2011
@@ -45,15 +45,9 @@
 /// ConstantExpr if unfoldable.
 static Constant *FoldBitCast(Constant *C, Type *DestTy,
                              const TargetData &TD) {
-  
-  ConstantVector *CV = dyn_cast<ConstantVector>(C);
-  IntegerType *IntVTy = dyn_cast<IntegerType>(DestTy);
-  // When casting vectors to scalar integers, catch the
-  // obvious splat cases.
-  if (IntVTy && CV) {
-    if (CV->isNullValue()) return ConstantInt::getNullValue(IntVTy);
-    if (CV->isAllOnesValue()) return ConstantInt::getAllOnesValue(IntVTy);
-  }
+  // Catch the obvious splat cases.
+  if (C->isNullValue()) return Constant::getNullValue(DestTy);
+  if (C->isAllOnesValue()) return Constant::getAllOnesValue(DestTy);
   
   // The code below only handles casts to vectors currently.
   VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
@@ -68,6 +62,7 @@
   }
   
   // If this is a bitcast from constant vector -> vector, fold it.
+  ConstantVector *CV = dyn_cast<ConstantVector>(C);
   if (CV == 0)
     return ConstantExpr::getBitCast(C, DestTy);
   

Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=138340&r1=138339&r2=138340&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Tue Aug 23 12:48:43 2011
@@ -62,6 +62,21 @@
   return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this);
 }
 
+bool Constant::isAllOnesValue() const {
+  // Check for -1 integers
+  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
+    return CI->isAllOnesValue();
+
+  // +0.0 is null.
+  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
+    return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue();
+
+  // Check for constant vectors
+  if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
+    return CV->isAllOnesValue();
+
+  return false;
+}
 // Constructor to create a '0' constant of arbitrary type...
 Constant *Constant::getNullValue(Type *Ty) {
   switch (Ty->getTypeID()) {
@@ -126,7 +141,7 @@
   SmallVector<Constant*, 16> Elts;
   VectorType *VTy = cast<VectorType>(Ty);
   Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
-  assert(Elts[0] && "Not a vector integer type!");
+  assert(Elts[0] && "Invalid AllOnes value!");
   return cast<ConstantVector>(ConstantVector::get(Elts));
 }
 
@@ -1064,13 +1079,16 @@
   // Check out first element.
   const Constant *Elt = getOperand(0);
   const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
-  if (!CI || !CI->isAllOnesValue()) return false;
+  const ConstantFP *CF = dyn_cast<ConstantFP>(Elt);
+
   // Then make sure all remaining elements point to the same value.
   for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
     if (getOperand(I) != Elt)
       return false;
   
-  return true;
+  // First value is all-ones.
+  return (CI && CI->isAllOnesValue()) || 
+         (CF && CF->isAllOnesValue());
 }
 
 /// getSplatValue - If this is a splat constant, where all of the

Modified: llvm/trunk/test/Transforms/InstCombine/bitcast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/bitcast.ll?rev=138340&r1=138339&r2=138340&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/bitcast.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/bitcast.ll Tue Aug 23 12:48:43 2011
@@ -113,3 +113,27 @@
 ; CHECK: @ISPC0
 ; CHECK: ret i64 0
 }
+
+
+define i64 @Vec2(i64 %in) {
+  %out = and i64 %in, xor (i64 bitcast (<4 x i16> <i16 0, i16 0, i16 0, i16 0> to i64), i64 0)
+  ret i64 %out
+; CHECK: @Vec2
+; CHECK: ret i64 0
+}
+
+define i64 @All11(i64 %in) {
+  %out = and i64 %in, xor (i64 bitcast (<2 x float> bitcast (i64 -1 to <2 x float>) to i64), i64 -1) 
+  ret i64 %out
+; CHECK: @All11
+; CHECK: ret i64 0
+}
+
+
+define i64 @All111(i32 %in) {
+  %out = and i32 %in, xor (i64 bitcast (<1 x float> bitcast (i32 -1 to <1 x float>) to i32), i32 -1) 
+  ret i32 %out
+; CHECK: @All11
+; CHECK: ret i32 0
+}
+





More information about the llvm-commits mailing list