Skip to content

Commit

Permalink
IR: Split up Constant{Array,Vector}::get(), NFC
Browse files Browse the repository at this point in the history
Introduce `getImpl()` that tries the simplification logic from `get()`
and then gives up.  This allows the logic to be reused elsewhere in a
follow-up commit.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215963 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dexonsmith committed Aug 19, 2014
1 parent 6290308 commit 45d53fd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 8 additions & 0 deletions include/llvm/IR/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ class ConstantArray : public Constant {
// ConstantArray accessors
static Constant *get(ArrayType *T, ArrayRef<Constant*> V);

private:
static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);

public:
/// Transparently provide more efficient getOperand methods.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);

Expand Down Expand Up @@ -439,6 +443,10 @@ class ConstantVector : public Constant {
// ConstantVector accessors
static Constant *get(ArrayRef<Constant*> V);

private:
static Constant *getImpl(ArrayRef<Constant *> V);

public:
/// getSplat - Return a ConstantVector with the specified constant in each
/// element.
static Constant *getSplat(unsigned NumElts, Constant *Elt);
Expand Down
17 changes: 13 additions & 4 deletions lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,11 @@ ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
}

Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
if (Constant *C = getImpl(Ty, V))
return C;
return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
}
Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
// Empty arrays are canonicalized to ConstantAggregateZero.
if (V.empty())
return ConstantAggregateZero::get(Ty);
Expand All @@ -811,7 +816,6 @@ Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
assert(V[i]->getType() == Ty->getElementType() &&
"Wrong type in array element initializer");
}
LLVMContextImpl *pImpl = Ty->getContext().pImpl;

// If this is an all-zero array, return a ConstantAggregateZero object. If
// all undef, return an UndefValue, if "all simple", then return a
Expand Down Expand Up @@ -893,7 +897,7 @@ Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
}

// Otherwise, we really do want to create a ConstantArray.
return pImpl->ArrayConstants.getOrCreate(Ty, V);
return nullptr;
}

/// getTypeForElements - Return an anonymous struct type to use for a constant
Expand Down Expand Up @@ -981,9 +985,14 @@ ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)

// ConstantVector accessors.
Constant *ConstantVector::get(ArrayRef<Constant*> V) {
if (Constant *C = getImpl(V))
return C;
VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
}
Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
assert(!V.empty() && "Vectors can't be empty");
VectorType *T = VectorType::get(V.front()->getType(), V.size());
LLVMContextImpl *pImpl = T->getContext().pImpl;

// If this is an all-undef or all-zero vector, return a
// ConstantAggregateZero or UndefValue.
Expand Down Expand Up @@ -1075,7 +1084,7 @@ Constant *ConstantVector::get(ArrayRef<Constant*> V) {

// Otherwise, the element type isn't compatible with ConstantDataVector, or
// the operand list constants a ConstantExpr or something else strange.
return pImpl->VectorConstants.getOrCreate(T, V);
return nullptr;
}

Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
Expand Down

0 comments on commit 45d53fd

Please sign in to comment.