diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2020-06-23 13:52:21 +0300 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2020-06-23 13:53:15 +0300 |
commit | d57e9aca0137dd088825abd80ea4a616e49d17ed (patch) | |
tree | 4b78752a4ade916547df646f35fbacbf9203740e /llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | |
parent | f3f016dbaa0db9ae0f6706ccc6dee26770ac4d33 (diff) | |
download | llvm-d57e9aca0137dd088825abd80ea4a616e49d17ed.zip llvm-d57e9aca0137dd088825abd80ea4a616e49d17ed.tar.gz llvm-d57e9aca0137dd088825abd80ea4a616e49d17ed.tar.bz2 |
[IndVarSimplify] Don't replace IV user with unsafe loop-invariant (PR45360)
Summary:
As [[ https://bugs.llvm.org/show_bug.cgi?id=45360 | PR45360 ]] reports,
with new cost-model we can sometimes end up being able to expand `udiv`/`urem` instructions.
And that exposes at least one instance of when we do that
regardless of whether or not it is safe to do.
In this particular case, it's `SimplifyIndvar::replaceIVUserWithLoopInvariant()`.
It seems to me, we simply need to check with `isSafeToExpandAt()` first.
The test isn't great. I'm not sure how to make it only run `-indvars`.
Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=45360 | PR45360 ]].
Reviewers: mkazantsev, reames, helloqirun
Reviewed By: mkazantsev
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D82108
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyIndVar.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp index 05632ca..d3d0c33 100644 --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -657,7 +657,7 @@ static Instruction *GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint) { return Hint; } -/// Replace the UseInst with a constant if possible. +/// Replace the UseInst with a loop invariant expression if it is safe. bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) { if (!SE->isSCEVable(I->getType())) return false; @@ -673,6 +673,13 @@ bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) { return false; auto *IP = GetLoopInvariantInsertPosition(L, I); + + if (!isSafeToExpandAt(S, IP, *SE)) { + LLVM_DEBUG(dbgs() << "INDVARS: Can not replace IV user: " << *I + << " with non-speculable loop invariant: " << *S << '\n'); + return false; + } + auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP); I->replaceAllUsesWith(Invariant); |