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