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