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