[llvm-commits] [llvm] r53380 - in /llvm/trunk: lib/VMCore/ConstantFold.cpp test/Transforms/ConstProp/2008-07-07-VectorCompare.ll

Chris Lattner sabre at nondot.org
Wed Jul 9 17:08:17 PDT 2008


Author: lattner
Date: Wed Jul  9 19:08:17 2008
New Revision: 53380

URL: http://llvm.org/viewvc/llvm-project?rev=53380&view=rev
Log:
elementwise comparison of vector constants was completely wrong.  Fix
it for PR2529

Modified:
    llvm/trunk/lib/VMCore/ConstantFold.cpp
    llvm/trunk/test/Transforms/ConstProp/2008-07-07-VectorCompare.ll

Modified: llvm/trunk/lib/VMCore/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ConstantFold.cpp?rev=53380&r1=53379&r2=53380&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/ConstantFold.cpp (original)
+++ llvm/trunk/lib/VMCore/ConstantFold.cpp Wed Jul  9 19:08:17 2008
@@ -1350,27 +1350,40 @@
     }
   } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
     if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
-      if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) {
-        for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
-          Constant *C = ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ,
-                                              CP1->getOperand(i),
-                                              CP2->getOperand(i));
-          if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
-            return CB;
-        }
-        // Otherwise, could not decide from any element pairs.
-        return 0;
-      } else if (pred == ICmpInst::ICMP_EQ) {
-        for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
-          Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ,
-                                              CP1->getOperand(i),
-                                              CP2->getOperand(i));
-          if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
-            return CB;
+      // If we can constant fold the comparison of each element, constant fold
+      // the whole vector comparison.
+      SmallVector<Constant*, 4> Elts;
+      const Type *InEltTy = CP1->getOperand(0)->getType();
+      bool isFP = InEltTy->isFloatingPoint();
+      const Type *ResEltTy = InEltTy;
+      if (isFP)
+        ResEltTy = IntegerType::get(InEltTy->getPrimitiveSizeInBits());
+      
+      for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
+        // Compare the elements, producing an i1 result or constant expr.
+        Constant *C;
+        if (isFP)
+          C = ConstantExpr::getFCmp(pred, CP1->getOperand(i),
+                                    CP2->getOperand(i));
+        else
+          C = ConstantExpr::getICmp(pred, CP1->getOperand(i),
+                                    CP2->getOperand(i));
+
+        // If it is a bool or undef result, convert to the dest type.
+        if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
+          if (CI->isZero())
+            Elts.push_back(Constant::getNullValue(ResEltTy));
+          else
+            Elts.push_back(Constant::getAllOnesValue(ResEltTy));
+        } else if (isa<UndefValue>(C)) {
+          Elts.push_back(UndefValue::get(ResEltTy));
+        } else {
+          break;
         }
-        // Otherwise, could not decide from any element pairs.
-        return 0;
       }
+      
+      if (Elts.size() == CP1->getNumOperands())
+        return ConstantVector::get(&Elts[0], Elts.size());
     }
   }
 

Modified: llvm/trunk/test/Transforms/ConstProp/2008-07-07-VectorCompare.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstProp/2008-07-07-VectorCompare.ll?rev=53380&r1=53379&r2=53380&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/ConstProp/2008-07-07-VectorCompare.ll (original)
+++ llvm/trunk/test/Transforms/ConstProp/2008-07-07-VectorCompare.ll Wed Jul  9 19:08:17 2008
@@ -6,9 +6,17 @@
         ret <4 x i32> %foo
 }
 
-define <4 x i32> @main(i32 %argc, i8** %argv) {
+define <4 x i32> @test2(i32 %argc, i8** %argv) {
 entry:  
         %foo = vicmp slt <4 x i32> <i32 undef, i32 undef, i32 undef, i32
 undef>, <i32 undef, i32 undef, i32 undef, i32 undef>
         ret <4 x i32> %foo
 }
+
+
+define <4 x i32> @test3() {
+       %foo = vfcmp ueq <4 x float> <float 0.0, float 0.0, float 0.0, float
+undef>, <float 1.0, float 1.0, float 1.0, float undef>
+	ret <4 x i32> %foo
+}
+





More information about the llvm-commits mailing list