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