/// estimate can not be made.
Optional<unsigned> getLoopEstimatedTripCount(Loop *L);
+/// Check inner loop (L) backedge count is known to be invariant on all iterations
+/// of its outer loop. If the loop has no parent, this is trivially true.
+bool hasInvariantIterationCount(Loop *L, ScalarEvolution &SE);
+
/// Helper to consistently add the set of standard passes to a loop pass's \c
/// AnalysisUsage.
///
// Check inner loop backedge count is consistent on all iterations of the
// outer loop
- auto CheckInnerLoopIterationCountInvariant = [](Loop *SubLoop, Loop *OuterL,
- ScalarEvolution &SE) {
- BasicBlock *SubLoopLatch = SubLoop->getLoopLatch();
- const SCEV *SubLoopBECountSC = SE.getExitCount(SubLoop, SubLoopLatch);
- if (isa<SCEVCouldNotCompute>(SubLoopBECountSC) ||
- !SubLoopBECountSC->getType()->isIntegerTy())
- return false;
- ScalarEvolution::LoopDisposition LD =
- SE.getLoopDisposition(SubLoopBECountSC, OuterL);
- if (LD != ScalarEvolution::LoopInvariant)
- return false;
- return true;
- };
- if (!CheckInnerLoopIterationCountInvariant(SubLoop, L, SE)) {
+ if (!hasInvariantIterationCount(SubLoop, SE)) {
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Inner loop iteration count is "
"not consistent on each iteration\n");
return false;
return (FalseVal + (TrueVal / 2)) / TrueVal;
}
+bool llvm::hasInvariantIterationCount(Loop *InnerLoop,
+ ScalarEvolution &SE) {
+ Loop *OuterL = InnerLoop->getParentLoop();
+ if (!OuterL)
+ return true;
+
+ // Get the backedge taken count for the inner loop
+ BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
+ const SCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch);
+ if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) ||
+ !InnerLoopBECountSC->getType()->isIntegerTy())
+ return false;
+
+ // Get whether count is invariant to the outer loop
+ ScalarEvolution::LoopDisposition LD =
+ SE.getLoopDisposition(InnerLoopBECountSC, OuterL);
+ if (LD != ScalarEvolution::LoopInvariant)
+ return false;
+
+ return true;
+}
+
/// Adds a 'fast' flag to floating point operations.
static Value *addFastMathFlag(Value *V) {
if (isa<FPMathOperator>(V)) {