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())
116 return ConstantInt::get(
FP->getContext(),
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);
148 return ConstantInt::get(CI->getContext(), V);
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();
262 return ConstantFP::get(V->getContext(), Val);
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)) {
278 return ConstantInt::get(FPC->getContext(), IntVal);
281 case Instruction::UIToFP:
282 case Instruction::SIToFP:
284 const APInt &api = CI->getValue();
288 APFloat::rmNearestTiesToEven);
289 return ConstantFP::get(V->getContext(), apf);
292 case Instruction::ZExt:
295 return ConstantInt::get(V->getContext(),
299 case Instruction::SExt:
302 return ConstantInt::get(V->getContext(),
306 case Instruction::Trunc: {
307 if (V->getType()->isVectorTy())
310 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
312 return ConstantInt::get(V->getContext(),
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) {
349 ConstantInt::get(Ty, i));
351 ConstantInt::get(Ty, 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) {
567 ConstantInt::get(Ty, 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:
648 return ConstantFP::get(
C->getContext(),
neg(CV));
650 }
else if (
auto *VTy = dyn_cast<FixedVectorType>(
C->getType())) {
660 for (
unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
661 Constant *ExtractIdx = ConstantInt::get(Ty, i);
666 Result.push_back(Res);
683 Opcode, C1->
getType(),
false)) {
689 Opcode, C1->
getType(),
true)) {
695 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
700 bool IsScalableVector = isa<ScalableVectorType>(C1->
getType());
701 bool HasScalarUndefOrScalableVectorUndef =
703 (isa<UndefValue>(C1) || isa<UndefValue>(C2));
704 if (HasScalarUndefOrScalableVectorUndef) {
706 case Instruction::Xor:
707 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
712 case Instruction::Add:
713 case Instruction::Sub:
715 case Instruction::And:
716 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
719 case Instruction::Mul: {
721 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
732 case Instruction::SDiv:
733 case Instruction::UDiv:
740 case Instruction::URem:
741 case Instruction::SRem:
748 case Instruction::Or:
749 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
752 case Instruction::LShr:
754 if (isa<UndefValue>(C2))
758 case Instruction::AShr:
760 if (isa<UndefValue>(C2))
765 case Instruction::Shl:
767 if (isa<UndefValue>(C2))
771 case Instruction::FSub:
776 case Instruction::FAdd:
777 case Instruction::FMul:
778 case Instruction::FDiv:
779 case Instruction::FRem:
781 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
791 case Instruction::BinaryOpsEnd:
797 assert((!HasScalarUndefOrScalableVectorUndef) &&
"Unexpected UndefValue");
800 if (
ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
802 case Instruction::Mul:
806 case Instruction::UDiv:
807 case Instruction::SDiv:
811 case Instruction::URem:
812 case Instruction::SRem:
818 case Instruction::And:
824 if (CE1->getOpcode() == Instruction::PtrToInt &&
825 isa<GlobalValue>(CE1->getOperand(0))) {
826 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
843 if (isa<Function>(GV) && !
DL.getFunctionPtrAlign())
845 }
else if (isa<GlobalVariable>(GV)) {
846 GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne();
850 unsigned DstWidth = CI2->getBitWidth();
851 unsigned SrcWidth = std::min(DstWidth,
Log2(GVAlign));
855 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
861 case Instruction::Or:
862 if (CI2->isMinusOne())
865 case Instruction::Xor:
867 switch (CE1->getOpcode()) {
870 case Instruction::ICmp:
871 case Instruction::FCmp:
882 }
else if (isa<ConstantInt>(C1)) {
890 if (
ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
891 if (
ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
892 const APInt &C1V = CI1->getValue();
893 const APInt &C2V = CI2->getValue();
897 case Instruction::Add:
898 return ConstantInt::get(CI1->getContext(), C1V + C2V);
899 case Instruction::Sub:
900 return ConstantInt::get(CI1->getContext(), C1V - C2V);
901 case Instruction::Mul:
902 return ConstantInt::get(CI1->getContext(), C1V * C2V);
903 case Instruction::UDiv:
904 assert(!CI2->isZero() &&
"Div by zero handled above");
905 return ConstantInt::get(CI1->getContext(), C1V.
udiv(C2V));
906 case Instruction::SDiv:
907 assert(!CI2->isZero() &&
"Div by zero handled above");
910 return ConstantInt::get(CI1->getContext(), C1V.
sdiv(C2V));
911 case Instruction::URem:
912 assert(!CI2->isZero() &&
"Div by zero handled above");
913 return ConstantInt::get(CI1->getContext(), C1V.
urem(C2V));
914 case Instruction::SRem:
915 assert(!CI2->isZero() &&
"Div by zero handled above");
918 return ConstantInt::get(CI1->getContext(), C1V.
srem(C2V));
919 case Instruction::And:
920 return ConstantInt::get(CI1->getContext(), C1V & C2V);
921 case Instruction::Or:
922 return ConstantInt::get(CI1->getContext(), C1V | C2V);
923 case Instruction::Xor:
924 return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
925 case Instruction::Shl:
927 return ConstantInt::get(CI1->getContext(), C1V.
shl(C2V));
929 case Instruction::LShr:
931 return ConstantInt::get(CI1->getContext(), C1V.
lshr(C2V));
933 case Instruction::AShr:
935 return ConstantInt::get(CI1->getContext(), C1V.
ashr(C2V));
941 case Instruction::SDiv:
942 case Instruction::UDiv:
943 case Instruction::URem:
944 case Instruction::SRem:
945 case Instruction::LShr:
946 case Instruction::AShr:
947 case Instruction::Shl:
948 if (CI1->isZero())
return C1;
953 }
else if (
ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
954 if (
ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
955 const APFloat &C1V = CFP1->getValueAPF();
956 const APFloat &C2V = CFP2->getValueAPF();
961 case Instruction::FAdd:
962 (void)C3V.
add(C2V, APFloat::rmNearestTiesToEven);
963 return ConstantFP::get(C1->
getContext(), C3V);
964 case Instruction::FSub:
965 (void)C3V.
subtract(C2V, APFloat::rmNearestTiesToEven);
966 return ConstantFP::get(C1->
getContext(), C3V);
967 case Instruction::FMul:
968 (void)C3V.
multiply(C2V, APFloat::rmNearestTiesToEven);
969 return ConstantFP::get(C1->
getContext(), C3V);
970 case Instruction::FDiv:
971 (void)C3V.
divide(C2V, APFloat::rmNearestTiesToEven);
972 return ConstantFP::get(C1->
getContext(), C3V);
973 case Instruction::FRem:
975 return ConstantFP::get(C1->
getContext(), C3V);
978 }
else if (
auto *VTy = dyn_cast<VectorType>(C1->
getType())) {
994 if (
auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
998 for (
unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
999 Constant *ExtractIdx = ConstantInt::get(Ty, i);
1012 Result.push_back(Res);
1028 if (!isa<ConstantExpr>(
T) || cast<ConstantExpr>(
T)->
getOpcode() != Opcode)
1031 }
else if (isa<ConstantExpr>(C2)) {
1041 case Instruction::Add:
1042 case Instruction::Sub:
1044 case Instruction::Shl:
1045 case Instruction::LShr:
1046 case Instruction::AShr:
1050 case Instruction::SDiv:
1051 case Instruction::UDiv:
1055 case Instruction::URem:
1056 case Instruction::SRem:
1071 auto isGlobalUnsafeForEquality = [](
const GlobalValue *GV) {
1072 if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
1074 if (
const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
1075 Type *Ty = GVar->getValueType();
1087 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
1088 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
1089 return ICmpInst::ICMP_NE;
1090 return ICmpInst::BAD_ICMP_PREDICATE;
1101 "Cannot compare different types of values!");
1102 if (V1 == V2)
return ICmpInst::ICMP_EQ;
1106 return ICmpInst::BAD_ICMP_PREDICATE;
1112 auto GetComplexity = [](
Constant *V) {
1113 if (isa<ConstantExpr>(V))
1115 if (isa<GlobalValue>(V))
1117 if (isa<BlockAddress>(V))
1121 if (GetComplexity(V1) < GetComplexity(V2)) {
1123 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1124 return ICmpInst::getSwappedPredicate(SwappedRelation);
1125 return ICmpInst::BAD_ICMP_PREDICATE;
1128 if (
const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1130 if (
const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1134 if (BA2->getFunction() != BA->getFunction())
1135 return ICmpInst::ICMP_NE;
1136 }
else if (isa<ConstantPointerNull>(V2)) {
1137 return ICmpInst::ICMP_NE;
1139 }
else if (
const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1142 if (
const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1144 }
else if (isa<BlockAddress>(V2)) {
1145 return ICmpInst::ICMP_NE;
1146 }
else if (isa<ConstantPointerNull>(V2)) {
1152 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1154 GV->getType()->getAddressSpace()))
1155 return ICmpInst::ICMP_UGT;
1157 }
else if (
auto *CE1 = dyn_cast<ConstantExpr>(V1)) {
1162 switch (CE1->getOpcode()) {
1163 case Instruction::GetElementPtr: {
1167 if (isa<ConstantPointerNull>(V2)) {
1170 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1173 if (!GV->hasExternalWeakLinkage() && CE1GEP->
isInBounds())
1174 return ICmpInst::ICMP_UGT;
1176 }
else if (
const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1177 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1181 return ICmpInst::BAD_ICMP_PREDICATE;
1184 }
else if (
const auto *CE2GEP = dyn_cast<GEPOperator>(V2)) {
1187 const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand());
1188 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1190 if (CE1Op0 != CE2Op0) {
1193 cast<GlobalValue>(CE2Op0));
1194 return ICmpInst::BAD_ICMP_PREDICATE;
1205 return ICmpInst::BAD_ICMP_PREDICATE;
1213 VT->getElementCount());
1218 if (Predicate == FCmpInst::FCMP_FALSE)
1221 if (Predicate == FCmpInst::FCMP_TRUE)
1225 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1228 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1229 bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1238 if (isIntegerPredicate)
1250 if (Predicate == ICmpInst::ICMP_UGE)
1253 if (Predicate == ICmpInst::ICMP_ULT)
1259 switch (Predicate) {
1260 case ICmpInst::ICMP_EQ:
1261 if (isa<ConstantInt>(C2))
1264 case ICmpInst::ICMP_NE:
1271 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1272 const APInt &V1 = cast<ConstantInt>(C1)->getValue();
1273 const APInt &V2 = cast<ConstantInt>(C2)->getValue();
1275 }
else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1276 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1277 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1279 }
else if (
auto *C1VTy = dyn_cast<VectorType>(C1->
getType())) {
1285 C1VTy->getElementCount(),
1290 if (isa<ScalableVectorType>(C1VTy))
1298 for (
unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue();
1314 if (Predicate == FCmpInst::FCMP_ONE)
1316 else if (Predicate == FCmpInst::FCMP_UEQ)
1324 case ICmpInst::BAD_ICMP_PREDICATE:
1326 case ICmpInst::ICMP_EQ:
1329 Result = ICmpInst::isTrueWhenEqual(Predicate);
1331 case ICmpInst::ICMP_ULT:
1332 switch (Predicate) {
1333 case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_ULE:
1335 case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_UGE:
1341 case ICmpInst::ICMP_SLT:
1342 switch (Predicate) {
1343 case ICmpInst::ICMP_SLT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_SLE:
1345 case ICmpInst::ICMP_SGT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_SGE:
1351 case ICmpInst::ICMP_UGT:
1352 switch (Predicate) {
1353 case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_UGE:
1355 case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_ULE:
1361 case ICmpInst::ICMP_SGT:
1362 switch (Predicate) {
1363 case ICmpInst::ICMP_SGT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_SGE:
1365 case ICmpInst::ICMP_SLT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_SLE:
1371 case ICmpInst::ICMP_ULE:
1372 if (Predicate == ICmpInst::ICMP_UGT)
1374 if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE)
1377 case ICmpInst::ICMP_SLE:
1378 if (Predicate == ICmpInst::ICMP_SGT)
1380 if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE)
1383 case ICmpInst::ICMP_UGE:
1384 if (Predicate == ICmpInst::ICMP_ULT)
1386 if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE)
1389 case ICmpInst::ICMP_SGE:
1390 if (Predicate == ICmpInst::ICMP_SLT)
1392 if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE)
1395 case ICmpInst::ICMP_NE:
1396 if (Predicate == ICmpInst::ICMP_EQ)
1398 if (Predicate == ICmpInst::ICMP_NE)
1405 return ConstantInt::get(ResultTy, Result);
1407 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
1412 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1420template<
typename IndexTy>
1423 if (Idxs.
empty())
return true;
1426 if (cast<Constant>(Idxs[0])->isNullValue())
return true;
1430 if (
auto *CI = dyn_cast<ConstantInt>(Idxs[0])) {
1434 auto *CV = cast<ConstantDataVector>(Idxs[0]);
1435 CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
1436 if (!CI || !CI->isOne())
1440 for (
unsigned i = 1, e = Idxs.
size(); i != e; ++i)
1441 if (!cast<Constant>(Idxs[i])->isNullValue())
1456 if (IndexVal < 0 || (IndexVal != 0 && (
uint64_t)IndexVal >= NumElements))
1468 if (PointeeTy !=
GEP->getResultElementType())
1472 if (
GEP->getInRange())
1475 Constant *Idx0 = cast<Constant>(Idxs[0]);
1483 GEP->getSourceElementType(), cast<Constant>(
GEP->getPointerOperand()),
1484 NewIndices, InBounds &&
GEP->isInBounds());
1502 auto *LastIdx = cast<Constant>(
GEP->getOperand(
GEP->getNumOperands()-1));
1503 Type *LastIdxTy = LastIdx->getType();
1509 NewIndices.
append(
GEP->idx_begin(),
GEP->idx_end() - 1);
1513 if (LastIdxTy != Idx0->
getType()) {
1514 unsigned CommonExtendedWidth =
1517 CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
1521 if (Idx0->
getType() != CommonTy)
1523 if (LastIdx->getType() != CommonTy)
1526 if (!Idx0 || !LastIdx)
1534 GEP->getSourceElementType(), cast<Constant>(
GEP->getPointerOperand()),
1535 NewIndices, InBounds &&
GEP->isInBounds());
1540 std::optional<ConstantRange>
InRange,
1542 if (Idxs.
empty())
return C;
1547 if (isa<PoisonValue>(
C))
1550 if (isa<UndefValue>(
C))
1554 auto IsNoOp = [&]() {
1561 return IdxC->
isNullValue() || isa<UndefValue>(IdxC);
1565 return GEPTy->
isVectorTy() && !
C->getType()->isVectorTy()
1567 cast<VectorType>(GEPTy)->getElementCount(),
C)
1571 if (
auto *
GEP = dyn_cast<GEPOperator>(CE))
1579 Type *Ty = PointeeTy;
1580 Type *Prev =
C->getType();
1583 !isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]);
1584 for (
unsigned i = 1, e = Idxs.
size(); i != e;
1585 Prev = Ty, Ty = (++GEPIter).getIndexedType(), ++i) {
1586 if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) {
1591 if (!isa<ConstantInt>(Idxs[i - 1]) && !isa<ConstantDataVector>(Idxs[i - 1]))
1594 if (isa<StructType>(Ty)) {
1598 if (isa<VectorType>(Ty)) {
1603 auto *STy = cast<ArrayType>(Ty);
1604 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
1608 if (CI->isNegative()) {
1614 auto *CV = cast<ConstantDataVector>(Idxs[i]);
1615 bool IsInRange =
true;
1616 for (
unsigned I = 0, E = CV->getNumElements();
I != E; ++
I) {
1617 auto *CI = cast<ConstantInt>(CV->getElementAsConstant(
I));
1619 if (CI->isNegative()) {
1629 if (isa<StructType>(Prev)) {
1637 uint64_t NumElements = STy->getArrayNumElements();
1649 Constant *CurrIdx = cast<Constant>(Idxs[i]);
1651 NewIdxs[i - 1] ? NewIdxs[i - 1] : cast<Constant>(Idxs[i - 1]);
1653 bool IsPrevIdxVector = PrevIdx->getType()->isVectorTy();
1654 bool UseVector = IsCurrIdxVector || IsPrevIdxVector;
1656 if (!IsCurrIdxVector && IsPrevIdxVector)
1658 cast<FixedVectorType>(PrevIdx->getType())->getNumElements(), CurrIdx);
1660 if (!IsPrevIdxVector && IsCurrIdxVector)
1662 cast<FixedVectorType>(CurrIdx->
getType())->getNumElements(), PrevIdx);
1669 ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
1670 : cast<FixedVectorType>(CurrIdx->
getType())->getNumElements(),
1681 assert(NewIdxs[i] !=
nullptr && Div !=
nullptr &&
"Should have folded");
1683 unsigned CommonExtendedWidth =
1684 std::max(PrevIdx->getType()->getScalarSizeInBits(),
1686 CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
1695 ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
1696 : cast<FixedVectorType>(CurrIdx->
getType())->getNumElements());
1698 if (!PrevIdx->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
1705 assert(PrevIdx && Div &&
"Should have folded");
1710 if (!NewIdxs.
empty()) {
1711 for (
unsigned i = 0, e = Idxs.
size(); i != e; ++i)
1712 if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
1720 if (
auto *GV = dyn_cast<GlobalVariable>(
C))
1721 if (!GV->hasExternalWeakLinkage() && GV->getValueType() == PointeeTy &&
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
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.
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< ConstantRange > InRange=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.
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 * 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 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 AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static Constant * getAllOnesValue(Type *Ty)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
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)
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.
Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, bool InBounds, std::optional< ConstantRange > InRange, ArrayRef< Value * > Idxs)
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 * 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.