Home | History | Annotate | Download | only in IR

Lines Matching full:constant

1 //===-- Constants.cpp - Implement Constant nodes --------------------------===//
10 // This file implements the Constant* classes.
40 // Constant Class
43 void Constant::anchor() { }
45 bool Constant::isNegativeZeroValue() const {
64 // Return true iff this constant is positive zero (floating point), negative
66 bool Constant::isZeroValue() const {
75 bool Constant::isNullValue() const {
84 // constant zero is zero for aggregates and cpnull is null for pointers.
88 bool Constant::isAllOnesValue() const {
97 // Check for constant vectors which are splats of -1 values.
99 if (Constant *Splat = CV->getSplatValue())
102 // Check for constant vectors which are splats of -1 values.
104 if (Constant *Splat = CV->getSplatValue())
110 // Constructor to create a '0' constant of arbitrary type...
111 Constant *Constant::getNullValue(Type *Ty) {
142 llvm_unreachable("Cannot create a null constant of that type!");
146 Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
149 // Create the base integer constant.
150 Constant *C = ConstantInt::get(Ty->getContext(), V);
163 Constant *Constant::getAllOnesValue(Type *Ty) {
180 /// constant that corresponds to the specified element if possible, or null if
182 /// 'this' is a constant expr.
183 Constant *Constant::getAggregateElement(unsigned Elt) const {
204 Constant *Constant::getAggregateElement(Constant *Elt) const {
212 void Constant::destroyConstantImpl() {
213 // When a Constant is destroyed, there may be lingering
214 // references to the constant by other constants in the constant pool. These
223 if (!isa<Constant>(V)) {
229 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
230 cast<Constant>(V)->destroyConstant();
232 // The constant should remove itself from our use list...
233 assert((use_empty() || use_back() != V) && "Constant not removed!");
240 static bool canTrapImpl(const Constant *C,
243 // The only thing that could possibly trap are constant exprs.
273 /// canTrap - Return true if evaluation of this constant could trap. This is
274 /// true for things like constant expressions that could divide by zero.
275 bool Constant::canTrap() const {
281 bool Constant::isThreadDependent() const {
282 SmallPtrSet<const Constant*, 64> Visited;
283 SmallVector<const Constant*, 64> WorkList;
288 const Constant *C = WorkList.pop_back_val();
296 const Constant *D = dyn_cast<Constant>(C->getOperand(I));
307 /// isConstantUsed - Return true if the constant has users other than constant
309 bool Constant::isConstantUsed() const {
311 const Constant *UC = dyn_cast<Constant>(*UI);
328 /// NoRelocation: This constant pool entry is guaranteed to never have a
329 /// relocation applied to it (because it holds a simple constant like
337 Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
368 cast<Constant>(getOperand(i))->getRelocationInfo());
376 static bool removeDeadUsersOfConstant(const Constant *C) {
380 const Constant *User = dyn_cast<Constant>(C->use_back());
381 if (!User) return false; // Non-constant usage;
383 return false; // Constant wasn't dead
386 const_cast<Constant*>(C)->destroyConstant();
391 /// removeDeadConstantUsers - If there are any dead constant users dangling
392 /// off of this constant, remove them. This method is useful for clients
395 void Constant::removeDeadConstantUsers() const {
399 const Constant *User = dyn_cast<Constant>(*I);
407 // If the constant wasn't dead, remember that this was the last live use
408 // and move on to the next constant.
414 // If the constant was dead, then the iterator is invalidated.
434 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
435 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
452 Constant *ConstantInt::getTrue(Type *Ty) {
464 Constant *ConstantInt::getFalse(Type *Ty) {
492 Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
493 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
511 Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
515 Constant *ConstantInt::get(Type *Ty, const APInt& V) {
554 /// get() - This returns a constant fp for the specified value in the
555 /// specified type. This should only be used for simple constant values like
557 Constant *ConstantFP::get(Type *Ty, double V) {
564 Constant *C = get(Context, FV);
574 Constant *ConstantFP::get(Type *Ty, StringRef Str) {
578 Constant *C = get(Context, FV);
590 APFloat apf = cast<ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
596 Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
599 Constant *C = getNegativeZero(ScalarTy);
605 return Constant::getNullValue(Ty);
645 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
660 Constant *ConstantAggregateZero::getSequentialElement() const {
661 return Constant::getNullValue(getType()->getSequentialElementType());
666 Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
667 return Constant::getNullValue(getType()->getStructElementType(Elt));
672 Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
680 Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
705 UndefValue *UndefValue::getElementValue(Constant *C) const {
733 ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
734 : Constant(T, ConstantArrayVal,
738 "Invalid initializer vector for constant array");
745 Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
759 Constant *C = V[0];
839 /// getTypeForElements - Return an anonymous struct type to use for a constant
842 ArrayRef<Constant*> V,
853 StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
861 ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
862 : Constant(T, ConstantStructVal,
866 "Invalid initializer vector for constant structure");
874 Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
902 Constant *ConstantStruct::get(StructType *T, ...) {
904 SmallVector<Constant*, 8> Values;
906 while (Constant *Val = va_arg(ap, llvm::Constant*))
912 ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
913 : Constant(T, ConstantVectorVal,
923 Constant *ConstantVector::get(ArrayRef<Constant*> V) {
930 Constant *C = V[0];
1021 Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
1028 SmallVector<Constant*, 32> Elts(NumElts, V);
1035 // Constant.h
1087 /// getWithOperandReplaced - Return a constant expression identical to this
1089 Constant *
1090 ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
1096 SmallVector<Constant*, 8> NewOps;
1103 /// getWithOperands - This returns the current constant expression with the
1106 Constant *ConstantExpr::
1107 getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const {
1241 /// destroyConstant - Remove the constant from the constant table.
1248 /// destroyConstant - Remove the constant from the constant table...
1259 // destroyConstant - Remove the constant from the constant table...
1266 // destroyConstant - Remove the constant from the constant table...
1273 /// getSplatValue - If this is a splat vector constant, meaning that all of
1275 Constant *Constant::getSplatValue() const {
1286 /// getSplatValue - If this is a splat constant, where all of the
1288 Constant *ConstantVector::getSplatValue() const {
1290 Constant *Elt = getOperand(0);
1298 /// If C is a constant integer then return its value, otherwise C must be a
1299 /// vector of constant integers, all equal, and the common value is returned.
1300 const APInt &Constant::getUniqueInteger() const {
1304 const Constant *C = this->getAggregateElement(0U);
1321 // destroyConstant - Remove the constant from the constant table...
1325 // Free the constant and any dangling references to it.
1341 // destroyConstant - Remove the constant from the constant table.
1344 // Free the constant and any dangling references to it.
1368 : Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1376 // destroyConstant - Remove the constant from the constant table.
1428 static inline Constant *getFoldedCast(
1429 Instruction::CastOps opc, Constant *C, Type *Ty) {
1432 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1437 // Look up the constant in the table first to ensure uniqueness.
1443 Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty) {
1467 Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
1473 Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
1479 Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
1485 Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
1495 Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty,
1508 Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
1520 Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty) {
1534 Constant *ConstantExpr::getSExt(Constant *C, Type *Ty) {
1548 Constant *ConstantExpr::getZExt(Constant *C, Type *Ty) {
1562 Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty) {
1574 Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty) {
1586 Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty) {
1597 Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty) {
1608 Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty) {
1619 Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty) {
1630 Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy) {
1642 Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy) {
1654 Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy) {
1665 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
1670 "Invalid opcode in binary constant expression");
1672 "Operand types in binary constant expression should match");
1732 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1735 Constant *ArgVec[] = { C1, C2 };
1742 Constant *ConstantExpr::getSizeOf(Type* Ty) {
1745 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1746 Constant *GEP = getGetElementPtr(
1747 Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1752 Constant *ConstantExpr::getAlignOf(Type* Ty) {
1757 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
1758 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
1759 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1760 Constant *Indices[2] = { Zero, One };
1761 Constant *GEP = getGetElementPtr(NullPtr, Indices);
1766 Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
1771 Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
1774 Constant *GEPIdx[] = {
1778 Constant *GEP = getGetElementPtr(
1779 Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1784 Constant *ConstantExpr::getCompare(unsigned short Predicate,
1785 Constant *C1, Constant *C2) {
1806 Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2) {
1809 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1812 Constant *ArgVec[] = { C, V1, V2 };
1819 Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs,
1822 "Non-pointer type for constant GetElementPtr expression");
1824 if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, Idxs))
1835 // Look up the constant in the table first to ensure uniqueness
1836 std::vector<Constant*> ArgVec;
1846 ArgVec.push_back(cast<Constant>(Idxs[i]));
1855 Constant *
1856 ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) {
1861 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
1864 // Look up the constant in the table first to ensure uniqueness
1865 Constant *ArgVec[] = { LHS, RHS };
1877 Constant *
1878 ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) {
1882 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
1885 // Look up the constant in the table first to ensure uniqueness
1886 Constant *ArgVec[] = { LHS, RHS };
1898 Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1904 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1907 // Look up the constant in the table first to ensure uniqueness
1908 Constant *ArgVec[] = { Val, Idx };
1916 Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1917 Constant *Idx) {
1925 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1927 // Look up the constant in the table first to ensure uniqueness
1928 Constant *ArgVec[] = { Val, Elt, Idx };
1935 Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1936 Constant *Mask) {
1938 "Invalid shuffle vector constant expr operands!");
1940 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1947 // Look up the constant in the table first to ensure uniqueness
1948 Constant *ArgVec[] = { V1, V2, Mask };
1955 Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
1958 "Non-first-class type for constant insertvalue expression");
1965 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
1968 Constant *ArgVec[] = { Agg, Val };
1975 Constant *ConstantExpr::getExtractValue(Constant *Agg,
1985 "Non-first-class type for constant extractvalue expression");
1986 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
1989 Constant *ArgVec[] = { Agg };
1996 Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
2003 Constant *ConstantExpr::getFNeg(Constant *C) {
2009 Constant *ConstantExpr::getNot(Constant *C) {
2012 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
2015 Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2022 Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
2026 Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2033 Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
2037 Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2044 Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
2048 Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2053 Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2058 Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
2062 Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
2066 Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
2070 Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
2074 Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
2078 Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
2082 Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
2086 Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2093 Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2098 Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2104 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
2106 Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) {
2115 return Constant::getNullValue(Ty);
2121 return Constant::getAllOnesValue(Ty);
2126 /// operation, i.e. a constant C such that X op C = C and C op X = C for
2129 Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2136 return Constant::getAllOnesValue(Ty);
2140 return Constant::getNullValue(Ty);
2144 // destroyConstant - Remove the constant from the constant table...
2158 GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
2234 Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
2265 // Remove the constant from the StringMap.
2304 /// get() constructors - Return a constant with array type with an element
2307 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) {
2312 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
2317 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
2322 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
2327 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) {
2332 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) {
2343 Constant *ConstantDataArray::getString(LLVMContext &Context,
2357 /// get() constructors - Return a constant with vector type with an element
2360 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
2365 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
2370 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
2375 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
2380 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
2385 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
2391 Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2487 /// getElementAsConstant - Return a Constant for a specified index's element.
2488 /// Note that this has to compute a new constant to return, so it isn't as
2490 Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
2517 /// getSplatValue - If this is a splat constant, meaning that all of the
2519 Constant *ConstantDataVector::getSplatValue() const {
2535 /// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2548 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2549 Constant *ToC = cast<Constant>(To);
2553 SmallVector<Constant*, 8> Values;
2558 // Fill values with the modified operands of the constant array. Also,
2565 Constant *Val = cast<Constant>(O->get());
2574 Constant *Replacement = 0;
2589 // creating a new constant array, inserting it, replaceallusesof'ing the
2617 // Delete the old constant!
2623 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2624 Constant *ToC = cast<Constant>(To);
2629 SmallVector<Constant*, 8> Values;
2634 // Fill values with the modified operands of the constant struct. Also,
2641 Constant *Val = cast<Constant>(O->get());
2648 Constant *Val = cast<Constant>(O->get());
2654 Values.push_back(cast<Constant>(O->get()));
2660 Constant *Replacement = 0;
2675 // creating a new constant struct, inserting it, replaceallusesof'ing the
2692 // Delete the old constant!
2698 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2700 SmallVector<Constant*, 8> Values;
2703 Constant *Val = getOperand(i);
2704 if (Val == From) Val = cast<Constant>(To);
2708 Constant *Replacement = get(Values);
2714 // Delete the old constant!
2720 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2721 Constant *To = cast<Constant>(ToV);
2723 SmallVector<Constant*, 8> NewOps;
2725 Constant *Op = getOperand(i);
2729 Constant *Replacement = getWithOperands(NewOps);
2735 // Delete the old constant!