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