Detect a recurrence that monotonically increases with mul.
Details
Diff Detail
Event Timeline
llvm/lib/Analysis/ValueTracking.cpp | ||
---|---|---|
2459 | I think the matchSimpleRecurrence() check should be higher, e.g. PN->getNumIncomingValues() == 2 is already part of it. | |
2461 | I'd rename R/L to Start and Step. | |
2468 | Why does this exclude C = 1? {1,*,2}<nuw> is still non-zero. The one value is important for inequality, but here we're checking for non-zero. | |
2471 | Again, this isOne() check looks unnecessary to me. | |
llvm/test/Analysis/ValueTracking/monotonic-phi.ll | ||
61 | Why is this or needed? | |
79 | Why does this return false? Isn't this supposed to be a negative test case, due to lack of nuw/nsw? |
llvm/lib/Analysis/ValueTracking.cpp | ||
---|---|---|
2459 | Yep, you are right. I will move it up. | |
2461 | Yep, I will rename them. | |
2468 | You are right!!! We are checking non-zero. I will update it. | |
2471 | Yep, I will update it. | |
llvm/test/Analysis/ValueTracking/monotonic-phi.ll | ||
61 | We do not need it. I will remove it. | |
79 | Yep, it is negative test case due to lack of nuw/nsw. It could be better to check CHECK: ret i1 %cmp. I will update it. |
This looks good to me, but I think needs a couple more tests:
- Positive test for nuw instead of nsw.
- Negative test for zero start value.
- Negative test for negative multiply. (We can't really test zero multiply, as it will get folded away.)
- Negative test for negative start value -- alternatively, you can slightly extend this patch and split the start value check between add and mul: Add requires strictly positive start, but for mul non-zero is sufficient, I believe.
llvm/lib/Analysis/ValueTracking.cpp | ||
---|---|---|
2462 | Style suggestion: I would write this using m_APInt matchers, which allows you to avoid some of the nesting: Value *Start, *Step; const APInt *StartC, *StepC; if (Q.IIQ.UseInstrInfo && matchSimpleRecurrence(PN, BO, Start, Step) && match(Start, m_APInt(StartC)) && match(Step, m_APInt(StepC)) && StartC->isStrictlyPositive()) { ... } | |
llvm/test/Analysis/ValueTracking/monotonic-phi.ll | ||
79 | Ah, I guess I misread a CHECK-NOT as CHECK here? I've regenerated the check lines for this test in https://github.com/llvm/llvm-project/commit/b7aae9fab14540ad3b4ccda8a5f3a7284f404e63, so you can generate the expected output using llvm/utils/update_test_checks.py. |
Thanks @nikic If possible, can you also review https://reviews.llvm.org/D98422 please? I think it could also be ready to merge.
I think the matchSimpleRecurrence() check should be higher, e.g. PN->getNumIncomingValues() == 2 is already part of it.