37f0cd1a44c75477dd99d50c1a6d18bc0bd6e560
[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 APFloat;
25 class APInt;
26 class ArrayType;
27 class Constant;
28 class ConstantAggregateZero;
29 class ConstantArray;
30 class ConstantFP;
31 class ConstantInt;
32 class ConstantPointerNull;
33 class ConstantStruct;
34 class ConstantVector;
35 class FunctionType;
36 class IntegerType;
37 class LLVMContextImpl;
38 class MDNode;
39 class MDString;
40 class OpaqueType;
41 class PointerType;
42 class StringRef;
43 class StructType;
44 class Type;
45 class UndefValue;
46 class Use;
47 class Value;
48 class VectorType;
49
50 /// This is an important class for using LLVM in a threaded context.  It
51 /// (opaquely) owns and manages the core "global" data of LLVM's core 
52 /// infrastructure, including the type and constant uniquing tables.
53 /// LLVMContext itself provides no locking guarantees, so you should be careful
54 /// to have one context per thread.
55 class LLVMContext {
56   LLVMContextImpl* pImpl;
57   
58   friend class ConstantInt;
59   friend class ConstantFP;
60 public:
61   LLVMContext();
62   ~LLVMContext();
63   
64   // Constant accessors
65   Constant* getNullValue(const Type* Ty);
66   
67   /// @returns the value for an integer constant of the given type that has all
68   /// its bits set to true.
69   /// @brief Get the all ones value
70   Constant* getAllOnesValue(const Type* Ty);
71   
72   // UndefValue accessors
73   UndefValue* getUndef(const Type* Ty);
74   
75   // ConstantInt accessors
76   ConstantInt* getTrue();
77   ConstantInt* getFalse();
78   
79   // ConstantPointerNull accessors
80   ConstantPointerNull* getConstantPointerNull(const PointerType* T);
81   
82   // ConstantStruct accessors
83   Constant* getConstantStruct(const StructType* T,
84                               const std::vector<Constant*>& V);
85   Constant* getConstantStruct(const std::vector<Constant*>& V,
86                               bool Packed = false);
87   Constant* getConstantStruct(Constant* const *Vals, unsigned NumVals,
88                               bool Packed = false);
89                               
90   // ConstantAggregateZero accessors
91   ConstantAggregateZero* getConstantAggregateZero(const Type* Ty);
92   
93   // ConstantArray accessors
94   Constant* getConstantArray(const ArrayType* T,
95                              const std::vector<Constant*>& V);
96   Constant* getConstantArray(const ArrayType* T, Constant* const* Vals,
97                              unsigned NumVals);
98                              
99   /// This method constructs a ConstantArray and initializes it with a text
100   /// string. The default behavior (AddNull==true) causes a null terminator to
101   /// be placed at the end of the array. This effectively increases the length
102   /// of the array by one (you've been warned).  However, in some situations 
103   /// this is not desired so if AddNull==false then the string is copied without
104   /// null termination.
105   Constant* getConstantArray(const StringRef &Initializer,
106                              bool AddNull = true);
107                              
108   // ConstantExpr accessors
109   Constant* getConstantExpr(unsigned Opcode, Constant* C1, Constant* C2);
110   Constant* getConstantExprTrunc(Constant* C, const Type* Ty);
111   Constant* getConstantExprSExt(Constant* C, const Type* Ty);
112   Constant* getConstantExprZExt(Constant* C, const Type* Ty);
113   Constant* getConstantExprFPTrunc(Constant* C, const Type* Ty);
114   Constant* getConstantExprFPExtend(Constant* C, const Type* Ty);
115   Constant* getConstantExprUIToFP(Constant* C, const Type* Ty);
116   Constant* getConstantExprSIToFP(Constant* C, const Type* Ty);
117   Constant* getConstantExprFPToUI(Constant* C, const Type* Ty);
118   Constant* getConstantExprFPToSI(Constant* C, const Type* Ty);
119   Constant* getConstantExprPtrToInt(Constant* C, const Type* Ty);
120   Constant* getConstantExprIntToPtr(Constant* C, const Type* Ty);
121   Constant* getConstantExprBitCast(Constant* C, const Type* Ty);
122   Constant* getConstantExprCast(unsigned ops, Constant* C, const Type* Ty);
123   Constant* getConstantExprZExtOrBitCast(Constant* C, const Type* Ty);
124   Constant* getConstantExprSExtOrBitCast(Constant* C, const Type* Ty);
125   Constant* getConstantExprTruncOrBitCast(Constant* C, const Type* Ty);
126   Constant* getConstantExprPointerCast(Constant* C, const Type* Ty);
127   Constant* getConstantExprIntegerCast(Constant* C, const Type* Ty,
128                                        bool isSigned);
129   Constant* getConstantExprFPCast(Constant* C, const Type* Ty);
130   Constant* getConstantExprSelect(Constant* C, Constant* V1, Constant* V2);
131   
132   /// getAlignOf constant expr - computes the alignment of a type in a target
133   /// independent way (Note: the return type is an i32; Note: assumes that i8
134   /// is byte aligned).
135   ///
136   Constant* getConstantExprAlignOf(const Type* Ty);
137   Constant* getConstantExprCompare(unsigned short pred,
138                                    Constant* C1, Constant* C2);
139   Constant* getConstantExprNeg(Constant* C);
140   Constant* getConstantExprFNeg(Constant* C);
141   Constant* getConstantExprNot(Constant* C);
142   Constant* getConstantExprAdd(Constant* C1, Constant* C2);
143   Constant* getConstantExprFAdd(Constant* C1, Constant* C2);
144   Constant* getConstantExprSub(Constant* C1, Constant* C2);
145   Constant* getConstantExprFSub(Constant* C1, Constant* C2);
146   Constant* getConstantExprMul(Constant* C1, Constant* C2);
147   Constant* getConstantExprFMul(Constant* C1, Constant* C2);
148   Constant* getConstantExprUDiv(Constant* C1, Constant* C2);
149   Constant* getConstantExprSDiv(Constant* C1, Constant* C2);
150   Constant* getConstantExprFDiv(Constant* C1, Constant* C2);
151   Constant* getConstantExprURem(Constant* C1, Constant* C2);
152   Constant* getConstantExprSRem(Constant* C1, Constant* C2);
153   Constant* getConstantExprFRem(Constant* C1, Constant* C2);
154   Constant* getConstantExprAnd(Constant* C1, Constant* C2);
155   Constant* getConstantExprOr(Constant* C1, Constant* C2);
156   Constant* getConstantExprXor(Constant* C1, Constant* C2);
157   Constant* getConstantExprICmp(unsigned short pred, Constant* LHS,
158                                 Constant* RHS);
159   Constant* getConstantExprFCmp(unsigned short pred, Constant* LHS,
160                                 Constant* RHS);
161   Constant* getConstantExprShl(Constant* C1, Constant* C2);
162   Constant* getConstantExprLShr(Constant* C1, Constant* C2);
163   Constant* getConstantExprAShr(Constant* C1, Constant* C2);
164   Constant* getConstantExprGetElementPtr(Constant* C, Constant* const* IdxList, 
165                                          unsigned NumIdx);
166   Constant* getConstantExprGetElementPtr(Constant* C, Value* const* IdxList, 
167                                           unsigned NumIdx);
168   Constant* getConstantExprExtractElement(Constant* Vec, Constant* Idx);
169   Constant* getConstantExprInsertElement(Constant* Vec, Constant* Elt,
170                                          Constant* Idx);
171   Constant* getConstantExprShuffleVector(Constant* V1, Constant* V2,
172                                          Constant* Mask);
173   Constant* getConstantExprExtractValue(Constant* Agg, const unsigned* IdxList, 
174                                         unsigned NumIdx);
175   Constant* getConstantExprInsertValue(Constant* Agg, Constant* Val,
176                                        const unsigned* IdxList,
177                                        unsigned NumIdx);
178
179   /// getSizeOf constant expr - computes the size of a type in a target
180   /// independent way (Note: the return type is an i64).
181   ///
182   Constant* getConstantExprSizeOf(const Type* Ty);
183   
184   // ConstantVector accessors
185   Constant* getConstantVector(const VectorType* T,
186                               const std::vector<Constant*>& V);
187   Constant* getConstantVector(const std::vector<Constant*>& V);
188   Constant* getConstantVector(Constant* const* Vals, unsigned NumVals);
189   
190   // MDNode accessors
191   MDNode* getMDNode(Value* const* Vals, unsigned NumVals);
192   
193   // MDString accessors
194   MDString* getMDString(const StringRef &Str);
195   
196   // FunctionType accessors
197   FunctionType* getFunctionType(const Type* Result, bool isVarArg);
198   FunctionType* getFunctionType(const Type* Result,
199                                 const std::vector<const Type*>& Params,
200                                 bool isVarArg);
201                                 
202   // IntegerType accessors
203   const IntegerType* getIntegerType(unsigned NumBits);
204   
205   // OpaqueType accessors
206   OpaqueType* getOpaqueType();
207   
208   // StructType accessors
209   StructType* getStructType(bool isPacked=false);
210   StructType* getStructType(const std::vector<const Type*>& Params,
211                             bool isPacked = false);
212   StructType* getStructType(const Type* type, ...);
213   
214   // ArrayType accessors
215   ArrayType* getArrayType(const Type* ElementType, uint64_t NumElements);
216   
217   // PointerType accessors
218   PointerType* getPointerType(const Type* ElementType, unsigned AddressSpace);
219   PointerType* getPointerTypeUnqual(const Type* ElementType);
220   
221   // VectorType accessors
222   VectorType* getVectorType(const Type* ElementType, unsigned NumElements);
223   VectorType* getVectorTypeInteger(const VectorType* VTy);
224   VectorType* getVectorTypeExtendedElement(const VectorType* VTy);
225   VectorType* getVectorTypeTruncatedElement(const VectorType* VTy);
226   
227   // Other helpers
228   /// @brief Create a result type for fcmp/icmp
229   const Type* makeCmpResultType(const Type* opnd_type);
230   
231   // Methods for erasing constants
232   void erase(MDString *M);
233   void erase(MDNode *M);
234   void erase(ConstantAggregateZero *Z);
235   void erase(ConstantArray *Z);
236   void erase(ConstantStruct *S);
237   void erase(ConstantVector *V);
238   
239   // RAUW helpers
240   Constant *replaceUsesOfWithOnConstant(ConstantArray *CA,
241                                              Value *From, Value *To, Use *U);
242   Constant *replaceUsesOfWithOnConstant(ConstantStruct *CS, Value *From,
243                                         Value *To, Use *U);
244 };
245
246 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.
247 extern LLVMContext& getGlobalContext();
248
249 }
250
251 #endif