@@ -71,50 +71,51 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
7171 if (SrcTy == DestTy)
7272 return V; // no-op cast
7373
74- // Handle casts from one vector constant to another. We know that the src
75- // and dest type have the same size (otherwise its an illegal cast).
76- if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
77- if (V->isAllOnesValue ())
78- return Constant::getAllOnesValue (DestTy);
74+ if (V->isAllOnesValue ())
75+ return Constant::getAllOnesValue (DestTy);
7976
77+ // Handle ConstantInt -> ConstantFP
78+ if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
8079 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
8180 // This allows for other simplifications (although some of them
8281 // can only be handled by Analysis/ConstantFolding.cpp).
83- if (!isa<VectorType>(SrcTy))
84- if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
85- return ConstantExpr::getBitCast (ConstantVector::get (V), DestPTy);
86- return nullptr ;
87- }
82+ if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
83+ return ConstantExpr::getBitCast (ConstantVector::get (V), DestTy);
8884
89- // Handle integral constant input.
90- if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
85+ // Make sure dest type is compatible with the folded fp constant.
9186 // See note below regarding the PPC_FP128 restriction.
92- if (DestTy->isFloatingPointTy () && !DestTy->isPPC_FP128Ty ())
93- return ConstantFP::get (DestTy->getContext (),
94- APFloat (DestTy->getFltSemantics (),
95- CI->getValue ()));
87+ if (!DestTy->isFPOrFPVectorTy () || DestTy->isPPC_FP128Ty () ||
88+ DestTy->getScalarSizeInBits () != SrcTy->getScalarSizeInBits ())
89+ return nullptr ;
9690
97- // Otherwise, can't fold this (vector?)
98- return nullptr ;
91+ return ConstantFP::get (
92+ DestTy,
93+ APFloat (DestTy->getScalarType ()->getFltSemantics (), CI->getValue ()));
9994 }
10095
101- // Handle ConstantFP input: FP -> Integral.
96+ // Handle ConstantFP -> ConstantInt
10297 if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
98+ // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
99+ // This allows for other simplifications (although some of them
100+ // can only be handled by Analysis/ConstantFolding.cpp).
101+ if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
102+ return ConstantExpr::getBitCast (ConstantVector::get (V), DestTy);
103+
103104 // PPC_FP128 is really the sum of two consecutive doubles, where the first
104105 // double is always stored first in memory, regardless of the target
105106 // endianness. The memory layout of i128, however, depends on the target
106107 // endianness, and so we can't fold this without target endianness
107108 // information. This should instead be handled by
108109 // Analysis/ConstantFolding.cpp
109- if (FP-> getType () ->isPPC_FP128Ty ())
110+ if (SrcTy ->isPPC_FP128Ty ())
110111 return nullptr ;
111112
112113 // Make sure dest type is compatible with the folded integer constant.
113- if (!DestTy->isIntegerTy ())
114+ if (!DestTy->isIntOrIntVectorTy () ||
115+ DestTy->getScalarSizeInBits () != SrcTy->getScalarSizeInBits ())
114116 return nullptr ;
115117
116- return ConstantInt::get (FP->getContext (),
117- FP->getValueAPF ().bitcastToAPInt ());
118+ return ConstantInt::get (DestTy, FP->getValueAPF ().bitcastToAPInt ());
118119 }
119120
120121 return nullptr ;
0 commit comments