This started as a small change, I swear. Unfortunately, lots of things call the...
[oota-llvm.git] / lib / VMCore / LLVMContext.cpp
1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
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 implements LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/LLVMContext.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/MDNode.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "LLVMContextImpl.h"
21
22 using namespace llvm;
23
24 static ManagedStatic<LLVMContext> GlobalContext;
25
26 LLVMContext& llvm::getGlobalContext() {
27   return *GlobalContext;
28 }
29
30 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
31 LLVMContext::~LLVMContext() { delete pImpl; }
32
33 // Constant accessors
34 Constant* LLVMContext::getNullValue(const Type* Ty) {
35   return Constant::getNullValue(Ty);
36 }
37
38 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
39   return Constant::getAllOnesValue(Ty);
40 }
41
42 // UndefValue accessors.
43 UndefValue* LLVMContext::getUndef(const Type* Ty) {
44   return UndefValue::get(Ty);
45 }
46
47 // ConstantInt accessors.
48 ConstantInt* LLVMContext::getConstantIntTrue() {
49   return ConstantInt::getTrue();
50 }
51
52 ConstantInt* LLVMContext::getConstantIntFalse() {
53   return ConstantInt::getFalse();
54 }
55
56 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
57                                          bool isSigned) {
58   return ConstantInt::get(Ty, V, isSigned);
59 }
60
61
62 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
63                                          bool isSigned) {
64   return ConstantInt::get(Ty, V, isSigned);
65 }
66
67 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
68                                                int64_t V) {
69   return ConstantInt::getSigned(Ty, V);
70 }
71
72 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
73   return ConstantInt::get(V);
74 }
75
76 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
77   return ConstantInt::get(Ty, V);
78 }
79
80 ConstantInt* LLVMContext::getConstantIntAllOnesValue(const Type* Ty) {
81   return ConstantInt::getAllOnesValue(Ty);
82 }
83
84
85 // ConstantPointerNull accessors.
86 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
87   return ConstantPointerNull::get(T);
88 }
89
90
91 // ConstantStruct accessors.
92 Constant* LLVMContext::getConstantStruct(const StructType* T,
93                                          const std::vector<Constant*>& V) {
94   return ConstantStruct::get(T, V);
95 }
96
97 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
98                                          bool Packed) {
99   return ConstantStruct::get(V, Packed);
100 }
101
102 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
103                                          unsigned NumVals, bool Packed) {
104   return ConstantStruct::get(Vals, NumVals, Packed);
105 }
106
107
108 // ConstantAggregateZero accessors.
109 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
110   return ConstantAggregateZero::get(Ty);
111 }
112
113
114 // ConstantArray accessors.
115 Constant* LLVMContext::getConstantArray(const ArrayType* T,
116                                         const std::vector<Constant*>& V) {
117   return ConstantArray::get(T, V);
118 }
119
120 Constant* LLVMContext::getConstantArray(const ArrayType* T,
121                                         Constant* const* Vals,
122                                         unsigned NumVals) {
123   return ConstantArray::get(T, Vals, NumVals);
124 }
125
126 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
127                                         bool AddNull) {
128   return ConstantArray::get(Initializer, AddNull);
129 }
130
131
132 // ConstantExpr accessors.
133 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
134                                        Constant* C2) {
135   return ConstantExpr::get(Opcode, C1, C2);
136 }
137
138 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
139   return ConstantExpr::getTrunc(C, Ty);
140 }
141
142 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
143   return ConstantExpr::getSExt(C, Ty);
144 }
145
146 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
147   return ConstantExpr::getZExt(C, Ty);  
148 }
149
150 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
151   return ConstantExpr::getFPTrunc(C, Ty);
152 }
153
154 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
155   return ConstantExpr::getFPExtend(C, Ty);
156 }
157
158 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
159   return ConstantExpr::getUIToFP(C, Ty);
160 }
161
162 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
163   return ConstantExpr::getSIToFP(C, Ty);
164 }
165
166 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
167   return ConstantExpr::getFPToUI(C, Ty);
168 }
169
170 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
171   return ConstantExpr::getFPToSI(C, Ty);
172 }
173
174 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
175   return ConstantExpr::getPtrToInt(C, Ty);
176 }
177
178 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
179   return ConstantExpr::getIntToPtr(C, Ty);
180 }
181
182 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
183   return ConstantExpr::getBitCast(C, Ty);
184 }
185
186 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
187                                            const Type* Ty) {
188   return ConstantExpr::getCast(ops, C, Ty);
189 }
190
191 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
192                                                     const Type* Ty) {
193   return ConstantExpr::getZExtOrBitCast(C, Ty);
194 }
195
196 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
197                                                     const Type* Ty) {
198   return ConstantExpr::getSExtOrBitCast(C, Ty);
199 }
200
201 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
202                                                      const Type* Ty) {
203   return ConstantExpr::getTruncOrBitCast(C, Ty);  
204 }
205
206 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
207   return ConstantExpr::getPointerCast(C, Ty);
208 }
209
210 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
211                                                   bool isSigned) {
212   return ConstantExpr::getIntegerCast(C, Ty, isSigned);
213 }
214
215 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
216   return ConstantExpr::getFPCast(C, Ty);
217 }
218
219 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
220                                              Constant* V2) {
221   return ConstantExpr::getSelect(C, V1, V2);
222 }
223
224 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
225   return ConstantExpr::getAlignOf(Ty);
226 }
227
228 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
229                                  Constant* C1, Constant* C2) {
230   return ConstantExpr::getCompare(pred, C1, C2);
231 }
232
233 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
234   return ConstantExpr::getNeg(C);
235 }
236
237 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
238   return ConstantExpr::getFNeg(C);
239 }
240
241 Constant* LLVMContext::getConstantExprNot(Constant* C) {
242   return ConstantExpr::getNot(C);
243 }
244
245 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
246   return ConstantExpr::getAdd(C1, C2);
247 }
248
249 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
250   return ConstantExpr::getFAdd(C1, C2);
251 }
252
253 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
254   return ConstantExpr::getSub(C1, C2);
255 }
256
257 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
258   return ConstantExpr::getFSub(C1, C2);
259 }
260
261 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
262   return ConstantExpr::getMul(C1, C2);
263 }
264
265 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
266   return ConstantExpr::getFMul(C1, C2);
267 }
268
269 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
270   return ConstantExpr::getUDiv(C1, C2);
271 }
272
273 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
274   return ConstantExpr::getSDiv(C1, C2);
275 }
276
277 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
278   return ConstantExpr::getFDiv(C1, C2);
279 }
280
281 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
282   return ConstantExpr::getURem(C1, C2);
283 }
284
285 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
286   return ConstantExpr::getSRem(C1, C2);
287 }
288
289 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
290   return ConstantExpr::getFRem(C1, C2);
291 }
292
293 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
294   return ConstantExpr::getAnd(C1, C2);
295 }
296
297 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
298   return ConstantExpr::getOr(C1, C2);
299 }
300
301 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
302   return ConstantExpr::getXor(C1, C2);
303 }
304
305 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
306                               Constant* RHS) {
307   return ConstantExpr::getICmp(pred, LHS, RHS);
308 }
309
310 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
311                               Constant* RHS) {
312   return ConstantExpr::getFCmp(pred, LHS, RHS);
313 }
314
315 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
316   return ConstantExpr::getShl(C1, C2);
317 }
318
319 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
320   return ConstantExpr::getLShr(C1, C2);
321 }
322
323 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
324   return ConstantExpr::getAShr(C1, C2);
325 }
326
327 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
328                                                     Constant* const* IdxList, 
329                                                     unsigned NumIdx) {
330   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
331 }
332
333 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
334                                                     Value* const* IdxList, 
335                                                     unsigned NumIdx) {
336   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
337 }
338
339 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
340                                                      Constant* Idx) {
341   return ConstantExpr::getExtractElement(Vec, Idx);
342 }
343
344 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
345                                                     Constant* Elt,
346                                                     Constant* Idx) {
347   return ConstantExpr::getInsertElement(Vec, Elt, Idx);
348 }
349
350 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
351                                                     Constant* Mask) {
352   return ConstantExpr::getShuffleVector(V1, V2, Mask);
353 }
354
355 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
356                                                    const unsigned* IdxList, 
357                                                    unsigned NumIdx) {
358   return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
359 }
360
361 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
362                                                   const unsigned* IdxList,
363                                                   unsigned NumIdx) {
364   return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
365 }
366
367 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
368   return ConstantExpr::getSizeOf(Ty);
369 }
370
371 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
372   return ConstantExpr::getZeroValueForNegationExpr(Ty);
373 }
374
375
376 // ConstantFP accessors.
377 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
378   return ConstantFP::get(V);
379 }
380
381 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
382   return ConstantFP::get(Ty, V);
383 }
384
385 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
386   return ConstantFP::getNegativeZero(Ty);
387 }
388
389
390 // ConstantVector accessors.
391 Constant* LLVMContext::getConstantVector(const VectorType* T,
392                             const std::vector<Constant*>& V) {
393   return ConstantVector::get(T, V);
394 }
395
396 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
397   return ConstantVector::get(V);
398 }
399
400 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
401                                          unsigned NumVals) {
402   return ConstantVector::get(Vals, NumVals);
403 }
404
405 ConstantVector* LLVMContext::getConstantVectorAllOnesValue(
406                                                          const VectorType* Ty) {
407   return ConstantVector::getAllOnesValue(Ty);
408 }
409
410 // MDNode accessors
411 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
412   return MDNode::get(Vals, NumVals);
413 }
414
415 // MDString accessors
416 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
417   return MDString::get(StrBegin, StrEnd);
418 }
419
420 MDString* LLVMContext::getMDString(const std::string &Str) {
421   return MDString::get(Str);
422 }
423
424 // FunctionType accessors
425 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
426   return FunctionType::get(Result, isVarArg);
427 }
428
429 FunctionType* LLVMContext::getFunctionType(const Type* Result,
430                                          const std::vector<const Type*>& Params,
431                                          bool isVarArg) {
432   return FunctionType::get(Result, Params, isVarArg);
433 }
434                                 
435 // IntegerType accessors
436 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
437   return IntegerType::get(NumBits);
438 }
439   
440 // OpaqueType accessors
441 OpaqueType* LLVMContext::getOpaqueType() {
442   return OpaqueType::get();
443 }
444
445 // StructType accessors
446 StructType* LLVMContext::getStructType(bool isPacked) {
447   return StructType::get(isPacked);
448 }
449
450 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
451                                        bool isPacked) {
452   return StructType::get(Params, isPacked);
453 }
454
455 // ArrayType accessors
456 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
457                                      uint64_t NumElements) {
458   return ArrayType::get(ElementType, NumElements);
459 }
460   
461 // PointerType accessors
462 PointerType* LLVMContext::getPointerType(const Type* ElementType,
463                                          unsigned AddressSpace) {
464   return PointerType::get(ElementType, AddressSpace);
465 }
466
467 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
468   return PointerType::getUnqual(ElementType);
469 }
470   
471 // VectorType accessors
472 VectorType* LLVMContext::getVectorType(const Type* ElementType,
473                                        unsigned NumElements) {
474   return VectorType::get(ElementType, NumElements);
475 }
476
477 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
478   return VectorType::getInteger(VTy);  
479 }
480
481 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
482   return VectorType::getExtendedElementVectorType(VTy);
483 }
484
485 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
486   return VectorType::getTruncatedElementVectorType(VTy);
487 }
488
489 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
490   if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
491     return getVectorType(Type::Int1Ty, vt->getNumElements());
492   }
493   return Type::Int1Ty;
494 }