diff options
author | Robert Bocchino <bocchino@illinois.edu> | 2006-01-17 20:07:22 +0000 |
---|---|---|
committer | Robert Bocchino <bocchino@illinois.edu> | 2006-01-17 20:07:22 +0000 |
commit | c152f9cd26e7cb32352c513389a18ffd892ecaec (patch) | |
tree | b584bdb434f8a0cca75c8606eb3d151239aa04fa /lib/VMCore/ConstantFold.cpp | |
parent | 956fd7254f976581a27ba0ee73d7707ff484d2c3 (diff) |
VMCore support for the insertelement operation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25408 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/ConstantFold.cpp')
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 3f7bc7db33..a6fbf42fa2 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -734,6 +734,63 @@ Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val, return 0; } +Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val, + const Constant *Elt, + const Constant *Idx) { + const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx); + if (!CIdx) return 0; + unsigned idxVal = CIdx->getValue(); + if (const UndefValue *UVal = dyn_cast<UndefValue>(Val)) { + // Insertion of scalar constant into packed undef + // Optimize away insertion of undef + if (isa<UndefValue>(Elt)) + return const_cast<Constant*>(Val); + // Otherwise break the aggregate undef into multiple undefs and do + // the insertion + unsigned numOps = + cast<PackedType>(Val->getType())->getNumElements(); + std::vector<Constant*> Ops; + Ops.reserve(numOps); + for (unsigned i = 0; i < numOps; ++i) { + const Constant *Op = + (i == idxVal) ? Elt : UndefValue::get(Elt->getType()); + Ops.push_back(const_cast<Constant*>(Op)); + } + return ConstantPacked::get(Ops); + } + if (const ConstantAggregateZero *CVal = + dyn_cast<ConstantAggregateZero>(Val)) { + // Insertion of scalar constant into packed aggregate zero + // Optimize away insertion of zero + if (Elt->isNullValue()) + return const_cast<Constant*>(Val); + // Otherwise break the aggregate zero into multiple zeros and do + // the insertion + unsigned numOps = + cast<PackedType>(Val->getType())->getNumElements(); + std::vector<Constant*> Ops; + Ops.reserve(numOps); + for (unsigned i = 0; i < numOps; ++i) { + const Constant *Op = + (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType()); + Ops.push_back(const_cast<Constant*>(Op)); + } + return ConstantPacked::get(Ops); + } + if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) { + // Insertion of scalar constant into packed constant + std::vector<Constant*> Ops; + Ops.reserve(CVal->getNumOperands()); + for (unsigned i = 0; i < CVal->getNumOperands(); ++i) { + const Constant *Op = + (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i)); + Ops.push_back(const_cast<Constant*>(Op)); + } + return ConstantPacked::get(Ops); + } + return 0; +} + /// isZeroSizedType - This type is zero sized if its an array or structure of /// zero sized types. The only leaf zero sized type is an empty structure. static bool isMaybeZeroSizedType(const Type *Ty) { |