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