[llvm-commits] [llvm] r53544 - in /llvm/trunk: lib/VMCore/Constants.cpp test/Assembler/vector-cmp.ll

Chris Lattner sabre at nondot.org
Sun Jul 13 22:17:31 PDT 2008


Author: lattner
Date: Mon Jul 14 00:17:31 2008
New Revision: 53544

URL: http://llvm.org/viewvc/llvm-project?rev=53544&view=rev
Log:
Fix a bunch of bugs handling vector compare constant expressions, fixing
PR2317.

Added:
    llvm/trunk/test/Assembler/vector-cmp.ll
Modified:
    llvm/trunk/lib/VMCore/Constants.cpp

Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=53544&r1=53543&r2=53544&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Mon Jul 14 00:17:31 2008
@@ -730,7 +730,8 @@
 }
 
 bool ConstantExpr::isCompare() const {
-  return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
+  return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp ||
+         getOpcode() == Instruction::VICmp || getOpcode() == Instruction::VFCmp;
 }
 
 bool ConstantExpr::hasIndices() const {
@@ -2201,9 +2202,8 @@
 
 Constant *
 ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
-  assert(isa<VectorType>(LHS->getType()) &&
+  assert(isa<VectorType>(LHS->getType()) && LHS->getType() == RHS->getType() &&
          "Tried to create vicmp operation on non-vector type!");
-  assert(LHS->getType() == RHS->getType());
   assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE && 
          pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate");
 
@@ -2211,23 +2211,30 @@
   const Type *EltTy = VTy->getElementType();
   unsigned NumElts = VTy->getNumElements();
 
-  SmallVector<Constant *, 16> Elts;
-  for (unsigned i = 0; i != NumElts; ++i) {
-    Constant *FC = ConstantFoldCompareInstruction(pred, LHS->getOperand(i),
-                                                  RHS->getOperand(i));
-    if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
-      if (FCI->getZExtValue())
-        Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
-      else
-        Elts.push_back(ConstantInt::get(EltTy, 0ULL));
-    } else if (FC && isa<UndefValue>(FC)) {
-      Elts.push_back(UndefValue::get(EltTy));
-    } else {
-      break;
+  // See if we can fold the element-wise comparison of the LHS and RHS.
+  SmallVector<Constant *, 16> LHSElts, RHSElts;
+  LHS->getVectorElements(LHSElts);
+  RHS->getVectorElements(RHSElts);
+                    
+  if (!LHSElts.empty() && !RHSElts.empty()) {
+    SmallVector<Constant *, 16> Elts;
+    for (unsigned i = 0; i != NumElts; ++i) {
+      Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
+                                                    RHSElts[i]);
+      if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
+        if (FCI->getZExtValue())
+          Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
+        else
+          Elts.push_back(ConstantInt::get(EltTy, 0ULL));
+      } else if (FC && isa<UndefValue>(FC)) {
+        Elts.push_back(UndefValue::get(EltTy));
+      } else {
+        break;
+      }
     }
+    if (Elts.size() == NumElts)
+      return ConstantVector::get(&Elts[0], Elts.size());
   }
-  if (Elts.size() == NumElts)
-    return ConstantVector::get(&Elts[0], Elts.size());
 
   // Look up the constant in the table first to ensure uniqueness
   std::vector<Constant*> ArgVec;
@@ -2251,23 +2258,30 @@
   const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits());
   const Type *ResultTy = VectorType::get(REltTy, NumElts);
 
-  SmallVector<Constant *, 16> Elts;
-  for (unsigned i = 0; i != NumElts; ++i) {
-    Constant *FC = ConstantFoldCompareInstruction(pred, LHS->getOperand(i),
-                                                        RHS->getOperand(i));
-    if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
-      if (FCI->getZExtValue())
-        Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
-      else
-        Elts.push_back(ConstantInt::get(REltTy, 0ULL));
-    } else if (FC && isa<UndefValue>(FC)) {
-      Elts.push_back(UndefValue::get(REltTy));
-    } else {
-      break;
+  // See if we can fold the element-wise comparison of the LHS and RHS.
+  SmallVector<Constant *, 16> LHSElts, RHSElts;
+  LHS->getVectorElements(LHSElts);
+  RHS->getVectorElements(RHSElts);
+  
+  if (!LHSElts.empty() && !RHSElts.empty()) {
+    SmallVector<Constant *, 16> Elts;
+    for (unsigned i = 0; i != NumElts; ++i) {
+      Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
+                                                    RHSElts[i]);
+      if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
+        if (FCI->getZExtValue())
+          Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
+        else
+          Elts.push_back(ConstantInt::get(REltTy, 0ULL));
+      } else if (FC && isa<UndefValue>(FC)) {
+        Elts.push_back(UndefValue::get(REltTy));
+      } else {
+        break;
+      }
     }
+    if (Elts.size() == NumElts)
+      return ConstantVector::get(&Elts[0], Elts.size());
   }
-  if (Elts.size() == NumElts)
-    return ConstantVector::get(&Elts[0], Elts.size());
 
   // Look up the constant in the table first to ensure uniqueness
   std::vector<Constant*> ArgVec;
@@ -2683,8 +2697,14 @@
     if (C2 == From) C2 = To;
     if (getOpcode() == Instruction::ICmp)
       Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
-    else
+    else if (getOpcode() == Instruction::FCmp)
       Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
+    else if (getOpcode() == Instruction::VICmp)
+      Replacement = ConstantExpr::getVICmp(getPredicate(), C1, C2);
+    else {
+      assert(getOpcode() == Instruction::VFCmp);
+      Replacement = ConstantExpr::getVFCmp(getPredicate(), C1, C2);
+    }
   } else if (getNumOperands() == 2) {
     Constant *C1 = getOperand(0);
     Constant *C2 = getOperand(1);

Added: llvm/trunk/test/Assembler/vector-cmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/vector-cmp.ll?rev=53544&view=auto

==============================================================================
--- llvm/trunk/test/Assembler/vector-cmp.ll (added)
+++ llvm/trunk/test/Assembler/vector-cmp.ll Mon Jul 14 00:17:31 2008
@@ -0,0 +1,16 @@
+; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | grep {global.*vicmp slt}
+; PR2317
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
+target triple = "i686-apple-darwin9.2.2"
+
+define <4 x i32> @foo(<4 x float> %a, <4 x float> %b) nounwind  {
+entry:
+	%cmp = vfcmp olt <4 x float> %a, %b		; <4 x i32> [#uses=1]
+	ret <4 x i32> %cmp
+}
+
+global <4 x i32> vicmp slt ( <4 x i32> <i32 1, i32 1, i32 1, i32 1>, <4 x i32>  <i32 1, i32 2, i32 1, i32 2> ) ;
+
+ at B = external global i32;
+
+global <4 x i32> vicmp slt ( <4 x i32> <i32 ptrtoint (i32 * @B to i32), i32 1, i32 1, i32 1>, <4 x i32>  <i32 1, i32 2, i32 1, i32 2> ) ;





More information about the llvm-commits mailing list