1 //===-- llvm/LLVMContext.h - Class for managing "global" state --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares LLVMContext, a container of "global" state in LLVM, such
11 // as the global type and constant uniquing tables.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LLVMCONTEXT_H
16 #define LLVM_LLVMCONTEXT_H
18 #include "llvm/Support/DataTypes.h"
28 class ConstantAggregateZero;
32 class ConstantPointerNull;
37 class LLVMContextImpl;
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.
56 LLVMContextImpl* pImpl;
58 friend class ConstantInt;
59 friend class ConstantFP;
65 Constant* getNullValue(const Type* Ty);
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);
72 // UndefValue accessors
73 UndefValue* getUndef(const Type* Ty);
75 // ConstantInt accessors
76 ConstantInt* getTrue();
77 ConstantInt* getFalse();
79 // ConstantPointerNull accessors
80 ConstantPointerNull* getConstantPointerNull(const PointerType* T);
82 // ConstantStruct accessors
83 Constant* getConstantStruct(const StructType* T,
84 const std::vector<Constant*>& V);
85 Constant* getConstantStruct(const std::vector<Constant*>& V,
87 Constant* getConstantStruct(Constant* const *Vals, unsigned NumVals,
90 // ConstantAggregateZero accessors
91 ConstantAggregateZero* getConstantAggregateZero(const Type* Ty);
93 // ConstantArray accessors
94 Constant* getConstantArray(const ArrayType* T,
95 const std::vector<Constant*>& V);
96 Constant* getConstantArray(const ArrayType* T, Constant* const* Vals,
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);
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,
129 Constant* getConstantExprFPCast(Constant* C, const Type* Ty);
130 Constant* getConstantExprSelect(Constant* C, Constant* V1, Constant* V2);
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).
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,
159 Constant* getConstantExprFCmp(unsigned short pred, Constant* LHS,
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,
166 Constant* getConstantExprGetElementPtr(Constant* C, Value* const* IdxList,
168 Constant* getConstantExprExtractElement(Constant* Vec, Constant* Idx);
169 Constant* getConstantExprInsertElement(Constant* Vec, Constant* Elt,
171 Constant* getConstantExprShuffleVector(Constant* V1, Constant* V2,
173 Constant* getConstantExprExtractValue(Constant* Agg, const unsigned* IdxList,
175 Constant* getConstantExprInsertValue(Constant* Agg, Constant* Val,
176 const unsigned* IdxList,
179 /// getSizeOf constant expr - computes the size of a type in a target
180 /// independent way (Note: the return type is an i64).
182 Constant* getConstantExprSizeOf(const Type* Ty);
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);
191 MDNode* getMDNode(Value* const* Vals, unsigned NumVals);
193 // MDString accessors
194 MDString* getMDString(const StringRef &Str);
196 // FunctionType accessors
197 FunctionType* getFunctionType(const Type* Result, bool isVarArg);
198 FunctionType* getFunctionType(const Type* Result,
199 const std::vector<const Type*>& Params,
202 // IntegerType accessors
203 const IntegerType* getIntegerType(unsigned NumBits);
205 // OpaqueType accessors
206 OpaqueType* getOpaqueType();
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, ...);
214 // ArrayType accessors
215 ArrayType* getArrayType(const Type* ElementType, uint64_t NumElements);
217 // PointerType accessors
218 PointerType* getPointerType(const Type* ElementType, unsigned AddressSpace);
219 PointerType* getPointerTypeUnqual(const Type* ElementType);
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);
228 /// @brief Create a result type for fcmp/icmp
229 const Type* makeCmpResultType(const Type* opnd_type);
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);
240 Constant *replaceUsesOfWithOnConstant(ConstantArray *CA,
241 Value *From, Value *To, Use *U);
242 Constant *replaceUsesOfWithOnConstant(ConstantStruct *CS, Value *From,
246 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.
247 extern LLVMContext& getGlobalContext();