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