e71183855a65aa3830b22b9776deb6252cc75ab9
[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   Constant* getConstantInt(const Type* Ty, uint64_t V,
74                               bool isSigned = false);
75   ConstantInt* getConstantInt(const IntegerType* Ty, uint64_t V,
76                               bool isSigned = false);
77   ConstantInt* getConstantIntSigned(const IntegerType* Ty, int64_t V);
78   ConstantInt* getConstantInt(const APInt& V);
79   Constant* getConstantInt(const Type* Ty, const APInt& V);
80   
81   // ConstantPointerNull accessors
82   ConstantPointerNull* getConstantPointerNull(const PointerType* T);
83   
84   // ConstantStruct accessors
85   Constant* getConstantStruct(const StructType* T,
86                               const std::vector<Constant*>& V);
87   Constant* getConstantStruct(const std::vector<Constant*>& V,
88                               bool Packed = false);
89   Constant* getConstantStruct(Constant* const *Vals, unsigned NumVals,
90                               bool Packed = false);
91                               
92   // ConstantAggregateZero accessors
93   ConstantAggregateZero* getConstantAggregateZero(const Type* Ty);
94   
95   // ConstantArray accessors
96   Constant* getConstantArray(const ArrayType* T,
97                              const std::vector<Constant*>& V);
98   Constant* getConstantArray(const ArrayType* T, Constant* const* Vals,
99                              unsigned NumVals);
100   Constant* getConstantArray(const std::string& Initializer,
101                              bool AddNull = true);
102                              
103   // ConstantExpr accessors
104   Constant* getConstantExpr(unsigned Opcode, Constant* C1, Constant* C2);
105   Constant* getConstantExprTrunc(Constant* C, const Type* Ty);
106   Constant* getConstantExprSExt(Constant* C, const Type* Ty);
107   Constant* getConstantExprZExt(Constant* C, const Type* Ty);
108   Constant* getConstantExprFPTrunc(Constant* C, const Type* Ty);
109   Constant* getConstantExprFPExtend(Constant* C, const Type* Ty);
110   Constant* getConstantExprUIToFP(Constant* C, const Type* Ty);
111   Constant* getConstantExprSIToFP(Constant* C, const Type* Ty);
112   Constant* getConstantExprFPToUI(Constant* C, const Type* Ty);
113   Constant* getConstantExprFPToSI(Constant* C, const Type* Ty);
114   Constant* getConstantExprPtrToInt(Constant* C, const Type* Ty);
115   Constant* getConstantExprIntToPtr(Constant* C, const Type* Ty);
116   Constant* getConstantExprBitCast(Constant* C, const Type* Ty);
117   Constant* getConstantExprCast(unsigned ops, Constant* C, const Type* Ty);
118   Constant* getConstantExprZExtOrBitCast(Constant* C, const Type* Ty);
119   Constant* getConstantExprSExtOrBitCast(Constant* C, const Type* Ty);
120   Constant* getConstantExprTruncOrBitCast(Constant* C, const Type* Ty);
121   Constant* getConstantExprPointerCast(Constant* C, const Type* Ty);
122   Constant* getConstantExprIntegerCast(Constant* C, const Type* Ty,
123                                        bool isSigned);
124   Constant* getConstantExprFPCast(Constant* C, const Type* Ty);
125   Constant* getConstantExprSelect(Constant* C, Constant* V1, Constant* V2);
126   
127   /// getAlignOf constant expr - computes the alignment of a type in a target
128   /// independent way (Note: the return type is an i32; Note: assumes that i8
129   /// is byte aligned).
130   ///
131   Constant* getConstantExprAlignOf(const Type* Ty);
132   Constant* getConstantExprCompare(unsigned short pred,
133                                    Constant* C1, Constant* C2);
134   Constant* getConstantExprNeg(Constant* C);
135   Constant* getConstantExprFNeg(Constant* C);
136   Constant* getConstantExprNot(Constant* C);
137   Constant* getConstantExprAdd(Constant* C1, Constant* C2);
138   Constant* getConstantExprFAdd(Constant* C1, Constant* C2);
139   Constant* getConstantExprSub(Constant* C1, Constant* C2);
140   Constant* getConstantExprFSub(Constant* C1, Constant* C2);
141   Constant* getConstantExprMul(Constant* C1, Constant* C2);
142   Constant* getConstantExprFMul(Constant* C1, Constant* C2);
143   Constant* getConstantExprUDiv(Constant* C1, Constant* C2);
144   Constant* getConstantExprSDiv(Constant* C1, Constant* C2);
145   Constant* getConstantExprFDiv(Constant* C1, Constant* C2);
146   Constant* getConstantExprURem(Constant* C1, Constant* C2);
147   Constant* getConstantExprSRem(Constant* C1, Constant* C2);
148   Constant* getConstantExprFRem(Constant* C1, Constant* C2);
149   Constant* getConstantExprAnd(Constant* C1, Constant* C2);
150   Constant* getConstantExprOr(Constant* C1, Constant* C2);
151   Constant* getConstantExprXor(Constant* C1, Constant* C2);
152   Constant* getConstantExprICmp(unsigned short pred, Constant* LHS,
153                                 Constant* RHS);
154   Constant* getConstantExprFCmp(unsigned short pred, Constant* LHS,
155                                 Constant* RHS);
156   Constant* getConstantExprShl(Constant* C1, Constant* C2);
157   Constant* getConstantExprLShr(Constant* C1, Constant* C2);
158   Constant* getConstantExprAShr(Constant* C1, Constant* C2);
159   Constant* getConstantExprGetElementPtr(Constant* C, Constant* const* IdxList, 
160                                          unsigned NumIdx);
161   Constant* getConstantExprGetElementPtr(Constant* C, Value* const* IdxList, 
162                                           unsigned NumIdx);
163   Constant* getConstantExprExtractElement(Constant* Vec, Constant* Idx);
164   Constant* getConstantExprInsertElement(Constant* Vec, Constant* Elt,
165                                          Constant* Idx);
166   Constant* getConstantExprShuffleVector(Constant* V1, Constant* V2,
167                                          Constant* Mask);
168   Constant* getConstantExprExtractValue(Constant* Agg, const unsigned* IdxList, 
169                                         unsigned NumIdx);
170   Constant* getConstantExprInsertValue(Constant* Agg, Constant* Val,
171                                        const unsigned* IdxList,
172                                        unsigned NumIdx);
173
174   /// getSizeOf constant expr - computes the size of a type in a target
175   /// independent way (Note: the return type is an i64).
176   ///
177   Constant* getConstantExprSizeOf(const Type* Ty);
178   
179   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
180   /// method returns the negative zero constant for floating point or vector
181   /// floating point types; for all other types, it returns the null value.
182   Constant* getZeroValueForNegation(const Type* Ty);
183   
184   // ConstantFP accessors
185   ConstantFP* getConstantFP(const APFloat& V);
186   
187   /// get() - This returns a ConstantFP, or a vector containing a splat of a
188   /// ConstantFP, for the specified value in the specified type.  This should
189   /// only be used for simple constant values like 2.0/1.0 etc, that are
190   /// known-valid both as host double and as the target format.
191   Constant* getConstantFP(const Type* Ty, double V);
192   ConstantFP* getConstantFPNegativeZero(const Type* Ty);
193   
194   // ConstantVector accessors
195   Constant* getConstantVector(const VectorType* T,
196                               const std::vector<Constant*>& V);
197   Constant* getConstantVector(const std::vector<Constant*>& V);
198   Constant* getConstantVector(Constant* const* Vals, unsigned NumVals);
199   
200   // MDNode accessors
201   MDNode* getMDNode(Value* const* Vals, unsigned NumVals);
202   
203   // MDString accessors
204   MDString* getMDString(const char *StrBegin, const char *StrEnd);
205   MDString* getMDString(const std::string &Str);
206   
207   // FunctionType accessors
208   FunctionType* getFunctionType(const Type* Result, bool isVarArg);
209   FunctionType* getFunctionType(const Type* Result,
210                                 const std::vector<const Type*>& Params,
211                                 bool isVarArg);
212                                 
213   // IntegerType accessors
214   const IntegerType* getIntegerType(unsigned NumBits);
215   
216   // OpaqueType accessors
217   OpaqueType* getOpaqueType();
218   
219   // StructType accessors
220   StructType* getStructType(bool isPacked=false);
221   StructType* getStructType(const std::vector<const Type*>& Params,
222                             bool isPacked = false);
223   StructType* getStructType(const Type* type, ...);
224   
225   // ArrayType accessors
226   ArrayType* getArrayType(const Type* ElementType, uint64_t NumElements);
227   
228   // PointerType accessors
229   PointerType* getPointerType(const Type* ElementType, unsigned AddressSpace);
230   PointerType* getPointerTypeUnqual(const Type* ElementType);
231   
232   // VectorType accessors
233   VectorType* getVectorType(const Type* ElementType, unsigned NumElements);
234   VectorType* getVectorTypeInteger(const VectorType* VTy);
235   VectorType* getVectorTypeExtendedElement(const VectorType* VTy);
236   VectorType* getVectorTypeTruncatedElement(const VectorType* VTy);
237   
238   // Other helpers
239   /// @brief Create a result type for fcmp/icmp
240   const Type* makeCmpResultType(const Type* opnd_type);
241 };
242
243 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.
244 extern LLVMContext& getGlobalContext();
245
246 }
247
248 #endif