llvm.org GIT mirror llvm / 6f83c9c
Move ConstantFP construction back to the 2.5-ish API. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77247 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
26 changed file(s) with 188 addition(s) and 207 deletion(s). Raw diff Collapse all Expand all
158158
159159

                  
                
160160 Value *NumberExprAST::Codegen() {
161 return getGlobalContext().getConstantFP(APFloat(Val));
161 return ConstantFP::get(getGlobalContext(), APFloat(Val));
162162 }
163163
164164
10331033 Value *ErrorV(const char *Str) { Error(Str); return 0; }
10341034
10351035 Value *NumberExprAST::Codegen() {
1036 return getGlobalContext().getConstantFP(APFloat(Val));
1036 return ConstantFP::get(getGlobalContext(), APFloat(Val));
10371037 }
10381038
10391039 Value *VariableExprAST::Codegen() {
868868 Value *ErrorV(const char *Str) { Error(Str); return 0; }
869869
870870 Value *NumberExprAST::Codegen() {
871 return getGlobalContext().getConstantFP(APFloat(Val));
871 return ConstantFP::get(getGlobalContext(), APFloat(Val));
872872 }
873873
874874 Value *VariableExprAST::Codegen() {
363363
364364 // Convert condition to a bool by comparing equal to 0.0.
365365 CondV = Builder.CreateFCmpONE(CondV,
366 getGlobalContext().getConstantFP(APFloat(0.0)),
366 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
367367 "ifcond");
368368
369369
795795 if (StepVal == 0) return 0;
796796 } else {
797797 // If not specified, use 1.0.
798 StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
798 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
799799 }
800800
801801 Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
814814
815815 // Convert condition to a bool by comparing equal to 0.0.
816816 EndCond = Builder.CreateFCmpONE(EndCond,
817 getGlobalContext().getConstantFP(APFloat(0.0)),
817 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
818818 "loopcond");
819819
820820
13591359 Value *ErrorV(const char *Str) { Error(Str); return 0; }
13601360
13611361 Value *NumberExprAST::Codegen() {
1362 return getGlobalContext().getConstantFP(APFloat(Val));
1362 return ConstantFP::get(getGlobalContext(), APFloat(Val));
13631363 }
13641364
13651365 Value *VariableExprAST::Codegen() {
14101410
14111411 // Convert condition to a bool by comparing equal to 0.0.
14121412 CondV = Builder.CreateFCmpONE(CondV,
1413 getGlobalContext().getConstantFP(APFloat(0.0)),
1413 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
14141414 "ifcond");
14151415
14161416 Function *TheFunction = Builder.GetInsertBlock()->getParent();
15091509 if (StepVal == 0) return 0;
15101510 } else {
15111511 // If not specified, use 1.0.
1512 StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
1512 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
15131513 }
15141514
15151515 Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
15201520
15211521 // Convert condition to a bool by comparing equal to 0.0.
15221522 EndCond = Builder.CreateFCmpONE(EndCond,
1523 getGlobalContext().getConstantFP(APFloat(0.0)),
1523 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
15241524 "loopcond");
15251525
15261526 // Create the "after loop" block and insert it.
13641364 Value *ErrorV(const char *Str) { Error(Str); return 0; }
13651365
13661366 Value *NumberExprAST::Codegen() {
1367 return getGlobalContext().getConstantFP(APFloat(Val));
1367 return ConstantFP::get(getGlobalContext(), APFloat(Val));
13681368 }
13691369
13701370 Value *VariableExprAST::Codegen() {
14351435
14361436 // Convert condition to a bool by comparing equal to 0.0.
14371437 CondV = Builder.CreateFCmpONE(CondV,
1438 getGlobalContext().getConstantFP(APFloat(0.0)),
1438 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
14391439 "ifcond");
14401440
14411441 Function *TheFunction = Builder.GetInsertBlock()->getParent();
15341534 if (StepVal == 0) return 0;
15351535 } else {
15361536 // If not specified, use 1.0.
1537 StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
1537 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
15381538 }
15391539
15401540 Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
15451545
15461546 // Convert condition to a bool by comparing equal to 0.0.
15471547 EndCond = Builder.CreateFCmpONE(EndCond,
1548 getGlobalContext().getConstantFP(APFloat(0.0)),
1548 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
15491549 "loopcond");
15501550
15511551 // Create the "after loop" block and insert it.
922922 InitVal = Init->Codegen();
923923 if (InitVal == 0) return 0;
924924 } else { // If not specified, use 0.0.
925 InitVal = getGlobalContext().getConstantFP(APFloat(0.0));
925 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
926926 }
927927
928928 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
16221622
16231623
16241624 Value *NumberExprAST::Codegen() {
1625 return getGlobalContext().getConstantFP(APFloat(Val));
1625 return ConstantFP::get(getGlobalContext(), APFloat(Val));
16261626 }
16271627
16281628 Value *VariableExprAST::Codegen() {
17151715
17161716 // Convert condition to a bool by comparing equal to 0.0.
17171717 CondV = Builder.CreateFCmpONE(CondV,
1718 getGlobalContext().getConstantFP(APFloat(0.0)),
1718 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
17191719 "ifcond");
17201720
17211721 Function *TheFunction = Builder.GetInsertBlock()->getParent();
18211821 if (StepVal == 0) return 0;
18221822 } else {
18231823 // If not specified, use 1.0.
1824 StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
1824 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
18251825 }
18261826
18271827 // Compute the end condition.
18361836
18371837 // Convert condition to a bool by comparing equal to 0.0.
18381838 EndCond = Builder.CreateFCmpONE(EndCond,
1839 getGlobalContext().getConstantFP(APFloat(0.0)),
1839 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
18401840 "loopcond");
18411841
18421842 // Create the "after loop" block and insert it.
18801880 InitVal = Init->Codegen();
18811881 if (InitVal == 0) return 0;
18821882 } else { // If not specified, use 0.0.
1883 InitVal = getGlobalContext().getConstantFP(APFloat(0.0));
1883 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
18841884 }
18851885
18861886 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
622622
623623
624624 Value *NumberExprAST::Codegen() {
625 return getGlobalContext().getConstantFP(APFloat(Val));
625 return ConstantFP::get(getGlobalContext(), APFloat(Val));
626626 }
627627
628628 Value *VariableExprAST::Codegen() {
715715
716716 // Convert condition to a bool by comparing equal to 0.0.
717717 CondV = Builder.CreateFCmpONE(CondV,
718 getGlobalContext().getConstantFP(APFloat(0.0)),
718 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
719719 "ifcond");
720720
721721 Function *TheFunction = Builder.GetInsertBlock()->getParent();
820820 if (StepVal == 0) return 0;
821821 } else {
822822 // If not specified, use 1.0.
823 StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
823 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
824824 }
825825
826826 // Compute the end condition.
835835
836836 // Convert condition to a bool by comparing equal to 0.0.
837837 EndCond = Builder.CreateFCmpONE(EndCond,
838 getGlobalContext().getConstantFP(APFloat(0.0)),
838 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
839839 "loopcond");
840840
841841 // Create the "after loop" block and insert it.
878878 InitVal = Init->Codegen();
879879 if (InitVal == 0) return 0;
880880 } else { // If not specified, use 0.0.
881 InitVal = getGlobalContext().getConstantFP(APFloat(0.0));
881 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
882882 }
883883
884884 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
4949 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
5050 ConstantInt(const IntegerType *Ty, const APInt& V);
5151 APInt Val;
52 friend class LLVMContextImpl;
5352 protected:
5453 // allocate space for exactly zero operands
5554 void *operator new(size_t s) {
237236 return User::operator new(s, 0);
238237 }
239238 public:
239 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
240 /// method returns the negative zero constant for floating point or vector
241 /// floating point types; for all other types, it returns the null value.
242 static Constant* getZeroValueForNegation(const Type* Ty);
243
244 /// get() - This returns a ConstantFP, or a vector containing a splat of a
245 /// ConstantFP, for the specified value in the specified type. This should
246 /// only be used for simple constant values like 2.0/1.0 etc, that are
247 /// known-valid both as host double and as the target format.
248 static Constant* get(const Type* Ty, double V);
249 static ConstantFP* get(LLVMContext &Context, const APFloat& V);
250 static ConstantFP* getNegativeZero(const Type* Ty);
251
240252 /// isValueValidForType - return true if Ty is big enough to represent V.
241253 static bool isValueValidForType(const Type *Ty, const APFloat& V);
242254 inline const APFloat& getValueAPF() const { return Val; }
5555 LLVMContextImpl* pImpl;
5656
5757 friend class ConstantInt;
58 friend class ConstantFP;
5859 public:
5960 LLVMContext();
6061 ~LLVMContext();
179180 ///
180181 Constant* getConstantExprSizeOf(const Type* Ty);
181182
182 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
183 /// method returns the negative zero constant for floating point or vector
184 /// floating point types; for all other types, it returns the null value.
185 Constant* getZeroValueForNegation(const Type* Ty);
186
187 // ConstantFP accessors
188 ConstantFP* getConstantFP(const APFloat& V);
189
190 /// get() - This returns a ConstantFP, or a vector containing a splat of a
191 /// ConstantFP, for the specified value in the specified type. This should
192 /// only be used for simple constant values like 2.0/1.0 etc, that are
193 /// known-valid both as host double and as the target format.
194 Constant* getConstantFP(const Type* Ty, double V);
195 ConstantFP* getConstantFPNegativeZero(const Type* Ty);
196
197183 // ConstantVector accessors
198184 Constant* getConstantVector(const VectorType* T,
199185 const std::vector& V);
505505 }
506506 private:
507507 bool matchIfNeg(Value *LHS, Value *RHS, LLVMContext &Context) {
508 return LHS == Context.getZeroValueForNegation(LHS->getType()) &&
508 return LHS == ConstantFP::getZeroValueForNegation(LHS->getType()) &&
509509 L.match(RHS, Context);
510510 }
511511 };
534534 }
535535 private:
536536 bool matchIfFNeg(Value *LHS, Value *RHS, LLVMContext &Context) {
537 return LHS == Context.getZeroValueForNegation(LHS->getType()) &&
537 return LHS == ConstantFP::getZeroValueForNegation(LHS->getType()) &&
538538 L.match(RHS, Context);
539539 }
540540 };
654654 }
655655
656656 if (Ty == Type::FloatTy)
657 return Context.getConstantFP(APFloat((float)V));
657 return ConstantFP::get(Context, APFloat((float)V));
658658 if (Ty == Type::DoubleTy)
659 return Context.getConstantFP(APFloat(V));
659 return ConstantFP::get(Context, APFloat(V));
660660 llvm_unreachable("Can only constant fold float/double");
661661 return 0; // dummy return to suppress warning
662662 }
673673 }
674674
675675 if (Ty == Type::FloatTy)
676 return Context.getConstantFP(APFloat((float)V));
676 return ConstantFP::get(Context, APFloat((float)V));
677677 if (Ty == Type::DoubleTy)
678 return Context.getConstantFP(APFloat(V));
678 return ConstantFP::get(Context, APFloat(V));
679679 llvm_unreachable("Can only constant fold float/double");
680680 return 0; // dummy return to suppress warning
681681 }
795795 }
796796 } else if (ConstantInt *Op2C = dyn_cast(Operands[1])) {
797797 if (Name == "llvm.powi.f32") {
798 return Context.getConstantFP(APFloat((float)std::pow((float)Op1V,
798 return ConstantFP::get(Context, APFloat((float)std::pow((float)Op1V,
799799 (int)Op2C->getZExtValue())));
800800 } else if (Name == "llvm.powi.f64") {
801 return Context.getConstantFP(APFloat((double)std::pow((double)Op1V,
801 return ConstantFP::get(Context, APFloat((double)std::pow((double)Op1V,
802802 (int)Op2C->getZExtValue())));
803803 }
804804 }
21422142 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
21432143 &Ignored);
21442144 }
2145 V = Context.getConstantFP(ID.APFloatVal);
2145 V = ConstantFP::get(Context, ID.APFloatVal);
21462146
21472147 if (V->getType() != Ty)
21482148 return Error(ID.Loc, "floating point constant does not have type '" +
895895 if (Record.empty())
896896 return Error("Invalid FLOAT record");
897897 if (CurTy == Type::FloatTy)
898 V = Context.getConstantFP(APFloat(APInt(32, (uint32_t)Record[0])));
898 V = ConstantFP::get(Context, APFloat(APInt(32, (uint32_t)Record[0])));
899899 else if (CurTy == Type::DoubleTy)
900 V = Context.getConstantFP(APFloat(APInt(64, Record[0])));
900 V = ConstantFP::get(Context, APFloat(APInt(64, Record[0])));
901901 else if (CurTy == Type::X86_FP80Ty) {
902902 // Bits are not stored the same way as a normal i80 APInt, compensate.
903903 uint64_t Rearrange[2];
904904 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
905905 Rearrange[1] = Record[0] >> 48;
906 V = Context.getConstantFP(APFloat(APInt(80, 2, Rearrange)));
906 V = ConstantFP::get(Context, APFloat(APInt(80, 2, Rearrange)));
907907 } else if (CurTy == Type::FP128Ty)
908 V = Context.getConstantFP(APFloat(APInt(128, 2, &Record[0]), true));
908 V = ConstantFP::get(Context, APFloat(APInt(128, 2, &Record[0]), true));
909909 else if (CurTy == Type::PPC_FP128Ty)
910 V = Context.getConstantFP(APFloat(APInt(128, 2, &Record[0])));
910 V = ConstantFP::get(Context, APFloat(APInt(128, 2, &Record[0])));
911911 else
912912 V = Context.getUndef(CurTy);
913913 break;
915915
916916
917917 SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
918 return getConstantFP(*Context->getConstantFP(V), VT, isTarget);
918 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
919919 }
920920
921921 SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
21472147 const VectorType *DestTy = cast(I.getType());
21482148 const Type *ElTy = DestTy->getElementType();
21492149 unsigned VL = DestTy->getNumElements();
2150 std::vector NZ(VL,
2151 DAG.getContext()->getConstantFPNegativeZero(ElTy));
2150 std::vector NZ(VL, ConstantFP::getNegativeZero(ElTy));
21522151 Constant *CNZ = DAG.getContext()->getConstantVector(&NZ[0], NZ.size());
21532152 if (CV == CNZ) {
21542153 SDValue Op2 = getValue(I.getOperand(1));
21592158 }
21602159 }
21612160 if (ConstantFP *CFP = dyn_cast(I.getOperand(0)))
2162 if (CFP->isExactlyValue(
2163 DAG.getContext()->getConstantFPNegativeZero(Ty)->getValueAPF())) {
2161 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
21642162 SDValue Op2 = getValue(I.getOperand(1));
21652163 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
21662164 Op2.getValueType(), Op2));
489489 C = ConstantInt::get(F->getContext(), AV.IntVal);
490490 break;
491491 case Type::FloatTyID:
492 C = Context.getConstantFP(APFloat(AV.FloatVal));
492 C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
493493 break;
494494 case Type::DoubleTyID:
495 C = Context.getConstantFP(APFloat(AV.DoubleVal));
495 C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
496496 break;
497497 case Type::PPC_FP128TyID:
498498 case Type::X86_FP80TyID:
499499 case Type::FP128TyID:
500 C = Context.getConstantFP(APFloat(AV.IntVal));
500 C = ConstantFP::get(F->getContext(), APFloat(AV.IntVal));
501501 break;
502502 case Type::PointerTyID:
503503 void *ArgPtr = GVTOP(AV);
48974897
48984898 std::vector CV1;
48994899 CV1.push_back(
4900 Context->getConstantFP(APFloat(APInt(64, 0x4530000000000000ULL))));
4900 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
49014901 CV1.push_back(
4902 Context->getConstantFP(APFloat(APInt(64, 0x4330000000000000ULL))));
4902 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
49034903 Constant *C1 = Context->getConstantVector(CV1);
49044904 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
49054905
51125112 EltVT = VT.getVectorElementType();
51135113 std::vector CV;
51145114 if (EltVT == MVT::f64) {
5115 Constant *C = Context->getConstantFP(APFloat(APInt(64, ~(1ULL << 63))));
5115 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
51165116 CV.push_back(C);
51175117 CV.push_back(C);
51185118 } else {
5119 Constant *C = Context->getConstantFP(APFloat(APInt(32, ~(1U << 31))));
5119 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
51205120 CV.push_back(C);
51215121 CV.push_back(C);
51225122 CV.push_back(C);
51425142 }
51435143 std::vector CV;
51445144 if (EltVT == MVT::f64) {
5145 Constant *C = Context->getConstantFP(APFloat(APInt(64, 1ULL << 63)));
5145 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
51465146 CV.push_back(C);
51475147 CV.push_back(C);
51485148 } else {
5149 Constant *C = Context->getConstantFP(APFloat(APInt(32, 1U << 31)));
5149 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
51505150 CV.push_back(C);
51515151 CV.push_back(C);
51525152 CV.push_back(C);
51935193 // First get the sign bit of second operand.
51945194 std::vector CV;
51955195 if (SrcVT == MVT::f64) {
5196 CV.push_back(Context->getConstantFP(APFloat(APInt(64, 1ULL << 63))));
5197 CV.push_back(Context->getConstantFP(APFloat(APInt(64, 0))));
5196 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5197 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
51985198 } else {
5199 CV.push_back(Context->getConstantFP(APFloat(APInt(32, 1U << 31))));
5200 CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
5201 CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
5202 CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
5199 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5200 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5201 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5202 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
52035203 }
52045204 Constant *C = Context->getConstantVector(CV);
52055205 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
52225222 // Clear first operand sign bit.
52235223 CV.clear();
52245224 if (VT == MVT::f64) {
5225 CV.push_back(Context->getConstantFP(APFloat(APInt(64, ~(1ULL << 63)))));
5226 CV.push_back(Context->getConstantFP(APFloat(APInt(64, 0))));
5225 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5226 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
52275227 } else {
5228 CV.push_back(Context->getConstantFP(APFloat(APInt(32, ~(1U << 31)))));
5229 CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
5230 CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
5231 CV.push_back(Context->getConstantFP(APFloat(APInt(32, 0))));
5228 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5229 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5230 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5231 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
52325232 }
52335233 C = Context->getConstantVector(CV);
52345234 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
23532353 if (Constant *RHSC = dyn_cast(RHS)) {
23542354 // X + 0 --> X
23552355 if (ConstantFP *CFP = dyn_cast(RHSC)) {
2356 if (CFP->isExactlyValue(Context->getConstantFPNegativeZero
2356 if (CFP->isExactlyValue(ConstantFP::getNegativeZero
23572357 (I.getType())->getValueAPF()))
23582358 return ReplaceInstUsesWith(I, LHS);
23592359 }
87788778 APFloat F = CFP->getValueAPF();
87798779 (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
87808780 if (!losesInfo)
8781 return Context->getConstantFP(F);
8781 return ConstantFP::get(*Context, F);
87828782 return 0;
87838783 }
87848784
21762176 if (isa(BackedgeTakenCount))
21772177 return;
21782178
2179 LLVMContext &Context = L->getHeader()->getContext();
2180
21812179 for (unsigned Stride = 0, e = IU->StrideOrder.size(); Stride != e;
21822180 ++Stride) {
21832181 std::map::iterator SI =
22392237
22402238 ConstantInt *Init = dyn_cast(PH->getIncomingValue(Entry));
22412239 if (!Init) continue;
2242 Constant *NewInit = Context.getConstantFP(DestTy, Init->getZExtValue());
2240 Constant *NewInit = ConstantFP::get(DestTy, Init->getZExtValue());
22432241
22442242 BinaryOperator *Incr =
22452243 dyn_cast(PH->getIncomingValue(Latch));
22632261 PHINode *NewPH = PHINode::Create(DestTy, "IV.S.", PH);
22642262
22652263 /* create new increment. '++d' in above example. */
2266 Constant *CFP = Context.getConstantFP(DestTy, C->getZExtValue());
2264 Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue());
22672265 BinaryOperator *NewIncr =
22682266 BinaryOperator::Create(Incr->getOpcode() == Instruction::Add ?
22692267 Instruction::FAdd : Instruction::FSub,
10341034 if (Op2C == 0) return 0;
10351035
10361036 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
1037 return Context->getConstantFP(CI->getType(), 1.0);
1037 return ConstantFP::get(CI->getType(), 1.0);
10381038
10391039 if (Op2C->isExactlyValue(0.5)) {
10401040 // FIXME: This is not safe for -0.0 and -inf. This can only be done when
10541054 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
10551055 return B.CreateFMul(Op1, Op1, "pow2");
10561056 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
1057 return B.CreateFDiv(Context->getConstantFP(CI->getType(), 1.0),
1057 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
10581058 Op1, "powrecip");
10591059 return 0;
10601060 }
10931093 else
10941094 Name = "ldexpl";
10951095
1096 Constant *One = Context->getConstantFP(APFloat(1.0f));
1096 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
10971097 if (Op->getType() != Type::FloatTy)
10981098 One = Context->getConstantExprFPExtend(One, Op->getType());
10991099
160160 return V;
161161
162162 if (DestTy->isFloatingPoint())
163 return Context.getConstantFP(APFloat(CI->getValue(),
163 return ConstantFP::get(Context, APFloat(CI->getValue(),
164164 DestTy != Type::PPC_FP128Ty));
165165
166166 // Otherwise, can't fold this (vector?)
244244 DestTy == Type::FP128Ty ? APFloat::IEEEquad :
245245 APFloat::Bogus,
246246 APFloat::rmNearestTiesToEven, &ignored);
247 return Context.getConstantFP(Val);
247 return ConstantFP::get(Context, Val);
248248 }
249249 return 0; // Can't fold.
250250 case Instruction::FPToUI:
278278 (void)apf.convertFromAPInt(api,
279279 opc==Instruction::SIToFP,
280280 APFloat::rmNearestTiesToEven);
281 return Context.getConstantFP(apf);
281 return ConstantFP::get(Context, apf);
282282 }
283283 return 0;
284284 case Instruction::ZExt:
794794 break;
795795 case Instruction::FAdd:
796796 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
797 return Context.getConstantFP(C3V);
797 return ConstantFP::get(Context, C3V);
798798 case Instruction::FSub:
799799 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
800 return Context.getConstantFP(C3V);
800 return ConstantFP::get(Context, C3V);
801801 case Instruction::FMul:
802802 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
803 return Context.getConstantFP(C3V);
803 return ConstantFP::get(Context, C3V);
804804 case Instruction::FDiv:
805805 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
806 return Context.getConstantFP(C3V);
806 return ConstantFP::get(Context, C3V);
807807 case Instruction::FRem:
808808 (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
809 return Context.getConstantFP(C3V);
809 return ConstantFP::get(Context, C3V);
810810 }
811811 }
812812 } else if (const VectorType *VTy = dyn_cast(C1->getType())) {
256256 return &APFloat::PPCDoubleDouble;
257257 }
258258 #endif
259
260 /// get() - This returns a constant fp for the specified value in the
261 /// specified type. This should only be used for simple constant values like
262 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
263 Constant* ConstantFP::get(const Type* Ty, double V) {
264 LLVMContext &Context = Ty->getContext();
265
266 APFloat FV(V);
267 bool ignored;
268 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
269 APFloat::rmNearestTiesToEven, &ignored);
270 Constant *C = get(Context, FV);
271
272 // For vectors, broadcast the value.
273 if (const VectorType *VTy = dyn_cast(Ty))
274 return Context.getConstantVector(
275 std::vector(VTy->getNumElements(), C));
276
277 return C;
278 }
279
280 ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
281 LLVMContext &Context = Ty->getContext();
282 APFloat apf = cast (Context.getNullValue(Ty))->getValueAPF();
283 apf.changeSign();
284 return get(Context, apf);
285 }
286
287
288 Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
289 LLVMContext &Context = Ty->getContext();
290 if (const VectorType *PTy = dyn_cast(Ty))
291 if (PTy->getElementType()->isFloatingPoint()) {
292 std::vector zeros(PTy->getNumElements(),
293 getNegativeZero(PTy->getElementType()));
294 return Context.getConstantVector(PTy, zeros);
295 }
296
297 if (Ty->isFloatingPoint())
298 return getNegativeZero(Ty);
299
300 return Context.getNullValue(Ty);
301 }
302
303
304 // ConstantFP accessors.
305 ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
306 DenseMapAPFloatKeyInfo::KeyTy Key(V);
307
308 LLVMContextImpl* pImpl = Context.pImpl;
309
310 pImpl->ConstantsLock.reader_acquire();
311 ConstantFP *&Slot = pImpl->FPConstants[Key];
312 pImpl->ConstantsLock.reader_release();
313
314 if (!Slot) {
315 sys::SmartScopedWriter Writer(pImpl->ConstantsLock);
316 ConstantFP *&NewSlot = pImpl->FPConstants[Key];
317 if (!NewSlot) {
318 const Type *Ty;
319 if (&V.getSemantics() == &APFloat::IEEEsingle)
320 Ty = Type::FloatTy;
321 else if (&V.getSemantics() == &APFloat::IEEEdouble)
322 Ty = Type::DoubleTy;
323 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
324 Ty = Type::X86_FP80Ty;
325 else if (&V.getSemantics() == &APFloat::IEEEquad)
326 Ty = Type::FP128Ty;
327 else {
328 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
329 "Unknown FP format");
330 Ty = Type::PPC_FP128Ty;
331 }
332 NewSlot = new ConstantFP(Ty, V);
333 }
334
335 return NewSlot;
336 }
337
338 return Slot;
339 }
259340
260341 ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
261342 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
387387 bool ignored;
388388 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
389389 &ignored);
390 return wrap(getGlobalContext().getConstantFP(APN));
390 return wrap(ConstantFP::get(getGlobalContext(), APN));
391391 }
392392
393393 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
394 return wrap(getGlobalContext().getConstantFP(
394 return wrap(ConstantFP::get(getGlobalContext(),
395395 APFloat(SemanticsForType(unwrap(RealTy)), Text)));
396396 }
397397
15761576 BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
15771577 Value *Op, const Twine &Name,
15781578 Instruction *InsertBefore) {
1579 Value *zero = Context.getZeroValueForNegation(Op->getType());
1579 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
15801580 return new BinaryOperator(Instruction::Sub,
15811581 zero, Op,
15821582 Op->getType(), Name, InsertBefore);
15851585 BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
15861586 Value *Op, const Twine &Name,
15871587 BasicBlock *InsertAtEnd) {
1588 Value *zero = Context.getZeroValueForNegation(Op->getType());
1588 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
15891589 return new BinaryOperator(Instruction::Sub,
15901590 zero, Op,
15911591 Op->getType(), Name, InsertAtEnd);
15941594 BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
15951595 Value *Op, const Twine &Name,
15961596 Instruction *InsertBefore) {
1597 Value *zero = Context.getZeroValueForNegation(Op->getType());
1597 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
15981598 return new BinaryOperator(Instruction::FSub,
15991599 zero, Op,
16001600 Op->getType(), Name, InsertBefore);
16031603 BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
16041604 Value *Op, const Twine &Name,
16051605 BasicBlock *InsertAtEnd) {
1606 Value *zero = Context.getZeroValueForNegation(Op->getType());
1606 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
16071607 return new BinaryOperator(Instruction::FSub,
16081608 zero, Op,
16091609 Op->getType(), Name, InsertAtEnd);
4040 case Type::IntegerTyID:
4141 return ConstantInt::get(Ty, 0);
4242 case Type::FloatTyID:
43 return getConstantFP(APFloat(APInt(32, 0)));
43 return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
4444 case Type::DoubleTyID:
45 return getConstantFP(APFloat(APInt(64, 0)));
45 return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
4646 case Type::X86_FP80TyID:
47 return getConstantFP(APFloat(APInt(80, 2, zero)));
47 return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
4848 case Type::FP128TyID:
49 return getConstantFP(APFloat(APInt(128, 2, zero), true));
49 return ConstantFP::get(Ty->getContext(),
50 APFloat(APInt(128, 2, zero), true));
5051 case Type::PPC_FP128TyID:
51 return getConstantFP(APFloat(APInt(128, 2, zero)));
52 return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
5253 case Type::PointerTyID:
5354 return getConstantPointerNull(cast(Ty));
5455 case Type::StructTyID:
275276 assert(C->getType()->isIntOrIntVector() &&
276277 "Cannot NEG a nonintegral value!");
277278 return getConstantExpr(Instruction::Sub,
278 getZeroValueForNegation(C->getType()),
279 ConstantFP::getZeroValueForNegation(C->getType()),
279280 C);
280281 }
281282
283284 assert(C->getType()->isFPOrFPVector() &&
284285 "Cannot FNEG a non-floating-point value!");
285286 return getConstantExpr(Instruction::FSub,
286 getZeroValueForNegation(C->getType()),
287 ConstantFP::getZeroValueForNegation(C->getType()),
287288 C);
288289 }
289290
422423 getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
423424 return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
424425 }
425
426 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
427 if (const VectorType *PTy = dyn_cast(Ty))
428 if (PTy->getElementType()->isFloatingPoint()) {
429 std::vector zeros(PTy->getNumElements(),
430 getConstantFPNegativeZero(PTy->getElementType()));
431 return getConstantVector(PTy, zeros);
432 }
433
434 if (Ty->isFloatingPoint())
435 return getConstantFPNegativeZero(Ty);
436
437 return getNullValue(Ty);
438 }
439
440
441 // ConstantFP accessors.
442 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
443 return pImpl->getConstantFP(V);
444 }
445
446 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
447 if (Ty == Type::FloatTy)
448 return &APFloat::IEEEsingle;
449 if (Ty == Type::DoubleTy)
450 return &APFloat::IEEEdouble;
451 if (Ty == Type::X86_FP80Ty)
452 return &APFloat::x87DoubleExtended;
453 else if (Ty == Type::FP128Ty)
454 return &APFloat::IEEEquad;
455
456 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
457 return &APFloat::PPCDoubleDouble;
458 }
459
460 /// get() - This returns a constant fp for the specified value in the
461 /// specified type. This should only be used for simple constant values like
462 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
463 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
464 APFloat FV(V);
465 bool ignored;
466 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
467 APFloat::rmNearestTiesToEven, &ignored);
468 Constant *C = getConstantFP(FV);
469
470 // For vectors, broadcast the value.
471 if (const VectorType *VTy = dyn_cast(Ty))
472 return
473 getConstantVector(std::vector(VTy->getNumElements(), C));
474
475 return C;
476 }
477
478 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
479 APFloat apf = cast (getNullValue(Ty))->getValueAPF();
480 apf.changeSign();
481 return getConstantFP(apf);
482 }
483
484426
485427 // ConstantVector accessors.
486428 Constant* LLVMContext::getConstantVector(const VectorType* T,
4747
4848 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) :
4949 Context(C), TheTrueVal(0), TheFalseVal(0) { }
50
51
52 ConstantFP *LLVMContextImpl::getConstantFP(const APFloat &V) {
53 DenseMapAPFloatKeyInfo::KeyTy Key(V);
54
55 ConstantsLock.reader_acquire();
56 ConstantFP *&Slot = FPConstants[Key];
57 ConstantsLock.reader_release();
58
59 if (!Slot) {
60 sys::SmartScopedWriter Writer(ConstantsLock);
61 ConstantFP *&NewSlot = FPConstants[Key];
62 if (!NewSlot) {
63 const Type *Ty;
64 if (&V.getSemantics() == &APFloat::IEEEsingle)
65 Ty = Type::FloatTy;
66 else if (&V.getSemantics() == &APFloat::IEEEdouble)
67 Ty = Type::DoubleTy;
68 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
69 Ty = Type::X86_FP80Ty;
70 else if (&V.getSemantics() == &APFloat::IEEEquad)
71 Ty = Type::FP128Ty;
72 else {
73 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
74 "Unknown FP format");
75 Ty = Type::PPC_FP128Ty;
76 }
77 NewSlot = new ConstantFP(Ty, V);
78 }
79
80 return NewSlot;
81 }
82
83 return Slot;
84 }
8550
8651 MDString *LLVMContextImpl::getMDString(const char *StrBegin,
8752 unsigned StrLength) {
456456 LLVMContextImpl(const LLVMContextImpl&);
457457
458458 friend class ConstantInt;
459 friend class ConstantFP;
459460 public:
460461 LLVMContextImpl(LLVMContext &C);
461
462 ConstantFP *getConstantFP(const APFloat &V);
463462
464463 MDString *getMDString(const char *StrBegin, unsigned StrLength);
465464