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