llvm.org GIT mirror llvm / 09d24dd
[ConstantRange] Rename make{Guaranteed -> Exact}NoWrapRegion() NFC Following D60632 makeGuaranteedNoWrapRegion() always returns an exact nowrap region. Rename the function accordingly. This is in line with the naming of makeExactICmpRegion(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358875 91177308-0d34-0410-b5e6-96231b3b80d8 Nikita Popov 5 months ago
6 changed file(s) with 65 addition(s) and 66 deletion(s). Raw diff Collapse all Expand all
130130 ///
131131 /// Examples:
132132 /// typedef OverflowingBinaryOperator OBO;
133 /// #define MGNR makeGuaranteedNoWrapRegion
134 /// MGNR(Add, [i8 1, 2), OBO::NoSignedWrap) == [-128, 127)
135 /// MGNR(Add, [i8 1, 2), OBO::NoUnsignedWrap) == [0, -1)
136 /// MGNR(Add, [i8 0, 1), OBO::NoUnsignedWrap) == Full Set
137 /// MGNR(Add, [i8 -1, 6), OBO::NoSignedWrap) == [INT_MIN+1, INT_MAX-4)
138 /// MGNR(Sub, [i8 1, 2), OBO::NoSignedWrap) == [-127, 128)
139 /// MGNR(Sub, [i8 1, 2), OBO::NoUnsignedWrap) == [1, 0)
140 static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
141 const ConstantRange &Other,
142 unsigned NoWrapKind);
133 /// #define MENR makeExactNoWrapRegion
134 /// MENR(Add, [i8 1, 2), OBO::NoSignedWrap) == [-128, 127)
135 /// MENR(Add, [i8 1, 2), OBO::NoUnsignedWrap) == [0, -1)
136 /// MENR(Add, [i8 0, 1), OBO::NoUnsignedWrap) == Full Set
137 /// MENR(Add, [i8 -1, 6), OBO::NoSignedWrap) == [INT_MIN+1, INT_MAX-4)
138 /// MENR(Sub, [i8 1, 2), OBO::NoSignedWrap) == [-127, 128)
139 /// MENR(Sub, [i8 1, 2), OBO::NoUnsignedWrap) == [1, 0)
140 static ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
141 const ConstantRange &Other,
142 unsigned NoWrapKind);
143143
144144 /// Set up \p Pred and \p RHS such that
145145 /// ConstantRange::makeExactICmpRegion(Pred, RHS) == *this. Return true if
11451145 return ValueLatticeElement::getOverdefined();
11461146
11471147 // Calculate the possible values of %x for which no overflow occurs.
1148 ConstantRange NWR = ConstantRange::makeGuaranteedNoWrapRegion(
1148 ConstantRange NWR = ConstantRange::makeExactNoWrapRegion(
11491149 WO->getBinaryOp(), ConstantRange(*C), WO->getNoWrapKind());
11501150
11511151 // If overflow is false, %x is constrained to NWR. If overflow is true, %x is
23622362
23632363 // (A C) --> (A C) if the op doesn't sign overflow.
23642364 if (!(SignOrUnsignWrap & SCEV::FlagNSW)) {
2365 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2365 auto NSWRegion = ConstantRange::makeExactNoWrapRegion(
23662366 Opcode, C, OBO::NoSignedWrap);
23672367 if (NSWRegion.contains(SE->getSignedRange(Ops[1])))
23682368 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
23702370
23712371 // (A C) --> (A C) if the op doesn't unsign overflow.
23722372 if (!(SignOrUnsignWrap & SCEV::FlagNUW)) {
2373 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2373 auto NUWRegion = ConstantRange::makeExactNoWrapRegion(
23742374 Opcode, C, OBO::NoUnsignedWrap);
23752375 if (NUWRegion.contains(SE->getUnsignedRange(Ops[1])))
23762376 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
44704470 ConstantRange AddRecRange = getSignedRange(AR);
44714471 ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this));
44724472
4473 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4473 auto NSWRegion = ConstantRange::makeExactNoWrapRegion(
44744474 Instruction::Add, IncRange, OBO::NoSignedWrap);
44754475 if (NSWRegion.contains(AddRecRange))
44764476 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW);
44804480 ConstantRange AddRecRange = getUnsignedRange(AR);
44814481 ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this));
44824482
4483 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4483 auto NUWRegion = ConstantRange::makeExactNoWrapRegion(
44844484 Instruction::Add, IncRange, OBO::NoUnsignedWrap);
44854485 if (NUWRegion.contains(AddRecRange))
44864486 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW);
180180 }
181181
182182 ConstantRange
183 ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
184 const ConstantRange &Other,
185 unsigned NoWrapKind) {
183 ConstantRange::makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
184 const ConstantRange &Other,
185 unsigned NoWrapKind) {
186186 using OBO = OverflowingBinaryOperator;
187187
188188 // Computes the intersection of CR0 and CR1. It is different from
261261 return Result;
262262
263263 case Instruction::Mul: {
264 // Equivalent to calling makeGuaranteedNoWrapRegion() on [V, V+1).
264 // Equivalent to calling makeExactNoWrapRegion() on [V, V+1).
265265 const bool Unsigned = NoWrapKind == OBO::NoUnsignedWrap;
266266 const auto makeSingleValueRegion = [Unsigned,
267267 BitWidth](APInt V) -> ConstantRange {
840840 ConstantRange ConstantRange::addWithNoSignedWrap(const APInt &Other) const {
841841 // Calculate the subset of this range such that "X + Other" is
842842 // guaranteed not to wrap (overflow) for all X in this subset.
843 // makeGuaranteedNoWrapRegion will produce an exact NSW range.
844 auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(BinaryOperator::Add,
845 ConstantRange(Other),
846 OverflowingBinaryOperator::NoSignedWrap);
843 auto NSWRange = ConstantRange::makeExactNoWrapRegion(
844 BinaryOperator::Add, ConstantRange(Other),
845 OverflowingBinaryOperator::NoSignedWrap);
847846 auto NSWConstrainedRange = intersectWith(NSWRange);
848847
849848 return NSWConstrainedRange.add(ConstantRange(Other));
401401 static bool willNotOverflow(WithOverflowInst *WO, LazyValueInfo *LVI) {
402402 Value *RHS = WO->getRHS();
403403 ConstantRange RRange = LVI->getConstantRange(RHS, WO->getParent(), WO);
404 ConstantRange NWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
404 ConstantRange NWRegion = ConstantRange::makeExactNoWrapRegion(
405405 WO->getBinaryOp(), RRange, WO->getNoWrapKind());
406406 // As an optimization, do not compute LRange if we do not need it.
407407 if (NWRegion.isEmptySet())
639639
640640 bool Changed = false;
641641 if (!NUW) {
642 ConstantRange NUWRange = ConstantRange::makeGuaranteedNoWrapRegion(
642 ConstantRange NUWRange = ConstantRange::makeExactNoWrapRegion(
643643 BinOp->getOpcode(), RRange, OBO::NoUnsignedWrap);
644644 if (!NUWRange.isEmptySet()) {
645645 bool NewNUW = NUWRange.contains(LazyLRange());
648648 }
649649 }
650650 if (!NSW) {
651 ConstantRange NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(
651 ConstantRange NSWRange = ConstantRange::makeExactNoWrapRegion(
652652 BinOp->getOpcode(), RRange, OBO::NoSignedWrap);
653653 if (!NSWRange.isEmptySet()) {
654654 bool NewNSW = NSWRange.contains(LazyLRange());
890890 ConstantRange(APInt(8, 4), APInt(8, -128)));
891891 }
892892
893 TEST(ConstantRange, MakeGuaranteedNoWrapRegion) {
893 TEST(ConstantRange, MakeExactNoWrapRegion) {
894894 const int IntMin4Bits = 8;
895895 const int IntMax4Bits = 7;
896896 typedef OverflowingBinaryOperator OBO;
898898 for (int Const : {0, -1, -2, 1, 2, IntMin4Bits, IntMax4Bits}) {
899899 APInt C(4, Const, true /* = isSigned */);
900900
901 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
901 auto NUWRegion = ConstantRange::makeExactNoWrapRegion(
902902 Instruction::Add, C, OBO::NoUnsignedWrap);
903903
904904 EXPECT_FALSE(NUWRegion.isEmptySet());
905905
906 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
906 auto NSWRegion = ConstantRange::makeExactNoWrapRegion(
907907 Instruction::Add, C, OBO::NoSignedWrap);
908908
909909 EXPECT_FALSE(NSWRegion.isEmptySet());
926926 for (int Const : {0, -1, -2, 1, 2, IntMin4Bits, IntMax4Bits}) {
927927 APInt C(4, Const, true /* = isSigned */);
928928
929 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
929 auto NUWRegion = ConstantRange::makeExactNoWrapRegion(
930930 Instruction::Sub, C, OBO::NoUnsignedWrap);
931931
932932 EXPECT_FALSE(NUWRegion.isEmptySet());
933933
934 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
934 auto NSWRegion = ConstantRange::makeExactNoWrapRegion(
935935 Instruction::Sub, C, OBO::NoSignedWrap);
936936
937937 EXPECT_FALSE(NSWRegion.isEmptySet());
951951 }
952952 }
953953
954 auto NSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
954 auto NSWForAllValues = ConstantRange::makeExactNoWrapRegion(
955955 Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
956956 OBO::NoSignedWrap);
957957 EXPECT_TRUE(NSWForAllValues.isSingleElement() &&
958958 NSWForAllValues.getSingleElement()->isMinValue());
959959
960 NSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
960 NSWForAllValues = ConstantRange::makeExactNoWrapRegion(
961961 Instruction::Sub, ConstantRange(32, /* isFullSet = */ true),
962962 OBO::NoSignedWrap);
963963 EXPECT_TRUE(NSWForAllValues.isSingleElement() &&
964964 NSWForAllValues.getSingleElement()->isMaxValue());
965965
966 auto NUWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
966 auto NUWForAllValues = ConstantRange::makeExactNoWrapRegion(
967967 Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
968968 OBO::NoUnsignedWrap);
969969 EXPECT_TRUE(NUWForAllValues.isSingleElement() &&
970970 NUWForAllValues.getSingleElement()->isMinValue());
971971
972 NUWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
972 NUWForAllValues = ConstantRange::makeExactNoWrapRegion(
973973 Instruction::Sub, ConstantRange(32, /* isFullSet = */ true),
974974 OBO::NoUnsignedWrap);
975975 EXPECT_TRUE(NUWForAllValues.isSingleElement() &&
976976 NUWForAllValues.getSingleElement()->isMaxValue());
977977
978 EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
978 EXPECT_TRUE(ConstantRange::makeExactNoWrapRegion(
979979 Instruction::Add, APInt(32, 0), OBO::NoUnsignedWrap).isFullSet());
980 EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
980 EXPECT_TRUE(ConstantRange::makeExactNoWrapRegion(
981981 Instruction::Add, APInt(32, 0), OBO::NoSignedWrap).isFullSet());
982 EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
982 EXPECT_TRUE(ConstantRange::makeExactNoWrapRegion(
983983 Instruction::Sub, APInt(32, 0), OBO::NoUnsignedWrap).isFullSet());
984 EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
984 EXPECT_TRUE(ConstantRange::makeExactNoWrapRegion(
985985 Instruction::Sub, APInt(32, 0), OBO::NoSignedWrap).isFullSet());
986986
987987 ConstantRange OneToFive(APInt(32, 1), APInt(32, 6));
988 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
988 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
989989 Instruction::Add, OneToFive, OBO::NoSignedWrap),
990990 ConstantRange(APInt::getSignedMinValue(32),
991991 APInt::getSignedMaxValue(32) - 4));
992 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
992 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
993993 Instruction::Add, OneToFive, OBO::NoUnsignedWrap),
994994 ConstantRange(APInt::getMinValue(32), APInt::getMinValue(32) - 5));
995 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
995 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
996996 Instruction::Sub, OneToFive, OBO::NoSignedWrap),
997997 ConstantRange(APInt::getSignedMinValue(32) + 5,
998998 APInt::getSignedMinValue(32)));
999 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
999 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10001000 Instruction::Sub, OneToFive, OBO::NoUnsignedWrap),
10011001 ConstantRange(APInt::getMinValue(32) + 5, APInt::getMinValue(32)));
10021002
10031003 ConstantRange MinusFiveToMinusTwo(APInt(32, -5), APInt(32, -1));
1004 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1004 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10051005 Instruction::Add, MinusFiveToMinusTwo, OBO::NoSignedWrap),
10061006 ConstantRange(APInt::getSignedMinValue(32) + 5,
10071007 APInt::getSignedMinValue(32)));
1008 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1008 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10091009 Instruction::Add, MinusFiveToMinusTwo, OBO::NoUnsignedWrap),
10101010 ConstantRange(APInt(32, 0), APInt(32, 2)));
1011 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1011 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10121012 Instruction::Sub, MinusFiveToMinusTwo, OBO::NoSignedWrap),
10131013 ConstantRange(APInt::getSignedMinValue(32),
10141014 APInt::getSignedMaxValue(32) - 4));
1015 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1015 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10161016 Instruction::Sub, MinusFiveToMinusTwo, OBO::NoUnsignedWrap),
10171017 ConstantRange(APInt::getMaxValue(32) - 1,
10181018 APInt::getMinValue(32)));
10191019
10201020 ConstantRange MinusOneToOne(APInt(32, -1), APInt(32, 2));
1021 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1021 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10221022 Instruction::Add, MinusOneToOne, OBO::NoSignedWrap),
10231023 ConstantRange(APInt::getSignedMinValue(32) + 1,
10241024 APInt::getSignedMinValue(32) - 1));
1025 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1025 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10261026 Instruction::Add, MinusOneToOne, OBO::NoUnsignedWrap),
10271027 ConstantRange(APInt(32, 0), APInt(32, 1)));
1028 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1028 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10291029 Instruction::Sub, MinusOneToOne, OBO::NoSignedWrap),
10301030 ConstantRange(APInt::getSignedMinValue(32) + 1,
10311031 APInt::getSignedMinValue(32) - 1));
1032 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1032 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10331033 Instruction::Sub, MinusOneToOne, OBO::NoUnsignedWrap),
10341034 ConstantRange(APInt::getMaxValue(32),
10351035 APInt::getMinValue(32)));
10361036
10371037 ConstantRange One(APInt(32, 1), APInt(32, 2));
1038 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1038 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10391039 Instruction::Add, One, OBO::NoSignedWrap),
10401040 ConstantRange(APInt::getSignedMinValue(32),
10411041 APInt::getSignedMaxValue(32)));
1042 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1042 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10431043 Instruction::Add, One, OBO::NoUnsignedWrap),
10441044 ConstantRange(APInt::getMinValue(32), APInt::getMaxValue(32)));
1045 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1045 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10461046 Instruction::Sub, One, OBO::NoSignedWrap),
10471047 ConstantRange(APInt::getSignedMinValue(32) + 1,
10481048 APInt::getSignedMinValue(32)));
1049 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1049 EXPECT_EQ(ConstantRange::makeExactNoWrapRegion(
10501050 Instruction::Sub, One, OBO::NoUnsignedWrap),
10511051 ConstantRange(APInt::getMinValue(32) + 1, APInt::getMinValue(32)));
10521052 }
10621062 return;
10631063
10641064 ConstantRange NoWrap =
1065 ConstantRange::makeGuaranteedNoWrapRegion(BinOp, CR2, NoWrapKind);
1065 ConstantRange::makeExactNoWrapRegion(BinOp, CR2, NoWrapKind);
10661066 ForeachNumInConstantRange(CR1, [&](const APInt &N1) {
10671067 bool NoOverflow = true;
10681068 ForeachNumInConstantRange(CR2, [&](const APInt &N2) {
10741074 });
10751075 }
10761076
1077 // Show that makeGuaranteedNoWrapRegion is precise if only one of
1077 // Show that makeExactNoWrapRegion is precise if only one of
10781078 // NoUnsignedWrap or NoSignedWrap is used.
10791079 TEST(ConstantRange, NoWrapRegionExhaustive) {
10801080 TestNoWrapRegionExhaustive(
12031203 EXPECT_EQ(RHS, APInt(32, -1));
12041204 }
12051205
1206 TEST(ConstantRange, MakeGuaranteedNoWrapRegionMulUnsignedSingleValue) {
1206 TEST(ConstantRange, MakeExactNoWrapRegionMulUnsignedSingleValue) {
12071207 typedef OverflowingBinaryOperator OBO;
12081208
12091209 for (uint64_t I = std::numeric_limits::min();
12101210 I <= std::numeric_limits::max(); I++) {
1211 auto Range = ConstantRange::makeGuaranteedNoWrapRegion(
1211 auto Range = ConstantRange::makeExactNoWrapRegion(
12121212 Instruction::Mul, ConstantRange(APInt(8, I), APInt(8, I + 1)),
12131213 OBO::NoUnsignedWrap);
12141214
12211221 }
12221222 }
12231223
1224 TEST(ConstantRange, MakeGuaranteedNoWrapRegionMulSignedSingleValue) {
1224 TEST(ConstantRange, MakeExactNoWrapRegionMulSignedSingleValue) {
12251225 typedef OverflowingBinaryOperator OBO;
12261226
12271227 for (int64_t I = std::numeric_limits::min();
12281228 I <= std::numeric_limits::max(); I++) {
1229 auto Range = ConstantRange::makeGuaranteedNoWrapRegion(
1229 auto Range = ConstantRange::makeExactNoWrapRegion(
12301230 Instruction::Mul,
12311231 ConstantRange(APInt(8, I, /*isSigned=*/true),
12321232 APInt(8, I + 1, /*isSigned=*/true)),
12421242 }
12431243 }
12441244
1245 TEST(ConstantRange, MakeGuaranteedNoWrapRegionMulUnsignedRange) {
1245 TEST(ConstantRange, MakeExactNoWrapRegionMulUnsignedRange) {
12461246 typedef OverflowingBinaryOperator OBO;
12471247
12481248 for (uint64_t Lo = std::numeric_limits::min();
12491249 Lo <= std::numeric_limits::max(); Lo++) {
12501250 for (uint64_t Hi = Lo; Hi <= std::numeric_limits::max(); Hi++) {
12511251 EXPECT_EQ(
1252 ConstantRange::makeGuaranteedNoWrapRegion(
1252 ConstantRange::makeExactNoWrapRegion(
12531253 Instruction::Mul, ConstantRange(APInt(8, Lo), APInt(8, Hi + 1)),
12541254 OBO::NoUnsignedWrap),
1255 ConstantRange::makeGuaranteedNoWrapRegion(
1255 ConstantRange::makeExactNoWrapRegion(
12561256 Instruction::Mul, ConstantRange(APInt(8, Hi), APInt(8, Hi + 1)),
12571257 OBO::NoUnsignedWrap));
12581258 }
12591259 }
12601260 }
12611261
1262 TEST(ConstantRange, MakeGuaranteedNoWrapRegionMulSignedRange) {
1262 TEST(ConstantRange, MakeExactNoWrapRegionMulSignedRange) {
12631263 typedef OverflowingBinaryOperator OBO;
12641264
12651265 int Lo = -12, Hi = 16;
1266 auto Range = ConstantRange::makeGuaranteedNoWrapRegion(
1266 auto Range = ConstantRange::makeExactNoWrapRegion(
12671267 Instruction::Mul,
12681268 ConstantRange(APInt(8, Lo, /*isSigned=*/true),
12691269 APInt(8, Hi + 1, /*isSigned=*/true)),