50 assert(
Op &&
Op->isCast() &&
"Can't fold cast of cast without a cast!");
52 assert(CastInst::isCast(opc) &&
"Invalid cast opcode");
55 Type *SrcTy =
Op->getOperand(0)->getType();
56 Type *MidTy =
Op->getType();
67 nullptr, FakeIntPtrTy,
nullptr);
71 Type *SrcTy = V->getType();
77 if (
VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
78 if (V->isAllOnesValue())
84 if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
109 if (
FP->getType()->isPPC_FP128Ty())
117 FP->getValueAPF().bitcastToAPInt());
134 assert(
C->getType()->isIntegerTy() &&
135 (cast<IntegerType>(
C->getType())->getBitWidth() & 7) == 0 &&
136 "Non-byte sized integer input");
137 [[maybe_unused]]
unsigned CSize = cast<IntegerType>(
C->getType())->getBitWidth()/8;
138 assert(ByteSize &&
"Must be accessing some piece");
139 assert(ByteStart+ByteSize <= CSize &&
"Extracting invalid piece from input");
140 assert(ByteSize != CSize &&
"Should not extract everything");
144 APInt V = CI->getValue();
146 V.lshrInPlace(ByteStart*8);
147 V = V.trunc(ByteSize*8);
154 if (!CE)
return nullptr;
156 switch (CE->getOpcode()) {
157 default:
return nullptr;
158 case Instruction::Shl: {
159 ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
164 if ((ShAmt & 7) != 0)
169 if (ShAmt.
uge(ByteStart + ByteSize))
173 if (ShAmt.
ule(ByteStart))
192 if (isa<PoisonValue>(V))
195 if (isa<UndefValue>(V)) {
199 if (opc == Instruction::ZExt || opc == Instruction::SExt ||
200 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
206 opc != Instruction::AddrSpaceCast)
222 if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
224 cast<FixedVectorType>(DestTy)->getNumElements() ==
225 cast<FixedVectorType>(V->getType())->getNumElements()) {
226 VectorType *DestVecTy = cast<VectorType>(DestTy);
234 cast<VectorType>(DestTy)->getElementCount(), Res);
239 e = cast<FixedVectorType>(V->getType())->getNumElements();
255 case Instruction::FPTrunc:
256 case Instruction::FPExt:
257 if (
ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
259 APFloat Val = FPC->getValueAPF();
265 case Instruction::FPToUI:
266 case Instruction::FPToSI:
267 if (
ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
268 const APFloat &V = FPC->getValueAPF();
270 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
271 APSInt IntVal(DestBitWidth, opc == Instruction::FPToUI);
272 if (APFloat::opInvalidOp ==
273 V.convertToInteger(IntVal, APFloat::rmTowardZero, &
ignored)) {
281 case Instruction::UIToFP:
282 case Instruction::SIToFP:
284 const APInt &api = CI->getValue();
288 APFloat::rmNearestTiesToEven);
292 case Instruction::ZExt:
299 case Instruction::SExt:
306 case Instruction::Trunc: {
307 if (V->getType()->isVectorTy())
310 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
313 CI->getValue().trunc(DestBitWidth));
319 if ((DestBitWidth & 7) == 0 &&
320 (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
326 case Instruction::BitCast:
328 case Instruction::AddrSpaceCast:
329 case Instruction::IntToPtr:
330 case Instruction::PtrToInt:
338 if (
Cond->isNullValue())
return V2;
339 if (
Cond->isAllOnesValue())
return V1;
343 auto *V1VTy = CondV->
getType();
346 for (
unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i) {
352 auto *
Cond = cast<Constant>(CondV->getOperand(i));
353 if (isa<PoisonValue>(
Cond)) {
355 }
else if (V1Element == V2Element) {
357 }
else if (isa<UndefValue>(
Cond)) {
358 V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
360 if (!isa<ConstantInt>(
Cond))
break;
361 V =
Cond->isNullValue() ? V2Element : V1Element;
367 if (Result.size() == V1VTy->getNumElements())
371 if (isa<PoisonValue>(
Cond))
374 if (isa<UndefValue>(
Cond)) {
375 if (isa<UndefValue>(V1))
return V1;
379 if (V1 == V2)
return V1;
381 if (isa<PoisonValue>(V1))
383 if (isa<PoisonValue>(V2))
389 if (isa<PoisonValue>(
C))
394 if (isa<ConstantExpr>(
C))
397 if (isa<ConstantInt>(
C) || isa<GlobalVariable>(
C) || isa<ConstantFP>(
C) ||
398 isa<ConstantPointerNull>(
C) || isa<Function>(
C))
401 if (
C->getType()->isVectorTy())
402 return !
C->containsPoisonElement() && !
C->containsConstantExpression();
407 if (isa<UndefValue>(V1) && NotPoison(V2))
return V2;
408 if (isa<UndefValue>(V2) && NotPoison(V1))
return V1;
415 auto *ValVTy = cast<VectorType>(Val->
getType());
419 if (isa<PoisonValue>(Val) || isa<UndefValue>(
Idx))
423 if (isa<UndefValue>(Val))
426 auto *CIdx = dyn_cast<ConstantInt>(
Idx);
430 if (
auto *ValFVTy = dyn_cast<FixedVectorType>(Val->
getType())) {
432 if (CIdx->uge(ValFVTy->getNumElements()))
437 if (
auto *CE = dyn_cast<ConstantExpr>(Val)) {
438 if (
auto *
GEP = dyn_cast<GEPOperator>(CE)) {
440 Ops.
reserve(CE->getNumOperands());
441 for (
unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
443 if (
Op->getType()->isVectorTy()) {
451 return CE->getWithOperands(Ops, ValVTy->getElementType(),
false,
452 GEP->getSourceElementType());
453 }
else if (CE->getOpcode() == Instruction::InsertElement) {
454 if (
const auto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) {
456 APSInt(CIdx->getValue()))) {
457 return CE->getOperand(1);
469 if (CIdx->getValue().ult(ValVTy->getElementCount().getKnownMinValue())) {
480 if (isa<UndefValue>(
Idx))
485 if (isa<ConstantAggregateZero>(Val) && Elt->
isNullValue())
489 if (!CIdx)
return nullptr;
493 if (isa<ScalableVectorType>(Val->
getType()))
496 auto *ValTy = cast<FixedVectorType>(Val->
getType());
498 unsigned NumElts = ValTy->getNumElements();
499 if (CIdx->
uge(NumElts))
503 Result.reserve(NumElts);
506 for (
unsigned i = 0; i != NumElts; ++i) {
508 Result.push_back(Elt);
521 auto *V1VTy = cast<VectorType>(V1->
getType());
522 unsigned MaskNumElts = Mask.size();
525 Type *EltTy = V1VTy->getElementType();
534 if (
all_of(Mask, [](
int Elt) {
return Elt == 0; })) {
540 auto *VTy = VectorType::get(EltTy, MaskEltCount);
542 }
else if (!MaskEltCount.isScalable())
547 if (isa<ScalableVectorType>(V1VTy))
550 unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue();
554 for (
unsigned i = 0; i != MaskNumElts; ++i) {
561 if (
unsigned(Elt) >= SrcNumElts*2)
563 else if (
unsigned(Elt) >= SrcNumElts) {
572 Result.push_back(InElt);
599 NumElts = ST->getNumElements();
601 NumElts = cast<ArrayType>(Agg->
getType())->getNumElements();
604 for (
unsigned i = 0; i != NumElts; ++i) {
606 if (!
C)
return nullptr;
624 bool IsScalableVector = isa<ScalableVectorType>(
C->getType());
625 bool HasScalarUndefOrScalableVectorUndef =
626 (!
C->getType()->isVectorTy() || IsScalableVector) && isa<UndefValue>(
C);
628 if (HasScalarUndefOrScalableVectorUndef) {
630 case Instruction::FNeg:
632 case Instruction::UnaryOpsEnd:
638 assert(!HasScalarUndefOrScalableVectorUndef &&
"Unexpected UndefValue");
640 assert(!isa<ConstantInt>(
C) &&
"Unexpected Integer UnaryOp");
643 const APFloat &CV = CFP->getValueAPF();
647 case Instruction::FNeg:
650 }
else if (
auto *VTy = dyn_cast<FixedVectorType>(
C->getType())) {
660 for (
unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
666 Result.push_back(Res);
694 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
699 bool IsScalableVector = isa<ScalableVectorType>(C1->
getType());
700 bool HasScalarUndefOrScalableVectorUndef =
702 (isa<UndefValue>(C1) || isa<UndefValue>(C2));
703 if (HasScalarUndefOrScalableVectorUndef) {
705 case Instruction::Xor:
706 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
711 case Instruction::Add:
712 case Instruction::Sub:
714 case Instruction::And:
715 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
718 case Instruction::Mul: {
720 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
731 case Instruction::SDiv:
732 case Instruction::UDiv:
742 case Instruction::URem:
743 case Instruction::SRem:
750 case Instruction::Or:
751 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
754 case Instruction::LShr:
756 if (isa<UndefValue>(C2))
763 case Instruction::AShr:
765 if (isa<UndefValue>(C2))
773 case Instruction::Shl:
775 if (isa<UndefValue>(C2))
782 case Instruction::FSub:
787 case Instruction::FAdd:
788 case Instruction::FMul:
789 case Instruction::FDiv:
790 case Instruction::FRem:
792 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
802 case Instruction::BinaryOpsEnd:
808 assert((!HasScalarUndefOrScalableVectorUndef) &&
"Unexpected UndefValue");
811 if (
ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
813 case Instruction::Add:
814 if (CI2->isZero())
return C1;
816 case Instruction::Sub:
817 if (CI2->isZero())
return C1;
819 case Instruction::Mul:
820 if (CI2->isZero())
return C2;
824 case Instruction::UDiv:
825 case Instruction::SDiv:
831 case Instruction::URem:
832 case Instruction::SRem:
838 case Instruction::And:
839 if (CI2->isZero())
return C2;
840 if (CI2->isMinusOne())
845 if (CE1->getOpcode() == Instruction::PtrToInt &&
846 isa<GlobalValue>(CE1->getOperand(0))) {
847 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
864 if (isa<Function>(GV) && !
DL.getFunctionPtrAlign())
866 }
else if (isa<GlobalVariable>(GV)) {
867 GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne();
871 unsigned DstWidth = CI2->getType()->getBitWidth();
872 unsigned SrcWidth = std::min(DstWidth,
Log2(GVAlign));
876 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
882 case Instruction::Or:
883 if (CI2->isZero())
return C1;
884 if (CI2->isMinusOne())
887 case Instruction::Xor:
888 if (CI2->isZero())
return C1;
891 switch (CE1->getOpcode()) {
893 case Instruction::ICmp:
894 case Instruction::FCmp:
905 }
else if (isa<ConstantInt>(C1)) {
913 if (
ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
914 if (
ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
915 const APInt &C1V = CI1->getValue();
916 const APInt &C2V = CI2->getValue();
920 case Instruction::Add:
922 case Instruction::Sub:
924 case Instruction::Mul:
926 case Instruction::UDiv:
927 assert(!CI2->isZero() &&
"Div by zero handled above");
929 case Instruction::SDiv:
930 assert(!CI2->isZero() &&
"Div by zero handled above");
934 case Instruction::URem:
935 assert(!CI2->isZero() &&
"Div by zero handled above");
937 case Instruction::SRem:
938 assert(!CI2->isZero() &&
"Div by zero handled above");
942 case Instruction::And:
944 case Instruction::Or:
946 case Instruction::Xor:
948 case Instruction::Shl:
952 case Instruction::LShr:
956 case Instruction::AShr:
964 case Instruction::SDiv:
965 case Instruction::UDiv:
966 case Instruction::URem:
967 case Instruction::SRem:
968 case Instruction::LShr:
969 case Instruction::AShr:
970 case Instruction::Shl:
971 if (CI1->isZero())
return C1;
976 }
else if (
ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
977 if (
ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
978 const APFloat &C1V = CFP1->getValueAPF();
979 const APFloat &C2V = CFP2->getValueAPF();
984 case Instruction::FAdd:
985 (void)C3V.
add(C2V, APFloat::rmNearestTiesToEven);
987 case Instruction::FSub:
988 (void)C3V.
subtract(C2V, APFloat::rmNearestTiesToEven);
990 case Instruction::FMul:
991 (void)C3V.
multiply(C2V, APFloat::rmNearestTiesToEven);
993 case Instruction::FDiv:
994 (void)C3V.
divide(C2V, APFloat::rmNearestTiesToEven);
996 case Instruction::FRem:
1001 }
else if (
auto *VTy = dyn_cast<VectorType>(C1->
getType())) {
1017 if (
auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
1021 for (
unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
1035 Result.push_back(Res);
1054 }
else if (isa<ConstantExpr>(C2)) {
1064 case Instruction::Add:
1065 case Instruction::Sub:
1067 case Instruction::Shl:
1068 case Instruction::LShr:
1069 case Instruction::AShr:
1073 case Instruction::SDiv:
1074 case Instruction::UDiv:
1078 case Instruction::URem:
1079 case Instruction::SRem:
1094 auto isGlobalUnsafeForEquality = [](
const GlobalValue *GV) {
1095 if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
1097 if (
const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
1098 Type *Ty = GVar->getValueType();
1110 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
1111 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
1112 return ICmpInst::ICMP_NE;
1113 return ICmpInst::BAD_ICMP_PREDICATE;
1124 "Cannot compare different types of values!");
1125 if (V1 == V2)
return ICmpInst::ICMP_EQ;
1129 return ICmpInst::BAD_ICMP_PREDICATE;
1135 auto GetComplexity = [](
Constant *V) {
1136 if (isa<ConstantExpr>(V))
1138 if (isa<GlobalValue>(V))
1140 if (isa<BlockAddress>(V))
1144 if (GetComplexity(V1) < GetComplexity(V2)) {
1146 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1147 return ICmpInst::getSwappedPredicate(SwappedRelation);
1148 return ICmpInst::BAD_ICMP_PREDICATE;
1151 if (
const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1153 if (
const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1157 if (BA2->getFunction() != BA->getFunction())
1158 return ICmpInst::ICMP_NE;
1159 }
else if (isa<ConstantPointerNull>(V2)) {
1160 return ICmpInst::ICMP_NE;
1162 }
else if (
const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1165 if (
const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1167 }
else if (isa<BlockAddress>(V2)) {
1168 return ICmpInst::ICMP_NE;
1169 }
else if (isa<ConstantPointerNull>(V2)) {
1175 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1177 GV->getType()->getAddressSpace()))
1178 return ICmpInst::ICMP_UGT;
1187 case Instruction::GetElementPtr: {
1191 if (isa<ConstantPointerNull>(V2)) {
1194 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1197 if (!GV->hasExternalWeakLinkage() && CE1GEP->
isInBounds())
1198 return ICmpInst::ICMP_UGT;
1200 }
else if (
const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1201 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1205 return ICmpInst::BAD_ICMP_PREDICATE;
1208 }
else if (
const auto *CE2GEP = dyn_cast<GEPOperator>(V2)) {
1211 const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand());
1212 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1214 if (CE1Op0 != CE2Op0) {
1217 cast<GlobalValue>(CE2Op0));
1218 return ICmpInst::BAD_ICMP_PREDICATE;
1229 return ICmpInst::BAD_ICMP_PREDICATE;
1237 VT->getElementCount());
1242 if (Predicate == FCmpInst::FCMP_FALSE)
1245 if (Predicate == FCmpInst::FCMP_TRUE)
1249 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1252 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1253 bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1262 if (isIntegerPredicate)
1274 if (Predicate == ICmpInst::ICMP_UGE)
1277 if (Predicate == ICmpInst::ICMP_ULT)
1283 switch (Predicate) {
1284 case ICmpInst::ICMP_EQ:
1285 if (isa<ConstantInt>(C2))
1288 case ICmpInst::ICMP_NE:
1295 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1296 const APInt &V1 = cast<ConstantInt>(C1)->getValue();
1297 const APInt &V2 = cast<ConstantInt>(C2)->getValue();
1299 }
else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1300 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1301 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1303 }
else if (
auto *C1VTy = dyn_cast<VectorType>(C1->
getType())) {
1309 C1VTy->getElementCount(),
1314 if (isa<ScalableVectorType>(C1VTy))
1322 for (
unsigned I = 0,
E = C1VTy->getElementCount().getKnownMinValue();
1338 if (Predicate == FCmpInst::FCMP_ONE)
1340 else if (Predicate == FCmpInst::FCMP_UEQ)
1348 case ICmpInst::BAD_ICMP_PREDICATE:
1350 case ICmpInst::ICMP_EQ:
1353 Result = ICmpInst::isTrueWhenEqual(Predicate);
1355 case ICmpInst::ICMP_ULT:
1356 switch (Predicate) {
1357 case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_ULE:
1359 case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_UGE:
1365 case ICmpInst::ICMP_SLT:
1366 switch (Predicate) {
1367 case ICmpInst::ICMP_SLT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_SLE:
1369 case ICmpInst::ICMP_SGT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_SGE:
1375 case ICmpInst::ICMP_UGT:
1376 switch (Predicate) {
1377 case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_UGE:
1379 case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_ULE:
1385 case ICmpInst::ICMP_SGT:
1386 switch (Predicate) {
1387 case ICmpInst::ICMP_SGT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_SGE:
1389 case ICmpInst::ICMP_SLT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_SLE:
1395 case ICmpInst::ICMP_ULE:
1396 if (Predicate == ICmpInst::ICMP_UGT)
1398 if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE)
1401 case ICmpInst::ICMP_SLE:
1402 if (Predicate == ICmpInst::ICMP_SGT)
1404 if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE)
1407 case ICmpInst::ICMP_UGE:
1408 if (Predicate == ICmpInst::ICMP_ULT)
1410 if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE)
1413 case ICmpInst::ICMP_SGE:
1414 if (Predicate == ICmpInst::ICMP_SLT)
1416 if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE)
1419 case ICmpInst::ICMP_NE:
1420 if (Predicate == ICmpInst::ICMP_EQ)
1422 if (Predicate == ICmpInst::ICMP_NE)
1431 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
1436 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1444template<
typename IndexTy>
1447 if (Idxs.
empty())
return true;
1450 if (cast<Constant>(Idxs[0])->isNullValue())
return true;
1454 if (
auto *CI = dyn_cast<ConstantInt>(Idxs[0])) {
1458 auto *CV = cast<ConstantDataVector>(Idxs[0]);
1459 CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
1460 if (!CI || !CI->isOne())
1464 for (
unsigned i = 1, e = Idxs.
size(); i != e; ++i)
1465 if (!cast<Constant>(Idxs[i])->isNullValue())
1480 if (IndexVal < 0 || (IndexVal != 0 && (
uint64_t)IndexVal >= NumElements))
1492 if (PointeeTy !=
GEP->getResultElementType())
1495 Constant *Idx0 = cast<Constant>(Idxs[0]);
1503 GEP->getSourceElementType(), cast<Constant>(
GEP->getPointerOperand()),
1504 NewIndices, InBounds &&
GEP->isInBounds(),
GEP->getInRangeIndex());
1522 auto *LastIdx = cast<Constant>(
GEP->getOperand(
GEP->getNumOperands()-1));
1523 Type *LastIdxTy = LastIdx->getType();
1529 NewIndices.
append(
GEP->idx_begin(),
GEP->idx_end() - 1);
1533 if (LastIdxTy != Idx0->
getType()) {
1534 unsigned CommonExtendedWidth =
1537 CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
1541 if (Idx0->
getType() != CommonTy)
1543 if (LastIdx->getType() != CommonTy)
1546 if (!Idx0 || !LastIdx)
1556 std::optional<unsigned> IRIndex =
GEP->getInRangeIndex();
1557 if (IRIndex && *IRIndex ==
GEP->getNumIndices() - 1)
1558 IRIndex = std::nullopt;
1561 GEP->getSourceElementType(), cast<Constant>(
GEP->getPointerOperand()),
1562 NewIndices, InBounds &&
GEP->isInBounds(), IRIndex);
1567 std::optional<unsigned> InRangeIndex,
1569 if (Idxs.
empty())
return C;
1574 if (isa<PoisonValue>(
C))
1577 if (isa<UndefValue>(
C))
1581 auto IsNoOp = [&]() {
1588 return IdxC->
isNullValue() || isa<UndefValue>(IdxC);
1592 return GEPTy->
isVectorTy() && !
C->getType()->isVectorTy()
1594 cast<VectorType>(GEPTy)->getElementCount(),
C)
1598 if (
auto *
GEP = dyn_cast<GEPOperator>(CE))
1606 Type *Ty = PointeeTy;
1607 Type *Prev =
C->getType();
1610 !isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]);
1611 for (
unsigned i = 1, e = Idxs.
size(); i != e;
1612 Prev = Ty, Ty = (++GEPIter).getIndexedType(), ++i) {
1613 if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) {
1618 if (!isa<ConstantInt>(Idxs[i - 1]) && !isa<ConstantDataVector>(Idxs[i - 1]))
1621 if (InRangeIndex && i == *InRangeIndex + 1) {
1627 if (isa<StructType>(Ty)) {
1631 if (isa<VectorType>(Ty)) {
1636 auto *STy = cast<ArrayType>(Ty);
1637 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
1641 if (CI->isNegative()) {
1647 auto *CV = cast<ConstantDataVector>(Idxs[i]);
1649 for (
unsigned I = 0,
E = CV->getNumElements();
I !=
E; ++
I) {
1650 auto *CI = cast<ConstantInt>(CV->getElementAsConstant(
I));
1652 if (CI->isNegative()) {
1662 if (isa<StructType>(Prev)) {
1670 uint64_t NumElements = STy->getArrayNumElements();
1682 Constant *CurrIdx = cast<Constant>(Idxs[i]);
1684 NewIdxs[i - 1] ? NewIdxs[i - 1] : cast<Constant>(Idxs[i - 1]);
1686 bool IsPrevIdxVector = PrevIdx->getType()->isVectorTy();
1687 bool UseVector = IsCurrIdxVector || IsPrevIdxVector;
1689 if (!IsCurrIdxVector && IsPrevIdxVector)
1691 cast<FixedVectorType>(PrevIdx->getType())->getNumElements(), CurrIdx);
1693 if (!IsPrevIdxVector && IsCurrIdxVector)
1695 cast<FixedVectorType>(CurrIdx->
getType())->getNumElements(), PrevIdx);
1702 ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
1703 : cast<FixedVectorType>(CurrIdx->
getType())->getNumElements(),
1714 assert(NewIdxs[i] !=
nullptr && Div !=
nullptr &&
"Should have folded");
1716 unsigned CommonExtendedWidth =
1717 std::max(PrevIdx->getType()->getScalarSizeInBits(),
1719 CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
1728 ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
1729 : cast<FixedVectorType>(CurrIdx->
getType())->getNumElements());
1731 if (!PrevIdx->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
1738 assert(PrevIdx && Div &&
"Should have folded");
1743 if (!NewIdxs.
empty()) {
1744 for (
unsigned i = 0, e = Idxs.
size(); i != e; ++i)
1745 if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
1753 if (
auto *GV = dyn_cast<GlobalVariable>(
C))
1754 if (!GV->hasExternalWeakLinkage() && GV->getValueType() == PointeeTy &&
1757 true, InRangeIndex);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static unsigned foldConstantCastPair(unsigned opc, ConstantExpr *Op, Type *DstTy)
This function determines which opcode to use to fold two constant cast expressions together.
static Constant * foldMaybeUndesirableCast(unsigned opc, Constant *V, Type *DestTy)
static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, const GlobalValue *GV2)
static Constant * FoldBitCast(Constant *V, Type *DestTy)
static bool isIndexInRangeOfArrayType(uint64_t NumElements, const ConstantInt *CI)
Test whether a given ConstantInt is in-range for a SequentialType.
static Constant * foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds, ArrayRef< Value * > Idxs)
static bool isInBoundsIndices(ArrayRef< IndexTy > Idxs)
Test whether the given sequence of normalized indices is "inbounds".
static Constant * ExtractConstantBytes(Constant *C, unsigned ByteStart, unsigned ByteSize)
V is an integer constant which only has a subset of its bytes used.
static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2)
This function determines if there is anything we can decide about the two constants provided.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
Module.h This file contains the declarations for the Module class.
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
static constexpr uint32_t Opcode
opStatus divide(const APFloat &RHS, roundingMode RM)
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
opStatus subtract(const APFloat &RHS, roundingMode RM)
opStatus add(const APFloat &RHS, roundingMode RM)
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
opStatus multiply(const APFloat &RHS, roundingMode RM)
opStatus mod(const APFloat &RHS)
Class for arbitrary precision integers.
APInt udiv(const APInt &RHS) const
Unsigned division operation.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
uint64_t getZExtValue() const
Get zero extended value.
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
unsigned getBitWidth() const
Return the number of bits in the APInt.
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
unsigned getSignificantBits() const
Get the minimum bit size for this signed APInt.
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
APInt shl(unsigned shiftAmt) const
Left-shift function.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
An arbitrary precision integer that knows its signedness.
static bool isSameValue(const APSInt &I1, const APSInt &I2)
Determine if two APSInts have the same value, zero- or sign-extending as needed.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
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.
The address of a basic block.
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.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
bool isTrueWhenEqual() const
This is just a convenience.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
static ConstantAggregateZero * get(Type *Ty)
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
A constant value that is initialized with an expression using other constant values.
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 * getNot(Constant *C)
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 Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, std::optional< unsigned > InRangeIndex=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
static Constant * getXor(Constant *C1, Constant *C2)
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.
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
ConstantFP - Floating Point Values [float, double].
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...
static Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
This is the shared class of boolean and integer constants.
static ConstantInt * getTrue(LLVMContext &Context)
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 ConstantInt * getFalse(LLVMContext &Context)
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
const APInt & getValue() const
Return the constant as an APInt value reference.
bool uge(uint64_t Num) const
This function will return true iff this constant represents a value with active bits bigger than 64 b...
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Constant Vector Declarations.
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 AllowUndefs=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.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
static bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
static Type * getGEPReturnType(Value *Ptr, ArrayRef< Value * > IdxList)
Returns the pointer type returned by the GEP instruction, which may be a vector of pointers.
Module * getParent()
Get the module that this global value is contained inside of...
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isEquality() const
Return true if this predicate is either EQ or NE.
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
Class to represent integer types.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
A Module instance is used to store all the information related to an LLVM module.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Class to represent struct types.
The instances of the Type class are immutable: once they are created, they are never changed.
unsigned getIntegerBitWidth() 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 isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
bool isX86_MMXTy() const
Return true if this is X86 MMX.
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
bool isFloatingPointTy() const
Return true if this is one of the floating-point 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.
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 * 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.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
LLVMContext & getContext() const
All values hold a context through their type.
Base class of all SIMD vector types.
Type * getElementType() const
bool isSequential() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
bool match(Val *V, const Pattern &P)
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
auto m_Undef()
Match an arbitrary undef constant.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
This is an optimization pass for GlobalISel generic memory operations.
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 * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, bool InBounds, std::optional< unsigned > InRangeIndex, ArrayRef< Value * > Idxs)
Constant * ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2)
Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
gep_type_iterator gep_type_end(const User *GEP)
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
constexpr int PoisonMaskElem
Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
constexpr unsigned BitWidth
gep_type_iterator gep_type_begin(const User *GEP)
APFloat neg(APFloat X)
Returns the negated value of the argument.
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...
unsigned Log2(Align A)
Returns the log2 of the alignment.
Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, ArrayRef< int > Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and mask.
Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
This struct is a compact representation of a valid (non-zero power of two) alignment.