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))
795 if (
ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
807 if (isa<UndefValue>(Idx))
811 if (!CIdx)
return nullptr;
814 if (CIdx->uge(NumElts))
820 uint64_t IdxVal = CIdx->getZExtValue();
821 for (
unsigned i = 0; i != NumElts; ++i) {
841 if (isa<UndefValue>(Mask))
845 if (isa<ConstantExpr>(Mask))
return nullptr;
851 for (
unsigned i = 0; i != MaskNumElts; ++i) {
858 if (
unsigned(Elt) >= SrcNumElts*2)
860 else if (
unsigned(Elt) >= SrcNumElts) {
896 NumElts =
ST->getNumElements();
898 NumElts = cast<SequentialType>(Agg->
getType())->getNumElements();
901 for (
unsigned i = 0; i != NumElts; ++i) {
903 if (!C)
return nullptr;
924 if (HasScalarUndef) {
925 switch (static_cast<Instruction::UnaryOps>(Opcode)) {
926 case Instruction::FNeg:
928 case Instruction::UnaryOpsEnd:
934 assert(!HasScalarUndef &&
"Unexpected UndefValue");
936 assert(!isa<ConstantInt>(C) &&
"Unexpected Integer UnaryOp");
938 if (
ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
939 const APFloat &CV = CFP->getValueAPF();
943 case Instruction::FNeg:
950 for (
unsigned i = 0,
e = VTy->getNumElements(); i !=
e; ++i) {
970 (isa<UndefValue>(C1) || isa<UndefValue>(C2));
971 if (HasScalarUndef) {
972 switch (static_cast<Instruction::BinaryOps>(Opcode)) {
973 case Instruction::Xor:
974 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
980 case Instruction::Sub:
982 case Instruction::And:
983 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
986 case Instruction::Mul: {
988 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
999 case Instruction::SDiv:
1000 case Instruction::UDiv:
1002 if (isa<UndefValue>(C2))
1010 case Instruction::URem:
1011 case Instruction::SRem:
1020 case Instruction::Or:
1021 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1024 case Instruction::LShr:
1026 if (isa<UndefValue>(C2))
1033 case Instruction::AShr:
1035 if (isa<UndefValue>(C2))
1043 case Instruction::Shl:
1045 if (isa<UndefValue>(C2))
1052 case Instruction::FAdd:
1053 case Instruction::FSub:
1054 case Instruction::FMul:
1055 case Instruction::FDiv:
1056 case Instruction::FRem:
1058 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1068 case Instruction::BinaryOpsEnd:
1074 assert(!HasScalarUndef &&
"Unexpected UndefValue");
1077 if (
ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1080 if (CI2->isZero())
return C1;
1082 case Instruction::Sub:
1083 if (CI2->isZero())
return C1;
1085 case Instruction::Mul:
1086 if (CI2->isZero())
return C2;
1090 case Instruction::UDiv:
1091 case Instruction::SDiv:
1097 case Instruction::URem:
1098 case Instruction::SRem:
1104 case Instruction::And:
1105 if (CI2->isZero())
return C2;
1106 if (CI2->isMinusOne())
1111 if (CE1->getOpcode() == Instruction::ZExt) {
1112 unsigned DstWidth = CI2->
getType()->getBitWidth();
1116 if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
1121 if (CE1->getOpcode() == Instruction::PtrToInt &&
1122 isa<GlobalValue>(CE1->getOperand(0))) {
1123 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
1139 if (!GVAlign && isa<Function>(GV))
1141 }
else if (isa<Function>(GV)) {
1149 if (GVAlign && *GVAlign > 1) {
1150 unsigned DstWidth = CI2->getType()->getBitWidth();
1151 unsigned SrcWidth = std::min(DstWidth,
Log2(*GVAlign));
1155 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
1161 case Instruction::Or:
1162 if (CI2->isZero())
return C1;
1163 if (CI2->isMinusOne())
1166 case Instruction::Xor:
1167 if (CI2->isZero())
return C1;
1170 switch (CE1->getOpcode()) {
1172 case Instruction::ICmp:
1173 case Instruction::FCmp:
1179 CE1->getOperand(1));
1183 case Instruction::AShr:
1186 if (CE1->getOpcode() == Instruction::ZExt)
1190 }
else if (isa<ConstantInt>(C1)) {
1196 if (
ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
1197 if (
ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1198 const APInt &C1V = CI1->getValue();
1199 const APInt &C2V = CI2->getValue();
1205 case Instruction::Sub:
1207 case Instruction::Mul:
1209 case Instruction::UDiv:
1210 assert(!CI2->isZero() &&
"Div by zero handled above");
1212 case Instruction::SDiv:
1213 assert(!CI2->isZero() &&
"Div by zero handled above");
1217 case Instruction::URem:
1218 assert(!CI2->isZero() &&
"Div by zero handled above");
1220 case Instruction::SRem:
1221 assert(!CI2->isZero() &&
"Div by zero handled above");
1225 case Instruction::And:
1227 case Instruction::Or:
1229 case Instruction::Xor:
1231 case Instruction::Shl:
1235 case Instruction::LShr:
1239 case Instruction::AShr:
1247 case Instruction::SDiv:
1248 case Instruction::UDiv:
1249 case Instruction::URem:
1250 case Instruction::SRem:
1251 case Instruction::LShr:
1252 case Instruction::AShr:
1253 case Instruction::Shl:
1254 if (CI1->isZero())
return C1;
1259 }
else if (
ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
1260 if (
ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
1261 const APFloat &C1V = CFP1->getValueAPF();
1262 const APFloat &C2V = CFP2->getValueAPF();
1267 case Instruction::FAdd:
1270 case Instruction::FSub:
1273 case Instruction::FMul:
1276 case Instruction::FDiv:
1279 case Instruction::FRem:
1288 for (
unsigned i = 0,
e = VTy->getNumElements(); i !=
e; ++i) {
1312 if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(
T)->
getOpcode() != Opcode)
1315 }
else if (isa<ConstantExpr>(C2)) {
1326 case Instruction::Sub:
1328 case Instruction::Mul:
1330 case Instruction::Shl:
1331 case Instruction::LShr:
1332 case Instruction::AShr:
1336 case Instruction::SDiv:
1337 case Instruction::UDiv:
1341 case Instruction::URem:
1342 case Instruction::SRem:
1358 if (
StructType *STy = dyn_cast<StructType>(Ty)) {
1359 if (STy->isOpaque())
return true;
1362 for (
unsigned i = 0,
e = STy->getNumElements(); i !=
e; ++i)
1366 }
else if (
ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1380 if (C1 == C2)
return 0;
1384 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1388 if (cast<ConstantInt>(C1)->getValue().getActiveBits() > 64 ||
1389 cast<ConstantInt>(C2)->getValue().getActiveBits() > 64)
1394 int64_t C1Val = cast<ConstantInt>(C1)->getSExtValue();
1395 int64_t C2Val = cast<ConstantInt>(C2)->getSExtValue();
1397 if (C1Val == C2Val)
return 0;
1425 "Cannot compare values of different types!");
1431 if (!isa<ConstantExpr>(V1)) {
1432 if (!isa<ConstantExpr>(V2)) {
1461 case Instruction::FPTrunc:
1462 case Instruction::FPExt:
1463 case Instruction::UIToFP:
1464 case Instruction::SIToFP:
1478 auto isGlobalUnsafeForEquality = [](
const GlobalValue *GV) {
1479 if (GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage())
1481 if (
const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
1482 Type *Ty = GVar->getValueType();
1494 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
1495 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
1515 "Cannot compare different types of values!");
1518 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
1519 !isa<BlockAddress>(V1)) {
1520 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(
V2) &&
1521 !isa<BlockAddress>(V2)) {
1548 }
else if (
const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1549 if (isa<ConstantExpr>(V2)) {
1560 if (
const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1562 }
else if (isa<BlockAddress>(V2)) {
1565 assert(isa<ConstantPointerNull>(V2) &&
"Canonicalization guarantee!");
1571 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1573 GV->getType()->getAddressSpace()))
1576 }
else if (
const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1577 if (isa<ConstantExpr>(V2)) {
1588 if (
const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1592 if (BA2->getFunction() != BA->getFunction())
1596 assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
1597 "Canonicalization guarantee!");
1607 case Instruction::Trunc:
1608 case Instruction::FPTrunc:
1609 case Instruction::FPExt:
1610 case Instruction::FPToUI:
1611 case Instruction::FPToSI:
1614 case Instruction::UIToFP:
1615 case Instruction::SIToFP:
1616 case Instruction::BitCast:
1617 case Instruction::ZExt:
1618 case Instruction::SExt:
1620 if (CE1Op0->getType()->isFPOrFPVectorTy())
1626 if (CE1->
getOpcode() == Instruction::ZExt) isSigned =
false;
1627 if (CE1->
getOpcode() == Instruction::SExt) isSigned =
true;
1634 case Instruction::GetElementPtr: {
1638 if (isa<ConstantPointerNull>(V2)) {
1641 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1642 if (GV->hasExternalWeakLinkage())
1650 }
else if (isa<ConstantPointerNull>(CE1Op0)) {
1661 }
else if (
const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1662 if (isa<ConstantPointerNull>(CE1Op0)) {
1663 if (GV2->hasExternalWeakLinkage())
1671 }
else if (
const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1679 "Surprising getelementptr!");
1695 case Instruction::GetElementPtr:
1698 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1700 if (CE1Op0 != CE2Op0) {
1704 cast<GlobalValue>(CE2Op0));
1767 VT->getNumElements());
1779 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1790 if (isIntegerPredicate)
1800 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1802 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
1804 GV->getType()->getAddressSpace())) {
1812 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1814 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
1816 GV->getType()->getAddressSpace())) {
1828 if (isa<ConstantInt>(C2))
1838 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1839 const APInt &V1 = cast<ConstantInt>(C1)->getValue();
1840 const APInt &
V2 = cast<ConstantInt>(C2)->getValue();
1854 }
else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1855 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1856 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1918 (isa<ConstantExpr>(C1) || isa<ConstantExpr>(C2))) {
2059 if (CE2->getOpcode() == Instruction::BitCast &&
2069 if ((CE1->getOpcode() == Instruction::SExt &&
2071 (CE1->getOpcode() == Instruction::ZExt &&
2075 if (CE1Inverse == CE1Op0) {
2085 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
2098 template<
typename IndexTy>
2101 if (Idxs.
empty())
return true;
2104 if (cast<Constant>(Idxs[0])->isNullValue())
return true;
2108 if (
auto *CI = dyn_cast<ConstantInt>(Idxs[0])) {
2112 auto *CV = cast<ConstantDataVector>(Idxs[0]);
2113 CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
2114 if (!CI || !CI->isOne())
2118 for (
unsigned i = 1,
e = Idxs.
size(); i !=
e; ++i)
2119 if (!cast<Constant>(Idxs[i])->isNullValue())
2134 if (IndexVal < 0 || (NumElements > 0 && (uint64_t)IndexVal >= NumElements))
2145 if (Idxs.
empty())
return C;
2150 if (isa<UndefValue>(C))
2153 Constant *Idx0 = cast<Constant>(Idxs[0]);
2154 if (Idxs.
size() == 1 && (Idx0->
isNullValue() || isa<UndefValue>(Idx0)))
2157 cast<VectorType>(GEPTy)->getNumElements(), C)
2162 for (
unsigned i = 0,
e = Idxs.
size(); i !=
e; ++i)
2163 if (!isa<UndefValue>(Idxs[i]) &&
2164 !cast<Constant>(Idxs[i])->isNullValue()) {
2172 assert(Ty &&
"Invalid indices for GEP!");
2180 for (
unsigned i = 0,
e = Idxs.
size(); i !=
e; ++i) {
2181 if (
auto *VT = dyn_cast<VectorType>(Idxs[i]->
getType())) {
2196 if (CE->getOpcode() == Instruction::GetElementPtr) {
2221 bool PerformFold =
false;
2225 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Idx0))
2229 !CE->getOperand(CE->getNumOperands() - 1)
2235 NewIndices.
reserve(Idxs.
size() + CE->getNumOperands());
2236 NewIndices.
append(CE->op_begin() + 1, CE->op_end() - 1);
2240 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
2244 if (IdxTy != Idx0->
getType()) {
2245 unsigned CommonExtendedWidth =
2248 CommonExtendedWidth =
std::max(CommonExtendedWidth, 64U);
2268 if (IRIndex && *IRIndex == CE->getNumOperands() - 2 && !Idx0->
isNullValue())
2272 cast<GEPOperator>(CE)->getSourceElementType(), CE->getOperand(0),
2273 NewIndices, InBounds && cast<GEPOperator>(CE)->isInBounds(),
2291 if (SrcPtrTy && DstPtrTy) {
2296 if (SrcArrayTy && DstArrayTy
2301 Idxs, InBounds, InRangeIndex);
2310 Type *Ty = PointeeTy;
2313 !isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]);
2314 for (
unsigned i = 1,
e = Idxs.
size(); i !=
e;
2315 Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
2316 if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) {
2321 if (!isa<ConstantInt>(Idxs[i - 1]) && !isa<ConstantDataVector>(Idxs[i - 1]))
2324 if (InRangeIndex && i == *InRangeIndex + 1) {
2330 if (isa<StructType>(Ty)) {
2334 auto *STy = cast<SequentialType>(Ty);
2335 if (isa<VectorType>(STy)) {
2340 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
2344 if (CI->getSExtValue() < 0) {
2350 auto *CV = cast<ConstantDataVector>(Idxs[i]);
2352 for (
unsigned I = 0,
E = CV->getNumElements();
I !=
E; ++
I) {
2353 auto *CI = cast<ConstantInt>(CV->getElementAsConstant(
I));
2355 if (CI->getSExtValue() < 0) {
2360 if (InRange || Unknown)
2365 if (isa<StructType>(Prev)) {
2375 uint64_t NumElements = STy->getArrayNumElements();
2379 Constant *CurrIdx = cast<Constant>(Idxs[i]);
2381 NewIdxs[i - 1] ? NewIdxs[i - 1] : cast<Constant>(Idxs[i - 1]);
2383 bool IsPrevIdxVector = PrevIdx->getType()->isVectorTy();
2384 bool UseVector = IsCurrIdxVector || IsPrevIdxVector;
2386 if (!IsCurrIdxVector && IsPrevIdxVector)
2388 PrevIdx->getType()->getVectorNumElements(), CurrIdx);
2390 if (!IsPrevIdxVector && IsCurrIdxVector)
2398 IsPrevIdxVector ? PrevIdx->getType()->getVectorNumElements()
2406 unsigned CommonExtendedWidth =
2407 std::max(PrevIdx->getType()->getScalarSizeInBits(),
2409 CommonExtendedWidth =
std::max(CommonExtendedWidth, 64U);
2416 ExtendedTy, IsPrevIdxVector
2417 ? PrevIdx->getType()->getVectorNumElements()
2420 if (!PrevIdx->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
2430 if (!NewIdxs.
empty()) {
2431 for (
unsigned i = 0,
e = Idxs.
size(); i !=
e; ++i)
2432 if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
2439 if (!Unknown && !InBounds)
2440 if (
auto *GV = dyn_cast<GlobalVariable>(C))
2443 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 greather 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.
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.
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 greather 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.
static Type * getGEPReturnType(Value *Ptr, ArrayRef< Value *> IdxList)
Returns the pointer type returned by the GEP instruction, which may be a vector of pointers...
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 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.