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