[llvm] 67a902f - [LLVM] Refactor ConstantFP member functions to rely on its native vector support. (#197931)
via llvm-commits
llvm-commits at lists.llvm.org
Mon May 18 04:30:22 PDT 2026
Author: Paul Walker
Date: 2026-05-18T12:30:17+01:00
New Revision: 67a902f209437b700810893664150c5816337e3f
URL: https://github.com/llvm/llvm-project/commit/67a902f209437b700810893664150c5816337e3f
DIFF: https://github.com/llvm/llvm-project/commit/67a902f209437b700810893664150c5816337e3f.diff
LOG: [LLVM] Refactor ConstantFP member functions to rely on its native vector support. (#197931)
ConstantFP has native vector support so many of its member functions can
be simplified by removing the ConstantVector indirection and can return
a ConstantFP* rather than a generic Constant*.
NOTE: The changes to ConstantFolding.cpp are reverting part of
https://github.com/llvm/llvm-project/pull/193254 that was necessary at
the time.
Added:
Modified:
llvm/include/llvm/IR/Constants.h
llvm/lib/Analysis/ConstantFolding.cpp
llvm/lib/IR/Constants.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index d5b977c5f8430..38232dd54ba08 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -440,23 +440,23 @@ class ConstantFP final : public ConstantData {
/// for the specified value in the specified type. This should only be used
/// for simple constant values like 2.0/1.0 etc, that are known-valid both as
/// host double and as the target format.
- LLVM_ABI static Constant *get(Type *Ty, double V);
+ LLVM_ABI static ConstantFP *get(Type *Ty, double V);
/// If Ty is a vector type, return a Constant with a splat of the given
/// value. Otherwise return a ConstantFP for the given value.
- LLVM_ABI static Constant *get(Type *Ty, const APFloat &V);
+ LLVM_ABI static ConstantFP *get(Type *Ty, const APFloat &V);
- LLVM_ABI static Constant *get(Type *Ty, StringRef Str);
+ LLVM_ABI static ConstantFP *get(Type *Ty, StringRef Str);
LLVM_ABI static ConstantFP *get(LLVMContext &Context, const APFloat &V);
- LLVM_ABI static Constant *getNaN(Type *Ty, bool Negative = false,
- uint64_t Payload = 0);
- LLVM_ABI static Constant *getQNaN(Type *Ty, bool Negative = false,
- APInt *Payload = nullptr);
- LLVM_ABI static Constant *getSNaN(Type *Ty, bool Negative = false,
- APInt *Payload = nullptr);
- LLVM_ABI static Constant *getZero(Type *Ty, bool Negative = false);
- static Constant *getNegativeZero(Type *Ty) { return getZero(Ty, true); }
- LLVM_ABI static Constant *getInfinity(Type *Ty, bool Negative = false);
+ LLVM_ABI static ConstantFP *getNaN(Type *Ty, bool Negative = false,
+ uint64_t Payload = 0);
+ LLVM_ABI static ConstantFP *getQNaN(Type *Ty, bool Negative = false,
+ APInt *Payload = nullptr);
+ LLVM_ABI static ConstantFP *getSNaN(Type *Ty, bool Negative = false,
+ APInt *Payload = nullptr);
+ LLVM_ABI static ConstantFP *getZero(Type *Ty, bool Negative = false);
+ static ConstantFP *getNegativeZero(Type *Ty) { return getZero(Ty, true); }
+ LLVM_ABI static ConstantFP *getInfinity(Type *Ty, bool Negative = false);
/// Return true if Ty is big enough to represent V.
LLVM_ABI static bool isValueValidForType(Type *Ty, const APFloat &V);
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 3badc43beb6bf..5be3788e872d5 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1462,8 +1462,8 @@ Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
return ConstantFoldBinaryInstruction(Opcode, LHS, RHS);
}
-static Constant *flushDenormalConstant(Type *Ty, const APFloat &APF,
- DenormalMode::DenormalModeKind Mode) {
+static ConstantFP *flushDenormalConstant(Type *Ty, const APFloat &APF,
+ DenormalMode::DenormalModeKind Mode) {
switch (Mode) {
case DenormalMode::Dynamic:
return nullptr;
@@ -1490,9 +1490,9 @@ static DenormalMode getInstrDenormalMode(const Instruction *CtxI, Type *Ty) {
Ty->getScalarType()->getFltSemantics());
}
-static Constant *flushDenormalConstantFP(ConstantFP *CFP,
- const Instruction *Inst,
- bool IsOutput) {
+static ConstantFP *flushDenormalConstantFP(ConstantFP *CFP,
+ const Instruction *Inst,
+ bool IsOutput) {
const APFloat &APF = CFP->getValueAPF();
if (!APF.isDenormal())
return CFP;
@@ -1514,7 +1514,7 @@ Constant *llvm::FlushFPConstant(Constant *Operand, const Instruction *Inst,
VectorType *VecTy = dyn_cast<VectorType>(Ty);
if (VecTy) {
if (auto *Splat = dyn_cast_or_null<ConstantFP>(Operand->getSplatValue())) {
- Constant *Folded = flushDenormalConstantFP(Splat, Inst, IsOutput);
+ ConstantFP *Folded = flushDenormalConstantFP(Splat, Inst, IsOutput);
if (!Folded)
return nullptr;
return ConstantVector::getSplat(VecTy->getElementCount(), Folded);
@@ -1539,7 +1539,7 @@ Constant *llvm::FlushFPConstant(Constant *Operand, const Instruction *Inst,
if (!CFP)
return nullptr;
- Constant *Folded = flushDenormalConstantFP(CFP, Inst, IsOutput);
+ ConstantFP *Folded = flushDenormalConstantFP(CFP, Inst, IsOutput);
if (!Folded)
return nullptr;
NewElts.push_back(Folded);
@@ -1556,7 +1556,7 @@ Constant *llvm::FlushFPConstant(Constant *Operand, const Instruction *Inst,
NewElts.push_back(ConstantFP::get(Ty, Elt));
} else {
DenormalMode Mode = getInstrDenormalMode(Inst, Ty);
- Constant *Folded =
+ ConstantFP *Folded =
flushDenormalConstant(Ty, Elt, IsOutput ? Mode.Output : Mode.Input);
if (!Folded)
return nullptr;
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 179fa15f6b5c5..d02b5af6c7748 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -1130,92 +1130,71 @@ void ConstantByte::destroyConstantImpl() {
// ConstantFP
//===----------------------------------------------------------------------===//
-Constant *ConstantFP::get(Type *Ty, double V) {
+ConstantFP *ConstantFP::get(Type *Ty, double V) {
LLVMContext &Context = Ty->getContext();
APFloat FV(V);
bool ignored;
FV.convert(Ty->getScalarType()->getFltSemantics(),
APFloat::rmNearestTiesToEven, &ignored);
- Constant *C = get(Context, FV);
- // For vectors, broadcast the value.
if (VectorType *VTy = dyn_cast<VectorType>(Ty))
- return ConstantVector::getSplat(VTy->getElementCount(), C);
+ return get(Context, VTy->getElementCount(), FV);
- return C;
+ return get(Context, FV);
}
-Constant *ConstantFP::get(Type *Ty, const APFloat &V) {
- ConstantFP *C = get(Ty->getContext(), V);
- assert(C->getType() == Ty->getScalarType() &&
+ConstantFP *ConstantFP::get(Type *Ty, const APFloat &V) {
+ LLVMContext &Context = Ty->getContext();
+ assert(Ty->getScalarType() ==
+ Type::getFloatingPointTy(Context, V.getSemantics()) &&
"ConstantFP type doesn't match the type implied by its value!");
- // For vectors, broadcast the value.
if (auto *VTy = dyn_cast<VectorType>(Ty))
- return ConstantVector::getSplat(VTy->getElementCount(), C);
+ return get(Context, VTy->getElementCount(), V);
- return C;
+ return get(Ty->getContext(), V);
}
-Constant *ConstantFP::get(Type *Ty, StringRef Str) {
+ConstantFP *ConstantFP::get(Type *Ty, StringRef Str) {
LLVMContext &Context = Ty->getContext();
-
APFloat FV(Ty->getScalarType()->getFltSemantics(), Str);
- Constant *C = get(Context, FV);
- // For vectors, broadcast the value.
if (VectorType *VTy = dyn_cast<VectorType>(Ty))
- return ConstantVector::getSplat(VTy->getElementCount(), C);
+ return get(Context, VTy->getElementCount(), FV);
- return C;
+ return get(Context, FV);
}
-Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) {
+ConstantFP *ConstantFP::getInfinity(Type *Ty, bool Negative) {
const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
- APFloat NaN = APFloat::getNaN(Semantics, Negative, Payload);
- Constant *C = get(Ty->getContext(), NaN);
-
- if (VectorType *VTy = dyn_cast<VectorType>(Ty))
- return ConstantVector::getSplat(VTy->getElementCount(), C);
+ return get(Ty, APFloat::getInf(Semantics, Negative));
+}
- return C;
+ConstantFP *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) {
+ const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
+ APFloat NaN = APFloat::getNaN(Semantics, Negative, Payload);
+ return get(Ty, NaN);
}
-Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) {
+ConstantFP *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) {
const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
APFloat NaN = APFloat::getQNaN(Semantics, Negative, Payload);
- Constant *C = get(Ty->getContext(), NaN);
-
- if (VectorType *VTy = dyn_cast<VectorType>(Ty))
- return ConstantVector::getSplat(VTy->getElementCount(), C);
-
- return C;
+ return get(Ty, NaN);
}
-Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) {
+ConstantFP *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) {
const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
APFloat NaN = APFloat::getSNaN(Semantics, Negative, Payload);
- Constant *C = get(Ty->getContext(), NaN);
-
- if (VectorType *VTy = dyn_cast<VectorType>(Ty))
- return ConstantVector::getSplat(VTy->getElementCount(), C);
-
- return C;
+ return get(Ty, NaN);
}
-Constant *ConstantFP::getZero(Type *Ty, bool Negative) {
+ConstantFP *ConstantFP::getZero(Type *Ty, bool Negative) {
const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
APFloat NegZero = APFloat::getZero(Semantics, Negative);
- Constant *C = get(Ty->getContext(), NegZero);
-
- if (VectorType *VTy = dyn_cast<VectorType>(Ty))
- return ConstantVector::getSplat(VTy->getElementCount(), C);
-
- return C;
+ return get(Ty, NegZero);
}
-
// ConstantFP accessors.
ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
LLVMContextImpl* pImpl = Context.pImpl;
@@ -1250,16 +1229,6 @@ ConstantFP *ConstantFP::get(LLVMContext &Context, ElementCount EC,
return Slot.get();
}
-Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
- const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
- Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
-
- if (VectorType *VTy = dyn_cast<VectorType>(Ty))
- return ConstantVector::getSplat(VTy->getElementCount(), C);
-
- return C;
-}
-
ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
: ConstantData(Ty, ConstantFPVal), Val(V) {
assert(&V.getSemantics() == &Ty->getScalarType()->getFltSemantics() &&
More information about the llvm-commits
mailing list