Skip to content

Commit

Permalink
[LoopVectorize] Preserve CFG analyses if CFG wasn't modified
Browse files Browse the repository at this point in the history
One of transforms the loop vectorizer makes is LCSSA formation. In some cases it
is the only transform it makes. We should not drop CFG analyzes if only LCSSA was
formed and no actual CFG changes was made.

We should think of expanding this logic to other passes as well, and maybe make
it a part of PM framework.

Reviewed By: Florian Hahn
Differential Revision: https://reviews.llvm.org/D78360
  • Loading branch information
xortator committed Apr 24, 2020
1 parent db79974 commit 9cd4deb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
22 changes: 16 additions & 6 deletions llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h
Expand Up @@ -116,6 +116,15 @@ struct LoopVectorizeOptions {
}
};

/// Storage for information about made changes.
struct LoopVectorizeResult {
bool MadeAnyChange;
bool MadeCFGChange;

LoopVectorizeResult(bool MadeAnyChange, bool MadeCFGChange)
: MadeAnyChange(MadeAnyChange), MadeCFGChange(MadeCFGChange) {}
};

/// The LoopVectorize Pass.
struct LoopVectorizePass : public PassInfoMixin<LoopVectorizePass> {
private:
Expand Down Expand Up @@ -146,12 +155,13 @@ struct LoopVectorizePass : public PassInfoMixin<LoopVectorizePass> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);

// Shim for old PM.
bool runImpl(Function &F, ScalarEvolution &SE_, LoopInfo &LI_,
TargetTransformInfo &TTI_, DominatorTree &DT_,
BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_,
DemandedBits &DB_, AliasAnalysis &AA_, AssumptionCache &AC_,
std::function<const LoopAccessInfo &(Loop &)> &GetLAA_,
OptimizationRemarkEmitter &ORE_, ProfileSummaryInfo *PSI_);
LoopVectorizeResult
runImpl(Function &F, ScalarEvolution &SE_, LoopInfo &LI_,
TargetTransformInfo &TTI_, DominatorTree &DT_,
BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_, DemandedBits &DB_,
AliasAnalysis &AA_, AssumptionCache &AC_,
std::function<const LoopAccessInfo &(Loop &)> &GetLAA_,
OptimizationRemarkEmitter &ORE_, ProfileSummaryInfo *PSI_);

bool processLoop(Loop *L);
};
Expand Down
20 changes: 11 additions & 9 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Expand Up @@ -1632,7 +1632,7 @@ struct LoopVectorize : public FunctionPass {
[&](Loop &L) -> const LoopAccessInfo & { return LAA->getInfo(&L); };

return Impl.runImpl(F, *SE, *LI, *TTI, *DT, *BFI, TLI, *DB, *AA, *AC,
GetLAA, *ORE, PSI);
GetLAA, *ORE, PSI).MadeAnyChange;
}

void getAnalysisUsage(AnalysisUsage &AU) const override {
Expand Down Expand Up @@ -7954,7 +7954,7 @@ bool LoopVectorizePass::processLoop(Loop *L) {
return true;
}

bool LoopVectorizePass::runImpl(
LoopVectorizeResult LoopVectorizePass::runImpl(
Function &F, ScalarEvolution &SE_, LoopInfo &LI_, TargetTransformInfo &TTI_,
DominatorTree &DT_, BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_,
DemandedBits &DB_, AliasAnalysis &AA_, AssumptionCache &AC_,
Expand Down Expand Up @@ -7982,17 +7982,17 @@ bool LoopVectorizePass::runImpl(
// interleaving.
if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true)) &&
TTI->getMaxInterleaveFactor(1) < 2)
return false;
return LoopVectorizeResult(false, false);

bool Changed = false;
bool Changed = false, CFGChanged = false;

// The vectorizer requires loops to be in simplified form.
// Since simplification may add new inner loops, it has to run before the
// legality and profitability checks. This means running the loop vectorizer
// will simplify all loops, regardless of whether anything end up being
// vectorized.
for (auto &L : *LI)
Changed |=
Changed |= CFGChanged |=
simplifyLoop(L, DT, LI, SE, AC, nullptr, false /* PreserveLCSSA */);

// Build up a worklist of inner-loops to vectorize. This is necessary as
Expand All @@ -8013,11 +8013,11 @@ bool LoopVectorizePass::runImpl(
// transform.
Changed |= formLCSSARecursively(*L, *DT, LI, SE);

Changed |= processLoop(L);
Changed |= CFGChanged |= processLoop(L);
}

// Process each loop nest in the function.
return Changed;
return LoopVectorizeResult(Changed, CFGChanged);
}

PreservedAnalyses LoopVectorizePass::run(Function &F,
Expand Down Expand Up @@ -8046,9 +8046,9 @@ PreservedAnalyses LoopVectorizePass::run(Function &F,
AM.getResult<ModuleAnalysisManagerFunctionProxy>(F).getManager();
ProfileSummaryInfo *PSI =
MAM.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
bool Changed =
LoopVectorizeResult Result =
runImpl(F, SE, LI, TTI, DT, BFI, &TLI, DB, AA, AC, GetLAA, ORE, PSI);
if (!Changed)
if (!Result.MadeAnyChange)
return PreservedAnalyses::all();
PreservedAnalyses PA;

Expand All @@ -8062,5 +8062,7 @@ PreservedAnalyses LoopVectorizePass::run(Function &F,
}
PA.preserve<BasicAA>();
PA.preserve<GlobalsAA>();
if (!Result.MadeCFGChange)
PA.preserveSet<CFGAnalyses>();
return PA;
}
Expand Up @@ -11,8 +11,8 @@ define i32 @novect(i32* %p) {
; CHECK: Invalidating all non-preserved analyses for: novect
; CHECK: Clearing all analysis results for: <possibly invalidated loop>
; CHECK: Invalidating analysis: ScalarEvolutionAnalysis on novect
; CHECK: Invalidating analysis: BranchProbabilityAnalysis on novect
; CHECK: Invalidating analysis: BlockFrequencyAnalysis on novect
; CHECK-NOT: Invalidating analysis: BranchProbabilityAnalysis on novect
; CHECK-NOT: Invalidating analysis: BlockFrequencyAnalysis on novect
; CHECK: Invalidating analysis: DemandedBitsAnalysis on novect
; CHECK: Invalidating analysis: MemorySSAAnalysis on novect
; CHECK: Running pass: JumpThreadingPass on novect
Expand Down

0 comments on commit 9cd4deb

Please sign in to comment.