29 #include "llvm/Config/config.h" 41 #include "llvm/IR/IntrinsicsX86.h" 71 for (
unsigned i = 0; i != NumSrcElts; ++i) {
78 if (Element && isa<UndefValue>(Element)) {
83 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
99 "Invalid constantexpr bitcast!");
108 if (
auto *VTy = dyn_cast<VectorType>(C->
getType())) {
111 unsigned NumSrcElts = VTy->getNumElements();
112 Type *SrcEltTy = VTy->getElementType();
125 if (
Constant *CE = foldConstVectorToAPInt(Result, DestTy, C,
126 SrcEltTy, NumSrcElts, DL))
129 if (isa<IntegerType>(DestTy))
144 if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
150 if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
154 unsigned NumDstElt = DestVTy->getNumElements();
156 if (NumDstElt == NumSrcElt)
160 Type *DstEltTy = DestVTy->getElementType();
193 if (!isa<ConstantVector>(C) &&
194 !isa<ConstantDataVector>(C))
205 if (NumDstElt < NumSrcElt) {
208 unsigned Ratio = NumSrcElt/NumDstElt;
211 for (
unsigned i = 0; i != NumDstElt; ++i) {
214 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
215 for (
unsigned j = 0; j != Ratio; ++j) {
217 if (Src && isa<UndefValue>(Src))
220 Src = dyn_cast_or_null<ConstantInt>(Src);
230 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
241 unsigned Ratio = NumDstElt/NumSrcElt;
245 for (
unsigned i = 0; i != NumSrcElt; ++i) {
251 if (isa<UndefValue>(Element)) {
261 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
262 for (
unsigned j = 0; j != Ratio; ++j) {
267 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
293 if ((GV = dyn_cast<GlobalValue>(C))) {
295 Offset =
APInt(BitWidth, 0);
301 if (!CE)
return false;
304 if (CE->getOpcode() == Instruction::PtrToInt ||
305 CE->getOpcode() == Instruction::BitCast)
314 APInt TmpOffset(BitWidth, 0);
321 if (!
GEP->accumulateConstantOffset(DL, TmpOffset))
340 Cast = Instruction::IntToPtr;
342 Cast = Instruction::PtrToInt;
380 bool ReadDataFromGlobal(
Constant *C, uint64_t ByteOffset,
unsigned char *CurPtr,
383 "Out of range access");
387 if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
390 if (
auto *CI = dyn_cast<ConstantInt>(C)) {
391 if (CI->getBitWidth() > 64 ||
392 (CI->getBitWidth() & 7) != 0)
395 uint64_t Val = CI->getZExtValue();
396 unsigned IntBytes =
unsigned(CI->getBitWidth()/8);
398 for (
unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
401 n = IntBytes - n - 1;
402 CurPtr[i] = (
unsigned char)(Val >> (n * 8));
408 if (
auto *CFP = dyn_cast<ConstantFP>(C)) {
409 if (CFP->getType()->isDoubleTy()) {
411 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
413 if (CFP->getType()->isFloatTy()){
415 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
417 if (CFP->getType()->isHalfTy()){
419 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
424 if (
auto *CS = dyn_cast<ConstantStruct>(C)) {
428 ByteOffset -= CurEltOffset;
435 if (ByteOffset < EltSize &&
436 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
443 if (Index == CS->getType()->getNumElements())
449 if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
453 CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
454 BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
456 CurEltOffset = NextEltOffset;
461 if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
462 isa<ConstantDataSequential>(C)) {
465 uint64_t
Index = ByteOffset / EltSize;
466 uint64_t
Offset = ByteOffset - Index * EltSize;
468 if (
auto *AT = dyn_cast<ArrayType>(C->
getType()))
469 NumElts = AT->getNumElements();
473 for (; Index != NumElts; ++
Index) {
478 uint64_t BytesWritten = EltSize -
Offset;
479 assert(BytesWritten <= EltSize &&
"Not indexing into this element?");
480 if (BytesWritten >= BytesLeft)
484 BytesLeft -= BytesWritten;
485 CurPtr += BytesWritten;
490 if (
auto *CE = dyn_cast<ConstantExpr>(C)) {
491 if (CE->getOpcode() == Instruction::IntToPtr &&
492 CE->getOperand(0)->getType() == DL.
getIntPtrType(CE->getType())) {
493 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
504 auto *PTy = cast<PointerType>(C->
getType());
509 unsigned AS = PTy->getAddressSpace();
529 if (
Constant *Res = FoldReinterpretLoadFromConstPtr(C, MapTy, DL)) {
549 unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
550 if (BytesLoaded > 32 || BytesLoaded == 0)
559 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
560 !GV->getInitializer()->getType()->isSized())
564 int64_t InitializerSize = DL.
getTypeAllocSize(GV->getInitializer()->getType());
567 if (Offset <= -1 * static_cast<int64_t>(BytesLoaded))
571 if (Offset >= InitializerSize)
574 unsigned char RawBytes[32] = {0};
575 unsigned char *CurPtr = RawBytes;
576 unsigned BytesLeft = BytesLoaded;
585 if (!ReadDataFromGlobal(GV->getInitializer(),
Offset, CurPtr, BytesLeft, DL))
588 APInt ResultVal =
APInt(IntType->getBitWidth(), 0);
590 ResultVal = RawBytes[BytesLoaded - 1];
591 for (
unsigned i = 1; i != BytesLoaded; ++i) {
593 ResultVal |= RawBytes[BytesLoaded - 1 - i];
596 ResultVal = RawBytes[0];
597 for (
unsigned i = 1; i != BytesLoaded; ++i) {
599 ResultVal |= RawBytes[i];
626 if (
auto *GV = dyn_cast<GlobalVariable>(C))
627 if (GV->isConstant() && GV->hasDefinitiveInitializer())
628 return GV->getInitializer();
630 if (
auto *GA = dyn_cast<GlobalAlias>(C))
631 if (GA->getAliasee() && !GA->isInterposable())
639 if (CE->getOpcode() == Instruction::GetElementPtr) {
640 if (
auto *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
641 if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
649 if (CE->getOpcode() == Instruction::BitCast)
650 if (
Constant *LoadedC = ConstantFoldLoadThroughBitcastExpr(CE, Ty, DL))
657 size_t StrLen = Str.
size();
661 if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
664 APInt SingleChar(NumBits, 0);
667 SingleChar =
static_cast<uint64_t
>(
C);
668 StrVal = (StrVal << 8) | SingleChar;
671 for (
unsigned char C : Str.
bytes()) {
672 SingleChar =
static_cast<uint64_t
>(
C);
673 StrVal = (StrVal << 8) | SingleChar;
677 StrVal = (StrVal << 8) | SingleChar;
690 if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
691 if (GV->getInitializer()->isNullValue())
693 if (isa<UndefValue>(GV->getInitializer()))
699 return FoldReinterpretLoadFromConstPtr(CE, Ty, DL);
707 if (
auto *C = dyn_cast<Constant>(LI->
getOperand(0)))
725 if (Opc == Instruction::And) {
728 if ((Known1.
One | Known0.
Zero).isAllOnesValue()) {
732 if ((Known0.
One | Known1.
Zero).isAllOnesValue()) {
745 if (Opc == Instruction::Sub) {
774 for (
unsigned i = 1,
e = Ops.
size(); i !=
e; ++i) {
777 SrcElemTy, Ops.
slice(1, i - 1)))) &&
778 Ops[i]->
getType()->getScalarType() != IntIdxScalarTy) {
780 Type *NewType = Ops[i]->getType()->isVectorTy()
796 SrcElemTy, Ops[0], NewIdxs,
false, InRangeIndex);
806 auto *OldPtrTy = cast<PointerType>(Ptr->
getType());
807 Ptr = cast<Constant>(Ptr->stripPointerCasts());
808 auto *NewPtrTy = cast<PointerType>(Ptr->getType());
813 if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
814 NewPtrTy = ElemTy->
getPointerTo(OldPtrTy->getAddressSpace());
834 if (
Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy,
846 for (
unsigned i = 1,
e = Ops.
size(); i !=
e; ++i)
847 if (!isa<ConstantInt>(Ops[i])) {
853 assert((!CE || CE->getType() == IntIdxTy) &&
854 "CastGEPIndices didn't canonicalize index types!");
855 if (CE && CE->getOpcode() == Instruction::Sub &&
856 CE->getOperand(0)->isNullValue()) {
874 Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
877 while (
auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
884 bool AllConstantInt =
true;
885 for (
Value *NestedOp : NestedOps)
886 if (!isa<ConstantInt>(NestedOp)) {
887 AllConstantInt =
false;
896 Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
901 APInt BasePtr(BitWidth, 0);
902 if (
auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
903 if (CE->getOpcode() == Instruction::IntToPtr) {
904 if (
auto *
Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
905 BasePtr =
Base->getValue().zextOrTrunc(BitWidth);
909 auto *PTy = cast<PointerType>(Ptr->getType());
927 if (!NewIdxs.
empty())
935 }
else if (
auto *ATy = dyn_cast<SequentialType>(Ty)) {
936 Ty = ATy->getElementType();
956 Offset -= NewIdx * ElemSize;
960 auto *STy = cast<StructType>(Ty);
975 Offset -=
APInt(BitWidth, SL.getElementOffset(ElIdx));
976 Ty = STy->getTypeAtIndex(ElIdx);
978 }
while (Ty != ResElemTy);
991 NewIdxs.
size() > *LastIRIndex) {
992 InRangeIndex = LastIRIndex;
993 for (
unsigned I = 0;
I <= *LastIRIndex; ++
I)
1000 InBounds, InRangeIndex);
1002 "Computed GetElementPtr has unexpected type!");
1006 if (Ty != ResElemTy)
1017 Constant *ConstantFoldInstOperandsImpl(
const Value *InstOrCE,
unsigned Opcode,
1032 if (
auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) {
1033 if (
Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
1041 if (
auto *CE = dyn_cast<ConstantExpr>(InstOrCE))
1042 return CE->getWithOperands(Ops);
1045 default:
return nullptr;
1046 case Instruction::ICmp:
1049 if (
auto *
F = dyn_cast<Function>(Ops.
back())) {
1050 const auto *Call = cast<CallBase>(InstOrCE);
1057 case Instruction::ExtractElement:
1059 case Instruction::ExtractValue:
1061 Ops[0], cast<ExtractValueInst>(InstOrCE)->getIndices());
1062 case Instruction::InsertElement:
1064 case Instruction::ShuffleVector:
1081 if (!isa<ConstantVector>(C) && !isa<ConstantExpr>(C))
1086 auto *NewC = cast<Constant>(&NewU);
1089 if (isa<ConstantVector>(NewC) || isa<ConstantExpr>(NewC)) {
1090 auto It = FoldedOps.
find(NewC);
1091 if (It == FoldedOps.
end()) {
1093 ConstantFoldConstantImpl(NewC, DL, TLI, FoldedOps)) {
1094 FoldedOps.
insert({NewC, FoldedC});
1097 FoldedOps.
insert({NewC, NewC});
1106 if (
auto *CE = dyn_cast<ConstantExpr>(C)) {
1107 if (CE->isCompare())
1111 return ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI);
1114 assert(isa<ConstantVector>(C));
1123 if (
auto *PN = dyn_cast<PHINode>(I)) {
1127 for (
Value *Incoming : PN->incoming_values()) {
1132 if (isa<UndefValue>(Incoming))
1139 if (
auto *FoldedC = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps))
1143 if (CommonValue && C != CommonValue)
1160 auto *
Op = cast<Constant>(&OpU);
1162 if (
auto *FoldedOp = ConstantFoldConstantImpl(
Op, DL, TLI, FoldedOps))
1168 if (
const auto *CI = dyn_cast<CmpInst>(I))
1172 if (
const auto *LI = dyn_cast<LoadInst>(I))
1173 return ConstantFoldLoadInst(LI, DL);
1175 if (
auto *IVI = dyn_cast<InsertValueInst>(I)) {
1177 cast<Constant>(IVI->getAggregateOperand()),
1178 cast<Constant>(IVI->getInsertedValueOperand()),
1182 if (
auto *EVI = dyn_cast<ExtractValueInst>(I)) {
1184 cast<Constant>(EVI->getAggregateOperand()),
1194 return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1201 return ConstantFoldInstOperandsImpl(I, I->
getOpcode(), Ops, DL, TLI);
1218 if (
auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1220 if (CE0->getOpcode() == Instruction::IntToPtr) {
1232 if (CE0->getOpcode() == Instruction::PtrToInt) {
1234 if (CE0->getType() == IntPtrTy) {
1242 if (
auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1243 if (CE0->getOpcode() == CE1->getOpcode()) {
1244 if (CE0->getOpcode() == Instruction::IntToPtr) {
1258 if (CE0->getOpcode() == Instruction::PtrToInt) {
1260 if (CE0->getType() == IntPtrTy &&
1261 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1263 Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI);
1272 CE0->getOpcode() == Instruction::Or && Ops1->
isNullValue()) {
1274 Predicate, CE0->getOperand(0), Ops1, DL, TLI);
1276 Predicate, CE0->getOperand(1), Ops1, DL, TLI);
1281 }
else if (isa<ConstantExpr>(Ops1)) {
1302 if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS))
1303 if (
Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL))
1315 case Instruction::PtrToInt:
1318 if (
auto *CE = dyn_cast<ConstantExpr>(C)) {
1319 if (CE->getOpcode() == Instruction::IntToPtr) {
1320 Constant *Input = CE->getOperand(0);
1323 if (PtrWidth < InWidth) {
1334 case Instruction::IntToPtr:
1339 if (
auto *CE = dyn_cast<ConstantExpr>(C)) {
1340 if (CE->getOpcode() == Instruction::PtrToInt) {
1341 Constant *SrcPtr = CE->getOperand(0);
1343 unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1345 if (MidIntSize >= SrcPtrSize) {
1348 return FoldBitCast(CE->getOperand(0), DestTy, DL);
1354 case Instruction::Trunc:
1355 case Instruction::ZExt:
1356 case Instruction::SExt:
1357 case Instruction::FPTrunc:
1358 case Instruction::FPExt:
1359 case Instruction::UIToFP:
1360 case Instruction::SIToFP:
1361 case Instruction::FPToUI:
1362 case Instruction::FPToSI:
1363 case Instruction::AddrSpaceCast:
1365 case Instruction::BitCast:
1406 case Intrinsic::fabs:
1411 case Intrinsic::log:
1413 case Intrinsic::log10:
1414 case Intrinsic::exp:
1415 case Intrinsic::exp2:
1416 case Intrinsic::floor:
1417 case Intrinsic::ceil:
1418 case Intrinsic::sqrt:
1419 case Intrinsic::sin:
1420 case Intrinsic::cos:
1421 case Intrinsic::trunc:
1422 case Intrinsic::rint:
1423 case Intrinsic::nearbyint:
1424 case Intrinsic::pow:
1425 case Intrinsic::powi:
1426 case Intrinsic::bswap:
1427 case Intrinsic::ctpop:
1428 case Intrinsic::ctlz:
1429 case Intrinsic::cttz:
1430 case Intrinsic::fshl:
1431 case Intrinsic::fshr:
1432 case Intrinsic::fma:
1433 case Intrinsic::fmuladd:
1434 case Intrinsic::copysign:
1435 case Intrinsic::launder_invariant_group:
1436 case Intrinsic::strip_invariant_group:
1438 case Intrinsic::masked_load:
1439 case Intrinsic::sadd_with_overflow:
1440 case Intrinsic::uadd_with_overflow:
1441 case Intrinsic::ssub_with_overflow:
1442 case Intrinsic::usub_with_overflow:
1443 case Intrinsic::smul_with_overflow:
1444 case Intrinsic::umul_with_overflow:
1445 case Intrinsic::sadd_sat:
1446 case Intrinsic::uadd_sat:
1447 case Intrinsic::ssub_sat:
1448 case Intrinsic::usub_sat:
1449 case Intrinsic::smul_fix:
1450 case Intrinsic::smul_fix_sat:
1451 case Intrinsic::convert_from_fp16:
1452 case Intrinsic::convert_to_fp16:
1453 case Intrinsic::bitreverse:
1454 case Intrinsic::x86_sse_cvtss2si:
1455 case Intrinsic::x86_sse_cvtss2si64:
1456 case Intrinsic::x86_sse_cvttss2si:
1457 case Intrinsic::x86_sse_cvttss2si64:
1458 case Intrinsic::x86_sse2_cvtsd2si:
1459 case Intrinsic::x86_sse2_cvtsd2si64:
1460 case Intrinsic::x86_sse2_cvttsd2si:
1461 case Intrinsic::x86_sse2_cvttsd2si64:
1462 case Intrinsic::x86_avx512_vcvtss2si32:
1463 case Intrinsic::x86_avx512_vcvtss2si64:
1464 case Intrinsic::x86_avx512_cvttss2si:
1465 case Intrinsic::x86_avx512_cvttss2si64:
1466 case Intrinsic::x86_avx512_vcvtsd2si32:
1467 case Intrinsic::x86_avx512_vcvtsd2si64:
1468 case Intrinsic::x86_avx512_cvttsd2si:
1469 case Intrinsic::x86_avx512_cvttsd2si64:
1470 case Intrinsic::x86_avx512_vcvtss2usi32:
1471 case Intrinsic::x86_avx512_vcvtss2usi64:
1472 case Intrinsic::x86_avx512_cvttss2usi:
1473 case Intrinsic::x86_avx512_cvttss2usi64:
1474 case Intrinsic::x86_avx512_vcvtsd2usi32:
1475 case Intrinsic::x86_avx512_vcvtsd2usi64:
1476 case Intrinsic::x86_avx512_cvttsd2usi:
1477 case Intrinsic::x86_avx512_cvttsd2usi64:
1478 case Intrinsic::is_constant:
1496 return Name ==
"acos" || Name ==
"acosf" ||
1497 Name ==
"asin" || Name ==
"asinf" ||
1498 Name ==
"atan" || Name ==
"atanf" ||
1499 Name ==
"atan2" || Name ==
"atan2f";
1501 return Name ==
"ceil" || Name ==
"ceilf" ||
1502 Name ==
"cos" || Name ==
"cosf" ||
1503 Name ==
"cosh" || Name ==
"coshf";
1505 return Name ==
"exp" || Name ==
"expf" ||
1506 Name ==
"exp2" || Name ==
"exp2f";
1508 return Name ==
"fabs" || Name ==
"fabsf" ||
1509 Name ==
"floor" || Name ==
"floorf" ||
1510 Name ==
"fmod" || Name ==
"fmodf";
1512 return Name ==
"log" || Name ==
"logf" ||
1513 Name ==
"log2" || Name ==
"log2f" ||
1514 Name ==
"log10" || Name ==
"log10f";
1516 return Name ==
"nearbyint" || Name ==
"nearbyintf";
1518 return Name ==
"pow" || Name ==
"powf";
1520 return Name ==
"rint" || Name ==
"rintf" ||
1521 Name ==
"round" || Name ==
"roundf";
1523 return Name ==
"sin" || Name ==
"sinf" ||
1524 Name ==
"sinh" || Name ==
"sinhf" ||
1525 Name ==
"sqrt" || Name ==
"sqrtf";
1527 return Name ==
"tan" || Name ==
"tanf" ||
1528 Name ==
"tanh" || Name ==
"tanhf" ||
1529 Name ==
"trunc" || Name ==
"truncf";
1537 if (Name.
size() < 12 || Name[1] !=
'_')
1543 return Name ==
"__acos_finite" || Name ==
"__acosf_finite" ||
1544 Name ==
"__asin_finite" || Name ==
"__asinf_finite" ||
1545 Name ==
"__atan2_finite" || Name ==
"__atan2f_finite";
1547 return Name ==
"__cosh_finite" || Name ==
"__coshf_finite";
1549 return Name ==
"__exp_finite" || Name ==
"__expf_finite" ||
1550 Name ==
"__exp2_finite" || Name ==
"__exp2f_finite";
1552 return Name ==
"__log_finite" || Name ==
"__logf_finite" ||
1553 Name ==
"__log10_finite" || Name ==
"__log10f_finite";
1555 return Name ==
"__pow_finite" || Name ==
"__powf_finite";
1557 return Name ==
"__sinh_finite" || Name ==
"__sinhf_finite";
1577 inline void llvm_fenv_clearexcept() {
1578 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT 1579 feclearexcept(FE_ALL_EXCEPT);
1585 inline bool llvm_fenv_testexcept() {
1586 int errno_val = errno;
1587 if (errno_val == ERANGE || errno_val == EDOM)
1589 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT 1590 if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
1596 Constant *ConstantFoldFP(
double (*NativeFP)(
double),
double V,
Type *Ty) {
1597 llvm_fenv_clearexcept();
1599 if (llvm_fenv_testexcept()) {
1600 llvm_fenv_clearexcept();
1604 return GetConstantFoldFPValue(V, Ty);
1607 Constant *ConstantFoldBinaryFP(
double (*NativeFP)(
double,
double),
double V,
1608 double W,
Type *Ty) {
1609 llvm_fenv_clearexcept();
1611 if (llvm_fenv_testexcept()) {
1612 llvm_fenv_clearexcept();
1616 return GetConstantFoldFPValue(V, Ty);
1626 Constant *ConstantFoldSSEConvertToInt(
const APFloat &Val,
bool roundTowardZero,
1627 Type *Ty,
bool IsSigned) {
1630 assert(ResultWidth <= 64 &&
1631 "Can only constant fold conversions to 64 and 32 bit ints");
1634 bool isExact =
false;
1639 IsSigned, mode, &isExact);
1661 static bool isManifestConstant(
const Constant *c) {
1662 if (isa<ConstantData>(c)) {
1664 }
else if (isa<ConstantAggregate>(c) || isa<ConstantExpr>(c)) {
1666 if (!isManifestConstant(cast<Constant>(subc)))
1674 static bool getConstIntOrUndef(
Value *Op,
const APInt *&C) {
1675 if (
auto *CI = dyn_cast<ConstantInt>(Op)) {
1676 C = &CI->getValue();
1679 if (isa<UndefValue>(Op)) {
1692 assert(Operands.
size() == 1 &&
"Wrong number of operands.");
1694 if (IntrinsicID == Intrinsic::is_constant) {
1698 if (isManifestConstant(Operands[0]))
1702 if (isa<UndefValue>(Operands[0])) {
1705 if (IntrinsicID == Intrinsic::cos ||
1706 IntrinsicID == Intrinsic::ctpop)
1708 if (IntrinsicID == Intrinsic::bswap ||
1709 IntrinsicID == Intrinsic::bitreverse ||
1710 IntrinsicID == Intrinsic::launder_invariant_group ||
1711 IntrinsicID == Intrinsic::strip_invariant_group)
1715 if (isa<ConstantPointerNull>(Operands[0])) {
1717 if (IntrinsicID == Intrinsic::launder_invariant_group ||
1718 IntrinsicID == Intrinsic::strip_invariant_group) {
1726 Caller, Operands[0]->
getType()->getPointerAddressSpace())) {
1733 if (
auto *Op = dyn_cast<ConstantFP>(Operands[0])) {
1734 if (IntrinsicID == Intrinsic::convert_to_fp16) {
1735 APFloat Val(Op->getValueAPF());
1747 APFloat U = Op->getValueAPF();
1749 if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) {
1759 if (IntrinsicID == Intrinsic::ceil) {
1764 if (IntrinsicID == Intrinsic::floor) {
1769 if (IntrinsicID == Intrinsic::trunc) {
1774 if (IntrinsicID == Intrinsic::fabs) {
1782 if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
1789 double V = getValueAsDouble(Op);
1791 switch (IntrinsicID) {
1793 case Intrinsic::log:
1794 return ConstantFoldFP(log, V, Ty);
1797 return ConstantFoldFP(
Log2, V, Ty);
1798 case Intrinsic::log10:
1800 return ConstantFoldFP(log10, V, Ty);
1801 case Intrinsic::exp:
1802 return ConstantFoldFP(exp, V, Ty);
1803 case Intrinsic::exp2:
1805 return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
1806 case Intrinsic::sin:
1807 return ConstantFoldFP(sin, V, Ty);
1808 case Intrinsic::cos:
1809 return ConstantFoldFP(cos, V, Ty);
1810 case Intrinsic::sqrt:
1811 return ConstantFoldFP(sqrt, V, Ty);
1824 case LibFunc_acos_finite:
1825 case LibFunc_acosf_finite:
1827 return ConstantFoldFP(acos, V, Ty);
1831 case LibFunc_asin_finite:
1832 case LibFunc_asinf_finite:
1834 return ConstantFoldFP(asin, V, Ty);
1839 return ConstantFoldFP(atan, V, Ty);
1843 if (TLI->
has(Func)) {
1851 return ConstantFoldFP(cos, V, Ty);
1855 case LibFunc_cosh_finite:
1856 case LibFunc_coshf_finite:
1858 return ConstantFoldFP(cosh, V, Ty);
1862 case LibFunc_exp_finite:
1863 case LibFunc_expf_finite:
1865 return ConstantFoldFP(exp, V, Ty);
1869 case LibFunc_exp2_finite:
1870 case LibFunc_exp2f_finite:
1873 return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
1877 if (TLI->
has(Func)) {
1883 case LibFunc_floorf:
1884 if (TLI->
has(Func)) {
1891 case LibFunc_log_finite:
1892 case LibFunc_logf_finite:
1893 if (V > 0.0 && TLI->
has(Func))
1894 return ConstantFoldFP(log, V, Ty);
1898 case LibFunc_log2_finite:
1899 case LibFunc_log2f_finite:
1900 if (V > 0.0 && TLI->
has(Func))
1902 return ConstantFoldFP(
Log2, V, Ty);
1905 case LibFunc_log10f:
1906 case LibFunc_log10_finite:
1907 case LibFunc_log10f_finite:
1908 if (V > 0.0 && TLI->
has(Func))
1910 return ConstantFoldFP(log10, V, Ty);
1912 case LibFunc_nearbyint:
1913 case LibFunc_nearbyintf:
1916 if (TLI->
has(Func)) {
1922 case LibFunc_roundf:
1923 if (TLI->
has(Func)) {
1931 return ConstantFoldFP(sin, V, Ty);
1935 case LibFunc_sinh_finite:
1936 case LibFunc_sinhf_finite:
1938 return ConstantFoldFP(sinh, V, Ty);
1942 if (V >= 0.0 && TLI->
has(Func))
1943 return ConstantFoldFP(sqrt, V, Ty);
1948 return ConstantFoldFP(tan, V, Ty);
1953 return ConstantFoldFP(tanh, V, Ty);
1956 case LibFunc_truncf:
1957 if (TLI->
has(Func)) {
1966 if (
auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
1967 switch (IntrinsicID) {
1968 case Intrinsic::bswap:
1970 case Intrinsic::ctpop:
1972 case Intrinsic::bitreverse:
1974 case Intrinsic::convert_from_fp16: {
1984 "Precision lost during fp16 constfolding");
1994 if (isa<ConstantVector>(Operands[0]) ||
1995 isa<ConstantDataVector>(Operands[0])) {
1996 auto *Op = cast<Constant>(Operands[0]);
1997 switch (IntrinsicID) {
1999 case Intrinsic::x86_sse_cvtss2si:
2000 case Intrinsic::x86_sse_cvtss2si64:
2001 case Intrinsic::x86_sse2_cvtsd2si:
2002 case Intrinsic::x86_sse2_cvtsd2si64:
2004 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2005 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2009 case Intrinsic::x86_sse_cvttss2si:
2010 case Intrinsic::x86_sse_cvttss2si64:
2011 case Intrinsic::x86_sse2_cvttsd2si:
2012 case Intrinsic::x86_sse2_cvttsd2si64:
2014 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2015 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2031 assert(Operands.
size() == 2 &&
"Wrong number of operands.");
2033 if (
auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
2036 double Op1V = getValueAsDouble(Op1);
2038 if (
auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
2039 if (Op2->getType() != Op1->getType())
2042 double Op2V = getValueAsDouble(Op2);
2043 if (IntrinsicID == Intrinsic::pow) {
2044 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
2046 if (IntrinsicID == Intrinsic::copysign) {
2047 APFloat V1 = Op1->getValueAPF();
2054 const APFloat &C1 = Op1->getValueAPF();
2055 const APFloat &C2 = Op2->getValueAPF();
2060 const APFloat &C1 = Op1->getValueAPF();
2061 const APFloat &C2 = Op2->getValueAPF();
2066 const APFloat &C1 = Op1->getValueAPF();
2067 const APFloat &C2 = Op2->getValueAPF();
2072 const APFloat &C1 = Op1->getValueAPF();
2073 const APFloat &C2 = Op2->getValueAPF();
2087 case LibFunc_pow_finite:
2088 case LibFunc_powf_finite:
2090 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
2094 if (TLI->
has(Func)) {
2095 APFloat V = Op1->getValueAPF();
2096 if (APFloat::opStatus::opOK == V.
mod(Op2->getValueAPF()))
2101 case LibFunc_atan2f:
2102 case LibFunc_atan2_finite:
2103 case LibFunc_atan2f_finite:
2105 return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
2108 }
else if (
auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
2109 if (IntrinsicID == Intrinsic::powi && Ty->
isHalfTy())
2111 APFloat((
float)std::pow((
float)Op1V,
2112 (
int)Op2C->getZExtValue())));
2113 if (IntrinsicID == Intrinsic::powi && Ty->
isFloatTy())
2115 APFloat((
float)std::pow((
float)Op1V,
2116 (
int)Op2C->getZExtValue())));
2117 if (IntrinsicID == Intrinsic::powi && Ty->
isDoubleTy())
2119 APFloat((
double)std::pow((
double)Op1V,
2120 (
int)Op2C->getZExtValue())));
2125 if (Operands[0]->
getType()->isIntegerTy() &&
2126 Operands[1]->getType()->isIntegerTy()) {
2127 const APInt *C0, *C1;
2128 if (!getConstIntOrUndef(Operands[0], C0) ||
2129 !getConstIntOrUndef(Operands[1], C1))
2132 switch (IntrinsicID) {
2134 case Intrinsic::usub_with_overflow:
2135 case Intrinsic::ssub_with_overflow:
2136 case Intrinsic::uadd_with_overflow:
2137 case Intrinsic::sadd_with_overflow:
2144 cast<StructType>(Ty),
2149 case Intrinsic::smul_with_overflow:
2150 case Intrinsic::umul_with_overflow: {
2158 switch (IntrinsicID) {
2160 case Intrinsic::sadd_with_overflow:
2161 Res = C0->
sadd_ov(*C1, Overflow);
2163 case Intrinsic::uadd_with_overflow:
2164 Res = C0->
uadd_ov(*C1, Overflow);
2166 case Intrinsic::ssub_with_overflow:
2167 Res = C0->
ssub_ov(*C1, Overflow);
2169 case Intrinsic::usub_with_overflow:
2170 Res = C0->
usub_ov(*C1, Overflow);
2172 case Intrinsic::smul_with_overflow:
2173 Res = C0->
smul_ov(*C1, Overflow);
2175 case Intrinsic::umul_with_overflow:
2176 Res = C0->
umul_ov(*C1, Overflow);
2185 case Intrinsic::uadd_sat:
2186 case Intrinsic::sadd_sat:
2191 if (IntrinsicID == Intrinsic::uadd_sat)
2195 case Intrinsic::usub_sat:
2196 case Intrinsic::ssub_sat:
2201 if (IntrinsicID == Intrinsic::usub_sat)
2205 case Intrinsic::cttz:
2206 case Intrinsic::ctlz:
2207 assert(C1 &&
"Must be constant int");
2214 if (IntrinsicID == Intrinsic::cttz)
2224 if ((isa<ConstantVector>(Operands[0]) ||
2225 isa<ConstantDataVector>(Operands[0])) &&
2228 isa<ConstantInt>(Operands[1]) &&
2229 cast<ConstantInt>(Operands[1])->getValue() == 4) {
2230 auto *Op = cast<Constant>(Operands[0]);
2231 switch (IntrinsicID) {
2233 case Intrinsic::x86_avx512_vcvtss2si32:
2234 case Intrinsic::x86_avx512_vcvtss2si64:
2235 case Intrinsic::x86_avx512_vcvtsd2si32:
2236 case Intrinsic::x86_avx512_vcvtsd2si64:
2238 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2239 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2243 case Intrinsic::x86_avx512_vcvtss2usi32:
2244 case Intrinsic::x86_avx512_vcvtss2usi64:
2245 case Intrinsic::x86_avx512_vcvtsd2usi32:
2246 case Intrinsic::x86_avx512_vcvtsd2usi64:
2248 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2249 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2253 case Intrinsic::x86_avx512_cvttss2si:
2254 case Intrinsic::x86_avx512_cvttss2si64:
2255 case Intrinsic::x86_avx512_cvttsd2si:
2256 case Intrinsic::x86_avx512_cvttsd2si64:
2258 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2259 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2263 case Intrinsic::x86_avx512_cvttss2usi:
2264 case Intrinsic::x86_avx512_cvttss2usi64:
2265 case Intrinsic::x86_avx512_cvttsd2usi:
2266 case Intrinsic::x86_avx512_cvttsd2usi64:
2268 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2269 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2284 assert(Operands.
size() == 3 &&
"Wrong number of operands.");
2286 if (
const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
2287 if (
const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
2288 if (
const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
2289 switch (IntrinsicID) {
2291 case Intrinsic::fma:
2292 case Intrinsic::fmuladd: {
2293 APFloat V = Op1->getValueAPF();
2303 if (
const auto *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
2304 if (
const auto *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
2305 if (
const auto *Op3 = dyn_cast<ConstantInt>(Operands[2])) {
2306 switch (IntrinsicID) {
2308 case Intrinsic::smul_fix:
2309 case Intrinsic::smul_fix_sat: {
2316 APInt Lhs = Op1->getValue();
2317 APInt Rhs = Op2->getValue();
2320 assert(Scale < Width &&
"Illegal scale.");
2321 unsigned ExtendedWidth = Width * 2;
2324 if (IntrinsicID == Intrinsic::smul_fix_sat) {
2340 if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) {
2341 const APInt *C0, *C1, *C2;
2342 if (!getConstIntOrUndef(Operands[0], C0) ||
2343 !getConstIntOrUndef(Operands[1], C1) ||
2344 !getConstIntOrUndef(Operands[2], C2))
2347 bool IsRight = IntrinsicID == Intrinsic::fshr;
2349 return Operands[IsRight ? 1 : 0];
2356 unsigned ShAmt = C2->
urem(BitWidth);
2358 return Operands[IsRight ? 1 : 0];
2361 unsigned LshrAmt = IsRight ? ShAmt : BitWidth - ShAmt;
2362 unsigned ShlAmt = !IsRight ? ShAmt : BitWidth - ShAmt;
2379 if (Operands.
size() == 1)
2380 return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call);
2382 if (Operands.
size() == 2)
2383 return ConstantFoldScalarCall2(Name, IntrinsicID, Ty, Operands, TLI, Call);
2385 if (Operands.
size() == 3)
2386 return ConstantFoldScalarCall3(Name, IntrinsicID, Ty, Operands, TLI, Call);
2402 if (IntrinsicID == Intrinsic::masked_load) {
2403 auto *SrcPtr = Operands[0];
2404 auto *
Mask = Operands[2];
2405 auto *Passthru = Operands[3];
2411 auto *MaskElt =
Mask->getAggregateElement(
I);
2414 auto *PassthruElt = Passthru->getAggregateElement(
I);
2416 if (isa<UndefValue>(MaskElt)) {
2424 if (MaskElt->isNullValue()) {
2428 }
else if (MaskElt->isOneValue()) {
2443 for (
unsigned J = 0, JE = Operands.
size(); J != JE; ++J) {
2446 Lane[J] = Operands[J];
2450 Constant *Agg = Operands[J]->getAggregateElement(
I);
2459 ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI, Call);
2481 if (
auto *VTy = dyn_cast<VectorType>(Ty))
2513 case LibFunc_log10l:
2515 case LibFunc_log10f:
2522 if (OpC->getType()->isDoubleTy())
2525 if (OpC->getType()->isFloatTy())
2534 if (OpC->getType()->isDoubleTy())
2537 if (OpC->getType()->isFloatTy())
2552 case LibFunc_tanf: {
2555 Type *Ty = OpC->getType();
2557 double OpV = getValueAsDouble(OpC);
2558 return ConstantFoldFP(tan, OpV, Ty) !=
nullptr;
2581 if (OpC->getType()->isDoubleTy())
2584 if (OpC->getType()->isFloatTy())
2607 const APFloat &Op1 = Op1C->getValueAPF();
2612 case LibFunc_powf: {
2617 if (Ty == Op1C->getType()) {
2618 double Op0V = getValueAsDouble(Op0C);
2619 double Op1V = getValueAsDouble(Op1C);
2620 return ConstantFoldBinaryFP(pow, Op0V, Op1V, Ty) !=
nullptr;
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
opStatus roundToIntegral(roundingMode RM)
Type * getVectorElementType() const
unsigned Log2(Align A)
Returns the log2 of the alignment.
static Constant * FoldBitCast(Constant *V, Type *DestTy)
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
A parsed version of the target data layout string in and methods for querying it. ...
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
static IntegerType * getInt1Ty(LLVMContext &C)
uint64_t getZExtValue() const
Get zero extended value.
const T & back() const
back - Get the last element.
MutableArrayRef< T > makeMutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
This class represents lattice values for constants.
Constant * ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE)
ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a getelementptr constantexpr, return the constant value being addressed by the constant expression, or null if something is funny and we can't decide.
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Function * getCaller()
Helper to get the caller (the parent function).
Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant *> Operands, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
void push_back(const T &Elt)
APInt uadd_sat(const APInt &RHS) const
Optional< unsigned > getInRangeIndex() const
Returns the offset of the index with an inrange attachment, or None if none.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
static uint64_t round(uint64_t Acc, uint64_t Input)
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
float convertToFloat() const
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL)
If this constant is a constant offset from a global, return the global and the constant.
LLVMContext & getContext() const
All values hold a context through their type.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Type * getStructElementType(unsigned N) const
const fltSemantics & getSemantics() const
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
An instruction for reading from memory.
static IntegerType * getInt64Ty(LLVMContext &C)
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
bool isVectorTy() const
True if this is an instance of VectorType.
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 maximum semantics.
static IntegerType * getInt16Ty(LLVMContext &C)
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it...
unsigned getBitWidth() const
Return the number of bits in the APInt.
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
amode Optimize addressing mode
Value * getArgOperand(unsigned i) const
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
bool isVolatile() const
Return true if this is a load from a volatile memory location.
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy)
This method can be used to determine if a cast from S to DstTy using Opcode op is valid or not...
Type * getIndexType(Type *PtrTy) const
Returns the type of a GEP index.
Type * getPointerElementType() const
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
APInt shl(unsigned shiftAmt) const
Left-shift function.
roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
APInt zextOrSelf(unsigned width) const
Zero extend or truncate to width.
A Use represents the edge between a Value definition and its users.
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 minimum semantics.
Windows NT (Windows on ARM)
iterator_range< const unsigned char * > bytes() const
mir Rename Register Operands
uint64_t getNumElements() const
For scalable vectors, this will return the minimum number of elements in the vector.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
This file implements a class to represent arbitrary precision integral constant values and operations...
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
A constant value that is initialized with an expression using other constant values.
int64_t getSExtValue() const
Get sign extended value.
Type * getType() const
All values are typed, get the type of this value.
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
bool has(LibFunc F) const
Tests whether a library function is available.
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
bool isLittleEndian() const
Layout endianness...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
static const fltSemantics & IEEEdouble() LLVM_READNONE
Value * getOperand(unsigned i) const
Class to represent pointers.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
iterator find(const_arg_type_t< KeyT > Val)
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
static Constant * getInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
bool isNegative() const
Determine sign of this APInt.
Type * getReturnType() const
Returns the type of the ret val.
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
The instances of the Type class are immutable: once they are created, they are never changed...
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
const APInt & getConstant() const
Returns the value when all bits have a known value.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
static Constant * getAnd(Constant *C1, Constant *C2)
ConstantFP - Floating Point Values [float, double].
bool isOneValue() const
Determine if this is a value of 1.
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
double convertToDouble() const
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, Type *OnlyIfReducedTy=nullptr)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
bool isConstant() const
Returns true if we know the value of all bits.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
static Constant * get(StructType *T, ArrayRef< Constant *> V)
bool isX86_MMXTy() const
Return true if this is X86 MMX.
Class to represent integer types.
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL)
ConstantFoldLoadFromConstPtr - Return the value that a load from C would produce if it is constant an...
static double log2(double V)
static Constant * getAllOnesValue(Type *Ty)
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Constant * ConstantFoldLoadThroughGEPIndices(Constant *C, ArrayRef< Constant *> Indices)
ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr indices (with an implied zero ...
static wasm::ValType getType(const TargetRegisterClass *RC)
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCompareInstOperands - Attempt to constant fold a compare instruction (icmp/fcmp) with the...
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
APInt ssub_sat(const APInt &RHS) const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void copySign(const APFloat &RHS)
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function...
const APFloat & getValueAPF() const
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Type * getSequentialElementType() const
unsigned getNumOperands() const
This is the shared class of boolean and integer constants.
static const fltSemantics & IEEEhalf() LLVM_READNONE
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Provides information about what library functions are available for the current target.
bool isAggregateType() const
Return true if the type is an aggregate type.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
bool hasVectorInstrinsicScalarOpd(Intrinsic::ID ID, unsigned ScalarOpdIdx)
Identifies if the vector form of the intrinsic has a scalar operand.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
static ConstantInt * getTrue(LLVMContext &Context)
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
unsigned getVectorNumElements() const
Class to represent vector types.
Class for arbitrary precision integers.
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Type * getResultElementType() const
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
opStatus mod(const APFloat &RHS)
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
bool isNonIntegralPointerType(PointerType *PT) const
opStatus
IEEE-754R 7: Default exception handling.
unsigned getNumArgOperands() const
uint64_t getElementOffset(unsigned Idx) const
static Type * getIndexedType(Type *Ty, ArrayRef< Value *> IdxList)
Returns the type of the element that would be loaded with a load instruction with the specified param...
static IntegerType * getInt32Ty(LLVMContext &C)
unsigned getIntegerBitWidth() const
LLVM_NODISCARD bool empty() const
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
StringRef getName() const
Return a constant reference to the value's name.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
APInt smul_ov(const APInt &RHS, bool &Overflow) const
static Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Returns the opcode necessary to cast Val into Ty using usual casting rules.
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getOr(Constant *C1, Constant *C2)
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
iterator_range< value_op_iterator > operand_values()
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant *> Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands...
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
bool isStrictFP() const
Determine if the call requires strict floating point semantics.
APInt umul_ov(const APInt &RHS, bool &Overflow) const
bool getConstantStringInfo(const Value *V, StringRef &Str, uint64_t Offset=0, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
Constant * ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL)
ConstantFoldLoadThroughBitcast - try to cast constant to destination type returning null if unsuccess...
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Type * getElementType() const
APInt usub_sat(const APInt &RHS) const
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
StringRef - Represent a constant reference to a string, i.e.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Type * getSourceElementType() const
APInt bitcastToAPInt() const
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
APInt sadd_sat(const APInt &RHS) const
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
static Constant * get(ArrayRef< Constant *> V)
APInt sextOrSelf(unsigned width) const
Sign extend or truncate to width.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value *> Indices) const
Returns the offset from the beginning of the type for the specified indices.
PointerType * getType() const
Global values are always pointers.
bool isNullValue() const
Determine if all bits are clear.
bool isStructTy() const
True if this is an instance of StructType.
cmpResult compare(const APFloat &RHS) const
const BasicBlock * getParent() const
APInt sdiv_ov(const APInt &RHS, bool &Overflow) const
const fltSemantics & getFltSemantics() const
APInt usub_ov(const APInt &RHS, bool &Overflow) const
static Constant * get(unsigned Opcode, Constant *C1, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a unary operator constant expression, folding if possible.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.