-
Notifications
You must be signed in to change notification settings - Fork 5.4k
JIT: Add RangeOps::Xor and RangeOps::Not
#126553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
49c1b40
dda7a50
605b7e3
46043a0
8c60835
5174434
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -736,9 +736,23 @@ Range RangeCheck::GetRangeFromAssertionsWorker( | |
| break; | ||
|
|
||
| case VNF_NEG: | ||
| case VNF_NOT: | ||
| { | ||
| Range r1 = GetRangeFromAssertionsWorker(comp, funcApp.m_args[0], assertions, --budget, visited); | ||
| Range unaryOpResult = RangeOps::Negate(r1); | ||
| Range unaryOpResult = Range(Limit(Limit::keUnknown)); | ||
| switch (funcApp.m_func) | ||
| { | ||
| case VNF_NEG: | ||
| unaryOpResult = RangeOps::Negate(r1); | ||
| break; | ||
|
|
||
| case VNF_NOT: | ||
| unaryOpResult = RangeOps::Not(r1); | ||
| break; | ||
|
|
||
| default: | ||
| unreached(); | ||
| } | ||
|
|
||
| // We can use the result only if it never overflows. | ||
| result = unaryOpResult.IsConstantRange() ? unaryOpResult : result; | ||
|
|
@@ -751,6 +765,7 @@ Range RangeCheck::GetRangeFromAssertionsWorker( | |
| case VNF_SUB: | ||
| case VNF_AND: | ||
| case VNF_OR: | ||
| case VNF_XOR: | ||
| case VNF_RSH: | ||
| case VNF_RSZ: | ||
| case VNF_UMOD: | ||
|
|
@@ -776,6 +791,9 @@ Range RangeCheck::GetRangeFromAssertionsWorker( | |
| case VNF_OR: | ||
| binOpResult = RangeOps::Or(r1, r2); | ||
| break; | ||
| case VNF_XOR: | ||
| binOpResult = RangeOps::Xor(r1, r2); | ||
| break; | ||
| case VNF_LSH: | ||
| binOpResult = RangeOps::ShiftLeft(r1, r2); | ||
| break; | ||
|
|
@@ -1553,7 +1571,9 @@ Range RangeCheck::ComputeRangeForBinOp(BasicBlock* block, GenTreeOp* binop, bool | |
| { | ||
| assert(binop->OperIs(GT_ADD, GT_OR, GT_XOR, GT_AND, GT_RSH, GT_RSZ, GT_LSH, GT_UMOD, GT_MUL)); | ||
|
|
||
| // For XOR we only care about Log2 pattern for now | ||
| // To handle the Log2 pattern of "63 ^ LZCNT(x | 1)" we are missing precise | ||
| // range info for "LZCNT(x | 1)" (should be [0, 63]) and 64bit support. Special case it: | ||
| // https://github.com/dotnet/runtime/pull/113790 | ||
| if (binop->OperIs(GT_XOR)) | ||
| { | ||
| int upperBound; | ||
|
|
@@ -1562,7 +1582,6 @@ Range RangeCheck::ComputeRangeForBinOp(BasicBlock* block, GenTreeOp* binop, bool | |
| assert(upperBound > 0); | ||
| return Range(Limit(Limit::keConstant, 0), Limit(Limit::keConstant, upperBound)); | ||
| } | ||
| return Range(Limit(Limit::keUnknown)); | ||
| } | ||
|
|
||
| GenTree* op1 = binop->gtGetOp1(); | ||
|
|
@@ -1632,6 +1651,9 @@ Range RangeCheck::ComputeRangeForBinOp(BasicBlock* block, GenTreeOp* binop, bool | |
| case GT_OR: | ||
| r = RangeOps::Or(op1Range, op2Range); | ||
| break; | ||
| case GT_XOR: | ||
| r = RangeOps::Xor(op1Range, op2Range); | ||
| break; | ||
|
Comment on lines
+1654
to
+1656
|
||
| case GT_UMOD: | ||
| r = RangeOps::UnsignedMod(op1Range, op2Range); | ||
| break; | ||
|
|
@@ -1964,15 +1986,10 @@ bool RangeCheck::ComputeDoesOverflow(BasicBlock* block, GenTree* expr, const Ran | |
| overflows = DoesBinOpOverflow(block, expr->AsOp(), range); | ||
| } | ||
| // These operators don't overflow. | ||
| else if (expr->OperIs(GT_AND, GT_RSH, GT_RSZ, GT_UMOD, GT_NEG)) | ||
| else if (expr->OperIs(GT_AND, GT_RSH, GT_RSZ, GT_UMOD, GT_NEG, GT_XOR, GT_NOT)) | ||
| { | ||
| overflows = false; | ||
| } | ||
| else if (expr->OperIs(GT_XOR) && vnStore->IsVNLog2(m_compiler->vnStore->VNConservativeNormalValue(expr->gtVNPair))) | ||
| { | ||
| // For XOR we only care about Log2 pattern for now, which never overflows. | ||
| overflows = false; | ||
| } | ||
| // Walk through phi arguments to check if phi arguments involve arithmetic that overflows. | ||
| else if (expr->OperIs(GT_PHI)) | ||
| { | ||
|
|
@@ -2070,6 +2087,12 @@ Range RangeCheck::ComputeRange(BasicBlock* block, GenTree* expr, bool monIncreas | |
| Range op1Range = GetRangeWorker(block, expr->gtGetOp1(), monIncreasing DEBUGARG(indent + 1)); | ||
| range = RangeOps::Negate(op1Range); | ||
| } | ||
| else if (expr->OperIs(GT_NOT)) | ||
| { | ||
| // Compute range for not, e.g: [2, 8] -> [-9..-3] | ||
| Range op1Range = GetRangeWorker(block, expr->gtGetOp1(), monIncreasing DEBUGARG(indent + 1)); | ||
| range = RangeOps::Not(op1Range); | ||
| } | ||
| // If phi, then compute the range for arguments, calling the result "dependent" when looping begins. | ||
| else if (expr->OperIs(GT_PHI)) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -479,6 +479,46 @@ struct RangeOps | |
| return Range(Limit(Limit::keUnknown)); | ||
| } | ||
|
|
||
| static Range Xor(const Range& r1, const Range& r2) | ||
| { | ||
| int r1ConstVal; | ||
| int r2ConstVal; | ||
| bool r1IsConstVal = r1.IsSingleValueConstant(&r1ConstVal); | ||
| bool r2IsConstVal = r2.IsSingleValueConstant(&r2ConstVal); | ||
|
|
||
| // Both ranges are single constant values. | ||
| // Example: [5..5] ^ [3..3] = [6..6] | ||
| if (r1IsConstVal && r2IsConstVal) | ||
| { | ||
| return Range(Limit(Limit::keConstant, r1ConstVal ^ r2ConstVal)); | ||
| } | ||
|
|
||
| auto isSubToXorValid = [=](uint64_t cns, Range range) { | ||
| uint64_t lo = (uint64_t)range.LowerLimit().GetConstant(); | ||
| uint64_t hi = (uint64_t)range.UpperLimit().GetConstant(); | ||
| uint64_t knownBits = BitOperations::BitsetFromRange(lo, hi); | ||
|
|
||
| // Zero out bits outside of TYPE. This handles cases that rely on overflow (int.MaxValue - x) | ||
| uint32_t sizeInBits = genTypeSize(TYP_INT) * BITS_PER_BYTE; | ||
| knownBits &= (1ULL << (sizeInBits - 1)) - 1; | ||
|
|
||
| // Can sub be perfomed without carry? | ||
| return (cns & knownBits) == knownBits; | ||
| }; | ||
|
|
||
| // Example: [3..5] ^ [-1..-1] = [-6..-4] | ||
| if (r1IsConstVal && r2.IsConstantRange() && isSubToXorValid(r1ConstVal, r2)) | ||
| { | ||
| return Subtract(r1, r2); | ||
| } | ||
| if (r2IsConstVal && r1.IsConstantRange() && isSubToXorValid(r2ConstVal, r1)) | ||
| { | ||
| return Subtract(r2, r1); | ||
| } | ||
|
|
||
| return Range(Limit(Limit::keUnknown)); | ||
| } | ||
|
|
||
| static Range UnsignedMod(const Range& r1, const Range& r2) | ||
| { | ||
| // For X UMOD Y we only handle the case when Y is a fixed positive constant. | ||
|
|
@@ -661,6 +701,12 @@ struct RangeOps | |
| return result; | ||
| } | ||
|
|
||
| static Range Not(const Range& range) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. last I checked Not produced 0 diffs, so we'd better remove it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #126553 (comment) Here I was talking about 2 large diffs in libraries_tests_no_tiered_compilation caused by NOT handling. Additionally it fixes regressions from my recent transformation (#126529). Example regression in Which this fixes by adding NOT handling (diffs): |
||
| { | ||
| Range cns = Limit(Limit::keConstant, -1); | ||
| return Subtract(cns, range); | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------ | ||
| // EvalRelop: Evaluate the relation between two ranges for the given relop | ||
| // Example: "x >= y" is AlwaysTrue when "x.LowerLimit() >= y.UpperLimit()" | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does this rewrite do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just factored out the
BitsetFromRangesince we now use it in morph and rangecheck and adjusted the code a little. We can useBitsetFromRangeto do other stuff for example transform ADD to OR if we wanted.