31#include "llvm/Config/config.h"
45#include "llvm/IR/IntrinsicsAArch64.h"
46#include "llvm/IR/IntrinsicsAMDGPU.h"
47#include "llvm/IR/IntrinsicsARM.h"
48#include "llvm/IR/IntrinsicsNVPTX.h"
49#include "llvm/IR/IntrinsicsWebAssembly.h"
50#include "llvm/IR/IntrinsicsX86.h"
79 unsigned BitShift =
DL.getTypeSizeInBits(SrcEltTy);
80 for (
unsigned i = 0; i != NumSrcElts; ++i) {
82 if (
DL.isLittleEndian())
83 Element =
C->getAggregateElement(NumSrcElts - i - 1);
85 Element =
C->getAggregateElement(i);
87 if (isa_and_nonnull<UndefValue>(Element)) {
92 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
97 Result |= ElementCI->getValue().zext(
Result.getBitWidth());
108 "Invalid constantexpr bitcast!");
114 if (
auto *VTy = dyn_cast<VectorType>(
C->getType())) {
117 unsigned NumSrcElts = cast<FixedVectorType>(VTy)->getNumElements();
118 Type *SrcEltTy = VTy->getElementType();
131 if (
Constant *CE = foldConstVectorToAPInt(Result, DestTy,
C,
132 SrcEltTy, NumSrcElts,
DL))
135 if (isa<IntegerType>(DestTy))
136 return ConstantInt::get(DestTy, Result);
144 auto *DestVTy = dyn_cast<VectorType>(DestTy);
150 if (!isa<VectorType>(
C->getType()) &&
151 (isa<ConstantFP>(
C) || isa<ConstantInt>(
C))) {
158 if (!isa<FixedVectorType>(
C->getType()))
162 if (!isa<ConstantDataVector>(
C) && !isa<ConstantVector>(
C) &&
163 !isa<ConstantInt>(
C) && !isa<ConstantFP>(
C))
167 unsigned NumDstElt = cast<FixedVectorType>(DestVTy)->getNumElements();
168 unsigned NumSrcElt = cast<FixedVectorType>(
C->getType())->getNumElements();
169 if (NumDstElt == NumSrcElt)
172 Type *SrcEltTy = cast<VectorType>(
C->getType())->getElementType();
173 Type *DstEltTy = DestVTy->getElementType();
205 assert((isa<ConstantVector>(
C) ||
206 isa<ConstantDataVector>(
C) || isa<ConstantInt>(
C)) &&
207 "Constant folding cannot fail for plain fp->int bitcast!");
214 bool isLittleEndian =
DL.isLittleEndian();
217 if (NumDstElt < NumSrcElt) {
220 unsigned Ratio = NumSrcElt/NumDstElt;
223 for (
unsigned i = 0; i != NumDstElt; ++i) {
226 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
227 for (
unsigned j = 0;
j != Ratio; ++
j) {
228 Constant *Src =
C->getAggregateElement(SrcElt++);
229 if (isa_and_nonnull<UndefValue>(Src))
231 cast<VectorType>(
C->getType())->getElementType());
233 Src = dyn_cast_or_null<ConstantInt>(Src);
240 assert(Src &&
"Constant folding cannot fail on plain integers");
244 Instruction::Shl, Src, ConstantInt::get(Src->getType(), ShiftAmt),
246 assert(Src &&
"Constant folding cannot fail on plain integers");
248 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
252 assert(Elt &&
"Constant folding cannot fail on plain integers");
260 unsigned Ratio = NumDstElt/NumSrcElt;
261 unsigned DstBitSize =
DL.getTypeSizeInBits(DstEltTy);
264 for (
unsigned i = 0; i != NumSrcElt; ++i) {
265 auto *Element =
C->getAggregateElement(i);
270 if (isa<UndefValue>(Element)) {
276 auto *Src = dyn_cast<ConstantInt>(Element);
280 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
281 for (
unsigned j = 0;
j != Ratio; ++
j) {
284 APInt Elt = Src->getValue().lshr(ShiftAmt);
285 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
288 Result.push_back(ConstantInt::get(DstEltTy, Elt.
trunc(DstBitSize)));
306 if ((GV = dyn_cast<GlobalValue>(
C))) {
312 if (
auto *FoundDSOEquiv = dyn_cast<DSOLocalEquivalent>(
C)) {
314 *DSOEquiv = FoundDSOEquiv;
315 GV = FoundDSOEquiv->getGlobalValue();
322 auto *CE = dyn_cast<ConstantExpr>(
C);
323 if (!CE)
return false;
326 if (CE->getOpcode() == Instruction::PtrToInt ||
327 CE->getOpcode() == Instruction::BitCast)
332 auto *
GEP = dyn_cast<GEPOperator>(CE);
336 unsigned BitWidth =
DL.getIndexTypeSizeInBits(
GEP->getType());
345 if (!
GEP->accumulateConstantOffset(
DL, TmpOffset))
355 Type *SrcTy =
C->getType();
359 TypeSize DestSize =
DL.getTypeSizeInBits(DestTy);
360 TypeSize SrcSize =
DL.getTypeSizeInBits(SrcTy);
361 if (!TypeSize::isKnownGE(SrcSize, DestSize))
372 if (SrcSize == DestSize &&
379 Cast = Instruction::IntToPtr;
381 Cast = Instruction::PtrToInt;
402 ElemC =
C->getAggregateElement(Elem++);
403 }
while (ElemC &&
DL.getTypeSizeInBits(ElemC->
getType()).isZero());
408 if (
auto *VT = dyn_cast<VectorType>(SrcTy))
409 if (!
DL.typeSizeEqualsStoreSize(VT->getElementType()))
412 C =
C->getAggregateElement(0u);
427 assert(ByteOffset <=
DL.getTypeAllocSize(
C->getType()) &&
428 "Out of range access");
432 if (isa<ConstantAggregateZero>(
C) || isa<UndefValue>(
C))
435 if (
auto *CI = dyn_cast<ConstantInt>(
C)) {
436 if ((CI->getBitWidth() & 7) != 0)
438 const APInt &Val = CI->getValue();
439 unsigned IntBytes =
unsigned(CI->getBitWidth()/8);
441 for (
unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
442 unsigned n = ByteOffset;
443 if (!
DL.isLittleEndian())
444 n = IntBytes - n - 1;
451 if (
auto *CFP = dyn_cast<ConstantFP>(
C)) {
452 if (CFP->getType()->isDoubleTy()) {
454 return ReadDataFromGlobal(
C, ByteOffset, CurPtr, BytesLeft,
DL);
456 if (CFP->getType()->isFloatTy()){
458 return ReadDataFromGlobal(
C, ByteOffset, CurPtr, BytesLeft,
DL);
460 if (CFP->getType()->isHalfTy()){
462 return ReadDataFromGlobal(
C, ByteOffset, CurPtr, BytesLeft,
DL);
467 if (
auto *CS = dyn_cast<ConstantStruct>(
C)) {
471 ByteOffset -= CurEltOffset;
476 uint64_t EltSize =
DL.getTypeAllocSize(CS->getOperand(Index)->getType());
478 if (ByteOffset < EltSize &&
479 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
486 if (Index == CS->getType()->getNumElements())
492 if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
496 CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
497 BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
499 CurEltOffset = NextEltOffset;
504 if (isa<ConstantArray>(
C) || isa<ConstantVector>(
C) ||
505 isa<ConstantDataSequential>(
C)) {
508 if (
auto *AT = dyn_cast<ArrayType>(
C->getType())) {
509 NumElts = AT->getNumElements();
510 EltTy = AT->getElementType();
511 EltSize =
DL.getTypeAllocSize(EltTy);
513 NumElts = cast<FixedVectorType>(
C->getType())->getNumElements();
514 EltTy = cast<FixedVectorType>(
C->getType())->getElementType();
517 if (!
DL.typeSizeEqualsStoreSize(EltTy))
520 EltSize =
DL.getTypeStoreSize(EltTy);
526 if (!ReadDataFromGlobal(
C->getAggregateElement(Index),
Offset, CurPtr,
531 assert(BytesWritten <= EltSize &&
"Not indexing into this element?");
532 if (BytesWritten >= BytesLeft)
536 BytesLeft -= BytesWritten;
537 CurPtr += BytesWritten;
542 if (
auto *CE = dyn_cast<ConstantExpr>(
C)) {
543 if (
CE->getOpcode() == Instruction::IntToPtr &&
544 CE->getOperand(0)->getType() ==
DL.getIntPtrType(
CE->getType())) {
545 return ReadDataFromGlobal(
CE->getOperand(0), ByteOffset, CurPtr,
557 if (isa<ScalableVectorType>(LoadTy))
560 auto *IntType = dyn_cast<IntegerType>(LoadTy);
573 DL.getTypeSizeInBits(LoadTy).getFixedValue());
594 unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
595 if (BytesLoaded > 32 || BytesLoaded == 0)
599 if (
Offset <= -1 *
static_cast<int64_t
>(BytesLoaded))
603 TypeSize InitializerSize =
DL.getTypeAllocSize(
C->getType());
611 unsigned char RawBytes[32] = {0};
612 unsigned char *CurPtr = RawBytes;
613 unsigned BytesLeft = BytesLoaded;
622 if (!ReadDataFromGlobal(
C,
Offset, CurPtr, BytesLeft,
DL))
625 APInt ResultVal =
APInt(IntType->getBitWidth(), 0);
626 if (
DL.isLittleEndian()) {
627 ResultVal = RawBytes[BytesLoaded - 1];
628 for (
unsigned i = 1; i != BytesLoaded; ++i) {
630 ResultVal |= RawBytes[BytesLoaded - 1 - i];
633 ResultVal = RawBytes[0];
634 for (
unsigned i = 1; i != BytesLoaded; ++i) {
636 ResultVal |= RawBytes[i];
640 return ConstantInt::get(IntType->getContext(), ResultVal);
660 if (NBytes > UINT16_MAX)
668 unsigned char *CurPtr = RawBytes.
data();
670 if (!ReadDataFromGlobal(
Init,
Offset, CurPtr, NBytes,
DL))
683 if (!isa<ConstantAggregate>(
Base) && !isa<ConstantDataSequential>(
Base))
688 if (!
Offset.isZero() || !Indices[0].isZero())
693 if (Index.isNegative() || Index.getActiveBits() >= 32)
696 C =
C->getAggregateElement(Index.getZExtValue());
722 if (
Offset.getSignificantBits() <= 64)
724 FoldReinterpretLoadFromConst(
C, Ty,
Offset.getSExtValue(),
DL))
741 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
744 C = cast<Constant>(
C->stripAndAccumulateConstantOffsets(
765 if (isa<PoisonValue>(
C))
767 if (isa<UndefValue>(
C))
771 if (!
DL.typeSizeEqualsStoreSize(
C->getType()))
775 if (
C->isAllOnesValue() &&
795 if (Opc == Instruction::And) {
798 if ((Known1.
One | Known0.
Zero).isAllOnes()) {
802 if ((Known0.
One | Known1.
Zero).isAllOnes()) {
814 if (Opc == Instruction::Sub) {
820 unsigned OpSize =
DL.getTypeSizeInBits(Op0->
getType());
837 std::optional<ConstantRange>
InRange,
839 Type *IntIdxTy =
DL.getIndexType(ResultTy);
844 for (
unsigned i = 1, e = Ops.
size(); i != e; ++i) {
847 SrcElemTy, Ops.
slice(1, i - 1)))) &&
848 Ops[i]->getType()->getScalarType() != IntIdxScalarTy) {
851 Ops[i]->getType()->isVectorTy() ? IntIdxTy : IntIdxScalarTy;
875 Type *SrcElemTy =
GEP->getSourceElementType();
877 if (!SrcElemTy->
isSized() || isa<ScalableVectorType>(SrcElemTy))
880 if (
Constant *
C = CastGEPIndices(SrcElemTy, Ops, ResTy,
GEP->getNoWrapFlags(),
881 GEP->getInRange(),
DL, TLI))
885 if (!
Ptr->getType()->isPointerTy())
888 Type *IntIdxTy =
DL.getIndexType(
Ptr->getType());
890 for (
unsigned i = 1, e = Ops.
size(); i != e; ++i)
891 if (!isa<ConstantInt>(Ops[i]) || !Ops[i]->
getType()->isIntegerTy())
894 unsigned BitWidth =
DL.getTypeSizeInBits(IntIdxTy);
897 DL.getIndexedOffsetInType(
901 std::optional<ConstantRange>
InRange =
GEP->getInRange();
907 bool Overflow =
false;
908 while (
auto *
GEP = dyn_cast<GEPOperator>(
Ptr)) {
909 NW &=
GEP->getNoWrapFlags();
914 bool AllConstantInt =
true;
915 for (
Value *NestedOp : NestedOps)
916 if (!isa<ConstantInt>(NestedOp)) {
917 AllConstantInt =
false;
931 Ptr = cast<Constant>(
GEP->getOperand(0));
932 SrcElemTy =
GEP->getSourceElementType();
947 if (
auto *CE = dyn_cast<ConstantExpr>(
Ptr)) {
948 if (
CE->getOpcode() == Instruction::IntToPtr) {
949 if (
auto *
Base = dyn_cast<ConstantInt>(
CE->getOperand(0)))
954 auto *PTy = cast<PointerType>(
Ptr->getType());
955 if ((
Ptr->isNullValue() || BasePtr != 0) &&
956 !
DL.isNonIntegralPointerType(PTy)) {
963 bool CanBeNull, CanBeFreed;
965 Ptr->getPointerDereferenceableBytes(
DL, CanBeNull, CanBeFreed);
966 if (DerefBytes != 0 && !CanBeNull &&
Offset.sle(DerefBytes))
977 ConstantInt::get(Ctx,
Offset), NW,
986Constant *ConstantFoldInstOperandsImpl(
const Value *InstOrCE,
unsigned Opcode,
990 bool AllowNonDeterministic) {
1000 case Instruction::FAdd:
1001 case Instruction::FSub:
1002 case Instruction::FMul:
1003 case Instruction::FDiv:
1004 case Instruction::FRem:
1008 if (
const auto *
I = dyn_cast<Instruction>(InstOrCE)) {
1010 AllowNonDeterministic);
1019 if (
auto *
GEP = dyn_cast<GEPOperator>(InstOrCE)) {
1020 Type *SrcElemTy =
GEP->getSourceElementType();
1028 GEP->getNoWrapFlags(),
1032 if (
auto *CE = dyn_cast<ConstantExpr>(InstOrCE))
1033 return CE->getWithOperands(Ops);
1036 default:
return nullptr;
1037 case Instruction::ICmp:
1038 case Instruction::FCmp: {
1039 auto *
C = cast<CmpInst>(InstOrCE);
1043 case Instruction::Freeze:
1045 case Instruction::Call:
1046 if (
auto *
F = dyn_cast<Function>(Ops.
back())) {
1047 const auto *
Call = cast<CallBase>(InstOrCE);
1050 AllowNonDeterministic);
1053 case Instruction::Select:
1055 case Instruction::ExtractElement:
1057 case Instruction::ExtractValue:
1059 Ops[0], cast<ExtractValueInst>(InstOrCE)->getIndices());
1060 case Instruction::InsertElement:
1062 case Instruction::InsertValue:
1064 Ops[0], Ops[1], cast<InsertValueInst>(InstOrCE)->getIndices());
1065 case Instruction::ShuffleVector:
1067 Ops[0], Ops[1], cast<ShuffleVectorInst>(InstOrCE)->getShuffleMask());
1068 case Instruction::Load: {
1069 const auto *LI = dyn_cast<LoadInst>(InstOrCE);
1070 if (LI->isVolatile())
1089 if (!isa<ConstantVector>(
C) && !isa<ConstantExpr>(
C))
1093 for (
const Use &OldU :
C->operands()) {
1094 Constant *OldC = cast<Constant>(&OldU);
1098 if (isa<ConstantVector>(OldC) || isa<ConstantExpr>(OldC)) {
1099 auto It = FoldedOps.
find(OldC);
1100 if (It == FoldedOps.
end()) {
1101 NewC = ConstantFoldConstantImpl(OldC,
DL, TLI, FoldedOps);
1102 FoldedOps.
insert({OldC, NewC});
1110 if (
auto *CE = dyn_cast<ConstantExpr>(
C)) {
1111 if (
Constant *Res = ConstantFoldInstOperandsImpl(
1112 CE,
CE->getOpcode(), Ops,
DL, TLI,
true))
1117 assert(isa<ConstantVector>(
C));
1126 if (
auto *PN = dyn_cast<PHINode>(
I)) {
1142 C = ConstantFoldConstantImpl(
C,
DL, TLI, FoldedOps);
1145 if (CommonValue &&
C != CommonValue)
1156 if (!
all_of(
I->operands(), [](
Use &U) { return isa<Constant>(U); }))
1161 for (
const Use &OpU :
I->operands()) {
1162 auto *
Op = cast<Constant>(&OpU);
1164 Op = ConstantFoldConstantImpl(
Op,
DL, TLI, FoldedOps);
1174 return ConstantFoldConstantImpl(
C,
DL, TLI, FoldedOps);
1181 bool AllowNonDeterministic) {
1182 return ConstantFoldInstOperandsImpl(
I,
I->getOpcode(), Ops,
DL, TLI,
1183 AllowNonDeterministic);
1200 if (
auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1202 if (CE0->getOpcode() == Instruction::IntToPtr) {
1203 Type *IntPtrTy =
DL.getIntPtrType(CE0->getType());
1215 if (CE0->getOpcode() == Instruction::PtrToInt) {
1216 Type *IntPtrTy =
DL.getIntPtrType(CE0->getOperand(0)->getType());
1217 if (CE0->getType() == IntPtrTy) {
1225 if (
auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1226 if (CE0->getOpcode() == CE1->getOpcode()) {
1227 if (CE0->getOpcode() == Instruction::IntToPtr) {
1228 Type *IntPtrTy =
DL.getIntPtrType(CE0->getType());
1242 if (CE0->getOpcode() == Instruction::PtrToInt) {
1243 Type *IntPtrTy =
DL.getIntPtrType(CE0->getOperand(0)->getType());
1244 if (CE0->getType() == IntPtrTy &&
1245 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1247 Predicate, CE0->getOperand(0), CE1->getOperand(0),
DL, TLI);
1259 unsigned IndexWidth =
DL.getIndexTypeSizeInBits(Ops0->
getType());
1260 APInt Offset0(IndexWidth, 0);
1263 DL, Offset0, IsEqPred,
1266 APInt Offset1(IndexWidth, 0);
1268 DL, Offset1, IsEqPred,
1271 if (Stripped0 == Stripped1)
1277 }
else if (isa<ConstantExpr>(Ops1)) {
1280 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1309 if (isa<ConstantExpr>(
LHS) || isa<ConstantExpr>(
RHS))
1324 return ConstantFP::get(Ty->
getContext(), APF);
1326 return ConstantFP::get(
1356 IsOutput ? Mode.Output : Mode.Input);
1361 if (
ConstantFP *CFP = dyn_cast<ConstantFP>(Operand))
1364 if (isa<ConstantAggregateZero, UndefValue, ConstantExpr>(Operand))
1368 VectorType *VecTy = dyn_cast<VectorType>(Ty);
1380 if (
const auto *CV = dyn_cast<ConstantVector>(Operand)) {
1382 for (
unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1384 if (isa<UndefValue>(Element)) {
1389 ConstantFP *CFP = dyn_cast<ConstantFP>(Element);
1402 if (
const auto *CDV = dyn_cast<ConstantDataVector>(Operand)) {
1404 for (
unsigned I = 0, E = CDV->getNumElements();
I < E; ++
I) {
1405 const APFloat &Elt = CDV->getElementAsAPFloat(
I);
1407 NewElts.
push_back(ConstantFP::get(Ty, Elt));
1427 bool AllowNonDeterministic) {
1440 if (!AllowNonDeterministic)
1441 if (
auto *
FP = dyn_cast_or_null<FPMathOperator>(
I))
1442 if (
FP->hasNoSignedZeros() ||
FP->hasAllowReassoc() ||
1443 FP->hasAllowContract() ||
FP->hasAllowReciprocal())
1457 if (!AllowNonDeterministic &&
C->isNaN())
1473 case Instruction::PtrToInt:
1474 if (
auto *CE = dyn_cast<ConstantExpr>(
C)) {
1478 if (CE->getOpcode() == Instruction::IntToPtr) {
1481 DL.getIntPtrType(CE->getType()),
1483 }
else if (
auto *
GEP = dyn_cast<GEPOperator>(CE)) {
1487 unsigned BitWidth =
DL.getIndexTypeSizeInBits(
GEP->getType());
1489 auto *
Base = cast<Constant>(
GEP->stripAndAccumulateConstantOffsets(
1490 DL, BaseOffset,
true));
1491 if (
Base->isNullValue()) {
1492 FoldedValue = ConstantInt::get(CE->getContext(), BaseOffset);
1495 if (
GEP->getNumIndices() == 1 &&
1496 GEP->getSourceElementType()->isIntegerTy(8)) {
1497 auto *
Ptr = cast<Constant>(
GEP->getPointerOperand());
1498 auto *Sub = dyn_cast<ConstantExpr>(
GEP->getOperand(1));
1499 Type *IntIdxTy =
DL.getIndexType(
Ptr->getType());
1500 if (Sub && Sub->getType() == IntIdxTy &&
1501 Sub->getOpcode() == Instruction::Sub &&
1502 Sub->getOperand(0)->isNullValue())
1515 case Instruction::IntToPtr:
1520 if (
auto *CE = dyn_cast<ConstantExpr>(
C)) {
1521 if (CE->getOpcode() == Instruction::PtrToInt) {
1522 Constant *SrcPtr = CE->getOperand(0);
1523 unsigned SrcPtrSize =
DL.getPointerTypeSizeInBits(SrcPtr->
getType());
1524 unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1526 if (MidIntSize >= SrcPtrSize) {
1534 case Instruction::Trunc:
1535 case Instruction::ZExt:
1536 case Instruction::SExt:
1537 case Instruction::FPTrunc:
1538 case Instruction::FPExt:
1539 case Instruction::UIToFP:
1540 case Instruction::SIToFP:
1541 case Instruction::FPToUI:
1542 case Instruction::FPToSI:
1543 case Instruction::AddrSpaceCast:
1545 case Instruction::BitCast:
1556 Type *SrcTy =
C->getType();
1557 if (SrcTy == DestTy)
1571 if (Call->isNoBuiltin())
1573 if (Call->getFunctionType() !=
F->getFunctionType())
1575 switch (
F->getIntrinsicID()) {
1578 case Intrinsic::bswap:
1579 case Intrinsic::ctpop:
1580 case Intrinsic::ctlz:
1581 case Intrinsic::cttz:
1582 case Intrinsic::fshl:
1583 case Intrinsic::fshr:
1584 case Intrinsic::launder_invariant_group:
1585 case Intrinsic::strip_invariant_group:
1586 case Intrinsic::masked_load:
1587 case Intrinsic::get_active_lane_mask:
1588 case Intrinsic::abs:
1589 case Intrinsic::smax:
1590 case Intrinsic::smin:
1591 case Intrinsic::umax:
1592 case Intrinsic::umin:
1593 case Intrinsic::scmp:
1594 case Intrinsic::ucmp:
1595 case Intrinsic::sadd_with_overflow:
1596 case Intrinsic::uadd_with_overflow:
1597 case Intrinsic::ssub_with_overflow:
1598 case Intrinsic::usub_with_overflow:
1599 case Intrinsic::smul_with_overflow:
1600 case Intrinsic::umul_with_overflow:
1601 case Intrinsic::sadd_sat:
1602 case Intrinsic::uadd_sat:
1603 case Intrinsic::ssub_sat:
1604 case Intrinsic::usub_sat:
1605 case Intrinsic::smul_fix:
1606 case Intrinsic::smul_fix_sat:
1607 case Intrinsic::bitreverse:
1608 case Intrinsic::is_constant:
1609 case Intrinsic::vector_reduce_add:
1610 case Intrinsic::vector_reduce_mul:
1611 case Intrinsic::vector_reduce_and:
1612 case Intrinsic::vector_reduce_or:
1613 case Intrinsic::vector_reduce_xor:
1614 case Intrinsic::vector_reduce_smin:
1615 case Intrinsic::vector_reduce_smax:
1616 case Intrinsic::vector_reduce_umin:
1617 case Intrinsic::vector_reduce_umax:
1619 case Intrinsic::amdgcn_perm:
1620 case Intrinsic::amdgcn_wave_reduce_umin:
1621 case Intrinsic::amdgcn_wave_reduce_umax:
1622 case Intrinsic::amdgcn_s_wqm:
1623 case Intrinsic::amdgcn_s_quadmask:
1624 case Intrinsic::amdgcn_s_bitreplicate:
1625 case Intrinsic::arm_mve_vctp8:
1626 case Intrinsic::arm_mve_vctp16:
1627 case Intrinsic::arm_mve_vctp32:
1628 case Intrinsic::arm_mve_vctp64:
1629 case Intrinsic::aarch64_sve_convert_from_svbool:
1631 case Intrinsic::wasm_trunc_signed:
1632 case Intrinsic::wasm_trunc_unsigned:
1637 case Intrinsic::minnum:
1638 case Intrinsic::maxnum:
1639 case Intrinsic::minimum:
1640 case Intrinsic::maximum:
1641 case Intrinsic::log:
1642 case Intrinsic::log2:
1643 case Intrinsic::log10:
1644 case Intrinsic::exp:
1645 case Intrinsic::exp2:
1646 case Intrinsic::exp10:
1647 case Intrinsic::sqrt:
1648 case Intrinsic::sin:
1649 case Intrinsic::cos:
1650 case Intrinsic::sincos:
1651 case Intrinsic::pow:
1652 case Intrinsic::powi:
1653 case Intrinsic::ldexp:
1654 case Intrinsic::fma:
1655 case Intrinsic::fmuladd:
1656 case Intrinsic::frexp:
1657 case Intrinsic::fptoui_sat:
1658 case Intrinsic::fptosi_sat:
1659 case Intrinsic::convert_from_fp16:
1660 case Intrinsic::convert_to_fp16:
1661 case Intrinsic::amdgcn_cos:
1662 case Intrinsic::amdgcn_cubeid:
1663 case Intrinsic::amdgcn_cubema:
1664 case Intrinsic::amdgcn_cubesc:
1665 case Intrinsic::amdgcn_cubetc:
1666 case Intrinsic::amdgcn_fmul_legacy:
1667 case Intrinsic::amdgcn_fma_legacy:
1668 case Intrinsic::amdgcn_fract:
1669 case Intrinsic::amdgcn_sin:
1671 case Intrinsic::x86_sse_cvtss2si:
1672 case Intrinsic::x86_sse_cvtss2si64:
1673 case Intrinsic::x86_sse_cvttss2si:
1674 case Intrinsic::x86_sse_cvttss2si64:
1675 case Intrinsic::x86_sse2_cvtsd2si:
1676 case Intrinsic::x86_sse2_cvtsd2si64:
1677 case Intrinsic::x86_sse2_cvttsd2si:
1678 case Intrinsic::x86_sse2_cvttsd2si64:
1679 case Intrinsic::x86_avx512_vcvtss2si32:
1680 case Intrinsic::x86_avx512_vcvtss2si64:
1681 case Intrinsic::x86_avx512_cvttss2si:
1682 case Intrinsic::x86_avx512_cvttss2si64:
1683 case Intrinsic::x86_avx512_vcvtsd2si32:
1684 case Intrinsic::x86_avx512_vcvtsd2si64:
1685 case Intrinsic::x86_avx512_cvttsd2si:
1686 case Intrinsic::x86_avx512_cvttsd2si64:
1687 case Intrinsic::x86_avx512_vcvtss2usi32:
1688 case Intrinsic::x86_avx512_vcvtss2usi64:
1689 case Intrinsic::x86_avx512_cvttss2usi:
1690 case Intrinsic::x86_avx512_cvttss2usi64:
1691 case Intrinsic::x86_avx512_vcvtsd2usi32:
1692 case Intrinsic::x86_avx512_vcvtsd2usi64:
1693 case Intrinsic::x86_avx512_cvttsd2usi:
1694 case Intrinsic::x86_avx512_cvttsd2usi64:
1695 return !Call->isStrictFP();
1698 case Intrinsic::nvvm_fmax_d:
1699 case Intrinsic::nvvm_fmax_f:
1700 case Intrinsic::nvvm_fmax_ftz_f:
1701 case Intrinsic::nvvm_fmax_ftz_nan_f:
1702 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
1703 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
1704 case Intrinsic::nvvm_fmax_nan_f:
1705 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
1706 case Intrinsic::nvvm_fmax_xorsign_abs_f:
1709 case Intrinsic::nvvm_fmin_d:
1710 case Intrinsic::nvvm_fmin_f:
1711 case Intrinsic::nvvm_fmin_ftz_f:
1712 case Intrinsic::nvvm_fmin_ftz_nan_f:
1713 case Intrinsic::nvvm_fmin_ftz_nan_xorsign_abs_f:
1714 case Intrinsic::nvvm_fmin_ftz_xorsign_abs_f:
1715 case Intrinsic::nvvm_fmin_nan_f:
1716 case Intrinsic::nvvm_fmin_nan_xorsign_abs_f:
1717 case Intrinsic::nvvm_fmin_xorsign_abs_f:
1720 case Intrinsic::nvvm_f2i_rm:
1721 case Intrinsic::nvvm_f2i_rn:
1722 case Intrinsic::nvvm_f2i_rp:
1723 case Intrinsic::nvvm_f2i_rz:
1724 case Intrinsic::nvvm_f2i_rm_ftz:
1725 case Intrinsic::nvvm_f2i_rn_ftz:
1726 case Intrinsic::nvvm_f2i_rp_ftz:
1727 case Intrinsic::nvvm_f2i_rz_ftz:
1728 case Intrinsic::nvvm_f2ui_rm:
1729 case Intrinsic::nvvm_f2ui_rn:
1730 case Intrinsic::nvvm_f2ui_rp:
1731 case Intrinsic::nvvm_f2ui_rz:
1732 case Intrinsic::nvvm_f2ui_rm_ftz:
1733 case Intrinsic::nvvm_f2ui_rn_ftz:
1734 case Intrinsic::nvvm_f2ui_rp_ftz:
1735 case Intrinsic::nvvm_f2ui_rz_ftz:
1736 case Intrinsic::nvvm_d2i_rm:
1737 case Intrinsic::nvvm_d2i_rn:
1738 case Intrinsic::nvvm_d2i_rp:
1739 case Intrinsic::nvvm_d2i_rz:
1740 case Intrinsic::nvvm_d2ui_rm:
1741 case Intrinsic::nvvm_d2ui_rn:
1742 case Intrinsic::nvvm_d2ui_rp:
1743 case Intrinsic::nvvm_d2ui_rz:
1746 case Intrinsic::nvvm_f2ll_rm:
1747 case Intrinsic::nvvm_f2ll_rn:
1748 case Intrinsic::nvvm_f2ll_rp:
1749 case Intrinsic::nvvm_f2ll_rz:
1750 case Intrinsic::nvvm_f2ll_rm_ftz:
1751 case Intrinsic::nvvm_f2ll_rn_ftz:
1752 case Intrinsic::nvvm_f2ll_rp_ftz:
1753 case Intrinsic::nvvm_f2ll_rz_ftz:
1754 case Intrinsic::nvvm_f2ull_rm:
1755 case Intrinsic::nvvm_f2ull_rn:
1756 case Intrinsic::nvvm_f2ull_rp:
1757 case Intrinsic::nvvm_f2ull_rz:
1758 case Intrinsic::nvvm_f2ull_rm_ftz:
1759 case Intrinsic::nvvm_f2ull_rn_ftz:
1760 case Intrinsic::nvvm_f2ull_rp_ftz:
1761 case Intrinsic::nvvm_f2ull_rz_ftz:
1762 case Intrinsic::nvvm_d2ll_rm:
1763 case Intrinsic::nvvm_d2ll_rn:
1764 case Intrinsic::nvvm_d2ll_rp:
1765 case Intrinsic::nvvm_d2ll_rz:
1766 case Intrinsic::nvvm_d2ull_rm:
1767 case Intrinsic::nvvm_d2ull_rn:
1768 case Intrinsic::nvvm_d2ull_rp:
1769 case Intrinsic::nvvm_d2ull_rz:
1773 case Intrinsic::fabs:
1774 case Intrinsic::copysign:
1775 case Intrinsic::is_fpclass:
1778 case Intrinsic::ceil:
1779 case Intrinsic::floor:
1780 case Intrinsic::round:
1781 case Intrinsic::roundeven:
1782 case Intrinsic::trunc:
1783 case Intrinsic::nearbyint:
1784 case Intrinsic::rint:
1785 case Intrinsic::canonicalize:
1788 case Intrinsic::experimental_constrained_fma:
1789 case Intrinsic::experimental_constrained_fmuladd:
1790 case Intrinsic::experimental_constrained_fadd:
1791 case Intrinsic::experimental_constrained_fsub:
1792 case Intrinsic::experimental_constrained_fmul:
1793 case Intrinsic::experimental_constrained_fdiv:
1794 case Intrinsic::experimental_constrained_frem:
1795 case Intrinsic::experimental_constrained_ceil:
1796 case Intrinsic::experimental_constrained_floor:
1797 case Intrinsic::experimental_constrained_round:
1798 case Intrinsic::experimental_constrained_roundeven:
1799 case Intrinsic::experimental_constrained_trunc:
1800 case Intrinsic::experimental_constrained_nearbyint:
1801 case Intrinsic::experimental_constrained_rint:
1802 case Intrinsic::experimental_constrained_fcmp:
1803 case Intrinsic::experimental_constrained_fcmps:
1810 if (!
F->hasName() || Call->isStrictFP())
1821 return Name ==
"acos" ||
Name ==
"acosf" ||
1822 Name ==
"asin" ||
Name ==
"asinf" ||
1823 Name ==
"atan" ||
Name ==
"atanf" ||
1824 Name ==
"atan2" ||
Name ==
"atan2f";
1826 return Name ==
"ceil" ||
Name ==
"ceilf" ||
1830 return Name ==
"exp" ||
Name ==
"expf" ||
Name ==
"exp2" ||
1833 return Name ==
"fabs" ||
Name ==
"fabsf" ||
1834 Name ==
"floor" ||
Name ==
"floorf" ||
1837 return Name ==
"ilogb" ||
Name ==
"ilogbf";
1839 return Name ==
"log" ||
Name ==
"logf" ||
Name ==
"logl" ||
1840 Name ==
"log2" ||
Name ==
"log2f" ||
Name ==
"log10" ||
1841 Name ==
"log10f" ||
Name ==
"logb" ||
Name ==
"logbf" ||
1842 Name ==
"log1p" ||
Name ==
"log1pf";
1844 return Name ==
"nearbyint" ||
Name ==
"nearbyintf";
1846 return Name ==
"pow" ||
Name ==
"powf";
1848 return Name ==
"remainder" ||
Name ==
"remainderf" ||
1849 Name ==
"rint" ||
Name ==
"rintf" ||
1850 Name ==
"round" ||
Name ==
"roundf";
1852 return Name ==
"sin" ||
Name ==
"sinf" ||
1853 Name ==
"sinh" ||
Name ==
"sinhf" ||
1856 return Name ==
"tan" ||
Name ==
"tanf" ||
1857 Name ==
"tanh" ||
Name ==
"tanhf" ||
1858 Name ==
"trunc" ||
Name ==
"truncf";
1866 if (
Name.size() < 12 ||
Name[1] !=
'_')
1872 return Name ==
"__acos_finite" ||
Name ==
"__acosf_finite" ||
1873 Name ==
"__asin_finite" ||
Name ==
"__asinf_finite" ||
1874 Name ==
"__atan2_finite" ||
Name ==
"__atan2f_finite";
1876 return Name ==
"__cosh_finite" ||
Name ==
"__coshf_finite";
1878 return Name ==
"__exp_finite" ||
Name ==
"__expf_finite" ||
1879 Name ==
"__exp2_finite" ||
Name ==
"__exp2f_finite";
1881 return Name ==
"__log_finite" ||
Name ==
"__logf_finite" ||
1882 Name ==
"__log10_finite" ||
Name ==
"__log10f_finite";
1884 return Name ==
"__pow_finite" ||
Name ==
"__powf_finite";
1886 return Name ==
"__sinh_finite" ||
Name ==
"__sinhf_finite";
1897 APF.convert(Ty->
getFltSemantics(), APFloat::rmNearestTiesToEven, &unused);
1898 return ConstantFP::get(Ty->
getContext(), APF);
1905#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
1906Constant *GetConstantFoldFPValue128(float128 V,
Type *Ty) {
1908 return ConstantFP::get(Ty, V);
1914inline void llvm_fenv_clearexcept() {
1915#if HAVE_DECL_FE_ALL_EXCEPT
1916 feclearexcept(FE_ALL_EXCEPT);
1922inline bool llvm_fenv_testexcept() {
1923 int errno_val = errno;
1924 if (errno_val == ERANGE || errno_val == EDOM)
1926#if HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT
1927 if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
1941 llvm_fenv_clearexcept();
1942 double Result = NativeFP(
V.convertToDouble());
1943 if (llvm_fenv_testexcept()) {
1944 llvm_fenv_clearexcept();
1948 return GetConstantFoldFPValue(Result, Ty);
1951#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
1952Constant *ConstantFoldFP128(float128 (*NativeFP)(float128),
const APFloat &V,
1954 llvm_fenv_clearexcept();
1955 float128
Result = NativeFP(
V.convertToQuad());
1956 if (llvm_fenv_testexcept()) {
1957 llvm_fenv_clearexcept();
1961 return GetConstantFoldFPValue128(Result, Ty);
1965Constant *ConstantFoldBinaryFP(
double (*NativeFP)(
double,
double),
1967 llvm_fenv_clearexcept();
1968 double Result = NativeFP(
V.convertToDouble(),
W.convertToDouble());
1969 if (llvm_fenv_testexcept()) {
1970 llvm_fenv_clearexcept();
1974 return GetConstantFoldFPValue(Result, Ty);
1984 if (isa<ConstantAggregateZero>(
Op))
1988 if (isa<PoisonValue>(
Op) ||
Op->containsPoisonElement())
1992 if (!isa<ConstantVector>(
Op) && !isa<ConstantDataVector>(
Op))
1995 auto *EltC = dyn_cast<ConstantInt>(
Op->getAggregateElement(0U));
1999 APInt Acc = EltC->getValue();
2001 if (!(EltC = dyn_cast<ConstantInt>(
Op->getAggregateElement(
I))))
2003 const APInt &
X = EltC->getValue();
2005 case Intrinsic::vector_reduce_add:
2008 case Intrinsic::vector_reduce_mul:
2011 case Intrinsic::vector_reduce_and:
2014 case Intrinsic::vector_reduce_or:
2017 case Intrinsic::vector_reduce_xor:
2020 case Intrinsic::vector_reduce_smin:
2023 case Intrinsic::vector_reduce_smax:
2026 case Intrinsic::vector_reduce_umin:
2029 case Intrinsic::vector_reduce_umax:
2035 return ConstantInt::get(
Op->getContext(), Acc);
2045Constant *ConstantFoldSSEConvertToInt(
const APFloat &Val,
bool roundTowardZero,
2046 Type *Ty,
bool IsSigned) {
2049 assert(ResultWidth <= 64 &&
2050 "Can only constant fold conversions to 64 and 32 bit ints");
2053 bool isExact =
false;
2055 : APFloat::rmNearestTiesToEven;
2058 IsSigned,
mode, &isExact);
2059 if (status != APFloat::opOK &&
2060 (!roundTowardZero || status != APFloat::opInexact))
2062 return ConstantInt::get(Ty, UIntVal, IsSigned);
2066 Type *Ty =
Op->getType();
2069 return Op->getValueAPF().convertToDouble();
2073 APF.
convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &unused);
2078 if (
auto *CI = dyn_cast<ConstantInt>(
Op)) {
2079 C = &CI->getValue();
2082 if (isa<UndefValue>(
Op)) {
2101 if (St == APFloat::opStatus::opOK)
2106 if (ORM && *ORM == RoundingMode::Dynamic)
2111 if (EB && *EB != fp::ExceptionBehavior::ebStrict)
2123 if (!ORM || *ORM == RoundingMode::Dynamic)
2128 return RoundingMode::NearestTiesToEven;
2138 return ConstantFP::get(
2150 if (Src.isNormal() || Src.isInfinity())
2151 return ConstantFP::get(CI->
getContext(), Src);
2158 return ConstantFP::get(CI->
getContext(), Src);
2190 if (IntrinsicID == Intrinsic::is_constant) {
2194 if (
Operands[0]->isManifestConstant())
2199 if (isa<PoisonValue>(
Operands[0])) {
2201 if (IntrinsicID == Intrinsic::canonicalize)
2205 if (isa<UndefValue>(
Operands[0])) {
2209 if (IntrinsicID == Intrinsic::cos ||
2210 IntrinsicID == Intrinsic::ctpop ||
2211 IntrinsicID == Intrinsic::fptoui_sat ||
2212 IntrinsicID == Intrinsic::fptosi_sat ||
2213 IntrinsicID == Intrinsic::canonicalize)
2215 if (IntrinsicID == Intrinsic::bswap ||
2216 IntrinsicID == Intrinsic::bitreverse ||
2217 IntrinsicID == Intrinsic::launder_invariant_group ||
2218 IntrinsicID == Intrinsic::strip_invariant_group)
2222 if (isa<ConstantPointerNull>(
Operands[0])) {
2224 if (IntrinsicID == Intrinsic::launder_invariant_group ||
2225 IntrinsicID == Intrinsic::strip_invariant_group) {
2230 Call->getParent() ?
Call->getCaller() :
nullptr;
2240 if (
auto *
Op = dyn_cast<ConstantFP>(
Operands[0])) {
2241 if (IntrinsicID == Intrinsic::convert_to_fp16) {
2245 Val.
convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &lost);
2252 if (IntrinsicID == Intrinsic::wasm_trunc_signed ||
2253 IntrinsicID == Intrinsic::wasm_trunc_unsigned) {
2254 bool Signed = IntrinsicID == Intrinsic::wasm_trunc_signed;
2261 bool IsExact =
false;
2263 U.convertToInteger(
Int, APFloat::rmTowardZero, &IsExact);
2265 if (
Status == APFloat::opOK ||
Status == APFloat::opInexact)
2266 return ConstantInt::get(Ty,
Int);
2271 if (IntrinsicID == Intrinsic::fptoui_sat ||
2272 IntrinsicID == Intrinsic::fptosi_sat) {
2275 IntrinsicID == Intrinsic::fptoui_sat);
2277 U.convertToInteger(
Int, APFloat::rmTowardZero, &IsExact);
2278 return ConstantInt::get(Ty,
Int);
2281 if (IntrinsicID == Intrinsic::canonicalize)
2282 return constantFoldCanonicalize(Ty, Call, U);
2284#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
2286 if (IntrinsicID == Intrinsic::log) {
2287 float128
Result = logf128(
Op->getValueAPF().convertToQuad());
2288 return GetConstantFoldFPValue128(Result, Ty);
2293 Fp128Func == LibFunc_logl)
2294 return ConstantFoldFP128(logf128,
Op->getValueAPF(), Ty);
2304 if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) {
2305 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2309 if (IntrinsicID == Intrinsic::round) {
2310 U.roundToIntegral(APFloat::rmNearestTiesToAway);
2314 if (IntrinsicID == Intrinsic::roundeven) {
2315 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2319 if (IntrinsicID == Intrinsic::ceil) {
2320 U.roundToIntegral(APFloat::rmTowardPositive);
2324 if (IntrinsicID == Intrinsic::floor) {
2325 U.roundToIntegral(APFloat::rmTowardNegative);
2329 if (IntrinsicID == Intrinsic::trunc) {
2330 U.roundToIntegral(APFloat::rmTowardZero);
2334 if (IntrinsicID == Intrinsic::fabs) {
2339 if (IntrinsicID == Intrinsic::amdgcn_fract) {
2345 FloorU.roundToIntegral(APFloat::rmTowardNegative);
2347 APFloat AlmostOne(
U.getSemantics(), 1);
2348 AlmostOne.next(
true);
2355 std::optional<APFloat::roundingMode>
RM;
2356 switch (IntrinsicID) {
2359 case Intrinsic::experimental_constrained_nearbyint:
2360 case Intrinsic::experimental_constrained_rint: {
2361 auto CI = cast<ConstrainedFPIntrinsic>(Call);
2362 RM = CI->getRoundingMode();
2363 if (!RM || *RM == RoundingMode::Dynamic)
2367 case Intrinsic::experimental_constrained_round:
2368 RM = APFloat::rmNearestTiesToAway;
2370 case Intrinsic::experimental_constrained_ceil:
2371 RM = APFloat::rmTowardPositive;
2373 case Intrinsic::experimental_constrained_floor:
2374 RM = APFloat::rmTowardNegative;
2376 case Intrinsic::experimental_constrained_trunc:
2377 RM = APFloat::rmTowardZero;
2381 auto CI = cast<ConstrainedFPIntrinsic>(Call);
2384 if (IntrinsicID == Intrinsic::experimental_constrained_rint &&
2385 St == APFloat::opInexact) {
2386 std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
2390 }
else if (
U.isSignaling()) {
2391 std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
2400 switch (IntrinsicID) {
2402 case Intrinsic::nvvm_f2i_rm:
2403 case Intrinsic::nvvm_f2i_rn:
2404 case Intrinsic::nvvm_f2i_rp:
2405 case Intrinsic::nvvm_f2i_rz:
2406 case Intrinsic::nvvm_f2i_rm_ftz:
2407 case Intrinsic::nvvm_f2i_rn_ftz:
2408 case Intrinsic::nvvm_f2i_rp_ftz:
2409 case Intrinsic::nvvm_f2i_rz_ftz:
2411 case Intrinsic::nvvm_f2ui_rm:
2412 case Intrinsic::nvvm_f2ui_rn:
2413 case Intrinsic::nvvm_f2ui_rp:
2414 case Intrinsic::nvvm_f2ui_rz:
2415 case Intrinsic::nvvm_f2ui_rm_ftz:
2416 case Intrinsic::nvvm_f2ui_rn_ftz:
2417 case Intrinsic::nvvm_f2ui_rp_ftz:
2418 case Intrinsic::nvvm_f2ui_rz_ftz:
2420 case Intrinsic::nvvm_d2i_rm:
2421 case Intrinsic::nvvm_d2i_rn:
2422 case Intrinsic::nvvm_d2i_rp:
2423 case Intrinsic::nvvm_d2i_rz:
2425 case Intrinsic::nvvm_d2ui_rm:
2426 case Intrinsic::nvvm_d2ui_rn:
2427 case Intrinsic::nvvm_d2ui_rp:
2428 case Intrinsic::nvvm_d2ui_rz:
2430 case Intrinsic::nvvm_f2ll_rm:
2431 case Intrinsic::nvvm_f2ll_rn:
2432 case Intrinsic::nvvm_f2ll_rp:
2433 case Intrinsic::nvvm_f2ll_rz:
2434 case Intrinsic::nvvm_f2ll_rm_ftz:
2435 case Intrinsic::nvvm_f2ll_rn_ftz:
2436 case Intrinsic::nvvm_f2ll_rp_ftz:
2437 case Intrinsic::nvvm_f2ll_rz_ftz:
2439 case Intrinsic::nvvm_f2ull_rm:
2440 case Intrinsic::nvvm_f2ull_rn:
2441 case Intrinsic::nvvm_f2ull_rp:
2442 case Intrinsic::nvvm_f2ull_rz:
2443 case Intrinsic::nvvm_f2ull_rm_ftz:
2444 case Intrinsic::nvvm_f2ull_rn_ftz:
2445 case Intrinsic::nvvm_f2ull_rp_ftz:
2446 case Intrinsic::nvvm_f2ull_rz_ftz:
2448 case Intrinsic::nvvm_d2ll_rm:
2449 case Intrinsic::nvvm_d2ll_rn:
2450 case Intrinsic::nvvm_d2ll_rp:
2451 case Intrinsic::nvvm_d2ll_rz:
2453 case Intrinsic::nvvm_d2ull_rm:
2454 case Intrinsic::nvvm_d2ull_rn:
2455 case Intrinsic::nvvm_d2ull_rp:
2456 case Intrinsic::nvvm_d2ull_rz: {
2459 return ConstantInt::get(Ty, 0);
2467 auto FloatToRound = IsFTZ ? FTZPreserveSign(U) :
U;
2469 bool IsExact =
false;
2471 FloatToRound.convertToInteger(ResInt, RMode, &IsExact);
2473 if (
Status != APFloat::opInvalidOp)
2474 return ConstantInt::get(Ty, ResInt);
2491 switch (IntrinsicID) {
2493 case Intrinsic::log:
2494 return ConstantFoldFP(log, APF, Ty);
2495 case Intrinsic::log2:
2497 return ConstantFoldFP(
log2, APF, Ty);
2498 case Intrinsic::log10:
2500 return ConstantFoldFP(log10, APF, Ty);
2501 case Intrinsic::exp:
2502 return ConstantFoldFP(exp, APF, Ty);
2503 case Intrinsic::exp2:
2505 return ConstantFoldBinaryFP(pow,
APFloat(2.0), APF, Ty);
2506 case Intrinsic::exp10:
2508 return ConstantFoldBinaryFP(pow,
APFloat(10.0), APF, Ty);
2509 case Intrinsic::sin:
2510 return ConstantFoldFP(sin, APF, Ty);
2511 case Intrinsic::cos:
2512 return ConstantFoldFP(cos, APF, Ty);
2513 case Intrinsic::sqrt:
2514 return ConstantFoldFP(sqrt, APF, Ty);
2515 case Intrinsic::amdgcn_cos:
2516 case Intrinsic::amdgcn_sin: {
2517 double V = getValueAsDouble(
Op);
2518 if (V < -256.0 || V > 256.0)
2523 bool IsCos = IntrinsicID == Intrinsic::amdgcn_cos;
2524 double V4 =
V * 4.0;
2525 if (V4 == floor(V4)) {
2527 const double SinVals[4] = { 0.0, 1.0, 0.0, -1.0 };
2528 V = SinVals[((int)V4 + (IsCos ? 1 : 0)) & 3];
2535 return GetConstantFoldFPValue(V, Ty);
2551 case LibFunc_acos_finite:
2552 case LibFunc_acosf_finite:
2554 return ConstantFoldFP(acos, APF, Ty);
2558 case LibFunc_asin_finite:
2559 case LibFunc_asinf_finite:
2561 return ConstantFoldFP(asin, APF, Ty);
2566 return ConstantFoldFP(atan, APF, Ty);
2570 if (TLI->
has(Func)) {
2571 U.roundToIntegral(APFloat::rmTowardPositive);
2578 return ConstantFoldFP(cos, APF, Ty);
2582 case LibFunc_cosh_finite:
2583 case LibFunc_coshf_finite:
2585 return ConstantFoldFP(cosh, APF, Ty);
2589 case LibFunc_exp_finite:
2590 case LibFunc_expf_finite:
2592 return ConstantFoldFP(exp, APF, Ty);
2596 case LibFunc_exp2_finite:
2597 case LibFunc_exp2f_finite:
2600 return ConstantFoldBinaryFP(pow,
APFloat(2.0), APF, Ty);
2604 if (TLI->
has(Func)) {
2610 case LibFunc_floorf:
2611 if (TLI->
has(Func)) {
2612 U.roundToIntegral(APFloat::rmTowardNegative);
2618 case LibFunc_log_finite:
2619 case LibFunc_logf_finite:
2621 return ConstantFoldFP(log, APF, Ty);
2625 case LibFunc_log2_finite:
2626 case LibFunc_log2f_finite:
2629 return ConstantFoldFP(
log2, APF, Ty);
2632 case LibFunc_log10f:
2633 case LibFunc_log10_finite:
2634 case LibFunc_log10f_finite:
2637 return ConstantFoldFP(log10, APF, Ty);
2640 case LibFunc_ilogbf:
2642 return ConstantInt::get(Ty,
ilogb(APF),
true);
2647 return ConstantFoldFP(logb, APF, Ty);
2650 case LibFunc_log1pf:
2655 return ConstantFoldFP(log1p, APF, Ty);
2662 return ConstantFoldFP(erf, APF, Ty);
2664 case LibFunc_nearbyint:
2665 case LibFunc_nearbyintf:
2668 if (TLI->
has(Func)) {
2669 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2674 case LibFunc_roundf:
2675 if (TLI->
has(Func)) {
2676 U.roundToIntegral(APFloat::rmNearestTiesToAway);
2683 return ConstantFoldFP(sin, APF, Ty);
2687 case LibFunc_sinh_finite:
2688 case LibFunc_sinhf_finite:
2690 return ConstantFoldFP(sinh, APF, Ty);
2695 return ConstantFoldFP(sqrt, APF, Ty);
2700 return ConstantFoldFP(tan, APF, Ty);
2705 return ConstantFoldFP(tanh, APF, Ty);
2708 case LibFunc_truncf:
2709 if (TLI->
has(Func)) {
2710 U.roundToIntegral(APFloat::rmTowardZero);
2718 if (
auto *
Op = dyn_cast<ConstantInt>(
Operands[0])) {
2719 switch (IntrinsicID) {
2720 case Intrinsic::bswap:
2721 return ConstantInt::get(Ty->
getContext(),
Op->getValue().byteSwap());
2722 case Intrinsic::ctpop:
2723 return ConstantInt::get(Ty,
Op->getValue().popcount());
2724 case Intrinsic::bitreverse:
2725 return ConstantInt::get(Ty->
getContext(),
Op->getValue().reverseBits());
2726 case Intrinsic::convert_from_fp16: {
2727 APFloat Val(APFloat::IEEEhalf(),
Op->getValue());
2735 assert(status != APFloat::opInexact && !lost &&
2736 "Precision lost during fp16 constfolding");
2738 return ConstantFP::get(Ty->
getContext(), Val);
2741 case Intrinsic::amdgcn_s_wqm: {
2743 Val |= (Val & 0x5555555555555555ULL) << 1 |
2744 ((Val >> 1) & 0x5555555555555555ULL);
2745 Val |= (Val & 0x3333333333333333ULL) << 2 |
2746 ((Val >> 2) & 0x3333333333333333ULL);
2747 return ConstantInt::get(Ty, Val);
2750 case Intrinsic::amdgcn_s_quadmask: {
2753 for (
unsigned I = 0;
I <
Op->getBitWidth() / 4; ++
I, Val >>= 4) {
2757 QuadMask |= (1ULL <<
I);
2759 return ConstantInt::get(Ty, QuadMask);
2762 case Intrinsic::amdgcn_s_bitreplicate: {
2764 Val = (Val & 0x000000000000FFFFULL) | (Val & 0x00000000FFFF0000ULL) << 16;
2765 Val = (Val & 0x000000FF000000FFULL) | (Val & 0x0000FF000000FF00ULL) << 8;
2766 Val = (Val & 0x000F000F000F000FULL) | (Val & 0x00F000F000F000F0ULL) << 4;
2767 Val = (Val & 0x0303030303030303ULL) | (Val & 0x0C0C0C0C0C0C0C0CULL) << 2;
2768 Val = (Val & 0x1111111111111111ULL) | (Val & 0x2222222222222222ULL) << 1;
2769 Val = Val | Val << 1;
2770 return ConstantInt::get(Ty, Val);
2778 switch (IntrinsicID) {
2780 case Intrinsic::vector_reduce_add:
2781 case Intrinsic::vector_reduce_mul:
2782 case Intrinsic::vector_reduce_and:
2783 case Intrinsic::vector_reduce_or:
2784 case Intrinsic::vector_reduce_xor:
2785 case Intrinsic::vector_reduce_smin:
2786 case Intrinsic::vector_reduce_smax:
2787 case Intrinsic::vector_reduce_umin:
2788 case Intrinsic::vector_reduce_umax:
2795 if (isa<ConstantVector>(
Operands[0]) ||
2796 isa<ConstantDataVector>(
Operands[0])) {
2798 switch (IntrinsicID) {
2800 case Intrinsic::x86_sse_cvtss2si:
2801 case Intrinsic::x86_sse_cvtss2si64:
2802 case Intrinsic::x86_sse2_cvtsd2si:
2803 case Intrinsic::x86_sse2_cvtsd2si64:
2805 dyn_cast_or_null<ConstantFP>(
Op->getAggregateElement(0U)))
2806 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2810 case Intrinsic::x86_sse_cvttss2si:
2811 case Intrinsic::x86_sse_cvttss2si64:
2812 case Intrinsic::x86_sse2_cvttsd2si:
2813 case Intrinsic::x86_sse2_cvttsd2si64:
2815 dyn_cast_or_null<ConstantFP>(
Op->getAggregateElement(0U)))
2816 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2829 auto *FCmp = cast<ConstrainedFPCmpIntrinsic>(Call);
2831 if (FCmp->isSignaling()) {
2833 St = APFloat::opInvalidOp;
2836 St = APFloat::opInvalidOp;
2840 return ConstantInt::get(
Call->getType()->getScalarType(), Result);
2854 const auto *Op1 = dyn_cast<ConstantFP>(
Operands[0]);
2858 const auto *Op2 = dyn_cast<ConstantFP>(
Operands[1]);
2862 const APFloat &Op1V = Op1->getValueAPF();
2863 const APFloat &Op2V = Op2->getValueAPF();
2870 case LibFunc_pow_finite:
2871 case LibFunc_powf_finite:
2873 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
2877 if (TLI->
has(Func)) {
2879 if (APFloat::opStatus::opOK ==
V.mod(Op2->getValueAPF()))
2883 case LibFunc_remainder:
2884 case LibFunc_remainderf:
2885 if (TLI->
has(Func)) {
2887 if (APFloat::opStatus::opOK ==
V.remainder(Op2->getValueAPF()))
2892 case LibFunc_atan2f:
2898 case LibFunc_atan2_finite:
2899 case LibFunc_atan2f_finite:
2901 return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
2916 bool IsOp0Undef = isa<UndefValue>(
Operands[0]);
2917 bool IsOp1Undef = isa<UndefValue>(
Operands[1]);
2918 switch (IntrinsicID) {
2919 case Intrinsic::maxnum:
2920 case Intrinsic::minnum:
2921 case Intrinsic::maximum:
2922 case Intrinsic::minimum:
2923 case Intrinsic::nvvm_fmax_d:
2924 case Intrinsic::nvvm_fmin_d:
2932 case Intrinsic::nvvm_fmax_f:
2933 case Intrinsic::nvvm_fmax_ftz_f:
2934 case Intrinsic::nvvm_fmax_ftz_nan_f:
2935 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
2936 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
2937 case Intrinsic::nvvm_fmax_nan_f:
2938 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
2939 case Intrinsic::nvvm_fmax_xorsign_abs_f:
2941 case Intrinsic::nvvm_fmin_f:
2942 case Intrinsic::nvvm_fmin_ftz_f:
2943 case Intrinsic::nvvm_fmin_ftz_nan_f:
2944 case Intrinsic::nvvm_fmin_ftz_nan_xorsign_abs_f:
2945 case Intrinsic::nvvm_fmin_ftz_xorsign_abs_f:
2946 case Intrinsic::nvvm_fmin_nan_f:
2947 case Intrinsic::nvvm_fmin_nan_xorsign_abs_f:
2948 case Intrinsic::nvvm_fmin_xorsign_abs_f:
2952 if (!IsOp0Undef && !IsOp1Undef)
2954 if (
auto *
Op = dyn_cast<ConstantFP>(
Operands[IsOp0Undef ? 1 : 0])) {
2956 APInt NVCanonicalNaN(32, 0x7fffffff);
2957 return ConstantFP::get(
2961 return ConstantFP::get(Ty, FTZPreserveSign(
Op->getValueAPF()));
2969 if (
const auto *Op1 = dyn_cast<ConstantFP>(
Operands[0])) {
2970 const APFloat &Op1V = Op1->getValueAPF();
2972 if (
const auto *Op2 = dyn_cast<ConstantFP>(
Operands[1])) {
2973 if (Op2->getType() != Op1->getType())
2975 const APFloat &Op2V = Op2->getValueAPF();
2977 if (
const auto *ConstrIntr =
2978 dyn_cast_if_present<ConstrainedFPIntrinsic>(Call)) {
2982 switch (IntrinsicID) {
2985 case Intrinsic::experimental_constrained_fadd:
2986 St = Res.
add(Op2V, RM);
2988 case Intrinsic::experimental_constrained_fsub:
2991 case Intrinsic::experimental_constrained_fmul:
2994 case Intrinsic::experimental_constrained_fdiv:
2995 St = Res.
divide(Op2V, RM);
2997 case Intrinsic::experimental_constrained_frem:
3000 case Intrinsic::experimental_constrained_fcmp:
3001 case Intrinsic::experimental_constrained_fcmps:
3002 return evaluateCompare(Op1V, Op2V, ConstrIntr);
3006 return ConstantFP::get(Ty->
getContext(), Res);
3010 switch (IntrinsicID) {
3013 case Intrinsic::copysign:
3015 case Intrinsic::minnum:
3017 case Intrinsic::maxnum:
3019 case Intrinsic::minimum:
3021 case Intrinsic::maximum:
3024 case Intrinsic::nvvm_fmax_d:
3025 case Intrinsic::nvvm_fmax_f:
3026 case Intrinsic::nvvm_fmax_ftz_f:
3027 case Intrinsic::nvvm_fmax_ftz_nan_f:
3028 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
3029 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
3030 case Intrinsic::nvvm_fmax_nan_f:
3031 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
3032 case Intrinsic::nvvm_fmax_xorsign_abs_f:
3034 case Intrinsic::nvvm_fmin_d:
3035 case Intrinsic::nvvm_fmin_f:
3036 case Intrinsic::nvvm_fmin_ftz_f:
3037 case Intrinsic::nvvm_fmin_ftz_nan_f:
3038 case Intrinsic::nvvm_fmin_ftz_nan_xorsign_abs_f:
3039 case Intrinsic::nvvm_fmin_ftz_xorsign_abs_f:
3040 case Intrinsic::nvvm_fmin_nan_f:
3041 case Intrinsic::nvvm_fmin_nan_xorsign_abs_f:
3042 case Intrinsic::nvvm_fmin_xorsign_abs_f: {
3044 bool ShouldCanonicalizeNaNs = !(IntrinsicID == Intrinsic::nvvm_fmax_d ||
3045 IntrinsicID == Intrinsic::nvvm_fmin_d);
3050 APFloat A = IsFTZ ? FTZPreserveSign(Op1V) : Op1V;
3051 APFloat B = IsFTZ ? FTZPreserveSign(Op2V) : Op2V;
3053 bool XorSign =
false;
3055 XorSign =
A.isNegative() ^
B.isNegative();
3060 bool IsFMax =
false;
3061 switch (IntrinsicID) {
3062 case Intrinsic::nvvm_fmax_d:
3063 case Intrinsic::nvvm_fmax_f:
3064 case Intrinsic::nvvm_fmax_ftz_f:
3065 case Intrinsic::nvvm_fmax_ftz_nan_f:
3066 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
3067 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
3068 case Intrinsic::nvvm_fmax_nan_f:
3069 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
3070 case Intrinsic::nvvm_fmax_xorsign_abs_f:
3076 if (ShouldCanonicalizeNaNs) {
3078 if (
A.isNaN() &&
B.isNaN())
3079 return ConstantFP::get(Ty, NVCanonicalNaN);
3080 else if (IsNaNPropagating && (
A.isNaN() ||
B.isNaN()))
3081 return ConstantFP::get(Ty, NVCanonicalNaN);
3084 if (
A.isNaN() &&
B.isNaN())
3091 if (IsXorSignAbs && XorSign != Res.
isNegative())
3094 return ConstantFP::get(Ty->
getContext(), Res);
3101 switch (IntrinsicID) {
3104 case Intrinsic::pow:
3105 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
3106 case Intrinsic::amdgcn_fmul_legacy:
3111 return ConstantFP::get(Ty->
getContext(), Op1V * Op2V);
3114 }
else if (
auto *Op2C = dyn_cast<ConstantInt>(
Operands[1])) {
3115 switch (IntrinsicID) {
3116 case Intrinsic::ldexp: {
3117 return ConstantFP::get(
3119 scalbn(Op1V, Op2C->getSExtValue(), APFloat::rmNearestTiesToEven));
3121 case Intrinsic::is_fpclass: {
3134 return ConstantInt::get(Ty, Result);
3136 case Intrinsic::powi: {
3137 int Exp =
static_cast<int>(Op2C->getSExtValue());
3144 Res.
convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
3147 return ConstantFP::get(Ty->
getContext(), Res);
3164 const APInt *C0, *C1;
3165 if (!getConstIntOrUndef(
Operands[0], C0) ||
3166 !getConstIntOrUndef(
Operands[1], C1))
3169 switch (IntrinsicID) {
3171 case Intrinsic::smax:
3172 case Intrinsic::smin:
3173 case Intrinsic::umax:
3174 case Intrinsic::umin:
3184 return ConstantInt::get(
3190 case Intrinsic::scmp:
3191 case Intrinsic::ucmp:
3196 return ConstantInt::get(Ty, 0);
3199 if (IntrinsicID == Intrinsic::scmp)
3200 Res = C0->
sgt(*C1) ? 1 : C0->
slt(*C1) ? -1 : 0;
3202 Res = C0->
ugt(*C1) ? 1 : C0->
ult(*C1) ? -1 : 0;
3203 return ConstantInt::get(Ty, Res,
true);
3205 case Intrinsic::usub_with_overflow:
3206 case Intrinsic::ssub_with_overflow:
3212 case Intrinsic::uadd_with_overflow:
3213 case Intrinsic::sadd_with_overflow:
3218 cast<StructType>(Ty),
3223 case Intrinsic::smul_with_overflow:
3224 case Intrinsic::umul_with_overflow: {
3232 switch (IntrinsicID) {
3234 case Intrinsic::sadd_with_overflow:
3235 Res = C0->
sadd_ov(*C1, Overflow);
3237 case Intrinsic::uadd_with_overflow:
3238 Res = C0->
uadd_ov(*C1, Overflow);
3240 case Intrinsic::ssub_with_overflow:
3241 Res = C0->
ssub_ov(*C1, Overflow);
3243 case Intrinsic::usub_with_overflow:
3244 Res = C0->
usub_ov(*C1, Overflow);
3246 case Intrinsic::smul_with_overflow:
3247 Res = C0->
smul_ov(*C1, Overflow);
3249 case Intrinsic::umul_with_overflow:
3250 Res = C0->
umul_ov(*C1, Overflow);
3259 case Intrinsic::uadd_sat:
3260 case Intrinsic::sadd_sat:
3270 if (IntrinsicID == Intrinsic::uadd_sat)
3271 return ConstantInt::get(Ty, C0->
uadd_sat(*C1));
3273 return ConstantInt::get(Ty, C0->
sadd_sat(*C1));
3274 case Intrinsic::usub_sat:
3275 case Intrinsic::ssub_sat:
3285 if (IntrinsicID == Intrinsic::usub_sat)
3286 return ConstantInt::get(Ty, C0->
usub_sat(*C1));
3288 return ConstantInt::get(Ty, C0->
ssub_sat(*C1));
3289 case Intrinsic::cttz:
3290 case Intrinsic::ctlz:
3291 assert(C1 &&
"Must be constant int");
3298 if (IntrinsicID == Intrinsic::cttz)
3303 case Intrinsic::abs:
3304 assert(C1 &&
"Must be constant int");
3315 return ConstantInt::get(Ty, C0->
abs());
3316 case Intrinsic::amdgcn_wave_reduce_umin:
3317 case Intrinsic::amdgcn_wave_reduce_umax:
3318 return dyn_cast<Constant>(
Operands[0]);
3325 if ((isa<ConstantVector>(
Operands[0]) ||
3326 isa<ConstantDataVector>(
Operands[0])) &&
3330 cast<ConstantInt>(
Operands[1])->getValue() == 4) {
3332 switch (IntrinsicID) {
3334 case Intrinsic::x86_avx512_vcvtss2si32:
3335 case Intrinsic::x86_avx512_vcvtss2si64:
3336 case Intrinsic::x86_avx512_vcvtsd2si32:
3337 case Intrinsic::x86_avx512_vcvtsd2si64:
3339 dyn_cast_or_null<ConstantFP>(
Op->getAggregateElement(0U)))
3340 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3344 case Intrinsic::x86_avx512_vcvtss2usi32:
3345 case Intrinsic::x86_avx512_vcvtss2usi64:
3346 case Intrinsic::x86_avx512_vcvtsd2usi32:
3347 case Intrinsic::x86_avx512_vcvtsd2usi64:
3349 dyn_cast_or_null<ConstantFP>(
Op->getAggregateElement(0U)))
3350 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3354 case Intrinsic::x86_avx512_cvttss2si:
3355 case Intrinsic::x86_avx512_cvttss2si64:
3356 case Intrinsic::x86_avx512_cvttsd2si:
3357 case Intrinsic::x86_avx512_cvttsd2si64:
3359 dyn_cast_or_null<ConstantFP>(
Op->getAggregateElement(0U)))
3360 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3364 case Intrinsic::x86_avx512_cvttss2usi:
3365 case Intrinsic::x86_avx512_cvttss2usi64:
3366 case Intrinsic::x86_avx512_cvttsd2usi:
3367 case Intrinsic::x86_avx512_cvttsd2usi64:
3369 dyn_cast_or_null<ConstantFP>(
Op->getAggregateElement(0U)))
3370 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3398 if (
S1.isNegative() &&
S1.isNonZero() && !
S1.isNaN()) {
3420 switch (IntrinsicID) {
3423 case Intrinsic::amdgcn_cubeid:
3425 case Intrinsic::amdgcn_cubema:
3427 case Intrinsic::amdgcn_cubesc:
3429 case Intrinsic::amdgcn_cubetc:
3436 const APInt *C0, *C1, *C2;
3437 if (!getConstIntOrUndef(
Operands[0], C0) ||
3438 !getConstIntOrUndef(
Operands[1], C1) ||
3439 !getConstIntOrUndef(
Operands[2], C2))
3446 unsigned NumUndefBytes = 0;
3447 for (
unsigned I = 0;
I < 32;
I += 8) {
3456 const APInt *Src = ((Sel & 10) == 10 || (Sel & 12) == 4) ? C0 : C1;
3460 B = Src->extractBitsAsZExtValue(8, (Sel & 3) * 8);
3462 B = Src->extractBitsAsZExtValue(1, (Sel & 1) ? 31 : 15) * 0xff;
3465 Val.insertBits(
B,
I, 8);
3468 if (NumUndefBytes == 4)
3471 return ConstantInt::get(Ty, Val);
3482 if (
const auto *Op1 = dyn_cast<ConstantFP>(
Operands[0])) {
3483 if (
const auto *Op2 = dyn_cast<ConstantFP>(
Operands[1])) {
3484 if (
const auto *Op3 = dyn_cast<ConstantFP>(
Operands[2])) {
3485 const APFloat &C1 = Op1->getValueAPF();
3486 const APFloat &C2 = Op2->getValueAPF();
3487 const APFloat &C3 = Op3->getValueAPF();
3489 if (
const auto *ConstrIntr = dyn_cast<ConstrainedFPIntrinsic>(Call)) {
3493 switch (IntrinsicID) {
3496 case Intrinsic::experimental_constrained_fma:
3497 case Intrinsic::experimental_constrained_fmuladd:
3501 if (mayFoldConstrained(
3503 return ConstantFP::get(Ty->
getContext(), Res);
3507 switch (IntrinsicID) {
3509 case Intrinsic::amdgcn_fma_legacy: {
3519 case Intrinsic::fma:
3520 case Intrinsic::fmuladd: {
3522 V.fusedMultiplyAdd(C2, C3, APFloat::rmNearestTiesToEven);
3525 case Intrinsic::amdgcn_cubeid:
3526 case Intrinsic::amdgcn_cubema:
3527 case Intrinsic::amdgcn_cubesc:
3528 case Intrinsic::amdgcn_cubetc: {
3529 APFloat V = ConstantFoldAMDGCNCubeIntrinsic(IntrinsicID, C1, C2, C3);
3537 if (IntrinsicID == Intrinsic::smul_fix ||
3538 IntrinsicID == Intrinsic::smul_fix_sat) {
3544 const APInt *C0, *C1;
3545 if (!getConstIntOrUndef(
Operands[0], C0) ||
3546 !getConstIntOrUndef(
Operands[1], C1))
3560 unsigned Scale = cast<ConstantInt>(
Operands[2])->getZExtValue();
3562 assert(Scale < Width &&
"Illegal scale.");
3563 unsigned ExtendedWidth = Width * 2;
3565 (C0->
sext(ExtendedWidth) * C1->
sext(ExtendedWidth)).ashr(Scale);
3566 if (IntrinsicID == Intrinsic::smul_fix_sat) {
3575 if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) {
3576 const APInt *C0, *C1, *C2;
3577 if (!getConstIntOrUndef(
Operands[0], C0) ||
3578 !getConstIntOrUndef(
Operands[1], C1) ||
3579 !getConstIntOrUndef(
Operands[2], C2))
3582 bool IsRight = IntrinsicID == Intrinsic::fshr;
3596 unsigned LshrAmt = IsRight ? ShAmt :
BitWidth - ShAmt;
3597 unsigned ShlAmt = !IsRight ? ShAmt :
BitWidth - ShAmt;
3599 return ConstantInt::get(Ty, C1->
lshr(LshrAmt));
3601 return ConstantInt::get(Ty, C0->
shl(ShlAmt));
3602 return ConstantInt::get(Ty, C0->
shl(ShlAmt) | C1->
lshr(LshrAmt));
3605 if (IntrinsicID == Intrinsic::amdgcn_perm)
3606 return ConstantFoldAMDGCNPermIntrinsic(
Operands, Ty);
3618 return ConstantFoldScalarCall1(
Name, IntrinsicID, Ty,
Operands, TLI, Call);
3623 return FoldedLibCall;
3625 return ConstantFoldIntrinsicCall2(IntrinsicID, Ty,
Operands, Call);
3629 return ConstantFoldScalarCall3(
Name, IntrinsicID, Ty,
Operands, TLI, Call);
3634static Constant *ConstantFoldFixedVectorCall(
3642 switch (IntrinsicID) {
3643 case Intrinsic::masked_load: {
3652 auto *MaskElt =
Mask->getAggregateElement(
I);
3655 auto *PassthruElt = Passthru->getAggregateElement(
I);
3657 if (isa<UndefValue>(MaskElt)) {
3665 if (MaskElt->isNullValue()) {
3669 }
else if (MaskElt->isOneValue()) {
3681 case Intrinsic::arm_mve_vctp8:
3682 case Intrinsic::arm_mve_vctp16:
3683 case Intrinsic::arm_mve_vctp32:
3684 case Intrinsic::arm_mve_vctp64: {
3685 if (
auto *
Op = dyn_cast<ConstantInt>(
Operands[0])) {
3690 for (
unsigned i = 0; i < Lanes; i++) {
3700 case Intrinsic::get_active_lane_mask: {
3701 auto *Op0 = dyn_cast<ConstantInt>(
Operands[0]);
3702 auto *Op1 = dyn_cast<ConstantInt>(
Operands[1]);
3706 uint64_t Limit = Op1->getZExtValue();
3709 for (
unsigned i = 0; i < Lanes; i++) {
3710 if (
Base + i < Limit)
3725 for (
unsigned J = 0, JE =
Operands.size(); J != JE; ++J) {
3741 ConstantFoldScalarCall(
Name, IntrinsicID, Ty, Lane, TLI, Call);
3750static Constant *ConstantFoldScalableVectorCall(
3754 switch (IntrinsicID) {
3755 case Intrinsic::aarch64_sve_convert_from_svbool: {
3756 auto *Src = dyn_cast<Constant>(
Operands[0]);
3757 if (!Src || !Src->isNullValue())
3768static std::pair<Constant *, Constant *>
3770 if (isa<PoisonValue>(
Op))
3773 auto *ConstFP = dyn_cast<ConstantFP>(
Op);
3777 const APFloat &
U = ConstFP->getValueAPF();
3779 APFloat FrexpMant =
frexp(U, FrexpExp, APFloat::rmNearestTiesToEven);
3780 Constant *Result0 = ConstantFP::get(ConstFP->getType(), FrexpMant);
3787 return {Result0, Result1};
3797 switch (IntrinsicID) {
3798 case Intrinsic::frexp: {
3802 if (
auto *FVTy0 = dyn_cast<FixedVectorType>(Ty0)) {
3806 for (
unsigned I = 0, E = FVTy0->getNumElements();
I != E; ++
I) {
3808 std::tie(Results0[
I], Results1[
I]) =
3809 ConstantFoldScalarFrexpCall(Lane, Ty1);
3818 auto [Result0, Result1] = ConstantFoldScalarFrexpCall(
Operands[0], Ty1);
3823 case Intrinsic::sincos: {
3827 auto ConstantFoldScalarSincosCall =
3828 [&](
Constant *
Op) -> std::pair<Constant *, Constant *> {
3830 ConstantFoldScalarCall(
Name, Intrinsic::sin, TyScalar,
Op, TLI, Call);
3832 ConstantFoldScalarCall(
Name, Intrinsic::cos, TyScalar,
Op, TLI, Call);
3833 return std::make_pair(SinResult, CosResult);
3836 if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3842 std::tie(SinResults[
I], CosResults[
I]) =
3843 ConstantFoldScalarSincosCall(Lane);
3844 if (!SinResults[
I] || !CosResults[
I])
3852 auto [SinResult, CosResult] = ConstantFoldScalarSincosCall(
Operands[0]);
3853 if (!SinResult || !CosResult)
3860 return ConstantFoldScalarCall(
Name, IntrinsicID, StTy,
Operands, TLI, Call);
3871 return ConstantFoldIntrinsicCall2(
ID, Ty, {
LHS,
RHS},
3872 dyn_cast_if_present<CallBase>(
FMFSource));
3878 bool AllowNonDeterministic) {
3879 if (Call->isNoBuiltin())
3896 Type *Ty =
F->getReturnType();
3901 if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty))
3902 return ConstantFoldFixedVectorCall(
3905 if (
auto *SVTy = dyn_cast<ScalableVectorType>(Ty))
3906 return ConstantFoldScalableVectorCall(
3909 if (
auto *StTy = dyn_cast<StructType>(Ty))
3910 return ConstantFoldStructCall(
Name, IID, StTy,
Operands,
3911 F->getDataLayout(), TLI, Call);
3916 return ConstantFoldScalarCall(
Name, IID, Ty,
Operands, TLI, Call);
3923 if (Call->isNoBuiltin() || Call->isStrictFP())
3925 Function *
F = Call->getCalledFunction();
3933 if (Call->arg_size() == 1) {
3934 if (
ConstantFP *OpC = dyn_cast<ConstantFP>(Call->getArgOperand(0))) {
3943 case LibFunc_log10l:
3945 case LibFunc_log10f:
3946 return Op.isNaN() || (!
Op.isZero() && !
Op.isNegative());
3949 return !
Op.isNaN() && !
Op.isZero() && !
Op.isInfinity();
3955 if (OpC->getType()->isDoubleTy())
3957 if (OpC->getType()->isFloatTy())
3965 if (OpC->getType()->isDoubleTy())
3967 if (OpC->getType()->isFloatTy())
3977 return !
Op.isInfinity();
3981 case LibFunc_tanf: {
3984 Type *Ty = OpC->getType();
3986 return ConstantFoldFP(tan, OpC->getValueAPF(), Ty) !=
nullptr;
4012 if (OpC->getType()->isDoubleTy())
4014 if (OpC->getType()->isFloatTy())
4021 return Op.isNaN() ||
Op.isZero() || !
Op.isNegative();
4031 if (Call->arg_size() == 2) {
4032 ConstantFP *Op0C = dyn_cast<ConstantFP>(Call->getArgOperand(0));
4033 ConstantFP *Op1C = dyn_cast<ConstantFP>(Call->getArgOperand(1));
4041 case LibFunc_powf: {
4047 return ConstantFoldBinaryFP(pow, Op0, Op1, Ty) !=
nullptr;
4055 case LibFunc_remainderl:
4056 case LibFunc_remainder:
4057 case LibFunc_remainderf:
4062 case LibFunc_atan2f:
4063 case LibFunc_atan2l:
4079void TargetFolder::anchor() {}
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static Constant * FoldBitCast(Constant *V, Type *DestTy)
static ConstantFP * flushDenormalConstant(Type *Ty, const APFloat &APF, DenormalMode::DenormalModeKind Mode)
Constant * getConstantAtOffset(Constant *Base, APInt Offset, const DataLayout &DL)
If this Offset points exactly to the start of an aggregate element, return that element,...
static ConstantFP * flushDenormalConstantFP(ConstantFP *CFP, const Instruction *Inst, bool IsOutput)
static DenormalMode getInstrDenormalMode(const Instruction *CtxI, Type *Ty)
Return the denormal mode that can be assumed when executing a floating point operation at CtxI.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
amode Optimize addressing mode
mir Rename Register Operands
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
This file contains the definitions of the enumerations and flags associated with NVVM Intrinsics,...
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
static SymbolRef::Type getType(const Symbol *Sym)
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
opStatus divide(const APFloat &RHS, roundingMode RM)
void copySign(const APFloat &RHS)
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
opStatus subtract(const APFloat &RHS, roundingMode RM)
double convertToDouble() const
Converts this APFloat to host double value.
bool isPosInfinity() const
opStatus add(const APFloat &RHS, roundingMode RM)
const fltSemantics & getSemantics() const
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
opStatus multiply(const APFloat &RHS, roundingMode RM)
float convertToFloat() const
Converts this APFloat to host float value.
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
APInt bitcastToAPInt() const
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
opStatus mod(const APFloat &RHS)
bool isNegInfinity() const
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Class for arbitrary precision integers.
APInt umul_ov(const APInt &RHS, bool &Overflow) const
APInt usub_sat(const APInt &RHS) const
bool isMinSignedValue() const
Determine if this is the smallest signed value.
uint64_t getZExtValue() const
Get zero extended value.
uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
APInt trunc(unsigned width) const
Truncate to new width.
APInt abs() const
Get the absolute value.
APInt sadd_sat(const APInt &RHS) const
bool sgt(const APInt &RHS) const
Signed greater than comparison.
APInt usub_ov(const APInt &RHS, bool &Overflow) const
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
unsigned countr_zero() const
Count the number of trailing zero bits.
unsigned countl_zero() const
The APInt version of std::countl_zero.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
APInt uadd_sat(const APInt &RHS) const
APInt smul_ov(const APInt &RHS, bool &Overflow) const
APInt sext(unsigned width) const
Sign extend to a new width.
APInt shl(unsigned shiftAmt) const
Left-shift function.
bool slt(const APInt &RHS) const
Signed less than comparison.
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
bool isOne() const
Determine if this is a value of 1.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
APInt ssub_sat(const APInt &RHS) const
An arbitrary precision integer that knows its signedness.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
const T & back() const
back - Get the last element.
size_t size() const
size - Get the array size.
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.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
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 bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy)
This method can be used to determine if a cast from SrcTy to DstTy using Opcode op is valid or not.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
bool isFPPredicate() const
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static bool isDesirableCastOp(unsigned Opcode)
Whether creating a constant expression for this cast is desirable.
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
static bool isSupportedGetElementPtr(const Type *SrcElemTy)
Whether creating a constant expression for this getelementptr type is supported.
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
static bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
const APFloat & getValueAPF() const
static Constant * getZero(Type *Ty, bool Negative=false)
This is the shared class of boolean and integer constants.
static ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
static ConstantInt * getFalse(LLVMContext &Context)
static ConstantInt * getBool(LLVMContext &Context, bool V)
static Constant * get(StructType *T, ArrayRef< Constant * > V)
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static Constant * getAllOnesValue(Type *Ty)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Constrained floating point compare intrinsics.
This is the common base class for constrained floating point intrinsics.
std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
std::optional< RoundingMode > getRoundingMode() const
Wrapper for a function that represents a value that functionally represents the original function.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
static bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This provides a helper for copying FMF from an instruction or setting specified flags.
Class to represent fixed width SIMD vectors.
unsigned getNumElements() const
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags inBounds()
GEPNoWrapFlags withoutNoUnsignedSignedWrap() const
static GEPNoWrapFlags noUnsignedWrap()
bool hasNoUnsignedSignedWrap() const
static Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
PointerType * getType() const
Global values are always pointers.
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
bool isEquality() const
Return true if this predicate is either EQ or NE.
const Function * getFunction() const
Return the function this instruction belongs to.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
This is an important class for using LLVM in a threaded context.
static APInt getSaturationPoint(Intrinsic::ID ID, unsigned numBits)
Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values, so there is a certain thre...
ICmpInst::Predicate getPredicate() const
Returns the comparison predicate underlying the intrinsic.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Class to represent scalable SIMD vectors.
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
unsigned getElementContainingOffset(uint64_t FixedOffset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
TypeSize getElementOffset(unsigned Idx) const
Class to represent struct types.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
unsigned getIntegerBitWidth() const
Type * getStructElementType(unsigned N) const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
bool isPointerTy() const
True if this is an instance of PointerType.
static IntegerType * getInt1Ty(LLVMContext &C)
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
@ HalfTyID
16-bit floating point type
@ FloatTyID
32-bit floating point type
@ DoubleTyID
64-bit floating point type
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
bool isFP128Ty() const
Return true if this is 'fp128'.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isStructTy() const
True if this is an instance of StructType.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
static IntegerType * getInt16Ty(LLVMContext &C)
bool isAggregateType() const
Return true if the type is an aggregate type.
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static IntegerType * getInt8Ty(LLVMContext &C)
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
bool isX86_AMXTy() const
Return true if this is X86 AMX.
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
TypeID getTypeID() const
Return the type id for the type.
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
bool isIEEELikeFPTy() const
Return true if this is a well-behaved IEEE-like type, which has a IEEE compatible layout as defined b...
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
LLVMContext & getContext() const
All values hold a context through their type.
Base class of all SIMD vector types.
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
const ParentTy * getParent() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ C
The default llvm calling convention, compatible with C.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
@ CE
Windows NT (Windows on ARM)
int ilogb(const IEEEFloat &Arg)
@ ebStrict
This corresponds to "fpexcept.strict".
@ ebIgnore
This corresponds to "fpexcept.ignore".
bool FPToIntegerIntrinsicResultIsSigned(Intrinsic::ID IntrinsicID)
APFloat::roundingMode GetFPToIntegerRoundingMode(Intrinsic::ID IntrinsicID)
bool FPToIntegerIntrinsicShouldFTZ(Intrinsic::ID IntrinsicID)
bool FMinFMaxIsXorSignAbs(Intrinsic::ID IntrinsicID)
bool FMinFMaxShouldFTZ(Intrinsic::ID IntrinsicID)
bool FMinFMaxPropagatesNaNs(Intrinsic::ID IntrinsicID)
NodeAddr< FuncNode * > Func
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Constant * ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, Constant *RHS, Type *Ty, Instruction *FMFSource)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Constant * ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL)
ConstantFoldLoadThroughBitcast - try to cast constant to destination type returning null if unsuccess...
static double log2(double V)
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I, bool AllowNonDeterministic=true)
Attempt to constant fold a floating point binary operation with the specified operands,...
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
unsigned getPointerAddressSpace(const Type *T)
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Constant * ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2)
Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)
If this constant is a constant offset from a global, return the global and the constant.
bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 maximumNumber semantics.
Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
Constant * FlushFPConstant(Constant *Operand, const Instruction *I, bool IsOutput)
Attempt to flush float point constant according to denormal mode set in the instruction's parent func...
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 minimumNumber semantics.
bool isVectorIntrinsicWithScalarOpAtArg(Intrinsic::ID ID, unsigned ScalarOpdIdx, const TargetTransformInfo *TTI)
Identifies if the vector form of the intrinsic has a scalar operand.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
RoundingMode
Rounding mode.
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
constexpr unsigned BitWidth
Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
opStatus
IEEE-754R 7: Default exception handling.
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
DenormalModeKind
Represent handled modes for denormal (aka subnormal) modes in the floating point environment.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getDynamic()
static constexpr DenormalMode getIEEE()
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
bool isConstant() const
Returns true if we know the value of all bits.
const APInt & getConstant() const
Returns the value when all bits have a known value.