Move ConstantFP construction back to the 2.5-ish API.

llvm-svn: 77247
This commit is contained in:
Owen Anderson 2009-07-27 20:59:43 +00:00
parent 7b2b1ec6b1
commit 69c464dec4
26 changed files with 188 additions and 207 deletions

View File

@ -159,7 +159,7 @@ we'll do numeric literals:</p>
<div class="doc_code">
<pre>
Value *NumberExprAST::Codegen() {
return getGlobalContext().getConstantFP(APFloat(Val));
return ConstantFP::get(getGlobalContext(), APFloat(Val));
}
</pre>
</div>
@ -1034,7 +1034,7 @@ static std::map&lt;std::string, Value*&gt; NamedValues;
Value *ErrorV(const char *Str) { Error(Str); return 0; }
Value *NumberExprAST::Codegen() {
return getGlobalContext().getConstantFP(APFloat(Val));
return ConstantFP::get(getGlobalContext(), APFloat(Val));
}
Value *VariableExprAST::Codegen() {

View File

@ -869,7 +869,7 @@ static FunctionPassManager *TheFPM;
Value *ErrorV(const char *Str) { Error(Str); return 0; }
Value *NumberExprAST::Codegen() {
return getGlobalContext().getConstantFP(APFloat(Val));
return ConstantFP::get(getGlobalContext(), APFloat(Val));
}
Value *VariableExprAST::Codegen() {

View File

@ -364,7 +364,7 @@ Value *IfExprAST::Codegen() {
// Convert condition to a bool by comparing equal to 0.0.
CondV = Builder.CreateFCmpONE(CondV,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"ifcond");
</pre>
</div>
@ -796,7 +796,7 @@ references to it will naturally find it in the symbol table.</p>
if (StepVal == 0) return 0;
} else {
// If not specified, use 1.0.
StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
}
Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
@ -815,7 +815,7 @@ will be the value of the loop variable on the next iteration of the loop.</p>
// Convert condition to a bool by comparing equal to 0.0.
EndCond = Builder.CreateFCmpONE(EndCond,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"loopcond");
</pre>
</div>
@ -1360,7 +1360,7 @@ static FunctionPassManager *TheFPM;
Value *ErrorV(const char *Str) { Error(Str); return 0; }
Value *NumberExprAST::Codegen() {
return getGlobalContext().getConstantFP(APFloat(Val));
return ConstantFP::get(getGlobalContext(), APFloat(Val));
}
Value *VariableExprAST::Codegen() {
@ -1411,7 +1411,7 @@ Value *IfExprAST::Codegen() {
// Convert condition to a bool by comparing equal to 0.0.
CondV = Builder.CreateFCmpONE(CondV,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"ifcond");
Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();
@ -1510,7 +1510,7 @@ Value *ForExprAST::Codegen() {
if (StepVal == 0) return 0;
} else {
// If not specified, use 1.0.
StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
}
Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
@ -1521,7 +1521,7 @@ Value *ForExprAST::Codegen() {
// Convert condition to a bool by comparing equal to 0.0.
EndCond = Builder.CreateFCmpONE(EndCond,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"loopcond");
// Create the "after loop" block and insert it.

View File

@ -1365,7 +1365,7 @@ static FunctionPassManager *TheFPM;
Value *ErrorV(const char *Str) { Error(Str); return 0; }
Value *NumberExprAST::Codegen() {
return getGlobalContext().getConstantFP(APFloat(Val));
return ConstantFP::get(getGlobalContext(), APFloat(Val));
}
Value *VariableExprAST::Codegen() {
@ -1436,7 +1436,7 @@ Value *IfExprAST::Codegen() {
// Convert condition to a bool by comparing equal to 0.0.
CondV = Builder.CreateFCmpONE(CondV,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"ifcond");
Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();
@ -1535,7 +1535,7 @@ Value *ForExprAST::Codegen() {
if (StepVal == 0) return 0;
} else {
// If not specified, use 1.0.
StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
}
Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
@ -1546,7 +1546,7 @@ Value *ForExprAST::Codegen() {
// Convert condition to a bool by comparing equal to 0.0.
EndCond = Builder.CreateFCmpONE(EndCond,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"loopcond");
// Create the "after loop" block and insert it.

View File

@ -923,7 +923,7 @@ that we replace in OldBindings.</p>
InitVal = Init-&gt;Codegen();
if (InitVal == 0) return 0;
} else { // If not specified, use 0.0.
InitVal = getGlobalContext().getConstantFP(APFloat(0.0));
InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
}
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
@ -1623,7 +1623,7 @@ static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
Value *NumberExprAST::Codegen() {
return getGlobalContext().getConstantFP(APFloat(Val));
return ConstantFP::get(getGlobalContext(), APFloat(Val));
}
Value *VariableExprAST::Codegen() {
@ -1716,7 +1716,7 @@ Value *IfExprAST::Codegen() {
// Convert condition to a bool by comparing equal to 0.0.
CondV = Builder.CreateFCmpONE(CondV,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"ifcond");
Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();
@ -1822,7 +1822,7 @@ Value *ForExprAST::Codegen() {
if (StepVal == 0) return 0;
} else {
// If not specified, use 1.0.
StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
}
// Compute the end condition.
@ -1837,7 +1837,7 @@ Value *ForExprAST::Codegen() {
// Convert condition to a bool by comparing equal to 0.0.
EndCond = Builder.CreateFCmpONE(EndCond,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"loopcond");
// Create the "after loop" block and insert it.
@ -1881,7 +1881,7 @@ Value *VarExprAST::Codegen() {
InitVal = Init-&gt;Codegen();
if (InitVal == 0) return 0;
} else { // If not specified, use 0.0.
InitVal = getGlobalContext().getConstantFP(APFloat(0.0));
InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
}
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);

View File

@ -623,7 +623,7 @@ static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
Value *NumberExprAST::Codegen() {
return getGlobalContext().getConstantFP(APFloat(Val));
return ConstantFP::get(getGlobalContext(), APFloat(Val));
}
Value *VariableExprAST::Codegen() {
@ -716,7 +716,7 @@ Value *IfExprAST::Codegen() {
// Convert condition to a bool by comparing equal to 0.0.
CondV = Builder.CreateFCmpONE(CondV,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"ifcond");
Function *TheFunction = Builder.GetInsertBlock()->getParent();
@ -821,7 +821,7 @@ Value *ForExprAST::Codegen() {
if (StepVal == 0) return 0;
} else {
// If not specified, use 1.0.
StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
}
// Compute the end condition.
@ -836,7 +836,7 @@ Value *ForExprAST::Codegen() {
// Convert condition to a bool by comparing equal to 0.0.
EndCond = Builder.CreateFCmpONE(EndCond,
getGlobalContext().getConstantFP(APFloat(0.0)),
ConstantFP::get(getGlobalContext(), APFloat(0.0)),
"loopcond");
// Create the "after loop" block and insert it.
@ -879,7 +879,7 @@ Value *VarExprAST::Codegen() {
InitVal = Init->Codegen();
if (InitVal == 0) return 0;
} else { // If not specified, use 0.0.
InitVal = getGlobalContext().getConstantFP(APFloat(0.0));
InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
}
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);

View File

@ -50,7 +50,6 @@ class ConstantInt : public Constant {
ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
ConstantInt(const IntegerType *Ty, const APInt& V);
APInt Val;
friend class LLVMContextImpl;
protected:
// allocate space for exactly zero operands
void *operator new(size_t s) {
@ -238,6 +237,19 @@ protected:
return User::operator new(s, 0);
}
public:
/// Floating point negation must be implemented with f(x) = -0.0 - x. This
/// method returns the negative zero constant for floating point or vector
/// floating point types; for all other types, it returns the null value.
static Constant* getZeroValueForNegation(const Type* Ty);
/// get() - This returns a ConstantFP, or a vector containing a splat of a
/// ConstantFP, 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.
static Constant* get(const Type* Ty, double V);
static ConstantFP* get(LLVMContext &Context, const APFloat& V);
static ConstantFP* getNegativeZero(const Type* Ty);
/// isValueValidForType - return true if Ty is big enough to represent V.
static bool isValueValidForType(const Type *Ty, const APFloat& V);
inline const APFloat& getValueAPF() const { return Val; }

View File

@ -56,6 +56,7 @@ class LLVMContext {
LLVMContextImpl* pImpl;
friend class ConstantInt;
friend class ConstantFP;
public:
LLVMContext();
~LLVMContext();
@ -180,21 +181,6 @@ public:
///
Constant* getConstantExprSizeOf(const Type* Ty);
/// Floating point negation must be implemented with f(x) = -0.0 - x. This
/// method returns the negative zero constant for floating point or vector
/// floating point types; for all other types, it returns the null value.
Constant* getZeroValueForNegation(const Type* Ty);
// ConstantFP accessors
ConstantFP* getConstantFP(const APFloat& V);
/// get() - This returns a ConstantFP, or a vector containing a splat of a
/// ConstantFP, 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.
Constant* getConstantFP(const Type* Ty, double V);
ConstantFP* getConstantFPNegativeZero(const Type* Ty);
// ConstantVector accessors
Constant* getConstantVector(const VectorType* T,
const std::vector<Constant*>& V);

View File

@ -506,7 +506,7 @@ struct neg_match {
}
private:
bool matchIfNeg(Value *LHS, Value *RHS, LLVMContext &Context) {
return LHS == Context.getZeroValueForNegation(LHS->getType()) &&
return LHS == ConstantFP::getZeroValueForNegation(LHS->getType()) &&
L.match(RHS, Context);
}
};
@ -535,7 +535,7 @@ struct fneg_match {
}
private:
bool matchIfFNeg(Value *LHS, Value *RHS, LLVMContext &Context) {
return LHS == Context.getZeroValueForNegation(LHS->getType()) &&
return LHS == ConstantFP::getZeroValueForNegation(LHS->getType()) &&
L.match(RHS, Context);
}
};

View File

@ -655,9 +655,9 @@ static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
}
if (Ty == Type::FloatTy)
return Context.getConstantFP(APFloat((float)V));
return ConstantFP::get(Context, APFloat((float)V));
if (Ty == Type::DoubleTy)
return Context.getConstantFP(APFloat(V));
return ConstantFP::get(Context, APFloat(V));
llvm_unreachable("Can only constant fold float/double");
return 0; // dummy return to suppress warning
}
@ -674,9 +674,9 @@ static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
}
if (Ty == Type::FloatTy)
return Context.getConstantFP(APFloat((float)V));
return ConstantFP::get(Context, APFloat((float)V));
if (Ty == Type::DoubleTy)
return Context.getConstantFP(APFloat(V));
return ConstantFP::get(Context, APFloat(V));
llvm_unreachable("Can only constant fold float/double");
return 0; // dummy return to suppress warning
}
@ -796,10 +796,10 @@ llvm::ConstantFoldCall(Function *F,
}
} else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
if (Name == "llvm.powi.f32") {
return Context.getConstantFP(APFloat((float)std::pow((float)Op1V,
return ConstantFP::get(Context, APFloat((float)std::pow((float)Op1V,
(int)Op2C->getZExtValue())));
} else if (Name == "llvm.powi.f64") {
return Context.getConstantFP(APFloat((double)std::pow((double)Op1V,
return ConstantFP::get(Context, APFloat((double)std::pow((double)Op1V,
(int)Op2C->getZExtValue())));
}
}

View File

@ -2143,7 +2143,7 @@ bool LLParser::ConvertGlobalValIDToValue(const Type *Ty, ValID &ID,
ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
&Ignored);
}
V = Context.getConstantFP(ID.APFloatVal);
V = ConstantFP::get(Context, ID.APFloatVal);
if (V->getType() != Ty)
return Error(ID.Loc, "floating point constant does not have type '" +

View File

@ -896,19 +896,19 @@ bool BitcodeReader::ParseConstants() {
if (Record.empty())
return Error("Invalid FLOAT record");
if (CurTy == Type::FloatTy)
V = Context.getConstantFP(APFloat(APInt(32, (uint32_t)Record[0])));
V = ConstantFP::get(Context, APFloat(APInt(32, (uint32_t)Record[0])));
else if (CurTy == Type::DoubleTy)
V = Context.getConstantFP(APFloat(APInt(64, Record[0])));
V = ConstantFP::get(Context, APFloat(APInt(64, Record[0])));
else if (CurTy == Type::X86_FP80Ty) {
// Bits are not stored the same way as a normal i80 APInt, compensate.
uint64_t Rearrange[2];
Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
Rearrange[1] = Record[0] >> 48;
V = Context.getConstantFP(APFloat(APInt(80, 2, Rearrange)));
V = ConstantFP::get(Context, APFloat(APInt(80, 2, Rearrange)));
} else if (CurTy == Type::FP128Ty)
V = Context.getConstantFP(APFloat(APInt(128, 2, &Record[0]), true));
V = ConstantFP::get(Context, APFloat(APInt(128, 2, &Record[0]), true));
else if (CurTy == Type::PPC_FP128Ty)
V = Context.getConstantFP(APFloat(APInt(128, 2, &Record[0])));
V = ConstantFP::get(Context, APFloat(APInt(128, 2, &Record[0])));
else
V = Context.getUndef(CurTy);
break;

View File

@ -916,7 +916,7 @@ SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
return getConstantFP(*Context->getConstantFP(V), VT, isTarget);
return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
}
SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){

View File

@ -2148,8 +2148,7 @@ void SelectionDAGLowering::visitFSub(User &I) {
const VectorType *DestTy = cast<VectorType>(I.getType());
const Type *ElTy = DestTy->getElementType();
unsigned VL = DestTy->getNumElements();
std::vector<Constant*> NZ(VL,
DAG.getContext()->getConstantFPNegativeZero(ElTy));
std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Constant *CNZ = DAG.getContext()->getConstantVector(&NZ[0], NZ.size());
if (CV == CNZ) {
SDValue Op2 = getValue(I.getOperand(1));
@ -2160,8 +2159,7 @@ void SelectionDAGLowering::visitFSub(User &I) {
}
}
if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
if (CFP->isExactlyValue(
DAG.getContext()->getConstantFPNegativeZero(Ty)->getValueAPF())) {
if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
SDValue Op2 = getValue(I.getOperand(1));
setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Op2.getValueType(), Op2));

View File

@ -490,15 +490,15 @@ GenericValue JIT::runFunction(Function *F,
C = ConstantInt::get(F->getContext(), AV.IntVal);
break;
case Type::FloatTyID:
C = Context.getConstantFP(APFloat(AV.FloatVal));
C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
break;
case Type::DoubleTyID:
C = Context.getConstantFP(APFloat(AV.DoubleVal));
C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
break;
case Type::PPC_FP128TyID:
case Type::X86_FP80TyID:
case Type::FP128TyID:
C = Context.getConstantFP(APFloat(AV.IntVal));
C = ConstantFP::get(F->getContext(), APFloat(AV.IntVal));
break;
case Type::PointerTyID:
void *ArgPtr = GVTOP(AV);

View File

@ -4898,9 +4898,9 @@ SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
std::vector<Constant*> CV1;
CV1.push_back(
Context->getConstantFP(APFloat(APInt(64, 0x4530000000000000ULL))));
ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
CV1.push_back(
Context->getConstantFP(APFloat(APInt(64, 0x4330000000000000ULL))));
ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Constant *C1 = Context->getConstantVector(CV1);
SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
@ -5113,11 +5113,11 @@ SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
EltVT = VT.getVectorElementType();
std::vector<Constant*> CV;
if (EltVT == MVT::f64) {
Constant *C = Context->getConstantFP(APFloat(APInt(64, ~(1ULL << 63))));
Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
CV.push_back(C);
CV.push_back(C);
} else {
Constant *C = Context->getConstantFP(APFloat(APInt(32, ~(1U << 31))));
Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
CV.push_back(C);
CV.push_back(C);
CV.push_back(C);
@ -5143,11 +5143,11 @@ SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
}
std::vector<Constant*> CV;
if (EltVT == MVT::f64) {
Constant *C = Context->getConstantFP(APFloat(APInt(64, 1ULL << 63)));
Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
CV.push_back(C);
CV.push_back(C);
} else {
Constant *C = Context->getConstantFP(APFloat(APInt(32, 1U << 31)));
Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
CV.push_back(C);
CV.push_back(C);
CV.push_back(C);
@ -5194,13 +5194,13 @@ SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
// First get the sign bit of second operand.
std::vector<Constant*> CV;
if (SrcVT == MVT::f64) {
CV.push_back(Context->getConstantFP(APFloat(APInt(64, 1ULL << 63))));
CV.push_back(Context->getConstantFP(APFloat(APInt(64, 0))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
} else {
CV.push_back(Context->getConstantFP(APFloat(APInt(32, 1U << 31))));
CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
}
Constant *C = Context->getConstantVector(CV);
SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
@ -5223,13 +5223,13 @@ SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
// Clear first operand sign bit.
CV.clear();
if (VT == MVT::f64) {
CV.push_back(Context->getConstantFP(APFloat(APInt(64, ~(1ULL << 63)))));
CV.push_back(Context->getConstantFP(APFloat(APInt(64, 0))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
} else {
CV.push_back(Context->getConstantFP(APFloat(APInt(32, ~(1U << 31)))));
CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
}
C = Context->getConstantVector(CV);
CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);

View File

@ -2354,7 +2354,7 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
// X + 0 --> X
if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
if (CFP->isExactlyValue(Context->getConstantFPNegativeZero
if (CFP->isExactlyValue(ConstantFP::getNegativeZero
(I.getType())->getValueAPF()))
return ReplaceInstUsesWith(I, LHS);
}
@ -8779,7 +8779,7 @@ static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem,
APFloat F = CFP->getValueAPF();
(void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
if (!losesInfo)
return Context->getConstantFP(F);
return ConstantFP::get(*Context, F);
return 0;
}

View File

@ -2177,8 +2177,6 @@ void LoopStrengthReduce::OptimizeShadowIV(Loop *L) {
if (isa<SCEVCouldNotCompute>(BackedgeTakenCount))
return;
LLVMContext &Context = L->getHeader()->getContext();
for (unsigned Stride = 0, e = IU->StrideOrder.size(); Stride != e;
++Stride) {
std::map<const SCEV *, IVUsersOfOneStride *>::iterator SI =
@ -2240,7 +2238,7 @@ void LoopStrengthReduce::OptimizeShadowIV(Loop *L) {
ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry));
if (!Init) continue;
Constant *NewInit = Context.getConstantFP(DestTy, Init->getZExtValue());
Constant *NewInit = ConstantFP::get(DestTy, Init->getZExtValue());
BinaryOperator *Incr =
dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch));
@ -2264,7 +2262,7 @@ void LoopStrengthReduce::OptimizeShadowIV(Loop *L) {
PHINode *NewPH = PHINode::Create(DestTy, "IV.S.", PH);
/* create new increment. '++d' in above example. */
Constant *CFP = Context.getConstantFP(DestTy, C->getZExtValue());
Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue());
BinaryOperator *NewIncr =
BinaryOperator::Create(Incr->getOpcode() == Instruction::Add ?
Instruction::FAdd : Instruction::FSub,

View File

@ -1035,7 +1035,7 @@ struct VISIBILITY_HIDDEN PowOpt : public LibCallOptimization {
if (Op2C == 0) return 0;
if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
return Context->getConstantFP(CI->getType(), 1.0);
return ConstantFP::get(CI->getType(), 1.0);
if (Op2C->isExactlyValue(0.5)) {
// FIXME: This is not safe for -0.0 and -inf. This can only be done when
@ -1055,7 +1055,7 @@ struct VISIBILITY_HIDDEN PowOpt : public LibCallOptimization {
if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
return B.CreateFMul(Op1, Op1, "pow2");
if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
return B.CreateFDiv(Context->getConstantFP(CI->getType(), 1.0),
return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
Op1, "powrecip");
return 0;
}
@ -1094,7 +1094,7 @@ struct VISIBILITY_HIDDEN Exp2Opt : public LibCallOptimization {
else
Name = "ldexpl";
Constant *One = Context->getConstantFP(APFloat(1.0f));
Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
if (Op->getType() != Type::FloatTy)
One = Context->getConstantExprFPExtend(One, Op->getType());

View File

@ -161,7 +161,7 @@ static Constant *FoldBitCast(LLVMContext &Context,
return V;
if (DestTy->isFloatingPoint())
return Context.getConstantFP(APFloat(CI->getValue(),
return ConstantFP::get(Context, APFloat(CI->getValue(),
DestTy != Type::PPC_FP128Ty));
// Otherwise, can't fold this (vector?)
@ -245,7 +245,7 @@ Constant *llvm::ConstantFoldCastInstruction(LLVMContext &Context,
DestTy == Type::FP128Ty ? APFloat::IEEEquad :
APFloat::Bogus,
APFloat::rmNearestTiesToEven, &ignored);
return Context.getConstantFP(Val);
return ConstantFP::get(Context, Val);
}
return 0; // Can't fold.
case Instruction::FPToUI:
@ -279,7 +279,7 @@ Constant *llvm::ConstantFoldCastInstruction(LLVMContext &Context,
(void)apf.convertFromAPInt(api,
opc==Instruction::SIToFP,
APFloat::rmNearestTiesToEven);
return Context.getConstantFP(apf);
return ConstantFP::get(Context, apf);
}
return 0;
case Instruction::ZExt:
@ -795,19 +795,19 @@ Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context,
break;
case Instruction::FAdd:
(void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
return Context.getConstantFP(C3V);
return ConstantFP::get(Context, C3V);
case Instruction::FSub:
(void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
return Context.getConstantFP(C3V);
return ConstantFP::get(Context, C3V);
case Instruction::FMul:
(void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
return Context.getConstantFP(C3V);
return ConstantFP::get(Context, C3V);
case Instruction::FDiv:
(void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
return Context.getConstantFP(C3V);
return ConstantFP::get(Context, C3V);
case Instruction::FRem:
(void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
return Context.getConstantFP(C3V);
return ConstantFP::get(Context, C3V);
}
}
} else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {

View File

@ -258,6 +258,87 @@ static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
}
#endif
/// get() - This returns a constant fp 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 double and as the target format.
Constant* ConstantFP::get(const Type* Ty, double V) {
LLVMContext &Context = Ty->getContext();
APFloat FV(V);
bool ignored;
FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
APFloat::rmNearestTiesToEven, &ignored);
Constant *C = get(Context, FV);
// For vectors, broadcast the value.
if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
return Context.getConstantVector(
std::vector<Constant *>(VTy->getNumElements(), C));
return C;
}
ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
LLVMContext &Context = Ty->getContext();
APFloat apf = cast <ConstantFP>(Context.getNullValue(Ty))->getValueAPF();
apf.changeSign();
return get(Context, apf);
}
Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
LLVMContext &Context = Ty->getContext();
if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
if (PTy->getElementType()->isFloatingPoint()) {
std::vector<Constant*> zeros(PTy->getNumElements(),
getNegativeZero(PTy->getElementType()));
return Context.getConstantVector(PTy, zeros);
}
if (Ty->isFloatingPoint())
return getNegativeZero(Ty);
return Context.getNullValue(Ty);
}
// ConstantFP accessors.
ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
DenseMapAPFloatKeyInfo::KeyTy Key(V);
LLVMContextImpl* pImpl = Context.pImpl;
pImpl->ConstantsLock.reader_acquire();
ConstantFP *&Slot = pImpl->FPConstants[Key];
pImpl->ConstantsLock.reader_release();
if (!Slot) {
sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
ConstantFP *&NewSlot = pImpl->FPConstants[Key];
if (!NewSlot) {
const Type *Ty;
if (&V.getSemantics() == &APFloat::IEEEsingle)
Ty = Type::FloatTy;
else if (&V.getSemantics() == &APFloat::IEEEdouble)
Ty = Type::DoubleTy;
else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
Ty = Type::X86_FP80Ty;
else if (&V.getSemantics() == &APFloat::IEEEquad)
Ty = Type::FP128Ty;
else {
assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
"Unknown FP format");
Ty = Type::PPC_FP128Ty;
}
NewSlot = new ConstantFP(Ty, V);
}
return NewSlot;
}
return Slot;
}
ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
: Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&

View File

@ -388,11 +388,11 @@ LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
bool ignored;
APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
&ignored);
return wrap(getGlobalContext().getConstantFP(APN));
return wrap(ConstantFP::get(getGlobalContext(), APN));
}
LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
return wrap(getGlobalContext().getConstantFP(
return wrap(ConstantFP::get(getGlobalContext(),
APFloat(SemanticsForType(unwrap(RealTy)), Text)));
}

View File

@ -1577,7 +1577,7 @@ BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
Value *Op, const Twine &Name,
Instruction *InsertBefore) {
Value *zero = Context.getZeroValueForNegation(Op->getType());
Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
return new BinaryOperator(Instruction::Sub,
zero, Op,
Op->getType(), Name, InsertBefore);
@ -1586,7 +1586,7 @@ BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
Value *Op, const Twine &Name,
BasicBlock *InsertAtEnd) {
Value *zero = Context.getZeroValueForNegation(Op->getType());
Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
return new BinaryOperator(Instruction::Sub,
zero, Op,
Op->getType(), Name, InsertAtEnd);
@ -1595,7 +1595,7 @@ BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
Value *Op, const Twine &Name,
Instruction *InsertBefore) {
Value *zero = Context.getZeroValueForNegation(Op->getType());
Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
return new BinaryOperator(Instruction::FSub,
zero, Op,
Op->getType(), Name, InsertBefore);
@ -1604,7 +1604,7 @@ BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
Value *Op, const Twine &Name,
BasicBlock *InsertAtEnd) {
Value *zero = Context.getZeroValueForNegation(Op->getType());
Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
return new BinaryOperator(Instruction::FSub,
zero, Op,
Op->getType(), Name, InsertAtEnd);

View File

@ -41,15 +41,16 @@ Constant* LLVMContext::getNullValue(const Type* Ty) {
case Type::IntegerTyID:
return ConstantInt::get(Ty, 0);
case Type::FloatTyID:
return getConstantFP(APFloat(APInt(32, 0)));
return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
case Type::DoubleTyID:
return getConstantFP(APFloat(APInt(64, 0)));
return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
case Type::X86_FP80TyID:
return getConstantFP(APFloat(APInt(80, 2, zero)));
return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
case Type::FP128TyID:
return getConstantFP(APFloat(APInt(128, 2, zero), true));
return ConstantFP::get(Ty->getContext(),
APFloat(APInt(128, 2, zero), true));
case Type::PPC_FP128TyID:
return getConstantFP(APFloat(APInt(128, 2, zero)));
return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
case Type::PointerTyID:
return getConstantPointerNull(cast<PointerType>(Ty));
case Type::StructTyID:
@ -276,7 +277,7 @@ Constant* LLVMContext::getConstantExprNeg(Constant* C) {
assert(C->getType()->isIntOrIntVector() &&
"Cannot NEG a nonintegral value!");
return getConstantExpr(Instruction::Sub,
getZeroValueForNegation(C->getType()),
ConstantFP::getZeroValueForNegation(C->getType()),
C);
}
@ -284,7 +285,7 @@ Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
assert(C->getType()->isFPOrFPVector() &&
"Cannot FNEG a non-floating-point value!");
return getConstantExpr(Instruction::FSub,
getZeroValueForNegation(C->getType()),
ConstantFP::getZeroValueForNegation(C->getType()),
C);
}
@ -424,65 +425,6 @@ Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
}
Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
if (PTy->getElementType()->isFloatingPoint()) {
std::vector<Constant*> zeros(PTy->getNumElements(),
getConstantFPNegativeZero(PTy->getElementType()));
return getConstantVector(PTy, zeros);
}
if (Ty->isFloatingPoint())
return getConstantFPNegativeZero(Ty);
return getNullValue(Ty);
}
// ConstantFP accessors.
ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
return pImpl->getConstantFP(V);
}
static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
if (Ty == Type::FloatTy)
return &APFloat::IEEEsingle;
if (Ty == Type::DoubleTy)
return &APFloat::IEEEdouble;
if (Ty == Type::X86_FP80Ty)
return &APFloat::x87DoubleExtended;
else if (Ty == Type::FP128Ty)
return &APFloat::IEEEquad;
assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
return &APFloat::PPCDoubleDouble;
}
/// get() - This returns a constant fp 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 double and as the target format.
Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
APFloat FV(V);
bool ignored;
FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
APFloat::rmNearestTiesToEven, &ignored);
Constant *C = getConstantFP(FV);
// For vectors, broadcast the value.
if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
return
getConstantVector(std::vector<Constant *>(VTy->getNumElements(), C));
return C;
}
ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
apf.changeSign();
return getConstantFP(apf);
}
// ConstantVector accessors.
Constant* LLVMContext::getConstantVector(const VectorType* T,
const std::vector<Constant*>& V) {

View File

@ -49,41 +49,6 @@ static std::vector<Constant*> getValType(ConstantVector *CP) {
LLVMContextImpl::LLVMContextImpl(LLVMContext &C) :
Context(C), TheTrueVal(0), TheFalseVal(0) { }
ConstantFP *LLVMContextImpl::getConstantFP(const APFloat &V) {
DenseMapAPFloatKeyInfo::KeyTy Key(V);
ConstantsLock.reader_acquire();
ConstantFP *&Slot = FPConstants[Key];
ConstantsLock.reader_release();
if (!Slot) {
sys::SmartScopedWriter<true> Writer(ConstantsLock);
ConstantFP *&NewSlot = FPConstants[Key];
if (!NewSlot) {
const Type *Ty;
if (&V.getSemantics() == &APFloat::IEEEsingle)
Ty = Type::FloatTy;
else if (&V.getSemantics() == &APFloat::IEEEdouble)
Ty = Type::DoubleTy;
else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
Ty = Type::X86_FP80Ty;
else if (&V.getSemantics() == &APFloat::IEEEquad)
Ty = Type::FP128Ty;
else {
assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
"Unknown FP format");
Ty = Type::PPC_FP128Ty;
}
NewSlot = new ConstantFP(Ty, V);
}
return NewSlot;
}
return Slot;
}
MDString *LLVMContextImpl::getMDString(const char *StrBegin,
unsigned StrLength) {
sys::SmartScopedWriter<true> Writer(ConstantsLock);

View File

@ -457,11 +457,10 @@ class LLVMContextImpl {
LLVMContextImpl(const LLVMContextImpl&);
friend class ConstantInt;
friend class ConstantFP;
public:
LLVMContextImpl(LLVMContext &C);
ConstantFP *getConstantFP(const APFloat &V);
MDString *getMDString(const char *StrBegin, unsigned StrLength);
MDNode *getMDNode(Value*const* Vals, unsigned NumVals);