+ if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond))
+ return const_cast<Constant*>(CB->getValue() ? V1 : V2);
+
+ if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
+ if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
+ if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
+ if (V1 == V2) return const_cast<Constant*>(V1);
+ return 0;
+}
+
+Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
+ const Constant *Idx) {
+ if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
+ return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
+ if (Val->isNullValue()) // ee(zero, x) -> zero
+ return Constant::getNullValue(
+ cast<PackedType>(Val->getType())->getElementType());
+
+ if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
+ if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
+ return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
+ } else if (isa<UndefValue>(Idx)) {
+ // ee({w,x,y,z}, undef) -> w (an arbitrary value).
+ return const_cast<Constant*>(CVal->getOperand(0));
+ }
+ }
+ return 0;
+}
+
+Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
+ const Constant *Elt,
+ const Constant *Idx) {
+ const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
+ if (!CIdx) return 0;
+ uint64_t idxVal = CIdx->getZExtValue();
+ 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;
+}
+
+Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
+ const Constant *V2,
+ const Constant *Mask) {
+ // TODO: