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