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