Home | History | Annotate | Download | only in IR

Lines Matching refs: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 {
69 // Return true iff this constant is positive zero (floating point), negative
71 bool Constant::isZeroValue() const {
91 bool Constant::isNullValue() const {
100 // constant zero is zero for aggregates, cpnull is null for pointers, none for
106 bool Constant::isAllOnesValue() const {
115 // Check for constant vectors which are splats of -1 values.
117 if (Constant *Splat = CV->getSplatValue())
120 // Check for constant vectors which are splats of -1 values.
122 if (Constant *Splat = CV->getSplatValue())
128 bool Constant::isOneValue() const {
137 // Check for constant vectors which are splats of 1 values.
139 if (Constant *Splat = CV->getSplatValue())
142 // Check for constant vectors which are splats of 1 values.
144 if (Constant *Splat = CV->getSplatValue())
150 bool Constant::isMinSignedValue() const {
159 // Check for constant vectors which are splats of INT_MIN values.
161 if (Constant *Splat = CV->getSplatValue())
164 // Check for constant vectors which are splats of INT_MIN values.
166 if (Constant *Splat = CV->getSplatValue())
172 bool Constant::isNotMinSignedValue() const {
181 // Check for constant vectors which are splats of INT_MIN values.
183 if (Constant *Splat = CV->getSplatValue())
186 // Check for constant vectors which are splats of INT_MIN values.
188 if (Constant *Splat = CV->getSplatValue())
195 // Constructor to create a '0' constant of arbitrary type...
196 Constant *Constant::getNullValue(Type *Ty) {
229 llvm_unreachable("Cannot create a null constant of that type!");
233 Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
236 // Create the base integer constant.
237 Constant *C = ConstantInt::get(Ty->getContext(), V);
250 Constant *Constant::getAllOnesValue(Type *Ty) {
267 /// constant that corresponds to the specified element if possible, or null if
269 /// 'this' is a constant expr.
270 Constant *Constant::getAggregateElement(unsigned Elt) const {
292 Constant *Constant::getAggregateElement(Constant *Elt) const {
299 void Constant::destroyConstant() {
301 /// a chance to remove the constant from any maps/pools it's contained in.
304 llvm_unreachable("Not a constant!");
312 // When a Constant is destroyed, there may be lingering
313 // references to the constant by other constants in the constant pool. These
322 if (!isa<Constant>(V)) {
328 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
329 cast<Constant>(V)->destroyConstant();
331 // The constant should remove itself from our use list...
332 assert((use_empty() || user_back() != V) && "Constant not removed!");
339 static bool canTrapImpl(const Constant *C,
342 // The only thing that could possibly trap are constant exprs.
372 /// canTrap - Return true if evaluation of this constant could trap. This is
373 /// true for things like constant expressions that could divide by zero.
374 bool Constant::canTrap() const {
381 ConstHasGlobalValuePredicate(const Constant *C,
383 SmallPtrSet<const Constant *, 8> Visited;
384 SmallVector<const Constant *, 8> WorkList;
389 const Constant *WorkItem = WorkList.pop_back_val();
394 const Constant *ConstOp = dyn_cast<Constant>(Op);
405 bool Constant::isThreadDependent() const {
412 bool Constant::isDLLImportDependent() const {
419 /// Return true if the constant has users other than constant exprs and other
421 bool Constant::isConstantUsed() const {
423 const Constant *UC = dyn_cast<Constant>(U);
433 bool Constant::needsRelocation() const {
459 Result |= cast<Constant>(getOperand(i))->needsRelocation();
467 static bool removeDeadUsersOfConstant(const Constant *C) {
471 const Constant *User = dyn_cast<Constant>(C->user_back());
472 if (!User) return false; // Non-constant usage;
474 return false; // Constant wasn't dead
477 const_cast<Constant*>(C)->destroyConstant();
482 /// removeDeadConstantUsers - If there are any dead constant users dangling
483 /// off of this constant, remove them. This method is useful for clients
486 void Constant::removeDeadConstantUsers() const {
490 const Constant *User = dyn_cast<Constant>(*I);
498 // If the constant wasn't dead, remember that this was the last live use
499 // and move on to the next constant.
505 // If the constant was dead, then the iterator is invalidated.
525 : Constant(Ty, ConstantIntVal, nullptr, 0), Val(V) {
526 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
543 Constant *ConstantInt::getTrue(Type *Ty) {
555 Constant *ConstantInt::getFalse(Type *Ty) {
581 Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
582 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
600 Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
604 Constant *ConstantInt::get(Type *Ty, const APInt& V) {
621 /// Remove the constant from the constant table.
648 /// get() - This returns a constant fp for the specified value in the
649 /// specified type. This should only be used for simple constant values like
651 Constant *ConstantFP::get(Type *Ty, double V) {
658 Constant *C = get(Context, FV);
668 Constant *ConstantFP::get(Type *Ty, StringRef Str) {
672 Constant *C = get(Context, FV);
681 Constant *ConstantFP::getNaN(Type *Ty, bool Negative, unsigned Type) {
684 Constant *C = get(Ty->getContext(), NaN);
692 Constant *ConstantFP::getNegativeZero(Type *Ty) {
695 Constant *C = get(Ty->getContext(), NegZero);
704 Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
708 return Constant::getNullValue(Ty);
741 Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
743 Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
752 : Constant(Ty, ConstantFPVal, nullptr, 0), Val(V) {
761 /// Remove the constant from the constant table.
772 Constant *ConstantAggregateZero::getSequentialElement() const {
773 return Constant::getNullValue(getType()->getSequentialElementType());
778 Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
779 return Constant::getNullValue(getType()->getStructElementType(Elt));
784 Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
792 Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
825 UndefValue *UndefValue::getElementValue(Constant *C) const {
861 static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
865 for (Constant *C : V)
874 static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
878 for (Constant *C : V)
887 static Constant *getSequenceIfElementsMatch(Constant *C,
888 ArrayRef<Constant *> V) {
913 ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
914 : Constant(T, ConstantArrayVal,
918 "Invalid initializer vector for constant array");
925 Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
926 if (Constant *C = getImpl(Ty, V))
931 Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
944 Constant *C = V[0];
960 /// getTypeForElements - Return an anonymous struct type to use for a constant
963 ArrayRef<Constant*> V,
974 StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
982 ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
983 : Constant(T, ConstantStructVal,
987 "Invalid initializer vector for constant structure");
995 Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
1023 Constant *ConstantStruct::get(StructType *T, ...) {
1025 SmallVector<Constant*, 8> Values;
1027 while (Constant *Val = va_arg(ap, llvm::Constant*))
1033 ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
1034 : Constant(T, ConstantVectorVal,
1044 Constant *ConstantVector::get(ArrayRef<Constant*> V) {
1045 if (Constant *C = getImpl(V))
1051 Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
1057 Constant *C = V[0];
1084 Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
1091 SmallVector<Constant*, 32> Elts(NumElts, V);
1102 /// Remove the constant from the constant table.
1109 // Constant.h
1160 /// getWithOperandReplaced - Return a constant expression identical to this
1162 Constant *
1163 ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
1169 SmallVector<Constant*, 8> NewOps;
1176 /// getWithOperands - This returns the current constant expression with the
1179 Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1323 /// destroyConstant - Remove the constant from the constant table.
1329 /// destroyConstant - Remove the constant from the constant table...
1339 // destroyConstant - Remove the constant from the constant table...
1345 // destroyConstant - Remove the constant from the constant table...
1351 /// getSplatValue - If this is a splat vector constant, meaning that all of
1353 Constant *Constant::getSplatValue() const {
1364 /// getSplatValue - If this is a splat constant, where all of the
1366 Constant *ConstantVector::getSplatValue() const {
1368 Constant *Elt = getOperand(0);
1376 /// If C is a constant integer then return its value, otherwise C must be a
1377 /// vector of constant integers, all equal, and the common value is returned.
1378 const APInt &Constant::getUniqueInteger() const {
1382 const Constant *C = this->getAggregateElement(0U);
1398 // destroyConstant - Remove the constant from the constant table...
1416 // destroyConstant - Remove the constant from the constant table.
1419 // Free the constant and any dangling references to it.
1442 : Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1461 // destroyConstant - Remove the constant from the constant table.
1508 static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
1512 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1520 // Look up the constant in the table first to ensure uniqueness.
1526 Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
1565 Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
1571 Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
1577 Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
1583 Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
1598 Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
1609 Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty,
1622 Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
1634 Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
1648 Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
1662 Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
1676 Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
1688 Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
1700 Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
1711 Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
1722 Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
1733 Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
1744 Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
1757 Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
1770 Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
1782 Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
1803 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
1808 "Invalid opcode in binary constant expression");
1810 "Operand types in binary constant expression should match");
1870 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1876 Constant *ArgVec[] = { C1, C2 };
1883 Constant *ConstantExpr::getSizeOf(Type* Ty) {
1886 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1887 Constant *GEP = getGetElementPtr(
1888 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1893 Constant *ConstantExpr::getAlignOf(Type* Ty) {
1898 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
1899 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
1900 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1901 Constant *Indices[2] = { Zero, One };
1902 Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
1907 Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
1912 Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
1915 Constant *GEPIdx[] = {
1919 Constant *GEP = getGetElementPtr(
1920 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1925 Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
1926 Constant *C2, bool OnlyIfReduced) {
1947 Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
1951 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1957 Constant *ArgVec[] = { C, V1, V2 };
1964 Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
1974 if (Constant *FC = ConstantFoldGetElementPtr(Ty, C, InBounds, Idxs))
1988 // Look up the constant in the table first to ensure uniqueness
1989 std::vector<Constant*> ArgVec;
1999 ArgVec.push_back(cast<Constant>(Idxs[i]));
2009 Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
2010 Constant *RHS, bool OnlyIfReduced) {
2015 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
2021 // Look up the constant in the table first to ensure uniqueness
2022 Constant *ArgVec[] = { LHS, RHS };
2034 Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
2035 Constant *RHS, bool OnlyIfReduced) {
2039 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
2045 // Look up the constant in the table first to ensure uniqueness
2046 Constant *ArgVec[] = { LHS, RHS };
2058 Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
2065 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2072 // Look up the constant in the table first to ensure uniqueness
2073 Constant *ArgVec[] = { Val, Idx };
2080 Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2081 Constant *Idx, Type *OnlyIfReducedTy) {
2089 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2095 // Look up the constant in the table first to ensure uniqueness
2096 Constant *ArgVec[] = { Val, Elt, Idx };
2103 Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2104 Constant *Mask, Type *OnlyIfReducedTy) {
2106 "Invalid shuffle vector constant expr operands!");
2108 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2118 // Look up the constant in the table first to ensure uniqueness
2119 Constant *ArgVec[] = { V1, V2, Mask };
2126 Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
2130 "Non-first-class type for constant insertvalue expression");
2137 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2143 Constant *ArgVec[] = { Agg, Val };
2150 Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2160 "Non-first-class type for constant extractvalue expression");
2161 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2167 Constant *ArgVec[] = { Agg };
2174 Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
2181 Constant *ConstantExpr::getFNeg(Constant *C) {
2187 Constant *ConstantExpr::getNot(Constant *C) {
2190 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
2193 Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2200 Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
2204 Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2211 Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
2215 Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2222 Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
2226 Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2231 Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2236 Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
2240 Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
2244 Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
2248 Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
2252 Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
2256 Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
2260 Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
2264 Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2271 Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2276 Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2282 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
2284 Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) {
2293 return Constant::getNullValue(Ty);
2299 return Constant::getAllOnesValue(Ty);
2304 /// operation, i.e. a constant C such that X op C = C and C op X = C for
2307 Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2314 return Constant::getAllOnesValue(Ty);
2318 return Constant::getNullValue(Ty);
2322 // destroyConstant - Remove the constant from the constant table...
2333 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2415 Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
2448 // Remove the constant from the StringMap.
2484 /// get() constructors - Return a constant with array type with an element
2487 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) {
2492 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
2497 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
2502 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
2507 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) {
2512 Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) {
2518 /// getFP() constructors - Return a constant with array type with an element
2523 Constant *ConstantDataArray::getFP(LLVMContext &Context,
2529 Constant *ConstantDataArray::getFP(LLVMContext &Context,
2535 Constant *ConstantDataArray::getFP(LLVMContext &Context,
2547 Constant *ConstantDataArray::getString(LLVMContext &Context,
2561 /// get() constructors - Return a constant with vector type with an element
2564 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
2569 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
2574 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
2579 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
2584 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
2589 Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
2595 /// getFP() constructors - Return a constant with vector type with an element
2600 Constant *ConstantDataVector::getFP(LLVMContext &Context,
2606 Constant *ConstantDataVector::getFP(LLVMContext &Context,
2612 Constant *ConstantDataVector::getFP(LLVMContext &Context,
2619 Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2725 /// getElementAsConstant - Return a Constant for a specified index's element.
2726 /// Note that this has to compute a new constant to return, so it isn't as
2728 Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
2756 /// getSplatValue - If this is a splat constant, meaning that all of the
2758 Constant *ConstantDataVector::getSplatValue() const {
2774 /// Update this constant array to change uses of
2785 void Constant::handleOperandChange(Value *From, Value *To, Use *U) {
2789 llvm_unreachable("Not a constant!");
2808 // Delete the old constant!
2845 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2846 Constant *ToC = cast<Constant>(To);
2848 SmallVector<Constant*, 8> Values;
2851 // Fill values with the modified operands of the constant array. Also,
2859 Constant *Val = cast<Constant>(O->get());
2874 // Check for any other type of constant-folding.
2875 if (Constant *C = getImpl(getType(), Values))
2884 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2885 Constant *ToC = cast<Constant>(To);
2891 SmallVector<Constant*, 8> Values;
2894 // Fill values with the modified operands of the constant struct. Also,
2901 Constant *Val = cast<Constant>(O->get());
2908 Constant *Val = cast<Constant>(O->get());
2914 Values.push_back(cast<Constant>(O->get()));
2930 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2931 Constant *ToC = cast<Constant>(To);
2933 SmallVector<Constant*, 8> Values;
2937 Constant *Val = getOperand(i);
2945 if (Constant *C = getImpl(Values))
2955 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2956 Constant *To = cast<Constant>(ToV);
2958 SmallVector<Constant*, 8> NewOps;
2961 Constant *Op = getOperand(i);
2970 if (Constant *C = getWithOperands(NewOps, getType(), true))