@@ -295,6 +295,13 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
295
295
}
296
296
}
297
297
298
+ static Constant *foldMaybeUndesirableCast (unsigned opc, Constant *V,
299
+ Type *DestTy) {
300
+ return ConstantExpr::isDesirableCastOp (opc)
301
+ ? ConstantExpr::getCast (opc, V, DestTy)
302
+ : ConstantFoldCastInstruction (opc, V, DestTy);
303
+ }
304
+
298
305
Constant *llvm::ConstantFoldCastInstruction (unsigned opc, Constant *V,
299
306
Type *DestTy) {
300
307
if (isa<PoisonValue>(V))
@@ -320,7 +327,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
320
327
if (CE->isCast ()) {
321
328
// Try hard to fold cast of cast because they are often eliminable.
322
329
if (unsigned newOpc = foldConstantCastPair (opc, CE, DestTy))
323
- return ConstantExpr::getCast (newOpc, CE->getOperand (0 ), DestTy);
330
+ return foldMaybeUndesirableCast (newOpc, CE->getOperand (0 ), DestTy);
324
331
} else if (CE->getOpcode () == Instruction::GetElementPtr &&
325
332
// Do not fold addrspacecast (gep 0, .., 0). It might make the
326
333
// addrspacecast uncanonicalized.
@@ -357,18 +364,22 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
357
364
Type *DstEltTy = DestVecTy->getElementType ();
358
365
// Fast path for splatted constants.
359
366
if (Constant *Splat = V->getSplatValue ()) {
367
+ Constant *Res = foldMaybeUndesirableCast (opc, Splat, DstEltTy);
368
+ if (!Res)
369
+ return nullptr ;
360
370
return ConstantVector::getSplat (
361
- cast<VectorType>(DestTy)->getElementCount (),
362
- ConstantExpr::getCast (opc, Splat, DstEltTy));
371
+ cast<VectorType>(DestTy)->getElementCount (), Res);
363
372
}
364
373
SmallVector<Constant *, 16 > res;
365
374
Type *Ty = IntegerType::get (V->getContext (), 32 );
366
375
for (unsigned i = 0 ,
367
376
e = cast<FixedVectorType>(V->getType ())->getNumElements ();
368
377
i != e; ++i) {
369
- Constant *C =
370
- ConstantExpr::getExtractElement (V, ConstantInt::get (Ty, i));
371
- res.push_back (ConstantExpr::getCast (opc, C, DstEltTy));
378
+ Constant *C = ConstantExpr::getExtractElement (V, ConstantInt::get (Ty, i));
379
+ Constant *Casted = foldMaybeUndesirableCast (opc, C, DstEltTy);
380
+ if (!Casted)
381
+ return nullptr ;
382
+ res.push_back (Casted);
372
383
}
373
384
return ConstantVector::get (res);
374
385
}
0 commit comments