add a helper method for code that wants to handle vector
authorChris Lattner <sabre@nondot.org>
Thu, 10 Jul 2008 00:28:11 +0000 (00:28 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 10 Jul 2008 00:28:11 +0000 (00:28 +0000)
constants by element without caring how they are formed.

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

lib/VMCore/Constants.cpp

index 9d898f0b4dbf3d9ee677d1b6b9d3029792824475..7908638fd6795a8b7a3dd0e5b5fbc63c7e2c07b3 100644 (file)
@@ -155,6 +155,31 @@ ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
 }
 
 
+/// getVectorElements - This method, which is only valid on constant of vector
+/// type, returns the elements of the vector in the specified smallvector.
+/// This handles breaking down a vector undef into undef elements, etc.
+void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
+  assert(isa<VectorType>(getType()) && "Not a vector constant!");
+  
+  if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
+    for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
+      Elts.push_back(CV->getOperand(i));
+    return;
+  }
+  
+  const VectorType *VT = cast<VectorType>(getType());
+  if (isa<ConstantAggregateZero>(this)) {
+    Elts.assign(VT->getNumElements(), 
+                Constant::getNullValue(VT->getElementType()));
+    return;
+  }
+  
+  assert(isa<UndefValue>(this) && "Unknown vector constant type!");
+  Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
+}
+
+
+
 //===----------------------------------------------------------------------===//
 //                                ConstantInt
 //===----------------------------------------------------------------------===//