LLVM  14.0.0git
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 (getType()->isVectorTy())
47  if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
48  return SplatCFP->isNegativeZeroValue();
49 
50  // We've already handled true FP case; any other FP vectors can't represent -0.0.
51  if (getType()->isFPOrFPVectorTy())
52  return false;
53 
54  // Otherwise, just use +0.0.
55  return isNullValue();
56 }
57 
58 // Return true iff this constant is positive zero (floating point), negative
59 // zero (floating point), or a null value.
60 bool Constant::isZeroValue() const {
61  // Floating point values have an explicit -0.0 value.
62  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
63  return CFP->isZero();
64 
65  // Check for constant splat vectors of 1 values.
66  if (getType()->isVectorTy())
67  if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
68  return SplatCFP->isZero();
69 
70  // Otherwise, just use +0.0.
71  return isNullValue();
72 }
73 
74 bool Constant::isNullValue() const {
75  // 0 is null.
76  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
77  return CI->isZero();
78 
79  // +0.0 is null.
80  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
81  // ppc_fp128 determine isZero using high order double only
82  // Should check the bitwise value to make sure all bits are zero.
83  return CFP->isExactlyValue(+0.0);
84 
85  // constant zero is zero for aggregates, cpnull is null for pointers, none for
86  // tokens.
87  return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) ||
88  isa<ConstantTokenNone>(this);
89 }
90 
92  // Check for -1 integers
93  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
94  return CI->isMinusOne();
95 
96  // Check for FP which are bitcasted from -1 integers
97  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
98  return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue();
99 
100  // Check for constant splat vectors of 1 values.
101  if (getType()->isVectorTy())
102  if (const auto *SplatVal = getSplatValue())
103  return SplatVal->isAllOnesValue();
104 
105  return false;
106 }
107 
108 bool Constant::isOneValue() const {
109  // Check for 1 integers
110  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
111  return CI->isOne();
112 
113  // Check for FP which are bitcasted from 1 integers
114  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
115  return CFP->getValueAPF().bitcastToAPInt().isOneValue();
116 
117  // Check for constant splat vectors of 1 values.
118  if (getType()->isVectorTy())
119  if (const auto *SplatVal = getSplatValue())
120  return SplatVal->isOneValue();
121 
122  return false;
123 }
124 
126  // Check for 1 integers
127  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
128  return !CI->isOneValue();
129 
130  // Check for FP which are bitcasted from 1 integers
131  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
132  return !CFP->getValueAPF().bitcastToAPInt().isOneValue();
133 
134  // Check that vectors don't contain 1
135  if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
136  for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
137  Constant *Elt = getAggregateElement(I);
138  if (!Elt || !Elt->isNotOneValue())
139  return false;
140  }
141  return true;
142  }
143 
144  // Check for splats that don't contain 1
145  if (getType()->isVectorTy())
146  if (const auto *SplatVal = getSplatValue())
147  return SplatVal->isNotOneValue();
148 
149  // It *may* contain 1, we can't tell.
150  return false;
151 }
152 
154  // Check for INT_MIN integers
155  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
156  return CI->isMinValue(/*isSigned=*/true);
157 
158  // Check for FP which are bitcasted from INT_MIN integers
159  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
160  return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
161 
162  // Check for splats of INT_MIN values.
163  if (getType()->isVectorTy())
164  if (const auto *SplatVal = getSplatValue())
165  return SplatVal->isMinSignedValue();
166 
167  return false;
168 }
169 
171  // Check for INT_MIN integers
172  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
173  return !CI->isMinValue(/*isSigned=*/true);
174 
175  // Check for FP which are bitcasted from INT_MIN integers
176  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
177  return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
178 
179  // Check that vectors don't contain INT_MIN
180  if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
181  for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
182  Constant *Elt = getAggregateElement(I);
183  if (!Elt || !Elt->isNotMinSignedValue())
184  return false;
185  }
186  return true;
187  }
188 
189  // Check for splats that aren't INT_MIN
190  if (getType()->isVectorTy())
191  if (const auto *SplatVal = getSplatValue())
192  return SplatVal->isNotMinSignedValue();
193 
194  // It *may* contain INT_MIN, we can't tell.
195  return false;
196 }
197 
199  if (auto *CFP = dyn_cast<ConstantFP>(this))
200  return CFP->getValueAPF().isFiniteNonZero();
201 
202  if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
203  for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
204  auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I));
205  if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
206  return false;
207  }
208  return true;
209  }
210 
211  if (getType()->isVectorTy())
212  if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
213  return SplatCFP->isFiniteNonZeroFP();
214 
215  // It *may* contain finite non-zero, we can't tell.
216  return false;
217 }
218 
219 bool Constant::isNormalFP() const {
220  if (auto *CFP = dyn_cast<ConstantFP>(this))
221  return CFP->getValueAPF().isNormal();
222 
223  if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
224  for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
225  auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I));
226  if (!CFP || !CFP->getValueAPF().isNormal())
227  return false;
228  }
229  return true;
230  }
231 
232  if (getType()->isVectorTy())
233  if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
234  return SplatCFP->isNormalFP();
235 
236  // It *may* contain a normal fp value, we can't tell.
237  return false;
238 }
239 
241  if (auto *CFP = dyn_cast<ConstantFP>(this))
242  return CFP->getValueAPF().getExactInverse(nullptr);
243 
244  if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
245  for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
246  auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I));
247  if (!CFP || !CFP->getValueAPF().getExactInverse(nullptr))
248  return false;
249  }
250  return true;
251  }
252 
253  if (getType()->isVectorTy())
254  if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
255  return SplatCFP->hasExactInverseFP();
256 
257  // It *may* have an exact inverse fp value, we can't tell.
258  return false;
259 }
260 
261 bool Constant::isNaN() const {
262  if (auto *CFP = dyn_cast<ConstantFP>(this))
263  return CFP->isNaN();
264 
265  if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
266  for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
267  auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I));
268  if (!CFP || !CFP->isNaN())
269  return false;
270  }
271  return true;
272  }
273 
274  if (getType()->isVectorTy())
275  if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))
276  return SplatCFP->isNaN();
277 
278  // It *may* be NaN, we can't tell.
279  return false;
280 }
281 
283  // Are they fully identical?
284  if (this == Y)
285  return true;
286 
287  // The input value must be a vector constant with the same type.
288  auto *VTy = dyn_cast<VectorType>(getType());
289  if (!isa<Constant>(Y) || !VTy || VTy != Y->getType())
290  return false;
291 
292  // TODO: Compare pointer constants?
293  if (!(VTy->getElementType()->isIntegerTy() ||
294  VTy->getElementType()->isFloatingPointTy()))
295  return false;
296 
297  // They may still be identical element-wise (if they have `undef`s).
298  // Bitcast to integer to allow exact bitwise comparison for all types.
299  Type *IntTy = VectorType::getInteger(VTy);
300  Constant *C0 = ConstantExpr::getBitCast(const_cast<Constant *>(this), IntTy);
301  Constant *C1 = ConstantExpr::getBitCast(cast<Constant>(Y), IntTy);
303  return isa<UndefValue>(CmpEq) || match(CmpEq, m_One());
304 }
305 
306 static bool
308  function_ref<bool(const Constant *)> HasFn) {
309  if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
310  if (HasFn(C))
311  return true;
312  if (isa<ConstantAggregateZero>(C))
313  return false;
314  if (isa<ScalableVectorType>(C->getType()))
315  return false;
316 
317  for (unsigned i = 0, e = cast<FixedVectorType>(VTy)->getNumElements();
318  i != e; ++i)
319  if (HasFn(C->getAggregateElement(i)))
320  return true;
321  }
322 
323  return false;
324 }
325 
328  this, [&](const auto *C) { return isa<UndefValue>(C); });
329 }
330 
333  this, [&](const auto *C) { return isa<PoisonValue>(C); });
334 }
335 
337  if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
338  for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
339  if (isa<ConstantExpr>(getAggregateElement(i)))
340  return true;
341  }
342  return false;
343 }
344 
345 /// Constructor to create a '0' constant of arbitrary type.
347  switch (Ty->getTypeID()) {
348  case Type::IntegerTyID:
349  return ConstantInt::get(Ty, 0);
350  case Type::HalfTyID:
351  return ConstantFP::get(Ty->getContext(),
353  case Type::BFloatTyID:
354  return ConstantFP::get(Ty->getContext(),
356  case Type::FloatTyID:
357  return ConstantFP::get(Ty->getContext(),
359  case Type::DoubleTyID:
360  return ConstantFP::get(Ty->getContext(),
362  case Type::X86_FP80TyID:
363  return ConstantFP::get(Ty->getContext(),
365  case Type::FP128TyID:
366  return ConstantFP::get(Ty->getContext(),
368  case Type::PPC_FP128TyID:
369  return ConstantFP::get(Ty->getContext(),
371  APInt::getNullValue(128)));
372  case Type::PointerTyID:
373  return ConstantPointerNull::get(cast<PointerType>(Ty));
374  case Type::StructTyID:
375  case Type::ArrayTyID:
378  return ConstantAggregateZero::get(Ty);
379  case Type::TokenTyID:
380  return ConstantTokenNone::get(Ty->getContext());
381  default:
382  // Function, Label, or Opaque type?
383  llvm_unreachable("Cannot create a null constant of that type!");
384  }
385 }
386 
388  Type *ScalarTy = Ty->getScalarType();
389 
390  // Create the base integer constant.
391  Constant *C = ConstantInt::get(Ty->getContext(), V);
392 
393  // Convert an integer to a pointer, if necessary.
394  if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
395  C = ConstantExpr::getIntToPtr(C, PTy);
396 
397  // Broadcast a scalar to a vector, if necessary.
398  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
399  C = ConstantVector::getSplat(VTy->getElementCount(), C);
400 
401  return C;
402 }
403 
405  if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
406  return ConstantInt::get(Ty->getContext(),
407  APInt::getAllOnesValue(ITy->getBitWidth()));
408 
409  if (Ty->isFloatingPointTy()) {
411  Ty->getPrimitiveSizeInBits());
412  return ConstantFP::get(Ty->getContext(), FL);
413  }
414 
415  VectorType *VTy = cast<VectorType>(Ty);
417  getAllOnesValue(VTy->getElementType()));
418 }
419 
421  assert((getType()->isAggregateType() || getType()->isVectorTy()) &&
422  "Must be an aggregate/vector constant");
423 
424  if (const auto *CC = dyn_cast<ConstantAggregate>(this))
425  return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;
426 
427  if (const auto *CAZ = dyn_cast<ConstantAggregateZero>(this))
428  return Elt < CAZ->getElementCount().getKnownMinValue()
429  ? CAZ->getElementValue(Elt)
430  : nullptr;
431 
432  // FIXME: getNumElements() will fail for non-fixed vector types.
433  if (isa<ScalableVectorType>(getType()))
434  return nullptr;
435 
436  if (const auto *PV = dyn_cast<PoisonValue>(this))
437  return Elt < PV->getNumElements() ? PV->getElementValue(Elt) : nullptr;
438 
439  if (const auto *UV = dyn_cast<UndefValue>(this))
440  return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
441 
442  if (const auto *CDS = dyn_cast<ConstantDataSequential>(this))
443  return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
444  : nullptr;
445 
446  return nullptr;
447 }
448 
450  assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
451  if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt)) {
452  // Check if the constant fits into an uint64_t.
453  if (CI->getValue().getActiveBits() > 64)
454  return nullptr;
455  return getAggregateElement(CI->getZExtValue());
456  }
457  return nullptr;
458 }
459 
461  /// First call destroyConstantImpl on the subclass. This gives the subclass
462  /// a chance to remove the constant from any maps/pools it's contained in.
463  switch (getValueID()) {
464  default:
465  llvm_unreachable("Not a constant!");
466 #define HANDLE_CONSTANT(Name) \
467  case Value::Name##Val: \
468  cast<Name>(this)->destroyConstantImpl(); \
469  break;
470 #include "llvm/IR/Value.def"
471  }
472 
473  // When a Constant is destroyed, there may be lingering
474  // references to the constant by other constants in the constant pool. These
475  // constants are implicitly dependent on the module that is being deleted,
476  // but they don't know that. Because we only find out when the CPV is
477  // deleted, we must now notify all of our users (that should only be
478  // Constants) that they are, in fact, invalid now and should be deleted.
479  //
480  while (!use_empty()) {
481  Value *V = user_back();
482 #ifndef NDEBUG // Only in -g mode...
483  if (!isa<Constant>(V)) {
484  dbgs() << "While deleting: " << *this
485  << "\n\nUse still stuck around after Def is destroyed: " << *V
486  << "\n\n";
487  }
488 #endif
489  assert(isa<Constant>(V) && "References remain to Constant being destroyed");
490  cast<Constant>(V)->destroyConstant();
491 
492  // The constant should remove itself from our use list...
493  assert((use_empty() || user_back() != V) && "Constant not removed!");
494  }
495 
496  // Value has no outstanding references it is safe to delete it now...
497  deleteConstant(this);
498 }
499 
501  switch (C->getValueID()) {
502  case Constant::ConstantIntVal:
503  delete static_cast<ConstantInt *>(C);
504  break;
505  case Constant::ConstantFPVal:
506  delete static_cast<ConstantFP *>(C);
507  break;
508  case Constant::ConstantAggregateZeroVal:
509  delete static_cast<ConstantAggregateZero *>(C);
510  break;
511  case Constant::ConstantArrayVal:
512  delete static_cast<ConstantArray *>(C);
513  break;
514  case Constant::ConstantStructVal:
515  delete static_cast<ConstantStruct *>(C);
516  break;
517  case Constant::ConstantVectorVal:
518  delete static_cast<ConstantVector *>(C);
519  break;
520  case Constant::ConstantPointerNullVal:
521  delete static_cast<ConstantPointerNull *>(C);
522  break;
523  case Constant::ConstantDataArrayVal:
524  delete static_cast<ConstantDataArray *>(C);
525  break;
526  case Constant::ConstantDataVectorVal:
527  delete static_cast<ConstantDataVector *>(C);
528  break;
529  case Constant::ConstantTokenNoneVal:
530  delete static_cast<ConstantTokenNone *>(C);
531  break;
532  case Constant::BlockAddressVal:
533  delete static_cast<BlockAddress *>(C);
534  break;
535  case Constant::DSOLocalEquivalentVal:
536  delete static_cast<DSOLocalEquivalent *>(C);
537  break;
538  case Constant::UndefValueVal:
539  delete static_cast<UndefValue *>(C);
540  break;
541  case Constant::PoisonValueVal:
542  delete static_cast<PoisonValue *>(C);
543  break;
544  case Constant::ConstantExprVal:
545  if (isa<UnaryConstantExpr>(C))
546  delete static_cast<UnaryConstantExpr *>(C);
547  else if (isa<BinaryConstantExpr>(C))
548  delete static_cast<BinaryConstantExpr *>(C);
549  else if (isa<SelectConstantExpr>(C))
550  delete static_cast<SelectConstantExpr *>(C);
551  else if (isa<ExtractElementConstantExpr>(C))
552  delete static_cast<ExtractElementConstantExpr *>(C);
553  else if (isa<InsertElementConstantExpr>(C))
554  delete static_cast<InsertElementConstantExpr *>(C);
555  else if (isa<ShuffleVectorConstantExpr>(C))
556  delete static_cast<ShuffleVectorConstantExpr *>(C);
557  else if (isa<ExtractValueConstantExpr>(C))
558  delete static_cast<ExtractValueConstantExpr *>(C);
559  else if (isa<InsertValueConstantExpr>(C))
560  delete static_cast<InsertValueConstantExpr *>(C);
561  else if (isa<GetElementPtrConstantExpr>(C))
562  delete static_cast<GetElementPtrConstantExpr *>(C);
563  else if (isa<CompareConstantExpr>(C))
564  delete static_cast<CompareConstantExpr *>(C);
565  else
566  llvm_unreachable("Unexpected constant expr");
567  break;
568  default:
569  llvm_unreachable("Unexpected constant");
570  }
571 }
572 
573 static bool canTrapImpl(const Constant *C,
574  SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) {
575  assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
576  // The only thing that could possibly trap are constant exprs.
577  const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
578  if (!CE)
579  return false;
580 
581  // ConstantExpr traps if any operands can trap.
582  for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
583  if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) {
584  if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps))
585  return true;
586  }
587  }
588 
589  // Otherwise, only specific operations can trap.
590  switch (CE->getOpcode()) {
591  default:
592  return false;
593  case Instruction::UDiv:
594  case Instruction::SDiv:
595  case Instruction::URem:
596  case Instruction::SRem:
597  // Div and rem can trap if the RHS is not known to be non-zero.
598  if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
599  return true;
600  return false;
601  }
602 }
603 
604 bool Constant::canTrap() const {
606  return canTrapImpl(this, NonTrappingOps);
607 }
608 
609 /// Check if C contains a GlobalValue for which Predicate is true.
610 static bool
612  bool (*Predicate)(const GlobalValue *)) {
615  WorkList.push_back(C);
616  Visited.insert(C);
617 
618  while (!WorkList.empty()) {
619  const Constant *WorkItem = WorkList.pop_back_val();
620  if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
621  if (Predicate(GV))
622  return true;
623  for (const Value *Op : WorkItem->operands()) {
624  const Constant *ConstOp = dyn_cast<Constant>(Op);
625  if (!ConstOp)
626  continue;
627  if (Visited.insert(ConstOp).second)
628  WorkList.push_back(ConstOp);
629  }
630  }
631  return false;
632 }
633 
635  auto DLLImportPredicate = [](const GlobalValue *GV) {
636  return GV->isThreadLocal();
637  };
638  return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
639 }
640 
642  auto DLLImportPredicate = [](const GlobalValue *GV) {
643  return GV->hasDLLImportStorageClass();
644  };
645  return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
646 }
647 
649  for (const User *U : users()) {
650  const Constant *UC = dyn_cast<Constant>(U);
651  if (!UC || isa<GlobalValue>(UC))
652  return true;
653 
654  if (UC->isConstantUsed())
655  return true;
656  }
657  return false;
658 }
659 
661  return getRelocationInfo() == GlobalRelocation;
662 }
663 
665  return getRelocationInfo() != NoRelocation;
666 }
667 
668 Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
669  if (isa<GlobalValue>(this))
670  return GlobalRelocation; // Global reference.
671 
672  if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
673  return BA->getFunction()->getRelocationInfo();
674 
675  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {
676  if (CE->getOpcode() == Instruction::Sub) {
677  ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
678  ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
679  if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
680  RHS->getOpcode() == Instruction::PtrToInt) {
681  Constant *LHSOp0 = LHS->getOperand(0);
682  Constant *RHSOp0 = RHS->getOperand(0);
683 
684  // While raw uses of blockaddress need to be relocated, differences
685  // between two of them don't when they are for labels in the same
686  // function. This is a common idiom when creating a table for the
687  // indirect goto extension, so we handle it efficiently here.
688  if (isa<BlockAddress>(LHSOp0) && isa<BlockAddress>(RHSOp0) &&
689  cast<BlockAddress>(LHSOp0)->getFunction() ==
690  cast<BlockAddress>(RHSOp0)->getFunction())
691  return NoRelocation;
692 
693  // Relative pointers do not need to be dynamically relocated.
694  if (auto *RHSGV =
695  dyn_cast<GlobalValue>(RHSOp0->stripInBoundsConstantOffsets())) {
696  auto *LHS = LHSOp0->stripInBoundsConstantOffsets();
697  if (auto *LHSGV = dyn_cast<GlobalValue>(LHS)) {
698  if (LHSGV->isDSOLocal() && RHSGV->isDSOLocal())
699  return LocalRelocation;
700  } else if (isa<DSOLocalEquivalent>(LHS)) {
701  if (RHSGV->isDSOLocal())
702  return LocalRelocation;
703  }
704  }
705  }
706  }
707  }
708 
709  PossibleRelocationsTy Result = NoRelocation;
710  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
711  Result =
712  std::max(cast<Constant>(getOperand(i))->getRelocationInfo(), Result);
713 
714  return Result;
715 }
716 
717 /// If the specified constantexpr is dead, remove it. This involves recursively
718 /// eliminating any dead users of the constantexpr.
719 static bool removeDeadUsersOfConstant(const Constant *C) {
720  if (isa<GlobalValue>(C)) return false; // Cannot remove this
721 
722  while (!C->use_empty()) {
723  const Constant *User = dyn_cast<Constant>(C->user_back());
724  if (!User) return false; // Non-constant usage;
726  return false; // Constant wasn't dead
727  }
728 
729  // If C is only used by metadata, it should not be preserved but should have
730  // its uses replaced.
731  if (C->isUsedByMetadata()) {
732  const_cast<Constant *>(C)->replaceAllUsesWith(
733  UndefValue::get(C->getType()));
734  }
735  const_cast<Constant*>(C)->destroyConstant();
736  return true;
737 }
738 
739 
741  Value::const_user_iterator I = user_begin(), E = user_end();
742  Value::const_user_iterator LastNonDeadUser = E;
743  while (I != E) {
744  const Constant *User = dyn_cast<Constant>(*I);
745  if (!User) {
746  LastNonDeadUser = I;
747  ++I;
748  continue;
749  }
750 
752  // If the constant wasn't dead, remember that this was the last live use
753  // and move on to the next constant.
754  LastNonDeadUser = I;
755  ++I;
756  continue;
757  }
758 
759  // If the constant was dead, then the iterator is invalidated.
760  if (LastNonDeadUser == E)
761  I = user_begin();
762  else
763  I = std::next(LastNonDeadUser);
764  }
765 }
766 
768  assert(C && Replacement && "Expected non-nullptr constant arguments");
769  Type *Ty = C->getType();
770  if (match(C, m_Undef())) {
771  assert(Ty == Replacement->getType() && "Expected matching types");
772  return Replacement;
773  }
774 
775  // Don't know how to deal with this constant.
776  auto *VTy = dyn_cast<FixedVectorType>(Ty);
777  if (!VTy)
778  return C;
779 
780  unsigned NumElts = VTy->getNumElements();
781  SmallVector<Constant *, 32> NewC(NumElts);
782  for (unsigned i = 0; i != NumElts; ++i) {
783  Constant *EltC = C->getAggregateElement(i);
784  assert((!EltC || EltC->getType() == Replacement->getType()) &&
785  "Expected matching types");
786  NewC[i] = EltC && match(EltC, m_Undef()) ? Replacement : EltC;
787  }
788  return ConstantVector::get(NewC);
789 }
790 
792  assert(C && Other && "Expected non-nullptr constant arguments");
793  if (match(C, m_Undef()))
794  return C;
795 
796  Type *Ty = C->getType();
797  if (match(Other, m_Undef()))
798  return UndefValue::get(Ty);
799 
800  auto *VTy = dyn_cast<FixedVectorType>(Ty);
801  if (!VTy)
802  return C;
803 
804  Type *EltTy = VTy->getElementType();
805  unsigned NumElts = VTy->getNumElements();
806  assert(isa<FixedVectorType>(Other->getType()) &&
807  cast<FixedVectorType>(Other->getType())->getNumElements() == NumElts &&
808  "Type mismatch");
809 
810  bool FoundExtraUndef = false;
811  SmallVector<Constant *, 32> NewC(NumElts);
812  for (unsigned I = 0; I != NumElts; ++I) {
813  NewC[I] = C->getAggregateElement(I);
814  Constant *OtherEltC = Other->getAggregateElement(I);
815  assert(NewC[I] && OtherEltC && "Unknown vector element");
816  if (!match(NewC[I], m_Undef()) && match(OtherEltC, m_Undef())) {
817  NewC[I] = UndefValue::get(EltTy);
818  FoundExtraUndef = true;
819  }
820  }
821  if (FoundExtraUndef)
822  return ConstantVector::get(NewC);
823  return C;
824 }
825 
827  if (isa<ConstantData>(this))
828  return true;
829  if (isa<ConstantAggregate>(this) || isa<ConstantExpr>(this)) {
830  for (const Value *Op : operand_values())
831  if (!cast<Constant>(Op)->isManifestConstant())
832  return false;
833  return true;
834  }
835  return false;
836 }
837 
838 //===----------------------------------------------------------------------===//
839 // ConstantInt
840 //===----------------------------------------------------------------------===//
841 
842 ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V)
843  : ConstantData(Ty, ConstantIntVal), Val(V) {
844  assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
845 }
846 
848  LLVMContextImpl *pImpl = Context.pImpl;
849  if (!pImpl->TheTrueVal)
851  return pImpl->TheTrueVal;
852 }
853 
855  LLVMContextImpl *pImpl = Context.pImpl;
856  if (!pImpl->TheFalseVal)
858  return pImpl->TheFalseVal;
859 }
860 
862  return V ? getTrue(Context) : getFalse(Context);
863 }
864 
866  assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
868  if (auto *VTy = dyn_cast<VectorType>(Ty))
869  return ConstantVector::getSplat(VTy->getElementCount(), TrueC);
870  return TrueC;
871 }
872 
874  assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
876  if (auto *VTy = dyn_cast<VectorType>(Ty))
877  return ConstantVector::getSplat(VTy->getElementCount(), FalseC);
878  return FalseC;
879 }
880 
882  return V ? getTrue(Ty) : getFalse(Ty);
883 }
884 
885 // Get a ConstantInt from an APInt.
887  // get an existing value or the insertion position
888  LLVMContextImpl *pImpl = Context.pImpl;
889  std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V];
890  if (!Slot) {
891  // Get the corresponding integer type for the bit width of the value.
893  Slot.reset(new ConstantInt(ITy, V));
894  }
895  assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
896  return Slot.get();
897 }
898 
899 Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
900  Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
901 
902  // For vectors, broadcast the value.
903  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
904  return ConstantVector::getSplat(VTy->getElementCount(), C);
905 
906  return C;
907 }
908 
909 ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
910  return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
911 }
912 
914  return get(Ty, V, true);
915 }
916 
918  return get(Ty, V, true);
919 }
920 
922  ConstantInt *C = get(Ty->getContext(), V);
923  assert(C->getType() == Ty->getScalarType() &&
924  "ConstantInt type doesn't match the type implied by its value!");
925 
926  // For vectors, broadcast the value.
927  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
928  return ConstantVector::getSplat(VTy->getElementCount(), C);
929 
930  return C;
931 }
932 
934  return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
935 }
936 
937 /// Remove the constant from the constant table.
938 void ConstantInt::destroyConstantImpl() {
939  llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
940 }
941 
942 //===----------------------------------------------------------------------===//
943 // ConstantFP
944 //===----------------------------------------------------------------------===//
945 
946 Constant *ConstantFP::get(Type *Ty, double V) {
947  LLVMContext &Context = Ty->getContext();
948 
949  APFloat FV(V);
950  bool ignored;
952  APFloat::rmNearestTiesToEven, &ignored);
953  Constant *C = get(Context, FV);
954 
955  // For vectors, broadcast the value.
956  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
957  return ConstantVector::getSplat(VTy->getElementCount(), C);
958 
959  return C;
960 }
961 
963  ConstantFP *C = get(Ty->getContext(), V);
964  assert(C->getType() == Ty->getScalarType() &&
965  "ConstantFP type doesn't match the type implied by its value!");
966 
967  // For vectors, broadcast the value.
968  if (auto *VTy = dyn_cast<VectorType>(Ty))
969  return ConstantVector::getSplat(VTy->getElementCount(), C);
970 
971  return C;
972 }
973 
975  LLVMContext &Context = Ty->getContext();
976 
977  APFloat FV(Ty->getScalarType()->getFltSemantics(), Str);
978  Constant *C = get(Context, FV);
979 
980  // For vectors, broadcast the value.
981  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
982  return ConstantVector::getSplat(VTy->getElementCount(), C);
983 
984  return C;
985 }
986 
987 Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) {
988  const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
989  APFloat NaN = APFloat::getNaN(Semantics, Negative, Payload);
990  Constant *C = get(Ty->getContext(), NaN);
991 
992  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
993  return ConstantVector::getSplat(VTy->getElementCount(), C);
994 
995  return C;
996 }
997 
998 Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) {
999  const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
1000  APFloat NaN = APFloat::getQNaN(Semantics, Negative, Payload);
1001  Constant *C = get(Ty->getContext(), NaN);
1002 
1003  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1004  return ConstantVector::getSplat(VTy->getElementCount(), C);
1005 
1006  return C;
1007 }
1008 
1009 Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) {
1010  const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
1011  APFloat NaN = APFloat::getSNaN(Semantics, Negative, Payload);
1012  Constant *C = get(Ty->getContext(), NaN);
1013 
1014  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1015  return ConstantVector::getSplat(VTy->getElementCount(), C);
1016 
1017  return C;
1018 }
1019 
1021  const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
1022  APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
1023  Constant *C = get(Ty->getContext(), NegZero);
1024 
1025  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1026  return ConstantVector::getSplat(VTy->getElementCount(), C);
1027 
1028  return C;
1029 }
1030 
1031 
1033  if (Ty->isFPOrFPVectorTy())
1034  return getNegativeZero(Ty);
1035 
1036  return Constant::getNullValue(Ty);
1037 }
1038 
1039 
1040 // ConstantFP accessors.
1042  LLVMContextImpl* pImpl = Context.pImpl;
1043 
1044  std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
1045 
1046  if (!Slot) {
1048  Slot.reset(new ConstantFP(Ty, V));
1049  }
1050 
1051  return Slot.get();
1052 }
1053 
1054 Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
1055  const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();
1056  Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
1057 
1058  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1059  return ConstantVector::getSplat(VTy->getElementCount(), C);
1060 
1061  return C;
1062 }
1063 
1064 ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
1065  : ConstantData(Ty, ConstantFPVal), Val(V) {
1066  assert(&V.getSemantics() == &Ty->getFltSemantics() &&
1067  "FP type Mismatch");
1068 }
1069 
1070 bool ConstantFP::isExactlyValue(const APFloat &V) const {
1071  return Val.bitwiseIsEqual(V);
1072 }
1073 
1074 /// Remove the constant from the constant table.
1075 void ConstantFP::destroyConstantImpl() {
1076  llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
1077 }
1078 
1079 //===----------------------------------------------------------------------===//
1080 // ConstantAggregateZero Implementation
1081 //===----------------------------------------------------------------------===//
1082 
1084  if (auto *AT = dyn_cast<ArrayType>(getType()))
1085  return Constant::getNullValue(AT->getElementType());
1086  return Constant::getNullValue(cast<VectorType>(getType())->getElementType());
1087 }
1088 
1090  return Constant::getNullValue(getType()->getStructElementType(Elt));
1091 }
1092 
1094  if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
1095  return getSequentialElement();
1096  return getStructElement(cast<ConstantInt>(C)->getZExtValue());
1097 }
1098 
1100  if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
1101  return getSequentialElement();
1102  return getStructElement(Idx);
1103 }
1104 
1106  Type *Ty = getType();
1107  if (auto *AT = dyn_cast<ArrayType>(Ty))
1108  return ElementCount::getFixed(AT->getNumElements());
1109  if (auto *VT = dyn_cast<VectorType>(Ty))
1110  return VT->getElementCount();
1112 }
1113 
1114 //===----------------------------------------------------------------------===//
1115 // UndefValue Implementation
1116 //===----------------------------------------------------------------------===//
1117 
1119  if (ArrayType *ATy = dyn_cast<ArrayType>(getType()))
1120  return UndefValue::get(ATy->getElementType());
1121  return UndefValue::get(cast<VectorType>(getType())->getElementType());
1122 }
1123 
1125  return UndefValue::get(getType()->getStructElementType(Elt));
1126 }
1127 
1129  if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
1130  return getSequentialElement();
1131  return getStructElement(cast<ConstantInt>(C)->getZExtValue());
1132 }
1133 
1135  if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
1136  return getSequentialElement();
1137  return getStructElement(Idx);
1138 }
1139 
1140 unsigned UndefValue::getNumElements() const {
1141  Type *Ty = getType();
1142  if (auto *AT = dyn_cast<ArrayType>(Ty))
1143  return AT->getNumElements();
1144  if (auto *VT = dyn_cast<VectorType>(Ty))
1145  return cast<FixedVectorType>(VT)->getNumElements();
1146  return Ty->getStructNumElements();
1147 }
1148 
1149 //===----------------------------------------------------------------------===//
1150 // PoisonValue Implementation
1151 //===----------------------------------------------------------------------===//
1152 
1154  if (ArrayType *ATy = dyn_cast<ArrayType>(getType()))
1155  return PoisonValue::get(ATy->getElementType());
1156  return PoisonValue::get(cast<VectorType>(getType())->getElementType());
1157 }
1158 
1160  return PoisonValue::get(getType()->getStructElementType(Elt));
1161 }
1162 
1164  if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
1165  return getSequentialElement();
1166  return getStructElement(cast<ConstantInt>(C)->getZExtValue());
1167 }
1168 
1170  if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))
1171  return getSequentialElement();
1172  return getStructElement(Idx);
1173 }
1174 
1175 //===----------------------------------------------------------------------===//
1176 // ConstantXXX Classes
1177 //===----------------------------------------------------------------------===//
1178 
1179 template <typename ItTy, typename EltTy>
1180 static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
1181  for (; Start != End; ++Start)
1182  if (*Start != Elt)
1183  return false;
1184  return true;
1185 }
1186 
1187 template <typename SequentialTy, typename ElementTy>
1189  assert(!V.empty() && "Cannot get empty int sequence.");
1190 
1192  for (Constant *C : V)
1193  if (auto *CI = dyn_cast<ConstantInt>(C))
1194  Elts.push_back(CI->getZExtValue());
1195  else
1196  return nullptr;
1197  return SequentialTy::get(V[0]->getContext(), Elts);
1198 }
1199 
1200 template <typename SequentialTy, typename ElementTy>
1202  assert(!V.empty() && "Cannot get empty FP sequence.");
1203 
1205  for (Constant *C : V)
1206  if (auto *CFP = dyn_cast<ConstantFP>(C))
1207  Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
1208  else
1209  return nullptr;
1210  return SequentialTy::getFP(V[0]->getType(), Elts);
1211 }
1212 
1213 template <typename SequenceTy>
1216  // We speculatively build the elements here even if it turns out that there is
1217  // a constantexpr or something else weird, since it is so uncommon for that to
1218  // happen.
1219  if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1220  if (CI->getType()->isIntegerTy(8))
1221  return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
1222  else if (CI->getType()->isIntegerTy(16))
1223  return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
1224  else if (CI->getType()->isIntegerTy(32))
1225  return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
1226  else if (CI->getType()->isIntegerTy(64))
1227  return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
1228  } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
1229  if (CFP->getType()->isHalfTy() || CFP->getType()->isBFloatTy())
1230  return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
1231  else if (CFP->getType()->isFloatTy())
1232  return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
1233  else if (CFP->getType()->isDoubleTy())
1234  return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
1235  }
1236 
1237  return nullptr;
1238 }
1239 
1242  : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
1243  V.size()) {
1244  llvm::copy(V, op_begin());
1245 
1246  // Check that types match, unless this is an opaque struct.
1247  if (auto *ST = dyn_cast<StructType>(T)) {
1248  if (ST->isOpaque())
1249  return;
1250  for (unsigned I = 0, E = V.size(); I != E; ++I)
1251  assert(V[I]->getType() == ST->getTypeAtIndex(I) &&
1252  "Initializer for struct element doesn't match!");
1253  }
1254 }
1255 
1256 ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
1257  : ConstantAggregate(T, ConstantArrayVal, V) {
1258  assert(V.size() == T->getNumElements() &&
1259  "Invalid initializer for constant array");
1260 }
1261 
1263  if (Constant *C = getImpl(Ty, V))
1264  return C;
1265  return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
1266 }
1267 
1269  // Empty arrays are canonicalized to ConstantAggregateZero.
1270  if (V.empty())
1271  return ConstantAggregateZero::get(Ty);
1272 
1273  for (unsigned i = 0, e = V.size(); i != e; ++i) {
1274  assert(V[i]->getType() == Ty->getElementType() &&
1275  "Wrong type in array element initializer");
1276  }
1277 
1278  // If this is an all-zero array, return a ConstantAggregateZero object. If
1279  // all undef, return an UndefValue, if "all simple", then return a
1280  // ConstantDataArray.
1281  Constant *C = V[0];
1282  if (isa<PoisonValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
1283  return PoisonValue::get(Ty);
1284 
1285  if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
1286  return UndefValue::get(Ty);
1287 
1288  if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
1289  return ConstantAggregateZero::get(Ty);
1290 
1291  // Check to see if all of the elements are ConstantFP or ConstantInt and if
1292  // the element type is compatible with ConstantDataVector. If so, use it.
1294  return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
1295 
1296  // Otherwise, we really do want to create a ConstantArray.
1297  return nullptr;
1298 }
1299 
1302  bool Packed) {
1303  unsigned VecSize = V.size();
1304  SmallVector<Type*, 16> EltTypes(VecSize);
1305  for (unsigned i = 0; i != VecSize; ++i)
1306  EltTypes[i] = V[i]->getType();
1307 
1308  return StructType::get(Context, EltTypes, Packed);
1309 }
1310 
1311 
1313  bool Packed) {
1314  assert(!V.empty() &&
1315  "ConstantStruct::getTypeForElements cannot be called on empty list");
1316  return getTypeForElements(V[0]->getContext(), V, Packed);
1317 }
1318 
1319 ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
1320  : ConstantAggregate(T, ConstantStructVal, V) {
1321  assert((T->isOpaque() || V.size() == T->getNumElements()) &&
1322  "Invalid initializer for constant struct");
1323 }
1324 
1325 // ConstantStruct accessors.
1327  assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
1328  "Incorrect # elements specified to ConstantStruct::get");
1329 
1330  // Create a ConstantAggregateZero value if all elements are zeros.
1331  bool isZero = true;
1332  bool isUndef = false;
1333  bool isPoison = false;
1334 
1335  if (!V.empty()) {
1336  isUndef = isa<UndefValue>(V[0]);
1337  isPoison = isa<PoisonValue>(V[0]);
1338  isZero = V[0]->isNullValue();
1339  // PoisonValue inherits UndefValue, so its check is not necessary.
1340  if (isUndef || isZero) {
1341  for (unsigned i = 0, e = V.size(); i != e; ++i) {
1342  if (!V[i]->isNullValue())
1343  isZero = false;
1344  if (!isa<PoisonValue>(V[i]))
1345  isPoison = false;
1346  if (isa<PoisonValue>(V[i]) || !isa<UndefValue>(V[i]))
1347  isUndef = false;
1348  }
1349  }
1350  }
1351  if (isZero)
1353  if (isPoison)
1354  return PoisonValue::get(ST);
1355  if (isUndef)
1356  return UndefValue::get(ST);
1357 
1358  return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
1359 }
1360 
1361 ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
1362  : ConstantAggregate(T, ConstantVectorVal, V) {
1363  assert(V.size() == cast<FixedVectorType>(T)->getNumElements() &&
1364  "Invalid initializer for constant vector");
1365 }
1366 
1367 // ConstantVector accessors.
1369  if (Constant *C = getImpl(V))
1370  return C;
1371  auto *Ty = FixedVectorType::get(V.front()->getType(), V.size());
1372  return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
1373 }
1374 
1376  assert(!V.empty() && "Vectors can't be empty");
1377  auto *T = FixedVectorType::get(V.front()->getType(), V.size());
1378 
1379  // If this is an all-undef or all-zero vector, return a
1380  // ConstantAggregateZero or UndefValue.
1381  Constant *C = V[0];
1382  bool isZero = C->isNullValue();
1383  bool isUndef = isa<UndefValue>(C);
1384  bool isPoison = isa<PoisonValue>(C);
1385 
1386  if (isZero || isUndef) {
1387  for (unsigned i = 1, e = V.size(); i != e; ++i)
1388  if (V[i] != C) {
1389  isZero = isUndef = isPoison = false;
1390  break;
1391  }
1392  }
1393 
1394  if (isZero)
1395  return ConstantAggregateZero::get(T);
1396  if (isPoison)
1397  return PoisonValue::get(T);
1398  if (isUndef)
1399  return UndefValue::get(T);
1400 
1401  // Check to see if all of the elements are ConstantFP or ConstantInt and if
1402  // the element type is compatible with ConstantDataVector. If so, use it.
1404  return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
1405 
1406  // Otherwise, the element type isn't compatible with ConstantDataVector, or
1407  // the operand list contains a ConstantExpr or something else strange.
1408  return nullptr;
1409 }
1410 
1412  if (!EC.isScalable()) {
1413  // If this splat is compatible with ConstantDataVector, use it instead of
1414  // ConstantVector.
1415  if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
1417  return ConstantDataVector::getSplat(EC.getKnownMinValue(), V);
1418 
1419  SmallVector<Constant *, 32> Elts(EC.getKnownMinValue(), V);
1420  return get(Elts);
1421  }
1422 
1423  Type *VTy = VectorType::get(V->getType(), EC);
1424 
1425  if (V->isNullValue())
1426  return ConstantAggregateZero::get(VTy);
1427  else if (isa<UndefValue>(V))
1428  return UndefValue::get(VTy);
1429 
1430  Type *I32Ty = Type::getInt32Ty(VTy->getContext());
1431 
1432  // Move scalar into vector.
1433  Constant *UndefV = UndefValue::get(VTy);
1434  V = ConstantExpr::getInsertElement(UndefV, V, ConstantInt::get(I32Ty, 0));
1435  // Build shuffle mask to perform the splat.
1436  SmallVector<int, 8> Zeros(EC.getKnownMinValue(), 0);
1437  // Splat.
1438  return ConstantExpr::getShuffleVector(V, UndefV, Zeros);
1439 }
1440 
1442  LLVMContextImpl *pImpl = Context.pImpl;
1443  if (!pImpl->TheNoneToken)
1444  pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
1445  return pImpl->TheNoneToken.get();
1446 }
1447 
1448 /// Remove the constant from the constant table.
1449 void ConstantTokenNone::destroyConstantImpl() {
1450  llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1451 }
1452 
1453 // Utility function for determining if a ConstantExpr is a CastOp or not. This
1454 // can't be inline because we don't want to #include Instruction.h into
1455 // Constant.h
1456 bool ConstantExpr::isCast() const {
1457  return Instruction::isCast(getOpcode());
1458 }
1459 
1461  return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
1462 }
1463 
1465  if (getOpcode() != Instruction::GetElementPtr) return false;
1466 
1467  gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
1468  User::const_op_iterator OI = std::next(this->op_begin());
1469 
1470  // The remaining indices may be compile-time known integers within the bounds
1471  // of the corresponding notional static array types.
1472  for (; GEPI != E; ++GEPI, ++OI) {
1473  if (isa<UndefValue>(*OI))
1474  continue;
1475  auto *CI = dyn_cast<ConstantInt>(*OI);
1476  if (!CI || (GEPI.isBoundedSequential() &&
1477  (CI->getValue().getActiveBits() > 64 ||
1478  CI->getZExtValue() >= GEPI.getSequentialNumElements())))
1479  return false;
1480  }
1481 
1482  // All the indices checked out.
1483  return true;
1484 }
1485 
1487  return getOpcode() == Instruction::ExtractValue ||
1488  getOpcode() == Instruction::InsertValue;
1489 }
1490 
1492  if (const ExtractValueConstantExpr *EVCE =
1493  dyn_cast<ExtractValueConstantExpr>(this))
1494  return EVCE->Indices;
1495 
1496  return cast<InsertValueConstantExpr>(this)->Indices;
1497 }
1498 
1499 unsigned ConstantExpr::getPredicate() const {
1500  return cast<CompareConstantExpr>(this)->predicate;
1501 }
1502 
1504  return cast<ShuffleVectorConstantExpr>(this)->ShuffleMask;
1505 }
1506 
1508  return cast<ShuffleVectorConstantExpr>(this)->ShuffleMaskForBitcode;
1509 }
1510 
1511 Constant *
1513  assert(Op->getType() == getOperand(OpNo)->getType() &&
1514  "Replacing operand with value of different type!");
1515  if (getOperand(OpNo) == Op)
1516  return const_cast<ConstantExpr*>(this);
1517 
1519  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1520  NewOps.push_back(i == OpNo ? Op : getOperand(i));
1521 
1522  return getWithOperands(NewOps);
1523 }
1524 
1526  bool OnlyIfReduced, Type *SrcTy) const {
1527  assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
1528 
1529  // If no operands changed return self.
1530  if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
1531  return const_cast<ConstantExpr*>(this);
1532 
1533  Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
1534  switch (getOpcode()) {
1535  case Instruction::Trunc:
1536  case Instruction::ZExt:
1537  case Instruction::SExt:
1538  case Instruction::FPTrunc:
1539  case Instruction::FPExt:
1540  case Instruction::UIToFP:
1541  case Instruction::SIToFP:
1542  case Instruction::FPToUI:
1543  case Instruction::FPToSI:
1544  case Instruction::PtrToInt:
1545  case Instruction::IntToPtr:
1546  case Instruction::BitCast:
1547  case Instruction::AddrSpaceCast:
1548  return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
1549  case Instruction::Select:
1550  return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
1551  case Instruction::InsertElement:
1552  return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
1553  OnlyIfReducedTy);
1554  case Instruction::ExtractElement:
1555  return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
1556  case Instruction::InsertValue:
1557  return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
1558  OnlyIfReducedTy);
1559  case Instruction::ExtractValue:
1560  return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
1561  case Instruction::FNeg:
1562  return ConstantExpr::getFNeg(Ops[0]);
1563  case Instruction::ShuffleVector:
1564  return ConstantExpr::getShuffleVector(Ops[0], Ops[1], getShuffleMask(),
1565  OnlyIfReducedTy);
1566  case Instruction::GetElementPtr: {
1567  auto *GEPO = cast<GEPOperator>(this);
1568  assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1570  SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
1571  GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy);
1572  }
1573  case Instruction::ICmp:
1574  case Instruction::FCmp:
1575  return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
1576  OnlyIfReducedTy);
1577  default:
1578  assert(getNumOperands() == 2 && "Must be binary operator?");
1579  return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
1580  OnlyIfReducedTy);
1581  }
1582 }
1583 
1584 
1585 //===----------------------------------------------------------------------===//
1586 // isValueValidForType implementations
1587 
1588 bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
1589  unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1590  if (Ty->isIntegerTy(1))
1591  return Val == 0 || Val == 1;
1592  return isUIntN(NumBits, Val);
1593 }
1594 
1595 bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
1596  unsigned NumBits = Ty->getIntegerBitWidth();
1597  if (Ty->isIntegerTy(1))
1598  return Val == 0 || Val == 1 || Val == -1;
1599  return isIntN(NumBits, Val);
1600 }
1601 
1603  // convert modifies in place, so make a copy.
1604  APFloat Val2 = APFloat(Val);
1605  bool losesInfo;
1606  switch (Ty->getTypeID()) {
1607  default:
1608  return false; // These can't be represented as floating point!
1609 
1610  // FIXME rounding mode needs to be more flexible
1611  case Type::HalfTyID: {
1612  if (&Val2.getSemantics() == &APFloat::IEEEhalf())
1613  return true;
1615  return !losesInfo;
1616  }
1617  case Type::BFloatTyID: {
1618  if (&Val2.getSemantics() == &APFloat::BFloat())
1619  return true;
1621  return !losesInfo;
1622  }
1623  case Type::FloatTyID: {
1624  if (&Val2.getSemantics() == &APFloat::IEEEsingle())
1625  return true;
1627  return !losesInfo;
1628  }
1629  case Type::DoubleTyID: {
1630  if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
1631  &Val2.getSemantics() == &APFloat::BFloat() ||
1632  &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1633  &Val2.getSemantics() == &APFloat::IEEEdouble())
1634  return true;
1636  return !losesInfo;
1637  }
1638  case Type::X86_FP80TyID:
1639  return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1640  &Val2.getSemantics() == &APFloat::BFloat() ||
1641  &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1642  &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1644  case Type::FP128TyID:
1645  return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1646  &Val2.getSemantics() == &APFloat::BFloat() ||
1647  &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1648  &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1649  &Val2.getSemantics() == &APFloat::IEEEquad();
1650  case Type::PPC_FP128TyID:
1651  return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1652  &Val2.getSemantics() == &APFloat::BFloat() ||
1653  &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1654  &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1655  &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
1656  }
1657 }
1658 
1659 
1660 //===----------------------------------------------------------------------===//
1661 // Factory Function Implementation
1662 
1664  assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
1665  "Cannot create an aggregate zero of non-aggregate type!");
1666 
1667  std::unique_ptr<ConstantAggregateZero> &Entry =
1668  Ty->getContext().pImpl->CAZConstants[Ty];
1669  if (!Entry)
1670  Entry.reset(new ConstantAggregateZero(Ty));
1671 
1672  return Entry.get();
1673 }
1674 
1675 /// Remove the constant from the constant table.
1676 void ConstantAggregateZero::destroyConstantImpl() {
1677  getContext().pImpl->CAZConstants.erase(getType());
1678 }
1679 
1680 /// Remove the constant from the constant table.
1681 void ConstantArray::destroyConstantImpl() {
1683 }
1684 
1685 
1686 //---- ConstantStruct::get() implementation...
1687 //
1688 
1689 /// Remove the constant from the constant table.
1690 void ConstantStruct::destroyConstantImpl() {
1692 }
1693 
1694 /// Remove the constant from the constant table.
1695 void ConstantVector::destroyConstantImpl() {
1697 }
1698 
1699 Constant *Constant::getSplatValue(bool AllowUndefs) const {
1700  assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1701  if (isa<ConstantAggregateZero>(this))
1702  return getNullValue(cast<VectorType>(getType())->getElementType());
1703  if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
1704  return CV->getSplatValue();
1705  if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1706  return CV->getSplatValue(AllowUndefs);
1707 
1708  // Check if this is a constant expression splat of the form returned by
1709  // ConstantVector::getSplat()
1710  const auto *Shuf = dyn_cast<ConstantExpr>(this);
1711  if (Shuf && Shuf->getOpcode() == Instruction::ShuffleVector &&
1712  isa<UndefValue>(Shuf->getOperand(1))) {
1713 
1714  const auto *IElt = dyn_cast<ConstantExpr>(Shuf->getOperand(0));
1715  if (IElt && IElt->getOpcode() == Instruction::InsertElement &&
1716  isa<UndefValue>(IElt->getOperand(0))) {
1717 
1718  ArrayRef<int> Mask = Shuf->getShuffleMask();
1719  Constant *SplatVal = IElt->getOperand(1);
1720  ConstantInt *Index = dyn_cast<ConstantInt>(IElt->getOperand(2));
1721 
1722  if (Index && Index->getValue() == 0 &&
1723  llvm::all_of(Mask, [](int I) { return I == 0; }))
1724  return SplatVal;
1725  }
1726  }
1727 
1728  return nullptr;
1729 }
1730 
1731 Constant *ConstantVector::getSplatValue(bool AllowUndefs) const {
1732  // Check out first element.
1733  Constant *Elt = getOperand(0);
1734  // Then make sure all remaining elements point to the same value.
1735  for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1736  Constant *OpC = getOperand(I);
1737  if (OpC == Elt)
1738  continue;
1739 
1740  // Strict mode: any mismatch is not a splat.
1741  if (!AllowUndefs)
1742  return nullptr;
1743 
1744  // Allow undefs mode: ignore undefined elements.
1745  if (isa<UndefValue>(OpC))
1746  continue;
1747 
1748  // If we do not have a defined element yet, use the current operand.
1749  if (isa<UndefValue>(Elt))
1750  Elt = OpC;
1751 
1752  if (OpC != Elt)
1753  return nullptr;
1754  }
1755  return Elt;
1756 }
1757 
1759  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1760  return CI->getValue();
1761  assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1762  const Constant *C = this->getAggregateElement(0U);
1763  assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1764  return cast<ConstantInt>(C)->getValue();
1765 }
1766 
1767 //---- ConstantPointerNull::get() implementation.
1768 //
1769 
1771  std::unique_ptr<ConstantPointerNull> &Entry =
1772  Ty->getContext().pImpl->CPNConstants[Ty];
1773  if (!Entry)
1774  Entry.reset(new ConstantPointerNull(Ty));
1775 
1776  return Entry.get();
1777 }
1778 
1779 /// Remove the constant from the constant table.
1780 void ConstantPointerNull::destroyConstantImpl() {
1781  getContext().pImpl->CPNConstants.erase(getType());
1782 }
1783 
1785  std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
1786  if (!Entry)
1787  Entry.reset(new UndefValue(Ty));
1788 
1789  return Entry.get();
1790 }
1791 
1792 /// Remove the constant from the constant table.
1793 void UndefValue::destroyConstantImpl() {
1794  // Free the constant and any dangling references to it.
1795  if (getValueID() == UndefValueVal) {
1796  getContext().pImpl->UVConstants.erase(getType());
1797  } else if (getValueID() == PoisonValueVal) {
1798  getContext().pImpl->PVConstants.erase(getType());
1799  }
1800  llvm_unreachable("Not a undef or a poison!");
1801 }
1802 
1804  std::unique_ptr<PoisonValue> &Entry = Ty->getContext().pImpl->PVConstants[Ty];
1805  if (!Entry)
1806  Entry.reset(new PoisonValue(Ty));
1807 
1808  return Entry.get();
1809 }
1810 
1811 /// Remove the constant from the constant table.
1812 void PoisonValue::destroyConstantImpl() {
1813  // Free the constant and any dangling references to it.
1814  getContext().pImpl->PVConstants.erase(getType());
1815 }
1816 
1818  assert(BB->getParent() && "Block must have a parent");
1819  return get(BB->getParent(), BB);
1820 }
1821 
1823  BlockAddress *&BA =
1824  F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
1825  if (!BA)
1826  BA = new BlockAddress(F, BB);
1827 
1828  assert(BA->getFunction() == F && "Basic block moved between functions");
1829  return BA;
1830 }
1831 
1833  : Constant(Type::getInt8PtrTy(F->getContext(), F->getAddressSpace()),
1834  Value::BlockAddressVal, &Op<0>(), 2) {
1835  setOperand(0, F);
1836  setOperand(1, BB);
1837  BB->AdjustBlockAddressRefCount(1);
1838 }
1839 
1841  if (!BB->hasAddressTaken())
1842  return nullptr;
1843 
1844  const Function *F = BB->getParent();
1845  assert(F && "Block must have a parent");
1846  BlockAddress *BA =
1847  F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
1848  assert(BA && "Refcount and block address map disagree!");
1849  return BA;
1850 }
1851 
1852 /// Remove the constant from the constant table.
1853 void BlockAddress::destroyConstantImpl() {
1855  ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
1856  getBasicBlock()->AdjustBlockAddressRefCount(-1);
1857 }
1858 
1859 Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
1860  // This could be replacing either the Basic Block or the Function. In either
1861  // case, we have to remove the map entry.
1862  Function *NewF = getFunction();
1863  BasicBlock *NewBB = getBasicBlock();
1864 
1865  if (From == NewF)
1866  NewF = cast<Function>(To->stripPointerCasts());
1867  else {
1868  assert(From == NewBB && "From does not match any operand");
1869  NewBB = cast<BasicBlock>(To);
1870  }
1871 
1872  // See if the 'new' entry already exists, if not, just update this in place
1873  // and return early.
1874  BlockAddress *&NewBA =
1875  getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1876  if (NewBA)
1877  return NewBA;
1878 
1879  getBasicBlock()->AdjustBlockAddressRefCount(-1);
1880 
1881  // Remove the old entry, this can't cause the map to rehash (just a
1882  // tombstone will get added).
1883  getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1884  getBasicBlock()));
1885  NewBA = this;
1886  setOperand(0, NewF);
1887  setOperand(1, NewBB);
1888  getBasicBlock()->AdjustBlockAddressRefCount(1);
1889 
1890  // If we just want to keep the existing value, then return null.
1891  // Callers know that this means we shouldn't delete this value.
1892  return nullptr;
1893 }
1894 
1897  if (!Equiv)
1898  Equiv = new DSOLocalEquivalent(GV);
1899 
1900  assert(Equiv->getGlobalValue() == GV &&
1901  "DSOLocalFunction does not match the expected global value");
1902  return Equiv;
1903 }
1904 
1905 DSOLocalEquivalent::DSOLocalEquivalent(GlobalValue *GV)
1906  : Constant(GV->getType(), Value::DSOLocalEquivalentVal, &Op<0>(), 1) {
1907  setOperand(0, GV);
1908 }
1909 
1910 /// Remove the constant from the constant table.
1911 void DSOLocalEquivalent::destroyConstantImpl() {
1912  const GlobalValue *GV = getGlobalValue();
1913  GV->getContext().pImpl->DSOLocalEquivalents.erase(GV);
1914 }
1915 
1916 Value *DSOLocalEquivalent::handleOperandChangeImpl(Value *From, Value *To) {
1917  assert(From == getGlobalValue() && "Changing value does not match operand.");
1918  assert(isa<Constant>(To) && "Can only replace the operands with a constant");
1919 
1920  // The replacement is with another global value.
1921  if (const auto *ToObj = dyn_cast<GlobalValue>(To)) {
1922  DSOLocalEquivalent *&NewEquiv =
1924  if (NewEquiv)
1925  return llvm::ConstantExpr::getBitCast(NewEquiv, getType());
1926  }
1927 
1928  // If the argument is replaced with a null value, just replace this constant
1929  // with a null value.
1930  if (cast<Constant>(To)->isNullValue())
1931  return To;
1932 
1933  // The replacement could be a bitcast or an alias to another function. We can
1934  // replace it with a bitcast to the dso_local_equivalent of that function.
1935  auto *Func = cast<Function>(To->stripPointerCastsAndAliases());
1937  if (NewEquiv)
1938  return llvm::ConstantExpr::getBitCast(NewEquiv, getType());
1939 
1940  // Replace this with the new one.
1942  NewEquiv = this;
1943  setOperand(0, Func);
1944 
1945  if (Func->getType() != getType()) {
1946  // It is ok to mutate the type here because this constant should always
1947  // reflect the type of the function it's holding.
1948  mutateType(Func->getType());
1949  }
1950  return nullptr;
1951 }
1952 
1953 //---- ConstantExpr::get() implementations.
1954 //
1955 
1956 /// This is a utility function to handle folding of casts and lookup of the
1957 /// cast in the ExprConstants map. It is used by the various get* methods below.
1959  bool OnlyIfReduced = false) {
1960  assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1961  // Fold a few common cases
1962  if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1963  return FC;
1964 
1965  if (OnlyIfReduced)
1966  return nullptr;
1967 
1968  LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1969 
1970  // Look up the constant in the table first to ensure uniqueness.
1971  ConstantExprKeyType Key(opc, C);
1972 
1973  return pImpl->ExprConstants.getOrCreate(Ty, Key);
1974 }
1975 
1977  bool OnlyIfReduced) {
1979  assert(Instruction::isCast(opc) && "opcode out of range");
1980  assert(C && Ty && "Null arguments to getCast");
1981  assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
1982 
1983  switch (opc) {
1984  default:
1985  llvm_unreachable("Invalid cast opcode");
1986  case Instruction::Trunc:
1987  return getTrunc(C, Ty, OnlyIfReduced);
1988  case Instruction::ZExt:
1989  return getZExt(C, Ty, OnlyIfReduced);
1990  case Instruction::SExt:
1991  return getSExt(C, Ty, OnlyIfReduced);
1992  case Instruction::FPTrunc:
1993  return getFPTrunc(C, Ty, OnlyIfReduced);
1994  case Instruction::FPExt:
1995  return getFPExtend(C, Ty, OnlyIfReduced);
1996  case Instruction::UIToFP:
1997  return getUIToFP(C, Ty, OnlyIfReduced);
1998  case Instruction::SIToFP:
1999  return getSIToFP(C, Ty, OnlyIfReduced);
2000  case Instruction::FPToUI:
2001  return getFPToUI(C, Ty, OnlyIfReduced);
2002  case Instruction::FPToSI:
2003  return getFPToSI(C, Ty, OnlyIfReduced);
2004  case Instruction::PtrToInt:
2005  return getPtrToInt(C, Ty, OnlyIfReduced);
2006  case Instruction::IntToPtr:
2007  return getIntToPtr(C, Ty, OnlyIfReduced);
2008  case Instruction::BitCast:
2009  return getBitCast(C, Ty, OnlyIfReduced);
2010  case Instruction::AddrSpaceCast:
2011  return getAddrSpaceCast(C, Ty, OnlyIfReduced);
2012  }
2013 }
2014 
2016  if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2017  return getBitCast(C, Ty);
2018  return getZExt(C, Ty);
2019 }
2020 
2022  if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2023  return getBitCast(C, Ty);
2024  return getSExt(C, Ty);
2025 }
2026 
2028  if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2029  return getBitCast(C, Ty);
2030  return getTrunc(C, Ty);
2031 }
2032 
2034  assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2035  assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2036  "Invalid cast");
2037 
2038  if (Ty->isIntOrIntVectorTy())
2039  return getPtrToInt(S, Ty);
2040 
2041  unsigned SrcAS = S->getType()->getPointerAddressSpace();
2042  if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
2043  return getAddrSpaceCast(S, Ty);
2044 
2045  return getBitCast(S, Ty);
2046 }
2047 
2049  Type *Ty) {
2050  assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2051  assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2052 
2053  if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2054  return getAddrSpaceCast(S, Ty);
2055 
2056  return getBitCast(S, Ty);
2057 }
2058 
2060  assert(C->getType()->isIntOrIntVectorTy() &&
2061  Ty->isIntOrIntVectorTy() && "Invalid cast");
2062  unsigned SrcBits = C->getType()->getScalarSizeInBits();
2063  unsigned DstBits = Ty->getScalarSizeInBits();
2064  Instruction::CastOps opcode =
2065  (SrcBits == DstBits ? Instruction::BitCast :
2066  (SrcBits > DstBits ? Instruction::Trunc :
2067  (isSigned ? Instruction::SExt : Instruction::ZExt)));
2068  return getCast(opcode, C, Ty);
2069 }
2070 
2072  assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2073  "Invalid cast");
2074  unsigned SrcBits = C->getType()->getScalarSizeInBits();
2075  unsigned DstBits = Ty->getScalarSizeInBits();
2076  if (SrcBits == DstBits)
2077  return C; // Avoid a useless cast
2078  Instruction::CastOps opcode =
2079  (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
2080  return getCast(opcode, C, Ty);
2081 }
2082 
2083 Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
2084 #ifndef NDEBUG
2085  bool fromVec = isa<VectorType>(C->getType());
2086  bool toVec = isa<VectorType>(Ty);
2087 #endif
2088  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2089  assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
2090  assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
2091  assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
2092  "SrcTy must be larger than DestTy for Trunc!");
2093 
2094  return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
2095 }
2096 
2097 Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
2098 #ifndef NDEBUG
2099  bool fromVec = isa<VectorType>(C->getType());
2100  bool toVec = isa<VectorType>(Ty);
2101 #endif
2102  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2103  assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
2104  assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
2105  assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
2106  "SrcTy must be smaller than DestTy for SExt!");
2107 
2108  return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
2109 }
2110 
2111 Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
2112 #ifndef NDEBUG
2113  bool fromVec = isa<VectorType>(C->getType());
2114  bool toVec = isa<VectorType>(Ty);
2115 #endif
2116  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2117  assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
2118  assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
2119  assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
2120  "SrcTy must be smaller than DestTy for ZExt!");
2121 
2122  return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
2123 }
2124 
2125 Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
2126 #ifndef NDEBUG
2127  bool fromVec = isa<VectorType>(C->getType());
2128  bool toVec = isa<VectorType>(Ty);
2129 #endif
2130  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2131  assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2132  C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
2133  "This is an illegal floating point truncation!");
2134  return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
2135 }
2136 
2137 Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
2138 #ifndef NDEBUG
2139  bool fromVec = isa<VectorType>(C->getType());
2140  bool toVec = isa<VectorType>(Ty);
2141 #endif
2142  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2143  assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2144  C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
2145  "This is an illegal floating point extension!");
2146  return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
2147 }
2148 
2149 Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
2150 #ifndef NDEBUG
2151  bool fromVec = isa<VectorType>(C->getType());
2152  bool toVec = isa<VectorType>(Ty);
2153 #endif
2154  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2155  assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
2156  "This is an illegal uint to floating point cast!");
2157  return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
2158 }
2159 
2160 Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
2161 #ifndef NDEBUG
2162  bool fromVec = isa<VectorType>(C->getType());
2163  bool toVec = isa<VectorType>(Ty);
2164 #endif
2165  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2166  assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
2167  "This is an illegal sint to floating point cast!");
2168  return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
2169 }
2170 
2171 Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
2172 #ifndef NDEBUG
2173  bool fromVec = isa<VectorType>(C->getType());
2174  bool toVec = isa<VectorType>(Ty);
2175 #endif
2176  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2177  assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
2178  "This is an illegal floating point to uint cast!");
2179  return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
2180 }
2181 
2182 Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
2183 #ifndef NDEBUG
2184  bool fromVec = isa<VectorType>(C->getType());
2185  bool toVec = isa<VectorType>(Ty);
2186 #endif
2187  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2188  assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
2189  "This is an illegal floating point to sint cast!");
2190  return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
2191 }
2192 
2194  bool OnlyIfReduced) {
2195  assert(C->getType()->isPtrOrPtrVectorTy() &&
2196  "PtrToInt source must be pointer or pointer vector");
2197  assert(DstTy->isIntOrIntVectorTy() &&
2198  "PtrToInt destination must be integer or integer vector");
2199  assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
2200  if (isa<VectorType>(C->getType()))
2201  assert(cast<FixedVectorType>(C->getType())->getNumElements() ==
2202  cast<FixedVectorType>(DstTy)->getNumElements() &&
2203  "Invalid cast between a different number of vector elements");
2204  return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
2205 }
2206 
2208  bool OnlyIfReduced) {
2209  assert(C->getType()->isIntOrIntVectorTy() &&
2210  "IntToPtr source must be integer or integer vector");
2211  assert(DstTy->isPtrOrPtrVectorTy() &&
2212  "IntToPtr destination must be a pointer or pointer vector");
2213  assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
2214  if (isa<VectorType>(C->getType()))
2215  assert(cast<VectorType>(C->getType())->getElementCount() ==
2216  cast<VectorType>(DstTy)->getElementCount() &&
2217  "Invalid cast between a different number of vector elements");
2218  return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
2219 }
2220 
2222  bool OnlyIfReduced) {
2223  assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
2224  "Invalid constantexpr bitcast!");
2225 
2226  // It is common to ask for a bitcast of a value to its own type, handle this
2227  // speedily.
2228  if (C->getType() == DstTy) return C;
2229 
2230  return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
2231 }
2232 
2234  bool OnlyIfReduced) {
2235  assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
2236  "Invalid constantexpr addrspacecast!");
2237 
2238  // Canonicalize addrspacecasts between different pointer types by first
2239  // bitcasting the pointer type and then converting the address space.
2240  PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
2241  PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
2242  if (!SrcScalarTy->hasSameElementTypeAs(DstScalarTy)) {
2244  DstScalarTy, SrcScalarTy->getAddressSpace());
2245  if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
2246  // Handle vectors of pointers.
2247  MidTy = FixedVectorType::get(MidTy,
2248  cast<FixedVectorType>(VT)->getNumElements());
2249  }
2250  C = getBitCast(C, MidTy);
2251  }
2252  return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
2253 }
2254 
2255 Constant *ConstantExpr::get(unsigned Opcode, Constant *C, unsigned Flags,
2256  Type *OnlyIfReducedTy) {
2257  // Check the operands for consistency first.
2258  assert(Instruction::isUnaryOp(Opcode) &&
2259  "Invalid opcode in unary constant expression");
2260 
2261 #ifndef NDEBUG
2262  switch (Opcode) {
2263  case Instruction::FNeg:
2264  assert(C->getType()->isFPOrFPVectorTy() &&
2265  "Tried to create a floating-point operation on a "
2266  "non-floating-point type!");
2267  break;
2268  default:
2269  break;
2270  }
2271 #endif
2272 
2273  if (Constant *FC = ConstantFoldUnaryInstruction(Opcode, C))
2274  return FC;
2275 
2276  if (OnlyIfReducedTy == C->getType())
2277  return nullptr;
2278 
2279  Constant *ArgVec[] = { C };
2280  ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
2281 
2282  LLVMContextImpl *pImpl = C->getContext().pImpl;
2283  return pImpl->ExprConstants.getOrCreate(C->getType(), Key);
2284 }
2285 
2287  unsigned Flags, Type *OnlyIfReducedTy) {
2288  // Check the operands for consistency first.
2289  assert(Instruction::isBinaryOp(Opcode) &&
2290  "Invalid opcode in binary constant expression");
2291  assert(C1->getType() == C2->getType() &&
2292  "Operand types in binary constant expression should match");
2293 
2294 #ifndef NDEBUG
2295  switch (Opcode) {
2296  case Instruction::Add:
2297  case Instruction::Sub:
2298  case Instruction::Mul:
2299  case Instruction::UDiv:
2300  case Instruction::SDiv:
2301  case Instruction::URem:
2302  case Instruction::SRem:
2303  assert(C1->getType()->isIntOrIntVectorTy() &&
2304  "Tried to create an integer operation on a non-integer type!");
2305  break;
2306  case Instruction::FAdd:
2307  case Instruction::FSub:
2308  case Instruction::FMul:
2309  case Instruction::FDiv:
2310  case Instruction::FRem:
2311  assert(C1->getType()->isFPOrFPVectorTy() &&
2312  "Tried to create a floating-point operation on a "
2313  "non-floating-point type!");
2314  break;
2315  case Instruction::And:
2316  case Instruction::Or:
2317  case Instruction::Xor:
2318  assert(C1->getType()->isIntOrIntVectorTy() &&
2319  "Tried to create a logical operation on a non-integral type!");
2320  break;
2321  case Instruction::Shl:
2322  case Instruction::LShr:
2323  case Instruction::AShr:
2324  assert(C1->getType()->isIntOrIntVectorTy() &&
2325  "Tried to create a shift operation on a non-integer type!");
2326  break;
2327  default:
2328  break;
2329  }
2330 #endif
2331 
2332  if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
2333  return FC;
2334 
2335  if (OnlyIfReducedTy == C1->getType())
2336  return nullptr;
2337 
2338  Constant *ArgVec[] = { C1, C2 };
2339  ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
2340 
2341  LLVMContextImpl *pImpl = C1->getContext().pImpl;
2342  return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
2343 }
2344 
2346  // sizeof is implemented as: (i64) gep (Ty*)null, 1
2347  // Note that a non-inbounds gep is used, as null isn't within any object.
2351  return getPtrToInt(GEP,
2352  Type::getInt64Ty(Ty->getContext()));
2353 }
2354 
2356  // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
2357  // Note that a non-inbounds gep is used, as null isn't within any object.
2358  Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
2359  Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
2362  Constant *Indices[2] = { Zero, One };
2363  Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
2364  return getPtrToInt(GEP,
2365  Type::getInt64Ty(Ty->getContext()));
2366 }
2367 
2370  FieldNo));
2371 }
2372 
2374  // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
2375  // Note that a non-inbounds gep is used, as null isn't within any object.
2376  Constant *GEPIdx[] = {
2378  FieldNo
2379  };
2382  return getPtrToInt(GEP,
2383  Type::getInt64Ty(Ty->getContext()));
2384 }
2385 
2387  Constant *C2, bool OnlyIfReduced) {
2388  assert(C1->getType() == C2->getType() && "Op types should be identical!");
2389 
2390  switch (Predicate) {
2391  default: llvm_unreachable("Invalid CmpInst predicate");
2397  case CmpInst::FCMP_TRUE:
2398  return getFCmp(Predicate, C1, C2, OnlyIfReduced);
2399 
2403  case CmpInst::ICMP_SLE:
2404  return getICmp(Predicate, C1, C2, OnlyIfReduced);
2405  }
2406 }
2407 
2409  Type *OnlyIfReducedTy) {
2410  assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
2411 
2413  return SC; // Fold common cases
2414 
2415  if (OnlyIfReducedTy == V1->getType())
2416  return nullptr;
2417 
2418  Constant *ArgVec[] = { C, V1, V2 };
2420 
2421  LLVMContextImpl *pImpl = C->getContext().pImpl;
2422  return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
2423 }
2424 
2426  ArrayRef<Value *> Idxs, bool InBounds,
2427  Optional<unsigned> InRangeIndex,
2428  Type *OnlyIfReducedTy) {
2429  PointerType *OrigPtrTy = cast<PointerType>(C->getType()->getScalarType());
2430  assert(Ty && "Must specify element type");
2431  assert(OrigPtrTy->isOpaqueOrPointeeTypeMatches(Ty));
2432 
2433  if (Constant *FC =
2434  ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
2435  return FC; // Fold a few common cases.
2436 
2437  // Get the result type of the getelementptr!
2438  Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
2439  assert(DestTy && "GEP indices invalid!");
2440  unsigned AS = OrigPtrTy->getAddressSpace();
2441  Type *ReqTy = OrigPtrTy->isOpaque()
2442  ? PointerType::get(OrigPtrTy->getContext(), AS)
2443  : DestTy->getPointerTo(AS);
2444 
2445  auto EltCount = ElementCount::getFixed(0);
2446  if (VectorType *VecTy = dyn_cast<VectorType>(C->getType()))
2447  EltCount = VecTy->getElementCount();
2448  else
2449  for (auto Idx : Idxs)
2450  if (VectorType *VecTy = dyn_cast<VectorType>(Idx->getType()))
2451  EltCount = VecTy->getElementCount();
2452 
2453  if (EltCount.isNonZero())
2454  ReqTy = VectorType::get(ReqTy, EltCount);
2455 
2456  if (OnlyIfReducedTy == ReqTy)
2457  return nullptr;
2458 
2459  // Look up the constant in the table first to ensure uniqueness
2460  std::vector<Constant*> ArgVec;
2461  ArgVec.reserve(1 + Idxs.size());
2462  ArgVec.push_back(C);
2463  auto GTI = gep_type_begin(Ty, Idxs), GTE = gep_type_end(Ty, Idxs);
2464  for (; GTI != GTE; ++GTI) {
2465  auto *Idx = cast<Constant>(GTI.getOperand());
2466  assert(
2467  (!isa<VectorType>(Idx->getType()) ||
2468  cast<VectorType>(Idx->getType())->getElementCount() == EltCount) &&
2469  "getelementptr index type missmatch");
2470 
2471  if (GTI.isStruct() && Idx->getType()->isVectorTy()) {
2472  Idx = Idx->getSplatValue();
2473  } else if (GTI.isSequential() && EltCount.isNonZero() &&
2474  !Idx->getType()->isVectorTy()) {
2475  Idx = ConstantVector::getSplat(EltCount, Idx);
2476  }
2477  ArgVec.push_back(Idx);
2478  }
2479 
2480  unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
2481  if (InRangeIndex && *InRangeIndex < 63)
2482  SubClassOptionalData |= (*InRangeIndex + 1) << 1;
2483  const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
2484  SubClassOptionalData, None, None, Ty);
2485 
2486  LLVMContextImpl *pImpl = C->getContext().pImpl;
2487  return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2488 }
2489 
2491  Constant *RHS, bool OnlyIfReduced) {
2492  assert(LHS->getType() == RHS->getType());
2494  "Invalid ICmp Predicate");
2495 
2496  if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
2497  return FC; // Fold a few common cases...
2498 
2499  if (OnlyIfReduced)
2500  return nullptr;
2501 
2502  // Look up the constant in the table first to ensure uniqueness
2503  Constant *ArgVec[] = { LHS, RHS };
2504  // Get the key type with both the opcode and predicate
2505  const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
2506 
2507  Type *ResultTy = Type::getInt1Ty(LHS->getContext());
2508  if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
2509  ResultTy = VectorType::get(ResultTy, VT->getElementCount());
2510 
2511  LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
2512  return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
2513 }
2514 
2516  Constant *RHS, bool OnlyIfReduced) {
2517  assert(LHS->getType() == RHS->getType());
2519  "Invalid FCmp Predicate");
2520 
2521  if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
2522  return FC; // Fold a few common cases...
2523 
2524  if (OnlyIfReduced)
2525  return nullptr;
2526 
2527  // Look up the constant in the table first to ensure uniqueness
2528  Constant *ArgVec[] = { LHS, RHS };
2529  // Get the key type with both the opcode and predicate
2530  const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
2531 
2532  Type *ResultTy = Type::getInt1Ty(LHS->getContext());
2533  if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
2534  ResultTy = VectorType::get(ResultTy, VT->getElementCount());
2535 
2536  LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
2537  return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
2538 }
2539 
2541  Type *OnlyIfReducedTy) {
2542  assert(Val->getType()->isVectorTy() &&
2543  "Tried to create extractelement operation on non-vector type!");
2544  assert(Idx->getType()->isIntegerTy() &&
2545  "Extractelement index must be an integer type!");
2546 
2548  return FC; // Fold a few common cases.
2549 
2550  Type *ReqTy = cast<VectorType>(Val->getType())->getElementType();
2551  if (OnlyIfReducedTy == ReqTy)
2552  return nullptr;
2553 
2554  // Look up the constant in the table first to ensure uniqueness
2555  Constant *ArgVec[] = { Val, Idx };
2556  const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
2557 
2558  LLVMContextImpl *pImpl = Val->getContext().pImpl;
2559  return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2560 }
2561 
2563  Constant *Idx, Type *OnlyIfReducedTy) {
2564  assert(Val->getType()->isVectorTy() &&
2565  "Tried to create insertelement operation on non-vector type!");
2566  assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() &&
2567  "Insertelement types must match!");
2568  assert(Idx->getType()->isIntegerTy() &&
2569  "Insertelement index must be i32 type!");
2570 
2571  if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2572  return FC; // Fold a few common cases.
2573 
2574  if (OnlyIfReducedTy == Val->getType())
2575  return nullptr;
2576 
2577  // Look up the constant in the table first to ensure uniqueness
2578  Constant *ArgVec[] = { Val, Elt, Idx };
2579  const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
2580 
2581  LLVMContextImpl *pImpl = Val->getContext().pImpl;
2582  return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
2583 }
2584 
2587  Type *OnlyIfReducedTy) {
2589  "Invalid shuffle vector constant expr operands!");
2590 
2592  return FC; // Fold a few common cases.
2593 
2594  unsigned NElts = Mask.size();
2595  auto V1VTy = cast<VectorType>(V1->getType());
2596  Type *EltTy = V1VTy->getElementType();
2597  bool TypeIsScalable = isa<ScalableVectorType>(V1VTy);
2598  Type *ShufTy = VectorType::get(EltTy, NElts, TypeIsScalable);
2599 
2600  if (OnlyIfReducedTy == ShufTy)
2601  return nullptr;
2602 
2603  // Look up the constant in the table first to ensure uniqueness
2604  Constant *ArgVec[] = {V1, V2};
2605  ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec, 0, 0, None, Mask);
2606 
2607  LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2608  return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
2609 }
2610 
2612  ArrayRef<unsigned> Idxs,
2613  Type *OnlyIfReducedTy) {
2614  assert(Agg->getType()->isFirstClassType() &&
2615  "Non-first-class type for constant insertvalue expression");
2616 
2618  Idxs) == Val->getType() &&
2619  "insertvalue indices invalid!");
2620  Type *ReqTy = Val->getType();
2621 
2622  if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2623  return FC;
2624 
2625  if (OnlyIfReducedTy == ReqTy)
2626  return nullptr;
2627 
2628  Constant *ArgVec[] = { Agg, Val };
2629  const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
2630 
2631  LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2632  return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2633 }
2634 
2636  Type *OnlyIfReducedTy) {
2637  assert(Agg->getType()->isFirstClassType() &&
2638  "Tried to create extractelement operation on non-first-class type!");
2639 
2640  Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
2641  (void)ReqTy;
2642  assert(ReqTy && "extractvalue indices invalid!");
2643 
2644  assert(Agg->getType()->isFirstClassType() &&
2645  "Non-first-class type for constant extractvalue expression");
2647  return FC;
2648 
2649  if (OnlyIfReducedTy == ReqTy)
2650  return nullptr;
2651 
2652  Constant *ArgVec[] = { Agg };
2653  const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
2654 
2655  LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2656  return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2657 }
2658 
2659 Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
2660  assert(C->getType()->isIntOrIntVectorTy() &&
2661  "Cannot NEG a nonintegral value!");
2662  return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
2663  C, HasNUW, HasNSW);
2664 }
2665 
2667  assert(C->getType()->isFPOrFPVectorTy() &&
2668  "Cannot FNEG a non-floating-point value!");
2669  return get(Instruction::FNeg, C);
2670 }
2671 
2673  assert(C->getType()->isIntOrIntVectorTy() &&
2674  "Cannot NOT a nonintegral value!");
2675  return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
2676 }
2677 
2679  bool HasNUW, bool HasNSW) {
2680  unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2682  return get(Instruction::Add, C1, C2, Flags);
2683 }
2684 
2686  return get(Instruction::FAdd, C1, C2);
2687 }
2688 
2690  bool HasNUW, bool HasNSW) {
2691  unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2693  return get(Instruction::Sub, C1, C2, Flags);
2694 }
2695 
2697  return get(Instruction::FSub, C1, C2);
2698 }
2699 
2701  bool HasNUW, bool HasNSW) {
2702  unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2704  return get(Instruction::Mul, C1, C2, Flags);
2705 }
2706 
2708  return get(Instruction::FMul, C1, C2);
2709 }
2710 
2712  return get(Instruction::UDiv, C1, C2,
2713  isExact ? PossiblyExactOperator::IsExact : 0);
2714 }
2715 
2717  return get(Instruction::SDiv, C1, C2,
2718  isExact ? PossiblyExactOperator::IsExact : 0);
2719 }
2720 
2722  return get(Instruction::FDiv, C1, C2);
2723 }
2724 
2726  return get(Instruction::URem, C1, C2);
2727 }
2728 
2730  return get(Instruction::SRem, C1, C2);
2731 }
2732 
2734  return get(Instruction::FRem, C1, C2);
2735 }
2736 
2738  return get(Instruction::And, C1, C2);
2739 }
2740 
2742  return get(Instruction::Or, C1, C2);
2743 }
2744 
2746  return get(Instruction::Xor, C1, C2);
2747 }
2748 
2751  return getSelect(Cmp, C1, C2);
2752 }
2753 
2755  bool HasNUW, bool HasNSW) {
2756  unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2758  return get(Instruction::Shl, C1, C2, Flags);
2759 }
2760 
2762  return get(Instruction::LShr, C1, C2,
2763  isExact ? PossiblyExactOperator::IsExact : 0);
2764 }
2765 
2767  return get(Instruction::AShr, C1, C2,
2768  isExact ? PossiblyExactOperator::IsExact : 0);
2769 }
2770 
2772  Type *Ty = C->getType();
2773  const APInt *IVal;
2774  if (match(C, m_APInt(IVal)) && IVal->isPowerOf2())
2775  return ConstantInt::get(Ty, IVal->logBase2());
2776 
2777  // FIXME: We can extract pow of 2 of splat constant for scalable vectors.
2778  auto *VecTy = dyn_cast<FixedVectorType>(Ty);
2779  if (!VecTy)
2780  return nullptr;
2781 
2783  for (unsigned I = 0, E = VecTy->getNumElements(); I != E; ++I) {
2784  Constant *Elt = C->getAggregateElement(I);
2785  if (!Elt)
2786  return nullptr;
2787  // Note that log2(iN undef) is *NOT* iN undef, because log2(iN undef) u< N.
2788  if (isa<UndefValue>(Elt)) {
2789  Elts.push_back(Constant::getNullValue(Ty->getScalarType()));
2790  continue;
2791  }
2792  if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())
2793  return nullptr;
2794  Elts.push_back(ConstantInt::get(Ty->getScalarType(), IVal->logBase2()));
2795  }
2796 
2797  return ConstantVector::get(Elts);
2798 }
2799 
2801  bool AllowRHSConstant) {
2802  assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed");
2803 
2804  // Commutative opcodes: it does not matter if AllowRHSConstant is set.
2805  if (Instruction::isCommutative(Opcode)) {
2806  switch (Opcode) {
2807  case Instruction::Add: // X + 0 = X
2808  case Instruction::Or: // X | 0 = X
2809  case Instruction::Xor: // X ^ 0 = X
2810  return Constant::getNullValue(Ty);
2811  case Instruction::Mul: // X * 1 = X
2812  return ConstantInt::get(Ty, 1);
2813  case Instruction::And: // X & -1 = X
2814  return Constant::getAllOnesValue(Ty);
2815  case Instruction::FAdd: // X + -0.0 = X
2816  // TODO: If the fadd has 'nsz', should we return +0.0?
2817  return ConstantFP::getNegativeZero(Ty);
2818  case Instruction::FMul: // X * 1.0 = X
2819  return ConstantFP::get(Ty, 1.0);
2820  default:
2821  llvm_unreachable("Every commutative binop has an identity constant");
2822  }
2823  }
2824 
2825  // Non-commutative opcodes: AllowRHSConstant must be set.
2826  if (!AllowRHSConstant)
2827  return nullptr;
2828 
2829  switch (Opcode) {
2830  case Instruction::Sub: // X - 0 = X
2831  case Instruction::Shl: // X << 0 = X
2832  case Instruction::LShr: // X >>u 0 = X
2833  case Instruction::AShr: // X >> 0 = X
2834  case Instruction::FSub: // X - 0.0 = X
2835  return Constant::getNullValue(Ty);
2836  case Instruction::SDiv: // X / 1 = X
2837  case Instruction::UDiv: // X /u 1 = X
2838  return ConstantInt::get(Ty, 1);
2839  case Instruction::FDiv: // X / 1.0 = X
2840  return ConstantFP::get(Ty, 1.0);
2841  default:
2842  return nullptr;
2843  }
2844 }
2845 
2847  switch (Opcode) {
2848  default:
2849  // Doesn't have an absorber.
2850  return nullptr;
2851 
2852  case Instruction::Or:
2853  return Constant::getAllOnesValue(Ty);
2854 
2855  case Instruction::And:
2856  case Instruction::Mul:
2857  return Constant::getNullValue(Ty);
2858  }
2859 }
2860 
2861 /// Remove the constant from the constant table.
2862 void ConstantExpr::destroyConstantImpl() {
2863  getType()->getContext().pImpl->ExprConstants.remove(this);
2864 }
2865 
2866 const char *ConstantExpr::getOpcodeName() const {
2868 }
2869 
2870 GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2871  Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2872  : ConstantExpr(DestTy, Instruction::GetElementPtr,
2874  (IdxList.size() + 1),
2875  IdxList.size() + 1),
2876  SrcElementTy(SrcElementTy),
2877  ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
2878  Op<0>() = C;
2879  Use *OperandList = getOperandList();
2880  for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2881  OperandList[i+1] = IdxList[i];
2882 }
2883 
2885  return SrcElementTy;
2886 }
2887 
2889  return ResElementTy;
2890 }
2891 
2892 //===----------------------------------------------------------------------===//
2893 // ConstantData* implementations
2894 
2896  if (ArrayType *ATy = dyn_cast<ArrayType>(getType()))
2897  return ATy->getElementType();
2898  return cast<VectorType>(getType())->getElementType();
2899 }
2900 
2902  return StringRef(DataElements, getNumElements()*getElementByteSize());
2903 }
2904 
2906  if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() || Ty->isDoubleTy())
2907  return true;
2908  if (auto *IT = dyn_cast<IntegerType>(Ty)) {
2909  switch (IT->getBitWidth()) {
2910  case 8:
2911  case 16:
2912  case 32:
2913  case 64:
2914  return true;
2915  default: break;
2916  }
2917  }
2918  return false;
2919 }
2920 
2922  if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
2923  return AT->getNumElements();
2924  return cast<FixedVectorType>(getType())->getNumElements();
2925 }
2926 
2927 
2929  return getElementType()->getPrimitiveSizeInBits()/8;
2930 }
2931 
2932 /// Return the start of the specified element.
2933 const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
2934  assert(Elt < getNumElements() && "Invalid Elt");
2935  return DataElements+Elt*getElementByteSize();
2936 }
2937 
2938 
2939 /// Return true if the array is empty or all zeros.
2940 static bool isAllZeros(StringRef Arr) {
2941  for (char I : Arr)
2942  if (I != 0)
2943  return false;
2944  return true;
2945 }
2946 
2947 /// This is the underlying implementation of all of the
2948 /// ConstantDataSequential::get methods. They all thunk down to here, providing
2949 /// the correct element type. We take the bytes in as a StringRef because
2950 /// we *want* an underlying "char*" to avoid TBAA type punning violations.
2952 #ifndef NDEBUG
2953  if (ArrayType *ATy = dyn_cast<ArrayType>(Ty))
2954  assert(isElementTypeCompatible(ATy->getElementType()));
2955  else
2956  assert(isElementTypeCompatible(cast<VectorType>(Ty)->getElementType()));
2957 #endif
2958  // If the elements are all zero or there are no elements, return a CAZ, which
2959  // is more dense and canonical.
2960  if (isAllZeros(Elements))
2961  return ConstantAggregateZero::get(Ty);
2962 
2963  // Do a lookup to see if we have already formed one of these.
2964  auto &Slot =
2965  *Ty->getContext()
2966  .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
2967  .first;
2968 
2969  // The bucket can point to a linked list of different CDS's that have the same
2970  // body but different types. For example, 0,0,0,1 could be a 4 element array
2971  // of i8, or a 1-element array of i32. They'll both end up in the same
2972  /// StringMap bucket, linked up by their Next pointers. Walk the list.
2973  std::unique_ptr<ConstantDataSequential> *Entry = &Slot.second;
2974  for (; *Entry; Entry = &(*Entry)->Next)
2975  if ((*Entry)->getType() == Ty)
2976  return Entry->get();
2977 
2978  // Okay, we didn't get a hit. Create a node of the right class, link it in,
2979  // and return it.
2980  if (isa<ArrayType>(Ty)) {
2981  // Use reset because std::make_unique can't access the constructor.
2982  Entry->reset(new ConstantDataArray(Ty, Slot.first().data()));
2983  return Entry->get();
2984  }
2985 
2986  assert(isa<VectorType>(Ty));
2987  // Use reset because std::make_unique can't access the constructor.
2988  Entry->reset(new ConstantDataVector(Ty, Slot.first().data()));
2989  return Entry->get();
2990 }
2991 
2992 void ConstantDataSequential::destroyConstantImpl() {
2993  // Remove the constant from the StringMap.
2996 
2997  auto Slot = CDSConstants.find(getRawDataValues());
2998 
2999  assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
3000 
3001  std::unique_ptr<ConstantDataSequential> *Entry = &Slot->getValue();
3002 
3003  // Remove the entry from the hash table.
3004  if (!(*Entry)->Next) {
3005  // If there is only one value in the bucket (common case) it must be this
3006  // entry, and removing the entry should remove the bucket completely.
3007  assert(Entry->get() == this && "Hash mismatch in ConstantDataSequential");
3008  getContext().pImpl->CDSConstants.erase(Slot);
3009  return;
3010  }
3011 
3012  // Otherwise, there are multiple entries linked off the bucket, unlink the
3013  // node we care about but keep the bucket around.
3014  while (true) {
3015  std::unique_ptr<ConstantDataSequential> &Node = *Entry;
3016  assert(Node && "Didn't find entry in its uniquing hash table!");
3017  // If we found our entry, unlink it from the list and we're done.
3018  if (Node.get() == this) {
3019  Node = std::move(Node->Next);
3020  return;
3021  }
3022 
3023  Entry = &Node->Next;
3024  }
3025 }
3026 
3027 /// getFP() constructors - Return a constant of array type with a float
3028 /// element type taken from argument `ElementType', and count taken from
3029 /// argument `Elts'. The amount of bits of the contained type must match the
3030 /// number of bits of the type contained in the passed in ArrayRef.
3031 /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
3032 /// that this can return a ConstantAggregateZero object.
3034  assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) &&
3035  "Element type is not a 16-bit float type");
3036  Type *Ty = ArrayType::get(ElementType, Elts.size());
3037  const char *Data = reinterpret_cast<const char *>(Elts.data());
3038  return getImpl(StringRef(Data, Elts.size() * 2), Ty);
3039 }
3041  assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type");
3042  Type *Ty = ArrayType::get(ElementType, Elts.size());
3043  const char *Data = reinterpret_cast<const char *>(Elts.data());
3044  return getImpl(StringRef(Data, Elts.size() * 4), Ty);
3045 }
3047  assert(ElementType->isDoubleTy() &&
3048  "Element type is not a 64-bit float type");
3049  Type *Ty = ArrayType::get(ElementType, Elts.size());
3050  const char *Data = reinterpret_cast<const char *>(Elts.data());
3051  return getImpl(StringRef(Data, Elts.size() * 8), Ty);
3052 }
3053 
3055  StringRef Str, bool AddNull) {
3056  if (!AddNull) {
3057  const uint8_t *Data = Str.bytes_begin();
3058  return get(Context, makeArrayRef(Data, Str.size()));
3059  }
3060 
3061  SmallVector<uint8_t, 64> ElementVals;
3062  ElementVals.append(Str.begin(), Str.end());
3063  ElementVals.push_back(0);
3064  return get(Context, ElementVals);
3065 }
3066 
3067 /// get() constructors - Return a constant with vector type with an element
3068 /// count and element type matching the ArrayRef passed in. Note that this
3069 /// can return a ConstantAggregateZero object.
3071  auto *Ty = FixedVectorType::get(Type::getInt8Ty(Context), Elts.size());
3072  const char *Data = reinterpret_cast<const char *>(Elts.data());
3073  return getImpl(StringRef(Data, Elts.size() * 1), Ty);
3074 }
3076  auto *Ty = FixedVectorType::get(Type::getInt16Ty(Context), Elts.size());
3077  const char *Data = reinterpret_cast<const char *>(Elts.data());
3078  return getImpl(StringRef(Data, Elts.size() * 2), Ty);
3079 }
3081  auto *Ty = FixedVectorType::get(Type::getInt32Ty(Context), Elts.size());
3082  const char *Data = reinterpret_cast<const char *>(Elts.data());
3083  return getImpl(StringRef(Data, Elts.size() * 4), Ty);
3084 }
3086  auto *Ty = FixedVectorType::get(Type::getInt64Ty(Context), Elts.size());
3087  const char *Data = reinterpret_cast<const char *>(Elts.data());
3088  return getImpl(StringRef(Data, Elts.size() * 8), Ty);
3089 }
3091  auto *Ty = FixedVectorType::get(Type::getFloatTy(Context), Elts.size());
3092  const char *Data = reinterpret_cast<const char *>(Elts.data());
3093  return getImpl(StringRef(Data, Elts.size() * 4), Ty);
3094 }
3096  auto *Ty = FixedVectorType::get(Type::getDoubleTy(Context), Elts.size());
3097  const char *Data = reinterpret_cast<const char *>(Elts.data());
3098  return getImpl(StringRef(Data, Elts.size() * 8), Ty);
3099 }
3100 
3101 /// getFP() constructors - Return a constant of vector type with a float
3102 /// element type taken from argument `ElementType', and count taken from
3103 /// argument `Elts'. The amount of bits of the contained type must match the
3104 /// number of bits of the type contained in the passed in ArrayRef.
3105 /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note
3106 /// that this can return a ConstantAggregateZero object.
3108  ArrayRef<uint16_t> Elts) {
3109  assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) &&
3110  "Element type is not a 16-bit float type");
3111  auto *Ty = FixedVectorType::get(ElementType, Elts.size());
3112  const char *Data = reinterpret_cast<const char *>(Elts.data());
3113  return getImpl(StringRef(Data, Elts.size() * 2), Ty);
3114 }
3116  ArrayRef<uint32_t> Elts) {
3117  assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type");
3118  auto *Ty = FixedVectorType::get(ElementType, Elts.size());
3119  const char *Data = reinterpret_cast<const char *>(Elts.data());
3120  return getImpl(StringRef(Data, Elts.size() * 4), Ty);
3121 }
3123  ArrayRef<uint64_t> Elts) {
3124  assert(ElementType->isDoubleTy() &&
3125  "Element type is not a 64-bit float type");
3126  auto *Ty = FixedVectorType::get(ElementType, Elts.size());
3127  const char *Data = reinterpret_cast<const char *>(Elts.data());
3128  return getImpl(StringRef(Data, Elts.size() * 8), Ty);
3129 }
3130 
3133  "Element type not compatible with ConstantData");
3134  if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
3135  if (CI->getType()->isIntegerTy(8)) {
3136  SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
3137  return get(V->getContext(), Elts);
3138  }
3139  if (CI->getType()->isIntegerTy(16)) {
3140  SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
3141  return get(V->getContext(), Elts);
3142  }
3143  if (CI->getType()->isIntegerTy(32)) {
3144  SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
3145  return get(V->getContext(), Elts);
3146  }
3147  assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
3148  SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
3149  return get(V->getContext(), Elts);
3150  }
3151 
3152  if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
3153  if (CFP->getType()->isHalfTy()) {
3155  NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3156  return getFP(V->getType(), Elts);
3157  }
3158  if (CFP->getType()->isBFloatTy()) {
3160  NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3161  return getFP(V->getType(), Elts);
3162  }
3163  if (CFP->getType()->isFloatTy()) {
3165  NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3166  return getFP(V->getType(), Elts);
3167  }
3168  if (CFP->getType()->isDoubleTy()) {
3170  NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
3171  return getFP(V->getType(), Elts);
3172  }
3173  }
3175 }
3176 
3177 
3178 uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
3179  assert(isa<IntegerType>(getElementType()) &&
3180  "Accessor can only be used when element is an integer");
3181  const char *EltPtr = getElementPointer(Elt);
3182 
3183  // The data is stored in host byte order, make sure to cast back to the right
3184  // type to load with the right endianness.
3185  switch (getElementType()->getIntegerBitWidth()) {
3186  default: llvm_unreachable("Invalid bitwidth for CDS");
3187  case 8:
3188  return *reinterpret_cast<const uint8_t *>(EltPtr);
3189  case 16:
3190  return *reinterpret_cast<const uint16_t *>(EltPtr);
3191  case 32:
3192  return *reinterpret_cast<const uint32_t *>(EltPtr);
3193  case 64:
3194  return *reinterpret_cast<const uint64_t *>(EltPtr);
3195  }
3196 }
3197 
3199  assert(isa<IntegerType>(getElementType()) &&
3200  "Accessor can only be used when element is an integer");
3201  const char *EltPtr = getElementPointer(Elt);
3202 
3203  // The data is stored in host byte order, make sure to cast back to the right
3204  // type to load with the right endianness.
3205  switch (getElementType()->getIntegerBitWidth()) {
3206  default: llvm_unreachable("Invalid bitwidth for CDS");
3207  case 8: {
3208  auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
3209  return APInt(8, EltVal);
3210  }
3211  case 16: {
3212  auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
3213  return APInt(16, EltVal);
3214  }
3215  case 32: {
3216  auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
3217  return APInt(32, EltVal);
3218  }
3219  case 64: {
3220  auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
3221  return APInt(64, EltVal);
3222  }
3223  }
3224 }
3225 
3227  const char *EltPtr = getElementPointer(Elt);
3228 
3229  switch (getElementType()->getTypeID()) {
3230  default:
3231  llvm_unreachable("Accessor can only be used when element is float/double!");
3232  case Type::HalfTyID: {
3233  auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
3234  return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
3235  }
3236  case Type::BFloatTyID: {
3237  auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
3238  return APFloat(APFloat::BFloat(), APInt(16, EltVal));
3239  }
3240  case Type::FloatTyID: {
3241  auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
3242  return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
3243  }
3244  case Type::DoubleTyID: {
3245  auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
3246  return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
3247  }
3248  }
3249 }
3250 
3252  assert(getElementType()->isFloatTy() &&
3253  "Accessor can only be used when element is a 'float'");
3254  return *reinterpret_cast<const float *>(getElementPointer(Elt));
3255 }
3256 
3258  assert(getElementType()->isDoubleTy() &&
3259  "Accessor can only be used when element is a 'float'");
3260  return *reinterpret_cast<const double *>(getElementPointer(Elt));
3261 }
3262 
3264  if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() ||
3265  getElementType()->isFloatTy() || getElementType()->isDoubleTy())
3267 
3269 }
3270 
3271 bool ConstantDataSequential::isString(unsigned CharSize) const {
3272  return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
3273 }
3274 
3276  if (!isString())
3277  return false;
3278 
3279  StringRef Str = getAsString();
3280 
3281  // The last value must be nul.
3282  if (Str.back() != 0) return false;
3283 
3284  // Other elements must be non-nul.
3285  return Str.drop_back().find(0) == StringRef::npos;
3286 }
3287 
3288 bool ConstantDataVector::isSplatData() const {
3289  const char *Base = getRawDataValues().data();
3290 
3291  // Compare elements 1+ to the 0'th element.
3292  unsigned EltSize = getElementByteSize();
3293  for (unsigned i = 1, e = getNumElements(); i != e; ++i)
3294  if (memcmp(Base, Base+i*EltSize, EltSize))
3295  return false;
3296 
3297  return true;
3298 }
3299 
3301  if (!IsSplatSet) {
3302  IsSplatSet = true;
3303  IsSplat = isSplatData();
3304  }
3305  return IsSplat;
3306 }
3307 
3309  // If they're all the same, return the 0th one as a representative.
3310  return isSplat() ? getElementAsConstant(0) : nullptr;
3311 }
3312 
3313 //===----------------------------------------------------------------------===//
3314 // handleOperandChange implementations
3315 
3316 /// Update this constant array to change uses of
3317 /// 'From' to be uses of 'To'. This must update the uniquing data structures
3318 /// etc.
3319 ///
3320 /// Note that we intentionally replace all uses of From with To here. Consider
3321 /// a large array that uses 'From' 1000 times. By handling this case all here,
3322 /// ConstantArray::handleOperandChange is only invoked once, and that
3323 /// single invocation handles all 1000 uses. Handling them one at a time would
3324 /// work, but would be really slow because it would have to unique each updated
3325 /// array instance.
3326 ///
3328  Value *Replacement = nullptr;
3329  switch (getValueID()) {
3330  default:
3331  llvm_unreachable("Not a constant!");
3332 #define HANDLE_CONSTANT(Name) \
3333  case Value::Name##Val: \
3334  Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
3335  break;
3336 #include "llvm/IR/Value.def"
3337  }
3338 
3339  // If handleOperandChangeImpl returned nullptr, then it handled
3340  // replacing itself and we don't want to delete or replace anything else here.
3341  if (!Replacement)
3342  return;
3343 
3344  // I do need to replace this with an existing value.
3345  assert(Replacement != this && "I didn't contain From!");
3346 
3347  // Everyone using this now uses the replacement.
3348  replaceAllUsesWith(Replacement);
3349 
3350  // Delete the old constant!
3351  destroyConstant();
3352 }
3353 
3354 Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
3355  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
3356  Constant *ToC = cast<Constant>(To);
3357 
3359  Values.reserve(getNumOperands()); // Build replacement array.
3360 
3361  // Fill values with the modified operands of the constant array. Also,
3362  // compute whether this turns into an all-zeros array.
3363  unsigned NumUpdated = 0;
3364 
3365  // Keep track of whether all the values in the array are "ToC".
3366  bool AllSame = true;
3367  Use *OperandList = getOperandList();
3368  unsigned OperandNo = 0;
3369  for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
3370  Constant *Val = cast<Constant>(O->get());
3371  if (Val == From) {
3372  OperandNo = (O - OperandList);
3373  Val = ToC;
3374  ++NumUpdated;
3375  }
3376  Values.push_back(Val);
3377  AllSame &= Val == ToC;
3378  }
3379 
3380  if (AllSame && ToC->isNullValue())
3382 
3383  if (AllSame && isa<UndefValue>(ToC))
3384  return UndefValue::get(getType());
3385 
3386  // Check for any other type of constant-folding.
3387  if (Constant *C = getImpl(getType(), Values))
3388  return C;
3389 
3390  // Update to the new value.
3392  Values, this, From, ToC, NumUpdated, OperandNo);
3393 }
3394 
3395 Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
3396  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
3397  Constant *ToC = cast<Constant>(To);
3398 
3399  Use *OperandList = getOperandList();
3400 
3402  Values.reserve(getNumOperands()); // Build replacement struct.
3403 
3404  // Fill values with the modified operands of the constant struct. Also,
3405  // compute whether this turns into an all-zeros struct.
3406  unsigned NumUpdated = 0;
3407  bool AllSame = true;
3408  unsigned OperandNo = 0;
3409  for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
3410  Constant *Val = cast<Constant>(O->get());
3411  if (Val == From) {
3412  OperandNo = (O - OperandList);
3413  Val = ToC;
3414  ++NumUpdated;
3415  }
3416  Values.push_back(Val);
3417  AllSame &= Val == ToC;
3418  }
3419 
3420  if (AllSame && ToC->isNullValue())
3422 
3423  if (AllSame && isa<UndefValue>(ToC))
3424  return UndefValue::get(getType());
3425 
3426  // Update to the new value.
3428  Values, this, From, ToC, NumUpdated, OperandNo);
3429 }
3430 
3431 Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
3432  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
3433  Constant *ToC = cast<Constant>(To);
3434 
3436  Values.reserve(getNumOperands()); // Build replacement array...
3437  unsigned NumUpdated = 0;
3438  unsigned OperandNo = 0;
3439  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3440  Constant *Val = getOperand(i);
3441  if (Val == From) {
3442  OperandNo = i;
3443  ++NumUpdated;
3444  Val = ToC;
3445  }
3446  Values.push_back(Val);
3447  }
3448 
3449  if (Constant *C = getImpl(Values))
3450  return C;
3451 
3452  // Update to the new value.
3454  Values, this, From, ToC, NumUpdated, OperandNo);
3455 }
3456 
3457 Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
3458  assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
3459  Constant *To = cast<Constant>(ToV);
3460 
3462  unsigned NumUpdated = 0;
3463  unsigned OperandNo = 0;
3464  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3465  Constant *Op = getOperand(i);
3466  if (Op == From) {
3467  OperandNo = i;
3468  ++NumUpdated;
3469  Op = To;
3470  }
3471  NewOps.push_back(Op);
3472  }
3473  assert(NumUpdated && "I didn't contain From!");
3474 
3475  if (Constant *C = getWithOperands(NewOps, getType(), true))
3476  return C;
3477 
3478  // Update to the new value.
3479  return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
3480  NewOps, this, From, To, NumUpdated, OperandNo);
3481 }
3482 
3484  SmallVector<Value *, 4> ValueOperands(operands());
3485  ArrayRef<Value*> Ops(ValueOperands);
3486 
3487  switch (getOpcode()) {
3488  case Instruction::Trunc:
3489  case Instruction::ZExt:
3490  case Instruction::SExt:
3491  case Instruction::FPTrunc:
3492  case Instruction::FPExt:
3493  case Instruction::UIToFP:
3494  case Instruction::SIToFP:
3495  case Instruction::FPToUI:
3496  case Instruction::FPToSI:
3497  case Instruction::PtrToInt:
3498  case Instruction::IntToPtr:
3499  case Instruction::BitCast:
3500  case Instruction::AddrSpaceCast:
3502  Ops[0], getType());
3503  case Instruction::Select:
3504  return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
3505  case Instruction::InsertElement:
3506  return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
3507  case Instruction::ExtractElement:
3508  return ExtractElementInst::Create(Ops[0], Ops[1]);
3509  case Instruction::InsertValue:
3510  return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
3511  case Instruction::ExtractValue:
3512  return ExtractValueInst::Create(Ops[0], getIndices());
3513  case Instruction::ShuffleVector:
3514  return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask());
3515 
3516  case Instruction::GetElementPtr: {
3517  const auto *GO = cast<GEPOperator>(this);
3518  if (GO->isInBounds())
3519  return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
3520  Ops[0], Ops.slice(1));
3521  return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
3522  Ops.slice(1));
3523  }
3524  case Instruction::ICmp:
3525  case Instruction::FCmp:
3527  (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
3528  case Instruction::FNeg:
3530  default:
3531  assert(getNumOperands() == 2 && "Must be binary operator?");
3532  BinaryOperator *BO =
3534  Ops[0], Ops[1]);
3535  if (isa<OverflowingBinaryOperator>(BO)) {
3540  }
3541  if (isa<PossiblyExactOperator>(BO))
3543  return BO;
3544  }
3545 }
i
i
Definition: README.txt:29
llvm::lltok::APFloat
@ APFloat
Definition: LLToken.h:491
llvm::CmpInst::FCMP_ULE
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:735
llvm::Type::ArrayTyID
@ ArrayTyID
Arrays.
Definition: Type.h:75
llvm::LLVMContext::pImpl
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:70
llvm::Type::FloatTyID
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
llvm::Constant::isAllOnesValue
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
Definition: Constants.cpp:91
llvm::Value::const_user_iterator
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:392
llvm::Value::stripPointerCastsAndAliases
const Value * stripPointerCastsAndAliases() const
Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
Definition: Value.cpp:679
MathExtras.h
llvm::Type::DoubleTyID
@ DoubleTyID
64-bit floating point type
Definition: Type.h:59
llvm::Constant::isFiniteNonZeroFP
bool isFiniteNonZeroFP() const
Return true if this is a finite and non-zero floating-point scalar constant or a fixed width vector c...
Definition: Constants.cpp:198
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::ConstantDataSequential::getElementByteSize
uint64_t getElementByteSize() const
Return the size (in bytes) of each element in the array/vector.
Definition: Constants.cpp:2928
llvm::Constant::isNormalFP
bool isNormalFP() const
Return true if this is a normal (as opposed to denormal, infinity, nan, or zero) floating-point scala...
Definition: Constants.cpp:219
llvm::Constant::isNaN
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:261
llvm::ConstantExpr::getFDiv
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2721
llvm::Type::getInt1Ty
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:200
llvm::ConstantExpr::getExtractElement
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2540
llvm::deleteConstant
void deleteConstant(Constant *C)
Definition: Constants.cpp:500
llvm::CmpInst::ICMP_EQ
@ ICMP_EQ
equal
Definition: InstrTypes.h:741
llvm::LLVMContextImpl::BlockAddresses
DenseMap< std::pair< const Function *, const BasicBlock * >, BlockAddress * > BlockAddresses
Definition: LLVMContextImpl.h:1415
llvm::ConstantExpr::getNot
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2672
llvm::StructType::get
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:372
llvm::ConstantFP::isValueValidForType
static bool isValueValidForType(Type *Ty, const APFloat &V)
Return true if Ty is big enough to represent V.
Definition: Constants.cpp:1602
llvm::ConstantExpr::getZExtOrBitCast
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2015
llvm::Instruction::UnaryOps
UnaryOps
Definition: Instruction.h:780
llvm::ConstantExpr::getSIToFP
static Constant * getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2160
llvm::generic_gep_type_iterator
Definition: GetElementPtrTypeIterator.h:31
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:720
llvm::User::operands
op_range operands()
Definition: User.h:242
llvm::Value::SubclassOptionalData
unsigned char SubclassOptionalData
Hold subclass data that can be dropped.
Definition: Value.h:91
llvm::Constant::containsPoisonElement
bool containsPoisonElement() const
Return true if this is a vector constant that includes any poison elements.
Definition: Constants.cpp:331
llvm::ConstantExpr::getOpcodeName
const char * getOpcodeName() const
Return a string representation for an opcode.
Definition: Constants.cpp:2866
llvm::ConstantAggregateZero::getSequentialElement
Constant * getSequentialElement() const
If this CAZ has array or vector type, return a zero with the right element type.
Definition: Constants.cpp:1083
isAllZeros
static bool isAllZeros(StringRef Arr)
Return true if the array is empty or all zeros.
Definition: Constants.cpp:2940
llvm::ElementCount
Definition: TypeSize.h:386
llvm::APFloatBase::IEEEsingle
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:170
llvm::ConstantUniqueMap::replaceOperandsInPlace
ConstantClass * replaceOperandsInPlace(ArrayRef< Constant * > Operands, ConstantClass *CP, Value *From, Constant *To, unsigned NumUpdated=0, unsigned OperandNo=~0u)
Definition: ConstantsContext.h:745
llvm::LLVMContextImpl::FPConstants
FPMapTy FPConstants
Definition: LLVMContextImpl.h:1370
llvm::ConstantExpr::getZExt
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2111
llvm::Function
Definition: Function.h:61
getFunction
static Function * getFunction(Constant *C)
Definition: Evaluator.cpp:255
llvm::ISD::ConstantFP
@ ConstantFP
Definition: ISDOpcodes.h:77
llvm::Instruction::isCast
bool isCast() const
Definition: Instruction.h:168
llvm::ConstantStruct::get
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1326
llvm::LLVMContextImpl::UVConstants
DenseMap< Type *, std::unique_ptr< UndefValue > > UVConstants
Definition: LLVMContextImpl.h:1408
rangeOnlyContains
static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt)
Definition: Constants.cpp:1180
llvm::ConstantExpr::getFRem
static Constant * getFRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2733
llvm::APInt::isPowerOf2
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:469
llvm::PointerType::get
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:693
llvm::ConstantExpr::getFPCast
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:2071
C1
instcombine should handle this C2 when C1
Definition: README.txt:263
llvm::ConstantFoldSelectInstruction
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Definition: ConstantFold.cpp:770
GetElementPtrTypeIterator.h
llvm::Type::getScalarType
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:319
llvm::ConstantExpr::getOpcode
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
Definition: Constants.h:1267
llvm::PoisonValue::getSequentialElement
PoisonValue * getSequentialElement() const
If this poison has array or vector type, return a poison with the right element type.
Definition: Constants.cpp:1153
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:60
llvm::ConstantExpr::getSExt
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2097
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
getImpl
static LazyValueInfoImpl & getImpl(void *&PImpl, AssumptionCache *AC, const Module *M)
This lazily constructs the LazyValueInfoImpl.
Definition: LazyValueInfo.cpp:1507
llvm::ConstantExpr::getICmp
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:2490
llvm::PoisonValue::getElementValue
PoisonValue * getElementValue(Constant *C) const
Return an poison of the right value for the specified GEP index if we can, otherwise return null (e....
Definition: Constants.cpp:1163
llvm::generic_gep_type_iterator::getSequentialNumElements
uint64_t getSequentialNumElements() const
Definition: GetElementPtrTypeIterator.h:131
llvm::CmpInst::FCMP_ONE
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:728
llvm::ConstantDataSequential::getElementAsInteger
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:3178
llvm::getSplatValue
Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
Definition: VectorUtils.cpp:360
ErrorHandling.h
llvm::ConstantExpr::getPredicate
unsigned getPredicate() const
Return the ICMP or FCMP predicate value.
Definition: Constants.cpp:1499
llvm::Type::getPointerAddressSpace
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:733
ManagedStatic.h
llvm::CastInst::castIsValid
static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy)
This method can be used to determine if a cast from SrcTy to DstTy using Opcode op is valid or not.
Definition: Instructions.cpp:3351
llvm::InsertValueInst::Create
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:2494
llvm::CastInst::Create
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
Definition: Instructions.cpp:2966
llvm::PointerType::getAddressSpace
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:686
llvm::ConstantAggregate
Base class for aggregate constants (with operands).
Definition: Constants.h:385
llvm::CmpInst::ICMP_NE
@ ICMP_NE
not equal
Definition: InstrTypes.h:742
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2221
R600_InstFlag::FC
@ FC
Definition: R600Defines.h:32
llvm::ExtractValueInst::getIndexedType
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...
Definition: Instructions.cpp:2388
llvm::LLVMContextImpl::ExprConstants
ConstantUniqueMap< ConstantExpr > ExprConstants
Definition: LLVMContextImpl.h:1419
llvm::BlockAddress::getBasicBlock
BasicBlock * getBasicBlock() const
Definition: Constants.h:877
memcmp
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:912
llvm::ConstantFP::getInfinity
static Constant * getInfinity(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1054
llvm::ConstantExpr::getSelect
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:2408
llvm::Type::getTypeID
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:135
llvm::ConstantExpr::getInsertValue
static Constant * getInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2611
llvm::APFloatBase::x87DoubleExtended
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
Definition: APFloat.cpp:179
llvm::Type::isFPOrFPVectorTy
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:189
llvm::ConstantDataVector::get
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:3070
llvm::ConstantDataSequential::getElementAsFloat
float getElementAsFloat(unsigned i) const
If this is an sequential container of floats, return the specified element as a float.
Definition: Constants.cpp:3251
llvm::InsertElementInst::Create
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1953
llvm::ConstantAggregateZero::getElementCount
ElementCount getElementCount() const
Return the number of elements in the array, vector, or struct.
Definition: Constants.cpp:1105
llvm::ConstantExpr::getOffsetOf
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:2368
llvm::CmpInst::ICMP_SGT
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:747
llvm::StringMap::end
iterator end()
Definition: StringMap.h:205
llvm::ExtractElementInst::Create
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1888
llvm::GetElementPtrConstantExpr
GetElementPtrConstantExpr - This class is private to Constants.cpp, and is used behind the scenes to ...
Definition: ConstantsContext.h:275
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::APInt::getBitWidth
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1581
Module.h
llvm::APFloatBase::BFloat
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:167
llvm::Constant::isNotOneValue
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:125
llvm::ConstantExpr::hasIndices
bool hasIndices() const
Return true if this is an insertvalue or extractvalue expression, and the getIndices() method may be ...
Definition: Constants.cpp:1486
llvm::ConstantDataSequential::isElementTypeCompatible
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:2905
llvm::ConstantExpr::getFPToSI
static Constant * getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2182
llvm::APFloat::getAllOnesValue
static APFloat getAllOnesValue(const fltSemantics &Semantics, unsigned BitWidth)
Returns a float which is bitcasted from an all one value int.
Definition: APFloat.cpp:4850
llvm::ConstantAggregateZero
All zero aggregate value.
Definition: Constants.h:336
llvm::Constant::needsDynamicRelocation
bool needsDynamicRelocation() const
Definition: Constants.cpp:660
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1598
llvm::Constant::needsRelocation
bool needsRelocation() const
This method classifies the entry according to whether or not it may generate a relocation entry (eith...
Definition: Constants.cpp:664
llvm::Instruction::setHasNoUnsignedWrap
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
Definition: Instruction.cpp:124
llvm::Optional< unsigned >
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
Operator.h
llvm::VectorType::getElementType
Type * getElementType() const
Definition: DerivedTypes.h:421
llvm::ShuffleVectorInst::isValidOperands
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.
Definition: Instructions.cpp:2019
llvm::CmpInst::ICMP_SLE
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:750
llvm::ConstantExpr::getExactLogBase2
static Constant * getExactLogBase2(Constant *C)
If C is a scalar/fixed width vector of known powers of 2, then this function returns a new scalar/fix...
Definition: Constants.cpp:2771
llvm::APFloat::getSNaN
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
Definition: APFloat.h:924
STLExtras.h
llvm::SmallVectorImpl::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:635
llvm::ArrayType
Class to represent array types.
Definition: DerivedTypes.h:356
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:218
llvm::ConstantFP::getNegativeZero
static Constant * getNegativeZero(Type *Ty)
Definition: Constants.cpp:1020
llvm::gep_type_begin
gep_type_iterator gep_type_begin(const User *GEP)
Definition: GetElementPtrTypeIterator.h:139
llvm::Type::isFloatingPointTy
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
Definition: Type.h:162
llvm::Constant::isOneValue
bool isOneValue() const
Returns true if the value is one.
Definition: Constants.cpp:108
llvm::Instruction::setIsExact
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
Definition: Instruction.cpp:132
llvm::CmpInst::FCMP_OGT
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:724
llvm::ConstantDataSequential::isString
bool isString(unsigned CharSize=8) const
This method returns true if this is an array of CharSize integers.
Definition: Constants.cpp:3271
llvm::BitmaskEnumDetail::Mask
std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:80
llvm::ConstantExpr::getWithOperandReplaced
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:1512
llvm::APFloat::getSemantics
const fltSemantics & getSemantics() const
Definition: APFloat.h:1225
llvm::LLVMContextImpl::DSOLocalEquivalents
DenseMap< const GlobalValue *, DSOLocalEquivalent * > DSOLocalEquivalents
Definition: LLVMContextImpl.h:1417
llvm::Type::getInt8Ty
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:201
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:203
llvm::Constant::isThreadDependent
bool isThreadDependent() const
Return true if the value can vary between threads.
Definition: Constants.cpp:634
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
llvm::ArrayRef::data
const T * data() const
Definition: ArrayRef.h:162
llvm::SelectInst::areInvalidOperands
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.
Definition: Instructions.cpp:75
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::ConstantFoldExtractValueInstruction
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
Definition: ConstantFold.cpp:1012
llvm::ConstantExpr::getWithOperands
Constant * getWithOperands(ArrayRef< Constant * > Ops) const
This returns the current constant expression with the operands replaced with the specified values.
Definition: Constants.h:1297
getFPSequenceIfElementsMatch
static Constant * getFPSequenceIfElementsMatch(ArrayRef< Constant * > V)
Definition: Constants.cpp:1201
llvm::ConstantExpr::getPointerCast
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:2033
llvm::LLVMContextImpl::TheNoneToken
std::unique_ptr< ConstantTokenNone > TheNoneToken
Definition: LLVMContextImpl.h:1426
llvm::APFloatBase::IEEEquad
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:176
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::lltok::equal
@ equal
Definition: LLToken.h:25
llvm::LLVMContextImpl::ArrayConstants
ArrayConstantsTy ArrayConstants
Definition: LLVMContextImpl.h:1398
llvm::gep_type_end
gep_type_iterator gep_type_end(const User *GEP)
Definition: GetElementPtrTypeIterator.h:146
isUndef
static bool isUndef(ArrayRef< int > Mask)
Definition: HexagonISelDAGToDAGHVX.cpp:912
llvm::CmpInst::FCMP_ULT
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:734
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::ConstantExpr::getSub
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2689
llvm::ConstantDataSequential::getElementAsAPFloat
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:3226
llvm::ConstantData
Base class for constants with no operands.
Definition: Constants.h:51
llvm::ConstantPointerNull
A constant pointer value that points to null.
Definition: Constants.h:534
llvm::PatternMatch::m_APInt
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:270
llvm::APFloat::getZero
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:885
llvm::PointerType::isOpaqueOrPointeeTypeMatches
bool isOpaqueOrPointeeTypeMatches(Type *Ty)
Return true if either this is an opaque pointer type or if this pointee type matches Ty.
Definition: DerivedTypes.h:692
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::Type::isArrayTy
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:225
llvm::Intrinsic::getType
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
Definition: Function.cpp:1292
llvm::all_of
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1547
llvm::SelectInst::Create
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
Definition: Instructions.h:1771
llvm::ConstantExpr::getShuffleMask
ArrayRef< int > getShuffleMask() const
Assert that this is a shufflevector and return the mask.
Definition: Constants.cpp:1503
GlobalValue.h
llvm::OperandTraits
Compile-time customization of User operands.
Definition: User.h:42
llvm::PPCISD::SC
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
Definition: PPCISelLowering.h:410
llvm::Constant::containsConstantExpression
bool containsConstantExpression() const
Return true if this is a fixed width vector constant that includes any constant expressions.
Definition: Constants.cpp:336
llvm::CmpInst::isFPPredicate
bool isFPPredicate() const
Definition: InstrTypes.h:813
Constants.h
llvm::PatternMatch::match
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
isZero
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:519
llvm::Constant::isNullValue
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:74
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::SmallVectorImpl::append
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:648
llvm::APFloatBase::IEEEhalf
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:164
llvm::Instruction::setHasNoSignedWrap
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
Definition: Instruction.cpp:128
llvm::User
Definition: User.h:44
llvm::UndefValue::getElementValue
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:1128
llvm::Type::getDoubleTy
static Type * getDoubleTy(LLVMContext &C)
Definition: Type.cpp:191
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
ItTy
llvm::CmpInst::ICMP_ULE
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:746
llvm::LLVMContextImpl
Definition: LLVMContextImpl.h:1324
llvm::ConstantExpr::getIntToPtr
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2207
llvm::LLVMContextImpl::CAZConstants
DenseMap< Type *, std::unique_ptr< ConstantAggregateZero > > CAZConstants
Definition: LLVMContextImpl.h:1395
llvm::UnaryConstantExpr
UnaryConstantExpr - This class is private to Constants.cpp, and is used behind the scenes to implemen...
Definition: ConstantsContext.h:46
llvm::VectorType::getInteger
static VectorType * getInteger(VectorType *VTy)
This static method gets a VectorType with the same number of elements as the input type,...
Definition: DerivedTypes.h:439
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
llvm::ConstantExpr::getUDiv
static Constant * getUDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2711
llvm::CmpInst::FCMP_UGE
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:733
llvm::ConstantExpr::getPtrToInt
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2193
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::Instruction::OtherOps
OtherOps
Definition: Instruction.h:815
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::Type::isVectorTy
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:237
llvm::VectorType::getElementCount
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Definition: DerivedTypes.h:626
llvm::ConstantExpr::getBinOpAbsorber
static Constant * getBinOpAbsorber(unsigned Opcode, Type *Ty)
Return the absorbing element for the given binary operation, i.e.
Definition: Constants.cpp:2846
llvm::Type::getFltSemantics
const fltSemantics & getFltSemantics() const
Definition: Type.h:169
llvm::Value::getValueID
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:529
llvm::isUIntN
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:455
llvm::IntegerType
Class to represent integer types.
Definition: DerivedTypes.h:40
llvm::Instruction::getOpcodeName
const char * getOpcodeName() const
Definition: Instruction.h:162
llvm::Instruction::CastOps
CastOps
Definition: Instruction.h:801
llvm::ConstantArray
ConstantArray - Constant Array Declarations.
Definition: Constants.h:409
llvm::Constant::getAllOnesValue
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:404
llvm::generic_gep_type_iterator::isBoundedSequential
bool isBoundedSequential() const
Definition: GetElementPtrTypeIterator.h:127
llvm::CmpInst::FCMP_UNO
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:730
llvm::Instruction
Definition: Instruction.h:45
containsUndefinedElement
static bool containsUndefinedElement(const Constant *C, function_ref< bool(const Constant *)> HasFn)
Definition: Constants.cpp:307
llvm::ConstantDataVector::getSplatValue
Constant * getSplatValue() const
If this is a splat constant, meaning that all of the elements have the same value,...
Definition: Constants.cpp:3308
llvm::Type::getScalarSizeInBits
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition: Type.cpp:153
LLVMContextImpl.h
llvm::APFloat::bitwiseIsEqual
bool bitwiseIsEqual(const APFloat &RHS) const
Definition: APFloat.h:1182
llvm::InsertValueConstantExpr
InsertValueConstantExpr - This class is private to Constants.cpp, and is used behind the scenes to im...
Definition: ConstantsContext.h:245
llvm::ConstantFoldGetElementPtr
Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, bool InBounds, Optional< unsigned > InRangeIndex, ArrayRef< Value * > Idxs)
Definition: ConstantFold.cpp:2438
llvm::ConstantFP
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:257
llvm::ConstantVector::getSplat
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1411
llvm::ConstantFP::isExactlyValue
bool isExactlyValue(const APFloat &V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition: Constants.cpp:1070
llvm::ShuffleVectorConstantExpr
ShuffleVectorConstantExpr - This class is private to Constants.cpp, and is used behind the scenes to ...
Definition: ConstantsContext.h:179
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1784
llvm::ConstantExpr::getXor
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2745
llvm::ConstantExpr::getURem
static Constant * getURem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2725
llvm::ConstantInt::get
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:899
llvm::CmpInst::FCMP_OEQ
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:723
llvm::ConstantExpr::getAsInstruction
Instruction * getAsInstruction() const
Returns an Instruction which implements the same operation as this ConstantExpr.
Definition: Constants.cpp:3483
llvm::ConstantDataSequential::getImpl
static Constant * getImpl(StringRef Bytes, Type *Ty)
This is the underlying implementation of all of the ConstantDataSequential::get methods.
Definition: Constants.cpp:2951
llvm::CmpInst::FCMP_OLT
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:726
llvm::PoisonValue::getStructElement
PoisonValue * getStructElement(unsigned Elt) const
If this poison has struct type, return a poison with the right element type for the specified element...
Definition: Constants.cpp:1159
StringMap.h
llvm::Type::PointerTyID
@ PointerTyID
Pointers.
Definition: Type.h:73
PatternMatch.h
llvm::ConstantStruct::getType
StructType * getType() const
Specialization - reduce amount of casting.
Definition: Constants.h:480
llvm::ConstantAggregate::ConstantAggregate
ConstantAggregate(Type *T, ValueTy VT, ArrayRef< Constant * > V)
Definition: Constants.cpp:1240
llvm::FixedVectorType::get
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:650
getSequenceIfElementsMatch
static Constant * getSequenceIfElementsMatch(Constant *C, ArrayRef< Constant * > V)
Definition: Constants.cpp:1214
llvm::ConstantFoldCompareInstruction
Constant * ConstantFoldCompareInstruction(unsigned short predicate, Constant *C1, Constant *C2)
Definition: ConstantFold.cpp:1941
llvm::PointerType::isOpaque
bool isOpaque() const
Definition: DerivedTypes.h:677
llvm::Type::BFloatTyID
@ BFloatTyID
16-bit floating point type (7-bit significand)
Definition: Type.h:57
llvm::Constant::isMinSignedValue
bool isMinSignedValue() const
Return true if the value is the smallest signed value.
Definition: Constants.cpp:153
llvm::LLVMContextImpl::CDSConstants
StringMap< std::unique_ptr< ConstantDataSequential > > CDSConstants
Definition: LLVMContextImpl.h:1412
llvm::isIntN
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:460
llvm::SelectConstantExpr
SelectConstantExpr - This class is private to Constants.cpp, and is used behind the scenes to impleme...
Definition: ConstantsContext.h:97
llvm::ArrayRef::slice
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:195
llvm::UndefValue::getNumElements
unsigned getNumElements() const
Return the number of elements in the array, vector, or struct.
Definition: Constants.cpp:1140
llvm::ConstantFP::getSNaN
static Constant * getSNaN(Type *Ty, bool Negative=false, APInt *Payload=nullptr)
Definition: Constants.cpp:1009
llvm::None
const NoneType None
Definition: None.h:23
llvm::LinearPolySize< ElementCount >::getFixed
static ElementCount getFixed(ScalarTy MinVal)
Definition: TypeSize.h:284
llvm::Type::getIntegerBitWidth
unsigned getIntegerBitWidth() const
Definition: DerivedTypes.h:96
llvm::ConstantExpr::getFNeg
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2666
llvm::ConstantExpr::isCompare
bool isCompare() const
Return true if this is a compare constant expression.
Definition: Constants.cpp:1460
llvm::ConstantExpr::getShuffleMaskForBitcode
Constant * getShuffleMaskForBitcode() const
Assert that this is a shufflevector and return the mask.
Definition: Constants.cpp:1507
llvm::ConstantFoldBinaryInstruction
Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
Definition: ConstantFold.cpp:1109
llvm::ConstantFoldExtractElementInstruction
Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
Definition: ConstantFold.cpp:859
llvm::PatternMatch::m_One
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:513
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:108
llvm::Instruction::isCommutative
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
Definition: Instruction.cpp:758
llvm::CmpInst::FCMP_FALSE
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:722
llvm::ConstantExpr::getAShr
static Constant * getAShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2766
llvm::Type::getStructNumElements
unsigned getStructNumElements() const
Definition: DerivedTypes.h:347
llvm::ConstantDataSequential::isCString
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:3275
llvm::ConstantFoldCastInstruction
Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
Definition: ConstantFold.cpp:546
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:201
llvm::APFloat::getQNaN
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:916
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
llvm::ConstantExpr::isCast
bool isCast() const
Return true if this is a convert constant expression.
Definition: Constants.cpp:1456
llvm::ISD::BlockAddress
@ BlockAddress
Definition: ISDOpcodes.h:84
llvm::ConstantExpr::getShuffleVector
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2585
llvm::ConstantArray::getType
ArrayType * getType() const
Specialize the getType() method to always return an ArrayType, which reduces the amount of casting ne...
Definition: Constants.h:428
llvm::VectorType
Base class of all SIMD vector types.
Definition: DerivedTypes.h:388
llvm::ConstantExpr::getTrunc
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2083
llvm::ConstantDataSequential::getRawDataValues
StringRef getRawDataValues() const
Return the raw, underlying, bytes of this data.
Definition: Constants.cpp:2901
llvm::APFloat
Definition: APFloat.h:701
llvm::ConstantVector::getType
FixedVectorType * getType() const
Specialize the getType() method to always return a FixedVectorType, which reduces the amount of casti...
Definition: Constants.h:516
llvm::Constant::isDLLImportDependent
bool isDLLImportDependent() const
Return true if the value is dependent on a dllimport variable.
Definition: Constants.cpp:641
llvm::ConstantExpr::getCompare
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:2386
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:179
llvm::ConstantFP::getQNaN
static Constant * getQNaN(Type *Ty, bool Negative=false, APInt *Payload=nullptr)
Definition: Constants.cpp:998
llvm::PPC::Predicate
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Definition: PPCPredicates.h:26
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::Value::mutateType
void mutateType(Type *Ty)
Mutate the type of this Value to be of the specified type.
Definition: Value.h:793
llvm::OverflowingBinaryOperator::NoSignedWrap
@ NoSignedWrap
Definition: Operator.h:72
llvm::PointerType::getUnqual
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:650
llvm::LLVMContextImpl::CPNConstants
DenseMap< PointerType *, std::unique_ptr< ConstantPointerNull > > CPNConstants
Definition: LLVMContextImpl.h:1406
llvm::Constant::isElementWiseEqual
bool isElementWiseEqual(Value *Y) const
Return true if this constant and a constant 'Y' are element-wise equal.
Definition: Constants.cpp:282
getFoldedCast
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:1958
llvm::ConstantExpr::getBinOpIdentity
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false)
Return the identity constant for a binary opcode.
Definition: Constants.cpp:2800
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::Constant::replaceUndefsWith
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition: Constants.cpp:767
llvm::ConstantDataVector::getFP
static Constant * getFP(Type *ElementType, ArrayRef< uint16_t > Elts)
getFP() constructors - Return a constant of vector type with a float element type taken from argument...
Definition: Constants.cpp:3107
llvm::ExtractValueInst::Create
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:2370
llvm::ConstantFoldInsertValueInstruction
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Definition: ConstantFold.cpp:1024
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
llvm::ConstantExpr::getIndices
ArrayRef< unsigned > getIndices() const
Assert that this is an insertvalue or exactvalue expression and return the list of indices.
Definition: Constants.cpp:1491
llvm::LLVMContextImpl::TheFalseVal
ConstantInt * TheFalseVal
Definition: LLVMContextImpl.h:1424
llvm::ConstantExpr::getAddrSpaceCast
static Constant * getAddrSpaceCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2233
llvm::APInt::logBase2
unsigned logBase2() const
Definition: APInt.h:1811
llvm::ConstantPointerNull::get
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1770
llvm::ConstantDataArray
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:678
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::DSOLocalEquivalent::getGlobalValue
GlobalValue * getGlobalValue() const
Definition: Constants.h:913
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
llvm::ConstantDataVector::isSplat
bool isSplat() const
Returns true if this is a splat constant, meaning that all elements have the same value.
Definition: Constants.cpp:3300
llvm::GetElementPtrConstantExpr::getSourceElementType
Type * getSourceElementType() const
Definition: Constants.cpp:2884
llvm::ConstantExpr::get
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:2255
llvm::ConstantExpr::getAlignOf
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:2355
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::ConstantDataVector
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:752
llvm::ExtractValueConstantExpr
ExtractValueConstantExpr - This class is private to Constants.cpp, and is used behind the scenes to i...
Definition: ConstantsContext.h:215
llvm::UndefValue
'undef' values are things that do not have specified contents.
Definition: Constants.h:1352
llvm::GetElementPtrInst
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:928
llvm::ConstantExpr::getShl
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2754
llvm::ConstantExpr::getFSub
static Constant * getFSub(Constant *C1, Constant *C2)
Definition: Constants.cpp:2696
llvm::ConstantExpr::getOr
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2741
getIntSequenceIfElementsMatch
static Constant * getIntSequenceIfElementsMatch(ArrayRef< Constant * > V)
Definition: Constants.cpp:1188
llvm::PointerType
Class to represent pointers.
Definition: DerivedTypes.h:631
llvm::ConstantDataSequential::getElementType
Type * getElementType() const
Return the element type of the array/vector.
Definition: Constants.cpp:2895
llvm::OverflowingBinaryOperator::NoUnsignedWrap
@ NoUnsignedWrap
Definition: Operator.h:71
llvm::ConstantUniqueMap::getOrCreate
ConstantClass * getOrCreate(TypeClass *Ty, ValType V)
Return the specified constant from the map, creating it if necessary.
Definition: ConstantsContext.h:720
llvm::Type::isHalfTy
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:141
llvm::GetElementPtrInst::CreateInBounds
static GetElementPtrInst * CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Definition: Instructions.h:990
llvm::CompareConstantExpr
Definition: ConstantsContext.h:309
llvm::ConstantVector
Constant Vector Declarations.
Definition: Constants.h:493
llvm::APFloatBase::PPCDoubleDouble
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
Definition: APFloat.cpp:185
llvm::ConstantFoldInsertElementInstruction
Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
Definition: ConstantFold.cpp:920
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::ConstantDataSequential::getElementAsConstant
Constant * getElementAsConstant(unsigned i) const
Return a Constant for a specified index's element.
Definition: Constants.cpp:3263
llvm::Constant::isNegativeZeroValue
bool isNegativeZeroValue() const
Return true if the value is what would be returned by getZeroValueForNegation.
Definition: Constants.cpp:40
llvm::Constant::isManifestConstant
bool isManifestConstant() const
Return true if a constant is ConstantData or a ConstantAggregate or ConstantExpr that contain only Co...
Definition: Constants.cpp:826
llvm::CmpInst::FCMP_OGE
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:725
llvm::ConstantExpr::getCast
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1976
llvm::LLVMContextImpl::TheTrueVal
ConstantInt * TheTrueVal
Definition: LLVMContextImpl.h:1423
llvm::CmpInst::ICMP_UGE
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:744
llvm::ConstantFP::getZeroValueForNegation
static Constant * getZeroValueForNegation(Type *Ty)
Floating point negation must be implemented with f(x) = -0.0 - x.
Definition: Constants.cpp:1032
llvm::ConstantAggregateZero::getElementValue
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:1093
llvm::Instruction::isBinaryOp
bool isBinaryOp() const
Definition: Instruction.h:165
llvm::ConstantVector::getSplatValue
Constant * getSplatValue(bool AllowUndefs=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1731
llvm::ArrayType::get
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:604
llvm::ConstantFP::getNaN
static Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
Definition: Constants.cpp:987
llvm::Constant::handleOperandChange
void handleOperandChange(Value *, Value *)
This method is a special form of User::replaceUsesOfWith (which does not work on constants) that does...
Definition: Constants.cpp:3327
llvm::ConstantExpr::getTruncOrBitCast
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2027
llvm::ConstantStruct
Definition: Constants.h:441
llvm::ConstantExpr::getSizeOf
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:2345
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:70
llvm::User::setOperand
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
llvm::CmpInst::ICMP_SLT
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:749
llvm::CmpInst::isIntPredicate
bool isIntPredicate() const
Definition: InstrTypes.h:814
llvm::Type::FP128TyID
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition: Type.h:61
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1528
llvm::LLVMContextImpl::IntConstants
IntMapTy IntConstants
Definition: LLVMContextImpl.h:1366
llvm::GetElementPtrInst::Create
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:954
llvm::Value::ValueTy
ValueTy
Concrete subclass of this.
Definition: Value.h:510
llvm::BlockAddress
The address of a basic block.
Definition: Constants.h:848
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
IT
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")))
llvm::Constant::removeDeadConstantUsers
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
Definition: Constants.cpp:740
llvm::BinaryOperator
Definition: InstrTypes.h:189
llvm::APInt::getAllOnesValue
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:567
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:212
llvm::APFloatBase::IEEEdouble
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:173
llvm::Type::PPC_FP128TyID
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm::User::getOperandList
const Use * getOperandList() const
Definition: User.h:162
llvm::PatternMatch::m_Undef
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:136
llvm::ConstantExpr::getSExtOrBitCast
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2021
llvm::ConstantExpr::getUMin
static Constant * getUMin(Constant *C1, Constant *C2)
Definition: Constants.cpp:2749
llvm::CmpInst::ICMP_ULT
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:745
llvm::ConstantDataSequential::getElementAsAPInt
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:3198
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::Constant::getAggregateElement
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:420
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
llvm::ConstantDataArray::get
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition: Constants.h:691
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:520
uint32_t
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:979
llvm::LLVMContextImpl::StructConstants
StructConstantsTy StructConstants
Definition: LLVMContextImpl.h:1401
llvm::ConstantExpr::getInsertElement
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2562
llvm::ConstantDataArray::getString
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:3054
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::LLVMContextImpl::PVConstants
DenseMap< Type *, std::unique_ptr< PoisonValue > > PVConstants
Definition: LLVMContextImpl.h:1410
llvm::ConstantExpr::getIntegerCast
static Constant * getIntegerCast(Constant *C, Type *Ty, bool IsSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:2059
llvm::ConstantExprKeyType
Definition: ConstantsContext.h:504
llvm::ConstantVector::get
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1368
llvm::Type::isPtrOrPtrVectorTy
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:234
llvm::ConstantExpr::getFMul
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2707
llvm::ifs::IFSSymbolType::Func
@ Func
llvm::Type::getContext
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:127
llvm::ArrayRef::front
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:168
users
iv users
Definition: IVUsers.cpp:52
llvm::Value::stripPointerCasts
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:675
llvm::ConstantInt::getFalse
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:854
llvm::ConstantExpr::getFCmp
static Constant * getFCmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
Definition: Constants.cpp:2515
ConstantFold.h
llvm::Type::IntegerTyID
@ IntegerTyID
Arbitrary bit width integers.
Definition: Type.h:71
llvm::ConstantDataArray::getFP
static Constant * getFP(Type *ElementType, ArrayRef< uint16_t > Elts)
getFP() constructors - Return a constant of array type with a float element type taken from argument ...
Definition: Constants.cpp:3033
llvm::APInt::getNullValue
static APInt getNullValue(unsigned numBits)
Get the '0' value.
Definition: APInt.h:574
llvm::ConstantAggregateZero::getStructElement
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:1089
llvm::MCID::Select
@ Select
Definition: MCInstrDesc.h:162
llvm::CmpInst::FCMP_UGT
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:732
llvm::BlockAddress::getFunction
Function * getFunction() const
Definition: Constants.h:876
canTrapImpl
static bool canTrapImpl(const Constant *C, SmallPtrSetImpl< const ConstantExpr * > &NonTrappingOps)
Definition: Constants.cpp:573
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::UndefValue::getSequentialElement
UndefValue * getSequentialElement() const
If this Undef has array or vector type, return a undef with the right element type.
Definition: Constants.cpp:1118
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:204
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:936
llvm::ConstantExpr::getSDiv
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2716
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:847
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:346
llvm::PointerType::hasSameElementTypeAs
bool hasSameElementTypeAs(PointerType *Other)
Return true if both pointer types have the same element type.
Definition: DerivedTypes.h:700
llvm::Type::isFloatTy
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:147
uint16_t
llvm::ConstantExpr::getNeg
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2659
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::ConstantDataSequential::getElementAsDouble
double getElementAsDouble(unsigned i) const
If this is an sequential container of doubles, return the specified element as a double.
Definition: Constants.cpp:3257
llvm::PossiblyExactOperator::IsExact
@ IsExact
Definition: Operator.h:124
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:314
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:153
llvm::ConstantInt::getBool
static ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:861
llvm::Type::FixedVectorTyID
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:76
llvm::Constant::isZeroValue
bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition: Constants.cpp:60
llvm::ConstantExpr::getAdd
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2678
llvm::Type::isIntOrIntVectorTy
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:207
llvm::ConstantFP::get
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:946
llvm::ConstantTokenNone::get
static ConstantTokenNone * get(LLVMContext &Context)
Return the ConstantTokenNone.
Definition: Constants.cpp:1441
llvm::ConstantInt::getSigned
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.cpp:913
llvm::fltSemantics
Definition: APFloat.cpp:54
getTypeID
static char getTypeID(Type *Ty)
Definition: ExternalFunctions.cpp:73
llvm::ConstantExpr::getAnd
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2737
llvm::ConstantFoldShuffleVectorInstruction
Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, ArrayRef< int > Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and mask.
Definition: ConstantFold.cpp:957
removeDeadUsersOfConstant
static bool removeDeadUsersOfConstant(const Constant *C)
If the specified constantexpr is dead, remove it.
Definition: Constants.cpp:719
llvm::UndefValue::getStructElement
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:1124
llvm::Type::getPointerTo
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Definition: Type.cpp:738
llvm::Type::TokenTyID
@ TokenTyID
Tokens.
Definition: Type.h:68
llvm::Type::isDoubleTy
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:150
ConstHasGlobalValuePredicate
static bool ConstHasGlobalValuePredicate(const Constant *C, bool(*Predicate)(const GlobalValue *))
Check if C contains a GlobalValue for which Predicate is true.
Definition: Constants.cpp:611
llvm::ConstantArray::get
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1262
llvm::ConstantExpr::getGetElementPtr
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:1210
llvm::GetElementPtrInst::getIndexedType
static Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
Definition: Instructions.cpp:1762
llvm::ConstantExpr::getLShr
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2761
llvm::ConstantUniqueMap::remove
void remove(ConstantClass *CP)
Remove this constant from the map.
Definition: ConstantsContext.h:738
llvm::ConstantExpr::getFPTrunc
static Constant * getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2125
llvm::CmpInst::ICMP_SGE
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:748
llvm::ConstantExpr::getFPToUI
static Constant * getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2171
llvm::MCID::Add
@ Add
Definition: MCInstrDesc.h:183
llvm::CmpInst::Create
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.
Definition: Instructions.cpp:3637
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:476
llvm::Constant::mergeUndefsWith
static Constant * mergeUndefsWith(Constant *C, Constant *Other)
Merges undefs of a Constant with another Constant, along with the undefs already present.
Definition: Constants.cpp:791
Predicate
llvm::Instruction::BinaryOps
BinaryOps
Definition: Instruction.h:787
llvm::Type::StructTyID
@ StructTyID
Structures.
Definition: Type.h:74
llvm::GetElementPtrConstantExpr::getResultElementType
Type * getResultElementType() const
Definition: Constants.cpp:2888
llvm::ShuffleVectorInst
This instruction constructs a fixed permutation of two input vectors.
Definition: Instructions.h:2011
llvm::APFloatBase::rmNearestTiesToEven
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:190
llvm::UnaryOperator::Create
static UnaryOperator * Create(UnaryOps Op, Value *S, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a unary instruction, given the opcode and an operand.
Definition: Instructions.cpp:2435
Instructions.h
llvm::Type::getFloatingPointTy
static Type * getFloatingPointTy(LLVMContext &C, const fltSemantics &S)
Definition: Type.h:446
llvm::APFloat::convert
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:4825
llvm::Constant::getIntegerValue
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:387
llvm::Constant::hasExactInverseFP
bool hasExactInverseFP() const
Return true if this scalar has an exact multiplicative inverse or this vector has an exact multiplica...
Definition: Constants.cpp:240
llvm::User::getNumOperands
unsigned getNumOperands() const
Definition: User.h:191
llvm::Type::isStructTy
bool isStructTy() const
True if this is an instance of StructType.
Definition: Type.h:222
llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2048
llvm::ConstantExpr::getSRem
static Constant * getSRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2729
llvm::InsertElementConstantExpr
InsertElementConstantExpr - This class is private to Constants.cpp, and is used behind the scenes to ...
Definition: ConstantsContext.h:151
SmallVector.h
llvm::Type::isBFloatTy
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition: Type.h:144
llvm::Constant::getUniqueInteger
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:1758
llvm::ConstantExpr::getFPExtend
static Constant * getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2137
llvm::Value::stripInBoundsConstantOffsets
const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
Definition: Value.cpp:687
llvm::CmpInst::ICMP_UGT
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:743
llvm::User::op_begin
op_iterator op_begin()
Definition: User.h:234
llvm::PointerType::getWithSamePointeeType
static PointerType * getWithSamePointeeType(PointerType *PT, unsigned AddressSpace)
This constructs a pointer type with the same pointee type as input PointerType (or opaque pointer is ...
Definition: DerivedTypes.h:665
llvm::CmpInst::FCMP_UNE
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:736
llvm::Constant::containsUndefOrPoisonElement
bool containsUndefOrPoisonElement() const
Return true if this is a vector constant that includes any undef or poison elements.
Definition: Constants.cpp:326
llvm::ConstantDataVector::getSplat
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:3131
llvm::BinaryConstantExpr
BinaryConstantExpr - This class is private to Constants.cpp, and is used behind the scenes to impleme...
Definition: ConstantsContext.h:70
llvm::Constant::canTrap
bool canTrap() const
Return true if evaluation of this constant could trap.
Definition: Constants.cpp:604
llvm::BlockAddress::get
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1822
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
llvm::IntegerType::getBitWidth
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:71
llvm::ConstantInt::isValueValidForType
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:1588
llvm::ConstantExpr::getMul
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2700
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::Instruction::isUnaryOp
bool isUnaryOp() const
Definition: Instruction.h:164
llvm::APFloat::getNaN
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:905
llvm::Constant::getSplatValue
Constant * getSplatValue(bool AllowUndefs=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1699
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:149
llvm::CmpInst::FCMP_OLE
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:727
llvm::LLVMContextImpl::VectorConstants
VectorConstantsTy VectorConstants
Definition: LLVMContextImpl.h:1404
llvm::ConstantStruct::getTypeForElements
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:1312
llvm::Type::getInt16Ty
static IntegerType * getInt16Ty(LLVMContext &C)
Definition: Type.cpp:202
llvm::Type::ScalableVectorTyID
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition: Type.h:77
llvm::GlobalValue::getType
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
DerivedTypes.h
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:343
pred
hexagon gen pred
Definition: HexagonGenPredicate.cpp:134
llvm::Constant::isNotMinSignedValue
bool isNotMinSignedValue() const
Return true if the value is not the smallest signed value, or, for vectors, does not contain smallest...
Definition: Constants.cpp:170
llvm::IntegerType::get
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:275
llvm::APFloat::getInf
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:894
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
GEP
Hexagon Common GEP
Definition: HexagonCommonGEP.cpp:172
llvm::User::Op
Use & Op()
Definition: User.h:133
llvm::BlockAddress::lookup
static BlockAddress * lookup(const BasicBlock *BB)
Lookup an existing BlockAddress constant for the given BasicBlock.
Definition: Constants.cpp:1840
From
BlockVerifier::State From
Definition: BlockVerifier.cpp:55
llvm::ConstantPointerNull::getType
PointerType * getType() const
Specialize the getType() method to always return an PointerType, which reduces the amount of casting ...
Definition: Constants.h:550
llvm::Constant::isConstantUsed
bool isConstantUsed() const
Return true if the constant has users other than constant expressions and other dangling things.
Definition: Constants.cpp:648
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition: User.h:169
llvm::ConstantDataSequential::getAsString
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition: Constants.h:644
raw_ostream.h
llvm::ConstantFoldUnaryInstruction
Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
Definition: ConstantFold.cpp:1053
llvm::ConstantExpr::getFAdd
static Constant * getFAdd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2685
llvm::ConstantAggregateZero::get
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1663
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:624
llvm::BinaryOperator::Create
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.
Definition: Instructions.cpp:2564
llvm::CmpInst::FCMP_TRUE
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:737
llvm::ConstantTokenNone
A constant token which is empty.
Definition: Constants.h:826
llvm::DSOLocalEquivalent::get
static DSOLocalEquivalent * get(GlobalValue *GV)
Return a DSOLocalEquivalent for the specified global value.
Definition: Constants.cpp:1895
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::DSOLocalEquivalent
Wrapper for a function that represents a value that functionally represents the original function.
Definition: Constants.h:894
Debug.h
llvm::VectorType::get
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:634
llvm::Type::HalfTyID
@ HalfTyID
16-bit floating point type
Definition: Type.h:56
llvm::Type::X86_FP80TyID
@ X86_FP80TyID
80-bit floating point type (X87)
Definition: Type.h:60
llvm::Type::getFloatTy
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:190
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:154
llvm::ArrayType::getElementType
Type * getElementType() const
Definition: DerivedTypes.h:369
llvm::ConstantExpr::getExtractValue
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2635
llvm::Constant::destroyConstant
void destroyConstant()
Called if some element of this constant is no longer valid.
Definition: Constants.cpp:460
llvm::CmpInst::FCMP_ORD
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:729
llvm::ExtractElementConstantExpr
ExtractElementConstantExpr - This class is private to Constants.cpp, and is used behind the scenes to...
Definition: ConstantsContext.h:124
llvm::Type::isFirstClassType
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
Definition: Type.h:253
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1172
llvm::CmpInst::FCMP_UEQ
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:731
llvm::ConstantExpr::isGEPWithNoNotionalOverIndexing
bool isGEPWithNoNotionalOverIndexing() const
Return true if this is a getelementptr expression and all the index operands are compile-time known i...
Definition: Constants.cpp:1464
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
llvm::Type::getPrimitiveSizeInBits
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:128
llvm::SmallPtrSetImpl::insert