Enhance constant folding of bitcast operations on vectors of floats.
authorNadav Rotem <nadav.rotem@intel.com>
Thu, 17 Feb 2011 21:22:27 +0000 (21:22 +0000)
committerNadav Rotem <nadav.rotem@intel.com>
Thu, 17 Feb 2011 21:22:27 +0000 (21:22 +0000)
Add getAllOnesValue of FP numbers to Constants and APFloat.
Add more tests.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125776 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/APFloat.h
lib/Support/APFloat.cpp
lib/VMCore/ConstantFold.cpp
lib/VMCore/Constants.cpp
test/Transforms/InstCombine/bitcast-vec-uniform.ll

index dfe4e0f49adb2836f7202fcd2d551e662691495b..ca4138b825a64373050152c59e18bba9752b8366 100644 (file)
@@ -246,6 +246,13 @@ namespace llvm {
     static APFloat getSmallestNormalized(const fltSemantics &Sem,
                                          bool Negative = false);
 
+    /// getAllOnesValue - Returns a float which is bitcasted from
+    /// an all one value int.
+    ///
+    /// \param BitWidth - Select float type
+    /// \param isIEEE   - If 128 bit number, select between PPC and IEEE
+    static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false);
+
     /// Profile - Used to insert APFloat objects, or objects that contain
     ///  APFloat objects, into FoldingSets.
     void Profile(FoldingSetNodeID& NID) const;
index 6d018afa3f63422c69e7d02d5894deb94cffd8f6..e765ba0a27bbc11bde785cffc1ac88cf4bbad47d 100644 (file)
@@ -3197,6 +3197,12 @@ APFloat::initFromAPInt(const APInt& api, bool isIEEE)
     llvm_unreachable(0);
 }
 
+APFloat
+APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE)
+{
+  return APFloat(APInt::getAllOnesValue(BitWidth), isIEEE);
+}
+
 APFloat APFloat::getLargest(const fltSemantics &Sem, bool Negative) {
   APFloat Val(Sem, fcNormal, Negative);
 
index c8292c6d612b42ab15b63dcf3fcd73f86118207f..573efb7e5731559263d17e9d8986177de8f900aa 100644 (file)
@@ -43,8 +43,7 @@ using namespace llvm;
 static Constant *BitCastConstantVector(ConstantVector *CV,
                                        const VectorType *DstTy) {
 
-  if (CV->isAllOnesValue() && DstTy->getElementType()->isIntegerTy())
-      return Constant::getAllOnesValue(DstTy);
+  if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
   if (CV->isNullValue()) return Constant::getNullValue(DstTy);
 
   // If this cast changes element count then we can't handle it here:
index cac37cbcc386d2b71870c8bccdd3fd563d491fc5..a2ac768d22118f6791f3793b1f157d9f14af59ea 100644 (file)
@@ -93,7 +93,13 @@ Constant *Constant::getAllOnesValue(const Type *Ty) {
   if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
     return ConstantInt::get(Ty->getContext(),
                             APInt::getAllOnesValue(ITy->getBitWidth()));
-  
+
+  if (Ty->isFloatingPointTy()) {
+    APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
+                                          !Ty->isPPC_FP128Ty());
+    return ConstantFP::get(Ty->getContext(), FL);
+  }
+
   SmallVector<Constant*, 16> Elts;
   const VectorType *VTy = cast<VectorType>(Ty);
   Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
index a0d4678b0a04862c9d34e34947769590e0976a6f..5975f1ec396ea88dae671395a9409c66a99802bf 100644 (file)
@@ -18,7 +18,8 @@ define <4 x i32> @b(<1 x i64> %y) {
 }
 
 ; CHECK: @foo
-; CHECK: bitcast
+; CHECK-NOT: bitcast
+; CHECK: ret
 
 ; from MultiSource/Benchmarks/Bullet
 define <2 x float> @foo() {
@@ -27,4 +28,43 @@ define <2 x float> @foo() {
 }
 
 
+; CHECK: @foo2
+; CHECK-NOT: bitcast
+; CHECK: ret
+define <2 x double> @foo2() {
+  %cast = bitcast i128 -1 to <2 x double>
+  ret <2 x double> %cast
+}
 
+; CHECK: @foo3
+; CHECK-NOT: bitcast
+; CHECK: ret
+define <1 x float> @foo3() {
+  %cast = bitcast i32 -1 to <1 x float>
+  ret <1 x float> %cast
+}
+
+; CHECK: @foo4
+; CHECK-NOT: bitcast
+; CHECK: ret
+define float @foo4() {
+  %cast = bitcast <1 x i32 ><i32 -1> to float
+  ret float %cast
+}
+
+; CHECK: @foo5
+; CHECK-NOT: bitcast
+; CHECK: ret
+define double @foo5() {
+  %cast = bitcast <2 x i32 ><i32 -1, i32 -1> to double
+  ret double %cast
+}
+
+
+; CHECK: @foo6
+; CHECK-NOT: bitcast
+; CHECK: ret
+define <2 x double> @foo6() {
+  %cast = bitcast <4 x i32><i32 -1, i32 -1, i32 -1, i32 -1> to <2 x double>
+  ret <2 x double> %cast
+}