1 //===-- llvm/LLVMContext.h - Class for managing "global" state --*- 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 declares LLVMContext, a container of "global" state in LLVM, such
11 // as the global type and constant uniquing tables.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LLVMCONTEXT_H
16 #define LLVM_LLVMCONTEXT_H
18 #include "llvm/Support/DataTypes.h"
24 class LLVMContextImpl;
27 class ConstantPointerNull;
29 class ConstantAggregateZero;
48 /// This is an important class for using LLVM in a threaded context. It
49 /// (opaquely) owns and manages the core "global" data of LLVM's core
50 /// infrastructure, including the type and constant uniquing tables.
51 /// LLVMContext itself provides no locking guarantees, so you should be careful
52 /// to have one context per thread.
54 LLVMContextImpl* pImpl;
60 Constant* getNullValue(const Type* Ty);
62 /// @returns the value for an integer constant of the given type that has all
63 /// its bits set to true.
64 /// @brief Get the all ones value
65 Constant* getAllOnesValue(const Type* Ty);
67 // UndefValue accessors
68 UndefValue* getUndef(const Type* Ty);
70 // ConstantInt accessors
71 ConstantInt* getTrue();
72 ConstantInt* getFalse();
74 /// If Ty is a vector type, return a Constant with a splat of the given
75 /// value. Otherwise return a ConstantInt for the given value.
76 Constant* getConstantInt(const Type* Ty, uint64_t V,
77 bool isSigned = false);
79 /// Return a ConstantInt with the specified integer value for the specified
80 /// type. If the type is wider than 64 bits, the value will be zero-extended
81 /// to fit the type, unless isSigned is true, in which case the value will
82 /// be interpreted as a 64-bit signed integer and sign-extended to fit
84 /// @brief Get a ConstantInt for a specific value.
85 ConstantInt* getConstantInt(const IntegerType* Ty, uint64_t V,
86 bool isSigned = false);
88 /// Return a ConstantInt with the specified value for the specified type. The
89 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
90 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
91 /// signed value for the type Ty.
92 /// @brief Get a ConstantInt for a specific signed value.
93 ConstantInt* getConstantIntSigned(const IntegerType* Ty, int64_t V);
94 Constant *getConstantIntSigned(const Type *Ty, int64_t V);
96 /// Return a ConstantInt with the specified value and an implied Type. The
97 /// type is the integer type that corresponds to the bit width of the value.
98 ConstantInt* getConstantInt(const APInt& V);
100 /// If Ty is a vector type, return a Constant with a splat of the given
101 /// value. Otherwise return a ConstantInt for the given value.
102 Constant* getConstantInt(const Type* Ty, const APInt& V);
104 // ConstantPointerNull accessors
105 ConstantPointerNull* getConstantPointerNull(const PointerType* T);
107 // ConstantStruct accessors
108 Constant* getConstantStruct(const StructType* T,
109 const std::vector<Constant*>& V);
110 Constant* getConstantStruct(const std::vector<Constant*>& V,
111 bool Packed = false);
112 Constant* getConstantStruct(Constant* const *Vals, unsigned NumVals,
113 bool Packed = false);
115 // ConstantAggregateZero accessors
116 ConstantAggregateZero* getConstantAggregateZero(const Type* Ty);
118 // ConstantArray accessors
119 Constant* getConstantArray(const ArrayType* T,
120 const std::vector<Constant*>& V);
121 Constant* getConstantArray(const ArrayType* T, Constant* const* Vals,
124 /// This method constructs a ConstantArray and initializes it with a text
125 /// string. The default behavior (AddNull==true) causes a null terminator to
126 /// be placed at the end of the array. This effectively increases the length
127 /// of the array by one (you've been warned). However, in some situations
128 /// this is not desired so if AddNull==false then the string is copied without
129 /// null termination.
130 Constant* getConstantArray(const std::string& Initializer,
131 bool AddNull = true);
133 // ConstantExpr accessors
134 Constant* getConstantExpr(unsigned Opcode, Constant* C1, Constant* C2);
135 Constant* getConstantExprTrunc(Constant* C, const Type* Ty);
136 Constant* getConstantExprSExt(Constant* C, const Type* Ty);
137 Constant* getConstantExprZExt(Constant* C, const Type* Ty);
138 Constant* getConstantExprFPTrunc(Constant* C, const Type* Ty);
139 Constant* getConstantExprFPExtend(Constant* C, const Type* Ty);
140 Constant* getConstantExprUIToFP(Constant* C, const Type* Ty);
141 Constant* getConstantExprSIToFP(Constant* C, const Type* Ty);
142 Constant* getConstantExprFPToUI(Constant* C, const Type* Ty);
143 Constant* getConstantExprFPToSI(Constant* C, const Type* Ty);
144 Constant* getConstantExprPtrToInt(Constant* C, const Type* Ty);
145 Constant* getConstantExprIntToPtr(Constant* C, const Type* Ty);
146 Constant* getConstantExprBitCast(Constant* C, const Type* Ty);
147 Constant* getConstantExprCast(unsigned ops, Constant* C, const Type* Ty);
148 Constant* getConstantExprZExtOrBitCast(Constant* C, const Type* Ty);
149 Constant* getConstantExprSExtOrBitCast(Constant* C, const Type* Ty);
150 Constant* getConstantExprTruncOrBitCast(Constant* C, const Type* Ty);
151 Constant* getConstantExprPointerCast(Constant* C, const Type* Ty);
152 Constant* getConstantExprIntegerCast(Constant* C, const Type* Ty,
154 Constant* getConstantExprFPCast(Constant* C, const Type* Ty);
155 Constant* getConstantExprSelect(Constant* C, Constant* V1, Constant* V2);
157 /// getAlignOf constant expr - computes the alignment of a type in a target
158 /// independent way (Note: the return type is an i32; Note: assumes that i8
159 /// is byte aligned).
161 Constant* getConstantExprAlignOf(const Type* Ty);
162 Constant* getConstantExprCompare(unsigned short pred,
163 Constant* C1, Constant* C2);
164 Constant* getConstantExprNeg(Constant* C);
165 Constant* getConstantExprFNeg(Constant* C);
166 Constant* getConstantExprNot(Constant* C);
167 Constant* getConstantExprAdd(Constant* C1, Constant* C2);
168 Constant* getConstantExprFAdd(Constant* C1, Constant* C2);
169 Constant* getConstantExprSub(Constant* C1, Constant* C2);
170 Constant* getConstantExprFSub(Constant* C1, Constant* C2);
171 Constant* getConstantExprMul(Constant* C1, Constant* C2);
172 Constant* getConstantExprFMul(Constant* C1, Constant* C2);
173 Constant* getConstantExprUDiv(Constant* C1, Constant* C2);
174 Constant* getConstantExprSDiv(Constant* C1, Constant* C2);
175 Constant* getConstantExprFDiv(Constant* C1, Constant* C2);
176 Constant* getConstantExprURem(Constant* C1, Constant* C2);
177 Constant* getConstantExprSRem(Constant* C1, Constant* C2);
178 Constant* getConstantExprFRem(Constant* C1, Constant* C2);
179 Constant* getConstantExprAnd(Constant* C1, Constant* C2);
180 Constant* getConstantExprOr(Constant* C1, Constant* C2);
181 Constant* getConstantExprXor(Constant* C1, Constant* C2);
182 Constant* getConstantExprICmp(unsigned short pred, Constant* LHS,
184 Constant* getConstantExprFCmp(unsigned short pred, Constant* LHS,
186 Constant* getConstantExprShl(Constant* C1, Constant* C2);
187 Constant* getConstantExprLShr(Constant* C1, Constant* C2);
188 Constant* getConstantExprAShr(Constant* C1, Constant* C2);
189 Constant* getConstantExprGetElementPtr(Constant* C, Constant* const* IdxList,
191 Constant* getConstantExprGetElementPtr(Constant* C, Value* const* IdxList,
193 Constant* getConstantExprExtractElement(Constant* Vec, Constant* Idx);
194 Constant* getConstantExprInsertElement(Constant* Vec, Constant* Elt,
196 Constant* getConstantExprShuffleVector(Constant* V1, Constant* V2,
198 Constant* getConstantExprExtractValue(Constant* Agg, const unsigned* IdxList,
200 Constant* getConstantExprInsertValue(Constant* Agg, Constant* Val,
201 const unsigned* IdxList,
204 /// getSizeOf constant expr - computes the size of a type in a target
205 /// independent way (Note: the return type is an i64).
207 Constant* getConstantExprSizeOf(const Type* Ty);
209 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
210 /// method returns the negative zero constant for floating point or vector
211 /// floating point types; for all other types, it returns the null value.
212 Constant* getZeroValueForNegation(const Type* Ty);
214 // ConstantFP accessors
215 ConstantFP* getConstantFP(const APFloat& V);
217 /// get() - This returns a ConstantFP, or a vector containing a splat of a
218 /// ConstantFP, for the specified value in the specified type. This should
219 /// only be used for simple constant values like 2.0/1.0 etc, that are
220 /// known-valid both as host double and as the target format.
221 Constant* getConstantFP(const Type* Ty, double V);
222 ConstantFP* getConstantFPNegativeZero(const Type* Ty);
224 // ConstantVector accessors
225 Constant* getConstantVector(const VectorType* T,
226 const std::vector<Constant*>& V);
227 Constant* getConstantVector(const std::vector<Constant*>& V);
228 Constant* getConstantVector(Constant* const* Vals, unsigned NumVals);
231 MDNode* getMDNode(Value* const* Vals, unsigned NumVals);
233 // MDString accessors
234 MDString* getMDString(const char *StrBegin, const char *StrEnd);
235 MDString* getMDString(const std::string &Str);
237 // FunctionType accessors
238 FunctionType* getFunctionType(const Type* Result, bool isVarArg);
239 FunctionType* getFunctionType(const Type* Result,
240 const std::vector<const Type*>& Params,
243 // IntegerType accessors
244 const IntegerType* getIntegerType(unsigned NumBits);
246 // OpaqueType accessors
247 OpaqueType* getOpaqueType();
249 // StructType accessors
250 StructType* getStructType(bool isPacked=false);
251 StructType* getStructType(const std::vector<const Type*>& Params,
252 bool isPacked = false);
253 StructType* getStructType(const Type* type, ...);
255 // ArrayType accessors
256 ArrayType* getArrayType(const Type* ElementType, uint64_t NumElements);
258 // PointerType accessors
259 PointerType* getPointerType(const Type* ElementType, unsigned AddressSpace);
260 PointerType* getPointerTypeUnqual(const Type* ElementType);
262 // VectorType accessors
263 VectorType* getVectorType(const Type* ElementType, unsigned NumElements);
264 VectorType* getVectorTypeInteger(const VectorType* VTy);
265 VectorType* getVectorTypeExtendedElement(const VectorType* VTy);
266 VectorType* getVectorTypeTruncatedElement(const VectorType* VTy);
269 /// @brief Create a result type for fcmp/icmp
270 const Type* makeCmpResultType(const Type* opnd_type);
272 // Methods for erasing constants
273 void erase(MDString *M);
274 void erase(MDNode *M);
277 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.
278 extern LLVMContext& getGlobalContext();