552e7992c6da5be2eaad13eade0b40a87cefdc87
[oota-llvm.git] / include / llvm / LLVMContext.h
1 //===-- llvm/LLVMContext.h - Class for managing "global" state --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares LLVMContext, a container of "global" state in LLVM, such
11 // as the global type and constant uniquing tables.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LLVMCONTEXT_H
16 #define LLVM_LLVMCONTEXT_H
17
18 #include "llvm/Support/DataTypes.h"
19 #include <vector>
20 #include <string>
21
22 namespace llvm {
23
24 class LLVMContextImpl;
25 class Constant;
26 class ConstantInt;
27 class ConstantPointerNull;
28 class ConstantStruct;
29 class ConstantAggregateZero;
30 class ConstantArray;
31 class ConstantFP;
32 class ConstantVector;
33 class UndefValue;
34 class MDNode;
35 class MDString;
36 class IntegerType;
37 class PointerType;
38 class StructType;
39 class ArrayType;
40 class VectorType;
41 class OpaqueType;
42 class FunctionType;
43 class Type;
44 class APInt;
45 class APFloat;
46 class Value;
47
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.
53 class LLVMContext {
54   LLVMContextImpl* pImpl;
55 public:
56   LLVMContext();
57   ~LLVMContext();
58   
59   // Constant accessors
60   Constant* getNullValue(const Type* Ty);
61   
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);
66   
67   // UndefValue accessors
68   UndefValue* getUndef(const Type* Ty);
69   
70   // ConstantInt accessors
71   ConstantInt* getConstantIntTrue();
72   ConstantInt* getConstantIntFalse();
73   
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);
78                               
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
83   /// the type.
84   /// @brief Get a ConstantInt for a specific value.
85   ConstantInt* getConstantInt(const IntegerType* Ty, uint64_t V,
86                               bool isSigned = false);
87
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);
95   
96   ConstantInt* getConstantInt(const APInt& V);
97   
98   /// If Ty is a vector type, return a Constant with a splat of the given
99   /// value. Otherwise return a ConstantInt for the given value.
100   Constant* getConstantInt(const Type* Ty, const APInt& V);
101   
102   // ConstantPointerNull accessors
103   ConstantPointerNull* getConstantPointerNull(const PointerType* T);
104   
105   // ConstantStruct accessors
106   Constant* getConstantStruct(const StructType* T,
107                               const std::vector<Constant*>& V);
108   Constant* getConstantStruct(const std::vector<Constant*>& V,
109                               bool Packed = false);
110   Constant* getConstantStruct(Constant* const *Vals, unsigned NumVals,
111                               bool Packed = false);
112                               
113   // ConstantAggregateZero accessors
114   ConstantAggregateZero* getConstantAggregateZero(const Type* Ty);
115   
116   // ConstantArray accessors
117   Constant* getConstantArray(const ArrayType* T,
118                              const std::vector<Constant*>& V);
119   Constant* getConstantArray(const ArrayType* T, Constant* const* Vals,
120                              unsigned NumVals);
121                              
122   /// This method constructs a ConstantArray and initializes it with a text
123   /// string. The default behavior (AddNull==true) causes a null terminator to
124   /// be placed at the end of the array. This effectively increases the length
125   /// of the array by one (you've been warned).  However, in some situations 
126   /// this is not desired so if AddNull==false then the string is copied without
127   /// null termination.
128   Constant* getConstantArray(const std::string& Initializer,
129                              bool AddNull = true);
130                              
131   // ConstantExpr accessors
132   Constant* getConstantExpr(unsigned Opcode, Constant* C1, Constant* C2);
133   Constant* getConstantExprTrunc(Constant* C, const Type* Ty);
134   Constant* getConstantExprSExt(Constant* C, const Type* Ty);
135   Constant* getConstantExprZExt(Constant* C, const Type* Ty);
136   Constant* getConstantExprFPTrunc(Constant* C, const Type* Ty);
137   Constant* getConstantExprFPExtend(Constant* C, const Type* Ty);
138   Constant* getConstantExprUIToFP(Constant* C, const Type* Ty);
139   Constant* getConstantExprSIToFP(Constant* C, const Type* Ty);
140   Constant* getConstantExprFPToUI(Constant* C, const Type* Ty);
141   Constant* getConstantExprFPToSI(Constant* C, const Type* Ty);
142   Constant* getConstantExprPtrToInt(Constant* C, const Type* Ty);
143   Constant* getConstantExprIntToPtr(Constant* C, const Type* Ty);
144   Constant* getConstantExprBitCast(Constant* C, const Type* Ty);
145   Constant* getConstantExprCast(unsigned ops, Constant* C, const Type* Ty);
146   Constant* getConstantExprZExtOrBitCast(Constant* C, const Type* Ty);
147   Constant* getConstantExprSExtOrBitCast(Constant* C, const Type* Ty);
148   Constant* getConstantExprTruncOrBitCast(Constant* C, const Type* Ty);
149   Constant* getConstantExprPointerCast(Constant* C, const Type* Ty);
150   Constant* getConstantExprIntegerCast(Constant* C, const Type* Ty,
151                                        bool isSigned);
152   Constant* getConstantExprFPCast(Constant* C, const Type* Ty);
153   Constant* getConstantExprSelect(Constant* C, Constant* V1, Constant* V2);
154   
155   /// getAlignOf constant expr - computes the alignment of a type in a target
156   /// independent way (Note: the return type is an i32; Note: assumes that i8
157   /// is byte aligned).
158   ///
159   Constant* getConstantExprAlignOf(const Type* Ty);
160   Constant* getConstantExprCompare(unsigned short pred,
161                                    Constant* C1, Constant* C2);
162   Constant* getConstantExprNeg(Constant* C);
163   Constant* getConstantExprFNeg(Constant* C);
164   Constant* getConstantExprNot(Constant* C);
165   Constant* getConstantExprAdd(Constant* C1, Constant* C2);
166   Constant* getConstantExprFAdd(Constant* C1, Constant* C2);
167   Constant* getConstantExprSub(Constant* C1, Constant* C2);
168   Constant* getConstantExprFSub(Constant* C1, Constant* C2);
169   Constant* getConstantExprMul(Constant* C1, Constant* C2);
170   Constant* getConstantExprFMul(Constant* C1, Constant* C2);
171   Constant* getConstantExprUDiv(Constant* C1, Constant* C2);
172   Constant* getConstantExprSDiv(Constant* C1, Constant* C2);
173   Constant* getConstantExprFDiv(Constant* C1, Constant* C2);
174   Constant* getConstantExprURem(Constant* C1, Constant* C2);
175   Constant* getConstantExprSRem(Constant* C1, Constant* C2);
176   Constant* getConstantExprFRem(Constant* C1, Constant* C2);
177   Constant* getConstantExprAnd(Constant* C1, Constant* C2);
178   Constant* getConstantExprOr(Constant* C1, Constant* C2);
179   Constant* getConstantExprXor(Constant* C1, Constant* C2);
180   Constant* getConstantExprICmp(unsigned short pred, Constant* LHS,
181                                 Constant* RHS);
182   Constant* getConstantExprFCmp(unsigned short pred, Constant* LHS,
183                                 Constant* RHS);
184   Constant* getConstantExprShl(Constant* C1, Constant* C2);
185   Constant* getConstantExprLShr(Constant* C1, Constant* C2);
186   Constant* getConstantExprAShr(Constant* C1, Constant* C2);
187   Constant* getConstantExprGetElementPtr(Constant* C, Constant* const* IdxList, 
188                                          unsigned NumIdx);
189   Constant* getConstantExprGetElementPtr(Constant* C, Value* const* IdxList, 
190                                           unsigned NumIdx);
191   Constant* getConstantExprExtractElement(Constant* Vec, Constant* Idx);
192   Constant* getConstantExprInsertElement(Constant* Vec, Constant* Elt,
193                                          Constant* Idx);
194   Constant* getConstantExprShuffleVector(Constant* V1, Constant* V2,
195                                          Constant* Mask);
196   Constant* getConstantExprExtractValue(Constant* Agg, const unsigned* IdxList, 
197                                         unsigned NumIdx);
198   Constant* getConstantExprInsertValue(Constant* Agg, Constant* Val,
199                                        const unsigned* IdxList,
200                                        unsigned NumIdx);
201
202   /// getSizeOf constant expr - computes the size of a type in a target
203   /// independent way (Note: the return type is an i64).
204   ///
205   Constant* getConstantExprSizeOf(const Type* Ty);
206   
207   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
208   /// method returns the negative zero constant for floating point or vector
209   /// floating point types; for all other types, it returns the null value.
210   Constant* getZeroValueForNegation(const Type* Ty);
211   
212   // ConstantFP accessors
213   ConstantFP* getConstantFP(const APFloat& V);
214   
215   /// get() - This returns a ConstantFP, or a vector containing a splat of a
216   /// ConstantFP, for the specified value in the specified type.  This should
217   /// only be used for simple constant values like 2.0/1.0 etc, that are
218   /// known-valid both as host double and as the target format.
219   Constant* getConstantFP(const Type* Ty, double V);
220   ConstantFP* getConstantFPNegativeZero(const Type* Ty);
221   
222   // ConstantVector accessors
223   Constant* getConstantVector(const VectorType* T,
224                               const std::vector<Constant*>& V);
225   Constant* getConstantVector(const std::vector<Constant*>& V);
226   Constant* getConstantVector(Constant* const* Vals, unsigned NumVals);
227   
228   // MDNode accessors
229   MDNode* getMDNode(Value* const* Vals, unsigned NumVals);
230   
231   // MDString accessors
232   MDString* getMDString(const char *StrBegin, const char *StrEnd);
233   MDString* getMDString(const std::string &Str);
234   
235   // FunctionType accessors
236   FunctionType* getFunctionType(const Type* Result, bool isVarArg);
237   FunctionType* getFunctionType(const Type* Result,
238                                 const std::vector<const Type*>& Params,
239                                 bool isVarArg);
240                                 
241   // IntegerType accessors
242   const IntegerType* getIntegerType(unsigned NumBits);
243   
244   // OpaqueType accessors
245   OpaqueType* getOpaqueType();
246   
247   // StructType accessors
248   StructType* getStructType(bool isPacked=false);
249   StructType* getStructType(const std::vector<const Type*>& Params,
250                             bool isPacked = false);
251   StructType* getStructType(const Type* type, ...);
252   
253   // ArrayType accessors
254   ArrayType* getArrayType(const Type* ElementType, uint64_t NumElements);
255   
256   // PointerType accessors
257   PointerType* getPointerType(const Type* ElementType, unsigned AddressSpace);
258   PointerType* getPointerTypeUnqual(const Type* ElementType);
259   
260   // VectorType accessors
261   VectorType* getVectorType(const Type* ElementType, unsigned NumElements);
262   VectorType* getVectorTypeInteger(const VectorType* VTy);
263   VectorType* getVectorTypeExtendedElement(const VectorType* VTy);
264   VectorType* getVectorTypeTruncatedElement(const VectorType* VTy);
265   
266   // Other helpers
267   /// @brief Create a result type for fcmp/icmp
268   const Type* makeCmpResultType(const Type* opnd_type);
269 };
270
271 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.
272 extern LLVMContext& getGlobalContext();
273
274 }
275
276 #endif