aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/LoopUtils.cpp
diff options
context:
space:
mode:
authorZaara Syeda <syzaara@ca.ibm.com>2022-07-07 14:29:06 -0400
committerZaara Syeda <syzaara@ca.ibm.com>2022-07-07 15:11:33 -0400
commit58b9666dc1a09819377ae585b4718ef2ea951970 (patch)
tree0c4ad67552beb9de4aaf4d368b362abf57d8e4e3 /llvm/lib/Transforms/Utils/LoopUtils.cpp
parentf67fc3acad70c20d8088b72ee9563690c8ab9be0 (diff)
downloadllvm-58b9666dc1a09819377ae585b4718ef2ea951970.zip
llvm-58b9666dc1a09819377ae585b4718ef2ea951970.tar.gz
llvm-58b9666dc1a09819377ae585b4718ef2ea951970.tar.bz2
[LSR] Fix bug - check if loop has preheader before calling isInductionPHI
Fix bug exposed by https://reviews.llvm.org/D125990 rewriteLoopExitValues calls InductionDescriptor::isInductionPHI which requires the PHI node to have an incoming edge from the loop preheader. This adds checks before calling InductionDescriptor::isInductionPHI to see that the loop has a preheader. Also did some refactoring. Differential Revision: https://reviews.llvm.org/D129297
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopUtils.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/LoopUtils.cpp25
1 files changed, 16 insertions, 9 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index afc7c61..29d3cc7 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -1243,6 +1243,20 @@ static bool canLoopBeDeleted(Loop *L, SmallVector<RewritePhi, 8> &RewritePhiSet)
return true;
}
+/// Checks if it is safe to call InductionDescriptor::isInductionPHI for \p Phi,
+/// and returns true if this Phi is an induction phi in the loop. When
+/// isInductionPHI returns true, \p ID will be also be set by isInductionPHI.
+static bool checkIsIndPhi(PHINode *Phi, Loop *L, ScalarEvolution *SE,
+ InductionDescriptor &ID) {
+ if (!Phi)
+ return false;
+ if (!L->getLoopPreheader())
+ return false;
+ if (Phi->getParent() != L->getHeader())
+ return false;
+ return InductionDescriptor::isInductionPHI(Phi, L, SE, ID);
+}
+
int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI,
ScalarEvolution *SE,
const TargetTransformInfo *TTI,
@@ -1303,10 +1317,7 @@ int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI,
InductionDescriptor ID;
PHINode *IndPhi = dyn_cast<PHINode>(Inst);
if (IndPhi) {
- if (IndPhi->getParent() != L->getHeader())
- continue;
- // Do not consider non induction phis.
- if (!InductionDescriptor::isInductionPHI(IndPhi, L, SE, ID))
+ if (!checkIsIndPhi(IndPhi, L, SE, ID))
continue;
// This is an induction PHI. Check that the only users are PHI
// nodes, and induction variable update binary operators.
@@ -1327,12 +1338,8 @@ int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI,
continue;
if (llvm::any_of(Inst->users(), [&](User *U) {
PHINode *Phi = dyn_cast<PHINode>(U);
- if (!Phi)
+ if (Phi != PN && !checkIsIndPhi(Phi, L, SE, ID))
return true;
- if (Phi->getParent() == L->getHeader()) {
- if (!InductionDescriptor::isInductionPHI(Phi, L, SE, ID))
- return true;
- }
return false;
}))
continue;