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