[llvm] [ConstantFold] Support byte values in `bitcast` constant folding (PR #188030)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 18 11:17:22 PDT 2026


================
@@ -68,26 +68,53 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
   if (V->isAllOnesValue())
     return Constant::getAllOnesValue(DestTy);
 
-  // Handle ConstantInt -> ConstantFP
+  // Handle ConstantInt -> Constant{Byte, FP}
   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
     // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
     // This allows for other simplifications (although some of them
     // can only be handled by Analysis/ConstantFolding.cpp).
     if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
       return ConstantExpr::getBitCast(ConstantVector::get(V), DestTy);
 
+    if (DestTy->isByteTy() &&
+        DestTy->getScalarSizeInBits() == SrcTy->getScalarSizeInBits())
+      return ConstantByte::get(DestTy->getContext(), CI->getValue());
+
     // Make sure dest type is compatible with the folded fp constant.
     // See note below regarding the PPC_FP128 restriction.
-    if (!DestTy->isFPOrFPVectorTy() || DestTy->isPPC_FP128Ty() ||
-        DestTy->getScalarSizeInBits() != SrcTy->getScalarSizeInBits())
-      return nullptr;
+    if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty() &&
----------------
dtcxzyw wrote:

It rejects `vector int -> vector fp` cast. If `DestTy` is a scalar fp type, the call to `getScalarType` below is unnecessary. See also flags `use-constant-[int|fp]-for-[scalable/fixed-length]-splat`.


https://github.com/llvm/llvm-project/pull/188030


More information about the llvm-commits mailing list