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;
60 friend class ConstantStruct;
61 friend class ConstantArray;
62 friend class ConstantVector;
68 Constant* getNullValue(const Type* Ty);
70 /// @returns the value for an integer constant of the given type that has all
71 /// its bits set to true.
72 /// @brief Get the all ones value
73 Constant* getAllOnesValue(const Type* Ty);
75 // UndefValue accessors
76 UndefValue* getUndef(const Type* Ty);
78 // ConstantInt accessors
79 ConstantInt* getTrue();
80 ConstantInt* getFalse();
82 // ConstantPointerNull accessors
83 ConstantPointerNull* getConstantPointerNull(const PointerType* T);
85 // ConstantAggregateZero accessors
86 ConstantAggregateZero* getConstantAggregateZero(const Type* Ty);
88 // ConstantExpr accessors
89 Constant* getConstantExpr(unsigned Opcode, Constant* C1, Constant* C2);
90 Constant* getConstantExprTrunc(Constant* C, const Type* Ty);
91 Constant* getConstantExprSExt(Constant* C, const Type* Ty);
92 Constant* getConstantExprZExt(Constant* C, const Type* Ty);
93 Constant* getConstantExprFPTrunc(Constant* C, const Type* Ty);
94 Constant* getConstantExprFPExtend(Constant* C, const Type* Ty);
95 Constant* getConstantExprUIToFP(Constant* C, const Type* Ty);
96 Constant* getConstantExprSIToFP(Constant* C, const Type* Ty);
97 Constant* getConstantExprFPToUI(Constant* C, const Type* Ty);
98 Constant* getConstantExprFPToSI(Constant* C, const Type* Ty);
99 Constant* getConstantExprPtrToInt(Constant* C, const Type* Ty);
100 Constant* getConstantExprIntToPtr(Constant* C, const Type* Ty);
101 Constant* getConstantExprBitCast(Constant* C, const Type* Ty);
102 Constant* getConstantExprCast(unsigned ops, Constant* C, const Type* Ty);
103 Constant* getConstantExprZExtOrBitCast(Constant* C, const Type* Ty);
104 Constant* getConstantExprSExtOrBitCast(Constant* C, const Type* Ty);
105 Constant* getConstantExprTruncOrBitCast(Constant* C, const Type* Ty);
106 Constant* getConstantExprPointerCast(Constant* C, const Type* Ty);
107 Constant* getConstantExprIntegerCast(Constant* C, const Type* Ty,
109 Constant* getConstantExprFPCast(Constant* C, const Type* Ty);
110 Constant* getConstantExprSelect(Constant* C, Constant* V1, Constant* V2);
112 /// getAlignOf constant expr - computes the alignment of a type in a target
113 /// independent way (Note: the return type is an i32; Note: assumes that i8
114 /// is byte aligned).
116 Constant* getConstantExprAlignOf(const Type* Ty);
117 Constant* getConstantExprCompare(unsigned short pred,
118 Constant* C1, Constant* C2);
119 Constant* getConstantExprNeg(Constant* C);
120 Constant* getConstantExprFNeg(Constant* C);
121 Constant* getConstantExprNot(Constant* C);
122 Constant* getConstantExprAdd(Constant* C1, Constant* C2);
123 Constant* getConstantExprFAdd(Constant* C1, Constant* C2);
124 Constant* getConstantExprSub(Constant* C1, Constant* C2);
125 Constant* getConstantExprFSub(Constant* C1, Constant* C2);
126 Constant* getConstantExprMul(Constant* C1, Constant* C2);
127 Constant* getConstantExprFMul(Constant* C1, Constant* C2);
128 Constant* getConstantExprUDiv(Constant* C1, Constant* C2);
129 Constant* getConstantExprSDiv(Constant* C1, Constant* C2);
130 Constant* getConstantExprFDiv(Constant* C1, Constant* C2);
131 Constant* getConstantExprURem(Constant* C1, Constant* C2);
132 Constant* getConstantExprSRem(Constant* C1, Constant* C2);
133 Constant* getConstantExprFRem(Constant* C1, Constant* C2);
134 Constant* getConstantExprAnd(Constant* C1, Constant* C2);
135 Constant* getConstantExprOr(Constant* C1, Constant* C2);
136 Constant* getConstantExprXor(Constant* C1, Constant* C2);
137 Constant* getConstantExprICmp(unsigned short pred, Constant* LHS,
139 Constant* getConstantExprFCmp(unsigned short pred, Constant* LHS,
141 Constant* getConstantExprShl(Constant* C1, Constant* C2);
142 Constant* getConstantExprLShr(Constant* C1, Constant* C2);
143 Constant* getConstantExprAShr(Constant* C1, Constant* C2);
144 Constant* getConstantExprGetElementPtr(Constant* C, Constant* const* IdxList,
146 Constant* getConstantExprGetElementPtr(Constant* C, Value* const* IdxList,
148 Constant* getConstantExprExtractElement(Constant* Vec, Constant* Idx);
149 Constant* getConstantExprInsertElement(Constant* Vec, Constant* Elt,
151 Constant* getConstantExprShuffleVector(Constant* V1, Constant* V2,
153 Constant* getConstantExprExtractValue(Constant* Agg, const unsigned* IdxList,
155 Constant* getConstantExprInsertValue(Constant* Agg, Constant* Val,
156 const unsigned* IdxList,
159 /// getSizeOf constant expr - computes the size of a type in a target
160 /// independent way (Note: the return type is an i64).
162 Constant* getConstantExprSizeOf(const Type* Ty);
165 MDNode* getMDNode(Value* const* Vals, unsigned NumVals);
167 // MDString accessors
168 MDString* getMDString(const StringRef &Str);
170 // FunctionType accessors
171 FunctionType* getFunctionType(const Type* Result, bool isVarArg);
172 FunctionType* getFunctionType(const Type* Result,
173 const std::vector<const Type*>& Params,
176 // IntegerType accessors
177 const IntegerType* getIntegerType(unsigned NumBits);
179 // OpaqueType accessors
180 OpaqueType* getOpaqueType();
182 // StructType accessors
183 StructType* getStructType(bool isPacked=false);
184 StructType* getStructType(const std::vector<const Type*>& Params,
185 bool isPacked = false);
186 StructType* getStructType(const Type* type, ...);
188 // ArrayType accessors
189 ArrayType* getArrayType(const Type* ElementType, uint64_t NumElements);
191 // PointerType accessors
192 PointerType* getPointerType(const Type* ElementType, unsigned AddressSpace);
193 PointerType* getPointerTypeUnqual(const Type* ElementType);
195 // VectorType accessors
196 VectorType* getVectorType(const Type* ElementType, unsigned NumElements);
197 VectorType* getVectorTypeInteger(const VectorType* VTy);
198 VectorType* getVectorTypeExtendedElement(const VectorType* VTy);
199 VectorType* getVectorTypeTruncatedElement(const VectorType* VTy);
202 /// @brief Create a result type for fcmp/icmp
203 const Type* makeCmpResultType(const Type* opnd_type);
205 // Methods for erasing constants
206 void erase(MDString *M);
207 void erase(MDNode *M);
208 void erase(ConstantAggregateZero *Z);
211 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.
212 extern LLVMContext& getGlobalContext();