summaryrefslogtreecommitdiffstats
path: root/llvm/lib/VMCore/ConstantFolding.cpp
diff options
context:
space:
mode:
authorRobert Bocchino <bocchino@illinois.edu>2006-01-17 20:07:22 +0000
committerRobert Bocchino <bocchino@illinois.edu>2006-01-17 20:07:22 +0000
commitca27f0320bd5001db59ba22d51d80c93b20afc59 (patch)
tree5f954ea6b67c387ae8299eafb605f841a40d3cc4 /llvm/lib/VMCore/ConstantFolding.cpp
parente6336a9b69aff51cf511712e485c11fdc2fdc3a6 (diff)
downloadbcm5719-llvm-ca27f0320bd5001db59ba22d51d80c93b20afc59.tar.gz
bcm5719-llvm-ca27f0320bd5001db59ba22d51d80c93b20afc59.zip
VMCore support for the insertelement operation.
llvm-svn: 25408
Diffstat (limited to 'llvm/lib/VMCore/ConstantFolding.cpp')
-rw-r--r--llvm/lib/VMCore/ConstantFolding.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/llvm/lib/VMCore/ConstantFolding.cpp b/llvm/lib/VMCore/ConstantFolding.cpp
index 3f7bc7db33a..a6fbf42fa24 100644
--- a/llvm/lib/VMCore/ConstantFolding.cpp
+++ b/llvm/lib/VMCore/ConstantFolding.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) {
OpenPOWER on IntegriCloud