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