llvm.org GIT mirror llvm / d59ae90
Continue improving support for ConstantDataAggregate, and use the new methods recently added to (sometimes greatly!) simplify code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149024 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 7 years ago
5 changed file(s) with 156 addition(s) and 426 deletion(s). Raw diff Collapse all Expand all
21752175
21762176 std::vector Elts;
21772177 if (StructType *STy = dyn_cast(Init->getType())) {
2178
21792178 // Break up the constant into its elements.
2180 if (ConstantStruct *CS = dyn_cast(Init)) {
2181 for (User::op_iterator i = CS->op_begin(), e = CS->op_end(); i != e; ++i)
2182 Elts.push_back(cast(*i));
2183 } else if (isa(Init)) {
2184 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2185 Elts.push_back(Constant::getNullValue(STy->getElementType(i)));
2186 } else if (isa(Init)) {
2187 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2188 Elts.push_back(UndefValue::get(STy->getElementType(i)));
2189 } else {
2190 llvm_unreachable("This code is out of sync with "
2191 " ConstantFoldLoadThroughGEPConstantExpr");
2192 }
2179 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2180 Elts.push_back(Init->getAggregateElement(i));
21932181
21942182 // Replace the element that we are supposed to.
21952183 ConstantInt *CU = cast(Addr->getOperand(OpNo));
22082196 if (ArrayType *ATy = dyn_cast(InitTy))
22092197 NumElts = ATy->getNumElements();
22102198 else
2211 NumElts = cast(InitTy)->getNumElements();
2199 NumElts = InitTy->getVectorNumElements();
22122200
22132201 // Break up the array into elements.
2214 if (ConstantArray *CA = dyn_cast(Init)) {
2215 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i)
2216 Elts.push_back(cast(*i));
2217 } else if (ConstantVector *CV = dyn_cast(Init)) {
2218 for (User::op_iterator i = CV->op_begin(), e = CV->op_end(); i != e; ++i)
2219 Elts.push_back(cast(*i));
2220 } else if (isa(Init)) {
2221 Elts.assign(NumElts, Constant::getNullValue(InitTy->getElementType()));
2222 } else {
2223 assert(isa(Init) && "This code is out of sync with "
2224 " ConstantFoldLoadThroughGEPConstantExpr");
2225 Elts.assign(NumElts, UndefValue::get(InitTy->getElementType()));
2226 }
2202 for (uint64_t i = 0, e = NumElts; i != e; ++i)
2203 Elts.push_back(Init->getAggregateElement(i));
22272204
22282205 assert(CI->getZExtValue() < NumElts);
22292206 Elts[CI->getZExtValue()] =
12691269 return ReplaceInstUsesWith(EV, Agg);
12701270
12711271 if (Constant *C = dyn_cast(Agg)) {
1272 if (isa(C))
1273 return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType()));
1274
1275 if (isa(C))
1276 return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType()));
1277
1278 if (isa(C) || isa(C)) {
1279 // Extract the element indexed by the first index out of the constant
1280 Value *V = C->getOperand(*EV.idx_begin());
1281 if (EV.getNumIndices() > 1)
1282 // Extract the remaining indices out of the constant indexed by the
1283 // first index
1284 return ExtractValueInst::Create(V, EV.getIndices().slice(1));
1285 else
1286 return ReplaceInstUsesWith(EV, V);
1272 if (Constant *C2 = C->getAggregateElement(*EV.idx_begin())) {
1273 if (EV.getNumIndices() == 0)
1274 return ReplaceInstUsesWith(EV, C2);
1275 // Extract the remaining indices out of the constant indexed by the
1276 // first index
1277 return ExtractValueInst::Create(C2, EV.getIndices().slice(1));
12871278 }
12881279 return 0; // Can't handle other constants
1289 }
1280 }
1281
12901282 if (InsertValueInst *IV = dyn_cast(Agg)) {
12911283 // We're extracting from an insertvalue instruction, compare the indices
12921284 const unsigned *exti, *exte, *insi, *inse;
407407 return LV; // Common case, already in the map.
408408
409409 if (Constant *C = dyn_cast(V)) {
410 if (isa(C))
410 Constant *Elt = C->getAggregateElement(i);
411
412 if (Elt == 0)
413 LV.markOverdefined(); // Unknown sort of constant.
414 else if (isa(Elt))
411415 ; // Undef values remain undefined.
412 else if (ConstantStruct *CS = dyn_cast(C))
413 LV.markConstant(CS->getOperand(i)); // Constants are constant.
414 else if (isa(C)) {
415 Type *FieldTy = cast(V->getType())->getElementType(i);
416 LV.markConstant(Constant::getNullValue(FieldTy));
417 } else
418 LV.markOverdefined(); // Unknown sort of constant.
416 else
417 LV.markConstant(Elt); // Constants are constant.
419418 }
420419
421420 // All others are underdefined by default.
854854 }
855855 return;
856856 }
857
858 if (const ConstantDataArray *CA = dyn_cast(CV)) {
859 // As a special case, print the array as a string if it is an array of
860 // i8 with ConstantInt values.
861 if (CA->isString()) {
862 Out << "c\"";
863 PrintEscapedString(CA->getAsString(), Out);
864 Out << '"';
865 return;
866 }
867
868 Type *ETy = CA->getType()->getElementType();
869 Out << '[';
870 if (CA->getNumOperands()) {
871 TypePrinter.print(ETy, Out);
872 Out << ' ';
873 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
874 &TypePrinter, Machine,
875 Context);
876 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
877 Out << ", ";
878 TypePrinter.print(ETy, Out);
879 Out << ' ';
880 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
881 Machine, Context);
882 }
883 Out << ']';
884 }
885 return;
886 }
887
857888
858889 if (const ConstantStruct *CS = dyn_cast(CV)) {
859890 if (CS->getType()->isPacked())
885916 return;
886917 }
887918
888 if (const ConstantVector *CP = dyn_cast(CV)) {
889 Type *ETy = CP->getType()->getElementType();
890 assert(CP->getNumOperands() > 0 &&
891 "Number of operands for a PackedConst must be > 0");
919 if (isa(CV) || isa(CV)) {
920 Type *ETy = CV->getType()->getVectorElementType();
892921 Out << '<';
893922 TypePrinter.print(ETy, Out);
894923 Out << ' ';
895 WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine,
896 Context);
897 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
924 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
925 Machine, Context);
926 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
898927 Out << ", ";
899928 TypePrinter.print(ETy, Out);
900929 Out << ' ';
901 WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine,
902 Context);
930 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
931 Machine, Context);
903932 }
904933 Out << '>';
905934 return;
3737 // ConstantFold*Instruction Implementations
3838 //===----------------------------------------------------------------------===//
3939
40 /// BitCastConstantVector - Convert the specified ConstantVector node to the
40 /// BitCastConstantVector - Convert the specified vector Constant node to the
4141 /// specified vector type. At this point, we know that the elements of the
4242 /// input vector constant are all simple integer or FP values.
43 static Constant *BitCastConstantVector(ConstantVector *CV,
44 VectorType *DstTy) {
43 static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) {
4544
4645 if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
4746 if (CV->isNullValue()) return Constant::getNullValue(DstTy);
5049 // doing so requires endianness information. This should be handled by
5150 // Analysis/ConstantFolding.cpp
5251 unsigned NumElts = DstTy->getNumElements();
53 if (NumElts != CV->getNumOperands())
52 if (NumElts != CV->getType()->getVectorNumElements())
5453 return 0;
54
55 Type *DstEltTy = DstTy->getElementType();
5556
5657 // Check to verify that all elements of the input are simple.
58 SmallVector Result;
5759 for (unsigned i = 0; i != NumElts; ++i) {
58 if (!isa(CV->getOperand(i)) &&
59 !isa(CV->getOperand(i)))
60 return 0;
61 }
62
63 // Bitcast each element now.
64 std::vector Result;
65 Type *DstEltTy = DstTy->getElementType();
66 for (unsigned i = 0; i != NumElts; ++i)
67 Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i),
68 DstEltTy));
60 Constant *C = CV->getAggregateElement(i);
61 if (C == 0) return 0;
62 C = ConstantExpr::getBitCast(C, DstEltTy);
63 if (isa(C)) return 0;
64 Result.push_back(C);
65 }
66
6967 return ConstantVector::get(Result);
7068 }
7169
141139 if (isa(V))
142140 return Constant::getNullValue(DestTy);
143141
144 if (ConstantVector *CV = dyn_cast(V))
145 return BitCastConstantVector(CV, DestPTy);
142 // Handle ConstantVector and ConstantAggregateVector.
143 return BitCastConstantVector(V, DestPTy);
146144 }
147145
148146 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
691689
692690 Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
693691 Constant *V1, Constant *V2) {
694 if (ConstantInt *CB = dyn_cast(Cond))
695 return CB->getZExtValue() ? V1 : V2;
696
697 // Check for zero aggregate and ConstantVector of zeros
692 // Check for i1 and vector true/false conditions.
698693 if (Cond->isNullValue()) return V2;
699
700 if (ConstantVector* CondV = dyn_cast(Cond)) {
701
702 if (CondV->isAllOnesValue()) return V1;
703
704 VectorType *VTy = cast(V1->getType());
705 ConstantVector *CP1 = dyn_cast(V1);
706 ConstantVector *CP2 = dyn_cast(V2);
707
708 if ((CP1 || isa(V1)) &&
709 (CP2 || isa(V2))) {
710
711 // Find the element type of the returned vector
712 Type *EltTy = VTy->getElementType();
713 unsigned NumElem = VTy->getNumElements();
714 std::vector Res(NumElem);
715
716 bool Valid = true;
717 for (unsigned i = 0; i < NumElem; ++i) {
718 ConstantInt* c = dyn_cast(CondV->getOperand(i));
719 if (!c) {
720 Valid = false;
721 break;
722 }
723 Constant *C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
724 Constant *C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
725 Res[i] = c->getZExtValue() ? C1 : C2;
726 }
727 // If we were able to build the vector, return it
728 if (Valid) return ConstantVector::get(Res);
729 }
694 if (Cond->isAllOnesValue()) return V1;
695
696 // FIXME: CDV Condition.
697 // If the condition is a vector constant, fold the result elementwise.
698 if (ConstantVector *CondV = dyn_cast(Cond)) {
699 SmallVector Result;
700 for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){
701 ConstantInt *Cond = dyn_cast(CondV->getOperand(i));
702 if (Cond == 0) break;
703
704 Constant *Res = (Cond->getZExtValue() ? V2 : V1)->getAggregateElement(i);
705 if (Res == 0) break;
706 Result.push_back(Res);
707 }
708
709 // If we were able to build the vector, return it.
710 if (Result.size() == V1->getType()->getVectorNumElements())
711 return ConstantVector::get(Result);
730712 }
731713
732714
780762 Constant *Idx) {
781763 ConstantInt *CIdx = dyn_cast(Idx);
782764 if (!CIdx) return 0;
783 APInt idxVal = CIdx->getValue();
784 if (isa(Val)) {
785 // Insertion of scalar constant into vector undef
786 // Optimize away insertion of undef
787 if (isa(Elt))
788 return Val;
789 // Otherwise break the aggregate undef into multiple undefs and do
790 // the insertion
791 unsigned numOps =
792 cast(Val->getType())->getNumElements();
793 std::vector Ops;
794 Ops.reserve(numOps);
795 for (unsigned i = 0; i < numOps; ++i) {
796 Constant *Op =
797 (idxVal == i) ? Elt : UndefValue::get(Elt->getType());
798 Ops.push_back(Op);
799 }
800 return ConstantVector::get(Ops);
801 }
802 if (isa(Val)) {
803 // Insertion of scalar constant into vector aggregate zero
804 // Optimize away insertion of zero
805 if (Elt->isNullValue())
806 return Val;
807 // Otherwise break the aggregate zero into multiple zeros and do
808 // the insertion
809 unsigned numOps =
810 cast(Val->getType())->getNumElements();
811 std::vector Ops;
812 Ops.reserve(numOps);
813 for (unsigned i = 0; i < numOps; ++i) {
814 Constant *Op =
815 (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
816 Ops.push_back(Op);
817 }
818 return ConstantVector::get(Ops);
819 }
820 if (ConstantVector *CVal = dyn_cast(Val)) {
821 // Insertion of scalar constant into vector constant
822 std::vector Ops;
823 Ops.reserve(CVal->getNumOperands());
824 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
825 Constant *Op =
826 (idxVal == i) ? Elt : cast(CVal->getOperand(i));
827 Ops.push_back(Op);
828 }
829 return ConstantVector::get(Ops);
830 }
831
832 return 0;
833 }
834
835 /// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef
836 /// return the specified element value. Otherwise return null.
837 static Constant *GetVectorElement(Constant *C, unsigned EltNo) {
838 if (ConstantVector *CV = dyn_cast(C))
839 return CV->getOperand(EltNo);
840
841 Type *EltTy = cast(C->getType())->getElementType();
842 if (isa(C))
843 return Constant::getNullValue(EltTy);
844 if (isa(C))
845 return UndefValue::get(EltTy);
846 return 0;
765 const APInt &IdxVal = CIdx->getValue();
766
767 SmallVector Result;
768 for (unsigned i = 0, e = Val->getType()->getVectorNumElements(); i != e; ++i){
769 if (i == IdxVal) {
770 Result.push_back(Elt);
771 continue;
772 }
773
774 if (Constant *C = Val->getAggregateElement(i))
775 Result.push_back(C);
776 else
777 return 0;
778 }
779
780 return ConstantVector::get(Result);
847781 }
848782
849783 Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1,
859793 // Loop over the shuffle mask, evaluating each element.
860794 SmallVector Result;
861795 for (unsigned i = 0; i != MaskNumElts; ++i) {
862 Constant *InElt = GetVectorElement(Mask, i);
796 Constant *InElt = Mask->getAggregateElement(i);
863797 if (InElt == 0) return 0;
864798
865 if (isa(InElt))
799 if (isa(InElt)) {
800 Result.push_back(UndefValue::get(EltTy));
801 continue;
802 }
803 unsigned Elt = cast(InElt)->getZExtValue();
804 if (Elt >= SrcNumElts*2)
866805 InElt = UndefValue::get(EltTy);
867 else if (ConstantInt *CI = dyn_cast(InElt)) {
868 unsigned Elt = CI->getZExtValue();
869 if (Elt >= SrcNumElts*2)
870 InElt = UndefValue::get(EltTy);
871 else if (Elt >= SrcNumElts)
872 InElt = GetVectorElement(V2, Elt - SrcNumElts);
873 else
874 InElt = GetVectorElement(V1, Elt);
875 if (InElt == 0) return 0;
876 } else {
877 // Unknown value.
878 return 0;
879 }
806 else if (Elt >= SrcNumElts)
807 InElt = V2->getAggregateElement(Elt - SrcNumElts);
808 else
809 InElt = V1->getAggregateElement(Elt);
810 if (InElt == 0) return 0;
880811 Result.push_back(InElt);
881812 }
882813
889820 if (Idxs.empty())
890821 return Agg;
891822
892 if (isa(Agg)) // ev(undef, x) -> undef
893 return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(),
894 Idxs));
895
896 if (isa(Agg)) // ev(0, x) -> 0
897 return
898 Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
899 Idxs));
900
901 // Otherwise recurse.
902 if (ConstantStruct *CS = dyn_cast(Agg))
903 return ConstantFoldExtractValueInstruction(CS->getOperand(Idxs[0]),
904 Idxs.slice(1));
905
906 if (ConstantArray *CA = dyn_cast(Agg))
907 return ConstantFoldExtractValueInstruction(CA->getOperand(Idxs[0]),
908 Idxs.slice(1));
909 ConstantVector *CV = cast(Agg);
910 return ConstantFoldExtractValueInstruction(CV->getOperand(Idxs[0]),
911 Idxs.slice(1));
823 if (Constant *C = Agg->getAggregateElement(Idxs[0]))
824 return ConstantFoldExtractValueInstruction(C, Idxs.slice(1));
825
826 return 0;
912827 }
913828
914829 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
918833 if (Idxs.empty())
919834 return Val;
920835
921 if (isa(Agg)) {
922 // Insertion of constant into aggregate undef
923 // Optimize away insertion of undef.
924 if (isa(Val))
925 return Agg;
836 unsigned NumElts;
837 if (StructType *ST = dyn_cast(Agg->getType()))
838 NumElts = ST->getNumElements();
839 else if (ArrayType *AT = dyn_cast(Agg->getType()))
840 NumElts = AT->getNumElements();
841 else
842 NumElts = AT->getVectorNumElements();
843
844 SmallVector Result;
845 for (unsigned i = 0; i != NumElts; ++i) {
846 Constant *C = Agg->getAggregateElement(i);
847 if (C == 0) return 0;
926848
927 // Otherwise break the aggregate undef into multiple undefs and do
928 // the insertion.
929 CompositeType *AggTy = cast(Agg->getType());
930 unsigned numOps;
931 if (ArrayType *AR = dyn_cast(AggTy))
932 numOps = AR->getNumElements();
933 else
934 numOps = cast(AggTy)->getNumElements();
849 if (Idxs[0] == i)
850 C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1));
935851
936 std::vector Ops(numOps);
937 for (unsigned i = 0; i < numOps; ++i) {
938 Type *MemberTy = AggTy->getTypeAtIndex(i);
939 Constant *Op =
940 (Idxs[0] == i) ?
941 ConstantFoldInsertValueInstruction(UndefValue::get(MemberTy),
942 Val, Idxs.slice(1)) :
943 UndefValue::get(MemberTy);
944 Ops[i] = Op;
945 }
946
947 if (StructType* ST = dyn_cast(AggTy))
948 return ConstantStruct::get(ST, Ops);
949 return ConstantArray::get(cast(AggTy), Ops);
852 Result.push_back(C);
950853 }
951854
952 if (isa(Agg)) {
953 // Insertion of constant into aggregate zero
954 // Optimize away insertion of zero.
955 if (Val->isNullValue())
956 return Agg;
957
958 // Otherwise break the aggregate zero into multiple zeros and do
959 // the insertion.
960 CompositeType *AggTy = cast(Agg->getType());
961 unsigned numOps;
962 if (ArrayType *AR = dyn_cast(AggTy))
963 numOps = AR->getNumElements();
964 else
965 numOps = cast(AggTy)->getNumElements();
966
967 std::vector Ops(numOps);
968 for (unsigned i = 0; i < numOps; ++i) {
969 Type *MemberTy = AggTy->getTypeAtIndex(i);
970 Constant *Op =
971 (Idxs[0] == i) ?
972 ConstantFoldInsertValueInstruction(Constant::getNullValue(MemberTy),
973 Val, Idxs.slice(1)) :
974 Constant::getNullValue(MemberTy);
975 Ops[i] = Op;
976 }
977
978 if (StructType *ST = dyn_cast(AggTy))
979 return ConstantStruct::get(ST, Ops);
980 return ConstantArray::get(cast(AggTy), Ops);
981 }
982
983 if (isa(Agg) || isa(Agg)) {
984 // Insertion of constant into aggregate constant.
985 std::vector Ops(Agg->getNumOperands());
986 for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
987 Constant *Op = cast(Agg->getOperand(i));
988 if (Idxs[0] == i)
989 Op = ConstantFoldInsertValueInstruction(Op, Val, Idxs.slice(1));
990 Ops[i] = Op;
991 }
992
993 if (StructType* ST = dyn_cast(Agg->getType()))
994 return ConstantStruct::get(ST, Ops);
995 return ConstantArray::get(cast(Agg->getType()), Ops);
996 }
997
998 return 0;
855 if (StructType *ST = dyn_cast(Agg->getType()))
856 return ConstantStruct::get(ST, Result);
857 if (ArrayType *AT = dyn_cast(Agg->getType()))
858 return ConstantArray::get(AT, Result);
859 return ConstantVector::get(Result);
999860 }
1000861
1001862
11731034 // At this point we know neither constant is an UndefValue.
11741035 if (ConstantInt *CI1 = dyn_cast(C1)) {
11751036 if (ConstantInt *CI2 = dyn_cast(C2)) {
1176 using namespace APIntOps;
11771037 const APInt &C1V = CI1->getValue();
11781038 const APInt &C2V = CI2->getValue();
11791039 switch (Opcode) {
12701130 }
12711131 }
12721132 } else if (VectorType *VTy = dyn_cast(C1->getType())) {
1273 ConstantVector *CP1 = dyn_cast(C1);
1274 ConstantVector *CP2 = dyn_cast(C2);
1275 if ((CP1 != NULL || isa(C1)) &&
1276 (CP2 != NULL || isa(C2))) {
1277 std::vector Res;
1278 Type* EltTy = VTy->getElementType();
1279 Constant *C1 = 0;
1280 Constant *C2 = 0;
1281 switch (Opcode) {
1282 default:
1283 break;
1284 case Instruction::Add:
1285 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1286 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1287 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1288 Res.push_back(ConstantExpr::getAdd(C1, C2));
1289 }
1290 return ConstantVector::get(Res);
1291 case Instruction::FAdd:
1292 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1293 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1294 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1295 Res.push_back(ConstantExpr::getFAdd(C1, C2));
1296 }
1297 return ConstantVector::get(Res);
1298 case Instruction::Sub:
1299 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1300 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1301 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1302 Res.push_back(ConstantExpr::getSub(C1, C2));
1303 }
1304 return ConstantVector::get(Res);
1305 case Instruction::FSub:
1306 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1307 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1308 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1309 Res.push_back(ConstantExpr::getFSub(C1, C2));
1310 }
1311 return ConstantVector::get(Res);
1312 case Instruction::Mul:
1313 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1314 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1315 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1316 Res.push_back(ConstantExpr::getMul(C1, C2));
1317 }
1318 return ConstantVector::get(Res);
1319 case Instruction::FMul:
1320 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1321 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1322 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1323 Res.push_back(ConstantExpr::getFMul(C1, C2));
1324 }
1325 return ConstantVector::get(Res);
1326 case Instruction::UDiv:
1327 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1328 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1329 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1330 Res.push_back(ConstantExpr::getUDiv(C1, C2));
1331 }
1332 return ConstantVector::get(Res);
1333 case Instruction::SDiv:
1334 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1335 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1336 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1337 Res.push_back(ConstantExpr::getSDiv(C1, C2));
1338 }
1339 return ConstantVector::get(Res);
1340 case Instruction::FDiv:
1341 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1342 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1343 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1344 Res.push_back(ConstantExpr::getFDiv(C1, C2));
1345 }
1346 return ConstantVector::get(Res);
1347 case Instruction::URem:
1348 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1349 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1350 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1351 Res.push_back(ConstantExpr::getURem(C1, C2));
1352 }
1353 return ConstantVector::get(Res);
1354 case Instruction::SRem:
1355 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1356 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1357 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1358 Res.push_back(ConstantExpr::getSRem(C1, C2));
1359 }
1360 return ConstantVector::get(Res);
1361 case Instruction::FRem:
1362 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1363 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1364 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1365 Res.push_back(ConstantExpr::getFRem(C1, C2));
1366 }
1367 return ConstantVector::get(Res);
1368 case Instruction::And:
1369 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1370 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1371 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1372 Res.push_back(ConstantExpr::getAnd(C1, C2));
1373 }
1374 return ConstantVector::get(Res);
1375 case Instruction::Or:
1376 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1377 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1378 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1379 Res.push_back(ConstantExpr::getOr(C1, C2));
1380 }
1381 return ConstantVector::get(Res);
1382 case Instruction::Xor:
1383 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1384 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1385 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1386 Res.push_back(ConstantExpr::getXor(C1, C2));
1387 }
1388 return ConstantVector::get(Res);
1389 case Instruction::LShr:
1390 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1391 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1392 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1393 Res.push_back(ConstantExpr::getLShr(C1, C2));
1394 }
1395 return ConstantVector::get(Res);
1396 case Instruction::AShr:
1397 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1398 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1399 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1400 Res.push_back(ConstantExpr::getAShr(C1, C2));
1401 }
1402 return ConstantVector::get(Res);
1403 case Instruction::Shl:
1404 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1405 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1406 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1407 Res.push_back(ConstantExpr::getShl(C1, C2));
1408 }
1409 return ConstantVector::get(Res);
1410 }
1411 }
1133 // Perform elementwise folding.
1134 SmallVector Result;
1135 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1136 Constant *LHS = C1->getAggregateElement(i);
1137 Constant *RHS = C2->getAggregateElement(i);
1138 if (LHS == 0 || RHS == 0) break;
1139
1140 Result.push_back(ConstantExpr::get(Opcode, LHS, RHS));
1141 }
1142
1143 if (Result.size() == VTy->getNumElements())
1144 return ConstantVector::get(Result);
14121145 }
14131146
14141147 if (ConstantExpr *CE1 = dyn_cast(C1)) {