1 //===-- Constants.cpp - Implement Constant nodes --------------------------===//
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 the Constant* classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/Constants.h"
15 #include "ConstantFold.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/GetElementPtrTypeIterator.h"
22 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Operator.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <algorithm>
32 
33 using namespace llvm;
34 
35 //===----------------------------------------------------------------------===//
36 //                              Constant Class
37 //===----------------------------------------------------------------------===//
38 
39 bool Constant::isNegativeZeroValue() const {
40   // Floating point values have an explicit -0.0 value.
41   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
42     return CFP->isZero() && CFP->isNegative();
43 
44   // Equivalent for a vector of -0.0's.
45   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
46     if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
47       if (CV->getElementAsAPFloat(0).isNegZero())
48         return true;
49 
50   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
51     if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
52       if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative())
53         return true;
54 
55   // We've already handled true FP case; any other FP vectors can't represent -0.0.
56   if (getType()->isFPOrFPVectorTy())
57     return false;
58 
59   // Otherwise, just use +0.0.
60   return isNullValue();
61 }
62 
63 // Return true iff this constant is positive zero (floating point), negative
64 // zero (floating point), or a null value.
65 bool Constant::isZeroValue() const {
66   // Floating point values have an explicit -0.0 value.
67   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
68     return CFP->isZero();
69 
70   // Equivalent for a vector of -0.0's.
71   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
72     if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
73       if (CV->getElementAsAPFloat(0).isZero())
74         return true;
75 
76   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
77     if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
78       if (SplatCFP && SplatCFP->isZero())
79         return true;
80 
81   // Otherwise, just use +0.0.
82   return isNullValue();
83 }
84 
85 bool Constant::isNullValue() const {
86   // 0 is null.
87   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
88     return CI->isZero();
89 
90   // +0.0 is null.
91   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
92     return CFP->isZero() && !CFP->isNegative();
93 
94   // constant zero is zero for aggregates, cpnull is null for pointers, none for
95   // tokens.
96   return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) ||
97          isa<ConstantTokenNone>(this);
98 }
99 
100 bool Constant::isAllOnesValue() const {
101   // Check for -1 integers
102   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
103     return CI->isMinusOne();
104 
105   // Check for FP which are bitcasted from -1 integers
106   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
107     return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue();
108 
109   // Check for constant vectors which are splats of -1 values.
110   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
111     if (Constant *Splat = CV->getSplatValue())
112       return Splat->isAllOnesValue();
113 
114   // Check for constant vectors which are splats of -1 values.
115   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
116     if (CV->isSplat()) {
117       if (CV->getElementType()->isFloatingPointTy())
118         return CV->getElementAsAPFloat(0).bitcastToAPInt().isAllOnesValue();
119       return CV->getElementAsAPInt(0).isAllOnesValue();
120     }
121   }
122 
123   return false;
124 }
125 
126 bool Constant::isOneValue() const {
127   // Check for 1 integers
128   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
129     return CI->isOne();
130 
131   // Check for FP which are bitcasted from 1 integers
132   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
133     return CFP->getValueAPF().bitcastToAPInt().isOneValue();
134 
135   // Check for constant vectors which are splats of 1 values.
136   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
137     if (Constant *Splat = CV->getSplatValue())
138       return Splat->isOneValue();
139 
140   // Check for constant vectors which are splats of 1 values.
141   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
142     if (CV->isSplat()) {
143       if (CV->getElementType()->isFloatingPointTy())
144         return CV->getElementAsAPFloat(0).bitcastToAPInt().isOneValue();
145       return CV->getElementAsAPInt(0).isOneValue();
146     }
147   }
148 
149   return false;
150 }
151 
152 bool Constant::isMinSignedValue() const {
153   // Check for INT_MIN integers
154   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
155     return CI->isMinValue(/*isSigned=*/true);
156 
157   // Check for FP which are bitcasted from INT_MIN integers
158   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
159     return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
160 
161   // Check for constant vectors which are splats of INT_MIN values.
162   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
163     if (Constant *Splat = CV->getSplatValue())
164       return Splat->isMinSignedValue();
165 
166   // Check for constant vectors which are splats of INT_MIN values.
167   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
168     if (CV->isSplat()) {
169       if (CV->getElementType()->isFloatingPointTy())
170         return CV->getElementAsAPFloat(0).bitcastToAPInt().isMinSignedValue();
171       return CV->getElementAsAPInt(0).isMinSignedValue();
172     }
173   }
174 
175   return false;
176 }
177 
178 bool Constant::isNotMinSignedValue() const {
179   // Check for INT_MIN integers
180   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
181     return !CI->isMinValue(/*isSigned=*/true);
182 
183   // Check for FP which are bitcasted from INT_MIN integers
184   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
185     return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
186 
187   // Check that vectors don't contain INT_MIN
188   if (this->getType()->isVectorTy()) {
189     unsigned NumElts = this->getType()->getVectorNumElements();
190     for (unsigned i = 0; i != NumElts; ++i) {
191       Constant *Elt = this->getAggregateElement(i);
192       if (!Elt || !Elt->isNotMinSignedValue())
193         return false;
194     }
195     return true;
196   }
197 
198   // It *may* contain INT_MIN, we can't tell.
199   return false;
200 }
201 
202 bool Constant::isFiniteNonZeroFP() const {
203   if (auto *CFP = dyn_cast<ConstantFP>(this))
204     return CFP->getValueAPF().isFiniteNonZero();
205   if (!getType()->isVectorTy())
206     return false;
207   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
208     auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
209     if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
210       return false;
211   }
212   return true;
213 }
214 
215 bool Constant::isNormalFP() const {
216   if (auto *CFP = dyn_cast<ConstantFP>(this))
217     return CFP->getValueAPF().isNormal();
218   if (!getType()->isVectorTy())
219     return false;
220   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
221     auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
222     if (!CFP || !CFP->getValueAPF().isNormal())
223       return false;
224   }
225   return true;
226 }
227 
228 bool Constant::hasExactInverseFP() const {
229   if (auto *CFP = dyn_cast<ConstantFP>(this))
230     return CFP->getValueAPF().getExactInverse(nullptr);
231   if (!getType()->isVectorTy())
232     return false;
233   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
234     auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
235     if (!CFP || !CFP->getValueAPF().getExactInverse(nullptr))
236       return false;
237   }
238   return true;
239 }
240 
241 bool Constant::isNaN() const {
242   if (auto *CFP = dyn_cast<ConstantFP>(this))
243     return CFP->isNaN();
244   if (!getType()->isVectorTy())
245     return false;
246   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
247     auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
248     if (!CFP || !CFP->isNaN())
249       return false;
250   }
251   return true;
252 }
253 
254 bool Constant::containsUndefElement() const {
255   if (!getType()->isVectorTy())
256     return false;
257   for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i)
258     if (isa<UndefValue>(getAggregateElement(i)))
259       return true;
260 
261   return false;
262 }
263 
264 /// Constructor to create a '0' constant of arbitrary type.
265 Constant *Constant::getNullValue(Type *Ty) {
266   switch (Ty->getTypeID()) {
267   case Type::IntegerTyID:
268     return ConstantInt::get(Ty, 0);
269   case Type::HalfTyID:
270     return ConstantFP::get(Ty->getContext(),
271                            APFloat::getZero(APFloat::IEEEhalf()));
272   case Type::FloatTyID:
273     return ConstantFP::get(Ty->getContext(),
274                            APFloat::getZero(APFloat::IEEEsingle()));
275   case Type::DoubleTyID:
276     return ConstantFP::get(Ty->getContext(),
277                            APFloat::getZero(APFloat::IEEEdouble()));
278   case Type::X86_FP80TyID:
279     return ConstantFP::get(Ty->getContext(),
280                            APFloat::getZero(APFloat::x87DoubleExtended()));
281   case Type::FP128TyID:
282     return ConstantFP::get(Ty->getContext(),
283                            APFloat::getZero(APFloat::IEEEquad()));
284   case Type::PPC_FP128TyID:
285     return ConstantFP::get(Ty->getContext(),
286                            APFloat(APFloat::PPCDoubleDouble(),
287                                    APInt::getNullValue(128)));
288   case Type::PointerTyID:
289     return ConstantPointerNull::get(cast<PointerType>(Ty));
290   case Type::StructTyID:
291   case Type::ArrayTyID:
292   case Type::VectorTyID:
293     return ConstantAggregateZero::get(Ty);
294   case Type::TokenTyID:
295     return ConstantTokenNone::get(Ty->getContext());
296   default:
297     // Function, Label, or Opaque type?
298     llvm_unreachable("Cannot create a null constant of that type!");
299   }
300 }
301 
302 Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
303   Type *ScalarTy = Ty->getScalarType();
304 
305   // Create the base integer constant.
306   Constant *C = ConstantInt::get(Ty->getContext(), V);
307 
308   // Convert an integer to a pointer, if necessary.
309   if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
310     C = ConstantExpr::getIntToPtr(C, PTy);
311 
312   // Broadcast a scalar to a vector, if necessary.
313   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
314     C = ConstantVector::getSplat(VTy->getNumElements(), C);
315 
316   return C;
317 }
318 
319 Constant *Constant::getAllOnesValue(Type *Ty) {
320   if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
321     return ConstantInt::get(Ty->getContext(),
322                             APInt::getAllOnesValue(ITy->getBitWidth()));
323 
324   if (Ty->isFloatingPointTy()) {
325     APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
326                                           !Ty->isPPC_FP128Ty());
327     return ConstantFP::get(Ty->getContext(), FL);
328   }
329 
330   VectorType *VTy = cast<VectorType>(Ty);
331   return ConstantVector::getSplat(VTy->getNumElements(),
332                                   getAllOnesValue(VTy->getElementType()));
333 }
334 
335 Constant *Constant::getAggregateElement(unsigned Elt) const {
336   if (const ConstantAggregate *CC = dyn_cast<ConstantAggregate>(this))
337     return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;
338 
339   if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
340     return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
341 
342   if (const UndefValue *UV = dyn_cast<UndefValue>(this))
343     return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
344 
345   if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
346     return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
347                                        : nullptr;
348   return nullptr;
349 }
350 
351 Constant *Constant::getAggregateElement(Constant *Elt) const {
352   assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
353   if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt)) {
354     // Check if the constant fits into an uint64_t.
355     if (CI->getValue().getActiveBits() > 64)
356       return nullptr;
357     return getAggregateElement(CI->getZExtValue());
358   }
359   return nullptr;
360 }
361 
362 void Constant::destroyConstant() {
363   /// First call destroyConstantImpl on the subclass.  This gives the subclass
364   /// a chance to remove the constant from any maps/pools it's contained in.
365   switch (getValueID()) {
366   default:
367     llvm_unreachable("Not a constant!");
368 #define HANDLE_CONSTANT(Name)                                                  \
369   case Value::Name##Val:                                                       \
370     cast<Name>(this)->destroyConstantImpl();                                   \
371     break;
372 #include "llvm/IR/Value.def"
373   }
374 
375   // When a Constant is destroyed, there may be lingering
376   // references to the constant by other constants in the constant pool.  These
377   // constants are implicitly dependent on the module that is being deleted,
378   // but they don't know that.  Because we only find out when the CPV is
379   // deleted, we must now notify all of our users (that should only be
380   // Constants) that they are, in fact, invalid now and should be deleted.
381   //
382   while (!use_empty()) {
383     Value *V = user_back();
384 #ifndef NDEBUG // Only in -g mode...
385     if (!isa<Constant>(V)) {
386       dbgs() << "While deleting: " << *this
387              << "\n\nUse still stuck around after Def is destroyed: " << *V
388              << "\n\n";
389     }
390 #endif
391     assert(isa<Constant>(V) && "References remain to Constant being destroyed");
392     cast<Constant>(V)->destroyConstant();
393 
394     // The constant should remove itself from our use list...
395     assert((use_empty() || user_back() != V) && "Constant not removed!");
396   }
397 
398   // Value has no outstanding references it is safe to delete it now...
399   delete this;
400 }
401 
402 static bool canTrapImpl(const Constant *C,
403                         SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) {
404   assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
405   // The only thing that could possibly trap are constant exprs.
406   const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
407   if (!CE)
408     return false;
409 
410   // ConstantExpr traps if any operands can trap.
411   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
412     if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) {
413       if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps))
414         return true;
415     }
416   }
417 
418   // Otherwise, only specific operations can trap.
419   switch (CE->getOpcode()) {
420   default:
421     return false;
422   case Instruction::UDiv:
423   case Instruction::SDiv:
424   case Instruction::URem:
425   case Instruction::SRem:
426     // Div and rem can trap if the RHS is not known to be non-zero.
427     if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
428       return true;
429     return false;
430   }
431 }
432 
433 bool Constant::canTrap() const {
434   SmallPtrSet<const ConstantExpr *, 4> NonTrappingOps;
435   return canTrapImpl(this, NonTrappingOps);
436 }
437 
438 /// Check if C contains a GlobalValue for which Predicate is true.
439 static bool
440 ConstHasGlobalValuePredicate(const Constant *C,
441                              bool (*Predicate)(const GlobalValue *)) {
442   SmallPtrSet<const Constant *, 8> Visited;
443   SmallVector<const Constant *, 8> WorkList;
444   WorkList.push_back(C);
445   Visited.insert(C);
446 
447   while (!WorkList.empty()) {
448     const Constant *WorkItem = WorkList.pop_back_val();
449     if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
450       if (Predicate(GV))
451         return true;
452     for (const Value *Op : WorkItem->operands()) {
453       const Constant *ConstOp = dyn_cast<Constant>(Op);
454       if (!ConstOp)
455         continue;
456       if (Visited.insert(ConstOp).second)
457         WorkList.push_back(ConstOp);
458     }
459   }
460   return false;
461 }
462 
463 bool Constant::isThreadDependent() const {
464   auto DLLImportPredicate = [](const GlobalValue *GV) {
465     return GV->isThreadLocal();
466   };
467   return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
468 }
469 
470 bool Constant::isDLLImportDependent() const {
471   auto DLLImportPredicate = [](const GlobalValue *GV) {
472     return GV->hasDLLImportStorageClass();
473   };
474   return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
475 }
476 
477 bool Constant::isConstantUsed() const {
478   for (const User *U : users()) {
479     const Constant *UC = dyn_cast<Constant>(U);
480     if (!UC || isa<GlobalValue>(UC))
481       return true;
482 
483     if (UC->isConstantUsed())
484       return true;
485   }
486   return false;
487 }
488 
489 bool Constant::needsRelocation() const {
490   if (isa<GlobalValue>(this))
491     return true; // Global reference.
492 
493   if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
494     return BA->getFunction()->needsRelocation();
495 
496   // While raw uses of blockaddress need to be relocated, differences between
497   // two of them don't when they are for labels in the same function.  This is a
498   // common idiom when creating a table for the indirect goto extension, so we
499   // handle it efficiently here.
500   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
501     if (CE->getOpcode() == Instruction::Sub) {
502       ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
503       ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
504       if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
505           RHS->getOpcode() == Instruction::PtrToInt &&
506           isa<BlockAddress>(LHS->getOperand(0)) &&
507           isa<BlockAddress>(RHS->getOperand(0)) &&
508           cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
509               cast<BlockAddress>(RHS->getOperand(0))->getFunction())
510         return false;
511     }
512 
513   bool Result = false;
514   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
515     Result |= cast<Constant>(getOperand(i))->needsRelocation();
516 
517   return Result;
518 }
519 
520 /// If the specified constantexpr is dead, remove it. This involves recursively
521 /// eliminating any dead users of the constantexpr.
522 static bool removeDeadUsersOfConstant(const Constant *C) {
523   if (isa<GlobalValue>(C)) return false; // Cannot remove this
524 
525   while (!C->use_empty()) {
526     const Constant *User = dyn_cast<Constant>(C->user_back());
527     if (!User) return false; // Non-constant usage;
528     if (!removeDeadUsersOfConstant(User))
529       return false; // Constant wasn't dead
530   }
531 
532   const_cast<Constant*>(C)->destroyConstant();
533   return true;
534 }
535 
536 
537 void Constant::removeDeadConstantUsers() const {
538   Value::const_user_iterator I = user_begin(), E = user_end();
539   Value::const_user_iterator LastNonDeadUser = E;
540   while (I != E) {
541     const Constant *User = dyn_cast<Constant>(*I);
542     if (!User) {
543       LastNonDeadUser = I;
544       ++I;
545       continue;
546     }
547 
548     if (!removeDeadUsersOfConstant(User)) {
549       // If the constant wasn't dead, remember that this was the last live use
550       // and move on to the next constant.
551       LastNonDeadUser = I;
552       ++I;
553       continue;
554     }
555 
556     // If the constant was dead, then the iterator is invalidated.
557     if (LastNonDeadUser == E) {
558       I = user_begin();
559       if (I == E) break;
560     } else {
561       I = LastNonDeadUser;
562       ++I;
563     }
564   }
565 }
566 
567 
568 
569 //===----------------------------------------------------------------------===//
570 //                                ConstantInt
571 //===----------------------------------------------------------------------===//
572 
573 ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V)
574     : ConstantData(Ty, ConstantIntVal), Val(V) {
575   assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
576 }
577 
578 ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
579   LLVMContextImpl *pImpl = Context.pImpl;
580   if (!pImpl->TheTrueVal)
581     pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
582   return pImpl->TheTrueVal;
583 }
584 
585 ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
586   LLVMContextImpl *pImpl = Context.pImpl;
587   if (!pImpl->TheFalseVal)
588     pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
589   return pImpl->TheFalseVal;
590 }
591 
592 Constant *ConstantInt::getTrue(Type *Ty) {
593   assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
594   ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext());
595   if (auto *VTy = dyn_cast<VectorType>(Ty))
596     return ConstantVector::getSplat(VTy->getNumElements(), TrueC);
597   return TrueC;
598 }
599 
600 Constant *ConstantInt::getFalse(Type *Ty) {
601   assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
602   ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext());
603   if (auto *VTy = dyn_cast<VectorType>(Ty))
604     return ConstantVector::getSplat(VTy->getNumElements(), FalseC);
605   return FalseC;
606 }
607 
608 // Get a ConstantInt from an APInt.
609 ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
610   // get an existing value or the insertion position
611   LLVMContextImpl *pImpl = Context.pImpl;
612   std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V];
613   if (!Slot) {
614     // Get the corresponding integer type for the bit width of the value.
615     IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
616     Slot.reset(new ConstantInt(ITy, V));
617   }
618   assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
619   return Slot.get();
620 }
621 
622 Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
623   Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
624 
625   // For vectors, broadcast the value.
626   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
627     return ConstantVector::getSplat(VTy->getNumElements(), C);
628 
629   return C;
630 }
631 
632 ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
633   return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
634 }
635 
636 ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) {
637   return get(Ty, V, true);
638 }
639 
640 Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
641   return get(Ty, V, true);
642 }
643 
644 Constant *ConstantInt::get(Type *Ty, const APInt& V) {
645   ConstantInt *C = get(Ty->getContext(), V);
646   assert(C->getType() == Ty->getScalarType() &&
647          "ConstantInt type doesn't match the type implied by its value!");
648 
649   // For vectors, broadcast the value.
650   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
651     return ConstantVector::getSplat(VTy->getNumElements(), C);
652 
653   return C;
654 }
655 
656 ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {
657   return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
658 }
659 
660 /// Remove the constant from the constant table.
661 void ConstantInt::destroyConstantImpl() {
662   llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
663 }
664 
665 //===----------------------------------------------------------------------===//
666 //                                ConstantFP
667 //===----------------------------------------------------------------------===//
668 
669 static const fltSemantics *TypeToFloatSemantics(Type *Ty) {
670   if (Ty->isHalfTy())
671     return &APFloat::IEEEhalf();
672   if (Ty->isFloatTy())
673     return &APFloat::IEEEsingle();
674   if (Ty->isDoubleTy())
675     return &APFloat::IEEEdouble();
676   if (Ty->isX86_FP80Ty())
677     return &APFloat::x87DoubleExtended();
678   else if (Ty->isFP128Ty())
679     return &APFloat::IEEEquad();
680 
681   assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
682   return &APFloat::PPCDoubleDouble();
683 }
684 
685 Constant *ConstantFP::get(Type *Ty, double V) {
686   LLVMContext &Context = Ty->getContext();
687 
688   APFloat FV(V);
689   bool ignored;
690   FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
691              APFloat::rmNearestTiesToEven, &ignored);
692   Constant *C = get(Context, FV);
693 
694   // For vectors, broadcast the value.
695   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
696     return ConstantVector::getSplat(VTy->getNumElements(), C);
697 
698   return C;
699 }
700 
701 Constant *ConstantFP::get(Type *Ty, const APFloat &V) {
702   ConstantFP *C = get(Ty->getContext(), V);
703   assert(C->getType() == Ty->getScalarType() &&
704          "ConstantFP type doesn't match the type implied by its value!");
705 
706   // For vectors, broadcast the value.
707   if (auto *VTy = dyn_cast<VectorType>(Ty))
708     return ConstantVector::getSplat(VTy->getNumElements(), C);
709 
710   return C;
711 }
712 
713 Constant *ConstantFP::get(Type *Ty, StringRef Str) {
714   LLVMContext &Context = Ty->getContext();
715 
716   APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
717   Constant *C = get(Context, FV);
718 
719   // For vectors, broadcast the value.
720   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
721     return ConstantVector::getSplat(VTy->getNumElements(), C);
722 
723   return C;
724 }
725 
726 Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) {
727   const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
728   APFloat NaN = APFloat::getNaN(Semantics, Negative, Payload);
729   Constant *C = get(Ty->getContext(), NaN);
730 
731   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
732     return ConstantVector::getSplat(VTy->getNumElements(), C);
733 
734   return C;
735 }
736 
737 Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) {
738   const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
739   APFloat NaN = APFloat::getQNaN(Semantics, Negative, Payload);
740   Constant *C = get(Ty->getContext(), NaN);
741 
742   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
743     return ConstantVector::getSplat(VTy->getNumElements(), C);
744 
745   return C;
746 }
747 
748 Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) {
749   const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
750   APFloat NaN = APFloat::getSNaN(Semantics, Negative, Payload);
751   Constant *C = get(Ty->getContext(), NaN);
752 
753   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
754     return ConstantVector::getSplat(VTy->getNumElements(), C);
755 
756   return C;
757 }
758 
759 Constant *ConstantFP::getNegativeZero(Type *Ty) {
760   const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
761   APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
762   Constant *C = get(Ty->getContext(), NegZero);
763 
764   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
765     return ConstantVector::getSplat(VTy->getNumElements(), C);
766 
767   return C;
768 }
769 
770 
771 Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
772   if (Ty->isFPOrFPVectorTy())
773     return getNegativeZero(Ty);
774 
775   return Constant::getNullValue(Ty);
776 }
777 
778 
779 // ConstantFP accessors.
780 ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
781   LLVMContextImpl* pImpl = Context.pImpl;
782 
783   std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
784 
785   if (!Slot) {
786     Type *Ty;
787     if (&V.getSemantics() == &APFloat::IEEEhalf())
788       Ty = Type::getHalfTy(Context);
789     else if (&V.getSemantics() == &APFloat::IEEEsingle())
790       Ty = Type::getFloatTy(Context);
791     else if (&V.getSemantics() == &APFloat::IEEEdouble())
792       Ty = Type::getDoubleTy(Context);
793     else if (&V.getSemantics() == &APFloat::x87DoubleExtended())
794       Ty = Type::getX86_FP80Ty(Context);
795     else if (&V.getSemantics() == &APFloat::IEEEquad())
796       Ty = Type::getFP128Ty(Context);
797     else {
798       assert(&V.getSemantics() == &APFloat::PPCDoubleDouble() &&
799              "Unknown FP format");
800       Ty = Type::getPPC_FP128Ty(Context);
801     }
802     Slot.reset(new ConstantFP(Ty, V));
803   }
804 
805   return Slot.get();
806 }
807 
808 Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
809   const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
810   Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
811 
812   if (VectorType *VTy = dyn_cast<VectorType>(Ty))
813     return ConstantVector::getSplat(VTy->getNumElements(), C);
814 
815   return C;
816 }
817 
818 ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
819     : ConstantData(Ty, ConstantFPVal), Val(V) {
820   assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
821          "FP type Mismatch");
822 }
823 
824 bool ConstantFP::isExactlyValue(const APFloat &V) const {
825   return Val.bitwiseIsEqual(V);
826 }
827 
828 /// Remove the constant from the constant table.
829 void ConstantFP::destroyConstantImpl() {
830   llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
831 }
832 
833 //===----------------------------------------------------------------------===//
834 //                   ConstantAggregateZero Implementation
835 //===----------------------------------------------------------------------===//
836 
837 Constant *ConstantAggregateZero::getSequentialElement() const {
838   return Constant::getNullValue(getType()->getSequentialElementType());
839 }
840 
841 Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
842   return Constant::getNullValue(getType()->getStructElementType(Elt));
843 }
844 
845 Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
846   if (isa<SequentialType>(getType()))
847     return getSequentialElement();
848   return getStructElement(cast<ConstantInt>(C)->getZExtValue());
849 }
850 
851 Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
852   if (isa<SequentialType>(getType()))
853     return getSequentialElement();
854   return getStructElement(Idx);
855 }
856 
857 unsigned ConstantAggregateZero::getNumElements() const {
858   Type *Ty = getType();
859   if (auto *AT = dyn_cast<ArrayType>(Ty))
860     return AT->getNumElements();
861   if (auto *VT = dyn_cast<VectorType>(Ty))
862     return VT->getNumElements();
863   return Ty->getStructNumElements();
864 }
865 
866 //===----------------------------------------------------------------------===//
867 //                         UndefValue Implementation
868 //===----------------------------------------------------------------------===//
869 
870 UndefValue *UndefValue::getSequentialElement() const {
871   return UndefValue::get(getType()->getSequentialElementType());
872 }
873 
874 UndefValue *UndefValue::getStructElement(unsigned Elt) const {
875   return UndefValue::get(getType()->getStructElementType(Elt));
876 }
877 
878 UndefValue *UndefValue::getElementValue(Constant *C) const {
879   if (isa<SequentialType>(getType()))
880     return getSequentialElement();
881   return getStructElement(cast<ConstantInt>(C)->getZExtValue());
882 }
883 
884 UndefValue *UndefValue::getElementValue(unsigned Idx) const {
885   if (isa<SequentialType>(getType()))
886     return getSequentialElement();
887   return getStructElement(Idx);
888 }
889 
890 unsigned UndefValue::getNumElements() const {
891   Type *Ty = getType();
892   if (auto *ST = dyn_cast<SequentialType>(Ty))
893     return ST->getNumElements();
894   return Ty->getStructNumElements();
895 }
896 
897 //===----------------------------------------------------------------------===//
898 //                            ConstantXXX Classes
899 //===----------------------------------------------------------------------===//
900 
901 template <typename ItTy, typename EltTy>
902 static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
903   for (; Start != End; ++Start)
904     if (*Start != Elt)
905       return false;
906   return true;
907 }
908 
909 template <typename SequentialTy, typename ElementTy>
910 static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
911   assert(!V.empty() && "Cannot get empty int sequence.");
912 
913   SmallVector<ElementTy, 16> Elts;
914   for (Constant *C : V)
915     if (auto *CI = dyn_cast<ConstantInt>(C))
916       Elts.push_back(CI->getZExtValue());
917     else
918       return nullptr;
919   return SequentialTy::get(V[0]->getContext(), Elts);
920 }
921 
922 template <typename SequentialTy, typename ElementTy>
923 static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
924   assert(!V.empty() && "Cannot get empty FP sequence.");
925 
926   SmallVector<ElementTy, 16> Elts;
927   for (Constant *C : V)
928     if (auto *CFP = dyn_cast<ConstantFP>(C))
929       Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
930     else
931       return nullptr;
932   return SequentialTy::getFP(V[0]->getContext(), Elts);
933 }
934 
935 template <typename SequenceTy>
936 static Constant *getSequenceIfElementsMatch(Constant *C,
937                                             ArrayRef<Constant *> V) {
938   // We speculatively build the elements here even if it turns out that there is
939   // a constantexpr or something else weird, since it is so uncommon for that to
940   // happen.
941   if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
942     if (CI->getType()->isIntegerTy(8))
943       return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
944     else if (CI->getType()->isIntegerTy(16))
945       return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
946     else if (CI->getType()->isIntegerTy(32))
947       return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
948     else if (CI->getType()->isIntegerTy(64))
949       return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
950   } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
951     if (CFP->getType()->isHalfTy())
952       return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
953     else if (CFP->getType()->isFloatTy())
954       return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
955     else if (CFP->getType()->isDoubleTy())
956       return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
957   }
958 
959   return nullptr;
960 }
961 
962 ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT,
963                                      ArrayRef<Constant *> V)
964     : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
965                V.size()) {
966   llvm::copy(V, op_begin());
967 
968   // Check that types match, unless this is an opaque struct.
969   if (auto *ST = dyn_cast<StructType>(T))
970     if (ST->isOpaque())
971       return;
972   for (unsigned I = 0, E = V.size(); I != E; ++I)
973     assert(V[I]->getType() == T->getTypeAtIndex(I) &&
974            "Initializer for composite element doesn't match!");
975 }
976 
977 ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
978     : ConstantAggregate(T, ConstantArrayVal, V) {
979   assert(V.size() == T->getNumElements() &&
980          "Invalid initializer for constant array");
981 }
982 
983 Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
984   if (Constant *C = getImpl(Ty, V))
985     return C;
986   return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
987 }
988 
989 Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
990   // Empty arrays are canonicalized to ConstantAggregateZero.
991   if (V.empty())
992     return ConstantAggregateZero::get(Ty);
993 
994   for (unsigned i = 0, e = V.size(); i != e; ++i) {
995     assert(V[i]->getType() == Ty->getElementType() &&
996            "Wrong type in array element initializer");
997   }
998 
999   // If this is an all-zero array, return a ConstantAggregateZero object.  If
1000   // all undef, return an UndefValue, if "all simple", then return a
1001   // ConstantDataArray.
1002   Constant *C = V[0];
1003   if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
1004     return UndefValue::get(Ty);
1005 
1006   if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
1007     return ConstantAggregateZero::get(Ty);
1008 
1009   // Check to see if all of the elements are ConstantFP or ConstantInt and if
1010   // the element type is compatible with ConstantDataVector.  If so, use it.
1011   if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1012     return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
1013 
1014   // Otherwise, we really do want to create a ConstantArray.
1015   return nullptr;
1016 }
1017 
1018 StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
1019                                                ArrayRef<Constant*> V,
1020                                                bool Packed) {
1021   unsigned VecSize = V.size();
1022   SmallVector<Type*, 16> EltTypes(VecSize);
1023   for (unsigned i = 0; i != VecSize; ++i)
1024     EltTypes[i] = V[i]->getType();
1025 
1026   return StructType::get(Context, EltTypes, Packed);
1027 }
1028 
1029 
1030 StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
1031                                                bool Packed) {
1032   assert(!V.empty() &&
1033          "ConstantStruct::getTypeForElements cannot be called on empty list");
1034   return getTypeForElements(V[0]->getContext(), V, Packed);
1035 }
1036 
1037 ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
1038     : ConstantAggregate(T, ConstantStructVal, V) {
1039   assert((T->isOpaque() || V.size() == T->getNumElements()) &&
1040          "Invalid initializer for constant struct");
1041 }
1042 
1043 // ConstantStruct accessors.
1044 Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
1045   assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
1046          "Incorrect # elements specified to ConstantStruct::get");
1047 
1048   // Create a ConstantAggregateZero value if all elements are zeros.
1049   bool isZero = true;
1050   bool isUndef = false;
1051 
1052   if (!V.empty()) {
1053     isUndef = isa<UndefValue>(V[0]);
1054     isZero = V[0]->isNullValue();
1055     if (isUndef || isZero) {
1056       for (unsigned i = 0, e = V.size(); i != e; ++i) {
1057         if (!V[i]->isNullValue())
1058           isZero = false;
1059         if (!isa<UndefValue>(V[i]))
1060           isUndef = false;
1061       }
1062     }
1063   }
1064   if (isZero)
1065     return ConstantAggregateZero::get(ST);
1066   if (isUndef)
1067     return UndefValue::get(ST);
1068 
1069   return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
1070 }
1071 
1072 ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
1073     : ConstantAggregate(T, ConstantVectorVal, V) {
1074   assert(V.size() == T->getNumElements() &&
1075          "Invalid initializer for constant vector");
1076 }
1077 
1078 // ConstantVector accessors.
1079 Constant *ConstantVector::get(ArrayRef<Constant*> V) {
1080   if (Constant *C = getImpl(V))
1081     return C;
1082   VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
1083   return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
1084 }
1085 
1086 Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
1087   assert(!V.empty() && "Vectors can't be empty");
1088   VectorType *T = VectorType::get(V.front()->getType(), V.size());
1089 
1090   // If this is an all-undef or all-zero vector, return a
1091   // ConstantAggregateZero or UndefValue.
1092   Constant *C = V[0];
1093   bool isZero = C->isNullValue();
1094   bool isUndef = isa<UndefValue>(C);
1095 
1096   if (isZero || isUndef) {
1097     for (unsigned i = 1, e = V.size(); i != e; ++i)
1098       if (V[i] != C) {
1099         isZero = isUndef = false;
1100         break;
1101       }
1102   }
1103 
1104   if (isZero)
1105     return ConstantAggregateZero::get(T);
1106   if (isUndef)
1107     return UndefValue::get(T);
1108 
1109   // Check to see if all of the elements are ConstantFP or ConstantInt and if
1110   // the element type is compatible with ConstantDataVector.  If so, use it.
1111   if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1112     return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
1113 
1114   // Otherwise, the element type isn't compatible with ConstantDataVector, or
1115   // the operand list contains a ConstantExpr or something else strange.
1116   return nullptr;
1117 }
1118 
1119 Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
1120   // If this splat is compatible with ConstantDataVector, use it instead of
1121   // ConstantVector.
1122   if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
1123       ConstantDataSequential::isElementTypeCompatible(V->getType()))
1124     return ConstantDataVector::getSplat(NumElts, V);
1125 
1126   SmallVector<Constant*, 32> Elts(NumElts, V);
1127   return get(Elts);
1128 }
1129 
1130 ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
1131   LLVMContextImpl *pImpl = Context.pImpl;
1132   if (!pImpl->TheNoneToken)
1133     pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
1134   return pImpl->TheNoneToken.get();
1135 }
1136 
1137 /// Remove the constant from the constant table.
1138 void ConstantTokenNone::destroyConstantImpl() {
1139   llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1140 }
1141 
1142 // Utility function for determining if a ConstantExpr is a CastOp or not. This
1143 // can't be inline because we don't want to #include Instruction.h into
1144 // Constant.h
1145 bool ConstantExpr::isCast() const {
1146   return Instruction::isCast(getOpcode());
1147 }
1148 
1149 bool ConstantExpr::isCompare() const {
1150   return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
1151 }
1152 
1153 bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
1154   if (getOpcode() != Instruction::GetElementPtr) return false;
1155 
1156   gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
1157   User::const_op_iterator OI = std::next(this->op_begin());
1158 
1159   // The remaining indices may be compile-time known integers within the bounds
1160   // of the corresponding notional static array types.
1161   for (; GEPI != E; ++GEPI, ++OI) {
1162     if (isa<UndefValue>(*OI))
1163       continue;
1164     auto *CI = dyn_cast<ConstantInt>(*OI);
1165     if (!CI || (GEPI.isBoundedSequential() &&
1166                 (CI->getValue().getActiveBits() > 64 ||
1167                  CI->getZExtValue() >= GEPI.getSequentialNumElements())))
1168       return false;
1169   }
1170 
1171   // All the indices checked out.
1172   return true;
1173 }
1174 
1175 bool ConstantExpr::hasIndices() const {
1176   return getOpcode() == Instruction::ExtractValue ||
1177          getOpcode() == Instruction::InsertValue;
1178 }
1179 
1180 ArrayRef<unsigned> ConstantExpr::getIndices() const {
1181   if (const ExtractValueConstantExpr *EVCE =
1182         dyn_cast<ExtractValueConstantExpr>(this))
1183     return EVCE->Indices;
1184 
1185   return cast<InsertValueConstantExpr>(this)->Indices;
1186 }
1187 
1188 unsigned ConstantExpr::getPredicate() const {
1189   return cast<CompareConstantExpr>(this)->predicate;
1190 }
1191 
1192 Constant *
1193 ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
1194   assert(Op->getType() == getOperand(OpNo)->getType() &&
1195          "Replacing operand with value of different type!");
1196   if (getOperand(OpNo) == Op)
1197     return const_cast<ConstantExpr*>(this);
1198 
1199   SmallVector<Constant*, 8> NewOps;
1200   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1201     NewOps.push_back(i == OpNo ? Op : getOperand(i));
1202 
1203   return getWithOperands(NewOps);
1204 }
1205 
1206 Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1207                                         bool OnlyIfReduced, Type *SrcTy) const {
1208   assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
1209 
1210   // If no operands changed return self.
1211   if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
1212     return const_cast<ConstantExpr*>(this);
1213 
1214   Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
1215   switch (getOpcode()) {
1216   case Instruction::Trunc:
1217   case Instruction::ZExt:
1218   case Instruction::SExt:
1219   case Instruction::FPTrunc:
1220   case Instruction::FPExt:
1221   case Instruction::UIToFP:
1222   case Instruction::SIToFP:
1223   case Instruction::FPToUI:
1224   case Instruction::FPToSI:
1225   case Instruction::PtrToInt:
1226   case Instruction::IntToPtr:
1227   case Instruction::BitCast:
1228   case Instruction::AddrSpaceCast:
1229     return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
1230   case Instruction::Select:
1231     return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
1232   case Instruction::InsertElement:
1233     return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
1234                                           OnlyIfReducedTy);
1235   case Instruction::ExtractElement:
1236     return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
1237   case Instruction::InsertValue:
1238     return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
1239                                         OnlyIfReducedTy);
1240   case Instruction::ExtractValue:
1241     return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
1242   case Instruction::ShuffleVector:
1243     return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2],
1244                                           OnlyIfReducedTy);
1245   case Instruction::GetElementPtr: {
1246     auto *GEPO = cast<GEPOperator>(this);
1247     assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1248     return ConstantExpr::getGetElementPtr(
1249         SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
1250         GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy);
1251   }
1252   case Instruction::ICmp:
1253   case Instruction::FCmp:
1254     return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
1255                                     OnlyIfReducedTy);
1256   default:
1257     assert(getNumOperands() == 2 && "Must be binary operator?");
1258     return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
1259                              OnlyIfReducedTy);
1260   }
1261 }
1262 
1263 
1264 //===----------------------------------------------------------------------===//
1265 //                      isValueValidForType implementations
1266 
1267 bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
1268   unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1269   if (Ty->isIntegerTy(1))
1270     return Val == 0 || Val == 1;
1271   return isUIntN(NumBits, Val);
1272 }
1273 
1274 bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
1275   unsigned NumBits = Ty->getIntegerBitWidth();
1276   if (Ty->isIntegerTy(1))
1277     return Val == 0 || Val == 1 || Val == -1;
1278   return isIntN(NumBits, Val);
1279 }
1280 
1281 bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
1282   // convert modifies in place, so make a copy.
1283   APFloat Val2 = APFloat(Val);
1284   bool losesInfo;
1285   switch (Ty->getTypeID()) {
1286   default:
1287     return false;         // These can't be represented as floating point!
1288 
1289   // FIXME rounding mode needs to be more flexible
1290   case Type::HalfTyID: {
1291     if (&Val2.getSemantics() == &APFloat::IEEEhalf())
1292       return true;
1293     Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);
1294     return !losesInfo;
1295   }
1296   case Type::FloatTyID: {
1297     if (&Val2.getSemantics() == &APFloat::IEEEsingle())
1298       return true;
1299     Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1300     return !losesInfo;
1301   }
1302   case Type::DoubleTyID: {
1303     if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
1304         &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1305         &Val2.getSemantics() == &APFloat::IEEEdouble())
1306       return true;
1307     Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1308     return !losesInfo;
1309   }
1310   case Type::X86_FP80TyID:
1311     return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1312            &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1313            &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1314            &Val2.getSemantics() == &APFloat::x87DoubleExtended();
1315   case Type::FP128TyID:
1316     return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1317            &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1318            &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1319            &Val2.getSemantics() == &APFloat::IEEEquad();
1320   case Type::PPC_FP128TyID:
1321     return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1322            &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1323            &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1324            &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
1325   }
1326 }
1327 
1328 
1329 //===----------------------------------------------------------------------===//
1330 //                      Factory Function Implementation
1331 
1332 ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
1333   assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
1334          "Cannot create an aggregate zero of non-aggregate type!");
1335 
1336   std::unique_ptr<ConstantAggregateZero> &Entry =
1337       Ty->getContext().pImpl->CAZConstants[Ty];
1338   if (!Entry)
1339     Entry.reset(new ConstantAggregateZero(Ty));
1340 
1341   return Entry.get();
1342 }
1343 
1344 /// Remove the constant from the constant table.
1345 void ConstantAggregateZero::destroyConstantImpl() {
1346   getContext().pImpl->CAZConstants.erase(getType());
1347 }
1348 
1349 /// Remove the constant from the constant table.
1350 void ConstantArray::destroyConstantImpl() {
1351   getType()->getContext().pImpl->ArrayConstants.remove(this);
1352 }
1353 
1354 
1355 //---- ConstantStruct::get() implementation...
1356 //
1357 
1358 /// Remove the constant from the constant table.
1359 void ConstantStruct::destroyConstantImpl() {
1360   getType()->getContext().pImpl->StructConstants.remove(this);
1361 }
1362 
1363 /// Remove the constant from the constant table.
1364 void ConstantVector::destroyConstantImpl() {
1365   getType()->getContext().pImpl->VectorConstants.remove(this);
1366 }
1367 
1368 Constant *Constant::getSplatValue() const {
1369   assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1370   if (isa<ConstantAggregateZero>(this))
1371     return getNullValue(this->getType()->getVectorElementType());
1372   if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
1373     return CV->getSplatValue();
1374   if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1375     return CV->getSplatValue();
1376   return nullptr;
1377 }
1378 
1379 Constant *ConstantVector::getSplatValue() const {
1380   // Check out first element.
1381   Constant *Elt = getOperand(0);
1382   // Then make sure all remaining elements point to the same value.
1383   for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1384     if (getOperand(I) != Elt)
1385       return nullptr;
1386   return Elt;
1387 }
1388 
1389 const APInt &Constant::getUniqueInteger() const {
1390   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1391     return CI->getValue();
1392   assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1393   const Constant *C = this->getAggregateElement(0U);
1394   assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1395   return cast<ConstantInt>(C)->getValue();
1396 }
1397 
1398 //---- ConstantPointerNull::get() implementation.
1399 //
1400 
1401 ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
1402   std::unique_ptr<ConstantPointerNull> &Entry =
1403       Ty->getContext().pImpl->CPNConstants[Ty];
1404   if (!Entry)
1405     Entry.reset(new ConstantPointerNull(Ty));
1406 
1407   return Entry.get();
1408 }
1409 
1410 /// Remove the constant from the constant table.
1411 void ConstantPointerNull::destroyConstantImpl() {
1412   getContext().pImpl->CPNConstants.erase(getType());
1413 }
1414 
1415 UndefValue *UndefValue::get(Type *Ty) {
1416   std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
1417   if (!Entry)
1418     Entry.reset(new UndefValue(Ty));
1419 
1420   return Entry.get();
1421 }
1422 
1423 /// Remove the constant from the constant table.
1424 void UndefValue::destroyConstantImpl() {
1425   // Free the constant and any dangling references to it.
1426   getContext().pImpl->UVConstants.erase(getType());
1427 }
1428 
1429 BlockAddress *BlockAddress::get(BasicBlock *BB) {
1430   assert(BB->getParent() && "Block must have a parent");
1431   return get(BB->getParent(), BB);
1432 }
1433 
1434 BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1435   BlockAddress *&BA =
1436     F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
1437   if (!BA)
1438     BA = new BlockAddress(F, BB);
1439 
1440   assert(BA->getFunction() == F && "Basic block moved between functions");
1441   return BA;
1442 }
1443 
1444 BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1445 : Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1446            &Op<0>(), 2) {
1447   setOperand(0, F);
1448   setOperand(1, BB);
1449   BB->AdjustBlockAddressRefCount(1);
1450 }
1451 
1452 BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
1453   if (!BB->hasAddressTaken())
1454     return nullptr;
1455 
1456   const Function *F = BB->getParent();
1457   assert(F && "Block must have a parent");
1458   BlockAddress *BA =
1459       F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
1460   assert(BA && "Refcount and block address map disagree!");
1461   return BA;
1462 }
1463 
1464 /// Remove the constant from the constant table.
1465 void BlockAddress::destroyConstantImpl() {
1466   getFunction()->getType()->getContext().pImpl
1467     ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
1468   getBasicBlock()->AdjustBlockAddressRefCount(-1);
1469 }
1470 
1471 Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
1472   // This could be replacing either the Basic Block or the Function.  In either
1473   // case, we have to remove the map entry.
1474   Function *NewF = getFunction();
1475   BasicBlock *NewBB = getBasicBlock();
1476 
1477   if (From == NewF)
1478     NewF = cast<Function>(To->stripPointerCasts());
1479   else {
1480     assert(From == NewBB && "From does not match any operand");
1481     NewBB = cast<BasicBlock>(To);
1482   }
1483 
1484   // See if the 'new' entry already exists, if not, just update this in place
1485   // and return early.
1486   BlockAddress *&NewBA =
1487     getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1488   if (NewBA)
1489     return NewBA;
1490 
1491   getBasicBlock()->AdjustBlockAddressRefCount(-1);
1492 
1493   // Remove the old entry, this can't cause the map to rehash (just a
1494   // tombstone will get added).
1495   getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1496                                                           getBasicBlock()));
1497   NewBA = this;
1498   setOperand(0, NewF);
1499   setOperand(1, NewBB);
1500   getBasicBlock()->AdjustBlockAddressRefCount(1);
1501 
1502   // If we just want to keep the existing value, then return null.
1503   // Callers know that this means we shouldn't delete this value.
1504   return nullptr;
1505 }
1506 
1507 //---- ConstantExpr::get() implementations.
1508 //
1509 
1510 /// This is a utility function to handle folding of casts and lookup of the
1511 /// cast in the ExprConstants map. It is used by the various get* methods below.
1512 static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
1513                                bool OnlyIfReduced = false) {
1514   assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1515   // Fold a few common cases
1516   if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1517     return FC;
1518 
1519   if (OnlyIfReduced)
1520     return nullptr;
1521 
1522   LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1523 
1524   // Look up the constant in the table first to ensure uniqueness.
1525   ConstantExprKeyType Key(opc, C);
1526 
1527   return pImpl->ExprConstants.getOrCreate(Ty, Key);
1528 }
1529 
1530 Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
1531                                 bool OnlyIfReduced) {
1532   Instruction::CastOps opc = Instruction::CastOps(oc);
1533   assert(Instruction::isCast(opc) && "opcode out of range");
1534   assert(C && Ty && "Null arguments to getCast");
1535   assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
1536 
1537   switch (opc) {
1538   default:
1539     llvm_unreachable("Invalid cast opcode");
1540   case Instruction::Trunc:
1541     return getTrunc(C, Ty, OnlyIfReduced);
1542   case Instruction::ZExt:
1543     return getZExt(C, Ty, OnlyIfReduced);
1544   case Instruction::SExt:
1545     return getSExt(C, Ty, OnlyIfReduced);
1546   case Instruction::FPTrunc:
1547     return getFPTrunc(C, Ty, OnlyIfReduced);
1548   case Instruction::FPExt:
1549     return getFPExtend(C, Ty, OnlyIfReduced);
1550   case Instruction::UIToFP:
1551     return getUIToFP(C, Ty, OnlyIfReduced);
1552   case Instruction::SIToFP:
1553     return getSIToFP(C, Ty, OnlyIfReduced);
1554   case Instruction::FPToUI:
1555     return getFPToUI(C, Ty, OnlyIfReduced);
1556   case Instruction::FPToSI:
1557     return getFPToSI(C, Ty, OnlyIfReduced);
1558   case Instruction::PtrToInt:
1559     return getPtrToInt(C, Ty, OnlyIfReduced);
1560   case Instruction::IntToPtr:
1561     return getIntToPtr(C, Ty, OnlyIfReduced);
1562   case Instruction::BitCast:
1563     return getBitCast(C, Ty, OnlyIfReduced);
1564   case Instruction::AddrSpaceCast:
1565     return getAddrSpaceCast(C, Ty, OnlyIfReduced);
1566   }
1567 }
1568 
1569 Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
1570   if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
1571     return getBitCast(C, Ty);
1572   return getZExt(C, Ty);
1573 }
1574 
1575 Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
1576   if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
1577     return getBitCast(C, Ty);
1578   return getSExt(C, Ty);
1579 }
1580 
1581 Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
1582   if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
1583     return getBitCast(C, Ty);
1584   return getTrunc(C, Ty);
1585 }
1586 
1587 Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
1588   assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1589   assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
1590           "Invalid cast");
1591 
1592   if (Ty->isIntOrIntVectorTy())
1593     return getPtrToInt(S, Ty);
1594 
1595   unsigned SrcAS = S->getType()->getPointerAddressSpace();
1596   if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
1597     return getAddrSpaceCast(S, Ty);
1598 
1599   return getBitCast(S, Ty);
1600 }
1601 
1602 Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
1603                                                          Type *Ty) {
1604   assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1605   assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
1606 
1607   if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
1608     return getAddrSpaceCast(S, Ty);
1609 
1610   return getBitCast(S, Ty);
1611 }
1612 
1613 Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) {
1614   assert(C->getType()->isIntOrIntVectorTy() &&
1615          Ty->isIntOrIntVectorTy() && "Invalid cast");
1616   unsigned SrcBits = C->getType()->getScalarSizeInBits();
1617   unsigned DstBits = Ty->getScalarSizeInBits();
1618   Instruction::CastOps opcode =
1619     (SrcBits == DstBits ? Instruction::BitCast :
1620      (SrcBits > DstBits ? Instruction::Trunc :
1621       (isSigned ? Instruction::SExt : Instruction::ZExt)));
1622   return getCast(opcode, C, Ty);
1623 }
1624 
1625 Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
1626   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
1627          "Invalid cast");
1628   unsigned SrcBits = C->getType()->getScalarSizeInBits();
1629   unsigned DstBits = Ty->getScalarSizeInBits();
1630   if (SrcBits == DstBits)
1631     return C; // Avoid a useless cast
1632   Instruction::CastOps opcode =
1633     (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
1634   return getCast(opcode, C, Ty);
1635 }
1636 
1637 Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
1638 #ifndef NDEBUG
1639   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1640   bool toVec = Ty->getTypeID() == Type::VectorTyID;
1641 #endif
1642   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1643   assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1644   assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
1645   assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
1646          "SrcTy must be larger than DestTy for Trunc!");
1647 
1648   return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
1649 }
1650 
1651 Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
1652 #ifndef NDEBUG
1653   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1654   bool toVec = Ty->getTypeID() == Type::VectorTyID;
1655 #endif
1656   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1657   assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1658   assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
1659   assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
1660          "SrcTy must be smaller than DestTy for SExt!");
1661 
1662   return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
1663 }
1664 
1665 Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
1666 #ifndef NDEBUG
1667   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1668   bool toVec = Ty->getTypeID() == Type::VectorTyID;
1669 #endif
1670   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1671   assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1672   assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
1673   assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
1674          "SrcTy must be smaller than DestTy for ZExt!");
1675 
1676   return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
1677 }
1678 
1679 Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
1680 #ifndef NDEBUG
1681   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1682   bool toVec = Ty->getTypeID() == Type::VectorTyID;
1683 #endif
1684   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1685   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
1686          C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
1687          "This is an illegal floating point truncation!");
1688   return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
1689 }
1690 
1691 Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
1692 #ifndef NDEBUG
1693   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1694   bool toVec = Ty->getTypeID() == Type::VectorTyID;
1695 #endif
1696   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1697   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
1698          C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
1699          "This is an illegal floating point extension!");
1700   return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
1701 }
1702 
1703 Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
1704 #ifndef NDEBUG
1705   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1706   bool toVec = Ty->getTypeID() == Type::VectorTyID;
1707 #endif
1708   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1709   assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
1710          "This is an illegal uint to floating point cast!");
1711   return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
1712 }
1713 
1714 Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
1715 #ifndef NDEBUG
1716   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1717   bool toVec = Ty->getTypeID() == Type::VectorTyID;
1718 #endif
1719   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1720   assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
1721          "This is an illegal sint to floating point cast!");
1722   return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
1723 }
1724 
1725 Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
1726 #ifndef NDEBUG
1727   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1728   bool toVec = Ty->getTypeID() == Type::VectorTyID;
1729 #endif
1730   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1731   assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
1732          "This is an illegal floating point to uint cast!");
1733   return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
1734 }
1735 
1736 Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
1737 #ifndef NDEBUG
1738   bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1739   bool toVec = Ty->getTypeID() == Type::VectorTyID;
1740 #endif
1741   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1742   assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
1743          "This is an illegal floating point to sint cast!");
1744   return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
1745 }
1746 
1747 Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
1748                                     bool OnlyIfReduced) {
1749   assert(C->getType()->isPtrOrPtrVectorTy() &&
1750          "PtrToInt source must be pointer or pointer vector");
1751   assert(DstTy->isIntOrIntVectorTy() &&
1752          "PtrToInt destination must be integer or integer vector");
1753   assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
1754   if (isa<VectorType>(C->getType()))
1755     assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
1756            "Invalid cast between a different number of vector elements");
1757   return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
1758 }
1759 
1760 Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
1761                                     bool OnlyIfReduced) {
1762   assert(C->getType()->isIntOrIntVectorTy() &&
1763          "IntToPtr source must be integer or integer vector");
1764   assert(DstTy->isPtrOrPtrVectorTy() &&
1765          "IntToPtr destination must be a pointer or pointer vector");
1766   assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
1767   if (isa<VectorType>(C->getType()))
1768     assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
1769            "Invalid cast between a different number of vector elements");
1770   return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
1771 }
1772 
1773 Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
1774                                    bool OnlyIfReduced) {
1775   assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1776          "Invalid constantexpr bitcast!");
1777 
1778   // It is common to ask for a bitcast of a value to its own type, handle this
1779   // speedily.
1780   if (C->getType() == DstTy) return C;
1781 
1782   return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
1783 }
1784 
1785 Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
1786                                          bool OnlyIfReduced) {
1787   assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
1788          "Invalid constantexpr addrspacecast!");
1789 
1790   // Canonicalize addrspacecasts between different pointer types by first
1791   // bitcasting the pointer type and then converting the address space.
1792   PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
1793   PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
1794   Type *DstElemTy = DstScalarTy->getElementType();
1795   if (SrcScalarTy->getElementType() != DstElemTy) {
1796     Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
1797     if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
1798       // Handle vectors of pointers.
1799       MidTy = VectorType::get(MidTy, VT->getNumElements());
1800     }
1801     C = getBitCast(C, MidTy);
1802   }
1803   return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
1804 }
1805 
1806 Constant *ConstantExpr::get(unsigned Opcode, Constant *C, unsigned Flags,
1807                             Type *OnlyIfReducedTy) {
1808   // Check the operands for consistency first.
1809   assert(Instruction::isUnaryOp(Opcode) &&
1810          "Invalid opcode in unary constant expression");
1811 
1812 #ifndef NDEBUG
1813   switch (Opcode) {
1814   case Instruction::FNeg:
1815     assert(C->getType()->isFPOrFPVectorTy() &&
1816            "Tried to create a floating-point operation on a "
1817            "non-floating-point type!");
1818     break;
1819   default:
1820     break;
1821   }
1822 #endif
1823 
1824   // TODO: Try to constant fold operation.
1825 
1826   if (OnlyIfReducedTy == C->getType())
1827     return nullptr;
1828 
1829   Constant *ArgVec[] = { C };
1830   ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
1831 
1832   LLVMContextImpl *pImpl = C->getContext().pImpl;
1833   return pImpl->ExprConstants.getOrCreate(C->getType(), Key);
1834 }
1835 
1836 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
1837                             unsigned Flags, Type *OnlyIfReducedTy) {
1838   // Check the operands for consistency first.
1839   assert(Instruction::isBinaryOp(Opcode) &&
1840          "Invalid opcode in binary constant expression");
1841   assert(C1->getType() == C2->getType() &&
1842          "Operand types in binary constant expression should match");
1843 
1844 #ifndef NDEBUG
1845   switch (Opcode) {
1846   case Instruction::Add:
1847   case Instruction::Sub:
1848   case Instruction::Mul:
1849     assert(C1->getType() == C2->getType() && "Op types should be identical!");
1850     assert(C1->getType()->isIntOrIntVectorTy() &&
1851            "Tried to create an integer operation on a non-integer type!");
1852     break;
1853   case Instruction::FAdd:
1854   case Instruction::FSub:
1855   case Instruction::FMul:
1856     assert(C1->getType() == C2->getType() && "Op types should be identical!");
1857     assert(C1->getType()->isFPOrFPVectorTy() &&
1858            "Tried to create a floating-point operation on a "
1859            "non-floating-point type!");
1860     break;
1861   case Instruction::UDiv:
1862   case Instruction::SDiv:
1863     assert(C1->getType() == C2->getType() && "Op types should be identical!");
1864     assert(C1->getType()->isIntOrIntVectorTy() &&
1865            "Tried to create an arithmetic operation on a non-arithmetic type!");
1866     break;
1867   case Instruction::FDiv:
1868     assert(C1->getType() == C2->getType() && "Op types should be identical!");
1869     assert(C1->getType()->isFPOrFPVectorTy() &&
1870            "Tried to create an arithmetic operation on a non-arithmetic type!");
1871     break;
1872   case Instruction::URem:
1873   case Instruction::SRem:
1874     assert(C1->getType() == C2->getType() && "Op types should be identical!");
1875     assert(C1->getType()->isIntOrIntVectorTy() &&
1876            "Tried to create an arithmetic operation on a non-arithmetic type!");
1877     break;
1878   case Instruction::FRem:
1879     assert(C1->getType() == C2->getType() && "Op types should be identical!");
1880     assert(C1->getType()->isFPOrFPVectorTy() &&
1881            "Tried to create an arithmetic operation on a non-arithmetic type!");
1882     break;
1883   case Instruction::And:
1884   case Instruction::Or:
1885   case Instruction::Xor:
1886     assert(C1->getType() == C2->getType() && "Op types should be identical!");
1887     assert(C1->getType()->isIntOrIntVectorTy() &&
1888            "Tried to create a logical operation on a non-integral type!");
1889     break;
1890   case Instruction::Shl:
1891   case Instruction::LShr:
1892   case Instruction::AShr:
1893     assert(C1->getType() == C2->getType() && "Op types should be identical!");
1894     assert(C1->getType()->isIntOrIntVectorTy() &&
1895            "Tried to create a shift operation on a non-integer type!");
1896     break;
1897   default:
1898     break;
1899   }
1900 #endif
1901 
1902   if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1903     return FC;          // Fold a few common cases.
1904 
1905   if (OnlyIfReducedTy == C1->getType())
1906     return nullptr;
1907 
1908   Constant *ArgVec[] = { C1, C2 };
1909   ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
1910 
1911   LLVMContextImpl *pImpl = C1->getContext().pImpl;
1912   return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
1913 }
1914 
1915 Constant *ConstantExpr::getSizeOf(Type* Ty) {
1916   // sizeof is implemented as: (i64) gep (Ty*)null, 1
1917   // Note that a non-inbounds gep is used, as null isn't within any object.
1918   Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1919   Constant *GEP = getGetElementPtr(
1920       Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1921   return getPtrToInt(GEP,
1922                      Type::getInt64Ty(Ty->getContext()));
1923 }
1924 
1925 Constant *ConstantExpr::getAlignOf(Type* Ty) {
1926   // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
1927   // Note that a non-inbounds gep is used, as null isn't within any object.
1928   Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
1929   Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
1930   Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
1931   Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1932   Constant *Indices[2] = { Zero, One };
1933   Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
1934   return getPtrToInt(GEP,
1935                      Type::getInt64Ty(Ty->getContext()));
1936 }
1937 
1938 Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
1939   return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1940                                            FieldNo));
1941 }
1942 
1943 Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
1944   // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1945   // Note that a non-inbounds gep is used, as null isn't within any object.
1946   Constant *GEPIdx[] = {
1947     ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1948     FieldNo
1949   };
1950   Constant *GEP = getGetElementPtr(
1951       Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1952   return getPtrToInt(GEP,
1953                      Type::getInt64Ty(Ty->getContext()));
1954 }
1955 
1956 Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
1957                                    Constant *C2, bool OnlyIfReduced) {
1958   assert(C1->getType() == C2->getType() && "Op types should be identical!");
1959 
1960   switch (Predicate) {
1961   default: llvm_unreachable("Invalid CmpInst predicate");
1962   case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1963   case CmpInst::FCMP_OGE:   case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1964   case CmpInst::FCMP_ONE:   case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1965   case CmpInst::FCMP_UEQ:   case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1966   case CmpInst::FCMP_ULT:   case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1967   case CmpInst::FCMP_TRUE:
1968     return getFCmp(Predicate, C1, C2, OnlyIfReduced);
1969 
1970   case CmpInst::ICMP_EQ:  case CmpInst::ICMP_NE:  case CmpInst::ICMP_UGT:
1971   case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1972   case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1973   case CmpInst::ICMP_SLE:
1974     return getICmp(Predicate, C1, C2, OnlyIfReduced);
1975   }
1976 }
1977 
1978 Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
1979                                   Type *OnlyIfReducedTy) {
1980   assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
1981 
1982   if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1983     return SC;        // Fold common cases
1984 
1985   if (OnlyIfReducedTy == V1->getType())
1986     return nullptr;
1987 
1988   Constant *ArgVec[] = { C, V1, V2 };
1989   ConstantExprKeyType Key(Instruction::Select, ArgVec);
1990 
1991   LLVMContextImpl *pImpl = C->getContext().pImpl;
1992   return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
1993 }
1994 
1995 Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
1996                                          ArrayRef<Value *> Idxs, bool InBounds,
1997                                          Optional<unsigned> InRangeIndex,
1998                                          Type *OnlyIfReducedTy) {
1999   if (!Ty)
2000     Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType();
2001   else
2002     assert(
2003         Ty ==
2004         cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u));
2005 
2006   if (Constant *FC =
2007           ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
2008     return FC;          // Fold a few common cases.
2009 
2010   // Get the result type of the getelementptr!
2011   Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
2012   assert(DestTy && "GEP indices invalid!");
2013   unsigned AS = C->getType()->getPointerAddressSpace();
2014   Type *ReqTy = DestTy->getPointerTo(AS);
2015 
2016   unsigned NumVecElts = 0;
2017   if (C->getType()->isVectorTy())
2018     NumVecElts = C->getType()->getVectorNumElements();
2019   else for (auto Idx : Idxs)
2020     if (Idx->getType()->isVectorTy())
2021       NumVecElts = Idx->getType()->getVectorNumElements();
2022 
2023   if (NumVecElts)
2024     ReqTy = VectorType::get(ReqTy, NumVecElts);
2025 
2026   if (OnlyIfReducedTy == ReqTy)
2027     return nullptr;
2028 
2029   // Look up the constant in the table first to ensure uniqueness
2030   std::vector<Constant*> ArgVec;
2031   ArgVec.reserve(1 + Idxs.size());
2032   ArgVec.push_back(C);
2033   for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
2034     assert((!Idxs[i]->getType()->isVectorTy() ||
2035             Idxs[i]->getType()->getVectorNumElements() == NumVecElts) &&
2036            "getelementptr index type missmatch");
2037 
2038     Constant *Idx = cast<Constant>(Idxs[i]);
2039     if (NumVecElts && !Idxs[i]->getType()->isVectorTy())
2040       Idx = ConstantVector::getSplat(NumVecElts, Idx);
2041     ArgVec.push_back(Idx);
2042   }
2043 
2044   unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
2045   if (InRangeIndex && *InRangeIndex < 63)
2046     SubClassOptionalData |= (*InRangeIndex + 1) << 1;
2047   const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
2048                                 SubClassOptionalData, None, Ty);
2049 
2050   LLVMContextImpl *pImpl = C->getContext().pImpl;
2051   return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2052 }
2053 
2054 Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
2055                                 Constant *RHS, bool OnlyIfReduced) {
2056   assert(LHS->getType() == RHS->getType());
2057   assert(CmpInst::isIntPredicate((CmpInst::Predicate)pred) &&
2058          "Invalid ICmp Predicate");
2059 
2060   if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
2061     return FC;          // Fold a few common cases...
2062 
2063   if (OnlyIfReduced)
2064     return nullptr;
2065 
2066   // Look up the constant in the table first to ensure uniqueness
2067   Constant *ArgVec[] = { LHS, RHS };
2068   // Get the key type with both the opcode and predicate
2069   const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
2070 
2071   Type *ResultTy = Type::getInt1Ty(LHS->getContext());
2072   if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
2073     ResultTy = VectorType::get(ResultTy, VT->getNumElements());
2074 
2075   LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
2076   return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
2077 }
2078 
2079 Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
2080                                 Constant *RHS, bool OnlyIfReduced) {
2081   assert(LHS->getType() == RHS->getType());
2082   assert(CmpInst::isFPPredicate((CmpInst::Predicate)pred) &&
2083          "Invalid FCmp Predicate");
2084 
2085   if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
2086     return FC;          // Fold a few common cases...
2087 
2088   if (OnlyIfReduced)
2089     return nullptr;
2090 
2091   // Look up the constant in the table first to ensure uniqueness
2092   Constant *ArgVec[] = { LHS, RHS };
2093   // Get the key type with both the opcode and predicate
2094   const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
2095 
2096   Type *ResultTy = Type::getInt1Ty(LHS->getContext());
2097   if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
2098     ResultTy = VectorType::get(ResultTy, VT->getNumElements());
2099 
2100   LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
2101   return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
2102 }
2103 
2104 Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
2105                                           Type *OnlyIfReducedTy) {
2106   assert(Val->getType()->isVectorTy() &&
2107          "Tried to create extractelement operation on non-vector type!");
2108   assert(Idx->getType()->isIntegerTy() &&
2109          "Extractelement index must be an integer type!");
2110 
2111   if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2112     return FC;          // Fold a few common cases.
2113 
2114   Type *ReqTy = Val->getType()->getVectorElementType();
2115   if (OnlyIfReducedTy == ReqTy)
2116     return nullptr;
2117 
2118   // Look up the constant in the table first to ensure uniqueness
2119   Constant *ArgVec[] = { Val, Idx };
2120   const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
2121 
2122   LLVMContextImpl *pImpl = Val->getContext().pImpl;
2123   return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2124 }
2125 
2126 Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2127                                          Constant *Idx, Type *OnlyIfReducedTy) {
2128   assert(Val->getType()->isVectorTy() &&
2129          "Tried to create insertelement operation on non-vector type!");
2130   assert(Elt->getType() == Val->getType()->getVectorElementType() &&
2131          "Insertelement types must match!");
2132   assert(Idx->getType()->isIntegerTy() &&
2133          "Insertelement index must be i32 type!");
2134 
2135   if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2136     return FC;          // Fold a few common cases.
2137 
2138   if (OnlyIfReducedTy == Val->getType())
2139     return nullptr;
2140 
2141   // Look up the constant in the table first to ensure uniqueness
2142   Constant *ArgVec[] = { Val, Elt, Idx };
2143   const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
2144 
2145   LLVMContextImpl *pImpl = Val->getContext().pImpl;
2146   return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
2147 }
2148 
2149 Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2150                                          Constant *Mask, Type *OnlyIfReducedTy) {
2151   assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2152          "Invalid shuffle vector constant expr operands!");
2153 
2154   if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2155     return FC;          // Fold a few common cases.
2156 
2157   unsigned NElts = Mask->getType()->getVectorNumElements();
2158   Type *EltTy = V1->getType()->getVectorElementType();
2159   Type *ShufTy = VectorType::get(EltTy, NElts);
2160 
2161   if (OnlyIfReducedTy == ShufTy)
2162     return nullptr;
2163 
2164   // Look up the constant in the table first to ensure uniqueness
2165   Constant *ArgVec[] = { V1, V2, Mask };
2166   const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
2167 
2168   LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2169   return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
2170 }
2171 
2172 Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
2173                                        ArrayRef<unsigned> Idxs,
2174                                        Type *OnlyIfReducedTy) {
2175   assert(Agg->getType()->isFirstClassType() &&
2176          "Non-first-class type for constant insertvalue expression");
2177 
2178   assert(ExtractValueInst::getIndexedType(Agg->getType(),
2179                                           Idxs) == Val->getType() &&
2180          "insertvalue indices invalid!");
2181   Type *ReqTy = Val->getType();
2182 
2183   if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2184     return FC;
2185 
2186   if (OnlyIfReducedTy == ReqTy)
2187     return nullptr;
2188 
2189   Constant *ArgVec[] = { Agg, Val };
2190   const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
2191 
2192   LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2193   return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2194 }
2195 
2196 Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2197                                         Type *OnlyIfReducedTy) {
2198   assert(Agg->getType()->isFirstClassType() &&
2199          "Tried to create extractelement operation on non-first-class type!");
2200 
2201   Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
2202   (void)ReqTy;
2203   assert(ReqTy && "extractvalue indices invalid!");
2204 
2205   assert(Agg->getType()->isFirstClassType() &&
2206          "Non-first-class type for constant extractvalue expression");
2207   if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2208     return FC;
2209 
2210   if (OnlyIfReducedTy == ReqTy)
2211     return nullptr;
2212 
2213   Constant *ArgVec[] = { Agg };
2214   const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
2215 
2216   LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2217   return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2218 }
2219 
2220 Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
2221   assert(C->getType()->isIntOrIntVectorTy() &&
2222          "Cannot NEG a nonintegral value!");
2223   return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
2224                 C, HasNUW, HasNSW);
2225 }
2226 
2227 Constant *ConstantExpr::getFNeg(Constant *C) {
2228   assert(C->getType()->isFPOrFPVectorTy() &&
2229          "Cannot FNEG a non-floating-point value!");
2230   return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
2231 }
2232 
2233 Constant *ConstantExpr::getNot(Constant *C) {
2234   assert(C->getType()->isIntOrIntVectorTy() &&
2235          "Cannot NOT a nonintegral value!");
2236   return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
2237 }
2238 
2239 Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2240                                bool HasNUW, bool HasNSW) {
2241   unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2242                    (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
2243   return get(Instruction::Add, C1, C2, Flags);
2244 }
2245 
2246 Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
2247   return get(Instruction::FAdd, C1, C2);
2248 }
2249 
2250 Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2251                                bool HasNUW, bool HasNSW) {
2252   unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2253                    (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
2254   return get(Instruction::Sub, C1, C2, Flags);
2255 }
2256 
2257 Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
2258   return get(Instruction::FSub, C1, C2);
2259 }
2260 
2261 Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2262                                bool HasNUW, bool HasNSW) {
2263   unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2264                    (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
2265   return get(Instruction::Mul, C1, C2, Flags);
2266 }
2267 
2268 Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
2269   return get(Instruction::FMul, C1, C2);
2270 }
2271 
2272 Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2273   return get(Instruction::UDiv, C1, C2,
2274              isExact ? PossiblyExactOperator::IsExact : 0);
2275 }
2276 
2277 Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2278   return get(Instruction::SDiv, C1, C2,
2279              isExact ? PossiblyExactOperator::IsExact : 0);
2280 }
2281 
2282 Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
2283   return get(Instruction::FDiv, C1, C2);
2284 }
2285 
2286 Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
2287   return get(Instruction::URem, C1, C2);
2288 }
2289 
2290 Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
2291   return get(Instruction::SRem, C1, C2);
2292 }
2293 
2294 Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
2295   return get(Instruction::FRem, C1, C2);
2296 }
2297 
2298 Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
2299   return get(Instruction::And, C1, C2);
2300 }
2301 
2302 Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
2303   return get(Instruction::Or, C1, C2);
2304 }
2305 
2306 Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
2307   return get(Instruction::Xor, C1, C2);
2308 }
2309 
2310 Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2311                                bool HasNUW, bool HasNSW) {
2312   unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2313                    (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
2314   return get(Instruction::Shl, C1, C2, Flags);
2315 }
2316 
2317 Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2318   return get(Instruction::LShr, C1, C2,
2319              isExact ? PossiblyExactOperator::IsExact : 0);
2320 }
2321 
2322 Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2323   return get(Instruction::AShr, C1, C2,
2324              isExact ? PossiblyExactOperator::IsExact : 0);
2325 }
2326 
2327 Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty,
2328                                          bool AllowRHSConstant) {
2329   assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed");
2330 
2331   // Commutative opcodes: it does not matter if AllowRHSConstant is set.
2332   if (Instruction::isCommutative(Opcode)) {
2333     switch (Opcode) {
2334       case Instruction::Add: // X + 0 = X
2335       case Instruction::Or:  // X | 0 = X
2336       case Instruction::Xor: // X ^ 0 = X
2337         return Constant::getNullValue(Ty);
2338       case Instruction::Mul: // X * 1 = X
2339         return ConstantInt::get(Ty, 1);
2340       case Instruction::And: // X & -1 = X
2341         return Constant::getAllOnesValue(Ty);
2342       case Instruction::FAdd: // X + -0.0 = X
2343         // TODO: If the fadd has 'nsz', should we return +0.0?
2344         return ConstantFP::getNegativeZero(Ty);
2345       case Instruction::FMul: // X * 1.0 = X
2346         return ConstantFP::get(Ty, 1.0);
2347       default:
2348         llvm_unreachable("Every commutative binop has an identity constant");
2349     }
2350   }
2351 
2352   // Non-commutative opcodes: AllowRHSConstant must be set.
2353   if (!AllowRHSConstant)
2354     return nullptr;
2355 
2356   switch (Opcode) {
2357     case Instruction::Sub:  // X - 0 = X
2358     case Instruction::Shl:  // X << 0 = X
2359     case Instruction::LShr: // X >>u 0 = X
2360     case Instruction::AShr: // X >> 0 = X
2361     case Instruction::FSub: // X - 0.0 = X
2362       return Constant::getNullValue(Ty);
2363     case Instruction::SDiv: // X / 1 = X
2364     case Instruction::UDiv: // X /u 1 = X
2365       return ConstantInt::get(Ty, 1);
2366     case Instruction::FDiv: // X / 1.0 = X
2367       return ConstantFP::get(Ty, 1.0);
2368     default:
2369       return nullptr;
2370   }
2371 }
2372 
2373 Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2374   switch (Opcode) {
2375   default:
2376     // Doesn't have an absorber.
2377     return nullptr;
2378 
2379   case Instruction::Or:
2380     return Constant::getAllOnesValue(Ty);
2381 
2382   case Instruction::And:
2383   case Instruction::Mul:
2384     return Constant::getNullValue(Ty);
2385   }
2386 }
2387 
2388 /// Remove the constant from the constant table.
2389 void ConstantExpr::destroyConstantImpl() {
2390   getType()->getContext().pImpl->ExprConstants.remove(this);
2391 }
2392 
2393 const char *ConstantExpr::getOpcodeName() const {
2394   return Instruction::getOpcodeName(getOpcode());
2395 }
2396 
2397 GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2398     Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2399     : ConstantExpr(DestTy, Instruction::GetElementPtr,
2400                    OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
2401                        (IdxList.size() + 1),
2402                    IdxList.size() + 1),
2403       SrcElementTy(SrcElementTy),
2404       ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
2405   Op<0>() = C;
2406   Use *OperandList = getOperandList();
2407   for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2408     OperandList[i+1] = IdxList[i];
2409 }
2410 
2411 Type *GetElementPtrConstantExpr::getSourceElementType() const {
2412   return SrcElementTy;
2413 }
2414 
2415 Type *GetElementPtrConstantExpr::getResultElementType() const {
2416   return ResElementTy;
2417 }
2418 
2419 //===----------------------------------------------------------------------===//
2420 //                       ConstantData* implementations
2421 
2422 Type *ConstantDataSequential::getElementType() const {
2423   return getType()->getElementType();
2424 }
2425 
2426 StringRef ConstantDataSequential::getRawDataValues() const {
2427   return StringRef(DataElements, getNumElements()*getElementByteSize());
2428 }
2429 
2430 bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
2431   if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
2432   if (auto *IT = dyn_cast<IntegerType>(Ty)) {
2433     switch (IT->getBitWidth()) {
2434     case 8:
2435     case 16:
2436     case 32:
2437     case 64:
2438       return true;
2439     default: break;
2440     }
2441   }
2442   return false;
2443 }
2444 
2445 unsigned ConstantDataSequential::getNumElements() const {
2446   if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
2447     return AT->getNumElements();
2448   return getType()->getVectorNumElements();
2449 }
2450 
2451 
2452 uint64_t ConstantDataSequential::getElementByteSize() const {
2453   return getElementType()->getPrimitiveSizeInBits()/8;
2454 }
2455 
2456 /// Return the start of the specified element.
2457 const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
2458   assert(Elt < getNumElements() && "Invalid Elt");
2459   return DataElements+Elt*getElementByteSize();
2460 }
2461 
2462 
2463 /// Return true if the array is empty or all zeros.
2464 static bool isAllZeros(StringRef Arr) {
2465   for (char I : Arr)
2466     if (I != 0)
2467       return false;
2468   return true;
2469 }
2470 
2471 /// This is the underlying implementation of all of the
2472 /// ConstantDataSequential::get methods.  They all thunk down to here, providing
2473 /// the correct element type.  We take the bytes in as a StringRef because
2474 /// we *want* an underlying "char*" to avoid TBAA type punning violations.
2475 Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
2476   assert(isElementTypeCompatible(Ty->getSequentialElementType()));
2477   // If the elements are all zero or there are no elements, return a CAZ, which
2478   // is more dense and canonical.
2479   if (isAllZeros(Elements))
2480     return ConstantAggregateZero::get(Ty);
2481 
2482   // Do a lookup to see if we have already formed one of these.
2483   auto &Slot =
2484       *Ty->getContext()
2485            .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
2486            .first;
2487 
2488   // The bucket can point to a linked list of different CDS's that have the same
2489   // body but different types.  For example, 0,0,0,1 could be a 4 element array
2490   // of i8, or a 1-element array of i32.  They'll both end up in the same
2491   /// StringMap bucket, linked up by their Next pointers.  Walk the list.
2492   ConstantDataSequential **Entry = &Slot.second;
2493   for (ConstantDataSequential *Node = *Entry; Node;
2494        Entry = &Node->Next, Node = *Entry)
2495     if (Node->getType() == Ty)
2496       return Node;
2497 
2498   // Okay, we didn't get a hit.  Create a node of the right class, link it in,
2499   // and return it.
2500   if (isa<ArrayType>(Ty))
2501     return *Entry = new ConstantDataArray(Ty, Slot.first().data());
2502 
2503   assert(isa<VectorType>(Ty));
2504   return *Entry = new ConstantDataVector(Ty, Slot.first().data());
2505 }
2506 
2507 void ConstantDataSequential::destroyConstantImpl() {
2508   // Remove the constant from the StringMap.
2509   StringMap<ConstantDataSequential*> &CDSConstants =
2510     getType()->getContext().pImpl->CDSConstants;
2511 
2512   StringMap<ConstantDataSequential*>::iterator Slot =
2513     CDSConstants.find(getRawDataValues());
2514 
2515   assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
2516 
2517   ConstantDataSequential **Entry = &Slot->getValue();
2518 
2519   // Remove the entry from the hash table.
2520   if (!(*Entry)->Next) {
2521     // If there is only one value in the bucket (common case) it must be this
2522     // entry, and removing the entry should remove the bucket completely.
2523     assert((*Entry) == this && "Hash mismatch in ConstantDataSequential");
2524     getContext().pImpl->CDSConstants.erase(Slot);
2525   } else {
2526     // Otherwise, there are multiple entries linked off the bucket, unlink the
2527     // node we care about but keep the bucket around.
2528     for (ConstantDataSequential *Node = *Entry; ;
2529          Entry = &Node->Next, Node = *Entry) {
2530       assert(Node && "Didn't find entry in its uniquing hash table!");
2531       // If we found our entry, unlink it from the list and we're done.
2532       if (Node == this) {
2533         *Entry = Node->Next;
2534         break;
2535       }
2536     }
2537   }
2538 
2539   // If we were part of a list, make sure that we don't delete the list that is
2540   // still owned by the uniquing map.
2541   Next = nullptr;
2542 }
2543 
2544 /// getFP() constructors - Return a constant with array type with an element
2545 /// count and element type of float with precision matching the number of
2546 /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2547 /// double for 64bits) Note that this can return a ConstantAggregateZero
2548 /// object.
2549 Constant *ConstantDataArray::getFP(LLVMContext &Context,
2550                                    ArrayRef<uint16_t> Elts) {
2551   Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size());
2552   const char *Data = reinterpret_cast<const char *>(Elts.data());
2553   return getImpl(StringRef(Data, Elts.size() * 2), Ty);
2554 }
2555 Constant *ConstantDataArray::getFP(LLVMContext &Context,
2556                                    ArrayRef<uint32_t> Elts) {
2557   Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
2558   const char *Data = reinterpret_cast<const char *>(Elts.data());
2559   return getImpl(StringRef(Data, Elts.size() * 4), Ty);
2560 }
2561 Constant *ConstantDataArray::getFP(LLVMContext &Context,
2562                                    ArrayRef<uint64_t> Elts) {
2563   Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
2564   const char *Data = reinterpret_cast<const char *>(Elts.data());
2565   return getImpl(StringRef(Data, Elts.size() * 8), Ty);
2566 }
2567 
2568 Constant *ConstantDataArray::getString(LLVMContext &Context,
2569                                        StringRef Str, bool AddNull) {
2570   if (!AddNull) {
2571     const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data());
2572     return get(Context, makeArrayRef(Data, Str.size()));
2573   }
2574 
2575   SmallVector<uint8_t, 64> ElementVals;
2576   ElementVals.append(Str.begin(), Str.end());
2577   ElementVals.push_back(0);
2578   return get(Context, ElementVals);
2579 }
2580 
2581 /// get() constructors - Return a constant with vector type with an element
2582 /// count and element type matching the ArrayRef passed in.  Note that this
2583 /// can return a ConstantAggregateZero object.
2584 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
2585   Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size());
2586   const char *Data = reinterpret_cast<const char *>(Elts.data());
2587   return getImpl(StringRef(Data, Elts.size() * 1), Ty);
2588 }
2589 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
2590   Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size());
2591   const char *Data = reinterpret_cast<const char *>(Elts.data());
2592   return getImpl(StringRef(Data, Elts.size() * 2), Ty);
2593 }
2594 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
2595   Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size());
2596   const char *Data = reinterpret_cast<const char *>(Elts.data());
2597   return getImpl(StringRef(Data, Elts.size() * 4), Ty);
2598 }
2599 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
2600   Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size());
2601   const char *Data = reinterpret_cast<const char *>(Elts.data());
2602   return getImpl(StringRef(Data, Elts.size() * 8), Ty);
2603 }
2604 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
2605   Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2606   const char *Data = reinterpret_cast<const char *>(Elts.data());
2607   return getImpl(StringRef(Data, Elts.size() * 4), Ty);
2608 }
2609 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
2610   Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2611   const char *Data = reinterpret_cast<const char *>(Elts.data());
2612   return getImpl(StringRef(Data, Elts.size() * 8), Ty);
2613 }
2614 
2615 /// getFP() constructors - Return a constant with vector type with an element
2616 /// count and element type of float with the precision matching the number of
2617 /// bits in the ArrayRef passed in.  (i.e. half for 16bits, float for 32bits,
2618 /// double for 64bits) Note that this can return a ConstantAggregateZero
2619 /// object.
2620 Constant *ConstantDataVector::getFP(LLVMContext &Context,
2621                                     ArrayRef<uint16_t> Elts) {
2622   Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size());
2623   const char *Data = reinterpret_cast<const char *>(Elts.data());
2624   return getImpl(StringRef(Data, Elts.size() * 2), Ty);
2625 }
2626 Constant *ConstantDataVector::getFP(LLVMContext &Context,
2627                                     ArrayRef<uint32_t> Elts) {
2628   Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2629   const char *Data = reinterpret_cast<const char *>(Elts.data());
2630   return getImpl(StringRef(Data, Elts.size() * 4), Ty);
2631 }
2632 Constant *ConstantDataVector::getFP(LLVMContext &Context,
2633                                     ArrayRef<uint64_t> Elts) {
2634   Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2635   const char *Data = reinterpret_cast<const char *>(Elts.data());
2636   return getImpl(StringRef(Data, Elts.size() * 8), Ty);
2637 }
2638 
2639 Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2640   assert(isElementTypeCompatible(V->getType()) &&
2641          "Element type not compatible with ConstantData");
2642   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2643     if (CI->getType()->isIntegerTy(8)) {
2644       SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
2645       return get(V->getContext(), Elts);
2646     }
2647     if (CI->getType()->isIntegerTy(16)) {
2648       SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
2649       return get(V->getContext(), Elts);
2650     }
2651     if (CI->getType()->isIntegerTy(32)) {
2652       SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
2653       return get(V->getContext(), Elts);
2654     }
2655     assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
2656     SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
2657     return get(V->getContext(), Elts);
2658   }
2659 
2660   if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
2661     if (CFP->getType()->isHalfTy()) {
2662       SmallVector<uint16_t, 16> Elts(
2663           NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2664       return getFP(V->getContext(), Elts);
2665     }
2666     if (CFP->getType()->isFloatTy()) {
2667       SmallVector<uint32_t, 16> Elts(
2668           NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2669       return getFP(V->getContext(), Elts);
2670     }
2671     if (CFP->getType()->isDoubleTy()) {
2672       SmallVector<uint64_t, 16> Elts(
2673           NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2674       return getFP(V->getContext(), Elts);
2675     }
2676   }
2677   return ConstantVector::getSplat(NumElts, V);
2678 }
2679 
2680 
2681 uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
2682   assert(isa<IntegerType>(getElementType()) &&
2683          "Accessor can only be used when element is an integer");
2684   const char *EltPtr = getElementPointer(Elt);
2685 
2686   // The data is stored in host byte order, make sure to cast back to the right
2687   // type to load with the right endianness.
2688   switch (getElementType()->getIntegerBitWidth()) {
2689   default: llvm_unreachable("Invalid bitwidth for CDS");
2690   case 8:
2691     return *reinterpret_cast<const uint8_t *>(EltPtr);
2692   case 16:
2693     return *reinterpret_cast<const uint16_t *>(EltPtr);
2694   case 32:
2695     return *reinterpret_cast<const uint32_t *>(EltPtr);
2696   case 64:
2697     return *reinterpret_cast<const uint64_t *>(EltPtr);
2698   }
2699 }
2700 
2701 APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
2702   assert(isa<IntegerType>(getElementType()) &&
2703          "Accessor can only be used when element is an integer");
2704   const char *EltPtr = getElementPointer(Elt);
2705 
2706   // The data is stored in host byte order, make sure to cast back to the right
2707   // type to load with the right endianness.
2708   switch (getElementType()->getIntegerBitWidth()) {
2709   default: llvm_unreachable("Invalid bitwidth for CDS");
2710   case 8: {
2711     auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
2712     return APInt(8, EltVal);
2713   }
2714   case 16: {
2715     auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
2716     return APInt(16, EltVal);
2717   }
2718   case 32: {
2719     auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
2720     return APInt(32, EltVal);
2721   }
2722   case 64: {
2723     auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
2724     return APInt(64, EltVal);
2725   }
2726   }
2727 }
2728 
2729 APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
2730   const char *EltPtr = getElementPointer(Elt);
2731 
2732   switch (getElementType()->getTypeID()) {
2733   default:
2734     llvm_unreachable("Accessor can only be used when element is float/double!");
2735   case Type::HalfTyID: {
2736     auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
2737     return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
2738   }
2739   case Type::FloatTyID: {
2740     auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
2741     return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
2742   }
2743   case Type::DoubleTyID: {
2744     auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
2745     return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
2746   }
2747   }
2748 }
2749 
2750 float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
2751   assert(getElementType()->isFloatTy() &&
2752          "Accessor can only be used when element is a 'float'");
2753   return *reinterpret_cast<const float *>(getElementPointer(Elt));
2754 }
2755 
2756 double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
2757   assert(getElementType()->isDoubleTy() &&
2758          "Accessor can only be used when element is a 'float'");
2759   return *reinterpret_cast<const double *>(getElementPointer(Elt));
2760 }
2761 
2762 Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
2763   if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
2764       getElementType()->isDoubleTy())
2765     return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
2766 
2767   return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
2768 }
2769 
2770 bool ConstantDataSequential::isString(unsigned CharSize) const {
2771   return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
2772 }
2773 
2774 bool ConstantDataSequential::isCString() const {
2775   if (!isString())
2776     return false;
2777 
2778   StringRef Str = getAsString();
2779 
2780   // The last value must be nul.
2781   if (Str.back() != 0) return false;
2782 
2783   // Other elements must be non-nul.
2784   return Str.drop_back().find(0) == StringRef::npos;
2785 }
2786 
2787 bool ConstantDataVector::isSplat() const {
2788   const char *Base = getRawDataValues().data();
2789 
2790   // Compare elements 1+ to the 0'th element.
2791   unsigned EltSize = getElementByteSize();
2792   for (unsigned i = 1, e = getNumElements(); i != e; ++i)
2793     if (memcmp(Base, Base+i*EltSize, EltSize))
2794       return false;
2795 
2796   return true;
2797 }
2798 
2799 Constant *ConstantDataVector::getSplatValue() const {
2800   // If they're all the same, return the 0th one as a representative.
2801   return isSplat() ? getElementAsConstant(0) : nullptr;
2802 }
2803 
2804 //===----------------------------------------------------------------------===//
2805 //                handleOperandChange implementations
2806 
2807 /// Update this constant array to change uses of
2808 /// 'From' to be uses of 'To'.  This must update the uniquing data structures
2809 /// etc.
2810 ///
2811 /// Note that we intentionally replace all uses of From with To here.  Consider
2812 /// a large array that uses 'From' 1000 times.  By handling this case all here,
2813 /// ConstantArray::handleOperandChange is only invoked once, and that
2814 /// single invocation handles all 1000 uses.  Handling them one at a time would
2815 /// work, but would be really slow because it would have to unique each updated
2816 /// array instance.
2817 ///
2818 void Constant::handleOperandChange(Value *From, Value *To) {
2819   Value *Replacement = nullptr;
2820   switch (getValueID()) {
2821   default:
2822     llvm_unreachable("Not a constant!");
2823 #define HANDLE_CONSTANT(Name)                                                  \
2824   case Value::Name##Val:                                                       \
2825     Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To);         \
2826     break;
2827 #include "llvm/IR/Value.def"
2828   }
2829 
2830   // If handleOperandChangeImpl returned nullptr, then it handled
2831   // replacing itself and we don't want to delete or replace anything else here.
2832   if (!Replacement)
2833     return;
2834 
2835   // I do need to replace this with an existing value.
2836   assert(Replacement != this && "I didn't contain From!");
2837 
2838   // Everyone using this now uses the replacement.
2839   replaceAllUsesWith(Replacement);
2840 
2841   // Delete the old constant!
2842   destroyConstant();
2843 }
2844 
2845 Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
2846   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2847   Constant *ToC = cast<Constant>(To);
2848 
2849   SmallVector<Constant*, 8> Values;
2850   Values.reserve(getNumOperands());  // Build replacement array.
2851 
2852   // Fill values with the modified operands of the constant array.  Also,
2853   // compute whether this turns into an all-zeros array.
2854   unsigned NumUpdated = 0;
2855 
2856   // Keep track of whether all the values in the array are "ToC".
2857   bool AllSame = true;
2858   Use *OperandList = getOperandList();
2859   unsigned OperandNo = 0;
2860   for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2861     Constant *Val = cast<Constant>(O->get());
2862     if (Val == From) {
2863       OperandNo = (O - OperandList);
2864       Val = ToC;
2865       ++NumUpdated;
2866     }
2867     Values.push_back(Val);
2868     AllSame &= Val == ToC;
2869   }
2870 
2871   if (AllSame && ToC->isNullValue())
2872     return ConstantAggregateZero::get(getType());
2873 
2874   if (AllSame && isa<UndefValue>(ToC))
2875     return UndefValue::get(getType());
2876 
2877   // Check for any other type of constant-folding.
2878   if (Constant *C = getImpl(getType(), Values))
2879     return C;
2880 
2881   // Update to the new value.
2882   return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
2883       Values, this, From, ToC, NumUpdated, OperandNo);
2884 }
2885 
2886 Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
2887   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2888   Constant *ToC = cast<Constant>(To);
2889 
2890   Use *OperandList = getOperandList();
2891 
2892   SmallVector<Constant*, 8> Values;
2893   Values.reserve(getNumOperands());  // Build replacement struct.
2894 
2895   // Fill values with the modified operands of the constant struct.  Also,
2896   // compute whether this turns into an all-zeros struct.
2897   unsigned NumUpdated = 0;
2898   bool AllSame = true;
2899   unsigned OperandNo = 0;
2900   for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2901     Constant *Val = cast<Constant>(O->get());
2902     if (Val == From) {
2903       OperandNo = (O - OperandList);
2904       Val = ToC;
2905       ++NumUpdated;
2906     }
2907     Values.push_back(Val);
2908     AllSame &= Val == ToC;
2909   }
2910 
2911   if (AllSame && ToC->isNullValue())
2912     return ConstantAggregateZero::get(getType());
2913 
2914   if (AllSame && isa<UndefValue>(ToC))
2915     return UndefValue::get(getType());
2916 
2917   // Update to the new value.
2918   return getContext().pImpl->StructConstants.replaceOperandsInPlace(
2919       Values, this, From, ToC, NumUpdated, OperandNo);
2920 }
2921 
2922 Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
2923   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2924   Constant *ToC = cast<Constant>(To);
2925 
2926   SmallVector<Constant*, 8> Values;
2927   Values.reserve(getNumOperands());  // Build replacement array...
2928   unsigned NumUpdated = 0;
2929   unsigned OperandNo = 0;
2930   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2931     Constant *Val = getOperand(i);
2932     if (Val == From) {
2933       OperandNo = i;
2934       ++NumUpdated;
2935       Val = ToC;
2936     }
2937     Values.push_back(Val);
2938   }
2939 
2940   if (Constant *C = getImpl(Values))
2941     return C;
2942 
2943   // Update to the new value.
2944   return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
2945       Values, this, From, ToC, NumUpdated, OperandNo);
2946 }
2947 
2948 Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
2949   assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2950   Constant *To = cast<Constant>(ToV);
2951 
2952   SmallVector<Constant*, 8> NewOps;
2953   unsigned NumUpdated = 0;
2954   unsigned OperandNo = 0;
2955   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2956     Constant *Op = getOperand(i);
2957     if (Op == From) {
2958       OperandNo = i;
2959       ++NumUpdated;
2960       Op = To;
2961     }
2962     NewOps.push_back(Op);
2963   }
2964   assert(NumUpdated && "I didn't contain From!");
2965 
2966   if (Constant *C = getWithOperands(NewOps, getType(), true))
2967     return C;
2968 
2969   // Update to the new value.
2970   return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
2971       NewOps, this, From, To, NumUpdated, OperandNo);
2972 }
2973 
2974 Instruction *ConstantExpr::getAsInstruction() {
2975   SmallVector<Value *, 4> ValueOperands(op_begin(), op_end());
2976   ArrayRef<Value*> Ops(ValueOperands);
2977 
2978   switch (getOpcode()) {
2979   case Instruction::Trunc:
2980   case Instruction::ZExt:
2981   case Instruction::SExt:
2982   case Instruction::FPTrunc:
2983   case Instruction::FPExt:
2984   case Instruction::UIToFP:
2985   case Instruction::SIToFP:
2986   case Instruction::FPToUI:
2987   case Instruction::FPToSI:
2988   case Instruction::PtrToInt:
2989   case Instruction::IntToPtr:
2990   case Instruction::BitCast:
2991   case Instruction::AddrSpaceCast:
2992     return CastInst::Create((Instruction::CastOps)getOpcode(),
2993                             Ops[0], getType());
2994   case Instruction::Select:
2995     return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
2996   case Instruction::InsertElement:
2997     return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
2998   case Instruction::ExtractElement:
2999     return ExtractElementInst::Create(Ops[0], Ops[1]);
3000   case Instruction::InsertValue:
3001     return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
3002   case Instruction::ExtractValue:
3003     return ExtractValueInst::Create(Ops[0], getIndices());
3004   case Instruction::ShuffleVector:
3005     return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
3006 
3007   case Instruction::GetElementPtr: {
3008     const auto *GO = cast<GEPOperator>(this);
3009     if (GO->isInBounds())
3010       return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
3011                                                Ops[0], Ops.slice(1));
3012     return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
3013                                      Ops.slice(1));
3014   }
3015   case Instruction::ICmp:
3016   case Instruction::FCmp:
3017     return CmpInst::Create((Instruction::OtherOps)getOpcode(),
3018                            (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
3019 
3020   default:
3021     assert(getNumOperands() == 2 && "Must be binary operator?");
3022     BinaryOperator *BO =
3023       BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
3024                              Ops[0], Ops[1]);
3025     if (isa<OverflowingBinaryOperator>(BO)) {
3026       BO->setHasNoUnsignedWrap(SubclassOptionalData &
3027                                OverflowingBinaryOperator::NoUnsignedWrap);
3028       BO->setHasNoSignedWrap(SubclassOptionalData &
3029                              OverflowingBinaryOperator::NoSignedWrap);
3030     }
3031     if (isa<PossiblyExactOperator>(BO))
3032       BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
3033     return BO;
3034   }
3035 }
3036