1 //===--------------- LLVMContextImpl.cpp - Implementation ------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements LLVMContextImpl, the opaque implementation
13 //===----------------------------------------------------------------------===//
15 #include "LLVMContextImpl.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/LLVMContext.h"
21 // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
22 // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
23 // operator== and operator!= to ensure that the DenseMap doesn't attempt to
24 // compare APInt's of different widths, which would violate an APInt class
25 // invariant which generates an assertion.
26 ConstantInt *LLVMContextImpl::getConstantInt(const APInt& V) {
27 // Get the corresponding integer type for the bit width of the value.
28 const IntegerType *ITy = Context.getIntegerType(V.getBitWidth());
29 // get an existing value or the insertion position
30 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
32 ConstantsLock.reader_acquire();
33 ConstantInt *&Slot = IntConstants[Key];
34 ConstantsLock.reader_release();
37 sys::SmartScopedWriter<true> Writer(ConstantsLock);
38 ConstantInt *&NewSlot = IntConstants[Key];
40 NewSlot = new ConstantInt(ITy, V);
49 ConstantFP *LLVMContextImpl::getConstantFP(const APFloat &V) {
50 DenseMapAPFloatKeyInfo::KeyTy Key(V);
52 ConstantsLock.reader_acquire();
53 ConstantFP *&Slot = FPConstants[Key];
54 ConstantsLock.reader_release();
57 sys::SmartScopedWriter<true> Writer(ConstantsLock);
58 ConstantFP *&NewSlot = FPConstants[Key];
61 if (&V.getSemantics() == &APFloat::IEEEsingle)
63 else if (&V.getSemantics() == &APFloat::IEEEdouble)
65 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
66 Ty = Type::X86_FP80Ty;
67 else if (&V.getSemantics() == &APFloat::IEEEquad)
70 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
72 Ty = Type::PPC_FP128Ty;
74 NewSlot = new ConstantFP(Ty, V);