Begin the painful process of tearing apart the rat'ss nest that is Constants.cpp...
[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/Instruction.h"
19 #include "llvm/MDNode.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "LLVMContextImpl.h"
22
23 using namespace llvm;
24
25 static ManagedStatic<LLVMContext> GlobalContext;
26
27 LLVMContext& llvm::getGlobalContext() {
28   return *GlobalContext;
29 }
30
31 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
32 LLVMContext::~LLVMContext() { delete pImpl; }
33
34 // Constant accessors
35
36 // Constructor to create a '0' constant of arbitrary type...
37 static const uint64_t zero[2] = {0, 0};
38 Constant* LLVMContext::getNullValue(const Type* Ty) {
39   switch (Ty->getTypeID()) {
40   case Type::IntegerTyID:
41     return getConstantInt(Ty, 0);
42   case Type::FloatTyID:
43     return getConstantFP(APFloat(APInt(32, 0)));
44   case Type::DoubleTyID:
45     return getConstantFP(APFloat(APInt(64, 0)));
46   case Type::X86_FP80TyID:
47     return getConstantFP(APFloat(APInt(80, 2, zero)));
48   case Type::FP128TyID:
49     return getConstantFP(APFloat(APInt(128, 2, zero), true));
50   case Type::PPC_FP128TyID:
51     return getConstantFP(APFloat(APInt(128, 2, zero)));
52   case Type::PointerTyID:
53     return getConstantPointerNull(cast<PointerType>(Ty));
54   case Type::StructTyID:
55   case Type::ArrayTyID:
56   case Type::VectorTyID:
57     return getConstantAggregateZero(Ty);
58   default:
59     // Function, Label, or Opaque type?
60     assert(!"Cannot create a null constant of that type!");
61     return 0;
62   }
63 }
64
65 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
66   return Constant::getAllOnesValue(Ty);
67 }
68
69 // UndefValue accessors.
70 UndefValue* LLVMContext::getUndef(const Type* Ty) {
71   return UndefValue::get(Ty);
72 }
73
74 // ConstantInt accessors.
75 ConstantInt* LLVMContext::getConstantIntTrue() {
76   return ConstantInt::getTrue();
77 }
78
79 ConstantInt* LLVMContext::getConstantIntFalse() {
80   return ConstantInt::getFalse();
81 }
82
83 Constant* LLVMContext::getConstantInt(const Type* Ty, uint64_t V,
84                                          bool isSigned) {
85   return ConstantInt::get(Ty, V, isSigned);
86 }
87
88
89 ConstantInt* LLVMContext::getConstantInt(const IntegerType* Ty, uint64_t V,
90                                          bool isSigned) {
91   return ConstantInt::get(Ty, V, isSigned);
92 }
93
94 ConstantInt* LLVMContext::getConstantIntSigned(const IntegerType* Ty,
95                                                int64_t V) {
96   return ConstantInt::getSigned(Ty, V);
97 }
98
99 ConstantInt* LLVMContext::getConstantInt(const APInt& V) {
100   return ConstantInt::get(V);
101 }
102
103 Constant* LLVMContext::getConstantInt(const Type* Ty, const APInt& V) {
104   return ConstantInt::get(Ty, V);
105 }
106
107 ConstantInt* LLVMContext::getConstantIntAllOnesValue(const Type* Ty) {
108   return ConstantInt::getAllOnesValue(Ty);
109 }
110
111
112 // ConstantPointerNull accessors.
113 ConstantPointerNull* LLVMContext::getConstantPointerNull(const PointerType* T) {
114   return ConstantPointerNull::get(T);
115 }
116
117
118 // ConstantStruct accessors.
119 Constant* LLVMContext::getConstantStruct(const StructType* T,
120                                          const std::vector<Constant*>& V) {
121   return ConstantStruct::get(T, V);
122 }
123
124 Constant* LLVMContext::getConstantStruct(const std::vector<Constant*>& V,
125                                          bool Packed) {
126   return ConstantStruct::get(V, Packed);
127 }
128
129 Constant* LLVMContext::getConstantStruct(Constant* const *Vals,
130                                          unsigned NumVals, bool Packed) {
131   return ConstantStruct::get(Vals, NumVals, Packed);
132 }
133
134
135 // ConstantAggregateZero accessors.
136 ConstantAggregateZero* LLVMContext::getConstantAggregateZero(const Type* Ty) {
137   return ConstantAggregateZero::get(Ty);
138 }
139
140
141 // ConstantArray accessors.
142 Constant* LLVMContext::getConstantArray(const ArrayType* T,
143                                         const std::vector<Constant*>& V) {
144   return ConstantArray::get(T, V);
145 }
146
147 Constant* LLVMContext::getConstantArray(const ArrayType* T,
148                                         Constant* const* Vals,
149                                         unsigned NumVals) {
150   return ConstantArray::get(T, Vals, NumVals);
151 }
152
153 Constant* LLVMContext::getConstantArray(const std::string& Initializer,
154                                         bool AddNull) {
155   return ConstantArray::get(Initializer, AddNull);
156 }
157
158
159 // ConstantExpr accessors.
160 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
161                                        Constant* C2) {
162   return ConstantExpr::get(Opcode, C1, C2);
163 }
164
165 Constant* LLVMContext::getConstantExprTrunc(Constant* C, const Type* Ty) {
166   return ConstantExpr::getTrunc(C, Ty);
167 }
168
169 Constant* LLVMContext::getConstantExprSExt(Constant* C, const Type* Ty) {
170   return ConstantExpr::getSExt(C, Ty);
171 }
172
173 Constant* LLVMContext::getConstantExprZExt(Constant* C, const Type* Ty) {
174   return ConstantExpr::getZExt(C, Ty);  
175 }
176
177 Constant* LLVMContext::getConstantExprFPTrunc(Constant* C, const Type* Ty) {
178   return ConstantExpr::getFPTrunc(C, Ty);
179 }
180
181 Constant* LLVMContext::getConstantExprFPExtend(Constant* C, const Type* Ty) {
182   return ConstantExpr::getFPExtend(C, Ty);
183 }
184
185 Constant* LLVMContext::getConstantExprUIToFP(Constant* C, const Type* Ty) {
186   return ConstantExpr::getUIToFP(C, Ty);
187 }
188
189 Constant* LLVMContext::getConstantExprSIToFP(Constant* C, const Type* Ty) {
190   return ConstantExpr::getSIToFP(C, Ty);
191 }
192
193 Constant* LLVMContext::getConstantExprFPToUI(Constant* C, const Type* Ty) {
194   return ConstantExpr::getFPToUI(C, Ty);
195 }
196
197 Constant* LLVMContext::getConstantExprFPToSI(Constant* C, const Type* Ty) {
198   return ConstantExpr::getFPToSI(C, Ty);
199 }
200
201 Constant* LLVMContext::getConstantExprPtrToInt(Constant* C, const Type* Ty) {
202   return ConstantExpr::getPtrToInt(C, Ty);
203 }
204
205 Constant* LLVMContext::getConstantExprIntToPtr(Constant* C, const Type* Ty) {
206   return ConstantExpr::getIntToPtr(C, Ty);
207 }
208
209 Constant* LLVMContext::getConstantExprBitCast(Constant* C, const Type* Ty) {
210   return ConstantExpr::getBitCast(C, Ty);
211 }
212
213 Constant* LLVMContext::getConstantExprCast(unsigned ops, Constant* C,
214                                            const Type* Ty) {
215   return ConstantExpr::getCast(ops, C, Ty);
216 }
217
218 Constant* LLVMContext::getConstantExprZExtOrBitCast(Constant* C,
219                                                     const Type* Ty) {
220   return ConstantExpr::getZExtOrBitCast(C, Ty);
221 }
222
223 Constant* LLVMContext::getConstantExprSExtOrBitCast(Constant* C,
224                                                     const Type* Ty) {
225   return ConstantExpr::getSExtOrBitCast(C, Ty);
226 }
227
228 Constant* LLVMContext::getConstantExprTruncOrBitCast(Constant* C,
229                                                      const Type* Ty) {
230   return ConstantExpr::getTruncOrBitCast(C, Ty);  
231 }
232
233 Constant* LLVMContext::getConstantExprPointerCast(Constant* C, const Type* Ty) {
234   return ConstantExpr::getPointerCast(C, Ty);
235 }
236
237 Constant* LLVMContext::getConstantExprIntegerCast(Constant* C, const Type* Ty,
238                                                   bool isSigned) {
239   return ConstantExpr::getIntegerCast(C, Ty, isSigned);
240 }
241
242 Constant* LLVMContext::getConstantExprFPCast(Constant* C, const Type* Ty) {
243   return ConstantExpr::getFPCast(C, Ty);
244 }
245
246 Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
247                                              Constant* V2) {
248   return ConstantExpr::getSelect(C, V1, V2);
249 }
250
251 Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
252   // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
253   const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
254   Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
255   Constant *Zero = getConstantInt(Type::Int32Ty, 0);
256   Constant *One = getConstantInt(Type::Int32Ty, 1);
257   Constant *Indices[2] = { Zero, One };
258   Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
259   return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
260 }
261
262 Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
263                                  Constant* C1, Constant* C2) {
264   return ConstantExpr::getCompare(pred, C1, C2);
265 }
266
267 Constant* LLVMContext::getConstantExprNeg(Constant* C) {
268   // API compatibility: Adjust integer opcodes to floating-point opcodes.
269   if (C->getType()->isFPOrFPVector())
270     return getConstantExprFNeg(C);
271   assert(C->getType()->isIntOrIntVector() &&
272          "Cannot NEG a nonintegral value!");
273   return getConstantExpr(Instruction::Sub,
274              getZeroValueForNegation(C->getType()),
275              C);
276 }
277
278 Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
279   assert(C->getType()->isFPOrFPVector() &&
280          "Cannot FNEG a non-floating-point value!");
281   return getConstantExpr(Instruction::FSub,
282              getZeroValueForNegation(C->getType()),
283              C);
284 }
285
286 Constant* LLVMContext::getConstantExprNot(Constant* C) {
287   return ConstantExpr::getNot(C);
288 }
289
290 Constant* LLVMContext::getConstantExprAdd(Constant* C1, Constant* C2) {
291   return ConstantExpr::getAdd(C1, C2);
292 }
293
294 Constant* LLVMContext::getConstantExprFAdd(Constant* C1, Constant* C2) {
295   return ConstantExpr::getFAdd(C1, C2);
296 }
297
298 Constant* LLVMContext::getConstantExprSub(Constant* C1, Constant* C2) {
299   return ConstantExpr::getSub(C1, C2);
300 }
301
302 Constant* LLVMContext::getConstantExprFSub(Constant* C1, Constant* C2) {
303   return ConstantExpr::getFSub(C1, C2);
304 }
305
306 Constant* LLVMContext::getConstantExprMul(Constant* C1, Constant* C2) {
307   return ConstantExpr::getMul(C1, C2);
308 }
309
310 Constant* LLVMContext::getConstantExprFMul(Constant* C1, Constant* C2) {
311   return ConstantExpr::getFMul(C1, C2);
312 }
313
314 Constant* LLVMContext::getConstantExprUDiv(Constant* C1, Constant* C2) {
315   return ConstantExpr::getUDiv(C1, C2);
316 }
317
318 Constant* LLVMContext::getConstantExprSDiv(Constant* C1, Constant* C2) {
319   return ConstantExpr::getSDiv(C1, C2);
320 }
321
322 Constant* LLVMContext::getConstantExprFDiv(Constant* C1, Constant* C2) {
323   return ConstantExpr::getFDiv(C1, C2);
324 }
325
326 Constant* LLVMContext::getConstantExprURem(Constant* C1, Constant* C2) {
327   return ConstantExpr::getURem(C1, C2);
328 }
329
330 Constant* LLVMContext::getConstantExprSRem(Constant* C1, Constant* C2) {
331   return ConstantExpr::getSRem(C1, C2);
332 }
333
334 Constant* LLVMContext::getConstantExprFRem(Constant* C1, Constant* C2) {
335   return ConstantExpr::getFRem(C1, C2);
336 }
337
338 Constant* LLVMContext::getConstantExprAnd(Constant* C1, Constant* C2) {
339   return ConstantExpr::getAnd(C1, C2);
340 }
341
342 Constant* LLVMContext::getConstantExprOr(Constant* C1, Constant* C2) {
343   return ConstantExpr::getOr(C1, C2);
344 }
345
346 Constant* LLVMContext::getConstantExprXor(Constant* C1, Constant* C2) {
347   return ConstantExpr::getXor(C1, C2);
348 }
349
350 Constant* LLVMContext::getConstantExprICmp(unsigned short pred, Constant* LHS,
351                               Constant* RHS) {
352   return ConstantExpr::getICmp(pred, LHS, RHS);
353 }
354
355 Constant* LLVMContext::getConstantExprFCmp(unsigned short pred, Constant* LHS,
356                               Constant* RHS) {
357   return ConstantExpr::getFCmp(pred, LHS, RHS);
358 }
359
360 Constant* LLVMContext::getConstantExprShl(Constant* C1, Constant* C2) {
361   return ConstantExpr::getShl(C1, C2);
362 }
363
364 Constant* LLVMContext::getConstantExprLShr(Constant* C1, Constant* C2) {
365   return ConstantExpr::getLShr(C1, C2);
366 }
367
368 Constant* LLVMContext::getConstantExprAShr(Constant* C1, Constant* C2) {
369   return ConstantExpr::getAShr(C1, C2);
370 }
371
372 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
373                                                     Constant* const* IdxList, 
374                                                     unsigned NumIdx) {
375   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
376 }
377
378 Constant* LLVMContext::getConstantExprGetElementPtr(Constant* C,
379                                                     Value* const* IdxList, 
380                                                     unsigned NumIdx) {
381   return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
382 }
383
384 Constant* LLVMContext::getConstantExprExtractElement(Constant* Vec,
385                                                      Constant* Idx) {
386   return ConstantExpr::getExtractElement(Vec, Idx);
387 }
388
389 Constant* LLVMContext::getConstantExprInsertElement(Constant* Vec,
390                                                     Constant* Elt,
391                                                     Constant* Idx) {
392   return ConstantExpr::getInsertElement(Vec, Elt, Idx);
393 }
394
395 Constant* LLVMContext::getConstantExprShuffleVector(Constant* V1, Constant* V2,
396                                                     Constant* Mask) {
397   return ConstantExpr::getShuffleVector(V1, V2, Mask);
398 }
399
400 Constant* LLVMContext::getConstantExprExtractValue(Constant* Agg,
401                                                    const unsigned* IdxList, 
402                                                    unsigned NumIdx) {
403   return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
404 }
405
406 Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
407                                                   const unsigned* IdxList,
408                                                   unsigned NumIdx) {
409   return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
410 }
411
412 Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
413   // sizeof is implemented as: (i64) gep (Ty*)null, 1
414   Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
415   Constant *GEP = getConstantExprGetElementPtr(
416                             getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
417   return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
418 }
419
420 Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
421   if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
422     if (PTy->getElementType()->isFloatingPoint()) {
423       std::vector<Constant*> zeros(PTy->getNumElements(),
424                            getConstantFPNegativeZero(PTy->getElementType()));
425       return getConstantVector(PTy, zeros);
426     }
427
428   if (Ty->isFloatingPoint()) 
429     return getConstantFPNegativeZero(Ty);
430
431   return getNullValue(Ty);
432 }
433
434
435 // ConstantFP accessors.
436 ConstantFP* LLVMContext::getConstantFP(const APFloat& V) {
437   return ConstantFP::get(V);
438 }
439
440 Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
441   return ConstantFP::get(Ty, V);
442 }
443
444 ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
445   APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
446   apf.changeSign();
447   return getConstantFP(apf);
448 }
449
450
451 // ConstantVector accessors.
452 Constant* LLVMContext::getConstantVector(const VectorType* T,
453                             const std::vector<Constant*>& V) {
454   return ConstantVector::get(T, V);
455 }
456
457 Constant* LLVMContext::getConstantVector(const std::vector<Constant*>& V) {
458   return ConstantVector::get(V);
459 }
460
461 Constant* LLVMContext::getConstantVector(Constant* const* Vals,
462                                          unsigned NumVals) {
463   return ConstantVector::get(Vals, NumVals);
464 }
465
466 ConstantVector* LLVMContext::getConstantVectorAllOnesValue(
467                                                          const VectorType* Ty) {
468   return ConstantVector::getAllOnesValue(Ty);
469 }
470
471 // MDNode accessors
472 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
473   return MDNode::get(Vals, NumVals);
474 }
475
476 // MDString accessors
477 MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) {
478   return MDString::get(StrBegin, StrEnd);
479 }
480
481 MDString* LLVMContext::getMDString(const std::string &Str) {
482   return MDString::get(Str);
483 }
484
485 // FunctionType accessors
486 FunctionType* LLVMContext::getFunctionType(const Type* Result, bool isVarArg) {
487   return FunctionType::get(Result, isVarArg);
488 }
489
490 FunctionType* LLVMContext::getFunctionType(const Type* Result,
491                                          const std::vector<const Type*>& Params,
492                                          bool isVarArg) {
493   return FunctionType::get(Result, Params, isVarArg);
494 }
495                                 
496 // IntegerType accessors
497 const IntegerType* LLVMContext::getIntegerType(unsigned NumBits) {
498   return IntegerType::get(NumBits);
499 }
500   
501 // OpaqueType accessors
502 OpaqueType* LLVMContext::getOpaqueType() {
503   return OpaqueType::get();
504 }
505
506 // StructType accessors
507 StructType* LLVMContext::getStructType(bool isPacked) {
508   return StructType::get(isPacked);
509 }
510
511 StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
512                                        bool isPacked) {
513   return StructType::get(Params, isPacked);
514 }
515
516 StructType *LLVMContext::getStructType(const Type *type, ...) {
517   va_list ap;
518   std::vector<const llvm::Type*> StructFields;
519   va_start(ap, type);
520   while (type) {
521     StructFields.push_back(type);
522     type = va_arg(ap, llvm::Type*);
523   }
524   return StructType::get(StructFields);
525 }
526
527 // ArrayType accessors
528 ArrayType* LLVMContext::getArrayType(const Type* ElementType,
529                                      uint64_t NumElements) {
530   return ArrayType::get(ElementType, NumElements);
531 }
532   
533 // PointerType accessors
534 PointerType* LLVMContext::getPointerType(const Type* ElementType,
535                                          unsigned AddressSpace) {
536   return PointerType::get(ElementType, AddressSpace);
537 }
538
539 PointerType* LLVMContext::getPointerTypeUnqual(const Type* ElementType) {
540   return PointerType::getUnqual(ElementType);
541 }
542   
543 // VectorType accessors
544 VectorType* LLVMContext::getVectorType(const Type* ElementType,
545                                        unsigned NumElements) {
546   return VectorType::get(ElementType, NumElements);
547 }
548
549 VectorType* LLVMContext::getVectorTypeInteger(const VectorType* VTy) {
550   return VectorType::getInteger(VTy);  
551 }
552
553 VectorType* LLVMContext::getVectorTypeExtendedElement(const VectorType* VTy) {
554   return VectorType::getExtendedElementVectorType(VTy);
555 }
556
557 VectorType* LLVMContext::getVectorTypeTruncatedElement(const VectorType* VTy) {
558   return VectorType::getTruncatedElementVectorType(VTy);
559 }
560
561 const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
562   if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
563     return getVectorType(Type::Int1Ty, vt->getNumElements());
564   }
565   return Type::Int1Ty;
566 }