llvm.org GIT mirror llvm / 914e50c
Privatize the ConstantFP table. I'm on a roll! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76097 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 11 years ago
13 changed file(s) with 113 addition(s) and 115 deletion(s). Raw diff Collapse all Expand all
158158
159159

                  
                
160160 Value *NumberExprAST::Codegen() {
161 return ConstantFP::get(APFloat(Val));
161 return getGlobalContext().getConstantFP(APFloat(Val));
162162 }
163163
164164
169169 constants of Arbitrary Precision). This code basically just
170170 creates and returns a ConstantFP. Note that in the LLVM IR
171171 that constants are all uniqued together and shared. For this reason, the API
172 uses "the foo::get(..)" idiom instead of "new foo(..)" or "foo::Create(..)".

172 uses "the Context.get..." idiom instead of "new foo(..)" or "foo::Create(..)".

173173
174174
175175

                  
                
307307 Function *PrototypeAST::Codegen() {
308308 // Make the function type: double(double,double) etc.
309309 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
310 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
310 FunctionType *FT = getGlobalContext().getFunctionType(Type::DoubleTy, Doubles, false);
311311
312312 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
313313
319319 by an expression), it makes sense for it to return the LLVM Function it
320320 corresponds to when codegen'd.

321321
322

The call to FunctionType::get creates

322

The call to Context.get creates

323323 the FunctionType that should be used for a given Prototype. Since all
324324 function arguments in Kaleidoscope are of type double, the first line creates
325 a vector of "N" LLVM double types. It then uses the FunctionType::get
325 a vector of "N" LLVM double types. It then uses the Context.get
326326 method to create a function type that takes "N" doubles as arguments, returns
327327 one double as a result, and that is not vararg (the false parameter indicates
328328 this). Note that Types in LLVM are uniqued just like Constants are, so you
10331033 Value *ErrorV(const char *Str) { Error(Str); return 0; }
10341034
10351035 Value *NumberExprAST::Codegen() {
1036 return ConstantFP::get(APFloat(Val));
1036 return getGlobalContext().getConstantFP(APFloat(Val));
10371037 }
10381038
10391039 Value *VariableExprAST::Codegen() {
10811081 Function *PrototypeAST::Codegen() {
10821082 // Make the function type: double(double,double) etc.
10831083 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
1084 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
1084 FunctionType *FT = getGlobalContext().getFunctionType(Type::DoubleTy, Doubles, false);
10851085
10861086 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
10871087
868868 Value *ErrorV(const char *Str) { Error(Str); return 0; }
869869
870870 Value *NumberExprAST::Codegen() {
871 return ConstantFP::get(APFloat(Val));
871 return getGlobalContext().getConstantFP(APFloat(Val));
872872 }
873873
874874 Value *VariableExprAST::Codegen() {
916916 Function *PrototypeAST::Codegen() {
917917 // Make the function type: double(double,double) etc.
918918 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
919 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
919 FunctionType *FT = getGlobalContext().getFunctionType(Type::DoubleTy, Doubles, false);
920920
921921 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
922922
363363
364364 // Convert condition to a bool by comparing equal to 0.0.
365365 CondV = Builder.CreateFCmpONE(CondV,
366 ConstantFP::get(APFloat(0.0)),
366 getGlobalContext().getConstantFP(APFloat(0.0)),
367367 "ifcond");
368368
369369
795795 if (StepVal == 0) return 0;
796796 } else {
797797 // If not specified, use 1.0.
798 StepVal = ConstantFP::get(APFloat(1.0));
798 StepVal = getGlobalContext().getConstantFP(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 ConstantFP::get(APFloat(0.0)),
817 getGlobalContext().getConstantFP(APFloat(0.0)),
818818 "loopcond");
819819
820820
13591359 Value *ErrorV(const char *Str) { Error(Str); return 0; }
13601360
13611361 Value *NumberExprAST::Codegen() {
1362 return ConstantFP::get(APFloat(Val));
1362 return getGlobalContext().getConstantFP(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 ConstantFP::get(APFloat(0.0)),
1413 getGlobalContext().getConstantFP(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 = ConstantFP::get(APFloat(1.0));
1512 StepVal = getGlobalContext().getConstantFP(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 ConstantFP::get(APFloat(0.0)),
1523 getGlobalContext().getConstantFP(APFloat(0.0)),
15241524 "loopcond");
15251525
15261526 // Create the "after loop" block and insert it.
15441544
15451545
15461546 // for expr always returns 0.0.
1547 return Constant::getNullValue(Type::DoubleTy);
1547 return getGlobalContext().getNullValue(Type::DoubleTy);
15481548 }
15491549
15501550 Function *PrototypeAST::Codegen() {
15511551 // Make the function type: double(double,double) etc.
15521552 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
1553 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
1553 FunctionType *FT = getGlobalContext().getFunctionType(Type::DoubleTy, Doubles, false);
15541554
15551555 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
15561556
13641364 Value *ErrorV(const char *Str) { Error(Str); return 0; }
13651365
13661366 Value *NumberExprAST::Codegen() {
1367 return ConstantFP::get(APFloat(Val));
1367 return getGlobalContext().getConstantFP(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 ConstantFP::get(APFloat(0.0)),
1438 getGlobalContext().getConstantFP(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 = ConstantFP::get(APFloat(1.0));
1537 StepVal = getGlobalContext().getConstantFP(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 ConstantFP::get(APFloat(0.0)),
1548 getGlobalContext().getConstantFP(APFloat(0.0)),
15491549 "loopcond");
15501550
15511551 // Create the "after loop" block and insert it.
15751575 Function *PrototypeAST::Codegen() {
15761576 // Make the function type: double(double,double) etc.
15771577 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
1578 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
1578 FunctionType *FT = getGlobalContext().getFunctionType(Type::DoubleTy, Doubles, false);
15791579
15801580 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
15811581
922922 InitVal = Init->Codegen();
923923 if (InitVal == 0) return 0;
924924 } else { // If not specified, use 0.0.
925 InitVal = ConstantFP::get(APFloat(0.0));
925 InitVal = getGlobalContext().getConstantFP(APFloat(0.0));
926926 }
927927
928928 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
16221622
16231623
16241624 Value *NumberExprAST::Codegen() {
1625 return ConstantFP::get(APFloat(Val));
1625 return getGlobalContext().getConstantFP(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 ConstantFP::get(APFloat(0.0)),
1718 getGlobalContext().getConstantFP(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 = ConstantFP::get(APFloat(1.0));
1824 StepVal = getGlobalContext().getConstantFP(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 ConstantFP::get(APFloat(0.0)),
1839 getGlobalContext().getConstantFP(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 = ConstantFP::get(APFloat(0.0));
1883 InitVal = getGlobalContext().getConstantFP(APFloat(0.0));
18841884 }
18851885
18861886 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
19101910 Function *PrototypeAST::Codegen() {
19111911 // Make the function type: double(double,double) etc.
19121912 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
1913 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
1913 FunctionType *FT = getGlobalContext().getFunctionType(Type::DoubleTy, Doubles, false);
19141914
19151915 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
19161916
620620
621621
622622 Value *NumberExprAST::Codegen() {
623 return ConstantFP::get(APFloat(Val));
623 return getGlobalContext().getConstantFP(APFloat(Val));
624624 }
625625
626626 Value *VariableExprAST::Codegen() {
713713
714714 // Convert condition to a bool by comparing equal to 0.0.
715715 CondV = Builder.CreateFCmpONE(CondV,
716 ConstantFP::get(APFloat(0.0)),
716 getGlobalContext().getConstantFP(APFloat(0.0)),
717717 "ifcond");
718718
719719 Function *TheFunction = Builder.GetInsertBlock()->getParent();
818818 if (StepVal == 0) return 0;
819819 } else {
820820 // If not specified, use 1.0.
821 StepVal = ConstantFP::get(APFloat(1.0));
821 StepVal = getGlobalContext().getConstantFP(APFloat(1.0));
822822 }
823823
824824 // Compute the end condition.
833833
834834 // Convert condition to a bool by comparing equal to 0.0.
835835 EndCond = Builder.CreateFCmpONE(EndCond,
836 ConstantFP::get(APFloat(0.0)),
836 getGlobalContext().getConstantFP(APFloat(0.0)),
837837 "loopcond");
838838
839839 // Create the "after loop" block and insert it.
876876 InitVal = Init->Codegen();
877877 if (InitVal == 0) return 0;
878878 } else { // If not specified, use 0.0.
879 InitVal = ConstantFP::get(APFloat(0.0));
879 InitVal = getGlobalContext().getConstantFP(APFloat(0.0));
880880 }
881881
882882 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
906906 Function *PrototypeAST::Codegen() {
907907 // Make the function type: double(double,double) etc.
908908 std::vector Doubles(Args.size(), Type::DoubleTy);
909 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
909 FunctionType *FT =
910 getGlobalContext().getFunctionType(Type::DoubleTy, Doubles, false);
910911
911912 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
912913
10831084
10841085 int main() {
10851086 InitializeNativeTarget();
1086 LLVMContext Context;
1087 LLVMContext &Context = getGlobalContext();
10871088
10881089 // Install standard binary operators.
10891090 // 1 is lowest precedence.
212212 APFloat Val;
213213 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
214214 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
215 friend class LLVMContextImpl;
215216 protected:
216217 ConstantFP(const Type *Ty, const APFloat& V);
217218 protected:
220221 return User::operator new(s, 0);
221222 }
222223 public:
223 /// get() - Static factory methods - Return objects of the specified value
224 static ConstantFP *get(const APFloat &V);
225
226224 /// isValueValidForType - return true if Ty is big enough to represent V.
227225 static bool isValueValidForType(const Type *Ty, const APFloat& V);
228226 inline const APFloat& getValueAPF() const { return Val; }
915915
916916
917917 SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
918 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
918 return getConstantFP(*Context->getConstantFP(V), VT, isTarget);
919919 }
920920
921921 SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
21452145 const VectorType *DestTy = cast(I.getType());
21462146 const Type *ElTy = DestTy->getElementType();
21472147 unsigned VL = DestTy->getNumElements();
2148 std::vector NZ(VL, Context->getConstantFPNegativeZero(ElTy));
2148 std::vector NZ(VL,
2149 DAG.getContext()->getConstantFPNegativeZero(ElTy));
21492150 Constant *CNZ = DAG.getContext()->getConstantVector(&NZ[0], NZ.size());
21502151 if (CV == CNZ) {
21512152 SDValue Op2 = getValue(I.getOperand(1));
21572158 }
21582159 if (ConstantFP *CFP = dyn_cast(I.getOperand(0)))
21592160 if (CFP->isExactlyValue(
2160 Context->getConstantFPNegativeZero(Ty)->getValueAPF())) {
2161 DAG.getContext()->getConstantFPNegativeZero(Ty)->getValueAPF())) {
21612162 SDValue Op2 = getValue(I.getOperand(1));
21622163 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
21632164 Op2.getValueType(), Op2));
219219
220220 bool ConstantFP::isExactlyValue(const APFloat& V) const {
221221 return Val.bitwiseIsEqual(V);
222 }
223
224 namespace {
225 struct DenseMapAPFloatKeyInfo {
226 struct KeyTy {
227 APFloat val;
228 KeyTy(const APFloat& V) : val(V){}
229 KeyTy(const KeyTy& that) : val(that.val) {}
230 bool operator==(const KeyTy& that) const {
231 return this->val.bitwiseIsEqual(that.val);
232 }
233 bool operator!=(const KeyTy& that) const {
234 return !this->operator==(that);
235 }
236 };
237 static inline KeyTy getEmptyKey() {
238 return KeyTy(APFloat(APFloat::Bogus,1));
239 }
240 static inline KeyTy getTombstoneKey() {
241 return KeyTy(APFloat(APFloat::Bogus,2));
242 }
243 static unsigned getHashValue(const KeyTy &Key) {
244 return Key.val.getHashValue();
245 }
246 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
247 return LHS == RHS;
248 }
249 static bool isPod() { return false; }
250 };
251 }
252
253 //---- ConstantFP::get() implementation...
254 //
255 typedef DenseMap
256 DenseMapAPFloatKeyInfo> FPMapTy;
257
258 static ManagedStatic FPConstants;
259
260 ConstantFP *ConstantFP::get(const APFloat &V) {
261 DenseMapAPFloatKeyInfo::KeyTy Key(V);
262
263 ConstantsLock->reader_acquire();
264 ConstantFP *&Slot = (*FPConstants)[Key];
265 ConstantsLock->reader_release();
266
267 if (!Slot) {
268 sys::SmartScopedWriter Writer(*ConstantsLock);
269 ConstantFP *&NewSlot = (*FPConstants)[Key];
270 if (!NewSlot) {
271 const Type *Ty;
272 if (&V.getSemantics() == &APFloat::IEEEsingle)
273 Ty = Type::FloatTy;
274 else if (&V.getSemantics() == &APFloat::IEEEdouble)
275 Ty = Type::DoubleTy;
276 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
277 Ty = Type::X86_FP80Ty;
278 else if (&V.getSemantics() == &APFloat::IEEEquad)
279 Ty = Type::FP128Ty;
280 else {
281 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
282 "Unknown FP format");
283 Ty = Type::PPC_FP128Ty;
284 }
285 NewSlot = new ConstantFP(Ty, V);
286 }
287
288 return NewSlot;
289 }
290
291 return Slot;
292222 }
293223
294224 //===----------------------------------------------------------------------===//
481481
482482 // ConstantFP accessors.
483483 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
484 return ConstantFP::get(V);
484 return pImpl->getConstantFP(V);
485485 }
486486
487487 static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
4545 }
4646 }
4747
48 ConstantFP *LLVMContextImpl::getConstantFP(const APFloat &V) {
49 DenseMapAPFloatKeyInfo::KeyTy Key(V);
50
51 ConstantsLock.reader_acquire();
52 ConstantFP *&Slot = FPConstants[Key];
53 ConstantsLock.reader_release();
54
55 if (!Slot) {
56 sys::SmartScopedWriter Writer(ConstantsLock);
57 ConstantFP *&NewSlot = FPConstants[Key];
58 if (!NewSlot) {
59 const Type *Ty;
60 if (&V.getSemantics() == &APFloat::IEEEsingle)
61 Ty = Type::FloatTy;
62 else if (&V.getSemantics() == &APFloat::IEEEdouble)
63 Ty = Type::DoubleTy;
64 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
65 Ty = Type::X86_FP80Ty;
66 else if (&V.getSemantics() == &APFloat::IEEEquad)
67 Ty = Type::FP128Ty;
68 else {
69 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
70 "Unknown FP format");
71 Ty = Type::PPC_FP128Ty;
72 }
73 NewSlot = new ConstantFP(Ty, V);
74 }
75
76 return NewSlot;
77 }
78
79 return Slot;
80 }
1515 #define LLVM_LLVMCONTEXT_IMPL_H
1616
1717 #include "llvm/System/RWMutex.h"
18 #include "llvm/ADT/APFloat.h"
1819 #include "llvm/ADT/APInt.h"
1920 #include "llvm/ADT/DenseMap.h"
2021
2122 namespace llvm {
2223
2324 class ConstantInt;
25 class ConstantFP;
2426 class LLVMContext;
2527 class Type;
2628
4951 static bool isPod() { return false; }
5052 };
5153
54 struct DenseMapAPFloatKeyInfo {
55 struct KeyTy {
56 APFloat val;
57 KeyTy(const APFloat& V) : val(V){}
58 KeyTy(const KeyTy& that) : val(that.val) {}
59 bool operator==(const KeyTy& that) const {
60 return this->val.bitwiseIsEqual(that.val);
61 }
62 bool operator!=(const KeyTy& that) const {
63 return !this->operator==(that);
64 }
65 };
66 static inline KeyTy getEmptyKey() {
67 return KeyTy(APFloat(APFloat::Bogus,1));
68 }
69 static inline KeyTy getTombstoneKey() {
70 return KeyTy(APFloat(APFloat::Bogus,2));
71 }
72 static unsigned getHashValue(const KeyTy &Key) {
73 return Key.val.getHashValue();
74 }
75 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
76 return LHS == RHS;
77 }
78 static bool isPod() { return false; }
79 };
80
5281 class LLVMContextImpl {
5382 sys::SmartRWMutex ConstantsLock;
5483
5584 typedef DenseMap
5685 DenseMapAPIntKeyInfo> IntMapTy;
5786 IntMapTy IntConstants;
87
88 typedef DenseMap
89 DenseMapAPFloatKeyInfo> FPMapTy;
90 FPMapTy FPConstants;
5891
5992 LLVMContext &Context;
6093 LLVMContextImpl();
6497
6598 /// Return a ConstantInt with the specified value and an implied Type. The
6699 /// type is the integer type that corresponds to the bit width of the value.
67 ConstantInt* getConstantInt(const APInt &V);
100 ConstantInt *getConstantInt(const APInt &V);
101
102 ConstantFP *getConstantFP(const APFloat &V);
68103 };
69104
70105 }