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