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