@@ -71,50 +71,51 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
71
71
if (SrcTy == DestTy)
72
72
return V; // no-op cast
73
73
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);
79
76
77
+ // Handle ConstantInt -> ConstantFP
78
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
80
79
// Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
81
80
// This allows for other simplifications (although some of them
82
81
// 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);
88
84
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.
91
86
// 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 ;
96
90
97
- // Otherwise, can't fold this (vector?)
98
- return nullptr ;
91
+ return ConstantFP::get (
92
+ DestTy,
93
+ APFloat (DestTy->getScalarType ()->getFltSemantics (), CI->getValue ()));
99
94
}
100
95
101
- // Handle ConstantFP input: FP -> Integral.
96
+ // Handle ConstantFP -> ConstantInt
102
97
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
+
103
104
// PPC_FP128 is really the sum of two consecutive doubles, where the first
104
105
// double is always stored first in memory, regardless of the target
105
106
// endianness. The memory layout of i128, however, depends on the target
106
107
// endianness, and so we can't fold this without target endianness
107
108
// information. This should instead be handled by
108
109
// Analysis/ConstantFolding.cpp
109
- if (FP-> getType () ->isPPC_FP128Ty ())
110
+ if (SrcTy ->isPPC_FP128Ty ())
110
111
return nullptr ;
111
112
112
113
// Make sure dest type is compatible with the folded integer constant.
113
- if (!DestTy->isIntegerTy ())
114
+ if (!DestTy->isIntOrIntVectorTy () ||
115
+ DestTy->getScalarSizeInBits () != SrcTy->getScalarSizeInBits ())
114
116
return nullptr ;
115
117
116
- return ConstantInt::get (FP->getContext (),
117
- FP->getValueAPF ().bitcastToAPInt ());
118
+ return ConstantInt::get (DestTy, FP->getValueAPF ().bitcastToAPInt ());
118
119
}
119
120
120
121
return nullptr ;
0 commit comments