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