61 for (
unsigned i = 0; i != NumElts; ++i) {
81 assert(Op && Op->
isCast() &&
"Can't fold cast of cast without a cast!");
98 nullptr, FakeIntPtrTy,
nullptr);
109 if (
PointerType *DPTy = dyn_cast<PointerType>(DestTy))
110 if (PTy->getAddressSpace() == DPTy->getAddressSpace()
111 && PTy->getElementType()->isSized()) {
116 Type *ElTy = PTy->getElementType();
117 while (ElTy != DPTy->getElementType()) {
118 if (
StructType *STy = dyn_cast<StructType>(ElTy)) {
119 if (STy->getNumElements() == 0)
break;
120 ElTy = STy->getElementType(0);
123 dyn_cast<SequentialType>(ElTy)) {
124 ElTy = STy->getElementType();
131 if (ElTy == DPTy->getElementType())
139 if (
VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
141 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
142 "Not cast between same sized vectors!");
145 if (isa<ConstantAggregateZero>(V))
155 if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
161 if (isa<ConstantPointerNull>(V))
189 if (
FP->getType()->isPPC_FP128Ty())
197 FP->getValueAPF().bitcastToAPInt());
216 "Non-byte sized integer input");
218 assert(ByteSize &&
"Must be accessing some piece");
219 assert(ByteStart+ByteSize <= CSize &&
"Extracting invalid piece from input");
220 assert(ByteSize != CSize &&
"Should not extract everything");
224 APInt V = CI->getValue();
227 V = V.
trunc(ByteSize*8);
234 if (!CE)
return nullptr;
236 switch (CE->getOpcode()) {
237 default:
return nullptr;
238 case Instruction::Or: {
244 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS))
245 if (RHSC->isMinusOne())
253 case Instruction::And: {
267 case Instruction::LShr: {
273 if ((ShAmt & 7) != 0)
278 if (ShAmt.
uge(CSize - ByteStart))
282 if (ShAmt.
ule(CSize - (ByteStart + ByteSize)))
290 case Instruction::Shl: {
296 if ((ShAmt & 7) != 0)
301 if (ShAmt.
uge(ByteStart + ByteSize))
305 if (ShAmt.
ule(ByteStart))
313 case Instruction::ZExt: {
314 unsigned SrcBitSize =
315 cast<IntegerType>(CE->getOperand(0)->getType())->
getBitWidth();
318 if (ByteStart*8 >= SrcBitSize)
323 if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
324 return CE->getOperand(0);
328 if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
333 if ((ByteStart+ByteSize)*8 < SrcBitSize) {
334 assert((SrcBitSize&7) &&
"Shouldn't get byte sized case here");
354 if (
ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
360 if (
StructType *STy = dyn_cast<StructType>(Ty))
361 if (!STy->isPacked()) {
362 unsigned NumElems = STy->getNumElements();
370 for (
unsigned i = 1; i != NumElems; ++i)
385 if (!PTy->getElementType()->isIntegerTy(1))
388 PTy->getAddressSpace()),
411 if (
ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
420 if (
StructType *STy = dyn_cast<StructType>(Ty)) {
428 unsigned NumElems = STy->getNumElements();
436 for (
unsigned i = 1; i != NumElems; ++i)
437 if (MemberAlign !=
getFoldedAlignOf(STy->getElementType(i), DestTy,
true)) {
448 if (!PTy->getElementType()->isIntegerTy(1))
452 PTy->getAddressSpace()),
474 if (
ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
482 if (
StructType *STy = dyn_cast<StructType>(Ty))
483 if (!STy->isPacked()) {
484 unsigned NumElems = STy->getNumElements();
492 for (
unsigned i = 1; i != NumElems; ++i)
523 if (isa<UndefValue>(V)) {
527 if (opc == Instruction::ZExt || opc == Instruction::SExt ||
528 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
534 opc != Instruction::AddrSpaceCast)
544 }
else if (CE->getOpcode() == Instruction::GetElementPtr &&
547 opc != Instruction::AddrSpaceCast &&
550 !cast<GEPOperator>(CE)->getInRangeIndex().hasValue() &&
554 !CE->getType()->isVectorTy()) {
557 bool isAllNull =
true;
558 for (
unsigned i = 1,
e = CE->getNumOperands(); i !=
e; ++i)
559 if (!CE->getOperand(i)->isNullValue()) {
572 if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
576 VectorType *DestVecTy = cast<VectorType>(DestTy);
592 case Instruction::FPTrunc:
593 case Instruction::FPExt:
594 if (
ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
596 APFloat Val = FPC->getValueAPF();
608 case Instruction::FPToUI:
609 case Instruction::FPToSI:
610 if (
ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
611 const APFloat &V = FPC->getValueAPF();
614 APSInt IntVal(DestBitWidth, opc == Instruction::FPToUI);
624 case Instruction::IntToPtr:
628 case Instruction::PtrToInt:
636 if (CE->getOpcode() == Instruction::GetElementPtr &&
637 CE->getOperand(0)->isNullValue()) {
649 if (CE->getNumOperands() == 2) {
652 bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne();
659 }
else if (CE->getNumOperands() == 3 &&
660 CE->getOperand(1)->isNullValue()) {
662 if (
StructType *STy = dyn_cast<StructType>(Ty))
663 if (!STy->isPacked()) {
664 ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2));
666 STy->getNumElements() == 2 &&
667 STy->getElementType(0)->isIntegerTy(1)) {
672 if (Ty->isStructTy() || Ty->isArrayTy()) {
681 case Instruction::UIToFP:
682 case Instruction::SIToFP:
684 const APInt &api = CI->getValue();
692 case Instruction::ZExt:
696 CI->getValue().zext(BitWidth));
699 case Instruction::SExt:
703 CI->getValue().sext(BitWidth));
706 case Instruction::Trunc: {
713 CI->getValue().trunc(DestBitWidth));
719 if ((DestBitWidth & 7) == 0 &&
726 case Instruction::BitCast:
728 case Instruction::AddrSpaceCast:
749 auto *Cond = cast<Constant>(CondV->getOperand(i));
750 if (V1Element == V2Element) {
752 }
else if (isa<UndefValue>(Cond)) {
753 V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
755 if (!isa<ConstantInt>(Cond))
break;
766 if (isa<UndefValue>(Cond)) {
767 if (isa<UndefValue>(V1))
return V1;
770 if (isa<UndefValue>(V1))
return V2;
771 if (isa<UndefValue>(V2))
return V1;
772 if (V1 == V2)
return V1;
774 if (
ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
776 if (TrueVal->getOperand(0) == Cond)
779 if (
ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
781 if (FalseVal->getOperand(0) == Cond)
792 if (isa<UndefValue>(Val) || isa<UndefValue>(Idx))
804 if (
auto *CE = dyn_cast<ConstantExpr>(Val)) {
805 if (CE->getOpcode() == Instruction::GetElementPtr) {
807 Ops.
reserve(CE->getNumOperands());
808 for (
unsigned i = 0,
e = CE->getNumOperands(); i !=
e; ++i) {
818 return CE->getWithOperands(Ops, CE->getType()->getVectorElementType(),
820 Ops[0]->getType()->getPointerElementType());
830 if (isa<UndefValue>(Idx))
834 if (!CIdx)
return nullptr;
843 if (CIdx->uge(NumElts))
849 uint64_t IdxVal = CIdx->getZExtValue();
850 for (
unsigned i = 0; i != NumElts; ++i) {
870 if (isa<UndefValue>(Mask))
874 if (isa<ConstantExpr>(Mask))
return nullptr;
886 for (
unsigned i = 0; i != MaskNumElts; ++i) {
893 if (
unsigned(Elt) >= SrcNumElts*2)
895 else if (
unsigned(Elt) >= SrcNumElts) {
931 NumElts =
ST->getNumElements();
933 NumElts = cast<SequentialType>(Agg->
getType())->getNumElements();
936 for (
unsigned i = 0; i != NumElts; ++i) {
938 if (!C)
return nullptr;
959 if (HasScalarUndef) {
960 switch (static_cast<Instruction::UnaryOps>(Opcode)) {
961 case Instruction::FNeg:
963 case Instruction::UnaryOpsEnd:
969 assert(!HasScalarUndef &&
"Unexpected UndefValue");
971 assert(!isa<ConstantInt>(C) &&
"Unexpected Integer UnaryOp");
973 if (
ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
974 const APFloat &CV = CFP->getValueAPF();
978 case Instruction::FNeg:
985 for (
unsigned i = 0,
e = VTy->getNumElements(); i !=
e; ++i) {
1018 (isa<UndefValue>(C1) || isa<UndefValue>(C2));
1019 if (HasScalarUndef) {
1020 switch (static_cast<Instruction::BinaryOps>(Opcode)) {
1021 case Instruction::Xor:
1022 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1028 case Instruction::Sub:
1030 case Instruction::And:
1031 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1034 case Instruction::Mul: {
1036 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1047 case Instruction::SDiv:
1048 case Instruction::UDiv:
1050 if (isa<UndefValue>(C2))
1058 case Instruction::URem:
1059 case Instruction::SRem:
1068 case Instruction::Or:
1069 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1072 case Instruction::LShr:
1074 if (isa<UndefValue>(C2))
1081 case Instruction::AShr:
1083 if (isa<UndefValue>(C2))
1091 case Instruction::Shl:
1093 if (isa<UndefValue>(C2))
1100 case Instruction::FAdd:
1101 case Instruction::FSub:
1102 case Instruction::FMul:
1103 case Instruction::FDiv:
1104 case Instruction::FRem:
1106 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1116 case Instruction::BinaryOpsEnd:
1122 assert(!HasScalarUndef &&
"Unexpected UndefValue");
1125 if (
ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1128 if (CI2->isZero())
return C1;
1130 case Instruction::Sub:
1131 if (CI2->isZero())
return C1;
1133 case Instruction::Mul:
1134 if (CI2->isZero())
return C2;
1138 case Instruction::UDiv:
1139 case Instruction::SDiv:
1145 case Instruction::URem:
1146 case Instruction::SRem:
1152 case Instruction::And:
1153 if (CI2->isZero())
return C2;
1154 if (CI2->isMinusOne())
1159 if (CE1->getOpcode() == Instruction::ZExt) {
1160 unsigned DstWidth = CI2->
getType()->getBitWidth();
1164 if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
1169 if (CE1->getOpcode() == Instruction::PtrToInt &&
1170 isa<GlobalValue>(CE1->getOperand(0))) {
1171 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
1187 if (!GVAlign && isa<Function>(GV))
1189 }
else if (isa<Function>(GV)) {
1197 if (GVAlign && *GVAlign > 1) {
1198 unsigned DstWidth = CI2->getType()->getBitWidth();
1199 unsigned SrcWidth = std::min(DstWidth,
Log2(*GVAlign));
1203 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
1209 case Instruction::Or:
1210 if (CI2->isZero())
return C1;
1211 if (CI2->isMinusOne())
1214 case Instruction::Xor:
1215 if (CI2->isZero())
return C1;
1218 switch (CE1->getOpcode()) {
1220 case Instruction::ICmp:
1221 case Instruction::FCmp:
1227 CE1->getOperand(1));
1231 case Instruction::AShr:
1234 if (CE1->getOpcode() == Instruction::ZExt)
1238 }
else if (isa<ConstantInt>(C1)) {
1244 if (
ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
1245 if (
ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1246 const APInt &C1V = CI1->getValue();
1247 const APInt &C2V = CI2->getValue();
1253 case Instruction::Sub:
1255 case Instruction::Mul:
1257 case Instruction::UDiv:
1258 assert(!CI2->isZero() &&
"Div by zero handled above");
1260 case Instruction::SDiv:
1261 assert(!CI2->isZero() &&
"Div by zero handled above");
1265 case Instruction::URem:
1266 assert(!CI2->isZero() &&
"Div by zero handled above");
1268 case Instruction::SRem:
1269 assert(!CI2->isZero() &&
"Div by zero handled above");
1273 case Instruction::And:
1275 case Instruction::Or:
1277 case Instruction::Xor:
1279 case Instruction::Shl:
1283 case Instruction::LShr:
1287 case Instruction::AShr:
1295 case Instruction::SDiv:
1296 case Instruction::UDiv:
1297 case Instruction::URem:
1298 case Instruction::SRem:
1299 case Instruction::LShr:
1300 case Instruction::AShr:
1301 case Instruction::Shl:
1302 if (CI1->isZero())
return C1;
1307 }
else if (
ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
1308 if (
ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
1309 const APFloat &C1V = CFP1->getValueAPF();
1310 const APFloat &C2V = CFP2->getValueAPF();
1315 case Instruction::FAdd:
1318 case Instruction::FSub:
1321 case Instruction::FMul:
1324 case Instruction::FDiv:
1327 case Instruction::FRem:
1336 for (
unsigned i = 0,
e = VTy->getNumElements(); i !=
e; ++i) {
1360 if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(
T)->
getOpcode() != Opcode)
1363 }
else if (isa<ConstantExpr>(C2)) {
1374 case Instruction::Sub:
1376 case Instruction::Mul:
1378 case Instruction::Shl:
1379 case Instruction::LShr:
1380 case Instruction::AShr:
1384 case Instruction::SDiv:
1385 case Instruction::UDiv:
1389 case Instruction::URem:
1390 case Instruction::SRem:
1406 if (
StructType *STy = dyn_cast<StructType>(Ty)) {
1407 if (STy->isOpaque())
return true;
1410 for (
unsigned i = 0,
e = STy->getNumElements(); i !=
e; ++i)
1414 }
else if (
ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1428 if (C1 == C2)
return 0;
1432 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1436 if (cast<ConstantInt>(C1)->getValue().getActiveBits() > 64 ||
1437 cast<ConstantInt>(C2)->getValue().getActiveBits() > 64)
1442 int64_t C1Val = cast<ConstantInt>(C1)->getSExtValue();
1443 int64_t C2Val = cast<ConstantInt>(C2)->getSExtValue();
1445 if (C1Val == C2Val)
return 0;
1473 "Cannot compare values of different types!");
1479 if (!isa<ConstantExpr>(V1)) {
1480 if (!isa<ConstantExpr>(V2)) {
1509 case Instruction::FPTrunc:
1510 case Instruction::FPExt:
1511 case Instruction::UIToFP:
1512 case Instruction::SIToFP:
1526 auto isGlobalUnsafeForEquality = [](
const GlobalValue *GV) {
1527 if (GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage())
1529 if (
const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
1530 Type *Ty = GVar->getValueType();
1542 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
1543 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
1563 "Cannot compare different types of values!");
1566 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
1567 !isa<BlockAddress>(V1)) {
1568 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(
V2) &&
1569 !isa<BlockAddress>(V2)) {
1596 }
else if (
const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1597 if (isa<ConstantExpr>(V2)) {
1608 if (
const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1610 }
else if (isa<BlockAddress>(V2)) {
1613 assert(isa<ConstantPointerNull>(V2) &&
"Canonicalization guarantee!");
1619 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1621 GV->getType()->getAddressSpace()))
1624 }
else if (
const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1625 if (isa<ConstantExpr>(V2)) {
1636 if (
const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1640 if (BA2->getFunction() != BA->getFunction())
1644 assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
1645 "Canonicalization guarantee!");
1655 case Instruction::Trunc:
1656 case Instruction::FPTrunc:
1657 case Instruction::FPExt:
1658 case Instruction::FPToUI:
1659 case Instruction::FPToSI:
1662 case Instruction::UIToFP:
1663 case Instruction::SIToFP:
1664 case Instruction::BitCast:
1665 case Instruction::ZExt:
1666 case Instruction::SExt:
1668 if (CE1Op0->getType()->isFPOrFPVectorTy())
1674 if (CE1->
getOpcode() == Instruction::ZExt) isSigned =
false;
1675 if (CE1->
getOpcode() == Instruction::SExt) isSigned =
true;
1682 case Instruction::GetElementPtr: {
1686 if (isa<ConstantPointerNull>(V2)) {
1689 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1690 if (GV->hasExternalWeakLinkage())
1698 }
else if (isa<ConstantPointerNull>(CE1Op0)) {
1709 }
else if (
const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1710 if (isa<ConstantPointerNull>(CE1Op0)) {
1711 if (GV2->hasExternalWeakLinkage())
1719 }
else if (
const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1727 "Surprising getelementptr!");
1743 case Instruction::GetElementPtr:
1746 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1748 if (CE1Op0 != CE2Op0) {
1752 cast<GlobalValue>(CE2Op0));
1815 VT->getNumElements());
1827 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1838 if (isIntegerPredicate)
1848 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1850 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
1852 GV->getType()->getAddressSpace())) {
1860 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1862 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
1864 GV->getType()->getAddressSpace())) {
1876 if (isa<ConstantInt>(C2))
1886 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1887 const APInt &V1 = cast<ConstantInt>(C1)->getValue();
1888 const APInt &
V2 = cast<ConstantInt>(C2)->getValue();
1902 }
else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1903 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1904 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1966 (isa<ConstantExpr>(C1) || isa<ConstantExpr>(C2))) {
2107 if (CE2->getOpcode() == Instruction::BitCast &&
2117 if ((CE1->getOpcode() == Instruction::SExt &&
2119 (CE1->getOpcode() == Instruction::ZExt &&
2123 if (CE1Inverse == CE1Op0) {
2133 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
2146 template<
typename IndexTy>
2149 if (Idxs.
empty())
return true;
2152 if (cast<Constant>(Idxs[0])->isNullValue())
return true;
2156 if (
auto *CI = dyn_cast<ConstantInt>(Idxs[0])) {
2160 auto *CV = cast<ConstantDataVector>(Idxs[0]);
2161 CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
2162 if (!CI || !CI->isOne())
2166 for (
unsigned i = 1,
e = Idxs.
size(); i !=
e; ++i)
2167 if (!cast<Constant>(Idxs[i])->isNullValue())
2182 if (IndexVal < 0 || (NumElements > 0 && (uint64_t)IndexVal >= NumElements))
2193 if (Idxs.
empty())
return C;
2198 if (isa<UndefValue>(C))
2201 Constant *Idx0 = cast<Constant>(Idxs[0]);
2202 if (Idxs.
size() == 1 && (Idx0->
isNullValue() || isa<UndefValue>(Idx0)))
2205 cast<VectorType>(GEPTy)->getNumElements(), C)
2210 for (
unsigned i = 0,
e = Idxs.
size(); i !=
e; ++i)
2211 if (!isa<UndefValue>(Idxs[i]) &&
2212 !cast<Constant>(Idxs[i])->isNullValue()) {
2220 assert(Ty &&
"Invalid indices for GEP!");
2228 for (
unsigned i = 0,
e = Idxs.
size(); i !=
e; ++i) {
2229 if (
auto *VT = dyn_cast<VectorType>(Idxs[i]->
getType())) {
2244 if (CE->getOpcode() == Instruction::GetElementPtr) {
2269 bool PerformFold =
false;
2273 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Idx0))
2277 !CE->getOperand(CE->getNumOperands() - 1)
2283 NewIndices.
reserve(Idxs.
size() + CE->getNumOperands());
2284 NewIndices.
append(CE->op_begin() + 1, CE->op_end() - 1);
2288 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
2292 if (IdxTy != Idx0->
getType()) {
2293 unsigned CommonExtendedWidth =
2296 CommonExtendedWidth =
std::max(CommonExtendedWidth, 64U);
2316 if (IRIndex && *IRIndex == CE->getNumOperands() - 2 && !Idx0->
isNullValue())
2320 cast<GEPOperator>(CE)->getSourceElementType(), CE->getOperand(0),
2321 NewIndices, InBounds && cast<GEPOperator>(CE)->isInBounds(),
2339 if (SrcPtrTy && DstPtrTy) {
2344 if (SrcArrayTy && DstArrayTy
2349 Idxs, InBounds, InRangeIndex);
2358 Type *Ty = PointeeTy;
2361 !isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]);
2362 for (
unsigned i = 1,
e = Idxs.
size(); i !=
e;
2363 Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
2364 if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) {
2369 if (!isa<ConstantInt>(Idxs[i - 1]) && !isa<ConstantDataVector>(Idxs[i - 1]))
2372 if (InRangeIndex && i == *InRangeIndex + 1) {
2378 if (isa<StructType>(Ty)) {
2382 auto *STy = cast<SequentialType>(Ty);
2383 if (isa<VectorType>(STy)) {
2388 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
2392 if (CI->getSExtValue() < 0) {
2398 auto *CV = cast<ConstantDataVector>(Idxs[i]);
2400 for (
unsigned I = 0,
E = CV->getNumElements();
I !=
E; ++
I) {
2401 auto *CI = cast<ConstantInt>(CV->getElementAsConstant(
I));
2403 if (CI->getSExtValue() < 0) {
2408 if (InRange || Unknown)
2413 if (isa<StructType>(Prev)) {
2423 uint64_t NumElements = STy->getArrayNumElements();
2427 Constant *CurrIdx = cast<Constant>(Idxs[i]);
2429 NewIdxs[i - 1] ? NewIdxs[i - 1] : cast<Constant>(Idxs[i - 1]);
2431 bool IsPrevIdxVector = PrevIdx->getType()->isVectorTy();
2432 bool UseVector = IsCurrIdxVector || IsPrevIdxVector;
2434 if (!IsCurrIdxVector && IsPrevIdxVector)
2436 PrevIdx->getType()->getVectorNumElements(), CurrIdx);
2438 if (!IsPrevIdxVector && IsCurrIdxVector)
2446 IsPrevIdxVector ? PrevIdx->getType()->getVectorNumElements()
2454 unsigned CommonExtendedWidth =
2455 std::max(PrevIdx->getType()->getScalarSizeInBits(),
2457 CommonExtendedWidth =
std::max(CommonExtendedWidth, 64U);
2464 ExtendedTy, IsPrevIdxVector
2465 ? PrevIdx->getType()->getVectorNumElements()
2468 if (!PrevIdx->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
2478 if (!NewIdxs.
empty()) {
2479 for (
unsigned i = 0,
e = Idxs.
size(); i !=
e; ++i)
2480 if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
2487 if (!Unknown && !InBounds)
2488 if (
auto *GV = dyn_cast<GlobalVariable>(C))
2491 true, InRangeIndex);
bool isBoundedSequential() const
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Type * getVectorElementType() const
unsigned Log2(Align A)
Returns the log2 of the alignment.
static const fltSemantics & IEEEquad() LLVM_READNONE
static Constant * FoldBitCast(Constant *V, Type *DestTy)
static ConstantInt * getFalse(LLVMContext &Context)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
class_match< UndefValue > m_Undef()
Match an arbitrary undef constant.
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
static IntegerType * getInt1Ty(LLVMContext &C)
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
uint64_t getZExtValue() const
Get zero extended value.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
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.
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
void push_back(const T &Elt)
bool isFP128Ty() const
Return true if this is 'fp128'.
Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices...
bool slt(const APInt &RHS) const
Signed less than comparison.
APInt udiv(const APInt &RHS) const
Unsigned division operation.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
static Constant * getFoldedOffsetOf(Type *Ty, Constant *FieldNo, Type *DestTy, bool Folded)
Return a ConstantExpr with type DestTy for offsetof on Ty and FieldNo, with any known factors factore...
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
gep_type_iterator gep_type_end(const User *GEP)
Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
0 1 0 0 True if ordered and less than
LLVMContext & getContext() const
All values hold a context through their type.
1 1 1 0 True if unordered or not equal
bool sgt(const APInt &RHS) const
Signed greater than comparison.
APInt trunc(unsigned width) const
Truncate to new width.
static IntegerType * getInt64Ty(LLVMContext &C)
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
bool isVectorTy() const
True if this is an instance of VectorType.
static Constant * getFoldedAlignOf(Type *Ty, Type *DestTy, bool Folded)
Return a ConstantExpr with type DestTy for alignof on Ty, with any known factors factored out...
void reserve(size_type N)
bool sle(const APInt &RHS) const
Signed less or equal comparison.
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
unsigned getBitWidth() const
Return the number of bits in the APInt.
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.
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
1 0 0 1 True if unordered or equal
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
opStatus divide(const APFloat &RHS, roundingMode RM)
The address of a basic block.
bool match(Val *V, const Pattern &P)
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
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.
Class to represent struct types.
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2, bool isSigned)
This function determines if there is anything we can decide about the two constants provided...
Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
static Constant * BitCastConstantVector(Constant *CV, VectorType *DstTy)
Convert the specified vector Constant node to the specified vector type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
0 1 0 1 True if ordered and less than or equal
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
uint64_t getNumElements() const
For scalable vectors, this will return the minimum number of elements in the vector.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
static Constant * getSizeOf(Type *Ty)
getSizeOf constant expr - computes the (alloc) size of a type (in address-units, not bits) in a targe...
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.
static unsigned foldConstantCastPair(unsigned opc, ConstantExpr *Op, Type *DstTy)
This function determines which opcode to use to fold two constant cast expressions together...
Type * getType() const
All values are typed, get the type of this value.
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value. ...
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
static int IdxCompare(Constant *C1, Constant *C2, Type *ElTy)
Compare the two constants as though they were getelementptr indices.
Class to represent array types.
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
const APInt & getValue() const
Return the constant as an APInt value reference.
bool isGEPWithNoNotionalOverIndexing() const
Return true if this is a getelementptr expression and all the index operands are compile-time known i...
opStatus subtract(const APFloat &RHS, roundingMode RM)
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
static const fltSemantics & IEEEdouble() LLVM_READNONE
unsigned getAlignment() const
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'.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, const GlobalValue *GV2)
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, Constant *Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and indices...
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
bool isAllOnesValue() const
Determine if all bits are set.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
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...
bool ult(const APInt &RHS) const
Unsigned less than comparison.
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.
static Constant * getFoldedSizeOf(Type *Ty, Type *DestTy, bool Folded)
Return a ConstantExpr with type DestTy for sizeof on Ty, with any known factors factored out...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Constant * getAnd(Constant *C1, Constant *C2)
ConstantFP - Floating Point Values [float, double].
MaybeAlign getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
bool isMinSignedValue() const
Determine if this is the smallest signed value.
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false)
Return the identity constant for a binary opcode.
opStatus multiply(const APFloat &RHS, roundingMode RM)
static Constant * get(StructType *T, ArrayRef< Constant *> V)
0 1 1 1 True if ordered (no nans)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
bool isX86_MMXTy() const
Return true if this is X86 MMX.
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
get* - Return some common constants without having to specify the full Instruction::OPCODE identifier...
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
Class to represent integer types.
Constant Vector Declarations.
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static Constant * getNot(Constant *C)
static Constant * getAllOnesValue(Type *Ty)
1 1 1 1 Always true (always folded)
Type * getIndexedType() const
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static wasm::ValType getType(const TargetRegisterClass *RC)
Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
static int getMaskValue(const Constant *Mask, unsigned Elt)
Return the shuffle mask value for the specified element of the mask.
1 1 0 1 True if unordered, less than, or equal
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
static Constant * getFCmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
This is the superclass of the array and vector type classes.
0 0 1 0 True if ordered and greater than
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
static const fltSemantics & IEEEsingle() LLVM_READNONE
static Constant * ExtractConstantBytes(Constant *C, unsigned ByteStart, unsigned ByteSize)
V is an integer constant which only has a subset of its bytes used.
unsigned getNumOperands() const
static bool isIndexInRangeOfArrayType(uint64_t NumElements, const ConstantInt *CI)
Test whether a given ConstantInt is in-range for a SequentialType.
This is the shared class of boolean and integer constants.
bool isScalable() const
Returns whether or not this is a scalable vector (meaning the total element count is a multiple of th...
static const fltSemantics & IEEEhalf() LLVM_READNONE
Align max(MaybeAlign Lhs, Align Rhs)
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
1 1 0 0 True if unordered or less than
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
static Constant * getNUWMul(Constant *C1, Constant *C2)
APFloat neg(APFloat X)
Returns the negated value of the argument.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
static unsigned isEliminableCastPair(Instruction::CastOps firstOpcode, Instruction::CastOps secondOpcode, Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, Type *DstIntPtrTy)
Determine how a pair of casts can be eliminated, if they can be at all.
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 uge(const APInt &RHS) const
Unsigned greater or equal comparison.
static bool isMaybeZeroSizedType(Type *Ty)
This type is zero-sized if it's an array or structure of zero-sized types.
static ConstantInt * getTrue(LLVMContext &Context)
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
bool isCommutative() const
Return true if the instruction is commutative:
unsigned getVectorNumElements() const
bool isIntPredicate() const
bool isTrueWhenEqual() const
This is just a convenience.
Class to represent vector types.
Class for arbitrary precision integers.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
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 isX86_FP80Ty() const
Return true if this is x86 long double.
bool isSequential() const
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
Constant * ConstantFoldCompareInstruction(unsigned short predicate, Constant *C1, Constant *C2)
opStatus add(const APFloat &RHS, roundingMode RM)
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
static Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
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
uint64_t getSequentialNumElements() const
LLVM_NODISCARD bool empty() const
static Constant * getOffsetOf(StructType *STy, unsigned FieldNo)
getOffsetOf constant expr - computes the offset of a struct field in a target independent way (Note: ...
unsigned greater or equal
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
static bool isInBoundsIndices(ArrayRef< IndexTy > Idxs)
Test whether the given sequence of normalized indices is "inbounds".
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList)
Create an "inbounds" getelementptr.
bool isEquality() const
Return true if this predicate is either EQ or NE.
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 * getOr(Constant *C1, Constant *C2)
static const fltSemantics & Bogus() LLVM_READNONE
A Pseudo fltsemantic used to construct APFloats that cannot conflict with anything real...
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices...
0 1 1 0 True if ordered and operands are unequal
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
1 0 1 0 True if unordered or greater than
Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, bool InBounds, Optional< unsigned > InRangeIndex, ArrayRef< Value *> Idxs)
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getMinSignedBits() const
Get the minimum bit size for this signed APInt.
static Constant * getSRem(Constant *C1, Constant *C2)
0 0 0 1 True if ordered and equal
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
1 0 1 1 True if unordered, greater than, or equal
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
#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.
bool isCast() const
Return true if this is a convert constant expression.
Type * getElementType() const
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Type * getSourceElementType() const
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices...
bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty...
static APInt getNullValue(unsigned numBits)
Get the '0' value.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
static Constant * getAlignOf(Type *Ty)
getAlignOf constant expr - computes the alignment of a type in a target independent way (Note: the re...
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
0 0 1 1 True if ordered and greater than or equal
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
static Type * getGEPReturnType(Type *ElTy, Value *Ptr, ArrayRef< Value *> IdxList)
Returns the pointer type returned by the GEP instruction, which may be a vector of pointers...
static Constant * get(ArrayRef< Constant *> V)
Type * getElementType() const
static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2)
This function determines if there is anything we can decide about the two constants provided...
0 0 0 0 Always false (always folded)
bool empty() const
empty - Check if the array is empty.
cmpResult compare(const APFloat &RHS) const
static Constant * getXor(Constant *C1, Constant *C2)
const fltSemantics & getFltSemantics() const
gep_type_iterator gep_type_begin(const User *GEP)
static Constant * get(unsigned Opcode, Constant *C1, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a unary operator constant expression, folding if possible.