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