Skip to content

Commit 9cd4deb

Browse files
committed
[LoopVectorize] Preserve CFG analyses if CFG wasn't modified
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
1 parent db79974 commit 9cd4deb

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ struct LoopVectorizeOptions {
116116
}
117117
};
118118

119+
/// Storage for information about made changes.
120+
struct LoopVectorizeResult {
121+
bool MadeAnyChange;
122+
bool MadeCFGChange;
123+
124+
LoopVectorizeResult(bool MadeAnyChange, bool MadeCFGChange)
125+
: MadeAnyChange(MadeAnyChange), MadeCFGChange(MadeCFGChange) {}
126+
};
127+
119128
/// The LoopVectorize Pass.
120129
struct LoopVectorizePass : public PassInfoMixin<LoopVectorizePass> {
121130
private:
@@ -146,12 +155,13 @@ struct LoopVectorizePass : public PassInfoMixin<LoopVectorizePass> {
146155
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
147156

148157
// Shim for old PM.
149-
bool runImpl(Function &F, ScalarEvolution &SE_, LoopInfo &LI_,
150-
TargetTransformInfo &TTI_, DominatorTree &DT_,
151-
BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_,
152-
DemandedBits &DB_, AliasAnalysis &AA_, AssumptionCache &AC_,
153-
std::function<const LoopAccessInfo &(Loop &)> &GetLAA_,
154-
OptimizationRemarkEmitter &ORE_, ProfileSummaryInfo *PSI_);
158+
LoopVectorizeResult
159+
runImpl(Function &F, ScalarEvolution &SE_, LoopInfo &LI_,
160+
TargetTransformInfo &TTI_, DominatorTree &DT_,
161+
BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_, DemandedBits &DB_,
162+
AliasAnalysis &AA_, AssumptionCache &AC_,
163+
std::function<const LoopAccessInfo &(Loop &)> &GetLAA_,
164+
OptimizationRemarkEmitter &ORE_, ProfileSummaryInfo *PSI_);
155165

156166
bool processLoop(Loop *L);
157167
};

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,7 @@ struct LoopVectorize : public FunctionPass {
16321632
[&](Loop &L) -> const LoopAccessInfo & { return LAA->getInfo(&L); };
16331633

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

16381638
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -7954,7 +7954,7 @@ bool LoopVectorizePass::processLoop(Loop *L) {
79547954
return true;
79557955
}
79567956

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

7987-
bool Changed = false;
7987+
bool Changed = false, CFGChanged = false;
79887988

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

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

8016-
Changed |= processLoop(L);
8016+
Changed |= CFGChanged |= processLoop(L);
80178017
}
80188018

80198019
// Process each loop nest in the function.
8020-
return Changed;
8020+
return LoopVectorizeResult(Changed, CFGChanged);
80218021
}
80228022

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

@@ -8062,5 +8062,7 @@ PreservedAnalyses LoopVectorizePass::run(Function &F,
80628062
}
80638063
PA.preserve<BasicAA>();
80648064
PA.preserve<GlobalsAA>();
8065+
if (!Result.MadeCFGChange)
8066+
PA.preserveSet<CFGAnalyses>();
80658067
return PA;
80668068
}

llvm/test/Transforms/LoopVectorize/novect-lcssa-cfg-invalidation.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ define i32 @novect(i32* %p) {
1111
; CHECK: Invalidating all non-preserved analyses for: novect
1212
; CHECK: Clearing all analysis results for: <possibly invalidated loop>
1313
; CHECK: Invalidating analysis: ScalarEvolutionAnalysis on novect
14-
; CHECK: Invalidating analysis: BranchProbabilityAnalysis on novect
15-
; CHECK: Invalidating analysis: BlockFrequencyAnalysis on novect
14+
; CHECK-NOT: Invalidating analysis: BranchProbabilityAnalysis on novect
15+
; CHECK-NOT: Invalidating analysis: BlockFrequencyAnalysis on novect
1616
; CHECK: Invalidating analysis: DemandedBitsAnalysis on novect
1717
; CHECK: Invalidating analysis: MemorySSAAnalysis on novect
1818
; CHECK: Running pass: JumpThreadingPass on novect

0 commit comments

Comments
 (0)