Skip to content

Commit 02ad3f6

Browse files
nikicakiramenai
authored andcommitted
Reapply [ConstantFold] Remove unnecessary BitCastConstantVector() (NFCI)
ConstantFoldCastInstruction() already has generic code to perform lane-wise casts for vectors. There is no need to repeate it specifically for bitcasts. However, we do need to keep the special case for vectors of -1, which is not handled elsewhere.
1 parent bd3d3fb commit 02ad3f6

File tree

1 file changed

+3
-51
lines changed

1 file changed

+3
-51
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -37,45 +37,6 @@ using namespace llvm::PatternMatch;
3737
// ConstantFold*Instruction Implementations
3838
//===----------------------------------------------------------------------===//
3939

40-
/// Convert the specified vector Constant node to the specified vector type.
41-
/// At this point, we know that the elements of the input vector constant are
42-
/// all simple integer or FP values.
43-
static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) {
44-
45-
if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
46-
if (CV->isNullValue()) return Constant::getNullValue(DstTy);
47-
48-
// Do not iterate on scalable vector. The num of elements is unknown at
49-
// compile-time.
50-
if (isa<ScalableVectorType>(DstTy))
51-
return nullptr;
52-
53-
// If this cast changes element count then we can't handle it here:
54-
// doing so requires endianness information. This should be handled by
55-
// Analysis/ConstantFolding.cpp
56-
unsigned NumElts = cast<FixedVectorType>(DstTy)->getNumElements();
57-
if (NumElts != cast<FixedVectorType>(CV->getType())->getNumElements())
58-
return nullptr;
59-
60-
Type *DstEltTy = DstTy->getElementType();
61-
// Fast path for splatted constants.
62-
if (Constant *Splat = CV->getSplatValue()) {
63-
return ConstantVector::getSplat(DstTy->getElementCount(),
64-
ConstantExpr::getBitCast(Splat, DstEltTy));
65-
}
66-
67-
SmallVector<Constant*, 16> Result;
68-
Type *Ty = IntegerType::get(CV->getContext(), 32);
69-
for (unsigned i = 0; i != NumElts; ++i) {
70-
Constant *C =
71-
ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i));
72-
C = ConstantExpr::getBitCast(C, DstEltTy);
73-
Result.push_back(C);
74-
}
75-
76-
return ConstantVector::get(Result);
77-
}
78-
7940
/// This function determines which opcode to use to fold two constant cast
8041
/// expressions together. It uses CastInst::isEliminableCastPair to determine
8142
/// the opcode. Consequently its just a wrapper around that function.
@@ -114,24 +75,15 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
11475
// Handle casts from one vector constant to another. We know that the src
11576
// and dest type have the same size (otherwise its an illegal cast).
11677
if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
117-
if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
118-
assert(DestPTy->getPrimitiveSizeInBits() ==
119-
SrcTy->getPrimitiveSizeInBits() &&
120-
"Not cast between same sized vectors!");
121-
SrcTy = nullptr;
122-
// First, check for null. Undef is already handled.
123-
if (isa<ConstantAggregateZero>(V))
124-
return Constant::getNullValue(DestTy);
125-
126-
// Handle ConstantVector and ConstantAggregateVector.
127-
return BitCastConstantVector(V, DestPTy);
128-
}
78+
if (V->isAllOnesValue())
79+
return Constant::getAllOnesValue(DestTy);
12980

13081
// Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
13182
// This allows for other simplifications (although some of them
13283
// can only be handled by Analysis/ConstantFolding.cpp).
13384
if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
13485
return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
86+
return nullptr;
13587
}
13688

13789
// Finally, implement bitcast folding now. The code below doesn't handle

0 commit comments

Comments
 (0)